bludit/bl-kernel/functions.php

916 lines
21 KiB
PHP
Raw Normal View History

2016-05-29 19:21:11 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
// 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() {
2018-08-02 22:33:53 +02:00
global $categories;
return $categories->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() {
2018-08-02 22:33:53 +02:00
global $tags;
return $tags->reindex();
2017-03-26 20:51:32 +02:00
}
// Generate the page 404 Not found
function buildErrorPage() {
global $site;
global $L;
try {
$pageNotFoundKey = $site->pageNotFound();
2018-08-02 17:06:53 +02:00
$pageNotFound = New Page($pageNotFoundKey);
} catch (Exception $e) {
2018-08-02 17:06:53 +02:00
$pageNotFound = New Page(false);
$pageNotFound->setField('title', $L->get('page-not-found'));
$pageNotFound->setField('content', $L->get('page-not-found-content'));
2018-07-25 23:42:00 +02:00
$pageNotFound->setField('username', 'admin');
}
return $pageNotFound;
}
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
2020-06-04 08:59:07 +02:00
// If the slug has not a page associated returns FALSE and set not-found as true
function buildThePage() {
global $url;
try {
$pageKey = $url->slug();
2018-08-02 17:06:53 +02:00
$page = New Page($pageKey);
} catch (Exception $e) {
$url->setNotFound();
return false;
}
2019-05-26 23:09:38 +02:00
if ($page->draft() || $page->scheduled() || $page->autosave()) {
2019-05-09 17:20:35 +02:00
if ($url->parameter('preview')!==md5($page->uuid())) {
$url->setNotFound();
return false;
}
}
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!
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() {
global $url;
2017-05-12 20:18:44 +02:00
$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() {
global $url;
2017-05-12 20:18:44 +02:00
$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 $content / $content, defined on 69.pages.php
// This function is use for buildPagesForHome(), buildPagesByCategory(), buildPagesByTag()
function buildPagesFor($for, $categoryKey=false, $tagKey=false) {
2018-08-03 18:59:23 +02:00
global $pages;
2018-08-02 22:33:53 +02:00
global $categories;
global $tags;
global $site;
global $url;
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 23:21:45 +02:00
if ($for=='home') {
2017-05-10 23:21:45 +02:00
$onlyPublished = true;
2018-08-06 21:46:58 +02:00
$numberOfItems = $site->itemsPerPage();
$list = $pages->getList($pageNumber, $numberOfItems, $onlyPublished);
2018-03-27 18:40:03 +02:00
// Include sticky pages only in the first page
if ($pageNumber==1) {
2018-08-03 18:59:23 +02:00
$sticky = $pages->getStickyDB();
2018-03-27 18:40:03 +02:00
$list = array_merge($sticky, $list);
}
2017-05-10 23:21:45 +02:00
}
elseif ($for=='category') {
2018-08-06 21:46:58 +02:00
$numberOfItems = $site->itemsPerPage();
$list = $categories->getList($categoryKey, $pageNumber, $numberOfItems);
2017-05-10 23:21:45 +02:00
}
elseif ($for=='tag') {
2018-08-06 21:46:58 +02:00
$numberOfItems = $site->itemsPerPage();
$list = $tags->getList($tagKey, $pageNumber, $numberOfItems);
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
}
$content = array();
foreach ($list as $pageKey) {
try {
2018-08-02 17:06:53 +02:00
$page = new Page($pageKey);
if ( ($page->type()=='published') ||
($page->type()=='sticky') ||
($page->type()=='static')
) {
array_push($content, $page);
}
} catch (Exception $e) {
// continue
2017-05-10 20:40:28 +02:00
}
}
2018-10-18 19:21:57 +02:00
return $content;
2017-05-10 20:40:28 +02:00
}
// Returns an array with all the static pages as Page-Object
// The static pages are order by position all the time
function buildStaticPages() {
2018-08-03 18:59:23 +02:00
global $pages;
$list = array();
2018-08-03 18:59:23 +02:00
$pagesKey = $pages->getStaticDB();
foreach ($pagesKey as $pageKey) {
try {
2018-08-02 17:06:53 +02:00
$page = new Page($pageKey);
array_push($list, $page);
} catch (Exception $e) {
// continue
}
}
return $list;
}
2018-08-02 17:06:53 +02:00
// Returns the Page-Object if exists, FALSE otherwise
function buildPage($pageKey) {
try {
$page = new Page($pageKey);
return $page;
} catch (Exception $e) {
return false;
}
}
// Returns an array with all the parent pages as Page-Object
// The pages are order by the settings on the system
function buildParentPages() {
2018-08-03 18:59:23 +02:00
global $pages;
$list = array();
2018-08-03 18:59:23 +02:00
$pagesKey = $pages->getPublishedDB();
foreach ($pagesKey as $pageKey) {
try {
2018-08-02 17:06:53 +02:00
$page = new Page($pageKey);
2018-09-09 17:53:08 +02:00
if ($page->isParent()) {
array_push($list, $page);
}
} catch (Exception $e) {
// continue
}
}
return $list;
}
// 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;
2018-09-03 18:31:46 +02:00
if (pluginActivated($pluginClassName)) {
2017-12-27 14:19:28 +01:00
return $plugins['all'][$pluginClassName];
2017-12-20 20:37:17 +01:00
}
return false;
}
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 $L;
// 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($L->g('plugin-activated'));
return true;
}
}
return false;
}
function deactivatePlugin($pluginClassName) {
global $plugins;
global $syslog;
global $L;
// 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($L->g('plugin-deactivated'));
return true;
}
}
return false;
}
2019-11-18 19:41:54 +01:00
function deactivateAllPlugin() {
global $plugins;
global $syslog;
global $L;
// Check if the plugin exists
foreach ($plugins['all'] as $plugin) {
if ($plugin->uninstall()) {
// Add to syslog
$syslog->add(array(
'dictionaryKey'=>'plugin-deactivated',
'notes'=>$plugin->name()
));
}
}
return false;
}
2018-02-06 18:26:59 +01:00
function changePluginsPosition($pluginClassList) {
global $plugins;
global $syslog;
global $L;
2018-02-06 18:26:59 +01:00
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'=>''
));
Alert::set($L->g('The changes have been saved'));
2018-02-06 18:26:59 +01:00
return true;
}
2018-09-14 15:48:57 +02:00
/*
Create a new page
The array $args support all the keys from variable $dbFields of the class pages.class.php
If you don't pass all the keys, the default values are used, the default values are from $dbFields in the class pages.class.php
*/
2017-07-05 19:59:51 +02:00
function createPage($args) {
2018-08-03 18:59:23 +02:00
global $pages;
global $syslog;
global $L;
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
if (isset($args['uuid'])) {
2018-08-03 18:59:23 +02:00
$autosaveKey = $pages->getByUUID('autosave-'.$args['uuid']);
if (!empty($autosaveKey)) {
Log::set('Function createPage()'.LOG_SEP.'Autosave deleted for '.$args['title'], LOG_TYPE_INFO);
deletePage($autosaveKey);
}
2018-05-08 00:15:40 +02:00
}
2020-06-04 08:59:07 +02:00
// The user is always the one logged
2017-07-05 19:59:51 +02:00
$args['username'] = Session::get('username');
2018-07-11 23:36:46 +02:00
if (empty($args['username'])) {
Log::set('Function createPage()'.LOG_SEP.'Empty username.', LOG_TYPE_ERROR);
2017-07-05 19:59:51 +02:00
return false;
}
2018-08-03 18:59:23 +02:00
$key = $pages->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');
reindexCategories();
2019-02-22 14:25:40 +01:00
reindexTags();
2017-06-22 23:50:12 +02:00
// Add to syslog
$syslog->add(array(
2017-10-02 22:42:18 +02:00
'dictionaryKey'=>'new-content-created',
2019-02-20 23:13:59 +01:00
'notes'=>(empty($args['title'])?$key:$args['title'])
2017-06-22 23:50:12 +02:00
));
return $key;
}
2018-07-11 23:36:46 +02:00
Log::set('Function createNewPage()'.LOG_SEP.'Error occurred when trying to create the page', LOG_TYPE_ERROR);
Log::set('Function createNewPage()'.LOG_SEP.'Cleaning database...', LOG_TYPE_ERROR);
deletePage($key);
Log::set('Function createNewPage()'.LOG_SEP.'Cleaning finished...', LOG_TYPE_ERROR);
2017-06-22 23:50:12 +02:00
return false;
2017-06-23 00:41:00 +02:00
}
function editPage($args) {
2018-08-03 18:59:23 +02:00
global $pages;
global $syslog;
2017-06-23 00:41:00 +02:00
2019-05-26 23:09:38 +02:00
// Check if the autosave/preview page exists for this new page and delete it
if (isset($args['uuid'])) {
2019-02-25 16:32:00 +01:00
$autosaveKey = $pages->getByUUID('autosave-'.$args['uuid']);
if ($autosaveKey) {
2019-05-26 23:09:38 +02:00
Log::set('Function editPage()'.LOG_SEP.'Autosave/Preview deleted for '.$autosaveKey, LOG_TYPE_INFO);
2019-02-25 16:32:00 +01:00
deletePage($autosaveKey);
}
2018-05-08 00:15:40 +02:00
}
2018-07-11 23:36:46 +02:00
// Check if the key is not empty
2017-09-23 13:10:05 +02:00
if (empty($args['key'])) {
2018-07-11 23:36:46 +02:00
Log::set('Function editPage()'.LOG_SEP.'Empty key.', LOG_TYPE_ERROR);
2017-09-23 13:10:05 +02:00
return false;
}
// Check if the page key exist
2018-08-03 18:59:23 +02:00
if (!$pages->exists($args['key'])) {
2018-07-11 23:36:46 +02:00
Log::set('Function editPage()'.LOG_SEP.'Page key does not exist, '.$args['key'], LOG_TYPE_ERROR);
2017-07-05 19:59:51 +02:00
return false;
}
2018-08-03 18:59:23 +02:00
$key = $pages->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');
reindexCategories();
2019-02-22 14:25:40 +01:00
reindexTags();
2017-06-23 00:41:00 +02:00
// Add to syslog
$syslog->add(array(
2017-10-02 22:42:18 +02:00
'dictionaryKey'=>'content-edited',
2019-02-25 16:32:00 +01:00
'notes'=>empty($args['title'])?$key:$args['title']
2017-06-23 00:41:00 +02:00
));
return $key;
}
2018-07-11 23:36:46 +02:00
Log::set('Function editPage()'.LOG_SEP.'Something happen when try to edit the page.', LOG_TYPE_ERROR);
2017-06-23 00:41:00 +02:00
return false;
}
function deletePage($key) {
2018-08-03 18:59:23 +02:00
global $pages;
global $syslog;
2017-06-23 00:41:00 +02:00
2018-08-03 18:59:23 +02:00
if ($pages->delete($key)) {
2017-06-23 00:41:00 +02:00
// Call the plugins after page deleted
Theme::plugins('afterPageDelete');
reindexCategories();
2019-02-22 14:25:40 +01:00
reindexTags();
2017-06-23 00:41:00 +02:00
// 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) {
2018-08-03 18:59:23 +02:00
global $users;
global $syslog;
2018-08-03 18:59:23 +02:00
if ($users->set($args)) {
2018-07-17 23:58:01 +02:00
// Add to syslog
$syslog->add(array(
2018-05-14 00:00:10 +02:00
'dictionaryKey'=>'user-edited',
'notes'=>$args['username']
));
2018-07-17 23:58:01 +02:00
return true;
}
return false;
}
2018-05-14 00:00:10 +02:00
function disableUser($args) {
2018-08-03 18:59:23 +02:00
global $users;
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') {
2018-05-14 00:00:10 +02:00
return false;
}
// Check if the username exists
2018-08-03 18:59:23 +02:00
if (!$users->exists($username)) {
2018-05-14 00:00:10 +02:00
return false;
}
// Disable the user
2018-08-03 18:59:23 +02:00
if ($users->disableUser($username)) {
2018-07-17 23:58:01 +02:00
// Add to syslog
$syslog->add(array(
2018-05-14 00:00:10 +02:00
'dictionaryKey'=>'user-disabled',
'notes'=>$username
));
2018-07-17 23:58:01 +02:00
return true;
}
return false;
}
2018-05-14 00:00:10 +02:00
function deleteUser($args) {
2018-08-03 18:59:23 +02:00
global $users, $pages;
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
2018-08-03 18:59:23 +02:00
if (!$users->exists($username)) {
2018-05-14 00:00:10 +02:00
return false;
}
2018-05-14 00:00:10 +02:00
if ($deleteContent) {
2018-08-03 18:59:23 +02:00
$pages->deletePagesByUser(array('username'=>$username));
2018-05-14 00:00:10 +02:00
} else {
2018-08-03 18:59:23 +02:00
$pages->transferPages(array('oldUsername'=>$username));
}
2018-08-03 18:59:23 +02:00
if ($users->delete($username)) {
2018-07-17 23:58:01 +02:00
// Add to syslog
$syslog->add(array(
'dictionaryKey'=>'user-deleted',
2018-05-14 00:00:10 +02:00
'notes'=>$username
));
2018-07-17 23:58:01 +02:00
return true;
}
2017-07-05 19:59:51 +02:00
return false;
}
2017-07-05 23:30:30 +02:00
function createUser($args) {
2018-08-03 18:59:23 +02:00
global $users;
global $L;
global $syslog;
2017-07-05 19:59:51 +02:00
$args['new_username'] = Text::removeSpecialCharacters($args['new_username']);
2017-07-05 19:59:51 +02:00
// Check empty username
if (Text::isEmpty($args['new_username'])) {
Alert::set($L->g('username-field-is-empty'), ALERT_STATUS_FAIL);
2017-07-05 19:59:51 +02:00
return false;
}
// Check already exist username
2018-08-03 18:59:23 +02:00
if ($users->exists($args['new_username'])) {
Alert::set($L->g('username-already-exists'), ALERT_STATUS_FAIL);
2017-07-05 19:59:51 +02:00
return false;
}
// Password length
if (Text::length($args['new_password']) < PASSWORD_LENGTH) {
Alert::set($L->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($L->g('The password and confirmation password do not match'), ALERT_STATUS_FAIL);
2017-07-05 19:59:51 +02:00
return false;
}
// Filter form fields
$tmp = array();
$tmp['username'] = $args['new_username'];
2017-07-05 19:59:51 +02:00
$tmp['password'] = $args['new_password'];
$tmp['role'] = $args['role'];
$tmp['email'] = $args['email'];
// Add the user to the database
2018-08-03 18:59:23 +02:00
if ($users->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 $L;
2018-08-03 18:59:23 +02:00
global $pages;
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 (empty($args['homepage'])) {
$args['homepage'] = '';
$args['uriBlog'] = '';
}
if (empty($args['pageNotFound'])) {
$args['pageNotFound'] = '';
}
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 (!empty($args['uriBlog'])) {
$args['uriBlog'] = Text::addSlashes($args['uriBlog']);
2018-03-06 19:42:18 +01:00
} else {
if (!empty($args['homepage']) && empty($args['uriBlog'])) {
$args['uriBlog'] = '/blog/';
} else {
$args['uriBlog'] = '';
}
}
2019-05-12 12:32:12 +02:00
if (isset($args['extremeFriendly'])) {
$args['extremeFriendly'] = (($args['extremeFriendly']=='true')?true:false);
}
2018-07-28 21:00:12 +02:00
2019-09-02 18:24:34 +02:00
if (isset($args['customFields'])) {
// Custom fields need to be JSON format valid, also the empty JSON need to be "{}"
json_decode($args['customFields']);
if (json_last_error() != JSON_ERROR_NONE) {
return false;
}
$pages->setCustomFields($args['customFields']);
}
if ($site->set($args)) {
// Check current order-by if changed it reorder the content
if ($site->orderBy()!=ORDER_BY) {
if ($site->orderBy()=='date') {
2018-08-03 18:59:23 +02:00
$pages->sortByDate();
} else {
2018-08-03 18:59:23 +02:00
$pages->sortByPosition();
}
2018-08-03 18:59:23 +02:00
$pages->save();
}
// Add syslog
$syslog->add(array(
2019-06-16 22:31:48 +02:00
'dictionaryKey'=>'settings-changes',
2017-07-26 21:00:20 +02:00
'notes'=>''
));
// Create alert
Alert::set($L->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) {
2018-08-03 18:59:23 +02:00
global $users;
global $L;
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($L->g('Password must be at least 6 characters long'), ALERT_STATUS_FAIL);
2018-05-15 20:12:15 +02:00
return false;
}
if ($newPassword!=$confirmPassword) {
Alert::set($L->g('The password and confirmation password do not match'), ALERT_STATUS_FAIL);
2018-05-15 20:12:15 +02:00
return false;
}
2018-08-03 18:59:23 +02:00
if ($users->setPassword(array('username'=>$username, 'password'=>$newPassword))) {
2018-07-17 23:58:01 +02:00
// Add to syslog
$syslog->add(array(
2018-05-15 20:12:15 +02:00
'dictionaryKey'=>'user-password-changed',
'notes'=>$username
));
2018-07-17 23:58:01 +02:00
Alert::set($L->g('The changes have been saved'), ALERT_STATUS_OK);
2018-05-15 20:12:15 +02:00
return true;
}
return false;
}
2020-06-04 08:59:07 +02:00
// Returns true if the user is allowed to proceed
2018-05-20 21:48:43 +02:00
function checkRole($allowRoles, $redirect=true) {
global $login;
global $L;
2018-05-20 21:48:43 +02:00
global $syslog;
$userRole = $login->role();
2018-05-20 21:48:43 +02:00
if (in_array($userRole, $allowRoles)) {
return true;
}
if ($redirect) {
2018-07-17 23:58:01 +02:00
// Add to syslog
$syslog->add(array(
'dictionaryKey'=>'access-denied',
'notes'=>$login->username()
2018-05-20 21:48:43 +02:00
));
2018-07-17 23:58:01 +02:00
Alert::set($L->g('You do not have sufficient permissions'));
2018-05-20 21:48:43 +02:00
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
function createCategory($args) {
2018-08-02 22:33:53 +02:00
global $categories;
global $L;
global $syslog;
2017-11-01 19:38:56 +01:00
if (Text::isEmpty($args['name'])) {
Alert::set($L->g('Category name is empty'), ALERT_STATUS_FAIL);
2017-11-01 19:38:56 +01:00
return false;
}
if ($categories->add(array('name'=>$args['name'], 'description'=>$args['description']))) {
2018-07-17 23:58:01 +02:00
// Add to syslog
$syslog->add(array(
2017-11-01 19:38:56 +01:00
'dictionaryKey'=>'new-category-created',
'notes'=>$args['name']
2017-11-01 19:38:56 +01:00
));
Alert::set($L->g('Category added'), ALERT_STATUS_OK);
2017-11-01 19:38:56 +01:00
return true;
}
Alert::set($L->g('The category already exists'), ALERT_STATUS_FAIL);
2017-11-01 19:38:56 +01:00
return false;
}
function editCategory($args) {
global $L;
2018-08-03 18:59:23 +02:00
global $pages;
2018-08-02 22:33:53 +02:00
global $categories;
global $syslog;
2017-07-29 21:03:18 +02:00
if (Text::isEmpty($args['name']) || Text::isEmpty($args['newKey']) ) {
Alert::set($L->g('Empty fields'));
2017-07-29 21:03:18 +02:00
return false;
}
2018-08-02 22:33:53 +02:00
$newCategoryKey = $categories->edit($args);
if ($newCategoryKey==false) {
Alert::set($L->g('The category already exists'));
2017-07-29 21:03:18 +02:00
return false;
}
// Change the category key in the pages database
2018-08-03 18:59:23 +02:00
$pages->changeCategory($args['oldKey'], $newCategoryKey);
2017-07-29 21:03:18 +02:00
2018-07-17 23:58:01 +02:00
// Add to syslog
$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($L->g('The changes have been saved'));
2017-07-29 21:03:18 +02:00
return true;
}
function deleteCategory($args) {
global $L;
2018-08-02 22:33:53 +02:00
global $categories;
global $syslog;
2017-07-29 21:03:18 +02:00
// Remove the category by key
2018-08-02 22:33:53 +02:00
$categories->remove($args['oldKey']);
// 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
2018-07-17 23:58:01 +02:00
// Add to syslog
$syslog->add(array(
2017-07-29 21:03:18 +02:00
'dictionaryKey'=>'category-deleted',
2018-07-25 23:42:00 +02:00
'notes'=>$args['oldKey']
2017-07-29 21:03:18 +02:00
));
Alert::set($L->g('The changes have been saved'));
2017-07-29 21:03:18 +02:00
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() {
2018-08-02 22:33:53 +02:00
global $categories;
2017-12-18 23:49:53 +01:00
$list = array();
2018-08-02 22:33:53 +02:00
foreach ($categories->keys() as $key) {
2017-12-18 23:49:53 +01:00
$category = new Category($key);
array_push($list, $category);
}
return $list;
}
// Returns the object category if the category exists, FALSE otherwise
function getCategory($key) {
2018-08-02 17:06:53 +02:00
try {
$category = new Category($key);
return $category;
} catch (Exception $e) {
2017-12-18 23:49:53 +01:00
return false;
}
}
// Returns an array with all the tags
// By default, the database of tags is alphanumeric sorted
function getTags() {
2018-08-02 22:33:53 +02:00
global $tags;
2017-12-18 23:49:53 +01:00
$list = array();
2018-08-02 22:33:53 +02:00
foreach ($tags->db as $key=>$fields) {
2017-12-18 23:49:53 +01:00
$tag = new Tag($key);
array_push($list, $tag);
}
return $list;
}
2018-08-02 17:06:53 +02:00
// Returns the object tag if the tag exists, FALSE otherwise
function getTag($key) {
try {
$tag = new Tag($key);
return $tag;
} catch (Exception $e) {
return false;
}
}
2018-11-09 00:59:06 +01:00
// Activate a theme
function activateTheme($themeDirectory) {
global $site;
global $syslog;
2019-01-04 14:21:15 +01:00
global $L, $language;
if (Sanitize::pathFile(PATH_THEMES.$themeDirectory)) {
if (Filesystem::fileExists(PATH_THEMES.$themeDirectory.DS.'install.php')) {
include_once(PATH_THEMES.$themeDirectory.DS.'install.php');
2019-06-16 22:31:48 +02:00
}
2018-11-09 00:59:06 +01:00
$site->set(array('theme'=>$themeDirectory));
$syslog->add(array(
'dictionaryKey'=>'new-theme-configured',
2018-11-09 00:59:06 +01:00
'notes'=>$themeDirectory
));
Alert::set( $L->g('The changes have been saved') );
return true;
}
return false;
2017-12-27 14:19:28 +01:00
}
function ajaxResponse($status=0, $message="", $data=array()) {
$default = array('status'=>$status, 'message'=>$message);
$output = array_merge($default, $data);
exit (json_encode($output));
}
2019-05-09 17:20:35 +02:00
/*
| This function checks the image extension,
| generate a new filename to not overwrite the exists,
| generate the thumbnail,
| and move the image to a proper place
|
| @file string Path and filename of the image
| @imageDir string Path where the image is going to be stored
| @thumbnailDir string Path where the thumbnail is going to be stored, if you don't set the variable is not going to create the thumbnail
|
| @return string/boolean Path and filename of the new image or FALSE if there were some error
*/
function transformImage($file, $imageDir, $thumbnailDir=false) {
global $site;
// Check image extension
$fileExtension = Filesystem::extension($file);
$fileExtension = Text::lowercase($fileExtension);
2019-05-29 19:28:11 +02:00
if (!in_array($fileExtension, $GLOBALS['ALLOWED_IMG_EXTENSION']) ) {
return false;
}
// Generate a filename to not overwrite current image if exists
$filename = Filesystem::filename($file);
$nextFilename = Filesystem::nextFilename($imageDir, $filename);
// Move the image to a proper place and rename
$image = $imageDir.$nextFilename;
Filesystem::mv($file, $image);
chmod($image, 0644);
// Generate Thumbnail
if (!empty($thumbnailDir)) {
if ($fileExtension == 'svg') {
symlink($image, $thumbnailDir.$nextFilename);
} else {
$Image = new Image();
$Image->setImage($image, $site->thumbnailWidth(), $site->thumbnailHeight(), 'crop');
$Image->saveImage($thumbnailDir.$nextFilename, $site->thumbnailQuality(), true);
}
}
return $image;
2019-11-15 14:59:26 +01:00
}
function downloadRestrictedFile($file) {
if (is_file($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit(0);
}
}