From 78cacae12843270000ddde5a78b342095e25e902 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Fri, 8 Feb 2019 08:53:26 +0100 Subject: [PATCH] Bug fixes for API plugin --- bl-kernel/pages.class.php | 46 +++++++++++++++------------------------ bl-kernel/pagex.class.php | 7 +----- bl-plugins/api/plugin.php | 6 ++--- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/bl-kernel/pages.class.php b/bl-kernel/pages.class.php index cf02d797..a04406d4 100644 --- a/bl-kernel/pages.class.php +++ b/bl-kernel/pages.class.php @@ -77,14 +77,11 @@ class Pages extends dbJSON { // Content // This variable is not belong to the database so is not defined in $row - $contentRaw = $args['content']; + $contentRaw = (empty($args['content'])?'':$args['content']); // Parent // This variable is not belong to the database so is not defined in $row - $parent = ''; - if (!empty($args['parent'])) { - $parent = $args['parent']; - } + $parent = (empty($args['parent'])?'':$args['parent']); // Slug from the title or the content // This variable is not belong to the database so is not defined in $row @@ -118,13 +115,13 @@ class Pages extends dbJSON { } // Create the directory - if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) { + if (Filesystem::mkdir(PATH_PAGES.$key, true) === false) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory ['.PATH_PAGES.$key.']',LOG_TYPE_ERROR); return false; } // Create the index.txt and save the file - if( file_put_contents(PATH_PAGES.$key.DS.FILENAME, $contentRaw) === false ) { + if (file_put_contents(PATH_PAGES.$key.DS.FILENAME, $contentRaw) === false) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the content in the file ['.FILENAME.']',LOG_TYPE_ERROR); return false; } @@ -149,7 +146,6 @@ class Pages extends dbJSON { // Old key // This variable is not belong to the database so is not defined in $row $key = $args['key']; - $row = array(); // Check values on args or set default values foreach ($this->dbFields as $field=>$value) { @@ -172,23 +168,19 @@ class Pages extends dbJSON { // Content // This variable is not belong to the database so is not defined in $row - $contentRaw = $args['content']; + $contentRaw = (empty($args['content'])?'':$args['content']); // Parent // This variable is not belong to the database so is not defined in $row - $parent = ''; - if (!empty($args['parent'])) { - $parent = $args['parent']; - } + $parent = (empty($args['parent'])?'':$args['parent']); - // Slug from the title or the content + // Slug + // If the user change the slug the page key changes + // If the user send an empty slug the page key doesn't need to change // This variable is not belong to the database so is not defined in $row if (empty($args['slug'])) { - if (!empty($row['title'])) { - $slug = $this->generateSlug($row['title']); - } else { - $slug = $this->generateSlug($contentRaw); - } + $explode = explode('/', $key); + $slug = end($explode); } else { $slug = $args['slug']; } @@ -214,7 +206,7 @@ class Pages extends dbJSON { // Move the directory from old key to new key. if ($newKey!==$key) { - if( Filesystem::mv(PATH_PAGES.$key, PATH_PAGES.$newKey) === false ) { + if (Filesystem::mv(PATH_PAGES.$key, PATH_PAGES.$newKey) === false) { Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey); return false; } @@ -227,7 +219,7 @@ class Pages extends dbJSON { } // Remove the old key - unset ($this->db[$key]); + unset($this->db[$key]); // Reindex Orphan Children $this->reindexChildren($key, $newKey); @@ -669,14 +661,12 @@ class Pages extends dbJSON { if ($newKey!==$oldKey) { // Verify if the key is already been used - if( isset($this->db[$newKey]) ) { - if( !Text::endsWithNumeric($newKey) ) { - $newKey = $newKey.'-0'; - } - - while( isset($this->db[$newKey]) ) { - $newKey++; + if (isset($this->db[$newKey])) { + $i = 0; + while (isset($this->db[$newKey.'-'.$i])) { + $i++; } + $newKey = $newKey.'-'.$i; } } diff --git a/bl-kernel/pagex.class.php b/bl-kernel/pagex.class.php index b126218b..17575427 100644 --- a/bl-kernel/pagex.class.php +++ b/bl-kernel/pagex.class.php @@ -416,12 +416,7 @@ class Page { public function slug() { $explode = explode('/', $this->key()); - - // Remove the parent - if (!empty($explode[1])) { - return $explode[1]; - } - return $explode[0]; + return end($explode); } // Returns the parent key, if the page doesn't have a parent returns FALSE diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 8cdf4401..914cca64 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -69,7 +69,7 @@ class pluginAPI extends Plugin { $inputs = $this->getMethodInputs(); if ( empty($inputs) ) { - $this->response(404, 'Not Found', array('message'=>'Missing method inputs.')); + $this->response(400, 'Bad Request', array('message'=>'Missing method inputs.')); } // ENDPOINT PARAMETERS @@ -77,7 +77,7 @@ class pluginAPI extends Plugin { $parameters = $this->getEndpointParameters($URI); if ( empty($parameters) ) { - $this->response(404, 'Not Found', array('message'=>'Missing endpoint parameters.')); + $this->response(400, 'Bad Request', array('message'=>'Missing endpoint parameters.')); } // API TOKEN @@ -87,7 +87,7 @@ class pluginAPI extends Plugin { // Check empty token if (empty($inputs['token'])) { - $this->response(404, 'Not Found', array('message'=>'Missing API token.')); + $this->response(400, 'Bad Request', array('message'=>'Missing API token.')); } // Check if the token is valid