Skip to content
Snippets Groups Projects
Commit e01a0dfd authored by Mike Carper's avatar Mike Carper
Browse files

#325813: Bring in htaccess generation.

parent 5a9b1d0e
Branches
Tags
No related merge requests found
<?php
// $Id$
/**
* @file
* Admin page callbacks for the boost module.
*/
/**
* Form builder; Configure boost settings.
*
* @ingroup forms
* @see system_settings_form()
*/
function boost_admin_debug_settings() {
$form['boost_message_debug'] = array(
'#type' => 'checkbox',
'#title' => t('Send debug info for each request to watchdog.'),
'#default_value' => variable_get('boost_message_debug', BOOST_MESSAGE_DEBUG),
'#description' => t('Only use for debugging purposes as this can fill up watchdog fairly quickly.'),
);
return system_settings_form($form);
}
<?php
// $Id$
/**
* @file
* Admin page callbacks for the boost module.
*/
/**
* Form builder; Configure boost settings.
*
* @ingroup forms
* @see system_settings_form()
*/
function boost_admin_filesystem_settings() {
$form['boost_root_cache_dir'] = array(
'#type' => 'textfield',
'#title' => t('Root cache directory'),
'#default_value' => variable_get('boost_root_cache_dir', BOOST_ROOT_CACHE_DIR),
);
$form['boost_normal_dir'] = array(
'#type' => 'textfield',
'#title' => t('Normal cache directory'),
'#default_value' => variable_get('boost_normal_dir', BOOST_NORMAL_DIR),
);
$form['boost_char'] = array(
'#type' => 'textfield',
'#title' => t('Character replacement for "?" in the URL'),
'#default_value' => variable_get('boost_char', BOOST_CHAR),
);
$form['boost_permissions_file'] = array(
'#type' => 'textfield',
'#title' => t('Files: Enter in a 4 digit number (octal) that will be used by !link. Example 0664', array('!link' => l(t('chmod()'), 'http://php.net/chmod'))),
'#default_value' => variable_get('boost_permissions_file', BOOST_PERMISSIONS_FILE),
'#description' => t('Sometimes because of funky servers you need it use a different file mode then the default.'),
);
$form['boost_permissions_dir'] = array(
'#type' => 'textfield',
'#title' => t('Directories: Enter in a 4 digit number (octal) that will be used by !link. Example 0775', array('!link' => l(t('chmod()'), 'http://php.net/chmod'))),
'#default_value' => variable_get('boost_permissions_dir', BOOST_PERMISSIONS_DIR),
'#description' => t('Sometimes because of funky servers you need it use a different file mode then the default.'),
);
return system_settings_form($form);
}
<?php
// $Id$
/**
* @file
* Admin page callbacks for the boost module.
*/
/**
* Default apache server name.
*/
define('BOOST_SERVER_NAME_HTTP_HOST', '%{HTTP_HOST}');
/**
* Default apache document root.
*/
define('BOOST_DOCUMENT_ROOT', '%{DOCUMENT_ROOT}');
/**
* Default etag settings.
*/
define('BOOST_APACHE_ETAG', 3);
/**
* Default header setttings
*/
define('BOOST_APACHE_XHEADER', 1);
/**
* Default setting for SSL pages
*/
define('BOOST_SSL_BYPASS', TRUE);
/**
* Default setting for forcing all content to be utf8
*/
define('BOOST_FORCE_UTF8', TRUE);
/**
* Form builder; Configure boost settings.
*
* @ingroup forms
* @see system_settings_form()
*/
function boost_admin_htaccess_settings() {
global $base_path;
// Apache .htaccess settings generation
// $htaccess = boost_admin_generate_htaccess();
$form['htaccess'] = array(
'#type' => 'fieldset',
'#title' => t('Boost Apache .htaccess settings generation'),
'#description' => t('<a href="!link">Explanation of .htaccess variables</a> <br /><br /> <strong>Be sure to save the configuration and then go to the <a href="!rules">htaccess rules generation page</a> and copy the rules.</strong>', array('!link' => url('http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html'), '!rules' => url('admin/config/development/performance/boost/htaccess-generator'))),
);
$form['htaccess']['boost_server_name_http_host'] = array(
'#type' => 'radios',
'#title' => t('Servers URL or Name'),
'#default_value' => variable_get('boost_server_name_http_host', BOOST_SERVER_NAME_HTTP_HOST),
'#options' => array(
'%{HTTP_HOST}' => '%{HTTP_HOST}',
'%{SERVER_NAME}' => '%{SERVER_NAME}',
$_SERVER['HTTP_HOST'] => $_SERVER['HTTP_HOST'],
$_SERVER['SERVER_NAME'] => $_SERVER['SERVER_NAME'],
),
'#description' => t('Best to leave these as %{}, only try the last option(s) if boost is still not working.'),
);
// Set DOCUMENT_ROOT
$drupal_subdir = rtrim($base_path, '/');
$document_root = str_replace("\\", '/', getcwd()); // fix windows dir slashes
$document_root = trim(str_replace($drupal_subdir, '', $document_root)); // remove subdir
$options = array('%{DOCUMENT_ROOT}' => '%{DOCUMENT_ROOT}', $document_root => $document_root); // initial options
$rejects = array('SCRIPT_FILENAME', 'DOCUMENT_ROOT'); // values to ignore
$output = boost_admin_htaccess_array_find($document_root, $_SERVER, $rejects); //search for values that match getcwd
$description_extra = '';
if (!empty($output)) {
foreach ($output as $key => $value) {
$temp = '%{ENV:' . $key . '}';
$options[$temp] = $temp . ' = ' . $value; // adding values to options
if (strcmp($value, $document_root) == 0) {
$best = $temp; // set best since it's a match
}
}
}
if (strcmp($_SERVER['DOCUMENT_ROOT'], $document_root) == 0) {
$best = '%{DOCUMENT_ROOT}';
}
elseif (!isset($best)) {
$best = $document_root;
$description_extra = t('Please <a href="!link">open an boost issue on Drupal.org</a>, since apache and php might not be configured correctly.', array('!link' => url('http://drupal.org/node/add/project-issue/boost')));
}
$percent = 0;
$int = similar_text(substr(trim($_SERVER['DOCUMENT_ROOT']), 18, 1), substr(trim($document_root), 18, 1), $percent);
$description = t('Value of %best is recommended for this server.', array('%best' => $best)) . ' ' . $description_extra;
$form['htaccess']['boost_document_root'] = array(
'#type' => 'radios',
'#title' => t('Document Root'),
'#default_value' => variable_get('boost_document_root', BOOST_DOCUMENT_ROOT),
'#options' => $options,
'#description' => $description,
);
$form['htaccess']['boost_apache_etag'] = array(
'#type' => 'radios',
'#title' => t('ETag Settings'),
'#default_value' => variable_get('boost_apache_etag', BOOST_APACHE_ETAG),
'#options' => array(
3 => "Set FileETag 'MTime Size' - Useful in server clusters (Highly Recommended)",
2 => "Set FileETag 'All' - Default if enabled",
1 => "Set FileETag 'None' - Do not send an etag",
0 => 'Do Nothing',
),
'#description' => t('Uses <a href="!link">FileETag Directive</a> to set <a href="!about">ETags</a> for the files cached by Boost. <a href="!stack">More info on this subject</a>', array('!link' => url('http://httpd.apache.org/docs/trunk/mod/core.html#fileetag'), '!about' => url('http://en.wikipedia.org/wiki/HTTP_ETag'), '!stack' => url('http://stackoverflow.com/questions/tagged?tagnames=etag&sort=votes&pagesize=50'))),
);
$form['htaccess']['boost_apache_xheader'] = array(
'#type' => 'radios',
'#title' => t('Boost Tags'),
'#default_value' => variable_get('boost_apache_xheader', BOOST_APACHE_XHEADER),
'#options' => array(
1 => 'Set Boost header',
0 => 'Do not set Boost header',
),
'#description' => t('In order to identify that the page is being served from the cache, Boost can send out a header that will identify any files served from the boost cache.'),
);
$form['htaccess']['boost_ssl_bypass'] = array(
'#type' => 'checkbox',
'#title' => t('Bypass the boost cache for ssl requests.'),
'#default_value' => variable_get('boost_ssl_bypass', BOOST_SSL_BYPASS),
'#description' => t('Ticking this is recommended if you use the securepages module.'),
);
$enabled_file_extensions = array();
$types = boost_get_storage_types();
foreach ($types as $title => $content_types) {
foreach ($content_types as $type => $values) {
if ($values['enabled']) {
$enabled_file_extensions[$values['extension']]['gzip'] = $values['gzip'];
if (empty($enabled_file_extensions[$values['extension']]['content_type'])) {
$enabled_file_extensions[$values['extension']]['content_type'] = $type;
}
}
}
}
$form['htaccess']['boost_force_utf8'] = array(
'#type' => 'checkbox',
'#title' => t('Add "AddDefaultCharset utf-8" to the htaccess rules'),
'#default_value' => variable_get('boost_force_utf8', BOOST_FORCE_UTF8),
'#description' => t('Depending on your i18n settings you might want this disabled or enabled.') . boost_print_r($enabled_file_extensions),
);
// $form['htaccess']['boost_ignore_htaccess_warning'] = array(
// '#type' => 'checkbox',
// '#title' => t('Ignore .htaccess warning'),
// '#default_value' => variable_get('boost_ignore_htaccess_warning', FALSE),
// '#description' => t('Do not warn about missing or modified boost rules in the .htaccess file on the <a href="!link">status report page</a>. Enable this if you have a good reason to modify the boost rules in .htaccess.', array('!link' => url('admin/reports/status'))),
// );
return system_settings_form($form);
}
/**
* Form builder; Configure boost settings.
*
* @ingroup forms
* @see system_settings_form()
*/
function boost_admin_htaccess_generation() {
// Generated .htaccess output
$htaccess = boost_admin_htaccess_generate_htaccess();
$form['boost_generated'] = array(
'#type' => 'textarea',
'#title' => t('Generated Rules'),
'#default_value' => $htaccess,
'#rows' => count(explode("\n", $htaccess))+1,
'#wysiwyg' => FALSE,
'#description' => t("Copy this into your .htaccess file below <pre><tt> # If your site is running in a VirtualDocumentRoot at http://example.com/,
# uncomment the following line:
# RewriteBase / </tt></pre> and above <pre><tt> # Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().</tt></pre><br />Note that the generated rules' settings can be configure at !link.", array('!link' => l('admin/config/development/performance/boost/htaccess-settings', 'admin/config/development/performance/boost/htaccess-settings'))),
);
// $form['#submit'][] = 'boost_admin_htaccess_page_submit';
return $form;
}
/**
* Generate htaccess code.
*
* http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html
*
* @return string
* htaccess code
*/
function boost_admin_htaccess_generate_htaccess() {
global $base_path;
$server_name = variable_get('boost_server_name_http_host', BOOST_SERVER_NAME_HTTP_HOST);
$document_root = variable_get('boost_document_root', BOOST_DOCUMENT_ROOT);
$drupal_subdir = rtrim($base_path, '/');
// Various dir's
$cache_dir = variable_get('boost_root_cache_dir', BOOST_ROOT_CACHE_DIR);
$normal_dir = variable_get('boost_normal_dir', BOOST_NORMAL_DIR);
$char = variable_get('boost_char', BOOST_CHAR);
// Go through every storage type getting the extesion and if it supports gzip.
$enabled_file_extensions = array();
$types = boost_get_storage_types();
foreach ($types as $title => $content_types) {
foreach ($content_types as $type => $values) {
if ($values['enabled']) {
$enabled_file_extensions[$values['extension']]['gzip'] = $values['gzip'];
if (empty($enabled_file_extensions[$values['extension']]['content_type'])) {
$enabled_file_extensions[$values['extension']]['content_type'] = $type;
}
}
}
}
$output = array('gzip' => '', 'normal' => '');
$gzip_count = 0;
$normal_count = 0;
foreach ($enabled_file_extensions as $extension => $values) {
$type = $values['content_type'];
if ($values['gzip']) {
$output['gzip'] .= " RewriteCond $document_root$base_path$cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension -s\n";
$output['gzip'] .= " RewriteRule .* $cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension [L,T=$type,E=no-gzip:1]\n";
$gzip_count++;
}
$output['normal'] .= " RewriteCond $document_root$base_path$cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension -s\n";
$output['normal'] .= " RewriteRule .* $cache_dir/%{ENV:boostpath}/$server_name%{REQUEST_URI}$char%{QUERY_STRING}\.$extension [L,T=$type]\n";
$normal_count++;
}
$skip = !empty($gzip_count) ? $normal_count + $gzip_count + 1 : $normal_count;
// Generate the rules
$string = " ### BOOST START ###\n";
if (!empty($output)) {
$string .= "\n";
$string .= " # Allow for alt paths to be set via htaccess rules; allows for cached variants (future mobile support)\n";
$string .= " RewriteRule .* - [E=boostpath:$normal_dir]\n";
$string .= "\n";
$string .= " # Caching for anonymous users\n";
$string .= " # Skip boost IF not get request OR uri has wrong dir OR cookie is set OR request came from this server" . (variable_get('boost_ssl_bypass', BOOST_SSL_BYPASS) ? " OR https request" : "") . "\n";
$string .= " RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [OR]\n";
$string .= " RewriteCond %{REQUEST_URI} (^$base_path(admin|$cache_dir|misc|modules|sites|system|openid|themes|node/add|comment/reply))|(/(edit|user|user/(login|password|register))$) [OR]\n";
if (variable_get('boost_ssl_bypass', BOOST_SSL_BYPASS)) {
$string .= " RewriteCond %{HTTPS} on [OR]\n";
}
$string .= " RewriteCond %{HTTP_COOKIE} " . variable_get('boost_cookie', BOOST_COOKIE) . "\n";
$string .= " RewriteRule .* - [S=$skip]\n";
$string .= "\n";
$string .= " # GZIP\n";
$string .= " RewriteCond %{HTTP:Accept-encoding} !gzip\n";
$string .= " RewriteRule .* - [S=$gzip_count]\n";
$string .= $output['gzip'];
$string .= "\n";
$string .= " # NORMAL\n";
$string .= $output['normal'];
}
$string .= "\n";
$string .= " ### BOOST END ###\n";
return $string;
}
/**
* Returns all key/values in array that are equal.
*
* @param $needle
* What your searching for
* @param $haystack
* Array of values
* @param $a_not
* Optional array of key names to exclude
*/
function boost_admin_htaccess_array_find($needle, $haystack, $a_not = array()) {
$out = array();
foreach ($haystack as $key => $value) {
if (is_string($value) && strstr($value, $needle) !== FALSE) {
$good = TRUE;
foreach ($a_not as $not) {
if (strpos($key, $not) !== FALSE) {
$good = FALSE;
}
}
if ($good) {
$out[$key] = $value;
}
}
}
return $out;
}
\ No newline at end of file
; $Id$
name = Boost
description = Caches text as static files
description = Caches generated output as a static file to be served directly from the webserver.
package = Performance and scalability
core = 7.x
files[] = boost.module
......
......@@ -46,4 +46,41 @@ function boost_uninstall() {
* Implements hook_requirements().
*/
function boost_requirements($phase) {
$requirements = array();
$t = get_t();
// Check the server's ability to use boost
if ($phase == 'runtime') {
$cache_directories = array(
boost_get_normal_cache_dir(),
);
foreach ($cache_directories as $cache_directory) {
if (boost_mkdir($cache_directory)) {
// $root_file = file_put_contents($cache_directory . '/' . variable_get('boost_root_file', '.boost'), $cache_directory);
}
if (!is_dir($cache_directory)) {
$requirements['boost_default'] = array(
'title' => $t('Boost'),
'description' => $t('!cache_dir: does not exist.', array('!cache_dir' => $cache_directory)),
'severity' => REQUIREMENT_ERROR,
'value' => $t('Cache path'),
);
}
if (is_dir($cache_directory) && (!is_writable($cache_directory))) {
$requirements['boost_permissions'] = array(
'title' => $t('Boost'),
'description' => $t('Directory %dir credentials - Permissions: %fp. Owner %fo. Group %fg.<br /> Your credentials - Group ID: %gid. User ID: %uid. Current script owner: %user.', array('%dir' => getcwd() . '/' . $cache_directory, '%gid' => getmygid(), '%uid' => getmyuid(), '%user' => get_current_user(), '%fp' => substr(sprintf('%o', fileperms($cache_directory)), -4), '%fo' => fileowner($cache_directory), '%fg' => filegroup($cache_directory) )),
'severity' => REQUIREMENT_ERROR,
'value' => $t('Can not write to file-system'),
);
}
}
if (empty($requirements)) {
$requirements['boost'] = array(
'title' => $t('Boost'),
'severity' => REQUIREMENT_OK,
'value' => $t('Boost installed correctly, should be working if properly <a href="@settings">configured</a>.', array('@settings' => url('admin/config/development/performance/boost'))),
);
}
}
return $requirements;
}
......@@ -3,12 +3,50 @@
/**
* @file
* Caches generated text as static files.
* Caches generated output as a static file to be served directly from the
* webserver.
*/
/**
* Default cacheablily setting.
*/
define('BOOST_CACHEABILITY_PAGES', '');
/**
* Default cookie name.
*/
define('BOOST_COOKIE', 'DRUPAL_UID');
/**
* Default value for sending out debugging info via a message.
*/
define('BOOST_MESSAGE_DEBUG', FALSE);
/**
* Default value for the root cache dir.
*/
define('BOOST_ROOT_CACHE_DIR', 'cache');
/**
* Default value for the normal cache dir.
*/
define('BOOST_NORMAL_DIR', 'normal');
/**
* Default value for the character replacement of ? in the URL.
*/
define('BOOST_CHAR', '_');
/**
* Default value for file permissions created by boost.
*/
define('BOOST_PERMISSIONS_FILE', '0664');
/**
* Default value for directory permissions created by boost.
*/
define('BOOST_PERMISSIONS_DIR', '0775');
/**
* Implements hook_menu().
*/
......@@ -18,6 +56,10 @@ function boost_menu() {
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/development/performance/boost/default'] = array(
'title' => 'Boost Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/development/performance/boost'] = array(
'title' => 'Boost Settings',
'description' => 'Configuration for Boost.',
......@@ -26,54 +68,80 @@ function boost_menu() {
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'boost.admin.inc',
'weight' => 0,
);
$items['admin/config/development/performance/boost/debug'] = array(
'title' => 'Boost Debug Settings',
'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',
);
$items['admin/config/development/performance/boost/filesystem'] = array(
'title' => 'Boost File System Settings',
'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',
);
$items['admin/config/development/performance/boost/htaccess-settings'] = array(
'title' => 'Boost .htaccess Settings',
'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',
);
$items['admin/config/development/performance/boost/htaccess-generator'] = array(
'title' => 'Boost .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',
);
return $items;
}
/**
* Implements hook_init(). Performs page setup tasks if page not cached.
* Implements hook_init().
*
* Performs page setup tasks.
*/
function boost_init() {
global $user, $_boost;
// Check if Drupal is started from index.php - could cause problems with other
// contrib modules like ad module.
$uid = isset($user->uid) ? $user->uid : 0;
if (strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') !== FALSE) {
// Remove Boost cookie at logout if it still exists.
if (isset($_COOKIE[BOOST_COOKIE]) && $uid == 0) {
boost_set_cookie($uid, REQUEST_TIME - 86400);
}
// Remove Boost cookie if set to -1.
elseif (isset($_COOKIE[BOOST_COOKIE]) && $_COOKIE[BOOST_COOKIE] == '-1') {
boost_set_cookie($uid, REQUEST_TIME - 86400);
}
// Set Boost cookie if it doesn't exists and user is logged in.
elseif (!isset($_COOKIE[BOOST_COOKIE]) && $uid != 0) {
boost_set_cookie($uid);
}
}
global $_boost;
$_boost = boost_transform_url();
// Make sure the page is/should be cached according to our current configuration.
if ( strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') === FALSE
|| variable_get('site_offline', 0)
|| ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != 'HEAD')
|| $_SERVER['SERVER_SOFTWARE'] === 'PHP CLI'
|| isset($_GET['nocache'])
|| $uid != 0
|| $_boost['menu_item']['status'] != 200
|| variable_get('site_offline', 0)
) {
$_boost['cache_this'] = FALSE;
}
// Give modules a chance to alter the cookie handler callback used.
// hook_boost_cookie_handler_callback_alter
$cookie_handler_callback = 'boost_cookie_handler';
drupal_alter('boost_cookie_handler_callback', $cookie_handler_callback);
if (function_exists($cookie_handler_callback)) {
$cookie_handler_callback();
}
}
/**
* Implements boost_exit().
*/
function boost_exit() {
function boost_exit($destination = NULL) {
global $_boost;
// Bail out of caching.
if (!isset($_boost['cache_this'])) {
......@@ -93,33 +161,36 @@ function boost_exit() {
elseif ($_boost['menu_item']['status'] != 200) {
return;
}
elseif (!drupal_page_is_cacheable()) {
$_boost['is_cacheable'] = FALSE;
return;
}
// Get the important data.
// Get the data to cache.
$data = ob_get_contents();
$header_info = boost_get_header_info();
// Get cache info.
$info = boost_match_header_attributes($header_info);
if ($info['enabled'] === FALSE) {
// Get header info.
$_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;
}
// Attach extension to filename.
$_boost['filename'] .= '.' . $info['extension'];
// Add note to bottom of content.
if ($info['commment_start'] && $info['commment_end']) {
$expire = $info['lifetime_max'];
// Add note to bottom of content if possible.
if ($_boost['matched_header_info']['commment_start'] && $_boost['matched_header_info']['commment_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" . $info['commment_start'] . 'Page cached by Boost @ ' . $cached_at . ', expires @ ' . $expires_at . ', lifetime ' . format_interval($expire) . $info['commment_end'];
$note = "\n" . $_boost['matched_header_info']['commment_start'] . 'Page cached by Boost @ ' . $cached_at . ', expires @ ' . $expires_at . ', lifetime ' . format_interval($expire) . $_boost['matched_header_info']['commment_end'];
$data .= $note;
}
// Write info to a file.
if ($_boost['filename'] && !is_file($_boost['filename'])) {
if (!is_dir($_boost['directory'])) {
mkdir($_boost['directory'], 0777, TRUE);
}
file_put_contents($_boost['filename'], $data, LOCK_EX);
// Write data to a file.
if ($_boost['filename']) {
// Attach extension to filename.
$_boost['filename'] .= '.' . $_boost['matched_header_info']['extension'];
// Write to file.
boost_write_file($_boost['directory'], $_boost['filename'], &$data);
}
}
......@@ -182,8 +253,8 @@ function boost_transform_url($url = NULL, $b_path = NULL) {
$items[$hash] = array('cache_this' => FALSE);
return $items[$hash];
}
$parts['base_dir'] = 'cache/normal/' . $parts['host'] . $b_path;
$parts['filename'] = $parts['base_dir'] . $parts['path'] . '_' . $parts['query'];
$parts['base_dir'] = boost_get_normal_cache_dir() . '/' . $parts['host'] . $b_path;
$parts['filename'] = $parts['base_dir'] . $parts['path'] . variable_get('boost_char', BOOST_CHAR) . $parts['query'];
$parts['directory'] = dirname($parts['filename']);
// Get the internal path (node/8).
......@@ -213,6 +284,13 @@ function boost_transform_url($url = NULL, $b_path = NULL) {
return $items[$hash];
}
/**
* Returns the relative normal cache dir. cache/normal.
*/
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);
}
/**
* parse_url that takes into account the base_path
*
......@@ -260,7 +338,7 @@ function boost_parse_url($url = NULL, $b_path = NULL) {
}
// Get fully decoded URL.
$decoded1 = urldecode($parts['base_path'] . $parts['path'] . '_' . $parts['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);
......@@ -270,7 +348,7 @@ function boost_parse_url($url = NULL, $b_path = NULL) {
unset($decoded2);
unset($decoded1);
$parts['url_full'] = $parts['host'] . $parts['base_path'] . $parts['path'] . '_' . $parts['query'];
$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;
......@@ -390,7 +468,13 @@ function boost_is_cacheable($parts) {
* @return $parts
*/
function boost_boost_is_cacheable($parts) {
global $user;
if ($user->uid != 0) {
$parts['is_cacheable'] = FALSE;
}
else {
$parts['is_cacheable'] = TRUE;
}
return $parts;
}
......@@ -414,6 +498,31 @@ function boost_set_cookie($uid, $expires = NULL) {
}
}
/**
* Logic for the setting and removal of the boost cookie.
*/
function boost_cookie_handler() {
global $user;
// Check if Drupal is started from index.php - could cause problems with other
// contrib modules like ad module.
$uid = isset($user->uid) ? $user->uid : 0;
if (strpos($_SERVER['SCRIPT_FILENAME'], 'index.php') !== FALSE) {
// Remove Boost cookie at logout if it still exists.
if (isset($_COOKIE[BOOST_COOKIE]) && $uid == 0) {
boost_set_cookie($uid, REQUEST_TIME - 86400);
}
// Remove Boost cookie if set to -1.
elseif (isset($_COOKIE[BOOST_COOKIE]) && $_COOKIE[BOOST_COOKIE] == '-1') {
boost_set_cookie($uid, REQUEST_TIME - 86400);
}
// Set Boost cookie if it doesn't exists and user is logged in.
elseif (!isset($_COOKIE[BOOST_COOKIE]) && $uid != 0) {
boost_set_cookie($uid);
}
}
}
/**
* Gets menu router contex.
*
......@@ -474,9 +583,15 @@ function _boost_get_menu_router($parts) {
$modules = boost_module_implements('boost_menu_router', 'boost');
foreach ($modules as $module) {
if (($result = module_invoke($module, 'boost_menu_router', $parts)) !== NULL) {
return $result;
break;
}
}
// Remove extra data from the load function
unset($result['menu_item']['map']);
unset($result['menu_item']['page_arguments']);
return $result;
}
/**
......@@ -721,7 +836,7 @@ function boost_get_storage_types() {
}
// Merge it all back together
$merged = array_merge($secondary_types, $primary_types);
$merged = array_merge($primary_types, $secondary_types);
$types = array();
foreach ($merged as $type => $values) {
if ($values['title']) {
......@@ -735,10 +850,12 @@ function boost_get_storage_types() {
$keys = array(
'enabled',
'extension',
'gzip',
'lifetime_max',
'lifetime_min',
'commment_start',
'commment_end',
);
foreach ($types as $title => $content_types) {
foreach ($content_types as $type => $values) {
......@@ -768,6 +885,7 @@ function boost_boost_storage_types() {
'description' => t('HTML output, usually a webpage'),
'extension' => 'html',
'enabled' => TRUE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'commment_start' => '<!-- ',
......@@ -779,6 +897,7 @@ function boost_boost_storage_types() {
'description' => t('XML output, usually a feed'),
'extension' => 'xml',
'enabled' => FALSE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'commment_start' => '<!-- ',
......@@ -794,6 +913,7 @@ function boost_boost_storage_types() {
'description' => t('JSON output, usually a response to a AJAX request'),
'extension' => 'json',
'enabled' => FALSE,
'gzip' => TRUE,
'lifetime_max' => 3600,
'lifetime_min' => 0,
'commment_start' => '/* ',
......@@ -866,6 +986,78 @@ function boost_file_get_age($filename) {
return REQUEST_TIME - filemtime($filename);
}
/**
* Returns a nice html formatted version of print_r
*
* @param $data
*
* @return string
*/
function boost_print_r($data) {
return str_replace(' ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($data, TRUE))));
}
/**
* Write to a file.
*
* @param $directory
* relative directory.
* @param $filename
* relative filename.
* @param $data
* data to write to the file.
*/
function boost_write_file($directory, $filename, &$data) {
if (!is_file($filename)) {
// Create directory if it doesn't exist.
if (!boost_mkdir($directory)) {
return FALSE;
}
// Save data to a file.
if (file_put_contents($filename, $data, LOCK_EX) === FALSE) {
watchdog('boost', 'Could not create the file %file on your system', array('%dir' => $filename), WATCHDOG_ERROR);
return FALSE;
}
// chmod file so webserver can send it out.
$chmod_file = variable_get('boost_permissions_file', BOOST_PERMISSIONS_FILE);
if (is_numeric($chmod_file)) {
@chmod($filename, octdec($chmod_file));
}
}
return TRUE;
}
/**
* Create a directory.
*
* @param $directory
* relative directory.
*/
function boost_mkdir($directory) {
global $_boost;
// Only do something if it's not a dir
if (!is_dir($directory)) {
// Only operate in the cache dir.
if (strpos($directory, variable_get('boost_root_cache_dir', BOOST_ROOT_CACHE_DIR)) !== 0) {
watchdog('boost', 'Could not create the directory %dir on your system; it is outside the cache directory. Debug info below <br> %debug', array(
'%dir' => $directory,
'%debug' => boost_print_r($_boost),
),
WATCHDOG_CRITICAL);
return FALSE;
}
// Try to create the directory.
$chmod_dir = variable_get('boost_permissions_dir', BOOST_PERMISSIONS_DIR);
if (!mkdir($directory, octdec($chmod_dir), TRUE)) {
watchdog('boost', 'Could not create the directory %dir on your system', array('%dir' => $directory), WATCHDOG_ERROR);
return FALSE;
}
}
return TRUE;
}
/**
* Capture error conditions.
*
......@@ -904,4 +1096,9 @@ function boost_deliver_html_page($page_callback_result) {
// Call original function
drupal_deliver_html_page($page_callback_result);
// Last possible place to send a watchdog message without a shutdown function.
if (variable_get('boost_message_debug', BOOST_MESSAGE_DEBUG) && $_boost['args'][0] != 'admin') {
watchdog('boost', boost_print_r($_boost), array(), WATCHDOG_DEBUG);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment