TYPO3 API  SVNRelease
class.t3lib_matchcondition_backend.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2009-2011 Oliver Hader <oliver@typo3.org>
00006  *  All rights reserved
00007  *
00008  *  This script is part of the TYPO3 project. The TYPO3 project is
00009  *  free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  The GNU General Public License can be found at
00015  *  http://www.gnu.org/copyleft/gpl.html.
00016  *  A copy is found in the textfile GPL.txt and important notices to the license
00017  *  from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  *  This script is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  *  GNU General Public License for more details.
00024  *
00025  *  This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Matching TypoScript conditions for backend disposal.
00030  *
00031  * Used with the TypoScript parser.
00032  * Matches browserinfo, IPnumbers for use with templates
00033  *
00034  * @author  Kasper Skårhøj <kasperYYYY@typo3.com>
00035  * @package TYPO3
00036  * @subpackage t3lib
00037  */
00038 class t3lib_matchCondition_backend extends t3lib_matchCondition_abstract {
00039     /**
00040      * Constructor for this class
00041      *
00042      * @return  void
00043      */
00044     public function __construct() {
00045     }
00046 
00047     /**
00048      * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
00049      *
00050      * @param   string      $string: The condition to match against its criterias.
00051      * @return  boolean     Whether the condition matched
00052      * @see t3lib_tsparser::parse()
00053      */
00054     protected function evaluateCondition($string) {
00055         list($key, $value) = t3lib_div::trimExplode('=', $string, FALSE, 2);
00056 
00057         $result = parent::evaluateConditionCommon($key, $value);
00058 
00059         if (is_bool($result)) {
00060             return $result;
00061         } else {
00062             switch ($key) {
00063                 case 'usergroup':
00064                     $groupList = $this->getGroupList();
00065                     $values = t3lib_div::trimExplode(',', $value, TRUE);
00066                     foreach ($values as $test) {
00067                         if ($test == '*' || t3lib_div::inList($groupList, $test)) {
00068                             return TRUE;
00069                         }
00070                     }
00071                 break;
00072                 case 'adminUser':
00073                     if ($this->isUserLoggedIn()) {
00074                         $result = !((bool) $value XOR $this->isAdminUser());
00075                         return $result;
00076                     }
00077                 break;
00078                 case 'treeLevel':
00079                     $values = t3lib_div::trimExplode(',', $value, TRUE);
00080                     $treeLevel = count($this->rootline) - 1;
00081                         // If a new page is being edited or saved the treeLevel is higher by one:
00082                     if ($this->isNewPageWithPageId($this->pageId)) {
00083                         $treeLevel++;
00084                     }
00085                     foreach ($values as $test) {
00086                         if ($test == $treeLevel) {
00087                             return TRUE;
00088                         }
00089                     }
00090                 break;
00091                 case 'PIDupinRootline':
00092                 case 'PIDinRootline':
00093                     $values = t3lib_div::trimExplode(',', $value, TRUE);
00094                     if (($key == 'PIDinRootline') || (!in_array($this->pageId, $values)) || $this->isNewPageWithPageId($this->pageId)) {
00095                         foreach ($values as $test) {
00096                             foreach ($this->rootline as $rl_dat) {
00097                                 if ($rl_dat['uid'] == $test) {
00098                                     return TRUE;
00099                                 }
00100                             }
00101                         }
00102                     }
00103                 break;
00104             }
00105         }
00106 
00107         return FALSE;
00108     }
00109 
00110     /**
00111      * Returns GP / ENV vars
00112      *
00113      * @param   string      Identifier
00114      * @return  mixed       The value of the variable pointed to.
00115      * @access private
00116      */
00117     protected function getVariable($var) {
00118         $vars = explode(':', $var, 2);
00119 
00120         $val = parent::getVariableCommon($vars);
00121 
00122         return $val;
00123     }
00124 
00125     /**
00126      * Get the usergroup list of the current user.
00127      *
00128      * @return  string      The usergroup list of the current user
00129      */
00130     protected function getGroupList() {
00131         $groupList = $GLOBALS['BE_USER']->groupList;
00132         return $groupList;
00133     }
00134 
00135     /**
00136      * Tries to determine the ID of the page currently processed.
00137      * When User/Group TS-Config is parsed when no specific page is handled
00138      * (i.e. in the Extension Manager, etc.) this function will return "0", so that
00139      * the accordant conditions (e.g. PIDinRootline) will return "false"
00140      *
00141      * @return  integer     The determined page id or otherwise 0
00142      */
00143     protected function determinePageId() {
00144         $pageId = 0;
00145         $editStatement = t3lib_div::_GP('edit');
00146         $commandStatement = t3lib_div::_GP('cmd');
00147 
00148             // Determine id from module that was called with an id:
00149         if ($id = intval(t3lib_div::_GP('id'))) {
00150             $pageId = $id;
00151             // Determine id from an edit statement:
00152         } elseif (is_array($editStatement)) {
00153             list($table, $uidAndAction) = each($editStatement);
00154             list($uid, $action) = each($uidAndAction);
00155 
00156             if ($action === 'edit') {
00157                 $pageId = $this->getPageIdByRecord($table, $uid);
00158             } elseif ($action === 'new') {
00159                 $pageId = $this->getPageIdByRecord($table, $uid, TRUE);
00160             }
00161             // Determine id from a command statement:
00162         } elseif (is_array($commandStatement)) {
00163             list($table, $uidActionAndTarget) = each($commandStatement);
00164             list($uid, $actionAndTarget) = each($uidActionAndTarget);
00165             list($action, $target) = each($actionAndTarget);
00166 
00167             if ($action === 'delete') {
00168                 $pageId = $this->getPageIdByRecord($table, $uid);
00169             } elseif (($action === 'copy') || ($action === 'move')) {
00170                 $pageId = $this->getPageIdByRecord($table, $target, TRUE);
00171             }
00172         }
00173 
00174         return $pageId;
00175     }
00176 
00177     /**
00178      * Gets the properties for the current page.
00179      *
00180      * @return  array       The properties for the current page.
00181      */
00182     protected function getPage() {
00183         $pageId = (isset($this->pageId) ? $this->pageId : $this->determinePageId());
00184         return t3lib_BEfunc::getRecord('pages', $pageId);
00185     }
00186 
00187     /**
00188      * Gets the page id by a record.
00189      *
00190      * @param   string      $table: Name of the table
00191      * @param   integer     $id: Id of the accordant record
00192      * @param   boolean     $ignoreTable: Whether to ignore the page, if true a positive
00193      *                      id value is considered as page id without any further checks
00194      * @return  integer     Id of the page the record is persisted on
00195      */
00196     protected function getPageIdByRecord($table, $id, $ignoreTable = FALSE) {
00197         $pageId = 0;
00198         $id = (int) $id;
00199 
00200         if ($table && $id) {
00201             if (($ignoreTable || $table === 'pages') && $id >= 0) {
00202                 $pageId = $id;
00203             } else {
00204                 $record = t3lib_BEfunc::getRecordWSOL($table, abs($id), '*', '', FALSE);
00205                 $pageId = $record['pid'];
00206             }
00207         }
00208 
00209         return $pageId;
00210     }
00211 
00212     /**
00213      * Determine if record of table 'pages' with the given $pid is currently created in TCEforms.
00214      * This information is required for conditions in BE for PIDupinRootline.
00215      *
00216      * @param   integer     $pid: The pid the check for as parent page
00217      * @return  boolean     true if the is currently a new page record being edited with $pid as uid of the parent page
00218      */
00219     protected function isNewPageWithPageId($pageId) {
00220         if (isset($GLOBALS['SOBE']) && $GLOBALS['SOBE'] instanceof SC_alt_doc) {
00221             $pageId = intval($pageId);
00222             $elementsData = $GLOBALS['SOBE']->elementsData;
00223             $data = $GLOBALS['SOBE']->data;
00224 
00225                 // If saving a new page record:
00226             if (is_array($data) && isset($data['pages']) && is_array($data['pages'])) {
00227                 foreach ($data['pages'] as $uid => $fields) {
00228                     if (strpos($uid, 'NEW') === 0 && $fields['pid'] == $pageId) {
00229                         return TRUE;
00230                     }
00231                 }
00232             }
00233                 // If editing a new page record (not saved yet):
00234             if (is_array($elementsData)) {
00235                 foreach ($elementsData as $element) {
00236                     if ($element['cmd'] == 'new' && $element['table'] == 'pages') {
00237                         if ($element['pid'] < 0) {
00238                             $pageRecord = t3lib_BEfunc::getRecord('pages', abs($element['pid']), 'pid');
00239                             $element['pid'] = $pageRecord['pid'];
00240                         }
00241                         if ($element['pid'] == $pageId) {
00242                             return TRUE;
00243                         }
00244                     }
00245                 }
00246             }
00247         }
00248 
00249         return FALSE;
00250     }
00251 
00252     /**
00253      * Determines the rootline for the current page.
00254      *
00255      * @return  array       The rootline for the current page.
00256      */
00257     protected function determineRootline() {
00258         $pageId = (isset($this->pageId) ? $this->pageId : $this->determinePageId());
00259         $rootline = t3lib_BEfunc::BEgetRootLine($pageId, '', TRUE);
00260         return $rootline;
00261     }
00262 
00263     /**
00264      * Get prefix for user functions (normally 'user_').
00265      *
00266      * @return  string      The prefix for user functions (normally 'user_').
00267      */
00268     protected function getUserFuncClassPrefix() {
00269         $userFuncClassPrefix = 'user_';
00270         return $userFuncClassPrefix;
00271     }
00272 
00273     /**
00274      * Get the id of the current user.
00275      *
00276      * @return  integer     The id of the current user
00277      */
00278     protected function getUserId() {
00279         $userId = $GLOBALS['BE_USER']->user['uid'];
00280         return $userId;
00281     }
00282 
00283     /**
00284      * Determines if a user is logged in.
00285      *
00286      * @return  boolean     Determines if a user is logged in
00287      */
00288     protected function isUserLoggedIn() {
00289         $userLoggedIn = FALSE;
00290         if ($GLOBALS['BE_USER']->user['uid']) {
00291             $userLoggedIn = TRUE;
00292         }
00293         return $userLoggedIn;
00294     }
00295 
00296     /**
00297      * Determines whether the current user is admin.
00298      *
00299      * @return  boolean     Whether the current user is admin
00300      */
00301     protected function isAdminUser() {
00302         $isAdminUser = FALSE;
00303         if ($GLOBALS['BE_USER']->user['admin']) {
00304             $isAdminUser = TRUE;
00305         }
00306         return $isAdminUser;
00307     }
00308 
00309     /**
00310      * Set/write a log message.
00311      *
00312      * @param   string      $message: The log message to set/write
00313      * @return  void
00314      */
00315     protected function log($message) {
00316         if (is_object($GLOBALS['BE_USER'])) {
00317             $GLOBALS['BE_USER']->writelog(3, 0, 1, 0, $message, array());
00318         }
00319     }
00320 }
00321 
00322 
00323 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_backend.php'])) {
00324     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_backend.php']);
00325 }
00326 
00327 ?>