Use router parameters in Admin modules

- Remove 10 @TODO tags

# Conflicts:
#	src/Module/Admin/DBSync.php
#	src/Module/Admin/Themes/Details.php
#	src/Module/Admin/Themes/Embed.php
This commit is contained in:
Hypolite Petovan 2020-09-08 10:38:35 -04:00
parent 98eb53c20f
commit 2ce15cae1a
8 changed files with 427 additions and 501 deletions

View File

@ -34,24 +34,20 @@ class Details extends BaseAdmin
{
parent::post($parameters);
$a = DI::app();
$addon = Strings::sanitizeFilePathItem($parameters['addon']);
$redirect = 'admin/addons/' . $addon;
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$addon = $a->argv[2];
$addon = Strings::sanitizeFilePathItem($addon);
if (is_file('addon/' . $addon . '/' . $addon . '.php')) {
include_once 'addon/' . $addon . '/' . $addon . '.php';
if (function_exists($addon . '_addon_admin_post')) {
$func = $addon . '_addon_admin_post';
$func($a);
}
DI::baseUrl()->redirect('admin/addons/' . $addon);
$func(DI::app());
}
}
DI::baseUrl()->redirect('admin/addons');
DI::baseUrl()->redirect($redirect);
}
public static function content(array $parameters = [])
@ -62,10 +58,7 @@ class Details extends BaseAdmin
$addons_admin = Addon::getAdminList();
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$addon = $a->argv[2];
$addon = Strings::sanitizeFilePathItem($addon);
$addon = Strings::sanitizeFilePathItem($parameters['addon']);
if (!is_file("addon/$addon/$addon.php")) {
notice(DI::l10n()->t('Addon not found.'));
Addon::uninstall($addon);
@ -73,7 +66,7 @@ class Details extends BaseAdmin
}
if (($_GET['action'] ?? '') == 'toggle') {
parent::checkFormSecurityTokenRedirectOnError('/admin/addons', 'admin_themes', 't');
self::checkFormSecurityTokenRedirectOnError('/admin/addons', 'admin_addons', 't');
// Toggle addon status
if (Addon::isEnabled($addon)) {
@ -131,10 +124,7 @@ class Details extends BaseAdmin
'$screenshot' => '',
'$readme' => $readme,
'$form_security_token' => parent::getFormSecurityToken('admin_themes'),
'$form_security_token' => self::getFormSecurityToken('admin_addons'),
]);
}
DI::baseUrl()->redirect('admin/addons');
}
}

View File

@ -36,64 +36,62 @@ class DBSync extends BaseAdmin
$a = DI::app();
$o = '';
$action = $parameters['action'] ?? '';
$update = $parameters['update'] ?? 0;
if ($a->argc > 3 && $a->argv[2] === 'mark') {
// @TODO: Replace with parameter from router
$update = intval($a->argv[3]);
switch ($action) {
case 'mark':
if ($update) {
DI::config()->set('database', 'update_' . $update, 'success');
$curr = DI::config()->get('system', 'build');
if (intval($curr) == $update) {
DI::config()->set('system', 'build', intval($curr) + 1);
}
info(DI::l10n()->t('Update has been marked successful') . EOL);
}
DI::baseUrl()->redirect('admin/dbsync');
info(DI::l10n()->t('Update has been marked successful'));
}
if ($a->argc > 2) {
if ($a->argv[2] === 'check') {
break;
case 'check':
// @TODO Seems like a similar logic like Update::check()
$retval = DBStructure::update($a->getBasePath(), false, true);
if ($retval === '') {
$o .= DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
$o = DI::l10n()->t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
DI::config()->set('database', 'last_successful_update', DB_UPDATE_VERSION);
DI::config()->set('database', 'last_successful_update_time', time());
} else {
$o .= DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
$o = DI::l10n()->t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
}
if ($a->argv[2] === 'check') {
return $o;
}
} elseif (intval($a->argv[2])) {
case 'update':
require_once 'update.php';
// @TODO: Replace with parameter from router
$update = intval($a->argv[2]);
if ($update) {
$func = 'update_' . $update;
if (function_exists($func)) {
$retval = $func();
if ($retval === Update::FAILED) {
$o .= DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
$o = DI::l10n()->t("Executing %s failed with error: %s", $func, $retval);
} elseif ($retval === Update::SUCCESS) {
$o .= DI::l10n()->t('Update %s was successfully applied.', $func);
$o = DI::l10n()->t('Update %s was successfully applied.', $func);
DI::config()->set('database', $func, 'success');
} else {
$o .= DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
$o = DI::l10n()->t('Update %s did not return a status. Unknown if it succeeded.', $func);
}
} else {
$o .= DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
$o = DI::l10n()->t('There was no additional update function %s that needed to be called.', $func) . "<br />";
DI::config()->set('database', $func, 'success');
}
return $o;
}
}
break;
default:
$failed = [];
$configStmt = DBA::select('config', ['k', 'v'], ['cat' => 'database']);
while ($config = DBA::fetch($configStmt)) {
@ -123,4 +121,8 @@ class DBSync extends BaseAdmin
return $o;
}
DI::baseUrl()->redirect('admin/dbsync');
return '';
}
}

