Skip to content

Commit

Permalink
Merge pull request #2 from dignajar/bluditv2
Browse files Browse the repository at this point in the history
Bluditv2
  • Loading branch information
clickwork-git committed Jul 10, 2017
2 parents dfa786e + e858f15 commit 5af87f1
Show file tree
Hide file tree
Showing 375 changed files with 43,634 additions and 2,323 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
bl-content/*
bl-plugins/timemachine
bl-plugins/timemachine
bl-plugins/remote-content
bl-kernel/bludit.pro.php
24 changes: 16 additions & 8 deletions bl-kernel/abstract/dblist.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function __construct($file)
parent::__construct($file);
}

private function getList($key, $amountOfItems, $pageNumber)
public function getList($key, $pageNumber, $amountOfItems)
{
if( !isset($this->db[$key]) ) {
Log::set(__METHOD__.LOG_SEP.'Error key does not exist '.$key);
Expand All @@ -33,8 +33,11 @@ private function getList($key, $amountOfItems, $pageNumber)

$list = $this->db[$key]['list'];

// The first page number is 1, so the real is 0
$realPageNumber = $pageNumber - 1;

$total = count($list);
$init = (int) $amountOfItems * $pageNumber;
$init = (int) $amountOfItems * $realPageNumber;
$end = (int) min( ($init + $amountOfItems - 1), $total );
$outrange = $init<0 ? true : $init>$end;

Expand Down Expand Up @@ -62,6 +65,8 @@ public function add($name)

$this->db[$key]['name'] = $name;
$this->db[$key]['list'] = array();

$this->sortAlphanumeric();
$this->save();

return $key;
Expand Down Expand Up @@ -90,10 +95,18 @@ public function edit($oldKey, $newName)
unset( $this->db[$oldKey] );
}

$this->sortAlphanumeric();
$this->save();
return $newKey;
}

// Sort the categories by "Natural order"
private function sortAlphanumeric()
{
// Sort key alphanumeric strings, a01, a10, b10, c02
return ksort($this->db);
}

// Returns the name associated to the key, FALSE if the key doesn't exist
public function getName($key)
{
Expand All @@ -105,18 +118,13 @@ public function getName($key)
}

// Returns an array with key=>name of the list
public function getKeyNameArray($sortAlphanumeric=true)
public function getKeyNameArray()
{
$tmp = array();
foreach($this->db as $key=>$fields) {
$tmp[$key] = $fields['name'];
}

// Sort alphanumeric strings, a01, a10, a11, a20
if($sortAlphanumeric) {
natcasesort($tmp);
}

return $tmp;
}

Expand Down
186 changes: 132 additions & 54 deletions bl-kernel/abstract/plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@

class Plugin {

// (string) Plugin's directory name
// (string) directory name, just the name
// Ex: sitemap
public $directoryName;

// (string) Database path and filename
// (string) Absoulute database filename and path
// Ex: /www/bludit/bl-content/plugins/sitemap/db.php
public $filenameDb;

// (string) Absoulute metadata filename and path
// Ex: /www/bludit/bl-plugins/sitemap/metadata.json
public $filenameMetadata;

// (array) Plugin metadata
// Ex: array('author'=>'',...., 'notes'=>'')
public $metadata;

// (string) Class name
// Ex: pluginSitemap
public $className;

// (array) Database unserialized
public $db;

// (array) Database fields, only for initialize.
// (array) Database fields, only for initialize
public $dbFields;

// (string) Plugin's class name.
public $className;

// (array) Plugin's information.
public $metadata;
// (boolean) Enable or disable default Save and Cancel button on plugin settings
public $formButtons;

function __construct()
{
Expand All @@ -34,10 +43,12 @@ function __construct()
// Class Name
$this->className = $reflector->getName();

// Initialize dbFields from the children.
$this->formButtons = true;

// Call the method init() from the children
$this->init();

// Init empty database
// Init empty database with default values
$this->db = $this->dbFields;

$this->filenameDb = PATH_PLUGINS_DATABASES.$this->directoryName.DS.'db.php';
Expand All @@ -47,14 +58,37 @@ function __construct()
$metadataString = file_get_contents($this->filenameMetadata);
$this->metadata = json_decode($metadataString, true);

// If the plugin is installed then get the database.
if($this->installed())
{
// If the plugin is installed then get the database
if($this->installed()) {
$Tmp = new dbJSON($this->filenameDb);
$this->db = $Tmp->db;
}
}

// DEPRECATED
// 2017-06-19
public function setDb($args)
{
foreach($this->dbFields as $key=>$value) {
if( isset($args[$key]) ) {
$value = Sanitize::html( $args[$key] );
if($value==='false') { $value = false; }
elseif($value==='true') { $value = true; }
settype($value, gettype($this->dbFields[$key]));
$this->db[$key] = $value;
}
}

$this->save();
}

public function save()
{
$tmp = new dbJSON($this->filenameDb);
$tmp->db = $this->db;
$tmp->save();
}

public function htmlPath()
{
return HTML_PATH_PLUGINS.$this->directoryName.'/';
Expand All @@ -70,22 +104,41 @@ public function phpPathDB()
return PATH_PLUGINS_DATABASES.$this->directoryName.DS;
}

// Returns the item from plugin-data.
// Returns the value of the key from the metadata of the plugin, FALSE if the key doen't exit
public function getMetadata($key)
{
if(isset($this->metadata[$key])) {
return $this->metadata[$key];
}

return '';
return false;
}

// Set a key / value on the metadata of the plugin
public function setMetadata($key, $value)
{
$this->metadata[$key] = $value;
return true;
}

// Returns the value of the field from the database
// (string) $field
// (boolean) $html, TRUE returns the value sanitized, FALSE unsanitized
public function getValue($field, $html=true)
{
if( isset($this->db[$field]) ) {
if($html) {
return $this->db[$field];
}
else {
return Sanitize::htmlDecode($this->db[$field]);
}
}
return false;
}

// DEPRECATED
// 2017-06-16
public function getDbField($key, $html=true)
{
if(isset($this->db[$key])) {
Expand All @@ -103,33 +156,6 @@ public function getDbField($key, $html=true)
return '';
}

public function setDb($args)
{
$tmp = $this->db;

foreach($this->dbFields as $key=>$value)
{
if(isset($args[$key]))
{
// Sanitize value
$tmpValue = Sanitize::html( $args[$key] );

// Set type
settype($tmpValue, gettype($value));

// Set value
$tmp[$key] = $tmpValue;
}
}

$this->db = $tmp;

// Save db on file
$Tmp = new dbJSON($this->filenameDb);
$Tmp->db = $tmp;
$Tmp->save();
}

public function name()
{
return $this->getMetadata('name');
Expand Down Expand Up @@ -170,11 +196,22 @@ public function className()
return $this->className;
}

public function isCompatible()
public function formButtons()
{
$explode = explode(',', $this->getMetadata('compatible'));
return $this->formButtons;
}

return in_array(BLUDIT_VERSION, $explode);
public function isCompatible()
{
$bluditRoot = explode('.', BLUDIT_VERSION);
$compatible = explode(',', $this->getMetadata('compatible'));
foreach( $compatible as $version ) {
$root = explode('.', $version);
if( $root[0]==$bluditRoot[0] && $root[1]==$bluditRoot[1] ) {
return true;
}
}
return false;
}

public function directoryName()
Expand All @@ -201,25 +238,66 @@ public function install($position=0)

public function uninstall()
{
// Delete all files.
$files = Filesystem::listFiles( $this->phpPathDB() );
foreach($files as $file) {
unlink($file);
}

// Delete the directory.
rmdir(PATH_PLUGINS_DATABASES.$this->directoryName);
$path = PATH_PLUGINS_DATABASES.$this->directoryName;
return Filesystem::deleteRecursive($path);
}

public function installed()
{
return file_exists($this->filenameDb);
}

public function workspace()
{
return PATH_PLUGINS_DATABASES.$this->directoryName.DS;
}

public function init()
{
// This method is used on childre classes.
// The user can define your own dbFields.
// The user can define his own field of the database
}

public function post()
{
$args = $_POST;
foreach($this->dbFields as $key=>$value) {
if( isset($args[$key]) ) {
$value = Sanitize::html( $args[$key] );
if($value==='false') { $value = false; }
elseif($value==='true') { $value = true; }
settype($value, gettype($this->dbFields[$key]));
$this->db[$key] = $value;
}
}

return $this->save();
}

// Returns the parameters after the URI, FALSE if the URI doesn't match with the webhook
// Example: https://www.mybludit.com/api/foo/bar
public function webhook($URI=false, $returnsAfterURI=false)
{
global $Url;

if(empty($URI)) {
return false;
}

// Check URI start with the webhook
$startString = HTML_PATH_ROOT.$URI;
$URI = $Url->uri();
$length = mb_strlen($startString, CHARSET);
if( mb_substr($URI, 0, $length)!=$startString ) {
return false;
}

if($returnsAfterURI) {
return mb_substr($URI, $length);
}

Log::set(__METHOD__.LOG_SEP.'Webhook requested.');
return true;
}

}

0 comments on commit 5af87f1

Please sign in to comment.