Index: update.php =================================================================== RCS file: /cvs/drupal/drupal/update.php,v retrieving revision 1.268 diff -u -9 -p -r1.268 update.php --- update.php 20 Dec 2008 18:24:32 -0000 1.268 +++ update.php 7 Jan 2009 19:41:31 -0000 @@ -684,18 +684,19 @@ if (empty($op)) { print theme('update_page', '
', FALSE); exit; } install_goto('update.php?op=info'); } update_prepare_d7_bootstrap(); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); drupal_maintenance_theme(); +drupal_session_start(); // Turn error reporting back on. From now on, only fatal errors (which are // not passed through the error handler) will cause a message to be printed. ini_set('display_errors', TRUE); // Access check: if (!empty($update_free_access) || $user->uid == 1) { include_once DRUPAL_ROOT . '/includes/install.inc'; Index: includes/batch.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/batch.inc,v retrieving revision 1.29 diff -u -9 -p -r1.29 batch.inc --- includes/batch.inc 27 Dec 2008 19:12:09 -0000 1.29 +++ includes/batch.inc 7 Jan 2009 19:41:31 -0000 @@ -395,18 +395,19 @@ function _batch_finished() { // Use drupal_redirect_form() to handle the redirection logic. $form = isset($batch['form']) ? $batch['form'] : array(); if (empty($_batch['form_state']['rebuild']) && empty($_batch['form_state']['storage'])) { drupal_redirect_form($form, $redirect); } // We get here if $form['#redirect'] was FALSE, or if the form is a // multi-step form. We save the final $form_state value to be retrieved // by drupal_get_form(), and redirect to the originating page. + drupal_session_start(); $_SESSION['batch_form_state'] = $_batch['form_state']; drupal_goto($_batch['source_page']); } } /** * Shutdown function; store the current batch data for the next request. */ function _batch_shutdown() { Index: includes/bootstrap.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v retrieving revision 1.263 diff -u -9 -p -r1.263 bootstrap.inc --- includes/bootstrap.inc 4 Jan 2009 16:15:54 -0000 1.263 +++ includes/bootstrap.inc 7 Jan 2009 19:41:31 -0000 @@ -654,35 +654,53 @@ function variable_del($name) { cache_clear_all('variables', 'cache'); unset($conf[$name]); } /** * Retrieve the current page from the cache. * - * Note: we do not serve cached pages when status messages are waiting (from - * a redirected form submission which was completed). + * Note: we do not serve cached pages to authenticated users, or to anonymous + * users when $_SESSION is non-empty. $_SESSSION may contain status messages + * from a form submission, the contents of a shopping cart, or other user- + * specific content that should not be cached and displayed to other users. + * + * @param $retrieve + * If TRUE, look up and return the current page in the cache, or start output + * buffering if the conditions for caching are satisfied. If FALSE, only + * return a boolean value indicating whether the current request may be + * cached. + * @return + * The cache object, if the page was found in the cache; TRUE if the page was + * not found, but output buffering was started in order to possibly cache the + * current request; FALSE if the page was not found, and the current request + * may not be cached (e.g. because it belongs to an authenticated user). If + * $fetch is TRUE, only return either TRUE or FALSE. */ -function page_get_cache() { +function page_get_cache($retrieve) { global $user, $base_root; + static $ob_started = FALSE; - $cache = NULL; - - if (!$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && count(drupal_set_message()) == 0) { + if ($user->uid || ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD') || count(drupal_get_messages(NULL, FALSE))) { + return FALSE; + } + if ($retrieve) { $cache = cache_get($base_root . request_uri(), 'cache_page'); - - if (empty($cache)) { + if ($cache) { + return $cache; + } + else { ob_start(); + $ob_started = TRUE; } } - - return $cache; + return $ob_started; } /** * Includes a file with the provided type and name. This prevents * including a theme, engine, module, etc., more than once. * * @param $type * The type of item to load (i.e. theme, theme_engine, module). * @param $name @@ -938,18 +956,20 @@ function watchdog($type, $message, $vari * - 'status' * - 'warning' * - 'error' * @param $repeat * If this is FALSE and the message is already set, then the message won't * be repeated. */ function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) { if ($message) { + drupal_session_start(); + if (!isset($_SESSION['messages'])) { $_SESSION['messages'] = array(); } if (!isset($_SESSION['messages'][$type])) { $_SESSION['messages'][$type] = array(); } if ($repeat || !in_array($message, $_SESSION['messages'][$type])) { @@ -1076,19 +1096,19 @@ function drupal_bootstrap($phase = NULL) * drupal_bootstrap(). * * @see drupal_bootstrap */ function drupal_get_bootstrap_phase() { return drupal_bootstrap(); } function _drupal_bootstrap($phase) { - global $conf; + global $conf, $user; switch ($phase) { case DRUPAL_BOOTSTRAP_CONFIGURATION: drupal_initialize_variables(); // Start a page timer: timer_start('page'); // Initialize the configuration conf_init(); @@ -1122,48 +1142,68 @@ function _drupal_bootstrap($phase) { header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); print 'Sorry, ' . check_plain(ip_address()) . ' has been banned.'; exit(); } break; case DRUPAL_BOOTSTRAP_SESSION: require_once DRUPAL_ROOT . '/' . variable_get('session_inc', 'includes/session.inc'); session_set_save_handler('_sess_open', '_sess_close', '_sess_read', '_sess_write', '_sess_destroy_sid', '_sess_gc'); - session_start(); + // If a session cookie exists, initialize the session. Otherwise the + // session is only started on demand in drupal_session_start(), making + // anonymous users not use a session cookie unless something is stored in + // $_SESSION. This allows HTTP proxies to cache anonymous pageviews. + if (isset($_COOKIE[session_name()])) { + drupal_session_start(); + } + else { + $user = drupal_anonymous_user(); + } break; case DRUPAL_BOOTSTRAP_VARIABLES: // Initialize configuration variables, using values from settings.php if available. $conf = variable_init(isset($conf) ? $conf : array()); break; case DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE: $cache_mode = variable_get('cache', CACHE_DISABLED); // Get the page from the cache. - $cache = $cache_mode == CACHE_DISABLED ? '' : page_get_cache(); + $cache = $cache_mode == CACHE_DISABLED ? FALSE : page_get_cache(TRUE); // If the skipping of the bootstrap hooks is not enforced, call hook_boot. - if (!$cache || $cache_mode != CACHE_AGGRESSIVE) { + if (!is_object($cache) || $cache_mode != CACHE_AGGRESSIVE) { // Load module handling. require_once DRUPAL_ROOT . '/includes/module.inc'; module_invoke_all('boot'); } // If there is a cached page, display it. - if ($cache) { + if (is_object($cache)) { + // Destroy empty anonymous sessions. + if (drupal_session_is_started() && empty($_SESSION)) { + session_destroy(); + } drupal_page_cache_header($cache); // If the skipping of the bootstrap hooks is not enforced, call hook_exit. if ($cache_mode != CACHE_AGGRESSIVE) { module_invoke_all('exit'); } // We are done. exit; } // Prepare for non-cached page workflow. drupal_page_header(); + // If the session has not already been started and output buffering is + // not enabled, the session must be started now before the HTTP headers + // are sent. If output buffering is enabled, the session may be started + // at any time using drupal_session_start(). + if ($cache === FALSE) { + drupal_session_start(); + } break; case DRUPAL_BOOTSTRAP_LANGUAGE: drupal_init_language(); break; case DRUPAL_BOOTSTRAP_PATH: require_once DRUPAL_ROOT . '/includes/path.inc'; // Initialize $_GET['q'] prior to loading modules and invoking hook_init(). Index: includes/common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.842 diff -u -9 -p -r1.842 common.inc --- includes/common.inc 5 Jan 2009 22:23:58 -0000 1.842 +++ includes/common.inc 7 Jan 2009 19:41:32 -0000 @@ -333,21 +333,23 @@ function drupal_goto($path = '', $query // Remove newlines from the URL to avoid header injection attacks. $url = str_replace(array("\n", "\r"), '', $url); // Allow modules to react to the end of the page request before redirecting. // We do not want this while running update.php. if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') { module_invoke_all('exit', $url); } - // Even though session_write_close() is registered as a shutdown function, we - // need all session data written to the database before redirecting. - session_write_close(); + if (drupal_session_is_started()) { + // Even though session_write_close() is registered as a shutdown function, + // we need all session data written to the database before redirecting. + session_write_close(); + } header('Location: ' . $url, TRUE, $http_response_code); // The "Location" header sends a redirect status code to the HTTP daemon. In // some cases this can be wrong, so we make sure none of the code below the // drupal_goto() call gets executed upon redirection. exit(); } @@ -1826,18 +1828,24 @@ function l($text, $path, array $options } /** * Perform end-of-request tasks. * * This function sets the page cache if appropriate, and allows modules to * react to the closing of the page by calling hook_exit(). */ function drupal_page_footer() { + global $user; + + // Destroy empty anonymous sessions if possible. + if (!headers_sent() && drupal_session_is_started() && empty($_SESSION) && !$user->uid) { + session_destroy(); + } if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) { page_set_cache(); } module_invoke_all('exit'); module_implements(MODULE_IMPLEMENTS_WRITE_CACHE); _registry_check_code(REGISTRY_WRITE_LOOKUP_CACHE); @@ -2966,38 +2974,36 @@ function _drupal_bootstrap_full() { * The majority of all modern browsers support gzip or both of them. * We thus only deal with the gzip variant and unzip the cache in case * the browser does not accept gzip encoding. * * @see drupal_page_header */ function page_set_cache() { global $user, $base_root; - if (!$user->uid && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD') && count(drupal_get_messages(NULL, FALSE)) == 0) { - // This will fail in some cases, see page_get_cache() for the explanation. - if ($data = ob_get_contents()) { - $cache = TRUE; - if (variable_get('page_compression', TRUE) && function_exists('gzencode')) { - // We do not store the data in case the zlib mode is deflate. - // This should be rarely happening. - if (zlib_get_coding_type() == 'deflate') { - $cache = FALSE; - } - elseif (zlib_get_coding_type() == FALSE) { - $data = gzencode($data, 9, FORCE_GZIP); - } - // The remaining case is 'gzip' which means the data is - // already compressed and nothing left to do but to store it. - } - ob_end_flush(); - if ($cache && $data) { - cache_set($base_root . request_uri(), $data, 'cache_page', CACHE_TEMPORARY, drupal_get_headers()); - } + if (page_get_cache(FALSE)) { + $cache = TRUE; + $data = ob_get_contents(); + if (variable_get('page_compression', TRUE) && function_exists('gzencode')) { + // We do not store the data in case the zlib mode is deflate. This should + // be rarely happening. + if (zlib_get_coding_type() == 'deflate') { + $cache = FALSE; + } + elseif (zlib_get_coding_type() == FALSE) { + $data = gzencode($data, 9, FORCE_GZIP); + } + // The remaining case is 'gzip' which means the data is already + // compressed and nothing left to do but to store it. + } + ob_end_flush(); + if ($cache && $data) { + cache_set($base_root . request_uri(), $data, 'cache_page', CACHE_TEMPORARY, drupal_get_headers()); } } } /** * Executes a cron run when called * @return * Returns TRUE if ran successfully */ Index: includes/session.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/session.inc,v retrieving revision 1.64 diff -u -9 -p -r1.64 session.inc --- includes/session.inc 24 Nov 2008 06:12:45 -0000 1.64 +++ includes/session.inc 7 Jan 2009 19:41:32 -0000 @@ -118,23 +118,22 @@ function _sess_read($key) { * Session ID. * @param $value * Serialized array of the session data. * @return * This function will always return TRUE. */ function _sess_write($key, $value) { global $user; - // If saving of session data is disabled or if the client doesn't have a session, - // and one isn't being created ($value), do nothing. This keeps crawlers out of - // the session table. This reduces memory and server load, and gives more useful - // statistics. We can't eliminate anonymous session table rows without breaking - // the "Who's Online" block. + // If saving of session data is disabled, or if a new empty anonymous session + // has been started, do nothing. This keeps anonymous users, including + // crawlers, out of the session table, unless they actually have something + // stored in $_SESSION. if (!drupal_save_session() || ($user->uid == 0 && empty($_COOKIE[session_name()]) && empty($value))) { return TRUE; } db_merge('sessions') ->key(array('sid' => $key)) ->fields(array( 'uid' => $user->uid, 'cache' => isset($user->cache) ? $user->cache : 0, @@ -153,18 +152,45 @@ function _sess_write($key, $value) { )) ->condition('uid', $user->uid) ->execute(); } return TRUE; } /** + * Propagate $_SESSION and set session cookie if not already set. This function + * should be called before writing to $_SESSION. + * + * @param $start + * If FALSE, the session is not actually started. This is only used by + * drupal_session_is_started(). + * @return + * TRUE if session has already been started, or FALSE if it has not. + */ +function drupal_session_start($start = TRUE) { + static $started = FALSE; + if ($start && !$started) { + $started = TRUE; + session_start(); + } + return $started; +} + +/** + * Return whether a session has been started and the $_SESSION variable is + * available. + */ +function drupal_session_is_started() { + return drupal_session_start(FALSE); +} + +/** * Called when an anonymous user becomes authenticated or vice-versa. */ function drupal_session_regenerate() { $old_session_id = session_id(); extract(session_get_cookie_params()); // Set "httponly" to TRUE to reduce the risk of session stealing via XSS. session_set_cookie_params($lifetime, $path, $domain, $secure, TRUE); session_regenerate_id(); db_update('sessions') @@ -205,18 +231,21 @@ function drupal_session_count($timestamp * Cleanup a specific session. * * @param string $sid * Session ID. */ function _sess_destroy_sid($sid) { db_delete('sessions') ->condition('sid', $sid) ->execute(); + // Unset cookie. + extract(session_get_cookie_params()); + setcookie(session_name(), '', time() - 3600, $path, $domain, $secure, $httponly); } /** * End a specific user's session(s). * * @param string $uid * User ID. */ function drupal_session_destroy_uid($uid) { Index: modules/simpletest/drupal_web_test_case.php =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v retrieving revision 1.79 diff -u -9 -p -r1.79 drupal_web_test_case.php --- modules/simpletest/drupal_web_test_case.php 6 Jan 2009 12:44:20 -0000 1.79 +++ modules/simpletest/drupal_web_test_case.php 7 Jan 2009 19:41:32 -0000 @@ -913,20 +913,21 @@ class DrupalWebTestCase { } // Return the database prefix to the original. $db_prefix = $this->originalPrefix; // Return the user to the original one. $user = $this->originalUser; drupal_save_session(TRUE); - // Ensure that the internal logged in variable is reset. + // Ensure that internal logged in variable and cURL options are reset. $this->isLoggedIn = FALSE; + $this->additionalCurlOptions = array(); // Reload module list and implementations to ensure that test module hooks // aren't called after tests. module_list(TRUE); module_implements(MODULE_IMPLEMENTS_CLEAR_CACHE); // Rebuild caches. $this->refreshVariables(); Index: modules/simpletest/tests/session.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/session.test,v retrieving revision 1.9 diff -u -9 -p -r1.9 session.test --- modules/simpletest/tests/session.test 18 Dec 2008 00:42:55 -0000 1.9 +++ modules/simpletest/tests/session.test 7 Jan 2009 19:41:32 -0000 @@ -140,25 +140,122 @@ class SessionTestCase extends DrupalWebT // Test authenticated count. $this->assertEqual($authenticated, $this->session_count_authenticated, t('Correctly counted @count authenticated sessions.', array('@count' => $authenticated)), t('Session')); // Should return 0 sessions from 1 second from now. $this->assertEqual(drupal_session_count(time() + 1), 0, t('Correctly returned 0 sessions newer than the current time.'), t('Session')); } /** + * Test that empty anonymous sessions are destroyed. + */ + function testEmptyAnonymousSession() { + // With caching disabled, a session is always started. + $this->drupalGet(''); + $this->assertSessionCookie(FALSE); + $this->assertSessionStarted(TRUE); + $this->assertSessionEmpty(TRUE); + + variable_set('cache', CACHE_NORMAL); + + // During this request the session is destroyed in drupal_page_footer(), + // and the session cookie is unset. + $this->drupalGet(''); + $this->assertSessionCookie(TRUE); + $this->assertSessionStarted(TRUE); + $this->assertSessionEmpty(TRUE); + $this->assertFalse($this->drupalGetHeader('ETag'), t('Page was not cached.')); + // When PHP deletes a cookie, it sends "Set-Cookie: cookiename=deleted; + // expires=..." + $this->assertTrue(preg_match('/SESS\w+=deleted/', $this->drupalGetHeader('Set-Cookie')), t('Session cookie was deleted.')); + + // Verify that the session cookie was actually deleted. + $this->drupalGet(''); + $this->assertSessionCookie(FALSE); + $this->assertSessionStarted(FALSE); + $this->assertFalse($this->drupalGetHeader('Set-Cookie'), t('New session was not started.')); + + // Start a new session by setting a message. + $this->drupalGet('session-test/set-message'); + $this->assertSessionCookie(FALSE); + $this->assertSessionStarted(FALSE); + $this->assertTrue($this->drupalGetHeader('Set-Cookie'), t('New session was started.')); + + // Display the message. + $this->drupalGet(''); + $this->assertSessionCookie(TRUE); + $this->assertSessionStarted(TRUE); + $this->assertSessionEmpty(FALSE); + $this->assertFalse($this->drupalGetHeader('ETag'), t('Page was not cached.')); + $this->assertText(t('This is a dummy message.'), t('Message was displayed.')); + + // During this request the session is destroyed in _drupal_bootstrap(), + // and the session cookie is unset. + $this->drupalGet(''); + $this->assertSessionCookie(TRUE); + $this->assertSessionStarted(TRUE); + $this->assertSessionEmpty(TRUE); + $this->assertTrue($this->drupalGetHeader('ETag'), t('Page was cached.')); + $this->assertNoText(t('This is a dummy message.'), t('Message was not cached.')); + $this->assertTrue(preg_match('/SESS\w+=deleted/', $this->drupalGetHeader('Set-Cookie')), t('Session cookie was deleted.')); + + // Verify that session was destroyed. + $this->drupalGet(''); + $this->assertSessionCookie(FALSE); + $this->assertSessionStarted(FALSE); + $this->assertTrue($this->drupalGetHeader('ETag'), t('Page was cached.')); + $this->assertFalse($this->drupalGetHeader('Set-Cookie'), t('New session was not started.')); + } + + /** * Reset the cookie file so that it refers to the specified user. * * @param $uid User id to set as the active session. */ function sessionReset($uid = 0) { // Close the internal browser. $this->curlClose(); // Change cookie file for user. $this->cookieFile = file_directory_temp() . '/cookie.' . $uid . '.txt'; $this->additionalCurlOptions[CURLOPT_COOKIEFILE] = $this->cookieFile; $this->additionalCurlOptions[CURLOPT_COOKIESESSION] = TRUE; $this->drupalGet('session-test/get'); $this->assertResponse(200, t('Session test module is correctly enabled.'), t('Session')); } + + /** + * Assert whether the SimpleTest browser sent a session cookie. + */ + function assertSessionCookie($sent) { + if ($sent) { + $this->assertIdentical($this->drupalGetHeader('X-Session-Cookie'), '1', t('Session cookie was sent.')); + } + else { + $this->assertIdentical($this->drupalGetHeader('X-Session-Cookie'), '0', t('Session cookie was not sent.')); + } + } + + /** + * Assert whether session was started during the bootstrap process. + */ + function assertSessionStarted($started) { + if ($started) { + $this->assertIdentical($this->drupalGetHeader('X-Session-Started'), '1', t('Session was started.')); + } + else { + $this->assertIdentical($this->drupalGetHeader('X-Session-Started'), '0', t('Session was not started.')); + } + } + + /** + * Assert whether $_SESSION is empty at the beginning of the request. + */ + function assertSessionEmpty($empty) { + if ($empty) { + $this->assertIdentical($this->drupalGetHeader('X-Session-Empty'), '1', t('Session was empty.')); + } + else { + $this->assertIdentical($this->drupalGetHeader('X-Session-Empty'), '0', t('Session was not empty.')); + } + } } Index: modules/simpletest/tests/session_test.module =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/session_test.module,v retrieving revision 1.4 diff -u -9 -p -r1.4 session_test.module --- modules/simpletest/tests/session_test.module 24 Nov 2008 06:12:45 -0000 1.4 +++ modules/simpletest/tests/session_test.module 7 Jan 2009 19:41:32 -0000 @@ -25,38 +25,64 @@ function session_test_menu() { 'type' => MENU_CALLBACK, ); $items['session-test/no-set/%'] = array( 'title' => t('Disabled session set value'), 'page callback' => '_session_test_no_set', 'page arguments' => array(2), 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['session-test/set-message'] = array( + 'title' => t('Session value'), + 'page callback' => '_session_test_set_message', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); return $items; } /** + * Implement hook_boot(). + */ +function session_test_boot() { + header('X-Session-Cookie: ' . intval(isset($_COOKIE[session_name()]))); + header('X-Session-Started: ' . intval(drupal_session_is_started())); + header('X-Session-Empty: ' . intval(empty($_SESSION))); +} + +/** + * Implement hook_init(). + */ +function session_test_init() { + // hook_init() is called latter in the bootstrap process, but not in cached + // requests. Here the header set in hook_boot() is overwritten, so the + // session state is reported as late in the bootstrap process as possible. + header('X-Session-Started: ' . intval(drupal_session_is_started())); +} + +/** * Page callback, prints the stored session value to the screen. */ function _session_test_get() { if (!empty($_SESSION['session_test_value'])) { return t('The current value of the stored session variable is: %val', array('%val' => $_SESSION['session_test_value'])); } else { return ""; } } /** * Page callback, stores a value in $_SESSION['session_test_value']. */ function _session_test_set($value) { + drupal_session_start(); $_SESSION['session_test_value'] = $value; return t('The current value of the stored session variable has been set to %val', array('%val' => $value)); } /** * Menu callback: turns off session saving and then tries to save a value * anyway. */ function _session_test_no_set($value) { @@ -67,18 +93,29 @@ function _session_test_no_set($value) { /** * Menu callback: print the current session ID. */ function _session_test_id() { return 'session_id:' . session_id() . "\n"; } /** + * Menu callback, sets a message to me displayed on the following page. + */ +function _session_test_set_message() { + drupal_set_message(t('This is a dummy message.')); + print t('A message was set.'); + // Do not return anything, so the current request does not result in a themed + // page with messages. The message will be displayed in the following request + // instead. +} + +/** * Implementation of hook_user(). */ function session_test_user_login($edit = array(), $user = NULL) { if ($edit['name'] == 'session_test_user') { // Exit so we can verify that the session was regenerated // before hook_user() was called. exit; } } Index: modules/system/system.admin.inc =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.admin.inc,v retrieving revision 1.112 diff -u -9 -p -r1.112 system.admin.inc --- modules/system/system.admin.inc 6 Jan 2009 13:33:06 -0000 1.112 +++ modules/system/system.admin.inc 7 Jan 2009 19:41:32 -0000 @@ -1318,18 +1318,30 @@ function system_logging_overview() { * Form builder; Configure site performance settings. * * @ingroup forms * @see system_settings_form() */ function system_performance_settings() { $description = '

' . t("The normal cache mode is suitable for most sites and does not cause any side effects. The aggressive cache mode causes Drupal to skip the loading (boot) and unloading (exit) of enabled modules when serving a cached page. This results in an additional performance boost but can cause unwanted side effects.") . '

'; + // Check if the "Who's online" block is enabled. + $online_block_enabled = db_select('block') + ->condition('module', 'user') + ->condition('delta', 'online') + ->condition('status', 1) + ->countQuery() + ->execute() + ->fetchField(); + if ($online_block_enabled) { + $description .= '

' . t("When caching is enabled, anonymous user sessions are only saved to the database when needed, so the \"Who's online\" block does not display the number of anonymous users.") . '

'; + } + $problem_modules = array_unique(array_merge(module_implements('boot'), module_implements('exit'))); sort($problem_modules); if (count($problem_modules) > 0) { $description .= '

' . t('The following enabled modules are incompatible with aggressive mode caching and will not function properly: %modules', array('%modules' => implode(', ', $problem_modules))) . '.

'; } else { $description .= '

' . t('Currently, all enabled modules are compatible with the aggressive caching policy. Please note, if you use aggressive caching and enable new modules, you will need to check this page again to ensure compatibility.') . '

'; } Index: modules/user/user.module =================================================================== RCS file: /cvs/drupal/drupal/modules/user/user.module,v retrieving revision 1.952 diff -u -9 -p -r1.952 user.module --- modules/user/user.module 4 Jan 2009 16:10:48 -0000 1.952 +++ modules/user/user.module 7 Jan 2009 19:41:32 -0000 @@ -771,18 +771,19 @@ function user_block_configure($delta = ' '#default_value' => variable_get('user_block_whois_new_count', 5), '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)), ); return $form; case 'online': $period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval'); $form['user_block_seconds_online'] = array('#type' => 'select', '#title' => t('User activity'), '#default_value' => variable_get('user_block_seconds_online', 900), '#options' => $period, '#description' => t('A user is considered online for this long after they have last viewed a page.')); $form['user_block_max_list_count'] = array('#type' => 'select', '#title' => t('User list length'), '#default_value' => variable_get('user_block_max_list_count', 10), '#options' => drupal_map_assoc(array(0, 5, 10, 15, 20, 25, 30, 40, 50, 75, 100)), '#description' => t('Maximum number of currently online users to display.')); + $form['user_block_cache'] = array('#markup' => '

If page caching is disabled, the block shows the number of anonymous and authenticated users, respectively. If page caching is enabled, only the number of authenticated users is displayed.

'); return $form; } } /** * Implementation of hook_block_save(). */ function user_block_save($delta = '', $edit = array()) { global $user; @@ -836,27 +837,33 @@ function user_block_view($delta = '') { return $block; case 'online': if (user_access('access content')) { // Count users active within the defined period. $interval = REQUEST_TIME - variable_get('user_block_seconds_online', 900); // Perform database queries to gather online user lists. We use s.timestamp // rather than u.access because it is much faster. - $anonymous_count = drupal_session_count($interval); $authenticated_count = db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} s WHERE s.timestamp >= :timestamp AND s.uid > 0", array(':timestamp' => $interval))->fetchField(); - // Format the output with proper grammar. - if ($anonymous_count == 1 && $authenticated_count == 1) { - $output = t('There is currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests'))); - } - else { - $output = t('There are currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests'))); + // When page caching is enabled, sessions are only created for + // anonymous users when needed. + if (variable_get('cache', CACHE_DISABLED) == CACHE_DISABLED) { + $anonymous_count = drupal_session_count($interval); + // Format the output with proper grammar. + if ($anonymous_count == 1 && $authenticated_count == 1) { + $output = t('There is currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests'))); + } + else { + $output = t('There are currently %members and %visitors online.', array('%members' => format_plural($authenticated_count, '1 user', '@count users'), '%visitors' => format_plural($anonymous_count, '1 guest', '@count guests'))); + } + } else { + $output = format_plural($authenticated_count, 'There is currently 1 user online.', 'There are currently @count users online.'); } // Display a list of currently online users. $max_users = variable_get('user_block_max_list_count', 10); if ($authenticated_count && $max_users) { $items = db_query_range('SELECT u.uid, u.name, MAX(s.timestamp) AS max_timestamp FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.timestamp >= :interval AND s.uid > 0 GROUP BY u.uid, u.name ORDER BY max_timestamp DESC', array(':interval' => $interval), 0, $max_users)->fetchAll(); $output .= theme('user_list', $items, t('Online users')); }