v2.0 first push
This commit is contained in:
372
config.php
372
config.php
@@ -98,337 +98,6 @@ class PhpObject
|
||||
}
|
||||
}
|
||||
|
||||
class MySqlManager extends PhpObject
|
||||
{
|
||||
const DB_SERVER = Settings::DB_SERVER;
|
||||
const DB_LOGIN = Settings::DB_LOGIN;
|
||||
const DB_PASS = Settings::DB_PASS;
|
||||
const DB_NAME = Settings::DB_NAME;
|
||||
|
||||
const ID_TAG = 'id_';
|
||||
const USERS_TABLE = 'users';
|
||||
const THOUGHTS_TABLE = 'thoughts';
|
||||
const SETTINGS_TABLE = 'settings';
|
||||
|
||||
private $oConnection;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->oConnection = mysql_connect(self::DB_SERVER, self::DB_LOGIN, self::DB_PASS);
|
||||
if(!$this->oConnection)
|
||||
{
|
||||
$this->addError('bug connection');
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!mysql_select_db(self::DB_NAME, $this->oConnection))
|
||||
{
|
||||
$this->install();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
mysql_close($this->oConnection);
|
||||
}
|
||||
|
||||
private function install()
|
||||
{
|
||||
$this->setQuery("DROP DATABASE IF EXISTS ".self::DB_NAME);
|
||||
$this->setQuery("CREATE /* ".basename(__FILE__)." ".__LINE__." */ DATABASE ".self::DB_NAME);
|
||||
mysql_select_db(self::DB_NAME, $this->oConnection);
|
||||
@array_walk(array_map(array($this, 'getInstallQuery'), $this->getTables()), array($this, 'setQuery'));
|
||||
}
|
||||
|
||||
private function getInstallQuery($sTableName)
|
||||
{
|
||||
$asTableColumns = $this->getTableColumns($sTableName);
|
||||
$sQuery = "\n".implodeAll($asTableColumns, "` ", "\n", "`", ",")."\n".implode(", \n", $this->getTableConstraints($sTableName));
|
||||
return "CREATE /* ".basename(__FILE__)." ".__LINE__." */ TABLE `{$sTableName}` ({$sQuery})";
|
||||
}
|
||||
|
||||
private function setQuery($sQuery, $sTypeQuery=__FUNCTION__)
|
||||
{
|
||||
return $this->getQuery($sQuery, $sTypeQuery);
|
||||
}
|
||||
|
||||
private function getQuery($sQuery, $sTypeQuery=__FUNCTION__)
|
||||
{
|
||||
$oResult = mysql_query($sQuery, $this->oConnection);
|
||||
if(!$oResult)
|
||||
{
|
||||
$this->addError("\nErreur SQL : \n".$sQuery."\n".mysql_error());
|
||||
}
|
||||
return $oResult;
|
||||
}
|
||||
|
||||
public function getArrayQuery($sQuery, $bStringOnly=false, $sTypeQuery=__FUNCTION__)
|
||||
{
|
||||
$asResult = array();
|
||||
$oResult = $this->getQuery($sQuery, true, $sTypeQuery);
|
||||
if($oResult!==false)
|
||||
{
|
||||
while($asCurrentRow = mysql_fetch_array($oResult))
|
||||
{
|
||||
if($bStringOnly)
|
||||
{
|
||||
$asCurrentRow = $this->arrayKeyFilter($asCurrentRow, 'is_string');
|
||||
}
|
||||
|
||||
//One column case : collapse a level
|
||||
if(count($asCurrentRow)==1)
|
||||
{
|
||||
$asResult[] = array_shift($asCurrentRow);
|
||||
}
|
||||
else
|
||||
{
|
||||
$asResult[] = $asCurrentRow;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $asResult;
|
||||
}
|
||||
|
||||
private function arrayKeyFilter($asArray, $sCallBack)
|
||||
{
|
||||
$asValidKeys = array_flip(array_filter(array_keys($asArray), $sCallBack));
|
||||
return array_intersect_key($asArray, $asValidKeys);
|
||||
}
|
||||
|
||||
public static function getTables()
|
||||
{
|
||||
return array(self::USERS_TABLE, self::THOUGHTS_TABLE, self::SETTINGS_TABLE);
|
||||
}
|
||||
|
||||
public static function getId($sTableName)
|
||||
{
|
||||
return self::ID_TAG.substr($sTableName, 0, -1);
|
||||
}
|
||||
|
||||
public static function getText($sTableName)
|
||||
{
|
||||
return substr($sTableName, 0, -1);
|
||||
}
|
||||
|
||||
private static function isId($sColumnName, $sTableName='')
|
||||
{
|
||||
$asTables = ($sTableName=='')?self::getTables():array($sTableName);
|
||||
$asTableIds = array_map(array('self', 'getId'), $asTables);
|
||||
return in_array($sColumnName, $asTableIds);
|
||||
}
|
||||
|
||||
public static function getTablecolumns($sTableName)
|
||||
{
|
||||
$asTableColumns = array(self::getId($sTableName));
|
||||
|
||||
switch($sTableName)
|
||||
{
|
||||
case self::USERS_TABLE:
|
||||
$asTableColumns[] = 'user';
|
||||
$asTableColumns[] = 'pass';
|
||||
break;
|
||||
case self::THOUGHTS_TABLE:
|
||||
$asTableColumns[] = self::getId(self::USERS_TABLE);
|
||||
$asTableColumns[] = 'thought';
|
||||
break;
|
||||
case self::SETTINGS_TABLE:
|
||||
$asTableColumns[] = self::getId(self::USERS_TABLE);
|
||||
$asTableColumns[] = 'setting';
|
||||
$asTableColumns[] = 'value';
|
||||
break;
|
||||
default:
|
||||
$this->addError('Function '.__FUNCTION__.', table '.$sTableName.' not found');
|
||||
}
|
||||
$asTableColumns[] = 'led';
|
||||
$asTableName = array_fill(0, count($asTableColumns), $sTableName);
|
||||
return array_combine($asTableColumns, array_map(array('self', 'getColumnType'), $asTableColumns, $asTableName));
|
||||
}
|
||||
|
||||
private static function getColumnType($sColumnName, $sTableName)
|
||||
{
|
||||
$sColumnType = '';
|
||||
switch($sColumnName)
|
||||
{
|
||||
case 'user':
|
||||
$sColumnType = "varchar(20) NOT NULL";
|
||||
break;
|
||||
case 'pass':
|
||||
$sColumnType = "varchar(128) NOT NULL";
|
||||
break;
|
||||
case 'thought':
|
||||
$sColumnType = "longtext NOT NULL";
|
||||
break;
|
||||
case 'setting':
|
||||
$sColumnType = "varchar(20) NOT NULL";
|
||||
break;
|
||||
case 'value':
|
||||
$sColumnType = 'varchar(20) NOT NULL';
|
||||
break;
|
||||
case 'led':
|
||||
$sColumnType = "TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP";
|
||||
break;
|
||||
case self::isId($sColumnName, $sTableName):
|
||||
$sColumnType = "int(10) UNSIGNED auto_increment";
|
||||
break;
|
||||
case self::isId($sColumnName):
|
||||
$sColumnType = "int(10) UNSIGNED NOT NULL";
|
||||
break;
|
||||
}
|
||||
return $sColumnType;
|
||||
}
|
||||
|
||||
private static function getTableConstraints($sTableName)
|
||||
{
|
||||
//primary key
|
||||
$asTableConstraints = array('PRIMARY' => "PRIMARY KEY (`".self::getId($sTableName)."`)");
|
||||
|
||||
//other constraints
|
||||
switch($sTableName)
|
||||
{
|
||||
case 'user' :
|
||||
break;
|
||||
case 'thought' :
|
||||
break;
|
||||
}
|
||||
return $asTableConstraints;
|
||||
}
|
||||
|
||||
private function addQuotes($oData)
|
||||
{
|
||||
return array_map_encapsulate($oData, "'");
|
||||
}
|
||||
|
||||
private function getLastId()
|
||||
{
|
||||
return mysql_insert_id();
|
||||
}
|
||||
|
||||
public function insertRow($sTableName, $asData)
|
||||
{
|
||||
$this->cleanSql($sTableName);
|
||||
$this->cleanSql($asData);
|
||||
|
||||
$asQueryValues = $this->addQuotes($asData);
|
||||
$sQuery = "INSERT /* ".basename(__FILE__)." ".__LINE__." */
|
||||
INTO ".$sTableName." (`".implode("`, `", array_keys($asQueryValues))."`)
|
||||
VALUES (".implode(", ", $asQueryValues).")";
|
||||
|
||||
return $this->setQuery($sQuery)?$this->getLastId():false;
|
||||
}
|
||||
|
||||
public function updateRow($sTableName, $asConstraints, $asData)
|
||||
{
|
||||
if(!is_array($asConstraints))
|
||||
{
|
||||
$asConstraints = array($this->getId($sTableName)=>$asConstraints);
|
||||
}
|
||||
$iTableId =
|
||||
|
||||
$this->cleanSql($sTableName);
|
||||
$this->cleanSql($iTableId);
|
||||
$this->cleanSql($asData);
|
||||
$this->cleanSql($asConstraints);
|
||||
$asQueryValues = $this->addQuotes($asData);
|
||||
$asConstraintsValues = $this->addQuotes($asConstraints);
|
||||
|
||||
$sQuery = "UPDATE /* ".basename(__FILE__)." ".__LINE__." */ $sTableName
|
||||
SET ".implodeAll($asQueryValues, " = ", ", ")."
|
||||
WHERE ".implodeAll($asConstraintsValues, " = ", " AND ")." LIMIT 1";
|
||||
return $this->setQuery($sQuery)?$this->selectValue($sTableName, $this->getId($sTableName), $asConstraints):false;
|
||||
}
|
||||
|
||||
public function insertUpdateRow($sTableName, $asConstraints, $asData)
|
||||
{
|
||||
$iTableId = $this->selectValue($sTableName, $this->getId($sTableName), $asConstraints);
|
||||
if(!$iTableId)
|
||||
{
|
||||
$asData = array_merge($asConstraints, $asData);
|
||||
return $this->insertRow($sTableName, $asData);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->updateRow($sTableName, $asConstraints, $asData);
|
||||
}
|
||||
}
|
||||
|
||||
public function selectRow($sTableName, $asConstraints=array(), $sColumnName='*', $bStringOnly=false)
|
||||
{
|
||||
$asResult = $this->selectRows(array('select'=>$sColumnName, 'from'=>$sTableName, 'constraint'=>$asConstraints));
|
||||
$iCountNb = count($asResult);
|
||||
switch($iCountNb)
|
||||
{
|
||||
case 0 :
|
||||
return false;
|
||||
case $iCountNb > 1 :
|
||||
$this->addError('Trop de résultats pour un selectRow() : '.$iCountNb);
|
||||
break;
|
||||
}
|
||||
return array_shift($asResult);
|
||||
}
|
||||
function selectValue($sTableName, $sColumnName, $asConstraints)
|
||||
{
|
||||
if(is_numeric($asConstraints))
|
||||
{
|
||||
$asConstraints = array(self::getId($sTableName)=>$asConstraints);
|
||||
}
|
||||
return $this->selectRow($sTableName, $asConstraints, $sColumnName);
|
||||
}
|
||||
|
||||
public function selectRows($asInfo, $bStringOnly=true)
|
||||
{
|
||||
$sAttributes = array('select'=>"SELECT", 'from'=>"FROM", 'constraint'=>"WHERE", 'groupBy'=>"GROUP BY", 'orderBy'=>"ORDER BY");
|
||||
$asRowSeparators = array('select'=>", ", 'from'=>"", 'constraint'=>" AND ", 'groupBy'=>", ", 'orderBy'=>", ");
|
||||
$asOperators = array('constraint'=>" = ", 'orderBy'=>" ");
|
||||
|
||||
$sQuery = "/* ".basename(__FILE__)." ".__LINE__." */";
|
||||
foreach($sAttributes as $sStatement => $sKeyWord)
|
||||
{
|
||||
$asSelection = array_key_exists($sStatement, $asInfo)?$asInfo[$sStatement]:array();
|
||||
if(!is_array($asSelection))
|
||||
{
|
||||
$asSelection = array($asSelection);
|
||||
}
|
||||
|
||||
//if provided values
|
||||
if(!empty($asSelection))
|
||||
{
|
||||
$this->cleanSql($asSelection);
|
||||
|
||||
if($sStatement=='constraint')
|
||||
{
|
||||
$asSelection = $this->addQuotes($asSelection);
|
||||
}
|
||||
|
||||
$sQuery .= " ".$sKeyWord." ";
|
||||
|
||||
//in case of double value input
|
||||
if(array_key_exists($sStatement, $asOperators))
|
||||
{
|
||||
$sQuery .= implodeAll($asSelection, $asOperators[$sStatement], $asRowSeparators[$sStatement]);
|
||||
}
|
||||
else
|
||||
{
|
||||
$sQuery .= implode($asRowSeparators[$sStatement], $asSelection);
|
||||
}
|
||||
}
|
||||
//default value for select
|
||||
elseif($sStatement=='select')
|
||||
{
|
||||
$sQuery .= " ".$sKeyWord." * ";
|
||||
}
|
||||
}
|
||||
|
||||
return $this->getArrayQuery($sQuery, $bStringOnly);
|
||||
}
|
||||
|
||||
private function cleanSql(&$oData)
|
||||
{
|
||||
cleanData($oData, 'mysql_real_escape_string');
|
||||
}
|
||||
}
|
||||
|
||||
class Session extends PhpObject
|
||||
{
|
||||
private $iUserId;
|
||||
@@ -874,21 +543,8 @@ class MyThoughts extends PhpObject
|
||||
$this->asSettings = array();
|
||||
}
|
||||
|
||||
public function register($sLogin, $sPass)
|
||||
{
|
||||
$asData = array('user'=>$sLogin, 'pass'=>$sPass);
|
||||
$bRegistered = $this->oSession->register($asData);
|
||||
if($bRegistered)
|
||||
{
|
||||
$this->addThought(file_get_contents(self::WELCOME_MSG_FILE));
|
||||
}
|
||||
return $bRegistered;
|
||||
}
|
||||
|
||||
public function logMeIn($sLogin, $sPass)
|
||||
{
|
||||
return $this->oSession->logMeIn($sLogin, $sPass);
|
||||
}
|
||||
|
||||
|
||||
public function logMeOut()
|
||||
{
|
||||
@@ -928,12 +584,7 @@ class MyThoughts extends PhpObject
|
||||
die();
|
||||
}
|
||||
|
||||
public function addThought($sThought)
|
||||
{
|
||||
$asThought = array('thought'=>$this->encodeThought($sThought));
|
||||
$asThought[MySqlManager::getId(MySqlManager::USERS_TABLE)] = $this->oSession->getUserId();
|
||||
return $this->oMySql->insertRow(MySqlManager::THOUGHTS_TABLE, $asThought);
|
||||
}
|
||||
|
||||
|
||||
public function updateThought($iThoughtId, $sThought)
|
||||
{
|
||||
@@ -943,15 +594,7 @@ class MyThoughts extends PhpObject
|
||||
$this->oMySql->updateRow(MySqlManager::THOUGHTS_TABLE, $asConstraints, $asValues);
|
||||
}
|
||||
|
||||
private function encodeThought($sthought)
|
||||
{
|
||||
return base64_encode(serialize(explode("\n", $this->shuffleText($sthought))));
|
||||
}
|
||||
|
||||
private function decodeThought($sEncodedThought)
|
||||
{
|
||||
return $this->shuffleText(implode("\n", unserialize(base64_decode($sEncodedThought))));
|
||||
}
|
||||
|
||||
|
||||
private function shuffleText($sText)
|
||||
{
|
||||
@@ -1139,15 +782,6 @@ class MyThoughts extends PhpObject
|
||||
return $oErrorMask->getMask();
|
||||
}
|
||||
|
||||
public function getPage()
|
||||
{
|
||||
$this->oMenuMask->setTag('calendar', $this->oCalendar->getCalendar());
|
||||
$this->oMainMask->setTag('menu', $this->oMenuMask->getMask());
|
||||
$this->oMainMask->setTag('content', $this->oPageMask->getMask());
|
||||
$this->oMainMask->setTag('errors', $this->collectErrors());
|
||||
$this->oMainMask->setTag('post_token', $this->oSession->getNewPostToken());
|
||||
return $this->oMainMask->getMask();
|
||||
}
|
||||
}
|
||||
|
||||
class Calendar extends PhpObject
|
||||
|
||||
Reference in New Issue
Block a user