dcAdvancedCleaner/inc/class.dc.uninstaller.php

357 lines
9.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
2021-10-28 20:17:38 +00:00
# Localized l10n
__('delete table');
__('delete cache files');
__('delete plugin files');
__('delete theme files');
__('delete the version number');
__('Uninstall extensions');
__('delete %s blog settings');
__('delete %s global settings');
__('delete all %s settings');
__('delete %s table');
__('delete %s version number');
__('delete %s plugin files');
__('delete %s theme file');
__('delete %s cache files');
2018-02-19 18:10:58 +00:00
2021-10-28 20:17:38 +00:00
/**
* @brief Modules uninstall features handler
*
* Provides an object to handle modules uninstall features
2021-11-06 15:19:49 +00:00
* (themes or plugins).
2021-10-28 20:17:38 +00:00
* This class used dcAdvancedCleaner.
*/
2018-02-19 18:10:58 +00:00
class dcUninstaller
{
2021-09-06 22:09:09 +00:00
protected $path;
2021-11-06 15:19:49 +00:00
protected $modules = [];
protected $actions = ['user' => [], 'callback' => []];
2021-09-06 23:39:52 +00:00
protected $callbacks = ['user' => [], 'callback' => []];
2021-09-06 22:09:09 +00:00
2021-11-06 15:19:49 +00:00
protected $id = null;
2021-09-06 22:09:09 +00:00
protected $mroot = null;
2021-10-28 20:17:38 +00:00
private $ac;
private $allowed_actions = null;
2021-09-06 22:09:09 +00:00
/**
2021-10-28 20:17:38 +00:00
* Object constructor.
*/
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
$this->ac = new dcAdvancedCleaner();
2021-10-28 20:17:38 +00:00
$res = [];
2021-11-06 15:19:49 +00:00
foreach ($this->ac->get() as $cleaner) {
2021-10-28 20:17:38 +00:00
$res[$cleaner->id] = $cleaner->getActions();
}
$this->allowed_actions = $res;
2021-09-06 22:09:09 +00:00
}
2021-10-28 20:17:38 +00:00
public function getAllowedActions()
2021-09-06 22:09:09 +00:00
{
2021-10-28 20:17:38 +00:00
return $this->allowed_actions;
2021-09-06 22:09:09 +00:00
}
/**
2021-10-28 20:17:38 +00:00
* Loads modules.
2021-11-06 15:19:49 +00:00
*
* Files _defines.php and _uninstall.php must be present on module
2021-10-28 20:17:38 +00:00
* to be recognized.
* (path separator depends on your OS).
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $path Separated list of paths
*/
2021-09-06 22:09:09 +00:00
public function loadModules($path)
{
2021-11-06 15:19:49 +00:00
$this->path = explode(PATH_SEPARATOR, $path);
2021-09-06 22:09:09 +00:00
2021-09-06 23:39:52 +00:00
foreach ($this->path as $root) {
if (!is_dir($root) || !is_readable($root)) {
continue;
}
if (substr($root, -1) != '/') {
$root .= '/';
}
if (($d = @dir($root)) === false) {
continue;
}
while (($entry = $d->read()) !== false) {
$full_entry = $root . '/' . $entry;
2021-09-06 22:09:09 +00:00
2021-09-06 23:39:52 +00:00
if ($entry != '.' && $entry != '..' && is_dir($full_entry)) {
2021-09-06 22:09:09 +00:00
$this->loadModule($full_entry);
}
}
$d->close();
}
# Sort modules by name
2021-09-06 23:39:52 +00:00
uasort($this->modules, [$this, 'sortModules']);
2021-09-06 22:09:09 +00:00
}
/**
2021-10-28 20:17:38 +00:00
* Load one module.
2021-11-06 15:19:49 +00:00
*
* Files _defines.php and _uninstall.php must be present on module
2021-10-28 20:17:38 +00:00
* to be recognized.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $root path of module
*/
2021-09-06 22:09:09 +00:00
public function loadModule($root)
{
2021-09-06 23:39:52 +00:00
if (file_exists($root . '/_define.php')
&& file_exists($root . '/_uninstall.php')) {
2021-11-06 15:19:49 +00:00
$this->id = basename($root);
2021-09-06 22:09:09 +00:00
$this->mroot = $root;
2021-09-06 23:39:52 +00:00
require $root . '/_define.php';
require $root . '/_uninstall.php';
2021-09-06 22:09:09 +00:00
2021-11-06 15:19:49 +00:00
$this->id = null;
2021-09-06 22:09:09 +00:00
$this->mroot = null;
}
}
/**
2021-10-28 20:17:38 +00:00
* This method registers a module in modules list.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $name Module name
* @param string $desc Module description
* @param string $author Module author name
* @param string $version Module version
*/
2021-09-06 23:39:52 +00:00
public function registerModule($name, $desc, $author, $version, $properties = [])
2021-09-06 22:09:09 +00:00
{
if ($this->id) {
2021-09-06 23:39:52 +00:00
$this->modules[$this->id] = [
2021-11-06 15:19:49 +00:00
'root' => $this->mroot,
'name' => $name,
'desc' => $desc,
'author' => $author,
'version' => $version,
2022-11-15 10:34:45 +00:00
'root_writable' => is_writable($this->mroot),
2021-09-06 23:39:52 +00:00
];
2021-09-06 22:09:09 +00:00
}
}
/**
2021-10-28 20:17:38 +00:00
* Returns all modules associative array or only one module if <var>$id</var>
* is present.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $id Optionnal module ID
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @return array Modules
*/
2021-09-06 23:39:52 +00:00
public function getModules($id = null)
2021-09-06 22:09:09 +00:00
{
if ($id && isset($this->modules[$id])) {
return $this->modules[$id];
}
2021-11-06 15:19:49 +00:00
2021-09-06 22:09:09 +00:00
return $this->modules;
}
/**
2021-10-28 20:17:38 +00:00
* Returns true if the module with ID <var>$id</var> exists.
2021-11-06 15:19:49 +00:00
*
2022-11-15 10:34:45 +00:00
* @param string $id Module ID
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @return boolean Success
*/
2021-09-06 22:09:09 +00:00
public function moduleExists($id)
{
return isset($this->modules[$id]);
}
/**
2021-10-28 20:17:38 +00:00
* Add a predefined action to unsintall features.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* This action is set in _uninstall.php.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $type Type of action (from $allowed_actions)
* @param string $action Action (from $allowed_actions)
* @param string $ns Name of setting related to module.
* @param string $desc Description of action
*/
2022-12-20 20:32:34 +00:00
public function addUserAction($type, $action, $ns, $desc = '')
2021-09-06 22:09:09 +00:00
{
2021-09-06 23:39:52 +00:00
$this->addAction('user', $type, $action, $ns, $desc);
2021-09-06 22:09:09 +00:00
}
2022-12-20 20:32:34 +00:00
public function addDirectAction($type, $action, $ns, $desc = '')
2021-09-06 22:09:09 +00:00
{
2021-09-06 23:39:52 +00:00
$this->addAction('direct', $type, $action, $ns, $desc);
2021-09-06 22:09:09 +00:00
}
2021-09-06 23:39:52 +00:00
private function addAction($group, $type, $action, $ns, $desc)
2021-09-06 22:09:09 +00:00
{
$group = self::group($group);
2021-09-06 23:39:52 +00:00
if (null === $this->id) {
return null;
}
if (empty($type) || empty($ns)) {
return null;
}
2021-10-28 20:17:38 +00:00
if (!isset($this->allowed_actions[$type][$action])) {
2021-09-06 23:39:52 +00:00
return null;
}
if (empty($desc)) {
$desc = __($action);
}
$this->actions[$group][$this->id][$type][] = [
2021-10-28 20:17:38 +00:00
'ns' => $ns,
2021-09-06 22:09:09 +00:00
'action' => $action,
2022-11-15 10:34:45 +00:00
'desc' => $desc,
2021-09-06 23:39:52 +00:00
];
2021-09-06 22:09:09 +00:00
}
/**
2021-10-28 20:17:38 +00:00
* Returns modules <var>$id</var> predefined actions associative array
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $id Optionnal module ID
* @return array Modules id
*/
2021-09-06 22:09:09 +00:00
public function getUserActions($id)
{
2021-09-06 23:39:52 +00:00
return $this->getActions('user', $id);
2021-09-06 22:09:09 +00:00
}
public function getDirectActions($id)
{
2021-09-06 23:39:52 +00:00
return $this->getActions('direct', $id);
2021-09-06 22:09:09 +00:00
}
2021-09-06 23:39:52 +00:00
protected function getActions($group, $id)
2021-09-06 22:09:09 +00:00
{
$group = self::group($group);
2021-09-06 23:39:52 +00:00
if (!isset($this->actions[$group][$id])) {
return [];
}
$res = [];
2021-11-06 15:19:49 +00:00
foreach ($this->allowed_actions as $k => $v) {
2021-10-28 20:17:38 +00:00
if (!isset($this->actions[$group][$id][$k])) {
2021-09-06 23:39:52 +00:00
continue;
}
2021-10-28 20:17:38 +00:00
$res[$k] = $this->actions[$group][$id][$k];
2021-09-06 22:09:09 +00:00
}
2021-11-06 15:19:49 +00:00
2021-09-06 22:09:09 +00:00
return $res;
}
/**
2021-10-28 20:17:38 +00:00
* Add a callable function for unsintall features.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* This action is set in _uninstall.php.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $func Callable function
* @param string $desc Description of action
*/
2021-11-06 15:19:49 +00:00
protected function addUserCallback($func, $desc = '')
2021-09-06 22:09:09 +00:00
{
2021-09-06 23:39:52 +00:00
$this->addCallback('user', $func, $desc);
2021-09-06 22:09:09 +00:00
}
2021-09-06 23:39:52 +00:00
protected function addDirectCallback($func, $desc = '')
2021-09-06 22:09:09 +00:00
{
2021-09-06 23:39:52 +00:00
$this->addCallback('direct', $func, $desc);
2021-09-06 22:09:09 +00:00
}
2021-09-06 23:39:52 +00:00
private function addCallback($group, $func, $desc)
2021-09-06 22:09:09 +00:00
{
$group = self::group($group);
2021-09-06 23:39:52 +00:00
if (null === $this->id) {
return null;
}
if (empty($desc)) {
$desc = __('extra action');
}
if (!is_callable($func)) {
return null;
}
$this->callbacks[$group][$this->id][] = [
2021-09-06 22:09:09 +00:00
'func' => $func,
2022-11-15 10:34:45 +00:00
'desc' => $desc,
2021-09-06 23:39:52 +00:00
];
2021-09-06 22:09:09 +00:00
}
/**
2021-10-28 20:17:38 +00:00
* Returns modules <var>$id</var> callback actions associative array
2021-09-06 22:09:09 +00:00
2021-10-28 20:17:38 +00:00
* @param string $id Optionnal module ID
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @return array Modules id
*/
2021-09-06 22:09:09 +00:00
public function getUserCallbacks($id)
{
2021-09-06 23:39:52 +00:00
return $this->getCallbacks('user', $id);
2021-09-06 22:09:09 +00:00
}
public function getDirectCallbacks($id)
{
2021-09-06 23:39:52 +00:00
return $this->getCallbacks('direct', $id);
2021-09-06 22:09:09 +00:00
}
2021-09-06 23:39:52 +00:00
protected function getCallbacks($group, $id)
2021-09-06 22:09:09 +00:00
{
$group = self::group($group);
2021-09-06 23:39:52 +00:00
if (!isset($this->callbacks[$group][$id])) {
return [];
}
2021-11-06 15:19:49 +00:00
2021-09-06 22:09:09 +00:00
return $this->callbacks[$group][$id];
}
/**
2021-11-06 15:19:49 +00:00
* Execute a predifined action.
*
2021-10-28 20:17:38 +00:00
* This function call dcAdvancedCleaner to do actions.
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @param string $type Type of action (from $allowed_actions)
* @param string $action Action (from $allowed_actions)
* @param string $ns Name of setting related to module.
*
* @return boolean Success
*/
2021-09-06 23:39:52 +00:00
public function execute($type, $action, $ns)
2021-09-06 22:09:09 +00:00
{
2021-10-28 20:17:38 +00:00
if (!isset($this->allowed_actions[$type][$action]) || empty($ns)) {
return false;
2021-09-06 23:39:52 +00:00
}
2021-10-28 20:17:38 +00:00
$this->ac->set($type, $action, $ns);
return true;
2021-09-06 22:09:09 +00:00
}
2021-09-06 23:39:52 +00:00
private function sortModules($a, $b)
2021-09-06 22:09:09 +00:00
{
2021-09-06 23:39:52 +00:00
return strcasecmp($a['name'], $b['name']);
2021-09-06 22:09:09 +00:00
}
private function group($group)
{
2021-09-06 23:39:52 +00:00
return in_array($group, ['user','direct']) ? $group : null;
2021-09-06 22:09:09 +00:00
}
2021-11-06 15:19:49 +00:00
}