improves on dbpages

This commit is contained in:
Diego Najar 2017-05-08 21:26:06 +02:00
parent eef031c89f
commit d3d19f5109
3 changed files with 71 additions and 70 deletions

View File

@ -44,13 +44,13 @@ function updateBludit()
$date = Date::format($page['date'], 'Y-m-d H:i', DB_DATE_FORMAT); $date = Date::format($page['date'], 'Y-m-d H:i', DB_DATE_FORMAT);
if($date !== false) { if($date !== false) {
$dbPages->setPageDb($key, 'date', $date); $dbPages->setField($key, 'date', $date);
} }
// Checksum // Checksum
if( empty($post['md5file']) ) { if( empty($post['md5file']) ) {
$checksum = md5_file(PATH_PAGES.$key.DS.FILENAME); $checksum = md5_file(PATH_PAGES.$key.DS.FILENAME);
$dbPages->setPageDb($key, 'md5file', $checksum); $dbPages->setField($key, 'md5file', $checksum);
} }
} }

View File

@ -50,6 +50,9 @@ define('PATH_ADMIN_VIEWS', PATH_ADMIN.'views'.DS);
define('DEBUG_FILE', PATH_CONTENT.'debug.txt'); define('DEBUG_FILE', PATH_CONTENT.'debug.txt');
// PAGES DATABASE
define('DB_PAGES', PATH_DATABASES.'pages.php');
// Log separator // Log separator
define('LOG_SEP', ' | '); define('LOG_SEP', ' | ');

View File

