'; // TODO: add help text.
}
//hack to get drupal_get_messages before they are destroyed.
$GLOBALS['_boost_message_count'] = count(drupal_get_messages(NULL, FALSE));
}
/**
* Get some basic node attribues from the database given node id.
*
* @param $nid
* node ID
* @return node object
* node->nid
* node->type
* node->language
* node->path
* node->domain
* node->tids
*/
function boost_node_get_basics($nid) {
static $nodes = array();
// Is the node statically cached?
if (isset($nodes[$nid])) {
return $nodes[$nid];
}
// Retrieve node nid, type, language.
$op = 'load';
$node = db_fetch_object(db_query("SELECT nid, type, language FROM {node} WHERE nid = %d", $nid));
// Get path info
if (module_exists('path')) {
path_nodeapi($node, $op, NULL, NULL);
}
// Get domain access info
if (module_exists('domain')) {
domain_nodeapi($node, $op, NULL, NULL);
}
// Get taxonomy tid info
if (module_exists('taxonomy')) {
$node->tids = boost_taxonomy_node_get_tids($node->nid);
}
$nodes[$nid] = $node;
return $nodes[$nid];
}
/**
* Implementation of hook_ctools_render_alter().
*
* Needed to get the nodes that are inside of a panel's contex
*/
function boost_ctools_render_alter($info, $page, $args, $contexts, $task, $subtask, $handler) {
// return if page not going to be cached or if database turned off
if (!$GLOBALS['_boost_cache_this'] || BOOST_NO_DATABASE) {
return;
}
foreach ($handler->conf['display']->context as $context) {
if ($context->type == 'node') {
$node = $context->data;
// skip if node type is not set
if (!isset($node->type)) {
continue;
}
// set data
$relationship = array(
'child_page_callback' => 'node',
'child_page_type' => $node->type,
'child_page_id' => $node->nid,
);
if (BOOST_VERBOSE >= 9) {
$relationship['debug'] = array(
'panel-task' => $handler->task,
'panel-subtask' => $handler->subtask,
'node-path' => $node->path,
);
}
// send to global
$GLOBALS['_boost_relationships'][] = $relationship;
}
}
}
/**
* Implementation of hook_views_pre_render().
*
* This is called right before the render process. Used to grab the NID's listed
* in this view, and set the view node relationship in the database.
*
* @param &$view
* reference to the view being worked on
*/
function boost_views_pre_render(&$view) {
// return if not a view, page not going to be cached, or if database turned off
if (is_null($view) || !$GLOBALS['_boost_cache_this'] || BOOST_NO_DATABASE) {
return;
}
// return if view doesn't belong in the set of views to search
$views = boost_views_get_valid_array();
$hash = $view->name . ' - ' . $view->current_display;
if (!array_key_exists($hash, $views)) {
return;
}
foreach ($view->result as $item) {
// skip if nid is not a number
if (!is_numeric($item->nid)) {
continue;
}
$node = boost_node_get_basics($item->nid);
// skip if node type is not set
if (!isset($node->type)) {
continue;
}
// set data
$relationship = array(
'child_page_callback' => 'node',
'child_page_type' => $node->type,
'child_page_id' => $item->nid,
);
if (BOOST_VERBOSE >= 9) {
$relationship['debug'] = array(
'view-name' => $view->name,
'view-display' => $view->current_display,
'node-path' => $node->path,
);
}
// send to global
$GLOBALS['_boost_relationships'][] = $relationship;
}
}
/**
* Genrate a list of views based off of defaults.
*
* Views that have a path, can be cached by boost & anonymous users can access
*
* @return array
* (view_name . ' - ' . display_name . ' - /' . path => Enabled/Disabled)
*/
function boost_views_generate_default_list() {
$account = user_load(0);
$views = views_get_all_views();
$enabled = array();
$disabled = array();
foreach ($views as $view_name => $view) {
// disabled views get nothing.
if (!empty($view->disabled)) {
unset($views[$view_name]);
continue;
}
$view->init_display();
foreach ($view->display as $display_id => $display) {
// Anonymous users can access view
if (!$view->access($display_id, $account)) {
continue;
}
$hash = $view_name . ' - ' . $display_id;
// Use the default view
if (strcmp($display_id, 'default') == 0) {
$enabled[$hash] = $hash;
}
// View with a path
elseif (isset($display->display_options['path'])) {
$path = $display->display_options['path'];
$hash = $view_name . ' - ' . $display_id . ' - /' . $path;
// Path is cacheable via boost & does not take arguments
if (boost_is_cacheable($path) && !stristr($path, '%')) {
$enabled[$hash] = $hash;
}
// Path is not cacheable via boost or view takes arguments
else {
$disabled[$hash] = 0;
}
}
// All other displays mark as disabled
else {
$disabled[$hash] = 0;
}
}
}
ksort($disabled);
natcasesort($enabled);
$checkboxes = array_merge(array('line-break' => 0), $disabled, $enabled);
return $checkboxes;
}
/**
* Genrate a list of valid views that boost will search for new content.
*
* @return array
*/
function boost_views_get_valid_list($fresh = FALSE) {
// Load saved default values; if not saved, save it.
$defaults = variable_get('boost_views_list_default', FALSE);
if (!$defaults || $fresh) {
$defaults = boost_views_generate_default_list();
variable_set('boost_views_list_default', $defaults);
}
// Load custom settings
$list = variable_get('boost_views_list_custom', array());
// Load defaults. http://php.net/operators.array
$list += $defaults;
return $list;
}
/**
* Genrate a list of valid views that boost will search for new content.
*
* @return array
*/
function boost_views_get_valid_array() {
$list = boost_views_get_valid_list();
$views = array();
foreach ($list as $hash => $enabled) {
if ($enabled) {
$info = explode(' - ', $hash);
$page_type = $info[0];
$page_id = $info[1];
$key = $page_type . ' - ' . $page_id;
$views[$key]['page_type'] = $page_type;
$views[$key]['page_id'] = $page_id;
}
}
return $views;
}
/**
* Implementation of hook_views_pre_view().
*
* Hack due to this issue: http://drupal.org/node/619852
*/
function boost_views_pre_view(&$view) {
}
/**
* Send out a fast 404 and exit.
*/
function boost_fast404() {
global $base_path;
if (!headers_sent()) {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
}
print '' . "\n";
print '';
print '' . t('404 Not Found') . '';
print '
' . t('Not Found') . '
';
print '
' . t('The requested URL was not found on this server.') . '
';
}
//////////////////////////////////////////////////////////////////////////////
// Output buffering callback
/**
* PHP output buffering callback for static page caching.
*/
function _boost_ob_handler() {
global $_boost;
$buffer = ob_get_contents();
// If Compressed data was given to us decompress it
if (boost_headers_contain('gzip')) {
$decompressed_buffer = gzinflate(substr(substr($buffer, 10), 0, -8));
}
else {
$decompressed_buffer = $buffer;
}
// Ensure we're in the correct working directory, since some web servers (e.g. Apache) mess this up here.
chdir(dirname($_SERVER['SCRIPT_FILENAME']));
// Very late cache canceling
$router_item = _boost_get_menu_router();
if ( $router_item['page_callback'] == 'search404_page'
|| $router_item['page_callback'] == 'fivestar_vote'
) {
$GLOBALS['_boost_cache_this'] = FALSE;
}
// Check for PHP errors
if ($error = _boost_page_have_error()) {
$GLOBALS['_boost_cache_this'] = FALSE;
if (BOOST_VERBOSE >= 3) {
watchdog('boost', 'There are php errors on this page, preventing boost from caching. ERROR:
%error
!link !performance', array('%error' => boost_print_r($error, TRUE), '!link' => l(t('Lookup Error Type'), 'http://php.net/errorfunc.constants'), '!performance' => l(t('Turn Off Error Checking'), 'admin/settings/performance/boost')), WATCHDOG_WARNING);
}
}
// Check for drupal messages
if (BOOST_HALT_ON_MESSAGES && isset($GLOBALS['_boost_message_count']) && $GLOBALS['_boost_message_count'] != 0) {
$GLOBALS['_boost_cache_this'] = FALSE;
if (BOOST_VERBOSE >= 3) {
watchdog('boost', 'There are Drupal messages on this page, preventing boost from caching. MESSAGES: %msg !performance', array('%msg' => $GLOBALS['_boost_message_count'], '!performance' => l(t('Turn Off Error Checking'), 'admin/settings/performance/boost')), WATCHDOG_WARNING);
}
}
// Check the currently set content type and the HTTP response code. only cache
// 'text/*' pages that were output with a 200 status number.
if (!empty($decompressed_buffer)) {
$status = boost_get_http_status();
$types = boost_get_content_type();
if (BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_ob_handler_info'])) {
watchdog('boost', 'Debug: _boost_ob_handler() HTTP Info: !status - !types Path: !path Content Container: !callback Content Type: !type ID: !id Cache This: !cache.', array('!status' => $status, '!types' => implode(', ', $types), '!path' => boost_file_path($GLOBALS['_boost_path']), '!callback' => $router_item['page_callback'], '!type' => $router_item['page_type'], '!id' => $router_item['page_id'], '!cache' => $GLOBALS['_boost_cache_this'] ? 'TRUE' : 'FALSE'));
}
// Bail out if we can not cache
if ($status != 200 || $GLOBALS['_boost_cache_this'] == FALSE) {
return;
}
// Check for corret types and cache accordingly
$types = array_pop($types);
if (stristr($types, 'text/javascript') && BOOST_CACHE_JSON) {
if (BOOST_ASYNCHRONOUS_OUTPUT && !headers_sent()) {
boost_async_opp($buffer, FALSE, 'text/javascript; charset=utf-8');
}
boost_cache_set($GLOBALS['_boost_path'], $decompressed_buffer, BOOST_JSON_EXTENSION);
}
elseif ((stristr($types, 'application/rss') || stristr($types, 'text/xml') || stristr($types, 'application/rss+xml')) && BOOST_CACHE_XML) {
if (BOOST_ASYNCHRONOUS_OUTPUT && !headers_sent()) {
boost_async_opp($buffer, FALSE, 'text/xml; charset=utf-8');
}
boost_cache_set($GLOBALS['_boost_path'], $decompressed_buffer, BOOST_XML_EXTENSION);
}
elseif (stristr($types, 'text/html') && BOOST_CACHE_HTML) {
if (BOOST_ASYNCHRONOUS_OUTPUT && !headers_sent()) {
boost_async_opp($buffer, FALSE, 'text/html; charset=utf-8');
}
boost_cache_set($GLOBALS['_boost_path'], $decompressed_buffer, BOOST_FILE_EXTENSION);
// html output requires special handling of the aggregated js/css files.
boost_cache_css_js_files($decompressed_buffer);
}
}
}
/**
* Determines the MIME content type of the current page response based on
* the currently set Content-Type HTTP header.
*
* This should normally return the string 'text/html' unless another module
* has overridden the content type.
*/
function boost_get_content_type() {
$headers = explode("content-type: ", strtolower(drupal_get_headers()));
$types = array();
foreach ($headers as $header) {
if (! empty($header)) {
list($header_first_line) = explode('\n', $header);
list($header_first_part) = explode('; charset=', $header_first_line);
$types[] = $header_first_part;
}
}
return array_filter($types);
}
/**
* Determines the HTTP response code that the current page request will be
* returning by examining the HTTP headers that have been output so far.
*/
function boost_get_http_status() {
if (function_exists('drupal_get_headers')) {
foreach (explode("\n", drupal_get_headers()) as $header) {
preg_match('!^HTTP\/.*?\s+(\d+)!', $header, $matches);
if (isset($matches[1])) {
return (int) $matches[1];
}
}
}
return 200;
}
function boost_headers_contain($text) {
if (function_exists('headers_list')) {
$list = headers_list();
if (empty($list)) {
return FALSE;
}
foreach ($list as $header) {
$info = stristr($header, $text);
if ($info !== FALSE) {
return $info;
}
}
}
return FALSE;
}
//////////////////////////////////////////////////////////////////////////////
// Boost API implementation
/**
* Determines whether a given url can be cached or not by boost.
*
* To avoid potentially troublesome situations, the user login page is never
* cached, nor are any admin pages.
*
* @param $path
* Current URL
*
* $path = $GLOBALS['_boost_path'] most of the time
* uses $GLOBALS['_boost_query'] as well
*/
function boost_is_cacheable($path) {
global $base_root;
$is_front = FALSE;
if (empty($path)) {
$is_front = TRUE;
$path = variable_get('site_frontpage', 'node');
}
$normal_path = drupal_get_normal_path($path); // normalize path
$full = $normal_path . '-' . $GLOBALS['_boost_query'];
$decoded1 = urldecode($full);
$decoded2 = urldecode($decoded1);
while ($decoded1 != $decoded2) {
$decoded1 = urldecode($decoded2);
$decoded2 = urldecode($decoded1);
}
$decoded = $decoded2;
unset($decoded2);
unset($decoded1);
$url = $base_root . request_uri();
// Never cache
// the user login/registration/password/reset pages
// any admin pages
// comment reply pages
// boost_stats.php
// any shopping cart pages
// node add page
// openid login page
// filefield upload progress page
// URL variables that contain / or \
// if incoming URL contains '..' or null bytes or ://
// if url contains #
// Limit the maximum directory nesting depth of the path
// Do not cache if destination is set
if ( $normal_path == 'user'
|| preg_match('!^user/(login|register|password|reset)!', $normal_path)
|| preg_match('!^admin!', $normal_path)
|| preg_match('!^comment/reply!', $normal_path)
|| preg_match('!boost_stats.php$!', $normal_path)
|| preg_match('!^cart!', $normal_path)
|| preg_match('!^node/add!', $normal_path)
|| preg_match('!^openid!', $normal_path)
|| preg_match('!^filefield/progress/!', $normal_path)
|| strpos($GLOBALS['_boost_query'], '/')
|| strpos($GLOBALS['_boost_query'], "\\")
|| strpos($full, '..') !== FALSE
|| strpos($full, "\0") !== FALSE
|| strpos($decoded, '://') !== FALSE
|| strpos($decoded, '#') !== FALSE
|| strpos($decoded, '..') !== FALSE
|| strpos($decoded, "\0") !== FALSE
|| count(explode('/', $path)) > BOOST_MAX_PATH_DEPTH
|| !empty($_GET['destination'])
) {
return FALSE;
}
if (!BOOST_CACHE_XML && (preg_match('!/feed$!', $normal_path) || preg_match('!\.xml$!', $normal_path))) {
return FALSE;
}
if (!BOOST_CACHE_QUERY && ($GLOBALS['_boost_query'] != BOOST_CHAR || strstr($url, '?') !== FALSE)) {
return FALSE;
}
// Don't cache path if it can't be served by apache.
if (BOOST_ONLY_ASCII_PATH) {
if (preg_match('@[^/a-z0-9_\-&=,\.:]@i', $path)) {
return FALSE;
}
}
// Check if this domain has been whitelisted for caching.
$use_lists = variable_get('boost_domain_use_lists', BOOST_DOMAIN_NO_LISTS);
if ( $use_lists == BOOST_DOMAIN_WHITELIST_ONLY
|| $use_lists == BOOST_DOMAIN_BOTH_LISTS) {
$is_whitelisted = FALSE;
$whitelist = variable_get('boost_domain_whitelist', array());
$current_domain = $_SERVER['HTTP_HOST'];
/* It'd be possible (and involve less code overall) to use domain_lookup()
* here instead of stuffing everything into the "boost_domain_whitelist"
* variable, but this method will avoid the extra db query and hooks that
* calling domain_lookup can cause during page load.
*/
$is_whitelisted = isset($whitelist[$current_domain]);
if (!$is_whitelisted) {
// Loop through the list of wildcards, trying to match the current domain.
$whitelist_wild = variable_get('boost_domain_whitelist_wild', array());
$current_domain_array = explode('.', $current_domain); // www.bar.com -> array('www','bar','com')
foreach ($whitelist_wild as $wildcard) {
$wildcard_array = explode('.', $wildcard); // *.quux.qz -> array('*','quux','qz')
// If the arrays aren't the same size, don't bother matching their contents.
$wc_count = count($wildcard_array);
if (count($current_domain_array) != $wc_count) {
continue;
}
for ($i = 0; $i < $wc_count; $i++) {
/* If the current element isn't * and doesn't match the pattern, skip
* to the next wildcard. */
if ( $wildcard_array[$i] != '*'
&& $wildcard_array[$i] != $current_domain_array[$i]) {
break;
}
/* If the loop hasn't terminated by now, all elements of the current
* wildcard array were checked. Mark this domain as whitelisted and
* don't check any other whitelist elements.
*/
if ($i == $wc_count-1) {
$is_whitelisted = TRUE;
break 2;
}
}
}
}
if ( !$is_whitelisted
&& variable_get('boost_domain_whitelist_use_domain', FALSE)
&& function_exists('domain_alias_lookup')) {
/* Either I call domain_alias_lookup (and deal with the cost), I ignore
* its wildcard capabilities and stuff all aliases into
* boost_domain_whitelist, or I implement something subtly incompabile. */
$is_whitelisted = -1 != domain_alias_lookup($current_domain);
}
if (!$is_whitelisted) {
return FALSE;
}
}
// Check if this domain has been blacklisted for caching.
if ( $use_lists == BOOST_DOMAIN_BLACKLIST_ONLY
|| $use_lists == BOOST_DOMAIN_BOTH_LISTS) {
$blacklist = variable_get('boost_domain_blacklist', array());
if (isset($blacklist[$current_domain])) {
return FALSE;
}
}
// Check for reserved characters if on windows
// http://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
// " * : < > |
$chars = '"*:<>|';
if (stristr(PHP_OS, 'WIN') && preg_match("/[" . $chars . "]/", $full)) {
return FALSE;
}
// Don't cache if path is in the source; but allow the front page to be cached
if (variable_get('boost_cache_url_alias_src', FALSE) && !$is_front && (int)db_result(db_query("SELECT count(*) FROM {url_alias} WHERE src = '%s'", $path)) > 0) {
return FALSE;
}
// Invoke hook_boost_is_cacheable($path)
foreach (module_implements('boost_is_cacheable') as $module) {
if (($result = module_invoke($module, 'boost_is_cacheable', $path)) !== NULL) {
if (!$result) {
return FALSE;
}
}
}
// See http://api.drupal.org/api/function/block_list/6
// Match the user's cacheability settings against the path
if (BOOST_CACHEABILITY_PAGES) {
if (BOOST_CACHEABILITY_OPTION < 2) {
$page_match = drupal_match_path($path, BOOST_CACHEABILITY_PAGES);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], BOOST_CACHEABILITY_PAGES);
}
// When BOOST_CACHEABILITY_OPTION has a value of 0, boost will cache
// all pages except those listed in BOOST_CACHEABILITY_PAGES. When set
// to 1, boost will cache only on those pages listed in BOOST_CACHEABILITY_PAGES.
$page_match = !(BOOST_CACHEABILITY_OPTION xor $page_match);
}
else {
$page_match = drupal_eval(BOOST_CACHEABILITY_PAGES);
}
}
else {
$page_match = TRUE;
}
return $page_match;
}
/**
* This hook is run inorder to determine if a page should be cached.
* Runs in boost_init().
*
* @return FALSE to not cache the page, TRUE to cache the page.
* Returning a FALSE is absolute, Returning TRUE is more like an ignore,
* doesn't guarantee it will be cached; some other setting could make the
* is_boost_cacheable() call return FALSE to boost_init()
*
*/
function hook_boost_is_cacheable($path) {
return TRUE;
}
/**
* Determines whether a given Drupal page is currently cached or not.
*
* @param $path
* Current URL
*/
function boost_is_cached($path) {
// no more need to check if path is empty cause it is done on the input of this function before calling it
// no more need to use drupal_get_normal_path - we do not need the internal path (node/56) - we are fine with aliases
return file_exists(boost_file_path($path));
}
/**
* Deletes all files currently in the cache.
*/
function boost_cache_clear_all() {
global $_boost;
if (variable_get('boost_ignore_flush', 0) == 0) {
boost_cache_clear_all_db();
boost_cache_delete(TRUE);
if (BOOST_VERBOSE >= 5 && isset($_boost['verbose_option_selected']['boost_cache_clear_all'])) {
watchdog('boost', 'Flushed ALL files from static page cache.', array(), WATCHDOG_NOTICE);
}
return TRUE;
}
return FALSE;
}
/**
* Deletes all expired static files currently in the cache via filesystem.
*/
function boost_cache_expire_all_filesystem() {
boost_cache_delete(FALSE);
return TRUE;
}
/**
* Resets all entries in database.
*/
function boost_cache_clear_all_db() {
if (BOOST_FLUSH_ALL_MULTISITE) {
db_query("UPDATE {boost_cache} SET expire = %d", 0);
}
else {
db_query("UPDATE {boost_cache} SET expire = %d WHERE base_dir = '%s'", 0, BOOST_FILE_PATH);
}
}
/**
* Deletes files in the cache.
*
* @param $flush
* If true clear the entire cache directory.
*/
function boost_cache_delete($flush = FALSE) {
clearstatcache();
//recreate dirs
_boost_mkdir_p(BOOST_FILE_PATH);
_boost_mkdir_p(BOOST_GZIP_FILE_PATH);
//add in .boost root id file
_boost_write_file_chmod(BOOST_FILE_PATH . '/' . BOOST_ROOT_FILE, BOOST_FILE_PATH);
_boost_write_file_chmod(BOOST_GZIP_FILE_PATH . '/' . BOOST_ROOT_FILE, BOOST_FILE_PATH);
foreach (_boost_copy_file_get_domains(BOOST_PERM_FILE_PATH) as $dir) {
_boost_write_file_chmod($dir . '/' . BOOST_ROOT_FILE, $dir);
}
foreach (_boost_copy_file_get_domains(BOOST_PERM_GZIP_FILE_PATH) as $dir) {
_boost_write_file_chmod($dir . '/' . BOOST_ROOT_FILE, $dir);
}
//Flush Cache
if (file_exists(BOOST_FILE_PATH)) {
_boost_rmdir_rf(BOOST_FILE_PATH, $flush, TRUE);
}
if (file_exists(BOOST_GZIP_FILE_PATH)) {
_boost_rmdir_rf(BOOST_GZIP_FILE_PATH, $flush, TRUE);
}
//recreate dirs
_boost_mkdir_p(BOOST_FILE_PATH);
_boost_mkdir_p(BOOST_GZIP_FILE_PATH);
//add in .boost root id file
_boost_write_file_chmod(BOOST_FILE_PATH . '/' . BOOST_ROOT_FILE, BOOST_FILE_PATH);
_boost_write_file_chmod(BOOST_GZIP_FILE_PATH . '/' . BOOST_ROOT_FILE, BOOST_FILE_PATH);
foreach (_boost_copy_file_get_domains(BOOST_PERM_FILE_PATH) as $dir) {
_boost_write_file_chmod($dir . '/' . BOOST_ROOT_FILE, $dir);
}
foreach (_boost_copy_file_get_domains(BOOST_PERM_GZIP_FILE_PATH) as $dir) {
_boost_write_file_chmod($dir . '/' . BOOST_ROOT_FILE, $dir);
}
// Make sure cache dir has htaccess rules
boost_htaccess_cache_dir_put();
}
function boost_htaccess_cache_dir_put() {
// Server is not apache; do nothing
if (stristr($_SERVER["SERVER_SOFTWARE"], 'apache') == FALSE) {
return TRUE;
}
// Get some info
$cache_dir = BOOST_ROOT_CACHE_DIR;
$filename = $cache_dir . '/.htaccess';
$generated = boost_htaccess_cache_dir_generate();
$htaccess = file_exists($filename) ? file_get_contents($filename) : FALSE;
// htaccess exists and has the correct contents
if ($htaccess && strcmp($htaccess, $generated) === 0) {
return TRUE;
}
// cache dir doesn't exist, try to create it; if no go, bail out.
if (!is_dir($cache_dir) && !_boost_mkdir_p($cache_dir)) {
return FALSE;
}
// cache dir htaccess is not there, create it.
$result = file_put_contents($filename, $generated);
if ($result) {
return $result;
}
else {
return FALSE;
}
}
function boost_htaccess_cache_dir_generate() {
global $base_path;
// no dot
$_html = str_replace('.', '', BOOST_FILE_EXTENSION);
$_xml = str_replace('.', '', BOOST_XML_EXTENSION);
$_css = str_replace('.', '', BOOST_CSS_EXTENSION);
$_js = str_replace('.', '', BOOST_JS_EXTENSION);
$_json = str_replace('.', '', BOOST_JSON_EXTENSION);
$_gz = str_replace('.', '', BOOST_GZIP_EXTENSION);
// with a \ slash
$gz = str_replace('.', '\\.', BOOST_GZIP_EXTENSION);
$string = '';
if (BOOST_CACHE_HTML) {
if (variable_get('boost_force_utf8', TRUE)) {
$string .= "AddDefaultCharset utf-8\n";
}
}
switch (variable_get('boost_apache_etag', 0)) {
case 0:
break;
case 1:
$string .= "FileETag None\n";
break;
case 2:
$string .= "FileETag All\n";
break;
case 3:
$string .= "FileETag MTime Size\n";
break;
}
if (!BOOST_DISABLE_CLEAN_URL && (BOOST_CACHE_HTML || BOOST_CACHE_XML || BOOST_CACHE_JSON)) {
$files = array();
if (BOOST_CACHE_HTML) {
$files[] = $_html;
}
if (BOOST_CACHE_XML) {
$files[] = $_xml;
}
if (BOOST_CACHE_JSON) {
$files[] = $_json;
}
$files = '(' . implode('|' , $files) . ')';
if (BOOST_GZIP) {
$files = "$files|($files$gz)";
}
$string .= "\n";
$string .= " \n";
$string .= " ExpiresDefault A1\n";
$string .= " \n";
$string .= " \n";
$string .= " Header set Expires \"Sun, 19 Nov 1978 05:00:00 GMT\"\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', 0) > 0) {
$string .= " Header set X-Header \"Boost Citrus 1.8\"\n";
}
$string .= " \n";
$string .= "\n";
}
if (BOOST_CACHE_HTML || BOOST_CACHE_XML || BOOST_CACHE_CSS || BOOST_CACHE_JS || BOOST_CACHE_JSON) {
$string .= "\n";
$string .= BOOST_CACHE_HTML ? " AddCharset utf-8 .$_html\n" : '';
$string .= BOOST_CACHE_XML ? " AddCharset utf-8 .$_xml\n" : '';
$string .= BOOST_CACHE_CSS ? " AddCharset utf-8 .$_css\n" : '';
$string .= BOOST_CACHE_JS ? " AddCharset utf-8 .$_js\n" : '';
$string .= BOOST_CACHE_JSON ? " AddCharset utf-8 .$_json\n" : '';
$string .= BOOST_GZIP ? " AddEncoding gzip .$_gz\n" : '';
$string .= "\n";
}
// Fix for versions of apache that do not respect the T='' RewriteRule
$files = '';
if (BOOST_CACHE_HTML) {
$files .= "$_html|";
if (BOOST_GZIP) {
$files .= "$_html$gz|";
}
$files = trim($files, '|');
$string .= "\n";
$string .= " ForceType text/html\n";
$string .= "\n";
}
$files = '';
if (BOOST_CACHE_XML) {
$files .= "$_xml|";
if (BOOST_GZIP) {
$files .= "$_xml$gz|";
}
$files = trim($files, '|');
$string .= "\n";
$string .= " ForceType text/xml\n";
$string .= "\n";
}
$files = '';
if (BOOST_CACHE_JSON) {
$files .= "$_json|";
if (BOOST_GZIP) {
$files .= "$_json$gz|";
}
}
if (BOOST_CACHE_JS) {
$files .= "$_js|";
if (BOOST_GZIP) {
$files .= "$_js$gz|";
}
}
if ($files != '') {
$files = trim($files, '|');
$string .= "\n";
$string .= " ForceType text/javascript\n";
$string .= "\n";
}
$files = '';
if (BOOST_CACHE_CSS) {
$files .= "$_css|";
if (BOOST_GZIP) {
$files .= "$_css$gz|";
}
$files = trim($files, '|');
$string .= "\n";
$string .= " ForceType text/css\n";
$string .= "\n";
}
$string .= "\n";
$string .= "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\n";
$string .= "Options None\n";
$string .= "Options +FollowSymLinks\n";
$string .= "\n";
return $string;
}
/**
* Finds all possible paths/redirects/aliases given the root path.
*
* @param $paths
* Array of current URLs
* @param $both
* Expire database & file
* @param $force_flush
* Override the settings and kill the file
*/
function boost_cache_expire_derivative($paths, $both = FALSE, $force_flush = FALSE, $debug = FALSE) {
global $base_path;
$expire = array();
if (empty($paths)) {
return FALSE;
}
foreach ($paths as $path) {
// Given path
$expire[] = $path;
// Add the empty front page path if this is the alias
if ($path == variable_get('site_frontpage', 'node')) {
$expire[] = '';
$expire[] = 'rss.xml';
}
// Special front page feed handling
if (BOOST_CACHE_XML && ($path == '' || $path == '')) {
$expire[] = 'rss.xml';
}
// Path alias
$path_alias = url($path, array('absolute' => FALSE));
if ($base_path != '/') {
$path_alias = implode('/', array_diff_assoc(array_filter(explode('/', $path_alias)), array_filter(explode('/', $base_path))));
}
$expire[] = $path_alias;
// Path redirects
if (module_exists('path_redirect')) {
$path_redirects = boost_path_redirect_load(array('redirect' => $path));
if (isset($path_redirects)) {
foreach ($path_redirects as $path_redirect) {
$expire[] = $path_redirect['path'];
}
}
}
}
// Expire cached files
$counter = 0;
if (empty($expire)) {
return FALSE;
}
$expire = array_unique($expire);
if (!$debug) {
if (BOOST_NO_DATABASE ) {
$counter += boost_cache_expire_by_filename($expire, TRUE, $force_flush);
}
elseif ($both) {
$counter += boost_cache_expire_by_db($expire);
$counter += boost_cache_expire_by_filename($expire, TRUE, $force_flush);
}
else {
$counter += boost_cache_expire_by_db($expire);
if ($counter == 0) {
// Database was a negative. Fallback: Look into flushing by filename
$counter += boost_cache_expire_by_filename($expire, TRUE, $force_flush);
}
}
return $counter;
}
else {
return array('in' => $expire, 'db' => boost_cache_expire_by_db($expire, TRUE), 'filename' => boost_cache_expire_by_filename($expire, TRUE, FALSE, TRUE));
}
}
/**
* Expires the static file cache for the given paths via database.
*
* @param $paths
* Array of URL's
* @param $debug
* TRUE to display what would have happened
*/
function boost_cache_expire_by_db($paths, $debug = FALSE) {
$hashes = array();
if (empty($paths)) {
return FALSE;
}
// Get all cache files directly associated with this path
foreach ($paths as $path) {
// With URL Variables
$html = boost_file_path($path, TRUE, BOOST_FILE_EXTENSION);
if ($html !== FALSE) {
$xml = boost_file_path($path, TRUE, BOOST_XML_EXTENSION);
$json = boost_file_path($path, TRUE, BOOST_JSON_EXTENSION);
// Hash the paths
$hashes[] = md5($html);
$hashes[] = md5($xml);
$hashes[] = md5($json);
}
// Without URL Variables
$html = boost_file_path($path, FALSE, BOOST_FILE_EXTENSION);
if ($html !== FALSE) {
$xml = boost_file_path($path, FALSE, BOOST_XML_EXTENSION);
$json = boost_file_path($path, FALSE, BOOST_JSON_EXTENSION);
// Hash the paths
$hashes[] = md5($html);
$hashes[] = md5($xml);
$hashes[] = md5($json);
}
}
$result = boost_db_multi_select_in('boost_cache', 'hash', "'%s'", $hashes);
// Eliminate duplicates with the key hash
$data = array();
$counter = 0;
$filenames = array();
if ($result) {
while ($info = db_fetch_array($result)) {
if (($info['page_callback'] == 'node' || $info['page_callback'] == 'taxonomy') && $info['page_id'] == 0) {
// If we can't get a 'lock' just expire the file
$filenames[] = $info['filename'];
}
elseif ($info['page_id'] != '' && $info['page_type'] != '' && $info['page_callback'] != '') {
// Use boost_cache_expire_router() if we can get a 'lock' on this item in the database
$hash = BOOST_FILE_PATH . $info['page_callback'] . $info['page_type'] . $info['page_id'];
$data[$hash] = $info;
}
else {
// If we can't get a 'lock' just expire the file
$filenames[] = $info['filename'];
}
}
// Expire all files that match up
if (!$debug) {
if ($data) {
boost_set_base_dir_in_array($data);
$counter += boost_cache_expire_router($data);
}
if ($filenames) {
$counter += boost_cache_flush_by_filename($filenames);
}
}
else {
boost_set_base_dir_in_array($data);
return array($data, $filenames);
}
}
return $counter;
}
/**
* Expires the static file cache for paths matching a wildcard via filesystem.
*
* @param $path
* Array of URLs
* @param $wildcard
* If true get all chached files that start with this path.
* @param $force_flush
* If true kill file no matter what.
*/
function boost_cache_expire_by_filename($paths, $wildcard = TRUE, $force_flush, $debug = FALSE) {
$filenames = array();
if (empty($paths)) {
return FALSE;
}
foreach ($paths as $path) {
// Sanity check
if (boost_file_path($path, FALSE) === FALSE) {
continue;
}
// Get list of related files
$html = glob(boost_file_path($path, FALSE, NULL) . (($wildcard) ? '*' : '') . BOOST_FILE_EXTENSION, GLOB_NOSORT);
$xml = glob(boost_file_path($path, FALSE, NULL) . (($wildcard) ? '*' : '') . BOOST_XML_EXTENSION, GLOB_NOSORT);
$json = glob(boost_file_path($path, FALSE, NULL) . (($wildcard) ? '*' : '') . BOOST_JSON_EXTENSION, GLOB_NOSORT);
// Make sure something is in the arrays
$html[] = '';
$xml[] = '';
$json[] = '';
// Merge arrays
$filenames = array_filter(array_merge($filenames, $html, $xml, $json));
}
// Remove double slash from filename if it exists.
foreach ($filenames as $key => $filename) {
$filenames[$key] = implode('/', array_filter(explode('/', $filename)));
}
// Flush expired files
if (!$debug) {
boost_cache_flush_by_filename($filenames, $force_flush);
}
else {
return $filenames;
}
}
/**
* Expires the static file cache for files given.
*
* @param array $filenames
* filenames
* @param $force_flush
* If true get all chached files that start with this path.
*/
function boost_cache_flush_by_filename($filenames, $force_flush = FALSE) {
global $_boost;
$files = array();
if ($filenames) {
$filenames = array_unique($filenames);
foreach ($filenames as $filename) {
$files[] = array('filename' => $filename);
}
$counter = boost_cache_kill($files, $force_flush);
if (BOOST_VERBOSE >= 9 && isset($_boost['verbose_option_selected']['boost_cache_flush_filename'])) {
watchdog('boost', 'Debug: boost_cache_flush_by_filename() Following files where flushed: !list', array('!list' => implode(' ', $filenames)));
}
return $counter;
}
else {
return FALSE;
}
}
/**
* Expires the static file cache for the given router items.
*
* @param $router_items
* Array of $router_item array "objects"
* ['page_callback']
* ['page_type']
* ['page_id']
* ['base_dir']
* @param $force_flush
* Override BOOST_EXPIRE_NO_FLUSH setting
* @param $remove_from_db
* Remove this entry from the boost-cache table.
* @param $debug
* Do nothing but return what would have been expired.
*/
function boost_cache_expire_router($router_items, $force_flush = FALSE, $remove_from_db = FALSE, $debug = FALSE) {
// Get filenames & hash from db
if (!is_array($router_items)) {
return FALSE;
}
global $_boost;
$count = 0;
$files = $list = array();
foreach ($router_items as $dblookup) {
if (isset($dblookup['base_dir'])) {
if (isset($dblookup['page_id']) && isset($dblookup['page_type']) && isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE base_dir = '%s' AND page_callback = '%s' AND page_type = '%s' AND page_id = '%s'", $dblookup['base_dir'], $dblookup['page_callback'], $dblookup['page_type'], $dblookup['page_id']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE base_dir = '%s' AND page_callback = '%s' AND page_type = '%s' AND page_id = '%s'", $dblookup['base_dir'], $dblookup['page_callback'], $dblookup['page_type'], $dblookup['page_id']);
}
}
elseif (isset($dblookup['page_id']) && isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE base_dir = '%s' AND page_callback = '%s' AND page_id = '%s'", $dblookup['base_dir'], $dblookup['page_callback'], $dblookup['page_id']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE base_dir = '%s' AND page_callback = '%s' AND page_id = '%s'", $dblookup['base_dir'], $dblookup['page_callback'], $dblookup['page_id']);
}
}
elseif (isset($dblookup['page_type']) && isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE base_dir = '%s' AND page_callback = '%s' AND page_type = '%s'", $dblookup['base_dir'], $dblookup['page_callback'], $dblookup['page_type']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE base_dir = '%s' AND page_callback = '%s' AND page_type = '%s'", $dblookup['base_dir'], $dblookup['page_callback'], $dblookup['page_type']);
}
}
elseif (isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE base_dir = '%s' AND page_callback = '%s'", $dblookup['base_dir'], $dblookup['page_callback']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE base_dir = '%s' AND page_callback = '%s'", $dblookup['base_dir'], $dblookup['page_callback']);
}
}
else {
continue;
}
}
else {
if (isset($dblookup['page_id']) && isset($dblookup['page_type']) && isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE page_callback = '%s' AND page_type = '%s' AND page_id = '%s'", $dblookup['page_callback'], $dblookup['page_type'], $dblookup['page_id']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE page_callback = '%s' AND page_type = '%s' AND page_id = '%s'", $dblookup['page_callback'], $dblookup['page_type'], $dblookup['page_id']);
}
}
elseif (isset($dblookup['page_id']) && isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE page_callback = '%s' AND page_id = '%s'", $dblookup['page_callback'], $dblookup['page_id']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE page_callback = '%s' AND page_id = '%s'", $dblookup['page_callback'], $dblookup['page_id']);
}
}
elseif (isset($dblookup['page_type']) && isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE page_callback = '%s' AND page_type = '%s'", $dblookup['page_callback'], $dblookup['page_type']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE page_callback = '%s' AND page_type = '%s'", $dblookup['page_callback'], $dblookup['page_type']);
}
}
elseif (isset($dblookup['page_callback'])) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE page_callback = '%s'", $dblookup['page_callback']);
if (!$debug) {
db_query("DELETE FROM {boost_cache_relationships} WHERE page_callback = '%s'", $dblookup['page_callback']);
}
}
else {
continue;
}
}
while ($info = db_fetch_array($result)) {
if (stristr($info['filename'], '#')) {
continue;
}
$files[$info['hash']] = $info;
if (BOOST_VERBOSE >= 9 && isset($_boost['verbose_option_selected']['boost_cache_expire_router'])) {
$list[] = $info['filename'];
}
}
}
if (!$debug) {
if (count($files)) {
$count = boost_cache_kill($files, $force_flush);
if ($remove_from_db) {
boost_remove_db($files);
}
}
if (BOOST_VERBOSE >= 9 && isset($_boost['verbose_option_selected']['boost_cache_expire_router'])) {
watchdog('boost', 'Debug: boost_cache_expire_router() Following files where flushed: !list
Input: !input Files: !files', array(
'!list' => implode(' ', $list),
'!input' => boost_print_r($router_items, TRUE, TRUE),
'!files' => boost_print_r($files, TRUE, TRUE),
));
}
return $count;
}
else {
return $files;
}
}
/**
* Deletes cached page from file system & database.
*
* @param array $urls
* list of urls to remove from the boost cache
* @param boolean $force_flush = TRUE
* Override BOOST_EXPIRE_NO_FLUSH setting.
*/
function boost_cache_kill_url($urls, $force_flush = TRUE) {
global $base_path;
$files = array();
foreach ($urls as $value) {
$decoded = urldecode($value);
if ($decoded != $value) {
$urls[] = $decoded;
}
$raw = rawurldecode($value);
if ($raw != $decoded) {
$urls[] = $decoded;
}
}
$urls = array_unique($urls);
$hashes = array_map('md5', $urls);
$parts = array_map('parse_url', $urls);
foreach ($parts as $part) {
$files[]['filename'] = boost_file_path(ltrim($part['path'], $base_path), TRUE, BOOST_FILE_EXTENSION);
}
$result = boost_db_multi_select_in('boost_cache', 'hash_url', "'%s'", $hashes);
while ($row = db_fetch_array($result)) {
$files[] = array('filename' => $row['filename'], 'hash' => $row['hash']);
}
if (!empty($files)) {
boost_cache_kill($files, $force_flush);
boost_remove_db($files);
return TRUE;
}
else {
return FALSE;
}
}
/**
* Deletes cached page from file system.
*
* @param array $files
* An array of files. Each file is a secondary array must have a key for 'filename'
* Optional keys for 'hash' and 'base_dir' that will be recalculated if necessary.
* The hash is the primary key in the database. If omitted it will be recalculated from the filename.
* @param boolean $force_flush = FALSE
* Override BOOST_EXPIRE_NO_FLUSH setting.
*/
function boost_cache_kill($files, $force_flush = FALSE) {
$hashes = array();
$count = 0;
if (!$files) {
return FALSE;
}
// If not ignoring file removal
// AND site is multisite and cache path matches filename
// OR full base url matches filename
if (variable_get('boost_ignore_flush', 0) < 3) {
// Calc md5 hash and set base dir
foreach ($files as $key => $file) {
if (!is_string($file['filename'])) {
if (BOOST_VERBOSE >= 5) {
watchdog('boost', 'Error in boost_cache_kill() String was not given for filename: !output', array('!output' => boost_print_r($file, TRUE, TRUE)));
}
continue;
}
if (empty($file['hash'])) {
$files[$key]['hash'] = md5($file['filename']);
}
if (empty($file['base_dir'])) {
$files[$key]['base_dir'] = BOOST_FILE_PATH;
}
$hashes[] = $files[$key]['hash'];
if (stristr($files[$key]['filename'], BOOST_ROOT_CACHE_DIR) == FALSE) {
unset($files[$key]);
}
}
// Expire entries from Database
if (count($hashes)) {
if ($force_flush || !BOOST_EXPIRE_NO_FLUSH) {
boost_db_multi_update_set('boost_cache', 'expire', '%d', 0, 'hash', "'%s'", $hashes);
boost_db_multi_update_set('boost_cache', 'timer', '%d', 0, 'hash', "'%s'", $hashes);
}
else {
boost_db_multi_update_set('boost_cache', 'expire', '%d', 434966400, 'hash', "'%s'", $hashes);
$count = db_affected_rows();
}
}
// Kill Files from filesystem
if ($force_flush || !BOOST_EXPIRE_NO_FLUSH) {
foreach ($files as $file) {
$filenames = boost_get_all_filenames($file['filename'], $file['base_dir']);
foreach ($filenames as $key => $values) {
foreach ($values as $num => $filename) {
if (file_exists($filename)) {
@unlink($filename);
if ($key == 'normal' && $num == 0) {
$count++;
}
}
}
}
}
}
}
return $count;
}
/**
* Flushes all expired pages from cache.
*
* TODO del empty dirs if enabled
*/
function boost_cache_expire_all() {
if (variable_get('boost_ignore_flush', 0) < 2) {
if (BOOST_NO_DATABASE) {
boost_cache_expire_all_filesystem();
}
else {
boost_cache_expire_all_db();
}
return TRUE;
}
return FALSE;
}
/**
* Flushes all expired pages via database lookup.
*
* TODO del empty dirs if enabled
*/
function boost_cache_expire_all_db() {
global $_boost;
$list = $files = array();
if (BOOST_FLUSH_ALL_MULTISITE) {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE expire BETWEEN 1 AND %d", BOOST_TIME);
}
else {
$result = db_query("SELECT filename, hash, base_dir FROM {boost_cache} WHERE base_dir = '%s' AND expire BETWEEN 1 AND %d", BOOST_FILE_PATH, BOOST_TIME);
}
while ($boost = db_fetch_array($result)) {
$files[] = $boost;
if (BOOST_VERBOSE >= 9 && isset($_boost['verbose_option_selected']['boost_cache_expire_all_db_list'])) {
$list[] = $boost['filename'];
}
}
if (count($files)) {
$count = boost_cache_kill($files, TRUE);
}
if (BOOST_FLUSH_DIR) {
// TO-DO: del empty dirs.
}
if (BOOST_VERBOSE >= 9 && isset($_boost['verbose_option_selected']['boost_cache_expire_all_db_list'])) {
watchdog('boost', 'Debug: boost_cache_expire_all_db() Following files where flushed: !list', array('!list' => implode(' ', $list)));
}
elseif (BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_verbose_refined'])) {
watchdog('boost', 'Debug: boost_cache_expire_all_db() !num files where flushed', array('!num' => $count));
}
return TRUE;
}
/**
* Returns the cached contents of the specified page, if available.
*
* @param $path
* Current URL
*/
function boost_cache_get($path) {
if (($filename = boost_file_path($path))) {
if (file_exists($filename) && is_readable($filename)) {
return file_get_contents($filename);
}
}
return NULL;
}
/**
* Returns all possible filenames given the input and current settings
*
* @param $filename
* Name of file
* @param $base_dir
* Value from base_dir column in database
* @return
* returns a 2 dimensional array
* 1st dimension key is either gzip or normal
* 2nd dimension contains all the filenames
*/
function boost_get_all_filenames($filename, $base_dir = NULL) {
$namesA = array();
$namesB = array();
$filenames = array();
$base_dir = is_null($base_dir) ? BOOST_FILE_PATH : $base_dir;
if (stristr($filename, '[')) {
$paths = explode('/', $filename);
$end = array_pop($paths);
$end = preg_replace("(\x5B[0-9]\x5D)", ']', $end);
$paths[] = $end;
$namesA[] = implode('/', $paths);
}
$namesA[] = $filename;
foreach ($namesA as $filename) {
$namesB[] = $filename;
// Generate urlencoded filename, if name contains decoded characters
$paths = explode('/', $filename);
$end = array_pop($paths);
$end = str_replace('[', '%5B', $end);
$end = str_replace(']', '%5D', $end);
$end = str_replace(',', '%2C', $end);
$end = str_replace(' ', '%20', $end);
$paths[] = $end;
$namesB[] = implode('/', $paths);
}
$namesB = array_unique($namesB);
// Generate gzip filenames
foreach ($namesB as $name) {
if (BOOST_SET_FILE_ENCODING != '') {
$name = iconv("UTF-8", BOOST_SET_FILE_ENCODING, $name);
}
$filenames['normal'][] = $name;
if (BOOST_GZIP) {
// Replace the correct dir with the gzip version for the given base dir.
$gzip_base_path = implode('/', array_filter(explode('/', str_replace(BOOST_ROOT_CACHE_DIR . '/' . BOOST_NORMAL_DIR, BOOST_ROOT_CACHE_DIR . '/' . BOOST_GZIP_DIR . '/', $base_dir))));
$filenames['gzip'][] = str_replace($base_dir, $gzip_base_path, $name) . BOOST_GZIP_EXTENSION;
}
}
return $filenames;
}
/**
* Edit document before it is put into the boost cache.
*
* This hook is run at right before the page is cached by boost.
*
* $GLOBALS['_boost_cache_this'] and $GLOBALS['_boost_router_item'] are useful.
* set $GLOBALS['_boost_cache_this'] = FALSE if you wish to not cache this page.
*
* @param $path
* URL path of the document
* @param $data
* String containing the data
* @param $extension
* file extension type. Use to detect what type of document your operating on.
* @return
* $data string containing the document
*/
function hook_boost_preprocess($path, $data, $extension) {
return $data;
}
/**
* Replaces/Sets the cached contents of the specified page, if stale.
*
* @param $path
* Current URL
* @param $data
* URL's contents
* @param $extension
* File extension for this mime type
*/
function boost_cache_set($path, $data, $extension = BOOST_FILE_EXTENSION) {
// Exit if nothing is here to cache
if (empty($data)) {
return FALSE;
}
// Get custom expiration time if set
$router_item = _boost_get_menu_router();
$settings = boost_get_settings_db($router_item);
$expire = -2;
foreach ($settings as $value) {
if ($value != NULL) {
$expire = $value['lifetime'];
break;
}
}
$cached_at = date('Y-m-d H:i:s', BOOST_TIME);
// Code commenting style based on what is being cached.
// Append the Boost footer with the relevant timestamps
switch ($extension) {
case BOOST_FILE_EXTENSION:
$expire = ($expire == -2) ? BOOST_CACHE_LIFETIME : $expire;
if (variable_get('boost_apache_xheader', 0) < 2) {
$comment_start = '\n";
$expires_at = date('Y-m-d H:i:s', BOOST_TIME + $expire);
$comment = $comment_start . str_replace(array('%cached_at', '%expires_at'), array($cached_at, $expires_at), BOOST_BANNER) . $comment_end;
//$data = _boost_inject_code(rtrim($data), "\n" . $comment);
$data = rtrim($data) . "\n" . $comment;
}
break;
case BOOST_XML_EXTENSION:
$expire = ($expire == -2) ? BOOST_CACHE_XML_LIFETIME : $expire;
if (variable_get('boost_apache_xheader', 0) < 2) {
$comment_start = '\n";
$expires_at = date('Y-m-d H:i:s', BOOST_TIME + $expire);
$comment = $comment_start . str_replace(array('%cached_at', '%expires_at'), array($cached_at, $expires_at), BOOST_BANNER) . $comment_end;
$data = rtrim($data) . "\n" . $comment;
}
break;
case BOOST_JSON_EXTENSION:
$expire = ($expire == -2) ? BOOST_CACHE_JSON_LIFETIME : $expire;
if (variable_get('boost_apache_xheader', 0) < 2) {
$comment_start = '/* ';
$comment_end = " */\n";
$expires_at = date('Y-m-d H:i:s', BOOST_TIME + $expire);
$comment = $comment_start . str_replace(array('%cached_at', '%expires_at'), array($cached_at, $expires_at), BOOST_BANNER) . $comment_end;
$data = rtrim($data) . "\n" . $comment;
}
break;
}
// Invoke hook_boost_preprocess($path, $data, $extension)
foreach (module_implements('boost_preprocess') as $module) {
if (($result = module_invoke($module, 'boost_preprocess', $path, $data, $extension)) != NULL) {
$data = $result;
}
}
// Execute the pre-process function if one has been defined
if (function_exists(BOOST_PRE_PROCESS_FUNCTION)) {
$data = call_user_func(BOOST_PRE_PROCESS_FUNCTION, $path, $data, $extension);
}
db_set_active();
// Final check, make sure this page should be cached. Allow for the preprocess
// function to have a final say in if this page should be cached.
if (!$GLOBALS['_boost_cache_this'] || empty($data)) {
return FALSE;
}
// Create or update the static files as needed
if (($filename = boost_file_path($path, TRUE, $extension)) && (BOOST_OVERWRITE_FILE || !file_exists($filename) || boost_db_is_expired($filename))) {
// Special handling of the front page for aggressive gzip test
if ($path == '' && BOOST_AGGRESSIVE_GZIP && $extension == BOOST_FILE_EXTENSION) {
_boost_generate_gzip_test_file();
boost_cache_write($filename, _boost_inject_code($data,
'' . "\n"));
}
else {
boost_cache_write($filename, $data);
}
if (!BOOST_NO_DATABASE) {
boost_db_prep($filename, $extension, BOOST_TIME + $expire);
boost_cache_set_node_relationships(isset($GLOBALS['_boost_relationships']) ? $GLOBALS['_boost_relationships'] : array());
}
return TRUE;
}
else {
return FALSE;
}
}
/**
* Creates a parent child relationship for pages like views.
*
* @param $relationships
* Array of $router_item array "objects"
* Required
* ['child_page_callback']
* ['child_page_type']
* ['child_page_id']
* Optional
* ['base_dir']
* ['page_callback']
* ['page_type']
* ['page_id']
*
* If Optional is not set it will use values in $GLOBALS['_boost_router_item']
*/
function boost_cache_set_node_relationships($relationships) {
global $base_root, $_boost;
$url = $base_root . request_uri();
if (variable_get('boost_store_url_percent_enc', FALSE)) {
$url = urldecode(rawurlencode($url));
}
$hash_url = md5($url);
$router_item = _boost_get_menu_router();
if (!is_array($relationships) || empty($relationships)) {
return FALSE;
}
// Grab all entries related to this url
$old_data = array();
$results = db_query("SELECT hash, timestamp FROM {boost_cache_relationships} WHERE hash_url = '%s'", $hash_url);
while ($row = db_fetch_array($results)) {
$old_data[$row['hash']] = $row['timestamp'];
}
$counter = 0;
$new_data = array();
foreach ($relationships as $data) {
// If one of the required items is not set, skip this entry
if (!isset($data['child_page_callback']) || !isset($data['child_page_type']) || !isset($data['child_page_id'])) {
if (BOOST_VERBOSE >= 5) {
watchdog('boost', 'boost_cache_set_node_relationships() child_page_* was not set. !data !backtrace', array('!data' => boost_print_r($data, TRUE, TRUE), '!backtrace' => boost_backtrace(FALSE)));
}
continue;
}
// Set the optional parameters
$data['base_dir'] = isset($data['base_dir']) ? $data['base_dir'] : BOOST_FILE_PATH;
$data['page_callback'] = isset($data['page_callback']) ? $data['page_callback'] : $router_item['page_callback'];
$data['page_type'] = isset($data['page_type']) ? $data['page_type'] : $router_item['page_type'];
$data['page_id'] = isset($data['page_id']) ? $data['page_id'] : $router_item['page_id'];
// Sanity Checks
foreach ($data as $key => $value) {
$loop = 0;
while (is_array($data[$key]) && $loop < 25) {
$data[$key] = array_pop($value);
$loop++;
}
}
foreach ($data as $key => $value) {
if (is_object($value) || is_array($value)) {
if (BOOST_VERBOSE >= 5) {
watchdog('boost', 'boost_cache_set_node_relationships() "!key" has a value of "!value"; should be a string or int.', array('!key' => $key, '!value' => $value));
}
continue;
}
}
// Skip if this is referencing its self.
if ($data['page_callback'] == $data['child_page_callback'] && $data['page_type'] == $data['child_page_type'] && $data['page_id'] == $data['child_page_id']) {
continue;
}
// Create the primary key
$data['hash'] = md5($data['base_dir'] . $data['page_callback'] . $data['page_type'] . $data['page_id'] . $data['child_page_callback'] . $data['child_page_type'] . $data['child_page_id']);
$new_data[] = $data;
}
// Count number of entries
$update_required = FALSE;
if (count($new_data) !== count($old_data)) {
$update_required = TRUE;
}
// See if new and old data is not the same
if (!$update_required) {
foreach ($new_data as $data) {
if (empty($old_data[$data['hash']])) {
$update_required = TRUE;
break;
}
}
}
if ($update_required) {
foreach ($new_data as $data) {
// Insert data into database
db_query("UPDATE {boost_cache_relationships}
SET base_dir = '%s',
page_callback = '%s',
page_type = '%s',
page_id = '%s',
child_page_callback = '%s',
child_page_type = '%s',
child_page_id = '%s',
hash_url = '%s',
timestamp = '%d'
WHERE hash = '%s'", $data['base_dir'], $data['page_callback'], $data['page_type'], $data['page_id'], $data['child_page_callback'], $data['child_page_type'], $data['child_page_id'], $hash_url, BOOST_TIME, $data['hash']);
if (!db_affected_rows()) {
@db_query("INSERT INTO {boost_cache_relationships} (hash, base_dir, page_callback, page_type, page_id, child_page_callback, child_page_type, child_page_id, hash_url, timestamp) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)", $data['hash'], $data['base_dir'], $data['page_callback'], $data['page_type'], $data['page_id'], $data['child_page_callback'], $data['child_page_type'], $data['child_page_id'], $hash_url, BOOST_TIME);
}
$counter++;
}
$removed = boost_cache_prune_node_relationship($hash_url);
if (BOOST_VERBOSE >= 7 && isset($_boost['verbose_option_selected']['boost_cache_set_node_relationships'])) {
watchdog('boost', 'Debug: boost_cache_set_node_relationships() !num of !total given entries to the boost_cache_relationships table added or updated; !removed entries removed due to them being outdated. !dump',
array(
'!num' => $counter,
'!total' => count($GLOBALS['_boost_relationships']),
'!removed' => $removed,
'!dump' => str_replace(' ', ' ', nl2br(htmlentities(print_r($relationships, TRUE)))),
)
);
}
}
unset($GLOBALS['_boost_relationships']);
return TRUE;
}
/**
* Creates a parent child relationship for pages like views.
*
* @param $relationships
* Array of $router_item array "objects"
* Required
* ['page_callback']
* ['page_type']
* ['page_id']
* Optional
* ['base_dir']
*/
function boost_cache_get_node_relationships($relationships) {
if (!is_array($relationships)) {
return FALSE;
}
$results = array();
foreach ($relationships as $data) {
// If one of the required items is not set, skip this entry
if (!isset($data['page_callback']) || !isset($data['page_type']) || !isset($data['page_id'])) {
continue;
}
if (BOOST_FLUSH_ALL_MULTISITE) {
$result = db_query("SELECT page_callback, page_type, page_id, base_dir FROM {boost_cache_relationships} WHERE child_page_id = '%s' AND child_page_type = '%s' AND child_page_callback = '%s'", $data['page_id'], $data['page_type'], $data['page_callback']);
}
else {
$data['base_dir'] = isset($data['base_dir']) ? $data['base_dir'] : BOOST_FILE_PATH;
$result = db_query("SELECT page_callback, page_type, page_id, base_dir FROM {boost_cache_relationships} WHERE child_page_id = '%s' AND child_page_type = '%s' AND child_page_callback = '%s' AND base_dir = '%s'", $data['page_id'], $data['page_type'], $data['page_callback'], $data['base_dir']);
}
while ($info = db_fetch_array($result)) {
$hash = 'relationship base:' . $info['base_dir'] . ' ' . $info['page_callback'] . ' ' . $info['page_type'] . ' ' . $info['page_id'];
$results[$hash] = $info;
}
}
return $results;
}
/**
* Given hash of url delete any old relationships.
*
* @param $hash_url
*/
function boost_cache_prune_node_relationship($hash_url) {
// Grab all entires related to this URL; find ones that don't match the latest
// timestamp and remove them.
$records = 0;
$result = db_query("SELECT hash, timestamp FROM {boost_cache_relationships} WHERE hash_url = '%s' ORDER BY timestamp DESC", $hash_url);
while ($info = db_fetch_array($result)) {
if ($info['timestamp'] < BOOST_TIME) {
db_query("DELETE FROM {boost_cache_relationships} WHERE hash = '%s'", $info['hash']);
$records++;
}
}
return $records;
}
/**
* Figure out what is going in the database & put it in
*
* @param $filename
* Name of cached file; primary key in database
* @param $extension
* Filename extension: Used for content types.
* @param $expire
* Cache expiration time in seconds (UNIX time).
*/
function boost_db_prep($filename, $extension, $expire) {
$router_item = _boost_get_menu_router();
$timer = timer_read('page');
$timer_average = $timer;
$lifetime = -1;
$push = -1;
$settings = boost_get_settings_db($router_item);
foreach ($settings as $value) {
if ($value != NULL) {
$boost_settings_db = $value;
break;
}
}
$boost_db = boost_get_db($filename);
//get time data from actual entry, if this page has been cached before.
if ($boost_db) {
// $expire = $boost_db['lifetime'] != -1 ? $boost_db['lifetime'] + BOOST_TIME : $expire;
// $lifetime = $boost_db['lifetime'];
// $push = $boost_db['push'];
$timer_average = ($boost_db['timer_average'] + $timer)/2;
}
//get data from settings table, if this page has not been put into the cache.
if (isset($boost_settings_db)) {
$expire = $boost_settings_db['lifetime'] != -1 ? $boost_settings_db['lifetime'] + BOOST_TIME : $expire;
$lifetime = $boost_settings_db['lifetime'];
$push = $boost_settings_db['push'];
}
boost_put_db($filename, $expire, $lifetime, $push, $router_item, $timer, $timer_average, $extension);
}
/**
* Puts boost info into database.
*
* @param $filename
* Name of cached file; hash of this is primary key in database
* @param $expire
* Expiration time
* @param $lifetime
* Default lifetime
* @param $push
* Pre-cache this file
* @param $router_item
* Array containing page_callback, page_type & page_id.
* @param $timer
* Time it took drupal to build this page.
* @param $timer_average
* Average time Drupal has spent building this page.
* @param $extension
* Filename extension: Used for content types.
* @param $url
* Optional: Full URL of cached page
* @param $file_path
* Optional: BOOST_FILE_PATH
*/
function boost_put_db($filename, $expire, $lifetime, $push, $router_item, $timer, $timer_average, $extension, $url = NULL, $file_path = NULL) {
global $base_root;
$url = is_null($url) ? $base_root . request_uri() : $url;
$file_path = is_null($file_path) ? BOOST_FILE_PATH : $file_path;
$hash = md5($filename);
if (variable_get('boost_store_url_percent_enc', FALSE)) {
$url = urldecode(rawurlencode($url));
}
$hash_url = md5($url);
db_query("UPDATE {boost_cache} SET expire = %d, lifetime = %d, push = %d, page_callback = '%s', page_type = '%s', timer = %d, timer_average = %d, base_dir = '%s', page_id = '%s', extension = '%s', url = '%s', filename = '%s', hash_url = '%s' WHERE hash = '%s'", $expire, $lifetime, $push, $router_item['page_callback'], $router_item['page_type'], $timer, $timer_average, $file_path, $router_item['page_id'], $extension, $url, $filename, $hash_url, $hash);
if (!db_affected_rows()) {
@db_query("INSERT INTO {boost_cache} (hash, hash_url, filename, expire, lifetime, push, page_callback, page_type, timer, timer_average, base_dir, page_id, extension, url) VALUES ('%s', '%s', '%s', %d, %d, %d, '%s', '%s', %d, %d, '%s', '%s', '%s', '%s')", $hash, $hash_url, $filename, $expire, $lifetime, $push, $router_item['page_callback'], $router_item['page_type'], $timer, $timer_average, $file_path, $router_item['page_id'], $extension, $url);
}
}
/**
* Removes info from database. Use on 404 or 403.
*
* @param array $files
* An array of files. Each file is a secondary array with keys for 'filename' and 'hash'.
* The hash is the primary key in the database. If omitted it will be recalculated from the filename.
*/
function boost_remove_db($files) {
$hashes = array();
foreach ($files as $file) {
if (empty($file['hash'])) {
$file['hash'] = md5($file['filename']);
}
$hashes[] = $file['hash'];
}
if ($hashes) {
boost_db_multi_delete_in('boost_cache', 'hash', "'%s'", $hashes);
}
}
/**
* Puts boost info into database.
*
* @param $expire
* Expiration time
* @param $lifetime
* Default lifetime
* @param $push
* Pre-cache this file
* @param $router_item
* Array containing page_callback, page_type & page_id.
*/
function boost_put_settings_db($lifetime, $push, $router_item, $scope) {
switch ($scope) {
case 0:
db_query("UPDATE {boost_cache_settings} SET lifetime = %d, push = %d WHERE page_callback = '%s' AND page_type = '%s' AND base_dir = '%s' AND page_id = '%s'", $lifetime, $push, $router_item['page_callback'], $router_item['page_type'], BOOST_FILE_PATH, $router_item['page_id']);
if (!db_affected_rows()) {
@db_query("INSERT INTO {boost_cache_settings} (lifetime, push, page_callback, page_type, base_dir, page_id) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $lifetime, $push, $router_item['page_callback'], $router_item['page_type'], BOOST_FILE_PATH, $router_item['page_id']);
}
break;
case 1:
db_query("UPDATE {boost_cache_settings} SET lifetime = %d, push = %d WHERE page_callback = '%s' AND page_type = '%s' AND base_dir = '%s' AND page_id = '0'", $lifetime, $push, $router_item['page_callback'], $router_item['page_type'], BOOST_FILE_PATH);
if (!db_affected_rows()) {
@db_query("INSERT INTO {boost_cache_settings} (lifetime, push, page_callback, page_type, base_dir, page_id) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $lifetime, $push, $router_item['page_callback'], $router_item['page_type'], BOOST_FILE_PATH, 0);
}
break;
case 2:
db_query("UPDATE {boost_cache_settings} SET lifetime = %d, push = %d WHERE page_callback = '%s' AND page_type = '0' AND base_dir = '%s' AND page_id = '0'", $lifetime, $push, $router_item['page_callback'], BOOST_FILE_PATH);
if (!db_affected_rows()) {
@db_query("INSERT INTO {boost_cache_settings} (lifetime, push, page_callback, page_type, base_dir, page_id) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $lifetime, $push, $router_item['page_callback'], '0', BOOST_FILE_PATH, 0);
}
break;
}
}
/**
* Removes info from boost database.
*
* @param $csid
* Cache Settings primary ID
*/
function boost_remove_settings_db($csid) {
db_query("DELETE FROM {boost_cache_settings} WHERE csid = %d", $csid);
}
/**
* Sets per page configuration.
*
* @param $lifetime
* Default lifetime
* @param $push
* Pre-cache this file
* @param $scope
* At what level does this effect cache expiration
*/
function boost_set_db_page_settings($lifetime, $push, $scope) {
$router_item = _boost_get_menu_router();
$filename = boost_file_path($GLOBALS['_boost_path']);
$info = boost_get_db($filename);
if (!$info) {
$info['expire'] = 0;
}
elseif ($lifetime == -1) {
$info['expire'] = $info['expire'] - $info['lifetime'] + BOOST_CACHE_LIFETIME;
}
elseif ($info['lifetime'] == -1) {
$info['expire'] = $info['expire'] - BOOST_CACHE_LIFETIME + $lifetime;
}
elseif ($info['lifetime'] != $lifetime) {
$info['expire'] = $info['expire'] - $info['lifetime'] + $lifetime;
}
// Clear old files so they acquire the new settings.
$data = array();
switch ($scope) {
case 0:
$data[] = array('base_dir' => BOOST_FILE_PATH, 'page_callback' => $router_item['page_callback'], 'page_type' => $router_item['page_type'], 'page_id' => $router_item['page_id']);
break;
case 1:
$data[] = array('base_dir' => BOOST_FILE_PATH, 'page_callback' => $router_item['page_callback'], 'page_type' => $router_item['page_type']);
break;
case 2:
$data[] = array('base_dir' => BOOST_FILE_PATH, 'page_callback' => $router_item['page_callback']);
break;
}
boost_put_settings_db($lifetime, $push, $router_item, $scope);
$count = 0;
if ($data) {
$count += boost_cache_expire_router($data);
}
return $count;
}
/**
* Gets boost info from cache database.
*
* @param $filename
* Filename to be looked up in the database
*/
function boost_get_db($filename) {
$hash = md5($filename);
return db_fetch_array(db_query("SELECT * FROM {boost_cache} WHERE hash = '%s'", $hash));
}
/**
* Gets boost settings from cache settings database.
*
* @param $router_item
* Array containing page_callback, page_type & page_id.
*/
function boost_get_settings_db($router_item) {
$settings = array();
// Get a more exact match first
if (BOOST_FLUSH_ALL_MULTISITE) {
$settings[] = db_fetch_array(db_query_range("SELECT * FROM {boost_cache_settings} WHERE page_callback = '%s' AND page_type = '%s' AND page_id = '%s'", $router_item['page_callback'], $router_item['page_type'], $router_item['page_id'], 0, 1));
// Get for the content type
$settings[] = db_fetch_array(db_query_range("SELECT * FROM {boost_cache_settings} WHERE page_callback = '%s' AND page_type = '%s' AND page_id = '%s'", $router_item['page_callback'], $router_item['page_type'], 0, 0, 1));
// Finally get the content container (node, view, term, ect...)
$settings[] = db_fetch_array(db_query_range("SELECT * FROM {boost_cache_settings} WHERE page_callback = '%s' AND page_type = '%s' AND page_id = '%s'", $router_item['page_callback'], 0, 0, 0, 1));
}
else {
$settings[] = db_fetch_array(db_query_range("SELECT * FROM {boost_cache_settings} WHERE page_callback = '%s' AND page_type = '%s' AND base_dir = '%s' AND page_id = '%s'", $router_item['page_callback'], $router_item['page_type'], BOOST_FILE_PATH, $router_item['page_id'], 0, 1));
// Get for the content type
$settings[] = db_fetch_array(db_query_range("SELECT * FROM {boost_cache_settings} WHERE page_callback = '%s' AND page_type = '%s' AND base_dir = '%s' AND page_id = '%s'", $router_item['page_callback'], $router_item['page_type'], BOOST_FILE_PATH, 0, 0, 1));
// Finally get the content container (node, view, term, ect...)
$settings[] = db_fetch_array(db_query_range("SELECT * FROM {boost_cache_settings} WHERE page_callback = '%s' AND page_type = '%s' AND base_dir = '%s' AND page_id = '%s'", $router_item['page_callback'], 0, BOOST_FILE_PATH, 0, 0, 1));
}
return $settings;
}
/**
* Checks various timestamps in the database.
*
* @param $set_max
* bool Allow one to read and not set the max_timestamp.
*
* @return bool
* Returns TRUE if the site has changed since the last time this function was called.
*/
function boost_has_site_changed($set_max = FALSE) {
// Make sure database has been indexed.
if (boost_drupal_get_installed_schema_version('boost') <= 6120) {
return FALSE;
}
// Index any new tables
$ret = array();
_boost_index_exists($ret, 'node_revisions', 'timestamp');
_boost_index_exists($ret, 'files', 'timestamp');
_boost_index_exists($ret, 'comments', 'timestamp');
_boost_index_exists($ret, 'node', 'changed');
_boost_index_exists($ret, 'node_comment_statistics', 'last_comment_timestamp');
_boost_index_exists($ret, 'votingapi_vote', 'timestamp');
// Get timestamps from the database
$node_revisions = boost_get_time('node_revisions', 'timestamp');
//$history = boost_get_time('history', 'timestamp');
$files = boost_get_time('files', 'timestamp');
$comments = boost_get_time('comments', 'timestamp');
$voteapi_vote = boost_get_time('votingapi_vote', 'timestamp');
$node = boost_get_time('node', 'changed');
$last_comment_timestamp = boost_get_time('node_comment_statistics', 'last_comment_timestamp');
$max = max($node_revisions, $files, $comments, $node, $last_comment_timestamp, $voteapi_vote);
if ($max != variable_get('boost_max_timestamp', BOOST_TIME)) {
if ($set_max) {
variable_set('boost_max_timestamp', (int)$max);
}
return TRUE;
}
else {
return FALSE;
}
}
/**
* Returns the currently installed schema version for a module.
*
* @see drupal_get_installed_schema_version()
*
* @param $module
* A module name.
* @param $reset
* Set to TRUE after modifying the system table.
* @param $array
* Set to TRUE if you want to get information about all modules in the
* system.
* @return
* The currently installed schema version.
*/
function boost_drupal_get_installed_schema_version($module, $reset = FALSE, $array = FALSE) {
static $versions = array();
if ($reset) {
$versions = array();
}
if (!$versions) {
$versions = array();
$result = db_query("SELECT name, schema_version FROM {system} WHERE type = '%s'", 'module');
while ($row = db_fetch_object($result)) {
$versions[$row->name] = $row->schema_version;
}
}
return $array ? $versions : $versions[$module];
}
/**
* Checks various timestamps in the database.
*
* @param $table
* Database table name
* @param $column
* Column containing the time stamp
* @return int
* Returns largest time in the table.
*/
function boost_get_time($table, $column) {
if (db_table_exists($table)) {
return (int)db_result(db_query_range("SELECT %s FROM {%s} ORDER BY %s DESC", $column, $table, $column, 0, 1));
}
else {
return 0;
}
}
/**
* Writes data to filename in an atomic operation thats compatible with older
* versions of php (php < 5.2.4 file_put_contents() doesn't lock correctly).
*
* @param $filename
* Name of file to be written
* @param $buffer
* Contents of file
*/
function boost_cache_write($filename, $buffer) {
$filenames = boost_get_all_filenames($filename);
foreach ($filenames as $key => $values) {
if ($key == 'gzip') {
$data = gzencode($buffer, 9);
}
else {
$data = $buffer;
}
foreach ($values as $filename) {
if (!_boost_mkdir_p(dirname($filename))) {
if (BOOST_VERBOSE >= 3) {
watchdog('boost', 'Unable to create directory: %dir Group ID: %gid User ID: %uid Current script owner: %user ', array('%dir' => dirname($filename), '%gid' => getmygid(), '%uid' => getmyuid(), '%user' => get_current_user()), WATCHDOG_WARNING);
}
}
$tempfile = $filename . getmypid();
$oldfile = $tempfile . 'old';
if (@file_put_contents($tempfile, $data) === FALSE) {
if (BOOST_VERBOSE >= 3) {
watchdog('boost', 'Unable to write temp file: %file Group ID: %gid User ID: %uid Current script owner: %user ', array('%file' => $tempfile, '%gid' => getmygid(), '%uid' => getmyuid(), '%user' => get_current_user()), WATCHDOG_WARNING);
}
return FALSE;
}
else {
if (is_numeric(BOOST_PERMISSIONS_FILE)) {
@chmod($tempfile, octdec(BOOST_PERMISSIONS_FILE));
}
// Erase old file
if (BOOST_OVERWRITE_FILE) {
// Keep old file around just in case rename fails
@rename($filename, $oldfile);
}
// Put temp file in its final location
if (@rename($tempfile, $filename) === FALSE) {
// If rename failed then remove new file and put old file back
@unlink($tempfile);
@rename($oldfile, $filename);
if (BOOST_VERBOSE >= 5) {
watchdog('boost', 'Unable to rename file: %temp to %file Group ID: %gid User ID: %uid Current script owner: %user ', array('%temp' => $tempfile, '%file' => $filename, '%gid' => getmygid(), '%uid' => getmyuid(), '%user' => get_current_user()), WATCHDOG_WARNING);
}
return FALSE;
}
elseif (BOOST_OVERWRITE_FILE) {
// Rename is sucessful; remove old file
@unlink($oldfile);
}
}
}
}
return TRUE;
}
/**
* Returns the full directory path to the static file cache directory.
*
* @param $host
* Host name. Example: example.com
* @param $absolute
* Give path from system root if true. If false give path from web root.
* @param $root_dir
* Cache directory
* @param $normal_dir
* Normal directory
*/
function boost_cache_directory($host = NULL, $absolute = TRUE, $root_dir = NULL, $normal_dir = NULL) {
global $base_url;
$temp_base_url = $base_url;
$root_dir = is_null($root_dir) ? BOOST_ROOT_CACHE_DIR : $root_dir;
$normal_dir = is_null($normal_dir) ? BOOST_NORMAL_DIR : $normal_dir;
if ($temp_base_url == "http://") {
if (!BOOST_MULTISITE_SINGLE_DB) {
$temp_base_url = $temp_base_url . str_replace($root_dir . '/', '', variable_get('boost_file_path', boost_cache_directory(NULL, FALSE)));
}
elseif (BOOST_NORMAL_DIR != '' && db_result(db_query("SELECT count(DISTINCT base_dir) FROM {boost_cache}")) == 1) {
$temp_base_url = db_result(db_query("SELECT DISTINCT base_dir FROM {boost_cache}"));
$temp_base_url = $temp_base_url . str_replace($root_dir . '/', '', $base_dir);
$temp_base_url = $temp_base_url . str_replace($normal_dir . '/', '', $base_dir);
}
}
if (@parse_url($temp_base_url) === FALSE) {
//Error has been caught here
if (BOOST_VERBOSE >= 1) {
watchdog('boost', 'base_url is not set in your settings.php file. Please read Important Notes in boosts README.txt file.', array(), WATCHDOG_NOTICE);
}
return FALSE;
}
$parts = parse_url($temp_base_url);
$host = !empty($host) ? $host : $parts['host'];
$host = strtolower($host);
$parts['path'] = isset($parts['path']) ? $parts['path'] : '/';
$subdir = implode('/', array_filter(explode('/', (!empty($base_path)) ? $base_path : $parts['path'])));
return implode('/', !$absolute ? array_filter(array($root_dir, $normal_dir, $host, $subdir)) : array_filter(array(getcwd(), $root_dir, $normal_dir, $host, $subdir)));
}
/**
* Returns the static file path for a Drupal page.
*
* @param $path
* path to convert to boost's file naming convention
* @param $query
* add query to path
* @param $extension
* add extension to end of filename
* @param $file_path
* Optional: BOOST_FILE_PATH
*
* $path = $GLOBALS['_boost_path'] most of the time
*/
function boost_file_path($path, $query = TRUE, $extension = BOOST_FILE_EXTENSION, $file_path = NULL) {
//handling of url variables
if ($GLOBALS['_boost_query'] != BOOST_CHAR) {
if ($query) {
$path .= $GLOBALS['_boost_query'];
}
else {
$path .= BOOST_CHAR;
}
}
else {
$path .= $GLOBALS['_boost_query'];
}
// Under no circumstances should the incoming path contain '..' or null
// bytes; we also limit the maximum directory nesting depth of the path
if ( strpos($path, '..') !== FALSE
|| strpos($path, "\0") !== FALSE
|| count(explode('/', $path)) > BOOST_MAX_PATH_DEPTH
) {
return FALSE;
}
$file_path = is_null($file_path) ? BOOST_FILE_PATH : $file_path;
return implode('/', array($file_path, $path . (is_null($extension) ? '' : $extension)));
}
/**
* Returns the time it took to generate this cached page.
* @param $filename
* Name of cached file
*/
function boost_get_generation_time($filename) {
$boost_db = boost_get_db($filename);
return $boost_db['timer'] != 0 ? $boost_db['timer']/1000.0 : FALSE;
}
/**
* Returns the age of a cached file, measured in seconds since it was last
* updated.
* @param $filename
* Name of cached file
*/
function boost_file_get_age($filename) {
return BOOST_TIME - filemtime($filename);
}
function boost_db_get_age($filename) {
$boost_db = boost_get_db($filename);
return $boost_db['expire'] != 0 ? $boost_db['expire'] : FALSE;
}
/**
* Returns the remaining time-to-live for a cached file, measured in
* seconds.
* @param $filename
* Name of cached file
*/
function boost_file_get_ttl($filename) {
return BOOST_CACHE_LIFETIME - boost_file_get_age($filename);
}
function boost_db_get_ttl($filename) {
return boost_db_get_age($filename) - BOOST_TIME;
}
function boost_db_get_cache_age($filename) {
$boost_db = boost_get_db($filename);
$lifetime = BOOST_CACHE_LIFETIME;
if ($boost_db['lifetime'] != -1) {
$lifetime = $boost_db['lifetime'];
}
$time = BOOST_TIME - ($boost_db['expire'] - $lifetime);
return $time;
}
/**
* Determines whether a cached file has expired, i.e. whether its age
* exceeds the maximum cache lifetime as defined by Drupal's system
* settings.
* @param $filename
* Name of cached file
*/
function boost_file_is_expired($filename) {
return boost_file_get_age($filename) > BOOST_CACHE_LIFETIME;
}
function boost_db_is_expired($filename) {
return boost_db_get_age($filename) < BOOST_TIME;
}
/**
* Sets a special cookie preventing authenticated users getting served pages
* from the static page cache.
*
* @param $uid
* User ID Number
* @param $expires
* Expiration time
*/
function boost_set_cookie($uid, $expires = NULL) {
if (!$expires) {
// Let the old way still work, in case user object was passed
$uid = is_object($uid) ? $uid->uid : $uid;
$expires = ini_get('session.cookie_lifetime');
$expires = (!empty($expires) && is_numeric($expires)) ? BOOST_TIME + (int)$expires : 0;
setcookie(BOOST_COOKIE, strval($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');
}
$GLOBALS['_boost_cache_this'] = FALSE;
}
/**
* Retrieve a specific URL redirect from the database.
* http://drupal.org/node/451790
*
* @param $where
* Array containing 'redirect' => $path
*/
function boost_path_redirect_load($where = array(), $args = array(), $sort = array()) {
$redirects = array();
if (is_numeric($where)) {
$where = array('rid' => $where);
}
foreach ($where as $key => $value) {
if (is_string($key)) {
$args[] = $value;
$where[$key] = $key .' = '. (is_numeric($value) ? '%d' : "'%s'");
}
}
if ($where && $args) {
$sql = "SELECT * FROM {path_redirect} WHERE ". implode(' AND ', $where);
if ($sort) {
$sql .= ' ORDER BY '. implode(' ,', $sort);
}
$result = db_query($sql, $args);
while ($redirect = db_fetch_array($result)) {
$redirects[] = $redirect;
}
return $redirects;
}
}
/**
* Cache css and or js files.
*
* Parse the html file so we get all css/js files. drupal_get_js/css isn't 100%.
*
* @param $buffer
* String containing documents html.
*/
function boost_cache_css_js_files($buffer) {
if (BOOST_CACHE_CSS) {
// Extract external css files from html document
$css_files = explode(' $value) {
// Extract css filename
list($temp) = explode('" />', $value);
$temp = explode('href="', $temp);
$temp = explode('//', array_pop($temp));
$temp = explode(base_path(), array_pop($temp));
array_shift($temp);
list($temp) = explode('?', implode('/', $temp));
list($css_files[$key]) = explode('"', $temp);
}
_boost_copy_css_files($css_files);
}
if (BOOST_CACHE_JS) {
$js_files = explode('