2016-05-29 19:21:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class pluginAPI extends Plugin {
|
|
|
|
|
2016-05-30 05:36:13 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2016-06-05 03:31:07 +02:00
|
|
|
global $Security;
|
|
|
|
|
|
|
|
// This key is used for request such as get the list of all posts and pages
|
2016-12-01 02:39:16 +01:00
|
|
|
$token = md5($Security->key1().time().DOMAIN);
|
2016-06-05 03:31:07 +02:00
|
|
|
|
2016-05-30 05:36:13 +02:00
|
|
|
$this->dbFields = array(
|
2016-12-01 02:39:16 +01:00
|
|
|
'ping'=>0, // 0 = false, 1 = true
|
|
|
|
'token'=>$token, // Private key
|
|
|
|
'showAllAmount'=>15, // Amount of posts and pages for return
|
|
|
|
'authentication'=>1 // Authentication required
|
2016-05-30 05:36:13 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function form()
|
|
|
|
{
|
2016-06-03 03:37:52 +02:00
|
|
|
$html = '';
|
|
|
|
|
|
|
|
$html .= '<div>';
|
2017-05-10 21:27:18 +02:00
|
|
|
$html .= '<p><b>Token:</b> '.$this->getDbField('token').'</p>';
|
2016-06-05 03:31:07 +02:00
|
|
|
$html .= '<div class="tip">This key is private, do not share it with anyone.</div>';
|
|
|
|
$html .= '</div>';
|
|
|
|
|
2017-05-10 21:27:18 +02:00
|
|
|
$html .= '<div>Check the documentation about the API <a href="https://docs.bludit.com/en/api/introduction">Bludit Docs API</a></div>';
|
2016-06-03 03:37:52 +02:00
|
|
|
|
2016-05-30 05:36:13 +02:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2016-12-01 02:39:16 +01:00
|
|
|
|
|
|
|
// API HOOKS
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public function beforeRulesLoad()
|
2016-09-25 20:38:15 +02:00
|
|
|
{
|
2016-12-01 02:39:16 +01:00
|
|
|
global $Url;
|
2016-12-02 00:59:58 +01:00
|
|
|
global $dbPosts;
|
|
|
|
global $dbPages;
|
2016-09-25 20:38:15 +02:00
|
|
|
|
2016-12-01 02:39:16 +01:00
|
|
|
// Check if the URI start with /api/
|
|
|
|
$startString = HTML_PATH_ROOT.'api/';
|
|
|
|
$URI = $Url->uri();
|
|
|
|
$length = mb_strlen($startString, CHARSET);
|
|
|
|
if( mb_substr($URI, 0, $length)!=$startString ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the first part of the URI
|
|
|
|
$URI = mb_substr($URI, $length);
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
// GET
|
|
|
|
// POST
|
|
|
|
// PUT
|
|
|
|
// DELETE
|
|
|
|
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
|
|
|
|
// INPUTS
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
// token | authentication token
|
|
|
|
|
|
|
|
$inputs = json_decode(file_get_contents('php://input'),true);
|
|
|
|
|
|
|
|
if( empty($inputs) ) {
|
|
|
|
// Default variables for $input
|
2016-12-02 00:59:58 +01:00
|
|
|
$inputs = array(
|
|
|
|
'token'=>''
|
|
|
|
);
|
2016-12-01 02:39:16 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Sanitize inputs
|
|
|
|
foreach( $inputs as $key=>$value ) {
|
|
|
|
if(empty($value)) {
|
2016-12-02 00:59:58 +01:00
|
|
|
$this->response(array(
|
|
|
|
'status'=>'1',
|
|
|
|
'message'=>'Invalid input.'
|
|
|
|
));
|
2016-12-01 02:39:16 +01:00
|
|
|
} else {
|
|
|
|
$inputs[$key] = Sanitize::html($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PARAMETERS
|
|
|
|
// ------------------------------------------------------------
|
2016-12-02 00:59:58 +01:00
|
|
|
// /api/posts | GET | returns all posts
|
2017-04-26 18:56:10 +02:00
|
|
|
// /api/posts/{key} | GET | returns the post with the {key}
|
2016-12-02 00:59:58 +01:00
|
|
|
// /api/pages | GET | returns all pages
|
2017-04-26 18:56:10 +02:00
|
|
|
// /api/pages/{key} | GET | returns the page with the {key}
|
2016-12-02 00:59:58 +01:00
|
|
|
// /api/cli/regenerate | POST | check for new posts and pages
|
2016-12-01 02:39:16 +01:00
|
|
|
|
|
|
|
$parameters = explode('/', $URI);
|
|
|
|
|
|
|
|
// Sanitize parameters
|
|
|
|
foreach( $parameters as $key=>$value ) {
|
|
|
|
if(empty($value)) {
|
2016-12-02 00:59:58 +01:00
|
|
|
$this->response(array(
|
|
|
|
'status'=>'1',
|
|
|
|
'message'=>'Invalid parameter.'
|
|
|
|
));
|
2016-12-01 02:39:16 +01:00
|
|
|
} else {
|
|
|
|
$parameters[$key] = Sanitize::html($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check authentication
|
|
|
|
if( $this->getDbField('authentication')==1 ) {
|
|
|
|
if( $inputs['token']!=$this->getDbField('token') ) {
|
2016-12-01 19:09:29 +01:00
|
|
|
$this->response(array(
|
|
|
|
'status'=>'1',
|
|
|
|
'message'=>'Invalid token.'
|
|
|
|
));
|
2016-12-01 02:39:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// /api/posts
|
|
|
|
if( ($method==='GET') && ($parameters[0]==='posts') && empty($parameters[1]) ) {
|
2016-12-01 19:09:29 +01:00
|
|
|
$data = $this->getAllPosts();
|
|
|
|
$this->response($data);
|
2016-12-01 02:39:16 +01:00
|
|
|
}
|
|
|
|
// /api/pages
|
|
|
|
elseif( ($method==='GET') && ($parameters[0]==='pages') && empty($parameters[1]) ) {
|
2016-12-01 19:09:29 +01:00
|
|
|
$data = $this->getAllPages();
|
|
|
|
$this->response($data);
|
2016-12-01 02:39:16 +01:00
|
|
|
}
|
2017-04-26 18:56:10 +02:00
|
|
|
// /api/posts/{key}
|
2016-12-01 02:39:16 +01:00
|
|
|
elseif( ($method==='GET') && ($parameters[0]==='posts') && !empty($parameters[1]) ) {
|
2016-12-01 19:09:29 +01:00
|
|
|
$data = $this->getPost($parameters[1]);
|
|
|
|
$this->response($data);
|
2016-12-01 02:39:16 +01:00
|
|
|
}
|
2017-04-26 18:56:10 +02:00
|
|
|
// /api/pages/{key}
|
2016-12-01 02:39:16 +01:00
|
|
|
elseif( ($method==='GET') && ($parameters[0]==='pages') && !empty($parameters[1]) ) {
|
2016-12-01 19:09:29 +01:00
|
|
|
$data = $this->getPage($parameters[1]);
|
|
|
|
$this->response($data);
|
2016-12-01 02:39:16 +01:00
|
|
|
}
|
2016-12-02 00:59:58 +01:00
|
|
|
// /api/cli/regenerate
|
|
|
|
elseif( ($method==='POST') && ($parameters[0]==='cli') && ($parameters[1]==='regenerate') ) {
|
|
|
|
|
|
|
|
// Regenerate posts
|
|
|
|
if( $dbPosts->cliMode() ) {
|
|
|
|
reIndexTagsPosts();
|
|
|
|
}
|
2016-12-01 02:39:16 +01:00
|
|
|
|
2016-12-02 00:59:58 +01:00
|
|
|
// Regenerate pages
|
|
|
|
$dbPages->cliMode();
|
|
|
|
|
|
|
|
$this->response(array(
|
|
|
|
'status'=>'0',
|
|
|
|
'message'=>'Pages and post regenerated.'
|
|
|
|
));
|
|
|
|
}
|
2016-09-25 20:38:15 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 02:39:16 +01:00
|
|
|
// FUNCTIONS
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2016-12-01 19:09:29 +01:00
|
|
|
private function response($data=array())
|
|
|
|
{
|
|
|
|
$json = json_encode($data);
|
|
|
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
exit($json);
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:36:13 +02:00
|
|
|
private function ping()
|
|
|
|
{
|
|
|
|
if($this->getDbField('ping')) {
|
2016-06-05 03:31:07 +02:00
|
|
|
|
|
|
|
// Get the authentication key
|
2016-12-01 02:39:16 +01:00
|
|
|
$token = $this->getDbField('token');
|
2016-06-05 03:31:07 +02:00
|
|
|
|
2016-12-01 02:39:16 +01:00
|
|
|
$url = 'https://api.bludit.com/ping?token='.$token.'&url='.DOMAIN_BASE;
|
2016-09-24 18:24:56 +02:00
|
|
|
|
|
|
|
// Check if curl is installed
|
|
|
|
if( function_exists('curl_version') ) {
|
|
|
|
|
2016-09-25 20:20:03 +02:00
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
2016-09-24 18:24:56 +02:00
|
|
|
$out = curl_exec($ch);
|
|
|
|
|
2016-09-25 20:20:03 +02:00
|
|
|
if($out === false) {
|
|
|
|
Log::set('Plugin API : '.'Curl error: '.curl_error($ch));
|
2016-09-24 18:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
curl_close($ch);
|
|
|
|
}
|
2016-09-25 20:20:03 +02:00
|
|
|
else {
|
|
|
|
$options = array(
|
|
|
|
"ssl"=>array(
|
|
|
|
"verify_peer"=>false,
|
|
|
|
"verify_peer_name"=>false
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$stream = stream_context_create($options);
|
|
|
|
$out = file_get_contents($url, false, $stream);
|
|
|
|
}
|
2016-05-30 05:36:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPost($key)
|
2016-05-29 19:21:11 +02:00
|
|
|
{
|
|
|
|
// Generate the object Post
|
|
|
|
$Post = buildPost($key);
|
|
|
|
|
|
|
|
if(!$Post) {
|
2016-12-01 19:09:29 +01:00
|
|
|
return array(
|
|
|
|
'status'=>'1',
|
|
|
|
'message'=>'Post not found.'
|
|
|
|
);
|
2016-05-29 19:21:11 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:09:29 +01:00
|
|
|
$data['status'] = '0';
|
|
|
|
$data['message'] = '';
|
2016-12-02 00:59:58 +01:00
|
|
|
$data['data'] = $Post->json( $returnsArray=true );
|
2016-12-01 19:09:29 +01:00
|
|
|
|
2016-12-02 00:59:58 +01:00
|
|
|
return $data;
|
2016-05-29 19:21:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 03:31:07 +02:00
|
|
|
private function getAllPosts()
|
|
|
|
{
|
|
|
|
$posts = buildPostsForPage(0, $this->getDbField('showAllAmount'), true, false);
|
|
|
|
|
2016-12-01 19:09:29 +01:00
|
|
|
$tmp = array(
|
|
|
|
'status'=>'0',
|
2016-12-02 00:59:58 +01:00
|
|
|
'message'=>'',
|
|
|
|
'data'=>array()
|
2016-12-01 19:09:29 +01:00
|
|
|
);
|
2016-06-05 03:31:07 +02:00
|
|
|
|
|
|
|
foreach($posts as $Post) {
|
2016-12-02 00:59:58 +01:00
|
|
|
array_push($tmp['data'], $Post->json( $returnsArray=true ));
|
2016-06-05 03:31:07 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:09:29 +01:00
|
|
|
return $tmp;
|
2016-06-05 03:31:07 +02:00
|
|
|
}
|
|
|
|
|
2016-05-30 05:36:13 +02:00
|
|
|
private function getPage($key)
|
2016-05-29 19:21:11 +02:00
|
|
|
{
|
|
|
|
// Generate the object Page
|
|
|
|
$Page = buildPage($key);
|
|
|
|
|
|
|
|
if(!$Page) {
|
2016-12-01 19:09:29 +01:00
|
|
|
return array(
|
|
|
|
'status'=>'1',
|
|
|
|
'message'=>'Page not found.'
|
|
|
|
);
|
2016-05-29 19:21:11 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 19:09:29 +01:00
|
|
|
$data['status'] = '0';
|
|
|
|
$data['message'] = '';
|
2016-12-02 00:59:58 +01:00
|
|
|
$data['data'] = $Page->json( $returnsArray=true );
|
2016-12-01 19:09:29 +01:00
|
|
|
|
|
|
|
return $data;
|
2016-05-29 19:21:11 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 04:24:15 +02:00
|
|
|
private function getAllPages()
|
|
|
|
{
|
|
|
|
$pages = buildAllPages();
|
|
|
|
|
2016-12-01 19:09:29 +01:00
|
|
|
$tmp = array(
|
|
|
|
'status'=>'0',
|
2016-12-02 00:59:58 +01:00
|
|
|
'message'=>'',
|
|
|
|
'data'=>array()
|
2016-12-01 19:09:29 +01:00
|
|
|
);
|
2016-06-06 04:24:15 +02:00
|
|
|
|
|
|
|
foreach($pages as $Page) {
|
|
|
|
if($Page->published()) {
|
2016-12-02 00:59:58 +01:00
|
|
|
array_push($tmp['data'], $Page->json( $returnsArray=true ));
|
2016-06-06 04:24:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 19:09:29 +01:00
|
|
|
return $tmp;
|
2016-06-06 04:24:15 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 00:59:58 +01:00
|
|
|
}
|