[!!!][TASK] Move pages.url_scheme to compatibility7
[Packages/TYPO3.CMS.git] / typo3 / sysext / viewpage / Classes / Controller / ViewModuleController.php
1 <?php
2 namespace TYPO3\CMS\Viewpage\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Backend\View\BackendTemplateView;
20 use TYPO3\CMS\Core\Database\ConnectionPool;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
22 use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
23 use TYPO3\CMS\Core\Imaging\Icon;
24 use TYPO3\CMS\Core\Page\PageRenderer;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
27 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
28
29 /**
30 * Controller for viewing the frontend
31 */
32 class ViewModuleController extends ActionController
33 {
34 /**
35 * @var string
36 */
37 protected $defaultViewObjectName = BackendTemplateView::class;
38
39 /**
40 * @var BackendTemplateView
41 */
42 protected $view;
43
44 /**
45 * Set up the doc header properly here
46 *
47 * @param ViewInterface $view
48 */
49 protected function initializeView(ViewInterface $view)
50 {
51 /** @var BackendTemplateView $view */
52 parent::initializeView($view);
53 $this->registerButtons();
54 }
55
56 /**
57 * Registers the docheader buttons
58 */
59 protected function registerButtons()
60 {
61 $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
62 $showButton = $buttonBar->makeLinkButton()
63 ->setHref($this->getTargetUrl())
64 ->setOnClick('window.open(this.href, \'newTYPO3frontendWindow\').focus();return false;')
65 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.showPage'))
66 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-document-view', Icon::SIZE_SMALL));
67 $buttonBar->addButton($showButton);
68
69 $refreshButton = $buttonBar->makeLinkButton()
70 ->setHref('javascript:document.getElementById(\'tx_viewpage_iframe\').contentWindow.location.reload(true);')
71 ->setTitle($this->getLanguageService()->sL('LLL:EXT:viewpage/Resources/Private/Language/locallang.xlf:refreshPage'))
72 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon('actions-refresh', Icon::SIZE_SMALL));
73 $buttonBar->addButton($refreshButton, ButtonBar::BUTTON_POSITION_RIGHT, 1);
74
75 $currentRequest = $this->request;
76 $moduleName = $currentRequest->getPluginName();
77 $getVars = $this->request->getArguments();
78 $extensionName = $currentRequest->getControllerExtensionName();
79 if (count($getVars) === 0) {
80 $modulePrefix = strtolower('tx_' . $extensionName . '_' . $moduleName);
81 $getVars = ['id', 'M', $modulePrefix];
82 }
83 $shortcutButton = $buttonBar->makeShortcutButton()
84 ->setModuleName($moduleName)
85 ->setGetVariables($getVars);
86 $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT, 2);
87 }
88
89 /**
90 * Gets called before each action
91 *
92 * @return void
93 */
94 public function initializeAction()
95 {
96 $this->getLanguageService()->includeLLFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
97 $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
98 $pageRenderer->addInlineLanguageLabelFile('EXT:viewpage/Resources/Private/Language/locallang.xlf');
99 }
100
101 /**
102 * Show selected page from pagetree in iframe
103 *
104 * @return void
105 */
106 public function showAction()
107 {
108 $this->view->assign('widths', $this->getPreviewFrameWidths());
109 $this->view->assign('url', $this->getTargetUrl());
110 $this->view->assign('languages', $this->getPreviewLanguages());
111 $this->view->assign('pageTitle', $this->getPageTitle());
112 }
113
114 /**
115 * Determine the url to view
116 *
117 * @return string
118 */
119 protected function getTargetUrl()
120 {
121 $pageIdToShow = (int)GeneralUtility::_GP('id');
122
123 $permissionClause = $this->getBackendUser()->getPagePermsClause(1);
124 $pageRecord = BackendUtility::readPageAccess($pageIdToShow, $permissionClause);
125 if ($pageRecord) {
126 $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($pageRecord);
127
128 $adminCommand = $this->getAdminCommand($pageIdToShow);
129 $domainName = $this->getDomainName($pageIdToShow);
130 $languageParameter = $this->getLanguageParameter();
131 // Mount point overlay: Set new target page id and mp parameter
132 /** @var \TYPO3\CMS\Frontend\Page\PageRepository $sysPage */
133 $sysPage = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Page\PageRepository::class);
134 $sysPage->init(false);
135 $mountPointMpParameter = '';
136 $finalPageIdToShow = $pageIdToShow;
137 $mountPointInformation = $sysPage->getMountPointInfo($pageIdToShow);
138 if ($mountPointInformation && $mountPointInformation['overlay']) {
139 // New page id
140 $finalPageIdToShow = $mountPointInformation['mount_pid'];
141 $mountPointMpParameter = '&MP=' . $mountPointInformation['MPvar'];
142 }
143 // Modify relative path to protocol with host if domain record is given
144 $protocolAndHost = '..';
145 if ($domainName) {
146 // TCEMAIN.previewDomain can contain the protocol, check prevents double protocol URLs
147 if (strpos($domainName, '://') !== false) {
148 $protocolAndHost = $domainName;
149 } else {
150 $protocol = GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https' : 'http';
151 $protocolAndHost = $protocol . '://' . $domainName;
152 }
153 }
154 return $protocolAndHost . '/index.php?id=' . $finalPageIdToShow . $this->getTypeParameterIfSet($finalPageIdToShow) . $mountPointMpParameter . $adminCommand . $languageParameter;
155 } else {
156 return '#';
157 }
158 }
159
160 /**
161 * Get admin command
162 *
163 * @param int $pageId
164 * @return string
165 */
166 protected function getAdminCommand($pageId)
167 {
168 // The page will show only if there is a valid page and if this page may be viewed by the user
169 $pageinfo = BackendUtility::readPageAccess($pageId, $this->getBackendUser()->getPagePermsClause(1));
170 $addCommand = '';
171 if (is_array($pageinfo)) {
172 $addCommand = '&ADMCMD_editIcons=1' . BackendUtility::ADMCMD_previewCmds($pageinfo);
173 }
174 return $addCommand;
175 }
176
177 /**
178 * With page TS config it is possible to force a specific type id via mod.web_view.type
179 * for a page id or a page tree.
180 * The method checks if a type is set for the given id and returns the additional GET string.
181 *
182 * @param int $pageId
183 * @return string
184 */
185 protected function getTypeParameterIfSet($pageId)
186 {
187 $typeParameter = '';
188 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
189 $typeId = (int)$modTSconfig['properties']['type'];
190 if ($typeId > 0) {
191 $typeParameter = '&type=' . $typeId;
192 }
193 return $typeParameter;
194 }
195
196 /**
197 * Get domain name for requested page id
198 *
199 * @param int $pageId
200 * @return string|NULL Domain name from first sys_domains-Record or from TCEMAIN.previewDomain, NULL if neither is configured
201 */
202 protected function getDomainName($pageId)
203 {
204 $previewDomainConfig = $this->getBackendUser()->getTSConfig('TCEMAIN.previewDomain', BackendUtility::getPagesTSconfig($pageId));
205 if ($previewDomainConfig['value']) {
206 $domain = $previewDomainConfig['value'];
207 } else {
208 $domain = BackendUtility::firstDomainRecord(BackendUtility::BEgetRootLine($pageId));
209 }
210 return $domain;
211 }
212
213 /**
214 * Get available widths for preview frame
215 *
216 * @return array
217 */
218 protected function getPreviewFrameWidths()
219 {
220 $pageId = (int)GeneralUtility::_GP('id');
221 $modTSconfig = BackendUtility::getModTSconfig($pageId, 'mod.web_view');
222 $widths = [
223 '100%|100%' => $this->getLanguageService()->getLL('autoSize')
224 ];
225 if (is_array($modTSconfig['properties']['previewFrameWidths.'])) {
226 foreach ($modTSconfig['properties']['previewFrameWidths.'] as $item => $conf) {
227 $label = '';
228
229 $width = substr($item, 0, -1);
230 $data = ['width' => $width];
231 $label .= $width . 'px ';
232
233 //if height is set
234 if (isset($conf['height'])) {
235 $label .= ' × ' . $conf['height'] . 'px ';
236 $data['height'] = $conf['height'];
237 }
238
239 if (substr($conf['label'], 0, 4) !== 'LLL:') {
240 $label .= $conf['label'];
241 } else {
242 $label .= $this->getLanguageService()->sL(trim($conf['label']));
243 }
244 $value = ($data['width'] ?: '100%') . '|' . ($data['height'] ?: '100%');
245 $widths[$value] = $label;
246 }
247 }
248 return $widths;
249 }
250
251 /**
252 * Returns the preview languages
253 *
254 * @return array
255 */
256 protected function getPreviewLanguages()
257 {
258 $pageIdToShow = (int)GeneralUtility::_GP('id');
259 $modSharedTSconfig = BackendUtility::getModTSconfig($pageIdToShow, 'mod.SHARED');
260 if ($modSharedTSconfig['properties']['view.']['disableLanguageSelector'] === '1') {
261 return [];
262 }
263 $languages = [
264 0 => isset($modSharedTSconfig['properties']['defaultLanguageLabel'])
265 ? $modSharedTSconfig['properties']['defaultLanguageLabel'] . ' (' . $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage') . ')'
266 : $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_mod_web_list.xlf:defaultLanguage')
267 ];
268 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_language');
269 $queryBuilder->getRestrictions()
270 ->removeAll()
271 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
272
273 if (!$this->getBackendUser()->isAdmin()) {
274 $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
275 }
276
277 $result = $queryBuilder->select('sys_language.uid', 'sys_language.title')
278 ->from('sys_language')
279 ->join(
280 'sys_language',
281 'pages_language_overlay',
282 'o',
283 $queryBuilder->expr()->eq('o.sys_language_uid', $queryBuilder->quoteIdentifier('sys_language.uid'))
284 )
285 ->where(
286 $queryBuilder->expr()->eq(
287 'o.pid',
288 $queryBuilder->createNamedParameter($pageIdToShow, \PDO::PARAM_INT)
289 )
290 )
291 ->groupBy('sys_language.uid', 'sys_language.title')
292 ->orderBy('sys_language.sorting')
293 ->execute();
294
295 while ($row = $result->fetch()) {
296 if ($this->getBackendUser()->checkLanguageAccess($row['uid'])) {
297 $languages[$row['uid']] = $row['title'];
298 }
299 }
300 return $languages;
301 }
302
303 /**
304 * Returns the page title
305 *
306 * @return string
307 */
308 protected function getPageTitle()
309 {
310 $pageIdToShow = (int)GeneralUtility::_GP('id');
311 $pageRecord = BackendUtility::getRecord('pages', $pageIdToShow);
312 $pageRecordTitle = is_array($pageRecord)
313 ? BackendUtility::getRecordTitle('pages', $pageRecord)
314 : '';
315
316 return $pageRecordTitle;
317 }
318
319 /**
320 * Gets the L parameter from the user session
321 *
322 * @return string
323 */
324 protected function getLanguageParameter()
325 {
326 $states = $this->getBackendUser()->uc['moduleData']['web_view']['States'];
327 $languages = $this->getPreviewLanguages();
328 $languageParameter = '';
329 if (isset($states['languageSelectorValue']) && isset($languages[$states['languageSelectorValue']])) {
330 $languageParameter = '&L=' . (int)$states['languageSelectorValue'];
331 }
332 return $languageParameter;
333 }
334
335 /**
336 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
337 */
338 protected function getBackendUser()
339 {
340 return $GLOBALS['BE_USER'];
341 }
342
343 /**
344 * @return \TYPO3\CMS\Lang\LanguageService
345 */
346 protected function getLanguageService()
347 {
348 return $GLOBALS['LANG'];
349 }
350 }