bludit/bl-kernel/dbpages.class.php

695 lines
16 KiB
PHP
Raw Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
class dbPages extends dbJSON
{
private $parentKeyList = array();
private $dbFields = array(
2015-08-17 02:24:22 +02:00
'title'=> array('inFile'=>true, 'value'=>''),
'content'=> array('inFile'=>true, 'value'=>''),
2015-05-05 03:00:01 +02:00
'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''),
2015-09-18 05:24:10 +02:00
'tags'=> array('inFile'=>false, 'value'=>array()),
2017-05-08 21:26:06 +02:00
'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled
2015-11-13 02:25:59 +01:00
'date'=> array('inFile'=>false, 'value'=>''),
2016-05-29 02:54:18 +02:00
'dateModified'=> array('inFile'=>false, 'value'=>''),
'position'=> array('inFile'=>false, 'value'=>0),
2017-04-26 18:56:10 +02:00
'coverImage'=> array('inFile'=>false, 'value'=>''),
2017-04-26 21:26:17 +02:00
'category'=> array('inFile'=>false, 'value'=>''),
2017-05-08 21:26:06 +02:00
'md5file'=> array('inFile'=>false, 'value'=>''),
'uuid'=> array('inFile'=>false, 'value'=>''),
'allowComments'=> array('inFile'=>false, 'value'=>false)
2015-05-05 03:00:01 +02:00
);
function __construct()
{
2017-05-08 21:26:06 +02:00
parent::__construct(DB_PAGES);
2015-05-05 03:00:01 +02:00
}
2017-05-08 21:26:06 +02:00
// Create a new page
2015-05-05 03:00:01 +02:00
public function add($args)
{
$dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file
2017-05-08 21:26:06 +02:00
// The user is always the one loggued
2015-05-05 03:00:01 +02:00
$args['username'] = Session::get('username');
2015-05-31 03:06:55 +02:00
if( Text::isEmpty($args['username']) ) {
2015-05-05 03:00:01 +02:00
return false;
}
2017-05-08 21:26:06 +02:00
// Generate key
$key = $this->generateKey($args['slug'], $args['parent']);
// Generate UUID
$args['uuid'] = md5( uniqid() );
// Get current date
2015-11-13 02:25:59 +01:00
$args['date'] = Date::current(DB_DATE_FORMAT);
2015-05-05 03:00:01 +02:00
2017-05-08 21:26:06 +02:00
foreach($this->dbFields as $field=>$options) {
if( isset($args[$field]) ) {
2015-09-18 05:24:10 +02:00
if($field=='tags') {
2017-05-08 21:26:06 +02:00
$value = $this->generateTags($args['tags']);
2015-05-05 03:00:01 +02:00
}
else {
2015-09-18 05:24:10 +02:00
if( !$options['inFile'] ) {
2017-05-08 21:26:06 +02:00
// Sanitize if will be stored on database
$value = Sanitize::html($args[$field]);
2015-09-18 05:24:10 +02:00
}
else {
2017-05-08 21:26:06 +02:00
$value = $args[$field];
2015-09-18 05:24:10 +02:00
}
2015-05-05 03:00:01 +02:00
}
}
2017-05-08 21:26:06 +02:00
else {
// Default value for the field
$value = $options['value'];
2015-05-05 03:00:01 +02:00
}
2017-05-08 21:26:06 +02:00
// Where the data is stored
2015-05-05 03:00:01 +02:00
if($options['inFile']) {
2017-05-08 21:26:06 +02:00
$dataForFile[$field] = Text::firstCharUp($field).': '.$value;
2015-05-05 03:00:01 +02:00
}
2017-05-08 21:26:06 +02:00
else {
2015-05-05 03:00:01 +02:00
// Set type
2017-05-08 21:26:06 +02:00
settype($value, gettype($options['value']));
2015-05-05 03:00:01 +02:00
// Save on database
2017-05-08 21:26:06 +02:00
$dataForDb[$field] = $value;
2015-05-05 03:00:01 +02:00
}
}
2017-05-08 21:26:06 +02:00
// Create the directory
2015-05-31 03:06:55 +02:00
if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) {
2015-05-05 03:00:01 +02:00
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);
2016-07-26 01:40:51 +02:00
if( file_put_contents(PATH_PAGES.$key.DS.FILENAME, $data) === false ) {
2017-05-08 21:26:06 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
2015-05-05 03:00:01 +02:00
return false;
}
// Save the database
$this->db[$key] = $dataForDb;
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
2016-01-08 00:43:09 +01:00
return $key;
2015-05-05 03:00:01 +02:00
}
public function edit($args)
{
$dataForDb = array();
$dataForFile = array();
2017-05-08 21:26:06 +02:00
// The user is always the one loggued
2015-05-05 03:00:01 +02:00
$args['username'] = Session::get('username');
2015-05-31 03:06:55 +02:00
if( Text::isEmpty($args['username']) ) {
2015-05-05 03:00:01 +02:00
return false;
}
2017-05-08 21:26:06 +02:00
$newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']);
// If the page is draft then the time created is now
2015-08-22 18:33:33 +02:00
if( $this->db[$args['key']]['status']=='draft' ) {
2015-08-26 05:42:32 +02:00
$args['date'] = Date::current(DB_DATE_FORMAT);
2015-08-22 18:33:33 +02:00
}
else {
2015-08-26 05:42:32 +02:00
$args['date'] = $this->db[$args['key']]['date'];
2015-08-22 18:33:33 +02:00
}
2015-05-05 03:00:01 +02:00
2017-05-08 21:26:06 +02:00
// Current UUID
$args['uuid'] = $this->db[$args['key']]['uuid'];
2016-05-29 02:54:18 +02:00
// Modified date
$args['dateModified'] = Date::current(DB_DATE_FORMAT);
2017-05-08 21:26:06 +02:00
foreach($this->dbFields as $field=>$options) {
if( isset($args[$field]) ) {
2015-09-18 05:24:10 +02:00
if($field=='tags') {
2017-05-08 21:26:06 +02:00
$value = $this->generateTags($args['tags']);
2015-05-05 03:00:01 +02:00
}
else {
2015-09-18 05:24:10 +02:00
if( !$options['inFile'] ) {
2017-05-08 21:26:06 +02:00
// Sanitize if will be stored on database
$value = Sanitize::html($args[$field]);
2015-09-18 05:24:10 +02:00
}
else {
2017-05-08 21:26:06 +02:00
// Default value for the field
$value = $args[$field];
2015-09-18 05:24:10 +02:00
}
2015-05-05 03:00:01 +02:00
}
}
2017-05-08 21:26:06 +02:00
else {
$value = $options['value'];
2015-05-05 03:00:01 +02:00
}
2017-05-08 21:26:06 +02:00
// Where the data is stored
2015-05-05 03:00:01 +02:00
if($options['inFile']) {
2017-05-08 21:26:06 +02:00
$dataForFile[$field] = Text::firstCharUp($field).': '.$value;
2015-05-05 03:00:01 +02:00
}
2017-05-08 21:26:06 +02:00
else {
2015-05-05 03:00:01 +02:00
// Set type
2017-05-08 21:26:06 +02:00
settype($value, gettype($options['value']));
2015-05-05 03:00:01 +02:00
// Save on database
2017-05-08 21:26:06 +02:00
$dataForDb[$field] = $value;
2015-05-05 03:00:01 +02:00
}
}
2015-06-26 06:31:53 +02:00
// Move the directory from old key to new key.
2017-05-08 21:26:06 +02:00
if($newKey!==$args['key']) {
2015-05-31 03:06:55 +02:00
if( Filesystem::mv(PATH_PAGES.$args['key'], PATH_PAGES.$newKey) === false ) {
2015-05-05 03:00:01 +02:00
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 ) {
2017-05-08 21:26:06 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME);
2015-05-05 03:00:01 +02:00
return false;
}
2017-05-08 21:26:06 +02:00
// Remove the old key
unset( $this->db[$args['key']] );
2015-05-05 03:00:01 +02:00
// Save the database
$this->db[$newKey] = $dataForDb;
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
2016-01-08 00:43:09 +01:00
return $newKey;
2015-05-05 03:00:01 +02:00
}
public function delete($key)
{
2017-05-08 21:26:06 +02:00
// Page doesn't exist in database
2015-05-05 03:00:01 +02:00
if(!$this->pageExists($key)) {
Log::set(__METHOD__.LOG_SEP.'The page does not exist. Key: '.$key);
}
2017-05-08 21:26:06 +02:00
// Delete the index.txt file
if( Filesystem::rmfile(PATH_PAGES.$key.DS.FILENAME) === false ) {
2017-05-08 21:26:06 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the file '.FILENAME);
2015-05-05 03:00:01 +02:00
}
2017-05-08 21:26:06 +02:00
// Delete the directory
2015-05-31 03:06:55 +02:00
if( Filesystem::rmdir(PATH_PAGES.$key) === false ) {
2015-05-05 03:00:01 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the directory '.PATH_PAGES.$key);
}
2017-05-08 21:26:06 +02:00
// Remove from database
2015-05-05 03:00:01 +02:00
unset($this->db[$key]);
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
}
return true;
}
2017-05-10 20:40:28 +02:00
// Returns a database with published pages
public function getPublishedDB()
{
$tmp = $this->db;
foreach($tmp as $key=>$fields) {
if($fields['status']!='published') {
unset($tmp[$key]);
}
}
return $tmp;
}
2015-05-05 03:00:01 +02:00
// Return an array with the database for a page, FALSE otherwise.
2016-01-08 00:43:09 +01:00
public function getPageDB($key)
2015-05-05 03:00:01 +02:00
{
2017-05-08 21:26:06 +02:00
if( $this->pageExists($key) ) {
2015-05-05 03:00:01 +02:00
return $this->db[$key];
}
return false;
}
2017-05-10 20:40:28 +02:00
// Returns an array with a list of pages
// (int) $pageNumber, the page number
// (int) $amountOfItems, amount of items to return
// (boolean) $onlyPublished, TRUE to return only published pages
public function getList($pageNumber, $amountOfItems, $onlyPublished=true)
{
$db = $this->db;
if( $onlyPublished ) {
$db = $this->getPublishedDB();
}
$total = count($db);
$init = (int) $amountOfItems * $pageNumber;
$end = (int) min( ($init + $amountOfItems - 1), $total );
$outrange = $init<0 ? true : $init>$end;
if(!$outrange) {
return array_slice($db, $init, $amountOfItems, true);
}
return array();
}
// Returns the amount of pages
// (boolean) $total, TRUE returns the total of pages
// (boolean) $total, FALSE returns the total of published pages (without draft and scheduled)
public function numberPages($onlyPublished=true)
{
if( $onlyPublished ) {
$db = $this->getPublishedDB();
return count($db);
}
return count($this->db);
}
// ----- OLD
2017-05-08 21:26:06 +02:00
// Set a field of the database
public function setField($key, $field, $value)
2016-01-29 17:07:29 +01:00
{
2017-05-08 21:26:06 +02:00
if( $this->pageExists($key) ) {
settype($value, gettype($this->dbFields[$key]['value']));
2016-01-29 17:07:29 +01:00
$this->db[$key][$field] = $value;
}
return false;
}
2015-05-05 03:00:01 +02:00
// Return TRUE if the page exists, FALSE otherwise.
public function pageExists($key)
{
return isset($this->db[$key]);
}
public function parentKeyList()
{
return $this->parentKeyList;
}
public function parentKeyExists($key)
{
2017-05-08 21:26:06 +02:00
return isset( $this->parentKeyList[$key] );
2015-05-05 03:00:01 +02:00
}
public function addParentKey($key)
{
$this->parentKeyList[$key] = $key;
}
// Generate a valid Key/Slug.
public function generateKey($text, $parent=NO_PARENT_CHAR, $returnSlug=false, $oldKey='')
{
2015-05-31 03:06:55 +02:00
if(Text::isEmpty($text)) {
2015-05-05 03:00:01 +02:00
$text = 'empty';
}
2015-05-31 03:06:55 +02:00
if( Text::isEmpty($parent) || ($parent==NO_PARENT_CHAR) ) {
$newKey = Text::cleanUrl($text);
2015-05-05 03:00:01 +02:00
}
else {
2015-05-31 03:06:55 +02:00
$newKey = Text::cleanUrl($parent).'/'.Text::cleanUrl($text);
2015-05-05 03:00:01 +02:00
}
2017-05-08 22:01:16 +02:00
if($newKey!==$oldKey) {
// Verify if the key is already been used
if( isset($this->db[$newKey]) ) {
2015-05-31 03:06:55 +02:00
if( !Text::endsWithNumeric($newKey) ) {
2015-05-19 01:22:05 +02:00
$newKey = $newKey.'-0';
}
2015-05-05 03:00:01 +02:00
2015-05-19 01:22:05 +02:00
while( isset($this->db[$newKey]) ) {
$newKey++;
}
2015-05-05 03:00:01 +02:00
}
}
2017-05-08 22:01:16 +02:00
if($returnSlug) {
2015-05-05 03:00:01 +02:00
$explode = explode('/', $newKey);
if(isset($explode[1])) {
return $explode[1];
}
return $explode[0];
}
return $newKey;
}
2016-01-08 00:43:09 +01:00
// Returns the database
public function getDB()
2015-05-05 03:00:01 +02:00
{
return $this->db;
}
2015-09-18 05:24:10 +02:00
// Returns an Array, array('tagSlug'=>'tagName')
// (string) $tags, tag list separeted by comma.
public function generateTags($tags)
{
$tmp = array();
$tags = trim($tags);
if(empty($tags)) {
return $tmp;
}
// Make array
$tags = explode(',', $tags);
foreach($tags as $tag)
{
$tag = trim($tag);
$tagKey = Text::cleanUrl($tag);
$tmp[$tagKey] = $tag;
}
return $tmp;
}
2017-04-17 12:49:03 +02:00
// Change all posts with the old category key for the new category key
public function changeCategory($oldCategoryKey, $newCategoryKey)
{
foreach($this->db as $key=>$value) {
if($value['category']==$oldCategoryKey) {
$this->db[$key]['category'] = $newCategoryKey;
}
}
// Save database
return $this->save();
}
2015-10-31 18:51:40 +01:00
public function count()
{
$count = parent::count();
// DEBUG: Less than - 1 because the error page.
return $count - 1;
}
2017-05-08 22:20:53 +02:00
// Return TRUE if there are new pages published, FALSE otherwise.
public function scheduler()
{
// Get current date
$currentDate = Date::current(DB_DATE_FORMAT);
$saveDatabase = false;
2017-05-09 00:24:15 +02:00
// The database need to be sorted by date
foreach($this->db as $pageKey=>$fields) {
if($fields['status']=='scheduled') {
if($fields['date']<=$currentDate) {
$this->db[$pageKey]['status'] = 'published';
2017-05-08 22:20:53 +02:00
$saveDatabase = true;
}
}
2017-05-09 00:24:15 +02:00
elseif($fields['status']=='published') {
2017-05-08 22:20:53 +02:00
break;
}
}
if($saveDatabase) {
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
2017-05-09 00:24:15 +02:00
Log::set(__METHOD__.LOG_SEP.'New pages published from the scheduler.');
2017-05-08 22:20:53 +02:00
return true;
}
return false;
}
// --- OLD
public function cliMode()
{
// LOG
Log::set('CLI MODE - PAGES - Starting...');
$pageList = array();
$pagesDirectories = Filesystem::listDirectories(PATH_PAGES);
foreach( $pagesDirectories as $directory ) {
if( Sanitize::pathFile($directory.DS.FILENAME) ) {
// The key is the directory name
$key = basename($directory);
// Add the page key to the list
$pageList[$key] = true;
// LOG
Log::set('CLI MODE - Page found, key: '.$key);
// Search sub-pages
$subPaths = Filesystem::listDirectories($directory.DS);
foreach( $subPaths as $subDirectory )
{
// The key of the sub-page
$subKey = basename($subDirectory);
if( Sanitize::pathFile($subDirectory.DS.FILENAME) ) {
// Add the key of the sub-page, the key is composed by the directory/subdirectory
$pageList[$key.'/'.$subKey] = true;
// LOG
Log::set('CLI MODE - Page found, key: '.$key);
}
}
}
}
foreach( $pageList as $key=>$value ) {
if( !isset($this->db[$key]) ) {
// LOG
Log::set('CLI MODE - The page is not in the database, key: '.$key);
// Insert new post
$this->cliModeInsert($key);
}
else {
$checksum = md5_file(PATH_PAGES.$key.DS.FILENAME);
// If checksum is different, update the post
2016-07-25 16:56:43 +02:00
if( !isset($this->db[$key]['md5file']) ||
$this->db[$key]['md5file']!==$checksum ) {
// LOG
Log::set('CLI MODE - Different md5 checksum, key: '.$key);
// Update the post
$this->cliModeInsert($key, $update=true);
}
}
}
// LOG
Log::set('CLI MODE - Cleaning database...');
foreach( array_diff_key($this->db, $pageList) as $key=>$data ) {
// LOG
Log::set('CLI MODE - Removing page from database, key: '.$key);
// Remove the page from database
unset( $this->db[$key] );
}
// Save the database
$this->save();
// LOG
Log::set('CLI MODE - PAGES - Finishing...');
return true;
}
private function cliModeInsert($key, $update=false)
{
if($update) {
// LOG
Log::set('CLI MODE - cliModeInsert() - Updating the page, key: '.$key);
// Database from the current database
$dataForDb = $this->db[$key];
$dataForDb['dateModified'] = Date::current(DB_DATE_FORMAT);
}
else {
// LOG
Log::set('CLI MODE - cliModeInsert() - Inserting the new post, key: '.$key);
// Database for the new page, fields with the default values
$dataForDb = array();
foreach( $this->dbFields as $field=>$options ) {
if( !$options['inFile'] ) {
$dataForDb[$field] = $options['value'];
}
}
// Fields and value predefined in init.php
$dataForDb['username'] = CLI_USERNAME;
$dataForDb['status'] = CLI_STATUS;
$dataForDb['date'] = Date::current(DB_DATE_FORMAT);
}
// MD5 checksum
$dataForDb['md5file'] = md5_file(PATH_PAGES.$key.DS.FILENAME);
// Generate the Object from the file
$Page = new Page($key);
foreach( $this->dbFields as $field=>$options ) {
if( !$options['inFile'] ) {
// Get the field from the file
// If the field doesn't exist, the function returns FALSE
$data = $Page->getField($field);
if( $data!==false ) {
$tmpValue = '';
if( $field=='tags' ) {
$tmpValue = $this->generateTags($data);
}
elseif( $field=='date' ) {
// Validate format date from file
if( Valid::date($data, DB_DATE_FORMAT) ) {
$tmpValue = $data;
}
}
else {
$tmpValue = Sanitize::html($data);
}
settype($tmpValue, gettype($options['value']));
$dataForDb[$field] = $tmpValue;
}
}
}
// Insert row in the database
$this->db[$key] = $dataForDb;
return true;
}
2015-08-31 03:18:06 +02:00
public function regenerateCli()
2015-08-26 05:42:32 +02:00
{
$db = $this->db;
$newPaths = array();
$fields = array();
// Default fields and value
foreach($this->dbFields as $field=>$options) {
if(!$options['inFile']) {
$fields[$field] = $options['value'];
}
}
2015-09-15 01:07:15 +02:00
//$tmpPaths = glob(PATH_PAGES.'*', GLOB_ONLYDIR);
$tmpPaths = Filesystem::listDirectories(PATH_PAGES);
2015-08-26 05:42:32 +02:00
foreach($tmpPaths as $directory)
{
$key = basename($directory);
if(file_exists($directory.DS.FILENAME)) {
2015-08-26 05:42:32 +02:00
// The key is the directory name
$newPaths[$key] = true;
}
// Recovery pages from subdirectories
2015-09-15 01:07:15 +02:00
//$subPaths = glob($directory.DS.'*', GLOB_ONLYDIR);
$subPaths = Filesystem::listDirectories($directory.DS);
2015-08-26 05:42:32 +02:00
foreach($subPaths as $subDirectory)
{
$subKey = basename($subDirectory);
if(file_exists($subDirectory.DS.FILENAME)) {
2015-08-26 05:42:32 +02:00
// The key is composed by the directory/subdirectory
$newPaths[$key.'/'.$subKey] = true;
}
}
}
foreach($newPaths as $key=>$value)
{
2015-09-29 03:57:26 +02:00
if(!isset($this->db[$key]))
{
// Default values for the new pages.
$fields['status'] = CLI_STATUS;
$fields['date'] = Date::current(DB_DATE_FORMAT);
$fields['username'] = 'admin';
// Create the entry for the new page.
2015-08-26 05:42:32 +02:00
$this->db[$key] = $fields;
}
$Page = new Page($key);
// Update all fields from FILE to DATABASE.
foreach($fields as $f=>$v)
{
2015-09-20 23:46:50 +02:00
// If the field exists on the FILE, update it.
if($Page->getField($f))
{
$valueFromFile = $Page->getField($f);
if($f=='tags') {
// Generate tags array.
$this->db[$key]['tags'] = $this->generateTags($valueFromFile);
}
elseif($f=='date') {
// Validate Date from file
if(Valid::date($valueFromFile, DB_DATE_FORMAT)) {
$this->db[$key]['date'] = $valueFromFile;
}
}
else {
// Sanitize the values from file.
$this->db[$key][$f] = Sanitize::html($valueFromFile);
}
2015-08-26 05:42:32 +02:00
}
}
}
// Remove old pages from db
foreach( array_diff_key($db, $newPaths) as $key=>$data ) {
unset($this->db[$key]);
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return $this->db!=$db;
}
}