Merge "SpecialNewpages: add $attribs['class'] immediately before creating <li>"
[lhc/web/wiklou.git] / includes / session / CookieSessionProvider.php
index 8ce3174..74925bd 100644 (file)
@@ -217,19 +217,13 @@ class CookieSessionProvider extends SessionProvider {
                        [ 'prefix' => '' ] + $options
                );
 
-               $extendedCookies = $this->config->get( 'ExtendedLoginCookies' );
-               $extendedExpiry = $this->config->get( 'ExtendedLoginCookieExpiration' );
-
                foreach ( $cookies as $key => $value ) {
                        if ( $value === false ) {
                                $response->clearCookie( $key, $options );
                        } else {
-                               if ( $extendedExpiry !== null && in_array( $key, $extendedCookies ) ) {
-                                       $expiry = time() + (int)$extendedExpiry;
-                               } else {
-                                       $expiry = 0; // Default cookie expiration
-                               }
-                               $response->setCookie( $key, (string)$value, $expiry, $options );
+                               $expirationDuration = $this->getLoginCookieExpiration( $key, $session->shouldRememberUser() );
+                               $expiration = $expirationDuration ? $expirationDuration + time() : null;
+                               $response->setCookie( $key, (string)$value, $expiration, $options );
                        }
                }
 
@@ -276,7 +270,16 @@ class CookieSessionProvider extends SessionProvider {
        ) {
                $response = $request->response();
                if ( $set ) {
-                       $response->setCookie( 'forceHTTPS', 'true', $backend->shouldRememberUser() ? 0 : null,
+                       if ( $backend->shouldRememberUser() ) {
+                               $expirationDuration = $this->getLoginCookieExpiration(
+                                       'forceHTTPS',
+                                       true
+                               );
+                               $expiration = $expirationDuration ? $expirationDuration + time() : null;
+                       } else {
+                               $expiration = null;
+                       }
+                       $response->setCookie( 'forceHTTPS', 'true', $expiration,
                                [ 'prefix' => '', 'secure' => false ] + $this->cookieOptions );
                } else {
                        $response->clearCookie( 'forceHTTPS',
@@ -396,4 +399,41 @@ class CookieSessionProvider extends SessionProvider {
                return wfMessage( 'sessionprovider-nocookies' );
        }
 
+       public function getRememberUserDuration() {
+               return min( $this->getLoginCookieExpiration( 'UserID', true ),
+                       $this->getLoginCookieExpiration( 'Token', true ) ) ?: null;
+       }
+
+       /**
+        * Gets the list of cookies that must be set to the 'remember me' duration,
+        * if $wgExtendedLoginCookieExpiration is in use.
+        *
+        * @return string[] Array of unprefixed cookie keys
+        */
+       protected function getExtendedLoginCookies() {
+               return [ 'UserID', 'UserName', 'Token' ];
+       }
+
+       /**
+        * Returns the lifespan of the login cookies, in seconds. 0 means until the end of the session.
+        *
+        * Cookies that are session-length do not call this function.
+        *
+        * @param string $cookieName
+        * @param bool $shouldRememberUser Whether the user should be remembered
+        *   long-term
+        * @return int Cookie expiration time in seconds; 0 for session cookies
+        */
+       protected function getLoginCookieExpiration( $cookieName, $shouldRememberUser ) {
+               $extendedCookies = $this->getExtendedLoginCookies();
+               $normalExpiration = $this->config->get( 'CookieExpiration' );
+
+               if ( $shouldRememberUser && in_array( $cookieName, $extendedCookies, true ) ) {
+                       $extendedExpiration = $this->config->get( 'ExtendedLoginCookieExpiration' );
+
+                       return ( $extendedExpiration !== null ) ? (int)$extendedExpiration : (int)$normalExpiration;
+               } else {
+                       return (int)$normalExpiration;
+               }
+       }
 }