2021-09-01 23:35:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @brief improve, a plugin for Dotclear 2
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-01 23:35:23 +00:00
|
|
|
* @package Dotclear
|
2021-09-06 21:36:50 +00:00
|
|
|
* @subpackage Plugin
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-01 23:35:23 +00:00
|
|
|
* @author Jean-Christian Denis and contributors
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-01 23:35:23 +00:00
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*/
|
|
|
|
/**
|
2021-09-15 21:48:58 +00:00
|
|
|
* @brief Plugin improve action class
|
|
|
|
*
|
2021-09-01 23:35:23 +00:00
|
|
|
* Action class must extends class ImproveAction.
|
2021-11-01 21:17:44 +00:00
|
|
|
* If your class signature is myActionClass extends ImproveAction,
|
2021-09-15 21:48:58 +00:00
|
|
|
* do $core->addBehavior('ImproveAddAction'), ['myClass', 'create']);
|
2021-11-01 21:17:44 +00:00
|
|
|
* yoru action class is automatically created,
|
2021-09-01 23:35:23 +00:00
|
|
|
* then function init() of your class wil be called.
|
|
|
|
* One class must manage only one action.
|
2021-09-15 21:48:58 +00:00
|
|
|
*
|
|
|
|
* @package Plugin_improve
|
|
|
|
* @subpackage Action
|
|
|
|
*
|
|
|
|
* @copyright Jean-Christian Denis
|
|
|
|
* @copyright GPL-2.0-only
|
2021-09-01 23:35:23 +00:00
|
|
|
*/
|
|
|
|
abstract class ImproveAction
|
|
|
|
{
|
|
|
|
protected $core;
|
2021-11-01 21:17:44 +00:00
|
|
|
protected $module = [];
|
|
|
|
protected $path_full = '';
|
2021-09-15 21:48:58 +00:00
|
|
|
protected $path_extension = '';
|
2021-11-01 21:17:44 +00:00
|
|
|
protected $path_is_dir = null;
|
2021-09-01 23:35:23 +00:00
|
|
|
|
2021-11-01 21:17:44 +00:00
|
|
|
private $logs = ['success' => [], 'warning' => [], 'error' => []];
|
|
|
|
private $settings = [];
|
2021-09-01 23:35:23 +00:00
|
|
|
private $properties = [
|
2021-11-01 21:17:44 +00:00
|
|
|
'id' => '',
|
|
|
|
'name' => '',
|
|
|
|
'desc' => '',
|
2021-09-01 23:35:23 +00:00
|
|
|
'priority' => 500,
|
2021-11-01 21:17:44 +00:00
|
|
|
'config' => false, //mixed bool for internal, string for ext url
|
|
|
|
'types' => ['plugin']
|
2021-09-01 23:35:23 +00:00
|
|
|
];
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* ImproveAction constructor inits properpties and settings of a child class.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $core dcCore instance
|
|
|
|
*/
|
2021-09-01 23:35:23 +00:00
|
|
|
final public function __construct(dcCore $core)
|
|
|
|
{
|
|
|
|
$this->core = $core;
|
|
|
|
|
2021-11-01 21:17:44 +00:00
|
|
|
$settings = @unserialize($core->blog->settings->improve->get('settings_' . get_called_class()));
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->settings = is_array($settings) ? $settings : [];
|
2021-09-01 23:35:23 +00:00
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
// can overload priority by settings
|
2021-11-01 21:17:44 +00:00
|
|
|
if (1 < ($p = (int) $core->blog->settings->improve->get('priority_' . get_called_class()))) {
|
2021-09-01 23:35:23 +00:00
|
|
|
$this->priority = $p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Helper to create an instance of a ImproveAction child class.
|
|
|
|
*
|
|
|
|
* @param string $o ArrayObject of actions list
|
|
|
|
* @param string $core dcCore instance
|
|
|
|
*/
|
|
|
|
public static function create(arrayObject $o, dcCore $core)
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
$c = get_called_class();
|
|
|
|
$o->append(new $c($core));
|
2021-09-01 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Action initialisation function.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
|
|
|
* It's called when an instance of ImproveAction child class is created.
|
2021-09-15 21:48:58 +00:00
|
|
|
* Usefull to setup action class.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return bool True if initialisation is ok.
|
|
|
|
*/
|
|
|
|
abstract protected function init(): bool;
|
2021-09-01 23:35:23 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/// @name Properties methods
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
* @see getProperty();
|
|
|
|
*/
|
2021-09-12 08:46:26 +00:00
|
|
|
final public function __get(string $property)
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
return $this->getProperty($property);
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Get a definition property of action class
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return mixed A property of action definition.
|
|
|
|
*/
|
2021-09-12 08:46:26 +00:00
|
|
|
final public function getProperty(string $property)
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
return $this->properties[$property] ?? null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Set a definition property of action class
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* Property can be:
|
|
|
|
* - id : action id
|
|
|
|
* - name : action short name
|
|
|
|
* - desc : action short description,
|
|
|
|
* - priority : order of execution of this action
|
|
|
|
* - config : as configuration gui, false = none, true = internal, string = ext url
|
|
|
|
* - types : array of supported type of module, can : be plugins and/or themes
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param mixed $property one or more definition
|
|
|
|
* @param dtring $value value for a single property
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return mixed A property of action definition.
|
|
|
|
*/
|
2021-09-12 08:46:26 +00:00
|
|
|
final protected function setProperties($property, $value = null): bool
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
$properties = is_array($property) ? $property : [$property => $value];
|
2021-11-01 21:17:44 +00:00
|
|
|
foreach ($properties as $k => $v) {
|
2021-09-01 23:35:23 +00:00
|
|
|
if (isset($this->properties[$k])) {
|
|
|
|
if ($k == 'types' && !is_array($v)) {
|
|
|
|
$v = [$v];
|
|
|
|
}
|
|
|
|
$this->properties[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-01 23:35:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-09-15 21:48:58 +00:00
|
|
|
//@}
|
2021-09-01 23:35:23 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/// @name Settings methods
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
* Get a settings of action class
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $setting a settings id
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return mixed A setting of action.
|
|
|
|
*/
|
|
|
|
final protected function getSetting(string $setting)
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
return $this->settings[$setting] ?? null;
|
2021-09-01 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Set one or more setting of action class
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param mixed $settings one or more settings
|
|
|
|
* @param string $value value for a single setting
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return mixed A setting of action.
|
|
|
|
*/
|
|
|
|
final protected function setSettings($settings, $value = null)
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
2021-10-29 18:08:35 +00:00
|
|
|
$settings = is_array($settings) ? $settings : [$settings => $value];
|
2021-11-01 21:17:44 +00:00
|
|
|
foreach ($settings as $k => $v) {
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->settings[$k] = $v;
|
2021-09-01 23:35:23 +00:00
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-06 21:29:56 +00:00
|
|
|
return true;
|
2021-09-01 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Redirection after settings update
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* This save settings update before redirect.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $url redirect url after settings update
|
|
|
|
*/
|
2021-09-01 23:35:23 +00:00
|
|
|
final protected function redirect(string $url)
|
|
|
|
{
|
|
|
|
$this->core->blog->settings->improve->put(
|
2021-11-01 21:17:44 +00:00
|
|
|
'settings_' . get_called_class(),
|
|
|
|
serialize($this->settings),
|
|
|
|
'string',
|
|
|
|
null,
|
|
|
|
true,
|
2021-09-01 23:35:23 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->core->blog->triggerBlog();
|
2021-09-25 21:14:51 +00:00
|
|
|
dcPage::addSuccessNotice(__('Configuration successfully updated'));
|
2021-09-01 23:35:23 +00:00
|
|
|
http::redirect($url);
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Check if action class is well configured
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return boolean True if class action is well configured
|
|
|
|
*/
|
2021-09-01 23:35:23 +00:00
|
|
|
abstract public function isConfigured(): bool;
|
|
|
|
|
2021-11-06 00:40:29 +00:00
|
|
|
/**
|
|
|
|
* Get action configuration page header
|
|
|
|
*
|
|
|
|
* @return string Headers
|
|
|
|
*/
|
|
|
|
public function header(): ?string
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Get configuraton gui
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
|
|
|
* If action class uses internal configuration,
|
2021-09-15 21:48:58 +00:00
|
|
|
* it must share here html form content of its settings.
|
|
|
|
* It must not use enclose bloc "form" nor button "save".
|
2021-11-01 21:17:44 +00:00
|
|
|
* This function is also called to redirect form
|
2021-09-15 21:48:58 +00:00
|
|
|
* after validation with $this->redirect($url);
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $url post form redirect url
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return mixed A setting of action.
|
|
|
|
*/
|
|
|
|
public function configure(string $url): ?string
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
return null;
|
2021-09-01 23:35:23 +00:00
|
|
|
}
|
2021-09-15 21:48:58 +00:00
|
|
|
//@}
|
2021-09-01 23:35:23 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Set in class var current module definitions.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @see Improve::sanitizeModule()
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param array $module Full array of module definitons
|
|
|
|
*/
|
|
|
|
final public function setModule(array $module)
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->module = $module;
|
2021-09-01 23:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Set in class var current path definitons.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $path_full Full path
|
|
|
|
* @param string $path_extension Path extension (if it is a file)
|
|
|
|
* @param string $path_is_dir True if path is a directory
|
|
|
|
*/
|
|
|
|
final public function setPath(string $path_full, string $path_extension, bool $path_is_dir)
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
2021-11-01 21:17:44 +00:00
|
|
|
$this->path_full = $path_full;
|
2021-09-15 21:48:58 +00:00
|
|
|
$this->path_extension = $path_extension;
|
2021-11-01 21:17:44 +00:00
|
|
|
$this->path_is_dir = $path_is_dir;
|
2021-09-15 21:48:58 +00:00
|
|
|
}
|
2021-09-01 23:35:23 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/// @name Fix methods
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
* Called when starting to fix module.
|
|
|
|
*/
|
|
|
|
public function openModule(): ?bool
|
|
|
|
{
|
2021-09-01 23:35:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Called when open a directory to fix.
|
|
|
|
*/
|
|
|
|
public function openDirectory(): ?bool
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Called when open a file to fix.
|
|
|
|
*/
|
|
|
|
public function openFile(): ?bool
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Called when read content of a file to fix.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* Content is shared from action to another.
|
|
|
|
* If an action erase content, fix is stopped.
|
2021-11-01 21:17:44 +00:00
|
|
|
* If you want to erase a content you must erase
|
2021-09-15 21:48:58 +00:00
|
|
|
* the file on action openDirectory.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $content File content
|
|
|
|
*/
|
|
|
|
public function readFile(string &$content): ?bool
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Called when close a file to fix.
|
|
|
|
*/
|
|
|
|
public function closeFile(): ?bool
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
/**
|
|
|
|
* Called when close a module to fix.
|
|
|
|
*/
|
|
|
|
public function closeModule(): ?bool
|
2021-09-01 23:35:23 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-15 21:48:58 +00:00
|
|
|
//@}
|
|
|
|
|
|
|
|
/// @name Logs methods
|
|
|
|
//@{
|
|
|
|
/**
|
|
|
|
* Set an action log.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* Log must be use every time an action something happen.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $type type of message, can be error, warning, succes
|
|
|
|
* @param string $message message to log
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return boolean True if message is logged.
|
|
|
|
*/
|
|
|
|
final public function setLog(string $type, string $message): bool
|
|
|
|
{
|
|
|
|
if (empty($this->path_full) || !array_key_exists($type, $this->logs)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->logs[$type][$this->path_full][] = $message;
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if action class has log of given type.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param string $type type of message, can be error, warning, succes
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return boolean True if messages exist.
|
|
|
|
*/
|
|
|
|
final public function hasLog(string $type): bool
|
|
|
|
{
|
|
|
|
return array_key_exists($type, $this->logs) && !empty($this->logs[$type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get action logs.
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @param mixed $type type of message, can be error, warning, succes
|
2021-11-01 21:17:44 +00:00
|
|
|
*
|
2021-09-15 21:48:58 +00:00
|
|
|
* @return array Arry of given type of log or all if type is null
|
|
|
|
*/
|
|
|
|
final public function getLogs($type = null): array
|
|
|
|
{
|
|
|
|
if (null === $type) {
|
|
|
|
return $this->logs;
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
if (empty($this->path_full)
|
2021-09-15 21:48:58 +00:00
|
|
|
|| !array_key_exists($type, $this->logs)
|
|
|
|
|| !array_key_exists($this->path_full, $this->logs[$type])
|
|
|
|
) {
|
|
|
|
return [];
|
|
|
|
}
|
2021-11-01 21:17:44 +00:00
|
|
|
|
2021-09-15 21:48:58 +00:00
|
|
|
return $this->logs[$type][$this->path_full];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a log of type error.
|
|
|
|
*/
|
|
|
|
final public function setError(string $message)
|
|
|
|
{
|
|
|
|
$this->setLog('error', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check logs of type error exists.
|
|
|
|
*/
|
|
|
|
final public function hasError(): bool
|
|
|
|
{
|
|
|
|
return !empty($this->getLogs('error'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get logs of type error.
|
|
|
|
*/
|
|
|
|
final public function getErrors(): array
|
|
|
|
{
|
|
|
|
return $this->getLogs('error');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a log of type warning.
|
|
|
|
*/
|
|
|
|
final public function setWarning(string $message)
|
|
|
|
{
|
|
|
|
$this->setLog('warning', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check logs of type error warnings.
|
|
|
|
*/
|
|
|
|
final public function hasWarning(): bool
|
|
|
|
{
|
|
|
|
return !empty($this->getLogs('warning'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get logs of type warning.
|
|
|
|
*/
|
|
|
|
final public function getWarnings(): array
|
|
|
|
{
|
|
|
|
return $this->getLogs('warning');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a log of type success.
|
|
|
|
*/
|
|
|
|
final public function setSuccess(string $message)
|
|
|
|
{
|
|
|
|
$this->setLog('success', $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check logs of type error success.
|
|
|
|
*/
|
|
|
|
final public function hasSuccess(): bool
|
|
|
|
{
|
|
|
|
return !empty($this->getLogs('success'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get logs of type success.
|
|
|
|
*/
|
|
|
|
final public function getSuccess(): array
|
|
|
|
{
|
|
|
|
return $this->getLogs('success');
|
|
|
|
}
|
|
|
|
//@}
|
2021-11-01 21:17:44 +00:00
|
|
|
}
|