Fixed, sticky and system pages

This commit is contained in:
Diego Najar 2017-06-22 23:50:12 +02:00
parent 1f5dd3ac99
commit c2cf10c39b
6 changed files with 91 additions and 187 deletions

View File

@ -8,48 +8,6 @@
// Functions // Functions
// ============================================================================ // ============================================================================
function addPage($args)
{
global $dbPages;
global $Language;
global $Syslog;
// Add the page, if the $key is FALSE the creation of the page failure
$key = $dbPages->add($args);
if($key) {
// Re-index categories
reindexCategories();
// Re-index tags
reindextags();
// Call the plugins after page created
Theme::plugins('afterPageCreate');
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'new-page-created',
'notes'=>$args['title']
));
// Create an alert
Alert::set( $Language->g('Page added successfully') );
// Redirect
Redirect::page('pages');
return true;
}
else {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the page');
Log::set(__METHOD__.LOG_SEP.'Cleaning database...');
$dbPages->delete($key);
}
return false;
}
// ============================================================================ // ============================================================================
// Main before POST // Main before POST
// ============================================================================ // ============================================================================
@ -60,7 +18,11 @@ function addPage($args)
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) if( $_SERVER['REQUEST_METHOD'] == 'POST' )
{ {
addPage($_POST); if( createNewPage($_POST)!==false ) {
Alert::set( $Language->g('Page added successfully') );
}
Redirect::page('pages');
} }
// ============================================================================ // ============================================================================

View File

