[BUGFIX] Send no-cache headers in t3lib_userauth
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_userauth.php
index 4fa3301..3802b74 100644 (file)
  * @author     Kasper Skårhøj <kasperYYYY@typo3.com>
  * @author     René Fritz <r.fritz@colorcube.de>
  */
-/**
- * [CLASS/FUNCTION INDEX of SCRIPT]
- *
- *
- *
- *  111: class t3lib_userAuth
- *  195:        function start()
- *  329:        function checkAuthentication()
- *
- *                       SECTION: User Sessions
- *  569:        function createUserSession ($tempuser)
- *  606:        function fetchUserSession()
- *  657:        function logoff()
- *
- *                       SECTION: SQL Functions
- *  713:        function user_where_clause()
- *  727:        function ipLockClause()
- *  745:        function ipLockClause_remoteIPNumber($parts)
- *  766:        function hashLockClause()
- *  777:        function hashLockClause_getHashInt()
- *
- *                       SECTION: Session and Configuration Handling
- *  809:        function writeUC($variable='')
- *  824:        function unpack_uc($theUC='')
- *  840:        function pushModuleData($module,$data,$noSave=0)
- *  853:        function getModuleData($module,$type='')
- *  866:        function getSessionData($key)
- *  879:        function setAndSaveSessionData($key,$data)
- *
- *                       SECTION: Misc
- *  912:        function getLoginFormData()
- *  939:        function processLoginData($loginData, $security_level='')
- *  981:        function getAuthInfoArray()
- * 1011:        function compareUident($user, $loginData, $security_level='')
- * 1050:        function gc()
- * 1064:        function redirect()
- * 1086:        function writelog($type,$action,$error,$details_nr,$details,$data,$tablename,$recuid,$recpid)
- * 1095:        function checkLogFailures()
- * 1108:        function setBeUserByUid($uid)
- * 1120:        function setBeUserByName($name)
- * 1131:        function getRawUserByUid($uid)
- * 1149:        function getRawUserByName($name)
- *
- *                       SECTION: Create/update user - EXPERIMENTAL
- * 1188:        function fetchUserRecord($dbUser, $username, $extraWhere='' )
- *
- * TOTAL FUNCTIONS: 29
- * (This index is automatically created/updated by the extension "extdeveval")
- *
- */
 
 
 require_once(t3lib_extMgm::extPath('sv') . 'class.tx_sv_authbase.php');
