[BUGFIX] Add possibility to use session data in TS conditions
[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 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 (is_null($val)) {
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 $val = $this->getSessionVariable(substr($vars[1], 16));
113 } else {
114 $val = $this->getGlobal('TSFE|' . $vars[1]);
115 }
116 break;
117 default:
118 }
119 }
120 }
121 return $val;
122 }
123
124 /**
125 * Return variable from current frontend user session
126 *
127 * @param string $var Session key
128 * @return mixed|null The value of the variable pointed to or NULL if variable did not exist
129 */
130 protected function getSessionVariable(string $var)
131 {
132 $retVal = null;
133 $keyParts = explode('|', $var);
134 $sessionKey = array_shift($keyParts);
135 $tsfe = $this->getTypoScriptFrontendController();
136 if ($tsfe && is_object($tsfe->fe_user)) {
137 $retVal = $tsfe->fe_user->getSessionData($sessionKey);
138 foreach ($keyParts as $keyPart) {
139 if (is_object($retVal)) {
140 $retVal = $retVal->{$keyPart};
141 } elseif (is_array($retVal)) {
142 $retVal = $retVal[$keyPart];
143 } else {
144 break;
145 }
146 }
147 }
148 return $retVal;
149 }
150
151 /**
152 * Get the usergroup list of the current user.
153 *
154 * @return string The usergroup list of the current user
155 */
156 protected function getGroupList()
157 {
158 return $this->getTypoScriptFrontendController()->gr_list;
159 }
160
161 /**
162 * Determines the current page Id.
163 *
164 * @return int The current page Id
165 */
166 protected function determinePageId()
167 {
168 return (int)$this->getTypoScriptFrontendController()->id;
169 }
170
171 /**
172 * Gets the properties for the current page.
173 *
174 * @return array The properties for the current page.
175 */
176 protected function getPage()
177 {
178 return $this->getTypoScriptFrontendController()->page;
179 }
180
181 /**
182 * Determines the rootline for the current page.
183 *
184 * @return array The rootline for the current page.
185 */
186 protected function determineRootline()
187 {
188 return (array)$this->getTypoScriptFrontendController()->tmpl->rootLine;
189 }
190
191 /**
192 * Get the id of the current user.
193 *
194 * @return int The id of the current user
195 */
196 protected function getUserId()
197 {
198 return $this->getTypoScriptFrontendController()->fe_user->user['uid'];
199 }
200
201 /**
202 * Determines if a user is logged in.
203 *
204 * @return bool Determines if a user is logged in
205 */
206 protected function isUserLoggedIn()
207 {
208 return (bool)$this->getTypoScriptFrontendController()->loginUser;
209 }
210
211 /**
212 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
213 */
214 protected function getTypoScriptFrontendController()
215 {
216 return $GLOBALS['TSFE'];
217 }
218 }