@ -42,8 +42,8 @@ if( $Url->whereAmI()==='page' ) {
if($page===false) { if($page===false) {
$Url->setNotFound(true); $Url->setNotFound(true);
} }
// The page is not published // The page is not published, still scheduled or draft
elseif( !$page->published() ) { elseif( $page->scheduled() || $page->draft() ) {
$Url->setNotFound(true); $Url->setNotFound(true);
} }
else { else {

View File

@ -179,3 +179,34 @@ function printDebug($array) {
var_dump($array); var_dump($array);
echo '</pre>'; echo '</pre>';
} }
function createNewPage($args) {
global $dbPages;
global $Syslog;
$key = $dbPages->add($args);
if($key) {
// Call the plugins after page created
Theme::plugins('afterPageCreate');
// Re-index categories
reindexCategories();
// Re-index tags
reindextags();
// Add to syslog
$Syslog->add(array(
'dictionaryKey'=>'new-page-created',
'notes'=>$args['title']
));
return $key;
}
Log::set('Function createNewPage()'.LOG_SEP.'Error occurred when trying to create the page');
Log::set('Function createNewPage()'.LOG_SEP.'Cleaning database...');
$dbPages->delete($key);
return false;
}

View File

@ -335,29 +335,31 @@ class Page {
return $this->getValue('key'); return $this->getValue('key');
} }
// Returns TRUE if the post/page is published, FALSE otherwise. // (boolean) Returns TRUE if the page is published, FALSE otherwise
public function published() public function published()
{ {
return ($this->getValue('status')==='published'); return ($this->getValue('status')==='published');
} }
// Returns TRUE if the post/page is scheduled, FALSE otherwise. // (boolean) Returns TRUE if the page is scheduled, FALSE otherwise
public function scheduled() public function scheduled()
{ {
return ($this->getValue('status')==='scheduled'); return ($this->getValue('status')==='scheduled');
} }
// Returns TRUE if the post/page is draft, FALSE otherwise. // (boolean) Returns TRUE if the page is draft, FALSE otherwise
public function draft() public function draft()
{ {
return ($this->getValue('status')=='draft'); return ($this->getValue('status')=='draft');
} }
// (boolean) Returns TRUE if the page is sticky, FALSE otherwise
public function sticky() public function sticky()
{ {
return ($this->getValue('status')=='sticky'); return ($this->getValue('status')=='sticky');
} }
// (boolean) Returns TRUE if the page is fixed, FALSE otherwise
public function fixed() public function fixed()
{ {
return ($this->getValue('status')=='fixed'); return ($this->getValue('status')=='fixed');

View File

@ -8,40 +8,23 @@ class pluginAPI extends Plugin {
$token = md5( uniqid().time().DOMAIN ); $token = md5( uniqid().time().DOMAIN );
$this->dbFields = array( $this->dbFields = array(
'ping'=>0, // 0 = false, 1 = true
'token'=>$token, // API Token 'token'=>$token, // API Token
'showAllAmount'=>15, // Amount of posts and pages for return 'amountOfItems'=>15 // Amount of items to return
'authentication'=>1 // Authentication required
); );
} }
public function form() public function form()
{ {
$html = ''; $html = '<div>';
$html .= '<label>'.$Language->get('API Token').'</label>';
$html .= '<div>'; $html .= '<input type="text" value="'.$this->getValue('token').'" disabled>';
$html .= '<p><b>Authorization Key:</b> '.$this->getDbField('token').'</p>'; $html .= '<span class="tip">'.$Language->get('This token is for read only and is regenerated every time you install the plugin').'</span>';
$html .= '<div class="tip">This key is private, do not share it with anyone.</div>';
$html .= '</div>'; $html .= '</div>';
$html .= '<div>'; $html .= '<div>';
$html .= '<p><b>Show all posts:</b> <a href="'.DOMAIN_BASE.'api/show/all/posts/'.$this->getDbField('token').'">'.DOMAIN_BASE.'api/show/all/posts/'.$this->getDbField('token').'</a></p>'; $html .= '<label>'.$Language->get('Amount of pages').'</label>';
$html .= '<div class="tip">Get all posts from this site.</div>'; $html .= '<input id="jsamountOfItems" name="amountOfItems" type="text" value="'.$this->getValue('amountOfItems').'">';
$html .= '</div>'; $html .= '<span class="tip">'.$Language->get('The amount of pages to return when you call to /api/pages').'</span>';
$html .= '<div>';
$html .= '<p><b>Show all pages:</b> <a href="'.DOMAIN_BASE.'api/show/all/pages/'.$this->getDbField('token').'">'.DOMAIN_BASE.'api/show/all/pages/'.$this->getDbField('token').'</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>';
$html .= '<p><b>Show page:</b> <a href="'.DOMAIN_BASE.'api/show/page/{PAGE-NAME}">'.DOMAIN_BASE.'api/show/page/{PAGE-NAME}</a></p>';
$html .= '<div class="tip">Get a particular page, change the {PAGE-NAME} with the page friendly url.</div>';
$html .= '</div>'; $html .= '</div>';
return $html; return $html;
@ -110,43 +93,29 @@ class pluginAPI extends Plugin {
} }
} }
// REQUESTS
// ------------------------------------------------------------
// /api/posts // (GET) /api/pages
if( ($method==='GET') && ($parameters[0]==='posts') && empty($parameters[1]) ) { if( ($method==='GET') && ($parameters[0]==='pages') && empty($parameters[1]) ) {
$data = $this->getAllPosts(); $data = $this->getPages();
$this->response($data);
} }
// /api/pages // (GET) /api/pages/<key>
elseif( ($method==='GET') && ($parameters[0]==='pages') && empty($parameters[1]) ) {
$data = $this->getAllPages();
$this->response($data);
}
// /api/posts/{key}
elseif( ($method==='GET') && ($parameters[0]==='posts') && !empty($parameters[1]) ) {
$data = $this->getPost($parameters[1]);
$this->response($data);
}
// /api/pages/{key}
elseif( ($method==='GET') && ($parameters[0]==='pages') && !empty($parameters[1]) ) { elseif( ($method==='GET') && ($parameters[0]==='pages') && !empty($parameters[1]) ) {
$data = $this->getPage($parameters[1]); $data = $this->getPage($parameters[1]);
$this->response($data);
} }
// /api/cli/regenerate // (POST) /api/pages
elseif( ($method==='POST') && ($parameters[0]==='cli') && ($parameters[1]==='regenerate') ) { elseif( ($method==='POST') && ($parameters[0]==='pages') && empty($parameters[1]) && $writePermissions ) {
$data = $this->newPage($inputs);
// Regenerate posts
if( $dbPosts->cliMode() ) {
reIndexTagsPosts();
}
// Regenerate pages
$dbPages->cliMode();
$this->response(array(
'status'=>'0',
'message'=>'Pages and post regenerated.'
));
} }
else {
$data = array(
'status'=>'1',
'message'=>'Error: URI not found or Access denied.'
);
}
$this->response($data);
} }
// PRIVATE METHODS // PRIVATE METHODS
@ -215,80 +184,6 @@ class pluginAPI extends Plugin {
exit($json); exit($json);
} }
private function ping()
{
if($this->getDbField('ping')) {
// Get the authentication key
$token = $this->getDbField('token');
$url = 'https://api.bludit.com/ping?token='.$token.'&url='.DOMAIN_BASE;
// Check if curl is installed
if( function_exists('curl_version') ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($ch);
if($out === false) {
Log::set('Plugin API : '.'Curl error: '.curl_error($ch));
}
curl_close($ch);
}
else {
$options = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false
)
);
$stream = stream_context_create($options);
$out = file_get_contents($url, false, $stream);
}
}
}
private function getPost($key)
{
// Generate the object Post
$Post = buildPost($key);
if(!$Post) {
return array(
'status'=>'1',
'message'=>'Post not found.'
);
}
$data['status'] = '0';
$data['message'] = '';
$data['data'] = $Post->json( $returnsArray=true );
return $data;
}
private function getAllPosts()
{
$posts = buildPostsForPage(0, $this->getDbField('showAllAmount'), true, false);
$tmp = array(
'status'=>'0',
'message'=>'',
'data'=>array()
);
foreach($posts as $Post) {
array_push($tmp['data'], $Post->json( $returnsArray=true ));
}
return $tmp;
}
private function getPage($key) private function getPage($key)
{ {
// Generate the object Page // Generate the object Page
@ -301,30 +196,44 @@ class pluginAPI extends Plugin {
); );
} }
$data = array();
$data['status'] = '0'; $data['status'] = '0';
$data['message'] = ''; $data['message'] = 'Page filtered by key: '.$key;
$data['data'] = $Page->json( $returnsArray=true ); $data['data'] = $Page->json( $returnsArray=true );
return $data; return $data;
} }
private function getAllPages() private function getPages()
{ {
$pages = buildAllPages(); global $dbPages;
$onlyPublished = true;
$amountOfItems = $this->getValue('amountOfItems');
$pageNumber = 1;
$list = $dbPages->getList($pageNumber, $amountOfItems, $onlyPublished);
$tmp = array( $tmp = array(
'status'=>'0', 'status'=>'0',
'message'=>'', 'message'=>'List of pages, amount of items: '.$amountOfItems,
'data'=>array() 'data'=>array()
); );
foreach($pages as $Page) { // Get keys of pages
if($Page->published()) { $keys = array_keys($list);
array_push($tmp['data'], $Page->json( $returnsArray=true )); foreach($keys as $pageKey) {
} // Create the page object from the page key
$page = buildPage($pageKey);
array_push($tmp['data'], $Page->json( $returnsArray=true ));
} }
return $tmp; return $tmp;
} }
private function newPage($args)
{
// This function is defined on functions.php
return createNewPage($args);
}
} }

View File

@ -307,7 +307,7 @@ function install($adminPassword, $email, $timezone)
'description'=>$Language->get('Page not found'), 'description'=>$Language->get('Page not found'),
'username'=>'admin', 'username'=>'admin',
'tags'=>array(), 'tags'=>array(),
'status'=>'published', 'status'=>'system',
'date'=>$currentDate, 'date'=>$currentDate,
'dateModified'=>'', 'dateModified'=>'',
'allowComments'=>false, 'allowComments'=>false,