set up quill library
This commit is contained in:
32
inc/auth.php
32
inc/auth.php
@@ -9,15 +9,15 @@ class Auth extends PhpObject
|
||||
|
||||
/**
|
||||
* Database Connection
|
||||
* @var MySqlManager
|
||||
* @var Db
|
||||
*/
|
||||
private $oMySql;
|
||||
private $oDb;
|
||||
private $iUserId;
|
||||
private $sApiKey;
|
||||
|
||||
public function __construct($oMySql, $sApiKey='', $bAutoLogin=true)
|
||||
public function __construct($oDb, $sApiKey='', $bAutoLogin=true)
|
||||
{
|
||||
$this->oMySql = $oMySql;
|
||||
$this->oDb = $oDb;
|
||||
$this->setUserId(0);
|
||||
$this->sApiKey = $sApiKey;
|
||||
if($bAutoLogin) $this->autoLogIn();
|
||||
@@ -47,12 +47,12 @@ class Auth extends PhpObject
|
||||
$sPassToken = substr(strstr($sToken, self::TOKEN_SEP), strlen(self::TOKEN_SEP));
|
||||
if($sLoginToken!='' && $sPassToken!='')
|
||||
{
|
||||
$asEmpl = $this->oMySql->selectRow(MyThoughts::USER_TABLE, array("MD5(".MySqlManager::getText(MyThoughts::USER_TABLE).")"=>$sLoginToken));
|
||||
$asEmpl = $this->oDb->selectRow(MyThoughts::USER_TABLE, array("MD5(".Db::getText(MyThoughts::USER_TABLE).")"=>$sLoginToken));
|
||||
if(!empty($asEmpl))
|
||||
{
|
||||
if(self::CheckPassword($sPassToken, $asEmpl['pass']))
|
||||
{
|
||||
$this->setUserId($asEmpl[MySqlManager::getId(MyThoughts::USER_TABLE)]);
|
||||
$this->setUserId($asEmpl[Db::getId(MyThoughts::USER_TABLE)]);
|
||||
$this->resetAuthCookie($this->getUserId());
|
||||
}
|
||||
else $sDesc = 'wrong password';
|
||||
@@ -74,17 +74,17 @@ class Auth extends PhpObject
|
||||
$iUserId = addslashes(strstr($sCookie, self::TOKEN_SEP, true));
|
||||
$sCookie = substr(strstr($sCookie, self::TOKEN_SEP), strlen(self::TOKEN_SEP));
|
||||
|
||||
$asEmpl = $this->oMySql->selectRow(MyThoughts::USER_TABLE, array(MySqlManager::getId(MyThoughts::USER_TABLE)=>$iUserId));
|
||||
$asEmpl = $this->oDb->selectRow(MyThoughts::USER_TABLE, array(Db::getId(MyThoughts::USER_TABLE)=>$iUserId));
|
||||
if(!empty($asEmpl))
|
||||
{
|
||||
if($sCookie==$asEmpl['cookie'])
|
||||
{
|
||||
$this->setUserId($asEmpl[MySqlManager::getId(MyThoughts::USER_TABLE)]);
|
||||
$this->setUserId($asEmpl[Db::getId(MyThoughts::USER_TABLE)]);
|
||||
|
||||
//Reset pass once a day
|
||||
if(mb_substr($asEmpl['led'], 0, 10) != date('Y-m-d')) $this->resetAuthCookie($this->getUserId());
|
||||
}
|
||||
else $this->addError('token corrompu pour le user '.$asEmpl[MySqlManager::getId(MyThoughts::USER_TABLE)]);
|
||||
else $this->addError('token corrompu pour le user '.$asEmpl[Db::getId(MyThoughts::USER_TABLE)]);
|
||||
}
|
||||
else $this->addError('Utilisateur '.$iUserId.' inconnu');
|
||||
}
|
||||
@@ -93,11 +93,11 @@ class Auth extends PhpObject
|
||||
public function addUser($sSafeNickName, $sNickName, $bLogMeIn=false)
|
||||
{
|
||||
$sPass = self::HashPassword(self::getLoginToken($sSafeNickName));
|
||||
$bExist = $this->oMySql->pingValue(MyThoughts::USER_TABLE, array(MySqlManager::getText(MyThoughts::USER_TABLE)=>$sSafeNickName));
|
||||
$bExist = $this->oDb->pingValue(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$sSafeNickName));
|
||||
if($bExist) return -1;
|
||||
else
|
||||
{
|
||||
$iUserId = $this->oMySql->insertRow(MyThoughts::USER_TABLE, array(MySqlManager::getText(MyThoughts::USER_TABLE)=>$sSafeNickName, 'nickname'=>$sNickName));
|
||||
$iUserId = $this->oDb->insertRow(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$sSafeNickName, 'nickname'=>$sNickName, 'pass'=>$sPass));
|
||||
if($iUserId>0)
|
||||
{
|
||||
$this->resetPass($iUserId);
|
||||
@@ -115,17 +115,17 @@ class Auth extends PhpObject
|
||||
|
||||
private function resetPass($iUserId=0)
|
||||
{
|
||||
$sUserIdCol = MySqlManager::getId(MyThoughts::USER_TABLE);
|
||||
$sUserTextCol = MySqlManager::getText(MyThoughts::USER_TABLE);
|
||||
$sUserIdCol = Db::getId(MyThoughts::USER_TABLE);
|
||||
$sUserTextCol = Db::getText(MyThoughts::USER_TABLE);
|
||||
|
||||
$asInfo = array('select'=>array($sUserIdCol, $sUserTextCol), 'from'=>MyThoughts::USER_TABLE);
|
||||
if($iUserId>0) $asInfo['constraint'] = array($sUserIdCol=>$iUserId);
|
||||
|
||||
$asUsers = $this->oMySql->selectRows($asInfo);
|
||||
$asUsers = $this->oDb->selectRows($asInfo);
|
||||
foreach($asUsers as $asUser)
|
||||
{
|
||||
$sToken = self::HashPassword(self::getLoginToken($asUser[$sUserTextCol]));
|
||||
$this->oMySql->updateRow(MyThoughts::USER_TABLE, array(MySqlManager::getId(MyThoughts::USER_TABLE)=>$asUser[$sUserIdCol]), array('pass'=>$sToken));
|
||||
$this->oDb->updateRow(MyThoughts::USER_TABLE, array(Db::getId(MyThoughts::USER_TABLE)=>$asUser[$sUserIdCol]), array('pass'=>$sToken));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,7 +143,7 @@ class Auth extends PhpObject
|
||||
$sNewPass = self::getAuthCookie($iUserId);
|
||||
$iTimeLimit = time()+60*60*24*30;
|
||||
//mysqli_query($con, "UPDATE EMPLOYEE SET COOKIE = '".addslashes($sNewPass)."' WHERE ID = ".$iUserId);
|
||||
$this->oMySql->updateRow(MyThoughts::USER_TABLE, array(MySqlManager::getId(MyThoughts::USER_TABLE)=>$iUserId), array("cookie"=>$sNewPass));
|
||||
$this->oDb->updateRow(MyThoughts::USER_TABLE, array(Db::getId(MyThoughts::USER_TABLE)=>$iUserId), array("cookie"=>$sNewPass));
|
||||
setcookie(self::USER_COOKIE_PASS, $iUserId.self::TOKEN_SEP.$sNewPass, $iTimeLimit);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ class Calendar extends PhpObject
|
||||
{
|
||||
//TODO essayer avec selectRows
|
||||
$sQuery = "SELECT DATE_FORMAT(led, '%d') AS day
|
||||
FROM ".MySqlManager::THOUGHTS_TABLE."
|
||||
WHERE ".MySqlManager::getId(MySqlManager::USERS_TABLE)." = ".$this->oSession->getUserId()."
|
||||
FROM ".Db::THOUGHTS_TABLE."
|
||||
WHERE ".Db::getId(Db::USERS_TABLE)." = ".$this->oSession->getUserId()."
|
||||
AND YEAR(led) = ".$this->iYear."
|
||||
AND MONTH(led) = ".$this->iMonth."
|
||||
GROUP BY day
|
||||
|
||||
@@ -41,9 +41,9 @@ class MyThoughts extends PhpObject
|
||||
|
||||
/**
|
||||
* Database Connection
|
||||
* @var MySqlManager
|
||||
* @var Db
|
||||
*/
|
||||
private $oMySql;
|
||||
private $oDb;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -67,22 +67,22 @@ class MyThoughts extends PhpObject
|
||||
$this->setContext($sProcessPage);
|
||||
|
||||
//Load classes
|
||||
$this->oClassManagement->incClass('mysqlmanager');
|
||||
$this->oClassManagement->incClass('db');
|
||||
$this->oClassManagement->incClass('auth', true);
|
||||
//$this->oClassManagement->incClass('calendar', true);
|
||||
|
||||
//Init objects
|
||||
$this->oMySql = new MySqlManager(Settings::DB_SERVER, Settings::DB_LOGIN, Settings::DB_PASS, Settings::DB_NAME, self::getSqlOptions() , Settings::DB_ENC);
|
||||
if($this->oMySql->sDbState == MySqlManager::DB_NO_DATA) $this->install();
|
||||
else $this->oAuth = new Auth($this->oMySql, Settings::API_KEY);
|
||||
$this->oDb = new Db(Settings::DB_SERVER, Settings::DB_LOGIN, Settings::DB_PASS, Settings::DB_NAME, self::getSqlOptions() , Settings::DB_ENC);
|
||||
if($this->oDb->sDbState == Db::DB_NO_DATA) $this->install();
|
||||
else $this->oAuth = new Auth($this->oDb, Settings::API_KEY);
|
||||
}
|
||||
|
||||
private function install()
|
||||
{
|
||||
$this->oAuth = new Auth($this->oMySql, Settings::API_KEY, false);
|
||||
$this->oAuth = new Auth($this->oDb, Settings::API_KEY, false);
|
||||
|
||||
//Install DB
|
||||
$this->oMySql->install();
|
||||
$this->oDb->install();
|
||||
$this->addUser('franzz');
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ class MyThoughts extends PhpObject
|
||||
return file_get_contents($sPageFile);
|
||||
}
|
||||
|
||||
/* DB structure. See MySqlManager::__construct */
|
||||
/* DB structure. See Db::__construct */
|
||||
|
||||
private static function getSqlOptions()
|
||||
{
|
||||
@@ -164,26 +164,26 @@ class MyThoughts extends PhpObject
|
||||
(
|
||||
'tables' => array
|
||||
(
|
||||
self::USER_TABLE =>array(MySqlManager::getText(self::USER_TABLE), 'nickname', 'pass', 'cookie'),
|
||||
self::THOUGHT_TABLE =>array(MySqlManager::getId(self::USER_TABLE),
|
||||
MySqlManager::getText(self::THOUGHT_TABLE)),
|
||||
self::SETTINGS_TABLE=>array(MySqlManager::getId(self::USER_TABLE),
|
||||
MySqlManager::getText(self::SETTINGS_TABLE),
|
||||
self::USER_TABLE =>array(Db::getText(self::USER_TABLE), 'nickname', 'pass', 'cookie'),
|
||||
self::THOUGHT_TABLE =>array(Db::getId(self::USER_TABLE),
|
||||
Db::getText(self::THOUGHT_TABLE)),
|
||||
self::SETTINGS_TABLE=>array(Db::getId(self::USER_TABLE),
|
||||
Db::getText(self::SETTINGS_TABLE),
|
||||
'value')
|
||||
),
|
||||
'types' => array
|
||||
(
|
||||
MySqlManager::getText(self::USER_TABLE)=>"varchar(50) NOT NULL",
|
||||
Db::getText(self::USER_TABLE)=>"varchar(50) NOT NULL",
|
||||
'nickname'=>'varchar(60) NOT NULL',
|
||||
'pass'=>"varchar(256) NOT NULL",
|
||||
'cookie'=>"varchar(255) NOT NULL",
|
||||
MySqlManager::getText(self::THOUGHT_TABLE)=>"longtext",
|
||||
MySqlManager::getText(self::SETTINGS_TABLE)=>"varchar(20) NOT NULL",
|
||||
'pass'=>"varchar(256)",
|
||||
'cookie'=>"varchar(255)",
|
||||
Db::getText(self::THOUGHT_TABLE)=>"longtext",
|
||||
Db::getText(self::SETTINGS_TABLE)=>"varchar(20) NOT NULL",
|
||||
'value'=>"varchar(20) NOT NULL"
|
||||
),
|
||||
'constraints' => array
|
||||
(
|
||||
self::USER_TABLE=>"UNIQUE KEY `username` (`".MySqlManager::getText(self::USER_TABLE)."`)"
|
||||
self::USER_TABLE=>"UNIQUE KEY `username` (`".Db::getText(self::USER_TABLE)."`)"
|
||||
),
|
||||
'cascading_delete' => array
|
||||
(
|
||||
@@ -222,10 +222,10 @@ class MyThoughts extends PhpObject
|
||||
}
|
||||
else
|
||||
{
|
||||
$asKeys = array(MySqlManager::getId(self::USER_TABLE) => $this->oAuth->getUserId(),
|
||||
MySqlManager::getId(self::THOUGHT_TABLE)=> $iThoughtId);
|
||||
$asThought = array(MySqlManager::getText(self::THOUGHT_TABLE) => self::encodeThought($sThought));
|
||||
$iThoughtId = $this->oMySql->updateRow(self::THOUGHT_TABLE, $asKeys, $asThought);
|
||||
$asKeys = array(Db::getId(self::USER_TABLE) => $this->oAuth->getUserId(),
|
||||
Db::getId(self::THOUGHT_TABLE)=> $iThoughtId);
|
||||
$asThought = array(Db::getText(self::THOUGHT_TABLE) => self::encodeThought($sThought));
|
||||
$iThoughtId = $this->oDb->updateRow(self::THOUGHT_TABLE, $asKeys, $asThought);
|
||||
$sDesc = 'updated';
|
||||
}
|
||||
$bSuccess = ($iThoughtId>0);
|
||||
@@ -247,9 +247,9 @@ class MyThoughts extends PhpObject
|
||||
if($iUserId==-1) $iUserId = $this->oAuth->getUserId();
|
||||
if($iUserId!=0)
|
||||
{
|
||||
$asThought = array( MySqlManager::getId(self::USER_TABLE) => $iUserId,
|
||||
MySqlManager::getText(self::THOUGHT_TABLE) => self::encodeThought($sThought));
|
||||
$ithoughtId = $this->oMySql->insertRow(self::THOUGHT_TABLE, $asThought);
|
||||
$asThought = array( Db::getId(self::USER_TABLE) => $iUserId,
|
||||
Db::getText(self::THOUGHT_TABLE) => self::encodeThought($sThought));
|
||||
$ithoughtId = $this->oDb->insertRow(self::THOUGHT_TABLE, $asThought);
|
||||
}
|
||||
else $this->addError('Adding a thought with no user id');
|
||||
return $ithoughtId;
|
||||
@@ -260,8 +260,8 @@ class MyThoughts extends PhpObject
|
||||
$asThoughtInfo = array();
|
||||
if($iThoughtId>0)
|
||||
{
|
||||
$asThoughtInfo = $this->oMySql->selectRow(self::THOUGHT_TABLE, $iThoughtId);
|
||||
if(!$bThoughtContent) unset($asThoughtInfo[MySqlManager::getText(self::THOUGHT_TABLE)]);
|
||||
$asThoughtInfo = $this->oDb->selectRow(self::THOUGHT_TABLE, $iThoughtId);
|
||||
if(!$bThoughtContent) unset($asThoughtInfo[Db::getText(self::THOUGHT_TABLE)]);
|
||||
}
|
||||
else $this->addError('getting thought info with no thought id');
|
||||
return $asThoughtInfo;
|
||||
|
||||
Reference in New Issue
Block a user