includes/pageprocess.php
changeset 322 5f1cd51bf1be
parent 320 112debff64bd
child 325 e17cc42d77cf
equal deleted inserted replaced
320:112debff64bd 322:5f1cd51bf1be
     1 <?php
     1 <?php
     2 
     2 
     3 /*
     3 /*
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
     5  * Version 1.1.1
     5  * Version 1.0.3 (Dyrad)
     6  * pageprocess.php - intelligent retrieval of pages
     6  * pageprocess.php - intelligent retrieval of pages
     7  * Copyright (C) 2006-2007 Dan Fuhry
     7  * Copyright (C) 2006-2007 Dan Fuhry
     8  *
     8  *
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    14  */
    14  */
    15 
    15 
    16 /**
    16 /**
    17  * Class to handle fetching page text (possibly from a cache) and formatting it.
    17  * Class to handle fetching page text (possibly from a cache) and formatting it.
       
    18  * As of 1.0.4, this also handles the fetching and editing of certain data for pages.
    18  * @package Enano
    19  * @package Enano
    19  * @subpackage UI
    20  * @subpackage UI
    20  * @copyright 2007 Dan Fuhry
    21  * @copyright 2007 Dan Fuhry
    21  * @license GNU General Public License <http://www.gnu.org/licenses/gpl.html>
    22  * @license GNU General Public License <http://www.gnu.org/licenses/gpl.html>
    22  */
    23  */
   109   
   110   
   110   var $debug = array(
   111   var $debug = array(
   111       'enable' => false,
   112       'enable' => false,
   112       'works'  => false
   113       'works'  => false
   113     );
   114     );
       
   115   
       
   116   /**
       
   117    * The list of errors raised in the class.
       
   118    * @var array
       
   119    */
       
   120   
       
   121   var $_errors = array();
   114   
   122   
   115   /**
   123   /**
   116    * Constructor.
   124    * Constructor.
   117    * @param string The page ID (urlname) of the page
   125    * @param string The page ID (urlname) of the page
   118    * @param string The namespace of the page
   126    * @param string The namespace of the page
   310       }
   318       }
   311     }
   319     }
   312   }
   320   }
   313   
   321   
   314   /**
   322   /**
       
   323    * Fetches the wikitext or HTML source for the page.
       
   324    * @return string
       
   325    */
       
   326   
       
   327   function fetch_source()
       
   328   {
       
   329     if ( !$this->perms->get_permissions('view_source') )
       
   330     {
       
   331       return false;
       
   332     }
       
   333     return $this->fetch_text();
       
   334   }
       
   335   
       
   336   /**
       
   337    * Updates the content of the page.
       
   338    * @param string The new text for the page
       
   339    * @param string A summary of edits made to the page.
       
   340    * @return bool True on success, false on failure
       
   341    */
       
   342   
       
   343   function update_page($text, $edit_summary = false)
       
   344   {
       
   345     global $db, $session, $paths, $template, $plugins; // Common objects
       
   346     
       
   347     // Create the page if it doesn't exist
       
   348     if ( !$this->page_exists )
       
   349     {
       
   350       if ( !$this->create_page() )
       
   351       {
       
   352         return false;
       
   353       }
       
   354     }
       
   355       
       
   356     //
       
   357     // Validation
       
   358     //
       
   359     
       
   360     $page_id = $db->escape($this->page_id);
       
   361     $namespace = $db->escape($this->namespace);
       
   362     
       
   363     $q = $db->sql_query('SELECT protected FROM ' . table_prefix . "pages WHERE urlname='$page_id' AND namespace='$namespace';");
       
   364     if ( !$q )
       
   365       $db->_die('PageProcess updating page content');
       
   366     if ( $db->numrows() < 1 )
       
   367     {
       
   368       $this->raise_error('Page doesn\'t exist in the database');
       
   369       return false;
       
   370     }
       
   371     
       
   372     // Do we have permission to edit the page?
       
   373     if ( !$this->perms->get_permissions('edit_page') )
       
   374     {
       
   375       $this->raise_error('You do not have permission to edit this page.');
       
   376       return false;
       
   377     }
       
   378     
       
   379     list($protection) = $db->fetchrow_num();
       
   380     $db->free_result();
       
   381     
       
   382     if ( $protection == 1 )
       
   383     {
       
   384       // The page is protected - do we have permission to edit protected pages?
       
   385       if ( !$this->perms->get_permissions('even_when_protected') )
       
   386       {
       
   387         $this->raise_error('This page is protected, and you do not have permission to edit protected pages.');
       
   388         return false;
       
   389       }
       
   390     }
       
   391     else if ( $protection == 2 )
       
   392     {
       
   393       // The page is semi-protected.
       
   394       if (
       
   395            ( !$session->user_logged_in || // Is the user logged in?
       
   396              ( $session->user_logged_in && $session->reg_time + ( 4 * 86400 ) >= time() ) ) // If so, have they been registered for 4 days?
       
   397            && !$this->perms->get_permissions('even_when_protected') ) // And of course, is there an ACL that overrides semi-protection?
       
   398       {
       
   399         $this->raise_error('This page is protected, and you do not have permission to edit protected pages.');
       
   400         return false;
       
   401       }
       
   402     }
       
   403     
       
   404     // Protection validated
       
   405     
       
   406   }
       
   407   
       
   408   /**
       
   409    * Creates the page if it doesn't already exist.
       
   410    * @return bool True on success, false on failure.
       
   411    */
       
   412   
       
   413   function create_page()
       
   414   {
       
   415     global $db, $session, $paths, $template, $plugins; // Common objects
       
   416     
       
   417     // Do we have permission to create the page?
       
   418     if ( !$this->perms->get_permissions('create_page') )
       
   419     {
       
   420       $this->raise_error('You do not have permission to create this page.');
       
   421       return false;
       
   422     }
       
   423     
       
   424     // Does it already exist?
       
   425     if ( $this->page_exists )
       
   426     {
       
   427       $this->raise_error('The page already exists.');
       
   428       return false;
       
   429     }
       
   430     
       
   431     // It's not in there. Perform validation.
       
   432     
       
   433     // We can't create special, admin, or external pages.
       
   434     if ( $this->namespace == 'Special' || $this->namespace == 'Admin' || $this->namespace == 'Anonymous' )
       
   435     {
       
   436       $this->raise_error('You cannot create Special or Admin pages - they can\'t be stored in the database.');
       
   437       return false;
       
   438     }
       
   439     
       
   440     // Guess the proper title
       
   441     $name = dirtify_page_id($this->page_id);
       
   442     
       
   443     // Check for the restricted Project: prefix
       
   444     if ( substr($this->page_id, 0, 8) == 'Project:' )
       
   445     {
       
   446       $this->raise_error('The prefix "Project:" is reserved for internal links and can\'t be used on a page name.');
       
   447       return false;
       
   448     }
       
   449     
       
   450     // Validation successful - insert the page
       
   451     
       
   452     $metadata = array(
       
   453         'urlname' => $this->page_id,
       
   454         'namespace' => $this->namespace,
       
   455         'name' => $name,
       
   456         'special' => 0,
       
   457         'visible' => 1,
       
   458         'comments_on' => 1,
       
   459         'protected' => ( $this->namespace == 'System' ? 1 : 0 ),
       
   460         'delvotes' => 0,
       
   461         'delvote_ips' => serialize(array()),
       
   462         'wiki_mode' => 2
       
   463       );
       
   464     
       
   465     $paths->add_page($metadata);
       
   466     
       
   467     $page_id = $db->escape($this->page_id);
       
   468     $namespace = $db->escape($this->namespace);
       
   469     $name = $db->escape($name);
       
   470     $protect = ( $this->namespace == 'System' ) ? '1' : '0';
       
   471     $blank_array = $db->escape(serialize(array()));
       
   472     
       
   473     // Query 1: Metadata entry
       
   474     $q = $db->sql_query('INSERT INTO ' . table_prefix . "pages(name, urlname, namespace, protected, delvotes, delvote_ips, wiki_mode)\n"
       
   475                         . "VALUES ( '$name', '$page_id', '$namespace', $protect, 0, '$blank_array', 2 );");
       
   476     if ( !$q )
       
   477       $db->_die('PageProcessor page creation - metadata stage');
       
   478     
       
   479     // Query 2: Text insertion
       
   480     $q = $db->sql_query('INSERT INTO ' . table_prefix . "page_text(page_id, namespace, page_text)\n"
       
   481                         . "VALUES ( '$page_id', '$namespace', '' );");
       
   482     if ( !$q )
       
   483       $db->_die('PageProcessor page creation - text stage');
       
   484     
       
   485     // Page created. We're good!
       
   486     return true;
       
   487   }
       
   488   
       
   489   /**
   315    * Sets internal variables.
   490    * Sets internal variables.
   316    * @access private
   491    * @access private
   317    */
   492    */
   318   
   493   
   319   function _setup($page_id, $namespace, $revision_id)
   494   function _setup($page_id, $namespace, $revision_id)
   334     {
   509     {
   335       $fname = "page_Admin_{$this->page_id}";
   510       $fname = "page_Admin_{$this->page_id}";
   336     }
   511     }
   337     
   512     
   338     // Does the page "exist"?
   513     // Does the page "exist"?
   339     if ( $paths->cpage['urlname_nons'] == $page_id && $paths->namespace == $namespace && !$paths->page_exists && ( $this->namespace != 'Admin' || ($this->namespace == 'Admin' && !function_exists($fname) ) ) )
   514     if ( $paths->page_id == $page_id && $paths->namespace == $namespace && !$paths->page_exists && ( $this->namespace != 'Admin' || ($this->namespace == 'Admin' && !function_exists($fname) ) ) )
   340     {
   515     {
   341       $this->page_exists = false;
   516       $this->page_exists = false;
   342     }
   517     }
   343     else if ( !isset( $paths->pages[ $paths->nslist[$namespace] . $page_id ] ) && ( $this->namespace == 'Admin' && !function_exists($fname) ) )
   518     else if ( !isset( $paths->pages[ $paths->nslist[$namespace] . $page_id ] ) && ( $this->namespace == 'Admin' && !function_exists($fname) ) )
   344     {
   519     {
   352     // Compatibility with older databases
   527     // Compatibility with older databases
   353     if ( strstr($this->page_id, '.2e') && !$this->page_exists )
   528     if ( strstr($this->page_id, '.2e') && !$this->page_exists )
   354     {
   529     {
   355       $page_id = str_replace('.2e', '.', $page_id);
   530       $page_id = str_replace('.2e', '.', $page_id);
   356       
   531       
   357       if ( $paths->cpage['urlname_nons'] == $page_id && $paths->namespace == $namespace && !$paths->page_exists && ( $this->namespace != 'Admin' || ($this->namespace == 'Admin' && !function_exists($fname) ) ) )
   532       if ( $paths->page_id == $page_id && $paths->namespace == $namespace && !$paths->page_exists && ( $this->namespace != 'Admin' || ($this->namespace == 'Admin' && !function_exists($fname) ) ) )
   358       {
   533       {
   359         $this->page_exists = false;
   534         $this->page_exists = false;
   360       }
   535       }
   361       else if ( !isset( $paths->pages[ $paths->nslist[$namespace] . $page_id ] ) && ( $this->namespace == 'Admin' && !function_exists($fname) ) )
   536       else if ( !isset( $paths->pages[ $paths->nslist[$namespace] . $page_id ] ) && ( $this->namespace == 'Admin' && !function_exists($fname) ) )
   362       {
   537       {
   628   {
   803   {
   629     global $db, $session, $paths, $template, $plugins; // Common objects
   804     global $db, $session, $paths, $template, $plugins; // Common objects
   630     global $email;
   805     global $email;
   631     
   806     
   632     $page_urlname = dirtify_page_id($this->page_id);
   807     $page_urlname = dirtify_page_id($this->page_id);
   633     if ( $this->page_id == $paths->cpage['urlname_nons'] && $this->namespace == $paths->namespace )
   808     if ( $this->page_id == $paths->page_id && $this->namespace == $paths->namespace )
   634     {
   809     {
   635       $page_name = ( isset($paths->cpage['name']) ) ? $paths->cpage['name'] : $this->page_id;
   810       $page_name = ( isset($paths->cpage['name']) ) ? $paths->cpage['name'] : $this->page_id;
   636     }
   811     }
   637     else
   812     else
   638     {
   813     {
  1244     
  1419     
  1245     exit;
  1420     exit;
  1246     
  1421     
  1247   }
  1422   }
  1248   
  1423   
       
  1424   /**
       
  1425    * Raises an error.
       
  1426    * @param string Error string
       
  1427    */
       
  1428    
       
  1429   function raise_error($string)
       
  1430   {
       
  1431     if ( !is_string($string) )
       
  1432       return false;
       
  1433     $this->_errors[] = $string;
       
  1434   }
       
  1435   
       
  1436   /**
       
  1437    * Retrieves the latest error from the error stack and returns it ('pops' the error stack)
       
  1438    * @return string
       
  1439    */
       
  1440   
       
  1441   function pop_error()
       
  1442   {
       
  1443     if ( count($this->_errors) < 1 )
       
  1444       return false;
       
  1445     return array_pop($this->_errors);
       
  1446   }
       
  1447   
  1249 } // class PageProcessor
  1448 } // class PageProcessor
  1250 
  1449 
  1251 ?>
  1450 ?>