From 6038f9ed00aea775c33546a9756430a808f56827 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 5 Feb 2007 21:42:48 +0000 Subject: [PATCH] * Clean up session checks to better handle the case where the session was opened during the current request. May help with some caching corner cases. --- RELEASE-NOTES | 3 +++ includes/LoadBalancer.php | 3 +-- includes/OutputPage.php | 2 +- includes/RawPage.php | 4 +--- includes/Setup.php | 2 +- includes/Skin.php | 12 ++++++++++-- includes/SkinTemplate.php | 12 ++---------- includes/SpecialUserlogin.php | 12 +++++++++--- includes/WebRequest.php | 7 ++++++- includes/Wiki.php | 4 ++-- 10 files changed, 36 insertions(+), 25 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index e57f417269..8b84ab5d15 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -175,6 +175,9 @@ lighter making things easier to read. * Use faster AlphabeticPager for Special:Categories * (bug 8875) Show printable link in MonoBook sidebar for locally nonexistent pages; perhaps useful for categories and shared images +* Clean up session checks to better handle the case where the session was + opened during the current request. May help with some caching corner + cases. == Languages updated == diff --git a/includes/LoadBalancer.php b/includes/LoadBalancer.php index f256b7e7f8..d3d072eed3 100644 --- a/includes/LoadBalancer.php +++ b/includes/LoadBalancer.php @@ -502,8 +502,7 @@ class LoadBalancer { * Save master pos to the session and to memcached, if the session exists */ function saveMasterPos() { - global $wgSessionStarted; - if ( $wgSessionStarted && count( $this->mServers ) > 1 ) { + if ( session_id() != '' && count( $this->mServers ) > 1 ) { # If this entire request was served from a slave without opening a connection to the # master (however unlikely that may be), then we can fetch the position from the slave. if ( empty( $this->mConnections[0] ) ) { diff --git a/includes/OutputPage.php b/includes/OutputPage.php index e72e570d26..3fcef0fcc6 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -489,7 +489,7 @@ class OutputPage { # maintain different caches for logged-in users and non-logged in ones $wgRequest->response()->header( 'Vary: Accept-Encoding, Cookie' ); if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) { - if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) && + if( $wgUseSquid && session_id() == '' && ! $this->isPrintable() && $this->mSquidMaxage != 0 ) { if ( $wgUseESI ) { diff --git a/includes/RawPage.php b/includes/RawPage.php index 4353e94022..08130181ca 100644 --- a/includes/RawPage.php +++ b/includes/RawPage.php @@ -20,7 +20,6 @@ class RawPage { function __construct( &$article, $request = false ) { global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType; - global $wgUser; $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit'); $this->mArticle =& $article; @@ -83,8 +82,7 @@ class RawPage { // Output may contain user-specific data; vary for open sessions $this->mPrivateCache = ( $this->mSmaxage == 0 ) || - ( isset( $_COOKIE[ini_get( 'session.name' )] ) || - $wgUser->isLoggedIn() ); + ( session_id() != '' ); if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) { $this->mContentType = 'text/x-wiki'; diff --git a/includes/Setup.php b/includes/Setup.php index 6ed7c8de43..eaffbe1e5a 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -139,7 +139,7 @@ if ( $wgDBprefix ) { if( !ini_get( 'session.auto_start' ) ) session_name( $wgSessionName ? $wgSessionName : $wgCookiePrefix . '_session' ); -if( !$wgCommandLineMode && ( isset( $_COOKIE[session_name()] ) || isset( $_COOKIE[$wgCookiePrefix.'Token'] ) ) ) { +if( !$wgCommandLineMode && ( $wgRequest->checkSessionCookie() || isset( $_COOKIE[$wgCookiePrefix.'Token'] ) ) ) { wfIncrStats( 'request_with_session' ); wfSetupSession(); $wgSessionStarted = true; diff --git a/includes/Skin.php b/includes/Skin.php index 317097e7eb..09099e8b67 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -845,14 +845,22 @@ END; return $subpages; } + /** + * Returns true if the IP should be shown in the header + */ + function showIPinHeader() { + global $wgShowIPinHeader; + return $wgShowIPinHeader && session_id() != ''; + } + function nameAndLogin() { - global $wgUser, $wgTitle, $wgLang, $wgContLang, $wgShowIPinHeader; + global $wgUser, $wgTitle, $wgLang, $wgContLang; $lo = $wgContLang->specialPage( 'Userlogout' ); $s = ''; if ( $wgUser->isAnon() ) { - if( $wgShowIPinHeader && isset( $_COOKIE[ini_get('session.name')] ) ) { + if( $this->showIPinHeader() ) { $n = wfGetIP(); $tl = $this->makeKnownLinkObj( $wgUser->getTalkPage(), diff --git a/includes/SkinTemplate.php b/includes/SkinTemplate.php index ee3daec2be..382ac1f4bf 100644 --- a/includes/SkinTemplate.php +++ b/includes/SkinTemplate.php @@ -488,7 +488,7 @@ class SkinTemplate extends Skin { * @private */ function buildPersonalUrls() { - global $wgTitle, $wgShowIPinHeader; + global $wgTitle; $fname = 'SkinTemplate::buildPersonalUrls'; $pageurl = $wgTitle->getLocalURL(); @@ -538,7 +538,7 @@ class SkinTemplate extends Skin { 'active' => false ); } else { - if( $wgShowIPinHeader && isset( $_COOKIE[ini_get("session.name")] ) ) { + if( $this->showIPinHeader() ) { $href = &$this->userpageUrlDetails['href']; $personal_urls['anonuserpage'] = array( 'text' => $this->username, @@ -574,14 +574,6 @@ class SkinTemplate extends Skin { return $personal_urls; } - /** - * Returns true if the IP should be shown in the header - */ - function showIPinHeader() { - global $wgShowIPinHeader; - return $wgShowIPinHeader && isset( $_COOKIE[ini_get("session.name")] ); - } - function tabAction( $title, $message, $selected, $query='', $checkEdit=false ) { $classes = array(); if( $selected ) { diff --git a/includes/SpecialUserlogin.php b/includes/SpecialUserlogin.php index f14eede58c..8b652aab02 100644 --- a/includes/SpecialUserlogin.php +++ b/includes/SpecialUserlogin.php @@ -10,7 +10,7 @@ function wfSpecialUserlogin() { global $wgCommandLineMode; global $wgRequest; - if( !$wgCommandLineMode && !isset( $_COOKIE[session_name()] ) ) { + if( session_id() == '' ) { wfSetupSession(); } @@ -707,11 +707,17 @@ class LoginForm { } /** + * Check if a session cookie is present. + * + * This will not pick up a cookie set during _this_ request, but is + * meant to ensure that the client is returning the cookie which was + * set on a previous pass through the system. + * * @private */ function hasSessionCookie() { - global $wgDisableCookieCheck; - return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) ); + global $wgDisableCookieCheck, $wgRequest; + return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie(); } /** diff --git a/includes/WebRequest.php b/includes/WebRequest.php index 8dfc846135..b08fd4e4df 100644 --- a/includes/WebRequest.php +++ b/includes/WebRequest.php @@ -301,10 +301,15 @@ class WebRequest { * Returns true if there is a session cookie set. * This does not necessarily mean that the user is logged in! * + * If you want to check for an open session, use session_id() + * instead; that will also tell you if the session was opened + * during the current request (in which case the cookie will + * be sent back to the client at the end of the script run). + * * @return bool */ function checkSessionCookie() { - return isset( $_COOKIE[ini_get('session.name')] ); + return isset( $_COOKIE[session_name()] ); } /** diff --git a/includes/Wiki.php b/includes/Wiki.php index e5849a1a2f..85963f5b82 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -399,9 +399,9 @@ class MediaWiki { showCreditsPage( $article ); break; case 'submit': - if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) { + if( session_id() == '' ) { /* Send a cookie so anons get talk message notifications */ - User::SetupSession(); + wfSetupSession(); } /* Continue... */ case 'edit': -- 2.20.1