-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
BackendMain.php
280 lines (237 loc) · 9.47 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
<?php
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/
namespace Contao;
use Contao\CoreBundle\Event\ContaoCoreEvents;
use Contao\CoreBundle\Event\PreviewUrlCreateEvent;
use Contao\CoreBundle\Exception\AccessDeniedException;
use Knp\Bundle\TimeBundle\DateTimeFormatter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Main back end controller.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class BackendMain extends \Backend
{
/**
* Current Ajax object
* @var Ajax
*/
protected $objAjax;
/**
* @var BackendTemplate|object
*/
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()
{
$this->import('BackendUser', 'User');
parent::__construct();
if (!\System::getContainer()->get('security.authorization_checker')->isGranted('ROLE_USER'))
{
throw new AccessDeniedException('Access denied');
}
// Password change required
if ($this->User->pwChange)
{
$objSession = $this->Database->prepare("SELECT su FROM tl_session WHERE hash=?")
->execute($this->getSessionHash('BE_USER_AUTH'));
if (!$objSession->su)
{
$this->redirect('contao/password.php');
}
}
// Front end redirect
if (\Input::get('do') == 'feRedirect')
{
$this->redirectToFrontendPage(\Input::get('page'), \Input::get('article'));
}
\System::loadLanguageFile('default');
\System::loadLanguageFile('modules');
}
/**
* Run the controller and parse the login template
*
* @return Response
*/
public function run()
{
$packages = System::getContainer()->getParameter('kernel.packages');
$this->Template = new \BackendTemplate('be_main');
$this->Template->version = $GLOBALS['TL_LANG']['MSC']['version'] . ' ' . $packages['contao/core-bundle'];
$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');
/** @var BackendTemplate|object $objTemplate */
$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->welcome = sprintf($GLOBALS['TL_LANG']['MSC']['welcomeTo'], \Config::get('websiteTitle'));
$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 = \Config::get('websiteTitle');
}
// Default title
if ($this->Template->title == '')
{
$this->Template->title = $this->Template->headline;
}
/** @var SessionInterface $objSession */
$objSession = \System::getContainer()->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'];
}
// Website title
if (\Config::get('websiteTitle') != 'Contao Open Source CMS')
{
$this->Template->websiteTitle = \Config::get('websiteTitle');
}
$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->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->profileTitle = \StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['profileTitle']);
$this->Template->pageOffset = \Input::cookie('BE_PAGE_OFFSET');
$this->Template->logout = $GLOBALS['TL_LANG']['MSC']['logoutBT'];
$this->Template->logoutTitle = \StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['logoutBTTitle']);
$this->Template->backendModules = $GLOBALS['TL_LANG']['MSC']['backendModules'];
$this->Template->user = $this->User;
$this->Template->username = $GLOBALS['TL_LANG']['MSC']['user'] . ' ' . $GLOBALS['TL_USERNAME'];
$this->Template->skipNavigation = \StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['skipNavigation']);
$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">contao.org</a>');
$this->Template->ref = \System::getContainer()->get('request_stack')->getCurrentRequest()->attributes->get('_contao_referer_id');
$strSystemMessages = \Backend::getSystemMessages();
$this->Template->systemMessagesCount = substr_count($strSystemMessages, 'class="tl_');
$this->Template->systemErrorMessagesCount = substr_count($strSystemMessages, 'class="tl_error"');
// 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);
\System::getContainer()->get('event_dispatcher')->dispatch(ContaoCoreEvents::PREVIEW_URL_CREATE, $event);
if (($strQuery = $event->getQuery()) !== null)
{
$this->Template->frontendFile = '?' . $strQuery;
}
}
}
return $this->Template->getResponse();
}
}