From dbf7a14854d3ccf0bc0f69d61001d616eadba6fb Mon Sep 17 00:00:00 2001 From: dignajar Date: Sun, 29 May 2016 14:21:11 -0300 Subject: [PATCH] New plugin, API --- bl-kernel/boot/init.php | 3 + bl-kernel/boot/rules/70.posts.php | 110 ------------- bl-kernel/boot/rules/71.pages.php | 139 ---------------- bl-kernel/functions.php | 240 ++++++++++++++++++++++++++++ bl-kernel/helpers/text.class.php | 64 +++----- bl-kernel/page.class.php | 10 ++ bl-kernel/post.class.php | 11 ++ bl-plugins/api/languages/ar_MA.json | 7 + bl-plugins/api/languages/en_US.json | 7 + bl-plugins/api/languages/es_AR.json | 7 + bl-plugins/api/languages/ja_JP.json | 7 + bl-plugins/api/languages/ru_RU.json | 7 + bl-plugins/api/languages/uk_UA.json | 7 + bl-plugins/api/metadata.json | 10 ++ bl-plugins/api/plugin.php | 90 +++++++++++ 15 files changed, 427 insertions(+), 292 deletions(-) create mode 100644 bl-kernel/functions.php create mode 100644 bl-plugins/api/languages/ar_MA.json create mode 100644 bl-plugins/api/languages/en_US.json create mode 100644 bl-plugins/api/languages/es_AR.json create mode 100644 bl-plugins/api/languages/ja_JP.json create mode 100644 bl-plugins/api/languages/ru_RU.json create mode 100644 bl-plugins/api/languages/uk_UA.json create mode 100644 bl-plugins/api/metadata.json create mode 100644 bl-plugins/api/plugin.php diff --git a/bl-kernel/boot/init.php b/bl-kernel/boot/init.php index d3979932..4fb9037d 100644 --- a/bl-kernel/boot/init.php +++ b/bl-kernel/boot/init.php @@ -142,6 +142,9 @@ include(PATH_KERNEL.'parsedown.class.php'); include(PATH_KERNEL.'parsedownextra.class.php'); include(PATH_KERNEL.'security.class.php'); +// Include functions +include(PATH_KERNEL.'functions.php'); + // Include Helpers Classes include(PATH_HELPERS.'text.class.php'); include(PATH_HELPERS.'log.class.php'); diff --git a/bl-kernel/boot/rules/70.posts.php b/bl-kernel/boot/rules/70.posts.php index f2740863..620d95fb 100644 --- a/bl-kernel/boot/rules/70.posts.php +++ b/bl-kernel/boot/rules/70.posts.php @@ -8,116 +8,6 @@ // Filter by page number, by tag, etc. $posts = array(); -// ============================================================================ -// Functions -// ============================================================================ - -function reIndexTagsPosts() -{ - global $dbPosts; - global $dbTags; - - // Remove unpublished. - $dbPosts->removeUnpublished(); - - // Regenerate the tags index for posts. - $dbTags->reindexPosts( $dbPosts->db ); - - // Restore the database, before remove the unpublished. - $dbPosts->restoreDB(); - - return true; -} - -function buildPost($key) -{ - global $dbPosts; - global $dbUsers; - global $Parsedown; - global $Site; - - // Post object, content from FILE. - $Post = new Post($key); - if( !$Post->isValid() ) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from file with key: '.$key); - return false; - } - - // Post database, content from DATABASE JSON. - $db = $dbPosts->getPostDB($key); - if( !$db ) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from database with key: '.$key); - return false; - } - - // Foreach field from DATABASE. - foreach($db as $field=>$value) { - $Post->setField($field, $value); - } - - // Content in raw format - $contentRaw = $Post->content(); - $Post->setField('contentRaw', $contentRaw, true); - - // Parse the content - $content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities - $content = $Parsedown->text($content); // Parse Markdown. - $content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute. - $Post->setField('content', $content, true); - - // Pagebrake - $explode = explode(PAGE_BREAK, $content); - $Post->setField('breakContent', $explode[0], true); - $Post->setField('readMore', !empty($explode[1]), true); - - // Date format - $postDate = $Post->date(); - $Post->setField('dateRaw', $postDate, true); - - $postDateFormated = $Post->dateRaw( $Site->dateFormat() ); - $Post->setField('date', $postDateFormated, true); - - // User object - $username = $Post->username(); - $Post->setField('user', $dbUsers->getUser($username)); - - return $Post; -} - -function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeUnpublished=true, $tagKey=false) -{ - global $dbPosts; - global $dbTags; - global $Url; - - $posts = array(); - - if($tagKey) { - // Get the keys list from tags database, this database is optimized for this case. - $list = $dbTags->getList($pageNumber, $amount, $tagKey); - } - else { - // Get the keys list from posts database. - $list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished); - } - - // There are not posts for the page number then set the page notfound - if(empty($list) && $pageNumber>0) { - $Url->setNotFound(true); - } - - // Foreach post key, build the post. - foreach($list as $postKey=>$values) - { - $Post = buildPost($postKey); - if($Post!==false) { - array_push($posts, $Post); - } - } - - return $posts; -} - // ============================================================================ // Main // ============================================================================ diff --git a/bl-kernel/boot/rules/71.pages.php b/bl-kernel/boot/rules/71.pages.php index 1a76a231..63d64cb1 100644 --- a/bl-kernel/boot/rules/71.pages.php +++ b/bl-kernel/boot/rules/71.pages.php @@ -14,145 +14,6 @@ $pagesParents = array(NO_PARENT_CHAR=>array()); $pagesParentsPublished = array(); -// ============================================================================ -// Functions -// ============================================================================ - -function sortPages2($a, $b) -{ - if ($a->position() == $b->position()) { - return 0; - } - - return ($a->position() < $b->position()) ? -1 : 1; -} - -function sortPages($a, $b) -{ - if ($a['position'] == $b['position']) { - return 0; - } - - return ($a['position'] < $b['position']) ? -1 : 1; -} - -function buildPage($key) -{ - global $dbPages; - global $dbUsers; - global $Parsedown; - global $Site; - - // Page object, content from FILE. - $Page = new Page($key); - if( !$Page->isValid() ) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from file with key: '.$key); - return false; - } - - // Page database, content from DATABASE JSON. - $db = $dbPages->getPageDB($key); - if( !$db ) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key); - return false; - } - - // Foreach field from DATABASE. - foreach($db as $field=>$value) { - $Page->setField($field, $value); - } - - // Content in raw format - $contentRaw = $Page->content(); - $Page->setField('contentRaw', $Page->content(), true); - - // Parse markdown content. - $content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities - $content = $Parsedown->text($content); // Parse Markdown. - $content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute. - $Page->setField('content', $content, true); - - // Pagebrake - $explode = explode(PAGE_BREAK, $content); - $Page->setField('breakContent', $explode[0], true); - $Page->setField('readMore', !empty($explode[1]), true); - - // Date format - $pageDate = $Page->date(); - $Page->setField('dateRaw', $pageDate, true); - - $pageDateFormated = $Page->dateRaw( $Site->dateFormat() ); - $Page->setField('date', $pageDateFormated, true); - - // User object - $username = $Page->username(); - $Page->setField('user', $dbUsers->getUser($username)); - - return $Page; -} - -function buildAllPages() -{ - global $pagesParents; - global $pagesParentsPublished; - global $pagesPublished; - global $dbPages; - - // Get the page list - $list = $dbPages->getDB(); - - // Clean pages array. - $pages = array(); - - // Remove the error page - unset($list['error']); - - // Sorte pages - uasort($list, 'sortPages'); - - foreach($list as $key=>$db) - { - $Page = buildPage($key); - - if($Page!==false) - { - // Filter pages, with and without parent - - // If the page doesn't have a father, it's a parent page :P - if( $Page->parentKey()===false ) { - // Add the parent key in the dbPages - $dbPages->addParentKey($Page->key()); - - // Add the page as a parent page in the array - $pagesParents[NO_PARENT_CHAR][$Page->key()] = $Page; - - // If the page is published - if($Page->published()) { - $pagesParentsPublished[NO_PARENT_CHAR][$Page->key()] = $Page; - } - } - else { - $pagesParents[$Page->parentKey()][$Page->key()] = $Page; - - // If the page is published - if($Page->published()) { - $pagesParentsPublished[$Page->parentKey()][$Page->key()] = $Page; - } - } - - // All pages in one array - $pages[$Page->key()] = $Page; - - // If the page is published - if($Page->published()) { - $pagesPublished[$Page->parentKey()][$Page->key()] = $Page; - } - } - } - - return $pages; -} - // ============================================================================ // Main // ============================================================================ diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php new file mode 100644 index 00000000..7600389a --- /dev/null +++ b/bl-kernel/functions.php @@ -0,0 +1,240 @@ +removeUnpublished(); + + // Regenerate the tags index for posts. + $dbTags->reindexPosts( $dbPosts->db ); + + // Restore the database, before remove the unpublished. + $dbPosts->restoreDB(); + + return true; +} + +function buildPost($key) +{ + global $dbPosts; + global $dbUsers; + global $Parsedown; + global $Site; + + // Post object, content from FILE. + $Post = new Post($key); + if( !$Post->isValid() ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from file with key: '.$key); + return false; + } + + // Post database, content from DATABASE JSON. + $db = $dbPosts->getPostDB($key); + if( !$db ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the post from database with key: '.$key); + return false; + } + + // Foreach field from DATABASE. + foreach($db as $field=>$value) { + $Post->setField($field, $value); + } + + // Content in raw format + $contentRaw = $Post->content(); + $Post->setField('contentRaw', $contentRaw, true); + + // Parse the content + $content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities + $content = $Parsedown->text($content); // Parse Markdown. + $content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute. + $Post->setField('content', $content, true); + + // Pagebrake + $explode = explode(PAGE_BREAK, $content); + $Post->setField('breakContent', $explode[0], true); + $Post->setField('readMore', !empty($explode[1]), true); + + // Date format + $postDate = $Post->date(); + $Post->setField('dateRaw', $postDate, true); + + $postDateFormated = $Post->dateRaw( $Site->dateFormat() ); + $Post->setField('date', $postDateFormated, true); + + // User object + $username = $Post->username(); + $Post->setField('user', $dbUsers->getUser($username)); + + return $Post; +} + +function buildPostsForPage($pageNumber=0, $amount=POSTS_PER_PAGE_ADMIN, $removeUnpublished=true, $tagKey=false) +{ + global $dbPosts; + global $dbTags; + global $Url; + + $posts = array(); + + if($tagKey) { + // Get the keys list from tags database, this database is optimized for this case. + $list = $dbTags->getList($pageNumber, $amount, $tagKey); + } + else { + // Get the keys list from posts database. + $list = $dbPosts->getList($pageNumber, $amount, $removeUnpublished); + } + + // There are not posts for the page number then set the page notfound + if(empty($list) && $pageNumber>0) { + $Url->setNotFound(true); + } + + // Foreach post key, build the post. + foreach($list as $postKey=>$values) + { + $Post = buildPost($postKey); + if($Post!==false) { + array_push($posts, $Post); + } + } + + return $posts; +} + +// PAGE FUNCTIONS +// ---------------------------------------------------------------------------- + + +function sortPages($a, $b) +{ + if ($a['position'] == $b['position']) { + return 0; + } + + return ($a['position'] < $b['position']) ? -1 : 1; +} + +function buildPage($key) +{ + global $dbPages; + global $dbUsers; + global $Parsedown; + global $Site; + + // Page object, content from FILE. + $Page = new Page($key); + if( !$Page->isValid() ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from file with key: '.$key); + return false; + } + + // Page database, content from DATABASE JSON. + $db = $dbPages->getPageDB($key); + if( !$db ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying build the page from database with key: '.$key); + return false; + } + + // Foreach field from DATABASE. + foreach($db as $field=>$value) { + $Page->setField($field, $value); + } + + // Content in raw format + $contentRaw = $Page->content(); + $Page->setField('contentRaw', $Page->content(), true); + + // Parse markdown content. + $content = Text::pre2htmlentities($contentRaw); // Parse pre code with htmlentities + $content = $Parsedown->text($content); // Parse Markdown. + $content = Text::imgRel2Abs($content, HTML_PATH_UPLOADS); // Parse img src relative to absolute. + $Page->setField('content', $content, true); + + // Pagebrake + $explode = explode(PAGE_BREAK, $content); + $Page->setField('breakContent', $explode[0], true); + $Page->setField('readMore', !empty($explode[1]), true); + + // Date format + $pageDate = $Page->date(); + $Page->setField('dateRaw', $pageDate, true); + + $pageDateFormated = $Page->dateRaw( $Site->dateFormat() ); + $Page->setField('date', $pageDateFormated, true); + + // User object + $username = $Page->username(); + $Page->setField('user', $dbUsers->getUser($username)); + + return $Page; +} + +function buildAllPages() +{ + global $pagesParents; + global $pagesParentsPublished; + global $pagesPublished; + global $dbPages; + + // Get the page list + $list = $dbPages->getDB(); + + // Clean pages array. + $pages = array(); + + // Remove the error page + unset($list['error']); + + // Sorte pages + uasort($list, 'sortPages'); + + foreach($list as $key=>$db) + { + $Page = buildPage($key); + + if($Page!==false) + { + // Filter pages, with and without parent + + // If the page doesn't have a father, it's a parent page :P + if( $Page->parentKey()===false ) { + // Add the parent key in the dbPages + $dbPages->addParentKey($Page->key()); + + // Add the page as a parent page in the array + $pagesParents[NO_PARENT_CHAR][$Page->key()] = $Page; + + // If the page is published + if($Page->published()) { + $pagesParentsPublished[NO_PARENT_CHAR][$Page->key()] = $Page; + } + } + else { + $pagesParents[$Page->parentKey()][$Page->key()] = $Page; + + // If the page is published + if($Page->published()) { + $pagesParentsPublished[$Page->parentKey()][$Page->key()] = $Page; + } + } + + // All pages in one array + $pages[$Page->key()] = $Page; + + // If the page is published + if($Page->published()) { + $pagesPublished[$Page->parentKey()][$Page->key()] = $Page; + } + } + } + + return $pages; +} \ No newline at end of file diff --git a/bl-kernel/helpers/text.class.php b/bl-kernel/helpers/text.class.php index ad3d7999..14153a57 100644 --- a/bl-kernel/helpers/text.class.php +++ b/bl-kernel/helpers/text.class.php @@ -78,27 +78,24 @@ class Text { return $string; } + public static function startsWith($string, $startString) + { + $length = self::length($startString); + + return( mb_substr($string, 0, $length)===$startString ); + } + public static function endsWith($string, $endsString) { - $endsPosition = (-1)*self::length($endsString); + $length = (-1)*self::length($endsString); - if(MB_STRING) { - return( mb_substr($string, $endsPosition)===$endsString ); - } - - return( substr($string, $endsPosition)===$endsString ); + return( mb_substr($string, $length)===$endsString ); } public static function endsWithNumeric($string) { - $endsPosition = (-1)*self::length($string); - - if(MB_STRING) { - return( is_numeric(mb_substr($string, -1, 1)) ); - } - - return( is_numeric(substr($string, -1, 1)) ); + return( is_numeric(mb_substr($string, -1, 1)) ); } public static function randomText($length) @@ -142,48 +139,30 @@ class Text { // String to lowercase public static function lowercase($string, $encoding='UTF-8') { - if(MB_STRING) { - return mb_strtolower($string, $encoding); - } - - return strtolower($string); + return mb_strtolower($string, $encoding); } // Make a string's first character uppercase public static function firstCharUp($string, $encoding='UTF-8') { // Thanks http://stackoverflow.com/questions/2517947/ucfirst-function-for-multibyte-character-encodings - if(MB_STRING) - { - $strlen = mb_strlen($string, $encoding); - $firstChar = mb_substr($string, 0, 1, $encoding); - $then = mb_substr($string, 1, $strlen - 1, $encoding); + $strlen = mb_strlen($string, $encoding); + $firstChar = mb_substr($string, 0, 1, $encoding); + $then = mb_substr($string, 1, $strlen - 1, $encoding); - return mb_strtoupper($firstChar, $encoding).$then; - } - - return ucfirst($string); + return mb_strtoupper($firstChar, $encoding).$then; } // Find position of first occurrence of substring in a string otherwise returns FALSE. public static function stringPosition($string, $substring) { - if(MB_STRING) { - return mb_strpos($string, $substring, 0, 'UTF-8'); - } - - return strpos($string, $substring); + return mb_strpos($string, $substring, 0, 'UTF-8'); } // Returns the portion of string specified by the start and length parameters. public static function cut($string, $start, $length) { - if(MB_STRING) { - $cut = mb_substr($string, $start, $length, 'UTF-8'); - } - else { - $cut = substr($string, $start, $length); - } + $cut = mb_substr($string, $start, $length, 'UTF-8'); if(empty($cut)) { return ''; @@ -195,17 +174,16 @@ class Text { // Return string length public static function length($string) { - if(MB_STRING) - return mb_strlen($string, 'UTF-8'); - return strlen($string); + return mb_strlen($string, 'UTF-8'); } public static function isEmpty($string) { $string = trim($string); - if(empty($string)) + if(empty($string)) { return true; + } return false; } @@ -227,4 +205,4 @@ class Text { $string); } -} +} \ No newline at end of file diff --git a/bl-kernel/page.class.php b/bl-kernel/page.class.php index 7fcceb96..8f44ee2b 100644 --- a/bl-kernel/page.class.php +++ b/bl-kernel/page.class.php @@ -67,4 +67,14 @@ class Page extends Content { return $tmp; } + public function json() + { + $tmp['key'] = $this->key(); + $tmp['title'] = $this->title(); + $tmp['content'] = $this->content(); // Markdown parsed + $tmp['contentRaw'] = $this->contentRaw(); // No Markdown parsed + $tmp['description'] = $this->description(); + + return json_encode($tmp); + } } \ No newline at end of file diff --git a/bl-kernel/post.class.php b/bl-kernel/post.class.php index ddeafdde..d358ea25 100644 --- a/bl-kernel/post.class.php +++ b/bl-kernel/post.class.php @@ -28,4 +28,15 @@ class Post extends Content { { return ($this->getField('status')==='scheduled'); } + + public function json() + { + $tmp['key'] = $this->key(); + $tmp['title'] = $this->title(); + $tmp['content'] = $this->content(); // Markdown parsed + $tmp['contentRaw'] = $this->contentRaw(); // No Markdown parsed + $tmp['description'] = $this->description(); + + return json_encode($tmp); + } } \ No newline at end of file diff --git a/bl-plugins/api/languages/ar_MA.json b/bl-plugins/api/languages/ar_MA.json new file mode 100644 index 00000000..ca0383aa --- /dev/null +++ b/bl-plugins/api/languages/ar_MA.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "RSS Feed", + "description": "هذه الإضافة تساعد على توليد تغذية RSS لموقعك." + } +} \ No newline at end of file diff --git a/bl-plugins/api/languages/en_US.json b/bl-plugins/api/languages/en_US.json new file mode 100644 index 00000000..0e7a6df8 --- /dev/null +++ b/bl-plugins/api/languages/en_US.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "API", + "description": "API for Bludit" + } +} \ No newline at end of file diff --git a/bl-plugins/api/languages/es_AR.json b/bl-plugins/api/languages/es_AR.json new file mode 100644 index 00000000..a17b665f --- /dev/null +++ b/bl-plugins/api/languages/es_AR.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "RSS Feed", + "description": "Este plugin genera contenido RSS Feed para su sitio." + } +} \ No newline at end of file diff --git a/bl-plugins/api/languages/ja_JP.json b/bl-plugins/api/languages/ja_JP.json new file mode 100644 index 00000000..16e3ac94 --- /dev/null +++ b/bl-plugins/api/languages/ja_JP.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "RSS Feed", + "description": "サイトのRSSフィードを生成します。" + } +} \ No newline at end of file diff --git a/bl-plugins/api/languages/ru_RU.json b/bl-plugins/api/languages/ru_RU.json new file mode 100644 index 00000000..12fd3c91 --- /dev/null +++ b/bl-plugins/api/languages/ru_RU.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "RSS Feed", + "description": "Этот плагин генерирует RSS трансляцию на сайте." + } +} diff --git a/bl-plugins/api/languages/uk_UA.json b/bl-plugins/api/languages/uk_UA.json new file mode 100644 index 00000000..4e6a28ee --- /dev/null +++ b/bl-plugins/api/languages/uk_UA.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "RSS-канал", + "description": "Цей плагін генерувати RSS для вашого сайту." + } +} \ No newline at end of file diff --git a/bl-plugins/api/metadata.json b/bl-plugins/api/metadata.json new file mode 100644 index 00000000..acd7626f --- /dev/null +++ b/bl-plugins/api/metadata.json @@ -0,0 +1,10 @@ +{ + "author": "Bludit", + "email": "", + "website": "https://github.com/dignajar/bludit-plugins", + "version": "1.3", + "releaseDate": "2016-05-28", + "license": "MIT", + "compatible": "1.0,1.1,1.1.2,1.3", + "notes": "" +} \ No newline at end of file diff --git a/bl-plugins/api/plugin.php b/bl-plugins/api/plugin.php new file mode 100644 index 00000000..06c66c7a --- /dev/null +++ b/bl-plugins/api/plugin.php @@ -0,0 +1,90 @@ +'0', + 'bludit'=>'Bludit API plugin', + 'message'=>'The post doesn\'t exist' + )); + } + + return $Post->json(); + } + + public function getPage($key) + { + // Generate the object Page + $Page = buildPage($key); + + if(!$Page) { + return json_encode(array( + 'status'=>'0', + 'bludit'=>'Bludit API plugin', + 'message'=>'The post doesn\'t exist' + )); + } + + return $Page->json(); + } + + public function beforeRulesLoad() + { + global $Url; + + // The URI start with /api/ + $startString = HTML_PATH_ROOT.'api/'; + $URI = $Url->uri(); + $length = mb_strlen($startString, CHARSET); + if( mb_substr($URI, 0, $length)!=$startString ) { + return false; + } + + // Remove the first part of the URI + $URI = ltrim($URI, HTML_PATH_ROOT.'api/'); + + // Parameters + // ------------------------------------------------------------ + // show post {post slug} + // show page {page slug} + + // Get parameters + $parameters = explode('/', $URI); + + // Check parameters + for($i=0; $i<3; $i++) { + if(empty($parameters[$i])) { + return false; + } + } + + // Default JSON + $json = json_encode(array( + 'status'=>'0', + 'bludit'=>'Bludit API plugin', + 'message'=>'Check the parameters' + )); + + if($parameters[0] === 'show') { + + $key = $parameters[2]; + + if($parameters[1] === 'post') { + $json = $this->getPost($key); + } + elseif($parameters[1] === 'page') { + $json = $this->getPage($key); + } + } + + // Print the JSON + header('Content-Type: application/json'); + exit($json); + } +} \ No newline at end of file