Documentation TYPO3 par Ameos

class.tslib_feuserauth.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
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 ***************************************************************/
00079 class tslib_feUserAuth extends t3lib_userAuth {
00080         var $session_table = 'fe_sessions';             // Table to use for session data.
00081         var $name = 'fe_typo_user';                 // Session/Cookie name
00082         var $get_name = 'ftu';                                   // Session/GET-var name
00083 
00084         var $user_table = 'fe_users';                                   // Table in database with userdata
00085         var $username_column = 'username';                              // Column for login-name
00086         var $userident_column = 'password';                     // Column for password
00087         var $userid_column = 'uid';                                     // Column for user-id
00088         var $lastLogin_column = 'lastlogin';
00089 
00090         var $enablecolumns = Array (
00091                 'deleted' => 'deleted',
00092                 'disabled' => 'disable',
00093                 'starttime' => 'starttime',
00094                 'endtime' => 'endtime'
00095         );
00096         var $formfield_uname = 'user';                          // formfield with login-name
00097         var $formfield_uident = 'pass';                         // formfield with password
00098         var $formfield_chalvalue = 'challenge';         // formfield with a unique value which is used to encrypt the password and username
00099         var $formfield_status = 'logintype';            // formfield with status: *'login', 'logout'
00100         var $formfield_permanent = 'permalogin';        // formfield with 0 or 1 // 1 = permanent login enabled // 0 = session is valid for a browser session only
00101         var $security_level = '';                                       // sets the level of security. *'normal' = clear-text. 'challenged' = hashed password/username from form in $formfield_uident. 'superchallenged' = hashed password hashed again with username.
00102 
00103         var $auth_include = '';                                         // this is the name of the include-file containing the login form. If not set, login CAN be anonymous. If set login IS needed.
00104 
00105         var $auth_timeout_field = 6000;                         // Server session lifetime. If > 0: session-timeout in seconds. If false or <0: no timeout. If string: The string is a fieldname from the usertable where the timeout can be found.
00106 
00107         var $lifetime = 0;                              // Client session lifetime. 0 = Session-cookies. If session-cookies, the browser will stop the session when the browser is closed. Otherwise this specifies the lifetime of a cookie that keeps the session.
00108         var $sendNoCacheHeaders = 0;
00109         var $getFallBack = 1;                                           // If this is set, authentication is also accepted by the _GET. Notice that the identification is NOT 128bit MD5 hash but reduced. This is done in order to minimize the size for mobile-devices, such as WAP-phones
00110         var $hash_length = 10;
00111         var $getMethodEnabled = 1;                                      // Login may be supplied by url.
00112 
00113         var $usergroup_column = 'usergroup';
00114         var $usergroup_table = 'fe_groups';
00115         var $groupData = Array(
00116                 'title' =>Array(),
00117                 'uid' =>Array(),
00118                 'pid' =>Array()
00119         );
00120         var $TSdataArray=array();               // Used to accumulate the TSconfig data of the user
00121         var $userTS = array();
00122         var $userTSUpdated=0;
00123         var $showHiddenRecords=0;
00124 
00125                 // Session and user data:
00126                 /*
00127                         There are two types of data that can be stored: UserData and Session-Data. Userdata is for the login-user, and session-data for anyone viewing the pages.
00128                         'Keys' are keys in the internal dataarray of the data. When you get or set a key in one of the data-spaces (user or session) you decide the type of the variable (not object though)
00129                         'Reserved' keys are:
00130                                 - 'recs': Array: Used to 'register' records, eg in a shopping basket. Structure: [recs][tablename][record_uid]=number
00131                                 - sys: Reserved for TypoScript standard code.
00132                 */
00133         var $sesData = Array();
00134         var $sesData_change = 0;
00135         var $userData_change = 0;
00136 
00137 
00144         function start() {
00145                 if (intval($this->auth_timeout_field)>0 && intval($this->auth_timeout_field) < $this->lifetime) {
00146                                 // If server session timeout is non-zero but less than client session timeout: Copy this value instead.
00147                         $this->auth_timeout_field = $this->lifetime;
00148                 }
00149 
00150                 parent::start();
00151         }
00152 
00158         function getNewSessionRecord($tempuser) {
00159                 $insertFields = parent::getNewSessionRecord($tempuser);
00160                 $insertFields['ses_permanent'] = $this->is_permanent;
00161 
00162                 return $insertFields;
00163         }
00164 
00171         function isSetSessionCookie() {
00172                 $retVal = ($this->newSessionID || $this->forceSetCookie) && ($this->lifetime==0 || !$this->user['ses_permanent']);
00173                 return $retVal;
00174         }
00175 
00182         function isRefreshTimeBasedCookie() {
00183                 return $this->lifetime > 0 && $this->user['ses_permanent'];
00184         }
00185 
00192         function getLoginFormData() {
00193                 $loginData = parent::getLoginFormData();
00194                 if($GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'] == 0 || $GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'] == 1) {
00195                         if ($this->getMethodEnabled)    {
00196                                 $isPermanent = t3lib_div::_GP($this->formfield_permanent);
00197                         } else {
00198                                 $isPermanent = t3lib_div::_POST($this->formfield_permanent);
00199                         }
00200                         if(strlen($isPermanent) != 1) {
00201                                 $isPermanent = $GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'];
00202                         } elseif(!$isPermanent) {
00203                                 $this->forceSetCookie = true; // To make sure the user gets a session cookie and doesn't keep a possibly existing time based cookie, we need to force seeting the session cookie here
00204                         }
00205                         $isPermanent = $isPermanent?1:0;
00206                 } elseif($GLOBALS['TYPO3_CONF_VARS']['FE']['permalogin'] == 2) {
00207                         $isPermanent = 1;
00208                 } else {
00209                         $isPermanent = 0;
00210                 }
00211                 $loginData['permanent'] = $isPermanent;
00212                 $this->is_permanent = $isPermanent;
00213 
00214                 return $loginData;
00215         }
00216 
00223         function fetchGroupData()       {
00224                 $this->TSdataArray = array();
00225                 $this->userTS = array();
00226                 $this->userTSUpdated = 0;
00227                 $this->groupData = Array(
00228                         'title' => Array(),
00229                         'uid' => Array(),
00230                         'pid' => Array()
00231                 );
00232 
00233                         // Setting default configuration:
00234                 $this->TSdataArray[]=$GLOBALS['TYPO3_CONF_VARS']['FE']['defaultUserTSconfig'];
00235 
00236                         // get the info data for auth services
00237                 $authInfo = $this->getAuthInfoArray();
00238 
00239                 if ($this->writeDevLog)         t3lib_div::devLog('Get usergroups for user: '.t3lib_div::arrayToLogString($this->user, array($this->userid_column,$this->username_column)), 'tslib_feUserAuth');
00240 
00241                 $groupDataArr = array();
00242 
00243                         // use 'auth' service to find the groups for the user
00244                 $serviceChain='';
00245                 $subType = 'getGroups'.$this->loginType;
00246                 while (is_object($serviceObj = t3lib_div::makeInstanceService('auth', $subType, $serviceChain)))        {
00247                         $serviceChain.=','.$serviceObj->getServiceKey();
00248                         $serviceObj->initAuth($subType, array(), $authInfo, $this);
00249 
00250                         $groupData = $serviceObj->getGroups($this->user, $groupDataArr);
00251                         if (is_array($groupData) && count($groupData))  {
00252                                 $groupDataArr = t3lib_div::array_merge($groupDataArr, $groupData);      // Keys in $groupData should be unique ids of the groups (like "uid") so this function will override groups.
00253                         }
00254                         unset($serviceObj);
00255                 }
00256                 if ($this->writeDevLog AND $serviceChain)       t3lib_div::devLog($subType.' auth services called: '.$serviceChain, 'tslib_feUserAuth');
00257                 if ($this->writeDevLog AND !count($groupDataArr))       t3lib_div::devLog('No usergroups found by services', 'tslib_feUserAuth');
00258                 if ($this->writeDevLog AND count($groupDataArr))        t3lib_div::devLog(count($groupDataArr).' usergroup records found by services', 'tslib_feUserAuth');
00259 
00260 
00261                         // use 'auth' service to check the usergroups if they are really valid
00262                 foreach ($groupDataArr as $groupData)   {
00263                                 // by default a group is valid
00264                         $validGroup = TRUE;
00265 
00266                         $serviceChain='';
00267                         $subType = 'authGroups'.$this->loginType;
00268                         while (is_object($serviceObj = t3lib_div::makeInstanceService('auth', $subType, $serviceChain))) {
00269                                 $serviceChain.=','.$serviceObj->getServiceKey();
00270                                 $serviceObj->initAuth($subType, array(), $authInfo, $this);
00271 
00272                                 if (!$serviceObj->authGroup($this->user, $groupData)) {
00273                                         $validGroup = FALSE;
00274                                         if ($this->writeDevLog)         t3lib_div::devLog($subType.' auth service did not auth group: '.t3lib_div::arrayToLogString($groupData, 'uid,title'), 'tslib_feUserAuth', 2);
00275 
00276                                         break;
00277                                 }
00278                                 unset($serviceObj);
00279                         }
00280                         unset($serviceObj);
00281 
00282                         if ($validGroup) {
00283                                 $this->groupData['title'][$groupData['uid']]=$groupData['title'];
00284                                 $this->groupData['uid'][$groupData['uid']]=$groupData['uid'];
00285                                 $this->groupData['pid'][$groupData['uid']]=$groupData['pid'];
00286                                 $this->groupData['TSconfig'][$groupData['uid']]=$groupData['TSconfig'];
00287                         }
00288                 }
00289 
00290                 if (count($this->groupData) && count($this->groupData['TSconfig']))     {
00291                                 // TSconfig: collect it in the order it was collected
00292                         foreach($this->groupData['TSconfig'] as $TSdata)        {
00293                                 $this->TSdataArray[]=$TSdata;
00294                         }
00295 
00296                         $this->TSdataArray[]=$this->user['TSconfig'];
00297 
00298                                 // Sort information
00299                         ksort($this->groupData['title']);
00300                         ksort($this->groupData['uid']);
00301                         ksort($this->groupData['pid']);
00302                 }
00303 
00304                 return count($this->groupData['uid']) ? count($this->groupData['uid']) : 0;
00305         }
00306 
00313         function getUserTSconf()        {
00314                 if (!$this->userTSUpdated) {
00315                                 // Parsing the user TS (or getting from cache)
00316                         $this->TSdataArray = t3lib_TSparser::checkIncludeLines_array($this->TSdataArray);
00317                         $userTS = implode(chr(10).'[GLOBAL]'.chr(10),$this->TSdataArray);
00318                         $parseObj = t3lib_div::makeInstance('t3lib_TSparser');
00319                         $parseObj->parse($userTS);
00320                         $this->userTS = $parseObj->setup;
00321 
00322                         $this->userTSUpdated=1;
00323                 }
00324                 return $this->userTS;
00325         }
00326 
00327 
00328 
00329 
00330 
00331 
00332 
00333 
00334 
00335 
00336 
00337 
00338 
00339 
00340 
00341 
00342 
00343         /*****************************************
00344          *
00345          * Session data management functions
00346          *
00347          ****************************************/
00348 
00358         function fetchSessionData()     {
00359                 // Gets SesData if any
00360                 if ($this->id)  {
00361                         $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'fe_session_data', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'fe_session_data'));
00362                         if ($sesDataRow = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres))        {
00363                                 $this->sesData = unserialize($sesDataRow['content']);
00364                         }
00365                 }
00366                         // delete old data:
00367                 if ((rand()%100) <= 1) {                // a possibility of 1 % for garbage collection.
00368                         $GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_session_data', 'tstamp < '.intval(time()-3600*24));          // all data older than 24 hours are deleted.
00369                 }
00370         }
00371 
00380         function storeSessionData()     {
00381                         // Saves UC and SesData if changed.
00382                 if ($this->userData_change)     {
00383                         $this->writeUC('');
00384                 }
00385                 if ($this->sesData_change)      {
00386                         if ($this->id)  {
00387                                 $insertFields = array (
00388                                         'hash' => $this->id,
00389                                         'content' => serialize($this->sesData),
00390                                         'tstamp' => time()
00391                                 );
00392                                 $GLOBALS['TYPO3_DB']->exec_DELETEquery('fe_session_data', 'hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, 'fe_session_data'));
00393                                 $GLOBALS['TYPO3_DB']->exec_INSERTquery('fe_session_data', $insertFields);
00394                         }
00395                 }
00396         }
00397 
00406         function getKey($type,$key) {
00407                 if ($key)       {
00408                         switch($type)   {
00409                                 case 'user':
00410                                         return $this->uc[$key];
00411                                 break;
00412                                 case 'ses':
00413                                         return $this->sesData[$key];
00414                                 break;
00415                         }
00416                 }
00417         }
00418 
00431         function setKey($type,$key,$data)       {
00432                 if ($key)       {
00433                         switch($type)   {
00434                                 case 'user':
00435                                         if ($this->user['uid']) {
00436                                                 $this->uc[$key]=$data;
00437                                                 $this->userData_change=1;
00438                                         }
00439                                 break;
00440                                 case 'ses':
00441                                         $this->sesData[$key]=$data;
00442                                         $this->sesData_change=1;
00443                                 break;
00444                         }
00445                 }
00446         }
00447 
00457         function record_registration($recs,$maxSizeOfSessionData=0)     {
00458 
00459                         // Storing value ONLY if there is a confirmed cookie set (->cookieID), otherwise a shellscript could easily be spamming the fe_sessions table with bogus content and thus bloat the database
00460                 if (!$maxSizeOfSessionData || $this->cookieId===$this->id)      {
00461                         if ($recs['clear_all']) {
00462                                 $this->setKey('ses','recs','');
00463                         }
00464                         $change=0;
00465                         $recs_array=$this->getKey('ses','recs');
00466                         reset($recs);
00467                         while(list($table,$data)=each($recs))   {
00468                                 if (is_array($data))    {
00469                                         reset($data);
00470                                         while(list($rec_id,$value)=each($data)) {
00471                                                 if ($value != $recs_array[$table][$rec_id])     {
00472                                                         $recs_array[$table][$rec_id] = $value;
00473                                                         $change=1;
00474                                                 }
00475                                         }
00476                                 }
00477                         }
00478                         if ($change && (!$maxSizeOfSessionData || strlen(serialize($recs_array))<$maxSizeOfSessionData))        {
00479                                 $this->setKey('ses','recs',$recs_array);
00480                         }
00481                 }
00482         }
00483 }
00484 
00485 
00486 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_feuserauth.php'])        {
00487         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_feuserauth.php']);
00488 }
00489 ?>


Généré par L'expert TYPO3 avec  doxygen 1.4.6