includes/pageprocess.php
changeset 21 663fcf528726
parent 16 64e0d3d4cf14
child 22 d0314575e2f0
equal deleted inserted replaced
20:40105681f495 21:663fcf528726
     1 <?php
     1 <?php
     2 /*
     2 /*
     3  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     3  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     4  * Version 1.0 release candidate 3 (Druid)
     4  * Version 1.0 (Banshee)
     5  * pageprocess.php - intelligent retrieval of pages
     5  * pageprocess.php - intelligent retrieval of pages
     6  * Copyright (C) 2006-2007 Dan Fuhry
     6  * Copyright (C) 2006-2007 Dan Fuhry
     7  *
     7  *
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    30   
    30   
    31   var $page_id;
    31   var $page_id;
    32   var $namespace;
    32   var $namespace;
    33   
    33   
    34   /**
    34   /**
       
    35    * The revision ID (history entry) to send. If set to 0 (the default) then the most recent revision will be sent.
       
    36    * @var int
       
    37    */
       
    38   
       
    39   var $revision_id = 0;
       
    40   
       
    41   /**
    35    * Unsanitized page ID.
    42    * Unsanitized page ID.
    36    * @var string
    43    * @var string
    37    */
    44    */
    38   
    45   
    39   var $page_id_unclean;
    46   var $page_id_unclean;
    77    * Debugging information to track errors. You can set enable to false to disable sending debug information.
    84    * Debugging information to track errors. You can set enable to false to disable sending debug information.
    78    * @var array
    85    * @var array
    79    */
    86    */
    80   
    87   
    81   var $debug = array(
    88   var $debug = array(
    82       'enable' => true,
    89       'enable' => false,
    83       'works'  => false
    90       'works'  => false
    84     );
    91     );
    85   
    92   
    86   /**
    93   /**
    87    * Constructor.
    94    * Constructor.
    88    * @param string The page ID (urlname) of the page
    95    * @param string The page ID (urlname) of the page
    89    * @param string The namespace of the page
    96    * @param string The namespace of the page
    90    */
    97    * @param int Optional. The revision ID to send.
    91   
    98    */
    92   function __construct( $page_id, $namespace )
    99   
       
   100   function __construct( $page_id, $namespace, $revision_id = 0 )
    93   {
   101   {
    94     global $db, $session, $paths, $template, $plugins; // Common objects
   102     global $db, $session, $paths, $template, $plugins; // Common objects
    95     
   103     
    96     // See if we can get some debug info
   104     // See if we can get some debug info
    97     if ( function_exists('debug_backtrace') && $this->debug['enable'] )
   105     if ( function_exists('debug_backtrace') && $this->debug['enable'] )
   105     if ( !isset($paths->nslist[$namespace]) )
   113     if ( !isset($paths->nslist[$namespace]) )
   106     {
   114     {
   107       $this->send_error('The namespace "' . htmlspecialchars($namespace) . '" does not exist.');
   115       $this->send_error('The namespace "' . htmlspecialchars($namespace) . '" does not exist.');
   108     }
   116     }
   109     
   117     
   110     $this->_setup( $page_id, $namespace );
   118     if ( !is_int($revision_id) )
       
   119       $revision_id = 0;
       
   120     
       
   121     $this->_setup( $page_id, $namespace, $revision_id );
   111     
   122     
   112   }
   123   }
   113   
   124   
   114   /**
   125   /**
   115    * The main method to send the page content. Also responsible for checking permissions.
   126    * The main method to send the page content. Also responsible for checking permissions.
   193       }
   204       }
   194     }
   205     }
   195     else // (disabled for compatibility reasons) if ( in_array($this->namespace, array('Article', 'User', 'Project', 'Help', 'File', 'Category')) && $this->page_exists )
   206     else // (disabled for compatibility reasons) if ( in_array($this->namespace, array('Article', 'User', 'Project', 'Help', 'File', 'Category')) && $this->page_exists )
   196     {
   207     {
   197       // Send as regular page
   208       // Send as regular page
       
   209       
       
   210       // die($this->page_id);
       
   211       
   198       $text = $this->fetch_text();
   212       $text = $this->fetch_text();
   199       if ( $text == 'err_no_text_rows' )
   213       if ( $text == 'err_no_text_rows' )
   200       {
   214       {
   201         $this->err_no_rows();
   215         $this->err_no_rows();
   202         return false;
   216         return false;
   211   /**
   225   /**
   212    * Sets internal variables.
   226    * Sets internal variables.
   213    * @access private
   227    * @access private
   214    */
   228    */
   215   
   229   
   216   function _setup($page_id, $namespace)
   230   function _setup($page_id, $namespace, $revision_id)
   217   {
   231   {
   218     global $db, $session, $paths, $template, $plugins; // Common objects
   232     global $db, $session, $paths, $template, $plugins; // Common objects
   219     
   233     
   220     $page_id_cleaned = sanitize_page_id($page_id);
   234     $page_id_cleaned = sanitize_page_id($page_id);
   221     
   235     
   222     $this->page_id = $page_id_cleaned;
   236     $this->page_id = $page_id_cleaned;
   223     $this->namespace = $namespace;
   237     $this->namespace = $namespace;
       
   238     $this->revision_id = $revision_id;
   224     $this->page_id_unclean = dirtify_page_id($page_id);
   239     $this->page_id_unclean = dirtify_page_id($page_id);
   225     
   240     
   226     $this->perms = $session->fetch_page_acl( $page_id, $namespace );
   241     $this->perms = $session->fetch_page_acl( $page_id, $namespace );
   227     
   242     
   228     // Exception for Admin: pages
   243     // Exception for Admin: pages
   242     }
   257     }
   243     else
   258     else
   244     {
   259     {
   245       $this->page_exists = true;
   260       $this->page_exists = true;
   246     }
   261     }
       
   262     
       
   263     // Compatibility with older databases
       
   264     if ( strstr($this->page_id, '.2e') && !$this->page_exists )
       
   265     {
       
   266       $page_id = str_replace('.2e', '.', $page_id);
       
   267       
       
   268       if ( $paths->cpage['urlname_nons'] == $page_id && $paths->namespace == $namespace && !$paths->page_exists && ( $this->namespace != 'Admin' || ($this->namespace == 'Admin' && !function_exists($fname) ) ) )
       
   269       {
       
   270         $this->page_exists = false;
       
   271       }
       
   272       else if ( !isset( $paths->pages[ $paths->nslist[$namespace] . $page_id ] ) && ( $this->namespace == 'Admin' && !function_exists($fname) ) )
       
   273       {
       
   274         $this->page_exists = false;
       
   275       }
       
   276       else
       
   277       {
       
   278         $this->page_exists = true;
       
   279       }
       
   280       
       
   281     }
       
   282     
   247   }
   283   }
   248   
   284   
   249   /**
   285   /**
   250    * Renders it all in one go, and echoes it out. This assumes that the text is in the DB.
   286    * Renders it all in one go, and echoes it out. This assumes that the text is in the DB.
   251    * @access private
   287    * @access private
   254   function render()
   290   function render()
   255   {
   291   {
   256     $text = $this->fetch_text();
   292     $text = $this->fetch_text();
   257     
   293     
   258     $this->header();
   294     $this->header();
   259     if ( $this->send_headers )
   295     // if ( $this->send_headers )
   260     {
   296     // {
   261       display_page_headers();
   297       display_page_headers();
       
   298     // }
       
   299     
       
   300     if ( $this->revision_id )
       
   301     {
       
   302       echo '<div class="info-box" style="margin-left: 0; margin-top: 5px;"><b>Notice:</b><br />The page you are viewing was archived on '.date('F d, Y \a\t h:i a', $this->revision_id).'.<br /><a href="'.makeUrlNS($this->namespace, $this->page_id).'" onclick="ajaxReset(); return false;">View current version</a>  |  <a href="'.makeUrlNS($this->namespace, $this->pageid, 'do=rollback&amp;id='.$this->revision_id).'" onclick="ajaxRollback(\''.$this->revision_id.'\')">Restore this version</a></div><br />';
   262     }
   303     }
   263     
   304     
   264     $text = '?>' . RenderMan::render($text);
   305     $text = '?>' . RenderMan::render($text);
   265     // echo('<pre>'.htmlspecialchars($text).'</pre>');
   306     // echo('<pre>'.htmlspecialchars($text).'</pre>');
   266     eval ( $text );
   307     eval ( $text );
   267     
   308     
   268     if ( $this->send_headers )
   309     // if ( $this->send_headers )
   269     {
   310     // {
   270       display_page_footers();
   311       display_page_footers();
   271     }
   312     // }
   272     
   313     
   273     $this->footer();
   314     $this->footer();
   274   }
   315   }
   275   
   316   
   276   /**
   317   /**
   307     if ( !empty($this->text_cache) )
   348     if ( !empty($this->text_cache) )
   308     {
   349     {
   309       return $this->text_cache;
   350       return $this->text_cache;
   310     }
   351     }
   311     
   352     
   312     $q = $db->sql_query('SELECT page_text, char_tag FROM '.table_prefix.'page_text WHERE page_id=\'' . $this->page_id . '\' AND namespace=\'' . $this->namespace . '\';');
   353     if ( $this->revision_id > 0 && is_int($this->revision_id) )
   313     if ( !$q )
   354     {
   314     {
   355     
   315       $this->send_error('Error during SQL query.', true);
   356       $q = $db->sql_query('SELECT page_text, char_tag, date_string FROM '.table_prefix.'logs WHERE page_id=\'' . $this->page_id . '\' AND namespace=\'' . $this->namespace . '\' AND time_id=' . $this->revision_id . ';');
   316     }
   357       if ( !$q )
   317     if ( $db->numrows() < 1 )
   358       {
   318     {
   359         $this->send_error('Error during SQL query.', true);
   319       $this->page_exists = false;
   360       }
   320       return 'err_no_text_rows';
   361       if ( $db->numrows() < 1 )
   321     }
   362       {
   322     
   363         // Compatibility fix for old pages with dots in the page ID
   323     $row = $db->fetchrow();
   364         if ( strstr($this->page_id, '.2e') )
   324     $db->free_result();
   365         {
       
   366           $db->free_result();
       
   367           $page_id = str_replace('.2e', '.', $this->page_id);
       
   368           $q = $db->sql_query('SELECT page_text, char_tag, date_string FROM '.table_prefix.'logs WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $this->namespace . '\' AND time_id=' . $this->revision_id . ';');
       
   369           if ( !$q )
       
   370           {
       
   371             $this->send_error('Error during SQL query.', true);
       
   372           }
       
   373           if ( $db->numrows() < 1 )
       
   374           {
       
   375             $this->page_exists = false;
       
   376             return 'err_no_text_rows';
       
   377           }
       
   378         }
       
   379         else
       
   380         {
       
   381           $this->page_exists = false;
       
   382           return 'err_no_text_rows';
       
   383         }
       
   384       }
       
   385       else
       
   386       {
       
   387         $row = $db->fetchrow();
       
   388       }
       
   389       
       
   390       $db->free_result();
       
   391       
       
   392     }
       
   393     else
       
   394     {
       
   395       
       
   396       $q = $db->sql_query('SELECT page_text, char_tag FROM '.table_prefix.'page_text WHERE page_id=\'' . $this->page_id . '\' AND namespace=\'' . $this->namespace . '\';');
       
   397       if ( !$q )
       
   398       {
       
   399         $this->send_error('Error during SQL query.', true);
       
   400       }
       
   401       if ( $db->numrows() < 1 )
       
   402       {
       
   403         // Compatibility fix for old pages with dots in the page ID
       
   404         if ( strstr($this->page_id, '.2e') )
       
   405         {
       
   406           $db->free_result();
       
   407           $page_id = str_replace('.2e', '.', $this->page_id);
       
   408           $q = $db->sql_query('SELECT page_text, char_tag FROM '.table_prefix.'page_text WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $this->namespace . '\';');
       
   409           if ( !$q )
       
   410           {
       
   411             $this->send_error('Error during SQL query.', true);
       
   412           }
       
   413           if ( $db->numrows() < 1 )
       
   414           {
       
   415             $this->page_exists = false;
       
   416             return 'err_no_text_rows';
       
   417           }
       
   418         }
       
   419         else
       
   420         {
       
   421           $this->page_exists = false;
       
   422           return 'err_no_text_rows';
       
   423         }
       
   424       }
       
   425       
       
   426       $row = $db->fetchrow();
       
   427       $db->free_result();
       
   428       
       
   429     }
   325     
   430     
   326     if ( !empty($row['char_tag']) )
   431     if ( !empty($row['char_tag']) )
   327     {
   432     {
   328       // This page text entry uses the old text-escaping format
   433       // This page text entry uses the old text-escaping format
   329       $from = array(
   434       $from = array(
   378     
   483     
   379     $template->tpl_strings['PAGE_NAME'] = htmlspecialchars($page_name);
   484     $template->tpl_strings['PAGE_NAME'] = htmlspecialchars($page_name);
   380     
   485     
   381     $this->header();
   486     $this->header();
   382     
   487     
   383     if ( $send_headers )
   488     // if ( $send_headers )
   384     {
   489     // {
   385       display_page_headers();
   490       display_page_headers();
   386     }
   491     // }
   387    
   492    
   388     /*
       
   389     // Start left sidebar: basic user info, latest comments
   493     // Start left sidebar: basic user info, latest comments
   390     
   494     
   391     echo '<table border="0" cellspacing="4" cellpadding="0" style="width: 100%;">';
   495     echo '<table border="0" cellspacing="4" cellpadding="0" style="width: 100%;">';
   392     echo '<tr><td style="width: 150px;">';
   496     echo '<tr><td style="width: 150px;">';
   393     
   497     
   398             
   502             
   399     echo '  </table>
   503     echo '  </table>
   400           </div>';
   504           </div>';
   401     
   505     
   402     echo '</td><td>';
   506     echo '</td><td>';
   403     */
       
   404     
   507     
   405     // User's own content
   508     // User's own content
   406     
   509     
   407     $send_headers = $this->send_headers;
   510     $send_headers = $this->send_headers;
   408     $this->send_headers = false;
   511     $this->send_headers = false;
   413     }
   516     }
   414     else
   517     else
   415     {
   518     {
   416       $this->err_page_not_existent();
   519       $this->err_page_not_existent();
   417     }
   520     }
   418     
       
   419     /*
       
   420     
   521     
   421     // Right sidebar
   522     // Right sidebar
   422     
   523     
   423     echo '</td><td style="width: 150px;">';
   524     echo '</td><td style="width: 150px;">';
   424     
   525     
   430     echo '  </table>
   531     echo '  </table>
   431           </div>';
   532           </div>';
   432           
   533           
   433     echo '</tr></table>';
   534     echo '</tr></table>';
   434     
   535     
   435     if ( $send_headers )
   536     // if ( $send_headers )
   436     {
   537     // {
   437       display_page_footers();
   538       display_page_footers();
   438     }
   539     // }
   439     
       
   440     */
       
   441     
   540     
   442     $this->send_headers = $send_headers;
   541     $this->send_headers = $send_headers;
   443     unset($send_headers);
   542     unset($send_headers);
   444     
   543     
   445     $this->footer();
   544     $this->footer();
   541   /**
   640   /**
   542    * PHP 4 constructor.
   641    * PHP 4 constructor.
   543    * @see PageProcessor::__construct()
   642    * @see PageProcessor::__construct()
   544    */
   643    */
   545   
   644   
   546   function PageProcessor( $page_id, $namespace )
   645   function PageProcessor( $page_id, $namespace, $revision_id = 0 )
   547   {
   646   {
   548     $this->__construct($page_id, $namespace);
   647     $this->__construct($page_id, $namespace, $revision_id);
   549   }
   648   }
   550   
   649   
   551   /**
   650   /**
   552    * Send an error message and die
   651    * Send an error message and die. For debugging or critical technical errors only - nothing that would under normal circumstances be shown to the user.
   553    * @var string Error message
   652    * @var string Error message
   554    * @var bool If true, send DBAL's debugging information as well
   653    * @var bool If true, send DBAL's debugging information as well
   555    */
   654    */
   556    
   655    
   557   function send_error($message, $sql = false)
   656   function send_error($message, $sql = false)