API improves, fixed url router

This commit is contained in:
Diego Najar 2017-09-22 23:11:08 +02:00
parent c167e00c19
commit 1bd75ac2ee
7 changed files with 97 additions and 65 deletions

View File

@ -27,12 +27,16 @@ function printTable($title, $array) {
<tbody> <tbody>
'; ';
foreach($array as $key=>$value) { foreach ($array as $key=>$value) {
if($value===false) { $value = 'false'; } if($value===false) { $value = 'false'; }
elseif($value===true) { $value = 'true'; } elseif($value===true) { $value = 'true'; }
echo '<tr>'; echo '<tr>';
echo '<td>'.$key.'</td>'; echo '<td>'.$key.'</td>';
echo '<td>'.Sanitize::html($value).'</td>'; if (is_array($value)) {
echo '<td>'.json_encode($value).'</td>';
} else {
echo '<td>'.Sanitize::html($value).'</td>';
}
echo '</tr>'; echo '</tr>';
} }

View File

@ -4,6 +4,9 @@ HTML::title(array('title'=>$L->g('Developers'), 'icon'=>'support'));
echo '<h2>PHP version: '.phpversion().'</h2>'; echo '<h2>PHP version: '.phpversion().'</h2>';
// Loaded extensions
printTable('Server information', $_SERVER);
// Constanst defined by Bludit // Constanst defined by Bludit
$constants = get_defined_constants(true); $constants = get_defined_constants(true);
printTable('Constants', $constants['user']); printTable('Constants', $constants['user']);

View File

@ -6,7 +6,7 @@ Paginator::set('currentPage', $currentPage);
if($Url->whereAmI()=='admin') { if($Url->whereAmI()=='admin') {
$itemsPerPage = ITEMS_PER_PAGE_ADMIN; $itemsPerPage = ITEMS_PER_PAGE_ADMIN;
$amountOfItems = $dbPages->count(false); $amountOfItems = $dbPages->count(true);
} }
elseif($Url->whereAmI()=='tag') { elseif($Url->whereAmI()=='tag') {
$itemsPerPage = $Site->itemsPerPage(); $itemsPerPage = $Site->itemsPerPage();

View File

@ -18,7 +18,9 @@ class dbPages extends dbJSON
'category'=> array('inFile'=>false, 'value'=>''), 'category'=> array('inFile'=>false, 'value'=>''),
'md5file'=> array('inFile'=>false, 'value'=>''), 'md5file'=> array('inFile'=>false, 'value'=>''),
'uuid'=> array('inFile'=>false, 'value'=>''), 'uuid'=> array('inFile'=>false, 'value'=>''),
'allowComments'=> array('inFile'=>false, 'value'=>true) 'allowComments'=> array('inFile'=>false, 'value'=>true),
'parent'=> array('inFile'=>false, 'value'=>''),
'slug'=> array('inFile'=>false, 'value'=>'')
); );
function __construct() function __construct()
@ -32,8 +34,28 @@ class dbPages extends dbJSON
$dataForDb = array(); // This data will be saved in the database $dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file $dataForFile = array(); // This data will be saved in the file
foreach ($this->dbFields as $field=>$options) {
if (isset($args[$field])) {
if ($field=='tags') {
$value = $this->generateTags($args['tags']);
} else {
if( !$options['inFile'] ) {
// Sanitize if will be stored on database
$value = Sanitize::html($args[$field]);
} else {
$value = $args[$field];
}
}
} else {
// Default value for the field
$value = $options['value'];
}
$args[$field] = $value;
}
// Generate slug from content if the title is empty // Generate slug from content if the title is empty
if (empty($args['title'])) { if (empty($args['title']) || empty($args['slug'])) {
$tmpslug = Text::removeHTMLTags($args['content']); $tmpslug = Text::removeHTMLTags($args['content']);
$args['slug'] = Text::truncate($tmpslug, 60, ''); $args['slug'] = Text::truncate($tmpslug, 60, '');
} }
@ -48,37 +70,20 @@ class dbPages extends dbJSON
$currentDate = Date::current(DB_DATE_FORMAT); $currentDate = Date::current(DB_DATE_FORMAT);
// Validate date // Validate date
if( !Valid::date($args['date'], DB_DATE_FORMAT) ) { if ( !Valid::date($args['date'], DB_DATE_FORMAT) ) {
$args['date'] = $currentDate; $args['date'] = $currentDate;
} }
// Schedule page // Schedule page
if( ($args['date']>$currentDate) && ($args['status']=='published') ) { if ( ($args['date']>$currentDate) && ($args['status']=='published') ) {
$args['status'] = 'scheduled'; $args['status'] = 'scheduled';
} }
foreach($this->dbFields as $field=>$options) { foreach ($this->dbFields as $field=>$options) {
if( isset($args[$field]) ) { $value = $args[$field];
if($field=='tags') {
$value = $this->generateTags($args['tags']);
}
else {
if( !$options['inFile'] ) {
// Sanitize if will be stored on database
$value = Sanitize::html($args[$field]);
}
else {
$value = $args[$field];
}
}
}
else {
// Default value for the field
$value = $options['value'];
}
// Where the data is stored
if ($options['inFile']) { if ($options['inFile']) {
// Save on file
$dataForFile[$field] = $this->stylingFieldsForFile($field, $value); $dataForFile[$field] = $this->stylingFieldsForFile($field, $value);
} else { } else {
// Set type // Set type
@ -124,6 +129,26 @@ class dbPages extends dbJSON
$dataForDb = array(); $dataForDb = array();
$dataForFile = array(); $dataForFile = array();
foreach ($this->dbFields as $field=>$options) {
if (isset($args[$field])) {
if ($field=='tags') {
$value = $this->generateTags($args['tags']);
} else {
if( !$options['inFile'] ) {
// Sanitize if will be stored on database
$value = Sanitize::html($args[$field]);
} else {
$value = $args[$field];
}
}
} else {
// Default value for the field
$value = $options['value'];
}
$args[$field] = $value;
}
$newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']); $newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']);
// If the page is draft then the created time is the current // If the page is draft then the created time is the current
@ -136,30 +161,27 @@ class dbPages extends dbJSON
// Current UUID // Current UUID
$args['uuid'] = $this->db[$args['key']]['uuid']; $args['uuid'] = $this->db[$args['key']]['uuid'];
// Date
$currentDate = Date::current(DB_DATE_FORMAT);
// Modified date // Modified date
$args['dateModified'] = Date::current(DB_DATE_FORMAT); $args['dateModified'] = Date::current(DB_DATE_FORMAT);
foreach($this->dbFields as $field=>$options) { // Validate date
if( isset($args[$field]) ) { if ( !Valid::date($args['date'], DB_DATE_FORMAT) ) {
if($field=='tags') { $args['date'] = $currentDate;
$value = $this->generateTags($args['tags']); }
}
else { // Schedule page
if( !$options['inFile'] ) { if ( ($args['date']>$currentDate) && ($args['status']=='published') ) {
// Sanitize if will be stored on database $args['status'] = 'scheduled';
$value = Sanitize::html($args[$field]); }
}
else { foreach ($this->dbFields as $field=>$options) {
// Default value for the field $value = $args[$field];
$value = $args[$field];
}
}
}
else {
$value = $options['value'];
}
if ($options['inFile']) { if ($options['inFile']) {
// Save on file
$dataForFile[$field] = $this->stylingFieldsForFile($field, $value); $dataForFile[$field] = $this->stylingFieldsForFile($field, $value);
} else { } else {
// Set type // Set type
@ -254,8 +276,8 @@ class dbPages extends dbJSON
public function getPublishedDB() public function getPublishedDB()
{ {
$tmp = $this->db; $tmp = $this->db;
foreach($tmp as $key=>$fields) { foreach ($tmp as $key=>$fields) {
if($fields['status']!='published') { if ($fields['status']!='published') {
unset($tmp[$key]); unset($tmp[$key]);
} }
} }
@ -345,7 +367,7 @@ class dbPages extends dbJSON
// (boolean) $total, FALSE returns the total of published pages (without draft and scheduled) // (boolean) $total, FALSE returns the total of published pages (without draft and scheduled)
public function count($onlyPublished=true) public function count($onlyPublished=true)
{ {
if( $onlyPublished ) { if ($onlyPublished) {
$db = $this->getPublishedDB(); $db = $this->getPublishedDB();
return count($db); return count($db);
} }

View File

@ -304,12 +304,13 @@ function createPage($args) {
// The user is always the one loggued // The user is always the one loggued
$args['username'] = Session::get('username'); $args['username'] = Session::get('username');
if ( Text::isEmpty($args['username']) ) { if ( empty($args['username']) ) {
Log::set('Function createPage()'.LOG_SEP.'Empty username.');
return false; return false;
} }
// External Cover Image // External Cover Image
if ( Text::isNotEmpty(($args['externalCoverImage'])) ) { if ( !empty($args['externalCoverImage']) ) {
$args['coverImage'] = $args['externalCoverImage']; $args['coverImage'] = $args['externalCoverImage'];
unset($args['externalCoverImage']); unset($args['externalCoverImage']);
} }
@ -350,21 +351,17 @@ function editPage($args) {
// The user is always the one loggued // The user is always the one loggued
$args['username'] = Session::get('username'); $args['username'] = Session::get('username');
if ( Text::isEmpty($args['username']) ) { if ( empty($args['username']) ) {
Log::set('Function editPage()'.LOG_SEP.'Empty username.'); Log::set('Function editPage()'.LOG_SEP.'Empty username.');
return false; return false;
} }
// External Cover Image // External Cover Image
if ( Text::isNotEmpty(($args['externalCoverImage'])) ) { if ( !empty($args['externalCoverImage']) ) {
$args['coverImage'] = $args['externalCoverImage']; $args['coverImage'] = $args['externalCoverImage'];
unset($args['externalCoverImage']); unset($args['externalCoverImage']);
} }
if (!isset($args['parent'])) {
$args['parent'] = '';
}
$key = $dbPages->edit($args); $key = $dbPages->edit($args);
if ($key) { if ($key) {
// Call the plugins after page modified // Call the plugins after page modified

View File

@ -67,7 +67,7 @@ class Url
} }
// Check coincidence with complete filterURI // Check coincidence with complete filterURI
if ($subString==$filterURI) { if ($subString==$filterFull) {
$this->slug = mb_substr($this->uri, $filterFullLenght); $this->slug = mb_substr($this->uri, $filterFullLenght);
$this->setWhereAmI($filterName); $this->setWhereAmI($filterName);
$this->activeFilter = $filterURI; $this->activeFilter = $filterURI;

View File

@ -78,7 +78,7 @@ class pluginAPI extends Plugin {
$tokenAPI = $this->getValue('token'); $tokenAPI = $this->getValue('token');
// Check empty token // Check empty token
if ( empty($inputs['token']) ) { if (empty($inputs['token'])) {
$this->response(404, 'Not Found', array('message'=>'Missing API token.')); $this->response(404, 'Not Found', array('message'=>'Missing API token.'));
} }
@ -168,6 +168,11 @@ class pluginAPI extends Plugin {
break; break;
} }
// Try to get raw data
if (empty($inputs)) {
$inputs = file_get_contents('php://input');
}
return $this->cleanInputs($inputs); return $this->cleanInputs($inputs);
} }
@ -193,16 +198,17 @@ class pluginAPI extends Plugin {
private function cleanInputs($inputs) private function cleanInputs($inputs)
{ {
$tmp = array(); $tmp = array();
if ( is_array($inputs) ) { if (is_array($inputs)) {
foreach($inputs as $key=>$value) { foreach ($inputs as $key=>$value) {
$tmp[$key] = Sanitize::html($value); $tmp[$key] = Sanitize::html($value);
} }
} elseif ( is_string($inputs) ) { } elseif(is_string($inputs)) {
$tmp = json_decode($inputs, true); $tmp = json_decode($inputs, true);
if(json_last_error()===0) { if (json_last_error()!==JSON_ERROR_NONE) {
$tmp = array(); $tmp = array();
} }
} }
return $tmp; return $tmp;
} }
@ -264,7 +270,7 @@ class pluginAPI extends Plugin {
{ {
// This function is defined on functions.php // This function is defined on functions.php
$key = createPage($args); $key = createPage($args);
var_dump($key);exit;
if ($key===false) { if ($key===false) {
return array( return array(
'status'=>'1', 'status'=>'1',