function test_HTTP_Encoder() { global $thisDir; HTTP_Encoder::$encodeToIe6 = true; $methodTests = array(array('ua' => 'Any browser', 'ae' => 'compress, x-gzip', 'exp' => array('gzip', 'x-gzip'), 'desc' => 'recognize "x-gzip" as gzip'), array('ua' => 'Any browser', 'ae' => 'compress, x-gzip;q=0.5', 'exp' => array('gzip', 'x-gzip'), 'desc' => 'gzip w/ non-zero q'), array('ua' => 'Any browser', 'ae' => 'compress, x-gzip;q=0', 'exp' => array('compress', 'compress'), 'desc' => 'gzip w/ zero q'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'ae' => 'gzip, deflate', 'exp' => array('', ''), 'desc' => 'IE6 w/o "enhanced security"'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 'ae' => 'gzip, deflate', 'exp' => array('deflate', 'deflate'), 'desc' => 'IE6 w/ "enhanced security"'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.01)', 'ae' => 'gzip, deflate', 'exp' => array('', ''), 'desc' => 'IE5.5'), array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.25', 'ae' => 'gzip,deflate', 'exp' => array('deflate', 'deflate'), 'desc' => 'Opera identifying as IE6')); foreach ($methodTests as $test) { $_SERVER['HTTP_USER_AGENT'] = $test['ua']; $_SERVER['HTTP_ACCEPT_ENCODING'] = $test['ae']; $exp = $test['exp']; $ret = HTTP_Encoder::getAcceptedEncoding(); $passed = assertTrue($exp == $ret, 'HTTP_Encoder : ' . $test['desc']); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n--- AE | UA = {$test['ae']} | {$test['ua']}\n"; echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n"; echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n"; } } HTTP_Encoder::$encodeToIe6 = false; $methodTests = array(array('ua' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)', 'ae' => 'gzip, deflate', 'exp' => array('', ''), 'desc' => 'IE6 w/ "enhanced security"')); foreach ($methodTests as $test) { $_SERVER['HTTP_USER_AGENT'] = $test['ua']; $_SERVER['HTTP_ACCEPT_ENCODING'] = $test['ae']; $exp = $test['exp']; $ret = HTTP_Encoder::getAcceptedEncoding(); $passed = assertTrue($exp == $ret, 'HTTP_Encoder : ' . $test['desc']); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n--- AE | UA = {$test['ae']} | {$test['ua']}\n"; echo "Expected = " . preg_replace('/\\s+/', ' ', var_export($exp, 1)) . "\n"; echo "Returned = " . preg_replace('/\\s+/', ' ', var_export($ret, 1)) . "\n\n"; } } if (!function_exists('gzdeflate')) { echo "!WARN: HTTP_Encoder : Zlib support is not present in PHP. Encoding cannot be performed/tested.\n"; return; } // test compression of varied content (HTML,JS, & CSS) $variedContent = file_get_contents($thisDir . '/_test_files/html/before.html') . file_get_contents($thisDir . '/_test_files/css/subsilver.css') . file_get_contents($thisDir . '/_test_files/js/jquery-1.2.3.js'); $variedLength = strlen($variedContent); $encodingTests = array(array('method' => 'deflate', 'inv' => 'gzinflate', 'exp' => 32157), array('method' => 'gzip', 'inv' => '_gzdecode', 'exp' => 32175), array('method' => 'compress', 'inv' => 'gzuncompress', 'exp' => 32211)); foreach ($encodingTests as $test) { $e = new HTTP_Encoder(array('content' => $variedContent, 'method' => $test['method'])); $e->encode(9); $ret = strlen($e->getContent()); // test uncompression $roundTrip = @call_user_func($test['inv'], $e->getContent()); $desc = "HTTP_Encoder : {$test['method']} : uncompress possible"; $passed = assertTrue($variedContent == $roundTrip, $desc); // test expected compressed size $desc = "HTTP_Encoder : {$test['method']} : compressed to " . sprintf('%4.2f%% of original', $ret / $variedLength * 100); $passed = assertTrue(abs($ret - $test['exp']) < 100, $desc); if (__FILE__ === realpath($_SERVER['SCRIPT_FILENAME'])) { echo "\n--- {$test['method']}: expected bytes: ", "{$test['exp']}. Returned: {$ret} ", "(off by " . abs($ret - $test['exp']) . " bytes)\n\n"; } } }
/** * Runs minify */ function process() { require_once W3TC_LIB_MINIFY_DIR . '/Minify.php'; require_once W3TC_LIB_MINIFY_DIR . '/HTTP/Encoder.php'; HTTP_Encoder::$encodeToIe6 = $this->_config->get_boolean('minify.comprss.ie6'); Minify::$uploaderHoursBehind = $this->_config->get_integer('minify.fixtime'); Minify::setCache($this->_get_cache()); $serve_options = $this->_config->get_array('minify.options'); $serve_options['maxAge'] = $this->_config->get_integer('minify.maxage'); $serve_options['encodeOutput'] = $this->_config->get_boolean('minify.compress'); $serve_options['postprocessor'] = array(&$this, 'postprocessor'); if (stripos(PHP_OS, 'win') === 0) { Minify::setDocRoot(); } foreach ($this->_config->get_array('minify.symlinks') as $link => $target) { $link = str_replace('//', realpath($_SERVER['DOCUMENT_ROOT']), $link); $link = strtr($link, '/', DIRECTORY_SEPARATOR); $serve_options['minifierOptions']['text/css']['symlinks'][$link] = realpath($target); } if ($this->_config->get_boolean('minify.debug')) { $serve_options['debug'] = true; } if ($this->_config->get('minify.debug')) { require_once W3TC_LIB_MINIFY_DIR . '/Minify/Logger.php'; Minify_Logger::setLogger($this); } if (isset($_GET['f']) || isset($_GET['gg']) && isset($_GET['g']) && isset($_GET['t'])) { if (isset($_GET['gg']) && isset($_GET['g']) && isset($_GET['t'])) { $serve_options['minApp']['groups'] = $this->get_groups($_GET['gg'], $_GET['t']); if ($_GET['t'] == 'js' && (in_array($_GET['g'], array('include', 'include-nb')) && $this->_config->get_boolean('minify.js.combine.header') || in_array($_GET['g'], array('include-footer', 'include-footer-nb')) && $this->_config->get_boolean('minify.js.combine.footer'))) { $serve_options['minifiers']['application/x-javascript'] = array($this, 'minify_stub'); } /** * Setup user-friendly cache ID for disk cache */ if ($this->_config->get_string('minify.engine') == 'file') { $cacheId = sprintf('%s.%s.%s', $_GET['gg'], $_GET['g'], $_GET['t']); Minify::setCacheId($cacheId); } } @header('Pragma: public'); try { Minify::serve('MinApp', $serve_options); } catch (Exception $exception) { printf('<strong>W3 Total Cache Error:</strong> Minify error: %s', $exception->getMessage()); } } else { die('This file cannot be accessed directly'); } }
<?php /** * AJAX checks for zlib.output_compression * * @package Minify */ $_oc = ini_get('zlib.output_compression'); // allow access only if builder is enabled require __DIR__ . '/../config.php'; if (!$min_enableBuilder) { header('Location: /'); exit; } if (isset($_GET['hello'])) { // echo 'World!' // try to prevent double encoding (may not have an effect) ini_set('zlib.output_compression', '0'); require $min_libPath . '/HTTP/Encoder.php'; HTTP_Encoder::$encodeToIe6 = true; // just in case $he = new HTTP_Encoder(array('content' => 'World!', 'method' => 'deflate')); $he->encode(); $he->sendAll(); } else { // echo status "0" or "1" header('Content-Type: text/plain'); echo (int) $_oc; }
$_oc = ini_get('zlib.output_compression'); // allow access only if builder is enabled require dirname(__FILE__) . '/../config.php'; if (! $min_enableBuilder) { header('Location: /'); exit(); } if (isset($_GET['hello'])) { // echo 'World!' // try to prevent double encoding (may not have an effect) ini_set('zlib.output_compression', '0'); require $min_libPath . '/HTTP/Encoder.php'; HTTP_Encoder::$encodeToIe6 = true; // just in case $he = new HTTP_Encoder(array( 'content' => 'World!' ,'method' => 'deflate' )); $he->encode(); $he->sendAll(); } else { // echo status "0" or "1" header('Content-Type: text/plain'); echo (int)$_oc; }