bludit/bl-kernel/abstract/plugin.class.php

243 lines
4.6 KiB
PHP
Raw Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2015-03-08 18:02:59 +01:00
class Plugin {
2015-08-01 18:28:47 +02:00
// (string) Plugin's directory name
2015-03-08 18:02:59 +01:00
public $directoryName;
2015-05-05 03:00:01 +02:00
2015-03-08 18:02:59 +01:00
// (string) Database path and filename
2015-07-14 04:16:28 +02:00
public $filenameDb;
2015-05-05 03:00:01 +02:00
2016-01-14 05:42:18 +01:00
public $filenameMetadata;
2015-07-14 04:16:28 +02:00
// (array) Database unserialized
2015-03-08 18:02:59 +01:00
public $db;
// (array) Database fields, only for initialize.
public $dbFields;
2015-07-14 04:16:28 +02:00
// (string) Plugin's class name.
2015-03-08 18:02:59 +01:00
public $className;
2015-07-14 04:16:28 +02:00
// (array) Plugin's information.
2016-01-14 05:42:18 +01:00
public $metadata;
2016-11-07 20:07:42 +01:00
2016-11-07 20:10:37 +01:00
// (dbLanguage) Plugin's localisation.
2016-11-07 20:07:42 +01:00
public $language;
2015-07-03 22:44:26 +02:00
2016-11-07 20:07:42 +01:00
function __construct($locale)
2015-03-08 18:02:59 +01:00
{
2015-07-14 06:41:32 +02:00
$this->dbFields = array();
2015-07-14 04:16:28 +02:00
$reflector = new ReflectionClass(get_class($this));
2015-07-03 22:44:26 +02:00
2015-03-08 18:02:59 +01:00
// Directory name
2015-08-01 18:28:47 +02:00
$this->directoryName = basename(dirname($reflector->getFileName()));
2015-03-08 18:02:59 +01:00
// Class Name
$this->className = $reflector->getName();
2015-06-11 04:27:04 +02:00
// Initialize dbFields from the children.
$this->init();
2015-03-08 18:02:59 +01:00
// Init empty database
2015-06-11 04:27:04 +02:00
$this->db = $this->dbFields;
2015-03-08 18:02:59 +01:00
2016-11-07 20:07:42 +01:00
// Load localisation
$tmp = new dbLanguage($locale, PATH_PLUGINS.$this->directoryName.DS.'languages'.DS);
// Set name and description from the language file.
$this->setMetadata('name',$tmp->db['plugin-data']['name']);
$this->setMetadata('description',$tmp->db['plugin-data']['description']);
// Remove name and description
unset($tmp->db['plugin-data']);
$this->language = $tmp;
2015-08-01 18:28:47 +02:00
$this->filenameDb = PATH_PLUGINS_DATABASES.$this->directoryName.DS.'db.php';
2015-03-08 18:02:59 +01:00
2016-01-14 05:42:18 +01:00
// --- Metadata ---
$this->filenameMetadata = PATH_PLUGINS.$this->directoryName().DS.'metadata.json';
$metadataString = file_get_contents($this->filenameMetadata);
$this->metadata = json_decode($metadataString, true);
// If the plugin is installed then get the database.
2015-03-08 18:02:59 +01:00
if($this->installed())
{
2015-07-14 04:16:28 +02:00
$Tmp = new dbJSON($this->filenameDb);
2015-05-05 03:00:01 +02:00
$this->db = $Tmp->db;
2015-03-08 18:02:59 +01:00
}
}
2015-08-01 18:28:47 +02:00
public function htmlPath()
{
return HTML_PATH_PLUGINS.$this->directoryName.'/';
}
2015-12-09 02:10:46 +01:00
public function phpPath()
{
return PATH_PLUGINS.$this->directoryName.DS;
}
2016-01-11 23:51:00 +01:00
public function phpPathDB()
{
return PATH_PLUGINS_DATABASES.$this->directoryName.DS;
}
2015-07-14 04:16:28 +02:00
// Returns the item from plugin-data.
2016-01-14 05:42:18 +01:00
public function getMetadata($key)
2015-06-11 04:27:04 +02:00
{
2016-01-14 05:42:18 +01:00
if(isset($this->metadata[$key])) {
return $this->metadata[$key];
2015-06-12 06:00:04 +02:00
}
2015-08-01 18:28:47 +02:00
return '';
2015-07-03 22:44:26 +02:00
}
2016-01-14 05:42:18 +01:00
public function setMetadata($key, $value)
2015-07-03 22:44:26 +02:00
{
2016-01-14 05:42:18 +01:00
$this->metadata[$key] = $value;
return true;
2015-07-03 22:44:26 +02:00
}
2015-08-01 18:28:47 +02:00
public function getDbField($key, $html=true)
2015-07-14 04:16:28 +02:00
{
if(isset($this->db[$key])) {
2015-08-01 18:28:47 +02:00
if($html) {
// All fields from DBField are sanitized.
return $this->db[$key];
}
else {
// Decode HTML tags, this action unsanitized the variable.
return Sanitize::htmlDecode($this->db[$key]);
}
2015-07-14 04:16:28 +02:00
}
return '';
}
public function setDb($args)
2015-07-14 04:16:28 +02:00
{
$tmp = $this->db;
2015-07-14 04:16:28 +02:00
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;
}
2015-07-14 04:16:28 +02:00
}
$this->db = $tmp;
// Save db on file
$Tmp = new dbJSON($this->filenameDb);
$Tmp->db = $tmp;
$Tmp->save();
}
2015-07-03 22:44:26 +02:00
public function name()
{
2016-01-14 05:42:18 +01:00
return $this->getMetadata('name');
2015-06-12 06:00:04 +02:00
}
public function description()
{
2016-01-14 05:42:18 +01:00
return $this->getMetadata('description');
2015-07-03 22:44:26 +02:00
}
2015-06-12 06:00:04 +02:00
2015-07-03 22:44:26 +02:00
public function author()
{
2016-01-14 05:42:18 +01:00
return $this->getMetadata('author');
2015-07-03 22:44:26 +02:00
}
public function email()
{
2016-01-14 05:42:18 +01:00
return $this->getMetadata('email');
2015-07-03 22:44:26 +02:00
}
public function website()
{
2016-01-14 05:42:18 +01:00
return $this->getMetadata('website');
2015-06-12 06:00:04 +02:00
}
2015-07-26 23:57:39 +02:00
public function version()
{
2016-01-14 05:42:18 +01:00
return $this->getMetadata('version');
2015-07-26 23:57:39 +02:00
}
public function releaseDate()
{
2016-01-14 05:42:18 +01:00
return $this->getMetadata('releaseDate');
2015-07-26 23:57:39 +02:00
}
2015-06-12 06:00:04 +02:00
public function className()
{
return $this->className;
2015-06-11 04:27:04 +02:00
}
2016-06-20 02:45:09 +02:00
public function isCompatible()
{
$explode = explode(',', $this->getMetadata('compatible'));
return in_array(BLUDIT_VERSION, $explode);
}
2015-07-03 22:44:26 +02:00
public function directoryName()
{
return $this->directoryName;
}
2015-03-08 18:02:59 +01:00
// Return TRUE if the installation success, otherwise FALSE.
2015-07-28 03:01:52 +02:00
public function install($position=0)
2015-03-08 18:02:59 +01:00
{
2015-06-11 04:27:04 +02:00
if($this->installed()) {
2015-03-08 18:02:59 +01:00
return false;
2015-06-11 04:27:04 +02:00
}
2015-03-08 18:02:59 +01:00
// Create plugin directory for databases and other files
2015-06-11 04:27:04 +02:00
mkdir(PATH_PLUGINS_DATABASES.$this->directoryName, 0755, true);
2015-03-08 18:02:59 +01:00
2015-07-14 06:41:32 +02:00
// Create database
2015-08-01 18:28:47 +02:00
$this->dbFields['position'] = $position;
$this->setDb($this->dbFields);
2015-03-08 18:02:59 +01:00
return true;
}
public function uninstall()
{
2016-01-11 23:51:00 +01:00
// Delete all files.
$files = Filesystem::listFiles( $this->phpPathDB() );
foreach($files as $file) {
unlink($file);
}
// Delete the directory.
2015-06-11 04:27:04 +02:00
rmdir(PATH_PLUGINS_DATABASES.$this->directoryName);
2015-03-08 18:02:59 +01:00
}
public function installed()
{
2015-07-14 04:16:28 +02:00
return file_exists($this->filenameDb);
2015-03-08 18:02:59 +01:00
}
public function init()
{
2016-11-07 20:07:42 +01:00
// This method is used on child classes.
// The user can define their own dbFields.
2015-05-05 03:00:01 +02:00
}
2015-03-08 18:02:59 +01:00
2016-11-07 20:07:42 +01:00
public function L($key)
{
return $this->language->get($key);
}
}