View File

@ -42,13 +42,10 @@ class Queue extends BaseAdmin
{
parent::content($parameters);
$a = DI::app();
// @TODO: Replace with parameter from router
$deferred = $a->argc > 2 && $a->argv[2] == 'deferred';
$status = $parameters['status'] ?? '';
// get jobs from the workerqueue table
if ($deferred) {
if ($status == 'deferred') {
$condition = ["NOT `done` AND `retrial` > ?", 0];
$sub_title = DI::l10n()->t('Inspect Deferred Worker Queue');
$info = DI::l10n()->t("This page lists the deferred worker jobs. This are jobs that couldn't be executed at the first time.");

View File

@ -30,44 +30,11 @@ use Friendica\Util\Strings;
class Details extends BaseAdmin
{
public static function post(array $parameters = [])
{
parent::post($parameters);
$a = DI::app();
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$theme = $a->argv[2];
$theme = Strings::sanitizeFilePathItem($theme);
if (is_file("view/theme/$theme/config.php")) {
require_once "view/theme/$theme/config.php";
if (function_exists('theme_admin_post')) {
theme_admin_post($a);
}
}
info(DI::l10n()->t('Theme settings updated.'));
if (DI::mode()->isAjax()) {
return;
}
DI::baseUrl()->redirect('admin/themes/' . $theme);
}
}
public static function content(array $parameters = [])
{
parent::content($parameters);
$a = DI::app();
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$theme = $a->argv[2];
$theme = Strings::sanitizeFilePathItem($theme);
$theme = Strings::sanitizeFilePathItem($parameters['theme']);
if (!is_dir("view/theme/$theme")) {
notice(DI::l10n()->t("Item not found."));
return '';
@ -83,7 +50,7 @@ class Details extends BaseAdmin
}
if (!empty($_GET['action']) && $_GET['action'] == 'toggle') {
parent::checkFormSecurityTokenRedirectOnError('/admin/themes', 'admin_themes', 't');
self::checkFormSecurityTokenRedirectOnError('/admin/themes', 'admin_themes', 't');
if ($isEnabled) {
Theme::uninstall($theme);
@ -91,7 +58,7 @@ class Details extends BaseAdmin
} elseif (Theme::install($theme)) {
info(DI::l10n()->t('Theme %s successfully enabled.', $theme));
} else {
info(DI::l10n()->t('Theme %s failed to install.', $theme));
notice(DI::l10n()->t('Theme %s failed to install.', $theme));
}
DI::baseUrl()->redirect('admin/themes/' . $theme);
@ -136,10 +103,7 @@ class Details extends BaseAdmin
'$screenshot' => $screenshot,
'$readme' => $readme,
'$form_security_token' => parent::getFormSecurityToken("admin_themes"),
'$form_security_token' => self::getFormSecurityToken("admin_themes"),
]);
}
DI::baseUrl()->redirect('admin/themes');
}
}

View File

@ -30,15 +30,9 @@ class Embed extends BaseAdmin
{
public static function init(array $parameters = [])
{
$a = DI::app();
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$theme = $a->argv[2];
$theme = Strings::sanitizeFilePathItem($theme);
$theme = Strings::sanitizeFilePathItem($parameters['theme']);
if (is_file("view/theme/$theme/config.php")) {
$a->setCurrentTheme($theme);
}
DI::app()->setCurrentTheme($theme);
}
}
@ -46,42 +40,27 @@ class Embed extends BaseAdmin
{
parent::post($parameters);
$a = DI::app();
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$theme = $a->argv[2];
$theme = Strings::sanitizeFilePathItem($theme);
$theme = Strings::sanitizeFilePathItem($parameters['theme']);
if (is_file("view/theme/$theme/config.php")) {
self::checkFormSecurityTokenRedirectOnError('/admin/themes/' . $theme . '/embed?mode=minimal', 'admin_theme_settings');
require_once "view/theme/$theme/config.php";
if (function_exists('theme_admin_post')) {
theme_admin_post($a);
self::checkFormSecurityTokenRedirectOnError('/admin/themes/' . $theme . '/embed?mode=minimal', 'admin_theme_settings');
theme_admin_post(DI::app());
}
}
info(DI::l10n()->t('Theme settings updated.'));
if (DI::mode()->isAjax()) {
return;
}
DI::baseUrl()->redirect('admin/themes/' . $theme . '/embed?mode=minimal');
}
}
public static function content(array $parameters = [])
{
parent::content($parameters);
$a = DI::app();
if ($a->argc > 2) {
// @TODO: Replace with parameter from router
$theme = $a->argv[2];
$theme = Strings::sanitizeFilePathItem($theme);
$theme = Strings::sanitizeFilePathItem($parameters['theme']);
if (!is_dir("view/theme/$theme")) {
notice(DI::l10n()->t('Unknown theme.'));
return '';
@ -92,7 +71,7 @@ class Embed extends BaseAdmin
require_once "view/theme/$theme/config.php";
if (function_exists('theme_admin')) {
$admin_form = theme_admin($a);
$admin_form = theme_admin(DI::app());
}
}
@ -103,10 +82,7 @@ class Embed extends BaseAdmin
return Renderer::replaceMacros($t, [
'$action' => '/admin/themes/' . $theme . '/embed?mode=minimal',
'$form' => $admin_form,
'$form_security_token' => parent::getFormSecurityToken("admin_theme_settings"),
'$form_security_token' => self::getFormSecurityToken("admin_theme_settings"),
]);
}
return '';
}
}

