24b068e8c3a0def4f375b5e95ee7cbb2d37ed3b8
[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 criterias.
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 } else {
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
88 return false;
89 }
90
91 /**
92 * Returns GP / ENV / TSFE vars
93 *
94 * @param string $var Identifier
95 * @return mixed The value of the variable pointed to or NULL if variable did not exist
96 */
97 protected function getVariable($var)
98 {
99 $vars = explode(':', $var, 2);
100 $val = $this->getVariableCommon($vars);
101 if (is_null($val)) {
102 $splitAgain = explode('|', $vars[1], 2);
103 $k = trim($splitAgain[0]);
104 if ($k) {
105 switch ((string)trim($vars[0])) {
106 case 'TSFE':
107 $val = $this->getGlobal('TSFE|' . $vars[1]);
108 break;
109 default:
110 }
111 }
112 }
113 return $val;
114 }
115
116 /**
117 * Get the usergroup list of the current user.
118 *
119 * @return string The usergroup list of the current user
120 */
121 protected function getGroupList()
122 {
123 return $this->getTypoScriptFrontendController()->gr_list;
124 }
125
126 /**
127 * Determines the current page Id.
128 *
129 * @return int The current page Id
130 */
131 protected function determinePageId()
132 {
133 return (int)$this->getTypoScriptFrontendController()->id;
134 }
135
136 /**
137 * Gets the properties for the current page.
138 *
139 * @return array The properties for the current page.
140 */
141 protected function getPage()
142 {
143 return $this->getTypoScriptFrontendController()->page;
144 }
145
146 /**
147 * Determines the rootline for the current page.
148 *
149 * @return array The rootline for the current page.
150 */
151 protected function determineRootline()
152 {
153 return (array)$this->getTypoScriptFrontendController()->tmpl->rootLine;
154 }
155
156 /**
157 * Get the id of the current user.
158 *
159 * @return int The id of the current user
160 */
161 protected function getUserId()
162 {
163 return $this->getTypoScriptFrontendController()->fe_user->user['uid'];
164 }
165
166 /**
167 * Determines if a user is logged in.
168 *
169 * @return bool Determines if a user is logged in
170 */
171 protected function isUserLoggedIn()
172 {
173 return (bool)$this->getTypoScriptFrontendController()->loginUser;
174 }
175
176 /**
177 * Set/write a log message.
178 *
179 * @param string $message The log message to set/write
180 * @return void
181 */
182 protected function log($message)
183 {
184 if ($this->getTimeTracker() !== null) {
185 $this->getTimeTracker()->setTSlogMessage($message, 3);
186 }
187 }
188
189 /**
190 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
191 */
192 protected function getTypoScriptFrontendController()
193 {
194 return $GLOBALS['TSFE'];
195 }
196
197 /**
198 * @return \TYPO3\CMS\Core\TimeTracker\TimeTracker
199 */
200 protected function getTimeTracker()
201 {
202 return $GLOBALS['TT'];
203 }
204 }