Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2006 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 ***************************************************************/ 00088 require_once(t3lib_extMgm::extPath('sv').'class.tx_sv_authbase.php'); 00089 00090 00091 00092 00093 00094 00095 00096 00097 00111 class t3lib_userAuth { 00112 var $global_database = ''; // Which global database to connect to 00113 var $session_table = ''; // Table to use for session data. 00114 var $name = ''; // Session/Cookie name 00115 var $get_name = ''; // Session/GET-var name 00116 00117 var $user_table = ''; // Table in database with userdata 00118 var $username_column = ''; // Column for login-name 00119 var $userident_column = ''; // Column for password 00120 var $userid_column = ''; // Column for user-id 00121 var $lastLogin_column = ''; 00122 00123 var $enablecolumns = Array ( 00124 'rootLevel' => '', // Boolean: If true, 'AND pid=0' will be a part of the query... 00125 'disabled' => '', 00126 'starttime' => '', 00127 'endtime' => '', 00128 'deleted' => '' 00129 ); 00130 00131 var $formfield_uname = ''; // formfield with login-name 00132 var $formfield_uident = ''; // formfield with password 00133 var $formfield_chalvalue = ''; // formfield with a unique value which is used to encrypt the password and username 00134 var $formfield_status = ''; // formfield with status: *'login', 'logout'. If empty login is not verified. 00135 var $security_level = 'normal'; // sets the level of security. *'normal' = clear-text. 'challenged' = hashed password/username from form in $formfield_uident. 'superchallenged' = hashed password hashed again with username. 00136 00137 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. 00138 00139 var $auth_timeout_field = 0; // if > 0 : session-timeout in seconds. if string: The string is fieldname from the usertable where the timeout can be found. 00140 var $lifetime = 0; // 0 = Session-cookies. If session-cookies, the browser will stop session when the browser is closed. Else it keeps the session for $lifetime seconds. 00141 var $gc_time = 24; // GarbageCollection. Purge all session data older than $gc_time hours. 00142 var $gc_probability = 1; // Possibility (in percent) for GarbageCollection to be run. 00143 var $writeStdLog = FALSE; // Decides if the writelog() function is called at login and logout 00144 var $writeAttemptLog = FALSE; // If the writelog() functions is called if a login-attempt has be tried without success 00145 var $sendNoCacheHeaders = TRUE; // If this is set, headers is sent to assure, caching is NOT done 00146 var $getFallBack = FALSE; // 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 00147 var $hash_length = 32; // The ident-hash is normally 32 characters and should be! But if you are making sites for WAP-devices og other lowbandwidth stuff, you may shorten the length. Never let this value drop below 6. A length of 6 would give you more than 16 mio possibilities. 00148 var $getMethodEnabled = FALSE; // Setting this flag true lets user-authetication happen from GET_VARS if POST_VARS are not set. Thus you may supply username/password from the URL. 00149 var $lockIP = 4; // If set, will lock the session to the users IP address (all four numbers. Reducing to 1-3 means that only first, second or third part of the IP address is used). 00150 var $lockHashKeyWords = 'useragent'; // Keyword list (commalist with no spaces!): "useragent". Each keyword indicates some information that can be included in a integer hash made to lock down usersessions. 00151 00152 var $warningEmail = ''; // warning -emailaddress: 00153 var $warningPeriod = 3600; // Period back in time (in seconds) in which number of failed logins are collected 00154 var $warningMax = 3; // The maximum accepted number of warnings before an email is sent 00155 var $checkPid = TRUE; // If set, the user-record must $checkPid_value as pid 00156 var $checkPid_value=0; // The pid, the user-record must have as page-id 00157 00158 // Internals 00159 var $id; // Internal: Will contain session_id (MD5-hash) 00160 var $cookieId; // Internal: Will contain the session_id gotten from cookie or GET method. This is used in statistics as a reliable cookie (one which is known to come from $_COOKIE). 00161 var $loginFailure = FALSE; // Indicates if an authentication was started but failed 00162 var $loginSessionStarted = FALSE; // Will be set to true if the login session is actually written during auth-check. 00163 00164 var $user; // Internal: Will contain user- AND session-data from database (joined tables) 00165 var $get_URL_ID = ''; // Internal: Will will be set to the url--ready (eg. '&login=ab7ef8d...') GET-auth-var if getFallBack is true. Should be inserted in links! 00166 00167 var $newSessionID = FALSE; // Will be set to true if a new session ID was created 00168 var $forceSetCookie = FALSE; // Will force the session cookie to be set everytime (lifetime must be 0) 00169 var $dontSetCookie = FALSE; // Will prevent the setting of the session cookie (takes precedence over forceSetCookie) 00170 var $challengeStoredInCookie = FALSE; // If set, the challenge value will be stored in a session as well so the server can check that is was not forged. 00171 var $loginType = ''; // Login type, used for services. 00172 00173 var $svConfig = array(); // "auth" services configuration array from $TYPO3_CONF_VARS['SVCONF']['auth'] 00174 var $writeDevLog = FALSE; // write messages into the devlog? 00175 00176 00177 00178 00179 00180 00181 00182 00183 00195 function start() { 00196 global $TYPO3_CONF_VARS; 00197 00198 // backend or frontend login - used for auth services 00199 $this->loginType = ($this->name=='fe_typo_user') ? 'FE' : 'BE'; 00200 00201 // set level to normal if not already set 00202 $this->security_level = $this->security_level ? $this->security_level : 'normal'; 00203 00204 // enable dev logging if set 00205 if ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['writeDevLog']) $this->writeDevLog = TRUE; 00206 if ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['writeDevLog'.$this->loginType]) $this->writeDevLog = TRUE; 00207 if (TYPO3_DLOG) $this->writeDevLog = TRUE; 00208 00209 if ($this->writeDevLog) t3lib_div::devLog('## Beginning of auth logging.', 't3lib_userAuth'); 00210 00211 // Init vars. 00212 $mode = ''; 00213 $this->newSessionID = FALSE; 00214 // $id is set to ses_id if cookie is present. Else set to false, which will start a new session 00215 $id = isset($_COOKIE[$this->name]) ? stripslashes($_COOKIE[$this->name]) : ''; 00216 $this->hash_length = t3lib_div::intInRange($this->hash_length,6,32); 00217 $this->svConfig = $TYPO3_CONF_VARS['SVCONF']['auth']; 00218 00219 00220 // If fallback to get mode.... 00221 if (!$id && $this->getFallBack && $this->get_name) { 00222 $id = isset($_GET[$this->get_name]) ? t3lib_div::_GET($this->get_name) : ''; 00223 if (strlen($id)!=$this->hash_length) $id=''; 00224 $mode='get'; 00225 } 00226 $this->cookieId = $id; 00227 00228 // If new session... 00229 if (!$id) { 00230 // New random session-$id is made 00231 $id = substr(md5(uniqid('').getmypid()),0,$this->hash_length); 00232 // New session 00233 $this->newSessionID = TRUE; 00234 } 00235 00236 // Internal var 'id' is set 00237 $this->id = $id; 00238 00239 // If fallback to get mode.... 00240 if ($mode=='get' && $this->getFallBack && $this->get_name) { 00241 $this->get_URL_ID = '&'.$this->get_name.'='.$id; 00242 } 00243 // Make certain that NO user is set initially 00244 $this->user = ''; 00245 00246 // Setting cookies 00247 if ($TYPO3_CONF_VARS['SYS']['cookieDomain']) { 00248 if ($TYPO3_CONF_VARS['SYS']['cookieDomain']{0} == '/') { 00249 $matchCnt = @preg_match($TYPO3_CONF_VARS['SYS']['cookieDomain'], t3lib_div::getIndpEnv('TYPO3_HOST_ONLY'), $match); 00250 if ($matchCnt === FALSE) { 00251 t3lib_div::sysLog('The regular expression of $TYPO3_CONF_VARS[SYS][cookieDomain] contains errors. The session is not shared across sub-domains.', 'Core', 3); 00252 } elseif ($matchCnt) { 00253 $cookieDomain = $match[0]; 00254 } 00255 } else { 00256 $cookieDomain = $TYPO3_CONF_VARS['SYS']['cookieDomain']; 00257 } 00258 } 00259 00260 // If new session and the cookie is a sessioncookie, we need to set it only once! 00261 if (($this->newSessionID || $this->forceSetCookie) && $this->lifetime==0 ) { 00262 if (!$this->dontSetCookie) { 00263 if ($cookieDomain) { 00264 SetCookie($this->name, $id, 0, '/', $cookieDomain); 00265 } else { 00266 SetCookie($this->name, $id, 0, '/'); 00267 } 00268 if ($this->writeDevLog) t3lib_div::devLog('Set new Cookie: '.$id.($cookieDomain ? ', '.$cookieDomain : ''), 't3lib_userAuth'); 00269 } 00270 } 00271 00272 // If it is NOT a session-cookie, we need to refresh it. 00273 if ($this->lifetime > 0) { 00274 if (!$this->dontSetCookie) { 00275 if ($cookieDomain) { 00276 SetCookie($this->name, $id, time()+$this->lifetime, '/', $cookieDomain); 00277 } else { 00278 SetCookie($this->name, $id, time()+$this->lifetime, '/'); 00279 } 00280 if ($this->writeDevLog) t3lib_div::devLog('Update Cookie: '.$id.($cookieDomain ? ', '.$cookieDomain : ''), 't3lib_userAuth'); 00281 } 00282 } 00283 00284 // Check to see if anyone has submitted login-information and if so register the user with the session. $this->user[uid] may be used to write log... 00285 $this->checkAuthentication(); 00286 00287 // Make certain that NO user is set initially. ->check_authentication may have set a session-record which will provide us with a user record in the next section: 00288 unset($this->user); 00289 00290 // re-read user session 00291 $this->user = $this->fetchUserSession(); 00292 00293 if ($this->writeDevLog AND is_array($this->user)) t3lib_div::devLog('User session finally read: '.t3lib_div::arrayToLogString($this->user, array($this->userid_column,$this->username_column)), 't3lib_userAuth', -1); 00294 if ($this->writeDevLog AND !is_array($this->user)) t3lib_div::devLog('No user session found.', 't3lib_userAuth', 2); 00295 00296 // Hook for alternative ways of filling the $this->user array (is used by the "timtaw" extension) 00297 if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['postUserLookUp'])) { 00298 foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['postUserLookUp'] as $funcName) { 00299 $_params = array( 00300 'pObj' => &$this, 00301 ); 00302 t3lib_div::callUserFunction($funcName,$_params,$this); 00303 } 00304 } 00305 00306 // If any redirection (inclusion of file) then it will happen in this function 00307 $this->redirect(); 00308 00309 // Set all posible headers that could ensure that the script is not cached on the client-side 00310 if ($this->sendNoCacheHeaders) { 00311 header('Expires: 0'); 00312 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 00313 header('Cache-Control: no-cache, must-revalidate'); 00314 header('Pragma: no-cache'); 00315 } 00316 00317 // If we're lucky we'll get to clean up old sessions.... 00318 if ((rand()%100) <= $this->gc_probability) { 00319 $this->gc(); 00320 } 00321 } 00322 00329 function checkAuthentication() { 00330 00331 // No user for now - will be searched by service below 00332 $tempuserArr = array(); 00333 $tempuser = FALSE; 00334 00335 // User is not authenticated by default 00336 $authenticated = FALSE; 00337 00338 // User want to login with passed login data (name/password) 00339 $activeLogin = FALSE; 00340 00341 // Indicates if an active authentication failed (not auto login) 00342 $this->loginFailure = FALSE; 00343 00344 if ($this->writeDevLog) t3lib_div::devLog('Login type: '.$this->loginType, 't3lib_userAuth'); 00345 00346 // The info array provide additional information for auth services 00347 $authInfo = $this->getAuthInfoArray(); 00348 00349 // Get Login/Logout data submitted by a form or params 00350 $loginData = $this->getLoginFormData(); 00351 00352 if ($this->writeDevLog) t3lib_div::devLog('Login data: '.t3lib_div::arrayToLogString($loginData), 't3lib_userAuth'); 00353 00354 00355 // active logout (eg. with "logout" button) 00356 if ($loginData['status']=='logout') { 00357 if ($this->writeStdLog) $this->writelog(255,2,0,2,'User %s logged out',Array($this->user['username'])); // Logout written to log 00358 if ($this->writeDevLog) t3lib_div::devLog('User logged out. Id: '.$this->id, 't3lib_userAuth', -1); 00359 00360 $this->logoff(); 00361 } 00362 00363 // active login (eg. with login form) 00364 if ($loginData['status']=='login') { 00365 $activeLogin = TRUE; 00366 00367 if ($this->writeDevLog) t3lib_div::devLog('Active login (eg. with login form)', 't3lib_userAuth'); 00368 00369 // check referer for submitted login values 00370 if($this->formfield_status && $loginData['uident'] && $loginData['uname']) { 00371 $httpHost = t3lib_div::getIndpEnv('TYPO3_HOST_ONLY'); 00372 if (!$this->getMethodEnabled && ($httpHost!=$authInfo['refInfo']['host'] && !$GLOBALS['TYPO3_CONF_VARS']['SYS']['doNotCheckReferer'])) { 00373 die('Error: This host address ("'.$httpHost.'") and the referer host ("'.$authInfo['refInfo']['host'].'") mismatches!<br /> 00374 It\'s possible that the environment variable HTTP_REFERER is not passed to the script because of a proxy.<br /> 00375 The site administrator can disable this check in the "All Configuration" section of the Install Tool (flag: TYPO3_CONF_VARS[SYS][doNotCheckReferer]).'); 00376 } 00377 00378 // delete old user session if any 00379 $this->logoff(); 00380 } 00381 } 00382 00383 00384 // the following code makes auto-login possible (if configured). No submitted data needed 00385 00386 // re-read user session 00387 $authInfo['userSession'] = $this->fetchUserSession(); 00388 $haveSession = is_array($authInfo['userSession']) ? TRUE : FALSE; 00389 00390 if ($this->writeDevLog AND $haveSession) t3lib_div::devLog('User session found: '.t3lib_div::arrayToLogString($authInfo['userSession'], array($this->userid_column,$this->username_column)), 't3lib_userAuth', 0); 00391 if ($this->writeDevLog) t3lib_div::devLog('SV setup: '.t3lib_div::arrayToLogString($this->svConfig['setup']), 't3lib_userAuth', 0); 00392 00393 00394 // fetch user if ... 00395 if ( $activeLogin 00396 OR (!$haveSession AND $this->svConfig['setup'][$this->loginType.'_fetchUserIfNoSession']) 00397 OR $this->svConfig['setup'][$this->loginType.'_alwaysFetchUser']) { 00398 00399 // use 'auth' service to find the user 00400 // first found user will be used 00401 $serviceChain = ''; 00402 $subType = 'getUser'.$this->loginType; 00403 while (is_object($serviceObj = t3lib_div::makeInstanceService('auth', $subType, $serviceChain))) { 00404 $serviceChain.=','.$serviceObj->getServiceKey(); 00405 $serviceObj->initAuth($subType, $loginData, $authInfo, $this); 00406 if ($row=$serviceObj->getUser()) { 00407 $tempuserArr[] = $row; 00408 00409 if ($this->writeDevLog) t3lib_div::devLog('User found: '.t3lib_div::arrayToLogString($row, array($this->userid_column,$this->username_column)), 't3lib_userAuth', 0); 00410 00411 // user found, just stop to search for more if not configured to go on 00412 if(!$this->svConfig['setup'][$this->loginType.'_fetchAllUsers']) { 00413 break; 00414 } 00415 } 00416 unset($serviceObj); 00417 } 00418 unset($serviceObj); 00419 00420 if ($this->writeDevLog AND $this->svConfig['setup'][$this->loginType.'_alwaysFetchUser']) t3lib_div::devLog($this->loginType.'_alwaysFetchUser option is enabled', 't3lib_userAuth'); 00421 if ($this->writeDevLog AND $serviceChain) t3lib_div::devLog($subType.' auth services called: '.$serviceChain, 't3lib_userAuth'); 00422 if ($this->writeDevLog AND !count($tempuserArr)) t3lib_div::devLog('No user found by services', 't3lib_userAuth'); 00423 if ($this->writeDevLog AND count($tempuserArr)) t3lib_div::devLog(count($tempuserArr).' user records found by services', 't3lib_userAuth'); 00424 } 00425 00426 00427 // If no new user was set we use the already found user session 00428 if (!count($tempuserArr) AND $haveSession) { 00429 $tempuserArr[] = $authInfo['userSession']; 00430 $tempuser = $authInfo['userSession']; 00431 // User is authenticated because we found a user session 00432 $authenticated = TRUE; 00433 00434 if ($this->writeDevLog) t3lib_div::devLog('User session used: '.t3lib_div::arrayToLogString($authInfo['userSession'], array($this->userid_column,$this->username_column)), 't3lib_userAuth'); 00435 } 00436 00437 00438 // Re-auth user when 'auth'-service option is set 00439 if ($this->svConfig['setup'][$this->loginType.'_alwaysAuthUser']) { 00440 $authenticated = FALSE; 00441 if ($this->writeDevLog) t3lib_div::devLog('alwaysAuthUser option is enabled', 't3lib_userAuth'); 00442 } 00443 00444 00445 // Authenticate the user if needed 00446 if(count($tempuserArr) AND !$authenticated) { 00447 00448 foreach($tempuserArr as $tempuser) { 00449 00450 // use 'auth' service to authenticate the user 00451 // if one service returns FALSE then authentication failed 00452 // a service might return 100 which means there's no reason to stop but the user can't be authenticated by that service 00453 00454 if ($this->writeDevLog) t3lib_div::devLog('Auth user: '.t3lib_div::arrayToLogString($tempuser), 't3lib_userAuth'); 00455 00456 $serviceChain=''; 00457 $subType = 'authUser'.$this->loginType; 00458 while (is_object($serviceObj = t3lib_div::makeInstanceService('auth', $subType, $serviceChain))) { 00459 $serviceChain.=','.$serviceObj->getServiceKey(); 00460 $serviceObj->initAuth($subType, $loginData, $authInfo, $this); 00461 if (($ret=$serviceObj->authUser($tempuser)) > 0) { 00462 00463 // if the service returns >=200 then no more checking is needed - useful for IP checking without password 00464 if (intval($ret) >= 200) { 00465 $authenticated = TRUE; 00466 break; 00467 } elseif (intval($ret) >= 100) { 00468 // Just go on. User is still not authenticated but there's no reason to stop now. 00469 } else { 00470 $authenticated = TRUE; 00471 } 00472 00473 } else { 00474 $authenticated = FALSE; 00475 break; 00476 } 00477 unset($serviceObj); 00478 } 00479 unset($serviceObj); 00480 00481 if ($this->writeDevLog AND $serviceChain) t3lib_div::devLog($subType.' auth services called: '.$serviceChain, 't3lib_userAuth'); 00482 00483 if($authenticated) { 00484 // leave foreach() because a user is authenticated 00485 break; 00486 } 00487 } 00488 } 00489 00490 00491 // If user is authenticated a valid user is in $tempuser 00492 if ($authenticated) { 00493 // reset failure flag 00494 $this->loginFailure = FALSE; 00495 00496 00497 // Insert session record if needed: 00498 if (!($haveSession AND ( 00499 $tempuser['ses_id']==$this->id OR // check if the tempuser has the current session id 00500 $tempuser['uid']==$authInfo['userSession']['ses_userid'] // check if the tempuser has the uid of the fetched session user 00501 ))) { 00502 $this->createUserSession($tempuser); 00503 00504 // The login session is started. 00505 $this->loginSessionStarted = TRUE; 00506 } 00507 00508 // User logged in - write that to the log! 00509 if ($this->writeStdLog && $activeLogin) { 00510 $this->writelog(255,1,0,1, 00511 'User %s logged in from %s (%s)', 00512 Array($tempuser[$this->username_column], t3lib_div::getIndpEnv('REMOTE_ADDR'), t3lib_div::getIndpEnv('REMOTE_HOST')), 00513 '','','',-1,'',$tempuser['uid'] 00514 ); 00515 } 00516 00517 if ($this->writeDevLog && $activeLogin) t3lib_div::devLog('User '.$tempuser[$this->username_column].' logged in from '.t3lib_div::getIndpEnv('REMOTE_ADDR').' ('.t3lib_div::getIndpEnv('REMOTE_HOST').')', 't3lib_userAuth', -1); 00518 if ($this->writeDevLog && !$activeLogin) t3lib_div::devLog('User '.$tempuser[$this->username_column].' authenticated from '.t3lib_div::getIndpEnv('REMOTE_ADDR').' ('.t3lib_div::getIndpEnv('REMOTE_HOST').')', 't3lib_userAuth', -1); 00519 00520 if($GLOBALS['TYPO3_CONF_VARS']['BE']['lockSSL'] == 3 && $this->user_table == 'be_users') { 00521 $requestStr = substr(t3lib_div::getIndpEnv('TYPO3_REQUEST_SCRIPT'), strlen(t3lib_div::getIndpEnv('TYPO3_SITE_URL').TYPO3_mainDir)); 00522 if($requestStr == 'alt_main.php' && t3lib_div::getIndpEnv('TYPO3_SSL')) { 00523 list(,$url) = explode('://',t3lib_div::getIndpEnv('TYPO3_SITE_URL'),2); 00524 header('Location: http://'.$url.TYPO3_mainDir.'alt_main.php'); 00525 exit; 00526 } 00527 } 00528 00529 } elseif ($activeLogin OR count($tempuserArr)) { 00530 $this->loginFailure = TRUE; 00531 00532 if ($this->writeDevLog AND !count($tempuserArr) AND $activeLogin) t3lib_div::devLog('Login failed: '.t3lib_div::arrayToLogString($loginData), 't3lib_userAuth', 2); 00533 if ($this->writeDevLog AND count($tempuserArr)) t3lib_div::devLog('Login failed: '.t3lib_div::arrayToLogString($tempuser, array($this->userid_column,$this->username_column)), 't3lib_userAuth', 2); 00534 } 00535 00536 00537 // If there were a login failure, check to see if a warning email should be sent: 00538 if ($this->loginFailure AND $activeLogin) { 00539 if ($this->writeDevLog) t3lib_div::devLog('Call checkLogFailures: '.t3lib_div::arrayToLogString(array('warningEmail'=>$this->warningEmail,'warningPeriod'=>$this->warningPeriod,'warningMax'=>$this->warningMax,)), 't3lib_userAuth', -1); 00540 00541 $this->checkLogFailures($this->warningEmail, $this->warningPeriod, $this->warningMax); 00542 } 00543 } 00544 00545 00546 00547 00548 00549 00550 00551 00552 00553 00554 00555 00556 /************************* 00557 * 00558 * User Sessions 00559 * 00560 *************************/ 00561 00562 00569 function createUserSession ($tempuser) { 00570 00571 if ($this->writeDevLog) t3lib_div::devLog('Create session ses_id = '.$this->id, 't3lib_userAuth'); 00572 00573 // delete session entry first 00574 $GLOBALS['TYPO3_DB']->exec_DELETEquery( 00575 $this->session_table, 00576 'ses_id = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, $this->session_table).' 00577 AND ses_name = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->name, $this->session_table) 00578 ); 00579 00580 // re-create session entry 00581 $insertFields = array( 00582 'ses_id' => $this->id, 00583 'ses_name' => $this->name, 00584 'ses_iplock' => $tempuser['disableIPlock'] ? '[DISABLED]' : $this->ipLockClause_remoteIPNumber($this->lockIP), 00585 'ses_hashlock' => $this->hashLockClause_getHashInt(), 00586 'ses_userid' => $tempuser[$this->userid_column], 00587 'ses_tstamp' => $GLOBALS['EXEC_TIME'] 00588 ); 00589 $GLOBALS['TYPO3_DB']->exec_INSERTquery($this->session_table, $insertFields); 00590 00591 // Updating lastLogin_column carrying information about last login. 00592 if ($this->lastLogin_column) { 00593 $GLOBALS['TYPO3_DB']->exec_UPDATEquery( 00594 $this->user_table, 00595 $this->userid_column.'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($tempuser[$this->userid_column], $this->user_table), 00596 array($this->lastLogin_column => $GLOBALS['EXEC_TIME']) 00597 ); 00598 } 00599 } 00600 00606 function fetchUserSession() { 00607 00608 $user = ''; 00609 00610 if ($this->writeDevLog) t3lib_div::devLog('Fetch session ses_id = '.$this->id, 't3lib_userAuth'); 00611 00612 // The session_id is used to find user in the database. Two tables are joined: The session-table with user_id of the session and the usertable with its primary key 00613 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery( 00614 '*', 00615 $this->session_table.','.$this->user_table, 00616 $this->session_table.'.ses_id = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, $this->session_table).' 00617 AND '.$this->session_table.'.ses_name = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->name, $this->session_table).' 00618 AND '.$this->session_table.'.ses_userid = '.$this->user_table.'.'.$this->userid_column.' 00619 '.$this->ipLockClause().' 00620 '.$this->hashLockClause().' 00621 '.$this->user_where_clause() 00622 ); 00623 00624 00625 if ($user = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres)) { 00626 // A user was found 00627 if (is_string($this->auth_timeout_field)) { 00628 $timeout = intval($user[$this->auth_timeout_field]); // Get timeout-time from usertable 00629 } else { 00630 $timeout = intval($this->auth_timeout_field); // Get timeout from object 00631 } 00632 // If timeout > 0 (true) and currenttime has not exceeded the latest sessions-time plus the timeout in seconds then accept user 00633 // Option later on: We could check that last update was at least x seconds ago in order not to update twice in a row if one script redirects to another... 00634 if ($timeout>0 && ($GLOBALS['EXEC_TIME'] < ($user['ses_tstamp']+$timeout))) { 00635 $GLOBALS['TYPO3_DB']->exec_UPDATEquery( 00636 $this->session_table, 00637 'ses_id='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, $this->session_table).' 00638 AND ses_name='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->name, $this->session_table), 00639 array('ses_tstamp' => $GLOBALS['EXEC_TIME']) 00640 ); 00641 $user['ses_tstamp'] = $GLOBALS['EXEC_TIME']; // Make sure that the timestamp is also updated in the array 00642 } else { 00643 $this->logoff(); // delete any user set... 00644 } 00645 } else { 00646 $this->logoff(); // delete any user set... 00647 } 00648 return $user; 00649 } 00650 00657 function logoff() { 00658 if ($this->writeDevLog) t3lib_div::devLog('logoff: ses_id = '.$this->id, 't3lib_userAuth'); 00659 00660 // Hook for pre-processing the logoff() method, requested and implemented by andreas.otto@dkd.de: 00661 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'])) { 00662 $_params = array(); 00663 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'] as $_funcRef) { 00664 if ($_funcRef) { 00665 t3lib_div::callUserFunction($_funcRef,$_params,$this); 00666 } 00667 } 00668 } 00669 00670 $GLOBALS['TYPO3_DB']->exec_DELETEquery( 00671 $this->session_table, 00672 'ses_id = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->id, $this->session_table).' 00673 AND ses_name = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->name, $this->session_table) 00674 ); 00675 00676 $this->user = ''; 00677 00678 // Hook for post-processing the logoff() method, requested and implemented by andreas.otto@dkd.de: 00679 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_post_processing'])) { 00680 $_params = array(); 00681 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_post_processing'] as $_funcRef) { 00682 if ($_funcRef) { 00683 t3lib_div::callUserFunction($_funcRef,$_params,$this); 00684 } 00685 } 00686 } 00687 } 00688 00689 00690 00691 00692 00693 00694 00695 00696 00697 00698 00699 00700 00701 /************************* 00702 * 00703 * SQL Functions 00704 * 00705 *************************/ 00706 00713 function user_where_clause() { 00714 return (($this->enablecolumns['rootLevel']) ? 'AND '.$this->user_table.'.pid=0 ' : ''). 00715 (($this->enablecolumns['disabled']) ? ' AND '.$this->user_table.'.'.$this->enablecolumns['disabled'].'=0' : ''). 00716 (($this->enablecolumns['deleted']) ? ' AND '.$this->user_table.'.'.$this->enablecolumns['deleted'].'=0' : ''). 00717 (($this->enablecolumns['starttime']) ? ' AND ('.$this->user_table.'.'.$this->enablecolumns['starttime'].'<='.time().')' : ''). 00718 (($this->enablecolumns['endtime']) ? ' AND ('.$this->user_table.'.'.$this->enablecolumns['endtime'].'=0 OR '.$this->user_table.'.'.$this->enablecolumns['endtime'].'>'.time().')' : ''); 00719 } 00720 00727 function ipLockClause() { 00728 if ($this->lockIP) { 00729 $wherePart = 'AND ( 00730 '.$this->session_table.'.ses_iplock='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->ipLockClause_remoteIPNumber($this->lockIP),$this->session_table).' 00731 OR '.$this->session_table.'.ses_iplock=\'[DISABLED]\' 00732 )'; 00733 return $wherePart; 00734 } 00735 } 00736 00745 function ipLockClause_remoteIPNumber($parts) { 00746 $IP = t3lib_div::getIndpEnv('REMOTE_ADDR'); 00747 00748 if ($parts>=4) { 00749 return $IP; 00750 } else { 00751 $parts = t3lib_div::intInRange($parts,1,3); 00752 $IPparts = explode('.',$IP); 00753 for($a=4;$a>$parts;$a--) { 00754 unset($IPparts[$a-1]); 00755 } 00756 return implode('.',$IPparts); 00757 } 00758 } 00759 00766 function hashLockClause() { 00767 $wherePart = 'AND '.$this->session_table.'.ses_hashlock='.intval($this->hashLockClause_getHashInt()); 00768 return $wherePart; 00769 } 00770 00777 function hashLockClause_getHashInt() { 00778 $hashStr = ''; 00779 00780 if (t3lib_div::inList($this->lockHashKeyWords,'useragent')) $hashStr.=':'.t3lib_div::getIndpEnv('HTTP_USER_AGENT'); 00781 00782 return t3lib_div::md5int($hashStr); 00783 } 00784 00785 00786 00787 00788 00789 00790 00791 00792 00793 00794 00795 /************************* 00796 * 00797 * Session and Configuration Handling 00798 * 00799 *************************/ 00800 00809 function writeUC($variable='') { 00810 if (is_array($this->user) && $this->user[$this->userid_column]) { 00811 if (!is_array($variable)) { $variable = $this->uc; } 00812 00813 if ($this->writeDevLog) t3lib_div::devLog('writeUC: '.$this->userid_column.'='.intval($this->user[$this->userid_column]), 't3lib_userAuth'); 00814 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->user_table, $this->userid_column.'='.intval($this->user[$this->userid_column]), array('uc' => serialize($variable))); 00815 } 00816 } 00817 00824 function unpack_uc($theUC='') { 00825 if (!$theUC) $theUC=unserialize($this->user['uc']); 00826 if (is_array($theUC)) { 00827 $this->uc=$theUC; 00828 } 00829 } 00830 00840 function pushModuleData($module,$data,$noSave=0) { 00841 $this->uc['moduleData'][$module] = $data; 00842 $this->uc['moduleSessionID'][$module] = $this->id; 00843 if (!$noSave) $this->writeUC(); 00844 } 00845 00853 function getModuleData($module,$type='') { 00854 if ($type!='ses' || $this->uc['moduleSessionID'][$module]==$this->id) { 00855 return $this->uc['moduleData'][$module]; 00856 } 00857 } 00858 00866 function getSessionData($key) { 00867 $sesDat = unserialize($this->user['ses_data']); 00868 return $sesDat[$key]; 00869 } 00870 00879 function setAndSaveSessionData($key,$data) { 00880 $sesDat = unserialize($this->user['ses_data']); 00881 $sesDat[$key] = $data; 00882 $this->user['ses_data'] = serialize($sesDat); 00883 00884 if ($this->writeDevLog) t3lib_div::devLog('setAndSaveSessionData: ses_id = '.$this->user['ses_id'], 't3lib_userAuth'); 00885 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->session_table, 'ses_id='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->user['ses_id'], $this->session_table), array('ses_data' => $this->user['ses_data'])); 00886 } 00887 00888 00889 00890 00891 00892 00893 00894 00895 00896 00897 00898 00899 00900 /************************* 00901 * 00902 * Misc 00903 * 00904 *************************/ 00905 00912 function getLoginFormData() { 00913 $loginData=array(); 00914 if ($this->getMethodEnabled) { 00915 $loginData['status'] = t3lib_div::_GP($this->formfield_status); 00916 $loginData['uname'] = t3lib_div::_GP($this->formfield_uname); 00917 $loginData['uident'] = t3lib_div::_GP($this->formfield_uident); 00918 $loginData['chalvalue'] = t3lib_div::_GP($this->formfield_chalvalue); 00919 } else { 00920 $loginData['status'] = t3lib_div::_POST($this->formfield_status); 00921 $loginData['uname'] = t3lib_div::_POST($this->formfield_uname); 00922 $loginData['uident'] = t3lib_div::_POST($this->formfield_uident); 00923 $loginData['chalvalue'] = t3lib_div::_POST($this->formfield_chalvalue); 00924 } 00925 $loginData = $this->processLoginData($loginData); 00926 00927 return $loginData; 00928 } 00929 00939 function processLoginData($loginData, $security_level='') { 00940 global $TYPO3_CONF_VARS; 00941 00942 $loginSecurityLevel = $security_level ? $security_level : ($TYPO3_CONF_VARS[$this->loginType]['loginSecurityLevel'] ? $TYPO3_CONF_VARS[$this->loginType]['loginSecurityLevel'] : $this->security_level); 00943 00944 // Processing data according to the state it was submitted in. 00945 // ($loginSecurityLevel should reflect the security level used on the data being submitted in the login form) 00946 if ($loginSecurityLevel=='normal') { 00947 $loginData['uident_text'] = $loginData['uident']; 00948 $loginData['uident_challenged'] = (string)md5($loginData['uname'].':'.$loginData['uident'].':'.$loginData['chalvalue']); 00949 $loginData['uident_superchallenged'] = (string)md5($loginData['uname'].':'.(md5($loginData['uident'])).':'.$loginData['chalvalue']); 00950 } elseif ($loginSecurityLevel=='challenged') { 00951 $loginData['uident_text'] = ''; 00952 $loginData['uident_challenged'] = $loginData['uident']; 00953 $loginData['uident_superchallenged'] = ''; 00954 } elseif ($loginSecurityLevel=='superchallenged') { 00955 $loginData['uident_text'] = ''; 00956 $loginData['uident_challenged'] = ''; 00957 $loginData['uident_superchallenged'] = $loginData['uident']; 00958 } 00959 00960 // The password "uident" is set based on the internal security setting of TYPO3 00961 // Example: 00962 // $this->security_level for the backend must be "superchallenged" because passwords are stored as md5-hashes in the be_users table 00963 // $this->security_level for the frontend must be "normal" or "challenged" because passwords are stored as clear-text in the fe_users tables 00964 if ($this->security_level=='normal') { 00965 $loginData['uident'] = $loginData['uident_text']; 00966 } elseif ($this->security_level=='challenged') { 00967 $loginData['uident'] = $loginData['uident_challenged']; 00968 } elseif ($this->security_level=='superchallenged') { 00969 $loginData['uident'] = $loginData['uident_superchallenged']; 00970 } 00971 00972 return $loginData; 00973 } 00974 00981 function getAuthInfoArray() { 00982 $authInfo = array(); 00983 $authInfo['loginType'] = $this->loginType; 00984 $authInfo['refInfo'] = parse_url(t3lib_div::getIndpEnv('HTTP_REFERER')); 00985 $authInfo['HTTP_HOST'] = t3lib_div::getIndpEnv('HTTP_HOST'); 00986 $authInfo['REMOTE_ADDR'] = t3lib_div::getIndpEnv('REMOTE_ADDR'); 00987 $authInfo['REMOTE_HOST'] = t3lib_div::getIndpEnv('REMOTE_HOST'); 00988 $authInfo['security_level'] = $this->security_level; 00989 $authInfo['showHiddenRecords'] = $this->showHiddenRecords; 00990 // can be overidden in localconf by SVCONF: 00991 $authInfo['db_user']['table'] = $this->user_table; 00992 $authInfo['db_user']['userid_column'] = $this->userid_column; 00993 $authInfo['db_user']['username_column'] = $this->username_column; 00994 $authInfo['db_user']['userident_column'] = $this->userident_column; 00995 $authInfo['db_user']['usergroup_column'] = $this->usergroup_column; 00996 $authInfo['db_user']['enable_clause'] = $this->user_where_clause(); 00997 $authInfo['db_user']['checkPidList'] = $this->checkPid ? $this->checkPid_value : ''; 00998 $authInfo['db_user']['check_pid_clause'] = $this->checkPid ? ' AND pid IN ('.$GLOBALS['TYPO3_DB']->cleanIntList($authInfo['db_user']['checkPidList']).')' : ''; 00999 $authInfo['db_groups']['table'] = $this->usergroup_table; 01000 return $authInfo; 01001 } 01002 01011 function compareUident($user, $loginData, $security_level='') { 01012 01013 $OK = FALSE; 01014 $security_level = $security_level ? $security_level : $this->security_level; 01015 01016 switch ($security_level) { 01017 case 'superchallenged': // If superchallenged the password in the database ($user[$this->userident_column]) must be a md5-hash of the original password. 01018 case 'challenged': 01019 01020 // Check challenge stored in cookie: 01021 if ($this->challengeStoredInCookie) { 01022 session_start(); 01023 if ($_SESSION['login_challenge'] !== $loginData['chalvalue']) { 01024 if ($this->writeDevLog) t3lib_div::devLog('PHP Session stored challenge "'.$_SESSION['login_challenge'].'" and submitted challenge "'.$loginData['chalvalue'].'" did not match, so authentication failed!', 't3lib_userAuth', 2); 01025 $this->logoff(); 01026 return FALSE; 01027 } 01028 } 01029 01030 if ((string)$loginData['uident'] === (string)md5($user[$this->username_column].':'.$user[$this->userident_column].':'.$loginData['chalvalue'])) { 01031 $OK = TRUE; 01032 }; 01033 break; 01034 default: // normal 01035 if ((string)$loginData['uident'] === (string)$user[$this->userident_column]) { 01036 $OK = TRUE; 01037 }; 01038 break; 01039 } 01040 01041 return $OK; 01042 } 01043 01050 function gc() { 01051 $GLOBALS['TYPO3_DB']->exec_DELETEquery( 01052 $this->session_table, 01053 'ses_tstamp < '.intval(time()-($this->gc_time*60*60)).' 01054 AND ses_name = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->name, $this->session_table) 01055 ); 01056 } 01057 01064 function redirect() { 01065 if (!$this->userid && $this->auth_url) { // if no userid AND an include-document for login is given 01066 include ($this->auth_include); 01067 exit; 01068 } 01069 } 01070 01086 function writelog($type,$action,$error,$details_nr,$details,$data,$tablename,$recuid,$recpid) { 01087 } 01088 01095 function checkLogFailures() { 01096 } 01097 01108 function setBeUserByUid($uid) { 01109 $this->user = $this->getRawUserByUid($uid); 01110 } 01111 01120 function setBeUserByName($name) { 01121 $this->user = $this->getRawUserByName($name); 01122 } 01123 01131 function getRawUserByUid($uid) { 01132 $user = FALSE; 01133 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->user_table, 'uid='.intval($uid).' '.$this->user_where_clause()); 01134 if ($dbres) { 01135 $user = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres); 01136 $GLOBALS['TYPO3_DB']->sql_free_result($dbres); 01137 } 01138 return $user; 01139 } 01140 01149 function getRawUserByName($name) { 01150 $user = FALSE; 01151 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->user_table, 'username='.$GLOBALS['TYPO3_DB']->fullQuoteStr($name, $this->user_table).' '.$this->user_where_clause()); 01152 if ($dbres) { 01153 $user = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres); 01154 $GLOBALS['TYPO3_DB']->sql_free_result($dbres); 01155 } 01156 return $user; 01157 } 01158 01159 01160 01161 01162 01163 01164 01165 01166 01167 01168 01169 01170 01171 01172 01173 /************************* 01174 * 01175 * Create/update user - EXPERIMENTAL 01176 * 01177 *************************/ 01178 01188 function fetchUserRecord($dbUser, $username, $extraWhere='' ) { 01189 $user = FALSE; 01190 01191 $usernameClause = $username ? ($dbUser['username_column'].'='.$GLOBALS['TYPO3_DB']->fullQuoteStr($username, $dbUser['table'])) : ''; 01192 01193 if ($username OR $extraWhere) { 01194 01195 // Look up the user by the username and/or extraWhere: 01196 $dbres = $GLOBALS['TYPO3_DB']->exec_SELECTquery( 01197 '*', 01198 $dbUser['table'], 01199 $usernameClause. 01200 $dbUser['check_pid_clause']. 01201 $dbUser['enable_clause']. 01202 $extraWhere 01203 ); 01204 01205 if ($dbres) { 01206 $user = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($dbres); 01207 $GLOBALS['TYPO3_DB']->sql_free_result($dbres); 01208 } 01209 } 01210 return $user; 01211 } 01212 } 01213 01214 01215 01216 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_userauth.php']) { 01217 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_userauth.php']); 01218 } 01219 ?>