Added feature #5613: Enable/integrate conditions in Page TSconfig
[Packages/TYPO3.CMS.git] / t3lib / matchcondition / class.t3lib_matchcondition_frontend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Matching TypoScript conditions for frontend disposal.
30 *
31 * Used with the TypoScript parser.
32 * Matches browserinfo, IPnumbers for use with templates
33 *
34 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
35 * @package TYPO3
36 * @subpackage t3lib
37 */
38 class t3lib_matchCondition_frontend extends t3lib_matchCondition_abstract {
39 /**
40 * @var array
41 */
42 protected $deprecatedHooks = array();
43
44 /**
45 * Constructor for this class
46 *
47 * @return void
48 */
49 public function __construct() {
50 $this->initializeDeprecatedHooks();
51 }
52
53 /**
54 * Initializes deprectated hooks that existed in t3lib_matchCondition until TYPO3 4.3.
55 *
56 * @return void
57 */
58 protected function initializeDeprecatedHooks() {
59 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass']:
60 $matchConditionHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass'];
61 if (is_array($matchConditionHooks)) {
62 t3lib_div::deprecationLog(
63 'The hook $TYPO3_CONF_VARS[SC_OPTIONS][t3lib/class.t3lib_matchcondition.php][matchConditionClass] ' .
64 'is deprecated since TYPO3 4.3. Use the new hooks getBrowserInfo and getDeviceType in ' .
65 't3lib_utility_Client instead.'
66 );
67
68 foreach ($matchConditionHooks as $hookClass) {
69 $this->deprecatedHooks[] = t3lib_div::getUserObj($hookClass, '');
70 }
71 }
72 }
73
74 /**
75 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
76 *
77 * @param string $string: The condition to match against its criterias.
78 * @return boolean Whether the condition matched
79 * @see t3lib_tsparser::parse()
80 */
81 protected function evaluateCondition($string) {
82 list($key, $value) = t3lib_div::trimExplode('=', $string, false, 2);
83
84 $result = parent::evaluateConditionCommon($key, $value);
85
86 if (is_bool($result)) {
87 return $result;
88 } else {
89 switch ($key) {
90 case 'usergroup':
91 $groupList = $this->getGroupList();
92 if ($groupList != '0,-1') { // '0,-1' is the default usergroups when not logged in!
93 $values = t3lib_div::trimExplode(',', $value, true);
94 foreach ($values as $test) {
95 if ($test == '*' || t3lib_div::inList($groupList, $test)) {
96 return true;
97 }
98 }
99 }
100 break;
101 case 'treeLevel':
102 $values = t3lib_div::trimExplode(',', $value, true);
103 $treeLevel = count($this->rootline) - 1;
104 foreach ($values as $test) {
105 if ($test == $treeLevel) {
106 return true;
107 }
108 }
109 break;
110 case 'PIDupinRootline':
111 case 'PIDinRootline':
112 $values = t3lib_div::trimExplode(',', $value, true);
113 if (($key=='PIDinRootline') || (!in_array($this->pageId, $values))) {
114 foreach ($values as $test) {
115 foreach ($this->rootline as $rl_dat) {
116 if ($rl_dat['uid'] == $test) {
117 return true;
118 }
119 }
120 }
121 }
122 break;
123 }
124 }
125
126 return false;
127 }
128
129 /**
130 * Generates an array with abstracted browser information
131 *
132 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
133 * @return array Contains keys "browser", "version", "system"
134 */
135 protected function getBrowserInfo($userAgent) {
136 // Exceute deprecated hooks:
137 // @deprecated since TYPO3 4.3
138 foreach($this->deprecatedHooks as $hookObj) {
139 if (method_exists($hookObj, 'browserInfo')) {
140 $result = $hookObj->browserInfo($userAgent);
141 if (strlen($result)) {
142 return $result;
143 }
144 }
145 }
146
147 return parent::getBrowserInfo($userAgent);
148 }
149
150 /**
151 * Gets a code for a browsing device based on the input useragent string.
152 *
153 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
154 * @return string Code for the specific device type
155 */
156 protected function getDeviceType($userAgent) {
157 // Exceute deprecated hooks:
158 // @deprecated since TYPO3 4.3
159 foreach($this->deprecatedHooks as $hookObj) {
160 if (method_exists($hookObj, 'whichDevice')) {
161 $result = $hookObj->whichDevice($userAgent);
162 if (strlen($result)) {
163 return $result;
164 }
165 }
166 }
167
168 // deprecated, see above
169 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'])) {
170 foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'] as $_classRef) {
171 $_procObj = t3lib_div::getUserObj($_classRef);
172 return $_procObj->whichDevice_ext($useragent);
173 }
174 }
175
176 return parent::getDeviceType($userAgent);
177 }
178
179 /**
180 * Returns GP / ENV / TSFE vars
181 *
182 * @param string Identifier
183 * @return mixed The value of the variable pointed to.
184 */
185 protected function getVariable($var) {
186 $vars = explode(':', $var, 2);
187
188 $val = parent::getVariableCommon($vars);
189
190 if (is_null($val)) {
191 $splitAgain=explode('|', $vars[1], 2);
192 $k = trim($splitAgain[0]);
193 if ($k) {
194 switch((string)trim($vars[0])) {
195 case 'TSFE':
196 $val = $this->getGlobal('TSFE|' . $vars[1]);
197 break;
198 }
199 }
200 }
201
202 return $val;
203 }
204
205 /**
206 * Get the usergroup list of the current user.
207 *
208 * @return string The usergroup list of the current user
209 */
210 protected function getGroupList() {
211 $groupList = $GLOBALS['TSFE']->gr_list;
212 return $groupList;
213 }
214
215 /**
216 * Determines the current page Id.
217 *
218 * @return integer The current page Id
219 */
220 protected function determinePageId() {
221 return (int)$GLOBALS['TSFE']->id;
222 }
223
224 /**
225 * Determines the rootline for the current page.
226 *
227 * @return array The rootline for the current page.
228 */
229 protected function determineRootline() {
230 $rootline = (array)$GLOBALS['TSFE']->tmpl->rootLine;
231 return $rootline;
232 }
233
234 /**
235 * Get prefix for user functions (normally 'user_').
236 *
237 * @return string The prefix for user functions (normally 'user_').
238 */
239 protected function getUserFuncClassPrefix() {
240 $userFuncClassPrefix = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix'];
241 return $userFuncClassPrefix;
242 }
243
244 /**
245 * Get the id of the current user.
246 *
247 * @return integer The id of the current user
248 */
249 protected function getUserId() {
250 $userId = $GLOBALS['TSFE']->fe_user->user['uid'];
251 return $userId;
252 }
253
254 /**
255 * Determines if a user is logged in.
256 *
257 * @return boolean Determines if a user is logged in
258 */
259 protected function isUserLoggedIn() {
260 $userLoggedIn = false;
261 if ($GLOBALS['TSFE']->loginUser) {
262 $userLoggedIn = true;
263 }
264 return $userLoggedIn;
265 }
266
267 /**
268 * Set/write a log message.
269 *
270 * @param string $message: The log message to set/write
271 * @return void
272 */
273 protected function log($message) {
274 if (is_object($GLOBALS['TT'])) {
275 $GLOBALS['TT']->setTSlogMessage($message,3);
276 }
277 }
278 }
279
280
281 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_frontend.php']) {
282 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_frontend.php']);
283 }
284
285 ?>