a3b93f46706ba926672c29852b8088d238256323
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Configuration / TypoScript / ConditionMatching / ConditionMatcher.php
1 <?php
2 namespace TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching;
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\Controller\EditDocumentController;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Matching TypoScript conditions for backend disposal.
24 *
25 * Used with the TypoScript parser.
26 * Matches browserinfo, IPnumbers for use with templates
27 */
28 class ConditionMatcher extends AbstractConditionMatcher
29 {
30 /**
31 * Constructor for this class
32 */
33 public function __construct()
34 {
35 }
36
37 /**
38 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
39 *
40 * @param string $string The condition to match against its criteria.
41 * @return bool Whether the condition matched
42 * @see \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::parse()
43 */
44 protected function evaluateCondition($string)
45 {
46 list($key, $value) = GeneralUtility::trimExplode('=', $string, false, 2);
47 $result = $this->evaluateConditionCommon($key, $value);
48 if (is_bool($result)) {
49 return $result;
50 }
51 switch ($key) {
52 case 'usergroup':
53 $groupList = $this->getGroupList();
54 $values = GeneralUtility::trimExplode(',', $value, true);
55 foreach ($values as $test) {
56 if ($test === '*' || GeneralUtility::inList($groupList, $test)) {
57 return true;
58 }
59 }
60 break;
61 case 'adminUser':
62 if ($this->isUserLoggedIn()) {
63 return !((bool)$value xor $this->isAdminUser());
64 }
65 break;
66 case 'treeLevel':
67 $values = GeneralUtility::trimExplode(',', $value, true);
68 $treeLevel = count($this->rootline) - 1;
69 // If a new page is being edited or saved the treeLevel is higher by one:
70 if ($this->isNewPageWithPageId($this->pageId)) {
71 $treeLevel++;
72 }
73 foreach ($values as $test) {
74 if ($test == $treeLevel) {
75 return true;
76 }
77 }
78 break;
79 case 'PIDupinRootline':
80 case 'PIDinRootline':
81 $values = GeneralUtility::trimExplode(',', $value, true);
82 if ($key === 'PIDinRootline' || !in_array($this->pageId, $values) || $this->isNewPageWithPageId($this->pageId)) {
83 foreach ($values as $test) {
84 foreach ($this->rootline as $rl_dat) {
85 if ($rl_dat['uid'] == $test) {
86 return true;
87 }
88 }
89 }
90 }
91 break;
92 default:
93 $conditionResult = $this->evaluateCustomDefinedCondition($string);
94 if ($conditionResult !== null) {
95 return $conditionResult;
96 }
97 }
98
99 return false;
100 }
101
102 /**
103 * Returns GP / ENV vars
104 *
105 * @param string $var Identifier
106 * @return mixed The value of the variable pointed to or NULL if variable did not exist
107 * @access private
108 */
109 protected function getVariable($var)
110 {
111 $vars = explode(':', $var, 2);
112 return $this->getVariableCommon($vars);
113 }
114
115 /**
116 * Get the usergroup list of the current user.
117 *
118 * @return string The usergroup list of the current user
119 */
120 protected function getGroupList()
121 {
122 return $this->getBackendUserAuthentication()->groupList;
123 }
124
125 /**
126 * Tries to determine the ID of the page currently processed.
127 * When User/Group TS-Config is parsed when no specific page is handled
128 * (i.e. in the Extension Manager, etc.) this function will return "0", so that
129 * the accordant conditions (e.g. PIDinRootline) will return "FALSE"
130 *
131 * @return int The determined page id or otherwise 0
132 */
133 protected function determinePageId()
134 {
135 $pageId = 0;
136 $editStatement = GeneralUtility::_GP('edit');
137 $commandStatement = GeneralUtility::_GP('cmd');
138 // Determine id from module that was called with an id:
139 if ($id = (int)GeneralUtility::_GP('id')) {
140 $pageId = $id;
141 } elseif (is_array($editStatement)) {
142 $table = key($editStatement);
143 $uidAndAction = current($editStatement);
144 $uid = key($uidAndAction);
145 $action = current($uidAndAction);
146 if ($action === 'edit') {
147 $pageId = $this->getPageIdByRecord($table, $uid);
148 } elseif ($action === 'new') {
149 $pageId = $this->getPageIdByRecord($table, $uid, true);
150 }
151 } elseif (is_array($commandStatement)) {
152 $table = key($commandStatement);
153 $uidActionAndTarget = current($commandStatement);
154 $uid = key($uidActionAndTarget);
155 $actionAndTarget = current($uidActionAndTarget);
156 $action = key($actionAndTarget);
157 $target = current($actionAndTarget);
158 if ($action === 'delete') {
159 $pageId = $this->getPageIdByRecord($table, $uid);
160 } elseif ($action === 'copy' || $action === 'move') {
161 $pageId = $this->getPageIdByRecord($table, $target, true);
162 }
163 }
164 return $pageId;
165 }
166
167 /**
168 * Gets the properties for the current page.
169 *
170 * @return array The properties for the current page.
171 */
172 protected function getPage()
173 {
174 $pageId = isset($this->pageId) ? $this->pageId : $this->determinePageId();
175 return BackendUtility::getRecord('pages', $pageId);
176 }
177
178 /**
179 * Gets the page id by a record.
180 *
181 * @param string $table Name of the table
182 * @param int $id Id of the accordant record
183 * @param bool $ignoreTable Whether to ignore the page, if TRUE a positive
184 * @return int Id of the page the record is persisted on
185 */
186 protected function getPageIdByRecord($table, $id, $ignoreTable = false)
187 {
188 $pageId = 0;
189 $id = (int)$id;
190 if ($table && $id) {
191 if (($ignoreTable || $table === 'pages') && $id >= 0) {
192 $pageId = $id;
193 } else {
194 $record = BackendUtility::getRecordWSOL($table, abs($id), '*', '', false);
195 $pageId = $record['pid'];
196 }
197 }
198 return $pageId;
199 }
200
201 /**
202 * Determine if record of table 'pages' with the given $pid is currently created in TCEforms.
203 * This information is required for conditions in BE for PIDupinRootline.
204 *
205 * @param int $pageId The pid the check for as parent page
206 * @return bool TRUE if the is currently a new page record being edited with $pid as uid of the parent page
207 */
208 protected function isNewPageWithPageId($pageId)
209 {
210 if (isset($GLOBALS['SOBE']) && $GLOBALS['SOBE'] instanceof EditDocumentController) {
211 $pageId = (int)$pageId;
212 $elementsData = $GLOBALS['SOBE']->elementsData;
213 $data = $GLOBALS['SOBE']->data;
214 // If saving a new page record:
215 if (is_array($data) && isset($data['pages']) && is_array($data['pages'])) {
216 foreach ($data['pages'] as $uid => $fields) {
217 if (strpos($uid, 'NEW') === 0 && $fields['pid'] == $pageId) {
218 return true;
219 }
220 }
221 }
222 // If editing a new page record (not saved yet):
223 if (is_array($elementsData)) {
224 foreach ($elementsData as $element) {
225 if ($element['cmd'] === 'new' && $element['table'] === 'pages') {
226 if ($element['pid'] < 0) {
227 $pageRecord = BackendUtility::getRecord('pages', abs($element['pid']), 'pid');
228 $element['pid'] = $pageRecord['pid'];
229 }
230 if ($element['pid'] == $pageId) {
231 return true;
232 }
233 }
234 }
235 }
236 }
237 return false;
238 }
239
240 /**
241 * Determines the rootline for the current page.
242 *
243 * @return array The rootline for the current page.
244 */
245 protected function determineRootline()
246 {
247 $pageId = isset($this->pageId) ? $this->pageId : $this->determinePageId();
248 return BackendUtility::BEgetRootLine($pageId, '', true);
249 }
250
251 /**
252 * Get the id of the current user.
253 *
254 * @return int The id of the current user
255 */
256 protected function getUserId()
257 {
258 return $this->getBackendUserAuthentication()->user['uid'];
259 }
260
261 /**
262 * Determines if a user is logged in.
263 *
264 * @return bool Determines if a user is logged in
265 */
266 protected function isUserLoggedIn()
267 {
268 return (bool)$this->getBackendUserAuthentication()->user['uid'];
269 }
270
271 /**
272 * Determines whether the current user is admin.
273 *
274 * @return bool Whether the current user is admin
275 */
276 protected function isAdminUser()
277 {
278 return $this->getBackendUserAuthentication()->isAdmin();
279 }
280
281 /**
282 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
283 */
284 protected function getBackendUserAuthentication()
285 {
286 return $GLOBALS['BE_USER'];
287 }
288 }