From 647d23267694b69050816c1c438f3b2768ef6bcc Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 7 Jul 2013 20:48:13 +0200 Subject: [PATCH] Forcing JavaScript to be inserted on the page bottom --- .../files/lib/util/HeaderUtil.class.php | 78 ++++++++++++------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/wcfsetup/install/files/lib/util/HeaderUtil.class.php b/wcfsetup/install/files/lib/util/HeaderUtil.class.php index 2b14c96178..f61365be4d 100644 --- a/wcfsetup/install/files/lib/util/HeaderUtil.class.php +++ b/wcfsetup/install/files/lib/util/HeaderUtil.class.php @@ -1,6 +1,7 @@ 0 && HTTP_GZIP_LEVEL < 10 && !defined('HTTP_DISABLE_GZIP')) { - self::compressOutput(); - } + ob_start(array('wcf\util\HeaderUtil', 'parseOutput')); // send Internet Explorer compatibility mode @header('X-UA-Compatible: IE=edge'); @@ -57,34 +61,52 @@ final class HeaderUtil { } /** - * Enables the gzip compression of the page output. + * Parses the rendered output. + * + * @param string $output + * @return string */ - public static function compressOutput() { - if (function_exists('gzcompress') && !@ini_get('zlib.output_compression') && !@ini_get('output_handler') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) { - if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip')) { - @header('Content-Encoding: x-gzip'); - } - else { - @header('Content-Encoding: gzip'); + public static function parseOutput($output) { + self::$output = $output; + + // move script tags to the bottom of the page + $javascript = array(); + self::$output = preg_replace_callback('~~s', function($matches) use (&$javascript) { + $javascript[] = $matches[0]; + return ''; + }, self::$output); + + self::$output = str_replace(array('', ''), array('', ''), self::$output); + self::$output .= "\n".implode("\n", $javascript)."\n"; + + // 3rd party plugins may differ the actual output before it is sent to the browser + // please be aware, that $eventObj is not available here due to this being a static + // class. Use HeaderUtil::$output to modify it. + EventHandler::getInstance()->fireAction('wcf\util\HeaderUtil', 'parseOutput'); + + // enable gzip compression + if (HTTP_ENABLE_GZIP && HTTP_GZIP_LEVEL > 0 && HTTP_GZIP_LEVEL < 10 && !defined('HTTP_DISABLE_GZIP')) { + if (function_exists('gzcompress') && !@ini_get('zlib.output_compression') && !@ini_get('output_handler') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) { + if (strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip')) { + @header('Content-Encoding: x-gzip'); + } + else { + @header('Content-Encoding: gzip'); + } + + $size = strlen(self::$output); + $crc = crc32(self::$output); + + $newOutput = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff"; + $newOutput .= substr(gzcompress(self::$output, HTTP_GZIP_LEVEL), 2, -4); + $newOutput .= pack('V', $crc); + $newOutput .= pack('V', $size); + + self::$output = $newOutput; } - ob_start(array('wcf\util\HeaderUtil', 'getCompressedOutput')); } - } - - /** - * Outputs the compressed page content. - */ - public static function getCompressedOutput($output) { - $size = strlen($output); - $crc = crc32($output); - - $newOutput = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff"; - $newOutput .= substr(gzcompress($output, HTTP_GZIP_LEVEL), 2, -4); - unset($output); - $newOutput .= pack('V', $crc); - $newOutput .= pack('V', $size); - return $newOutput; + return self::$output; } /** -- 2.20.1