New plugin, API

This commit is contained in:
dignajar 2016-05-29 14:21:11 -03:00
parent 9827b18b43
commit dbf7a14854
15 changed files with 427 additions and 292 deletions

View File

@ -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');

View File

@ -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
// ============================================================================

View File

@ -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
// ============================================================================

240
bl-kernel/functions.php Normal file
View File

@ -0,0 +1,240 @@
<?php defined('BLUDIT') or die('Bludit CMS.');
// POST 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;
}
// 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;
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "RSS Feed",
"description": "هذه الإضافة تساعد على توليد تغذية RSS لموقعك."
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "API",
"description": "API for Bludit"
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "RSS Feed",
"description": "Este plugin genera contenido RSS Feed para su sitio."
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "RSS Feed",
"description": "サイトのRSSフィードを生成します。"
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "RSS Feed",
"description": "Этот плагин генерирует RSS трансляцию на сайте."
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "RSS-канал",
"description": "Цей плагін генерувати RSS для вашого сайту."
}
}

View File

@ -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": ""
}

90
bl-plugins/api/plugin.php Normal file
View File

@ -0,0 +1,90 @@
<?php
class pluginAPI extends Plugin {
public function getPost($key)
{
// Generate the object Post
$Post = buildPost($key);
if(!$Post) {
return json_encode(array(
'status'=>'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);
}
}