bludit/bl-kernel/dbposts.class.php

562 lines
13 KiB
PHP
Raw Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
class dbPosts extends dbJSON
{
private $dbFields = array(
2015-08-22 18:33:33 +02:00
'title'=> array('inFile'=>true, 'value'=>''),
'content'=> array('inFile'=>true, 'value'=>''),
'description'=> array('inFile'=>false, 'value'=>''),
'username'=> array('inFile'=>false, 'value'=>''),
2015-09-02 02:42:21 +02:00
'status'=> array('inFile'=>false, 'value'=>'draft'), // published, draft, scheduled
2015-09-18 05:24:10 +02:00
'tags'=> array('inFile'=>false, 'value'=>array()),
2016-05-07 05:10:10 +02:00
'allowComments'=> array('inFile'=>false, 'value'=>0),
'date'=> array('inFile'=>false, 'value'=>''),
2016-05-29 02:54:18 +02:00
'dateModified'=> array('inFile'=>false, 'value'=>''),
'coverImage'=> array('inFile'=>false, 'value'=>''),
'md5file'=> array('inFile'=>false, 'value'=>'')
2015-05-05 03:00:01 +02:00
);
function __construct()
{
parent::__construct(PATH_DATABASES.'posts.php');
2015-07-20 05:14:12 +02:00
}
2016-06-17 19:08:08 +02:00
// Return the amount of posts
// $total = TRUE, returns the total of posts
// $total = FALSE, return the amount of published posts
2015-07-20 05:14:12 +02:00
public function numberPost($total=false)
{
// Amount of total posts, published, scheduled and draft
2015-07-20 05:14:12 +02:00
if($total) {
2016-06-17 19:08:08 +02:00
return count($this->db);
}
// Amount of published posts
$i = 0;
foreach($this->db as $values) {
if($values['status']=='published') {
$i++;
}
2015-07-20 05:14:12 +02:00
}
2016-06-17 19:08:08 +02:00
return $i;
2015-05-05 03:00:01 +02:00
}
2016-01-08 00:43:09 +01:00
// Returns the database
public function getDB()
{
return $this->db;
}
2015-07-22 05:15:02 +02:00
// Return an array with the post's database, FALSE otherwise.
2016-01-08 00:43:09 +01:00
public function getPostDB($key)
2015-05-05 03:00:01 +02:00
{
2015-07-07 00:22:03 +02:00
if($this->postExists($key)) {
return $this->db[$key];
}
return false;
}
2016-01-08 00:43:09 +01:00
public function setPostDb($key, $field, $value)
2015-08-26 05:42:32 +02:00
{
if($this->postExists($key)) {
$this->db[$key][$field] = $value;
}
return false;
}
2015-07-07 00:22:03 +02:00
// Return TRUE if the post exists, FALSE otherwise.
public function postExists($key)
{
return isset( $this->db[$key] );
2015-05-05 03:00:01 +02:00
}
// Generate a valid Key/Slug.
public function generateKey($text, $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
$newKey = Text::cleanUrl($text);
2015-05-05 03:00:01 +02:00
if($newKey===$oldKey) {
return $newKey;
}
// 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-05 03:00:01 +02:00
$newKey = $newKey.'-0';
}
while( isset($this->db[$newKey]) ) {
$newKey++;
}
}
return $newKey;
}
public function add($args)
{
$dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file
// Current date, format of DB_DATE_FORMAT
2015-09-02 02:42:21 +02:00
$currentDate = Date::current(DB_DATE_FORMAT);
2015-05-05 03:00:01 +02:00
// Generate the database key / index
2015-05-05 03:00:01 +02:00
$key = $this->generateKey($args['slug']);
// The user is always who is 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']) ) {
Log::set(__METHOD__.LOG_SEP.'Session username doesnt exists.');
2015-05-05 03:00:01 +02:00
return false;
}
2016-05-07 05:10:10 +02:00
// If the date is not valid, then set the current date.
2015-08-26 05:42:32 +02:00
if(!Valid::date($args['date'], DB_DATE_FORMAT)) {
2015-09-02 02:42:21 +02:00
$args['date'] = $currentDate;
}
2016-01-14 05:42:18 +01:00
// Schedule post ?
2015-09-02 02:42:21 +02:00
if( ($args['date']>$currentDate) && ($args['status']=='published') ) {
$args['status'] = 'scheduled';
2015-05-05 03:00:01 +02:00
}
// Verify arguments with the database fields.
foreach($this->dbFields as $field=>$options)
{
// If the field is in the arguments
2015-05-05 03:00:01 +02:00
if( isset($args[$field]) )
{
2015-09-18 05:24:10 +02:00
if($field=='tags') {
$tmpValue = $this->generateTags($args['tags']);
2015-05-05 03:00:01 +02:00
}
else {
2015-09-18 05:24:10 +02:00
// Sanitize if will be saved on database.
if( !$options['inFile'] ) {
$tmpValue = Sanitize::html($args[$field]);
}
else {
$tmpValue = $args[$field];
}
2015-05-05 03:00:01 +02:00
}
}
// Set a default value if not in the arguments
2015-05-05 03:00:01 +02:00
else
{
$tmpValue = $options['value'];
}
// Check where the field will be written, in the file or in the database
2015-05-05 03:00:01 +02:00
if($options['inFile']) {
2015-05-31 03:06:55 +02:00
$dataForFile[$field] = Text::firstCharUp($field).': '.$tmpValue;
2015-05-05 03:00:01 +02:00
}
else
{
// Set type
settype($tmpValue, gettype($options['value']));
// Save on database
$dataForDb[$field] = $tmpValue;
}
}
// Make the directory.
2015-05-31 03:06:55 +02:00
if( Filesystem::mkdir(PATH_POSTS.$key) === false ) {
2015-05-05 03:00:01 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_POSTS.$key);
return false;
}
// Make the index.txt and save the file.
$data = implode("\n", $dataForFile);
if( file_put_contents(PATH_POSTS.$key.DS.FILENAME, $data) === false ) {
2015-05-05 03:00:01 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file index.txt');
return false;
}
// Calculate the checksum of the file
$dataForDb['md5file'] = md5_file(PATH_POSTS.$key.DS.FILENAME);
2015-05-05 03:00:01 +02:00
// Save the database
$this->db[$key] = $dataForDb;
2015-09-02 02:42:21 +02:00
// Sort posts before save
2015-09-02 02:42:21 +02:00
$this->sortByDate();
2015-05-05 03:00:01 +02:00
if( $this->save() === false ) {
// Trying to rollback
Log::set(__METHOD__.LOG_SEP.'Rollback...');
Filesystem::rmfile(PATH_POSTS.$key.DS.FILENAME);
Filesystem::rmdir(PATH_POSTS.$key);
2015-05-05 03:00:01 +02:00
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)
{
if( $this->delete($args['key']) ) {
2016-05-29 02:54:18 +02:00
// Modified date
$args['dateModified'] = Date::current(DB_DATE_FORMAT);
2015-05-05 03:00:01 +02:00
return $this->add($args);
}
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the post.');
return false;
}
public function delete($key)
{
// Post doesn't exist in database.
if(!$this->postExists($key)) {
Log::set(__METHOD__.LOG_SEP.'The post does not exist. Key: '.$key);
}
// Delete the index.txt file.
if( Filesystem::rmfile(PATH_POSTS.$key.DS.FILENAME) === false ) {
2015-05-05 03:00:01 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the file index.txt');
}
// Delete the directory.
2015-05-31 03:06:55 +02:00
if( Filesystem::rmdir(PATH_POSTS.$key) === false ) {
2015-05-05 03:00:01 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the directory '.PATH_POSTS.$key);
}
// Remove from database.
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;
}
2015-08-31 03:18:06 +02:00
// Returns an array with a list of posts keys, filtered by a page number.
public function getList($pageNumber, $postPerPage, $removeUnpublished=true)
2015-05-05 03:00:01 +02:00
{
2016-06-17 19:08:08 +02:00
$totalPosts = $this->numberPost(true);
2015-05-05 03:00:01 +02:00
2015-08-31 03:18:06 +02:00
// Remove the unpublished posts.
if($removeUnpublished) {
2015-05-05 03:00:01 +02:00
$this->removeUnpublished();
2016-06-17 19:08:08 +02:00
$totalPosts = $this->numberPost(true);
2015-05-05 03:00:01 +02:00
}
2015-07-20 05:14:12 +02:00
$init = (int) $postPerPage * $pageNumber;
2015-08-31 03:18:06 +02:00
$end = (int) min( ($init + $postPerPage - 1), $totalPosts - 1 );
2015-08-30 01:26:46 +02:00
$outrange = $init<0 ? true : $init>$end;
2015-07-20 05:14:12 +02:00
2015-09-02 02:42:21 +02:00
if(!$outrange) {
2016-02-07 00:44:43 +01:00
$tmp = array_slice($this->db, $init, $postPerPage, true);
2016-06-17 19:08:08 +02:00
// Restore the database because we deleted the unpublished posts.
2016-02-07 00:44:43 +01:00
$this->restoreDB();
return $tmp;
2015-05-05 03:00:01 +02:00
}
return array();
}
// Delete all posts from an user
2015-07-22 05:15:02 +02:00
public function deletePostsByUser($username)
{
foreach($this->db as $key=>$value) {
2015-07-22 05:15:02 +02:00
if($value['username']==$username) {
$this->delete($key);
Log::set(__METHOD__.LOG_SEP.'Post deleted: '.$key);
2015-07-22 05:15:02 +02:00
}
}
2016-02-26 15:42:28 +01:00
Log::set(__METHOD__.LOG_SEP.'Posts from the user '.$username.' were delete.');
2015-07-22 05:15:02 +02:00
return true;
}
// Link-up all posts from an user to another user.
public function linkPostsToUser($oldUsername, $newUsername)
{
foreach($this->db as $key=>$value) {
2015-07-22 05:15:02 +02:00
if($value['username']==$oldUsername) {
$this->db[$key]['username'] = $newUsername;
}
}
2015-09-02 02:42:21 +02:00
// Sort posts before save.
$this->sortByDate();
2015-07-22 05:15:02 +02:00
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
2016-02-26 15:42:28 +01:00
Log::set(__METHOD__.LOG_SEP.'Posts linked to another user.');
2015-07-22 05:15:02 +02:00
return true;
}
2015-09-02 02:42:21 +02:00
// Remove unpublished posts, status != published.
public function removeUnpublished()
2015-05-05 03:00:01 +02:00
{
2016-06-17 19:08:08 +02:00
foreach($this->db as $key=>$values) {
2015-09-02 02:42:21 +02:00
if($values['status']!='published') {
2015-08-31 03:18:06 +02:00
unset($this->db[$key]);
2015-05-05 03:00:01 +02:00
}
}
2015-08-31 03:18:06 +02:00
return true;
2015-05-05 03:00:01 +02:00
}
2015-09-02 02:42:21 +02:00
// Return TRUE if there are new posts published, FALSE otherwise.
public function scheduler()
{
// Get current date
2015-09-02 02:42:21 +02:00
$currentDate = Date::current(DB_DATE_FORMAT);
$saveDatabase = false;
2016-01-14 05:42:18 +01:00
// Check scheduled posts
2015-09-02 02:42:21 +02:00
foreach($this->db as $postKey=>$values)
{
if($values['status']=='scheduled') {
// Publish post
2015-09-02 02:42:21 +02:00
if($values['date']<=$currentDate) {
2015-09-02 02:42:21 +02:00
$this->db[$postKey]['status'] = 'published';
$saveDatabase = true;
}
}
elseif($values['status']=='published') {
break;
}
}
2016-01-14 05:42:18 +01:00
// Save the database ?
2015-09-02 02:42:21 +02:00
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 posts published from the scheduler.');
2015-09-02 02:42:21 +02:00
return true;
}
return false;
}
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;
}
2015-08-31 03:18:06 +02:00
// Sort posts by date.
public function sortByDate($HighToLow=true)
2015-05-05 03:00:01 +02:00
{
2015-08-31 03:18:06 +02:00
if($HighToLow) {
uasort($this->db, array($this, 'sortHighToLow'));
}
else {
uasort($this->db, array($this, 'sortLowToHigh'));
2015-05-05 03:00:01 +02:00
}
2015-08-31 03:18:06 +02:00
return true;
}
private function sortLowToHigh($a, $b) {
return $a['date']>$b['date'];
}
private function sortHighToLow($a, $b) {
return $a['date']<$b['date'];
}
public function cliMode()
2015-08-31 03:18:06 +02:00
{
// LOG
Log::set('CLI MODE - POSTS - Starting...');
2015-08-31 03:18:06 +02:00
$postList = array();
2015-05-05 03:00:01 +02:00
2016-05-07 05:10:10 +02:00
$postsDirectories = Filesystem::listDirectories(PATH_POSTS);
foreach( $postsDirectories as $directory ) {
if( Sanitize::pathFile($directory.DS.FILENAME) ) {
// The key is the directory name
2015-09-20 23:46:50 +02:00
$key = basename($directory);
2015-08-31 03:18:06 +02:00
// Add the key to the list
$postList[$key] = true;
2016-05-07 05:10:10 +02:00
// Checksum
$checksum = md5_file($directory.DS.FILENAME);
2015-08-31 03:18:06 +02:00
// LOG
Log::set('CLI MODE - Post found, key: '.$key);
2016-05-07 05:10:10 +02:00
if( !isset($this->db[$key]) ) {
2015-08-31 03:18:06 +02:00
// LOG
Log::set('CLI MODE - The post is not in the database, key: '.$key);
2015-09-10 04:33:31 +02:00
// Insert new post
$this->cliModeInsert($key);
}
else {
// If checksum is different, update the post
if( $this->db[$key]['md5file']!==$checksum ) {
2016-05-07 05:10:10 +02:00
// LOG
Log::set('CLI MODE - Different md5 checksum, key: '.$key);
2016-02-26 15:42:28 +01:00
// Update the post
$this->cliModeInsert($key, $update=true);
2015-09-10 04:33:31 +02:00
}
2015-08-31 03:18:06 +02:00
}
}
}
// LOG
Log::set('CLI MODE - Cleaning database...');
foreach( array_diff_key($this->db, $postList) as $key=>$data ) {
// LOG
Log::set('CLI MODE - Removing post from database, key: '.$key);
// Removing the post from database
unset( $this->db[$key] );
2015-08-31 03:18:06 +02:00
}
// Sort posts before save
2015-09-10 04:33:31 +02:00
$this->sortByDate();
// Save the database
$this->save();
// LOG
Log::set('CLI MODE - POSTS - Finishing...');
return true;
}
private function cliModeInsert($key, $update=false)
{
if($update) {
// LOG
Log::set('CLI MODE - cliModeInsert() - Updating the post, key: '.$key);
// Database from the current database
$dataForDb = $this->db[$key];
$dataForDb['dateModified'] = Date::current(DB_DATE_FORMAT);
2015-08-31 03:18:06 +02:00
}
else {
// LOG
Log::set('CLI MODE - cliModeInsert() - Inserting the new post, key: '.$key);
2015-08-31 03:18:06 +02:00
// Database for the new post, 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);
2016-02-26 15:42:28 +01:00
}
// MD5 checksum
$dataForDb['md5file'] = md5_file(PATH_POSTS.$key.DS.FILENAME);
// Generate the Object from the file
$Post = new Post($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 = $Post->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;
if( $data > $currentDate ) {
$dataForDb['status'] = 'scheduled';
}
}
}
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-05-05 03:00:01 +02:00
}
}