9 * |
9 * |
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
12 * |
12 * |
13 */ |
13 */ |
14 |
14 |
15 // Set up gzip encoding before any output is sent |
15 // Set up gzip encoding before any output is sent |
16 |
16 |
17 $aggressive_optimize_html = false; |
17 $aggressive_optimize_html = false; |
18 |
18 |
19 global $do_gzip; |
19 global $do_gzip; |
20 $do_gzip = false; |
20 $do_gzip = false; |
21 |
21 |
22 if(isset($_SERVER['PATH_INFO'])) $v = $_SERVER['PATH_INFO']; |
22 if(isset($_SERVER['PATH_INFO'])) $v = $_SERVER['PATH_INFO']; |
23 elseif(isset($_GET['title'])) $v = $_GET['title']; |
23 elseif(isset($_GET['title'])) $v = $_GET['title']; |
24 else $v = ''; |
24 else $v = ''; |
25 |
25 |
26 error_reporting(E_ALL); |
26 error_reporting(E_ALL); |
27 |
27 |
28 // 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 |
28 // 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 |
29 // { |
29 // { |
30 // $do_gzip = ( function_exists('gzcompress') && ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') ) ) ? true : false; |
30 // $do_gzip = ( function_exists('gzcompress') && ( isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') ) ) ? true : false; |
31 // // Uncomment the following line to enable debugConsole (requires PHP 5 or later) |
31 // // Uncomment the following line to enable debugConsole (requires PHP 5 or later) |
32 // // define('ENANO_DEBUG', ''); |
32 // // define('ENANO_DEBUG', ''); |
33 // } |
33 // } |
34 |
34 |
35 if(defined('ENANO_DEBUG')) $do_gzip = false; |
35 if(defined('ENANO_DEBUG')) $do_gzip = false; |
36 |
36 |
37 if($aggressive_optimize_html || $do_gzip) |
37 if($aggressive_optimize_html || $do_gzip) |
38 { |
38 { |
39 ob_start(); |
39 ob_start(); |
40 } |
40 } |
41 |
41 |
42 require('includes/common.php'); |
42 require('includes/common.php'); |
43 |
43 |
44 global $db, $session, $paths, $template, $plugins; // Common objects |
44 global $db, $session, $paths, $template, $plugins; // Common objects |
45 |
45 |
46 if(!isset($_GET['do'])) $_GET['do'] = 'view'; |
46 if(!isset($_GET['do'])) $_GET['do'] = 'view'; |
47 switch($_GET['do']) |
47 switch($_GET['do']) |
48 { |
48 { |
49 default: |
49 default: |
50 die_friendly('Invalid action', '<p>The action "'.$_GET['do'].'" is not defined. Return to <a href="'.makeUrl($paths->page).'">viewing this page\'s text</a>.</p>'); |
50 die_friendly('Invalid action', '<p>The action "'.$_GET['do'].'" is not defined. Return to <a href="'.makeUrl($paths->page).'">viewing this page\'s text</a>.</p>'); |
358 case 'aclmanager': |
358 case 'aclmanager': |
359 $data = ( isset($_POST['data']) ) ? $_POST['data'] : Array('mode' => 'listgroups'); |
359 $data = ( isset($_POST['data']) ) ? $_POST['data'] : Array('mode' => 'listgroups'); |
360 PageUtils::aclmanager($data); |
360 PageUtils::aclmanager($data); |
361 break; |
361 break; |
362 } |
362 } |
363 |
363 |
364 // |
364 // |
365 // Optimize HTML by replacing newlines with spaces (excludes <pre>, <script>, and <style> blocks) |
365 // Optimize HTML by replacing newlines with spaces (excludes <pre>, <script>, and <style> blocks) |
366 // |
366 // |
367 if ($aggressive_optimize_html) |
367 if ($aggressive_optimize_html) |
368 { |
368 { |
369 // Load up the HTML |
369 // Load up the HTML |
370 $html = ob_get_contents(); |
370 $html = ob_get_contents(); |
371 ob_end_clean(); |
371 ob_end_clean(); |
372 |
372 |
373 // Which tags to strip - you can change this if needed |
373 // Which tags to strip - you can change this if needed |
374 $strip_tags = Array('pre', 'script', 'style', 'enano:no-opt'); |
374 $strip_tags = Array('pre', 'script', 'style', 'enano:no-opt'); |
375 $strip_tags = implode('|', $strip_tags); |
375 $strip_tags = implode('|', $strip_tags); |
376 |
376 |
377 // Strip out the tags and replace with placeholders |
377 // Strip out the tags and replace with placeholders |
378 preg_match_all("#<($strip_tags)(.*?)>(.*?)</($strip_tags)>#is", $html, $matches); |
378 preg_match_all("#<($strip_tags)(.*?)>(.*?)</($strip_tags)>#is", $html, $matches); |
379 $seed = md5(microtime() . mt_rand()); // Random value used for placeholders |
379 $seed = md5(microtime() . mt_rand()); // Random value used for placeholders |
380 for ($i = 0;$i < sizeof($matches[1]); $i++) |
380 for ($i = 0;$i < sizeof($matches[1]); $i++) |
381 { |
381 { |
382 $html = str_replace("<{$matches[1][$i]}{$matches[2][$i]}>{$matches[3][$i]}</{$matches[4][$i]}>", "{DONT_STRIP_ME_NAKED:$seed:$i}", $html); |
382 $html = str_replace("<{$matches[1][$i]}{$matches[2][$i]}>{$matches[3][$i]}</{$matches[4][$i]}>", "{DONT_STRIP_ME_NAKED:$seed:$i}", $html); |
383 } |
383 } |
384 |
384 |
385 // Finally, process the HTML |
385 // Finally, process the HTML |
386 $html = preg_replace("#\n([ ]*)#", " ", $html); |
386 $html = preg_replace("#\n([ ]*)#", " ", $html); |
387 |
387 |
388 // Remove annoying spaces between tags |
388 // Remove annoying spaces between tags |
389 $html = preg_replace("#>([ ]*?){2,}<#", "> <", $html); |
389 $html = preg_replace("#>([ ]*?){2,}<#", "> <", $html); |
390 |
390 |
391 // Re-insert untouchable tags |
391 // Re-insert untouchable tags |
392 for ($i = 0;$i < sizeof($matches[1]); $i++) |
392 for ($i = 0;$i < sizeof($matches[1]); $i++) |
393 { |
393 { |
394 $html = str_replace("{DONT_STRIP_ME_NAKED:$seed:$i}", "<{$matches[1][$i]}{$matches[2][$i]}>{$matches[3][$i]}</{$matches[4][$i]}>", $html); |
394 $html = str_replace("{DONT_STRIP_ME_NAKED:$seed:$i}", "<{$matches[1][$i]}{$matches[2][$i]}>{$matches[3][$i]}</{$matches[4][$i]}>", $html); |
395 } |
395 } |
396 |
396 |
397 // Remove <enano:no-opt> blocks (can be used by themes that don't want their HTML optimized) |
397 // Remove <enano:no-opt> blocks (can be used by themes that don't want their HTML optimized) |
398 $html = preg_replace('#<(\/|)enano:no-opt(.*?)>#', '', $html); |
398 $html = preg_replace('#<(\/|)enano:no-opt(.*?)>#', '', $html); |
399 |
399 |
400 // Tell snoopish users what's going on |
400 // Tell snoopish users what's going on |
401 $html = str_replace('<html>', "\n<!-- NOTE: This HTML document has been Aggressively Optimized(TM) by Enano to make page loading faster. -->\n<html>", $html); |
401 $html = str_replace('<html>', "\n<!-- NOTE: This HTML document has been Aggressively Optimized(TM) by Enano to make page loading faster. -->\n<html>", $html); |
402 |
402 |
403 // Re-enable output buffering to allow the Gzip function (below) to work |
403 // Re-enable output buffering to allow the Gzip function (below) to work |
404 ob_start(); |
404 ob_start(); |
405 |
405 |
406 // Done, send it to the user |
406 // Done, send it to the user |
407 echo( $html ); |
407 echo( $html ); |
408 } |
408 } |
409 |
409 |
410 // |
410 // |
411 // Compress buffered output if required and send to browser |
411 // Compress buffered output if required and send to browser |
412 // |
412 // |
413 if ( $do_gzip ) |
413 if ( $do_gzip ) |
414 { |
414 { |
415 // |
415 // |
416 // Copied from phpBB, which was in turn borrowed from php.net |
416 // Copied from phpBB, which was in turn borrowed from php.net |
417 // |
417 // |
418 $gzip_contents = ob_get_contents(); |
418 $gzip_contents = ob_get_contents(); |
419 ob_end_clean(); |
419 ob_end_clean(); |
420 |
420 |
421 $gzip_size = strlen($gzip_contents); |
421 $gzip_size = strlen($gzip_contents); |
422 $gzip_crc = crc32($gzip_contents); |
422 $gzip_crc = crc32($gzip_contents); |
423 |
423 |
424 $gzip_contents = gzcompress($gzip_contents, 9); |
424 $gzip_contents = gzcompress($gzip_contents, 9); |
425 $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4); |
425 $gzip_contents = substr($gzip_contents, 0, strlen($gzip_contents) - 4); |
426 |
426 |
427 header('Content-encoding: gzip'); |
427 header('Content-encoding: gzip'); |
428 echo "\x1f\x8b\x08\x00\x00\x00\x00\x00"; |
428 echo "\x1f\x8b\x08\x00\x00\x00\x00\x00"; |
429 echo $gzip_contents; |
429 echo $gzip_contents; |
430 echo pack('V', $gzip_crc); |
430 echo pack('V', $gzip_crc); |
431 echo pack('V', $gzip_size); |
431 echo pack('V', $gzip_size); |
432 } |
432 } |
433 |
433 |
434 $db->close(); |
434 $db->close(); |
435 |
435 |
436 ?> |
436 ?> |