bludit/bl-kernel/functions.php

913 lines
21 KiB
PHP
Raw Normal View History

2016-05-29 19:21:11 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
// 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));
2018-01-21 23:23:22 +01:00
// Get the keys of the child
2018-02-08 16:53:35 +01:00
$page->setField('childrenKeys', $dbPages->getChildren($key));
2018-01-21 23:23:22 +01:00
2017-05-10 20:40:28 +02:00
return $page;
2017-05-09 00:24:15 +02:00
}
// Re-index database of categories
2017-12-26 17:45:02 +01:00
// If you create/edit/remove a page is necessary regenerate the database of categories
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
}
// Re-index database of tags
// If you create/edit/remove a page is necessary regenerate the database of tags
function reindexTags() {
global $dbTags;
2017-06-23 00:41:00 +02:00
return $dbTags->reindex();
2017-03-26 20:51:32 +02:00
}
// Generate on the fly a 404 page-not-found
// Returns a Page-Object
function buildErrorPage() {
global $dbPages;
global $Language;
2017-09-21 20:42:03 +02:00
global $dbUsers;
$page = new Page(false);
2017-12-26 17:45:02 +01:00
$page->setField('title', $Language->get('page-not-found'));
$page->setField('content', $Language->get('page-not-found-content'));
$page->setField('user', $dbUsers->getUser('admin'));
return $page;
}
2017-12-26 17:45:02 +01:00
// This function is only used from the rule 69.pages.php, DO NOT use this function!
// This function generate a particular page from the current slug of the url
2017-12-26 17:45:02 +01:00
// The page is stored on the global variable $page
// If the slug has not a page associacted returns FALSE and is set not-found as true
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;
}
2017-12-26 17:45:02 +01:00
// The page is NOT published
elseif ( $page->scheduled() || $page->draft() ) {
$Url->setNotFound();
return false;
}
2017-12-26 17:45:02 +01:00
// The page was generate successfully
2017-10-04 23:42:14 +02:00
$content[0] = $pages[0] = $page;
return true;
}
2017-12-26 17:45:02 +01:00
// This function is only used from the rule 69.pages.php, DO NOT use this function!
function buildPagesForHome() {
2017-05-10 23:21:45 +02:00
return buildPagesFor('home');
2017-05-10 20:40:28 +02:00
}
2017-12-26 17:45:02 +01:00
// This function is only used from the rule 69.pages.php, DO NOT use this function!
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
}
2017-12-26 17:45:02 +01:00
// This function is only used from the rule 69.pages.php, DO NOT use this function!
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);
}
// This function is only used from the rule 69.pages.php, DO NOT use this function!
// Generate the global variables $pages / $content, defined on 69.pages.php
// This function is use for buildPagesForHome(), buildPagesByCategory(), buildPagesByTag()
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);
2018-03-27 18:40:03 +02:00
// Include sticky pages only in the first page
if ($pageNumber==1) {
$sticky = $dbPages->getStickyDB();
$list = array_merge($sticky, $list);
}
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
foreach ($list as $pageKey) {
2017-05-10 20:40:28 +02:00
$page = buildPage($pageKey);
if ($page!==false) {
2017-05-10 20:40:28 +02:00
array_push($pages, $page);
}
}
2017-10-04 23:42:14 +02:00
$content = $pages;
2017-05-10 20:40:28 +02:00
return $pages;
}
// Returns an array with all the static pages as Page-Object
// The static pages are order by position all the time
function buildStaticPages() {
global $dbPages;
$list = array();
$staticPages = $dbPages->getStaticDB();
foreach ($staticPages as $pageKey) {
$staticPage = buildPage($pageKey);
array_push($list, $staticPage);
}
return $list;
}
// Returns an array with all the parent pages as Page-Object
// The pages are order by the settings on the system
function buildParentPages() {
global $dbPages;
$list = array();
$pagesKey = $dbPages->getPublishedDB();
foreach ($pagesKey as $pageKey) {
$page = buildPage($pageKey);
if ($page->isParent()) {
array_push($list, $page);
}
}
return $list;
}
// DEPRECATED
2017-07-13 00:44:39 +02:00
// Generate the global variable $pagesByParent, defined on 69.pages.php
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));
}
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
}
}
}
}
// DEPRECATED
2017-07-11 23:53:53 +02:00
// Returns an Array with all pages existing on the system
/*
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 the Plugin-Object if is enabled and installed, FALSE otherwise
2017-12-27 14:19:28 +01:00
function getPlugin($pluginClassName) {
2017-12-20 20:37:17 +01:00
global $plugins;
2017-12-27 14:19:28 +01:00
if (pluginEnabled($pluginClassName)) {
return $plugins['all'][$pluginClassName];
2017-12-20 20:37:17 +01:00
}
return false;
}
2017-12-27 14:44:57 +01:00
// DEPRACTED
2017-12-27 14:19:28 +01:00
// Returns TRUE if the plugin is enabled and installed, FALSE otherwise
function pluginEnabled($pluginClassName) {
2017-12-27 14:44:57 +01:00
return pluginActivated($pluginClassName);
}
2017-12-27 14:44:57 +01:00
// Returns TRUE if the plugin is activaed / installed, FALSE otherwise
function pluginActivated($pluginClassName) {
global $plugins;
if (isset($plugins['all'][$pluginClassName])) {
return $plugins['all'][$pluginClassName]->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];
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;
}
2018-02-06 18:26:59 +01:00
function changePluginsPosition($pluginClassList) {
global $plugins;
global $syslog;
2018-02-06 18:26:59 +01:00
global $Language;
foreach ($pluginClassList as $position=>$pluginClassName) {
if (isset($plugins['all'][$pluginClassName])) {
$plugin = $plugins['all'][$pluginClassName];
$plugin->setPosition(++$position);
}
}
// Add to syslog
$syslog->add(array(
2018-02-06 18:26:59 +01:00
'dictionaryKey'=>'plugins-sorted',
'notes'=>''
));
return true;
}
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
2018-05-08 00:15:40 +02:00
// Check if the autosave page exists for this new page and delete it
$pageKey = $dbPages->getByUUID('autosave-'.$args['uuid']);
if (!empty($pageKey)) {
deletePage($pageKey);
}
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;
}
2018-05-08 23:25:18 +02:00
// // External Cover Image
// if ( !empty($args['externalCoverImage']) ) {
// $args['coverImage'] = $args['externalCoverImage'];
// unset($args['externalCoverImage']);
// }
2017-08-11 21:22:26 +02:00
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-06-23 00:41:00 +02:00
2018-05-08 00:15:40 +02:00
// Check if the autosave page exists for this new page and delete it
$pageKey = $dbPages->getByUUID('autosave-'.$args['uuid']);
if (!empty($pageKey)) {
deletePage($pageKey);
}
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;
}
2018-05-08 23:25:18 +02:00
// // External Cover Image
// if (!empty($args['externalCoverImage'])) {
// $args['coverImage'] = $args['externalCoverImage'];
// unset($args['externalCoverImage']);
// }
2017-08-11 21:22:26 +02:00
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;
2017-06-23 00:41:00 +02:00
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;
}
2018-05-14 00:00:10 +02:00
function editUser($args) {
global $dbUsers;
global $syslog;
2018-05-14 00:00:10 +02:00
if ($dbUsers->set($args)) {
$syslog->add(array(
2018-05-14 00:00:10 +02:00
'dictionaryKey'=>'user-edited',
'notes'=>$args['username']
));
return true;
}
return false;
}
2018-05-14 00:00:10 +02:00
function disableUser($args) {
global $dbUsers;
2018-05-14 00:00:10 +02:00
global $Login;
global $syslog;
2018-05-14 00:00:10 +02:00
// Arguments
$username = $args['username'];
// Only administrators can disable users
if ($Login->role()!=='admin') {
return false;
}
// Check if the username exists
if (!$dbUsers->exists($username)) {
return false;
}
// Disable the user
if ($dbUsers->disableUser($username)) {
$syslog->add(array(
2018-05-14 00:00:10 +02:00
'dictionaryKey'=>'user-disabled',
'notes'=>$username
));
return true;
}
return false;
}
2018-05-14 00:00:10 +02:00
function deleteUser($args) {
global $dbUsers, $dbPages;
global $Login;
global $syslog;
2018-05-14 00:00:10 +02:00
// Arguments
$username = $args['username'];
$deleteContent = isset($args['deleteContent']) ? $args['deleteContent'] : false;
// Only administrators can delete users
if ($Login->role()!=='admin') {
return false;
}
2018-05-14 00:00:10 +02:00
// The user admin cannot be deleted
if ($username=='admin') {
return false;
}
2018-05-14 00:00:10 +02:00
// Check if the username exists
if (!$dbUsers->exists($username)) {
return false;
}
2018-05-14 00:00:10 +02:00
if ($deleteContent) {
$dbPages->deletePagesByUser(array('username'=>$username));
} else {
$dbPages->transferPages(array('oldUsername'=>$username));
}
2018-05-14 00:00:10 +02:00
if ($dbUsers->delete($username)) {
$syslog->add(array(
'dictionaryKey'=>'user-deleted',
2018-05-14 00:00:10 +02:00
'notes'=>$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;
2017-07-05 19:59:51 +02:00
// Check empty username
if (Text::isEmpty($args['new_username'])) {
2017-07-05 19:59:51 +02:00
Alert::set($Language->g('username-field-is-empty'), ALERT_STATUS_FAIL);
return false;
}
// Check already exist username
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 (Text::length($args['new_password']) < PASSWORD_LENGTH) {
2017-10-07 21:49:41 +02:00
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']) {
2017-07-05 19:59:51 +02:00
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)) {
2017-07-05 19:59:51 +02:00
// 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']);
}
2018-03-06 19:42:18 +01:00
if (isset($args['uriTag'])) {
$args['uriTag'] = Text::addSlashes($args['uriTag']);
}
2018-03-06 19:42:18 +01:00
if (isset($args['uriCategory'])) {
$args['uriCategory'] = Text::addSlashes($args['uriCategory']);
}
if (isset($args['uriBlog'])) {
$args['uriBlog'] = Text::addSlashes($args['uriBlog']);
2018-03-06 19:42:18 +01:00
} else {
$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
$syslog->add(array(
2017-07-26 21:00:20 +02:00
'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
}
2018-05-15 20:12:15 +02:00
function changeUserPassword($args) {
global $dbUsers;
global $Language;
global $syslog;
2018-05-15 20:12:15 +02:00
// Arguments
$username = $args['username'];
$newPassword = $args['newPassword'];
$confirmPassword = $args['confirmPassword'];
// Password length
if (Text::length($newPassword) < 6) {
Alert::set($Language->g('Password must be at least 6 characters long'), ALERT_STATUS_FAIL);
return false;
}
if ($newPassword!=$confirmPassword) {
Alert::set($Language->g('The password and confirmation password do not match'), ALERT_STATUS_FAIL);
return false;
}
if ($dbUsers->setPassword(array('username'=>$username, 'password'=>$newPassword))) {
$syslog->add(array(
2018-05-15 20:12:15 +02:00
'dictionaryKey'=>'user-password-changed',
'notes'=>$username
));
Alert::set($Language->g('The changes have been saved'), ALERT_STATUS_OK);
return true;
}
return false;
}
2018-05-20 21:48:43 +02:00
// Returns true if the user is allowed to procceded
function checkRole($allowRoles, $redirect=true) {
global $Login;
global $Language;
global $syslog;
$userRole = $Login->role();
if (in_array($userRole, $allowRoles)) {
return true;
}
if ($redirect) {
$syslog->add(array(
2018-05-20 21:48:43 +02:00
'dictionaryKey'=>'access-deny',
'notes'=>$Login->username()
));
Alert::set($Language->g('You do not have sufficient permissions'));
Redirect::page('dashboard');
}
return false;
}
2017-11-01 19:38:56 +01:00
// Add a new category to the system
// Returns TRUE is successfully added, FALSE otherwise
2017-11-01 19:38:56 +01:00
function createCategory($category) {
global $dbCategories;
global $Language;
global $syslog;
2017-11-01 19:38:56 +01:00
if (Text::isEmpty($category)) {
Alert::set($Language->g('Category name is empty'), ALERT_STATUS_FAIL);
return false;
}
if ($dbCategories->add(array('name'=>$category))) {
$syslog->add(array(
2017-11-01 19:38:56 +01:00
'dictionaryKey'=>'new-category-created',
'notes'=>$category
));
Alert::set($Language->g('Category added'), ALERT_STATUS_OK);
return true;
}
Alert::set($Language->g('The category already exists'), ALERT_STATUS_FAIL);
2017-11-01 19:38:56 +01:00
return false;
}
function editCategory($args) {
2017-07-29 21:03:18 +02:00
global $Language;
global $dbPages;
global $dbCategories;
global $syslog;
2017-07-29 21:03:18 +02:00
if (Text::isEmpty($args['name']) || Text::isEmpty($args['newKey']) ) {
2017-07-29 21:03:18 +02:00
Alert::set($Language->g('Empty fields'));
return false;
}
$newCategoryKey = $dbCategories->edit($args);
if ($newCategoryKey==false) {
Alert::set($Language->g('The category already exists'));
2017-07-29 21:03:18 +02:00
return false;
}
// Change the category key in the pages database
$dbPages->changeCategory($args['oldKey'], $newCategoryKey);
2017-07-29 21:03:18 +02:00
$syslog->add(array(
2017-07-29 21:03:18 +02:00
'dictionaryKey'=>'category-edited',
'notes'=>$newCategoryKey
2017-07-29 21:03:18 +02:00
));
Alert::set($Language->g('The changes have been saved'));
return true;
}
function deleteCategory($args) {
2017-07-29 21:03:18 +02:00
global $Language;
global $dbCategories;
global $syslog;
2017-07-29 21:03:18 +02:00
// Remove the category by key
$dbCategories->remove($args['oldCategoryKey']);
// Remove the category from the pages ? or keep it if the user want to recovery the category ?
2017-07-29 21:03:18 +02:00
$syslog->add(array(
2017-07-29 21:03:18 +02:00
'dictionaryKey'=>'category-deleted',
'notes'=>$args['oldCategoryKey']
2017-07-29 21:03:18 +02:00
));
Alert::set($Language->g('The changes have been saved'));
return true;
}
2017-12-18 23:49:53 +01:00
// Returns an array with all the categories
// By default, the database of categories is alphanumeric sorted
function getCategories() {
global $dbCategories;
$list = array();
foreach ($dbCategories->db as $key=>$fields) {
$category = new Category($key);
array_push($list, $category);
}
return $list;
}
// Returns the object category if the category exists, FALSE otherwise
function getCategory($key) {
$category = new Category($key);
if (!$category->isValid()) {
return false;
}
return $category;
}
// Returns an array with all the tags
// By default, the database of tags is alphanumeric sorted
function getTags() {
global $dbTags;
$list = array();
foreach ($dbTags->db as $key=>$fields) {
$tag = new Tag($key);
array_push($list, $tag);
}
return $list;
}
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;
2017-12-27 14:19:28 +01:00
}