]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseModeration.php
Merge pull request #13238 from annando/issue-13221
[friendica.git] / src / Module / BaseModeration.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Renderer;
28 use Friendica\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\Navigation\SystemMessages;
30 use Friendica\Network\HTTPException;
31 use Friendica\Util\Profiler;
32 use Psr\Log\LoggerInterface;
33
34 /**
35  * This abstract module is meant to be extended by all modules that are reserved to moderator users.
36  *
37  * It performs a blanket permission check in all the module methods as long as the relevant `parent::method()` is
38  * called in the inheriting module.
39  *
40  * Additionally, it puts together the moderation page aside with all the moderation links.
41  *
42  * @package Friendica\Module
43  */
44 abstract class BaseModeration extends BaseModule
45 {
46         /** @var IHandleUserSessions */
47         protected $session;
48         /** @var SystemMessages */
49         protected $systemMessages;
50         /** @var App */
51         protected $app;
52         /** @var App\Page */
53         protected $page;
54
55         public function __construct(App\Page $page, App $app, SystemMessages $systemMessages, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
56         {
57                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
58
59                 $this->session        = $session;
60                 $this->systemMessages = $systemMessages;
61                 $this->app            = $app;
62                 $this->page           = $page;
63         }
64
65         /**
66          * Checks moderator access and throws exceptions if not logged-in moderator
67          *
68          * @param bool $interactive
69          * @return void
70          * @throws HTTPException\ForbiddenException
71          * @throws HTTPException\InternalServerErrorException
72          */
73         public function checkModerationAccess(bool $interactive = false)
74         {
75                 if (!$this->session->getLocalUserId()) {
76                         if ($interactive) {
77                                 $this->systemMessages->addNotice($this->t('Please login to continue.'));
78                                 $this->session->set('return_path', $this->args->getQueryString());
79                                 $this->baseUrl->redirect('login');
80                         } else {
81                                 throw new HTTPException\UnauthorizedException($this->t('Please login to continue.'));
82                         }
83                 }
84
85                 if (!$this->app->isSiteAdmin()) {
86                         throw new HTTPException\ForbiddenException($this->t('You don\'t have access to administration pages.'));
87                 }
88
89                 if ($this->session->getSubManagedUserId()) {
90                         throw new HTTPException\ForbiddenException($this->t('Submanaged account can\'t access the administration pages. Please log back in as the main account.'));
91                 }
92         }
93
94         protected function content(array $request = []): string
95         {
96                 $this->checkModerationAccess(true);
97
98                 // Header stuff
99                 $this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('moderation/settings_head.tpl'), []);
100
101                 /*
102                  * Side bar links
103                  */
104
105                 // array(url, name, extra css classes)
106                 // not part of $aside to make the template more adjustable
107                 $aside_sub = [
108                         'information' => [$this->t('Information'), [
109                                 'overview' => ['moderation', $this->t('Overview'), 'overview'],
110                         ]],
111                         'configuration' => [$this->t('Configuration'), [
112                                 'users' => ['moderation/users', $this->t('Users'), 'users'],
113                         ]],
114                         'tools' => [$this->t('Tools'), [
115                                 'contactblock' => ['moderation/blocklist/contact', $this->t('Contact Blocklist'), 'contactblock'],
116                                 'blocklist'    => ['moderation/blocklist/server', $this->t('Server Blocklist'), 'blocklist'],
117                                 'deleteitem'   => ['moderation/item/delete', $this->t('Delete Item'), 'deleteitem'],
118                         ]],
119                         'diagnostics' => [$this->t('Diagnostics'), [
120                                 'itemsource' => ['moderation/item/source', $this->t('Item Source'), 'itemsource'],
121                         ]],
122                 ];
123
124                 $t = Renderer::getMarkupTemplate('moderation/aside.tpl');
125                 $this->page['aside'] .= Renderer::replaceMacros($t, [
126                         '$subpages'  => $aside_sub,
127                         '$admtxt'    => $this->t('Moderation'),
128                         '$h_pending' => $this->t('User registrations waiting for confirmation'),
129                         '$modurl'    => 'moderation/'
130                 ]);
131
132                 return '';
133         }
134 }