ajax.php
changeset 413 6607cd646d6d
parent 408 7ecbe721217c
child 416 53fcdf309a82
equal deleted inserted replaced
412:4746dd07cc48 413:6607cd646d6d
    94   switch($_GET['_mode']) {
    94   switch($_GET['_mode']) {
    95     case "checkusername":
    95     case "checkusername":
    96       echo PageUtils::checkusername($_GET['name']);
    96       echo PageUtils::checkusername($_GET['name']);
    97       break;
    97       break;
    98     case "getsource":
    98     case "getsource":
    99       header('Content-type: application/json');
    99       header('Content-type: text/plain');
   100       $password = ( isset($_GET['pagepass']) ) ? $_GET['pagepass'] : false;
   100       $password = ( isset($_GET['pagepass']) ) ? $_GET['pagepass'] : false;
   101       $revid = ( isset($_GET['revid']) ) ? intval($_GET['revid']) : 0;
   101       $revid = ( isset($_GET['revid']) ) ? intval($_GET['revid']) : 0;
   102       $page = new PageProcessor($paths->page_id, $paths->namespace, $revid);
   102       $page = new PageProcessor($paths->page_id, $paths->namespace, $revid);
   103       $page->password = $password;
   103       $page->password = $password;
       
   104       $have_draft = false;
   104       if ( $src = $page->fetch_source() )
   105       if ( $src = $page->fetch_source() )
   105       {
   106       {
   106         $allowed = true;
   107         $allowed = true;
       
   108         $q = $db->sql_query('SELECT author, time_id, page_text FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
       
   109                                AND page_id = \'' . $db->escape($paths->page_id) . '\'
       
   110                                AND namespace = \'' . $db->escape($paths->namespace) . '\'
       
   111                                AND is_draft = 1;');
       
   112         if ( !$q )
       
   113           $db->die_json();
       
   114         
       
   115         if ( $db->numrows() > 0 )
       
   116         {
       
   117           $have_draft = true;
       
   118         }
   107       }
   119       }
   108       else if ( $src !== false )
   120       else if ( $src !== false )
   109       {
   121       {
   110         $allowed = true;
   122         $allowed = true;
   111         $src = '';
   123         $src = '';
   125           'auth_view_source' => $allowed,
   137           'auth_view_source' => $allowed,
   126           'auth_edit' => $auth_edit,
   138           'auth_edit' => $auth_edit,
   127           'time' => time(),
   139           'time' => time(),
   128           'require_captcha' => false,
   140           'require_captcha' => false,
   129           'allow_wysiwyg' => $auth_wysiwyg,
   141           'allow_wysiwyg' => $auth_wysiwyg,
   130           'revid' => $revid
   142           'revid' => $revid,
       
   143           'have_draft' => false
   131         );
   144         );
       
   145       
       
   146       if ( $have_draft )
       
   147       {
       
   148         $row = $db->fetchrow($q);
       
   149         $return['have_draft'] = true;
       
   150         $return['draft_author'] = $row['author'];
       
   151         $return['draft_time'] = enano_date('d M Y h:i a', intval($row['time_id']));
       
   152         if ( isset($_GET['get_draft']) && @$_GET['get_draft'] === '1' )
       
   153         {
       
   154           $return['src'] = $row['page_text'];
       
   155         }
       
   156       }
   132       
   157       
   133       if ( $revid > 0 )
   158       if ( $revid > 0 )
   134       {
   159       {
   135         // Retrieve information about this revision and the current one
   160         // Retrieve information about this revision and the current one
   136         $q = $db->sql_query('SELECT l1.author AS currentrev_author, l2.author AS oldrev_author FROM ' . table_prefix . 'logs AS l1
   161         $q = $db->sql_query('SELECT l1.author AS currentrev_author, l2.author AS oldrev_author FROM ' . table_prefix . 'logs AS l1
   137   LEFT JOIN ' . table_prefix . 'logs AS l2
   162   LEFT JOIN ' . table_prefix . 'logs AS l2
   138     ON ( l2.time_id = ' . $revid . '
   163     ON ( l2.time_id = ' . $revid . '
   139          AND l2.log_type  = \'page\'
   164          AND l2.log_type  = \'page\'
   140          AND l2.action    = \'edit\'
   165          AND l2.action    = \'edit\'
   141          AND l2.page_id   = \'ACL_Tests\'
   166          AND l2.page_id   = \'' . $db->escape($paths->page_id)   . '\'
   142          AND l2.namespace = \'Article\'
   167          AND l2.namespace = \'' . $db->escape($paths->namespace) . '\'
   143         )
   168         )
   144   WHERE l1.log_type  = \'page\'
   169   WHERE l1.log_type  = \'page\'
   145     AND l1.action    = \'edit\'
   170     AND l1.action    = \'edit\'
   146     AND l1.page_id   = \'ACL_Tests\'
   171     AND l1.page_id   = \'' . $db->escape($paths->page_id)   . '\'
   147     AND l1.namespace = \'Article\'
   172     AND l1.namespace = \'' . $db->escape($paths->namespace) . '\'
   148     AND l1.time_id >= ' . $revid . '
   173     AND l1.time_id >= ' . $revid . '
   149   ORDER BY l1.time_id DESC;');
   174   ORDER BY l1.time_id DESC;');
   150         if ( !$q )
   175         if ( !$q )
   151           $db->die_json();
   176           $db->die_json();
   152         
   177         
   153         $rev_count = $db->numrows() - 1;
   178         $rev_count = $db->numrows() - 1;
   154         $row = $db->fetchrow();
   179         if ( $rev_count == -1 )
   155         $return['undo_info'] = array(
   180         {
   156           'old_author'     => $row['oldrev_author'],
   181           $return = array(
   157           'current_author' => $row['currentrev_author'],
   182               'mode' => 'error',
   158           'undo_count'     => $rev_count
   183               'error' => '[Internal] No rows returned by revision info query. SQL:<pre>' . $db->latest_query . '</pre>'
   159         );
   184             );
       
   185         }
       
   186         else
       
   187         {
       
   188           $row = $db->fetchrow();
       
   189           $return['undo_info'] = array(
       
   190             'old_author'     => $row['oldrev_author'],
       
   191             'current_author' => $row['currentrev_author'],
       
   192             'undo_count'     => $rev_count
       
   193           );
       
   194         }
   160       }
   195       }
   161       
   196       
   162       if ( $auth_edit && !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
   197       if ( $auth_edit && !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
   163       {
   198       {
   164         $return['require_captcha'] = true;
   199         $return['require_captcha'] = true;
   165         $return['captcha_id'] = $session->make_captcha();
   200         $return['captcha_id'] = $session->make_captcha();
   166       }
   201       }
       
   202       
       
   203       $template->load_theme();
       
   204       $return['toolbar_templates'] = $template->extract_vars('toolbar.tpl');
   167       
   205       
   168       echo enano_json_encode($return);
   206       echo enano_json_encode($return);
   169       break;
   207       break;
   170     case "getpage":
   208     case "getpage":
   171       // echo PageUtils::getpage($paths->page, false, ( (isset($_GET['oldid'])) ? $_GET['oldid'] : false ));
   209       // echo PageUtils::getpage($paths->page, false, ( (isset($_GET['oldid'])) ? $_GET['oldid'] : false ));
   179       break;
   217       break;
   180     case "savepage":
   218     case "savepage":
   181       $summ = ( isset($_POST['summary']) ) ? $_POST['summary'] : '';
   219       $summ = ( isset($_POST['summary']) ) ? $_POST['summary'] : '';
   182       $minor = isset($_POST['minor']);
   220       $minor = isset($_POST['minor']);
   183       $e = PageUtils::savepage($paths->page_id, $paths->namespace, $_POST['text'], $summ, $minor);
   221       $e = PageUtils::savepage($paths->page_id, $paths->namespace, $_POST['text'], $summ, $minor);
   184       if($e=='good')
   222       if ( $e == 'good' )
   185       {
   223       {
   186         $page = new PageProcessor($paths->page_id, $paths->namespace);
   224         $page = new PageProcessor($paths->page_id, $paths->namespace);
   187         $page->send();
   225         $page->send();
   188       }
   226       }
   189       else
   227       else
   192       }
   230       }
   193       break;
   231       break;
   194     case "savepage_json":
   232     case "savepage_json":
   195       header('Content-type: application/json');
   233       header('Content-type: application/json');
   196       if ( !isset($_POST['r']) )
   234       if ( !isset($_POST['r']) )
   197         die('Invalid request');
   235         die('Invalid request [1]');
   198       
   236       
   199       $request = enano_json_decode($_POST['r']);
   237       $request = enano_json_decode($_POST['r']);
   200       if ( !isset($request['src']) || !isset($request['summary']) || !isset($request['minor_edit']) || !isset($request['time']) )
   238       if ( !isset($request['src']) || !isset($request['summary']) || !isset($request['minor_edit']) || !isset($request['time']) || !isset($request['draft']) )
   201         die('Invalid request');
   239         die('Invalid request [2]<pre>' . htmlspecialchars(print_r($request, true)) . '</pre>');
   202       
   240       
   203       $time = intval($request['time']);
   241       $time = intval($request['time']);
   204       
   242       
   205       // Verify that no edits have been made since the editor was requested
   243       if ( $request['draft'] )
   206       $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;");
   244       {
   207       if ( !$q )
   245         //
   208         $db->die_json();
   246         // The user wants to save a draft version of the page.
   209       
   247         //
   210       $row = $db->fetchrow();
   248         
   211       $db->free_result();
   249         // Delete any draft copies if they exist
   212       
   250         $q = $db->sql_query('DELETE FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
   213       if ( $row['time_id'] > $time )
   251                                AND page_id = \'' . $db->escape($paths->page_id) . '\'
   214       {
   252                                AND namespace = \'' . $db->escape($paths->namespace) . '\'
       
   253                                AND is_draft = 1;');
       
   254         if ( !$q )
       
   255           $db->die_json();
       
   256         
       
   257         $src = RenderMan::preprocess_text($request['src'], false, false);
       
   258         
       
   259         // Save the draft
       
   260         $q = $db->sql_query('INSERT INTO ' . table_prefix . 'logs ( log_type, action, page_id, namespace, author, edit_summary, page_text, is_draft, time_id )
       
   261                                VALUES (
       
   262                                  \'page\',
       
   263                                  \'edit\',
       
   264                                  \'' . $db->escape($paths->page_id) . '\',
       
   265                                  \'' . $db->escape($paths->namespace) . '\',
       
   266                                  \'' . $db->escape($session->username) . '\',
       
   267                                  \'' . $db->escape($request['summary']) . '\',
       
   268                                  \'' . $db->escape($src) . '\',
       
   269                                  1,
       
   270                                  ' . time() . '
       
   271                                );');
       
   272         
       
   273         // Done!
   215         $return = array(
   274         $return = array(
   216           'mode' => 'obsolete',
   275             'mode' => 'success',
   217           'author' => $row['author'],
   276             'is_draft' => true
   218           'date_string' => enano_date('d M Y h:i a', $row['time_id']),
       
   219           'time' => $row['time_id'] // time() ???
       
   220           );
   277           );
   221         echo enano_json_encode($return);
   278       }
   222         break;
   279       else
   223       }
   280       {
   224       
   281         // Verify that no edits have been made since the editor was requested
   225       // Verify captcha, if needed
   282         $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;");
   226       if ( !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
   283         if ( !$q )
   227       {
   284           $db->die_json();
   228         if ( !isset($request['captcha_id']) || !isset($request['captcha_code']) )
   285         
   229         {
   286         $row = $db->fetchrow();
   230           die('Invalid request, need captcha metadata');
   287         $db->free_result();
   231         }
   288         
   232         $code_correct = strtolower($session->get_captcha($request['captcha_id']));
   289         if ( $row['time_id'] > $time )
   233         $code_input = strtolower($request['captcha_code']);
   290         {
   234         if ( $code_correct !== $code_input )
   291           $return = array(
   235         {
   292             'mode' => 'obsolete',
       
   293             'author' => $row['author'],
       
   294             'date_string' => enano_date('d M Y h:i a', $row['time_id']),
       
   295             'time' => $row['time_id'] // time() ???
       
   296             );
       
   297           echo enano_json_encode($return);
       
   298           break;
       
   299         }
       
   300         
       
   301         // Verify captcha, if needed
       
   302         if ( !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
       
   303         {
       
   304           if ( !isset($request['captcha_id']) || !isset($request['captcha_code']) )
       
   305           {
       
   306             die('Invalid request, need captcha metadata');
       
   307           }
       
   308           $code_correct = strtolower($session->get_captcha($request['captcha_id']));
       
   309           $code_input = strtolower($request['captcha_code']);
       
   310           if ( $code_correct !== $code_input )
       
   311           {
       
   312             $return = array(
       
   313               'mode' => 'errors',
       
   314               'errors' => array($lang->get('editor_err_captcha_wrong')),
       
   315               'new_captcha' => $session->make_captcha()
       
   316             );
       
   317             echo enano_json_encode($return);
       
   318             break;
       
   319           }
       
   320         }
       
   321         
       
   322         // Verification complete. Start the PageProcessor and let it do the dirty work for us.
       
   323         $page = new PageProcessor($paths->page_id, $paths->namespace);
       
   324         if ( $page->update_page($request['src'], $request['summary'], ( $request['minor_edit'] == 1 )) )
       
   325         {
       
   326           $return = array(
       
   327               'mode' => 'success',
       
   328               'is_draft' => false
       
   329             );
       
   330         }
       
   331         else
       
   332         {
       
   333           $errors = array();
       
   334           while ( $err = $page->pop_error() )
       
   335           {
       
   336             $errors[] = $err;
       
   337           }
   236           $return = array(
   338           $return = array(
   237             'mode' => 'errors',
   339             'mode' => 'errors',
   238             'errors' => array($lang->get('editor_err_captcha_wrong')),
   340             'errors' => array_values($errors)
   239             'new_captcha' => $session->make_captcha()
   341             );
   240           );
   342           if ( !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
   241           echo enano_json_encode($return);
   343           {
   242           break;
   344             $return['new_captcha'] = $session->make_captcha();
   243         }
   345           }
   244       }
   346         }
   245       
   347         
   246       // Verification complete. Start the PageProcessor and let it do the dirty work for us.
   348         // If this is based on a draft version, delete the draft - we no longer need it.
   247       $page = new PageProcessor($paths->page_id, $paths->namespace);
   349         if ( @$request['used_draft'] )
   248       if ( $page->update_page($request['src'], $request['summary'], ( $request['minor_edit'] == 1 )) )
   350         {
   249       {
   351           $q = $db->sql_query('DELETE FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
   250         $return = array(
   352                                  AND page_id = \'' . $db->escape($paths->page_id) . '\'
   251             'mode' => 'success'
   353                                  AND namespace = \'' . $db->escape($paths->namespace) . '\'
   252           );
   354                                  AND is_draft = 1;');
   253       }
       
   254       else
       
   255       {
       
   256         $errors = array();
       
   257         while ( $err = $page->pop_error() )
       
   258         {
       
   259           $errors[] = $err;
       
   260         }
       
   261         $return = array(
       
   262           'mode' => 'errors',
       
   263           'errors' => array_values($errors)
       
   264           );
       
   265         if ( !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
       
   266         {
       
   267           $return['new_captcha'] = $session->make_captcha();
       
   268         }
   355         }
   269       }
   356       }
   270       
   357       
   271       echo enano_json_encode($return);
   358       echo enano_json_encode($return);
   272       
   359