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.');
|
||||
return mb_substr($URI, $length);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -30,7 +30,7 @@ function checkPost($args)
|
|||
if($username!=false)
|
||||
{
|
||||
// Generate the token and the token expiration date.
|
||||
$token = $dbUsers->generateTokenEmail($username);
|
||||
$token = $dbUsers->setTokenEmail($username);
|
||||
|
||||
// ---- EMAIL ----
|
||||
$link = $Site->url().'admin/login-email?tokenEmail='.$token.'&username='.$username;
|
||||
|
|
|
@ -27,7 +27,7 @@ class dbPages extends dbJSON
|
|||
}
|
||||
|
||||
// Create a new page
|
||||
public function add($args)
|
||||
public function add($args, $climode=false)
|
||||
{
|
||||
$dataForDb = array(); // This data will be saved in the database
|
||||
$dataForFile = array(); // This data will be saved in the file
|
||||
|
@ -84,17 +84,19 @@ class dbPages extends dbJSON
|
|||
}
|
||||
}
|
||||
|
||||
// Create the directory
|
||||
if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) {
|
||||
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_PAGES.$key);
|
||||
return false;
|
||||
}
|
||||
if( $climode===false ) {
|
||||
// Create the directory
|
||||
if( Filesystem::mkdir(PATH_PAGES.$key, true) === 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.
|
||||
$data = implode("\n", $dataForFile);
|
||||
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);
|
||||
return false;
|
||||
// Make the index.txt and save the file.
|
||||
$data = implode("\n", $dataForFile);
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Checksum MD5
|
||||
|
@ -112,7 +114,7 @@ class dbPages extends dbJSON
|
|||
return $key;
|
||||
}
|
||||
|
||||
public function edit($args)
|
||||
public function edit($args, $climode=false)
|
||||
{
|
||||
$dataForDb = array();
|
||||
$dataForFile = array();
|
||||
|
@ -166,21 +168,23 @@ class dbPages extends dbJSON
|
|||
}
|
||||
}
|
||||
|
||||
// Move the directory from old key to new key.
|
||||
if($newKey!==$args['key']) {
|
||||
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);
|
||||
if( $climode===false ) {
|
||||
// Move the directory from old key to new key.
|
||||
if($newKey!==$args['key']) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
unset( $this->db[$args['key']] );
|
||||
|
||||
|
@ -490,40 +494,97 @@ class dbPages extends dbJSON
|
|||
|
||||
public function rescanClimode()
|
||||
{
|
||||
$pagesDirectories = Filesystem::listDirectories(PATH_PAGES, $regex='*', $sortByDate=false);
|
||||
foreach($pagesDirectories as $directory) {
|
||||
Log::set('CLI MODE'.LOG_SEP.'Starting re-scan on pages directory.');
|
||||
$pageList = array();
|
||||
|
||||
// Search for pages
|
||||
$directories = Filesystem::listDirectories(PATH_PAGES, $regex='*', $sortByDate=false);
|
||||
foreach($directories as $directory) {
|
||||
if( Sanitize::pathFile($directory.DS.FILENAME) ) {
|
||||
// Page key
|
||||
$pageKey = basename($directory);
|
||||
$pageList[$pageKey] = true;
|
||||
|
||||
// Checksum
|
||||
$checksum = md5_file($directory.DS.FILENAME);
|
||||
|
||||
if( !isset($this->db[$pageKey]) ) {
|
||||
insertClimode($pageKey);
|
||||
} elseif($this->db[$pageKey]['checksum']!=$checksum) {
|
||||
updateClimode($pageKey);
|
||||
// Search for children pages
|
||||
$subDirectories = Filesystem::listDirectories(PATH_PAGES.$pageKey.DS, $regex='*', $sortByDate=false);
|
||||
foreach($subDirectories as $subDirectory) {
|
||||
if( Sanitize::pathFile($subDirectory.DS.FILENAME) ) {
|
||||
$subPageKey = basename($subDirectory);
|
||||
$subPageKey = $pageKey.'/'.$subPageKey;
|
||||
$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
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public function parentKeyList()
|
||||
{
|
||||
return $this->parentKeyList;
|
||||
|
|
|
@ -51,7 +51,6 @@ class dbUsers extends dbJSON
|
|||
if( isset($args[$field]) ) {
|
||||
$value = Sanitize::html($args[$field]);
|
||||
}
|
||||
// Use the default value for the field
|
||||
else {
|
||||
$value = $options['value'];
|
||||
}
|
||||
|
@ -63,14 +62,9 @@ class dbUsers extends dbJSON
|
|||
$dataForDb[$field] = $value;
|
||||
}
|
||||
|
||||
// Current date
|
||||
$dataForDb['registered'] = Date::current(DB_DATE_FORMAT);
|
||||
|
||||
// Password
|
||||
$dataForDb['salt'] = Text::randomText(SALT_LENGTH);
|
||||
$dataForDb['password'] = sha1($dataForDb['password'].$dataForDb['salt']);
|
||||
|
||||
// Auth token
|
||||
$dataForDb['salt'] = $this->generateSalt();
|
||||
$dataForDb['password'] = $this->generatePasswordHash($dataForDb['password'], $dataForDb['salt']);
|
||||
$dataForDb['tokenAuth'] = $this->generateAuthToken();
|
||||
|
||||
// Save the database
|
||||
|
@ -107,7 +101,7 @@ class dbUsers extends dbJSON
|
|||
|
||||
public function getUser($username)
|
||||
{
|
||||
if($this->userExists($username)) {
|
||||
if($this->exists($username)) {
|
||||
$User = new User();
|
||||
$User->setField('username', $username);
|
||||
|
||||
|
@ -126,10 +120,25 @@ class dbUsers extends dbJSON
|
|||
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)
|
||||
{
|
||||
$salt = Text::randomText(SALT_LENGTH);
|
||||
$hash = sha1($password.$salt);
|
||||
$salt = $this->generateSalt();
|
||||
$hash = $this->generatePasswordHash($password, $salt);
|
||||
$tokenAuth = $this->generateAuthToken();
|
||||
|
||||
$args['username'] = $username;
|
||||
|
@ -140,28 +149,7 @@ class dbUsers extends dbJSON
|
|||
return $this->set($args);
|
||||
}
|
||||
|
||||
// ---- OLD
|
||||
// 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.
|
||||
// Return the username associated to an email, FALSE otherwise
|
||||
public function getByEmail($email)
|
||||
{
|
||||
foreach($this->db as $username=>$values) {
|
||||
|
@ -169,7 +157,6 @@ class dbUsers extends dbJSON
|
|||
return $username;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -184,28 +171,37 @@ class dbUsers extends dbJSON
|
|||
return false;
|
||||
}
|
||||
|
||||
// Return TRUE if the user exists, FALSE otherwise.
|
||||
public function userExists($username)
|
||||
{
|
||||
return isset($this->db[$username]);
|
||||
}
|
||||
|
||||
public function generateTokenEmail($username)
|
||||
public function setTokenEmail($username)
|
||||
{
|
||||
// Random hash
|
||||
$token = sha1(Text::randomText(SALT_LENGTH).time());
|
||||
$token = $this->generateEmailToken();
|
||||
$this->db[$username]['tokenEmail'] = $token;
|
||||
|
||||
// Token time to live, defined by TOKEN_EMAIL_TTL
|
||||
$this->db[$username]['tokenEmailTTL'] = Date::currentOffset(DB_DATE_FORMAT, TOKEN_EMAIL_TTL);
|
||||
|
||||
// Save the database
|
||||
if( $this->save() === false ) {
|
||||
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
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
|
||||
if( $dbUsers->userExists($args['new_username']) ) {
|
||||
if( $dbUsers->exists($args['new_username']) ) {
|
||||
Alert::set($Language->g('username-already-exists'), ALERT_STATUS_FAIL);
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -117,7 +117,7 @@ class Login {
|
|||
$this->setLogin($username, $user['role']);
|
||||
|
||||
// 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);
|
||||
|
||||
|
|
|
@ -75,6 +75,7 @@ class Page {
|
|||
return($this->vars!==false);
|
||||
}
|
||||
|
||||
// DEPRACTED
|
||||
// Returns the value from the $field, FALSE if the field doesn't exist
|
||||
public function getField($field)
|
||||
{
|
||||
|
@ -94,6 +95,11 @@ class Page {
|
|||
return false;
|
||||
}
|
||||
|
||||
public function getDB()
|
||||
{
|
||||
return $this->vars;
|
||||
}
|
||||
|
||||
// Set a field with a value
|
||||
public function setField($field, $value, $overwrite=true)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue