You are here

function minifyhtml_exit in Minify Source HTML 7

Implements hook_exit().

File

./minifyhtml.module, line 102
Hook and helper functions for the Minify HTML module.

Code

function minifyhtml_exit() {
  if (variable_get('minifyhtml_minify', 0)) {

    // Get current path value.
    $current_path = function_exists('current_path') ? current_path() : $_GET['q'];

    // The content type of the page must be text/html to proceed. All other
    // content types must be ignored - the name of the module is minifyhtml
    // after all.
    // Make sure to exclude image style images (is this even required
    // anymore??).
    // Make sure the buffer has a length.
    if (stripos(drupal_get_http_header('content-type'), 'text/html') !== FALSE && !is_file($current_path) && ob_get_length()) {

      // Catch the output buffer. Converted from ob_get_clean().
      $page = ob_get_contents();

      // If the content should be encoded, try to decode it.
      $decoded = FALSE;
      if (variable_get('page_compression', TRUE) && extension_loaded('zlib')) {
        $decoded = @gzinflate(substr(substr($page, 10), 0, -8));
        if ($decoded) {
          $page = $decoded;
        }
      }

      // Minify the HTML.
      minifyhtml_minify($page);

      // If the content was decoded before being minified, it needs to be
      // re-encoded.
      if (variable_get('page_compression', TRUE) && extension_loaded('zlib') && $decoded) {
        $page = gzencode($page, 9, FORCE_GZIP);
      }

      // If, for some reason, the minification failed and some artifacts still
      // remain in the source this will cause a mostly white unusable page. The
      // fallback for this scenario is to revert and notify.
      if (strpos($page, '%' . MINIFYHTML_PLACEHOLDER)) {
        $page = ob_get_contents();
        watchdog('minifyhtml', 'Minifyhtml failed on %path', array(
          '%path' => $current_path,
        ), WATCHDOG_WARNING, $current_path);
      }

      // Re-populate the output buffer.
      ob_clean();
      print $page;
    }
  }
}