dcAdvancedCleaner/inc/class.dc.advanced.cleaner.php

78 lines
2.0 KiB
PHP
Raw Normal View History

2018-02-19 18:10:58 +00:00
<?php
2021-09-06 22:09:09 +00:00
/**
* @brief dcAdvancedCleaner, a plugin for Dotclear 2
2021-11-06 15:19:49 +00:00
*
2021-09-06 22:09:09 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:19:49 +00:00
*
2021-09-06 22:09:09 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-06 15:19:49 +00:00
*
2021-09-06 22:09:09 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-09-06 23:39:52 +00:00
if (!defined('DC_ADMIN_CONTEXT')) {
return null;
}
2018-02-19 18:10:58 +00:00
class dcAdvancedCleaner
{
2021-10-28 20:17:38 +00:00
protected $core;
protected $cleaners = [];
2021-09-06 23:39:52 +00:00
2021-10-28 20:17:38 +00:00
public function __construct($core)
2021-09-06 22:09:09 +00:00
{
2021-10-28 20:17:38 +00:00
$this->core = $core;
2021-11-06 15:19:49 +00:00
$cleaners = new arrayObject();
2021-09-06 22:09:09 +00:00
2021-10-28 20:17:38 +00:00
try {
$this->core->callBehavior('advancedCleanerAdd', $cleaners, $this->core);
2021-09-06 22:09:09 +00:00
2021-11-06 15:19:49 +00:00
foreach ($cleaners as $cleaner) {
if ($cleaner instanceof advancedCleaner && !isset($this->cleaners[$cleaner->id])) {
2021-10-28 20:17:38 +00:00
$this->cleaners[$cleaner->id] = $cleaner;
2021-09-06 23:39:52 +00:00
}
2021-09-06 22:09:09 +00:00
}
2021-10-28 20:17:38 +00:00
} catch (Exception $e) {
$core->error->add($e->getMessage());
2021-09-06 22:09:09 +00:00
}
}
2021-10-28 20:17:38 +00:00
public function get($type = null, $silent = false)
2021-09-06 22:09:09 +00:00
{
2021-10-28 20:17:38 +00:00
if (null === $type) {
return $this->cleaners;
2021-09-06 22:09:09 +00:00
}
2021-10-28 20:17:38 +00:00
if (isset($this->cleaners[$type])) {
return $this->cleaners[$type];
2021-09-06 23:39:52 +00:00
}
2021-10-28 20:17:38 +00:00
if ($silent) {
2021-09-06 22:09:09 +00:00
return false;
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
throw new exception(sprintf(__('unknow cleaner type %s'), $type));
2021-09-06 22:09:09 +00:00
}
2021-10-28 20:17:38 +00:00
public function set($type, $action, $ns)
2021-09-06 22:09:09 +00:00
{
2021-10-28 20:17:38 +00:00
if (!isset($this->cleaners[$type])) {
throw new exception(sprintf(__('unknow cleaner type %s'), $type));
2021-09-06 23:39:52 +00:00
}
2021-10-28 20:17:38 +00:00
if (strtolower($ns) == 'dcadvancedcleaner') {
throw new exception(__("dcAdvancedCleaner can't remove itself"));
2021-09-06 22:09:09 +00:00
}
2021-10-28 20:17:38 +00:00
# BEHAVIOR dcAdvancedCleanerBeforeAction
$this->core->callBehavior('dcAdvancedCleanerBeforeAction', $type, $action, $ns);
2021-09-06 22:09:09 +00:00
2021-10-28 20:17:38 +00:00
$ret = $this->cleaners[$type]->set($action, $ns);
2021-09-06 22:09:09 +00:00
2021-10-28 20:17:38 +00:00
if ($ret === false) {
$msg = $this->cleaners[$type]->error($action);
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
throw new Exception($msg ?? __('Unknow error'));
2021-09-06 22:09:09 +00:00
}
2021-10-28 20:17:38 +00:00
return true;
2021-09-06 22:09:09 +00:00
}
2021-11-06 15:19:49 +00:00
}