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

76 lines
1.9 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 $cleaners = [];
2021-09-06 23:39:52 +00:00
2022-11-15 10:34:45 +00:00
public function __construct()
2021-09-06 22:09:09 +00:00
{
2022-11-15 10:34:45 +00:00
$cleaners = new arrayObject();
2021-09-06 22:09:09 +00:00
2021-10-28 20:17:38 +00:00
try {
2022-11-15 10:34:45 +00:00
dcCore::app()->callBehavior('advancedCleanerAdd', $cleaners);
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) {
2022-11-15 10:34:45 +00:00
dcCore::app()->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
}
2022-12-10 17:02:56 +00:00
if ($ns == basename(dirname('../' . __DIR__))) {
2021-10-28 20:17:38 +00:00
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
2022-11-15 10:34:45 +00:00
dcCore::app()->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
}