@ -10,78 +10,77 @@ class dbPages extends dbJSON
'description'=> array('inFile'=>false, 'value'=>''), 'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''), 'username'=> array('inFile'=>false, 'value'=>''),
'tags'=> array('inFile'=>false, 'value'=>array()), 'tags'=> array('inFile'=>false, 'value'=>array()),
'status'=> array('inFile'=>false, 'value'=>'draft'), 'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled
'date'=> array('inFile'=>false, 'value'=>''), 'date'=> array('inFile'=>false, 'value'=>''),
'dateModified'=> array('inFile'=>false, 'value'=>''), 'dateModified'=> array('inFile'=>false, 'value'=>''),
'position'=> array('inFile'=>false, 'value'=>0), 'position'=> array('inFile'=>false, 'value'=>0),
'coverImage'=> array('inFile'=>false, 'value'=>''), 'coverImage'=> array('inFile'=>false, 'value'=>''),
'category'=> array('inFile'=>false, 'value'=>''), 'category'=> array('inFile'=>false, 'value'=>''),
'uuid'=> array('inFile'=>false, 'value'=>'') 'md5file'=> array('inFile'=>false, 'value'=>''),
'uuid'=> array('inFile'=>false, 'value'=>''),
'allowComments'=> array('inFile'=>false, 'value'=>false)
); );
function __construct() function __construct()
{ {
parent::__construct(PATH_DATABASES.'pages.php'); parent::__construct(DB_PAGES);
} }
// Create a new page
public function add($args) public function add($args)
{ {
$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
$key = $this->generateKey($args['slug'], $args['parent']); // The user is always the one loggued
// Generate UUID
$args['uuid'] = md5(time().DOMAIN);
// The user is always the one loggued.
$args['username'] = Session::get('username'); $args['username'] = Session::get('username');
if( Text::isEmpty($args['username']) ) { if( Text::isEmpty($args['username']) ) {
return false; return false;
} }
// Current date. // Generate key
$key = $this->generateKey($args['slug'], $args['parent']);
// Generate UUID
$args['uuid'] = md5( uniqid() );
// Get current date
$args['date'] = Date::current(DB_DATE_FORMAT); $args['date'] = Date::current(DB_DATE_FORMAT);
// Verify arguments with the database fields. foreach($this->dbFields as $field=>$options) {
foreach($this->dbFields as $field=>$options) if( isset($args[$field]) ) {
{
if( isset($args[$field]) )
{
if($field=='tags') { if($field=='tags') {
$tmpValue = $this->generateTags($args['tags']); $value = $this->generateTags($args['tags']);
} }
else { else {
// Sanitize if will be saved on database.
if( !$options['inFile'] ) { if( !$options['inFile'] ) {
$tmpValue = Sanitize::html($args[$field]); // Sanitize if will be stored on database
$value = Sanitize::html($args[$field]);
} }
else { else {
$tmpValue = $args[$field]; $value = $args[$field];
} }
} }
} }
// Default value for the field. else {
else // Default value for the field
{ $value = $options['value'];
$tmpValue = $options['value'];
} }
// Check where the field will be written, in file or database. // Where the data is stored
if($options['inFile']) { if($options['inFile']) {
$dataForFile[$field] = Text::firstCharUp($field).': '.$tmpValue; $dataForFile[$field] = Text::firstCharUp($field).': '.$value;
} }
else else {
{
// Set type // Set type
settype($tmpValue, gettype($options['value'])); settype($value, gettype($options['value']));
// Save on database // Save on database
$dataForDb[$field] = $tmpValue; $dataForDb[$field] = $value;
} }
} }
// Make the directory. Recursive. // Create the directory
if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) { if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_PAGES.$key); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_PAGES.$key);
return false; return false;
@ -90,7 +89,7 @@ class dbPages extends dbJSON
// 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 index.txt'); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
return false; return false;
} }
@ -109,15 +108,15 @@ class dbPages extends dbJSON
$dataForDb = array(); $dataForDb = array();
$dataForFile = array(); $dataForFile = array();
$newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']); // The user is always the one loggued
// The user is always the one loggued.
$args['username'] = Session::get('username'); $args['username'] = Session::get('username');
if( Text::isEmpty($args['username']) ) { if( Text::isEmpty($args['username']) ) {
return false; return false;
} }
// If the page is draft then the time created is now. $newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']);
// If the page is draft then the time created is now
if( $this->db[$args['key']]['status']=='draft' ) { if( $this->db[$args['key']]['status']=='draft' ) {
$args['date'] = Date::current(DB_DATE_FORMAT); $args['date'] = Date::current(DB_DATE_FORMAT);
} }
@ -125,50 +124,47 @@ class dbPages extends dbJSON
$args['date'] = $this->db[$args['key']]['date']; $args['date'] = $this->db[$args['key']]['date'];
} }
// Current UUID
$args['uuid'] = $this->db[$args['key']]['uuid'];
// Modified date // Modified date
$args['dateModified'] = Date::current(DB_DATE_FORMAT); $args['dateModified'] = Date::current(DB_DATE_FORMAT);
// Verify arguments with the database fields. foreach($this->dbFields as $field=>$options) {
foreach($this->dbFields as $field=>$options) if( isset($args[$field]) ) {
{
if( isset($args[$field]) )
{
if($field=='tags') { if($field=='tags') {
$tmpValue = $this->generateTags($args['tags']); $value = $this->generateTags($args['tags']);
} }
else { else {
// Sanitize if will be saved on database.
if( !$options['inFile'] ) { if( !$options['inFile'] ) {
$tmpValue = Sanitize::html($args[$field]); // Sanitize if will be stored on database
$value = Sanitize::html($args[$field]);
} }
else { else {
$tmpValue = $args[$field]; // Default value for the field
$value = $args[$field];
} }
} }
} }
// Default value for the field. else {
else $value = $options['value'];
{
$tmpValue = $options['value'];
} }
// Check where the field will be written, if in the file or in the database. // Where the data is stored
if($options['inFile']) { if($options['inFile']) {
$dataForFile[$field] = Text::firstCharUp($field).': '.$tmpValue; $dataForFile[$field] = Text::firstCharUp($field).': '.$value;
} }
else else {
{
// Set type // Set type
settype($tmpValue, gettype($options['value'])); settype($value, gettype($options['value']));
// Save on database // Save on database
$dataForDb[$field] = $tmpValue; $dataForDb[$field] = $value;
} }
} }
// Move the directory from old key to new key. // Move the directory from old key to new key.
if($newKey!==$args['key']) if($newKey!==$args['key']) {
{
if( Filesystem::mv(PATH_PAGES.$args['key'], PATH_PAGES.$newKey) === false ) { 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); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey);
return false; return false;
@ -178,11 +174,11 @@ class dbPages extends dbJSON
// 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.$newKey.DS.FILENAME, $data) === false ) { 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 index.txt'); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
return false; return false;
} }
// Remove the old key. // Remove the old key
unset( $this->db[$args['key']] ); unset( $this->db[$args['key']] );
// Save the database // Save the database
@ -197,22 +193,22 @@ class dbPages extends dbJSON
public function delete($key) public function delete($key)
{ {
// Page doesn't exist in database. // Page doesn't exist in database
if(!$this->pageExists($key)) { if(!$this->pageExists($key)) {
Log::set(__METHOD__.LOG_SEP.'The page does not exist. Key: '.$key); Log::set(__METHOD__.LOG_SEP.'The page does not exist. Key: '.$key);
} }
// Delete the index.txt file. // Delete the index.txt file
if( Filesystem::rmfile(PATH_PAGES.$key.DS.FILENAME) === false ) { if( Filesystem::rmfile(PATH_PAGES.$key.DS.FILENAME) === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the file index.txt'); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the file '.FILENAME);
} }
// Delete the directory. // Delete the directory
if( Filesystem::rmdir(PATH_PAGES.$key) === false ) { if( Filesystem::rmdir(PATH_PAGES.$key) === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the directory '.PATH_PAGES.$key); Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the directory '.PATH_PAGES.$key);
} }
// Remove from database. // Remove from database
unset($this->db[$key]); unset($this->db[$key]);
// Save the database. // Save the database.
@ -233,9 +229,11 @@ class dbPages extends dbJSON
return false; return false;
} }
public function setPageDb($key, $field, $value) // Set a field of the database
public function setField($key, $field, $value)
{ {
if( $this->pageExists($key) ) { if( $this->pageExists($key) ) {
settype($value, gettype($this->dbFields[$key]['value']));
$this->db[$key][$field] = $value; $this->db[$key][$field] = $value;
} }