@@ -124,7 +74,15 @@ abstract class t3lib_userAuth {
        var $formfield_uident = ''; // formfield with password
        var $formfield_chalvalue = ''; // formfield with a unique value which is used to encrypt the password and username
        var $formfield_status = ''; // formfield with status: *'login', 'logout'. If empty login is not verified.
-       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.
+
+       /**
+        * Sets the level of security. *'normal' = clear-text. 'challenged' = hashed password/username.
+        * from form in $formfield_uident. 'superchallenged' = hashed password hashed again with username.
+        *
+        * @var string
+        * @deprecated since 4.7 will be removed in 4.9
+        */
+       public $security_level = 'normal';
 
        var $auth_timeout_field = 0; // 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.
        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.
@@ -137,7 +95,7 @@ abstract class t3lib_userAuth {
        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.
        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.
        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).
-       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. Configurable through $TYPO3_CONF_VARS[TYPO3_MODE]['lockHashKeyWords']
+       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. Configurable through $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['lockHashKeyWords']
 
        var $warningEmail = ''; // warning -emailaddress:
        var $warningPeriod = 3600; // Period back in time (in seconds) in which number of failed logins are collected
@@ -160,7 +118,7 @@ abstract class t3lib_userAuth {
        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.
        var $loginType = ''; // Login type, used for services.
 
-       var $svConfig = array(); // "auth" services configuration array from $TYPO3_CONF_VARS['SVCONF']['auth']
+       var $svConfig = array(); // "auth" services configuration array from $GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth']
        var $writeDevLog = FALSE; // write messages into the devlog?
 
 
@@ -176,8 +134,6 @@ abstract class t3lib_userAuth {
         * @return      void
         */
        function start() {
-               global $TYPO3_CONF_VARS;
-
                        // backend or frontend login - used for auth services
                if (empty($this->loginType)) {
                        throw new t3lib_exception('No loginType defined, should be set explicitly by subclass');
@@ -186,17 +142,17 @@ abstract class t3lib_userAuth {
                        // set level to normal if not already set
                if (!$this->security_level) {
                                // Notice: cannot use TYPO3_MODE here because BE user can be logged in and operate inside FE!
-                       $this->security_level = trim($TYPO3_CONF_VARS[$this->loginType]['loginSecurityLevel']);
+                       $this->security_level = trim($GLOBALS['TYPO3_CONF_VARS'][$this->loginType]['loginSecurityLevel']);
                        if (!$this->security_level) {
                                $this->security_level = 'normal';
                        }
                }
 
                        // enable dev logging if set
-               if ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['writeDevLog']) {
+               if ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['writeDevLog']) {
                        $this->writeDevLog = TRUE;
                }
-               if ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['writeDevLog' . $this->loginType]) {
+               if ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['writeDevLog' . $this->loginType]) {
                        $this->writeDevLog = TRUE;
                }
                if (TYPO3_DLOG) {
@@ -212,7 +168,7 @@ abstract class t3lib_userAuth {
                $this->newSessionID = FALSE;
                        // $id is set to ses_id if cookie is present. Else set to FALSE, which will start a new session
                $id = $this->getCookie($this->name);
-               $this->svConfig = $TYPO3_CONF_VARS['SVCONF']['auth'];
+               $this->svConfig = $GLOBALS['TYPO3_CONF_VARS']['SVCONF']['auth'];
 
                        // if we have a flash client, take the ID from the GP
                if (!$id && $GLOBALS['CLIENT']['BROWSER'] == 'flash') {
@@ -246,11 +202,36 @@ abstract class t3lib_userAuth {
                }
 
                        // Set session hashKey lock keywords from configuration; currently only 'useragent' can be used.
-               $this->lockHashKeyWords = $TYPO3_CONF_VARS[$this->loginType]['lockHashKeyWords'];
+               $this->lockHashKeyWords = $GLOBALS['TYPO3_CONF_VARS'][$this->loginType]['lockHashKeyWords'];
 
                        // Make certain that NO user is set initially
                $this->user = '';
 
+                       // Set all posible headers that could ensure that the script is not cached on the client-side
+               if ($this->sendNoCacheHeaders) {
+                       header('Expires: 0');
+                       header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
+
+                       $cacheControlHeader = 'no-cache, must-revalidate';
+                       $pragmaHeader = 'no-cache';
+
+                               // Prevent error message in IE when using a https connection
+                               // see http://forge.typo3.org/issues/24125
+                       $clientInfo = t3lib_div::clientInfo();
+                       if (($clientInfo['BROWSER'] === 'msie') && t3lib_div::getIndpEnv('TYPO3_SSL')) {
+
+                                               // Some IEs can not handle no-cache
+                                               // see http://support.microsoft.com/kb/323308/en-us
+                                       $cacheControlHeader = 'must-revalidate';
+
+                                               // IE needs "Pragma: private" if SSL connection
+                                       $pragmaHeader = 'private';
+                       }
+
+                       header('Cache-Control: ' . $cacheControlHeader);
+                       header('Pragma: ' . $pragmaHeader);
+               }
+
                        // 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...
                $this->checkAuthentication();
 
@@ -282,8 +263,8 @@ abstract class t3lib_userAuth {
                }
 
                        // Hook for alternative ways of filling the $this->user array (is used by the "timtaw" extension)
-               if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['postUserLookUp'])) {
-                       foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['postUserLookUp'] as $funcName) {
+               if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['postUserLookUp'])) {
+                       foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['postUserLookUp'] as $funcName) {
                                $_params = array(
                                        'pObj' => &$this,
                                );
@@ -291,14 +272,6 @@ abstract class t3lib_userAuth {
                        }
                }
 
-                       // Set all posible headers that could ensure that the script is not cached on the client-side
-               if ($this->sendNoCacheHeaders) {
-                       header('Expires: 0');
-                       header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
-                       header('Cache-Control: no-cache, must-revalidate');
-                       header('Pragma: no-cache');
-               }
-
                        // Set $this->gc_time if not explicitely specified
                if ($this->gc_time == 0) {
                        $this->gc_time = ($this->auth_timeout_field == 0 ? 86400 : $this->auth_timeout_field); // Default to 1 day if $this->auth_timeout_field is 0
@@ -361,7 +334,7 @@ abstract class t3lib_userAuth {
 
        /**
         * Gets the domain to be used on setting cookies.
-        * The information is taken from the value in $TYPO3_CONF_VARS[SYS][cookieDomain].
+        * The information is taken from the value in $GLOBALS['TYPO3_CONF_VARS']['SYS']['cookieDomain'].
         *
         * @return      string          The domain to be used on setting cookies
         */
@@ -822,10 +795,10 @@ abstract class t3lib_userAuth {
 
                if ($statement && $user) {
                                // A user was found
-                       if (is_string($this->auth_timeout_field)) {
-                               $timeout = intval($user[$this->auth_timeout_field]); // Get timeout-time from usertable
-                       } else {
+                       if (t3lib_Utility_Math::canBeInterpretedAsInteger($this->auth_timeout_field)) {
                                $timeout = intval($this->auth_timeout_field); // Get timeout from object
+                       } else {
+                               $timeout = intval($user[$this->auth_timeout_field]); // Get timeout-time from usertable
                        }
                                // If timeout > 0 (TRUE) and currenttime has not exceeded the latest sessions-time plus the timeout in seconds then accept user
                                // 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...
@@ -860,6 +833,9 @@ abstract class t3lib_userAuth {
                        t3lib_div::devLog('logoff: ses_id = ' . $this->id, 't3lib_userAuth');
                }
 
+                       // Release the locked records
+               t3lib_BEfunc::lockRecords();
+
                        // Hook for pre-processing the logoff() method, requested and implemented by andreas.otto@dkd.de:
                if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'])) {
                        $_params = array();
@@ -1021,7 +997,7 @@ abstract class t3lib_userAuth {
                if ($parts >= 4) {
                        return $IP;
                } else {
-                       $parts = t3lib_div::intInRange($parts, 1, 3);
+                       $parts = t3lib_utility_Math::forceIntegerInRange($parts, 1, 3);
                        $IPparts = explode('.', $IP);
                        for ($a = 4; $a > $parts; $a--) {
                                unset($IPparts[$a - 1]);
@@ -1204,44 +1180,48 @@ abstract class t3lib_userAuth {
 
        /**
         * Processes Login data submitted by a form or params depending on the
-        * security_level
+        * passwordTransmissionStrategy
         *
         * @param       array           login data array
-        * @param       string          Alternative security_level. Used when authentication services wants to override the default.
+        * @param       string          Alternative passwordTransmissionStrategy. Used when authentication services wants to override the default.
         * @return      array           processed login data array
         * @internal
         */
-       function processLoginData($loginData, $security_level = '') {
-               global $TYPO3_CONF_VARS;
-
-               $loginSecurityLevel = $security_level ? $security_level : ($TYPO3_CONF_VARS[$this->loginType]['loginSecurityLevel'] ? $TYPO3_CONF_VARS[$this->loginType]['loginSecurityLevel'] : $this->security_level);
-
-                       // Processing data according to the state it was submitted in.
-                       // ($loginSecurityLevel should reflect the security level used on the data being submitted in the login form)
-               if ($loginSecurityLevel == 'normal') {
-                       $loginData['uident_text'] = $loginData['uident'];
-                       $loginData['uident_challenged'] = (string) md5($loginData['uname'] . ':' . $loginData['uident'] . ':' . $loginData['chalvalue']);
-                       $loginData['uident_superchallenged'] = (string) md5($loginData['uname'] . ':' . (md5($loginData['uident'])) . ':' . $loginData['chalvalue']);
-               } elseif ($loginSecurityLevel == 'challenged') {
-                       $loginData['uident_text'] = '';
-                       $loginData['uident_challenged'] = $loginData['uident'];
-                       $loginData['uident_superchallenged'] = '';
-               } elseif ($loginSecurityLevel == 'superchallenged') {
-                       $loginData['uident_text'] = '';
-                       $loginData['uident_challenged'] = '';
-                       $loginData['uident_superchallenged'] = $loginData['uident'];
+       function processLoginData($loginData, $passwordTransmissionStrategy = '') {
+               $passwordTransmissionStrategy = $passwordTransmissionStrategy ? $passwordTransmissionStrategy : ($GLOBALS['TYPO3_CONF_VARS'][$this->loginType]['loginSecurityLevel'] ? trim($GLOBALS['TYPO3_CONF_VARS'][$this->loginType]['loginSecurityLevel']) : $this->security_level);
+
+               if ($this->writeDevLog) {
+                       t3lib_div::devLog('Login data before processing: ' . t3lib_div::arrayToLogString($loginData), 't3lib_userAuth');
                }
 
-                       // The password "uident" is set based on the internal security setting of TYPO3
-                       // Example:
-                       // $this->security_level for the backend must be "superchallenged" because passwords are stored as md5-hashes in the be_users table
-                       // $this->security_level for the frontend must be "normal" or "challenged" because passwords are stored as clear-text in the fe_users tables
-               if ($this->security_level == 'normal') {
-                       $loginData['uident'] = $loginData['uident_text'];
-               } elseif ($this->security_level == 'challenged') {
-                       $loginData['uident'] = $loginData['uident_challenged'];
-               } elseif ($this->security_level == 'superchallenged') {
-                       $loginData['uident'] = $loginData['uident_superchallenged'];
+               $serviceChain = '';
+               $subType = 'processLoginData' . $this->loginType;
+               $authInfo = $this->getAuthInfoArray();
+               $isLoginDataProcessed = FALSE;
+               $processedLoginData = $loginData;
+               while (is_object($serviceObject = t3lib_div::makeInstanceService('auth', $subType, $serviceChain))) {
+                       $serviceChain .= ',' . $serviceObject->getServiceKey();
+                       $serviceObject->initAuth($subType, $loginData, $authInfo, $this);
+
+                       $serviceResult = $serviceObject->processLoginData($processedLoginData, $passwordTransmissionStrategy);
+
+                       if (!empty($serviceResult)) {
+                               $isLoginDataProcessed = TRUE;
+
+                                       // if the service returns >=200 then no more processing is needed
+                               if (intval($serviceResult) >= 200)      {
+                                       unset($serviceObject);
+                                       break;
+                               }
+                       }
+                       unset($serviceObject);
+               }
+
+               if ($isLoginDataProcessed) {
+                       $loginData = $processedLoginData;
+                       if ($this->writeDevLog) {
+                               t3lib_div::devLog('Processed login data: '.t3lib_div::arrayToLogString($processedLoginData), 't3lib_userAuth');
+                       }
                }
 
                return $loginData;
@@ -1260,6 +1240,7 @@ abstract class t3lib_userAuth {
                $authInfo['HTTP_HOST'] = t3lib_div::getIndpEnv('HTTP_HOST');
                $authInfo['REMOTE_ADDR'] = t3lib_div::getIndpEnv('REMOTE_ADDR');
                $authInfo['REMOTE_HOST'] = t3lib_div::getIndpEnv('REMOTE_HOST');
+               /** @deprecated the usage of $authInfo['security_level'] is deprecated since 4.7 */
                $authInfo['security_level'] = $this->security_level;
                $authInfo['showHiddenRecords'] = $this->showHiddenRecords;
                        // can be overidden in localconf by SVCONF:
@@ -1280,15 +1261,15 @@ abstract class t3lib_userAuth {
         *
         * @param       array           user data array
         * @param       array           login data array
-        * @param       string          Alternative security_level. Used when authentication services wants to override the default.
+        * @param       string          Alternative passwordCompareStrategy. Used when authentication services wants to override the default.
         * @return      boolean         TRUE if login data matched
         */
-       function compareUident($user, $loginData, $security_level = '') {
+       function compareUident($user, $loginData, $passwordCompareStrategy = '') {
 
                $OK = FALSE;
-               $security_level = $security_level ? $security_level : $this->security_level;
+               $passwordCompareStrategy = $passwordCompareStrategy ? $passwordCompareStrategy : $this->security_level;
 
-               switch ($security_level) {
+               switch ($passwordCompareStrategy) {
                        case 'superchallenged': // If superchallenged the password in the database ($user[$this->userident_column]) must be a md5-hash of the original password.
                        case 'challenged':
 
@@ -1304,12 +1285,12 @@ abstract class t3lib_userAuth {
                                        }
                                }
 
-                               if ((string) $loginData['uident'] === (string) md5($user[$this->username_column] . ':' . $user[$this->userident_column] . ':' . $loginData['chalvalue'])) {
+                               if ((string) $loginData['uident_' . $passwordCompareStrategy] === (string) md5($user[$this->username_column] . ':' . $user[$this->userident_column] . ':' . $loginData['chalvalue'])) {
                                        $OK = TRUE;
                                }
                        break;
                        default: // normal
-                               if ((string) $loginData['uident'] === (string) $user[$this->userident_column]) {
+                               if ((string) $loginData['uident_text'] === (string) $user[$this->userident_column]) {
                                        $OK = TRUE;
                                }
                        break;
@@ -1353,10 +1334,13 @@ abstract class t3lib_userAuth {
        /**
         * DUMMY: Check login failures (in some extension classes)
         *
-        * @return      void
+        * @param string $email Email address
+        * @param integer $secondsBack Number of sections back in time to check. This is a kind of limit for how many failures an hour for instance
+        * @param integer $maxFailures Max allowed failures before a warning mail is sent
+        * @return void
         * @ignore
         */
-       function checkLogFailures() {
+       function checkLogFailures($email, $secondsBack, $maxFailures) {
        }
 
        /**
@@ -1467,4 +1451,4 @@ if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLA
        include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_userauth.php']);
 }
 
-?>
\ No newline at end of file
+?>