RSS Plugin, improves on code

This commit is contained in:
dignajar 2016-01-11 23:18:20 -03:00
parent 640b58ecdf
commit 696d49c9bc
6 changed files with 295 additions and 516 deletions

View File

@ -0,0 +1,274 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
class Content {
public $vars;
function __construct($path)
{
if($this->build($path)===false) {
$this->vars = false;
}
}
// Return true if valid
public function isValid()
{
return($this->vars!==false);
}
public function getField($field)
{
if(isset($this->vars[$field])) {
return $this->vars[$field];
}
return false;
}
// $notoverwrite true if you don't want to replace the value if are set previusly
public function setField($field, $value, $overwrite=true)
{
if($overwrite || empty($this->vars[$field])) {
$this->vars[$field] = $value;
}
return true;
}
private function build($path)
{
if( !Sanitize::pathFile($path, 'index.txt') ) {
return false;
}
$tmp = 0;
$lines = file($path.'index.txt');
foreach($lines as $lineNumber=>$line)
{
$parts = array_map('trim', explode(':', $line, 2));
// Lowercase variable
$parts[0] = Text::lowercase($parts[0]);
// If variables is content then break the foreach and process the content after.
if($parts[0]==='content')
{
$tmp = $lineNumber;
break;
}
if( !empty($parts[0]) && !empty($parts[1]) ) {
// Sanitize all fields, except Content.
$this->vars[$parts[0]] = Sanitize::html($parts[1]);
}
}
// Process the content.
if($tmp!==0)
{
// Next line after "Content:" variable
$tmp++;
// Remove lines after Content
$output = array_slice($lines, $tmp);
if(!empty($parts[1])) {
array_unshift($output, "\n");
array_unshift($output, $parts[1]);
}
$implode = implode($output);
$this->vars['content'] = $implode;
// Sanitize content.
//$this->vars['content'] = Sanitize::html($implode);
}
}
// Returns the post title.
public function title()
{
return $this->getField('title');
}
// Returns the content.
// This content is markdown parser.
// (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content.
// (boolean) $raw, TRUE returns the content without sanitized, FALSE otherwise.
public function content($fullContent=true, $raw=true)
{
// This content is not sanitized.
$content = $this->getField('content');
if(!$fullContent) {
$content = $this->getField('breakContent');
}
if($raw) {
return $content;
}
return Sanitize::html($content);
}
public function readMore()
{
return $this->getField('readMore');
}
// Returns the content. This content is not markdown parser.
// (boolean) $raw, TRUE returns the content without sanitized, FALSE otherwise.
public function contentRaw($raw=true)
{
// This content is not sanitized.
$content = $this->getField('contentRaw');
if($raw) {
return $content;
}
return Sanitize::html($content);
}
public function key()
{
return $this->getField('key');
}
// Returns TRUE if the post is published, FALSE otherwise.
public function published()
{
return ($this->getField('status')==='published');
}
// Returns TRUE if the post is scheduled, FALSE otherwise.
public function scheduled()
{
return ($this->getField('status')==='scheduled');
}
// Returns TRUE if the post is draft, FALSE otherwise.
public function draft()
{
return ($this->getField('status')=='draft');
}
public function coverImage($absolute=true)
{
$fileName = $this->getField('coverImage');
if(empty($fileName)) {
return false;
}
if($absolute) {
return HTML_PATH_UPLOADS.$fileName;
}
return $fileName;
}
public function profilePicture()
{
return HTML_PATH_UPLOADS_PROFILES.$this->username().'.jpg';
}
// Returns the user object if $field is false, otherwise returns the field's value.
public function user($field=false)
{
// Get the user object.
$User = $this->getField('user');
if($field) {
return $User->getField($field);
}
return $User;
}
public function username()
{
return $this->getField('username');
}
public function description()
{
return $this->getField('description');
}
// Returns the post date according to locale settings and format settings.
public function date()
{
return $this->getField('date');
}
// Returns the post date according to locale settings and format as database stored.
public function dateRaw($format=false)
{
$date = $this->getField('dateRaw');
if($format) {
return Date::format($date, DB_DATE_FORMAT, $format);
}
return $date;
}
public function tags($returnsArray=false)
{
global $Url;
$tags = $this->getField('tags');
if($returnsArray) {
if($tags==false) {
return array();
}
return $tags;
}
else {
if($tags==false) {
return false;
}
// Return string with tags separeted by comma.
return implode(', ', $tags);
}
}
public function permalink($absolute=false)
{
global $Url;
global $Site;
$filterType = $this->getField('filterType');
$url = trim(DOMAIN_BASE,'/');
$key = $this->key();
$filter = trim($Url->filters($filterType), '/');
$htmlPath = trim(HTML_PATH_ROOT,'/');
if(empty($filter)) {
$tmp = $key;
}
else {
$tmp = $filter.'/'.$key;
}
if($absolute) {
return $url.'/'.$tmp;
}
if(empty($htmlPath)) {
return '/'.$tmp;
}
return '/'.$htmlPath.'/'.$tmp;
}
}

