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>
';
foreach($array as $key=>$value) {
foreach ($array as $key=>$value) {
if($value===false) { $value = 'false'; }
elseif($value===true) { $value = 'true'; }
echo '<tr>';
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>';
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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