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'=>''),
|
2015-12-31 01:35:28 +01:00
|
|
|
'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'=>''),
|
2017-05-30 20:28:55 +02:00
|
|
|
'allowComments'=> array('inFile'=>false, 'value'=>true)
|
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
|
2017-07-06 23:27:22 +02:00
|
|
|
public function add($args, $climode=false)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
// Generate key
|
2017-07-05 22:55:03 +02:00
|
|
|
$key = $this->generateKey($args['slug'], $args['parent']);
|
2017-05-08 21:26:06 +02:00
|
|
|
|
|
|
|
// Generate UUID
|
2017-06-23 00:41:00 +02:00
|
|
|
$args['uuid'] = $this->generateUUID();
|
2017-05-08 21:26:06 +02:00
|
|
|
|
2017-05-16 00:46:20 +02:00
|
|
|
// Date
|
|
|
|
$currentDate = Date::current(DB_DATE_FORMAT);
|
|
|
|
|
|
|
|
// Validate date
|
2017-07-05 19:59:51 +02:00
|
|
|
if( !Valid::date($args['date'], DB_DATE_FORMAT) ) {
|
2017-05-16 00:46:20 +02:00
|
|
|
$args['date'] = $currentDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule page
|
|
|
|
if( ($args['date']>$currentDate) && ($args['status']=='published') ) {
|
|
|
|
$args['status'] = 'scheduled';
|
|
|
|
}
|
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-07-06 23:27:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
// Checksum MD5
|
|
|
|
$dataForDb['md5file'] = md5_file(PATH_PAGES.$key.DS.FILENAME);
|
|
|
|
|
2017-05-16 00:46:20 +02:00
|
|
|
// Insert in database
|
2015-05-05 03:00:01 +02:00
|
|
|
$this->db[$key] = $dataForDb;
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Sort database
|
2017-05-21 22:01:44 +02:00
|
|
|
$this->sortBy();
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Save database
|
2017-07-05 19:59:51 +02:00
|
|
|
$this->save();
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2016-01-08 00:43:09 +01:00
|
|
|
return $key;
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
public function edit($args, $climode=false)
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
|
|
|
$dataForDb = array();
|
|
|
|
$dataForFile = array();
|
|
|
|
|
2017-07-05 22:55:03 +02:00
|
|
|
$newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']);
|
2015-05-05 03:00:01 +02:00
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
// If the page is draft then the created time is the current
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
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;
|
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
// Checksum MD5
|
|
|
|
$dataForDb['md5file'] = md5_file(PATH_PAGES.$newKey.DS.FILENAME);
|
|
|
|
|
2017-05-16 00:46:20 +02:00
|
|
|
// Insert in database
|
2015-05-05 03:00:01 +02:00
|
|
|
$this->db[$newKey] = $dataForDb;
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Sort database
|
2017-05-21 22:01:44 +02:00
|
|
|
$this->sortBy();
|
2017-05-16 00:46:20 +02:00
|
|
|
|
|
|
|
// Save database
|
2017-07-05 19:59:51 +02:00
|
|
|
$this->save();
|
2015-05-05 03:00:01 +02:00
|
|
|
|
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
|
2017-05-17 00:04:53 +02:00
|
|
|
if(!$this->exists($key)) {
|
2015-05-05 03:00:01 +02:00
|
|
|
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
|
2016-07-17 01:19:10 +02:00
|
|
|
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-06-20 23:56:22 +02:00
|
|
|
// Change a value of a page
|
|
|
|
public function setField($key, $field, $value)
|
|
|
|
{
|
|
|
|
if( $this->exists($key) ) {
|
|
|
|
settype($value, gettype($this->dbFields[$field]['value']));
|
|
|
|
$this->db[$key][$field] = $value;
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setStatus($key, $value)
|
|
|
|
{
|
|
|
|
return $this->setField($key, 'status', $value);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-22 00:21:08 +02:00
|
|
|
// (array) Returns a database with the fixed pages
|
|
|
|
public function getFixedDB()
|
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
|
|
|
foreach($tmp as $key=>$fields) {
|
|
|
|
if($fields['status']!='fixed') {
|
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2017-06-18 22:44:07 +02:00
|
|
|
// Returns a database with drafts pages
|
|
|
|
public function getDraftDB()
|
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
|
|
|
foreach($tmp as $key=>$fields) {
|
|
|
|
if($fields['status']!='draft') {
|
|
|
|
unset($tmp[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $tmp;
|
|
|
|
}
|
|
|
|
|
2017-06-25 22:54:59 +02:00
|
|
|
// Returns a database with drafts pages
|
|
|
|
public function getScheduledDB()
|
|
|
|
{
|
|
|
|
$tmp = $this->db;
|
|
|
|
foreach($tmp as $key=>$fields) {
|
|
|
|
if($fields['status']!='scheduled') {
|
|
|
|
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-17 00:04:53 +02:00
|
|
|
if( $this->exists($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
|
2017-07-10 23:40:46 +02:00
|
|
|
// The database is sorted by date or by position
|
2017-05-10 20:40:28 +02:00
|
|
|
// (int) $pageNumber, the page number
|
|
|
|
// (int) $amountOfItems, amount of items to return
|
|
|
|
// (boolean) $onlyPublished, TRUE to return only published pages
|
2017-06-18 22:44:07 +02:00
|
|
|
public function getList($pageNumber, $amountOfItems, $onlyPublished=true)
|
2017-05-10 20:40:28 +02:00
|
|
|
{
|
|
|
|
$db = $this->db;
|
|
|
|
|
|
|
|
if( $onlyPublished ) {
|
|
|
|
$db = $this->getPublishedDB();
|
|
|
|
}
|
|
|
|
|
2017-06-23 00:41:00 +02:00
|
|
|
// Remove Error page from the list
|
|
|
|
unset($db['error']);
|
|
|
|
|
2017-05-24 00:48:29 +02:00
|
|
|
// The first page number is 1, so the real is 0
|
|
|
|
$realPageNumber = $pageNumber - 1;
|
|
|
|
|
2017-05-10 20:40:28 +02:00
|
|
|
$total = count($db);
|
2017-05-24 00:48:29 +02:00
|
|
|
$init = (int) $amountOfItems * $realPageNumber;
|
2017-05-10 20:40:28 +02:00
|
|
|
$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)
|
2017-05-16 00:46:20 +02:00
|
|
|
public function count($onlyPublished=true)
|
2017-05-10 20:40:28 +02:00
|
|
|
{
|
|
|
|
if( $onlyPublished ) {
|
|
|
|
$db = $this->getPublishedDB();
|
|
|
|
return count($db);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($this->db);
|
|
|
|
}
|
|
|
|
|
2017-07-05 22:55:03 +02:00
|
|
|
// Returns an array with all parents pages key, a parent page is not a child
|
|
|
|
public function getParents()
|
2017-05-16 00:46:20 +02:00
|
|
|
{
|
2017-07-05 22:55:03 +02:00
|
|
|
$db = $this->getPublishedDB();
|
|
|
|
foreach($db as $key=>$fields) {
|
|
|
|
// if the key has slash then is a child
|
2017-05-16 00:46:20 +02:00
|
|
|
if( Text::stringContains($key, '/') ) {
|
|
|
|
unset($db[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $db;
|
|
|
|
}
|
|
|
|
|
2017-06-23 00:41:00 +02:00
|
|
|
// Return TRUE if the page exists, FALSE otherwise
|
2017-05-17 00:04:53 +02:00
|
|
|
public function exists($key)
|
|
|
|
{
|
|
|
|
return isset( $this->db[$key] );
|
|
|
|
}
|
|
|
|
|
2017-05-21 22:01:44 +02:00
|
|
|
public function sortBy()
|
|
|
|
{
|
|
|
|
if( ORDER_BY=='date' ) {
|
|
|
|
return $this->sortByDate(true);
|
|
|
|
} else {
|
|
|
|
return $this->sortByPosition(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort pages by position
|
|
|
|
public function sortByPosition($HighToLow=false)
|
|
|
|
{
|
|
|
|
if($HighToLow) {
|
|
|
|
uasort($this->db, array($this, 'sortByPositionHighToLow'));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uasort($this->db, array($this, 'sortByPositionLowToHigh'));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sortByPositionLowToHigh($a, $b) {
|
|
|
|
return $a['position']>$b['position'];
|
|
|
|
}
|
|
|
|
private function sortByPositionHighToLow($a, $b) {
|
|
|
|
return $a['position']<$b['position'];
|
|
|
|
}
|
|
|
|
|
2017-05-16 00:46:20 +02:00
|
|
|
// Sort pages by date
|
|
|
|
public function sortByDate($HighToLow=true)
|
|
|
|
{
|
|
|
|
if($HighToLow) {
|
2017-05-21 22:01:44 +02:00
|
|
|
uasort($this->db, array($this, 'sortByDateHighToLow'));
|
2017-05-16 00:46:20 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-05-21 22:01:44 +02:00
|
|
|
uasort($this->db, array($this, 'sortByDateLowToHigh'));
|
2017-05-16 00:46:20 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-21 22:01:44 +02:00
|
|
|
private function sortByDateLowToHigh($a, $b) {
|
2017-05-16 00:46:20 +02:00
|
|
|
return $a['date']>$b['date'];
|
|
|
|
}
|
2017-05-21 22:01:44 +02:00
|
|
|
private function sortByDateHighToLow($a, $b) {
|
2017-05-16 00:46:20 +02:00
|
|
|
return $a['date']<$b['date'];
|
|
|
|
}
|
|
|
|
|
2017-06-23 00:41:00 +02:00
|
|
|
private function generateUUID() {
|
|
|
|
return md5( uniqid().time() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns TRUE if there are new pages published, FALSE otherwise
|
|
|
|
public function scheduler()
|
|
|
|
{
|
|
|
|
// Get current date
|
|
|
|
$currentDate = Date::current(DB_DATE_FORMAT);
|
|
|
|
$saveDatabase = false;
|
|
|
|
|
|
|
|
// 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';
|
|
|
|
$saveDatabase = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif( ($fields['status']=='published') && (ORDER_BY=='date') ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($saveDatabase) {
|
|
|
|
if( $this->save() === false ) {
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::set(__METHOD__.LOG_SEP.'New pages published from the scheduler.');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
// Generate a valid Key/Slug
|
2017-07-05 22:55:03 +02:00
|
|
|
public function generateKey($text, $parent=false, $returnSlug=false, $oldKey='')
|
2015-05-05 03:00:01 +02:00
|
|
|
{
|
2015-05-31 03:06:55 +02:00
|
|
|
if(Text::isEmpty($text)) {
|
2015-05-05 03:00:01 +02:00
|
|
|
$text = 'empty';
|
|
|
|
}
|
|
|
|
|
2017-07-05 22:55:03 +02:00
|
|
|
if( empty($parent) ) {
|
2015-05-31 03:06:55 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2017-07-05 19:59:51 +02:00
|
|
|
public function rescanClimode()
|
|
|
|
{
|
2017-07-06 23:27:22 +02:00
|
|
|
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) {
|
2017-07-05 19:59:51 +02:00
|
|
|
if( Sanitize::pathFile($directory.DS.FILENAME) ) {
|
|
|
|
$pageKey = basename($directory);
|
2017-07-06 23:27:22 +02:00
|
|
|
$pageList[$pageKey] = true;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
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);
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// New page
|
|
|
|
if( !isset($this->db[$pageKey]) ) {
|
|
|
|
$this->verifyFieldsClimode($pageKey, true);
|
|
|
|
}
|
|
|
|
// Update page
|
|
|
|
elseif($this->db[$pageKey]['md5file']!=$checksum) {
|
|
|
|
$this->verifyFieldsClimode($pageKey, false);
|
2017-07-05 19:59:51 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 23:27:22 +02:00
|
|
|
|
|
|
|
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();
|
2017-07-05 19:59:51 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
private function verifyFieldsClimode($key, $insert=true)
|
2017-07-05 19:59:51 +02:00
|
|
|
{
|
2017-07-06 23:27:22 +02:00
|
|
|
$page = new Page($key);
|
|
|
|
$db = $page->getDB();
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// Content from file
|
|
|
|
$db['content'] = $db['contentRaw'];
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// Parent
|
|
|
|
$db['parent'] = '';
|
|
|
|
$db['slug'] = $key;
|
|
|
|
$explodeKey = explode('/', $key);
|
|
|
|
if(isset($explodeKey[1])) {
|
|
|
|
$db['parent'] = $explodeKey[0];
|
|
|
|
$db['slug'] = $explodeKey[1];
|
|
|
|
}
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// Date
|
|
|
|
if( !isset($db['date']) ) {
|
|
|
|
$db['date'] = Date::current(DB_DATE_FORMAT);
|
|
|
|
}
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// Status
|
|
|
|
if( !isset($db['status']) ) {
|
|
|
|
$db['status'] = CLI_STATUS;
|
|
|
|
}
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// Owner username
|
|
|
|
if( !isset($db['username']) ) {
|
|
|
|
$db['username'] = CLI_USERNAME;
|
|
|
|
}
|
2017-07-05 19:59:51 +02:00
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 19:59:51 +02:00
|
|
|
|
|
|
|
|
2017-07-06 23:27:22 +02:00
|
|
|
// ----- OLD
|
2017-07-05 19:59:51 +02:00
|
|
|
|
|
|
|
public function parentKeyList()
|
|
|
|
{
|
|
|
|
return $this->parentKeyList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parentKeyExists($key)
|
|
|
|
{
|
|
|
|
return isset( $this->parentKeyList[$key] );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addParentKey($key)
|
|
|
|
{
|
|
|
|
$this->parentKeyList[$key] = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-05-08 22:20:53 +02:00
|
|
|
|
|
|
|
|
2017-06-23 00:41:00 +02:00
|
|
|
}
|