[!!!][TASK] Drop "documentation" extension
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Configuration / TypoScript / ConditionMatching / ConditionMatcher.php
1 <?php
2
3 namespace TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching;
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 Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractConditionMatcher;
20 use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22
23 /**
24 * Matching TypoScript conditions for frontend disposal.
25 *
26 * Used with the TypoScript parser. Matches browserinfo
27 * and IP numbers for use with templates.
28 */
29 class ConditionMatcher extends AbstractConditionMatcher
30 {
31 /**
32 * Evaluates a TypoScript condition given as input,
33 * eg. "[browser=net][...(other conditions)...]"
34 *
35 * @param string $string The condition to match against its criteria.
36 * @return bool Whether the condition matched
37 * @see \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::parse()
38 * @throws \TYPO3\CMS\Core\Configuration\TypoScript\Exception\InvalidTypoScriptConditionException
39 */
40 protected function evaluateCondition($string)
41 {
42 list($key, $value) = GeneralUtility::trimExplode('=', $string, false, 2);
43 $result = $this->evaluateConditionCommon($key, $value);
44
45 if (is_bool($result)) {
46 return $result;
47 }
48 switch ($key) {
49 case 'usergroup':
50 $groupList = $this->getGroupList();
51 // '0,-1' is the default usergroups when not logged in!
52 if ($groupList !== '0,-1') {
53 $values = GeneralUtility::trimExplode(',', $value, true);
54 foreach ($values as $test) {
55 if ($test === '*' || GeneralUtility::inList($groupList, $test)) {
56 return true;
57 }
58 }
59 }
60 break;
61 case 'treeLevel':
62 $values = GeneralUtility::trimExplode(',', $value, true);
63 $treeLevel = count($this->rootline) - 1;
64 foreach ($values as $test) {
65 if ($test == $treeLevel) {
66 return true;
67 }
68 }
69 break;
70 case 'PIDupinRootline':
71 case 'PIDinRootline':
72 $values = GeneralUtility::trimExplode(',', $value, true);
73 if ($key === 'PIDinRootline' || !in_array($this->pageId, $values)) {
74 foreach ($values as $test) {
75 foreach ($this->rootline as $rlDat) {
76 if ($rlDat['uid'] == $test) {
77 return true;
78 }
79 }
80 }
81 }
82 break;
83 case 'site':
84 $siteLanguage = $this->getCurrentSiteLanguage();
85 if ($siteLanguage instanceof SiteLanguage) {
86 $site = $siteLanguage->getSite();
87 $values = GeneralUtility::trimExplode(',', $value, true);
88 foreach ($values as $test) {
89 $point = strcspn($test, '=');
90 $testValue = substr($test, $point + 1);
91 $testValue = trim($testValue);
92 $theVarName = trim(substr($test, 0, $point));
93 $methodName = 'get' . ucfirst($theVarName);
94 if (method_exists($site, $methodName)) {
95 $sitePropertyValue = call_user_func([$site, $methodName]);
96 // loose check on purpose in order to check for integer values
97 if ($testValue == $sitePropertyValue) {
98 return true;
99 }
100 }
101 }
102 }
103 break;
104 case 'siteLanguage':
105 $siteLanguage = $this->getCurrentSiteLanguage();
106 if ($siteLanguage instanceof SiteLanguage) {
107 $values = GeneralUtility::trimExplode(',', $value, true);
108 foreach ($values as $test) {
109 $point = strcspn($test, '=');
110 $testValue = substr($test, $point + 1);
111 $testValue = trim($testValue);
112 $theVarName = trim(substr($test, 0, $point));
113 $methodName = 'get' . ucfirst($theVarName);
114 if (method_exists($siteLanguage, $methodName)) {
115 $languagePropertyValue = call_user_func([$siteLanguage, $methodName]);
116 // loose check on purpose in order to check for integer values
117 if ($testValue == $languagePropertyValue) {
118 return true;
119 }
120 }
121 }
122 }
123 break;
124 default:
125 $conditionResult = $this->evaluateCustomDefinedCondition($string);
126 if ($conditionResult !== null) {
127 return $conditionResult;
128 }
129 }
130
131 return false;
132 }
133
134 /**
135 * Returns GP / ENV / TSFE / session vars
136 *
137 * @example GP:L
138 * @example TSFE:fe_user|sesData|foo|bar
139 * @example TSFE:id
140 * @example ENV:HTTP_HOST
141 *
142 * @param string $var Identifier
143 * @return mixed|null The value of the variable pointed to or NULL if variable did not exist
144 */
145 protected function getVariable($var)
146 {
147 $vars = explode(':', $var, 2);
148 $val = $this->getVariableCommon($vars);
149 if ($val === null) {
150 $splitAgain = explode('|', $vars[1], 2);
151 $k = trim($splitAgain[0]);
152 if ($k) {
153 switch ((string)trim($vars[0])) {
154 case 'TSFE':
155 if (strpos($vars[1], 'fe_user|sesData|') === 0) {
156 trigger_error(
157 'Condition on TSFE|fe_user|sesData is deprecated and will be removed in TYPO3 CMS 10',
158 E_USER_DEPRECATED
159 );
160 $val = $this->getSessionVariable(substr($vars[1], 16));
161 } else {
162 $val = $this->getGlobal('TSFE|' . $vars[1]);
163 }
164 break;
165 case 'session':
166 $val = $this->getSessionVariable($vars[1]);
167 break;
168 default:
169 }
170 }
171 }
172 return $val;
173 }
174
175 /**
176 * Return variable from current frontend user session
177 *
178 * @param string $var Session key
179 * @return mixed|null The value of the variable pointed to or NULL if variable did not exist
180 */
181 protected function getSessionVariable(string $var)
182 {
183 $retVal = null;
184 $keyParts = explode('|', $var);
185 $sessionKey = array_shift($keyParts);
186 $tsfe = $this->getTypoScriptFrontendController();
187 if ($tsfe && is_object($tsfe->fe_user)) {
188 $retVal = $tsfe->fe_user->getSessionData($sessionKey);
189 foreach ($keyParts as $keyPart) {
190 if (is_object($retVal)) {
191 $retVal = $retVal->{$keyPart};
192 } elseif (is_array($retVal)) {
193 $retVal = $retVal[$keyPart];
194 } else {
195 break;
196 }
197 }
198 }
199 return $retVal;
200 }
201
202 /**
203 * Get the usergroup list of the current user.
204 *
205 * @return string The usergroup list of the current user
206 */
207 protected function getGroupList()
208 {
209 return $this->getTypoScriptFrontendController()->gr_list;
210 }
211
212 /**
213 * Determines the current page Id.
214 *
215 * @return int The current page Id
216 */
217 protected function determinePageId()
218 {
219 return (int)($this->getTypoScriptFrontendController()->id ?? 0);
220 }
221
222 /**
223 * Gets the properties for the current page.
224 *
225 * @return array The properties for the current page.
226 */
227 protected function getPage()
228 {
229 return $this->getTypoScriptFrontendController()->page;
230 }
231
232 /**
233 * Determines the rootline for the current page.
234 *
235 * @return array The rootline for the current page.
236 */
237 protected function determineRootline()
238 {
239 return (array)$this->getTypoScriptFrontendController()->tmpl->rootLine;
240 }
241
242 /**
243 * Get the id of the current user.
244 *
245 * @return int The id of the current user
246 */
247 protected function getUserId()
248 {
249 return $this->getTypoScriptFrontendController()->fe_user->user['uid'];
250 }
251
252 /**
253 * Determines if a user is logged in.
254 *
255 * @return bool Determines if a user is logged in
256 */
257 protected function isUserLoggedIn()
258 {
259 return (bool)$this->getTypoScriptFrontendController()->loginUser;
260 }
261
262 /**
263 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
264 */
265 protected function getTypoScriptFrontendController()
266 {
267 return $GLOBALS['TSFE'];
268 }
269
270 /**
271 * Returns the currently configured "site language" if a site is configured (= resolved) in the current request.
272 *
273 * @internal
274 */
275 protected function getCurrentSiteLanguage(): ?SiteLanguage
276 {
277 if ($GLOBALS['TYPO3_REQUEST'] instanceof ServerRequestInterface
278 && $GLOBALS['TYPO3_REQUEST']->getAttribute('language') instanceof SiteLanguage) {
279 return $GLOBALS['TYPO3_REQUEST']->getAttribute('language');
280 }
281 return null;
282 }
283 }