f24a1772221ee453041414a19a5aec2361c44fba
[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 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
27 */
28 class ConditionMatcher extends AbstractConditionMatcher {
29
30 /**
31 * Evaluates a TypoScript condition given as input,
32 * eg. "[browser=net][...(other conditions)...]"
33 *
34 * @param string $string The condition to match against its criterias.
35 * @return bool Whether the condition matched
36 * @see \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::parse()
37 * @throws \TYPO3\CMS\Core\Configuration\TypoScript\Exception\InvalidTypoScriptConditionException
38 */
39 protected function evaluateCondition($string) {
40 list($key, $value) = GeneralUtility::trimExplode('=', $string, FALSE, 2);
41 $result = parent::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 list($conditionClassName, $conditionParameters) = GeneralUtility::trimExplode(' ', $string, FALSE, 2);
83
84 // Check if the condition class name is a valid class
85 // This is necessary to not stop here for the conditions ELSE and GLOBAL
86 if (class_exists($conditionClassName)) {
87 // Use like this: [MyCompany\MyPackage\ConditionMatcher\MyOwnConditionMatcher = myvalue]
88 /** @var \TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractCondition $conditionObject */
89 $conditionObject = GeneralUtility::makeInstance($conditionClassName);
90 if (($conditionObject instanceof \TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching\AbstractCondition) === FALSE) {
91 throw new \TYPO3\CMS\Core\Configuration\TypoScript\Exception\InvalidTypoScriptConditionException(
92 '"' . $key . '" is not a valid TypoScript Condition object.',
93 1410286153
94 );
95 }
96
97 $conditionParameters = $this->parseUserFuncArguments($conditionParameters);
98 $conditionObject->setConditionMatcherInstance($this);
99 return $conditionObject->matchCondition($conditionParameters);
100 }
101 }
102 }
103
104 return FALSE;
105 }
106
107 /**
108 * Returns GP / ENV / TSFE vars
109 *
110 * @param string $var Identifier
111 * @return mixed The value of the variable pointed to or NULL if variable did not exist
112 */
113 protected function getVariable($var) {
114 $vars = explode(':', $var, 2);
115 $val = parent::getVariableCommon($vars);
116 if (is_null($val)) {
117 $splitAgain = explode('|', $vars[1], 2);
118 $k = trim($splitAgain[0]);
119 if ($k) {
120 switch ((string)trim($vars[0])) {
121 case 'TSFE':
122 $val = $this->getGlobal('TSFE|' . $vars[1]);
123 break;
124 default:
125 }
126 }
127 }
128 return $val;
129 }
130
131 /**
132 * Get the usergroup list of the current user.
133 *
134 * @return string The usergroup list of the current user
135 */
136 protected function getGroupList() {
137 return $this->getTypoScriptFrontendController()->gr_list;
138 }
139
140 /**
141 * Determines the current page Id.
142 *
143 * @return int The current page Id
144 */
145 protected function determinePageId() {
146 return (int)$this->getTypoScriptFrontendController()->id;
147 }
148
149 /**
150 * Gets the properties for the current page.
151 *
152 * @return array The properties for the current page.
153 */
154 protected function getPage() {
155 return $this->getTypoScriptFrontendController()->page;
156 }
157
158 /**
159 * Determines the rootline for the current page.
160 *
161 * @return array The rootline for the current page.
162 */
163 protected function determineRootline() {
164 return (array)$this->getTypoScriptFrontendController()->tmpl->rootLine;
165 }
166
167 /**
168 * Get the id of the current user.
169 *
170 * @return int The id of the current user
171 */
172 protected function getUserId() {
173 return $this->getTypoScriptFrontendController()->fe_user->user['uid'];
174 }
175
176 /**
177 * Determines if a user is logged in.
178 *
179 * @return bool Determines if a user is logged in
180 */
181 protected function isUserLoggedIn() {
182 return (bool)$this->getTypoScriptFrontendController()->loginUser;
183 }
184
185 /**
186 * Set/write a log message.
187 *
188 * @param string $message The log message to set/write
189 * @return void
190 */
191 protected function log($message) {
192 if ($this->getTimeTracker() !== NULL) {
193 $this->getTimeTracker()->setTSlogMessage($message, 3);
194 }
195 }
196
197 /**
198 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
199 */
200 protected function getTypoScriptFrontendController() {
201 return $GLOBALS['TSFE'];
202 }
203
204 /**
205 * @return \TYPO3\CMS\Core\TimeTracker\TimeTracker
206 */
207 protected function getTimeTracker() {
208 return $GLOBALS['TT'];
209 }
210
211 }