bludit/kernel/dbposts.class.php

425 lines
9.6 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'=>''),
'status'=> array('inFile'=>false, 'value'=>'draft'),
'tags'=> array('inFile'=>false, 'value'=>''),
'allowComments'=> array('inFile'=>false, 'value'=>false),
2015-08-26 05:42:32 +02:00
'date'=> 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,
'withoutDrafts'=>0
);
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'];
}
return $this->numberPosts['withoutDrafts'];
2015-05-05 03:00:01 +02:00
}
2015-07-22 05:15:02 +02:00
// Return an array with the post's database, FALSE otherwise.
2015-07-07 00:22:03 +02:00
public function getDb($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;
}
2015-08-26 05:42:32 +02:00
public function setDb($key, $field, $value)
{
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
// Generate the database key.
$key = $this->generateKey($args['slug']);
2015-08-26 05:42:32 +02:00
// The user is always the 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']) ) {
2015-05-05 03:00:01 +02:00
return false;
}
2015-08-26 05:42:32 +02:00
if(!Valid::date($args['date'], DB_DATE_FORMAT)) {
$args['date'] = Date::current(DB_DATE_FORMAT);
2015-05-05 03:00:01 +02:00
}
// Verify arguments with the database fields.
foreach($this->dbFields as $field=>$options)
{
if( isset($args[$field]) )
{
// Sanitize if will be saved on database.
if( !$options['inFile'] ) {
$tmpValue = Sanitize::html($args[$field]);
}
else {
$tmpValue = $args[$field];
}
}
// Default value for the field.
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;
}
}
// 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;
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
public function edit($args)
{
2015-08-26 05:42:32 +02:00
if( !Valid::date($args['date'], DB_DATE_FORMAT) ) {
$args['date'] = Date::current(DB_DATE_FORMAT);
2015-08-22 18:33:33 +02:00
}
2015-05-05 03:00:01 +02:00
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-26 05:42:32 +02:00
public function regenerate()
{
$db = $this->db;
$newPaths = array();
$fields = array();
// Default fields and value
foreach($this->dbFields as $field=>$options) {
if(!$options['inFile']) {
$fields[$field] = $options['value'];
}
}
$fields['status'] = CLI_STATUS;
$fields['date'] = Date::current(DB_DATE_FORMAT);
// Recovery pages from the first level of directories
$tmpPaths = glob(PATH_POSTS.'*', GLOB_ONLYDIR);
foreach($tmpPaths as $directory)
{
$key = basename($directory);
if(file_exists($directory.DS.'index.txt')) {
// The key is the directory name
$newPaths[$key] = true;
}
}
foreach($newPaths as $key=>$value)
{
if(!isset($this->db[$key])) {
$this->db[$key] = $fields;
}
$Post = new Post($key);
// Update all fields from FILE to DATABASE.
foreach($fields as $f=>$v)
{
if($Post->getField($f)) {
// DEBUG: Validar/Sanitizar valores, ej: validar formato fecha
$this->db[$key][$f] = $Post->getField($f);
}
}
// DEBUG: Update tags
}
// Remove old posts 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;
}
/*
2015-05-05 03:00:01 +02:00
public function regenerate()
{
$db = $this->db;
$paths = array();
$fields = array();
// Default fields and value
foreach($this->dbFields as $field=>$options) {
if(!$options['inFile']) {
$fields[$field] = $options['value'];
}
}
// Unix time stamp
2015-08-26 05:42:32 +02:00
$fields['date'] = Date::current(DB_DATE_FORMAT);
2015-05-05 03:00:01 +02:00
// Username
$fields['username'] = 'admin';
2015-06-26 06:31:53 +02:00
if(HANDMADE_PUBLISHED) {
2015-05-05 03:00:01 +02:00
$fields['status']='published';
2015-06-26 06:31:53 +02:00
}
2015-05-05 03:00:01 +02:00
// Recovery pages from the first level of directories
$tmpPaths = glob(PATH_POSTS.'*', GLOB_ONLYDIR);
foreach($tmpPaths as $directory)
{
$key = basename($directory);
2015-06-26 06:31:53 +02:00
if(file_exists($directory.DS.'index.txt')) {
2015-05-05 03:00:01 +02:00
// The key is the directory name
$paths[$key] = true;
}
}
// Remove old posts from db
foreach( array_diff_key($db, $paths) as $slug=>$data ) {
unset($this->db[$slug]);
}
// Insert new posts to db
foreach( array_diff_key($paths, $db) as $slug=>$data ) {
$this->db[$slug] = $fields;
}
2015-06-26 06:31:53 +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;
}
return $this->db!=$db;
2015-05-05 03:00:01 +02:00
}
2015-08-26 05:42:32 +02:00
*/
2015-08-30 01:26:46 +02:00
public function getList($pageNumber, $postPerPage, $draftPosts=false)
2015-05-05 03:00:01 +02:00
{
// DEBUG: Ver una mejor manera de eliminar draft post antes de ordenarlos
// DEBUG: Se eliminan antes de ordenarlos porque sino los draft cuentan como publicados en el PostPerPage.
2015-07-20 05:14:12 +02:00
if(!$draftPosts) {
2015-05-05 03:00:01 +02:00
$this->removeUnpublished();
2015-07-20 05:14:12 +02:00
$this->numberPosts['withoutDrafts'] = count($this->db);
2015-05-05 03:00:01 +02:00
}
2015-07-20 05:14:12 +02:00
$init = (int) $postPerPage * $pageNumber;
$end = (int) min( ($init + $postPerPage - 1), count($this->db) - 1 );
2015-08-30 01:26:46 +02:00
$outrange = $init<0 ? true : $init>$end;
2015-07-20 05:14:12 +02:00
// Sort posts
2015-08-30 01:26:46 +02:00
$this->sortByDate();
2015-05-05 03:00:01 +02:00
if(!$outrange) {
2015-08-30 01:26:46 +02:00
$tmp = $this->db;
2015-07-20 05:14:12 +02:00
return array_slice($tmp, $init, $postPerPage, true);
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;
}
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;
}
}
// Save the database.
if( $this->save() === false ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
return false;
}
return true;
}
2015-08-26 05:42:32 +02:00
// Remove the posts not published, status != published and date grater than current date.
2015-05-05 03:00:01 +02:00
// DEBUG: Ver una mejor manera de eliminar draft post antes de ordenarlos
private function removeUnpublished()
{
$tmp = array();
2015-08-26 05:42:32 +02:00
$currentDate = Date::current(DB_DATE_FORMAT);
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-08-26 05:42:32 +02:00
if( ($values['status']==='published') && ($values['date']<=$currentDate) ) {
$tmp[$key]=$values;
2015-05-05 03:00:01 +02:00
}
}
$this->db = $tmp;
}
private function sortByDate($low_to_high=false)
{
// high to low
function high_to_low($a, $b) {
2015-08-26 05:42:32 +02:00
return $a['date']<$b['date'];
2015-05-05 03:00:01 +02:00
}
// low to high
function low_to_high($a, $b) {
2015-08-26 05:42:32 +02:00
return $a['date']>$b['date'];
2015-05-05 03:00:01 +02:00
}
$tmp = $this->db;
if($low_to_high)
uasort($tmp, 'low_to_high');
else
uasort($tmp, 'high_to_low');
2015-08-30 01:26:46 +02:00
return true;
2015-05-05 03:00:01 +02:00
}
}