From 1f5dd3ac99284c44f7bb083625d81f6b75f0dc17 Mon Sep 17 00:00:00 2001 From: Diego Najar Date: Thu, 22 Jun 2017 00:21:08 +0200 Subject: [PATCH] Bug fixes, developer area, google plugin updated, rss and sitemap updated --- .../admin/themes/default/css/default.css | 6 + bl-kernel/admin/views/edit-page.php | 9 +- bl-kernel/admin/views/new-page.php | 7 +- bl-kernel/admin/views/pages.php | 9 +- bl-kernel/dbpages.class.php | 12 ++ bl-kernel/dbusers.class.php | 13 ++ bl-kernel/page.class.php | 16 ++ bl-plugins/api/plugin.php | 157 +++++++++++------- bl-plugins/fixedPages/languages/en_US.json | 10 +- bl-plugins/fixedPages/plugin.php | 152 ++++------------- bl-plugins/opengraph/plugin.php | 4 +- 11 files changed, 194 insertions(+), 201 deletions(-) diff --git a/bl-kernel/admin/themes/default/css/default.css b/bl-kernel/admin/themes/default/css/default.css index af989a50..3369e64c 100644 --- a/bl-kernel/admin/themes/default/css/default.css +++ b/bl-kernel/admin/themes/default/css/default.css @@ -11,6 +11,8 @@ } .label-draft, +.label-fixed, +.label-sticky, .label-empty-title, .label-time { background: #A979D1 none repeat scroll 0 0; @@ -24,6 +26,10 @@ font-size: 0.8em; } +.label-fixed { + background: #7BD179; +} + .label-empty-title { background: #53D192; } diff --git a/bl-kernel/admin/views/edit-page.php b/bl-kernel/admin/views/edit-page.php index 731a4861..80583795 100644 --- a/bl-kernel/admin/views/edit-page.php +++ b/bl-kernel/admin/views/edit-page.php @@ -133,8 +133,13 @@ echo '
'; 'name'=>'status', 'label'=>$L->g('Status'), 'class'=>'uk-width-1-1 uk-form-medium', - 'options'=>array('published'=>$L->g('Published'), 'draft'=>$L->g('Draft')), - 'selected'=>($page->draft()?'draft':'published'), + 'options'=>array( + 'published'=>$L->g('Published'), + 'draft'=>$L->g('Draft'), + 'fixed'=>$L->g('Fixed'), + 'sticky'=>$L->g('Sticky') + ), + 'selected'=>$page->status(), 'tip'=>'' )); diff --git a/bl-kernel/admin/views/new-page.php b/bl-kernel/admin/views/new-page.php index b18006dd..0a79ed1d 100644 --- a/bl-kernel/admin/views/new-page.php +++ b/bl-kernel/admin/views/new-page.php @@ -118,7 +118,12 @@ echo '
'; 'name'=>'status', 'label'=>$L->g('Status'), 'class'=>'uk-width-1-1 uk-form-medium', - 'options'=>array('published'=>$L->g('Published'), 'draft'=>$L->g('Draft')), + 'options'=>array( + 'published'=>$L->g('Published'), + 'draft'=>$L->g('Draft'), + 'fixed'=>$L->g('Fixed'), + 'sticky'=>$L->g('Sticky') + ), 'selected'=>'published', 'tip'=>'' )); diff --git a/bl-kernel/admin/views/pages.php b/bl-kernel/admin/views/pages.php index c8d41c30..ae71c33b 100644 --- a/bl-kernel/admin/views/pages.php +++ b/bl-kernel/admin/views/pages.php @@ -22,14 +22,11 @@ echo ' foreach($pages as $page) { $status = false; - if($page->scheduled()) { - $status = $Language->g('Scheduled'); - } - elseif(!$page->published()) { - $status = $Language->g('Draft'); + if($page->status()!='published') { + $status = $Language->g( $page->status() ); } echo ''; - echo ''.($status?''.$status.'':'').($page->title()?$page->title():''.$Language->g('Empty title').' ').''; + echo ''.($status?''.$status.'':'').($page->title()?$page->title():''.$Language->g('Empty title').' ').''; echo ''.( (ORDER_BY=='date') ? $page->dateRaw() : $page->position() ).''; diff --git a/bl-kernel/dbpages.class.php b/bl-kernel/dbpages.class.php index 7ebdc266..637c6ae7 100644 --- a/bl-kernel/dbpages.class.php +++ b/bl-kernel/dbpages.class.php @@ -268,6 +268,18 @@ class dbPages extends dbJSON return $tmp; } + // (array) Returns a database with the fixed pages + public function getFixedDB() + { + $tmp = $this->db; + foreach($tmp as $key=>$fields) { + if($fields['status']!='fixed') { + unset($tmp[$key]); + } + } + return $tmp; + } + // Returns a database with drafts pages public function getDraftDB() { diff --git a/bl-kernel/dbusers.class.php b/bl-kernel/dbusers.class.php index d40498cd..5f989619 100644 --- a/bl-kernel/dbusers.class.php +++ b/bl-kernel/dbusers.class.php @@ -13,6 +13,8 @@ class dbUsers extends dbJSON 'registered'=> array('inFile'=>false, 'value'=>'1985-03-15 10:00'), 'tokenEmail'=> array('inFile'=>false, 'value'=>''), 'tokenEmailTTL'=> array('inFile'=>false, 'value'=>'2009-03-15 14:00'), + 'tokenAuth'=> array('inFile'=>false, 'value'=>''), + 'tokenAuthTTL'=> array('inFile'=>false, 'value'=>'2009-03-15 14:00'), 'twitter'=> array('inFile'=>false, 'value'=>''), 'facebook'=> array('inFile'=>false, 'value'=>''), 'googlePlus'=> array('inFile'=>false, 'value'=>''), @@ -72,6 +74,17 @@ class dbUsers extends dbJSON return false; } + // Returns the username with the authentication token assigned, FALSE otherwise + public function getByAuthToken($token) + { + foreach($this->db as $username=>$fields) { + if($fields['tokenAuth']==$token) { + return $username; + } + } + return false; + } + // Return TRUE if the user exists, FALSE otherwise. public function userExists($username) { diff --git a/bl-kernel/page.class.php b/bl-kernel/page.class.php index be3681e2..82c717b4 100644 --- a/bl-kernel/page.class.php +++ b/bl-kernel/page.class.php @@ -353,6 +353,22 @@ class Page { return ($this->getValue('status')=='draft'); } + public function sticky() + { + return ($this->getValue('status')=='sticky'); + } + + public function fixed() + { + return ($this->getValue('status')=='fixed'); + } + + // (string) Returns status of the page + public function status() + { + return $this->getValue('status'); + } + // Returns the title field public function title() { diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php index 43c00517..d0235e41 100644 --- a/bl-plugins/api/plugin.php +++ b/bl-plugins/api/plugin.php @@ -4,14 +4,12 @@ class pluginAPI extends Plugin { public function init() { - global $Security; - - // This key is used for request such as get the list of all posts and pages - $token = md5($Security->key1().time().DOMAIN); + // Generate the API Token + $token = md5( uniqid().time().DOMAIN ); $this->dbFields = array( 'ping'=>0, // 0 = false, 1 = true - 'token'=>$token, // Private key + 'token'=>$token, // API Token 'showAllAmount'=>15, // Amount of posts and pages for return 'authentication'=>1 // Authentication required ); @@ -56,9 +54,11 @@ class pluginAPI extends Plugin { public function beforeRulesLoad() { global $Url; - global $dbPosts; global $dbPages; + global $dbUsers; + // CHECK URL + // ------------------------------------------------------------ // Check if the URI start with /api/ $startString = HTML_PATH_ROOT.'api/'; $URI = $Url->uri(); @@ -70,72 +70,46 @@ class pluginAPI extends Plugin { // Remove the first part of the URI $URI = mb_substr($URI, $length); - // METHODS - // ------------------------------------------------------------ - // GET - // POST - // PUT - // DELETE - - $method = $_SERVER['REQUEST_METHOD']; - // INPUTS // ------------------------------------------------------------ - // token | authentication token - - $inputs = json_decode(file_get_contents('php://input'),true); - - if( empty($inputs) ) { - // Default variables for $input - $inputs = array( - 'token'=>'' - ); - } - else { - // Sanitize inputs - foreach( $inputs as $key=>$value ) { - if(empty($value)) { - $this->response(array( - 'status'=>'1', - 'message'=>'Invalid input.' - )); - } else { - $inputs[$key] = Sanitize::html($value); - } - } - } + $inputs = $this->getInputs(); // PARAMETERS // ------------------------------------------------------------ - // /api/posts | GET | returns all posts - // /api/posts/{key} | GET | returns the post with the {key} - // /api/pages | GET | returns all pages - // /api/pages/{key} | GET | returns the page with the {key} - // /api/cli/regenerate | POST | check for new posts and pages + $parameters = $this->getParameters($URI); - $parameters = explode('/', $URI); + // API TOKEN + // ------------------------------------------------------------ + $tokenAPI = $this->getValue('token'); - // Sanitize parameters - foreach( $parameters as $key=>$value ) { - if(empty($value)) { - $this->response(array( - 'status'=>'1', - 'message'=>'Invalid parameter.' - )); - } else { - $parameters[$key] = Sanitize::html($value); + // Check empty token + if( empty($inputs['token']) ) { + $this->response(array( + 'status'=>'1', + 'message'=>'Missing API token.' + )); + } + + // Check the token is valid + if( $inputs['token']!=$tokenAPI ) { + $this->response(array( + 'status'=>'1', + 'message'=>'Invalid API token.' + )); + } + + // AUTHENTICATION TOKEN + // ------------------------------------------------------------ + $writePermissions = false; + if( !empty($inputs['authentication']) ) { + // Get the user with the authentication token + $username = $dbUsers->getByAuthToken($inputs['authentication']); + if( $username!==false ) { + // Enable write permissions + $writePermissions = true; } } - // Check authentication - if( $this->getDbField('authentication')==1 ) { - if( $inputs['token']!=$this->getDbField('token') ) { - $this->response(array( - 'status'=>'1', - 'message'=>'Invalid token.' - )); - } - } // /api/posts if( ($method==='GET') && ($parameters[0]==='posts') && empty($parameters[1]) ) { @@ -175,13 +149,68 @@ class pluginAPI extends Plugin { } } -// FUNCTIONS +// PRIVATE METHODS // ---------------------------------------------------------------------------- + private function getParameters($URI) + { + // PARAMETERS + // ------------------------------------------------------------ + // /api/pages | GET | returns all pages + // /api/pages/{key} | GET | returns the page with the {key} + // /api/cli/regenerate | POST | check for new posts and pages + + $parameters = explode('/', $URI); + + // Sanitize parameters + foreach($parameters as $key=>$value) { + $parameters[$key] = Sanitize::html($value); + } + + return $parameters; + } + + private function getInputs() + { + // METHODS + // ------------------------------------------------------------ + // GET + // POST + // PUT + // DELETE + + $method = $_SERVER['REQUEST_METHOD']; + + switch($method) { + case "POST": + $inputs = $_POST; + break; + case "GET": + case "DELETE": + $inputs = $_GET; + break; + case "PUT": + $inputs = file_get_contents("php://input"); + break; + default: + $inputs = json_encode(array()); + break; + } + + // Input data need to be JSON + $inputs = json_decode(file_get_contents('php://input'),true); + + // Sanitize inputs + foreach($inputs as $key=>$value) { + $inputs[$key] = Sanitize::html($value); + } + + return $inputs; + } + private function response($data=array()) { $json = json_encode($data); - header('Content-Type: application/json'); exit($json); } diff --git a/bl-plugins/fixedPages/languages/en_US.json b/bl-plugins/fixedPages/languages/en_US.json index 422ab2b7..29bf1c9f 100644 --- a/bl-plugins/fixedPages/languages/en_US.json +++ b/bl-plugins/fixedPages/languages/en_US.json @@ -1,7 +1,11 @@ { "plugin-data": { - "name": "Fixed Pages", - "description": "Show a list of links." - } + "name": "Fixed pages", + "description": "Shows a list of pages, you can define the amount of items and the order depends of settings." + }, + + "home-page": "Home page", + "show-home-link": "Show home link", + "amount-of-items": "Amount of items" } diff --git a/bl-plugins/fixedPages/plugin.php b/bl-plugins/fixedPages/plugin.php index d3f56589..bd14790b 100644 --- a/bl-plugins/fixedPages/plugin.php +++ b/bl-plugins/fixedPages/plugin.php @@ -4,140 +4,33 @@ class pluginFixedPages extends Plugin { public function init() { - // JSON database - $jsondb = json_encode(array( - 'about'=>'About' - )); - // Fields and default values for the database of this plugin $this->dbFields = array( 'label'=>'Fixed Pages', - 'jsondb'=>$jsondb + 'homeLink'=>true ); - - // Disable default Save and Cancel button - $this->formButtons = false; } - // Method called when a POST request is sent - public function post() - { - global $dbPages; - - // Get current jsondb value from database - // All data stored in the database is html encoded - $jsondb = $this->db['jsondb']; - $jsondb = Sanitize::htmlDecode($jsondb); - - // Convert JSON to Array - $pagesFixed = json_decode($jsondb, true); - - // Check if the user click on the button delete or add - if( isset($_POST['delete']) ) { - // Values from $_POST - $pageKey = $_POST['delete']; - - // Change the status of the page from fixed to published - $dbPages->setStatus($pageKey, 'published'); - - // Delete the link from the array - unset($pagesFixed[$pageKey]); - } - elseif( isset($_POST['add']) ) { - // Values from $_POST - $pageTitle = $_POST['newPageTitle']; - $pageKey = $_POST['newPageKey']; - - // Change the status of the page from fixed to published - $dbPages->setStatus($pageKey, 'fixed'); - - // Add the link - $pagesFixed[$pageKey] = $pageTitle; - } - - // Encode html to store the values on the database - $this->db['label'] = Sanitize::html($_POST['label']); - $this->db['jsondb'] = Sanitize::html(json_encode($pagesFixed)); - - // Save the database - return $this->save(); - } - - // Method called on plugin settings on the admin area + // Method called on the settings of the plugin on the admin area public function form() { global $Language; - global $dbPages; - - $options = array(); - foreach($dbPages->db as $key=>$fields) { - $page = buildPage($key); - if($page->published()) { - $options[$key] = $page->title(); - } - } $html = '
'; $html .= ''; - $html .= ''; + $html .= ''; $html .= ''.$Language->get('Title of the plugin for the sidebar').''; $html .= '
'; $html .= '
'; - $html .= ''; - $html .= '
'; - - // NEW PAGE - $html .= ''.$Language->get('New fixed page').''; - - $html .= '
'; - $html .= ''; - $html .= ''; - $html .= '
'; - - $html .= '
'; - $html .= ''; - $html .= ''; + $html .= ''; + $html .= ''; $html .= ''; + $html .= ''.$Language->get('Show the home link on the sidebar').''; $html .= '
'; - $html .= '
'; - $html .= ''; - $html .= '
'; - - // LIST OF PAGES - $html .= ''.$Language->get('Fixed pages').''; - - $jsondb = $this->getValue('jsondb', $unsanitized=false); - $pagesFixed = json_decode($jsondb, true); - foreach($pagesFixed as $pageKey=>$pageTitle) { - $html .= '
'; - $html .= ''; - $html .= ''; - $html .= '
'; - - $page = buildPage($pageKey); - if($page) { - $title = $page->title(); - } else { - $title = $Language->get('Error page deleted'); - } - - $html .= '
'; - $html .= ''; - $html .= ''; - $html .= '
'; - - $html .= '
'; - $html .= ''; - $html .= '
'; - - $html .= '
'; - } - return $html; } @@ -145,6 +38,11 @@ class pluginFixedPages extends Plugin { public function siteSidebar() { global $Language; + global $Url; + global $Site; + global $dbPages; + + $pages = $dbPages->getFixedDB(); // HTML for sidebar $html = '
'; @@ -152,15 +50,23 @@ class pluginFixedPages extends Plugin { $html .= '
'; $html .= '
    '; - // Get the JSON DB, getValue() with the option unsanitized HTML code - $jsondb = $this->getValue('jsondb', false); - $pagesFixed = json_decode($jsondb); - - // By default the database of categories are alphanumeric sorted - foreach($pagesFixed as $key=>$title) { + // Show Home page link + if( $this->getValue('homeLink') ) { $html .= '
  • '; - $html .= ''; - $html .= $title; + $html .= ''; + $html .= $Language->get('Home page'); + $html .= ''; + $html .= '
  • '; + } + + // Get keys of pages + $keys = array_keys($pages); + foreach($keys as $pageKey) { + // Create the page object from the page key + $page = buildPage($pageKey); + $html .= '
  • '; + $html .= ''; + $html .= $page->title(); $html .= ''; $html .= '
  • '; } diff --git a/bl-plugins/opengraph/plugin.php b/bl-plugins/opengraph/plugin.php index c32d65bd..ea66372e 100644 --- a/bl-plugins/opengraph/plugin.php +++ b/bl-plugins/opengraph/plugin.php @@ -41,8 +41,7 @@ class pluginOpenGraph extends Plugin { 'siteName' =>$Site->title() ); - switch($WHERE_AM_I) - { + switch($WHERE_AM_I) { // The user filter by page case 'page': $og['type'] = 'article'; @@ -56,6 +55,7 @@ class pluginOpenGraph extends Plugin { // The user is in the homepage default: + $content = ''; // The image it's from the first page if(isset($pages[0]) ) { $og['image'] = $pages[0]->coverImage($absolute=true);