From 1bd75ac2ee6ea63bdc9f975f6271b0e3c5e8e857 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Fri, 22 Sep 2017 23:11:08 +0200 Subject: [PATCH] API improves, fixed url router --- bl-kernel/admin/controllers/developers.php | 8 +- bl-kernel/admin/views/developers.php | 5 +- bl-kernel/boot/rules/99.paginator.php | 2 +- bl-kernel/dbpages.class.php | 114 ++++++++++++--------- bl-kernel/functions.php | 13 +-- bl-kernel/url.class.php | 2 +- bl-plugins/api/plugin.php | 18 ++-- 7 files changed, 97 insertions(+), 65 deletions(-) diff --git a/bl-kernel/admin/controllers/developers.php b/bl-kernel/admin/controllers/developers.php index bdf3ebf5..e91d5851 100644 --- a/bl-kernel/admin/controllers/developers.php +++ b/bl-kernel/admin/controllers/developers.php @@ -27,12 +27,16 @@ function printTable($title, $array) { '; - foreach($array as $key=>$value) { + foreach ($array as $key=>$value) { if($value===false) { $value = 'false'; } elseif($value===true) { $value = 'true'; } echo ''; echo ''.$key.''; - echo ''.Sanitize::html($value).''; + if (is_array($value)) { + echo ''.json_encode($value).''; + } else { + echo ''.Sanitize::html($value).''; + } echo ''; } diff --git a/bl-kernel/admin/views/developers.php b/bl-kernel/admin/views/developers.php index be331960..9361f82b 100644 --- a/bl-kernel/admin/views/developers.php +++ b/bl-kernel/admin/views/developers.php @@ -4,6 +4,9 @@ HTML::title(array('title'=>$L->g('Developers'), 'icon'=>'support')); echo '

PHP version: '.phpversion().'

'; +// Loaded extensions +printTable('Server information', $_SERVER); + // Constanst defined by Bludit $constants = get_defined_constants(true); printTable('Constants', $constants['user']); @@ -16,4 +19,4 @@ printTable('$Site object database',$Site->db); // Locales installed exec('locale -a', $locales); -printTable('Locales installed', $locales); \ No newline at end of file +printTable('Locales installed', $locales); diff --git a/bl-kernel/boot/rules/99.paginator.php b/bl-kernel/boot/rules/99.paginator.php index 36569c68..0b85a916 100644 --- a/bl-kernel/boot/rules/99.paginator.php +++ b/bl-kernel/boot/rules/99.paginator.php @@ -6,7 +6,7 @@ Paginator::set('currentPage', $currentPage); if($Url->whereAmI()=='admin') { $itemsPerPage = ITEMS_PER_PAGE_ADMIN; - $amountOfItems = $dbPages->count(false); + $amountOfItems = $dbPages->count(true); } elseif($Url->whereAmI()=='tag') { $itemsPerPage = $Site->itemsPerPage(); diff --git a/bl-kernel/dbpages.class.php b/bl-kernel/dbpages.class.php index 1223a5b2..8df11345 100644 --- a/bl-kernel/dbpages.class.php +++ b/bl-kernel/dbpages.class.php @@ -18,7 +18,9 @@ class dbPages extends dbJSON 'category'=> array('inFile'=>false, 'value'=>''), 'md5file'=> array('inFile'=>false, 'value'=>''), 'uuid'=> array('inFile'=>false, 'value'=>''), - 'allowComments'=> array('inFile'=>false, 'value'=>true) + 'allowComments'=> array('inFile'=>false, 'value'=>true), + 'parent'=> array('inFile'=>false, 'value'=>''), + 'slug'=> array('inFile'=>false, 'value'=>'') ); function __construct() @@ -32,8 +34,28 @@ class dbPages extends dbJSON $dataForDb = array(); // This data will be saved in the database $dataForFile = array(); // This data will be saved in the file + foreach ($this->dbFields as $field=>$options) { + if (isset($args[$field])) { + if ($field=='tags') { + $value = $this->generateTags($args['tags']); + } else { + if( !$options['inFile'] ) { + // Sanitize if will be stored on database + $value = Sanitize::html($args[$field]); + } else { + $value = $args[$field]; + } + } + } else { + // Default value for the field + $value = $options['value']; + } + + $args[$field] = $value; + } + // Generate slug from content if the title is empty - if (empty($args['title'])) { + if (empty($args['title']) || empty($args['slug'])) { $tmpslug = Text::removeHTMLTags($args['content']); $args['slug'] = Text::truncate($tmpslug, 60, ''); } @@ -48,37 +70,20 @@ class dbPages extends dbJSON $currentDate = Date::current(DB_DATE_FORMAT); // Validate date - if( !Valid::date($args['date'], DB_DATE_FORMAT) ) { + if ( !Valid::date($args['date'], DB_DATE_FORMAT) ) { $args['date'] = $currentDate; } // Schedule page - if( ($args['date']>$currentDate) && ($args['status']=='published') ) { + if ( ($args['date']>$currentDate) && ($args['status']=='published') ) { $args['status'] = 'scheduled'; } - foreach($this->dbFields as $field=>$options) { - if( isset($args[$field]) ) { - if($field=='tags') { - $value = $this->generateTags($args['tags']); - } - else { - if( !$options['inFile'] ) { - // Sanitize if will be stored on database - $value = Sanitize::html($args[$field]); - } - else { - $value = $args[$field]; - } - } - } - else { - // Default value for the field - $value = $options['value']; - } + foreach ($this->dbFields as $field=>$options) { + $value = $args[$field]; - // Where the data is stored if ($options['inFile']) { + // Save on file $dataForFile[$field] = $this->stylingFieldsForFile($field, $value); } else { // Set type @@ -124,6 +129,26 @@ class dbPages extends dbJSON $dataForDb = array(); $dataForFile = array(); + foreach ($this->dbFields as $field=>$options) { + if (isset($args[$field])) { + if ($field=='tags') { + $value = $this->generateTags($args['tags']); + } else { + if( !$options['inFile'] ) { + // Sanitize if will be stored on database + $value = Sanitize::html($args[$field]); + } else { + $value = $args[$field]; + } + } + } else { + // Default value for the field + $value = $options['value']; + } + + $args[$field] = $value; + } + $newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']); // If the page is draft then the created time is the current @@ -136,30 +161,27 @@ class dbPages extends dbJSON // Current UUID $args['uuid'] = $this->db[$args['key']]['uuid']; + // Date + $currentDate = Date::current(DB_DATE_FORMAT); + // Modified date $args['dateModified'] = Date::current(DB_DATE_FORMAT); - foreach($this->dbFields as $field=>$options) { - if( isset($args[$field]) ) { - if($field=='tags') { - $value = $this->generateTags($args['tags']); - } - else { - if( !$options['inFile'] ) { - // Sanitize if will be stored on database - $value = Sanitize::html($args[$field]); - } - else { - // Default value for the field - $value = $args[$field]; - } - } - } - else { - $value = $options['value']; - } + // Validate date + if ( !Valid::date($args['date'], DB_DATE_FORMAT) ) { + $args['date'] = $currentDate; + } + + // Schedule page + if ( ($args['date']>$currentDate) && ($args['status']=='published') ) { + $args['status'] = 'scheduled'; + } + + foreach ($this->dbFields as $field=>$options) { + $value = $args[$field]; if ($options['inFile']) { + // Save on file $dataForFile[$field] = $this->stylingFieldsForFile($field, $value); } else { // Set type @@ -254,8 +276,8 @@ class dbPages extends dbJSON public function getPublishedDB() { $tmp = $this->db; - foreach($tmp as $key=>$fields) { - if($fields['status']!='published') { + foreach ($tmp as $key=>$fields) { + if ($fields['status']!='published') { unset($tmp[$key]); } } @@ -345,7 +367,7 @@ class dbPages extends dbJSON // (boolean) $total, FALSE returns the total of published pages (without draft and scheduled) public function count($onlyPublished=true) { - if( $onlyPublished ) { + if ($onlyPublished) { $db = $this->getPublishedDB(); return count($db); } diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index dbc2771b..b1b58ba0 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -304,12 +304,13 @@ function createPage($args) { // The user is always the one loggued $args['username'] = Session::get('username'); - if ( Text::isEmpty($args['username']) ) { + if ( empty($args['username']) ) { + Log::set('Function createPage()'.LOG_SEP.'Empty username.'); return false; } // External Cover Image - if ( Text::isNotEmpty(($args['externalCoverImage'])) ) { + if ( !empty($args['externalCoverImage']) ) { $args['coverImage'] = $args['externalCoverImage']; unset($args['externalCoverImage']); } @@ -350,21 +351,17 @@ function editPage($args) { // The user is always the one loggued $args['username'] = Session::get('username'); - if ( Text::isEmpty($args['username']) ) { + if ( empty($args['username']) ) { Log::set('Function editPage()'.LOG_SEP.'Empty username.'); return false; } // External Cover Image - if ( Text::isNotEmpty(($args['externalCoverImage'])) ) { + if ( !empty($args['externalCoverImage']) ) { $args['coverImage'] = $args['externalCoverImage']; unset($args['externalCoverImage']); } - if (!isset($args['parent'])) { - $args['parent'] = ''; - } - $key = $dbPages->edit($args); if ($key) { // Call the plugins after page modified diff --git a/bl-kernel/url.class.php b/bl-kernel/url.class.php index 6b4629e3..f5fb9408 100644 --- a/bl-kernel/url.class.php +++ b/bl-kernel/url.class.php @@ -67,7 +67,7 @@ class Url } // Check coincidence with complete filterURI - if ($subString==$filterURI) { + if ($subString==$filterFull) { $this->slug = mb_substr($this->uri, $filterFullLenght); $this->setWhereAmI($filterName); $this->activeFilter = $filterURI; diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 4d7aafcb..2134e9b9 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -78,7 +78,7 @@ class pluginAPI extends Plugin { $tokenAPI = $this->getValue('token'); // Check empty token - if ( empty($inputs['token']) ) { + if (empty($inputs['token'])) { $this->response(404, 'Not Found', array('message'=>'Missing API token.')); } @@ -168,6 +168,11 @@ class pluginAPI extends Plugin { break; } + // Try to get raw data + if (empty($inputs)) { + $inputs = file_get_contents('php://input'); + } + return $this->cleanInputs($inputs); } @@ -193,16 +198,17 @@ class pluginAPI extends Plugin { private function cleanInputs($inputs) { $tmp = array(); - if ( is_array($inputs) ) { - foreach($inputs as $key=>$value) { + if (is_array($inputs)) { + foreach ($inputs as $key=>$value) { $tmp[$key] = Sanitize::html($value); } - } elseif ( is_string($inputs) ) { + } elseif(is_string($inputs)) { $tmp = json_decode($inputs, true); - if(json_last_error()===0) { + if (json_last_error()!==JSON_ERROR_NONE) { $tmp = array(); } } + return $tmp; } @@ -264,7 +270,7 @@ class pluginAPI extends Plugin { { // This function is defined on functions.php $key = createPage($args); - +var_dump($key);exit; if ($key===false) { return array( 'status'=>'1',