xref: /dokuwiki/lib/exe/ajax.php (revision 5aa52fafe8be8e728c0d2c9ff12c999e80766127)
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
26if(isset($_POST['call']))
27  $call = 'ajax_'.$_POST['call'];
28else if(isset($_GET['call']))
29  $call = 'ajax_'.$_GET['call'];
30else
31  return;
32if(function_exists($call)){
33  $call();
34}else{
35  $call = $_POST['call'];
36  $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
37  if ($evt->advise_before()) {
38    print "AJAX call '".htmlspecialchars($_POST['call'])."' unknown!\n";
39  }
40  $evt->advise_after();
41  unset($evt);
42}
43
44/**
45 * Searches for matching pagenames
46 *
47 * @author Andreas Gohr <andi@splitbrain.org>
48 */
49function ajax_qsearch(){
50  global $conf;
51  global $lang;
52
53  $query = cleanID($_POST['q']);
54  if(empty($query)) $query = cleanID($_GET['q']);
55  if(empty($query)) return;
56
57  require_once(DOKU_INC.'inc/html.php');
58  require_once(DOKU_INC.'inc/fulltext.php');
59
60  $data = array();
61  $data = ft_pageLookup($query);
62
63  if(!count($data)) return;
64
65  print '<strong>'.$lang['quickhits'].'</strong>';
66  print '<ul>';
67  foreach($data as $id){
68    print '<li>';
69    print html_wikilink(':'.$id);
70    print '</li>';
71  }
72  print '</ul>';
73}
74
75/**
76 * Support OpenSearch suggestions
77 *
78 * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
79 * @author Mike Frysinger <vapier@gentoo.org>
80 */
81function ajax_suggestions() {
82  global $conf;
83  global $lang;
84
85  $query = cleanID($_POST['q']);
86  if(empty($query)) $query = cleanID($_GET['q']);
87  if(empty($query)) return;
88
89  require_once(DOKU_INC.'inc/html.php');
90  require_once(DOKU_INC.'inc/fulltext.php');
91  require_once(DOKU_INC.'inc/JSON.php');
92
93  $data = array();
94  $data = ft_pageLookup($query);
95  if(!count($data)) return;
96
97  // limit results to 15 hits
98  $data = array_slice($data, 0, 15);
99  $data = array_map('trim',$data);
100  $data = array_map('noNS',$data);
101  $data = array_unique($data);
102  sort($data);
103
104  /* now construct a json */
105  $suggestions = array(
106    $query,  // the original query
107    $data,   // some suggestions
108    array(), // no description
109    array()  // no urls
110  );
111  $json = new JSON();
112
113  header('Content-Type: application/x-suggestions+json');
114  print $json->encode($suggestions);
115}
116
117/**
118 * Refresh a page lock and save draft
119 *
120 * Andreas Gohr <andi@splitbrain.org>
121 */
122function ajax_lock(){
123  global $conf;
124  global $lang;
125  $id = cleanID($_POST['id']);
126  if(empty($id)) return;
127
128  if(!checklock($id)){
129    lock($id);
130    echo 1;
131  }
132
133  if($conf['usedraft'] && $_POST['wikitext']){
134    $client = $_SERVER['REMOTE_USER'];
135    if(!$client) $client = clientIP(true);
136
137    $draft = array('id'     => $id,
138                   'prefix' => $_POST['prefix'],
139                   'text'   => $_POST['wikitext'],
140                   'suffix' => $_POST['suffix'],
141                   'date'   => $_POST['date'],
142                   'client' => $client,
143                  );
144    $cname = getCacheName($draft['client'].$id,'.draft');
145    if(io_saveFile($cname,serialize($draft))){
146      echo $lang['draftdate'].' '.date($conf['dformat']);
147    }
148  }
149
150}
151
152/**
153 * Delete a draft
154 *
155 * @author Andreas Gohr <andi@splitbrain.org>
156 */
157function ajax_draftdel(){
158  $id = cleanID($_POST['id']);
159  if(empty($id)) return;
160
161  $client = $_SERVER['REMOTE_USER'];
162  if(!$client) $client = clientIP(true);
163
164  $cname = getCacheName($client.$id,'.draft');
165  @unlink($cname);
166}
167
168/**
169 * Return subnamespaces for the Mediamanager
170 */
171function ajax_medians(){
172  global $conf;
173  require_once(DOKU_INC.'inc/search.php');
174  require_once(DOKU_INC.'inc/media.php');
175
176  // wanted namespace
177  $ns  = cleanID($_POST['ns']);
178  $dir  = utf8_encodeFN(str_replace(':','/',$ns));
179
180  $lvl = count(explode(':',$ns));
181
182  $data = array();
183  search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
184  foreach($data as $item){
185    $item['level'] = $lvl+1;
186    echo media_nstree_li($item);
187    echo media_nstree_item($item);
188    echo '</li>';
189  }
190}
191
192/**
193 * Return subnamespaces for the Mediamanager
194 */
195function ajax_medialist(){
196  global $conf;
197  require_once(DOKU_INC.'inc/media.php');
198
199  media_filelist($_POST['ns']);
200}
201
202//Setup VIM: ex: et ts=2 enc=utf-8 :
203?>
204