class.t3lib_matchcondition_backend.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2010 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      * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=311&cHash=487cbd5cdf
00117      */
00118     protected function getVariable($var) {
00119         $vars = explode(':', $var, 2);
00120 
00121         $val = parent::getVariableCommon($vars);
00122 
00123         return $val;
00124     }
00125 
00126     /**
00127      * Get the usergroup list of the current user.
00128      *
00129      * @return  string      The usergroup list of the current user
00130      */
00131     protected function getGroupList() {
00132         $groupList = $GLOBALS['BE_USER']->groupList;
00133         return $groupList;
00134     }
00135 
00136     /**
00137      * Tries to determine the ID of the page currently processed.
00138      * When User/Group TS-Config is parsed when no specific page is handled
00139      * (i.e. in the Extension Manager, etc.) this function will return "0", so that
00140      * the accordant conditions (e.g. PIDinRootline) will return "false"
00141      *
00142      * @return  integer     The determined page id or otherwise 0
00143      */
00144     protected function determinePageId() {
00145         $pageId = 0;
00146         $editStatement = t3lib_div::_GP('edit');
00147         $commandStatement = t3lib_div::_GP('cmd');
00148 
00149             // Determine id from module that was called with an id:
00150         if ($id = intval(t3lib_div::_GP('id'))) {
00151             $pageId = $id;
00152             // Determine id from an edit statement:
00153         } elseif (is_array($editStatement)) {
00154             list($table, $uidAndAction) = each($editStatement);
00155             list($uid, $action) = each($uidAndAction);
00156 
00157             if ($action === 'edit') {
00158                 $pageId = $this->getPageIdByRecord($table, $uid);
00159             } elseif ($action === 'new') {
00160                 $pageId = $this->getPageIdByRecord($table, $uid, true);
00161             }
00162             // Determine id from a command statement:
00163         } elseif (is_array($commandStatement)) {
00164             list($table, $uidActionAndTarget) = each($commandStatement);
00165             list($uid, $actionAndTarget) = each($uidActionAndTarget);
00166             list($action, $target) = each($actionAndTarget);
00167 
00168             if ($action === 'delete') {
00169                 $pageId = $this->getPageIdByRecord($table, $uid);
00170             } elseif (($action === 'copy') || ($action === 'move')) {
00171                 $pageId = $this->getPageIdByRecord($table, $target, true);
00172             }
00173         }
00174 
00175         return $pageId;
00176     }
00177 
00178     /**
00179      * Gets the page id by a record.
00180      *
00181      * @param   string      $table: Name of the table
00182      * @param   integer     $id: Id of the accordant record
00183      * @param   boolean     $ignoreTable: Whether to ignore the page, if true a positive
00184      *                      id value is considered as page id without any further checks
00185      * @return  integer     Id of the page the record is persisted on
00186      */
00187     protected function getPageIdByRecord($table, $id, $ignoreTable = false) {
00188         $pageId = 0;
00189         $id = (int)$id;
00190 
00191         if ($table && $id) {
00192             if (($ignoreTable || $table === 'pages') && $id >= 0) {
00193                 $pageId = $id;
00194             } else {
00195                 $record = t3lib_BEfunc::getRecordWSOL($table, abs($id), '*', '', false);
00196                 $pageId = $record['pid'];
00197             }
00198         }
00199 
00200         return $pageId;
00201     }
00202 
00203     /**
00204      * Determine if record of table 'pages' with the given $pid is currently created in TCEforms.
00205      * This information is required for conditions in BE for PIDupinRootline.
00206      *
00207      * @param   integer     $pid: The pid the check for as parent page
00208      * @return  boolean     true if the is currently a new page record being edited with $pid as uid of the parent page
00209      */
00210     protected function isNewPageWithPageId($pageId) {
00211         if (isset($GLOBALS['SOBE']) && $GLOBALS['SOBE'] instanceof SC_alt_doc) {
00212             $pageId = intval($pageId);
00213             $elementsData = $GLOBALS['SOBE']->elementsData;
00214             $data = $GLOBALS['SOBE']->data;
00215 
00216                 // If saving a new page record:
00217             if (is_array($data) && isset($data['pages']) && is_array($data['pages'])) {
00218                 foreach ($data['pages'] as $uid => $fields) {
00219                     if (strpos($uid, 'NEW') === 0 && $fields['pid'] == $pageId) {
00220                         return true;
00221                     }
00222                 }
00223             }
00224                 // If editing a new page record (not saved yet):
00225             if (is_array($elementsData)) {
00226                 foreach ($elementsData as $element) {
00227                     if ($element['cmd'] == 'new' && $element['table'] == 'pages') {
00228                         if ($element['pid'] < 0) {
00229                             $pageRecord = t3lib_BEfunc::getRecord('pages', abs($element['pid']), 'pid');
00230                             $element['pid'] = $pageRecord['pid'];
00231                         }
00232                         if ($element['pid'] == $pageId) {
00233                             return true;
00234                         }
00235                     }
00236                 }
00237             }
00238         }
00239 
00240         return false;
00241     }
00242 
00243     /**
00244      * Determines the rootline for the current page.
00245      *
00246      * @return  array       The rootline for the current page.
00247      */
00248     protected function determineRootline() {
00249         $pageId = (isset($this->pageId) ? $this->pageId : $this->determinePageId());
00250         $rootline = t3lib_BEfunc::BEgetRootLine($pageId, '', true);
00251         return $rootline;
00252     }
00253 
00254     /**
00255      * Get prefix for user functions (normally 'user_').
00256      *
00257      * @return  string      The prefix for user functions (normally 'user_').
00258      */
00259     protected function getUserFuncClassPrefix() {
00260         $userFuncClassPrefix = 'user_';
00261         return $userFuncClassPrefix;
00262     }
00263 
00264     /**
00265      * Get the id of the current user.
00266      *
00267      * @return  integer     The id of the current user
00268      */
00269     protected function getUserId() {
00270         $userId = $GLOBALS['BE_USER']->user['uid'];
00271         return $userId;
00272     }
00273 
00274     /**
00275      * Determines if a user is logged in.
00276      *
00277      * @return  boolean     Determines if a user is logged in
00278      */
00279     protected function isUserLoggedIn() {
00280         $userLoggedIn = false;
00281         if ($GLOBALS['BE_USER']->user['uid']) {
00282             $userLoggedIn = true;
00283         }
00284         return $userLoggedIn;
00285     }
00286 
00287     /**
00288      * Determines whether the current user is admin.
00289      *
00290      * @return  boolean     Whether the current user is admin
00291      */
00292     protected function isAdminUser() {
00293         $isAdminUser = false;
00294         if ($GLOBALS['BE_USER']->user['admin']) {
00295             $isAdminUser = true;
00296         }
00297         return $isAdminUser;
00298     }
00299 
00300     /**
00301      * Set/write a log message.
00302      *
00303      * @param   string      $message: The log message to set/write
00304      * @return  void
00305      */
00306     protected function log($message) {
00307         if (is_object($GLOBALS['BE_USER'])) {
00308             $GLOBALS['BE_USER']->writelog(3, 0, 1, 0, $message, array());
00309         }
00310     }
00311 }
00312 
00313 
00314 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_backend.php']) {
00315     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/matchcondition/class.t3lib_matchcondition_backend.php']);
00316 }
00317 
00318 ?>

Generated on Sat Sep 4 04:17:12 2010 for TYPO3 API by  doxygen 1.4.7