From 8aa7c7c56dd14adfc79832e558f01725c302281a Mon Sep 17 00:00:00 2001
From: krasi georgiev <krasi@vip-consult.co.uk>
Date: Wed, 2 Nov 2016 16:19:12 +0000
Subject: [PATCH] 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

---
 bl-plugins/api/plugin.php | 70 ++++++++++++++++++++++-----------------
 1 file changed, 40 insertions(+), 30 deletions(-)

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