11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
13 * |
13 * |
14 */ |
14 */ |
15 |
15 |
16 // Set up gzip encoding before any output is sent |
16 // Se t up gzip encoding before any output is sent |
17 |
17 |
18 $aggressive_optimize_html = false; |
18 $aggressive_optimize_html = true; |
19 |
19 |
20 global $do_gzip; |
20 global $do_gzip; |
21 $do_gzip = false; |
21 $do_gzip = true; |
22 |
22 |
23 if(isset($_SERVER['PATH_INFO'])) $v = $_SERVER['PATH_INFO']; |
23 if(isset($_SERVER['PATH_INFO'])) $v = $_SERVER['PATH_INFO']; |
24 elseif(isset($_GET['title'])) $v = $_GET['title']; |
24 elseif(isset($_GET['title'])) $v = $_GET['title']; |
25 else $v = ''; |
25 else $v = ''; |
|
26 |
|
27 if ( isset($_GET['nocompress']) ) |
|
28 $aggressive_optimize_html = false; |
26 |
29 |
27 error_reporting(E_ALL); |
30 error_reporting(E_ALL); |
28 |
31 |
29 // if(!strstr($v, 'CSS') && !strstr($v, 'UploadFile') && !strstr($v, 'DownloadFile')) // These pages are blacklisted because we can't have debugConsole's HTML output disrupting the flow of header() calls and whatnot |
32 // if(!strstr($v, 'CSS') && !strstr($v, 'UploadFile') && !strstr($v, 'DownloadFile')) // These pages are blacklisted because we can't have debugConsole's HTML output disrupting the flow of header() calls and whatnot |
30 // { |
33 // { |
369 { |
372 { |
370 // Load up the HTML |
373 // Load up the HTML |
371 $html = ob_get_contents(); |
374 $html = ob_get_contents(); |
372 ob_end_clean(); |
375 ob_end_clean(); |
373 |
376 |
374 // Which tags to strip - you can change this if needed |
377 $html = aggressive_optimize_html($html); |
375 $strip_tags = Array('pre', 'script', 'style', 'enano:no-opt'); |
|
376 $strip_tags = implode('|', $strip_tags); |
|
377 |
|
378 // Strip out the tags and replace with placeholders |
|
379 preg_match_all("#<($strip_tags)(.*?)>(.*?)</($strip_tags)>#is", $html, $matches); |
|
380 $seed = md5(microtime() . mt_rand()); // Random value used for placeholders |
|
381 for ($i = 0;$i < sizeof($matches[1]); $i++) |
|
382 { |
|
383 $html = str_replace("<{$matches[1][$i]}{$matches[2][$i]}>{$matches[3][$i]}</{$matches[4][$i]}>", "{DONT_STRIP_ME_NAKED:$seed:$i}", $html); |
|
384 } |
|
385 |
|
386 // Finally, process the HTML |
|
387 $html = preg_replace("#\n([ ]*)#", " ", $html); |
|
388 |
|
389 // Remove annoying spaces between tags |
|
390 $html = preg_replace("#>([ ]*?){2,}<#", "> <", $html); |
|
391 |
|
392 // Re-insert untouchable tags |
|
393 for ($i = 0;$i < sizeof($matches[1]); $i++) |
|
394 { |
|
395 $html = str_replace("{DONT_STRIP_ME_NAKED:$seed:$i}", "<{$matches[1][$i]}{$matches[2][$i]}>{$matches[3][$i]}</{$matches[4][$i]}>", $html); |
|
396 } |
|
397 |
|
398 // Remove <enano:no-opt> blocks (can be used by themes that don't want their HTML optimized) |
|
399 $html = preg_replace('#<(\/|)enano:no-opt(.*?)>#', '', $html); |
|
400 |
|
401 // Tell snoopish users what's going on |
|
402 $html = str_replace('<html>', "\n<!-- NOTE: This HTML document has been Aggressively Optimized(TM) by Enano to make page loading faster. -->\n<html>", $html); |
|
403 |
378 |
404 // Re-enable output buffering to allow the Gzip function (below) to work |
379 // Re-enable output buffering to allow the Gzip function (below) to work |
405 ob_start(); |
380 ob_start(); |
406 |
381 |
407 // Done, send it to the user |
382 // Done, send it to the user |
408 echo( $html ); |
383 echo( $html ); |
409 } |
384 } |
410 |
|
411 // |
|
412 // Compress buffered output if required and send to browser |
|
413 // |
|
414 if ( $do_gzip ) |
|
415 { |
|
416 // |
|
417 // Copied from phpBB, which was in turn borrowed from php.net |
|
418 // |
|
419 $gzip_contents = ob_get_contents(); |
|
420 ob_end_clean(); |
|
421 |
|
422 $gzip_size = strlen($gzip_contents); |
|
423 $gzip_crc = crc32($gzip_contents); |
|
424 |
|
425 $gzip_contents = gzcompress($gzip_contents, 9); |
|
426 $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4); |
|
427 |
|
428 header('Content-encoding: gzip'); |
|
429 echo "\x1f\x8b\x08\x00\x00\x00\x00\x00"; |
|
430 echo $gzip_contents; |
|
431 echo pack('V', $gzip_crc); |
|
432 echo pack('V', $gzip_size); |
|
433 } |
|
434 |
|
435 $db->close(); |
|
436 |
385 |
|
386 $db->close(); |
|
387 gzip_output(); |
|
388 |
437 ?> |
389 ?> |