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