includes/functions.php
changeset 80 cb7dde69c301
parent 78 4df25dfdde63
child 81 d7fc25acd3f3
equal deleted inserted replaced
79:5faff33a6580 80:cb7dde69c301
   730   }
   730   }
   731   
   731   
   732   if ( $paths->namespace != 'Special' && $paths->namespace != 'Admin' )
   732   if ( $paths->namespace != 'Special' && $paths->namespace != 'Admin' )
   733   {
   733   {
   734     echo '<div class="mdg-comment" style="margin: 10px 0 0 0;">';
   734     echo '<div class="mdg-comment" style="margin: 10px 0 0 0;">';
   735     if ( $session->user_level >= USER_LEVEL_ADMIN )
   735     echo '<div style="float: right;">';
   736     {
   736     echo '(<a href="#" onclick="ajaxCatToTag(); return false;">show page tags</a>)';
   737       echo '<div style="float: right;">';
   737     echo '</div>';
   738       echo '(<a href="#" onclick="ajaxCatToTag(); return false;">show page tags</a>)';
       
   739       echo '</div>';
       
   740     }
       
   741     echo '<div id="mdgCatBox">Categories: ';
   738     echo '<div id="mdgCatBox">Categories: ';
   742     
   739     
   743     $where = '( c.page_id=\'' . $db->escape($paths->cpage['urlname_nons']) . '\' AND c.namespace=\'' . $db->escape($paths->namespace) . '\' )';
   740     $where = '( c.page_id=\'' . $db->escape($paths->cpage['urlname_nons']) . '\' AND c.namespace=\'' . $db->escape($paths->namespace) . '\' )';
   744     $prefix = table_prefix;
   741     $prefix = table_prefix;
   745     $sql = <<<EOF
   742     $sql = <<<EOF
  2654     }
  2651     }
  2655   }
  2652   }
  2656   return $array;
  2653   return $array;
  2657 }
  2654 }
  2658 
  2655 
       
  2656 /**
       
  2657  * Sanitizes a page tag.
       
  2658  * @param string
       
  2659  * @return string
       
  2660  */
       
  2661 
       
  2662 function sanitize_tag($tag)
       
  2663 {
       
  2664   $tag = strtolower($tag);
       
  2665   $tag = preg_replace('/[^\w _-]+/', '', $tag);
       
  2666   $tag = trim($tag);
       
  2667   return $tag;
       
  2668 }
       
  2669 
       
  2670 /**
       
  2671  * Gzips the output buffer.
       
  2672  */
       
  2673 
       
  2674 function gzip_output()
       
  2675 {
       
  2676   global $do_gzip;
       
  2677   
       
  2678   //
       
  2679   // Compress buffered output if required and send to browser
       
  2680   //
       
  2681   if ( $do_gzip && function_exists('ob_gzhandler') )
       
  2682   {
       
  2683     //
       
  2684     // Copied from phpBB, which was in turn borrowed from php.net
       
  2685     //
       
  2686     $gzip_contents = ob_get_contents();
       
  2687     ob_end_clean();
       
  2688     
       
  2689     header('Content-encoding: gzip');
       
  2690     $gzip_contents = ob_gzhandler($gzip_contents);
       
  2691     echo $gzip_contents;
       
  2692   }
       
  2693 }
       
  2694 
       
  2695 /**
       
  2696  * Aggressively and hopefully non-destructively optimizes a blob of HTML.
       
  2697  * @param string HTML to process
       
  2698  * @return string much snaller HTML
       
  2699  */
       
  2700 
       
  2701 function aggressive_optimize_html($html)
       
  2702 {
       
  2703   $size_before = strlen($html);
       
  2704   
       
  2705   // kill carriage returns
       
  2706   $html = str_replace("\r", "", $html);
       
  2707   
       
  2708   // Optimize (but don't obfuscate) Javascript
       
  2709   preg_match_all('/<script(.*?)>(.+?)<\/script>/is', $html, $jscript);
       
  2710   
       
  2711   // list of Javascript reserved words - from about.com
       
  2712   $reserved_words = array('abstract', 'as', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class', 'continue', 'const', 'debugger', 'default', 'delete', 'do',
       
  2713                           'double', 'else', 'enum', 'export', 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'goto', 'if', 'implements', 'import',
       
  2714                           'in', 'instanceof', 'int', 'interface', 'is', 'long', 'namespace', 'native', 'new', 'null', 'package', 'private', 'protected', 'public',
       
  2715                           'return', 'short', 'static', 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try', 'typeof', 'use', 'var',
       
  2716                           'void', 'volatile', 'while', 'with');
       
  2717   
       
  2718   $reserved_words = '(' . implode('|', $reserved_words) . ')';
       
  2719   
       
  2720   for ( $i = 0; $i < count($jscript[0]); $i++ )
       
  2721   {
       
  2722     $js =& $jscript[2][$i];
       
  2723     
       
  2724     // for line optimization, explode it
       
  2725     $particles = explode("\n", $js);
       
  2726     
       
  2727     foreach ( $particles as $j => $atom )
       
  2728     {
       
  2729       // Remove comments
       
  2730       $atom = preg_replace('#\/\/(.+)#i', '', $atom);
       
  2731       
       
  2732       $atom = trim($atom);
       
  2733       if ( empty($atom) )
       
  2734         unset($particles[$j]);
       
  2735       else
       
  2736         $particles[$j] = $atom;
       
  2737     }
       
  2738     
       
  2739     $js = implode("\n", $particles);
       
  2740     
       
  2741     $js = preg_replace('#/\*(.*?)\*/#s', '', $js);
       
  2742     
       
  2743     // find all semicolons and then linebreaks, and replace with a single semicolon
       
  2744     $js = str_replace(";\n", ';', $js);
       
  2745     
       
  2746     // starting braces
       
  2747     $js = preg_replace('/\{([\s]+)/m', '{', $js);
       
  2748     $js = str_replace(")\n{", '){', $js);
       
  2749     
       
  2750     // ending braces (tricky)
       
  2751     $js = preg_replace('/\}([^;])/m', '};\\1', $js);
       
  2752     
       
  2753     // other rules
       
  2754     $js = str_replace("};\n", "};", $js);
       
  2755     $js = str_replace(",\n", ',', $js);
       
  2756     $js = str_replace("[\n", '[', $js);
       
  2757     $js = str_replace("]\n", ']', $js);
       
  2758     $js = str_replace("\n}", '}', $js);
       
  2759     
       
  2760     // newlines immediately before reserved words
       
  2761     $js = preg_replace("/(\)|;)\n$reserved_words/is", '\\1\\2', $js);
       
  2762     
       
  2763     // fix for firefox issue
       
  2764     $js = preg_replace('/\};([\s]*)(else|\))/i', '}\\2', $js);
       
  2765     
       
  2766     // apply changes
       
  2767     $html = str_replace($jscript[0][$i], "<script{$jscript[1][$i]}>$js</script>", $html);
       
  2768   }
       
  2769   
       
  2770   // Which tags to strip - you can change this if needed
       
  2771   $strip_tags = Array('pre', 'script', 'style', 'enano:no-opt');
       
  2772   $strip_tags = implode('|', $strip_tags);
       
  2773   
       
  2774   // Strip out the tags and replace with placeholders
       
  2775   preg_match_all("#<($strip_tags)(.*?)>(.*?)</($strip_tags)>#is", $html, $matches);
       
  2776   $seed = md5(microtime() . mt_rand()); // Random value used for placeholders
       
  2777   for ($i = 0;$i < sizeof($matches[1]); $i++)
       
  2778   {
       
  2779     $html = str_replace($matches[0][$i], "{DONT_STRIP_ME_NAKED:$seed:$i}", $html);
       
  2780   }
       
  2781   
       
  2782   // Finally, process the HTML
       
  2783   $html = preg_replace("#\n([ ]*)#", " ", $html);
       
  2784   
       
  2785   // Remove annoying spaces between tags
       
  2786   $html = preg_replace("#>([ ][ ]+)<#", "> <", $html);
       
  2787   
       
  2788   // Re-insert untouchable tags
       
  2789   for ($i = 0;$i < sizeof($matches[1]); $i++)
       
  2790   {
       
  2791     $html = str_replace("{DONT_STRIP_ME_NAKED:$seed:$i}", "<{$matches[1][$i]}{$matches[2][$i]}>{$matches[3][$i]}</{$matches[4][$i]}>", $html);
       
  2792   }
       
  2793   
       
  2794   // Remove <enano:no-opt> blocks (can be used by themes that don't want their HTML optimized)
       
  2795   $html = preg_replace('#<(\/|)enano:no-opt(.*?)>#', '', $html);
       
  2796   
       
  2797   $size_after = strlen($html);
       
  2798   
       
  2799   // Tell snoopish users what's going on
       
  2800   $html = str_replace('<html>', "\n".'<!-- NOTE: Enano has performed an HTML optimization routine on the HTML you see here. This is to enhance page loading speeds.
       
  2801      To view the uncompressed source of this page, add the "nocompress" parameter to the URI of this page: index.php?title=Main_Page&nocompress or Main_Page?nocompress'."
       
  2802      Size before compression: $size_before bytes
       
  2803      Size after compression:  $size_after bytes
       
  2804      -->\n<html>", $html);
       
  2805   return $html;
       
  2806 }
       
  2807 
  2659 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2808 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2660 
  2809 
  2661 ?>
  2810 ?>