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