add another api endpoint for the api pluign to get posts by tagname, rafactor a bit the api plugin andnow it needs an access key for all requests

This commit is contained in:
krasi georgiev 2016-11-02 16:19:12 +00:00
parent bad5a1c3f2
commit 8aa7c7c56d

View File

@ -133,6 +133,14 @@ class pluginAPI extends Plugin {
return json_encode($tmp); return json_encode($tmp);
} }
private function getTagPosts($tag,$amount=3,$pageNumber=0)
{
$posts = buildTagPosts($tag,$amount,$pageNumber);
return json_encode($posts);
}
private function getPage($key) private function getPage($key)
{ {
@ -167,7 +175,7 @@ class pluginAPI extends Plugin {
public function beforeRulesLoad() public function beforeRulesLoad()
{ {
global $Url; global $Url;
// The URI start with /api/ // The URI start with /api/
$startString = HTML_PATH_ROOT.'api/'; $startString = HTML_PATH_ROOT.'api/';
@ -176,6 +184,8 @@ class pluginAPI extends Plugin {
if( mb_substr($URI, 0, $length)!=$startString ) { if( mb_substr($URI, 0, $length)!=$startString ) {
return false; return false;
} }
header('Content-Type: application/json');
// Remove the first part of the URI // Remove the first part of the URI
$URI = mb_substr($URI, $length); $URI = mb_substr($URI, $length);
@ -188,41 +198,44 @@ class pluginAPI extends Plugin {
// show all pages {AUTH KEY} // show all pages {AUTH KEY}
// Get parameters // Get parameters
$parameters = explode('/', $URI); $parameters = explode('/', $URI);
for($i=0; $i<3; $i++) {
if(empty($parameters[$i])) { // Default JSON
return false;
} else {
// Sanizite
$parameters[$i] = Sanitize::html($parameters[$i]);
}
}
// Default JSON
$json = json_encode(array( $json = json_encode(array(
'status'=>'0', 'status'=>'0',
'bludit'=>'Bludit API plugin', 'bludit'=>'Bludit API plugin',
'message'=>'Check the parameters' 'message'=>'Check the parameters'
)); ));
if(!isset($_GET['key']) OR $_GET['key']!==$this->getDbField('authKey') ){
exit($json);
}
for($i=0; $i<count($parameters); $i++) {
// Sanizite
$parameters[$i] = Sanitize::html($parameters[$i]);
}
if($parameters[0]==='show') {
if($parameters[0]==='show') {
if($parameters[1]==='all') { if($parameters[1]==='all') {
// Authentication key from the URI if($parameters[2] === 'posts') {
$authKey = $parameters[3]; $json = $this->getAllPosts();
}
// Compare keys elseif($parameters[2] === 'pages') {
if( $authKey===$this->getDbField('authKey') ) { $json = $this->getAllPages();
}
if($parameters[2] === 'posts') { }
$json = $this->getAllPosts(); elseif($parameters[1]==='tag') {
} if(isset($parameters[2]) AND isset($parameters[3])AND isset($parameters[4])){
elseif($parameters[2] === 'pages') { $tag = $parameters[2];
$json = $this->getAllPages(); $limit = $parameters[3];
} $page = $parameters[4];
} $json = $this->getTagPosts($tag,$limit,$page);
}
} }
elseif($parameters[1]==='post' || $parameters[1]==='page') { elseif($parameters[1]==='post' || $parameters[1]==='page') {
@ -236,9 +249,6 @@ class pluginAPI extends Plugin {
} }
} }
} }
exit($json);
// Print the JSON
header('Content-Type: application/json');
exit($json);
} }
} }