includes/pageprocess.php
changeset 468 194a19711346
parent 458 c433348f3628
child 472 bc4b58034f4d
equal deleted inserted replaced
467:e4bbd6fb8df3 468:194a19711346
    53    */
    53    */
    54   
    54   
    55   var $revision_id = 0;
    55   var $revision_id = 0;
    56   
    56   
    57   /**
    57   /**
       
    58    * The time this revision was saved, as a UNIX timestamp
       
    59    * @var int
       
    60    */
       
    61   
       
    62   var $revision_time = 0;
       
    63   
       
    64   /**
    58    * Unsanitized page ID.
    65    * Unsanitized page ID.
    59    * @var string
    66    * @var string
    60    */
    67    */
    61   
    68   
    62   var $page_id_unclean;
    69   var $page_id_unclean;
   458     {
   465     {
   459       $this->raise_error($db->get_error());
   466       $this->raise_error($db->get_error());
   460       return false;
   467       return false;
   461     }
   468     }
   462     
   469     
       
   470     // If there's an identical draft copy, delete it
       
   471     $sql = 'DELETE FROM ' . table_prefix . "logs WHERE is_draft = 1 AND page_id = '{$this->page_id}' AND namespace = '{$this->namespace}' AND page_text = '{$text}';";
       
   472     if ( !$db->sql_query($sql) )
       
   473     {
       
   474       $this->raise_error($db->get_error());
       
   475       return false;
       
   476     }
       
   477     
   463     // Rebuild the search index
   478     // Rebuild the search index
   464     $paths->rebuild_page_index($this->page_id, $this->namespace);
   479     $paths->rebuild_page_index($this->page_id, $this->namespace);
   465     
   480     
   466     $this->text_cache = $text;
   481     $this->text_cache = $text;
   467     
   482     
   555     if ( !$q )
   570     if ( !$q )
   556       $db->_die('PageProcessor page creation - logging stage');
   571       $db->_die('PageProcessor page creation - logging stage');
   557     
   572     
   558     // Page created. We're good!
   573     // Page created. We're good!
   559     return true;
   574     return true;
       
   575   }
       
   576   
       
   577   /**
       
   578    * Rolls back a non-edit action in the logs
       
   579    * @param int Log entry (log_id) to roll back
       
   580    * @return array Standard Enano error/success protocol
       
   581    */
       
   582   
       
   583   function rollback_log_entry($log_id)
       
   584   {
       
   585     global $db, $session, $paths, $template, $plugins; // Common objects
       
   586     
       
   587     // Verify permissions
       
   588     if ( !$this->perms->get_permissions('history_rollback') )
       
   589     {
       
   590       return array(
       
   591         'success' => false,
       
   592         'error' => 'access_denied'
       
   593         );
       
   594     }
       
   595     
       
   596     // Check input
       
   597     $log_id = intval($log_id);
       
   598     if ( empty($log_id) )
       
   599     {
       
   600       return array(
       
   601         'success' => false,
       
   602         'error' => 'invalid_parameter'
       
   603         );
       
   604     }
       
   605     
       
   606     // Fetch the log entry
       
   607     $q = $db->sql_query('SELECT * FROM ' . table_prefix . "logs WHERE log_type = 'page' AND page_id='{$this->page_id}' AND namespace='{$this->namespace}' AND log_id = $log_id;");
       
   608     if ( !$q )
       
   609       $db->_die();
       
   610     
       
   611     // Is this even a valid log entry for this context?
       
   612     if ( $db->numrows() < 1 )
       
   613     {
       
   614       return array(
       
   615         'success' => false,
       
   616         'error' => 'entry_not_found'
       
   617         );
       
   618     }
       
   619     
       
   620     // All good, fetch and free the result
       
   621     $log_entry = $db->fetchrow();
       
   622     $db->free_result();
       
   623     
       
   624     // Let's see, what do we have here...
       
   625     switch ( $log_entry['action'] )
       
   626     {
       
   627       case 'rename':
       
   628         // Page was renamed, let the rename method handle this
       
   629         return $this->rename($log_entry['edit_summary']);
       
   630         break;
       
   631       case 'prot':
       
   632       case 'unprot':
       
   633       case 'semiprot':
       
   634         return $this->protect_page(intval($log_entry['page_text']), '__REVERSION__');
       
   635         break;
       
   636       default:
       
   637         break;
       
   638     }
       
   639   }
       
   640   
       
   641   /**
       
   642    * Renames the page
       
   643    * @param string New name
       
   644    * @return array Standard Enano error/success protocol
       
   645    */
       
   646   
       
   647   function rename_page($new_name)
       
   648   {
       
   649     global $db, $session, $paths, $template, $plugins; // Common objects
       
   650     
       
   651     // Check permissions
       
   652     if ( !$this->perms->get_permissions('rename') )
       
   653     {
       
   654       return array(
       
   655         'success' => false,
       
   656         'error' => 'access_denied'
       
   657         );
       
   658     }
       
   659     
       
   660     // If this is the same as the current name, return success
       
   661     $page_name = get_page_title_ns($this->page_id, $this->namespace);
       
   662     if ( $page_name === $new_name )
       
   663     {
       
   664       return array(
       
   665         'success' => true
       
   666         );
       
   667     }
       
   668     
       
   669     // Make sure the name is valid
       
   670     $new_name = trim($new_name);
       
   671     if ( empty($new_name) )
       
   672     {
       
   673       return array(
       
   674         'success' => false,
       
   675         'error' => 'invalid_parameter'
       
   676         );
       
   677     }
       
   678     
       
   679     // Log the action
       
   680     $username = $db->escape($session->username);
       
   681     $page_name = $db->escape($page_name);
       
   682     $time = time();
       
   683     
       
   684     $q = $db->sql_query('INSERT INTO ' . table_prefix . "logs ( log_type, action, page_id, namespace, author, edit_summary, time_id, date_string ) VALUES\n"
       
   685                       . "  ( 'page', 'rename', '{$this->page_id}', '{$this->namespace}', '$username', '$page_name', '$time', 'DATE_STRING COLUMN OBSOLETE, USE time_id' );");
       
   686     if ( !$q )
       
   687       $db->_die();
       
   688     
       
   689     // Not much to do but to rename it now
       
   690     $new_name = $db->escape($new_name);
       
   691     $q = $db->sql_query('UPDATE ' . table_prefix . "pages SET name = '$new_name' WHERE urlname = '{$this->page_id}' AND namespace = '{$this->namespace}';");
       
   692     if ( !$q )
       
   693       $db->_die();
       
   694     
       
   695     return array(
       
   696       'success' => true
       
   697       );
       
   698   }
       
   699   
       
   700   /**
       
   701    * Sets the protection level of the page
       
   702    * @param int Protection level, one of PROTECT_{FULL,SEMI,NONE}
       
   703    * @param string Reason for protection - required
       
   704    */
       
   705   
       
   706   function protect_page($protection_level, $reason)
       
   707   {
       
   708     global $db, $session, $paths, $template, $plugins; // Common objects
       
   709     
       
   710     // Validate permissions
       
   711     if ( !$this->perms->get_permissions('protect') )
       
   712     {
       
   713       return array(
       
   714         'success' => false,
       
   715         'error' => 'access_denied'
       
   716         );
       
   717     }
       
   718     
       
   719     // Validate input
       
   720     $reason = trim($reason);
       
   721     if ( !in_array($protection_level, array(PROTECT_NONE, PROTECT_FULL, PROTECT_SEMI)) || empty($reason) )
       
   722     {
       
   723       return array(
       
   724         'success' => false,
       
   725         'error' => 'invalid_parameter'
       
   726         );
       
   727     }
       
   728     
       
   729     // Retrieve page metadata
       
   730     $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
       
   731     if ( !isset($paths->pages[$pathskey]) )
       
   732     {
       
   733       return array(
       
   734         'success' => false,
       
   735         'error' => 'page_metadata_not_found'
       
   736         );
       
   737     }
       
   738     $metadata =& $paths->pages[$pathskey];
       
   739     
       
   740     // Log the action
       
   741     $username = $db->escape($session->username);
       
   742     $time = time();
       
   743     $existing_protection = intval($metadata['protected']);
       
   744     $reason = $db->escape($reason);
       
   745     
       
   746     $action = '[ insanity ]';
       
   747     switch($protection_level)
       
   748     {
       
   749       case PROTECT_FULL: $action = 'prot'; break;
       
   750       case PROTECT_NONE: $action = 'unprot'; break;
       
   751       case PROTECT_SEMI: $action = 'semiprot'; break;
       
   752     }
       
   753     
       
   754     $sql = 'INSERT INTO ' . table_prefix . "logs ( log_type, action, page_id, namespace, author, edit_summary, time_id, page_text, date_string ) VALUES\n"
       
   755          . "  ( 'page', '$action', '{$this->page_id}', '{$this->namespace}', '$username', '$reason', '$time', '$existing_protection', 'DATE_STRING COLUMN OBSOLETE, USE time_id' );";
       
   756     if ( !$db->sql_query($sql) )
       
   757     {
       
   758       $db->_die();
       
   759     }
       
   760     
       
   761     // Perform the actual protection
       
   762     $q = $db->sql_query('UPDATE ' . table_prefix . "pages SET protected = $protection_level WHERE urlname = '{$this->page_id}' AND namespace = '{$this->namespace}';");
       
   763     if ( !$q )
       
   764       $db->_die();
       
   765     
       
   766     return array(
       
   767       'success' => true
       
   768       );
   560   }
   769   }
   561   
   770   
   562   /**
   771   /**
   563    * Sets internal variables.
   772    * Sets internal variables.
   564    * @access private
   773    * @access private
   708     if ( $this->revision_id )
   917     if ( $this->revision_id )
   709     {
   918     {
   710       echo '<div class="info-box" style="margin-left: 0; margin-top: 5px;">
   919       echo '<div class="info-box" style="margin-left: 0; margin-top: 5px;">
   711               <b>' . $lang->get('page_msg_archived_title') . '</b><br />
   920               <b>' . $lang->get('page_msg_archived_title') . '</b><br />
   712               ' . $lang->get('page_msg_archived_body', array(
   921               ' . $lang->get('page_msg_archived_body', array(
   713                   'archive_date' => enano_date('F d, Y', $this->revision_id),
   922                   'archive_date' => enano_date('F d, Y', $this->revision_time),
   714                   'archive_time' => enano_date('h:i a', $this->revision_id),
   923                   'archive_time' => enano_date('h:i a', $this->revision_time),
   715                   'current_link' => makeUrlNS($this->namespace, $this->page_id),
   924                   'current_link' => makeUrlNS($this->namespace, $this->page_id),
   716                   'restore_link' => makeUrlNS($this->namespace, $this->page_id, 'do=rollback&amp;id='.$this->revision_id),
   925                   'restore_link' => makeUrlNS($this->namespace, $this->page_id, 'do=edit&amp;revid='.$this->revision_id),
   717                   'restore_onclick' => 'ajaxRollback(\''.$this->revision_id.'\'); return false;',
   926                   'restore_onclick' => 'ajaxEditor(\''.$this->revision_id.'\'); return false;',
   718                 )) . '
   927                 )) . '
   719             </div>
   928             </div>';
   720             <br />';
       
   721     }
   929     }
   722     
   930     
   723     if ( $redir_enabled )
   931     if ( $redir_enabled )
   724     {
   932     {
   725       echo $redir_html;
   933       echo $redir_html;
   794     }
  1002     }
   795     
  1003     
   796     if ( $this->revision_id > 0 && is_int($this->revision_id) )
  1004     if ( $this->revision_id > 0 && is_int($this->revision_id) )
   797     {
  1005     {
   798     
  1006     
   799       $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 . ';');
  1007       $q = $db->sql_query('SELECT page_text, char_tag, time_id FROM '.table_prefix.'logs WHERE log_type=\'page\' AND action=\'edit\' AND page_id=\'' . $this->page_id . '\' AND namespace=\'' . $this->namespace . '\' AND log_id=' . $this->revision_id . ';');
   800       if ( !$q )
  1008       if ( !$q )
   801       {
  1009       {
   802         $this->send_error('Error during SQL query.', true);
  1010         $this->send_error('Error during SQL query.', true);
   803       }
  1011       }
   804       if ( $db->numrows() < 1 )
  1012       if ( $db->numrows() < 1 )
   806         // Compatibility fix for old pages with dots in the page ID
  1014         // Compatibility fix for old pages with dots in the page ID
   807         if ( strstr($this->page_id, '.2e') )
  1015         if ( strstr($this->page_id, '.2e') )
   808         {
  1016         {
   809           $db->free_result();
  1017           $db->free_result();
   810           $page_id = str_replace('.2e', '.', $this->page_id);
  1018           $page_id = str_replace('.2e', '.', $this->page_id);
   811           $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 . ';');
  1019           $q = $db->sql_query('SELECT page_text, char_tag, time_id FROM '.table_prefix.'logs WHERE log_type=\'page\' AND action=\'edit\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $this->namespace . '\' AND log_id=' . $this->revision_id . ';');
   812           if ( !$q )
  1020           if ( !$q )
   813           {
  1021           {
   814             $this->send_error('Error during SQL query.', true);
  1022             $this->send_error('Error during SQL query.', true);
   815           }
  1023           }
   816           if ( $db->numrows() < 1 )
  1024           if ( $db->numrows() < 1 )
   882       $to = array("'", '"',  '\\');
  1090       $to = array("'", '"',  '\\');
   883       $row['page_text'] = str_replace($from, $to, $row['page_text']);
  1091       $row['page_text'] = str_replace($from, $to, $row['page_text']);
   884     }
  1092     }
   885     
  1093     
   886     $this->text_cache = $row['page_text'];
  1094     $this->text_cache = $row['page_text'];
       
  1095     
       
  1096     if ( isset($row['time_id']) )
       
  1097     {
       
  1098       $this->revision_time = intval($row['time_id']);
       
  1099     }
   887     
  1100     
   888     return $row['page_text'];
  1101     return $row['page_text'];
   889     
  1102     
   890   }
  1103   }
   891   
  1104   
  1525     }
  1738     }
  1526   }
  1739   }
  1527   
  1740   
  1528   /**
  1741   /**
  1529    * Send an error message and die. For debugging or critical technical errors only - nothing that would under normal circumstances be shown to the user.
  1742    * Send an error message and die. For debugging or critical technical errors only - nothing that would under normal circumstances be shown to the user.
  1530    * @var string Error message
  1743    * @param string Error message
  1531    * @var bool If true, send DBAL's debugging information as well
  1744    * @param bool If true, send DBAL's debugging information as well
  1532    */
  1745    */
  1533    
  1746    
  1534   function send_error($message, $sql = false)
  1747   function send_error($message, $sql = false)
  1535   {
  1748   {
  1536     global $db, $session, $paths, $template, $plugins; // Common objects
  1749     global $db, $session, $paths, $template, $plugins; // Common objects