Working on Web admin

This commit is contained in:
Jérôme Schneider 2012-04-25 22:53:25 +02:00
parent 14ac8dbf01
commit 4cfe315740
17 changed files with 93 additions and 176 deletions

View file

@ -67,37 +67,26 @@ function installTool() {
}
}
if(!defined("BAIKAL_CONTEXT") || BAIKAL_CONTEXT !== TRUE) {
die("Bootstrap.php may not be included outside the Baikal context");
}
# Asserting PHP 5.3.0+
if(version_compare(PHP_VERSION, '5.3.0', '<')) {
die('Baikal Fatal Error: Baikal requires PHP 5.3.0+ to run properly. You version is: ' . PHP_VERSION . '.');
}
if(!defined('PDO::ATTR_DRIVER_NAME')) {
die('Baikal Fatal Error: PDO is unavailable. It\'s required by Baikal.');
}
if(!in_array('sqlite', PDO::getAvailableDrivers())) {
die('Baikal Fatal Error: PDO::sqlite is unavailable. It\'s required by Baikal.');
}
# Registering Baikal classloader
define("BAIKAL_PATH_FRAMEWORKROOT", dirname(dirname(__FILE__)) . "/");
require_once(BAIKAL_PATH_FRAMEWORKROOT . '/Core/ClassLoader.php');
\Baikal\Core\ClassLoader::register();
\Baikal\Core\Tools::assertEnvironmentIsOk();
# determine Baïkal install root path
# not using realpath here to avoid symlinks resolution
define("BAIKAL_PATH_ROOT", dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . "/"); # ../../../../../
define("BAIKAL_PATH_CORE", BAIKAL_PATH_ROOT . "Core/");
define("BAIKAL_PATH_SPECIFIC", BAIKAL_PATH_ROOT . "Specific/");
define("BAIKAL_PATH_FRAMEWORKS", BAIKAL_PATH_CORE . "Frameworks/");
define("BAIKAL_PATH_WWWROOT", BAIKAL_PATH_CORE . "WWWRoot/");
require_once(BAIKAL_PATH_CORE . "Distrib.php");
# Determine BAIKAL_URI
@ -110,6 +99,7 @@ define("BAIKAL_BASEURI", $sBaseUrl);
define("BAIKAL_URI", $sProtocol . "://" . rmEndSlash($_SERVER["HTTP_HOST"]) . $sBaseUrl);
unset($sScript); unset($sDirName); unset($sBaseUrl); unset($sProtocol); unset($aParts);
# Check that a config file exists
if(
!file_exists(BAIKAL_PATH_SPECIFIC . "config.php") ||
@ -138,18 +128,20 @@ if(
installTool();
}
\Baikal\Core\Tools::assertEnvironmentIsOk();
\Baikal\Core\Tools::assertBaikalIsOk();
# Bootstrap Flake
require_once(BAIKAL_PATH_FRAMEWORKS . "Flake/Core/Bootstrap.php");
# Database
$pdo = new PDO('sqlite:' . BAIKAL_SQLITE_FILE);
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
# Establishing connection with database
$GLOBALS["DB"] = new \Flake\Core\Database\Sqlite(BAIKAL_SQLITE_FILE);
$bShouldCheckEnv = ((!defined("BAIKAL_CONTEXT_CLI") || BAIKAL_CONTEXT_CLI === FALSE) && (!defined("BAIKAL_CONTEXT_ADMIN") || BAIKAL_CONTEXT_ADMIN === FALSE));
if($bShouldCheckEnv === TRUE) {
# Mapping PHP errors to exceptions
function exception_error_handler($errno, $errstr, $errfile, $errline) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
@ -159,7 +151,7 @@ if(
unset($bShouldCheckEnv);
// Autoloader
# SabreDAV Autoloader
require_once(BAIKAL_PATH_SABREDAV . 'autoload.php');
}
}

View file

@ -32,7 +32,24 @@ class Tools {
}
public static function assertEnvironmentIsOk() {
# Asserting Baikal Context
if(!defined("BAIKAL_CONTEXT") || BAIKAL_CONTEXT !== TRUE) {
die("Bootstrap.php may not be included outside the Baikal context");
}
# Asserting PDO
if(!defined('PDO::ATTR_DRIVER_NAME')) {
die('Baikal Fatal Error: PDO is unavailable. It\'s required by Baikal.');
}
# Asserting PDO::SQLite
if(!in_array('sqlite', \PDO::getAvailableDrivers())) {
die('Baikal Fatal Error: PDO::sqlite is unavailable. It\'s required by Baikal.');
}
}
public static function assertBaikalIsOk() {
# Asserting DB file exists
if(!file_exists(BAIKAL_SQLITE_FILE)) {
throw new \Exception("DB file does not exist. To create it, please copy 'Core/Resources/baikal.empty.sqlite' to 'Specific/db/baikal.sqlite'.");
@ -79,21 +96,6 @@ class Tools {
}
}
public static function getUsers() {
$aUsers = array();
# Fetching user
$stmt = \Baikal\Core\Tools::db()->prepare("SELECT * FROM users");
$stmt->execute();
while(($user = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_FIRST)) !== FALSE) {
$aUsers[] = $user;
}
reset($aUsers);
return $aUsers;
}
public static function bashPrompt($prompt) {
print $prompt;
@flush();

View file

@ -29,9 +29,6 @@ define("BAIKALADMIN_PATH_ROOT", dirname(dirname(__FILE__)) . "/");
# Bootstrap Baïkal Core
require_once(dirname(dirname(dirname(__FILE__))) . "/Baikal/Core/Bootstrap.php"); # ../../, symlink-safe
# Bootstrap Flake
require_once(dirname(dirname(dirname(__FILE__))) . "/Flake/Core/Bootstrap.php");
# Bootstrap Formal
require_once(dirname(dirname(dirname(__FILE__))) . "/Formal/Core/Bootstrap.php");

View file

@ -40,9 +40,9 @@ if(array_key_exists("SERVER_NAME", $_SERVER) && $_SERVER["SERVER_NAME"] === "mon
define("FLAKE_PATH_ROOT", dirname(dirname(__FILE__)) . "/"); # ../
// les notices PHP ne sont pas affichées
ini_set("display_errors", 1);
ini_set("error_reporting", E_ALL & ~E_NOTICE);
# Display errors messages, except notices
#ini_set("display_errors", 1);
#ini_set("error_reporting", E_ALL & ~E_NOTICE);
if(!function_exists("appendSlash")) {
function appendSlash($sPath) {
@ -77,9 +77,8 @@ if(!\Flake\Util\Tools::isCliPhp()) {
setlocale(LC_ALL, FLAKE_LOCALE);
date_default_timezone_set(FLAKE_TIMEZONE);
if(defined("FLAKE_DB_FILEPATH") && file_exists(FLAKE_DB_FILEPATH) && is_readable(FLAKE_DB_FILEPATH)) {
$GLOBALS["DB"] = new \Flake\Core\Database\Sqlite();
$GLOBALS["DB"]->init(FLAKE_DB_FILEPATH);
if(defined("FLAKE_DB_FILEPATH") && file_exists(FLAKE_DB_FILEPATH) && is_readable(FLAKE_DB_FILEPATH) && !isset($GLOBALS["DB"])) {
$GLOBALS["DB"] = new \Flake\Core\Database\Sqlite(FLAKE_DB_FILEPATH);
}
$GLOBALS["TEMPLATESTACK"] = array();

View file

@ -154,8 +154,8 @@ abstract class Database extends \Flake\Core\FLObject {
return $query;
}
function fullQuoteStr($str, $table) {
return '\''.$this->quoteStr($str, $table).'\'';
function fullQuote($str, $table) {
return '\''.$this->quote($str, $table).'\'';
}
function fullQuoteArray($arr, $table, $noQuote=FALSE) {
@ -167,7 +167,7 @@ abstract class Database extends \Flake\Core\FLObject {
foreach($arr as $k => $v) {
if ($noQuote===FALSE || !in_array($k,$noQuote)) {
$arr[$k] = $this->fullQuoteStr($v, $table);
$arr[$k] = $this->fullQuote($v, $table);
}
}
return $arr;
@ -176,10 +176,8 @@ abstract class Database extends \Flake\Core\FLObject {
/* fonctions abstraites */
abstract function query($sSql);
abstract function fetch($rSql);
abstract function sql_insert_id();
abstract function lastInsertId();
abstract function quoteStr($str, $table);
abstract function quote($str);
}

View file

@ -1,78 +0,0 @@
<?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 Flake\Core;
class Mysql extends \Flake\Core\Database {
var $link = FALSE; // current DB link
var $debugOutput = FALSE;
var $store_lastBuiltQuery = TRUE;
var $debug_lastBuiltQuery = "";
/* fonctions abstraites */
function init($sHost = DB_host, $sLogin = DB_login, $sPassword = DB_password, $sDatabase = DB_database) {
if(is_resource($this->link)) {
$this->messageAndDie("DB already initialized");
}
$this->link = mysql_connect(
$sHost,
$sLogin,
$sPassword
) OR $this->messageAndDie("invalid DB credentials.");
mysql_select_db($sDatabase, $this->link) OR $this->messageAndDie("could not select DB");
// on initialise la connection UTF-8 aux données
mysql_query("set character_set_database='utf8'", $this->link);
mysql_query("set character_set_client='utf8'", $this->link);
mysql_query("set character_set_connection='utf8'", $this->link);
mysql_query("set character_set_results='utf8'", $this->link);
mysql_query("set character_set_server='utf8'", $this->link);
}
function query($sSql) {
return mysql_query($sSql, $this->link);
}
function fetch($rSql) {
if(is_resource($rSql)) {
return mysql_fetch_assoc($rSql);
}
return FALSE;
}
function sql_insert_id() {
return mysql_insert_id($this->link);
}
function quoteStr($str, $table) {
return mysql_real_escape_string($str, $this->link);
}
}

View file

@ -28,47 +28,49 @@ namespace Flake\Core\Database;
class Sqlite extends \Flake\Core\Database {
var $oDb = FALSE; // current DB link
var $debugOutput = FALSE;
var $store_lastBuiltQuery = TRUE;
var $debug_lastBuiltQuery = "";
var $sDbPath = "";
protected $oDb = FALSE; // current DB link
protected $debugOutput = FALSE;
protected $store_lastBuiltQuery = TRUE;
protected $debug_lastBuiltQuery = "";
protected $sDbPath = "";
function init($sDbPath) {
if(is_object($this->oDb)) {
$this->messageAndDie("DB already initialized");
}
public function __construct($sDbPath) {
$this->sDbPath = $sDbPath;
$this->oDb = new \SQLite3($this->sDbPath);
$this->oDb = new \PDO('sqlite:' . $this->sDbPath);
# $this->oDb->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
function query($sSql) {
return $this->oDb->query($sSql);
public function query($sSql) {
$stmt = $this->oDb->query($sSql);
return new \Flake\Core\Database\SqliteStatement($stmt);
}
public function lastInsertId() {
return $this->oDb->lastInsertId();
}
function fetch($rSql) {
if(is_object($rSql)) {
return $rSql->fetchArray(SQLITE3_ASSOC);
public function quote($str) {
return substr($this->oDb->quote($str), 1, -1); # stripping first and last quote
}
public function getPDO() {
return $this->oDb;
}
}
Class SqliteStatement {
protected $stmt = null;
public function __construct($stmt) {
$this->stmt = $stmt;
}
public function fetch() {
if($this->stmt !== FALSE) {
return $this->stmt->fetch(\PDO::FETCH_ASSOC, \PDO::FETCH_ORI_FIRST);
}
return FALSE;
}
function sql_insert_id() {
$rSql = $this->query("SELECT last_insert_rowid() as uid");
if(($aRes = $this->fetch($rSql)) !== FALSE) {
return intval($aRes["uid"]);
}
return FALSE;
}
function quoteStr($str, $table=FALSE) {
if(function_exists("sqlite_escape_string")) {
return sqlite_escape_string($str);
}
return addslashes($str);
}
}

View file

@ -72,10 +72,10 @@ abstract class Db extends \Flake\Core\Model {
$rSql = $GLOBALS["DB"]->exec_SELECTquery(
"*",
self::getDataTable(),
self::getPrimaryKey() . "='" . $GLOBALS["DB"]->quoteStr($sPrimary) . "'"
self::getPrimaryKey() . "='" . $GLOBALS["DB"]->quote($sPrimary) . "'"
);
if(($aRs = $GLOBALS["DB"]->fetch($rSql)) === FALSE) {
if(($aRs = $rSql->fetch()) === FALSE) {
throw new \Exception("\Flake\Core\Model '" . htmlspecialchars($sPrimary) . "' not found for model " . get_class($this));
}
@ -90,13 +90,13 @@ abstract class Db extends \Flake\Core\Model {
$this->getData()
);
$sPrimary = $GLOBALS["DB"]->sql_insert_id();
$sPrimary = $GLOBALS["DB"]->lastInsertId();
$this->initByPrimary($sPrimary);
$this->bFloating = FALSE;
} else {
$GLOBALS["DB"]->exec_UPDATEquery(
self::getDataTable(),
self::getPrimaryKey() . "='" . $GLOBALS["DB"]->quoteStr($this->getPrimary()) . "'",
self::getPrimaryKey() . "='" . $GLOBALS["DB"]->quote($this->getPrimary()) . "'",
$this->getData()
);
}
@ -105,7 +105,7 @@ abstract class Db extends \Flake\Core\Model {
public function destroy() {
$GLOBALS["DB"]->exec_DELETEquery(
self::getDataTable(),
self::getPrimaryKey() . "='" . $GLOBALS["DB"]->quoteStr($this->getPrimary()) . "'"
self::getPrimaryKey() . "='" . $GLOBALS["DB"]->quote($this->getPrimary()) . "'"
);
}

View file

@ -154,7 +154,7 @@ class Sql extends \Flake\Core\FLObject {
}
protected function escapeSqlValue($sValue) {
return $GLOBALS["DB"]->quoteStr(
return $GLOBALS["DB"]->quote(
$sValue,
$this->sDataTable
);
@ -207,7 +207,7 @@ class Sql extends \Flake\Core\FLObject {
$sSql = $this->getQuery();
$rSql = $GLOBALS["DB"]->query($sSql);
while(($aRs = $GLOBALS["DB"]->fetch($rSql)) !== FALSE) {
while(($aRs = $rSql->fetch()) !== FALSE) {
$oCollection->push(
$this->reify($aRs)
);

View file

@ -109,7 +109,7 @@ class Tools extends \Flake\Core\FLObject {
array_pop($trail); // la ligne d'appel à debug
array_pop($trail); // la ligne d'appel à debug
$aLastNode = array_pop($trail); // l'appel qui nous intéresse
$brOrHeader = $aLastNode['class'].$aLastNode['type'].$aLastNode['function'];
$brOrHeader = @strval($aLastNode['class']).@strval($aLastNode['type']).@strval($aLastNode['function']);
}
if ($brOrHeader) {

View file

@ -31,6 +31,8 @@ define("BAIKAL_CONTEXT_CLI", TRUE);
# Bootstraping Baikal
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/Core/Frameworks/Baikal/Core/Bootstrap.php"); # ../../../
$pdo = $GLOBALS["DB"]->getPDO();
$sUsername = @trim($argv[1]);
if($sUsername === "") {

View file

@ -30,6 +30,7 @@ define("BAIKAL_CONTEXT_CLI", TRUE);
# Bootstraping Baikal
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/Core/Frameworks/Baikal/Core/Bootstrap.php"); # ../../../
$pdo = $GLOBALS["DB"]->getPDO();
$sUsername = @trim($argv[1]);

View file

@ -30,6 +30,7 @@ define("BAIKAL_CONTEXT_CLI", TRUE);
# Bootstraping Baikal
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/Core/Frameworks/Baikal/Core/Bootstrap.php"); # ../../../
$pdo = $GLOBALS["DB"]->getPDO();
$sUsername = @trim($argv[1]);

View file

@ -30,6 +30,7 @@ define("BAIKAL_CONTEXT_CLI", TRUE);
# Bootstraping Baikal
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . "/Core/Frameworks/Baikal/Core/Bootstrap.php"); # ../../../
$pdo = $GLOBALS["DB"]->getPDO();
$sUsername = isset($argv[1]) ? trim($argv[1]) : "";

View file

@ -35,9 +35,9 @@ if(!defined("BAIKAL_CAL_ENABLED") || BAIKAL_CAL_ENABLED !== TRUE) {
}
# Backends
$authBackend = new Sabre_DAV_Auth_Backend_PDO($pdo);
$principalBackend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$calendarBackend = new Sabre_CalDAV_Backend_PDO($pdo);
$authBackend = new Sabre_DAV_Auth_Backend_PDO($GLOBALS["DB"]->getPDO());
$principalBackend = new Sabre_DAVACL_PrincipalBackend_PDO($GLOBALS["DB"]->getPDO());
$calendarBackend = new Sabre_CalDAV_Backend_PDO($GLOBALS["DB"]->getPDO());
# Directory structure
$nodes = array(

View file

@ -35,9 +35,9 @@ if(!defined("BAIKAL_CARD_ENABLED") || BAIKAL_CARD_ENABLED !== TRUE) {
}
# Backends
$authBackend = new Sabre_DAV_Auth_Backend_PDO($pdo);
$principalBackend = new Sabre_DAVACL_PrincipalBackend_PDO($pdo);
$carddavBackend = new Sabre_CardDAV_Backend_PDO($pdo);
$authBackend = new Sabre_DAV_Auth_Backend_PDO($GLOBALS["DB"]->getPDO());
$principalBackend = new Sabre_DAVACL_PrincipalBackend_PDO($GLOBALS["DB"]->getPDO());
$carddavBackend = new Sabre_CardDAV_Backend_PDO($GLOBALS["DB"]->getPDO());
# Setting up the directory tree
$nodes = array(

Binary file not shown.