includes/pageprocess.php
changeset 832 7152ca0a0ce9
parent 827 2c20563245b2
child 877 e9ee4e246f96
equal deleted inserted replaced
831:45e887f23282 832:7152ca0a0ce9
   227       if ( function_exists($funcname) )
   227       if ( function_exists($funcname) )
   228       {
   228       {
   229         $this->page_exists = true;
   229         $this->page_exists = true;
   230       }
   230       }
   231     }
   231     }
   232     if ( isset($paths->pages[$pathskey]) )
   232     if ( isPage($pathskey) )
   233     {
   233     {
   234       if ( $this->send_headers )
   234       if ( $this->send_headers )
   235       {
   235       {
   236         $template->init_vars($this);
   236         $template->init_vars($this);
   237       }
   237       }
   303   function send_from_db($strict_no_headers = false)
   303   function send_from_db($strict_no_headers = false)
   304   {
   304   {
   305     global $db, $session, $paths, $template, $plugins; // Common objects
   305     global $db, $session, $paths, $template, $plugins; // Common objects
   306     global $lang;
   306     global $lang;
   307     
   307     
   308     // Send as regular page
   308     $this->ns->send_from_db();
   309     if ( $this->send_headers )
       
   310     {
       
   311       $template->init_vars($this);
       
   312     }
       
   313     
       
   314     $text = $this->fetch_text();
       
   315     
       
   316     if ( $text == 'err_no_text_rows' )
       
   317     {
       
   318       $this->err_no_rows();
       
   319       return false;
       
   320     }
       
   321     else
       
   322     {
       
   323       $redirect = ( isset($_GET['redirect']) ) ? $_GET['redirect'] : 'YES YOU IDIOT';
       
   324       if ( preg_match('/^#redirect \[\[([^\]]+)\]\]/i', $text, $match) && $redirect != 'no' )
       
   325       {
       
   326         // Redirect page!
       
   327         $page_to = sanitize_page_id($match[1]);
       
   328         $page_id_data = RenderMan::strToPageID($page_to);
       
   329         if ( count($this->redirect_stack) >= 3 )
       
   330         {
       
   331           $this->render( (!$strict_no_headers), '<div class="usermessage"><b>' . $lang->get('page_err_redirects_exceeded') . '</b></div>' );
       
   332         }
       
   333         else
       
   334         {
       
   335           $result = $this->_handle_redirect($page_id_data[0], $page_id_data[1]);
       
   336           if ( $result !== true )
       
   337           {
       
   338             // There was some error during the redirect process - usually an infinite redirect
       
   339             $this->render( (!$strict_no_headers), '<div class="usermessage"><b>' . $result . '</b></div>' );
       
   340           }
       
   341         }
       
   342       }
       
   343       else
       
   344       {
       
   345         $this->render( (!$strict_no_headers) );
       
   346       }
       
   347     }
       
   348   }
   309   }
   349   
   310   
   350   /**
   311   /**
   351    * Fetches the wikitext or HTML source for the page.
   312    * Fetches the wikitext or HTML source for the page.
   352    * @return string
   313    * @return string
   363     if ( !$this->page_exists )
   324     if ( !$this->page_exists )
   364     {
   325     {
   365       return '';
   326       return '';
   366     }
   327     }
   367     $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
   328     $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
   368     if ( isset($paths->pages[$pathskey]) )
   329     if ( isPage($pathskey) )
   369     {
   330     {
   370       if ( isset($paths->pages[$pathskey]['password']) )
   331       if ( isset($paths->pages[$pathskey]['password']) )
   371       {
   332       {
   372         if ( $paths->pages[$pathskey]['password'] != sha1('') && $paths->pages[$pathskey]['password'] !== $this->password && !empty($paths->pages[$pathskey]['password']) )
   333         if ( $paths->pages[$pathskey]['password'] != sha1('') && $paths->pages[$pathskey]['password'] !== $this->password && !empty($paths->pages[$pathskey]['password']) )
   373         {
   334         {
   381   /**
   342   /**
   382    * Updates (saves/changes/edits) the content of the page.
   343    * Updates (saves/changes/edits) the content of the page.
   383    * @param string The new text for the page
   344    * @param string The new text for the page
   384    * @param string A summary of edits made to the page.
   345    * @param string A summary of edits made to the page.
   385    * @param bool If true, the edit is marked as a minor revision
   346    * @param bool If true, the edit is marked as a minor revision
       
   347    * @param string Page format - wikitext or xhtml. REQUIRED, and new in 1.1.6.
   386    * @return bool True on success, false on failure. When returning false, it will push errors to the PageProcessor error stack; read with $page->pop_error()
   348    * @return bool True on success, false on failure. When returning false, it will push errors to the PageProcessor error stack; read with $page->pop_error()
   387    */
   349    */
   388   
   350   
   389   function update_page($text, $edit_summary = false, $minor_edit = false)
   351   function update_page($text, $edit_summary = false, $minor_edit = false, $page_format)
   390   {
   352   {
   391     global $db, $session, $paths, $template, $plugins; // Common objects
   353     global $db, $session, $paths, $template, $plugins; // Common objects
   392     global $lang;
   354     global $lang;
   393     
   355     
   394     // Create the page if it doesn't exist
   356     // Create the page if it doesn't exist
   450     
   412     
   451     // Spam check
   413     // Spam check
   452     if ( !spamalyze($text) )
   414     if ( !spamalyze($text) )
   453     {
   415     {
   454       $this->raise_error($lang->get('editor_err_spamcheck_failed'));
   416       $this->raise_error($lang->get('editor_err_spamcheck_failed'));
       
   417       return false;
       
   418     }
       
   419     
       
   420     // Page format check
       
   421     if ( !in_array($page_format, array('xhtml', 'wikitext')) )
       
   422     {
       
   423       $this->raise_error("format \"$page_format\" not one of [xhtml, wikitext]");
   455       return false;
   424       return false;
   456     }
   425     }
   457     
   426     
   458     //
   427     //
   459     // Protection validated; update page content
   428     // Protection validated; update page content
   466     $edit_summary = ( strval($edit_summary) === $edit_summary ) ? $db->escape($edit_summary) : '';
   435     $edit_summary = ( strval($edit_summary) === $edit_summary ) ? $db->escape($edit_summary) : '';
   467     $minor_edit = ( $minor_edit ) ? '1' : '0';
   436     $minor_edit = ( $minor_edit ) ? '1' : '0';
   468     $date_string = enano_date('d M Y h:i a');
   437     $date_string = enano_date('d M Y h:i a');
   469     
   438     
   470     // Insert log entry
   439     // Insert log entry
   471     $sql = 'INSERT INTO ' . table_prefix . "logs ( time_id, date_string, log_type, action, page_id, namespace, author, page_text, edit_summary, minor_edit )\n"
   440     $sql = 'INSERT INTO ' . table_prefix . "logs ( time_id, date_string, log_type, action, page_id, namespace, author, page_text, edit_summary, minor_edit, page_format )\n"
   472          . "  VALUES ( $time, '$date_string', 'page', 'edit', '{$this->page_id}', '{$this->namespace}', '$author', '$text', '$edit_summary', $minor_edit );";
   441          . "  VALUES ( $time, '$date_string', 'page', 'edit', '{$this->page_id}', '{$this->namespace}', '$author', '$text', '$edit_summary', $minor_edit, '$page_format' );";
   473     if ( !$db->sql_query($sql) )
   442     if ( !$db->sql_query($sql) )
   474     {
   443     {
   475       $this->raise_error($db->get_error());
   444       $this->raise_error($db->get_error());
   476       return false;
   445       return false;
   477     }
   446     }
   488     $sql = 'DELETE FROM ' . table_prefix . "logs WHERE is_draft = 1 AND page_id = '{$this->page_id}' AND namespace = '{$this->namespace}' AND page_text = '{$text}';";
   457     $sql = 'DELETE FROM ' . table_prefix . "logs WHERE is_draft = 1 AND page_id = '{$this->page_id}' AND namespace = '{$this->namespace}' AND page_text = '{$text}';";
   489     if ( !$db->sql_query($sql) )
   458     if ( !$db->sql_query($sql) )
   490     {
   459     {
   491       $this->raise_error($db->get_error());
   460       $this->raise_error($db->get_error());
   492       return false;
   461       return false;
       
   462     }
       
   463     
       
   464     // Set page_format
       
   465     $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
       
   466     if ( $paths->pages[ $pathskey ]['page_format'] != $page_format )
       
   467     {
       
   468       // Note: no SQL injection to worry about here. Everything that goes into this is sanitized already, barring some rogue plugin.
       
   469       // (and if there's a rogue plugin running, we have bigger things to worry about anyway.)
       
   470       if ( !$db->sql_query('UPDATE ' . table_prefix . "pages SET page_format = '$page_format' WHERE urlname = '$this->page_id' AND namespace = '$this->namespace';") )
       
   471       {
       
   472         $this->raise_error($db->get_error());
       
   473         return false;
       
   474       }
       
   475       $paths->update_metadata_cache();
   493     }
   476     }
   494     
   477     
   495     // Rebuild the search index
   478     // Rebuild the search index
   496     $paths->rebuild_page_index($this->page_id, $this->namespace);
   479     $paths->rebuild_page_index($this->page_id, $this->namespace);
   497     
   480     
   671         if ( !$this->perms->get_permissions('history_rollback_extra') )
   654         if ( !$this->perms->get_permissions('history_rollback_extra') )
   672           return 'Administrative privileges are required for page undeletion.';
   655           return 'Administrative privileges are required for page undeletion.';
   673         
   656         
   674         // Rolling back the deletion of a page that was since created?
   657         // Rolling back the deletion of a page that was since created?
   675         $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
   658         $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
   676         if ( isset($paths->pages[$pathskey]) )
   659         if ( isPage($pathskey) )
   677           return array(
   660           return array(
   678               'success' => false,
   661               'success' => false,
   679               // This is a clean Christian in-joke.
   662               // This is a clean Christian in-joke.
   680               'error' => 'seeking_living_among_dead'
   663               'error' => 'seeking_living_among_dead'
   681             );
   664             );
   873         );
   856         );
   874     }
   857     }
   875     
   858     
   876     // Retrieve page metadata
   859     // Retrieve page metadata
   877     $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
   860     $pathskey = $paths->nslist[ $this->namespace ] . $this->page_id;
   878     if ( !isset($paths->pages[$pathskey]) )
   861     if ( !isPage($pathskey) )
   879     {
   862     {
   880       return array(
   863       return array(
   881         'success' => false,
   864         'success' => false,
   882         'error' => 'page_metadata_not_found'
   865         'error' => 'page_metadata_not_found'
   883         );
   866         );
  1036     if ( $looped )
  1019     if ( $looped )
  1037     {
  1020     {
  1038       return $lang->get('page_err_redirect_infinite_loop');
  1021       return $lang->get('page_err_redirect_infinite_loop');
  1039     }
  1022     }
  1040     $page_id_key = $paths->nslist[ $namespace ] . sanitize_page_id($page_id);
  1023     $page_id_key = $paths->nslist[ $namespace ] . sanitize_page_id($page_id);
  1041     if ( !isset($paths->pages[$page_id_key]) )
  1024     if ( !isPage($page_id_key) )
  1042     {
  1025     {
  1043       return $lang->get('page_err_redirect_to_nonexistent');
  1026       return $lang->get('page_err_redirect_to_nonexistent');
  1044     }
  1027     }
  1045     $this->redirect_stack[] = $arr_pid;
  1028     $this->redirect_stack[] = $arr_pid;
  1046     
  1029