View source
<?php
define('BOOST_CACHEABILITY_PAGES', '');
define('BOOST_GZIP', function_exists('gzencode'));
define('BOOST_COOKIE', 'DRUPAL_UID');
define('BOOST_MESSAGE_DEBUG', FALSE);
define('BOOST_ROOT_CACHE_DIR', 'cache');
define('BOOST_NORMAL_DIR', 'normal');
define('BOOST_CHAR', '_');
define('BOOST_IGNORE_FLUSH', TRUE);
define('BOOST_EXPIRE_CRON', TRUE);
define('BOOST_CRON_SCRIPT_NAME', 'cron.php');
define('BOOST_CRON_DRUSH_COMMANDS', 'core-cron');
define('BOOST_APACHE_ETAG', 3);
define('BOOST_APACHE_VARY_COOKIE', 0);
define('BOOST_APACHE_XHEADER', 1);
define('BOOST_ADD_DEFAULT_CHARSET', TRUE);
define('BOOST_CHARSET_TYPE', 'utf-8');
define('BOOST_VISIBILITY_NOTLISTED', 0);
define('BOOST_VISIBILITY_LISTED', 1);
define('BOOST_VISIBILITY_PHP', 2);
define('BOOST_MATCH_SYMLINKS_OPTIONS', 1);
function boost_menu() {
$items = array();
$path = drupal_get_path('module', 'boost');
$items['admin/config/system/boost'] = array(
'title' => 'Boost',
'description' => 'Configuration for Boost.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'boost_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'boost.admin.inc',
'file path' => $path,
);
$items['admin/config/system/boost/default'] = array(
'title' => 'Boost Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/system/boost/debug'] = array(
'title' => 'Debug',
'description' => 'Debug configuration for Boost.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'boost_admin_debug_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'file' => 'boost.admin.debug.inc',
'file path' => $path,
);
$items['admin/config/system/boost/filesystem'] = array(
'title' => 'File System',
'description' => 'File system configuration for Boost.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'boost_admin_filesystem_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'file' => 'boost.admin.filesystem.inc',
'file path' => $path,
);
$items['admin/config/system/boost/htaccess'] = array(
'title' => '.htaccess',
'description' => '.htaccess configuration for Boost.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'boost_admin_htaccess_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'file' => 'boost.admin.htaccess.inc',
'file path' => $path,
);
$items['admin/config/system/boost/htaccess/default'] = array(
'title' => '.htaccess Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/system/boost/htaccess/generator'] = array(
'title' => '.htaccess Generation',
'description' => '.htaccess generation for Boost.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'boost_admin_htaccess_generation',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'file' => 'boost.admin.htaccess.inc',
'file path' => $path,
);
$items['admin/config/system/boost/expiration'] = array(
'title' => 'Cache Expiration',
'description' => 'Cache expiration configuration for Boost.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'boost_admin_expiration_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'file' => 'boost.admin.expiration.inc',
'file path' => $path,
);
$items['admin/config/system/boost/listmodules'] = array(
'title' => 'Related Modules',
'description' => ' Compatible Modules that add features to Boost.',
'page callback' => 'boost_admin_modules_theme',
'page callback' => 'boost_compatible_output',
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_LOCAL_TASK,
'file' => 'boost.admin.modules.inc',
'weight' => 100,
);
return $items;
}
function boost_permission() {
return array(
'boost flush pages' => array(
'title' => t('Flush pages from Boost cache'),
'description' => t('Allow users to flush individual pages from the Boost cache using the Boost status block.'),
),
);
}
function boost_block_info() {
$blocks['status'] = array(
'info' => t('Boost: Pages cache status'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function boost_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'status':
module_load_include('inc', 'boost', 'boost.blocks');
return boost_block_view_status();
}
return $block;
}
function boost_init() {
global $_boost;
$_boost = array();
if (strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') === FALSE || $_SERVER['SERVER_SOFTWARE'] === 'PHP CLI' || $_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD' || isset($_GET['nocache']) || variable_get('maintenance_mode', 0) || defined('MAINTENANCE_MODE') || !empty($_SESSION['messages'])) {
$_boost['cache_this'] = FALSE;
}
else {
$_boost = boost_transform_url();
if (empty($_boost['menu_item']['status']) || $_boost['menu_item']['status'] != 200) {
$_boost['cache_this'] = FALSE;
}
}
$cookie_handler_callback = 'boost_cookie_handler';
drupal_alter('boost_cookie_handler_callback', $cookie_handler_callback);
if (function_exists($cookie_handler_callback)) {
$cookie_handler_callback();
}
}
function boost_form_alter(&$form, &$form_state, $form_id) {
global $_boost;
if (!empty($_boost['cache_this'])) {
$form['#immutable'] = TRUE;
}
}
function boost_drupal_goto_alter(&$path, &$options, &$http_response_code) {
global $_boost;
$_boost['is_cacheable'] = FALSE;
}
function boost_exit($destination = NULL) {
global $_boost;
if (!empty($destination)) {
$_boost['is_cacheable'] = FALSE;
}
if (!isset($_boost['cache_this'])) {
if (!isset($_boost['is_cacheable'])) {
return;
}
elseif (!$_boost['is_cacheable']) {
return;
}
}
if (isset($_boost['cache_this']) && $_boost['cache_this'] == FALSE) {
return;
}
elseif (!isset($_boost['is_cacheable']) || !$_boost['is_cacheable']) {
return;
}
elseif ($_boost['menu_item']['status'] != 200) {
return;
}
elseif (!drupal_page_is_cacheable()) {
$_boost['is_cacheable'] = FALSE;
return;
}
$data = ob_get_contents();
$_boost['header_info'] = boost_get_header_info();
$_boost['matched_header_info'] = boost_match_header_attributes($_boost['header_info']);
if ($_boost['matched_header_info']['enabled'] === FALSE) {
return;
}
if ($_boost['matched_header_info']['comment_start'] && $_boost['matched_header_info']['comment_end']) {
$expire = $_boost['matched_header_info']['lifetime_max'];
$cached_at = date('Y-m-d H:i:s', REQUEST_TIME);
$expires_at = date('Y-m-d H:i:s', REQUEST_TIME + $expire);
$note = "\n" . $_boost['matched_header_info']['comment_start'] . 'Page cached by Boost @ ' . $cached_at . ', expires @ ' . $expires_at . ', lifetime ' . format_interval($expire) . $_boost['matched_header_info']['comment_end'];
$data .= $note;
}
if ($_boost['filename']) {
$_boost['filename'] .= '.' . $_boost['matched_header_info']['extension'];
$_boost['filename'] = str_ireplace('%2F', '/', $_boost['filename']);
if (rawurldecode($_boost['filename']) != $_boost['filename']) {
boost_write_file(rawurldecode($_boost['filename']), $data);
}
boost_write_file($_boost['filename'], $data);
if (BOOST_GZIP && $_boost['matched_header_info']['gzip']) {
}
}
}
function boost_cron() {
global $_boost;
$_boost = boost_transform_url();
if (isset($_boost['base_dir']) && variable_get('boost_expire_cron', BOOST_EXPIRE_CRON)) {
$count = _boost_rmdir($_boost['base_dir'], FALSE);
if (variable_get('boost_message_debug', BOOST_MESSAGE_DEBUG)) {
watchdog('boost', 'Expired %count stale files from static page cache.', array(
'%count' => $count,
), WATCHDOG_NOTICE);
}
}
}
function boost_flush_caches() {
global $_boost;
$_boost = boost_transform_url();
if (function_exists('drush_get_command') && drupal_is_cli()) {
$command = drush_get_command();
$commands = explode(',', variable_get('boost_cron_drush_commands', BOOST_CRON_DRUSH_COMMANDS));
$cron_invoked = in_array($command['command'], $commands);
}
else {
$cron_invoked = basename($_SERVER['PHP_SELF']) == variable_get('boost_cron_script_name', BOOST_CRON_SCRIPT_NAME);
}
if (isset($_boost['base_dir']) && (!$cron_invoked || variable_get('boost_ignore_flush', BOOST_IGNORE_FLUSH) == FALSE)) {
$count = _boost_rmdir($_boost['base_dir'], TRUE);
watchdog('boost', 'Flushed all files (%count) from static page cache.', array(
'%count' => $count,
), WATCHDOG_NOTICE);
}
}
function boost_expire_cache($urls, $wildcards, $object_type, $object) {
global $base_root;
foreach ($urls as $key => $url) {
$boost = boost_transform_url(urldecode($url));
$boost['header_info'] = boost_get_header_info();
$boost['matched_header_info'] = boost_match_header_attributes($boost['header_info']);
if (!$boost['matched_header_info']['enabled']) {
continue;
}
if ($wildcards[$key]) {
$pattern = isset($boost['filename']) ? $boost['filename'] . '*.' . $boost['matched_header_info']['extension'] : NULL;
}
else {
$pattern = isset($boost['filename']) ? $boost['filename'] . '.' . $boost['matched_header_info']['extension'] : NULL;
}
$files = glob($pattern, GLOB_NOSORT);
if ($files) {
foreach ($files as $filename) {
if (unlink($filename)) {
boost_log('Removed !file from the boost cache.', array(
'!file' => $filename,
), WATCHDOG_DEBUG);
}
else {
boost_log('Could not delete the cache for !url, file !file does not exist.', array(
'!url' => $url,
'!file' => $filename,
), WATCHDOG_DEBUG);
}
}
}
}
}
function boost_page_delivery_callback_alter(&$callback, $set = FALSE) {
if ($callback === 'drupal_deliver_html_page') {
$callback = 'boost_deliver_html_page';
}
}
function boost_transform_url($url = NULL, $b_path = NULL) {
global $base_root, $base_path;
$items =& drupal_static(__FUNCTION__);
if ($url === NULL) {
$url = $base_root . request_uri();
}
if ($b_path == NULL) {
$b_path = $base_path;
}
$hash = $url . ' ' . $b_path;
if (!isset($items[$hash])) {
$parts = boost_parse_url($url, $b_path);
if (!$parts) {
$items[$hash] = array(
'cache_this' => FALSE,
);
return $items[$hash];
}
$parts['base_dir'] = boost_get_normal_cache_dir() . '/' . $parts['host'] . $b_path;
$parts['filename'] = $parts['base_dir'] . $parts['full_path'] . variable_get('boost_char', BOOST_CHAR) . $parts['query'];
$parts['directory'] = dirname($parts['filename']);
if (drupal_is_front_page()) {
$parts['normal_path'] = variable_get('site_frontpage', 'node');
}
else {
$parts['normal_path'] = drupal_get_normal_path($parts['path']);
}
$parts['path_alias'] = drupal_get_path_alias($parts['normal_path']);
$args = arg(NULL, $parts['normal_path']);
$args[0] = empty($args[0]) ? '' : $args[0];
$args[1] = empty($args[1]) ? '' : $args[1];
$args[2] = empty($args[2]) ? '' : $args[2];
$parts['args'] = $args;
if (!empty($parts['normal_path'])) {
$parts = _boost_get_menu_router($parts);
}
$parts = boost_is_cacheable($parts);
$items[$hash] = $parts;
}
return $items[$hash];
}
function boost_get_normal_cache_dir() {
return variable_get('boost_root_cache_dir', BOOST_ROOT_CACHE_DIR) . '/' . variable_get('boost_normal_dir', BOOST_NORMAL_DIR);
}
function boost_parse_url($url = NULL, $b_path = NULL) {
global $base_root, $base_path;
if ($url === NULL) {
$url = $base_root . request_uri();
}
if ($b_path == NULL) {
$b_path = $base_path;
}
$parts = parse_url($url);
if (empty($parts['host']) || empty($parts['path'])) {
return FALSE;
}
if (!isset($parts['query'])) {
$parts['query'] = '';
}
$parts['path'] = $parts['full_path'] = urldecode(preg_replace('/^' . preg_quote($b_path, '/') . '/i', '', $parts['path']));
$parts['base_path'] = $b_path;
$parts['query_array'] = array();
parse_str($parts['query'], $parts['query_array']);
if (drupal_multilingual() && variable_get('locale_language_negotiation_url_part') == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) {
$languages = language_list('enabled');
$languages = $languages[1];
list($language, $parts['path']) = language_url_split_prefix($parts['path'], $languages);
}
if (!empty($parts['query_array'])) {
$query = array();
foreach ($parts['query_array'] as $key => $val) {
if ($key !== 'q' && $key !== 'destination' && $key !== 'page' && !empty($val)) {
$query[$key] = $val;
}
if ($key === 'page' && is_numeric($val)) {
$parts['page_number'] = $val;
}
}
ksort($query);
$parts['query_extra'] = str_replace('&', '&', urldecode(http_build_query($query)));
}
$decoded1 = urldecode($parts['base_path'] . $parts['path'] . variable_get('boost_char', BOOST_CHAR) . $parts['query']);
$decoded2 = urldecode($decoded1);
while ($decoded1 != $decoded2) {
$decoded1 = urldecode($decoded2);
$decoded2 = urldecode($decoded1);
}
$decoded = $decoded2;
unset($decoded2, $decoded1);
$parts['url_full'] = $parts['host'] . $parts['base_path'] . $parts['path'] . variable_get('boost_char', BOOST_CHAR) . $parts['query'];
$parts['url'] = $url;
$parts['url_decoded'] = $decoded;
return $parts;
}
function boost_is_cacheable($parts, $request_type = 'normal') {
$path = $parts['path'];
$query = $parts['query'];
$full = $parts['url_full'];
$normal_path = $parts['normal_path'];
$alias = $parts['path_alias'];
$decoded = $parts['url_decoded'];
if ($normal_path === 'user' || !empty($query_array['destination']) || preg_match('!^user/(autocomplete|login|register|password|reset|logout)!', $normal_path) || 0 === strpos($normal_path, 'admin') || 0 === strpos($normal_path, 'comment/reply') || 0 === strpos($normal_path, 'node/add') || 0 === strpos($normal_path, 'openid/authenticate') || strpos($query, '/') !== FALSE || strpos($query, "\\") !== FALSE || strpos($full, '..') !== FALSE || strpos($full, "\0") !== FALSE || strpos($decoded, '://') !== FALSE || count(explode('/', $path)) > 10) {
$parts['is_cacheable'] = FALSE;
$parts['is_cacheable_reason'] = 'Core Drupal dynamic pages';
return $parts;
}
$chars = '"*:<>|';
if (FALSE !== stripos(PHP_OS, 'WIN') && preg_match("/[" . $chars . "]/", $full)) {
$parts['is_cacheable'] = FALSE;
$parts['is_cacheable_reason'] = 'Reserved characters on MS Windows';
return $parts;
}
$visibility = variable_get('boost_cacheability_option', BOOST_VISIBILITY_NOTLISTED);
$pages_setting = variable_get('boost_cacheability_pages', BOOST_CACHEABILITY_PAGES);
if ($pages_setting) {
$pages = drupal_strtolower($pages_setting);
if ($visibility < BOOST_VISIBILITY_PHP) {
$path = drupal_strtolower($alias);
$page_match = drupal_match_path($path, $pages);
if ($path != $normal_path) {
$page_match = $page_match || drupal_match_path($normal_path, $pages);
}
$page_match = !($visibility xor $page_match);
}
elseif (module_exists('php')) {
$page_match = php_eval($pages_setting);
}
else {
$page_match = FALSE;
}
}
else {
$page_match = TRUE;
}
$parts['is_cacheable'] = $page_match;
if (!$page_match) {
$parts['is_cacheable_reason'] = 'Page excluded from cache by the include/exclude paths defined by site admin.';
}
if (!$parts['is_cacheable']) {
return $parts;
}
$modules = boost_module_implements('boost_is_cacheable', 'boost');
foreach ($modules as $module) {
$result = module_invoke($module, 'boost_is_cacheable', $parts, $request_type);
if ($result['is_cacheable'] === FALSE) {
if (!isset($result['is_cacheable'])) {
$result['is_cacheable_reason'] = 'Page excluded from cache by a third-party module.';
}
return $result;
}
}
return $result;
}
function boost_boost_is_cacheable($parts, $request_type = 'normal') {
global $user;
if ($user->uid != 0 && $request_type !== 'status') {
$parts['is_cacheable'] = FALSE;
$parts['is_cacheable_reason'] = 'Boost only works for anonymous users.';
}
else {
$parts['is_cacheable'] = TRUE;
}
return $parts;
}
function boost_set_cookie($uid, $expires = NULL) {
if (!$expires) {
$expires = ini_get('session.cookie_lifetime');
$expires = !empty($expires) && is_numeric($expires) ? REQUEST_TIME + (int) $expires : 0;
setcookie(BOOST_COOKIE, (string) $uid, $expires, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
}
else {
setcookie(BOOST_COOKIE, '0', $expires, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure') == '1');
}
}
function boost_cookie_handler() {
global $user;
if (strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') === FALSE) {
return;
}
$uid = isset($user->uid) ? $user->uid : 0;
if (isset($_COOKIE[BOOST_COOKIE]) && $uid == 0) {
boost_set_cookie($uid, REQUEST_TIME - 86400);
}
elseif ((!isset($_COOKIE[BOOST_COOKIE]) || $_COOKIE[BOOST_COOKIE] == '-1') && $uid != 0) {
boost_set_cookie($uid);
}
elseif (isset($_COOKIE[BOOST_COOKIE]) && $_COOKIE[BOOST_COOKIE] == '-1' && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD')) {
boost_set_cookie($uid, REQUEST_TIME - 86400);
}
if ($uid == 0 && $_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD') {
boost_set_cookie(-1);
}
}
function _boost_get_menu_router($parts) {
$router_item = array();
$router_item['page_type'] = '';
$router_item['page_id'] = '';
$item = menu_get_item($parts['normal_path']);
if (is_array($item)) {
$router_item += $item;
if ($router_item['access']) {
$router_item['status'] = 200;
}
else {
$router_item['status'] = 403;
}
}
else {
$router_item['status'] = 404;
}
if (!empty($router_item['path'])) {
$menu_args = arg(NULL, $router_item['path']);
$diff = array();
foreach ($parts['args'] as $key => $value) {
if (!empty($value)) {
if (isset($menu_args[$key])) {
if ($value !== $menu_args[$key] && $menu_args[$key] !== '%') {
$diff[] = $value;
}
}
else {
$diff[] = $value;
}
}
}
}
if (!empty($diff)) {
$router_item['extra_arguments'] = implode('/', $diff);
}
else {
$router_item['extra_arguments'] = '';
}
if (!empty($router_item['include_file'])) {
require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
}
$parts['menu_item'] = $router_item;
$modules = boost_module_implements('boost_menu_router', 'boost');
foreach ($modules as $module) {
if (($result = module_invoke($module, 'boost_menu_router', $parts)) !== NULL) {
break;
}
}
unset($result['menu_item']['map'], $result['menu_item']['page_arguments']);
return $result;
}
function boost_boost_menu_router($parts) {
if ($parts['args'][0] === 'node' && is_numeric($parts['args'][1])) {
$node = node_load($parts['args'][1]);
$parts['menu_item']['page_callback'] = 'node';
$parts['menu_item']['page_id'] = $parts['args'][1];
if ($node) {
$parts['menu_item']['page_type'] = $node->type;
}
return $parts;
}
if ($parts['args'][0] === 'taxonomy' && is_numeric($parts['args'][2])) {
$term = taxonomy_term_load($parts['args'][2]);
$parts['menu_item']['page_callback'] = 'taxonomy';
$parts['menu_item']['page_id'] = $parts['args'][2];
if ($term) {
$vocab = taxonomy_vocabulary_load($term->vid);
$parts['menu_item']['page_type'] = $vocab->name;
}
return $parts;
}
if ($parts['args'][0] === 'user' && is_numeric($parts['args'][1])) {
$user = user_load($parts['args'][1]);
$parts['menu_item']['page_callback'] = 'user';
$parts['menu_item']['page_id'] = $parts['args'][1];
if ($user !== FALSE) {
$parts['menu_item']['page_type'] = implode(', ', $user->roles);
}
return $parts;
}
if (isset($parts['menu_item']['page_callback']) && $parts['menu_item']['page_callback'] === 'views_page') {
$page_arguments = $parts['menu_item']['page_arguments'];
if (!is_array($page_arguments)) {
$page_arguments = unserialize($page_arguments);
}
$parts['menu_item']['page_callback'] = 'view';
$parts['menu_item']['page_type'] = array_shift($page_arguments);
$parts['menu_item']['page_id'] = array_shift($page_arguments);
if (is_array($parts['menu_item']['page_id'])) {
$parts['menu_item']['page_id'] = array_shift($parts['menu_item']['page_id']);
}
return $parts;
}
if (isset($parts['menu_item']['page_callback']) && $parts['menu_item']['page_callback'] === 'page_manager_page_execute') {
$page_arguments = $parts['menu_item']['page_arguments'];
if (!is_array($page_arguments)) {
$page_arguments = unserialize($page_arguments);
}
$subtask_id = array_shift($page_arguments);
$page = page_manager_page_load($subtask_id);
$task = page_manager_get_task($page->task);
if ($function = ctools_plugin_get_function($task, 'page callback')) {
$parts['menu_item']['page_callback'] = $function;
}
$parts['menu_item']['page_type'] = $page->task;
$parts['menu_item']['page_id'] = $page->name;
return $parts;
}
elseif (isset($parts['menu_item']['page_arguments']) && is_array($parts['menu_item']['page_arguments'])) {
foreach ($parts['menu_item']['page_arguments'] as $string) {
if (is_string($string) && empty($parts['menu_item']['page_type'])) {
$parts['menu_item']['page_type'] = $string;
}
elseif (is_string($string)) {
$parts['menu_item']['page_id'] .= $string;
}
}
}
if (empty($parts['menu_item']['page_type'])) {
$parts['menu_item']['page_type'] = $parts['menu_item']['extra_arguments'];
}
elseif (empty($parts['menu_item']['page_id'])) {
$parts['menu_item']['page_id'] = $parts['menu_item']['extra_arguments'];
}
if (empty($parts['menu_item']['page_type']) && !empty($parts['query_extra'])) {
$parts['menu_item']['page_type'] = $parts['query_extra'];
}
elseif (empty($parts['menu_item']['page_id']) && !empty($parts['query_extra'])) {
$parts['menu_item']['page_id'] = $parts['query_extra'];
}
return $parts;
}
function boost_module_implements($hook, $name) {
$modules = module_implements($hook);
$pos = array_search($name, $modules);
if ($pos !== FALSE) {
$temp = $modules[$pos];
unset($modules[$pos]);
$modules[] = $temp;
}
return $modules;
}
function boost_get_header_info() {
$headers = drupal_get_http_header();
$status = '200 OK';
$status_number = '200';
$content_type = 'text/html; charset=utf-8';
$content_type_basic = 'text/html';
$charset = 'utf-8';
foreach ($headers as $name_lower => $value) {
if ($name_lower === 'status') {
$status = $value;
}
elseif ($name_lower === 'content-type') {
$content_type = $value;
}
}
preg_match('!^(\\d+)!', $status, $matches);
if (isset($matches[1])) {
$status_number = (int) $matches[1];
}
$content_type_info = explode('; charset=', $content_type);
$content_type_basic = array_shift($content_type_info);
if (!empty($content_type_info)) {
$charset = array_shift($content_type_info);
}
return array(
'status' => $status,
'status-number' => $status_number,
'content-type' => $content_type,
'content-type-basic' => $content_type_basic,
'charset' => $charset,
'headers_sent' => headers_sent(),
);
}
function boost_match_header_attributes($header_info) {
$type = $header_info['content-type-basic'];
$enabled = variable_get('boost_enabled_' . $type, -1);
if ($enabled === -1) {
boost_get_storage_types();
$enabled = variable_get('boost_enabled_' . $type, -1);
if ($enabled === -1) {
variable_set('boost_enabled_' . $type, FALSE);
$enabled = FALSE;
}
}
if (!$enabled) {
return array(
'enabled' => FALSE,
);
}
$keys = array(
'enabled',
'gzip',
'extension',
'lifetime_max',
'lifetime_min',
'comment_start',
'comment_end',
);
$info = array();
foreach ($keys as $key) {
$info[$key] = variable_get('boost_' . $key . '_' . $type, -1);
if ($info[$key] === -1) {
return array(
'enabled' => FALSE,
);
}
}
return $info;
}
function boost_get_storage_types() {
$primary_types = array();
$secondary_types = array();
$modules = boost_module_implements('boost_storage_types', 'boost');
$modules = array_reverse($modules);
foreach ($modules as $module) {
if (($result = module_invoke($module, 'boost_storage_types')) !== NULL) {
foreach ($result as $key => $values) {
if (!empty($values['variants'])) {
foreach ($values['variants'] as $type) {
$secondary_types[$type] = $values;
unset($secondary_types[$type]['variants']);
}
}
unset($values['variants']);
$primary_types[$key] = $values;
}
}
}
$merged = array_merge($primary_types, $secondary_types);
$types = array();
foreach ($merged as $type => $values) {
if ($values['title']) {
$title = $values['title'];
unset($values['title']);
$types[$title][$type] = $values;
}
}
$keys = array(
'enabled',
'extension',
'gzip',
'lifetime_max',
'lifetime_min',
'comment_start',
'comment_end',
);
foreach ($types as $title => $content_types) {
foreach ($content_types as $type => $values) {
foreach ($keys as $key) {
$types[$title][$type][$key] = variable_get('boost_' . $key . '_' . $type, $values[$key]);
if (variable_get('boost_' . $key . '_' . $type, -1) === -1) {
variable_set('boost_' . $key . '_' . $type, $values[$key]);
}
}
}
}
return $types;
}
function boost_boost_storage_types() {
$types = array();
$types['text/html'] = array(
'title' => t('HTML'),
'description' => t('HTML output, usually a webpage'),
'extension' => 'html',
'enabled' => TRUE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'comment_start' => '<!-- ',
'comment_end' => ' -->',
'variants' => array(),
);
$types['text/xml'] = array(
'title' => t('XML'),
'description' => t('XML output, usually a feed'),
'extension' => 'xml',
'enabled' => FALSE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'comment_start' => '<!-- ',
'comment_end' => ' -->',
'variants' => array(
'application/xml',
'application/rss',
'application/rss+xml',
),
);
$types['text/javascript'] = array(
'title' => t('text/javascript'),
'description' => t('Javascript output, usually a response to a AJAX request'),
'extension' => 'js',
'enabled' => FALSE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'comment_start' => '/* ',
'comment_end' => ' */',
'variants' => array(),
);
$types['application/javascript'] = array(
'title' => t('AJAX/JavaScript'),
'description' => t('Javascript output, usually a response to a AJAX request'),
'extension' => 'json',
'enabled' => FALSE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'comment_start' => '',
'comment_end' => '',
'variants' => array(),
);
$types['application/json'] = array(
'title' => t('AJAX/JSON'),
'description' => t('JSON output, usually a response to a AJAX request'),
'extension' => 'json',
'enabled' => FALSE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'comment_start' => '',
'comment_end' => '',
'variants' => array(),
);
return $types;
}
function _boost_rmdir($dir, $flush = TRUE) {
static $lifetimes = array();
static $counter = 0;
if (is_dir($dir) == FALSE) {
return FALSE;
}
if (!boost_in_cache_dir($dir)) {
return FALSE;
}
if (empty($lifetimes)) {
$types = boost_get_storage_types();
foreach ($types as $title => $content_types) {
foreach ($content_types as $type => $values) {
$lifetimes[$values['extension']] = $values['lifetime_max'];
}
}
boost_form_submit_handler();
}
$objects = scandir($dir);
$empty_dir = TRUE;
foreach ($objects as $object) {
if ($object === '.' || $object === '..') {
continue;
}
if ($object === '.htaccess') {
$empty_dir = FALSE;
continue;
}
$file = $dir . '/' . $object;
if (is_dir($file)) {
_boost_rmdir($file, $flush);
}
elseif ($flush) {
unlink($file);
$counter++;
}
else {
$ext = substr(strrchr($file, '.'), 1);
$age = boost_file_get_age($file);
if (isset($lifetimes[$ext]) && $age > $lifetimes[$ext]) {
unlink($file);
$counter++;
}
else {
$empty_dir = FALSE;
}
}
}
if ($empty_dir && is_dir($dir)) {
@rmdir($dir);
}
return $counter;
}
function boost_file_get_age($filename) {
return REQUEST_TIME - filemtime($filename);
}
function boost_print_r($data) {
return str_replace(' ', ' ', nl2br(htmlentities(print_r($data, TRUE))));
}
function boost_write_file($filename, $data) {
$directory = dirname($filename);
if (!boost_mkdir($directory)) {
return FALSE;
}
$tempname = drupal_tempnam($directory, 'boost');
if (file_put_contents($tempname, $data) === FALSE) {
watchdog('boost', 'Could not create the file %file on your system', array(
'%file' => $tempname,
), WATCHDOG_ERROR);
@unlink($tempname);
return FALSE;
}
if (@rename($tempname, $filename) === FALSE) {
$oldname = $tempname . 'old';
if (@rename($filename, $oldname) !== FALSE) {
if (@rename($tempname, $filename) === FALSE) {
watchdog('boost', 'Could not rename the file %file on your system', array(
'%file' => $filename,
), WATCHDOG_ERROR);
@unlink($tempname);
@rename($oldname, $filename);
return FALSE;
}
else {
@unlink($oldname);
}
}
}
drupal_chmod($filename);
return TRUE;
}
function boost_mkdir($directory) {
global $_boost;
if (!is_dir($directory)) {
if (!boost_in_cache_dir($directory)) {
return FALSE;
}
$mode = variable_get('file_chmod_directory', 0775);
if (!mkdir($directory, $mode, TRUE)) {
watchdog('boost', 'Could not create the directory %dir on your system', array(
'%dir' => $directory,
), WATCHDOG_ERROR);
return FALSE;
}
drupal_chmod($directory);
}
return TRUE;
}
function boost_in_cache_dir($file) {
global $_boost;
$good = TRUE;
$real_file = realpath($file);
$cache_dir = variable_get('boost_root_cache_dir', BOOST_ROOT_CACHE_DIR);
$real_cache_dir = realpath($cache_dir);
if (strpos($file, $cache_dir) !== 0 || $real_file && $real_cache_dir && strpos($real_file, $real_cache_dir) !== 0) {
$good = FALSE;
}
if (!$good) {
watchdog('boost', 'An operation outside of the cache directory was attempted on your system. %file or %real_file is outside the cache directory %cache or %real_cache. Debug info below <br> !debug', array(
'%file' => $file,
'%real_file' => $real_file,
'%cache' => $cache_dir,
'%real_cache' => $real_cache_dir,
'!debug' => boost_print_r($_boost),
), WATCHDOG_CRITICAL);
}
return $good;
}
function boost_deliver_html_page($page_callback_result) {
global $_boost;
if (is_int($page_callback_result)) {
switch ($page_callback_result) {
case MENU_NOT_FOUND:
$_boost['menu_item']['status'] = 404;
break;
case MENU_ACCESS_DENIED:
$_boost['menu_item']['status'] = 403;
break;
case MENU_SITE_OFFLINE:
$_boost['menu_item']['status'] = 503;
break;
}
}
drupal_deliver_html_page($page_callback_result);
if (variable_get('boost_message_debug', BOOST_MESSAGE_DEBUG) && $_boost['args'][0] !== 'admin') {
watchdog('boost', boost_print_r($_boost), array(), WATCHDOG_DEBUG);
}
}
function boost_form_submit_handler() {
register_shutdown_function('boost_htaccess_cache_dir_put');
}
function boost_htaccess_cache_dir_put() {
global $_boost, $base_path, $base_root;
if (empty($_boost['base_dir'])) {
$url = $base_root . request_uri();
$parts = parse_url($url);
$_boost['base_dir'] = boost_get_normal_cache_dir() . '/' . $parts['host'] . $base_path;
}
boost_write_file($_boost['base_dir'] . '.htaccess', boost_htaccess_cache_dir_generate());
}
function boost_htaccess_cache_dir_generate() {
$char_type = variable_get('boost_charset_type', BOOST_CHARSET_TYPE);
$etag = variable_get('boost_apache_etag', BOOST_APACHE_ETAG);
$gzip = FALSE;
$data = array();
$files = array();
$types = boost_get_storage_types();
foreach ($types as $title => $content_types) {
foreach ($content_types as $type => $values) {
if ($values['enabled']) {
$forcetype = '\\.' . $values['extension'];
if ($values['gzip']) {
$forcetype .= '(\\.gz)?$';
$gzip = TRUE;
}
else {
$forcetype .= '$';
}
$files[$values['extension']] = $values['extension'];
$data[$values['extension']] = array(
'type' => $type,
'forcetype' => $forcetype,
);
}
}
}
if (empty($data) || empty($files)) {
return FALSE;
}
$string = '';
if (variable_get('boost_add_default_charset', BOOST_ADD_DEFAULT_CHARSET)) {
$string .= 'AddDefaultCharset ' . $char_type . "\n";
}
if ($etag == 1) {
$string .= "FileETag None\n";
}
elseif ($etag == 2) {
$string .= "FileETag All\n";
}
elseif ($etag == 3) {
$string .= "FileETag MTime Size\n";
}
$files = '(' . implode('|', $files) . ')';
if ($gzip) {
$files .= '(\\.gz)?';
}
$string .= "<FilesMatch \"\\.{$files}\$\">\n";
$string .= " <IfModule mod_expires.c>\n";
$string .= " ExpiresDefault A5\n";
$string .= " </IfModule>\n";
$string .= " <IfModule mod_headers.c>\n";
$string .= " Header set Expires \"Sun, 19 Nov 1978 05:00:00 GMT\"\n";
$string .= " Header unset Last-Modified\n";
$string .= " Header append Vary Accept-Encoding\n";
if (variable_get('boost_apache_vary_cookie', BOOST_APACHE_VARY_COOKIE) > 0) {
$string .= " Header append Vary Cookie\n";
}
$string .= " Header set Cache-Control \"no-store, no-cache, must-revalidate, post-check=0, pre-check=0\"\n";
if (variable_get('boost_apache_xheader', BOOST_APACHE_XHEADER) > 0) {
$string .= " Header set X-Cached-By \"Boost\"\n";
}
$frame_options = variable_get('x_frame_options', 'SAMEORIGIN');
$string .= " Header set X-Frame-Options \"" . $frame_options . "\"\n";
$string .= " </IfModule>\n";
$string .= "</FilesMatch>\n";
$string .= "<IfModule mod_mime.c>\n";
foreach ($data as $extension => $values) {
$string .= ' AddCharset ' . $char_type . ' .' . $extension . "\n";
}
$string .= $gzip ? " AddEncoding gzip .gz\n" : '';
$string .= "</IfModule>\n";
foreach ($data as $extension => $values) {
$forcetype = $values['forcetype'];
$type = $values['type'];
$string .= "<FilesMatch \"{$forcetype}\">\n";
$string .= ' ForceType ' . $type . "\n";
$string .= "</FilesMatch>\n";
}
$string .= "\n";
$string .= "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\n";
$string .= "Options None\n";
$string .= 'Options +' . (variable_get('boost_match_symlinks_options', BOOST_MATCH_SYMLINKS_OPTIONS) ? "FollowSymLinks" : "SymLinksIfOwnerMatch") . "\n";
$string .= "\n";
return $string;
}
function boost_log($message, $variables, $severity = WATCHDOG_NOTICE) {
if ($severity == WATCHDOG_DEBUG && !variable_get('boost_message_debug', BOOST_MESSAGE_DEBUG)) {
return;
}
watchdog('boost', $message, $variables, $severity);
}