bludit/bl-kernel/functions.php

448 lines
9.7 KiB
PHP
Raw Normal View History

2016-05-29 19:21:11 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2017-07-07 23:38:01 +02:00
// (object) Returns a Page object, the class is page.class.php, FALSE if something fail to load the page
2017-07-10 23:40:46 +02:00
function buildPage($key) {
2017-05-09 00:24:15 +02:00
global $dbPages;
global $dbUsers;
2017-05-17 00:04:53 +02:00
global $dbCategories;
2017-05-09 00:24:15 +02:00
global $Parsedown;
global $Site;
2017-05-10 20:40:28 +02:00
// Page object, content from index.txt file
$page = new Page($key);
if( !$page->isValid() ) {
2017-05-09 00:24:15 +02:00
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from file with key: '.$key);
return false;
}
2017-05-10 20:40:28 +02:00
// Get the database from dbPages
2017-05-09 00:24:15 +02:00
$db = $dbPages->getPageDB($key);
if( !$db ) {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key);
return false;
}
2017-05-10 20:40:28 +02:00
// Foreach field from database set on the object
2017-05-09 00:24:15 +02:00
foreach($db as $field=>$value) {
2017-05-10 20:40:28 +02:00
$page->setField($field, $value);
2017-05-09 00:24:15 +02:00
}
2017-05-10 20:40:28 +02:00
// Parse Markdown
$contentRaw = $page->contentRaw();
2017-05-09 00:24:15 +02:00
$content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities
2017-05-10 20:40:28 +02:00
$content = $Parsedown->text($content); // Parse Markdown
2017-05-09 00:24:15 +02:00
$content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute.
2017-05-10 20:40:28 +02:00
$page->setField('content', $content, true);
2017-05-09 00:24:15 +02:00
// Pagebrake
$explode = explode(PAGE_BREAK, $content);
2017-05-10 20:40:28 +02:00
$page->setField('contentBreak', $explode[0], true);
$page->setField('readMore', !empty($explode[1]), true);
2017-05-09 00:24:15 +02:00
// Date format
2017-05-10 20:40:28 +02:00
$pageDate = $page->date();
$page->setField('dateRaw', $pageDate, true);
2017-05-09 00:24:15 +02:00
2017-05-10 20:40:28 +02:00
$pageDateFormated = $page->dateRaw( $Site->dateFormat() );
$page->setField('date', $pageDateFormated, true);
2017-05-09 00:24:15 +02:00
2017-05-10 20:40:28 +02:00
// Generate and set the User object
$username = $page->username();
$page->setField('user', $dbUsers->getUser($username));
2017-05-09 00:24:15 +02:00
2017-05-17 00:04:53 +02:00
// Category
$categoryKey = $page->categoryKey();
$page->setField('categoryMap', $dbCategories->getMap($categoryKey));
2017-05-10 20:40:28 +02:00
return $page;
2017-05-09 00:24:15 +02:00
}
2017-05-10 20:40:28 +02:00
function reindexCategories()
2016-05-29 19:21:11 +02:00
{
2017-05-10 20:40:28 +02:00
global $dbCategories;
2017-06-23 00:41:00 +02:00
return $dbCategories->reindex();
2016-05-29 19:21:11 +02:00
}
2017-05-10 20:40:28 +02:00
function reindexTags()
2017-03-26 20:51:32 +02:00
{
global $dbTags;
2017-06-23 00:41:00 +02:00
return $dbTags->reindex();
2017-03-26 20:51:32 +02:00
}
2017-05-10 23:21:45 +02:00
function buildPagesForAdmin()
{
return buildPagesFor('admin');
}
function buildPagesForHome()
2016-05-29 19:21:11 +02:00
{
2017-05-10 23:21:45 +02:00
return buildPagesFor('home');
2017-05-10 20:40:28 +02:00
}
2017-05-12 20:18:44 +02:00
function buildPagesByCategory()
2017-05-10 20:40:28 +02:00
{
2017-05-12 20:18:44 +02:00
global $Url;
$categoryKey = $Url->slug();
2017-05-10 23:21:45 +02:00
return buildPagesFor('category', $categoryKey, false);
2017-05-10 20:40:28 +02:00
}
2017-05-12 20:18:44 +02:00
function buildPagesByTag()
2017-05-10 23:21:45 +02:00
{
2017-05-12 20:18:44 +02:00
global $Url;
$tagKey = $Url->slug();
2017-05-10 23:21:45 +02:00
return buildPagesFor('tag', false, $tagKey);
}
function buildPagesFor($for, $categoryKey=false, $tagKey=false)
2017-05-10 20:40:28 +02:00
{
global $dbPages;
2017-05-10 23:21:45 +02:00
global $dbCategories;
2017-05-31 20:17:21 +02:00
global $dbTags;
2016-05-29 19:21:11 +02:00
global $Site;
2017-05-16 00:46:20 +02:00
global $Url;
global $pagesByKey;
2017-05-16 00:46:20 +02:00
global $pages;
2016-05-29 19:21:11 +02:00
2017-05-10 23:21:45 +02:00
// Get the page number from URL
$pageNumber = $Url->pageNumber();
2017-05-10 20:40:28 +02:00
if($for=='admin') {
2017-05-10 23:21:45 +02:00
$onlyPublished = false;
$amountOfItems = ITEMS_PER_PAGE_ADMIN;
$list = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
2016-05-29 19:21:11 +02:00
}
2017-05-10 20:40:28 +02:00
elseif($for=='home') {
2017-05-10 23:21:45 +02:00
$onlyPublished = true;
2017-05-16 00:46:20 +02:00
$amountOfItems = $Site->itemsPerPage();
$list = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
2017-05-10 23:21:45 +02:00
}
elseif($for=='category') {
2017-05-16 00:46:20 +02:00
$amountOfItems = $Site->itemsPerPage();
2017-05-10 23:21:45 +02:00
$list = $dbCategories->getList($categoryKey, $pageNumber, $amountOfItems);
}
elseif($for=='tag') {
2017-05-16 00:46:20 +02:00
$amountOfItems = $Site->itemsPerPage();
2017-05-10 23:21:45 +02:00
$list = $dbTags->getList($tagKey, $pageNumber, $amountOfItems);
2016-05-29 19:21:11 +02:00
}
2017-05-10 20:40:28 +02:00
// There are not items for the page number then set the page notfound
2017-05-24 00:48:29 +02:00
if( empty($list) && $pageNumber>1 ) {
2017-05-10 20:40:28 +02:00
$Url->setNotFound(true);
2016-05-29 19:21:11 +02:00
}
2017-05-16 00:46:20 +02:00
$pages = array(); // global variable
$pagesByKey = array(); // global variable
2017-05-10 20:40:28 +02:00
foreach($list as $pageKey=>$fields) {
$page = buildPage($pageKey);
if($page!==false) {
// $pagesByKey
$pagesByKey[$pageKey] = $page;
2017-07-10 23:40:46 +02:00
2017-05-16 00:46:20 +02:00
// $pages
2017-05-10 20:40:28 +02:00
array_push($pages, $page);
}
}
return $pages;
}
2017-07-13 00:44:39 +02:00
// Generate the global variable $pagesByParent, defined on 69.pages.php
// (boolean) $allPages, TRUE include all status, FALSE only include published status
2017-07-10 23:40:46 +02:00
function buildPagesByParent($allPages=true) {
global $dbPages;
global $pagesByParent;
2017-07-13 00:44:39 +02:00
global $pagesByParentByKey;
2017-07-10 23:40:46 +02:00
$keys = array_keys($dbPages->db);
foreach($keys as $pageKey) {
$page = buildPage($pageKey);
if($page!==false) {
if($allPages || $page->published()) {
$parentKey = $page->parentKey();
// FALSE if the page is parent
if($parentKey===false) {
array_push($pagesByParent[PARENT], $page);
2017-07-13 00:44:39 +02:00
$pagesByParentByKey[PARENT][$page->key()] = $page;
2017-07-10 23:40:46 +02:00
} else {
if( !isset($pagesByParent[$parentKey]) ) {
$pagesByParent[$parentKey] = array();
}
array_push($pagesByParent[$parentKey], $page);
2017-07-13 00:44:39 +02:00
$pagesByParentByKey[$parentKey][$page->key()] = $page;
2017-07-10 23:40:46 +02:00
}
}
}
}
}
2017-07-11 23:53:53 +02:00
// Returns an Array with all pages existing on the system
// (boolean) $allPages, TRUE returns all pages with any status, FALSE all published pages
/*
array(
pageKey1 => Page object,
pageKey2 => Page object,
...
pageKeyN => Page object,
)
*/
function buildAllpages($allPages=true) {
global $dbPages;
$tmp = array();
$keys = array_keys($dbPages->db);
foreach($keys as $pageKey) {
$page = buildPage($pageKey);
if($page!==false) {
if($allPages || $page->published()) {
$tmp[$page->key()] = $page;
}
}
}
return $tmp;
}
// Returns TRUE if the plugin is enabled, FALSE otherwise
function pluginEnabled($pluginName) {
global $plugins;
$pluginClass = 'plugin'.Text::firstCharUp($pluginName);
if( isset($plugins['all'][$pluginClass]) ) {
return $plugins['all'][$pluginClass]->installed();
}
return false;
}
function printDebug($array) {
echo '<pre>';
var_dump($array);
echo '</pre>';
2017-06-22 23:50:12 +02:00
}
2017-07-05 19:59:51 +02:00
function createPage($args) {
2017-06-22 23:50:12 +02:00
global $dbPages;
global $Syslog;
2017-07-05 19:59:51 +02:00
// The user is always the one loggued
$args['username'] = Session::get('username');
if( Text::isEmpty($args['username']) ) {
return false;
}
2017-06-22 23:50:12 +02:00
$key = $dbPages->add($args);
if($key) {
// Call the plugins after page created
Theme::plugins('afterPageCreate');
// Re-index categories
reindexCategories();
// Re-index tags
reindextags();
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'new-page-created',
'notes'=>$args['title']
));
return $key;
}
Log::set('Function createNewPage()'.LOG_SEP.'Error occurred when trying to create the page');
Log::set('Function createNewPage()'.LOG_SEP.'Cleaning database...');
$dbPages->delete($key);
return false;
2017-06-23 00:41:00 +02:00
}
function editPage($args) {
global $dbPages;
global $Syslog;
2017-07-05 19:59:51 +02:00
// The user is always the one loggued
$args['username'] = Session::get('username');
if( Text::isEmpty($args['username']) ) {
2017-07-07 23:38:01 +02:00
Log::set('Function editPage()'.LOG_SEP.'Empty username.');
2017-07-05 19:59:51 +02:00
return false;
}
2017-06-23 00:41:00 +02:00
if(!isset($args['parent'])) {
2017-07-13 00:44:39 +02:00
$args['parent'] = '';
2017-06-23 00:41:00 +02:00
}
$key = $dbPages->edit($args);
if($key) {
// Call the plugins after page modified
Theme::plugins('afterPageModify');
// Re-index categories
reindexCategories();
// Re-index tags
reindextags();
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'page-edited',
'notes'=>$args['title']
));
return $key;
}
2017-07-07 23:38:01 +02:00
Log::set('Function editPage()'.LOG_SEP.'ERROR: Something happen when try to edit the page.');
2017-06-23 00:41:00 +02:00
return false;
}
function deletePage($key) {
global $dbPages;
global $Syslog;
if( $dbPages->delete($key) ) {
// Call the plugins after page deleted
Theme::plugins('afterPageDelete');
// Re-index categories
reindexCategories();
// Re-index tags
reindextags();
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'page-deleted',
'notes'=>$key
));
return true;
}
return false;
}
function disableUser($username) {
global $dbUsers;
global $Login;
global $Syslog;
// The editors can't disable users
if($Login->role()!=='admin') {
return false;
}
if( $dbUsers->disableUser($username) ) {
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'user-disabled',
'notes'=>$username
));
return true;
}
return false;
}
function editUser($args) {
global $dbUsers;
global $Syslog;
if( $dbUsers->set($args) ) {
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'user-edited',
'notes'=>$args['username']
));
return true;
}
return false;
}
2017-07-07 23:38:01 +02:00
function deleteUser($args, $deleteContent=false) {
global $dbUsers;
global $Login;
global $Syslog;
// The user admin cannot be deleted
if($args['username']=='admin') {
return false;
}
// The editors can't delete users
if($Login->role()!=='admin') {
return false;
}
if($deleteContent) {
//$dbPosts->deletePostsByUser($args['username']);
}
else {
//$dbPosts->linkPostsToUser($args['username'], 'admin');
}
if( $dbUsers->delete($args['username']) ) {
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'user-deleted',
'notes'=>$args['username']
));
return true;
}
2017-07-05 19:59:51 +02:00
return false;
}
2017-07-05 23:30:30 +02:00
function createUser($args) {
2017-07-05 19:59:51 +02:00
global $dbUsers;
global $Language;
global $Syslog;
// Check empty username
if( Text::isEmpty($args['new_username']) ) {
Alert::set($Language->g('username-field-is-empty'), ALERT_STATUS_FAIL);
return false;
}
// Check already exist username
2017-07-06 23:27:22 +02:00
if( $dbUsers->exists($args['new_username']) ) {
2017-07-05 19:59:51 +02:00
Alert::set($Language->g('username-already-exists'), ALERT_STATUS_FAIL);
return false;
}
// Password length
if( strlen($args['new_password']) < 6 ) {
Alert::set($Language->g('Password must be at least 6 characters long'), ALERT_STATUS_FAIL);
return false;
}
// Check new password and confirm password are equal
if( $args['new_password'] != $args['confirm_password'] ) {
Alert::set($Language->g('The password and confirmation password do not match'), ALERT_STATUS_FAIL);
return false;
}
// Filter form fields
$tmp = array();
$tmp['username'] = $args['new_username'];
$tmp['password'] = $args['new_password'];
$tmp['role'] = $args['role'];
$tmp['email'] = $args['email'];
// Add the user to the database
if( $dbUsers->add($tmp) ) {
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'new-user',
'notes'=>$tmp['username']
));
return true;
}
return false;
}