dcAdvancedCleaner/inc/lib.advanced.cleaner.php

520 lines
12 KiB
PHP
Raw Normal View History

2021-10-28 20:17:38 +00:00
<?php
/**
* @brief dcAdvancedCleaner, a plugin for Dotclear 2
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-06 15:19:49 +00:00
*
2021-10-28 20:17:38 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
class advancedCleanerSettings extends advancedCleaner
{
protected function init(): bool
{
$this->setProperties([
'id' => 'settings',
'name' => __('Settings'),
2022-11-15 10:34:45 +00:00
'desc' => __('Namespaces registered in dcSettings'),
2021-10-28 20:17:38 +00:00
]);
$this->setActions([
'delete_global' => __('delete global settings'),
'delete_local' => __('delete blog settings'),
2022-11-15 10:34:45 +00:00
'delete_all' => __('delete all settings'),
2021-10-28 20:17:38 +00:00
]);
return true;
}
public function error($action): string
{
if ($action == 'delete_global') {
return __('Failed to delete global settings');
}
if ($action == 'delete_local') {
return __('Failed to delete local settings');
}
if ($action == 'delete_all') {
return __('Failed to delete all settings');
}
return '';
}
public function official(): array
{
return [
'akismet',
'antispam',
'breadcrumb',
'dcckeditor',
'dclegacyeditor',
2021-11-06 15:19:49 +00:00
'maintenance',
2021-10-28 20:17:38 +00:00
'pages',
'pings',
'system',
'themes',
2022-11-15 10:34:45 +00:00
'widgets',
2021-10-28 20:17:38 +00:00
];
}
public function get(): array
{
2022-11-15 10:34:45 +00:00
$res = dcCore::app()->con->select(
2021-10-28 20:17:38 +00:00
'SELECT setting_ns ' .
2022-11-15 10:34:45 +00:00
'FROM ' . dcCore::app()->prefix . 'setting ' .
2021-10-28 20:17:38 +00:00
'WHERE blog_id IS NULL ' .
2021-11-06 15:19:49 +00:00
'OR blog_id IS NOT NULL ' .
2021-10-28 20:17:38 +00:00
'GROUP BY setting_ns'
);
$rs = [];
2021-11-06 15:19:49 +00:00
$i = 0;
while ($res->fetch()) {
$rs[$i]['key'] = $res->setting_ns;
2022-11-15 10:34:45 +00:00
$rs[$i]['value'] = dcCore::app()->con->select(
'SELECT count(*) FROM ' . dcCore::app()->prefix . 'setting ' .
2021-11-06 15:19:49 +00:00
"WHERE setting_ns = '" . $res->setting_ns . "' " .
'AND (blog_id IS NULL OR blog_id IS NOT NULL) ' .
'GROUP BY setting_ns '
2021-10-28 20:17:38 +00:00
)->f(0);
$i++;
}
return $rs;
}
public function set($action, $ns): bool
{
if ($action == 'delete_global') {
2022-11-15 10:34:45 +00:00
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . 'setting ' .
2021-10-28 20:17:38 +00:00
'WHERE blog_id IS NULL ' .
2022-11-15 10:34:45 +00:00
"AND setting_ns = '" . dcCore::app()->con->escape($ns) . "' "
2021-10-28 20:17:38 +00:00
);
return true;
}
if ($action == 'delete_local') {
2022-11-15 10:34:45 +00:00
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . 'setting ' .
"WHERE blog_id = '" . dcCore::app()->con->escape(dcCore::app()->blog->id) . "' " .
"AND setting_ns = '" . dcCore::app()->con->escape($ns) . "' "
2021-10-28 20:17:38 +00:00
);
return true;
}
if ($action == 'delete_all') {
2022-11-15 10:34:45 +00:00
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . 'setting ' .
"WHERE setting_ns = '" . dcCore::app()->con->escape($ns) . "' " .
2021-10-28 20:17:38 +00:00
"AND (blog_id IS NULL OR blog_id != '') "
);
return true;
}
return false;
}
}
class advancedCleanerTables extends advancedCleaner
{
protected function init(): bool
{
$this->setProperties([
'id' => 'tables',
'name' => __('Tables'),
2022-11-15 10:34:45 +00:00
'desc' => __('All database tables of Dotclear'),
2021-10-28 20:17:38 +00:00
]);
$this->setActions([
'delete' => __('delete'),
2022-11-15 10:34:45 +00:00
'empty' => __('empty'),
2021-10-28 20:17:38 +00:00
]);
return true;
}
public function error($action): string
{
if ($action == 'empty') {
return __('Failed to empty table');
}
if ($action == 'delete') {
return __('Failed to delete table');
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return '';
}
public function official(): array
{
return [
'blog',
'category',
'comment',
'link',
'log',
'media',
'meta',
'permissions',
'ping',
'post',
'post_media',
'pref',
'session',
'setting',
'spamrule',
'user',
2022-11-15 10:34:45 +00:00
'version',
2021-10-28 20:17:38 +00:00
];
}
public function get(): array
{
2022-11-15 10:34:45 +00:00
$object = dbSchema::init(dcCore::app()->con);
2021-11-06 15:19:49 +00:00
$res = $object->getTables();
2021-10-28 20:17:38 +00:00
$rs = [];
2021-11-06 15:19:49 +00:00
$i = 0;
foreach ($res as $k => $v) {
2022-11-15 10:34:45 +00:00
if ('' != dcCore::app()->prefix) {
if (!preg_match('/^' . preg_quote(dcCore::app()->prefix) . '(.*?)$/', $v, $m)) {
2021-10-28 20:17:38 +00:00
continue;
}
$v = $m[1];
}
2021-11-06 15:19:49 +00:00
$rs[$i]['key'] = $v;
2022-11-15 10:34:45 +00:00
$rs[$i]['value'] = dcCore::app()->con->select('SELECT count(*) FROM ' . $res[$k])->f(0);
2021-10-28 20:17:38 +00:00
$i++;
}
return $rs;
}
public function set($action, $ns): bool
{
if (in_array($action, ['empty', 'delete'])) {
2022-11-15 10:34:45 +00:00
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->con->escapeSystem(dcCore::app()->prefix . $ns)
2021-10-28 20:17:38 +00:00
);
}
if ($action == 'empty') {
return true;
}
if ($action == 'delete') {
2022-11-15 10:34:45 +00:00
dcCore::app()->con->execute(
'DROP TABLE ' . dcCore::app()->con->escapeSystem(dcCore::app()->prefix . $ns)
2021-10-28 20:17:38 +00:00
);
return true;
}
return false;
}
}
class advancedCleanerVersions extends advancedCleaner
{
protected function init(): bool
{
$this->setProperties([
'id' => 'versions',
'name' => __('Versions'),
2022-11-15 10:34:45 +00:00
'desc' => __('Versions registered in table "version" of Dotclear'),
2021-10-28 20:17:38 +00:00
]);
$this->setActions([
2022-11-15 10:34:45 +00:00
'delete' => __('delete'),
2021-10-28 20:17:38 +00:00
]);
return true;
}
public function error($action): string
{
if ($action == 'delete') {
return __('Failed to delete version');
}
return '';
}
public function official(): array
{
return [
'antispam',
'blogroll',
'blowupConfig',
'core',
'dcCKEditor',
'dcLegacyEditor',
'pages',
'pings',
'simpleMenu',
'tags',
2022-11-15 10:34:45 +00:00
'widgets',
2021-10-28 20:17:38 +00:00
];
}
public function get(): array
{
2022-11-15 10:34:45 +00:00
$res = dcCore::app()->con->select('SELECT * FROM ' . dcCore::app()->prefix . 'version');
2021-10-28 20:17:38 +00:00
$rs = [];
2021-11-06 15:19:49 +00:00
$i = 0;
2021-10-28 20:17:38 +00:00
while ($res->fetch()) {
2021-11-06 15:19:49 +00:00
$rs[$i]['key'] = $res->module;
2021-10-28 20:17:38 +00:00
$rs[$i]['value'] = $res->version;
$i++;
}
return $rs;
}
public function set($action, $ns): bool
{
if ($action == 'delete') {
2022-11-15 10:34:45 +00:00
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . 'version ' .
"WHERE module = '" . dcCore::app()->con->escape($ns) . "' "
2021-10-28 20:17:38 +00:00
);
return true;
}
return false;
}
}
class advancedCleanerPlugins extends advancedCleaner
{
protected function init(): bool
{
$this->setProperties([
'id' => 'plugins',
'name' => __('Plugins'),
2022-11-15 10:34:45 +00:00
'desc' => __('Folders from plugins directories'),
2021-10-28 20:17:38 +00:00
]);
$this->setActions([
'delete' => __('delete'),
2022-11-15 10:34:45 +00:00
'empty' => __('empty'),
2021-10-28 20:17:38 +00:00
]);
return true;
}
public function error($action): string
{
if ($action == 'empty') {
return __('Failed to empty plugin folder');
}
if ($action == 'delete') {
return __('Failed to delete plugin folder');
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return '';
}
public function official(): array
{
return explode(',', DC_DISTRIB_PLUGINS);
}
public function get(): array
{
$res = self::getDirs(explode(PATH_SEPARATOR, DC_PLUGINS_ROOT));
sort($res);
return $res;
}
public function set($action, $ns): bool
{
if ($action == 'empty') {
$res = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
self::delDir($res, $ns, false);
return true;
}
if ($action == 'delete') {
$res = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT);
self::delDir($res, $ns, true);
return true;
}
return false;
}
}
class advancedCleanerThemes extends advancedCleaner
{
protected function init(): bool
{
$this->setProperties([
'id' => 'themes',
'name' => __('Themes'),
2022-11-15 10:34:45 +00:00
'desc' => __('Folders from blog themes directory'),
2021-10-28 20:17:38 +00:00
]);
$this->setActions([
'delete' => __('delete'),
2022-11-15 10:34:45 +00:00
'empty' => __('empty'),
2021-10-28 20:17:38 +00:00
]);
return true;
}
public function error($action): string
{
if ($action == 'empty') {
return __('Failed to empty themes folder');
}
if ($action == 'delete') {
return __('Failed to delete themes folder');
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return '';
}
public function official(): array
{
return explode(',', DC_DISTRIB_THEMES);
}
public function get(): array
{
2022-11-15 10:34:45 +00:00
$res = self::getDirs(dcCore::app()->blog->themes_path);
2021-10-28 20:17:38 +00:00
sort($res);
return $res;
}
public function set($action, $ns): bool
{
if ($action == 'empty') {
2022-11-15 10:34:45 +00:00
self::delDir(dcCore::app()->blog->themes_path, $ns, false);
2021-10-28 20:17:38 +00:00
return true;
}
if ($action == 'delete') {
2022-11-15 10:34:45 +00:00
self::delDir(dcCore::app()->blog->themes_path, $ns, true);
2021-10-28 20:17:38 +00:00
return true;
}
return false;
}
}
class advancedCleanerCaches extends advancedCleaner
{
protected function init(): bool
{
$this->setProperties([
'id' => 'caches',
'name' => __('Cache'),
2022-11-15 10:34:45 +00:00
'desc' => __('Folders from cache directory'),
2021-10-28 20:17:38 +00:00
]);
$this->setActions([
'delete' => __('delete'),
2022-11-15 10:34:45 +00:00
'empty' => __('empty'),
2021-10-28 20:17:38 +00:00
]);
return true;
}
public function error($action): string
{
if ($action == 'empty') {
return __('Failed to empty cache folder');
}
if ($action == 'delete') {
return __('Failed to delete cache folder');
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return '';
}
public function official(): array
{
return ['cbfeed', 'cbtpl', 'dcrepo', 'versions'];
}
public function get(): array
{
return self::getDirs(DC_TPL_CACHE);
}
public function set($action, $ns): bool
{
if ($action == 'empty') {
self::delDir(DC_TPL_CACHE, $ns, false);
return true;
}
if ($action == 'delete') {
self::delDir(DC_TPL_CACHE, $ns, true);
return true;
}
return false;
}
}
class advancedCleanerVars extends advancedCleaner
{
protected function init(): bool
{
$this->setProperties([
'id' => 'vars',
'name' => __('Var'),
2022-11-15 10:34:45 +00:00
'desc' => __('Folders from Dotclear VAR directory'),
2021-10-28 20:17:38 +00:00
]);
$this->setActions([
2022-11-15 10:34:45 +00:00
'delete' => __('delete'),
2021-10-28 20:17:38 +00:00
]);
return true;
}
public function error($action): string
{
if ($action == 'delete') {
return __('Failed to delete var folder');
}
2021-11-06 15:19:49 +00:00
2021-10-28 20:17:38 +00:00
return '';
}
public function official(): array
{
return [];
}
public function get(): array
{
return self::getDirs(DC_VAR);
}
public function set($action, $ns): bool
{
if ($action == 'delete') {
self::delDir(DC_VAR, $ns, true);
return true;
}
return false;
}
2021-11-06 15:19:49 +00:00
}