]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseAdmin.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / src / Module / BaseAdmin.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\BaseModule;
25 use Friendica\Core\Addon;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session;
28 use Friendica\DI;
29 use Friendica\Network\HTTPException;
30
31 require_once 'boot.php';
32
33 /**
34  * This abstract module is meant to be extended by all modules that are reserved to administrator users.
35  *
36  * It performs a blanket permission check in all the module methods as long as the relevant `parent::method()` is
37  * called in the inheriting module.
38  *
39  * Additionally, it puts together the administration page aside with all the administration links.
40  *
41  * @package Friendica\Module
42  */
43 abstract class BaseAdmin extends BaseModule
44 {
45         /**
46          * Checks admin access and throws exceptions if not logged-in administrator
47          *
48          * @param bool $interactive
49          * @return void
50          * @throws HTTPException\ForbiddenException
51          * @throws HTTPException\InternalServerErrorException
52          */
53         public static function checkAdminAccess(bool $interactive = false)
54         {
55                 if (!Session::getLocalUser()) {
56                         if ($interactive) {
57                                 DI::sysmsg()->addNotice(DI::l10n()->t('Please login to continue.'));
58                                 DI::session()->set('return_path', DI::args()->getQueryString());
59                                 DI::baseUrl()->redirect('login');
60                         } else {
61                                 throw new HTTPException\UnauthorizedException(DI::l10n()->t('Please login to continue.'));
62                         }
63                 }
64
65                 if (!DI::app()->isSiteAdmin()) {
66                         throw new HTTPException\ForbiddenException(DI::l10n()->t('You don\'t have access to administration pages.'));
67                 }
68
69                 if (!empty($_SESSION['submanage'])) {
70                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Submanaged account can\'t access the administration pages. Please log back in as the main account.'));
71                 }
72         }
73
74         protected function content(array $request = []): string
75         {
76                 self::checkAdminAccess(true);
77
78                 // Header stuff
79                 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('admin/settings_head.tpl'), []);
80
81                 /*
82                  * Side bar links
83                  */
84
85                 // array(url, name, extra css classes)
86                 // not part of $aside to make the template more adjustable
87                 $aside_sub = [
88                         'information' => [DI::l10n()->t('Information'), [
89                                 'overview'     => ['admin'             , DI::l10n()->t('Overview')                , 'overview'],
90                                 'federation'   => ['admin/federation'  , DI::l10n()->t('Federation Statistics')   , 'federation']
91                         ]],
92                         'configuration' => [DI::l10n()->t('Configuration'), [
93                                 'site'         => ['admin/site'        , DI::l10n()->t('Site')                    , 'site'],
94                                 'storage'      => ['admin/storage'     , DI::l10n()->t('Storage')                 , 'storage'],
95                                 'users'        => ['admin/users'       , DI::l10n()->t('Users')                   , 'users'],
96                                 'addons'       => ['admin/addons'      , DI::l10n()->t('Addons')                  , 'addons'],
97                                 'themes'       => ['admin/themes'      , DI::l10n()->t('Themes')                  , 'themes'],
98                                 'features'     => ['admin/features'    , DI::l10n()->t('Additional features')     , 'features'],
99                                 'tos'          => ['admin/tos'         , DI::l10n()->t('Terms of Service')        , 'tos'],
100                         ]],
101                         'database' => [DI::l10n()->t('Database'), [
102                                 'dbsync'       => ['admin/dbsync'      , DI::l10n()->t('DB updates')              , 'dbsync'],
103                                 'deferred'     => ['admin/queue/deferred', DI::l10n()->t('Inspect Deferred Workers'), 'deferred'],
104                                 'workerqueue'  => ['admin/queue'       , DI::l10n()->t('Inspect worker Queue')    , 'workerqueue'],
105                         ]],
106                         'tools' => [DI::l10n()->t('Tools'), [
107                                 'contactblock' => ['admin/blocklist/contact', DI::l10n()->t('Contact Blocklist')  , 'contactblock'],
108                                 'blocklist'    => ['admin/blocklist/server' , DI::l10n()->t('Server Blocklist')   , 'blocklist'],
109                                 'deleteitem'   => ['admin/item/delete' , DI::l10n()->t('Delete Item')             , 'deleteitem'],
110                         ]],
111                         'logs' => [DI::l10n()->t('Logs'), [
112                                 'logsconfig'   => ['admin/logs/', DI::l10n()->t('Logs')                           , 'logs'],
113                                 'logsview'     => ['admin/logs/view'    , DI::l10n()->t('View Logs')              , 'viewlogs'],
114                         ]],
115                         'diagnostics' => [DI::l10n()->t('Diagnostics'), [
116                                 'phpinfo'      => ['admin/phpinfo'           , DI::l10n()->t('PHP Info')          , 'phpinfo'],
117                                 'probe'        => ['probe'             , DI::l10n()->t('probe address')           , 'probe'],
118                                 'webfinger'    => ['webfinger'         , DI::l10n()->t('check webfinger')         , 'webfinger'],
119                                 'itemsource'   => ['admin/item/source' , DI::l10n()->t('Item Source')             , 'itemsource'],
120                                 'babel'        => ['babel'             , DI::l10n()->t('Babel')                   , 'babel'],
121                                 'debug/ap'     => ['debug/ap'          , DI::l10n()->t('ActivityPub Conversion')  , 'debug/ap'],
122                         ]],
123                 ];
124
125                 $t = Renderer::getMarkupTemplate('admin/aside.tpl');
126                 DI::page()['aside'] .= Renderer::replaceMacros($t, [
127                         '$admin' => ['addons_admin' => Addon::getAdminList()],
128                         '$subpages' => $aside_sub,
129                         '$admtxt' => DI::l10n()->t('Admin'),
130                         '$plugadmtxt' => DI::l10n()->t('Addon Features'),
131                         '$h_pending' => DI::l10n()->t('User registrations waiting for confirmation'),
132                         '$admurl' => 'admin/'
133                 ]);
134
135                 return '';
136         }
137 }