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