55176054b6de94108b1daf9d824fea5e80fe4d9a
[Packages/TYPO3.CMS.git] / t3lib / matchcondition / class.t3lib_matchcondition_backend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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 backend disposal.
30 *
31 * Used with the TypoScript parser.
32 * Matches browserinfo, IPnumbers for use with templates
33 *
34 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
35 * @package TYPO3
36 * @subpackage t3lib
37 */
38 class t3lib_matchCondition_backend extends t3lib_matchCondition_abstract {
39 /**
40 * Constructor for this class
41 *
42 * @return void
43 */
44 public function __construct() {
45 }
46
47 /**
48 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
49 *
50 * @param string $string: The condition to match against its criterias.
51 * @return boolean Whether the condition matched
52 * @see t3lib_tsparser::parse()
53 */
54 protected function evaluateCondition($string) {
55 list($key, $value) = t3lib_div::trimExplode('=', $string, FALSE, 2);
56
57 $result = parent::evaluateConditionCommon($key, $value);
58
59 if (is_bool($result)) {
60 return $result;
61 } else {
62 switch ($key) {
63 case 'usergroup':
64 $groupList = $this->getGroupList();
65 $values = t3lib_div::trimExplode(',', $value, TRUE);
66 foreach ($values as $test) {
67 if ($test == '*' || t3lib_div::inList($groupList, $test)) {
68 return TRUE;
69 }
70 }
71 break;
72 case 'adminUser':
73 if ($this->isUserLoggedIn()) {
74 $result = !((bool) $value XOR $this->isAdminUser());
75 return $result;
76 }
77 break;
78 case 'treeLevel':
79 $values = t3lib_div::trimExplode(',', $value, TRUE);
80 $treeLevel = count($this->rootline) - 1;
81 // If a new page is being edited or saved the treeLevel is higher by one:
82 if ($this->isNewPageWithPageId($this->pageId)) {
83 $treeLevel++;
84 }
85 foreach ($values as $test) {
86 if ($test == $treeLevel) {
87 return TRUE;
88 }
89 }
90 break;
91 case 'PIDupinRootline':
92 case 'PIDinRootline':
93 $values = t3lib_div::trimExplode(',', $value, TRUE);
94 if (($key == 'PIDinRootline') || (!in_array($this->pageId, $values)) || $this->isNewPageWithPageId($this->pageId)) {
95 foreach ($values as $test) {
96 foreach ($this->rootline as $rl_dat) {
97 if ($rl_dat['uid'] == $test) {
98 return TRUE;
99 }
100 }
101 }
102 }
103 break;
104 }
105 }
106
107 return FALSE;
108 }
109
110 /**
111 * Returns GP / ENV vars
112 *
113 * @param string Identifier
114 * @return mixed The value of the variable pointed to.
115 * @access private
116 */
117 protected function getVariable($var) {
118 $vars = explode(':', $var, 2);
119
120 $val = parent::getVariableCommon($vars);
121
122 return $val;
123 }
124
125 /**
126 * Get the usergroup list of the current user.
127 *
128 * @return string The usergroup list of the current user
129 */
130 protected function getGroupList() {
131 $groupList = $GLOBALS['BE_USER']->groupList;
132 return $groupList;
133 }
134
135 /**
136 * Tries to determine the ID of the page currently processed.
137 * When User/Group TS-Config is parsed when no specific page is handled
138 * (i.e. in the Extension Manager, etc.) this function will return "0", so that
139 * the accordant conditions (e.g. PIDinRootline) will return "FALSE"
140 *
141 * @return integer The determined page id or otherwise 0
142 */
143 protected function determinePageId() {
144 $pageId = 0;
145 $editStatement = t3lib_div::_GP('edit');
146 $commandStatement = t3lib_div::_GP('cmd');
147
148 // Determine id from module that was called with an id:
149 if ($id = intval(t3lib_div::_GP('id'))) {
150 $pageId = $id;
151 // Determine id from an edit statement:
152 } elseif (is_array($editStatement)) {
153 list($table, $uidAndAction) = each($editStatement);
154 list($uid, $action) = each($uidAndAction);
155
156 if ($action === 'edit') {
157 $pageId = $this->getPageIdByRecord($table, $uid);
158 } elseif ($action === 'new') {
159 $pageId = $this->getPageIdByRecord($table, $uid, TRUE);
160 }
161 // Determine id from a command statement:
162 } elseif (is_array($commandStatement)) {
163 list($table, $uidActionAndTarget) = each($commandStatement);
164 list($uid, $actionAndTarget) = each($uidActionAndTarget);
165 list($action, $target) = each($actionAndTarget);
166
167 if ($action === 'delete') {
168 $pageId = $this->getPageIdByRecord($table, $uid);
169 } elseif (($action === 'copy') || ($action === 'move')) {
170 $pageId = $this->getPageIdByRecord($table, $target, TRUE);
171 }
172 }
173
174 return $pageId;
175 }
176
177 /**
178 * Gets the properties for the current page.
179 *
180 * @return array The properties for the current page.
181 */
182 protected function getPage() {
183 $pageId = (isset($this->pageId) ? $this->pageId : $this->determinePageId());
184 return t3lib_BEfunc::getRecord('pages', $pageId);
185 }
186
187 /**
188 * Gets the page id by a record.
189 *
190 * @param string $table: Name of the table
191 * @param integer $id: Id of the accordant record
192 * @param boolean $ignoreTable: Whether to ignore the page, if TRUE a positive
193 * id value is considered as page id without any further checks
194 * @return integer Id of the page the record is persisted on
195 */
196 protected function getPageIdByRecord($table, $id, $ignoreTable = FALSE) {
197 $pageId = 0;
198 $id = (int) $id;
199
200 if ($table && $id) {
201 if (($ignoreTable || $table === 'pages') && $id >= 0) {
202 $pageId = $id;
203 } else {
204 $record = t3lib_BEfunc::getRecordWSOL($table, abs($id), '*', '', FALSE);
205 $pageId = $record['pid'];
206 }
207 }
208
209 return $pageId;
210 }
211
212 /**
213 * Determine if record of table 'pages' with the given $pid is currently created in TCEforms.
214 * This information is required for conditions in BE for PIDupinRootline.
215 *
216 * @param integer $pid: The pid the check for as parent page
217 * @return boolean TRUE if the is currently a new page record being edited with $pid as uid of the parent page
218 */
219 protected function isNewPageWithPageId($pageId) {
220 if (isset($GLOBALS['SOBE']) && $GLOBALS['SOBE'] instanceof SC_alt_doc) {
221 $pageId = intval($pageId);
222 $elementsData = $GLOBALS['SOBE']->elementsData;
223 $data = $GLOBALS['SOBE']->data;
224
225 // If saving a new page record:
226 if (is_array($data) && isset($data['pages']) && is_array($data['pages'])) {
227 foreach ($data['pages'] as $uid => $fields) {
228 if (strpos($uid, 'NEW') === 0 && $fields['pid'] == $pageId) {
229 return TRUE;
230 }
231 }
232 }
233 // If editing a new page record (not saved yet):
234 if (is_array($elementsData)) {
235 foreach ($elementsData as $element) {
236 if ($element['cmd'] == 'new' && $element['table'] == 'pages') {
237 if ($element['pid'] < 0) {
238 $pageRecord = t3lib_BEfunc::getRecord('pages', abs($element['pid']), 'pid');
239 $element['pid'] = $pageRecord['pid'];
240 }
241 if ($element['pid'] == $pageId) {
242 return TRUE;
243 }
244 }
245 }
246 }
247 }
248
249 return FALSE;
250 }
251
252 /**
253 * Determines the rootline for the current page.
254 *
255 * @return array The rootline for the current page.
256 */
257 protected function determineRootline() {
258 $pageId = (isset($this->pageId) ? $this->pageId : $this->determinePageId());
259 $rootline = t3lib_BEfunc::BEgetRootLine($pageId, '', TRUE);
260 return $rootline;
261 }
262
263 /**
264 * Get prefix for user functions (normally 'user_').
265 *
266 * @return string The prefix for user functions (normally 'user_').
267 */
268 protected function getUserFuncClassPrefix() {
269 $userFuncClassPrefix = 'user_';
270 return $userFuncClassPrefix;
271 }
272
273 /**
274 * Get the id of the current user.
275 *
276 * @return integer The id of the current user
277 */
278 protected function getUserId() {
279 $userId = $GLOBALS['BE_USER']->user['uid'];
280 return $userId;
281 }
282
283 /**
284 * Determines if a user is logged in.
285 *
286 * @return boolean Determines if a user is logged in
287 */
288 protected function isUserLoggedIn() {
289 $userLoggedIn = FALSE;
290 if ($GLOBALS['BE_USER']->user['uid']) {
291 $userLoggedIn = TRUE;
292 }
293 return $userLoggedIn;
294 }
295
296 /**
297 * Determines whether the current user is admin.
298 *
299 * @return boolean Whether the current user is admin
300 */
301 protected function isAdminUser() {
302 $isAdminUser = FALSE;
303 if ($GLOBALS['BE_USER']->user['admin']) {
304 $isAdminUser = TRUE;
305 }
306 return $isAdminUser;
307 }
308
309 /**
310 * Set/write a log message.
311 *
312 * @param string $message: The log message to set/write
313 * @return void
314 */
315 protected function log($message) {
316 if (is_object($GLOBALS['BE_USER'])) {
317 $GLOBALS['BE_USER']->writelog(3, 0, 1, 0, $message, array());
318 }
319 }
320 }
321
322
323 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_backend.php'])) {
324 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_backend.php']);
325 }
326
327 ?>