View File

@ -1,90 +0,0 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
class fileContent
{
public $vars;
function __construct($path)
{
if($this->build($path)===false) {
$this->vars = false;
}
}
// Return true if valid
public function isValid()
{
return($this->vars!==false);
}
public function getField($field)
{
if(isset($this->vars[$field])) {
return $this->vars[$field];
}
return false;
}
// $notoverwrite true if you don't want to replace the value if are set previusly
public function setField($field, $value, $overwrite=true)
{
if($overwrite || empty($this->vars[$field])) {
$this->vars[$field] = $value;
}
return true;
}
private function build($path)
{
if( !Sanitize::pathFile($path, 'index.txt') ) {
return false;
}
$tmp = 0;
$lines = file($path.'index.txt');
foreach($lines as $lineNumber=>$line)
{
$parts = array_map('trim', explode(':', $line, 2));
// Lowercase variable
$parts[0] = Text::lowercase($parts[0]);
// If variables is content then break the foreach and process the content after.
if($parts[0]==='content')
{
$tmp = $lineNumber;
break;
}
if( !empty($parts[0]) && !empty($parts[1]) ) {
// Sanitize all fields, except Content.
$this->vars[$parts[0]] = Sanitize::html($parts[1]);
}
}
// Process the content.
if($tmp!==0)
{
// Next line after "Content:" variable
$tmp++;
// Remove lines after Content
$output = array_slice($lines, $tmp);
if(!empty($parts[1])) {
array_unshift($output, "\n");
array_unshift($output, $parts[1]);
}
$implode = implode($output);
$this->vars['content'] = $implode;
// Sanitize content.
//$this->vars['content'] = Sanitize::html($implode);
}
}
}

View File

@ -113,7 +113,7 @@ if(MB_STRING)
// Inclde Abstract Classes
include(PATH_ABSTRACT.'dbjson.class.php');
include(PATH_ABSTRACT.'filecontent.class.php');
include(PATH_ABSTRACT.'content.class.php');
include(PATH_ABSTRACT.'plugin.class.php');
// Inclde Classes

View File

@ -2,8 +2,6 @@
class Filesystem {
// NEW
// Returns an array with the absolutes directories.
public static function listDirectories($path, $regex='*')
{
@ -52,50 +50,4 @@ class Filesystem {
return unlink($filename);
}
// OLD
public static function get_images($regex)
{
return self::ls(PATH_UPLOAD, $regex, '*', false, false, false);
}
// Devuelve un arreglo con el listado de archivos
// $path con una barra al final, ej: /home/
// $file_expression : *.0.*.*.*.*.*.*.*.*
// $ext : xml
// $flag_dir : si quiero listar directorios
// $sort_asc_numeric : ordeno ascedente numerico
// $sort_desc_numeric : ordeno descendente numerico
public static function ls($path, $file_expression = NULL, $ext, $flag_dir = false, $sort_asc_numeric = false, $sort_desc_numeric = true)
{
if($flag_dir)
{
$files = glob($path . $file_expression, GLOB_ONLYDIR);
}
else
{
$files = glob($path . $file_expression . '.' . $ext);
}
if( ($files==false) || (empty($files)) )
{
$files = array();
}
foreach($files as $key=>$file)
{
$files[$key] = basename($file);
}
// Sort
if($sort_asc_numeric)
{
sort($files, SORT_NUMERIC);
}
elseif($sort_desc_numeric)
{
rsort($files, SORT_NUMERIC);
}
return $files;
}
}
}

