diff --git a/.gitignore b/.gitignore index 51131f6..17bcfcb 100755 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,15 @@ -/.project -/settings.php -/.buildpath -/.settings/ +# App config files +/config/settings.php +/log.html + +# Build folder +/public/* +!/public/index.php + +# Dependencies files +/vendor/ +/node_modules/ +/composer.dev.lock + +# Lint caches +/.php-cs-fixer.cache diff --git a/.htaccess b/.htaccess deleted file mode 100644 index f3a6dd0..0000000 --- a/.htaccess +++ /dev/null @@ -1,3 +0,0 @@ -## Jul - deny access to the top-level git repository -RewriteEngine On -RewriteRule \.git - [F,L] diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..839ff05 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,38 @@ +in([__DIR__.'/lib', __DIR__.'/public']) + ->append([__DIR__.'/config/settings-sample.php']) + ->name('*.php'); + +return (new PhpCsFixer\Config()) + ->setIndent("\t") + ->setLineEnding("\n") + ->setRules([ + 'single_quote' => true, + 'array_syntax' => ['syntax' => 'short'], + 'braces_position' => [ + 'classes_opening_brace' => 'same_line', + 'functions_opening_brace' => 'same_line', + 'anonymous_functions_opening_brace' => 'same_line' + ], + 'concat_space' => ['spacing' => 'none'], + 'constant_case' => ['case' => 'lower'], + 'lowercase_keywords' => true, + 'visibility_required' => ['elements' => ['method', 'property', 'const']], + 'no_unused_imports' => true, + 'no_trailing_whitespace' => true, + 'no_trailing_whitespace_in_comment' => true, + 'single_blank_line_at_eof' => true, + 'no_empty_statement' => true, + 'trim_array_spaces' => true, + 'new_with_parentheses' => true + ]) + ->setFinder($finder); diff --git a/background.php b/background.php deleted file mode 100755 index 5906aec..0000000 --- a/background.php +++ /dev/null @@ -1,19 +0,0 @@ -=8.4", + "franzz/objects": "dev-vue" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.75" + }, + "autoload": { + "psr-4": { + "Franzz\\MyThoughts\\": "lib/", + "Franzz\\Objects\\": "../objects/inc/" + }, + "files": [ + "config/settings.php" + ] + }, + "scripts": { + "lint": "php-cs-fixer fix --dry-run --diff", + "lint-fix": "php-cs-fixer fix" + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..429d761 --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "franzz/mythoughts", + "description": "MyThoughts", + "type": "project", + "license": "GPL-3.0-or-later", + "repositories": [ + { + "type": "git", + "url": "https://git.lutran.fr/franzz/objects" + } + ], + "require": { + "php": ">=8.4", + "franzz/objects": "dev-vue" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.75" + }, + "autoload": { + "psr-4": { + "Franzz\\MyThoughts\\": "lib/" + }, + "files": [ + "config/settings.php" + ] + }, + "scripts": { + "lint": "php-cs-fixer fix --dry-run --diff", + "lint-fix": "php-cs-fixer fix" + } +} diff --git a/config.php b/config.php deleted file mode 100755 index fa48f61..0000000 --- a/config.php +++ /dev/null @@ -1,996 +0,0 @@ -asMessageStack = array(); - $this->asMessageStack[self::ERROR_TAB] = array(); - $this->asMessageStack[self::WARNING_TAB] = array(); - $this->iExtractMode = self::MODE_ARRAY; - } - - protected function addError($sError) - { - $this->addMessage(self::ERROR_TAB, $sError); - } - - protected function addWarning($sWarning) - { - $this->addMessage(self::WARNING_TAB, $sError); - } - - private function addMessage($sType, $sMessage) - { - $this->asMessageStack[$sType][] = $sMessage; - } - - protected function getCleanErrorStack() - { - return $this->getCleanMessageStack(self::ERROR_TAB); - } - - protected function getCleanWarningStack() - { - return $this->getCleanMessageStack(self::WARNING_TAB); - } - - private function getCleanMessageStack($sType) - { - switch($this->iExtractMode) - { - case self::MODE_TEXT: - $oMessageStack = implode("\n", $this->asMessageStack[$sType]); - break; - case self::MODE_ARRAY: - $oMessageStack = $this->asMessageStack[$sType]; - break; - case self::MODE_FILE: - $oMessageStack = implode("
", $this->asMessageStack[$sType]); - break; - } - $this->asMessageStack[$sType] = array(); - return $oMessageStack; - } - - function __destruct() - { - $sErrorStack = $this->getCleanErrorStack(); - - switch($this->iExtractMode) - { - case self::MODE_TEXT: - echo $sErrorStack; - break; - case self::MODE_ARRAY: - if(count($sErrorStack)>0) - { - pre($sErrorStack, 'Error Stack'); - } - break; - case self::MODE_FILE: - if($sErrorStack!='') - { - @file_put_contents('log.html', '
'.date('r')."
".$sErrorStack.'
', FILE_APPEND); - } - break; - } - } -} - -class Session extends PhpObject -{ - private $iUserId; - private $sLogin; - private $sToken; - private $sPostToken; - - private $oMySql; - - const SESSION_ID_USER = 'id_user'; - const SESSION_USER = 'user'; - const SESSION_TOKEN = 'token'; - const SESSION_POST_TOKEN = 'post_token'; - - public function __construct($oMySql) - { - parent::__construct(); - $this->iUserId = $this->sLogin = $this->sToken = $this->sPostToken = false; - $this->oMySql = $oMySql; - $this->syncSession(); - } - - public function getUserId() - { - return $this->iUserId; - } - - private function syncSession() - { - if(isset($_SESSION[self::SESSION_ID_USER])) - { - $this->iUserId = $_SESSION[self::SESSION_ID_USER]; - } - if(isset($_SESSION[self::SESSION_USER])) - { - $this->sLogin = $_SESSION[self::SESSION_USER]; - } - if(isset($_SESSION[self::SESSION_TOKEN])) - { - $this->sToken = $_SESSION[self::SESSION_TOKEN]; - } - if(isset($_SESSION[self::SESSION_POST_TOKEN])) - { - $this->sPostToken = $_SESSION[self::SESSION_POST_TOKEN]; - } - } - - private function setSession($iUserId, $sLogin) - { - $_SESSION[self::SESSION_ID_USER] = $iUserId; - $_SESSION[self::SESSION_USER] = $sLogin; - - //Token - $sToken = $this->createToken(); - $_SESSION[self::SESSION_TOKEN] = $sToken; - $this->setTokenCookie($sToken); - - $this->syncSession(); - } - - public function register($asData) - { - $sLogin = strtolower($asData['user']); - $sPass = $asData['pass']; - - if($sLogin=='' || $sPass=='') - { - $this->addError('Empty mandatory fields (Nickname or password)'); - } - elseif(htmlspecialchars($sLogin, ENT_QUOTES)!=$sLogin) - { - $this->addError('Nickname: HTML characters are forbidden'); - } - elseif($this->oMySql->selectRow(MySqlManager::USERS_TABLE, array('user'=>$sLogin))) - { - $this->addError('Nickname: This is already a user called by that name, choose a different one'); - } - else - { - $asData['pass'] = self::encryptPassword($sPass); - $iUserId = $this->oMySql->insertRow(MySqlManager::USERS_TABLE, $asData); - return $this->logMeIn($sLogin, $sPass); - } - return false; - } - - public function logMeIn($sLogin, $sPass) - { - $bResult = false; - $asUser = $this->oMySql->selectRow(MySqlManager::USERS_TABLE, array('user'=>$sLogin)); - if(!$asUser) - { - $this->addError('Utilisateur inconnu'); - } - elseif(!$this->checkPassword($sPass, $asUser['pass'])) - { - $this->addError('mot de pass incorrect'); - } - else - { - $this->setSession($asUser[MySqlManager::getId(MySqlManager::USERS_TABLE)], $sLogin); - $bResult = true; - } - return $bResult; - } - - public function isLogguedIn() - { - $bLogguedIn = false; - if($this->iUserId && $this->sLogin && $this->sToken) - { - //check if token is set and valid - if($this->checkToken()) - { - //Check if user got a actual account in the database - $bLogguedIn = $this->checkAccount($this->sLogin, $this->iUserId); - } - else - { - $this->addError('Authentication problem, please sign in again'); - } - } - return $bLogguedIn; - } - - private function checkAccount($sUserName, $iUserId=0) - { - $asConstraints = array('user'=>$sUserName); - if($iUserId>0) - { - $asConstraints[MySqlManager::getId(MySqlManager::USERS_TABLE)] = $iUserId; - } - return $this->oMySql->selectValue(MySqlManager::USERS_TABLE, 'COUNT(1)', $asConstraints); - } - - public function logMeOut() - { - $_SESSION = array(); - $this->setTokenCookie('', -1); - return session_destroy(); - } - - private static function encryptPassword($sPass) - { - $sRandomText = 'F_RA-1H"2{bvj)5f?0sd3r#fP,K]U|w}hGiN@(sZ.sDe!7*x/:Mq+&'; - for($iIndex=0; $iIndex < strlen($sPass); $iIndex++) - { - $sPass[$iIndex] = $sRandomText[$iIndex%strlen($sRandomText)] ^ $sPass[$iIndex]; - } - return md5($sPass); - } - - private static function createToken() - { - return self::encryptPassword( $_SERVER['HTTP_USER_AGENT']. - $_SERVER['REMOTE_ADDR']. - $_SERVER['REQUEST_TIME']. - strstr(microtime(), ' ', true). - $_SERVER['SERVER_SIGNATURE']. - $_SERVER['SERVER_ADMIN']); - } - - //Session Token - - private static function setTokenCookie($sToken, $iTime=1) - { - setcookie(self::SESSION_TOKEN, $sToken, time()+60*60*24*$iTime); - $_COOKIE[self::SESSION_TOKEN] = $sToken; - } - - private function checkToken() - { - return ($this->sToken && array_key_exists(self::SESSION_TOKEN, $_COOKIE) && $_COOKIE[self::SESSION_TOKEN] == $this->sToken); - } - - //Post Token - - private function refreshPostToken() - { - $this->sPostToken = self::createToken(); - $_SESSION[self::SESSION_POST_TOKEN] = $this->sPostToken; - } - - public function getNewPostToken() - { - $this->refreshPostToken(); - return $this->sPostToken; - } - - public function checkPostToken($sPostToken) - { - return ($this->sPostToken && $sPostToken!='' && $sPostToken == $this->sPostToken); - } - - private static function checkPassword($sClearPass, $sEncodedPass) - { - return self::encryptPassword($sClearPass) == $sEncodedPass; - } -} - -class Mask extends PhpObject -{ - public $sMaskName; - public $sFilePath; - private $sMask; - private $asTags; - private $asPartsSource; - private $aoInstances; - - const MASK_FOLDER = 'mask/'; - const START_TAG = 'START'; - const END_TAG = 'END'; - const TAG_MARK = '#'; - - public function __construct($sFileName='') - { - //init - parent::__construct(); - $this->sMaskName = ''; - $this->sFilePath = ''; - $this->sMask = ''; - $this->asTags = array(); - $this->asPartsSource = array(); - $this->aoInstances = array(); - $this->sFilePath = ''; - - //load file - if($sFileName!='') - { - $this->initFile($sFileName); - } - } - - public function initFile($sFileName) - { - $sFilePath = self::MASK_FOLDER.$sFileName.'.html'; - if(file_exists($sFilePath)) - { - $this->sFilePath = $sFilePath; - $sSource = file_get_contents($this->sFilePath); - $this->initMask($sFileName, $sSource); - } - else - { - $this->addError('Fichier introuvable à l\'adresse : '.$sFilePath); - } - } - - public function initFileFromString($sSource, $sPartName='', $iInstanceNb=0) - { - $this->initMask($sPartName.' (from row) '.$iInstanceNb, $sSource); - } - - private function initMask($sMaskName, $sSource) - { - $this->sMaskName = $sMaskName; - $this->sMask = $sSource; - $this->setParts(); - } - - private function setParts() - { - while(preg_match('/\<\!-- \[PART\] (?PThoughts on #date#.
-#thought_paragraph#
- -* * *
-$2
","$2","$2"];for(A=0;A<5;A++){for(var i=0;i$1
","$1
"];for(var i=0;i{{ user.email }}
+{{ sError }}
+ +