index.php
changeset 41 7c7920b65f42
parent 33 143d3ee32f3c
child 42 45ebe475ff75
equal deleted inserted replaced
40:723bb7acf914 41:7c7920b65f42
     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>');
   127         <br />
   127         <br />
   128         <textarea name="page_text" rows="20" cols="60" style="width: 97%;">'.$text.'</textarea><br />
   128         <textarea name="page_text" rows="20" cols="60" style="width: 97%;">'.$text.'</textarea><br />
   129         <br />
   129         <br />
   130         ';
   130         ';
   131       if($paths->wiki_mode)
   131       if($paths->wiki_mode)
   132         echo 'Edit summary: <input name="edit_summary" type="text" size="40" /><br /><label><input type="checkbox" name="minor" /> This is a minor edit</label><br />';  
   132         echo 'Edit summary: <input name="edit_summary" type="text" size="40" /><br /><label><input type="checkbox" name="minor" /> This is a minor edit</label><br />';
   133       echo '<br />
   133       echo '<br />
   134           <input type="submit" name="_save" value="Save changes" style="font-weight: bold;" />
   134           <input type="submit" name="_save" value="Save changes" style="font-weight: bold;" />
   135           <input type="submit" name="_preview" value="Preview changes" />
   135           <input type="submit" name="_preview" value="Preview changes" />
   136           <input type="submit" name="_revert" value="Revert changes" />
   136           <input type="submit" name="_revert" value="Revert changes" />
   137           <input type="submit" name="_cancel" value="Cancel" />
   137           <input type="submit" name="_cancel" value="Cancel" />
   221                  break;
   221                  break;
   222                default:
   222                default:
   223                  echo 'None;</b> Warning: request validation will fail after clicking submit<b>';
   223                  echo 'None;</b> Warning: request validation will fail after clicking submit<b>';
   224              }
   224              }
   225            ?></b></p>
   225            ?></b></p>
   226         <p><input type="submit" value="Protect page" style="font-weight: bold;" /></p> 
   226         <p><input type="submit" value="Protect page" style="font-weight: bold;" /></p>
   227       </form>
   227       </form>
   228       <?php
   228       <?php
   229       $template->footer();
   229       $template->footer();
   230       break;
   230       break;
   231     case 'rename':
   231     case 'rename':
   238       ?>
   238       ?>
   239       <form action="<?php echo makeUrl($paths->page, 'do=rename'); ?>" method="post">
   239       <form action="<?php echo makeUrl($paths->page, 'do=rename'); ?>" method="post">
   240         <?php if(isset($_POST['newname'])) echo '<p style="color: red;">Error: you must enter a new name for this page.</p>'; ?>
   240         <?php if(isset($_POST['newname'])) echo '<p style="color: red;">Error: you must enter a new name for this page.</p>'; ?>
   241         <p>Please enter a new name for this page:</p>
   241         <p>Please enter a new name for this page:</p>
   242         <p><input type="text" name="newname" size="40" /></p>
   242         <p><input type="text" name="newname" size="40" /></p>
   243         <p><input type="submit" value="Rename page" style="font-weight: bold;" /></p> 
   243         <p><input type="submit" value="Rename page" style="font-weight: bold;" /></p>
   244       </form>
   244       </form>
   245       <?php
   245       <?php
   246       $template->footer();    
   246       $template->footer();
   247       break;
   247       break;
   248     case 'flushlogs':
   248     case 'flushlogs':
   249       if(!$session->get_permissions('clear_logs')) die_friendly('Access denied', '<p>Flushing the logs for a page <u>requires</u> administrative rights.</p>');
   249       if(!$session->get_permissions('clear_logs')) die_friendly('Access denied', '<p>Flushing the logs for a page <u>requires</u> administrative rights.</p>');
   250       if(isset($_POST['_downthejohn']))
   250       if(isset($_POST['_downthejohn']))
   251       {
   251       {
   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 ?>