dcAdvancedCleaner/_admin.php

242 lines
8 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_CONTEXT_ADMIN')) {
return null;
}
2018-02-19 18:10:58 +00:00
2022-11-15 10:34:45 +00:00
dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem(
2021-09-06 22:09:09 +00:00
__('Advanced cleaner'),
2022-12-10 17:02:56 +00:00
dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
dcPage::getPF(basename(__DIR__) . '/icon.svg'),
2021-09-06 23:39:52 +00:00
preg_match(
2022-12-10 17:02:56 +00:00
'/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__))) . '(&.*)?$/',
2021-09-06 23:39:52 +00:00
$_SERVER['REQUEST_URI']
),
2022-11-15 10:34:45 +00:00
dcCore::app()->auth->isSuperAdmin()
2021-11-06 15:19:49 +00:00
);
2023-01-30 18:15:12 +00:00
dcCore::app()->addBehaviors([
'adminDashboardFavoritesV2' => function ($favs) {
$favs->register(basename(__DIR__), [
'title' => __('Advanced cleaner'),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . basename(__DIR__)),
'small-icon' => dcPage::getPF(basename(__DIR__) . '/icon.svg'),
'large-icon' => dcPage::getPF(basename(__DIR__) . '/icon-big.svg'),
//'permissions' => dcCore::app()->auth->isSuperAdmin(),
]);
},
'pluginsToolsTabsV2' => function () {
if (!dcCore::app()->blog->settings->__get(basename(__DIR__))->dcAdvancedCleaner_behavior_active) {
return null;
}
2023-01-30 18:15:12 +00:00
$path = DC_PLUGINS_ROOT;
$redir = dcCore::app()->adminurl->get('admin.plugins', [], '#uninstaller');
$title = '';
2023-01-30 18:15:12 +00:00
$uninstaller = new dcUninstaller();
$uninstaller->loadModules($path);
$modules = $uninstaller->getModules();
$props = $uninstaller->getAllowedActions();
2023-01-30 18:15:12 +00:00
echo '<div class="multi-part" id="uninstaller" title="' . __('Advanced uninstall') . '"><h3>' . __('Advanced uninstall') . '</h3>';
2023-01-30 18:15:12 +00:00
if (!count($modules)) {
echo '<p>' . __('There is no module with uninstall features') . '</p></div>';
2023-01-30 18:15:12 +00:00
return null;
}
2023-01-30 18:15:12 +00:00
echo
'<p>' . __('List of modules with advanced uninstall features') . '</p>' .
'<form method="post" action="' . $redir . '">' .
'<table class="clear"><tr>' .
'<th colspan="2">' . __('module') . '</th>';
2023-01-30 18:15:12 +00:00
foreach ($props as $pro_id => $prop) {
echo '<th>' . __($pro_id) . '</th>';
}
2023-01-30 18:15:12 +00:00
echo '<th>' . __('other') . '</th>' . '</tr>';
2023-01-30 18:15:12 +00:00
$i = 0;
foreach ($modules as $module_id => $module) {
echo
'<tr class="line">' .
'<td class="nowrap">' . $module_id . '</td>' .
'<td class="nowrap">' . $module['version'] . '</td>';
2023-01-30 18:15:12 +00:00
$actions = $uninstaller->getUserActions($module_id);
2023-01-30 18:15:12 +00:00
foreach ($props as $prop_id => $prop) {
echo '<td class="nowrap">';
2023-01-30 18:15:12 +00:00
if (!isset($actions[$prop_id])) {
echo '--</td>';
continue;
}
2023-01-30 18:15:12 +00:00
$j = 0;
foreach ($actions[$prop_id] as $action_id => $action) {
if (!isset($props[$prop_id][$action['action']])) {
continue;
}
$ret = base64_encode(serialize([
'type' => $prop_id,
'action' => $action['action'],
'ns' => $action['ns'],
]));
echo '<label class="classic">' .
form::checkbox(['actions[' . $module_id . '][' . $j . ']'], $ret) .
' ' . $action['desc'] . '</label><br />';
2023-01-30 18:15:12 +00:00
$j++;
}
echo '</td>';
}
2023-01-30 18:15:12 +00:00
echo '<td class="nowrap">';
2023-01-30 18:15:12 +00:00
$callbacks = $uninstaller->getUserCallbacks($module_id);
2023-01-30 18:15:12 +00:00
if (empty($callbacks)) {
echo '--';
}
$k = 0;
foreach ($callbacks as $callback_id => $callback) {
$ret = base64_encode(serialize($callback['func']));
2023-01-30 18:15:12 +00:00
echo '<label class="classic">' .
form::checkbox(['extras[' . $module_id . '][' . $k . ']'], $ret) .
' ' . $callback['desc'] . '</label><br />';
}
echo '</td></tr>';
}
echo
'</table>' .
'<p>' .
dcCore::app()->formNonce() .
form::hidden(['path'], $path) .
form::hidden(['redir'], $redir) .
form::hidden(['action'], 'uninstall') .
'<input type="submit" name="submit" value="' . __('Perform selected actions') . '" /> ' .
'</p>' .
'</form>';
echo '</div>';
},
'adminModulesListDoActions' => function ($list, $modules, $type) {
if (!dcCore::app()->blog->settings->__get(basename(__DIR__))->dcAdvancedCleaner_behavior_active) {
return null;
}
2023-01-30 18:15:12 +00:00
if (!isset($_POST['action']) || $_POST['action'] != 'uninstall'
|| (empty($_POST['extras']) && empty($_POST['actions']))
) {
return null;
}
2023-01-30 18:15:12 +00:00
$uninstaller = new dcUninstaller();
$uninstaller->loadModules($_POST['path']);
$modules = $uninstaller->getModules();
$props = $uninstaller->getAllowedActions();
try {
// Extras
if (!empty($_POST['extras'])) {
foreach ($_POST['extras'] as $module_id => $extras) {
foreach ($extras as $k => $sentence) {
$extra = @unserialize(@base64_decode($sentence));
if (!$extra || !is_callable($extra)) {
continue;
}
call_user_func($extra, $module_id);
}
}
}
2023-01-30 18:15:12 +00:00
// Actions
if (!empty($_POST['actions'])) {
foreach ($_POST['actions'] as $module_id => $actions) {
foreach ($actions as $k => $sentence) {
$action = @unserialize(@base64_decode($sentence));
if (!$action
|| !isset($action['type'])
|| !isset($action['action'])
|| !isset($action['ns'])
) {
continue;
}
$uninstaller->execute($action['type'], $action['action'], $action['ns']);
}
}
}
2023-01-30 18:15:12 +00:00
dcAdminNotices::addSuccessNotice(__('Action successfuly excecuted'));
http::redirect($_POST['redir']);
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
}
2023-01-30 18:15:12 +00:00
},
'pluginsBeforeDelete' => function ($plugin) {
dcAdvancedCleanerModuleBeforeDelete($plugin);
},
'themeBeforeDelete' => function ($theme) {
dcAdvancedCleanerModuleBeforeDelete($theme);
},
]);
function dcAdvancedCleanerModuleBeforeDelete($module)
{
2022-12-10 17:02:56 +00:00
if (!dcCore::app()->blog->settings->__get(basename(__DIR__))->dcAdvancedCleaner_behavior_active) {
return null;
}
2022-12-10 17:08:16 +00:00
$done = false;
$uninstaller = new dcUninstaller();
$uninstaller->loadModule($module['root']);
$m_callbacks = $uninstaller->getDirectCallbacks($module['id']);
$m_actions = $uninstaller->getDirectActions($module['id']);
foreach ($m_callbacks as $k => $callback) {
if (!isset($callback['func']) || !is_callable($callback['func'])) {
continue;
}
call_user_func($callback['func'], $module);
$done = true;
}
foreach ($m_actions as $type => $actions) {
foreach ($actions as $v) {
$uninstaller->execute($type, $v['action'], $v['ns']);
$done = true;
}
}
if ($done) {
if ('theme' == $module['type']) {
dcCore::app()->adminurl->redirect('admin.blog.theme', ['del' => 1]);
} else {
dcCore::app()->adminurl->redirect('admin.plugins', ['removed' => 1]);
}
}
}