[TASK] Streamline expressionLanguage usage in core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / ExpressionLanguage / FunctionsProvider / Typo3ConditionFunctionsProvider.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider;
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;
20 use TYPO3\CMS\Core\ExpressionLanguage\RequestWrapper;
21 use TYPO3\CMS\Core\Site\Entity\Site;
22 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\VersionNumberUtility;
25
26 /**
27 * Class TypoScriptConditionProvider
28 * @internal
29 */
30 class Typo3ConditionFunctionsProvider implements ExpressionFunctionProviderInterface
31 {
32 /**
33 * @return ExpressionFunction[] An array of Function instances
34 */
35 public function getFunctions()
36 {
37 return [
38 $this->getIpFunction(),
39 $this->getCompatVersionFunction(),
40 $this->getLoginUserFunction(),
41 $this->getTSFEFunction(),
42 $this->getUsergroupFunction(),
43 $this->getSessionFunction(),
44 $this->getSiteFunction(),
45 $this->getSiteLanguageFunction(),
46 ];
47 }
48
49 protected function getIpFunction(): ExpressionFunction
50 {
51 return new ExpressionFunction('ip', function ($str) {
52 // Not implemented, we only use the evaluator
53 }, function ($arguments, $str) {
54 if ($str === 'devIP') {
55 $str = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
56 }
57 return (bool)GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $str);
58 });
59 }
60
61 protected function getCompatVersionFunction(): ExpressionFunction
62 {
63 return new ExpressionFunction('compatVersion', function ($str) {
64 // Not implemented, we only use the evaluator
65 }, function ($arguments, $str) {
66 return VersionNumberUtility::convertVersionNumberToInteger(TYPO3_branch) >= VersionNumberUtility::convertVersionNumberToInteger($str);
67 });
68 }
69
70 protected function getLoginUserFunction(): ExpressionFunction
71 {
72 return new ExpressionFunction('loginUser', function ($str) {
73 // Not implemented, we only use the evaluator
74 }, function ($arguments, $str) {
75 $user = $arguments['backend']->user ?? $arguments['frontend']->user;
76 if ($user->isLoggedIn) {
77 foreach (GeneralUtility::trimExplode(',', $str, true) as $test) {
78 if ($test === '*' || (string)$user->userId === (string)$test) {
79 return true;
80 }
81 }
82 }
83 return false;
84 });
85 }
86
87 protected function getTSFEFunction(): ExpressionFunction
88 {
89 return new ExpressionFunction('getTSFE', function ($str) {
90 // Not implemented, we only use the evaluator
91 }, function ($arguments) {
92 return $GLOBALS['TSFE'];
93 });
94 }
95
96 protected function getUsergroupFunction(): ExpressionFunction
97 {
98 return new ExpressionFunction('usergroup', function ($str) {
99 // Not implemented, we only use the evaluator
100 }, function ($arguments, $str) {
101 $user = $arguments['backend']->user ?? $arguments['frontend']->user;
102 $groupList = $user->userGroupList ?? '';
103 // '0,-1' is the default usergroups string when not logged in!
104 if ($groupList !== '0,-1' && $groupList !== '') {
105 foreach (GeneralUtility::trimExplode(',', $str, true) as $test) {
106 if ($test === '*' || GeneralUtility::inList($groupList, $test)) {
107 return true;
108 }
109 }
110 }
111 return false;
112 });
113 }
114
115 protected function getSessionFunction(): ExpressionFunction
116 {
117 return new ExpressionFunction(
118 'session',
119 function ($str) {
120 // Not implemented, we only use the evaluator
121 },
122 function ($arguments, $str) {
123 $retVal = null;
124 $keyParts = explode('|', $str);
125 $sessionKey = array_shift($keyParts);
126 // @todo fetch data from be session if available
127 $tsfe = $GLOBALS['TSFE'] ?? null;
128 if ($tsfe && is_object($tsfe->fe_user)) {
129 $retVal = $tsfe->fe_user->getSessionData($sessionKey);
130 foreach ($keyParts as $keyPart) {
131 if (is_object($retVal)) {
132 $retVal = $retVal->{$keyPart};
133 } elseif (is_array($retVal)) {
134 $retVal = $retVal[$keyPart];
135 } else {
136 break;
137 }
138 }
139 }
140 return $retVal;
141 }
142 );
143 }
144
145 protected function getSiteFunction(): ExpressionFunction
146 {
147 return new ExpressionFunction(
148 'site',
149 function ($str) {
150 // Not implemented, we only use the evaluator
151 },
152 function ($arguments, $str) {
153 /** @var RequestWrapper $requestWrapper */
154 $requestWrapper = $arguments['request'];
155 $site = $requestWrapper->getSite();
156 if ($site instanceof Site) {
157 $methodName = 'get' . ucfirst(trim($str));
158 if (method_exists($site, $methodName)) {
159 return $site->$methodName();
160 }
161 }
162 return null;
163 }
164 );
165 }
166
167 protected function getSiteLanguageFunction(): ExpressionFunction
168 {
169 return new ExpressionFunction(
170 'siteLanguage',
171 function ($str) {
172 // Not implemented, we only use the evaluator
173 },
174 function ($arguments, $str) {
175 /** @var RequestWrapper $requestWrapper */
176 $requestWrapper = $arguments['request'];
177 $siteLanguage = $requestWrapper->getSiteLanguage();
178 if ($siteLanguage instanceof SiteLanguage) {
179 $methodName = 'get' . ucfirst(trim($str));
180 if (method_exists($siteLanguage, $methodName)) {
181 return $siteLanguage->$methodName();
182 }
183 }
184 return null;
185 }
186 );
187 }
188 }