bludit/bl-kernel/functions.php

697 lines
16 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-09-21 20:42:03 +02:00
if (empty($key)) {
return false;
}
2017-05-10 20:40:28 +02:00
// Page object, content from index.txt file
$page = new Page($key);
2017-07-29 21:03:18 +02:00
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);
2017-07-29 21:03:18 +02:00
if (!$db) {
2017-05-09 00:24:15 +02:00
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-07-29 21:03:18 +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
$content = Text::imgRel2Abs($content, DOMAIN_UPLOADS); // Parse img src relative to absolute (with domain)
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
}
function reindexCategories() {
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
}
function reindexTags() {
global $dbTags;
2017-06-23 00:41:00 +02:00
return $dbTags->reindex();
2017-03-26 20:51:32 +02:00
}
function buildErrorPage() {
global $dbPages;
global $Language;
2017-09-21 20:42:03 +02:00
global $dbUsers;
$page = new Page(false);
$page->setField('title', $Language->get('page-not-found'));
$page->setField('content', $Language->get('page-not-found-content'));
2017-09-21 20:42:03 +02:00
$page->setField('user', $dbUsers->getUser('admin'));
return $page;
}
function buildThePage() {
global $Url;
global $page, $Page;
2017-10-04 23:42:14 +02:00
global $content, $pages;
$page = $Page = buildPage( $Url->slug() );
// The page doesn't exist
if($page===false) {
$Url->setNotFound();
return false;
}
// The page is not published
elseif( $page->scheduled() || $page->draft() ) {
$Url->setNotFound();
return false;
}
2017-10-04 23:42:14 +02:00
$content[0] = $pages[0] = $page;
return true;
}
function buildPagesForHome() {
2017-05-10 23:21:45 +02:00
return buildPagesFor('home');
2017-05-10 20:40:28 +02:00
}
function buildPagesByCategory() {
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
}
function buildPagesByTag() {
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;
2017-10-04 23:42:14 +02:00
global $content, $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();
if ($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
}
// There are not items, invalid tag, invalid category, out of range, etc...
2017-07-30 23:15:33 +02:00
if ($list===false) {
$Url->setNotFound();
return false;
2016-05-29 19:21:11 +02:00
}
2017-05-16 00:46:20 +02:00
$pages = array(); // global variable
2017-05-10 20:40:28 +02:00
foreach($list as $pageKey=>$fields) {
$page = buildPage($pageKey);
if($page!==false) {
array_push($pages, $page);
}
}
2017-10-04 23:42:14 +02:00
$content = $pages;
2017-05-10 20:40:28 +02:00
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
function buildPagesByParent($publishedPages=true, $staticPages=true) {
2017-07-10 23:40:46 +02:00
global $dbPages;
global $pagesByParent;
2017-07-13 00:44:39 +02:00
global $pagesByParentByKey;
2017-07-10 23:40:46 +02:00
$onlyKeys = true;
$keys = array();
if ($publishedPages) {
$keys = array_merge($keys, $dbPages->getPublishedDB($onlyKeys));
}
if ($staticPages) {
$keys = array_merge($keys, $dbPages->getStaticDB($onlyKeys));
}
foreach ($keys as $pageKey) {
2017-07-10 23:40:46 +02:00
$page = buildPage($pageKey);
if ($page!==false) {
$parentKey = $page->parentKey();
// FALSE if the page is parent
if ($parentKey===false) {
array_push($pagesByParent[PARENT], $page);
$pagesByParentByKey[PARENT][$page->key()] = $page;
} else {
if (!isset($pagesByParent[$parentKey])) {
$pagesByParent[$parentKey] = array();
2017-07-10 23:40:46 +02:00
}
array_push($pagesByParent[$parentKey], $page);
$pagesByParentByKey[$parentKey][$page->key()] = $page;
2017-07-10 23:40:46 +02:00
}
}
}
}
2017-10-13 00:15:13 +02:00
function buildStaticPages() {
global $dbPages;
$tmp = array();
$staticPages = $dbPages->getStaticDB($onlyKeys=true);
foreach ($staticPages as $pageKey) {
$staticPage = buildPage($pageKey);
array_push($tmp, $staticPage);
}
return $tmp;
}
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($publishedPages=true, $staticPages=true, $draftPages=true, $scheduledPages=true) {
2017-07-11 23:53:53 +02:00
global $dbPages;
// Get DB
$onlyKeys = true;
$keys = array();
if ($publishedPages) {
$keys = array_merge($keys, $dbPages->getPublishedDB($onlyKeys));
}
if ($staticPages) {
$keys = array_merge($keys, $dbPages->getStaticDB($onlyKeys));
}
if ($draftPages) {
$keys = array_merge($keys, $dbPages->getDraftDB($onlyKeys));
}
if ($scheduledPages) {
$keys = array_merge($keys, $dbPages->getScheduledDB($onlyKeys));
}
2017-07-11 23:53:53 +02:00
$tmp = array();
foreach ($keys as $pageKey) {
2017-07-11 23:53:53 +02:00
$page = buildPage($pageKey);
if ($page!==false) {
$tmp[$page->key()] = $page;
2017-07-11 23:53:53 +02:00
}
}
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 activatePlugin($pluginClassName) {
global $plugins;
global $Syslog;
global $Language;
// Check if the plugin exists
if (isset($plugins['all'][$pluginClassName])) {
$plugin = $plugins['all'][$pluginClassName];
$blackList = array('pluginTimeMachine', 'pluginRemoteContent');
if (in_array($pluginClassName, $blackList) && !defined('BLUDIT_PRO')) {
return false;
}
if ($plugin->install()) {
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'plugin-activated',
'notes'=>$plugin->name()
));
// Create an alert
Alert::set($Language->g('plugin-activated'));
return true;
}
}
return false;
}
function deactivatePlugin($pluginClassName) {
global $plugins;
global $Syslog;
global $Language;
// Check if the plugin exists
if (isset($plugins['all'][$pluginClassName])) {
$plugin = $plugins['all'][$pluginClassName];
if ($plugin->uninstall()) {
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'plugin-deactivated',
'notes'=>$plugin->name()
));
// Create an alert
Alert::set($Language->g('plugin-deactivated'));
return true;
}
}
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;
global $Language;
2017-06-22 23:50:12 +02:00
2017-07-05 19:59:51 +02:00
// The user is always the one loggued
$args['username'] = Session::get('username');
2017-09-22 23:11:08 +02:00
if ( empty($args['username']) ) {
Log::set('Function createPage()'.LOG_SEP.'Empty username.');
2017-07-05 19:59:51 +02:00
return false;
}
2017-08-11 21:22:26 +02:00
// External Cover Image
2017-09-22 23:11:08 +02:00
if ( !empty($args['externalCoverImage']) ) {
2017-08-11 21:22:26 +02:00
$args['coverImage'] = $args['externalCoverImage'];
unset($args['externalCoverImage']);
}
2017-06-22 23:50:12 +02:00
$key = $dbPages->add($args);
2017-07-29 01:20:47 +02:00
if ($key) {
2017-06-22 23:50:12 +02:00
// Call the plugins after page created
Theme::plugins('afterPageCreate');
// Re-index categories
reindexCategories();
// Re-index tags
reindextags();
// Add to syslog
$Syslog->add(array(
2017-10-02 22:42:18 +02:00
'dictionaryKey'=>'new-content-created',
2017-06-22 23:50:12 +02:00
'notes'=>$args['title']
));
2017-10-02 22:42:18 +02:00
Alert::set( $Language->g('new-content-created') );
2017-06-22 23:50:12 +02:00
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);
Log::set('Function createNewPage()'.LOG_SEP.'Cleaning finished...');
2017-06-22 23:50:12 +02:00
return false;
2017-06-23 00:41:00 +02:00
}
function editPage($args) {
global $dbPages;
global $Syslog;
2017-09-23 13:10:05 +02:00
// Check the key is not empty
if (empty($args['key'])) {
Log::set('Function editPage()'.LOG_SEP.'Empty key.');
return false;
}
// Check if the page key exist
if (!$dbPages->exists($args['key'])) {
Log::set('Function editPage()'.LOG_SEP.'Page key does not exist, '.$args['key']);
2017-07-05 19:59:51 +02:00
return false;
}
2017-08-11 21:22:26 +02:00
// External Cover Image
2017-09-22 23:11:08 +02:00
if ( !empty($args['externalCoverImage']) ) {
2017-08-11 21:22:26 +02:00
$args['coverImage'] = $args['externalCoverImage'];
unset($args['externalCoverImage']);
}
2017-09-23 13:10:05 +02:00
// Title and content need to be here because from inside the dbPages is not visible
if (empty($args['title']) || empty($args['content'])) {
$page = buildPage($args['key']);
if (empty($args['title'])) {
$args['title'] = $page->title();
}
if (empty($args['content'])) {
$args['content'] = $page->contentRaw();
}
}
2017-06-23 00:41:00 +02:00
$key = $dbPages->edit($args);
2017-08-11 21:22:26 +02:00
if ($key) {
2017-06-23 00:41:00 +02:00
// Call the plugins after page modified
Theme::plugins('afterPageModify');
// Re-index categories
reindexCategories();
// Re-index tags
reindextags();
// Add to syslog
$Syslog->add(array(
2017-10-02 22:42:18 +02:00
'dictionaryKey'=>'content-edited',
2017-06-23 00:41:00 +02:00
'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(
2017-10-02 22:42:18 +02:00
'dictionaryKey'=>'content-deleted',
2017-06-23 00:41:00 +02:00
'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
2017-10-07 21:49:41 +02:00
if( Text::length($args['new_password']) < PASSWORD_LENGTH ) {
Alert::set($Language->g('Password must be at least '.PASSWORD_LENGTH.' characters long'), ALERT_STATUS_FAIL);
2017-07-05 19:59:51 +02:00
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-created',
2017-07-05 19:59:51 +02:00
'notes'=>$tmp['username']
));
return true;
}
2017-07-26 21:00:20 +02:00
return false;
}
function editSettings($args) {
global $Site;
global $Syslog;
global $Language;
global $dbPages;
2017-07-26 21:00:20 +02:00
if (isset($args['language'])) {
if ($args['language']!=$Site->language()) {
$tmp = new dbJSON(PATH_LANGUAGES.$args['language'].'.json', false);
if (isset($tmp->db['language-data']['locale'])) {
$args['locale'] = $tmp->db['language-data']['locale'];
} else {
$args['locale'] = $args['language'];
}
}
}
if (isset($args['uriPage'])) {
$args['uriPage'] = Text::addSlashes($args['uriPage']);
}
if (isset($args['uriTag'])) {
$args['uriTag'] = Text::addSlashes($args['uriTag']);
}
if (isset($args['uriCategory'])) {
$args['uriCategory'] = Text::addSlashes($args['uriCategory']);
}
if (isset($args['uriBlog'])) {
$args['uriBlog'] = Text::addSlashes($args['uriBlog']);
}
if ($Site->set($args)) {
// Check current order-by if changed it reorder the content
if ($Site->orderBy()!=ORDER_BY) {
if ($Site->orderBy()=='date') {
$dbPages->sortByDate();
} else {
$dbPages->sortByPosition();
}
$dbPages->save();
}
// Add syslog
2017-07-26 21:00:20 +02:00
$Syslog->add(array(
'dictionaryKey'=>'changes-on-settings',
'notes'=>''
));
// Create alert
Alert::set($Language->g('The changes have been saved'));
2017-07-26 21:00:20 +02:00
return true;
}
return false;
2017-07-29 21:03:18 +02:00
}
function editCategory($oldCategoryKey, $newCategory) {
global $Language;
global $dbPages;
global $dbCategories;
global $Syslog;
if( Text::isEmpty($oldCategoryKey) || Text::isEmpty($newCategory) ) {
Alert::set($Language->g('Empty fields'));
return false;
}
if( $dbCategories->edit($oldCategoryKey, $newCategory) == false ) {
Alert::set($Language->g('Already exist a category'));
return false;
}
$dbPages->changeCategory($oldCategoryKey, $newCategory);
$Syslog->add(array(
'dictionaryKey'=>'category-edited',
'notes'=>$newCategory
));
Alert::set($Language->g('The changes have been saved'));
return true;
}
function deleteCategory($categoryKey) {
global $Language;
global $dbCategories;
global $Syslog;
// Remove the category by key
$dbCategories->remove($categoryKey);
$Syslog->add(array(
'dictionaryKey'=>'category-deleted',
'notes'=>$categoryKey
));
Alert::set($Language->g('The changes have been saved'));
return true;
}
function activateTheme($themeDirectory) {
global $Site;
global $Syslog;
if (Sanitize::pathFile(PATH_THEMES.$themeDirectory)) {
$Site->set(array('theme'=>$themeDirname));
$Syslog->add(array(
'dictionaryKey'=>'new-theme-configured',
'notes'=>$themeDirname
));
Alert::set( $Language->g('The changes have been saved') );
return true;
}
return false;
}