You are here

function advagg_htaccess_check_generate in Advanced CSS/JS Aggregation 7

Same name and namespace in other branches
  1. 6 advagg.module \advagg_htaccess_check_generate()
  2. 7.2 advagg.missing.inc \advagg_htaccess_check_generate()

Generate .htaccess rules and place them in advagg dir

Parameters

$dest: destination of the file that just got saved.

$force: force recreate the .htaccess file.

3 calls to advagg_htaccess_check_generate()
advagg_admin_recreate_htaccess in includes/admin.inc
Rebuild htaccess files.
advagg_file_saver in ./advagg.module
Save a string to the specified destination. Verify that file size is not zero.
advagg_js_compress_file_saver in advagg_js_compress/advagg_js_compress.module
Save a string to the specified destination. Verify that file size is not zero.

File

./advagg.module, line 2476
Advanced CSS/JS aggregation module

Code

function advagg_htaccess_check_generate($dest, $force = FALSE) {
  global $base_path;
  if (!$force && !variable_get('advagg_dir_htaccess', ADVAGG_DIR_HTACCESS)) {
    return TRUE;
  }
  $dir = dirname($dest);
  $htaccess_file = $dir . '/.htaccess';
  advagg_clearstatcache(TRUE, $htaccess_file);
  if (!$force && file_exists($htaccess_file)) {
    return TRUE;
  }
  list($css_path, $js_path) = advagg_get_root_files_dir();
  $type = '';
  if ($dir == $js_path[1]) {
    $ext = 'js';
    $path = $js_path[1];
    $type = 'text/javascript';
  }
  elseif ($dir == $css_path[1]) {
    $ext = 'css';
    $path = $css_path[1];
    $type = 'text/css';
  }
  else {
    return FALSE;
  }
  $data = "\n";
  if (variable_get('advagg_gzip_compression', ADVAGG_GZIP_COMPRESSION)) {
    $data .= "<IfModule mod_rewrite.c>\n";
    $data .= "  RewriteEngine on\n";
    $data .= "  RewriteBase {$base_path}{$path}\n";
    $data .= "\n";
    $data .= "  # Send 404's back to index.php\n";
    $data .= "  RewriteCond %{REQUEST_FILENAME} !-s\n";
    $data .= "  RewriteRule ^(.*)\$ {$base_path}index.php?q={$path}/\$1 [L]\n";
    $data .= "\n";
    $data .= "  # Rules to correctly serve gzip compressed {$ext} files.\n";
    $data .= "  # Requires both mod_rewrite and mod_headers to be enabled.\n";
    $data .= "  <IfModule mod_headers.c>\n";
    $data .= "    # Serve gzip compressed {$ext} files if they exist and client accepts gzip.\n";
    $data .= "    RewriteCond %{HTTP:Accept-encoding} gzip\n";
    $data .= "    RewriteCond %{REQUEST_FILENAME}\\.gz -s\n";
    $data .= "    RewriteRule ^(.*)\\.{$ext}\$ \$1\\.{$ext}\\.gz [QSA]\n";
    $data .= "\n";
    $data .= "    # Serve correct content types, and prevent mod_deflate double gzip.\n";
    $data .= "    RewriteRule \\.{$ext}\\.gz\$ - [T={$type},E=no-gzip:1]\n";
    $data .= "\n";
    $data .= "    <FilesMatch \"\\.{$ext}\\.gz\$\">\n";
    $data .= "      # Serve correct encoding type.\n";
    $data .= "      Header set Content-Encoding gzip\n";
    $data .= "      # Force proxies to cache gzipped & non-gzipped {$ext} files separately.\n";
    $data .= "      Header append Vary Accept-Encoding\n";
    $data .= "    </FilesMatch>\n";
    $data .= "  </IfModule>\n";
    $data .= "</IfModule>\n";
    $data .= "\n";
  }
  $data .= "<FilesMatch \"^{$ext}_[0-9a-f]{32}_.+\\.{$ext}(\\.gz)?\">\n";
  $data .= "  # No mod_headers\n";
  $data .= "  <IfModule !mod_headers.c>\n";
  $data .= "    # No mod_expires\n";
  $data .= "    <IfModule !mod_expires.c>\n";
  $data .= "      # Use ETags.\n";
  $data .= "      FileETag MTime Size\n";
  $data .= "    </IfModule>\n";
  $data .= "\n";
  $data .= "    # Use Expires Directive.\n";
  $data .= "    <IfModule mod_expires.c>\n";
  $data .= "      # Do not use ETags.\n";
  $data .= "      FileETag None\n";
  $data .= "      # Enable expirations.\n";
  $data .= "      ExpiresActive On\n";
  $data .= "      # Cache all aggregated {$ext} files for 480 weeks after access (A).\n";
  $data .= "      ExpiresDefault A290304000\n";
  $data .= "    </IfModule>\n";
  $data .= "  </IfModule>\n";
  $data .= "\n";
  $data .= "  <IfModule mod_headers.c>\n";
  $data .= "    # Set a far future Cache-Control header to 480 weeks.\n";
  $data .= "    Header set Cache-Control \"max-age=290304000, no-transform, public\"\n";
  $data .= "    # Set a far future Expires header.\n";
  $data .= "    Header set Expires \"Tue, 20 Jan 2037 04:20:42 GMT\"\n";
  $data .= "    # Pretend the file was last modified a long time ago in the past.\n";
  $data .= "    Header set Last-Modified \"Wed, 20 Jan 1988 04:20:42 GMT\"\n";
  $data .= "    # Do not use etags for cache validation.\n";
  $data .= "    Header unset ETag\n";
  $data .= "  </IfModule>\n";
  $data .= "</FilesMatch>\n";
  if (!advagg_file_save_data($data, $htaccess_file, FILE_EXISTS_REPLACE)) {
    return FALSE;
  }
  return TRUE;
}