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
$authKey = md5 ( $Security -> key1 () . time () . DOMAIN_BASE );
2016-05-30 05:36:13 +02:00
$this -> dbFields = array (
2016-06-05 03:31:07 +02:00
'ping' => 0 , // 0 = false, 1 = true
'authKey' => $authKey , // Private key
'showAllAmount' => 15 // Amount of posts and pages for return
2016-05-30 05:36:13 +02:00
);
}
public function form ()
{
2016-06-03 03:37:52 +02:00
$html = '' ;
$html .= '<div>' ;
2016-06-05 03:31:07 +02:00
$html .= '<input type="hidden" name="ping" value="0">' ;
$html .= '<input name="ping" id="jsping" type="checkbox" value="1" ' . ( $this -> getDbField ( 'ping' ) ? 'checked' : '' ) . '>' ;
$html .= '<label class="forCheckbox" for="jsping">Ping Bludit.com</label>' ;
$html .= '<div class="tip">Enable this feature to share your posts and pages with Bludit.com.</div>' ;
2016-06-03 03:37:52 +02:00
$html .= '</div>' ;
$html .= '<div>' ;
2016-06-05 03:31:07 +02:00
$html .= '<p><b>Authorization Key:</b> ' . $this -> getDbField ( 'authKey' ) . '</p>' ;
$html .= '<div class="tip">This key is private, do not share it with anyone.</div>' ;
$html .= '</div>' ;
$html .= '<div>' ;
$html .= '<p><b>Show all posts:</b> <a href="' . DOMAIN_BASE . 'api/show/all/posts/' . $this -> getDbField ( 'authKey' ) . '">' . DOMAIN_BASE . 'api/show/all/posts/' . $this -> getDbField ( 'authKey' ) . '</a></p>' ;
$html .= '<div class="tip">Get all posts from this site.</div>' ;
2016-05-30 05:36:13 +02:00
$html .= '</div>' ;
2016-06-05 03:31:07 +02:00
$html .= '<div>' ;
$html .= '<p><b>Show all pages:</b> <a href="' . DOMAIN_BASE . 'api/show/all/pages/' . $this -> getDbField ( 'authKey' ) . '">' . DOMAIN_BASE . 'api/show/all/pages/' . $this -> getDbField ( 'authKey' ) . '</a></p>' ;
$html .= '<div class="tip">Get all pages from this site.</div>' ;
$html .= '</div>' ;
$html .= '<div>' ;
$html .= '<p><b>Show post:</b> <a href="' . DOMAIN_BASE . 'api/show/post/{POST-NAME}">' . DOMAIN_BASE . 'api/show/post/{POST-NAME}</a></p>' ;
$html .= '<div class="tip">Get a particular post, change the {POST-NAME} with the post friendly url.</div>' ;
$html .= '</div>' ;
$html .= '<div>' ;
2016-06-28 13:03:49 +02:00
$html .= '<p><b>Show page:</b> <a href="' . DOMAIN_BASE . 'api/show/page/{PAGE-NAME}">' . DOMAIN_BASE . 'api/show/page/{PAGE-NAME}</a></p>' ;
2016-06-05 03:31:07 +02:00
$html .= '<div class="tip">Get a particular page, change the {PAGE-NAME} with the page friendly url.</div>' ;
$html .= '</div>' ;
2016-06-03 03:37:52 +02:00
2016-05-30 05:36:13 +02:00
return $html ;
}
public function afterFormSave ()
{
$this -> ping ();
}
private function ping ()
{
if ( $this -> getDbField ( 'ping' )) {
2016-06-05 03:31:07 +02:00
// Get the authentication key
$authKey = $this -> getDbField ( 'authKey' );
2016-05-30 05:36:13 +02:00
// Just a request HTTP with the website URL
2016-06-05 03:31:07 +02:00
Log :: set ( file_get_contents ( 'https://www.bludit.com/api.php?authKey=' . $authKey ) );
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 ) {
return json_encode ( array (
'status' => '0' ,
'bludit' => 'Bludit API plugin' ,
'message' => 'The post doesn\'t exist'
));
}
return $Post -> json ();
}
2016-06-05 03:31:07 +02:00
private function getAllPosts ()
{
$posts = buildPostsForPage ( 0 , $this -> getDbField ( 'showAllAmount' ), true , false );
$tmp = array ();
foreach ( $posts as $Post ) {
array_push ( $tmp , $Post -> json ( $returnsArray = true ));
}
return json_encode ( $tmp );
}
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 ) {
return json_encode ( array (
'status' => '0' ,
'bludit' => 'Bludit API plugin' ,
2016-05-30 02:50:48 +02:00
'message' => 'The page doesn\'t exist'
2016-05-29 19:21:11 +02:00
));
}
return $Page -> json ();
}
2016-06-06 04:24:15 +02:00
private function getAllPages ()
{
$pages = buildAllPages ();
$tmp = array ();
foreach ( $pages as $Page ) {
if ( $Page -> published ()) {
array_push ( $tmp , $Page -> json ( $returnsArray = true ));
}
}
return json_encode ( $tmp );
}
2016-05-29 19:21:11 +02:00
public function beforeRulesLoad ()
{
global $Url ;
// 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 = ltrim ( $URI , HTML_PATH_ROOT . 'api/' );
// Parameters
// ------------------------------------------------------------
// show post {post slug}
// show page {page slug}
2016-06-05 03:31:07 +02:00
// show all posts {AUTH KEY}
// show all pages {AUTH KEY}
2016-05-29 19:21:11 +02:00
// Get parameters
$parameters = explode ( '/' , $URI );
2016-07-17 01:19:10 +02:00
for ( $i = 0 ; $i < 3 ; $i ++ ) {
2016-05-29 19:21:11 +02:00
if ( empty ( $parameters [ $i ])) {
return false ;
2016-06-05 03:31:07 +02:00
} else {
// Sanizite
$parameters [ $i ] = Sanitize :: html ( $parameters [ $i ]);
2016-05-29 19:21:11 +02:00
}
}
// Default JSON
$json = json_encode ( array (
'status' => '0' ,
'bludit' => 'Bludit API plugin' ,
'message' => 'Check the parameters'
));
2016-07-17 01:19:10 +02:00
2016-06-05 03:31:07 +02:00
if ( $parameters [ 0 ] === 'show' ) {
if ( $parameters [ 1 ] === 'all' ) {
2016-05-29 19:21:11 +02:00
2016-06-05 03:31:07 +02:00
// Authentication key from the URI
$authKey = $parameters [ 3 ];
2016-05-29 19:21:11 +02:00
2016-06-05 03:31:07 +02:00
// Compare keys
if ( $authKey === $this -> getDbField ( 'authKey' ) ) {
if ( $parameters [ 2 ] === 'posts' ) {
$json = $this -> getAllPosts ();
}
2016-06-06 04:24:15 +02:00
elseif ( $parameters [ 2 ] === 'pages' ) {
$json = $this -> getAllPages ();
2016-06-05 03:31:07 +02:00
}
}
2016-05-29 19:21:11 +02:00
}
2016-06-05 03:31:07 +02:00
elseif ( $parameters [ 1 ] === 'post' || $parameters [ 1 ] === 'page' ) {
$key = $parameters [ 2 ];
if ( $parameters [ 1 ] === 'post' ) {
$json = $this -> getPost ( $key );
}
elseif ( $parameters [ 1 ] === 'page' ) {
$json = $this -> getPage ( $key );
}
2016-05-29 19:21:11 +02:00
}
}
// Print the JSON
header ( 'Content-Type: application/json' );
exit ( $json );
}
2016-06-28 13:03:49 +02:00
}