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

339 lines
7.5 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 {
// (string) directory name, just the name
// Ex: sitemap
2015-03-08 18:02:59 +01:00
public $directoryName;
2015-05-05 03:00:01 +02:00
// (string) Absoulute database filename and path
// Ex: /www/bludit/bl-content/plugins/sitemap/db.php
2015-07-14 04:16:28 +02:00
public $filenameDb;
2015-05-05 03:00:01 +02:00
// (string) Absoulute metadata filename and path
// Ex: /www/bludit/bl-plugins/sitemap/metadata.json
2016-01-14 05:42:18 +01:00
public $filenameMetadata;
// (array) Plugin metadata
// Ex: array('author'=>'',...., 'notes'=>'')
public $metadata;
// (string) Class name
// Ex: pluginSitemap
public $className;
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
2015-03-08 18:02:59 +01:00
public $dbFields;
2017-07-07 23:38:01 +02:00
// (boolean) Enable or disable default Save and Cancel button on plugin settings
public $formButtons;
2020-05-12 18:24:06 +02:00
// (array) List of custom hooks
public $customHooks;
2015-03-08 18:02:59 +01:00
function __construct()
{
2015-07-14 06:41:32 +02:00
$this->dbFields = array();
2020-05-12 18:24:06 +02:00
$this->customHooks = array();
2015-07-14 06:41:32 +02:00
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();
$this->formButtons = true;
// Call the method init() from the children
2015-06-11 04:27:04 +02:00
$this->init();
// Init empty database with default values
2015-06-11 04:27:04 +02:00
$this->db = $this->dbFields;
2015-03-08 18:02:59 +01:00
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
2018-08-09 23:15:52 +02: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
}
}
public function save()
{
$tmp = new dbJSON($this->filenameDb);
$tmp->db = $this->db;
2017-12-20 00:25:42 +01:00
return $tmp->save();
}
2018-07-10 18:37:46 +02:00
public function includeCSS($filename)
{
return '<link rel="stylesheet" type="text/css" href="'.$this->domainPath().'css/'.$filename.'?version='.BLUDIT_VERSION.'">'.PHP_EOL;
}
public function includeJS($filename)
{
return '<script charset="utf-8" src="'.$this->domainPath().'js/'.$filename.'?version='.BLUDIT_VERSION.'"></script>'.PHP_EOL;
}
2019-02-02 16:16:13 +01:00
// Returns absolute URL and path of the plugin directory
// This function helps to include CSS or Javascript files with absolute URL
2018-07-10 18:37:46 +02:00
public function domainPath()
{
return DOMAIN_PLUGINS.$this->directoryName.'/';
}
2019-02-02 16:16:13 +01:00
// Returns relative path of the plugin directory
// This function helps to include CSS or Javascript files with relative URL
2015-08-01 18:28:47 +02:00
public function htmlPath()
{
return HTML_PATH_PLUGINS.$this->directoryName.'/';
}
2019-02-02 16:16:13 +01:00
// Returns absolute path of the plugin directory
// This function helps to include PHP libraries or some file at server level
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;
}
// Returns the value of the key from the metadata of the plugin, FALSE if the key doen't exit
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
}
return false;
2015-07-03 22:44:26 +02:00
}
// Set a key / value on the metadata of the plugin
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
}
// 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)
{
2018-08-09 23:15:52 +02:00
if (isset($this->db[$field])) {
if ($html) {
return $this->db[$field];
2018-08-09 23:15:52 +02:00
} else {
return Sanitize::htmlDecode($this->db[$field]);
}
}
2018-10-02 00:13:47 +02:00
return $this->dbFields[$field];
}
2018-07-01 14:17:24 +02:00
public function label()
{
return $this->getMetadata('label');
}
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
}
2017-07-30 23:15:33 +02:00
public function position()
{
return $this->getValue('position');
}
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
}
public function formButtons()
{
return $this->formButtons;
}
2016-06-20 02:45:09 +02:00
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;
2016-06-20 02:45:09 +02:00
}
2015-07-03 22:44:26 +02:00
public function directoryName()
{
return $this->directoryName;
}
2017-07-05 19:59:51 +02:00
// Return TRUE if the installation success, otherwise FALSE.
2017-07-30 23:15:33 +02:00
public function install($position=1)
2015-03-08 18:02:59 +01:00
{
2018-02-06 18:26:59 +01: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
2018-09-05 22:55:14 +02:00
// Create workspace
$workspace = $this->workspace();
mkdir($workspace, DIR_PERMISSIONS, true);
2018-09-05 22:55:14 +02:00
// Create plugin directory for the database
mkdir(PATH_PLUGINS_DATABASES.$this->directoryName, DIR_PERMISSIONS, true);
2015-03-08 18:02:59 +01:00
2015-08-01 18:28:47 +02:00
$this->dbFields['position'] = $position;
2018-08-09 23:15:52 +02:00
// Sanitize default values to store in the file
foreach ($this->dbFields as $key=>$value) {
$value = Sanitize::html($value);
settype($value, gettype($this->dbFields[$key]));
$this->db[$key] = $value;
}
2018-09-05 22:55:14 +02:00
// Create the database
2018-08-09 23:15:52 +02:00
return $this->save();
2015-03-08 18:02:59 +01:00
}
public function uninstall()
{
2018-09-05 22:55:14 +02:00
// Delete database
2017-07-05 23:30:30 +02:00
$path = PATH_PLUGINS_DATABASES.$this->directoryName;
2018-09-05 22:55:14 +02:00
Filesystem::deleteRecursive($path);
// Delete workspace
$workspace = $this->workspace();
Filesystem::deleteRecursive($workspace);
return true;
2015-03-08 18:02:59 +01:00
}
2019-03-10 18:27:24 +01:00
// Returns TRUE if the plugin is installed
// This function just check if the database of the plugin is created
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
}
2017-07-05 22:55:03 +02:00
public function workspace()
{
2018-09-05 22:55:14 +02:00
return PATH_WORKSPACES.$this->directoryName.DS;
2017-07-05 22:55:03 +02:00
}
2015-03-08 18:02:59 +01:00
public function init()
{
2015-06-11 04:27:04 +02:00
// This method is used on childre classes.
// The user can define his own field of the database
}
public function post()
{
$args = $_POST;
2019-03-10 18:27:24 +01:00
foreach ($this->dbFields as $field=>$value) {
if (isset($args[$field])) {
$finalValue = Sanitize::html( $args[$field] );
if ($finalValue==='false') { $finalValue = false; }
elseif ($finalValue==='true') { $finalValue = true; }
settype($finalValue, gettype($value));
$this->db[$field] = $finalValue;
}
}
return $this->save();
2015-05-05 03:00:01 +02:00
}
2015-03-08 18:02:59 +01:00
2018-02-06 18:26:59 +01:00
public function setField($field, $value)
{
$this->db[$field] = Sanitize::html($value);
return $this->save();
}
public function setPosition($position)
{
return $this->setField('position', $position);
}
2017-07-05 23:30:30 +02:00
// Returns the parameters after the URI, FALSE if the URI doesn't match with the webhook
2017-07-07 23:38:01 +02:00
// Example: https://www.mybludit.com/api/foo/bar
2017-07-29 01:20:47 +02:00
public function webhook($URI=false, $returnsAfterURI=false, $fixed=true)
2017-07-05 19:59:51 +02:00
{
global $url;
2017-07-05 19:59:51 +02:00
2017-07-29 01:20:47 +02:00
if (empty($URI)) {
2017-07-05 19:59:51 +02:00
return false;
}
// Check URI start with the webhook
$startString = HTML_PATH_ROOT.$URI;
$URI = $url->uri();
2017-07-05 19:59:51 +02:00
$length = mb_strlen($startString, CHARSET);
2017-07-29 01:20:47 +02:00
if (mb_substr($URI, 0, $length)!=$startString) {
2017-07-05 19:59:51 +02:00
return false;
}
2017-07-29 01:20:47 +02:00
$afterURI = mb_substr($URI, $length);
if (!empty($afterURI)) {
if ($fixed) {
return false;
}
if ($afterURI[0]!='/') {
return false;
}
}
if ($returnsAfterURI) {
return $afterURI;
2017-07-07 23:38:01 +02:00
}
2017-07-05 19:59:51 +02:00
Log::set(__METHOD__.LOG_SEP.'Webhook requested.');
2017-07-06 23:27:22 +02:00
return true;
2017-07-05 19:59:51 +02:00
}
}