‪TYPO3CMS  10.4
Typo3ConditionFunctionsProvider.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
21 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
28 
33 class ‪Typo3ConditionFunctionsProvider implements ExpressionFunctionProviderInterface
34 {
38  public function ‪getFunctions()
39  {
40  return [
41  $this->‪getLoginUserFunction(),
42  $this->‪getTSFEFunction(),
43  $this->‪getUsergroupFunction(),
44  $this->‪getSessionFunction(),
45  $this->‪getSiteFunction(),
47  ];
48  }
49 
50  protected function ‪getLoginUserFunction(): ExpressionFunction
51  {
52  return new ExpressionFunction('loginUser', function () {
53  // Not implemented, we only use the evaluator
54  }, function ($arguments, $str) {
55  $user = $arguments['frontend']->user ?? $arguments['backend']->user;
56  if ($user->isLoggedIn) {
57  foreach (‪GeneralUtility::trimExplode(',', $str, true) as $test) {
58  if ($test === '*' || (string)$user->userId === (string)$test) {
59  return true;
60  }
61  }
62  }
63  return false;
64  });
65  }
66 
67  protected function ‪getTSFEFunction(): ExpressionFunction
68  {
69  return new ExpressionFunction('getTSFE', function () {
70  // Not implemented, we only use the evaluator
71  }, function ($arguments) {
72  if ((‪$GLOBALS['TSFE'] ?? null) instanceof ‪TypoScriptFrontendController) {
73  return ‪$GLOBALS['TSFE'];
74  }
75  throw new ‪MissingTsfeException('TSFE is not available in this context', 1578831632);
76  });
77  }
78 
79  protected function ‪getUsergroupFunction(): ExpressionFunction
80  {
81  return new ExpressionFunction('usergroup', function () {
82  // Not implemented, we only use the evaluator
83  }, function ($arguments, $str) {
84  $user = $arguments['frontend']->user ?? $arguments['backend']->user;
85  $groupList = $user->userGroupList ?? '';
86  // '0,-1' is the default usergroups string when not logged in!
87  if ($groupList !== '0,-1' && $groupList !== '') {
88  foreach (‪GeneralUtility::trimExplode(',', $str, true) as $test) {
89  if ($test === '*' || GeneralUtility::inList($groupList, $test)) {
90  return true;
91  }
92  }
93  }
94  return false;
95  });
96  }
97 
98  protected function ‪getSessionFunction(): ExpressionFunction
99  {
100  return new ExpressionFunction(
101  'session',
102  function () {
103  // Not implemented, we only use the evaluator
104  },
105  function ($arguments, $str) {
106  $retVal = null;
107  $keyParts = explode('|', $str);
108  $sessionKey = array_shift($keyParts);
109  // @todo fetch data from be session if available
110  $tsfe = ‪$GLOBALS['TSFE'] ?? null;
111  if ($tsfe && is_object($tsfe->fe_user)) {
112  $retVal = $tsfe->fe_user->getSessionData($sessionKey);
113  foreach ($keyParts as $keyPart) {
114  if (is_object($retVal)) {
115  $retVal = $retVal->{$keyPart};
116  } elseif (is_array($retVal)) {
117  $retVal = $retVal[$keyPart];
118  } else {
119  break;
120  }
121  }
122  }
123  return $retVal;
124  }
125  );
126  }
127 
128  protected function ‪getSiteFunction(): ExpressionFunction
129  {
130  return new ExpressionFunction(
131  'site',
132  function () {
133  // Not implemented, we only use the evaluator
134  },
135  function ($arguments, $str) {
137  $requestWrapper = $arguments['request'];
138  $site = $requestWrapper->getSite();
139  if ($site instanceof ‪Site) {
140  $methodName = 'get' . ucfirst(trim($str));
141  if (method_exists($site, $methodName)) {
142  return $site->$methodName();
143  }
144  }
145  return null;
146  }
147  );
148  }
149 
150  protected function ‪getSiteLanguageFunction(): ExpressionFunction
151  {
152  return new ExpressionFunction(
153  'siteLanguage',
154  function () {
155  // Not implemented, we only use the evaluator
156  },
157  function ($arguments, $str) {
159  $requestWrapper = $arguments['request'];
160  $siteLanguage = $requestWrapper->getSiteLanguage();
161  if ($siteLanguage instanceof ‪SiteLanguage) {
162  $methodName = 'get' . ucfirst(trim($str));
163  if (method_exists($siteLanguage, $methodName)) {
164  return $siteLanguage->$methodName();
165  }
166  }
167  return null;
168  }
169  );
170  }
171 }
‪TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper
Definition: RequestWrapper.php:38
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSiteFunction
‪getSiteFunction()
Definition: Typo3ConditionFunctionsProvider.php:128
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider
Definition: DefaultFunctionsProvider.php:18
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getFunctions
‪ExpressionFunction[] getFunctions()
Definition: Typo3ConditionFunctionsProvider.php:38
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getLoginUserFunction
‪getLoginUserFunction()
Definition: Typo3ConditionFunctionsProvider.php:50
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getUsergroupFunction
‪getUsergroupFunction()
Definition: Typo3ConditionFunctionsProvider.php:79
‪TYPO3\CMS\Core\Exception\MissingTsfeException
Definition: MissingTsfeException.php:21
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getTSFEFunction
‪getTSFEFunction()
Definition: Typo3ConditionFunctionsProvider.php:67
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:40
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:26
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSessionFunction
‪getSessionFunction()
Definition: Typo3ConditionFunctionsProvider.php:98
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider
Definition: Typo3ConditionFunctionsProvider.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSiteLanguageFunction
‪getSiteLanguageFunction()
Definition: Typo3ConditionFunctionsProvider.php:150