mirror of
https://gitlab.com/CodeSolutionsProject/CodeShare.git
synced 2026-02-14 09:01:33 +01:00
Improve ReHightlight performance, add create tables and start to restore password system
This commit is contained in:
@@ -29,7 +29,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="6">
|
||||
<pre class="highlight"><code class='{{ code.lang }} codeto'>{{ code.code }}</code></pre>
|
||||
<pre class="highlight"><code class='{{ code.lang }} codeto toHightlight'>{{ code.code }}</code></pre>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
@@ -26,9 +26,10 @@ function ajaxresponse(){
|
||||
}
|
||||
|
||||
function reHightlight(){
|
||||
var codebox = document.getElementsByClassName("codeto");
|
||||
var codebox = document.getElementsByClassName("toHightlight");
|
||||
for(var i=0;i<codebox.length;++i){
|
||||
hljs.highlightBlock(codebox[i]);
|
||||
codebox.classList.remove('toHightlight');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,10 @@ class DB
|
||||
|
||||
/**
|
||||
* DB constructor
|
||||
*
|
||||
* @param Doctrine\DBAL\Connection Connection of database
|
||||
*/
|
||||
function __construct($db)
|
||||
function __construct(Doctrine\DBAL\Connection $db)
|
||||
{
|
||||
$this->conn = $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";
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
@@ -411,3 +414,19 @@ $app->get('/add', function(Request $request) use($app){
|
||||
}
|
||||
|
||||
})->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');
|
||||
Reference in New Issue
Block a user