mirror of
https://gitlab.com/CodeSolutionsProject/CodeShare.git
synced 2026-02-14 17:11:34 +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>
|
||||||
<tr>
|
<tr>
|
||||||
<td colspan="6">
|
<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>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -26,9 +26,10 @@ function ajaxresponse(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function reHightlight(){
|
function reHightlight(){
|
||||||
var codebox = document.getElementsByClassName("codeto");
|
var codebox = document.getElementsByClassName("toHightlight");
|
||||||
for(var i=0;i<codebox.length;++i){
|
for(var i=0;i<codebox.length;++i){
|
||||||
hljs.highlightBlock(codebox[i]);
|
hljs.highlightBlock(codebox[i]);
|
||||||
|
codebox.classList.remove('toHightlight');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,10 @@ class DB
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* DB constructor
|
* DB constructor
|
||||||
|
*
|
||||||
|
* @param Doctrine\DBAL\Connection Connection of database
|
||||||
*/
|
*/
|
||||||
function __construct($db)
|
function __construct(Doctrine\DBAL\Connection $db)
|
||||||
{
|
{
|
||||||
$this->conn = $db;
|
$this->conn = $db;
|
||||||
$this->createTable();
|
$this->createTable();
|
||||||
@@ -27,7 +29,7 @@ class DB
|
|||||||
return $queryBuilder;
|
return $queryBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute($queryBuilder){
|
public function execute(Doctrine\DBAL\Query\QueryBuilder $queryBuilder){
|
||||||
$query = $queryBuilder->execute();
|
$query = $queryBuilder->execute();
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
@@ -38,13 +40,78 @@ class DB
|
|||||||
}
|
}
|
||||||
/*End methods*/
|
/*End methods*/
|
||||||
|
|
||||||
|
/* Create Tables */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create table if not exist in deploy (Database must be exist).
|
* Create table if not exist in deploy (Database must be exist).
|
||||||
*/
|
*/
|
||||||
private function createTable()
|
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);
|
->setParameter(1,$idu);
|
||||||
$this->execute($queryBuilder);
|
$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:
|
case 404:
|
||||||
$text = "Page not found";
|
$text = "Page not found";
|
||||||
break;
|
break;
|
||||||
|
case 408:
|
||||||
|
$text = "Time out";
|
||||||
|
break;
|
||||||
case 503:
|
case 503:
|
||||||
$text = "Database unavailable";
|
$text = "Database unavailable";
|
||||||
break;
|
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"]));
|
return $app['twig']->render($app['fronthtml']."/add.twig", array("page" => $page, "user"=>$user, "supported" => $app["supported"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
})->bind('add')->method('GET|POST');
|
})->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