-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
BackendMain.php
410 lines (344 loc) · 13.5 KB
/
BackendMain.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
use Contao\CoreBundle\Event\ContaoCoreEvents;
use Contao\CoreBundle\Event\PreviewUrlCreateEvent;
use Contao\CoreBundle\Exception\AccessDeniedException;
use Contao\CoreBundle\Exception\ResponseException;
use Contao\CoreBundle\HttpKernel\JwtManager;
use Contao\CoreBundle\Util\PackageUtil;
use Knp\Bundle\TimeBundle\DateTimeFormatter;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
/**
* Main back end controller.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class BackendMain extends Backend
{
/**
* Current Ajax object
* @var Ajax
*/
protected $objAjax;
/**
* @var BackendTemplate
*/
protected $Template;
/**
* Initialize the controller
*
* 1. Import the user
* 2. Call the parent constructor
* 3. Authenticate the user
* 4. Load the language files
* DO NOT CHANGE THIS ORDER!
*/
public function __construct()
{
$container = System::getContainer();
/** @var AuthorizationCheckerInterface $authorizationChecker */
$authorizationChecker = $container->get('security.authorization_checker');
$this->import(BackendUser::class, 'User');
parent::__construct();
if (!$authorizationChecker->isGranted('ROLE_USER'))
{
throw new AccessDeniedException('Access denied');
}
// Password change required
if ($this->User->pwChange && !$authorizationChecker->isGranted('ROLE_PREVIOUS_ADMIN'))
{
$this->redirect('contao/password.php');
}
// Two-factor setup required
if (!$this->User->useTwoFactor && $container->getParameter('contao.security.two_factor.enforce_backend') && Input::get('do') != 'security')
{
$this->redirect($container->get('router')->generate('contao_backend', array('do'=>'security')));
}
// Front end redirect
if (Input::get('do') == 'feRedirect')
{
$this->redirectToFrontendPage(Input::get('page'), Input::get('article'));
}
// Debug redirect
if ($this->User->isAdmin && Input::get('do') == 'debug')
{
$objRequest = System::getContainer()->get('request_stack')->getCurrentRequest();
if ($objRequest === null)
{
throw new \RuntimeException('The request stack did not contain a request');
}
$objJwtManager = $objRequest->attributes->get(JwtManager::REQUEST_ATTRIBUTE);
$script = Input::get('enable') ? '/preview.php' : '';
if (!$objJwtManager instanceof JwtManager)
{
if (($qs = $objRequest->getQueryString()) !== null)
{
$qs = '?' . $qs;
}
$this->redirect($script . $objRequest->getPathInfo() . $qs);
}
$strReferer = Input::get('referer') ? '?' . base64_decode(Input::get('referer', true)) : '';
$objResponse = new RedirectResponse($script . $objRequest->getPathInfo() . $strReferer);
if (Input::get('enable') != $container->get('kernel')->isDebug())
{
$objJwtManager->addResponseCookie($objResponse, array('debug' => (bool) Input::get('enable')));
}
throw new ResponseException($objResponse);
}
// Backend user profile redirect
if (Input::get('do') == 'login' && (Input::get('act') != 'edit' && Input::get('id') != $this->User->id))
{
$strUrl = $container->get('router')->generate('contao_backend', array
(
'do' => 'login',
'act' => 'edit',
'id' => $this->User->id,
'ref' => $container->get('request_stack')->getCurrentRequest()->attributes->get('_contao_referer_id'),
'rt' => REQUEST_TOKEN,
));
$this->redirect($strUrl);
}
System::loadLanguageFile('default');
System::loadLanguageFile('modules');
}
/**
* Run the controller and parse the login template
*
* @return Response
*/
public function run()
{
try
{
$version = PackageUtil::getVersion('contao/core-bundle');
}
catch (\OutOfBoundsException $e)
{
$version = PackageUtil::getVersion('contao/contao');
}
$this->Template = new BackendTemplate('be_main');
$this->Template->version = $version;
if (isset($GLOBALS['TL_LANG']['MSC']['version']))
{
$this->Template->version = $GLOBALS['TL_LANG']['MSC']['version'] . ' ' . $version;
}
$this->Template->main = '';
// Ajax request
if ($_POST && Environment::get('isAjaxRequest'))
{
$this->objAjax = new Ajax(Input::post('action'));
$this->objAjax->executePreActions();
}
// Error
if (Input::get('act') == 'error')
{
$this->Template->error = $GLOBALS['TL_LANG']['ERR']['general'];
$this->Template->title = $GLOBALS['TL_LANG']['ERR']['general'];
@trigger_error('Using act=error has been deprecated and will no longer work in Contao 5.0. Throw an exception instead.', E_USER_DEPRECATED);
}
// Welcome screen
elseif (!Input::get('do') && !Input::get('act'))
{
$this->Template->main .= $this->welcomeScreen();
$this->Template->title = $GLOBALS['TL_LANG']['MSC']['home'];
}
// Open a module
elseif (Input::get('do'))
{
$picker = null;
if (isset($_GET['picker']))
{
$picker = System::getContainer()->get('contao.picker.builder')->createFromData(Input::get('picker', true));
if ($picker !== null)
{
if (($menu = $picker->getMenu()) && $menu->count() > 1)
{
$this->Template->pickerMenu = System::getContainer()->get('contao.menu.renderer')->render($menu);
}
}
}
$this->Template->main .= $this->getBackendModule(Input::get('do'), $picker);
$this->Template->title = $this->Template->headline;
}
return $this->output();
}
/**
* Add the welcome screen
*
* @return string
*/
protected function welcomeScreen()
{
System::loadLanguageFile('explain');
$objTemplate = new BackendTemplate('be_welcome');
$objTemplate->messages = Message::generateUnwrapped() . Backend::getSystemMessages();
$objTemplate->loginMsg = $GLOBALS['TL_LANG']['MSC']['firstLogin'];
// Add the login message
if ($this->User->lastLogin > 0)
{
$formatter = new DateTimeFormatter(System::getContainer()->get('translator'));
$diff = $formatter->formatDiff(new \DateTime(date('Y-m-d H:i:s', $this->User->lastLogin)), new \DateTime());
$objTemplate->loginMsg = sprintf(
$GLOBALS['TL_LANG']['MSC']['lastLogin'][1],
'<time title="' . Date::parse(Config::get('datimFormat'), $this->User->lastLogin) . '">' . $diff . '</time>'
);
}
// Add the versions overview
Versions::addToTemplate($objTemplate);
$objTemplate->showDifferences = StringUtil::specialchars(str_replace("'", "\\'", $GLOBALS['TL_LANG']['MSC']['showDifferences']));
$objTemplate->recordOfTable = StringUtil::specialchars(str_replace("'", "\\'", $GLOBALS['TL_LANG']['MSC']['recordOfTable']));
$objTemplate->systemMessages = $GLOBALS['TL_LANG']['MSC']['systemMessages'];
$objTemplate->shortcuts = $GLOBALS['TL_LANG']['MSC']['shortcuts'][0];
$objTemplate->shortcutsLink = $GLOBALS['TL_LANG']['MSC']['shortcuts'][1];
$objTemplate->editElement = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['editElement']);
return $objTemplate->parse();
}
/**
* Output the template file
*
* @return Response
*/
protected function output()
{
// Default headline
if ($this->Template->headline == '')
{
$this->Template->headline = $GLOBALS['TL_LANG']['MSC']['dashboard'];
}
// Default title
if ($this->Template->title == '')
{
$this->Template->title = $this->Template->headline;
}
$container = System::getContainer();
$objSession = $container->get('session');
// File picker reference (backwards compatibility)
if (Input::get('popup') && Input::get('act') != 'show' && ((Input::get('do') == 'page' && $this->User->hasAccess('page', 'modules')) || (Input::get('do') == 'files' && $this->User->hasAccess('files', 'modules'))) && $objSession->get('filePickerRef'))
{
$this->Template->managerHref = ampersand($objSession->get('filePickerRef'));
$this->Template->manager = (strpos($objSession->get('filePickerRef'), 'contao/page?') !== false) ? $GLOBALS['TL_LANG']['MSC']['pagePickerHome'] : $GLOBALS['TL_LANG']['MSC']['filePickerHome'];
}
$referer = null;
if ($request = $container->get('request_stack')->getCurrentRequest())
{
$referer = base64_encode($request->getQueryString());
}
$this->Template->theme = Backend::getTheme();
$this->Template->base = Environment::get('base');
$this->Template->language = $GLOBALS['TL_LANGUAGE'];
$this->Template->title = StringUtil::specialchars(strip_tags($this->Template->title));
$this->Template->host = Backend::getDecodedHostname();
$this->Template->charset = Config::get('characterSet');
$this->Template->account = $GLOBALS['TL_LANG']['MOD']['login'][1];
$this->Template->preview = $GLOBALS['TL_LANG']['MSC']['fePreview'];
$this->Template->previewTitle = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['fePreviewTitle']);
$this->Template->profile = $GLOBALS['TL_LANG']['MSC']['profile'];
$this->Template->canDebug = $this->User->isAdmin;
$this->Template->isDebug = $container->get('kernel')->isDebug();
$this->Template->debugMode = $GLOBALS['TL_LANG']['MSC']['debugMode'];
$this->Template->referer = $referer;
$this->Template->profileTitle = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['profileTitle']);
$this->Template->security = $GLOBALS['TL_LANG']['MSC']['security'];
$this->Template->logout = $GLOBALS['TL_LANG']['MSC']['logoutBT'];
$this->Template->logoutLink = System::getContainer()->get('security.logout_url_generator')->getLogoutUrl();
$this->Template->logoutTitle = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['logoutBTTitle']);
$this->Template->user = $this->User;
$this->Template->username = $GLOBALS['TL_LANG']['MSC']['user'] . ' ' . $this->User->getUsername();
$this->Template->request = ampersand(Environment::get('request'));
$this->Template->top = $GLOBALS['TL_LANG']['MSC']['backToTop'];
$this->Template->modules = $this->User->navigation();
$this->Template->home = $GLOBALS['TL_LANG']['MSC']['home'];
$this->Template->homeTitle = $GLOBALS['TL_LANG']['MSC']['homeTitle'];
$this->Template->backToTop = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['backToTopTitle']);
$this->Template->expandNode = $GLOBALS['TL_LANG']['MSC']['expandNode'];
$this->Template->collapseNode = $GLOBALS['TL_LANG']['MSC']['collapseNode'];
$this->Template->loadingData = $GLOBALS['TL_LANG']['MSC']['loadingData'];
$this->Template->isPopup = Input::get('popup');
$this->Template->systemMessages = $GLOBALS['TL_LANG']['MSC']['systemMessages'];
$this->Template->burger = $GLOBALS['TL_LANG']['MSC']['burgerTitle'];
$this->Template->learnMore = sprintf($GLOBALS['TL_LANG']['MSC']['learnMore'], '<a href="https://contao.org" target="_blank" rel="noreferrer noopener">contao.org</a>');
$this->Template->ref = $container->get('request_stack')->getCurrentRequest()->attributes->get('_contao_referer_id');
$this->Template->menu = $container->get('contao.menu.backend_menu_renderer')->render($container->get('contao.menu.backend_menu_builder')->create());
$this->Template->headerNavigation = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['headerNavigation']);
$strSystemMessages = Backend::getSystemMessages();
$this->Template->systemMessagesCount = substr_count($strSystemMessages, 'class="tl_');
$this->Template->systemErrorMessagesCount = substr_count($strSystemMessages, 'class="tl_error"');
$this->setImpersonatedLogout();
// Front end preview links
if (\defined('CURRENT_ID') && CURRENT_ID != '')
{
if (Input::get('do') == 'page')
{
$this->Template->frontendFile = '?page=' . CURRENT_ID;
}
elseif (Input::get('do') == 'article' && ($objArticle = ArticleModel::findByPk(CURRENT_ID)) !== null)
{
$this->Template->frontendFile = '?page=' . $objArticle->pid;
}
elseif (Input::get('do') != '')
{
$event = new PreviewUrlCreateEvent(Input::get('do'), CURRENT_ID);
$container->get('event_dispatcher')->dispatch(ContaoCoreEvents::PREVIEW_URL_CREATE, $event);
if (($strQuery = $event->getQuery()) !== null)
{
$this->Template->frontendFile = '?' . $strQuery;
}
}
}
return $this->Template->getResponse();
}
/**
* Adjusts the logout link if the current user is impersonated.
*
* @throws \RuntimeException
*/
private function setImpersonatedLogout()
{
$token = System::getContainer()->get('security.token_storage')->getToken();
if (!$token instanceof TokenInterface)
{
return;
}
$impersonatorUser = null;
foreach ($token->getRoles() as $role)
{
if ($role instanceof SwitchUserRole)
{
$impersonatorUser = $role->getSource()->getUsername();
break;
}
}
if (!$impersonatorUser)
{
return;
}
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
if ($request === null)
{
throw new \RuntimeException('The request stack did not contain a request');
}
$firewallMap = System::getContainer()->get('security.firewall.map');
// Generate the "exit impersonation" path from the current request
if (($firewallConfig = $firewallMap->getFirewallConfig($request)) === null || ($switchUserConfig = $firewallConfig->getSwitchUser()) === null)
{
return;
}
// Take the use back to the "users" module
$arrParams = array('do' => 'user', urlencode($switchUserConfig['parameter']) => SwitchUserListener::EXIT_VALUE);
$this->Template->logout = sprintf($GLOBALS['TL_LANG']['MSC']['switchBT'], $impersonatorUser);
$this->Template->logoutLink = System::getContainer()->get('router')->generate('contao_backend', $arrParams);
}
}
class_alias(BackendMain::class, 'BackendMain');