‪TYPO3CMS  9.5
ConditionMatcher.php
Go to the documentation of this file.
1 <?php
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 
22 
30 {
34  protected ‪$context;
35 
36  public function ‪__construct(‪Context ‪$context = null)
37  {
38  $this->context = ‪$context ?? GeneralUtility::makeInstance(Context::class);
39  $this->rootline = $this->‪determineRootline() ?? [];
41  }
42 
43  protected function ‪updateExpressionLanguageVariables(): void
44  {
45  $treeLevel = $this->rootline ? count($this->rootline) - 1 : 0;
46  if ($this->‪isNewPageWithPageId($this->pageId)) {
47  $treeLevel++;
48  }
49  $tree = new \stdClass();
50  $tree->level = $treeLevel;
51  $tree->rootLine = ‪$this->rootline;
52  $tree->rootLineIds = array_column($this->rootline, 'uid');
53 
54  $backendUserAspect = $this->context->getAspect('backend.user');
55  $backend = new \stdClass();
56  $backend->user = new \stdClass();
57  $backend->user->isAdmin = $backendUserAspect->get('isAdmin');
58  $backend->user->isLoggedIn = $backendUserAspect->get('isLoggedIn');
59  $backend->user->userId = $backendUserAspect->get('id');
60  $backend->user->userGroupList = implode(',', $backendUserAspect->get('groupIds'));
61 
62  $this->expressionLanguageResolverVariables = [
63  'tree' => $tree,
64  'backend' => $backend,
65  'page' => ‪BackendUtility::getRecord('pages', $this->pageId ?? $this->‪determinePageId()) ?: [],
66  ];
67  }
68 
77  protected function ‪evaluateCondition($string)
78  {
79  if ($this->‪strictSyntaxEnabled()) {
80  trigger_error('The old condition syntax will be removed in TYPO3 v10.0, use the new expression language. Used condition: [' . $string . '].', E_USER_DEPRECATED);
81  }
82 
83  list($key, $value) = GeneralUtility::trimExplode('=', $string, false, 2);
84  $result = $this->‪evaluateConditionCommon($key, $value);
85  if (is_bool($result)) {
86  return $result;
87  }
88 
89  switch ($key) {
90  case 'usergroup':
91  $groupList = $this->‪getGroupList();
92  $values = GeneralUtility::trimExplode(',', $value, true);
93  foreach ($values as $test) {
94  if ($test === '*' || GeneralUtility::inList($groupList, $test)) {
95  return true;
96  }
97  }
98  break;
99  case 'adminUser':
100  if ($this->‪isUserLoggedIn()) {
101  return !((bool)$value xor $this->‪isAdminUser());
102  }
103  break;
104  case 'treeLevel':
105  $values = GeneralUtility::trimExplode(',', $value, true);
106  $treeLevel = count($this->rootline) - 1;
107  // If a new page is being edited or saved the treeLevel is higher by one:
108  if ($this->‪isNewPageWithPageId($this->pageId)) {
109  $treeLevel++;
110  }
111  foreach ($values as $test) {
112  if ($test == $treeLevel) {
113  return true;
114  }
115  }
116  break;
117  case 'PIDupinRootline':
118  case 'PIDinRootline':
119  $values = GeneralUtility::trimExplode(',', $value, true);
120  if ($key === 'PIDinRootline' || !in_array($this->pageId, $values) || $this->‪isNewPageWithPageId($this->pageId)) {
121  foreach ($values as $test) {
122  foreach ($this->rootline as $rl_dat) {
123  if ($rl_dat['uid'] == $test) {
124  return true;
125  }
126  }
127  }
128  }
129  break;
130  default:
131  $conditionResult = $this->‪evaluateCustomDefinedCondition($string);
132  if ($conditionResult !== null) {
133  return $conditionResult;
134  }
135  }
136 
137  return false;
138  }
139 
147  protected function ‪getVariable($var)
148  {
149  $vars = explode(':', $var, 2);
150  return $this->‪getVariableCommon($vars);
151  }
152 
159  protected function ‪getGroupList()
160  {
161  return $this->‪getBackendUserAuthentication()->groupList;
162  }
163 
173  protected function ‪determinePageId()
174  {
175  ‪$pageId = 0;
176  $editStatement = GeneralUtility::_GP('edit');
177  $commandStatement = GeneralUtility::_GP('cmd');
178  // Determine id from module that was called with an id:
179  if ($id = (int)GeneralUtility::_GP('id')) {
180  ‪$pageId = $id;
181  } elseif (is_array($editStatement)) {
182  $table = key($editStatement);
183  $uidAndAction = current($editStatement);
184  $uid = key($uidAndAction);
185  $action = current($uidAndAction);
186  if ($action === 'edit') {
187  ‪$pageId = $this->‪getPageIdByRecord($table, $uid);
188  } elseif ($action === 'new') {
189  ‪$pageId = $this->‪getPageIdByRecord($table, $uid, true);
190  }
191  } elseif (is_array($commandStatement)) {
192  $table = key($commandStatement);
193  $uidActionAndTarget = current($commandStatement);
194  $uid = key($uidActionAndTarget);
195  $actionAndTarget = current($uidActionAndTarget);
196  $action = key($actionAndTarget);
197  $target = current($actionAndTarget);
198  if ($action === 'delete') {
199  ‪$pageId = $this->‪getPageIdByRecord($table, $uid);
200  } elseif ($action === 'copy' || $action === 'move') {
201  ‪$pageId = $this->‪getPageIdByRecord($table, $target, true);
202  }
203  }
204  return ‪$pageId;
205  }
206 
213  protected function ‪getPage()
214  {
215  ‪$pageId = $this->pageId ?? $this->‪determinePageId();
216  return ‪BackendUtility::getRecord('pages', ‪$pageId) ?? [];
217  }
218 
228  protected function ‪getPageIdByRecord($table, $id, $ignoreTable = false)
229  {
230  ‪$pageId = 0;
231  $id = (int)$id;
232  if ($table && $id) {
233  if (($ignoreTable || $table === 'pages') && $id >= 0) {
234  ‪$pageId = $id;
235  } else {
236  $record = ‪BackendUtility::getRecordWSOL($table, abs($id), '*', '', false);
237  ‪$pageId = $record['pid'];
238  }
239  }
240  return ‪$pageId;
241  }
242 
251  protected function ‪isNewPageWithPageId(‪$pageId)
252  {
253  if (isset(‪$GLOBALS['SOBE']) && ‪$GLOBALS['SOBE'] instanceof EditDocumentController) {
254  ‪$pageId = (int)‪$pageId;
255  $elementsData = ‪$GLOBALS['SOBE']->elementsData;
256  $data = ‪$GLOBALS['SOBE']->data;
257  // If saving a new page record:
258  if (is_array($data) && isset($data['pages']) && is_array($data['pages'])) {
259  foreach ($data['pages'] as $uid => ‪$fields) {
260  if (strpos($uid, 'NEW') === 0 && ‪$fields['pid'] == ‪$pageId) {
261  return true;
262  }
263  }
264  }
265  // If editing a new page record (not saved yet):
266  if (is_array($elementsData)) {
267  foreach ($elementsData as $element) {
268  if ($element['cmd'] === 'new' && $element['table'] === 'pages') {
269  if ($element['pid'] < 0) {
270  $pageRecord = ‪BackendUtility::getRecord('pages', abs($element['pid']), 'pid');
271  $element['pid'] = $pageRecord['pid'];
272  }
273  if ($element['pid'] == ‪$pageId) {
274  return true;
275  }
276  }
277  }
278  }
279  }
280  return false;
281  }
282 
289  protected function ‪determineRootline()
290  {
291  ‪$pageId = $this->pageId ?? $this->‪determinePageId();
293  }
294 
301  protected function ‪getUserId()
302  {
303  return $this->‪getBackendUserAuthentication()->user['uid'];
304  }
305 
312  protected function ‪isUserLoggedIn()
313  {
314  return (bool)$this->‪getBackendUserAuthentication()->user['uid'];
315  }
316 
323  protected function ‪isAdminUser()
324  {
325  return $this->‪getBackendUserAuthentication()->‪isAdmin();
326  }
327 
332  protected function ‪getBackendUserAuthentication()
333  {
334  return ‪$GLOBALS['BE_USER'] ?? null;
335  }
336 }
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\updateExpressionLanguageVariables
‪updateExpressionLanguageVariables()
Definition: ConditionMatcher.php:42
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\isNewPageWithPageId
‪bool isNewPageWithPageId($pageId)
Definition: ConditionMatcher.php:250
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getVariable
‪mixed getVariable($var)
Definition: ConditionMatcher.php:146
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching
Definition: ConditionMatcher.php:2
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\determinePageId
‪int determinePageId()
Definition: ConditionMatcher.php:172
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\evaluateCustomDefinedCondition
‪bool null evaluateCustomDefinedCondition($condition)
Definition: AbstractConditionMatcher.php:490
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher
Definition: ConditionMatcher.php:30
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:294
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\$pageId
‪int $pageId
Definition: AbstractConditionMatcher.php:44
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getBackendUserAuthentication
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUserAuthentication()
Definition: ConditionMatcher.php:331
‪TYPO3\CMS\Backend\Utility\BackendUtility\BEgetRootLine
‪static array BEgetRootLine($uid, $clause='', $workspaceOL=false, array $additionalFields=[])
Definition: BackendUtility.php:374
‪$fields
‪$fields
Definition: pages.php:4
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:49
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\isUserLoggedIn
‪bool isUserLoggedIn()
Definition: ConditionMatcher.php:311
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\determineRootline
‪array determineRootline()
Definition: ConditionMatcher.php:288
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\evaluateConditionCommon
‪bool null evaluateConditionCommon($key, $value)
Definition: AbstractConditionMatcher.php:336
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\$context
‪Context $context
Definition: ConditionMatcher.php:33
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getUserId
‪int getUserId()
Definition: ConditionMatcher.php:300
‪TYPO3\CMS\Backend\Controller\EditDocumentController
Definition: EditDocumentController.php:64
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\isAdminUser
‪bool isAdminUser()
Definition: ConditionMatcher.php:322
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher
Definition: AbstractConditionMatcher.php:37
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getPage
‪array getPage()
Definition: ConditionMatcher.php:212
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\$rootline
‪array $rootline
Definition: AbstractConditionMatcher.php:50
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getPageIdByRecord
‪int getPageIdByRecord($table, $id, $ignoreTable=false)
Definition: ConditionMatcher.php:227
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\evaluateCondition
‪bool evaluateCondition($string)
Definition: ConditionMatcher.php:76
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\__construct
‪__construct(Context $context=null)
Definition: ConditionMatcher.php:35
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:72
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecordWSOL
‪static array getRecordWSOL( $table, $uid, $fields=' *', $where='', $useDeleteClause=true, $unsetMovePointers=false)
Definition: BackendUtility.php:174
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:130
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\initializeExpressionLanguageResolver
‪initializeExpressionLanguageResolver()
Definition: AbstractConditionMatcher.php:74
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\strictSyntaxEnabled
‪bool strictSyntaxEnabled()
Definition: AbstractConditionMatcher.php:93
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher\getGroupList
‪string getGroupList()
Definition: ConditionMatcher.php:158
‪TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher\getVariableCommon
‪mixed getVariableCommon(array $vars)
Definition: AbstractConditionMatcher.php:564