Climode and user db improved
This commit is contained in:
parent
6fba1fd005
commit
1c0038bc55
|
@ -291,7 +291,7 @@ class Plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::set(__METHOD__.LOG_SEP.'Webhook requested.');
|
Log::set(__METHOD__.LOG_SEP.'Webhook requested.');
|
||||||
return mb_substr($URI, $length);
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -30,7 +30,7 @@ function checkPost($args)
|
||||||
if($username!=false)
|
if($username!=false)
|
||||||
{
|
{
|
||||||
// Generate the token and the token expiration date.
|
// Generate the token and the token expiration date.
|
||||||
$token = $dbUsers->generateTokenEmail($username);
|
$token = $dbUsers->setTokenEmail($username);
|
||||||
|
|
||||||
// ---- EMAIL ----
|
// ---- EMAIL ----
|
||||||
$link = $Site->url().'admin/login-email?tokenEmail='.$token.'&username='.$username;
|
$link = $Site->url().'admin/login-email?tokenEmail='.$token.'&username='.$username;
|
||||||
|
|
|
@ -27,7 +27,7 @@ class dbPages extends dbJSON
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new page
|
// Create a new page
|
||||||
public function add($args)
|
public function add($args, $climode=false)
|
||||||
{
|
{
|
||||||
$dataForDb = array(); // This data will be saved in the database
|
$dataForDb = array(); // This data will be saved in the database
|
||||||
$dataForFile = array(); // This data will be saved in the file
|
$dataForFile = array(); // This data will be saved in the file
|
||||||
|
@ -84,17 +84,19 @@ class dbPages extends dbJSON
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the directory
|
if( $climode===false ) {
|
||||||
if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) {
|
// Create the directory
|
||||||
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_PAGES.$key);
|
if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) {
|
||||||
return false;
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_PAGES.$key);
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Make the index.txt and save the file.
|
// Make the index.txt and save the file.
|
||||||
$data = implode("\n", $dataForFile);
|
$data = implode("\n", $dataForFile);
|
||||||
if( file_put_contents(PATH_PAGES.$key.DS.FILENAME, $data) === false ) {
|
if( file_put_contents(PATH_PAGES.$key.DS.FILENAME, $data) === false ) {
|
||||||
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checksum MD5
|
// Checksum MD5
|
||||||
|
@ -112,7 +114,7 @@ class dbPages extends dbJSON
|
||||||
return $key;
|
return $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function edit($args)
|
public function edit($args, $climode=false)
|
||||||
{
|
{
|
||||||
$dataForDb = array();
|
$dataForDb = array();
|
||||||
$dataForFile = array();
|
$dataForFile = array();
|
||||||
|
@ -166,21 +168,23 @@ class dbPages extends dbJSON
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move the directory from old key to new key.
|
if( $climode===false ) {
|
||||||
if($newKey!==$args['key']) {
|
// Move the directory from old key to new key.
|
||||||
if( Filesystem::mv(PATH_PAGES.$args['key'], PATH_PAGES.$newKey) === false ) {
|
if($newKey!==$args['key']) {
|
||||||
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey);
|
if( Filesystem::mv(PATH_PAGES.$args['key'], PATH_PAGES.$newKey) === false ) {
|
||||||
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make the index.txt and save the file.
|
||||||
|
$data = implode("\n", $dataForFile);
|
||||||
|
if( file_put_contents(PATH_PAGES.$newKey.DS.FILENAME, $data) === false ) {
|
||||||
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the index.txt and save the file.
|
|
||||||
$data = implode("\n", $dataForFile);
|
|
||||||
if( file_put_contents(PATH_PAGES.$newKey.DS.FILENAME, $data) === false ) {
|
|
||||||
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the old key
|
// Remove the old key
|
||||||
unset( $this->db[$args['key']] );
|
unset( $this->db[$args['key']] );
|
||||||
|
|
||||||
|
@ -490,40 +494,97 @@ class dbPages extends dbJSON
|
||||||
|
|
||||||
public function rescanClimode()
|
public function rescanClimode()
|
||||||
{
|
{
|
||||||
$pagesDirectories = Filesystem::listDirectories(PATH_PAGES, $regex='*', $sortByDate=false);
|
Log::set('CLI MODE'.LOG_SEP.'Starting re-scan on pages directory.');
|
||||||
foreach($pagesDirectories as $directory) {
|
$pageList = array();
|
||||||
|
|
||||||
|
// Search for pages
|
||||||
|
$directories = Filesystem::listDirectories(PATH_PAGES, $regex='*', $sortByDate=false);
|
||||||
|
foreach($directories as $directory) {
|
||||||
if( Sanitize::pathFile($directory.DS.FILENAME) ) {
|
if( Sanitize::pathFile($directory.DS.FILENAME) ) {
|
||||||
// Page key
|
|
||||||
$pageKey = basename($directory);
|
$pageKey = basename($directory);
|
||||||
|
$pageList[$pageKey] = true;
|
||||||
|
|
||||||
// Checksum
|
// Search for children pages
|
||||||
$checksum = md5_file($directory.DS.FILENAME);
|
$subDirectories = Filesystem::listDirectories(PATH_PAGES.$pageKey.DS, $regex='*', $sortByDate=false);
|
||||||
|
foreach($subDirectories as $subDirectory) {
|
||||||
if( !isset($this->db[$pageKey]) ) {
|
if( Sanitize::pathFile($subDirectory.DS.FILENAME) ) {
|
||||||
insertClimode($pageKey);
|
$subPageKey = basename($subDirectory);
|
||||||
} elseif($this->db[$pageKey]['checksum']!=$checksum) {
|
$subPageKey = $pageKey.'/'.$subPageKey;
|
||||||
updateClimode($pageKey);
|
$pageList[$subPageKey] = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log::set('CLI MODE'.LOG_SEP.'Updating pages...');
|
||||||
|
$keys = array_keys($pageList);
|
||||||
|
foreach($keys as $pageKey) {
|
||||||
|
// Checksum
|
||||||
|
$checksum = md5_file(PATH_PAGES.$pageKey.DS.FILENAME);
|
||||||
|
|
||||||
|
// New page
|
||||||
|
if( !isset($this->db[$pageKey]) ) {
|
||||||
|
$this->verifyFieldsClimode($pageKey, true);
|
||||||
|
}
|
||||||
|
// Update page
|
||||||
|
elseif($this->db[$pageKey]['md5file']!=$checksum) {
|
||||||
|
$this->verifyFieldsClimode($pageKey, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::set('CLI MODE'.LOG_SEP.'Removing pages...');
|
||||||
|
foreach( array_diff_key($this->db, $pageList) as $pageKey=>$data ) {
|
||||||
|
Log::set('CLI MODE'.LOG_SEP.'Removing page from database, key: '.$pageKey);
|
||||||
|
unset( $this->db[$pageKey] );
|
||||||
|
}
|
||||||
|
$this->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function insertClimode($key)
|
private function verifyFieldsClimode($key, $insert=true)
|
||||||
{
|
{
|
||||||
|
$page = new Page($key);
|
||||||
|
$db = $page->getDB();
|
||||||
|
|
||||||
|
// Content from file
|
||||||
|
$db['content'] = $db['contentRaw'];
|
||||||
|
|
||||||
|
// Parent
|
||||||
|
$db['parent'] = '';
|
||||||
|
$db['slug'] = $key;
|
||||||
|
$explodeKey = explode('/', $key);
|
||||||
|
if(isset($explodeKey[1])) {
|
||||||
|
$db['parent'] = $explodeKey[0];
|
||||||
|
$db['slug'] = $explodeKey[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Date
|
||||||
|
if( !isset($db['date']) ) {
|
||||||
|
$db['date'] = Date::current(DB_DATE_FORMAT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status
|
||||||
|
if( !isset($db['status']) ) {
|
||||||
|
$db['status'] = CLI_STATUS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Owner username
|
||||||
|
if( !isset($db['username']) ) {
|
||||||
|
$db['username'] = CLI_USERNAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
// New page or update page
|
||||||
|
if($insert) {
|
||||||
|
Log::set('CLI MODE'.LOG_SEP.'New page found, key:'.$key);
|
||||||
|
return $this->add($db, $climode=true);
|
||||||
|
} else {
|
||||||
|
Log::set('CLI MODE'.LOG_SEP.'Different checksum, updating page, key:'.$key);
|
||||||
|
return $this->edit($db, $climode=true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function updateClimode($key)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----- OLD
|
// ----- OLD
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function parentKeyList()
|
public function parentKeyList()
|
||||||
{
|
{
|
||||||
return $this->parentKeyList;
|
return $this->parentKeyList;
|
||||||
|
|
|
@ -51,7 +51,6 @@ class dbUsers extends dbJSON
|
||||||
if( isset($args[$field]) ) {
|
if( isset($args[$field]) ) {
|
||||||
$value = Sanitize::html($args[$field]);
|
$value = Sanitize::html($args[$field]);
|
||||||
}
|
}
|
||||||
// Use the default value for the field
|
|
||||||
else {
|
else {
|
||||||
$value = $options['value'];
|
$value = $options['value'];
|
||||||
}
|
}
|
||||||
|
@ -63,14 +62,9 @@ class dbUsers extends dbJSON
|
||||||
$dataForDb[$field] = $value;
|
$dataForDb[$field] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current date
|
|
||||||
$dataForDb['registered'] = Date::current(DB_DATE_FORMAT);
|
$dataForDb['registered'] = Date::current(DB_DATE_FORMAT);
|
||||||
|
$dataForDb['salt'] = $this->generateSalt();
|
||||||
// Password
|
$dataForDb['password'] = $this->generatePasswordHash($dataForDb['password'], $dataForDb['salt']);
|
||||||
$dataForDb['salt'] = Text::randomText(SALT_LENGTH);
|
|
||||||
$dataForDb['password'] = sha1($dataForDb['password'].$dataForDb['salt']);
|
|
||||||
|
|
||||||
// Auth token
|
|
||||||
$dataForDb['tokenAuth'] = $this->generateAuthToken();
|
$dataForDb['tokenAuth'] = $this->generateAuthToken();
|
||||||
|
|
||||||
// Save the database
|
// Save the database
|
||||||
|
@ -107,7 +101,7 @@ class dbUsers extends dbJSON
|
||||||
|
|
||||||
public function getUser($username)
|
public function getUser($username)
|
||||||
{
|
{
|
||||||
if($this->userExists($username)) {
|
if($this->exists($username)) {
|
||||||
$User = new User();
|
$User = new User();
|
||||||
$User->setField('username', $username);
|
$User->setField('username', $username);
|
||||||
|
|
||||||
|
@ -126,10 +120,25 @@ class dbUsers extends dbJSON
|
||||||
return md5( uniqid().time().DOMAIN );
|
return md5( uniqid().time().DOMAIN );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function generateEmailToken()
|
||||||
|
{
|
||||||
|
return $this->generateAuthToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generateSalt()
|
||||||
|
{
|
||||||
|
return Text::randomText(SALT_LENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generatePasswordHash($password, $salt)
|
||||||
|
{
|
||||||
|
return sha1($password.$salt);
|
||||||
|
}
|
||||||
|
|
||||||
public function setPassword($username, $password)
|
public function setPassword($username, $password)
|
||||||
{
|
{
|
||||||
$salt = Text::randomText(SALT_LENGTH);
|
$salt = $this->generateSalt();
|
||||||
$hash = sha1($password.$salt);
|
$hash = $this->generatePasswordHash($password, $salt);
|
||||||
$tokenAuth = $this->generateAuthToken();
|
$tokenAuth = $this->generateAuthToken();
|
||||||
|
|
||||||
$args['username'] = $username;
|
$args['username'] = $username;
|
||||||
|
@ -140,28 +149,7 @@ class dbUsers extends dbJSON
|
||||||
return $this->set($args);
|
return $this->set($args);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- OLD
|
// Return the username associated to an email, FALSE otherwise
|
||||||
// Returns array with the username databases filtered by username, FALSE otherwise
|
|
||||||
public function getDb($username)
|
|
||||||
{
|
|
||||||
if($this->userExists($username)) {
|
|
||||||
$user = $this->db[$username];
|
|
||||||
|
|
||||||
return $user;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function getAll()
|
|
||||||
{
|
|
||||||
return $this->db;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Return the username associated to an email, if the email does not exists return FALSE.
|
|
||||||
public function getByEmail($email)
|
public function getByEmail($email)
|
||||||
{
|
{
|
||||||
foreach($this->db as $username=>$values) {
|
foreach($this->db as $username=>$values) {
|
||||||
|
@ -169,7 +157,6 @@ class dbUsers extends dbJSON
|
||||||
return $username;
|
return $username;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,28 +171,37 @@ class dbUsers extends dbJSON
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return TRUE if the user exists, FALSE otherwise.
|
public function setTokenEmail($username)
|
||||||
public function userExists($username)
|
|
||||||
{
|
|
||||||
return isset($this->db[$username]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function generateTokenEmail($username)
|
|
||||||
{
|
{
|
||||||
// Random hash
|
// Random hash
|
||||||
$token = sha1(Text::randomText(SALT_LENGTH).time());
|
$token = $this->generateEmailToken();
|
||||||
$this->db[$username]['tokenEmail'] = $token;
|
$this->db[$username]['tokenEmail'] = $token;
|
||||||
|
|
||||||
// Token time to live, defined by TOKEN_EMAIL_TTL
|
// Token time to live, defined by TOKEN_EMAIL_TTL
|
||||||
$this->db[$username]['tokenEmailTTL'] = Date::currentOffset(DB_DATE_FORMAT, TOKEN_EMAIL_TTL);
|
$this->db[$username]['tokenEmailTTL'] = Date::currentOffset(DB_DATE_FORMAT, TOKEN_EMAIL_TTL);
|
||||||
|
|
||||||
// Save the database
|
// Save the database
|
||||||
if( $this->save() === false ) {
|
$this->save();
|
||||||
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $token;
|
return $token;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
// ---- OLD
|
||||||
|
// Returns array with the username databases filtered by username, FALSE otherwise
|
||||||
|
public function getDb($username)
|
||||||
|
{
|
||||||
|
if($this->exists($username)) {
|
||||||
|
$user = $this->db[$username];
|
||||||
|
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAll()
|
||||||
|
{
|
||||||
|
return $this->db;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -352,7 +352,7 @@ function createUser($args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check already exist username
|
// Check already exist username
|
||||||
if( $dbUsers->userExists($args['new_username']) ) {
|
if( $dbUsers->exists($args['new_username']) ) {
|
||||||
Alert::set($Language->g('username-already-exists'), ALERT_STATUS_FAIL);
|
Alert::set($Language->g('username-already-exists'), ALERT_STATUS_FAIL);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,7 +117,7 @@ class Login {
|
||||||
$this->setLogin($username, $user['role']);
|
$this->setLogin($username, $user['role']);
|
||||||
|
|
||||||
// Invalidate the current token.
|
// Invalidate the current token.
|
||||||
$this->dbUsers->generateTokenEmail($username);
|
$this->dbUsers->setTokenEmail($username);
|
||||||
|
|
||||||
Log::set(__METHOD__.LOG_SEP.'User logged succeeded by Token-email - Username: '.$username);
|
Log::set(__METHOD__.LOG_SEP.'User logged succeeded by Token-email - Username: '.$username);
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,7 @@ class Page {
|
||||||
return($this->vars!==false);
|
return($this->vars!==false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DEPRACTED
|
||||||
// Returns the value from the $field, FALSE if the field doesn't exist
|
// Returns the value from the $field, FALSE if the field doesn't exist
|
||||||
public function getField($field)
|
public function getField($field)
|
||||||
{
|
{
|
||||||
|
@ -94,6 +95,11 @@ class Page {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getDB()
|
||||||
|
{
|
||||||
|
return $this->vars;
|
||||||
|
}
|
||||||
|
|
||||||
// Set a field with a value
|
// Set a field with a value
|
||||||
public function setField($field, $value, $overwrite=true)
|
public function setField($field, $value, $overwrite=true)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue