bludit/bl-kernel/page.class.php

501 lines
11 KiB
PHP
Raw Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-03-08 18:02:59 +01:00
2017-05-09 22:57:56 +02:00
class Page {
private $vars;
2016-01-12 03:18:20 +01:00
2015-05-05 03:00:01 +02:00
function __construct($key)
2015-08-24 00:07:14 +02:00
{
2017-05-09 22:57:56 +02:00
$this->vars = false;
2017-08-21 19:45:57 +02:00
if ($this->build($key)) {
2017-05-09 22:57:56 +02:00
$this->vars['key'] = $key;
}
}
// Parse the content from the file index.txt
private function build($key)
{
$filePath = PATH_PAGES.$key.DS.FILENAME;
2017-06-25 22:54:59 +02:00
// Check if the file exists
2017-08-21 19:45:57 +02:00
if (!Sanitize::pathFile($filePath)) {
2017-05-09 22:57:56 +02:00
return false;
}
$tmp = 0;
$file = file($filePath);
2017-09-29 19:06:18 +02:00
foreach ($file as $lineNumber=>$line) {
2017-06-25 22:54:59 +02:00
// Split the line in 2 parts, limiter by :
$parts = explode(':', $line, 2);
2017-05-09 22:57:56 +02:00
2017-08-21 19:45:57 +02:00
$field = $parts[0]; // title, date, slug
$value = isset($parts[1])?$parts[1]:false; // value of title, value of date
2017-06-25 22:54:59 +02:00
2017-08-21 19:45:57 +02:00
// Remove all characters except letters and dash - from field
$field = preg_replace('/[^A-Za-z\-]/', '', $field);
// Field to lowercase
$field = Text::lowercase($field);
2017-05-09 22:57:56 +02:00
2017-06-25 22:54:59 +02:00
// Check if the current line start the content of the page
// We have two breakers, the word content or 3 dash ---
2017-09-29 19:06:18 +02:00
if ($field==='content') {
2017-05-09 22:57:56 +02:00
$tmp = $lineNumber;
2017-09-29 19:06:18 +02:00
$styleTypeUsed = 'Content:';
2017-05-09 22:57:56 +02:00
break;
}
2017-09-29 19:06:18 +02:00
if ($field==='---') {
$tmp = $lineNumber;
$styleTypeUsed = '---';
break;
}
if (!empty($field) && !empty($value)) {
2017-08-21 19:45:57 +02:00
// Remove missing dashs -
$field = preg_replace('/[^A-Za-z]/', '', $field);
// Remove <-- and -->
$value = preg_replace('/<\-\-/', '', $value);
$value = preg_replace('/\-\->/', '', $value);
2017-07-13 00:44:39 +02:00
2017-08-21 19:45:57 +02:00
// Remove empty spaces on borders
$value = trim($value);
2017-07-13 00:44:39 +02:00
2017-08-21 19:45:57 +02:00
// Position accepts only integers
if ($field=='position') {
2017-07-13 00:44:39 +02:00
$value = preg_replace('/[^0-9]/', '', $value);
}
2017-06-25 22:54:59 +02:00
// Sanitize all fields, except the content
2017-07-13 00:44:39 +02:00
$this->vars[$field] = Sanitize::html($value);
2017-05-09 22:57:56 +02:00
}
}
// Process the content
if ($tmp!==0) {
2017-09-29 19:06:18 +02:00
// Get all lines starting from "Content:" or "---"
$content = array_slice($file, $tmp);
2017-05-09 22:57:56 +02:00
2017-09-29 19:06:18 +02:00
// Remove "Content:" or "---" and keep next characters if there are
$content[0] = substr($content[0], strpos($content[0], $styleTypeUsed) + strlen($styleTypeUsed));
2017-10-05 17:07:24 +02:00
$content[0] = ltrim($content[0]);
2017-09-29 19:06:18 +02:00
// Join lines in one variable, this is RAW content from file
$this->vars['contentRaw'] = implode($content);
2017-05-09 22:57:56 +02:00
}
2017-05-16 00:46:20 +02:00
return true;
2017-05-09 22:57:56 +02:00
}
// Returns TRUE if the content is loaded correctly, FALSE otherwise
public function isValid()
{
2017-09-29 19:06:18 +02:00
return $this->vars!==false;
2017-05-09 22:57:56 +02:00
}
2017-07-06 23:27:22 +02:00
// DEPRACTED
2017-05-09 22:57:56 +02:00
// Returns the value from the $field, FALSE if the field doesn't exist
public function getField($field)
{
if(isset($this->vars[$field])) {
return $this->vars[$field];
}
return false;
}
public function getValue($field)
{
if(isset($this->vars[$field])) {
return $this->vars[$field];
}
return false;
}
2017-07-06 23:27:22 +02:00
public function getDB()
{
return $this->vars;
}
2017-05-09 22:57:56 +02:00
// Set a field with a value
public function setField($field, $value, $overwrite=true)
{
if($overwrite || empty($this->vars[$field])) {
$this->vars[$field] = $value;
}
return true;
}
// Returns the content
// This content is markdown parser
// (boolean) $fullContent, TRUE returns all content, if FALSE returns the first part of the content
// (boolean) $noSanitize, TRUE returns the content without sanitized
public function content($fullContent=true, $noSanitize=true)
{
// This content is not sanitized.
$content = $this->getValue('content');
2017-05-09 22:57:56 +02:00
if(!$fullContent) {
2017-05-10 20:40:28 +02:00
return $this->contentBreak();
2017-05-09 22:57:56 +02:00
}
if($noSanitize) {
return $content;
}
return Sanitize::html($content);
}
2017-05-10 20:40:28 +02:00
public function contentBreak()
{
return $this->getValue('contentBreak');
2017-05-10 20:40:28 +02:00
}
2017-05-09 22:57:56 +02:00
// Returns the raw content
// This content is not markdown parser
// (boolean) $noSanitize, TRUE returns the content without sanitized
public function contentRaw($noSanitize=true)
{
// This content is not sanitized.
$content = $this->getValue('contentRaw');
2017-05-09 22:57:56 +02:00
if($noSanitize) {
return $content;
}
return Sanitize::html($content);
}
// Returns the date according to locale settings and format settings
public function date()
{
return $this->getValue('date');
2017-05-09 22:57:56 +02:00
}
// Returns the date according to locale settings and format as database stored
// (string) $format, you can specify the date format
public function dateRaw($format=false)
{
$date = $this->getValue('dateRaw');
2017-05-09 22:57:56 +02:00
if($format) {
return Date::format($date, DB_DATE_FORMAT, $format);
}
return $date;
}
2017-08-03 23:28:32 +02:00
// Returns the date according to locale settings and format settings
public function dateModified()
{
return $this->getValue('dateModified');
}
2017-05-16 00:46:20 +02:00
// Returns the permalink
// (boolean) $absolute, TRUE returns the page link with the DOMAIN, FALSE without the DOMAIN
2017-05-30 20:28:55 +02:00
public function permalink($absolute=true)
2017-05-16 00:46:20 +02:00
{
// Get the key of the page
$key = $this->getValue('key');
2017-05-16 00:46:20 +02:00
if($absolute) {
return DOMAIN_PAGES.$key;
2017-05-16 00:46:20 +02:00
}
return HTML_PATH_ROOT.PAGE_URI_FILTER.$key;
2017-05-16 00:46:20 +02:00
}
2017-07-09 22:04:00 +02:00
// Returns the category name
public function category()
{
return $this->categoryMap('name');
}
2017-05-17 00:04:53 +02:00
// Returns the category key
public function categoryKey()
{
return $this->getValue('category');
2017-05-17 00:04:53 +02:00
}
// Returns the field from the array
// categoryMap = array( 'name'=>'', 'list'=>array() )
public function categoryMap($field)
{
$map = $this->getValue('categoryMap');
2017-05-17 00:04:53 +02:00
if($field=='key') {
return $this->categoryKey();
}
elseif($field=='name') {
return $map['name'];
}
elseif($field=='list') {
return $map['list'];
}
return false;
}
2017-05-09 22:57:56 +02:00
// Returns the user object
// (boolean) $field, TRUE returns the value of the field, FALSE returns the object
public function user($field=false)
{
// Get the user object.
$User = $this->getValue('user');
2017-05-09 22:57:56 +02:00
if($field) {
return $User->getField($field);
}
return $User;
}
// Returns the username who created the post/page
public function username()
{
return $this->getValue('username');
2017-05-09 22:57:56 +02:00
}
// Returns the description field
public function description()
{
return $this->getValue('description');
2017-05-09 22:57:56 +02:00
}
2015-03-08 18:02:59 +01:00
2017-05-09 22:57:56 +02:00
// Returns the tags
// (boolean) $returnsArray, TRUE to get the tags as an array, FALSE to get the tags separeted by comma
public function tags($returnsArray=false)
{
$tags = $this->getValue('tags');
2017-05-09 22:57:56 +02:00
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 json($returnsArray=false)
{
$tmp['key'] = $this->key();
$tmp['title'] = $this->title();
$tmp['content'] = $this->content(); // Markdown parsed
$tmp['contentRaw'] = $this->contentRaw(); // No Markdown parsed
$tmp['description'] = $this->description();
$tmp['date'] = $this->dateRaw();
$tmp['dateUTC'] = Date::convertToUTC($this->dateRaw(), DB_DATE_FORMAT, DB_DATE_FORMAT);
2017-05-09 22:57:56 +02:00
$tmp['permalink'] = $this->permalink(true);
if($returnsArray) {
return $tmp;
}
return json_encode($tmp);
}
2017-08-11 21:22:26 +02:00
// Returns the file name, FALSE there isn't a cover image setted
// If the user defined an External Cover Image the complete URL is going to be returned
2017-05-09 22:57:56 +02:00
// (boolean) $absolute, TRUE returns the absolute path and file name, FALSE just the file name
public function coverImage($absolute=true)
{
$fileName = $this->getValue('coverImage');
2017-08-11 21:22:26 +02:00
if (empty($fileName)) {
2017-05-09 22:57:56 +02:00
return false;
}
2017-08-11 21:22:26 +02:00
// Check if external cover image, is a valid URL
if (filter_var($fileName, FILTER_VALIDATE_URL)) {
return $fileName;
}
if ($absolute) {
return DOMAIN_UPLOADS.$fileName;
2017-05-09 22:57:56 +02:00
}
return $fileName;
}
// Returns TRUE if the content has the text splited
public function readMore()
{
return $this->getValue('readMore');
2017-05-09 22:57:56 +02:00
}
public function uuid()
{
return $this->getValue('uuid');
2017-05-09 22:57:56 +02:00
}
// Returns the field key
public function key()
{
return $this->getValue('key');
2017-05-09 22:57:56 +02:00
}
2017-06-22 23:50:12 +02:00
// (boolean) Returns TRUE if the page is published, FALSE otherwise
2017-05-09 22:57:56 +02:00
public function published()
{
return ($this->getValue('status')==='published');
2017-05-09 22:57:56 +02:00
}
2017-06-22 23:50:12 +02:00
// (boolean) Returns TRUE if the page is scheduled, FALSE otherwise
2017-05-09 22:57:56 +02:00
public function scheduled()
{
return ($this->getValue('status')==='scheduled');
2017-05-09 22:57:56 +02:00
}
2017-06-22 23:50:12 +02:00
// (boolean) Returns TRUE if the page is draft, FALSE otherwise
2017-05-09 22:57:56 +02:00
public function draft()
{
return ($this->getValue('status')=='draft');
2017-05-09 22:57:56 +02:00
}
2017-06-22 23:50:12 +02:00
// (boolean) Returns TRUE if the page is sticky, FALSE otherwise
public function sticky()
{
return ($this->getValue('status')=='sticky');
}
// (string) Returns status of the page
public function status()
{
return $this->getValue('status');
}
2017-05-09 22:57:56 +02:00
// Returns the title field
public function title()
{
return $this->getValue('title');
2015-05-19 01:22:05 +02:00
}
2017-05-30 20:28:55 +02:00
// Returns TRUE if the page has enabled the comments, FALSE otherwise
public function allowComments()
{
return $this->getValue('allowComments');
2017-05-30 20:28:55 +02:00
}
// Returns the page position
2015-05-05 03:00:01 +02:00
public function position()
{
return $this->getValue('position');
2015-03-08 18:02:59 +01:00
}
// Returns the page slug
2015-05-05 03:00:01 +02:00
public function slug()
2015-03-08 18:02:59 +01:00
{
$explode = explode('/', $this->getValue('key'));
2015-05-05 03:00:01 +02:00
2016-01-12 03:18:20 +01:00
// Check if the page have a parent.
2017-12-17 21:16:30 +01:00
if (!empty($explode[1])) {
2016-01-12 03:18:20 +01:00
return $explode[1];
2015-06-22 02:47:07 +02:00
}
2016-01-12 03:18:20 +01:00
return $explode[0];
2015-03-08 18:02:59 +01:00
}
// Returns the parent key, if the page doesn't have a parent returns FALSE
2015-05-05 03:00:01 +02:00
public function parentKey()
2015-03-08 18:02:59 +01:00
{
$explode = explode('/', $this->getValue('key'));
2017-12-17 21:16:30 +01:00
if (isset($explode[1])) {
2015-05-05 03:00:01 +02:00
return $explode[0];
}
2015-03-08 18:02:59 +01:00
return false;
}
2017-12-19 16:00:48 +01:00
// Returns TURE if the page has a parent, FALSE otherwise
public function hasParent()
{
return $this->parentKey()!==false;
}
// Returns the parent method output, if the page doesn't have a parent returns FALSE
2015-11-25 02:48:19 +01:00
public function parentMethod($method)
{
2017-07-11 23:53:53 +02:00
$parentKey = $this->parentKey();
2017-12-17 21:16:30 +01:00
if ($parentKey) {
2017-07-11 23:53:53 +02:00
$page = buildPage($parentKey);
return $page->{$method}();
2015-11-25 02:48:19 +01:00
}
return false;
}
// Returns an array with all children's key
2015-05-05 03:00:01 +02:00
public function children()
2015-03-08 18:02:59 +01:00
{
2018-01-21 23:23:22 +01:00
return $this->getValue('children');
2015-03-08 18:02:59 +01:00
}
2018-01-21 23:23:22 +01:00
2017-12-18 20:46:16 +01:00
public function subpages()
{
return $this->children();
}
2015-03-08 18:02:59 +01:00
2017-12-18 20:46:16 +01:00
// Returns TRUE if the page has subpages/children, FALSE otherwise
public function hasSubpages()
{
$subpages = $this->subpages();
return !empty($subpages);
}
2017-07-10 23:40:46 +02:00
2018-01-21 23:23:22 +01:00
public function isParent()
{
return $this->hasSubpages();
}
2017-07-10 23:40:46 +02:00
// Returns relative time (e.g. "1 minute ago")
// Based on http://stackoverflow.com/a/18602474
// Modified for Bludit
// $complete = false : short version
// $complete = true : full version
public function relativeTime($complete = false) {
$current = new DateTime;
$past = new DateTime($this->getValue('date'));
$elapsed = $current->diff($past);
$elapsed->w = floor($elapsed->d / 7);
$elapsed->d -= $elapsed->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach($string as $key => &$value) {
if($elapsed->$key) {
$value = $elapsed->$key . ' ' . $value . ($elapsed->$key > 1 ? 's' : ' ');
} else {
unset($string[$key]);
}
}
if(!$complete) {
$string = array_slice($string, 0 , 1);
}
return $string ? implode(', ', $string) . ' ago' : 'Just now';
}
2017-10-05 17:07:24 +02:00
}