View File

@ -1,182 +1,37 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
class Page extends fileContent
{
class Page extends Content {
function __construct($key)
{
// Database Key
$this->setField('key', $key);
// Set filterType
$this->setField('filterType', 'page');
parent::__construct(PATH_PAGES.$key.DS);
}
// Returns the post title.
public function title()
{
return $this->getField('title');
}
// Returns the content.
// This content is markdown parser.
// (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content.
// (boolean) $raw, TRUE returns the content without sanitized, FALSE otherwise.
public function content($fullContent=true, $raw=true)
{
// This content is not sanitized.
$content = $this->getField('content');
if(!$fullContent) {
$content = $this->getField('breakContent');
}
if($raw) {
return $content;
}
return Sanitize::html($content);
}
public function readMore()
{
return $this->getField('readMore');
}
// Returns the content. This content is not markdown parser.
// (boolean) $raw, TRUE returns the content without sanitized, FALSE otherwise.
public function contentRaw($raw=true)
{
// This content is not sanitized.
$content = $this->getField('contentRaw');
if($raw) {
return $content;
}
return Sanitize::html($content);
}
public function description()
{
return $this->getField('description');
}
public function tags($returnsArray=false)
{
global $Url;
$tags = $this->getField('tags');
if($returnsArray) {
if($tags==false) {
return array();
}
return $tags;
}
else {
if($tags==false) {
return false;
}
// Return string with tags separeted by comma.
return implode(', ', $tags);
}
}
// Returns the page position.
public function position()
{
return $this->getField('position');
}
// Returns the post date according to locale settings and format settings.
public function date()
{
return $this->getField('date');
}
// Returns the post date according to locale settings and format as database stored.
public function dateRaw($format=false)
{
$date = $this->getField('dateRaw');
if($format) {
return Date::format($date, DB_DATE_FORMAT, $format);
}
return $date;
}
// Returns the page slug.
public function slug()
{
$explode = explode('/', $this->getField('key'));
if(!empty($explode[1]))
// Check if the page have a parent.
if(!empty($explode[1])) {
return $explode[1];
}
return $explode[0];
}
public function key()
{
return $this->getField('key');
}
public function coverImage($absolute=true)
{
$fileName = $this->getField('coverImage');
if(empty($fileName)) {
return false;
}
if($absolute) {
return HTML_PATH_UPLOADS.$fileName;
}
return $fileName;
}
// Returns TRUE if the page is published, FALSE otherwise.
public function published()
{
return ($this->getField('status')==='published');
}
// Returns TRUE if the post is draft, FALSE otherwise.
public function draft()
{
return ($this->getField('status')=='draft');
}
// Returns the page permalink.
public function permalink($absolute=false)
{
global $Url;
global $Site;
$url = trim($Site->url(),'/');
$key = $this->key();
$filter = trim($Url->filters('page'), '/');
$htmlPath = trim(HTML_PATH_ROOT,'/');
if(empty($filter)) {
$tmp = $key;
}
else {
$tmp = $filter.'/'.$key;
}
if($absolute) {
return $url.'/'.$tmp;
}
if(empty($htmlPath)) {
return '/'.$tmp;
}
return '/'.$htmlPath.'/'.$tmp;
}
// Returns the parent key, if the page doesn't have a parent returns FALSE.
public function parentKey()
{
@ -212,35 +67,4 @@ class Page extends fileContent
return $tmp;
}
// Returns the user object if $field is false, otherwise returns the field's value.
public function user($field=false)
{
// Get the user object.
$User = $this->getField('user');
if($field) {
return $User->getField($field);
}
return $User;
}
// DEPRECATED
public function username()
{
return $this->getField('username');
}
// DEPRECATED
public function authorFirstName()
{
return $this->getField('authorFirstName');
}
// DEPRECATED
public function authorLastName()
{
return $this->getField('authorLastName');
}
}
}

