You are here

function boost_htaccess_cache_dir_generate in Boost 7

Same name and namespace in other branches
  1. 6 boost.module \boost_htaccess_cache_dir_generate()

Generate htaccess rules for the cache directory.

1 call to boost_htaccess_cache_dir_generate()
boost_htaccess_cache_dir_put in ./boost.module
Overwrite old htaccess rules with new ones.

File

./boost.module, line 1541
Caches generated output as a static file to be served directly from the webserver.

Code

function boost_htaccess_cache_dir_generate() {
  $char_type = variable_get('boost_charset_type', BOOST_CHARSET_TYPE);
  $etag = variable_get('boost_apache_etag', BOOST_APACHE_ETAG);

  // Go through every storage type getting data needed to build htaccess file.
  $gzip = FALSE;
  $data = array();
  $files = array();
  $types = boost_get_storage_types();
  foreach ($types as $title => $content_types) {
    foreach ($content_types as $type => $values) {
      if ($values['enabled']) {
        $forcetype = '\\.' . $values['extension'];
        if ($values['gzip']) {
          $forcetype .= '(\\.gz)?$';
          $gzip = TRUE;
        }
        else {
          $forcetype .= '$';
        }
        $files[$values['extension']] = $values['extension'];
        $data[$values['extension']] = array(
          'type' => $type,
          'forcetype' => $forcetype,
        );
      }
    }
  }
  if (empty($data) || empty($files)) {
    return FALSE;
  }

  // Add in default charset.
  $string = '';
  if (variable_get('boost_add_default_charset', BOOST_ADD_DEFAULT_CHARSET)) {
    $string .= 'AddDefaultCharset ' . $char_type . "\n";
  }

  // Set FileETag.
  if ($etag == 1) {
    $string .= "FileETag None\n";
  }
  elseif ($etag == 2) {
    $string .= "FileETag All\n";
  }
  elseif ($etag == 3) {
    $string .= "FileETag MTime Size\n";
  }

  // Set html expiration time to the past and put in boost header if desired.
  $files = '(' . implode('|', $files) . ')';
  if ($gzip) {
    $files .= '(\\.gz)?';
  }
  $string .= "<FilesMatch \"\\.{$files}\$\">\n";
  $string .= "  <IfModule mod_expires.c>\n";
  $string .= "    ExpiresDefault A5\n";
  $string .= "  </IfModule>\n";
  $string .= "  <IfModule mod_headers.c>\n";
  $string .= "    Header set Expires \"Sun, 19 Nov 1978 05:00:00 GMT\"\n";
  $string .= "    Header unset Last-Modified\n";
  $string .= "    Header append Vary Accept-Encoding\n";
  if (variable_get('boost_apache_vary_cookie', BOOST_APACHE_VARY_COOKIE) > 0) {
    $string .= "    Header append Vary Cookie\n";
  }
  $string .= "    Header set Cache-Control \"no-store, no-cache, must-revalidate, post-check=0, pre-check=0\"\n";
  if (variable_get('boost_apache_xheader', BOOST_APACHE_XHEADER) > 0) {
    $string .= "    Header set X-Cached-By \"Boost\"\n";
  }

  // Include X-Frame-Options from core (common.inc)
  $frame_options = variable_get('x_frame_options', 'SAMEORIGIN');
  $string .= "    Header set X-Frame-Options \"" . $frame_options . "\"\n";
  $string .= "  </IfModule>\n";
  $string .= "</FilesMatch>\n";

  // Set charset and content encoding.
  $string .= "<IfModule mod_mime.c>\n";
  foreach ($data as $extension => $values) {
    $string .= '  AddCharset ' . $char_type . ' .' . $extension . "\n";
  }
  $string .= $gzip ? "  AddEncoding gzip .gz\n" : '';
  $string .= "</IfModule>\n";

  // Fix for versions of apache that do not respect the T='' RewriteRule
  foreach ($data as $extension => $values) {
    $forcetype = $values['forcetype'];
    $type = $values['type'];
    $string .= "<FilesMatch \"{$forcetype}\">\n";
    $string .= '  ForceType ' . $type . "\n";
    $string .= "</FilesMatch>\n";
  }

  // Make sure files can not execute in the cache dir.
  $string .= "\n";
  $string .= "SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006\n";
  $string .= "Options None\n";
  $string .= 'Options +' . (variable_get('boost_match_symlinks_options', BOOST_MATCH_SYMLINKS_OPTIONS) ? "FollowSymLinks" : "SymLinksIfOwnerMatch") . "\n";
  $string .= "\n";
  return $string;
}