This commit is contained in:
Diego Najar 2016-12-01 15:09:29 -03:00
parent 89aec3a56d
commit 72c624db61
2 changed files with 49 additions and 32 deletions

View File

@ -3,7 +3,7 @@
// Load plugins rules // Load plugins rules
include(PATH_RULES.'60.plugins.php'); include(PATH_RULES.'60.plugins.php');
// Plugins before rules loaded, except plugins rules // Plugins before rules loaded
Theme::plugins('beforeRulesLoad'); Theme::plugins('beforeRulesLoad');
// Load rules // Load rules

View File

@ -141,42 +141,47 @@ class pluginAPI extends Plugin {
// Check authentication // Check authentication
if( $this->getDbField('authentication')==1 ) { if( $this->getDbField('authentication')==1 ) {
if( $inputs['token']!=$this->getDbField('token') ) { if( $inputs['token']!=$this->getDbField('token') ) {
return false; $this->response(array(
'status'=>'1',
'message'=>'Invalid token.'
));
} }
} }
// Default JSON
$json = json_encode(array(
'status'=>'0', // 0 = ok, 1 = error
'bludit'=>'Bludit API plugin',
'message'=>'Missing parameters, check the URL.'
));
// /api/posts // /api/posts
if( ($method==='GET') && ($parameters[0]==='posts') && empty($parameters[1]) ) { if( ($method==='GET') && ($parameters[0]==='posts') && empty($parameters[1]) ) {
$json = $this->getAllPosts(); $data = $this->getAllPosts();
$this->response($data);
} }
// /api/pages // /api/pages
elseif( ($method==='GET') && ($parameters[0]==='pages') && empty($parameters[1]) ) { elseif( ($method==='GET') && ($parameters[0]==='pages') && empty($parameters[1]) ) {
$json = $this->getAllPages(); $data = $this->getAllPages();
$this->response($data);
} }
// /api/posts/{slug} // /api/posts/{slug}
elseif( ($method==='GET') && ($parameters[0]==='posts') && !empty($parameters[1]) ) { elseif( ($method==='GET') && ($parameters[0]==='posts') && !empty($parameters[1]) ) {
$json = $this->getPost($key); $data = $this->getPost($parameters[1]);
$this->response($data);
} }
// /api/pages/{slug} // /api/pages/{slug}
elseif( ($method==='GET') && ($parameters[0]==='pages') && !empty($parameters[1]) ) { elseif( ($method==='GET') && ($parameters[0]==='pages') && !empty($parameters[1]) ) {
$json = $this->getPage($key); $data = $this->getPage($parameters[1]);
$this->response($data);
} }
// Print the JSON
header('Content-Type: application/json');
exit($json);
} }
// FUNCTIONS // FUNCTIONS
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
private function response($data=array())
{
$json = json_encode($data);
header('Content-Type: application/json');
exit($json);
}
private function ping() private function ping()
{ {
if($this->getDbField('ping')) { if($this->getDbField('ping')) {
@ -221,27 +226,33 @@ class pluginAPI extends Plugin {
$Post = buildPost($key); $Post = buildPost($key);
if(!$Post) { if(!$Post) {
return json_encode(array( return array(
'status'=>'0', 'status'=>'1',
'bludit'=>'Bludit API plugin', 'message'=>'Post not found.'
'message'=>'The post doesn\'t exist' );
));
} }
return $Post->json(); $data = $Post->json(true);
$data['status'] = '0';
$data['message'] = '';
return $data;
} }
private function getAllPosts() private function getAllPosts()
{ {
$posts = buildPostsForPage(0, $this->getDbField('showAllAmount'), true, false); $posts = buildPostsForPage(0, $this->getDbField('showAllAmount'), true, false);
$tmp = array(); $tmp = array(
'status'=>'0',
'message'=>''
);
foreach($posts as $Post) { foreach($posts as $Post) {
array_push($tmp, $Post->json( $returnsArray=true )); array_push($tmp, $Post->json( $returnsArray=true ));
} }
return json_encode($tmp); return $tmp;
} }
private function getPage($key) private function getPage($key)
@ -250,21 +261,27 @@ class pluginAPI extends Plugin {
$Page = buildPage($key); $Page = buildPage($key);
if(!$Page) { if(!$Page) {
return json_encode(array( return array(
'status'=>'0', 'status'=>'1',
'bludit'=>'Bludit API plugin', 'message'=>'Page not found.'
'message'=>'The page doesn\'t exist' );
));
} }
return $Page->json(); $data = $Page->json(true);
$data['status'] = '0';
$data['message'] = '';
return $data;
} }
private function getAllPages() private function getAllPages()
{ {
$pages = buildAllPages(); $pages = buildAllPages();
$tmp = array(); $tmp = array(
'status'=>'0',
'message'=>''
);
foreach($pages as $Page) { foreach($pages as $Page) {
if($Page->published()) { if($Page->published()) {
@ -272,7 +289,7 @@ class pluginAPI extends Plugin {
} }
} }
return json_encode($tmp); return $tmp;
} }
} }