View File

@ -1,69 +1,26 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
class Post extends fileContent
{
class Post extends Content {
function __construct($key)
{
// Database Key
$this->setField('key', $key);
// Set filterType
$this->setField('filterType', 'post');
parent::__construct(PATH_POSTS.$key.DS);
}
// Returns the post title.
public function title()
{
return $this->getField('title');
}
// Returns the content.
// This content is markdown parser.
// (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content.
// (boolean) $raw, TRUE returns the content without sanitized, FALSE otherwise.
public function content($fullContent=true, $raw=true)
{
// This content is not sanitized.
$content = $this->getField('content');
if(!$fullContent) {
$content = $this->getField('breakContent');
}
if($raw) {
return $content;
}
return Sanitize::html($content);
}
public function readMore()
{
return $this->getField('readMore');
}
// Returns the content. This content is not markdown parser.
// (boolean) $raw, TRUE returns the content without sanitized, FALSE otherwise.
public function contentRaw($raw=true)
{
// This content is not sanitized.
$content = $this->getField('contentRaw');
if($raw) {
return $content;
}
return Sanitize::html($content);
}
public function key()
{
return $this->getField('key');
}
// Returns TRUE if the post is published, FALSE otherwise.
public function published()
public function slug()
{
return ($this->getField('status')==='published');
return $this->getField('key');
}
// Returns TRUE if the post is scheduled, FALSE otherwise.
@ -71,142 +28,4 @@ class Post extends fileContent
{
return ($this->getField('status')==='scheduled');
}
// Returns TRUE if the post is draft, FALSE otherwise.
public function draft()
{
return ($this->getField('status')=='draft');
}
public function coverImage($absolute=true)
{
$fileName = $this->getField('coverImage');
if(empty($fileName)) {
return false;
}
if($absolute) {
return HTML_PATH_UPLOADS.$fileName;
}
return $fileName;
}
public function profilePicture()
{
return HTML_PATH_UPLOADS_PROFILES.$this->username().'.jpg';
}
// Returns the user object if $field is false, otherwise returns the field's value.
public function user($field=false)
{
// Get the user object.
$User = $this->getField('user');
if($field) {
return $User->getField($field);
}
return $User;
}
// DEPRECATED
public function username()
{
return $this->getField('username');
}
// DEPRECATED
public function authorFirstName()
{
return $this->getField('authorFirstName');
}
// DEPRECATED
public function authorLastName()
{
return $this->getField('authorLastName');
}
public function description()
{
return $this->getField('description');
}
// Returns the post date according to locale settings and format settings.
public function date()
{
return $this->getField('date');
}
// Returns the post date according to locale settings and format as database stored.
public function dateRaw($format=false)
{
$date = $this->getField('dateRaw');
if($format) {
return Date::format($date, DB_DATE_FORMAT, $format);
}
return $date;
}
public function tags($returnsArray=false)
{
global $Url;
$tags = $this->getField('tags');
if($returnsArray) {
if($tags==false) {
return array();
}
return $tags;
}
else {
if($tags==false) {
return false;
}
// Return string with tags separeted by comma.
return implode(', ', $tags);
}
}
public function slug()
{
return $this->getField('key');
}
public function permalink($absolute=false)
{
global $Url;
global $Site;
$url = trim(DOMAIN_BASE,'/');
$key = $this->key();
$filter = trim($Url->filters('post'), '/');
$htmlPath = trim(HTML_PATH_ROOT,'/');
if(empty($filter)) {
$tmp = $key;
}
else {
$tmp = $filter.'/'.$key;
}
if($absolute) {
return $url.'/'.$tmp;
}
if(empty($htmlPath)) {
return '/'.$tmp;
}
return '/'.$htmlPath.'/'.$tmp;
}
}
}