v2.0 first push
This commit is contained in:
174
inc/auth.php
Executable file
174
inc/auth.php
Executable file
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
class Auth extends PhpObject
|
||||
{
|
||||
const ALGO = PASSWORD_DEFAULT;
|
||||
const COST = 12;
|
||||
const TOKEN_SEP = '|';
|
||||
const USER_COOKIE_PASS = 'checksum';
|
||||
|
||||
/**
|
||||
* Database Connection
|
||||
* @var MySqlManager
|
||||
*/
|
||||
private $oMySql;
|
||||
private $iUserId;
|
||||
private $sApiKey;
|
||||
|
||||
public function __construct($oMySql, $sApiKey='', $bAutoLogin=true)
|
||||
{
|
||||
$this->oMySql = $oMySql;
|
||||
$this->setUserId(0);
|
||||
$this->sApiKey = $sApiKey;
|
||||
if($bAutoLogin) $this->autoLogIn();
|
||||
}
|
||||
|
||||
private function setUserId($iUserId)
|
||||
{
|
||||
$this->iUserId = $iUserId;
|
||||
}
|
||||
|
||||
public function getUserId()
|
||||
{
|
||||
return $this->iUserId;
|
||||
}
|
||||
|
||||
public function isLoggedIn()
|
||||
{
|
||||
return ($this->getUserId() > 0);
|
||||
}
|
||||
|
||||
public function logMeIn($sToken)
|
||||
{
|
||||
$sDesc = '';
|
||||
if($sToken!='')
|
||||
{
|
||||
$sLoginToken = addslashes(strstr($sToken, self::TOKEN_SEP, true));
|
||||
$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));
|
||||
if(!empty($asEmpl))
|
||||
{
|
||||
if(self::CheckPassword($sPassToken, $asEmpl['pass']))
|
||||
{
|
||||
$this->setUserId($asEmpl[MySqlManager::getId(MyThoughts::USER_TABLE)]);
|
||||
$this->resetAuthCookie($this->getUserId());
|
||||
}
|
||||
else $sDesc = 'wrong password';
|
||||
}
|
||||
else $sDesc = 'unknown nickname';
|
||||
}
|
||||
else $sDesc = 'corrupted token, please login again';
|
||||
}
|
||||
else $sDesc = 'no credentials has been received by the server';
|
||||
|
||||
return MyThoughts::getJsonResult($this->isLoggedIn(), $sDesc);
|
||||
}
|
||||
|
||||
public function autoLogIn()
|
||||
{
|
||||
if(isset($_COOKIE[self::USER_COOKIE_PASS]))
|
||||
{
|
||||
$sCookie = $_COOKIE[self::USER_COOKIE_PASS];
|
||||
$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));
|
||||
if(!empty($asEmpl))
|
||||
{
|
||||
if($sCookie==$asEmpl['cookie'])
|
||||
{
|
||||
$this->setUserId($asEmpl[MySqlManager::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('Utilisateur '.$iUserId.' inconnu');
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
if($bExist) return -1;
|
||||
else
|
||||
{
|
||||
$iUserId = $this->oMySql->insertRow(MyThoughts::USER_TABLE, array(MySqlManager::getText(MyThoughts::USER_TABLE)=>$sSafeNickName, 'nickname'=>$sNickName));
|
||||
if($iUserId>0)
|
||||
{
|
||||
$this->resetPass($iUserId);
|
||||
if($bLogMeIn) $this->logMeIn(md5($sSafeNickName).self::TOKEN_SEP.$this->getLoginToken($sSafeNickName));
|
||||
}
|
||||
}
|
||||
return $iUserId;
|
||||
}
|
||||
|
||||
//TODO integrate with logMeIn()
|
||||
public function checkApiKey($sApiKey)
|
||||
{
|
||||
return ($this->sApiKey!='' && $sApiKey==$this->sApiKey);
|
||||
}
|
||||
|
||||
private function resetPass($iUserId=0)
|
||||
{
|
||||
$sUserIdCol = MySqlManager::getId(MyThoughts::USER_TABLE);
|
||||
$sUserTextCol = MySqlManager::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);
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
private static function getLoginToken($sPass)
|
||||
{
|
||||
//Add Server Name
|
||||
$sServerName = array_key_exists('SERVER_NAME', $_SERVER)?$_SERVER['SERVER_NAME']:$_SERVER['PWD'];
|
||||
$sAppPath = $_SERVER['REQUEST_SCHEME'].'://'.str_replace(array('http://', 'https://'), '', $sServerName.dirname($_SERVER['SCRIPT_NAME']));
|
||||
$_GET['serv_name'] = $sAppPath.(mb_substr($sAppPath, -1)!='/'?'/':'');
|
||||
return md5($sPass.$_GET['serv_name']);
|
||||
}
|
||||
|
||||
private function resetAuthCookie($iUserId)
|
||||
{
|
||||
$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));
|
||||
setcookie(self::USER_COOKIE_PASS, $iUserId.self::TOKEN_SEP.$sNewPass, $iTimeLimit);
|
||||
}
|
||||
|
||||
private static function getAuthCookie()
|
||||
{
|
||||
return self::HashPassword
|
||||
(
|
||||
$_SERVER['HTTP_USER_AGENT'].
|
||||
$_SERVER['REMOTE_ADDR'].
|
||||
$_SERVER['REQUEST_TIME'].
|
||||
mb_strstr(microtime(), ' ', true).
|
||||
$_SERVER['SERVER_SIGNATURE'].
|
||||
$_SERVER['SERVER_ADMIN']
|
||||
);
|
||||
}
|
||||
|
||||
private static function HashPassword($sPass)
|
||||
{
|
||||
return password_hash($sPass, self::ALGO, array('cost'=>self::COST));
|
||||
}
|
||||
|
||||
private static function CheckPassword($sPass, $sHash)
|
||||
{
|
||||
return password_verify($sPass, $sHash);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
142
inc/calendar.php
Executable file
142
inc/calendar.php
Executable file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
class Calendar extends PhpObject
|
||||
{
|
||||
const CAL_YEAR = 'cy';
|
||||
const CAL_MONTH = 'cm';
|
||||
|
||||
private $oMySql;
|
||||
private $oSession;
|
||||
private $oMask;
|
||||
|
||||
private $iUserId;
|
||||
private $iYear;
|
||||
private $iMonth;
|
||||
|
||||
function __construct($oMySql, $oSession)
|
||||
{
|
||||
parent::__construct();
|
||||
$this->oMySql = $oMySql;
|
||||
$this->oSession = $oSession;
|
||||
$this->oMask = new Mask('calendar');
|
||||
$this->iYear = 0;
|
||||
$this->iMonth = 0;
|
||||
}
|
||||
|
||||
public function setDate($iYear=0, $iMonth=0)
|
||||
{
|
||||
if($iYear==0)
|
||||
{
|
||||
$iYear = date('Y');
|
||||
}
|
||||
if($iMonth==0)
|
||||
{
|
||||
$iMonth = date('m');
|
||||
}
|
||||
$this->iYear = $iYear;
|
||||
$this->iMonth = $iMonth;
|
||||
}
|
||||
|
||||
private function getThoughts()
|
||||
{
|
||||
//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()."
|
||||
AND YEAR(led) = ".$this->iYear."
|
||||
AND MONTH(led) = ".$this->iMonth."
|
||||
GROUP BY day
|
||||
ORDER BY day";
|
||||
|
||||
return $this->oMySql->getArrayQuery($sQuery, true);
|
||||
}
|
||||
|
||||
private function getUpdatedLink($asParams)
|
||||
{
|
||||
$sCurrentVariables = $_SERVER['QUERY_STRING'];
|
||||
$asCurrentVariables = explode('&', $sCurrentVariables);
|
||||
foreach($asCurrentVariables as $sParam)
|
||||
{
|
||||
$sKey = strstr($sParam, '=', true);
|
||||
$sValue = substr(strstr($sParam, '='), 1);
|
||||
$asVariables[$sKey] = $sValue;
|
||||
}
|
||||
return '?'.implodeAll(array_merge($asVariables, $asParams), '=', '&');
|
||||
}
|
||||
|
||||
private function getLink($iOffset)
|
||||
{
|
||||
$iTimeStamp = mktime(0, 0, 0, $this->iMonth + $iOffset, 1, $this->iYear);
|
||||
return $this->getUpdatedLink(array(self::CAL_MONTH=>date('n', $iTimeStamp), self::CAL_YEAR=>date('Y', $iTimeStamp)));
|
||||
}
|
||||
|
||||
private function setMaskItems()
|
||||
{
|
||||
//week starting on the sunday : offset = 0, monday : offset = 1
|
||||
$iOffset = 1;
|
||||
|
||||
//days in the month
|
||||
$iMonthLastDay = date('d', mktime(0, 0, 0, $this->iMonth+1, 0, $this->iYear));
|
||||
$asDays = range(1, $iMonthLastDay);
|
||||
|
||||
$iDayNb = 1 - date($iOffset?'N':'w', mktime(0, 0, 0, $this->iMonth, 1, $this->iYear)) + $iOffset;
|
||||
$iCalendarLastDay = $iMonthLastDay + (7 - date($iOffset?'N':'w', mktime(0, 0, 0, $this->iMonth+1, 0, $this->iYear))) + $iOffset;
|
||||
|
||||
//days with thoughts
|
||||
$asThoughts = $this->getThoughts();
|
||||
|
||||
while($iDayNb < $iCalendarLastDay)
|
||||
{
|
||||
$iCurrentDayTimeStamp = mktime(0, 0, 0, $this->iMonth, $iDayNb, $this->iYear);
|
||||
$sItemDate = date('d', $iCurrentDayTimeStamp);
|
||||
|
||||
//new week
|
||||
if(date('w', $iCurrentDayTimeStamp) == $iOffset)
|
||||
{
|
||||
$this->oMask->newInstance('WEEK');
|
||||
}
|
||||
|
||||
//day within month
|
||||
if(date('n', $iCurrentDayTimeStamp)==$this->iMonth)
|
||||
{
|
||||
$bThoughts = in_array($iDayNb, $asThoughts);
|
||||
|
||||
$sItemClass = $bThoughts?'full':'empty';
|
||||
$sItemLink = $bThoughts?$this->getUpdatedLink(array('d'=>date(MyThoughts::URL_DATE_FORMAT, $iCurrentDayTimeStamp), 'p'=>'r')):'#';
|
||||
$sItemLinkTitle = $bThoughts?'See my thoughts on '.date(MyThoughts::LAYOUT_DATE_FORMAT, $iCurrentDayTimeStamp):'';
|
||||
}
|
||||
else
|
||||
{
|
||||
$sItemClass = 'disabled';
|
||||
$sItemLink = '#';
|
||||
$sItemLinkTitle = '';
|
||||
}
|
||||
|
||||
$this->oMask->addInstance('DAY', array('item_day'=>$sItemDate, 'item_class'=>$sItemClass, 'item_link'=>$sItemLink, 'item_link_title'=>$sItemLinkTitle));
|
||||
$iDayNb++;
|
||||
}
|
||||
|
||||
//column titles
|
||||
$asDayNames = array('1'=>'Mon', '2'=>'Tue', '3'=>'Wed', '4'=>'Thu', '5'=>'Fri', '6'=>'Sat', $iOffset?'7':'0'=>'Sun');
|
||||
ksort($asDayNames);
|
||||
foreach($asDayNames as $sDayName)
|
||||
{
|
||||
$this->oMask->addInstance('TITLE', array('day_name'=>$sDayName));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getCalendar()
|
||||
{
|
||||
$sResult = '';
|
||||
if($this->iYear!=0 && $this->iMonth!=0)
|
||||
{
|
||||
$this->oMask->setTag('link_prev', $this->getLink(-1));
|
||||
$this->oMask->setTag('current_month', date('F', mktime(0, 0, 0, $this->iMonth, 1, $this->iYear)));
|
||||
$this->oMask->setTag('link_next', $this->getLink(1));
|
||||
$this->setMaskItems();
|
||||
$sResult = $this->oMask->getMask();
|
||||
}
|
||||
return $sResult;
|
||||
}
|
||||
}
|
||||
?>
|
||||
342
inc/mythoughts.php
Executable file
342
inc/mythoughts.php
Executable file
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Main Class
|
||||
* @author franzz
|
||||
* @version 2.0
|
||||
*/
|
||||
class MyThoughts extends PhpObject
|
||||
{
|
||||
//Interface keywords
|
||||
const SUCCESS = 'success';
|
||||
const ERROR = 'error';
|
||||
const UNAUTHORIZED = 'unauthorized';
|
||||
const NOT_FOUND = 'unknown action';
|
||||
|
||||
//SQL tables
|
||||
const USER_TABLE = 'users';
|
||||
const THOUGHT_TABLE = 'thoughts';
|
||||
const SETTINGS_TABLE = 'settings';
|
||||
|
||||
//Mythoughts
|
||||
const URL_DATE_FORMAT = 'Ymd';
|
||||
const LAYOUT_DATE_FORMAT = 'F \t\h\e jS, Y';
|
||||
const MYSQL_DATE_FORMAT = 'Y-m-d';
|
||||
const LAYOUT_TIME_FORMAT = 'G:i';
|
||||
const WELCOME_MSG_FILE = 'welcome';
|
||||
const SETTING_LAYOUT = 'layout';
|
||||
const LAYOUT_ONE_PAGE = '1';
|
||||
const LAYOUT_TWO_PAGES = '2';
|
||||
const SETTING_FONT = 'font';
|
||||
const FONT_THOUGHTS = 'thoughts';
|
||||
const FONT_ARIAL = 'Arial';
|
||||
const FONT_VERDANA = 'Verdana';
|
||||
const SETTING_SIZE = 'Size';
|
||||
const SIZE_16 = '16';
|
||||
const SIZE_18 = '18';
|
||||
const SIZE_20 = '20';
|
||||
|
||||
//Objects
|
||||
private $oClassManagement;
|
||||
|
||||
/**
|
||||
* Database Connection
|
||||
* @var MySqlManager
|
||||
*/
|
||||
private $oMySql;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Auth
|
||||
*/
|
||||
private $oAuth;
|
||||
|
||||
//Variables
|
||||
private $asContext;
|
||||
//...
|
||||
|
||||
/**
|
||||
* Main constructor [to be called from index.php]
|
||||
* @param ClassManagement $oClassManagement
|
||||
* @param string $sLang
|
||||
*/
|
||||
public function __construct($oClassManagement, $sProcessPage)
|
||||
{
|
||||
parent::__construct(__CLASS__, Settings::DEBUG);
|
||||
$this->oClassManagement = $oClassManagement;
|
||||
$this->setContext($sProcessPage);
|
||||
|
||||
//Load classes
|
||||
$this->oClassManagement->incClass('mysqlmanager');
|
||||
$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);
|
||||
}
|
||||
|
||||
private function install()
|
||||
{
|
||||
$this->oAuth = new Auth($this->oMySql, Settings::API_KEY, false);
|
||||
|
||||
//Install DB
|
||||
$this->oMySql->install();
|
||||
$this->addUser('franzz');
|
||||
}
|
||||
|
||||
private function setContext($sProcessPage)
|
||||
{
|
||||
//Browser <> PHP <> MySql synchronization
|
||||
date_default_timezone_set(Settings::TIMEZONE);
|
||||
ini_set('default_charset', Settings::TEXT_ENC);
|
||||
header('Content-Type: text/html; charset='.Settings::TEXT_ENC);
|
||||
mb_internal_encoding(Settings::TEXT_ENC);
|
||||
mb_http_output(Settings::TEXT_ENC);
|
||||
mb_http_input(Settings::TEXT_ENC);
|
||||
mb_language('uni');
|
||||
mb_regex_encoding(Settings::TEXT_ENC);
|
||||
|
||||
$this->asContext['process_page'] = basename($sProcessPage);
|
||||
|
||||
$sServerName = array_key_exists('SERVER_NAME', $_SERVER)?$_SERVER['SERVER_NAME']:$_SERVER['PWD'];
|
||||
$sAppPath = 'http://'.str_replace('http://', '', $sServerName.dirname($_SERVER['SCRIPT_NAME']));
|
||||
$this->asContext['serv_name'] = $sAppPath.(mb_substr($sAppPath, -1)!='/'?'/':'');
|
||||
}
|
||||
|
||||
public function addUncaughtError($sError)
|
||||
{
|
||||
$this->addError('Uncaught errors:'."\n".$sError);
|
||||
}
|
||||
|
||||
/* Authorizations handling */
|
||||
|
||||
public function isLoggedIn()
|
||||
{
|
||||
return $this->oAuth->isLoggedIn();
|
||||
}
|
||||
|
||||
public function logMeIn($sToken)
|
||||
{
|
||||
return $this->oAuth->logMeIn($sToken);
|
||||
}
|
||||
|
||||
public function checkApiKey($sApiKey)
|
||||
{
|
||||
return $this->oAuth->checkApiKey($sApiKey);
|
||||
}
|
||||
|
||||
/* Building main pages */
|
||||
|
||||
public function getPage($bLoggedIn)
|
||||
{
|
||||
/*$asMaskPaths = glob('masks/*.html');
|
||||
$asMaskNames = array_map('basename', $asMaskPaths, array_fill(1, count($asMaskPaths), '.html'));*/
|
||||
|
||||
//Constants
|
||||
$asPages = array('logon', 'write', 'settings', 'template');
|
||||
foreach($asPages as $sPage) $asGlobalVars['consts']['pages'][$sPage] = $this->getPageContent($sPage);
|
||||
$asGlobalVars['consts']['token_sep'] = Auth::TOKEN_SEP;
|
||||
$asGlobalVars['consts']['error'] = self::ERROR;
|
||||
$asGlobalVars['consts']['success'] = self::SUCCESS;
|
||||
$asGlobalVars['consts']['context'] = $this->asContext;
|
||||
$asGlobalVars['vars']['id'] = $this->oAuth->getUserId();
|
||||
$asGlobalVars['vars']['log_in'] = $bLoggedIn;
|
||||
|
||||
//Main Page
|
||||
$sPage = $this->getPageContent('index');
|
||||
$sPage = str_replace('asGlobalVars', json_encode($asGlobalVars), $sPage);
|
||||
return $sPage;
|
||||
}
|
||||
|
||||
private function getPageContent($sPage)
|
||||
{
|
||||
$sPageFile = 'masks/'.$sPage.'.html';
|
||||
return file_get_contents($sPageFile);
|
||||
}
|
||||
|
||||
/* DB structure. See MySqlManager::__construct */
|
||||
|
||||
private static function getSqlOptions()
|
||||
{
|
||||
return array
|
||||
(
|
||||
'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),
|
||||
'value')
|
||||
),
|
||||
'types' => array
|
||||
(
|
||||
MySqlManager::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",
|
||||
'value'=>"varchar(20) NOT NULL"
|
||||
),
|
||||
'constraints' => array
|
||||
(
|
||||
self::USER_TABLE=>"UNIQUE KEY `username` (`".MySqlManager::getText(self::USER_TABLE)."`)"
|
||||
),
|
||||
'cascading_delete' => array
|
||||
(
|
||||
self::USER_TABLE=>array(self::SETTINGS_TABLE)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/* My Thoughts public functions */
|
||||
|
||||
public function register($sNickName)
|
||||
{
|
||||
$iUserId = $this->addUser($sNickName, true);
|
||||
$bSuccess = false;
|
||||
$sDesc = '';
|
||||
switch($iUserId)
|
||||
{
|
||||
case -1:
|
||||
$sDesc = 'There is already a user using this nickname, sorry!';
|
||||
break;
|
||||
case 0:
|
||||
$sDesc = 'A database error occured. Contact admin';
|
||||
break;
|
||||
default:
|
||||
$bSuccess = true;
|
||||
}
|
||||
return self::getJsonResult($bSuccess, $sDesc);
|
||||
}
|
||||
|
||||
public function updateThought($sThought, $iThoughtId=0)
|
||||
{
|
||||
if($iThoughtId==0)
|
||||
{
|
||||
$iThoughtId = $this->addThought($sThought);
|
||||
$sDesc = 'created';
|
||||
}
|
||||
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);
|
||||
$sDesc = 'updated';
|
||||
}
|
||||
$bSuccess = ($iThoughtId>0);
|
||||
$sDesc = 'thought '.($bSuccess?'':'not ').$sDesc;
|
||||
return self::getJsonResult($bSuccess, $sDesc, $this->getThoughtInfo($iThoughtId));
|
||||
}
|
||||
|
||||
/* My Thoughts private functions */
|
||||
|
||||
private function addUser($sNickName, $bLogMeIn=false)
|
||||
{
|
||||
$iUserId = $this->oAuth->addUser(self::getSafeNickName($sNickName), $sNickName, $bLogMeIn);
|
||||
if($iUserId>0) $this->addThought(file_get_contents(self::WELCOME_MSG_FILE), $iUserId);
|
||||
return $iUserId;
|
||||
}
|
||||
|
||||
private function addThought($sThought, $iUserId=-1)
|
||||
{
|
||||
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);
|
||||
}
|
||||
else $this->addError('Adding a thought with no user id');
|
||||
return $ithoughtId;
|
||||
}
|
||||
|
||||
private function getThoughtInfo($iThoughtId, $bThoughtContent=false)
|
||||
{
|
||||
$asThoughtInfo = array();
|
||||
if($iThoughtId>0)
|
||||
{
|
||||
$asThoughtInfo = $this->oMySql->selectRow(self::THOUGHT_TABLE, $iThoughtId);
|
||||
if(!$bThoughtContent) unset($asThoughtInfo[MySqlManager::getText(self::THOUGHT_TABLE)]);
|
||||
}
|
||||
else $this->addError('getting thought info with no thought id');
|
||||
return $asThoughtInfo;
|
||||
}
|
||||
|
||||
/* Static toolbox functions */
|
||||
|
||||
private static function encodeThought($sthought)
|
||||
{
|
||||
return base64_encode(serialize(explode("\n", self::shuffleText($sthought))));
|
||||
}
|
||||
|
||||
private static function decodeThought($sEncodedThought)
|
||||
{
|
||||
return self::shuffleText(implode("\n", unserialize(base64_decode($sEncodedThought))));
|
||||
}
|
||||
|
||||
private static function shuffleText($sText)
|
||||
{
|
||||
$sRandomText = "let's_mess%a&bit;with~it,!just§for¨the^sake*of-it";
|
||||
for($iIndex=0; $iIndex < strlen($sText); $iIndex++)
|
||||
{
|
||||
$sText[$iIndex] = $sRandomText[$iIndex%strlen($sRandomText)] ^ $sText[$iIndex];
|
||||
}
|
||||
return $sText;
|
||||
}
|
||||
|
||||
public static function getJsonResult($bSuccess, $sDesc='', $asVars=array())
|
||||
{
|
||||
header('Content-type: application/json');
|
||||
return json_encode(array('result'=>$bSuccess?self::SUCCESS:self::ERROR, 'desc'=>ToolBox::mb_ucwords($sDesc))+$asVars);
|
||||
}
|
||||
|
||||
public function getSafeNickName($sNickName)
|
||||
{
|
||||
return $sNickName;
|
||||
}
|
||||
|
||||
public static function getDateTimeDesc($oTime)
|
||||
{
|
||||
$iTimeStamp = is_numeric($oTime)?$oTime:strtotime($oTime);
|
||||
$sCurTimeStamp = time();
|
||||
|
||||
$asWeekDays = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'satursday', 'sunday');
|
||||
$asMonths = array('january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
|
||||
$sSep = '|';
|
||||
$sFormat = 'Y'.$sSep.'n'.$sSep.'W'.$sSep.'N'.$sSep.'j'.$sSep.'G';
|
||||
list($sYear, $sMonth, $sWeek, $sWeekDay, $sDay, $sHour) = explode($sSep, date($sFormat, $iTimeStamp));
|
||||
list($sCurYear, $sCurMonth, $sCurWeek, $sCurWeekDay, $sCurDay, $sCurHour) = explode($sSep, date($sFormat, $sCurTimeStamp));
|
||||
|
||||
$sDesc = '';
|
||||
if($iTimeStamp>$sCurTimeStamp) $sDesc = 'in the future';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60) $sDesc = 'a few seconds ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*10) $sDesc = 'a few minutes ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*20) $sDesc = '15 minutes ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*50) $sDesc = 'half an hour ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*2) $sDesc = 'an hour ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24 && $sDay==$sCurDay) $sDesc = 'at '.$sHour.' o\'clock';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24) $sDesc = 'yesterday';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7 && $sWeek==$sCurWeek) $sDesc = $asWeekDays[$sWeekDay-1];
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*7) $sDesc = 'last '.$asWeekDays[$sWeekDay-1];
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*9) $sDesc = 'a week ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*12) $sDesc = '10 days ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*16) $sDesc = '2 weeks ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*23) $sDesc = '3 weeks ago';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*31 && $sMonth==$sCurMonth) $sDesc = 'on '.$asMonths[$sMonth-1].', '.$sDay;
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*30*2 && $sMonth==($sCurMonth-1)) $sDesc = 'last month';
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365 && $sYear==$sCurYear) $sDesc = 'in '.$asMonths[$sMonth-1];
|
||||
elseif($sCurTimeStamp-$iTimeStamp<60*60*24*365) $sDesc = 'in '.$asMonths[$sMonth-1].' '.$sYear;
|
||||
elseif($sYear==($sCurYear-1)) $sDesc = 'last year';
|
||||
else $sDesc = 'in '.$sYear;
|
||||
|
||||
//return self::mb_ucfirst($sDesc);
|
||||
return $sDesc;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user