--- boost.module 2008-10-25 21:30:34.000000000 +0400 +++ boost.module 2009-01-29 13:29:22.000000000 +0300 @@ -55,6 +55,8 @@ function boost_help($path, $arg) { * Implementation of hook_init(). Performs page setup tasks if page not cached. */ function boost_init() { +// exit(); + // Stop right here unless we're being called for an ordinary page request if (strpos($_SERVER['PHP_SELF'], 'index.php') === FALSE || variable_get('site_offline', 0)) return; @@ -71,14 +73,20 @@ function boost_init() { } // We only serve cached pages for GET requests by anonymous visitors: else if ($_SERVER['REQUEST_METHOD'] == 'GET') { + // We use $_REQUEST['q'] because drupal_init_path() had already changed $_GET['q'] + $boost_path = $_REQUEST['q']; + if (empty($boost_path)) { + // special handling for Drupal's front page + $boost_path = 'index'; + } // Make sure no query string (in addition to ?q=) was set, and that - // the page is cacheable according to our current configuration: - if (count($_GET) == 1 && boost_is_cacheable($_GET['q'])) { + // the page is cacheable according to our current configuration. + if (count($_GET) == 1 && boost_is_cacheable($boost_path)) { // In the event of errors such as drupal_not_found(), GET['q'] is // changed before _boost_ob_handler() is called. Apache is going to // look in the cache for the original path, however, so we need to // preserve it. - $GLOBALS['_boost_path'] = $_GET['q']; + $GLOBALS['_boost_path'] = $boost_path; ob_start('_boost_ob_handler'); } } @@ -273,11 +281,11 @@ function boost_block($op = 'list', $delt case 'status': // Don't show the block to anonymous users, nor on any pages that // aren't even cacheable to begin with (e.g. admin/*). - if (!empty($user->uid) && boost_is_cacheable($_GET['q'])) { + if (!empty($user->uid) && boost_is_cacheable($_REQUEST['q'])) { $output = t('This page is being served live to anonymous visitors, as it is not currently in the static page cache.'); - if (boost_is_cached($_GET['q'])) { - $ttl = boost_file_get_ttl(boost_file_path($_GET['q'])); + if (boost_is_cached($_REQUEST['q'])) { + $ttl = boost_file_get_ttl(boost_file_path($_REQUEST['q'])); $output = t('This page is being served to anonymous visitors from the static page cache.') . ' '; $output .= t($ttl < 0 ? 'The cached copy expired %interval ago.' : @@ -377,21 +385,21 @@ function _boost_get_http_header($regex, * RSS feeds provided by Drupal, since they would require special handling * in the mod_rewrite ruleset as they shouldn't be sent out using the * text/html content type. + * TODO: don't cache pages with unacceptable symbols */ function boost_is_cacheable($path) { - $alias = drupal_get_path_alias($path); - $path = drupal_get_normal_path($path); // normalize path + $normal_path = drupal_get_normal_path($path); // normalize path // Never cache the basic user login/registration pages or any administration pages - if ($path == 'user' || preg_match('!^user/(login|register|password)!', $path) || preg_match('!^admin!', $path)) + if ($normal_path == 'user' || preg_match('!^user/(login|register|password)!', $normal_path) || preg_match('!^admin!', $normal_path)) return FALSE; // At present, RSS feeds are not cacheable due to content type restrictions - if ($path == 'rss.xml' || preg_match('!/feed$!', $path)) + if ($normal_path == 'rss.xml' || preg_match('!/feed$!', $normal_path)) return FALSE; // Don't cache comment reply pages - if (preg_match('!^comment/reply!', $path)) + if (preg_match('!^comment/reply!', $normal_path)) return FALSE; // Match the user's cacheability settings against the path @@ -400,18 +408,13 @@ function boost_is_cacheable($path) { return !empty($result); } $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote(BOOST_CACHEABILITY_PAGES, '/')) .')$/'; - return !(BOOST_CACHEABILITY_OPTION xor preg_match($regexp, $alias)); + return !(BOOST_CACHEABILITY_OPTION xor preg_match($regexp, $path)); } /** * Determines whether a given Drupal page is currently cached or not. */ function boost_is_cached($path) { - $path = (empty($path) ? drupal_get_normal_path(variable_get('site_frontpage', 'node')) : $path); - $alias = drupal_get_path_alias($path); - $path = drupal_get_normal_path($path); // normalize path - - // TODO: also determine if alias/symlink exists? return file_exists(boost_file_path($path)); } @@ -440,17 +443,10 @@ function boost_cache_expire_all($callbac function boost_cache_expire($path, $wildcard = FALSE) { // TODO: handle wildcard. - $alias = drupal_get_path_alias($path); - $path = drupal_get_normal_path($path); // normalize path - if (($filename = boost_file_path($path)) && file_exists($filename)) { @unlink($filename); } - if ($alias != $path && ($symlink = boost_file_path($alias)) && is_link($symlink)) { - @unlink($symlink); - } - return TRUE; } @@ -458,8 +454,6 @@ function boost_cache_expire($path, $wild * Returns the cached contents of the specified page, if available. */ function boost_cache_get($path) { - $path = drupal_get_normal_path($path); // normalize path - if (($filename = boost_file_path($path))) { if (file_exists($filename) && is_readable($filename)) { return file_get_contents($filename); @@ -492,9 +486,6 @@ function boost_cache_set($path, $data = $data = call_user_func(BOOST_PRE_PROCESS_FUNCTION, $data); } - $alias = drupal_get_path_alias($path); - $path = drupal_get_normal_path($path); // normalize path - // Create or update the static file as needed if (($filename = boost_file_path($path))) { _boost_mkdir_p(dirname($filename)); @@ -503,19 +494,6 @@ function boost_cache_set($path, $data = watchdog('boost', 'Unable to write file: %file', array('%file' => $filename), array(), WATCHDOG_WARNING); } } - - // If a URL alias is defined, create that as a symlink to the actual file - if ($alias != $path && ($symlink = boost_file_path($alias))) { - _boost_mkdir_p(dirname($symlink)); - if (!is_link($symlink) || realpath(readlink($symlink)) != realpath($filename)) { - if (file_exists($symlink)) { - @unlink($symlink); - } - if (!_boost_symlink($filename, $symlink)) { - watchdog('boost', 'Unable to create symlink: %link to %target', array('%link' => $symlink, '%target' => $filename), array(), WATCHDOG_WARNING); - } - } - } } return TRUE; @@ -537,9 +515,6 @@ function boost_cache_directory($host = N * Returns the static file path for a Drupal page. */ function boost_file_path($path) { - if (empty($path) || $path == drupal_get_normal_path(variable_get('site_frontpage', 'node'))) { - $path = 'index'; // special handling for Drupal's front page - } // Under no circumstances should the incoming path contain '..' or null // bytes; we also limit the maximum directory nesting depth of the path @@ -647,34 +622,6 @@ function _boost_rmdir_rf($dirname, $call return ($empty && @rmdir($dirname)); } -/** - * Creates a symbolic link using a computed relative path where possible. - */ -function _boost_symlink($target, $link) { - if (!file_exists($target) || !file_exists(dirname($link))) - return FALSE; - - $target = explode('/', $target); - $link = explode('/', $link); - - // Only bother creating a relative link if the paths are in the same - // top-level directory; otherwise just symlink to the absolute path. - if ($target[1] == $link[1]) { - // Remove the common path prefix - $cwd = array(); - while (count($target) > 0 && count($link) > 0 && reset($target) == reset($link)) { - $cwd[] = array_shift($target); - array_shift($link); - } - // Compute the required relative path - if (count($link) > 1) - $target = array_merge(array_fill(0, count($link) - 1, '..'), $target); - $link = array_merge($cwd, $link); - } - - return symlink(implode('/', $target), implode('/', $link)); -} - ////////////////////////////////////////////////////////////////////////////// // PHP 4.x compatibility