From 000a9be9b767752765cf5689fb376379f53af30d Mon Sep 17 00:00:00 2001 From: JoseluCross Date: Tue, 16 Oct 2018 17:06:35 +0200 Subject: [PATCH] Improve ReHightlight performance, add create tables and start to restore password system --- Source/assets/html/codebox.twig | 2 +- Source/assets/js/functions.js | 3 +- Source/src/DB.php | 106 +++++++++++++++++++++++++++++++- Source/src/app.php | 21 ++++++- 4 files changed, 126 insertions(+), 6 deletions(-) diff --git a/Source/assets/html/codebox.twig b/Source/assets/html/codebox.twig index fdb49e2..8bec748 100644 --- a/Source/assets/html/codebox.twig +++ b/Source/assets/html/codebox.twig @@ -29,7 +29,7 @@ -
{{ code.code }}
+
{{ code.code }}
diff --git a/Source/assets/js/functions.js b/Source/assets/js/functions.js index a3a8978..a820922 100644 --- a/Source/assets/js/functions.js +++ b/Source/assets/js/functions.js @@ -26,9 +26,10 @@ function ajaxresponse(){ } function reHightlight(){ - var codebox = document.getElementsByClassName("codeto"); + var codebox = document.getElementsByClassName("toHightlight"); for(var i=0;iconn = $db; $this->createTable(); @@ -27,7 +29,7 @@ class DB return $queryBuilder; } - public function execute($queryBuilder){ + public function execute(Doctrine\DBAL\Query\QueryBuilder $queryBuilder){ $query = $queryBuilder->execute(); return $query; } @@ -38,13 +40,78 @@ class DB } /*End methods*/ + /* Create Tables */ /** * Create table if not exist in deploy (Database must be exist). */ private function createTable() { - /*TODO*/ + $schema = $this->conn->getSchemaManager(); + $this->createUsers($schema); + $this->createCodes($schema); + $this->createSources($schema); + } + + private function createUsers(Doctrine\DBAL\Schema\AbstractSchemaManager $schema){ + if(!$schema->tablesExist(array('Users'))){ + $users = new Doctrine\DBAL\Schema\Table("Users"); + + $users->addColumn('IDU','integer',array('unsigned' => true,'autoincrement' => true)); + $users->addColumn('email','string',array('length' => 64)); + $users->addColumn('pass','string',array('length' => 64)); + $users->addColumn('nick','string',array('length' => 40)); + $users->addColumn('token','string',array('length' => 64)); + $users->addColumn('ROLE','string',array('length' => 10)); + + $users->setPrimaryKey(array('IDU')); + $users->addUniqueIndex(array('email')); + $users->addUniqueIndex(array('nick')); + + $schema->createTable($users); + } + } + + private function createCodes(Doctrine\DBAL\Schema\AbstractSchemaManager $schema){ + if(!$schema->tablesExist(array('Codes'))){ + $codes = new \Doctrine\DBAL\Schema\Table('Codes'); + + $codes->addColumn('IDC','integer',array('unsigned' => true, 'autoincrement' => true)); + $codes->addColumn('Name','string',array('length'=>80)); + $codes->addColumn('Description',"text"); + $codes->addColumn('Input',"text"); + $codes->addColumn('Output',"text"); + $codes->addColumn('UserCreator',"integer",array('unsigned' => true)); + + $codes->setPrimaryKey(array('IDC')); + $codes->addForeignKeyConstraint("Users", array('UserCreator'), array('IDU')); + $codes->addIndex(array('UserCreator')); + + $schema->createTable($codes); + } + } + + private function createSources(Doctrine\DBAL\Schema\AbstractSchemaManager $schema){ + if(!$schema->tablesExist(array('Sources'))){ + $sources = new Doctrine\DBAL\Schema\Table('Sources'); + + $sources->addColumn('IDC','integer',array('unsigned' => true, 'autoincrement' => true)); + $sources->addColumn('IDU','integer',array('unsigned' => true,'autoincrement' => true)); + $sources->addColumn('Lang','string',array('length' => 15)); + $sources->addColumn('Version','integer', array('unsigned' => true)); + $sources->addColumn('Modification','integer', array('unsigned' => true)); + $sources->addColumn('Code',"text"); + $sources->addColumn('UseExtLib',"text",array("notnull" => false)); + $sources->addColumn('UseExtLibVer',"string",array("length"=>55,"notnull" => false)); + + $sources->setPrimaryKey(array('IDC','Lang','Version')); + $sources->addIndex(array('IDU')); + $sources->addForeignKeyConstraint('Users',array('IDU'),array('IDU')); + $sources->addForeignKeyConstraint('Codes',array('IDC'),array('IDC'),array('onUpdate'=>'CASCADE','onDelete'=>'CASCADE')); + + $schema->createTable($sources) + + } } /* @@ -656,4 +723,37 @@ class DB ->setParameter(1,$idu); $this->execute($queryBuilder); } + + /** + * Check if token to restore password is correct + * + * @param string $nick string nick to restore password + * @param string $token random string generate + * @param int $timestamp moment when restore activate + * @return bool true is are similar + */ + public function checkRestoreToken($nick,$token,$timestamp){ + $queryBuilder = $this->newQueryBuilder(); + $queryBuilder + ->select('token') + ->from('users') + ->where($queryBuilder->expr()-eq('nick','?')) + ->setParameter(0,$nick); + $tk = $this->getData($queryBuilder)[0]['token']; + return $tk == hash('sha256',"$token-$timestamp"); + } + + public function createRestoreToken($email){ + $token = randomString(75); + $now = time(); + $queryBuilder = $this->newQueryBuilder(); + $queryBuilder + ->update('Users') + ->set('token','?') + ->where($queryBuilder->expr()->eq('email','?')) + ->setParameter(0,hash('sha256',"$token-$now")) + ->setParameter(1,$email); + $this->execute($queryBuilder); + return "$token-$now"; + } } \ No newline at end of file diff --git a/Source/src/app.php b/Source/src/app.php index 5e6cfbe..2750225 100644 --- a/Source/src/app.php +++ b/Source/src/app.php @@ -240,6 +240,9 @@ function HTTPError($code){ case 404: $text = "Page not found"; break; + case 408: + $text = "Time out"; + break; case 503: $text = "Database unavailable"; break; @@ -410,4 +413,20 @@ $app->get('/add', function(Request $request) use($app){ return $app['twig']->render($app['fronthtml']."/add.twig", array("page" => $page, "user"=>$user, "supported" => $app["supported"])); } -})->bind('add')->method('GET|POST'); \ No newline at end of file +})->bind('add')->method('GET|POST'); + +$app->get('/restore/{nick}/{token}-{timestamp}', function(Request $request, $nick, $token, $timestamp) use($app){ + $time = 3600; //An hour + $now = time(); + if($now - $timestamp > $time){ + $app->abort(401); + }else{ + if(!$app['data']->checkRestoreToken($nick,$token,$timestamp)) + $app->abort(408); + if($request->getMethod()=='POST'){ + + }else{ + + } + } +})->bind('restorePass')->method('GET|POST'); \ No newline at end of file