View File

@ -101,23 +101,22 @@ class Users extends BaseAdmin
{
parent::content($parameters);
$a = DI::app();
$action = $parameters['action'] ?? '';
$uid = $parameters['uid'] ?? 0;
if ($a->argc > 3) {
// @TODO: Replace with parameter from router
$action = $a->argv[2];
$uid = $a->argv[3];
if ($uid) {
$user = User::getById($uid, ['username', 'blocked']);
if (!DBA::isResult($user)) {
notice('User not found' . EOL);
DI::baseUrl()->redirect('admin/users');
return ''; // NOTREACHED
}
}
switch ($action) {
case 'delete':
if (local_user() != $uid) {
parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
self::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
// delete user
User::remove($uid);
@ -127,30 +126,26 @@ class Users extends BaseAdmin
}
break;
case 'block':
parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
self::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
User::block($uid);
notice(DI::l10n()->t('User "%s" blocked', $user['username']));
break;
case 'unblock':
parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
self::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
User::block($uid, false);
notice(DI::l10n()->t('User "%s" unblocked', $user['username']));
break;
case 'allow':
parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
self::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
User::allow(Register::getPendingForUser($uid)['hash'] ?? '');
notice(DI::l10n()->t('Account approved.'));
break;
case 'deny':
parent::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
self::checkFormSecurityTokenRedirectOnError('/admin/users', 'admin_users', 't');
User::deny(Register::getPendingForUser($uid)['hash'] ?? '');
notice(DI::l10n()->t('Registration revoked'));
break;
}
DI::baseUrl()->redirect('admin/users');
}
default:
/* get pending */
$pending = Register::getPending();
@ -267,7 +262,7 @@ class Users extends BaseAdmin
'$confirm_delete_multi' => DI::l10n()->t('Selected users will be deleted!\n\nEverything these users had posted on this site will be permanently deleted!\n\nAre you sure?'),
'$confirm_delete' => DI::l10n()->t('The user {0} will be deleted!\n\nEverything this user has posted on this site will be permanently deleted!\n\nAre you sure?'),
'$form_security_token' => parent::getFormSecurityToken('admin_users'),
'$form_security_token' => self::getFormSecurityToken('admin_users'),
// values //
'$baseurl' => DI::baseUrl()->get(true),
@ -284,4 +279,8 @@ class Users extends BaseAdmin
return $o;
}
DI::baseUrl()->redirect('admin/users');
return '';
}
}

View File

@ -73,9 +73,7 @@ return [
'/blocklist/contact' => [Module\Admin\Blocklist\Contact::class, [R::GET, R::POST]],
'/blocklist/server' => [Module\Admin\Blocklist\Server::class, [R::GET, R::POST]],
'/dbsync[/check]' => [Module\Admin\DBSync::class, [R::GET]],
'/dbsync/{update:\d+}' => [Module\Admin\DBSync::class, [R::GET]],
'/dbsync/mark/{update:\d+}' => [Module\Admin\DBSync::class, [R::GET]],
'/dbsync[/{action}[/{update:\d+}]]' => [Module\Admin\DBSync::class, [R::GET]],
'/features' => [Module\Admin\Features::class, [R::GET, R::POST]],
'/federation' => [Module\Admin\Federation::class, [R::GET]],
@ -88,7 +86,7 @@ return [
'/phpinfo' => [Module\Admin\PhpInfo::class, [R::GET]],
'/queue[/deferred]' => [Module\Admin\Queue::class, [R::GET]],
'/queue[/{status}]' => [Module\Admin\Queue::class, [R::GET]],
'/site' => [Module\Admin\Site::class, [R::GET, R::POST]],

View File

@ -10,7 +10,7 @@
<ul>
<li><a href="{{$baseurl}}/admin/dbsync/mark/{{$f}}">{{$mark}}</a></li>
<li><a href="{{$baseurl}}/admin/dbsync/{{$f}}">{{$apply}}</a></li>
<li><a href="{{$baseurl}}/admin/dbsync/update/{{$f}}">{{$apply}}</a></li>
</ul>
<hr />