‪TYPO3CMS  9.5
Typo3ConditionFunctionsProvider.php
Go to the documentation of this file.
1 <?php
2 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 
18 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
19 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
26 
31 class ‪Typo3ConditionFunctionsProvider implements ExpressionFunctionProviderInterface
32 {
36  public function ‪getFunctions()
37  {
38  return [
39  $this->‪getLoginUserFunction(),
40  $this->‪getTSFEFunction(),
41  $this->‪getUsergroupFunction(),
42  $this->‪getSessionFunction(),
43  $this->‪getSiteFunction(),
45  ];
46  }
47 
48  protected function ‪getLoginUserFunction(): ExpressionFunction
49  {
50  return new ExpressionFunction('loginUser', function () {
51  // Not implemented, we only use the evaluator
52  }, function ($arguments, $str) {
53  $user = $arguments['frontend']->user ?? $arguments['backend']->user;
54  if ($user->isLoggedIn) {
55  foreach (GeneralUtility::trimExplode(',', $str, true) as $test) {
56  if ($test === '*' || (string)$user->userId === (string)$test) {
57  return true;
58  }
59  }
60  }
61  return false;
62  });
63  }
64 
65  protected function ‪getTSFEFunction(): ExpressionFunction
66  {
67  return new ExpressionFunction('getTSFE', function () {
68  // Not implemented, we only use the evaluator
69  }, function ($arguments) {
70  if ((‪$GLOBALS['TSFE'] ?? null) instanceof ‪TypoScriptFrontendController) {
71  return ‪$GLOBALS['TSFE'];
72  }
73  throw new ‪MissingTsfeException('TSFE is not available in this context', 1578831632);
74  });
75  }
76 
77  protected function ‪getUsergroupFunction(): ExpressionFunction
78  {
79  return new ExpressionFunction('usergroup', function () {
80  // Not implemented, we only use the evaluator
81  }, function ($arguments, $str) {
82  $user = $arguments['frontend']->user ?? $arguments['backend']->user;
83  $groupList = $user->userGroupList ?? '';
84  // '0,-1' is the default usergroups string when not logged in!
85  if ($groupList !== '0,-1' && $groupList !== '') {
86  foreach (GeneralUtility::trimExplode(',', $str, true) as $test) {
87  if ($test === '*' || GeneralUtility::inList($groupList, $test)) {
88  return true;
89  }
90  }
91  }
92  return false;
93  });
94  }
95 
96  protected function ‪getSessionFunction(): ExpressionFunction
97  {
98  return new ExpressionFunction(
99  'session',
100  function () {
101  // Not implemented, we only use the evaluator
102  },
103  function ($arguments, $str) {
104  $retVal = null;
105  $keyParts = explode('|', $str);
106  $sessionKey = array_shift($keyParts);
107  // @todo fetch data from be session if available
108  $tsfe = ‪$GLOBALS['TSFE'] ?? null;
109  if ($tsfe && is_object($tsfe->fe_user)) {
110  $retVal = $tsfe->fe_user->getSessionData($sessionKey);
111  foreach ($keyParts as $keyPart) {
112  if (is_object($retVal)) {
113  $retVal = $retVal->{$keyPart};
114  } elseif (is_array($retVal)) {
115  $retVal = $retVal[$keyPart];
116  } else {
117  break;
118  }
119  }
120  }
121  return $retVal;
122  }
123  );
124  }
125 
126  protected function ‪getSiteFunction(): ExpressionFunction
127  {
128  return new ExpressionFunction(
129  'site',
130  function () {
131  // Not implemented, we only use the evaluator
132  },
133  function ($arguments, $str) {
135  $requestWrapper = $arguments['request'];
136  $site = $requestWrapper->getSite();
137  if ($site instanceof ‪Site) {
138  $methodName = 'get' . ucfirst(trim($str));
139  if (method_exists($site, $methodName)) {
140  return $site->$methodName();
141  }
142  }
143  return null;
144  }
145  );
146  }
147 
148  protected function ‪getSiteLanguageFunction(): ExpressionFunction
149  {
150  return new ExpressionFunction(
151  'siteLanguage',
152  function () {
153  // Not implemented, we only use the evaluator
154  },
155  function ($arguments, $str) {
157  $requestWrapper = $arguments['request'];
158  $siteLanguage = $requestWrapper->getSiteLanguage();
159  if ($siteLanguage instanceof ‪SiteLanguage) {
160  $methodName = 'get' . ucfirst(trim($str));
161  if (method_exists($siteLanguage, $methodName)) {
162  return $siteLanguage->$methodName();
163  }
164  }
165  return null;
166  }
167  );
168  }
169 }
‪TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper
Definition: RequestWrapper.php:36
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSiteFunction
‪getSiteFunction()
Definition: Typo3ConditionFunctionsProvider.php:126
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider
Definition: DefaultFunctionsProvider.php:3
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getFunctions
‪ExpressionFunction[] getFunctions()
Definition: Typo3ConditionFunctionsProvider.php:36
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getLoginUserFunction
‪getLoginUserFunction()
Definition: Typo3ConditionFunctionsProvider.php:48
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getUsergroupFunction
‪getUsergroupFunction()
Definition: Typo3ConditionFunctionsProvider.php:77
‪TYPO3\CMS\Core\Exception\MissingTsfeException
Definition: MissingTsfeException.php:20
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getTSFEFunction
‪getTSFEFunction()
Definition: Typo3ConditionFunctionsProvider.php:65
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:39
‪TYPO3\CMS\Core\Site\Entity\SiteLanguage
Definition: SiteLanguage.php:25
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSessionFunction
‪getSessionFunction()
Definition: Typo3ConditionFunctionsProvider.php:96
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider
Definition: Typo3ConditionFunctionsProvider.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\Typo3ConditionFunctionsProvider\getSiteLanguageFunction
‪getSiteLanguageFunction()
Definition: Typo3ConditionFunctionsProvider.php:148