Working on web admin

This commit is contained in:
Jérôme Schneider 2012-05-04 22:39:09 +01:00
parent 13645a7ab5
commit 09e58888bb
19 changed files with 319 additions and 154 deletions

View file

@ -53,8 +53,8 @@ class Tools {
ini_set("error_reporting", E_ALL);
}
function handleException($exception) {
echo $exception;
public static function handleException($exception) {
echo "<pre>" . $exception . "<pre>";
}
public static function assertBaikalIsOk() {

View file

@ -30,8 +30,10 @@ class Framework extends \Flake\Core\Framework {
public static function installTool() {
if(defined("BAIKAL_CONTEXT_INSTALL") && BAIKAL_CONTEXT_INSTALL === TRUE) {
# Install tool has been launched and we're already on the install page
return;
} else {
# Install tool has been launched; redirecting user
$sInstallToolUrl = PROJECT_URI . "admin/install/";
header("Location: " . $sInstallToolUrl);
exit(0);
@ -79,9 +81,7 @@ class Framework extends \Flake\Core\Framework {
\Baikal\Core\Tools::assertBaikalIsOk();
set_error_handler("\Baikal\Framework::exception_error_handler");
unset($bShouldCheckEnv);
# SabreDAV Autoloader
require_once(BAIKAL_PATH_SABREDAV . 'autoload.php');
}
@ -91,7 +91,7 @@ class Framework extends \Flake\Core\Framework {
}
# Mapping PHP errors to exceptions; needed by SabreDAV
function exception_error_handler($errno, $errstr, $errfile, $errline) {
public static function exception_error_handler($errno, $errstr, $errfile, $errline) {
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
}

View file

@ -32,13 +32,13 @@ class Standard extends \Baikal\Model\Config {
"BAIKAL_TIMEZONE" => array(
"type" => "string",
),
"BAIKAL_CARD_ENABLED" => array(
"BAIKAL_ADMIN_ENABLED" => array(
"type" => "boolean",
),
"BAIKAL_CAL_ENABLED" => array(
"type" => "boolean",
),
"BAIKAL_ADMIN_ENABLED" => array(
"BAIKAL_CARD_ENABLED" => array(
"type" => "boolean",
),
"BAIKAL_ADMIN_PASSWORDHASH" => array(
@ -69,8 +69,12 @@ class Standard extends \Baikal\Model\Config {
)));
$oMorpho->add(new \Formal\Element\Checkbox(array(
"prop" => "BAIKAL_CARD_ENABLED",
"label" => "Enable CardDAV"
"prop" => "BAIKAL_ADMIN_ENABLED",
"label" => "Enable Web Admin",
"popover" => array(
"title" => "Warning !",
"content" => "If disabled, you'll lose access to this very admin interface !",
),
)));
$oMorpho->add(new \Formal\Element\Checkbox(array(
@ -79,12 +83,8 @@ class Standard extends \Baikal\Model\Config {
)));
$oMorpho->add(new \Formal\Element\Checkbox(array(
"prop" => "BAIKAL_ADMIN_ENABLED",
"label" => "Enable Web Admin",
"popover" => array(
"title" => "Warning !",
"content" => "If disabled, you'll lose access to this very admin interface !",
),
"prop" => "BAIKAL_CARD_ENABLED",
"label" => "Enable CardDAV"
)));
$oMorpho->add(new \Formal\Element\Password(array(

View file

@ -32,17 +32,47 @@ class Dashboard extends \Flake\Core\Controller {
}
public function render() {
$sLinkUsers = \BaikalAdmin\Controller\Users::link();
$oView = new \BaikalAdmin\View\Dashboard();
$oView->setData("BAIKAL_VERSION", BAIKAL_VERSION);
$sMessage =<<<MESSAGE
Ici, infos sur:<br />
* Nombre d'utilisateurs<br />
* Nombre de calendriers<br />
* Nombre d'évènements<br />
* Version + Mise à jour de baikal<br />
* <a href='{$sLinkUsers}'>Lien vers liste des utilisateurs</a><br />
* Lien vers paramétrage<br />
MESSAGE;
return $sMessage;
# Services status
$oView->setData("BAIKAL_CAL_ENABLED", BAIKAL_CAL_ENABLED);
$oView->setData("BAIKAL_CARD_ENABLED", BAIKAL_CARD_ENABLED);
# Statistics: Users
$iNbUsers = \Baikal\Model\User::getBaseRequester()->count();
$oView->setData("nbusers", $iNbUsers);
# Statistics: CalDAV
$iNbCalendars = \Baikal\Model\Calendar::getBaseRequester()->count();
$oView->setData("nbcalendars", $iNbCalendars);
$iNbEvents = \Baikal\Model\Calendar\Event::getBaseRequester()->count();
$oView->setData("nbevents", $iNbEvents);
if($iNbCalendars > 0) {
$fEventsPerCalendarAvg = $iNbEvents / $iNbCalendars;
} else {
$fEventsPerCalendarAvg = 0;
}
$oView->setData("eventspercalendaravg", $fEventsPerCalendarAvg);
# Statistics: CardDAV
$iNbBooks = \Baikal\Model\AddressBook::getBaseRequester()->count();
$oView->setData("nbbooks", $iNbBooks);
$iNbContacts = \Baikal\Model\AddressBook\Contact::getBaseRequester()->count();
$oView->setData("nbcontacts", $iNbEvents);
if($iNbBooks > 0) {
$fContactsPerBookAvg = $iNbContacts / $iNbBooks;
} else {
$fContactsPerBookAvg = 0;
}
$oView->setData("contactsperbookavg", $fContactsPerBookAvg);
return $oView->render();
}
}

View file

@ -79,7 +79,7 @@ PHP;
}
public function render() {
$sBigIcon = \BaikalAdmin\Model\Install::bigicon();
$sBigIcon = "glyph2x-magic";
$sBaikalVersion = BAIKAL_VERSION;
$sHtml = <<<HTML

View file

@ -36,7 +36,7 @@ class VersionUpgrade extends \Flake\Core\Controller {
}
public function render() {
$sBigIcon = \BaikalAdmin\Model\Install::bigicon();
$sBigIcon = "glyph2x-magic";
$sBaikalVersion = BAIKAL_VERSION;
$sBaikalConfiguredVersion = BAIKAL_CONFIGURED_VERSION;
@ -47,15 +47,6 @@ class VersionUpgrade extends \Flake\Core\Controller {
</header>
HTML;
/* $sHtml .= <<<HTML
<h2>What is this ?</h2>
<p>
This is the Baïkal Install Tool.<br />
It's displayed because you just installed or upgraded your Baïkal installation.<br />
<strong>Baïkal requires some maintenance in order to ensure everything works as expected.</strong>
</p>
HTML;
*/
return $sHtml;
}
}

View file

@ -24,48 +24,48 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace BaikalAdmin\Controller\Install;
namespace BaikalAdmin\Controller;
class AdminPassword extends \Flake\Core\Controller {
protected $aMessages = array();
protected $oModel; # \BaikalAdmin\Model\Install
protected $oForm; # \Formal\Form
/* public function __construct() {
parent::__construct();
$this->oModel = new \BaikalAdmin\Model\Install();
$this->oForm = $this->oModel->formForThisModelInstance(array(
"close" => FALSE
));
}
public function execute() {
if($this->oForm->submitted()) {
$this->oForm->execute();
}
}*/
class Login extends \Flake\Core\Controller {
public function execute() {
}
public function render() {
$sBigIcon = \BaikalAdmin\Model\Install::bigicon();
$sBaikalVersion = BAIKAL_VERSION;
$sActionUrl = \Flake\Util\Tools::getCurrentUrl();
$sSubmittedFlagName = "auth";
$sLogin = htmlspecialchars(\Flake\Util\Tools::POST("login"));
$sPassword = htmlspecialchars(\Flake\Util\Tools::POST("password"));
if(trim($sLogin) === "") {
$sLogin = "admin";
}
$sForm =<<<FORM
$sHtml = <<<HTML
<header class="jumbotron subhead" id="overview">
<h1><i class="{$sBigIcon}"></i>Baïkal maintainance wizard</h1>
<p class="lead">Maintaining Baïkal <strong>{$sBaikalVersion}</strong></p>
<h1><i class="glyph2x-lock"></i>Authentication</h1>
<p class="lead">Please authenticate to access Baïkal Web Admin.</p>
</header>
HTML;
$sHtml .= <<<HTML
<p>You have to set a password for the <strong>admin</strong> user.</p>
HTML;
<form class="form-horizontal" action="{$sActionUrl}" method="post" enctype="multipart/formdata">
<input type="hidden" name="{$sSubmittedFlagName}" value="1" />
<fieldset>
<p>
<label for="login">Login</label>
<input type="text" name="login" value="{$sLogin}" />
</p>
return $sHtml;
<p>
<label for="password">Password</label>
<input type="password" name="password" value="{$sPassword}" />
</p>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Authenticate</button>
</div>
</fieldset>
</form>
FORM;
return $sForm;
}
}

View file

@ -73,7 +73,7 @@ class Topbar extends \Flake\Core\Controller {
<a class="brand" href="{$sHomeLink}">Baïkal Web Admin</a>
<div class="nav-collapse">
<ul class="nav">
<li class="{$sActiveHome}"> <a href="{$sHomeLink}">Home</a></li>
<li class="{$sActiveHome}"> <a href="{$sHomeLink}">Dashboard</a></li>
<li class="{$sActiveUsers}"> <a href="{$sUsersLink}">Users and resources</a></li>
<li class="{$sActiveSettingsStandard}"> <a href="{$sSettingsStandardLink}">Settings</a></li>
<li class="{$sActiveSettingsSystem}"> <a href="{$sSettingsSystemLink}">System settings</a></li>

View file

@ -24,52 +24,24 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace BaikalAdmin\Model;
namespace BaikalAdmin\Controller\Navigation\Topbar;
class Install extends \Flake\Core\Model\NoDb {
protected $aData = array(
"test" => "",
);
public function persist() {
class Anonymous extends \Flake\Core\Controller {
public function execute() {
}
public function destroy() {
}
public function floating() {
return FALSE;
}
public function formMorphologyForThisModelInstance() {
$oMorpho = new \Formal\Form\Morphology();
$oMorpho->add(new \Formal\Element\Text(array(
"prop" => "test",
"label" => "Test"
)));
public function render() {
return $oMorpho;
}
public static function icon() {
return "icon-cog";
}
public static function mediumicon() {
return "glyph-magic";
}
public static function bigicon() {
return "glyph2x-magic";
}
public function label() {
return "Baïkal";
}
public static function humanName() {
return "Parameters";
$sHtml =<<<HTML
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand">Baïkal Web Admin</a>
</div>
</div>
</div>
HTML;
return $sHtml;
}
}

View file

@ -49,6 +49,12 @@ class Standard extends \Flake\Core\Controller {
}
public function render() {
return $this->oForm->render();
$sHeader =<<<FORM
<header class="jumbotron subhead" id="overview">
<h1><i class="glyph2x-adjust"></i>Baïkal settings</h1>
</header>
FORM;
return $sHeader . $this->oForm->render();
}
}

View file

@ -49,11 +49,18 @@ class System extends \Flake\Core\Controller {
}
public function render() {
$sHeader =<<<FORM
<header class="jumbotron subhead" id="overview">
<h1><i class="glyph2x-adjust"></i>Baïkal system settings</h1>
</header>
FORM;
$sMessage = \Formal\Core\Message::notice(
"Do not change anything on this page unless you really know what you are doing.<br />You might break Baïkal if you misconfigure something here.",
"Warning !",
FALSE
);
return $sMessage . $this->oForm->render();
return $sHeader . $sMessage . $this->oForm->render();
}
}

View file

@ -27,7 +27,7 @@
namespace BaikalAdmin\Core;
class Auth {
static function assertEnabled() {
public static function assertEnabled() {
if(!defined("BAIKAL_ADMIN_ENABLED") || BAIKAL_ADMIN_ENABLED !== TRUE) {
die("<h1>Ba&iuml;kal Admin is disabled.</h1>To enable it, set BAIKAL_ADMIN_ENABLED to TRUE in <b>Specific/config.php</b>");
}
@ -35,7 +35,7 @@ class Auth {
self::assertUnlocked();
}
static function assertUnlocked() {
public static function assertUnlocked() {
if(defined("BAIKAL_CONTEXT_INSTALL") && BAIKAL_CONTEXT_INSTALL === TRUE) {
$sToolName = "Ba&iuml;kal Install Tool";
@ -68,41 +68,30 @@ class Auth {
die("<h1>" . $sToolName . " is locked.</h1>To unlock it, create an empty file named ENABLE_ADMIN in <b>Specific/</b>");
}
}
static function assertAuthentified() {
if(!self::isAuthentified()) {
header(utf8_decode('WWW-Authenticate: Basic realm="Baïkal admin"'));
header('HTTP/1.0 401 Unauthorized');
die("Please authenticate.");
public static function isAuthenticated() {
if(isset($_SESSION["baikaladminauth"]) && $_SESSION["baikaladminauth"] === md5(BAIKAL_ADMIN_PASSWORDHASH)) {
return TRUE;
}
return TRUE;
}
static function isAuthentified() {
if(array_key_exists("PHP_AUTH_USER", $_SERVER)) {
$sUser = $_SERVER["PHP_AUTH_USER"];
} else {
$sUser = FALSE;
if(intval(\Flake\Util\Tools::POST("auth")) !== 1) {
return FALSE;
}
if(array_key_exists("PHP_AUTH_PW", $_SERVER)) {
$sPass = $_SERVER["PHP_AUTH_PW"];
} else {
$sPass = FALSE;
}
$sUser = \Flake\Util\Tools::POST("login");
$sPass = \Flake\Util\Tools::POST("password");
$sPassHash = self::hashAdminPassword($sPass);
if($sUser === "admin" && $sPassHash === BAIKAL_ADMIN_PASSWORDHASH) {
$_SESSION["baikaladminauth"] = md5(BAIKAL_ADMIN_PASSWORDHASH);
return TRUE;
}
return FALSE;
}
static function hashAdminPassword($sPassword) {
public static function hashAdminPassword($sPassword) {
return md5('admin:' . BAIKAL_AUTH_REALM . ':' . $sPassword);
}
}

View file

@ -0,0 +1,119 @@
<header class="jumbotron subhead" id="overview">
<h1><i class="glyph2x-circle-info"></i>Dashboard</h1>
</header>
<section id="about">
<div class="page-header">
<h1>About this system</h1>
</div>
<div class="row">
<div class="span3">
<h2>Version</h2>
<p>This systems runs Baïkal <span class="label label-info"><?= $BAIKAL_VERSION ?></span></p>
</div>
<div class="span3">
<h2>Services</h2>
<?
if($BAIKAL_CAL_ENABLED === TRUE) {
$sCalDAVClass = "label-success";
$sCalDAVText = "On";
} else {
$sCalDAVClass = "label-important";
$sCalDAVText = "Off";
}
if($BAIKAL_CARD_ENABLED === TRUE) {
$sCardDAVClass = "label-success";
$sCardDAVText = "On";
} else {
$sCardDAVClass = "label-important";
$sCardDAVText = "Off";
}
?>
<table class="table">
<tbody>
<tr>
<td>Web admin</td>
<td><span class="label label-success">On</span></p></td>
</tr>
<tr>
<td>CalDAV</td>
<td><span class="label <?= $sCalDAVClass ?>"><?= $sCalDAVText ?></span></td>
</tr>
<tr>
<td>CardDAV</td>
<td><span class="label <?= $sCardDAVClass ?>"><?= $sCardDAVText ?></span></td>
</tr>
</tbody>
</table>
</div>
<div class="span6">
<h2>License and credits</h2>
<?
$sUrl = "http://baikal.codr.fr/version/" . $BAIKAL_VERSION . "/about";
?>
<p>Baïkal is open source software, and released under the terms of the GNU GPL v3.</p>
<p>Baïkal is developped by Jérôme Schneider. You can contact him at <a href="mailto:mail@jeromeschneider.fr">mail@jeromeschneider.fr</a>.</p>
<p>Baïkal is based upon other open source projects. Read the README.md file to learn about that.</p>
<p><a href="<?= $sUrl ?>"><?= $sUrl ?></a></p>
</div>
</div>
</section>
<section id="statistics">
<div class="page-header">
<h1>Statistics</h1>
</div>
<div class="row">
<div class="span4">
<h2>Users</h2>
<table class="table">
<tbody>
<tr>
<td>Registered users</td>
<td><span class="badge"><?= $nbusers ?></span></td>
</tr>
</tbody>
</table>
</div>
<div class="span4">
<h2>CalDAV</h2>
<table class="table">
<tbody>
<tr>
<td>Number of calendars</td>
<td><span class="badge"><?= $nbcalendars ?></span></td>
</tr>
<tr>
<td>Number of events</td>
<td><span class="badge"><?= $nbevents ?></span></td>
</tr>
<tr>
<td>Events per calendar avg.</td>
<td><span class="badge"><?= $eventspercalendaravg ?></span></td>
</tr>
</tbody>
</table>
</div>
<div class="span4">
<h2>CardDAV</h2>
<table class="table">
<tbody>
<tr>
<td>Number of address books</td>
<td><span class="badge"><?= $nbbooks ?></span></td>
</tr>
<tr>
<td>Number of contacts</td>
<td><span class="badge"><?= $nbcontacts ?></span></td>
</tr>
<tr>
<td>Contacts per book avg.</td>
<td><span class="badge"><?= $contactsperbookavg ?></span></td>
</tr>
</tbody>
</table>
</div>
</div>
</section>

View file

@ -0,0 +1,31 @@
<?php
/***************************************************************
* Copyright notice
*
* (c) 2012 Jérôme Schneider <mail@jeromeschneider.fr>
* All rights reserved
*
* http://baikal.codr.fr
*
* This script is part of the Baïkal Server project. The Baïkal
* Server project is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace BaikalAdmin\View;
class Dashboard extends \BaikalAdmin\Core\View {
}

View file

@ -37,9 +37,8 @@ require_once(dirname(dirname(dirname(__FILE__))) . "/Flake/Core/Bootstrap.php");
# Bootstrap BaikalAdmin
\BaikalAdmin\Framework::bootstrap();
# Evaluate assertions
# Assert that BaikalAdmin is enabled
\BaikalAdmin\Core\Auth::assertEnabled();
\BaikalAdmin\Core\Auth::assertAuthentified();
# Create and setup a page object
$oPage = new \Flake\Controller\Page(BAIKALADMIN_PATH_TEMPLATES . "Page/index.html");
@ -47,10 +46,16 @@ $oPage->injectHTTPHeaders();
$oPage->setTitle("Baïkal Web Admin");
$oPage->setBaseUrl(PROJECT_URI);
$oPage->zone("navbar")->addBlock(new \BaikalAdmin\Controller\Navigation\Topbar());
# Authentication
if(\BaikalAdmin\Core\Auth::isAuthenticated() === FALSE) {
$oPage->zone("navbar")->addBlock(new \BaikalAdmin\Controller\Navigation\Topbar\Anonymous());
$oPage->zone("Payload")->addBlock(new \BaikalAdmin\Controller\Login());
} else {
$oPage->zone("navbar")->addBlock(new \BaikalAdmin\Controller\Navigation\Topbar());
# Route the request
$GLOBALS["ROUTER"]::route($oPage);
# Route the request
$GLOBALS["ROUTER"]::route($oPage);
}
# Render the page
echo $oPage->render();

View file

@ -54,8 +54,7 @@ if(!defined("BAIKAL_CONFIGURED_VERSION")) {
} elseif(!defined("BAIKAL_ADMIN_PASSWORDHASH")) {
# we have to set an admin password
$oPage->zone("Payload")->addBlock(new \BaikalAdmin\Controller\Install\AdminPassword());
$oPage->zone("Payload")->addBlock(new \BaikalAdmin\Controller\Install\Initialize());
} else {
# we have to initialize Baïkal (new installation)
$oPage->zone("Payload")->addBlock(new \BaikalAdmin\Controller\Install\VersionUpgrade());

View file

@ -45,4 +45,5 @@ abstract class Requester extends \Flake\Core\FLObject {
}
public abstract function execute();
public abstract function count();
}

View file

@ -170,7 +170,7 @@ class Sql extends \Flake\Core\FLObject {
return $this->bHasBeenExecuted;
}
public function getQuery() {
public function getQuery($sFields = "*") {
$sWhere = "1=1";
$sOrderBy = "";
$sLimit = "";
@ -194,7 +194,7 @@ class Sql extends \Flake\Core\FLObject {
}
return $GLOBALS["DB"]->SELECTquery(
"*",
$sFields,
$this->sDataTable,
$sWhere,
"",
@ -202,6 +202,10 @@ class Sql extends \Flake\Core\FLObject {
$sLimit
);
}
public function getCountQuery() {
return $this->getQuery("count(*) as nbitems");
}
public function execute() {
$oCollection = new \Flake\Core\CollectionTyped($this->sModelClass);
@ -218,4 +222,15 @@ class Sql extends \Flake\Core\FLObject {
return $oCollection;
}
public function count() {
$sSql = $this->getCountQuery();
$rSql = $GLOBALS["DB"]->query($sSql);
if(($aRs = $rSql->fetch()) !== FALSE) {
return intval($aRs["nbitems"]);
}
return 0;
}
}

Binary file not shown.