[BUGFIX] Fix fatal in web->functions for page without access
[Packages/TYPO3.CMS.git] / typo3 / sysext / func / Classes / Controller / PageFunctionsController.php
1 <?php
2 namespace TYPO3\CMS\Func\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 Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Backend\Template\ModuleTemplate;
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
21 use TYPO3\CMS\Core\Imaging\Icon;
22 use TYPO3\CMS\Core\Imaging\IconFactory;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Fluid\View\StandaloneView;
25 use TYPO3\CMS\Fluid\ViewHelpers\Be\InfoboxViewHelper;
26
27 /**
28 * Script Class for the Web > Functions module
29 * This class creates the framework to which other extensions can connect their sub-modules
30 */
31 class PageFunctionsController extends \TYPO3\CMS\Backend\Module\BaseScriptClass
32 {
33 /**
34 * @var array
35 * @internal
36 */
37 public $pageinfo;
38
39 /**
40 * ModuleTemplate Container
41 *
42 * @var ModuleTemplate
43 */
44 protected $moduleTemplate;
45
46 /**
47 * Document Template Object
48 *
49 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
50 */
51 public $doc;
52
53 /**
54 * The name of the module
55 *
56 * @var string
57 */
58 protected $moduleName = 'web_func';
59
60 /**
61 * @var IconFactory
62 */
63 protected $iconFactory;
64
65 /**
66 * Constructor
67 */
68 public function __construct()
69 {
70 $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
71 $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
72 $this->getLanguageService()->includeLLFile('EXT:lang/locallang_mod_web_func.xlf');
73 $this->MCONF = array(
74 'name' => $this->moduleName,
75 );
76 }
77
78 /**
79 * Injects the request object for the current request or subrequest
80 * Then checks for module functions that have hooked in, and renders menu etc.
81 *
82 * @param ServerRequestInterface $request the current request
83 * @param ResponseInterface $response
84 * @return ResponseInterface the response with the content
85 */
86 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
87 {
88 $GLOBALS['SOBE'] = $this;
89 $this->init();
90
91 // Checking for first level external objects
92 $this->checkExtObj();
93
94 // Checking second level external objects
95 $this->checkSubExtObj();
96 $this->main();
97
98 $this->moduleTemplate->setContent($this->content);
99
100 $response->getBody()->write($this->moduleTemplate->renderContent());
101 return $response;
102 }
103
104 /**
105 * Initialize module header etc and call extObjContent function
106 *
107 * @return void
108 */
109 public function main()
110 {
111 // Access check...
112 // The page will show only if there is a valid page and if this page may be viewed by the user
113 $this->pageinfo = BackendUtility::readPageAccess($this->id, $this->perms_clause);
114 if ($this->pageinfo) {
115 $this->moduleTemplate->getDocHeaderComponent()->setMetaInformation($this->pageinfo);
116 }
117 $access = is_array($this->pageinfo);
118 // We keep this here, in case somebody relies on the old doc being here
119 $this->doc = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
120 // Main
121 if ($this->id && $access) {
122 // JavaScript
123 $this->moduleTemplate->addJavaScriptCode(
124 'WebFuncInLineJS',
125 'if (top.fsMod) top.fsMod.recentIds["web"] = ' . (int)$this->id . ';');
126 // Setting up the context sensitive menu:
127 $this->moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/ClickMenu');
128 $this->content .= '<form action="' . htmlspecialchars(BackendUtility::getModuleUrl('web_func')) . '" id="PageFunctionsController" method="post"><input type="hidden" name="id" value="' . htmlspecialchars($this->id) . '" />';
129 $vContent = $this->moduleTemplate->getVersionSelector($this->id, true);
130 if ($vContent) {
131 $this->content .= '<div>' . $vContent . '</div>';
132 }
133 $this->extObjContent();
134 // Setting up the buttons and markers for docheader
135 $this->getButtons();
136 $this->generateMenu();
137 $this->content .= '</form>';
138 } else {
139 // If no access or if ID == zero
140 $title = $this->getLanguageService()->getLL('title');
141 $message = $this->getLanguageService()->getLL('clickAPage_content');
142 $view = GeneralUtility::makeInstance(StandaloneView::class);
143 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:func/Resources/Private/Templates/InfoBox.html'));
144 $view->assignMultiple(array(
145 'title' => $title,
146 'message' => $message,
147 'state' => InfoboxViewHelper::STATE_INFO
148 ));
149 $this->content = $view->render();
150 // Setting up the buttons and markers for docheader
151 $this->getButtons();
152 }
153 }
154
155 /**
156 * Generates the menu based on $this->MOD_MENU
157 *
158 * @throws \InvalidArgumentException
159 */
160 protected function generateMenu()
161 {
162 $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
163 $menu->setIdentifier('WebFuncJumpMenu');
164 foreach ($this->MOD_MENU['function'] as $controller => $title) {
165 $item = $menu
166 ->makeMenuItem()
167 ->setHref(
168 BackendUtility::getModuleUrl(
169 $this->moduleName,
170 [
171 'id' => $this->id,
172 'SET' => [
173 'function' => $controller
174 ]
175 ]
176 )
177 )
178 ->setTitle($title);
179 if ($controller === $this->MOD_SETTINGS['function']) {
180 $item->setActive(true);
181 }
182 $menu->addMenuItem($item);
183 }
184 $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
185 }
186
187 /**
188 * Print module content (from $this->content)
189 *
190 * @return void
191 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
192 */
193 public function printContent()
194 {
195 GeneralUtility::logDeprecatedFunction();
196 echo $this->content;
197 }
198
199 /**
200 * Create the panel of buttons for submitting the form or otherwise perform operations.
201 */
202 protected function getButtons()
203 {
204 $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
205 // CSH
206 $cshButton = $buttonBar->makeHelpButton()
207 ->setModuleName('_MOD_web_func')
208 ->setFieldName('');
209 $buttonBar->addButton($cshButton);
210 if ($this->id && is_array($this->pageinfo)) {
211 // View page
212 $viewButton = $buttonBar->makeLinkButton()
213 ->setOnClick(BackendUtility::viewOnClick($this->pageinfo['uid'], '', BackendUtility::BEgetRootLine($this->pageinfo['uid'])))
214 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.showPage', true))
215 ->setIcon($this->iconFactory->getIcon('actions-document-view', Icon::SIZE_SMALL))
216 ->setHref('#');
217 $buttonBar->addButton($viewButton);
218 // Shortcut
219 $shortcutButton = $buttonBar->makeShortcutButton()
220 ->setModuleName($this->moduleName)
221 ->setGetVariables(['id', 'edit_record', 'pointer', 'new_unique_uid', 'search_field', 'search_levels', 'showLimit'])
222 ->setSetVariables(array_keys($this->MOD_MENU));
223 $buttonBar->addButton($shortcutButton);
224 }
225 }
226
227 /**
228 * Returns LanguageService
229 *
230 * @return \TYPO3\CMS\Lang\LanguageService
231 */
232 protected function getLanguageService()
233 {
234 return $GLOBALS['LANG'];
235 }
236
237 /**
238 * Returns the current BE user.
239 *
240 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
241 */
242 protected function getBackendUser()
243 {
244 return $GLOBALS['BE_USER'];
245 }
246 }