xref: /dokuwiki/lib/exe/ajax.php (revision b4f284ac58f1be8e0efde3ef86ba0fcb88b49bb4)
1<?php
2/**
3 * DokuWiki AJAX call handler
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9//fix for Opera XMLHttpRequests
10if(!count($_POST) && $HTTP_RAW_POST_DATA){
11  parse_str($HTTP_RAW_POST_DATA, $_POST);
12}
13
14if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
15require_once(DOKU_INC.'inc/init.php');
16require_once(DOKU_INC.'inc/common.php');
17require_once(DOKU_INC.'inc/pageutils.php');
18require_once(DOKU_INC.'inc/auth.php');
19//close sesseion
20session_write_close();
21
22header('Content-Type: text/html; charset=utf-8');
23
24
25//call the requested function
26$call = 'ajax_'.$_POST['call'];
27if(function_exists($call)){
28  $call();
29}else{
30  print "The called function '".htmlspecialchars($call)."' does not exist!";
31}
32
33/**
34 * Searches for matching pagenames
35 *
36 * @author Andreas Gohr <andi@splitbrain.org>
37 */
38function ajax_qsearch(){
39  global $conf;
40  global $lang;
41
42  $query = cleanID($_POST['q']);
43  if(empty($query)) return;
44
45  require_once(DOKU_INC.'inc/html.php');
46  require_once(DOKU_INC.'inc/fulltext.php');
47
48  $data = array();
49  $data = ft_pageLookup($query);
50
51  if(!count($data)) return;
52
53  print '<strong>'.$lang['quickhits'].'</strong>';
54  print '<ul>';
55  foreach($data as $id){
56    print '<li>';
57    print html_wikilink(':'.$id);
58    print '</li>';
59  }
60  print '</ul>';
61}
62
63/**
64 * Refresh a page lock and save draft
65 *
66 * Andreas Gohr <andi@splitbrain.org>
67 */
68function ajax_lock(){
69  global $conf;
70  global $lang;
71  $id = cleanID($_POST['id']);
72  if(empty($id)) return;
73
74  if(!checklock($id)){
75    lock($id);
76    echo 1;
77  }
78
79  if($conf['usedraft'] && $_POST['wikitext']){
80    $client = $_SERVER['REMOTE_USER'];
81    if(!$client) $client = clientIP(true);
82
83    $draft = array('id'     => $id,
84                   'prefix' => $_POST['prefix'],
85                   'text'   => $_POST['wikitext'],
86                   'suffix' => $_POST['suffix'],
87                   'date'   => $_POST['date'],
88                   'client' => $client,
89                  );
90    $cname = getCacheName($draft['client'].$id,'.draft');
91    if(io_saveFile($cname,serialize($draft))){
92      echo $lang['draftdate'].' '.date($conf['dformat']);
93    }
94  }
95
96}
97
98/**
99 * Delete a draft
100 *
101 * @author Andreas Gohr <andi@splitbrain.org>
102 */
103function ajax_draftdel(){
104  $id = cleanID($_POST['id']);
105  if(empty($id)) return;
106
107  $client = $_SERVER['REMOTE_USER'];
108  if(!$client) $client = clientIP(true);
109
110  $cname = getCacheName($client.$id,'.draft');
111  @unlink($cname);
112}
113
114/**
115 * Return subnamespaces for the Mediamanager
116 */
117function ajax_medians(){
118  global $conf;
119  require_once(DOKU_INC.'inc/search.php');
120  require_once(DOKU_INC.'inc/media.php');
121
122  // wanted namespace
123  $ns  = cleanID($_POST['ns']);
124  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
125
126  $lvl = count(explode(':',$ns));
127
128  $data = array();
129  search($data,$conf['mediadir'],'search_index',array(),$dir);
130  foreach($data as $item){
131    $item['level'] = $lvl+1;
132    echo media_nstree_li($item);
133    echo media_nstree_item($item);
134    echo '</li>';
135  }
136}
137
138/**
139 * Return subnamespaces for the Mediamanager
140 */
141function ajax_medialist(){
142  global $conf;
143  require_once(DOKU_INC.'inc/media.php');
144
145  media_filelist($_POST['ns']);
146}
147
148//Setup VIM: ex: et ts=2 enc=utf-8 :
149?>
150