ajax.php
changeset 335 67bd3121a12e
parent 334 c72b545f1304
child 336 bfa2e9c23f03
--- a/ajax.php	Wed Dec 26 00:37:26 2007 -0500
+++ b/ajax.php	Thu Dec 27 22:09:33 2007 -0500
@@ -35,7 +35,7 @@
     define('ENANO_ROOT', dirname($filename));
     require(ENANO_ROOT.'/includes/functions.php');
     require(ENANO_ROOT.'/includes/dbal.php');
-    require(ENANO_ROOT.'/includes/json.php');
+    require(ENANO_ROOT.'/includes/json2.php');
     
     require(ENANO_ROOT . '/config.php');
     unset($dbuser, $dbpasswd);
@@ -96,21 +96,32 @@
       echo PageUtils::checkusername($_GET['name']);
       break;
     case "getsource":
+      header('Content-type: application/json');
       $password = ( isset($_GET['pagepass']) ) ? $_GET['pagepass'] : false;
       $page = new PageProcessor($paths->page_id, $paths->namespace);
       $page->password = $password;
       if ( $src = $page->fetch_source() )
       {
-        echo $src;
+        $allowed = true;
       }
       else if ( $src !== false )
       {
-        echo '';
+        $allowed = true;
+        $src = '';
       }
       else
       {
-        echo 'err_access_denied';
+        $allowed = false;
+        $src = '';
       }
+      $return = array(
+          'mode' => 'editor',
+          'src' => $src,
+          'auth_view_source' => $allowed,
+          'auth_edit' => $session->get_permissions('edit_page'),
+          'time' => time()
+        );
+      echo enano_json_encode($return);
       break;
     case "getpage":
       // echo PageUtils::getpage($paths->page, false, ( (isset($_GET['oldid'])) ? $_GET['oldid'] : false ));
@@ -136,6 +147,85 @@
         echo '<p>Error saving the page: '.$e.'</p>';
       }
       break;
+    case "savepage_json":
+      header('Content-type: application/json');
+      if ( !isset($_POST['r']) )
+        die('Invalid request');
+      
+      $request = enano_json_decode($_POST['r']);
+      if ( !isset($request['src']) || !isset($request['summary']) || !isset($request['minor_edit']) || !isset($request['time']) )
+        die('Invalid request');
+      
+      $time = intval($request['time']);
+      
+      // Verify that no edits have been made since the editor was requested
+      $q = $db->sql_query('SELECT time_id, author FROM ' . table_prefix . "logs WHERE log_type = 'page' AND action = 'edit' AND page_id = '{$paths->page_id}' AND namespace = '{$paths->namespace}' ORDER BY time_id DESC LIMIT 1;");
+      if ( !$q )
+        $db->die_json();
+      
+      $row = $db->fetchrow();
+      $db->free_result();
+      
+      if ( $row['time_id'] > $time )
+      {
+        $return = array(
+          'mode' => 'obsolete',
+          'author' => $row['author'],
+          'date_string' => date('d M Y h:i a', $row['time_id']),
+          'time' => $row['time_id'] // time() ???
+          );
+        echo enano_json_encode($return);
+        break;
+      }
+      
+      // Verification complete. Start the PageProcessor and let it do the dirty work for us.
+      $page = new PageProcessor($paths->page_id, $paths->namespace);
+      if ( $page->update_page($request['src'], $request['summary'], ( $request['minor_edit'] == 1 )) )
+      {
+        $return = array(
+            'mode' => 'success'
+          );
+      }
+      else
+      {
+        $errors = array();
+        while ( $err = $page->pop_error() )
+        {
+          $errors[] = $err;
+        }
+        $return = array(
+          'mode' => 'errors',
+          'errors' => array_values($errors)
+          );
+      }
+      
+      echo enano_json_encode($return);
+      
+      break;
+    case "diff_cur":
+      
+      // Lie about our content type to fool ad scripts
+      header('Content-type: application/xhtml+xml');
+      
+      if ( !isset($_POST['text']) )
+        die('Invalid request');
+      
+      $page = new PageProcessor($paths->page_id, $paths->namespace);
+      if ( !($src = $page->fetch_source()) )
+      {
+        die('Access denied');
+      }
+      
+      $diff = RenderMan::diff($src, $_POST['text']);
+      if ( $diff == '<table class="diff"></table>' )
+      {
+        $diff = '<p>' . $lang->get('editor_msg_diff_empty') . '</p>';
+      }
+      
+      echo '<div class="info-box">' . $lang->get('editor_msg_diff') . '</div>';
+      echo $diff;
+      
+      break;
     case "protect":
       echo PageUtils::protect($paths->page_id, $paths->namespace, (int)$_POST['level'], $_POST['reason']);
       break;