This commit is contained in:
dignajar 2016-12-01 20:59:58 -03:00
parent 0dc4dbdebf
commit 4c0a03a21a
1 changed files with 44 additions and 20 deletions

View File

@ -1,5 +1,6 @@
<?php <?php
class pluginAPI extends Plugin { class pluginAPI extends Plugin {
public function init() public function init()
@ -75,6 +76,8 @@ class pluginAPI extends Plugin {
public function beforeRulesLoad() public function beforeRulesLoad()
{ {
global $Url; global $Url;
global $dbPosts;
global $dbPages;
// Check if the URI start with /api/ // Check if the URI start with /api/
$startString = HTML_PATH_ROOT.'api/'; $startString = HTML_PATH_ROOT.'api/';
@ -104,13 +107,18 @@ class pluginAPI extends Plugin {
if( empty($inputs) ) { if( empty($inputs) ) {
// Default variables for $input // Default variables for $input
$inputs = array(); $inputs = array(
'token'=>''
);
} }
else { else {
// Sanitize inputs // Sanitize inputs
foreach( $inputs as $key=>$value ) { foreach( $inputs as $key=>$value ) {
if(empty($value)) { if(empty($value)) {
return false; $this->response(array(
'status'=>'1',
'message'=>'Invalid input.'
));
} else { } else {
$inputs[$key] = Sanitize::html($value); $inputs[$key] = Sanitize::html($value);
} }
@ -123,14 +131,17 @@ class pluginAPI extends Plugin {
// /api/posts/{slug} | GET | returns the post with the {slug} // /api/posts/{slug} | GET | returns the post with the {slug}
// /api/pages | GET | returns all pages // /api/pages | GET | returns all pages
// /api/pages/{slug} | GET | returns the page with the {slug} // /api/pages/{slug} | GET | returns the page with the {slug}
// /api/cli/regenerate | PUT | check for new posts and pages // /api/cli/regenerate | POST | check for new posts and pages
$parameters = explode('/', $URI); $parameters = explode('/', $URI);
// Sanitize parameters // Sanitize parameters
foreach( $parameters as $key=>$value ) { foreach( $parameters as $key=>$value ) {
if(empty($value)) { if(empty($value)) {
return false; $this->response(array(
'status'=>'1',
'message'=>'Invalid parameter.'
));
} else { } else {
$parameters[$key] = Sanitize::html($value); $parameters[$key] = Sanitize::html($value);
} }
@ -138,10 +149,6 @@ class pluginAPI extends Plugin {
// Check authentication // Check authentication
if( $this->getDbField('authentication')==1 ) { if( $this->getDbField('authentication')==1 ) {
if( empty($inputs['token']) ) {
return false;
}
if( $inputs['token']!=$this->getDbField('token') ) { if( $inputs['token']!=$this->getDbField('token') ) {
$this->response(array( $this->response(array(
'status'=>'1', 'status'=>'1',
@ -170,7 +177,22 @@ class pluginAPI extends Plugin {
$data = $this->getPage($parameters[1]); $data = $this->getPage($parameters[1]);
$this->response($data); $this->response($data);
} }
// /api/cli/regenerate
elseif( ($method==='POST') && ($parameters[0]==='cli') && ($parameters[1]==='regenerate') ) {
// Regenerate posts
if( $dbPosts->cliMode() ) {
reIndexTagsPosts();
}
// Regenerate pages
$dbPages->cliMode();
$this->response(array(
'status'=>'0',
'message'=>'Pages and post regenerated.'
));
}
} }
// FUNCTIONS // FUNCTIONS
@ -234,9 +256,9 @@ class pluginAPI extends Plugin {
); );
} }
$data = $Post->json(true);
$data['status'] = '0'; $data['status'] = '0';
$data['message'] = ''; $data['message'] = '';
$data['data'] = $Post->json( $returnsArray=true );
return $data; return $data;
} }
@ -247,11 +269,12 @@ class pluginAPI extends Plugin {
$tmp = array( $tmp = array(
'status'=>'0', 'status'=>'0',
'message'=>'' 'message'=>'',
'data'=>array()
); );
foreach($posts as $Post) { foreach($posts as $Post) {
array_push($tmp, $Post->json( $returnsArray=true )); array_push($tmp['data'], $Post->json( $returnsArray=true ));
} }
return $tmp; return $tmp;
@ -269,9 +292,9 @@ class pluginAPI extends Plugin {
); );
} }
$data = $Page->json(true);
$data['status'] = '0'; $data['status'] = '0';
$data['message'] = ''; $data['message'] = '';
$data['data'] = $Page->json( $returnsArray=true );
return $data; return $data;
} }
@ -282,12 +305,13 @@ class pluginAPI extends Plugin {
$tmp = array( $tmp = array(
'status'=>'0', 'status'=>'0',
'message'=>'' 'message'=>'',
'data'=>array()
); );
foreach($pages as $Page) { foreach($pages as $Page) {
if($Page->published()) { if($Page->published()) {
array_push($tmp, $Page->json( $returnsArray=true )); array_push($tmp['data'], $Page->json( $returnsArray=true ));
} }
} }