diff --git a/.gitignore b/.gitignore index ffbf549..22bcd47 100755 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ -/.project -/settings.php -/.buildpath -/.settings/ -/style/.sass-cache/ -/.externalToolBuilders/ -/settings.php +/.project +/settings.php +/.buildpath +/.settings/ +/style/.sass-cache/ +/.externalToolBuilders/ +/settings.php diff --git a/inc/auth.php b/inc/auth.php index d84c857..8924615 100755 --- a/inc/auth.php +++ b/inc/auth.php @@ -1,216 +1,216 @@ -oDb = $oDb; - $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 = ''; - $asUser = $this->getUserFromToken($sToken); - if($asUser['success']) - { - if(self::checkPassword($asUser['http_pass'], $asUser['pass'])) - { - $this->setUserId($asUser[Db::getId(MyThoughts::USER_TABLE)]); - $this->resetAuthCookie($this->getUserId()); - } - else $sDesc = 'wrong password'; - } - else $sDesc = $asUser['desc']; - - return array('success'=>$this->isLoggedIn(), 'desc'=>$sDesc); - } - - public function register($sToken, $sNickName, $bLogMeIn=false) - { - $bSuccess = false; - $sDesc = self::DEFAULT_ERROR; - $asUser = $this->getUserFromToken($sToken); - - if(array_key_exists('unknown_user', $asUser)) - { - $iUserId = $this->addUser($asUser['username'], $sNickName, $asUser['http_pass'], $bLogMeIn); - if($iUserId > 0) $bSuccess = true; - else $sDesc = 'Error: Could not add user'; - } - else $sDesc = 'Someone is already using this nickname, sorry!'; - - $asResult = array('success'=>$bSuccess, 'desc'=>$sDesc); - return $asResult; - } - - private function getUserFromToken($sToken) - { - $asResult = array(); - $bSuccess = false; - $sDesc = self::DEFAULT_ERROR; - - if($sToken!='') - { - $asResult['username'] = addslashes(strstr($sToken, self::TOKEN_SEP, true)); - $asResult['http_pass'] = substr(strstr($sToken, self::TOKEN_SEP), strlen(self::TOKEN_SEP)); - - if($asResult['username']!='' && $asResult['http_pass']!='') - { - $asUser = $this->oDb->selectRow(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$asResult['username'])); - if(!empty($asUser)) - { - $asResult += $asUser; - $bSuccess = true; - } - else - { - $asResult['unknown_user'] = true; - $sDesc = 'unknown nickname'; - } - } - else $sDesc = 'corrupted token, please login again'; - } - else $sDesc = 'no credentials has been received by the server'; - - $asResult['success'] = $bSuccess; - $asResult['desc'] = $sDesc; - return $asResult; - } - - 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->oDb->selectRow(MyThoughts::USER_TABLE, array(Db::getId(MyThoughts::USER_TABLE)=>$iUserId)); - if(!empty($asEmpl)) - { - if($sCookie==$asEmpl['cookie']) - { - $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[Db::getId(MyThoughts::USER_TABLE)]); - } - else $this->addError('Utilisateur '.$iUserId.' inconnu'); - } - } - - public function addUser($sUserHash, $sNickName, $sLoginToken, $bLogMeIn=false) - { - $sPass = self::hashPassword($sLoginToken); - $bExist = $this->oDb->pingValue(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$sUserHash)); - if($bExist) return -1; - else - { - $iUserId = $this->oDb->insertRow(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$sUserHash, 'nickname'=>$sNickName, 'pass'=>$sPass)); - if($iUserId>0 && $bLogMeIn) - { - $this->logMeIn($sUserHash.self::TOKEN_SEP.$sPass); - } - } - return $iUserId; - } - - //TODO integrate with logMeIn() - public function checkApiKey($sApiKey) - { - return ($this->sApiKey!='' && $sApiKey==$this->sApiKey); - } - - private function resetPass($iUserId=0) - { - $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->oDb->selectRows($asInfo); - foreach($asUsers as $asUser) - { - $sToken = self::hashPassword(self::getLoginToken($asUser[$sUserTextCol])); - $this->oDb->updateRow(MyThoughts::USER_TABLE, array(Db::getId(MyThoughts::USER_TABLE)=>$asUser[$sUserIdCol]), array('pass'=>$sToken)); - } - } - - public 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; - $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); - } - - 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); - } -} - +oDb = $oDb; + $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 = ''; + $asUser = $this->getUserFromToken($sToken); + if($asUser['success']) + { + if(self::checkPassword($asUser['http_pass'], $asUser['pass'])) + { + $this->setUserId($asUser[Db::getId(MyThoughts::USER_TABLE)]); + $this->resetAuthCookie($this->getUserId()); + } + else $sDesc = 'wrong password'; + } + else $sDesc = $asUser['desc']; + + return array('success'=>$this->isLoggedIn(), 'desc'=>$sDesc); + } + + public function register($sToken, $sNickName, $bLogMeIn=false) + { + $bSuccess = false; + $sDesc = self::DEFAULT_ERROR; + $asUser = $this->getUserFromToken($sToken); + + if(array_key_exists('unknown_user', $asUser)) + { + $iUserId = $this->addUser($asUser['username'], $sNickName, $asUser['http_pass'], $bLogMeIn); + if($iUserId > 0) $bSuccess = true; + else $sDesc = 'Error: Could not add user'; + } + else $sDesc = 'Someone is already using this nickname, sorry!'; + + $asResult = array('success'=>$bSuccess, 'desc'=>$sDesc); + return $asResult; + } + + private function getUserFromToken($sToken) + { + $asResult = array(); + $bSuccess = false; + $sDesc = self::DEFAULT_ERROR; + + if($sToken!='') + { + $asResult['username'] = addslashes(strstr($sToken, self::TOKEN_SEP, true)); + $asResult['http_pass'] = substr(strstr($sToken, self::TOKEN_SEP), strlen(self::TOKEN_SEP)); + + if($asResult['username']!='' && $asResult['http_pass']!='') + { + $asUser = $this->oDb->selectRow(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$asResult['username'])); + if(!empty($asUser)) + { + $asResult += $asUser; + $bSuccess = true; + } + else + { + $asResult['unknown_user'] = true; + $sDesc = 'unknown nickname'; + } + } + else $sDesc = 'corrupted token, please login again'; + } + else $sDesc = 'no credentials has been received by the server'; + + $asResult['success'] = $bSuccess; + $asResult['desc'] = $sDesc; + return $asResult; + } + + 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->oDb->selectRow(MyThoughts::USER_TABLE, array(Db::getId(MyThoughts::USER_TABLE)=>$iUserId)); + if(!empty($asEmpl)) + { + if($sCookie==$asEmpl['cookie']) + { + $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[Db::getId(MyThoughts::USER_TABLE)]); + } + else $this->addError('Utilisateur '.$iUserId.' inconnu'); + } + } + + public function addUser($sUserHash, $sNickName, $sLoginToken, $bLogMeIn=false) + { + $sPass = self::hashPassword($sLoginToken); + $bExist = $this->oDb->pingValue(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$sUserHash)); + if($bExist) return -1; + else + { + $iUserId = $this->oDb->insertRow(MyThoughts::USER_TABLE, array(Db::getText(MyThoughts::USER_TABLE)=>$sUserHash, 'nickname'=>$sNickName, 'pass'=>$sPass)); + if($iUserId>0 && $bLogMeIn) + { + $this->logMeIn($sUserHash.self::TOKEN_SEP.$sPass); + } + } + return $iUserId; + } + + //TODO integrate with logMeIn() + public function checkApiKey($sApiKey) + { + return ($this->sApiKey!='' && $sApiKey==$this->sApiKey); + } + + private function resetPass($iUserId=0) + { + $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->oDb->selectRows($asInfo); + foreach($asUsers as $asUser) + { + $sToken = self::hashPassword(self::getLoginToken($asUser[$sUserTextCol])); + $this->oDb->updateRow(MyThoughts::USER_TABLE, array(Db::getId(MyThoughts::USER_TABLE)=>$asUser[$sUserIdCol]), array('pass'=>$sToken)); + } + } + + public 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; + $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); + } + + 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); + } +} + ?> \ No newline at end of file diff --git a/inc/calendar.php b/inc/calendar.php index eb42960..a48918f 100755 --- a/inc/calendar.php +++ b/inc/calendar.php @@ -1,142 +1,142 @@ -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 ".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 - 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; - } -} +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 ".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 + 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; + } +} ?> \ No newline at end of file diff --git a/inc/mythoughts.php b/inc/mythoughts.php index 7f88719..f53ea7d 100755 --- a/inc/mythoughts.php +++ b/inc/mythoughts.php @@ -1,242 +1,242 @@ -oClassManagement->incClass('calendar', true); - $asClasses = array( array('name'=>'auth', 'project'=>true), - array('name'=>'thought', 'project'=>true)); - - parent::__construct($oClassManagement, $sProcessPage, $asClasses); - - //Init objects - if($this->oDb->sDbState == Db::DB_PEACHY) $this->oAuth = new Auth($this->oDb, Settings::API_KEY); - } - - protected function install() - { - $this->oAuth = new Auth($this->oDb, Settings::API_KEY, false); - - //Install DB - $this->oDb->install(); - } - - 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 register($sToken, $sNickname) - { - $asResult = $this->oAuth->register($sToken, $sNickname); - - if($asResult['success']) return $this->logMeIn($sToken); - else return self::getJsonResult($asResult['success'], $asResult['desc']); - } - - public function isLoggedIn() - { - return $this->oAuth->isLoggedIn(); - } - - public function logMeIn($sToken) - { - $asLogResult = $this->oAuth->logMeIn($sToken); - return MyThoughts::getJsonResult($asLogResult['success'], $asLogResult['desc'], $this->getVars()); - } - - public function checkApiKey($sApiKey) - { - return $this->oAuth->checkApiKey($sApiKey); - } - - /* Building main pages */ - - public function getPage() - { - //Constants - $asGlobalVars = array( - 'consts' => array( - 'token_sep' => Auth::TOKEN_SEP, - 'error' => self::ERROR, - 'success' => self::SUCCESS, - 'context' => $this->asContext, - 'cookie' => Auth::USER_COOKIE_PASS - ), - 'vars' => $this->getVars() - ); - - //Pages - $asPages = array('logon', 'logoff', 'write', 'read', 'settings', 'template', 'editor'); - foreach($asPages as $sPage) $asGlobalVars['consts']['pages'][$sPage] = $this->getPageContent($sPage); - - //Main Page - $sPage = $this->getPageContent('index'); - $sPage = str_replace('asGlobalVars', json_encode($asGlobalVars), $sPage); - return $sPage; - } - - private function getVars() { - return array( - 'id' => $this->oAuth->getUserId(), - 'log_in' => $this->isLoggedIn() - ); - } - - /* DB structure. See Db::__construct */ - - protected function getSqlOptions() - { - return array( - 'tables' => array( - self::USER_TABLE => array(Db::getText(self::USER_TABLE), 'nickname', 'pass', 'cookie'), - Thought::THOUGHT_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(Thought::THOUGHT_TABLE), 'created'), - self::SETTINGS_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(self::SETTINGS_TABLE), 'value') - ), - 'types' => array( - Db::getText(self::USER_TABLE) => "varchar(32) NOT NULL", - 'nickname' => "varchar(60) NOT NULL", - 'pass' => "varchar(256) NOT NULL", - 'cookie' => "varchar(255)", - Db::getText(Thought::THOUGHT_TABLE) => "longtext", - 'created' => "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP", - Db::getText(self::SETTINGS_TABLE) => "varchar(20) NOT NULL", - 'value' => "varchar(20) NOT NULL" - ), - 'constraints' => array( - self::USER_TABLE => "UNIQUE KEY `unique_username` (`".Db::getText(self::USER_TABLE)."`)" - ), - 'cascading_delete' => array( - self::USER_TABLE => array(self::SETTINGS_TABLE, Thought::THOUGHT_TABLE) - ) - ); - } - - /* Thoughts */ - - public function getThought($iThoughtId, $sFormat=self::OBJ) - { - $oThought = new Thought($this->oDb, $this->oAuth->getUserId()); - - if($iThoughtId=='last') $oThought->openLast(self::LAST_THOUGHT_LIMIT); - else $oThought->open($iThoughtId); - - switch($sFormat) - { - case self::OBJ: - return $oThought; break; - case self::ARRAY: - return $oThought->get(); break; - case self::JSON: - return self::getJsonResult(true, '', $oThought->get()); break; - } - } - - public function updateThought($asOps, $iThoughtId=0) - { - $oThought = new Thought($this->oDb, $this->oAuth->getUserId(), $iThoughtId); - - $oThought->setOps($asOps); - $iThoughtId = $oThought->save(); - - $bSuccess = ($iThoughtId>0); - $sDesc = 'thought '.($bSuccess?'':'not ').'saved'; - return self::getJsonResult($bSuccess, $sDesc, $this->getThought($iThoughtId, self::ARRAY)); - } - - public function getThoughtDates() - { - $asThoughts = Thought::getThoughtDates($this->oDb, $this->oAuth->getUserId()); - foreach($asThoughts as &$asThought) $asThought['created_f'] = self::formatDate($asThought['created'], 'j M'); - return self::getJsonResult(true, '', $asThoughts); - } - - /* Static toolbox functions */ - - public static function getSafeNickName($sNickName) - { - return $sNickName; - } - - private static function formatDate($iTime, $sFormat, $sField='') - { - $iTime = ($sField == '')?$iTime:$iTime[$sField]; - $iTime = is_numeric($iTime)?$iTime:strtotime($iTime); - return date($sFormat, $iTime); - } -} - +oClassManagement->incClass('calendar', true); + $asClasses = array( array('name'=>'auth', 'project'=>true), + array('name'=>'thought', 'project'=>true)); + + parent::__construct($oClassManagement, $sProcessPage, $asClasses); + + //Init objects + if($this->oDb->sDbState == Db::DB_PEACHY) $this->oAuth = new Auth($this->oDb, Settings::API_KEY); + } + + protected function install() + { + $this->oAuth = new Auth($this->oDb, Settings::API_KEY, false); + + //Install DB + $this->oDb->install(); + } + + 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 register($sToken, $sNickname) + { + $asResult = $this->oAuth->register($sToken, $sNickname); + + if($asResult['success']) return $this->logMeIn($sToken); + else return self::getJsonResult($asResult['success'], $asResult['desc']); + } + + public function isLoggedIn() + { + return $this->oAuth->isLoggedIn(); + } + + public function logMeIn($sToken) + { + $asLogResult = $this->oAuth->logMeIn($sToken); + return MyThoughts::getJsonResult($asLogResult['success'], $asLogResult['desc'], $this->getVars()); + } + + public function checkApiKey($sApiKey) + { + return $this->oAuth->checkApiKey($sApiKey); + } + + /* Building main pages */ + + public function getPage() + { + //Constants + $asGlobalVars = array( + 'consts' => array( + 'token_sep' => Auth::TOKEN_SEP, + 'error' => self::ERROR, + 'success' => self::SUCCESS, + 'context' => $this->asContext, + 'cookie' => Auth::USER_COOKIE_PASS + ), + 'vars' => $this->getVars() + ); + + //Pages + $asPages = array('logon', 'logoff', 'write', 'read', 'settings', 'template', 'editor'); + foreach($asPages as $sPage) $asGlobalVars['consts']['pages'][$sPage] = $this->getPageContent($sPage); + + //Main Page + $sPage = $this->getPageContent('index'); + $sPage = str_replace('asGlobalVars', json_encode($asGlobalVars), $sPage); + return $sPage; + } + + private function getVars() { + return array( + 'id' => $this->oAuth->getUserId(), + 'log_in' => $this->isLoggedIn() + ); + } + + /* DB structure. See Db::__construct */ + + protected function getSqlOptions() + { + return array( + 'tables' => array( + self::USER_TABLE => array(Db::getText(self::USER_TABLE), 'nickname', 'pass', 'cookie'), + Thought::THOUGHT_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(Thought::THOUGHT_TABLE), 'created'), + self::SETTINGS_TABLE => array(Db::getId(self::USER_TABLE), Db::getText(self::SETTINGS_TABLE), 'value') + ), + 'types' => array( + Db::getText(self::USER_TABLE) => "varchar(32) NOT NULL", + 'nickname' => "varchar(60) NOT NULL", + 'pass' => "varchar(256) NOT NULL", + 'cookie' => "varchar(255)", + Db::getText(Thought::THOUGHT_TABLE) => "longtext", + 'created' => "timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP", + Db::getText(self::SETTINGS_TABLE) => "varchar(20) NOT NULL", + 'value' => "varchar(20) NOT NULL" + ), + 'constraints' => array( + self::USER_TABLE => "UNIQUE KEY `unique_username` (`".Db::getText(self::USER_TABLE)."`)" + ), + 'cascading_delete' => array( + self::USER_TABLE => array(self::SETTINGS_TABLE, Thought::THOUGHT_TABLE) + ) + ); + } + + /* Thoughts */ + + public function getThought($iThoughtId, $sFormat=self::OBJ) + { + $oThought = new Thought($this->oDb, $this->oAuth->getUserId()); + + if($iThoughtId=='last') $oThought->openLast(self::LAST_THOUGHT_LIMIT); + else $oThought->open($iThoughtId); + + switch($sFormat) + { + case self::OBJ: + return $oThought; break; + case self::ARRAY: + return $oThought->get(); break; + case self::JSON: + return self::getJsonResult(true, '', $oThought->get()); break; + } + } + + public function updateThought($asOps, $iThoughtId=0) + { + $oThought = new Thought($this->oDb, $this->oAuth->getUserId(), $iThoughtId); + + $oThought->setOps($asOps); + $iThoughtId = $oThought->save(); + + $bSuccess = ($iThoughtId>0); + $sDesc = 'thought '.($bSuccess?'':'not ').'saved'; + return self::getJsonResult($bSuccess, $sDesc, $this->getThought($iThoughtId, self::ARRAY)); + } + + public function getThoughtDates() + { + $asThoughts = Thought::getThoughtDates($this->oDb, $this->oAuth->getUserId()); + foreach($asThoughts as &$asThought) $asThought['created_f'] = self::formatDate($asThought['created'], 'j M'); + return self::getJsonResult(true, '', $asThoughts); + } + + /* Static toolbox functions */ + + public static function getSafeNickName($sNickName) + { + return $sNickName; + } + + private static function formatDate($iTime, $sFormat, $sField='') + { + $iTime = ($sField == '')?$iTime:$iTime[$sField]; + $iTime = is_numeric($iTime)?$iTime:strtotime($iTime); + return date($sFormat, $iTime); + } +} + ?> \ No newline at end of file diff --git a/inc/setting.php b/inc/setting.php index 7c2c50e..38cf0a7 100644 --- a/inc/setting.php +++ b/inc/setting.php @@ -1,103 +1,103 @@ -oDb = $oDb; - } - - public static function getSettingsList() - { - //TODO Save on database (param table) - return array(self::SETTING_FONT, self::SETTING_SIZE, self::SETTING_LAYOUT); - } - - private static function getDefaultSetting($sSettingName) - { - switch($sSettingName) - { - case self::SETTING_FONT: - return self::FONT_THOUGHTS; - case self::SETTING_LAYOUT: - return self::LAYOUT_ONE_PAGE; - } - return false; - } - - private function getSetting($sSettingName) - { - if(!array_key_exists($sSettingName, $this->asSettings)) - { - $asConstraint = array(Db::getText(Db::SETTINGS_TABLE)=>$sSettingName, Db::getId(Db::USERS_TABLE)=>$this->oSession->getUserId()); - $oValue = $this->oMySql->selectValue(Db::SETTINGS_TABLE, 'value', $asConstraint); - $this->asSettings[$sSettingName] = (!$oValue)?self::getDefaultSetting($sSettingName):$oValue; - } - return $this->asSettings[$sSettingName]; - } - - private function setSetting($sValue, $sSettingName) - { - $this->oMySql->insertUpdateRow(Db::SETTINGS_TABLE, array('setting'=>$sSettingName, Db::getId(Db::USERS_TABLE)=>$this->oSession->getUserId()), array('value'=>$sValue)); - } - - public function setSettings($asSettings) - { - array_walk($asSettings, array($this, 'setSetting')); - } - - public function settingsPage() - { - $this->setPage('settings'); - $this->setPageTitle('Settings'); - $asSettingsOptions = array( self::SETTING_LAYOUT => array( - 'One extensible page' => self::LAYOUT_ONE_PAGE, - 'Two Pages, Diary like' => self::LAYOUT_TWO_PAGES), - self::SETTING_FONT => array( - 'AES Crawl' => self::FONT_THOUGHTS, - 'Arial' => self::FONT_ARIAL, - 'Verdana' => self::FONT_VERDANA), - self::SETTING_SIZE => array( - '16pt' => self::SIZE_16, - '18pt' => self::SIZE_18, - '20pt' => self::SIZE_20)); - - foreach(self::getSettingsList() as $sSettingName) - { - $this->oPageMask->newInstance('SETTING'); - $this->oPageMask->setInstanceTag('SETTING', 'setting_name', $sSettingName); - $sUserSetting = $this->getSetting($sSettingName); - foreach($asSettingsOptions[$sSettingName] as $sOptionName=>$sOptionValue) - { - if($sOptionValue == self::getDefaultSetting($sSettingName)) - { - $sOptionName .= ' (Default)'; - } - $sSelectedOption = ($sUserSetting==$sOptionValue)?'selected':''; - $asSettingOptions = array( 'setting_option_value'=>$sOptionValue, - 'setting_option_selected'=>$sSelectedOption, - 'setting_option_name'=>$sOptionName); - $this->oPageMask->addInstance('SETTING_OPTION', $asSettingOptions); - } - } - } +oDb = $oDb; + } + + public static function getSettingsList() + { + //TODO Save on database (param table) + return array(self::SETTING_FONT, self::SETTING_SIZE, self::SETTING_LAYOUT); + } + + private static function getDefaultSetting($sSettingName) + { + switch($sSettingName) + { + case self::SETTING_FONT: + return self::FONT_THOUGHTS; + case self::SETTING_LAYOUT: + return self::LAYOUT_ONE_PAGE; + } + return false; + } + + private function getSetting($sSettingName) + { + if(!array_key_exists($sSettingName, $this->asSettings)) + { + $asConstraint = array(Db::getText(Db::SETTINGS_TABLE)=>$sSettingName, Db::getId(Db::USERS_TABLE)=>$this->oSession->getUserId()); + $oValue = $this->oMySql->selectValue(Db::SETTINGS_TABLE, 'value', $asConstraint); + $this->asSettings[$sSettingName] = (!$oValue)?self::getDefaultSetting($sSettingName):$oValue; + } + return $this->asSettings[$sSettingName]; + } + + private function setSetting($sValue, $sSettingName) + { + $this->oMySql->insertUpdateRow(Db::SETTINGS_TABLE, array('setting'=>$sSettingName, Db::getId(Db::USERS_TABLE)=>$this->oSession->getUserId()), array('value'=>$sValue)); + } + + public function setSettings($asSettings) + { + array_walk($asSettings, array($this, 'setSetting')); + } + + public function settingsPage() + { + $this->setPage('settings'); + $this->setPageTitle('Settings'); + $asSettingsOptions = array( self::SETTING_LAYOUT => array( + 'One extensible page' => self::LAYOUT_ONE_PAGE, + 'Two Pages, Diary like' => self::LAYOUT_TWO_PAGES), + self::SETTING_FONT => array( + 'AES Crawl' => self::FONT_THOUGHTS, + 'Arial' => self::FONT_ARIAL, + 'Verdana' => self::FONT_VERDANA), + self::SETTING_SIZE => array( + '16pt' => self::SIZE_16, + '18pt' => self::SIZE_18, + '20pt' => self::SIZE_20)); + + foreach(self::getSettingsList() as $sSettingName) + { + $this->oPageMask->newInstance('SETTING'); + $this->oPageMask->setInstanceTag('SETTING', 'setting_name', $sSettingName); + $sUserSetting = $this->getSetting($sSettingName); + foreach($asSettingsOptions[$sSettingName] as $sOptionName=>$sOptionValue) + { + if($sOptionValue == self::getDefaultSetting($sSettingName)) + { + $sOptionName .= ' (Default)'; + } + $sSelectedOption = ($sUserSetting==$sOptionValue)?'selected':''; + $asSettingOptions = array( 'setting_option_value'=>$sOptionValue, + 'setting_option_selected'=>$sSelectedOption, + 'setting_option_name'=>$sOptionName); + $this->oPageMask->addInstance('SETTING_OPTION', $asSettingOptions); + } + } + } } \ No newline at end of file diff --git a/inc/thought.php b/inc/thought.php index d08c171..4273852 100644 --- a/inc/thought.php +++ b/inc/thought.php @@ -1,136 +1,136 @@ -oDb = $oDb; - $this->setUserId($iUserId); - $this->setId($iId); - } - - public function getId() - { - return $this->iId; - } - - public function setId($iId, $bOpen=true) - { - $this->iId = $iId; - if($this->iId > 0 && $bOpen) $this->open($this->iId); - } - - private function setUserId($iUserId) - { - $this->iUserId = $iUserId; - } - - public function setOps($asOps, $bSave=false) - { - $this->asOps = $asOps; - if($bSave) return $this->save(); - } - - public function openLast($iLimit=0) - { - $iId = $this->oDb->selectValue( - self::THOUGHT_TABLE, - "MAX(".Db::getId(self::THOUGHT_TABLE).")", - array(Db::getId(MyThoughts::USER_TABLE) => $this->iUserId)); - - $bSuccess = ($iId > 0); - if($bSuccess) $this->open($iId); - return $bSuccess; - } - - public function open($iId) - { - if($iId > 0) - { - if($this->iUserId > 0) { - $asWhere = array(Db::getId(self::THOUGHT_TABLE)=>$iId, Db::getId(MyThoughts::USER_TABLE) => $this->iUserId); - $asInfo = $this->oDb->selectRow(self::THOUGHT_TABLE, $asWhere); - - $this->iId = $asInfo[Db::getId(self::THOUGHT_TABLE)]; - $this->iUserId = $asInfo[Db::getId(MyThoughts::USER_TABLE)]; - $this->asOps = self::decodeThought($asInfo[Db::getText(self::THOUGHT_TABLE)]); - $this->iCreateTimestamp = strtotime($asInfo['created']); - $this->sLed = $asInfo['led']; - } - else $this->addError('getting thought info with no user id'); - } - else $this->addError('getting thought info with no thought id'); - } - - public function save() - { - $asThought = array( - Db::getId(MyThoughts::USER_TABLE) => $this->iUserId, - Db::getText(self::THOUGHT_TABLE) => self::encodeThought($this->asOps) - ); - - if($this->iId > 0) $this->oDb->updateRow(self::THOUGHT_TABLE, $this->iId, $asThought); - else $this->iId = $this->oDb->insertRow(self::THOUGHT_TABLE, $asThought); - - return $this->iId; - } - - public function get() - { - return array( - 'id' => $this->iId, - 'id_user' => $this->iUserId, - 'ops' => $this->asOps, - 'created' => $this->iCreateTimestamp, - 'created_f' => date('l, j F', $this->iCreateTimestamp), - 'led' => $this->sLed - ); - } - - public static function getThoughtDates(Db $oDb, int $iUser) - { - $asInfo = array( - 'select' => array(Db::getId(self::THOUGHT_TABLE), 'created'), - 'from' => self::THOUGHT_TABLE, - 'constraint'=> array(Db::getId(MyThoughts::USER_TABLE) => $iUser), - 'orderBy' => array('created'=>'DESC') - ); - - return $oDb->selectRows($asInfo); - } - - private static function encodeThought($sthought) - { - return base64_encode(serialize(explode("\n", self::shuffleText(json_encode($sthought))))); - } - - private static function decodeThought($sEncodedThought) - { - return json_decode(self::shuffleText(implode("\n", unserialize(base64_decode($sEncodedThought)))), true); - } - - private static function shuffleText($sText) - { - $sRandomText = Settings::RAND_TEXT; - for($iIndex=0; $iIndex < strlen($sText); $iIndex++) - { - $sText[$iIndex] = $sRandomText[$iIndex%strlen($sRandomText)] ^ $sText[$iIndex]; - } - return $sText; - } +oDb = $oDb; + $this->setUserId($iUserId); + $this->setId($iId); + } + + public function getId() + { + return $this->iId; + } + + public function setId($iId, $bOpen=true) + { + $this->iId = $iId; + if($this->iId > 0 && $bOpen) $this->open($this->iId); + } + + private function setUserId($iUserId) + { + $this->iUserId = $iUserId; + } + + public function setOps($asOps, $bSave=false) + { + $this->asOps = $asOps; + if($bSave) return $this->save(); + } + + public function openLast($iLimit=0) + { + $iId = $this->oDb->selectValue( + self::THOUGHT_TABLE, + "MAX(".Db::getId(self::THOUGHT_TABLE).")", + array(Db::getId(MyThoughts::USER_TABLE) => $this->iUserId)); + + $bSuccess = ($iId > 0); + if($bSuccess) $this->open($iId); + return $bSuccess; + } + + public function open($iId) + { + if($iId > 0) + { + if($this->iUserId > 0) { + $asWhere = array(Db::getId(self::THOUGHT_TABLE)=>$iId, Db::getId(MyThoughts::USER_TABLE) => $this->iUserId); + $asInfo = $this->oDb->selectRow(self::THOUGHT_TABLE, $asWhere); + + $this->iId = $asInfo[Db::getId(self::THOUGHT_TABLE)]; + $this->iUserId = $asInfo[Db::getId(MyThoughts::USER_TABLE)]; + $this->asOps = self::decodeThought($asInfo[Db::getText(self::THOUGHT_TABLE)]); + $this->iCreateTimestamp = strtotime($asInfo['created']); + $this->sLed = $asInfo['led']; + } + else $this->addError('getting thought info with no user id'); + } + else $this->addError('getting thought info with no thought id'); + } + + public function save() + { + $asThought = array( + Db::getId(MyThoughts::USER_TABLE) => $this->iUserId, + Db::getText(self::THOUGHT_TABLE) => self::encodeThought($this->asOps) + ); + + if($this->iId > 0) $this->oDb->updateRow(self::THOUGHT_TABLE, $this->iId, $asThought); + else $this->iId = $this->oDb->insertRow(self::THOUGHT_TABLE, $asThought); + + return $this->iId; + } + + public function get() + { + return array( + 'id' => $this->iId, + 'id_user' => $this->iUserId, + 'ops' => $this->asOps, + 'created' => $this->iCreateTimestamp, + 'created_f' => date('l, j F', $this->iCreateTimestamp), + 'led' => $this->sLed + ); + } + + public static function getThoughtDates(Db $oDb, int $iUser) + { + $asInfo = array( + 'select' => array(Db::getId(self::THOUGHT_TABLE), 'created'), + 'from' => self::THOUGHT_TABLE, + 'constraint'=> array(Db::getId(MyThoughts::USER_TABLE) => $iUser), + 'orderBy' => array('created'=>'DESC') + ); + + return $oDb->selectRows($asInfo); + } + + private static function encodeThought($sthought) + { + return base64_encode(serialize(explode("\n", self::shuffleText(json_encode($sthought))))); + } + + private static function decodeThought($sEncodedThought) + { + return json_decode(self::shuffleText(implode("\n", unserialize(base64_decode($sEncodedThought)))), true); + } + + private static function shuffleText($sText) + { + $sRandomText = Settings::RAND_TEXT; + for($iIndex=0; $iIndex < strlen($sText); $iIndex++) + { + $sText[$iIndex] = $sRandomText[$iIndex%strlen($sRandomText)] ^ $sText[$iIndex]; + } + return $sText; + } } \ No newline at end of file diff --git a/index.php b/index.php index fe93be5..3896908 100755 --- a/index.php +++ b/index.php @@ -1,196 +1,196 @@ -isLoggedIn(); - -$sResult = ''; -if($sAction=='logmein') $sResult = $oMyThoughts->logMeIn($sToken); -elseif($sAction!='' && $bLoggedIn) -{ - switch ($sAction) - { - case 'load': - $sResult = $oMyThoughts->getThought($iId, MyThoughts::JSON); - break; - case 'update': - $sResult = $oMyThoughts->updateThought($sContent, $iId); - break; - case 'thoughts': - $sResult = $oMyThoughts->getThoughtDates(); - break; - default: - $sResult = MyThoughts::getJsonResult(false, MyThoughts::NOT_FOUND); - } -} -elseif($sAction!='' && !$bLoggedIn) -{ - if($oMyThoughts->checkApiKey($iApiKey)) - { - switch ($sAction) - { - case '': - //$sResult = $oMyThoughts->apifunction(); - break; - default: - $sResult = MyThoughts::getJsonResult(false, MyThoughts::NOT_FOUND); - } - } - elseif($sAction=='register') $sResult = $oMyThoughts->register($sToken, $sNickName); - else $sResult = MyThoughts::getJsonResult(false, MyThoughts::UNAUTHORIZED); -} -else $sResult = $oMyThoughts->getPage(); - -$sDebug = ob_get_clean(); -if(Settings::DEBUG && $sDebug!='') $oMyThoughts->addUncaughtError($sDebug); - -echo $sResult; - -/* - -//load classes -session_start(); -require_once 'config.php'; - -//clean sent values -cleanPost($_POST); -cleanPost($_GET); -cleanPost($_REQUEST); - -//general -$sPage = (isset($_GET['p']) && $_GET['p']!='')?$_GET['p']:'w'; -$sPostToken = isset($_POST['post_token'])?$_POST['post_token']:''; - -//logon -$sLogin = (isset($_POST['login']) && $_POST['login']!='Nickname')?$_POST['login']:''; -$sPass = (isset($_POST['pass']) && $_POST['pass']!='Password')?$_POST['pass']:''; -$bRegister = (isset($_POST['register']) && $_POST['register']==1); - -//writing pad -$sThought = isset($_POST['thoughts'])?$_POST['thoughts']:''; -$iThoughtId = (isset($_POST['thought_id']) && $_POST['thought_id']!='')?$_POST['thought_id']:0; //update or insert -$bFinishedWriting = isset($_POST['finished']); - -//calendar -$iDay = isset($_GET['d'])?$_GET['d']:date(MyThoughts::URL_DATE_FORMAT); //d = yyyymmdd -$iCalYear = isset($_GET[Calendar::CAL_YEAR])?$_GET[Calendar::CAL_YEAR]:0; //cy = yyyy -$iCalMonth = isset($_GET[Calendar::CAL_MONTH])?$_GET[Calendar::CAL_MONTH]:0; //cm = m - -$oMyThougths = new MyThoughts(); -$bValidPost = ($sPostToken!='' && $oMyThougths->checkPostToken($sPostToken)); - -if($bValidPost) -{ - if($bRegister) - { - $oMyThougths->register($sLogin, $sPass); - $sPage = 'r'; - } - elseif($sLogin!='' && $sPass!='') - { - $oMyThougths->logMeIn($sLogin, $sPass); - } -} - -//if loggued in -if(!$oMyThougths->isLogguedIn()) -{ - $oMyThougths->logonPage($sLogin); -} -else -{ - $oMyThougths->activateMenu(); - $oMyThougths->setCalendarDate(); - switch($sPage) - { - case 'w': //write a thought - if($bValidPost && $sThought!='' && $sThought!='Talk to me.') - { - if($iThoughtId==0) - { - $iThoughtId = $oMyThougths->addThought($sThought); - } - else - { - $oMyThougths->updateThought($iThoughtId, $sThought); - } - } - if($bFinishedWriting) - { - $oMyThougths->readingPage(); - } - else - { - $oMyThougths->writingPage($iThoughtId); - } - break; - case 'r': //read a thought (per day) - if($iDay<=0 || !$oMyThougths->readingPage(strtotime($iDay))) - { - $oMyThougths->writingPage(); - } - break; - case 's': // go to settings page - if($bValidPost) - { - $asSettings = array_intersect_key($_POST, array_flip($oMyThougths->getSettingsList())); - $oMyThougths->setSettings($asSettings); - $oMyThougths->writingPage(); - } - else - { - $oMyThougths->settingsPage(); - } - break; - case 'q': //quit - $oMyThougths->logMeOut(); - } - - if($iCalYear!=0 && $iCalMonth!=0) - { - $oMyThougths->setCalendarDate($iCalYear, $iCalMonth); - } -} -echo $oMyThougths->getPage(); - -*/ +isLoggedIn(); + +$sResult = ''; +if($sAction=='logmein') $sResult = $oMyThoughts->logMeIn($sToken); +elseif($sAction!='' && $bLoggedIn) +{ + switch ($sAction) + { + case 'load': + $sResult = $oMyThoughts->getThought($iId, MyThoughts::JSON); + break; + case 'update': + $sResult = $oMyThoughts->updateThought($sContent, $iId); + break; + case 'thoughts': + $sResult = $oMyThoughts->getThoughtDates(); + break; + default: + $sResult = MyThoughts::getJsonResult(false, MyThoughts::NOT_FOUND); + } +} +elseif($sAction!='' && !$bLoggedIn) +{ + if($oMyThoughts->checkApiKey($iApiKey)) + { + switch ($sAction) + { + case '': + //$sResult = $oMyThoughts->apifunction(); + break; + default: + $sResult = MyThoughts::getJsonResult(false, MyThoughts::NOT_FOUND); + } + } + elseif($sAction=='register') $sResult = $oMyThoughts->register($sToken, $sNickName); + else $sResult = MyThoughts::getJsonResult(false, MyThoughts::UNAUTHORIZED); +} +else $sResult = $oMyThoughts->getPage(); + +$sDebug = ob_get_clean(); +if(Settings::DEBUG && $sDebug!='') $oMyThoughts->addUncaughtError($sDebug); + +echo $sResult; + +/* + +//load classes +session_start(); +require_once 'config.php'; + +//clean sent values +cleanPost($_POST); +cleanPost($_GET); +cleanPost($_REQUEST); + +//general +$sPage = (isset($_GET['p']) && $_GET['p']!='')?$_GET['p']:'w'; +$sPostToken = isset($_POST['post_token'])?$_POST['post_token']:''; + +//logon +$sLogin = (isset($_POST['login']) && $_POST['login']!='Nickname')?$_POST['login']:''; +$sPass = (isset($_POST['pass']) && $_POST['pass']!='Password')?$_POST['pass']:''; +$bRegister = (isset($_POST['register']) && $_POST['register']==1); + +//writing pad +$sThought = isset($_POST['thoughts'])?$_POST['thoughts']:''; +$iThoughtId = (isset($_POST['thought_id']) && $_POST['thought_id']!='')?$_POST['thought_id']:0; //update or insert +$bFinishedWriting = isset($_POST['finished']); + +//calendar +$iDay = isset($_GET['d'])?$_GET['d']:date(MyThoughts::URL_DATE_FORMAT); //d = yyyymmdd +$iCalYear = isset($_GET[Calendar::CAL_YEAR])?$_GET[Calendar::CAL_YEAR]:0; //cy = yyyy +$iCalMonth = isset($_GET[Calendar::CAL_MONTH])?$_GET[Calendar::CAL_MONTH]:0; //cm = m + +$oMyThougths = new MyThoughts(); +$bValidPost = ($sPostToken!='' && $oMyThougths->checkPostToken($sPostToken)); + +if($bValidPost) +{ + if($bRegister) + { + $oMyThougths->register($sLogin, $sPass); + $sPage = 'r'; + } + elseif($sLogin!='' && $sPass!='') + { + $oMyThougths->logMeIn($sLogin, $sPass); + } +} + +//if loggued in +if(!$oMyThougths->isLogguedIn()) +{ + $oMyThougths->logonPage($sLogin); +} +else +{ + $oMyThougths->activateMenu(); + $oMyThougths->setCalendarDate(); + switch($sPage) + { + case 'w': //write a thought + if($bValidPost && $sThought!='' && $sThought!='Talk to me.') + { + if($iThoughtId==0) + { + $iThoughtId = $oMyThougths->addThought($sThought); + } + else + { + $oMyThougths->updateThought($iThoughtId, $sThought); + } + } + if($bFinishedWriting) + { + $oMyThougths->readingPage(); + } + else + { + $oMyThougths->writingPage($iThoughtId); + } + break; + case 'r': //read a thought (per day) + if($iDay<=0 || !$oMyThougths->readingPage(strtotime($iDay))) + { + $oMyThougths->writingPage(); + } + break; + case 's': // go to settings page + if($bValidPost) + { + $asSettings = array_intersect_key($_POST, array_flip($oMyThougths->getSettingsList())); + $oMyThougths->setSettings($asSettings); + $oMyThougths->writingPage(); + } + else + { + $oMyThougths->settingsPage(); + } + break; + case 'q': //quit + $oMyThougths->logMeOut(); + } + + if($iCalYear!=0 && $iCalMonth!=0) + { + $oMyThougths->setCalendarDate($iCalYear, $iCalMonth); + } +} +echo $oMyThougths->getPage(); + +*/ ?> \ No newline at end of file diff --git a/masks/editor.html b/masks/editor.html index b5ecb3c..7c68141 100644 --- a/masks/editor.html +++ b/masks/editor.html @@ -1,11 +1,11 @@ -
-