bludit/bl-kernel/dbposts.class.php

508 lines
12 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'=>''),
'coverImage'=> array('inFile'=>false, 'value'=>''),
2016-01-16 15:01:29 +01:00
'checksum'=> array('inFile'=>false, 'value'=>'')
2015-05-05 03:00:01 +02:00
);
2015-07-20 05:14:12 +02:00
private $numberPosts = array(
'total'=>0,
2015-08-31 03:18:06 +02:00
'published'=>0
2015-07-20 05:14:12 +02:00
);
2015-05-05 03:00:01 +02:00
function __construct()
{
parent::__construct(PATH_DATABASES.'posts.php');
2015-07-20 05:14:12 +02:00
$this->numberPosts['total'] = count($this->db);
}
public function numberPost($total=false)
{
if($total) {
return $this->numberPosts['total'];
}
2015-08-31 03:18:06 +02:00
return $this->numberPosts['published'];
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
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.
$key = $this->generateKey($args['slug']);
2015-10-31 23:54:42 +01:00
// 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']) ) {
2016-02-26 15:42:28 +01:00
Log::set(__METHOD__.LOG_SEP.'The session does not have the username.');
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)
{
2015-09-18 05:24:10 +02:00
// 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
}
}
2015-09-18 05:24:10 +02:00
// 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, if in the file or in the database.
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;
}
}
2016-01-12 04:36:48 +01:00
// Create Hash
$serialize = serialize($dataForDb+$dataForFile);
2016-01-16 15:01:29 +01:00
$dataForDb['checksum'] = sha1($serialize);
2016-01-12 04:36:48 +01:00
2015-05-05 03:00:01 +02:00
// 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);
2015-06-26 06:31:53 +02:00
if( file_put_contents(PATH_POSTS.$key.DS.'index.txt', $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;
}
// Save the database
$this->db[$key] = $dataForDb;
2015-09-02 02:42:21 +02:00
// Sort posts before save.
$this->sortByDate();
2015-05-05 03:00:01 +02:00
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)
{
if( $this->delete($args['key']) ) {
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.
2015-06-26 06:31:53 +02:00
if( Filesystem::rmfile(PATH_POSTS.$key.DS.'index.txt') === 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
{
2015-08-31 03:18:06 +02:00
$totalPosts = $this->numberPosts['total'];
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();
2015-08-31 03:18:06 +02:00
$totalPosts = $this->numberPosts['published'];
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);
// Restore the database because we delete the unpublished posts.
$this->restoreDB();
return $tmp;
2015-05-05 03:00:01 +02:00
}
return array();
}
2015-07-22 05:15:02 +02:00
// Delete all posts from an user.
public function deletePostsByUser($username)
{
foreach($this->db as $key=>$value)
{
if($value['username']==$username) {
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;
}
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)
{
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
{
2015-08-26 05:42:32 +02:00
foreach($this->db as $key=>$values)
2015-05-05 03:00:01 +02:00
{
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
$this->numberPosts['published'] = count($this->db);
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.
$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.
if($values['date']<=$currentDate) {
$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;
}
2016-02-26 15:42:28 +01:00
Log::set(__METHOD__.LOG_SEP.'New post published from 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'];
}
2016-02-07 00:44:43 +01:00
// Return TRUE if there are new posts or orphan post deleted, FALSE otherwise.
2015-08-31 03:18:06 +02:00
public function regenerateCli()
{
$db = $this->db;
2015-09-20 23:46:50 +02:00
$allPosts = array();
2015-08-31 03:18:06 +02:00
$fields = array();
2015-09-20 23:46:50 +02:00
$currentDate = Date::current(DB_DATE_FORMAT);
2015-08-31 03:18:06 +02:00
2015-09-20 23:46:50 +02:00
// Generate default fields and values.
2016-05-07 05:10:10 +02:00
// --------------------------------------------------------------------------
2015-08-31 03:18:06 +02:00
foreach($this->dbFields as $field=>$options) {
if(!$options['inFile']) {
$fields[$field] = $options['value'];
}
2015-05-05 03:00:01 +02:00
}
2016-05-07 05:10:10 +02:00
$fields['status'] = CLI_STATUS;
$fields['username'] = CLI_USERNAME;
$fields['date'] = $currentDate;
2015-05-05 03:00:01 +02:00
2016-02-07 00:44:43 +01:00
// Get all posts from the first level of directories.
2016-05-07 05:10:10 +02:00
$postsDirectories = Filesystem::listDirectories(PATH_POSTS);
foreach($postsDirectories as $directory)
2015-08-31 03:18:06 +02:00
{
2016-05-07 05:10:10 +02:00
// Check if the post has the index.txt file.
2016-02-07 00:44:43 +01:00
if(Sanitize::pathFile($directory.DS.'index.txt'))
2015-09-20 23:46:50 +02:00
{
// The key is the directory name.
$key = basename($directory);
2015-08-31 03:18:06 +02:00
2016-05-07 05:10:10 +02:00
Log::set('----------------');
Log::set('CliMode - Post KEY: '.$key);
// This post exists
2015-09-20 23:46:50 +02:00
$allPosts[$key] = true;
2015-08-31 03:18:06 +02:00
2016-02-07 00:44:43 +01:00
// Create the new entry if not exist inside the DATABASE.
2016-05-07 05:10:10 +02:00
if( !isset($this->db[$key]) ) {
2016-02-07 00:44:43 +01:00
// New entry on database with the default fields and values.
2015-09-20 23:46:50 +02:00
$this->db[$key] = $fields;
2016-05-07 05:10:10 +02:00
Log::set('CliMode - New post: '.$key);
2015-09-20 23:46:50 +02:00
}
2015-08-31 03:18:06 +02:00
2015-09-20 23:46:50 +02:00
// Create the post from FILE.
$Post = new Post($key);
2015-09-10 04:33:31 +02:00
2015-09-20 23:46:50 +02:00
// Update all fields from FILE to DATABASE.
foreach($fields as $f=>$v)
{
2016-05-07 05:10:10 +02:00
// Get the value from FILE.
$valueFromFile = $Post->getField($f);
2015-09-20 23:46:50 +02:00
// If the field exists on the FILE, update it.
2016-05-07 05:10:10 +02:00
if( !empty($valueFromFile) )
2015-09-20 23:46:50 +02:00
{
2016-05-07 05:10:10 +02:00
Log::set('CliMode - Field to replace: '.$f);
Log::set('CliMode - value from file: '.$valueFromFile);
2016-02-26 15:42:28 +01:00
2015-09-20 23:46:50 +02:00
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;
2016-01-14 05:42:18 +01:00
if( $valueFromFile > $currentDate ) {
2015-09-20 23:46:50 +02:00
$this->db[$key]['status'] = 'scheduled';
}
}
}
else {
// Sanitize the values from file.
$this->db[$key][$f] = Sanitize::html($valueFromFile);
2015-09-10 04:33:31 +02:00
}
}
2015-08-31 03:18:06 +02:00
}
}
}
2015-09-20 23:46:50 +02:00
// Remove orphan posts from db, the orphan posts are posts deleted by hand (directory deleted).
foreach( array_diff_key($db, $allPosts) as $key=>$data ) {
2015-08-31 03:18:06 +02:00
unset($this->db[$key]);
Log::set('CliMode - Deleted post: '.$key);
2015-08-31 03:18:06 +02:00
}
2015-09-10 04:33:31 +02:00
// Sort posts before save.
$this->sortByDate();
2015-08-31 03:18:06 +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
if($this->db!=$db) {
2016-05-07 05:10:10 +02:00
Log::set(__METHOD__.LOG_SEP.'There are new or deleted posts.');
return true;
2016-02-26 15:42:28 +01:00
}
2016-05-07 05:10:10 +02:00
return false;
2015-05-05 03:00:01 +02:00
}
2016-02-26 15:42:28 +01:00
}