ajax.php
changeset 832 7152ca0a0ce9
parent 801 eb8b23f11744
child 870 82bbfe3dc8a0
equal deleted inserted replaced
831:45e887f23282 832:7152ca0a0ce9
    35       
    35       
    36       $have_draft = false;
    36       $have_draft = false;
    37       if ( $src = $page->fetch_source() )
    37       if ( $src = $page->fetch_source() )
    38       {
    38       {
    39         $allowed = true;
    39         $allowed = true;
    40         $q = $db->sql_query('SELECT author, time_id, page_text, edit_summary FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
    40         $q = $db->sql_query('SELECT author, time_id, page_text, edit_summary, page_format FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
    41                                AND page_id = \'' . $db->escape($paths->page_id) . '\'
    41                                AND page_id = \'' . $db->escape($paths->page_id) . '\'
    42                                AND namespace = \'' . $db->escape($paths->namespace) . '\'
    42                                AND namespace = \'' . $db->escape($paths->namespace) . '\'
    43                                AND is_draft = 1;');
    43                                AND is_draft = 1;');
    44         if ( !$q )
    44         if ( !$q )
    45           $db->die_json();
    45           $db->die_json();
    74           'allow_wysiwyg' => $auth_wysiwyg,
    74           'allow_wysiwyg' => $auth_wysiwyg,
    75           'revid' => $revid,
    75           'revid' => $revid,
    76           'have_draft' => false
    76           'have_draft' => false
    77         );
    77         );
    78       
    78       
       
    79       $return['page_format'] = $paths->cpage['page_format'];
       
    80       if ( $return['page_format'] == 'xhtml' )
       
    81       {
       
    82         // gently process headings to make tinymce format them correctly
       
    83         if ( preg_match_all('/^ *?(={1,6}) *(.+?) *\\1 *$/m', $return['src'], $matches) )
       
    84         {
       
    85           foreach ( $matches[0] as $i => $match )
       
    86           {
       
    87             $hi = strlen($matches[1][$i]);
       
    88             $heading = "<h{$hi}>{$matches[2][$i]}</h{$hi}>";
       
    89             $return['src'] = str_replace_once($match, $heading, $return['src']);
       
    90           }
       
    91         }
       
    92       }
       
    93       
    79       if ( $have_draft )
    94       if ( $have_draft )
    80       {
    95       {
    81         $row =& $draft_row;
    96         $row =& $draft_row;
    82         $return['have_draft'] = true;
    97         $return['have_draft'] = true;
    83         $return['draft_author'] = $row['author'];
    98         $return['draft_author'] = $row['author'];
    84         $return['draft_time'] = enano_date('d M Y h:i a', intval($row['time_id']));
    99         $return['draft_time'] = enano_date('d M Y h:i a', intval($row['time_id']));
    85         if ( isset($_GET['get_draft']) && @$_GET['get_draft'] === '1' )
   100         if ( isset($_GET['get_draft']) && @$_GET['get_draft'] === '1' )
    86         {
   101         {
    87           $return['src'] = $row['page_text'];
   102           $return['src'] = $row['page_text'];
    88           $return['edit_summary'] = $row['edit_summary'];
   103           $return['edit_summary'] = $row['edit_summary'];
       
   104           $return['page_format'] = $row['page_format'];
    89         }
   105         }
    90       }
   106       }
    91       
   107       
    92       $return['undo_info'] = array();
   108       $return['undo_info'] = array();
    93       
   109       
   210             );
   226             );
   211           }
   227           }
   212           else
   228           else
   213           {
   229           {
   214             $src = RenderMan::preprocess_text($request['src'], false, false);
   230             $src = RenderMan::preprocess_text($request['src'], false, false);
   215             
   231             $draft_format = $request['format'];
   216             // Save the draft
   232             if ( !in_array($draft_format, array('xhtml', 'wikitext')) )
   217             $q = $db->sql_query('INSERT INTO ' . table_prefix . 'logs ( log_type, action, page_id, namespace, author, edit_summary, page_text, is_draft, time_id )
   233             {
   218                                    VALUES (
   234               $return = array(
   219                                      \'page\',
   235                 'mode' => 'error',
   220                                      \'edit\',
   236                 'error' => 'invalid_format'
   221                                      \'' . $db->escape($paths->page_id) . '\',
       
   222                                      \'' . $db->escape($paths->namespace) . '\',
       
   223                                      \'' . $db->escape($session->username) . '\',
       
   224                                      \'' . $db->escape($request['summary']) . '\',
       
   225                                      \'' . $db->escape($src) . '\',
       
   226                                      1,
       
   227                                      ' . time() . '
       
   228                                    );');
       
   229             
       
   230             // Done!
       
   231             $return = array(
       
   232                 'mode' => 'success',
       
   233                 'is_draft' => true
       
   234               );
   237               );
       
   238             }
       
   239             else
       
   240             {
       
   241               // Save the draft
       
   242               $q = $db->sql_query('INSERT INTO ' . table_prefix . 'logs ( log_type, action, page_id, namespace, author, edit_summary, page_text, is_draft, time_id, page_format )
       
   243                                      VALUES (
       
   244                                        \'page\',
       
   245                                        \'edit\',
       
   246                                        \'' . $db->escape($paths->page_id) . '\',
       
   247                                        \'' . $db->escape($paths->namespace) . '\',
       
   248                                        \'' . $db->escape($session->username) . '\',
       
   249                                        \'' . $db->escape($request['summary']) . '\',
       
   250                                        \'' . $db->escape($src) . '\',
       
   251                                        1,
       
   252                                        ' . time() . ',
       
   253                                        \'' . $draft_format . '\'
       
   254                                      );');
       
   255               
       
   256               // Done!
       
   257               $return = array(
       
   258                   'mode' => 'success',
       
   259                   'is_draft' => true
       
   260                 );
       
   261             }
   235           }
   262           }
   236         }
   263         }
   237       }
   264       }
   238       else
   265       else
   239       {
   266       {
   278           }
   305           }
   279         }
   306         }
   280         
   307         
   281         // Verification complete. Start the PageProcessor and let it do the dirty work for us.
   308         // Verification complete. Start the PageProcessor and let it do the dirty work for us.
   282         $page = new PageProcessor($paths->page_id, $paths->namespace);
   309         $page = new PageProcessor($paths->page_id, $paths->namespace);
   283         if ( $page->update_page($request['src'], $request['summary'], ( $request['minor_edit'] == 1 )) )
   310         if ( $page->update_page($request['src'], $request['summary'], ( $request['minor_edit'] == 1 ), $request['format']) )
   284         {
   311         {
   285           $return = array(
   312           $return = array(
   286               'mode' => 'success',
   313               'mode' => 'success',
   287               'is_draft' => false
   314               'is_draft' => false
   288             );
   315             );
   427       echo PageUtils::setpass($paths->page_id, $paths->namespace, $_POST['password']);
   454       echo PageUtils::setpass($paths->page_id, $paths->namespace, $_POST['password']);
   428       break;
   455       break;
   429     case "fillusername":
   456     case "fillusername":
   430       break;
   457       break;
   431     case "fillpagename":
   458     case "fillpagename":
   432       $name = (isset($_GET['name'])) ? $_GET['name'] : false;
       
   433       if(!$name) die('userlist = new Array(); namelist = new Array(); errorstring=\'Invalid URI\'');
       
   434       $nd = RenderMan::strToPageID($name);
       
   435       $c = 0;
       
   436       $u = Array();
       
   437       $n = Array();
       
   438       
       
   439       $name = sanitize_page_id($name);
       
   440       $name = str_replace('_', ' ', $name);
       
   441       
       
   442       foreach ( $paths->pages as $i => $_ )
       
   443       {
       
   444         if( ( 
       
   445             preg_match('#'.preg_quote($name).'(.*)#i', $paths->pages[$i]['name']) ||
       
   446             preg_match('#'.preg_quote($name).'(.*)#i', $paths->pages[$i]['urlname']) ||
       
   447             preg_match('#'.preg_quote($name).'(.*)#i', $paths->pages[$i]['urlname_nons']) ||
       
   448             preg_match('#'.preg_quote(str_replace(' ', '_', $name)).'(.*)#i', $paths->pages[$i]['name']) ||
       
   449             preg_match('#'.preg_quote(str_replace(' ', '_', $name)).'(.*)#i', $paths->pages[$i]['urlname']) ||
       
   450             preg_match('#'.preg_quote(str_replace(' ', '_', $name)).'(.*)#i', $paths->pages[$i]['urlname_nons'])
       
   451             ) &&
       
   452            ( ( $nd[1] != 'Article' && $paths->pages[$i]['namespace'] == $nd[1] ) || $nd[1] == 'Article' )
       
   453             && $paths->pages[$i]['visible']
       
   454            )
       
   455         {
       
   456           $c++;
       
   457           $u[] = $paths->pages[$i]['name'];
       
   458           $n[] = $paths->pages[$i]['urlname'];
       
   459         }
       
   460       }
       
   461       if($c > 0)
       
   462       {
       
   463         echo 'userlist = new Array(); namelist = new Array(); errorstring = false; '."\n";
       
   464         for($i=0;$i<sizeof($u);$i++) // Can't use foreach because we need the value of $i and we need to use both $u and $n
       
   465         {
       
   466           echo "userlist[$i] = '".addslashes($n[$i])."';\n";
       
   467           echo "namelist[$i] = '".addslashes(htmlspecialchars($u[$i]))."';\n";
       
   468         }
       
   469       } else {
       
   470         die('userlist = new Array(); namelist = new Array(); errorstring=\'No page matches found.\'');
       
   471       }
       
   472       break;
   459       break;
   473     case "preview":
   460     case "preview":
   474       require_once(ENANO_ROOT.'/includes/pageutils.php');
   461       require_once(ENANO_ROOT.'/includes/pageutils.php');
   475       $template->init_vars();
   462       $template->init_vars();
   476       echo PageUtils::genPreview($_POST['text']);
   463       echo PageUtils::genPreview($_POST['text']);
       
   464       break;
       
   465     case "transform":
       
   466       header('Content-type: text/javascript');
       
   467       if ( !isset($_GET['to']) )
       
   468       {
       
   469         echo enano_json_encode(array(
       
   470             'mode' => 'error',
       
   471             'error' => '"to" not specified'
       
   472           ));
       
   473         break;
       
   474       }
       
   475       if ( !isset($_POST['text']) )
       
   476       {
       
   477         echo enano_json_encode(array(
       
   478             'mode' => 'error',
       
   479             'error' => '"text" not specified (must be on POST)'
       
   480           ));
       
   481         break;
       
   482       }
       
   483       switch($_GET['to'])
       
   484       {
       
   485         case 'xhtml':
       
   486           $result = RenderMan::render($_POST['text'], RENDER_WIKI_DEFAULT | RENDER_BLOCKONLY);
       
   487           break;
       
   488         case 'wikitext':
       
   489           $result = RenderMan::reverse_render($_POST['text']);
       
   490           break;
       
   491         default:
       
   492           $text =& $_POST['text'];
       
   493           $result = false;
       
   494           $code = $plugins->setHook('ajax_transform');
       
   495           foreach ( $code as $cmd )
       
   496           {
       
   497             eval($cmd);
       
   498           }
       
   499           if ( !$result )
       
   500           {
       
   501             echo enano_json_encode(array(
       
   502                 'mode' => 'error',
       
   503                 'error' => 'Invalid target format'
       
   504               ));
       
   505             break;
       
   506           }
       
   507           break;
       
   508       }
       
   509       
       
   510       // mostly for debugging, but I suppose this could be useful elsewhere.
       
   511       if ( isset($_POST['plaintext']) )
       
   512         die($result);
       
   513       
       
   514       echo enano_json_encode(array(
       
   515           'mode' => 'transformed_text',
       
   516           'text' => $result
       
   517         ));
   477       break;
   518       break;
   478     case "pagediff":
   519     case "pagediff":
   479       require_once(ENANO_ROOT.'/includes/pageutils.php');
   520       require_once(ENANO_ROOT.'/includes/pageutils.php');
   480       $id1 = ( isset($_GET['diff1']) ) ? (int)$_GET['diff1'] : false;
   521       $id1 = ( isset($_GET['diff1']) ) ? (int)$_GET['diff1'] : false;
   481       $id2 = ( isset($_GET['diff2']) ) ? (int)$_GET['diff2'] : false;
   522       $id2 = ( isset($_GET['diff2']) ) ? (int)$_GET['diff2'] : false;