Added Other AP
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<IfModule mod_rewrite.c>
|
||||
|
||||
RewriteEngine On
|
||||
RewriteBase /SAVVY/blog/
|
||||
#RewriteBase /
|
||||
|
||||
#Checks to
|
||||
RewriteCond %{REQUEST_FILENAME} !-f
|
||||
RewriteCond %{REQUEST_FILENAME} !-d
|
||||
RewriteRule ^(.*)$ index.php?/$1 [L]
|
||||
|
||||
</IfModule>
|
||||
|
||||
<IfModule !mod_rewrite.c>
|
||||
# If we don't have mod_rewrite installed, all 404's
|
||||
# can be sent to index.php, and everything works as normal.
|
||||
# Submitted by: ElliotHaughin
|
||||
|
||||
ErrorDocument 404 /index.php
|
||||
|
||||
</IfModule>
|
||||
|
||||
#Header add Access-Control-Allow-Origin "*"
|
||||
#Header add Access-Control-Expose-Headers "Access-Control-Allow-Origin"
|
||||
#Header add Access-Control-Allow-Headers "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With"
|
||||
#Header add Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
|
||||
#Header add Content-type "application/json"
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
class Blog {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// vi:ts=2
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
class BlogApi extends Api
|
||||
{
|
||||
public $apiName = 'blog';
|
||||
|
||||
public function __construct($requestUri, $encryption=true) {
|
||||
$this->cacheWhitelist = [
|
||||
"viewAction" => ['ttl' => 900], // 900 sec. = 15 min.
|
||||
"indexAction" => ['ttl' => 900]
|
||||
];
|
||||
parent::__construct($requestUri, $encryption);
|
||||
}
|
||||
|
||||
public function indexAction()
|
||||
{
|
||||
global $savvyext;
|
||||
$message = "Data not found";
|
||||
$limit = trim($this->requestParams["limit"] ?? "");
|
||||
$page = trim($this->requestParams["page"] ?? "");
|
||||
$params =[
|
||||
'limit' => $limit,
|
||||
'page' => $page
|
||||
];
|
||||
|
||||
$url = $savvyext->cfgReadChar('system.blog_api_url') . "/latest-articles?".http_build_query($params);
|
||||
|
||||
try {
|
||||
$body = file_get_contents($url, false);
|
||||
$result = json_decode($body, true);
|
||||
|
||||
return $this->response(
|
||||
array(
|
||||
'data' => $result,
|
||||
'error' => ""),
|
||||
200);
|
||||
} catch (Exception $e) {
|
||||
error_log(json_encode($e));
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
|
||||
return $this->response(
|
||||
array(
|
||||
"error" => $message,
|
||||
), 500);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method GET
|
||||
* Get single record (by id)
|
||||
* http://DOMAIN/blog/679
|
||||
* @return string
|
||||
*/
|
||||
public function viewAction()
|
||||
{
|
||||
global $savvyext;
|
||||
$message = "Data not found";
|
||||
$id = array_shift($this->requestUri);
|
||||
if(!empty($id)){
|
||||
$url = $savvyext->cfgReadChar('system.blog_api_url') . "/articles/" . intval($id);
|
||||
try {
|
||||
$body = file_get_contents($url, false);
|
||||
$result = json_decode($body, true);
|
||||
|
||||
return $this->response(
|
||||
array(
|
||||
'data' => $result,
|
||||
'error' => ""), 200);
|
||||
} catch (Exception $e) {
|
||||
error_log(json_encode($e));
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $this->response(
|
||||
array(
|
||||
"error" => $message,
|
||||
), 500);
|
||||
}
|
||||
|
||||
public function createAction()
|
||||
{
|
||||
return $this->response(
|
||||
array(
|
||||
"error" => "Data not found",
|
||||
), 400);
|
||||
|
||||
}
|
||||
|
||||
public function updateAction()
|
||||
{
|
||||
return $this->response(
|
||||
array(
|
||||
"error" => "Update error",
|
||||
), 400);
|
||||
}
|
||||
|
||||
public function deleteAction()
|
||||
{
|
||||
return $this->response(
|
||||
array(
|
||||
"error" => "Delete error",
|
||||
), 500);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
require_once('../../core/backend.php');
|
||||
require_once('../constants.php');
|
||||
|
||||
require_once('../common/Api.php');
|
||||
require_once('../common/Db.php');
|
||||
|
||||
require_once('Blog.php');
|
||||
require_once('BlogApi.php');
|
||||
|
||||
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
|
||||
$bankloginAuthToken = $savvyext->cfgReadChar('system.bank_token');
|
||||
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Expose-Headers: Access-Control-Allow-Origin");
|
||||
header("Access-Control-Allow-Headers: Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, client_id");
|
||||
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS");
|
||||
header('Content-type: application/json');
|
||||
|
||||
if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) {
|
||||
exit();
|
||||
}
|
||||
|
||||
$headers = getallheaders();
|
||||
if ((!isset($headers["Authorization"]) || substr($headers["Authorization"],-strlen($httpAuthToken))!=$httpAuthToken) &&
|
||||
(!isset($headers["authorization"]) || substr($headers["authorization"],-strlen($httpAuthToken))!=$httpAuthToken)) {
|
||||
header('HTTP/1.1 401 Unauthorized');
|
||||
header('Status: 401 Unauthorized');
|
||||
echo "{\"status\":\"Missing authorization\"}";
|
||||
exit();
|
||||
}
|
||||
|
||||
try {
|
||||
if (strpos($_SERVER['REQUEST_URI'],'/api/')===false) {
|
||||
throw new Exception("Invalid API request");
|
||||
}
|
||||
$requestUri = explode('/', trim($_SERVER['REQUEST_URI'],'/'));
|
||||
while (array_shift($requestUri) !== 'api') {
|
||||
};
|
||||
if ($requestUri[0]=='blog') {
|
||||
$api = new BlogApi($requestUri);
|
||||
}
|
||||
else {
|
||||
echo json_encode(Array('error' => 'Invalid API request'));
|
||||
}
|
||||
|
||||
echo $api->run();
|
||||
}
|
||||
catch (Exception $e) {
|
||||
echo json_encode(Array('error' => $e->getMessage()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user