Ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. namespace dokuwiki;
  3. use dokuwiki\Ui;
  4. use dokuwiki\Utf8\Sort;
  5. /**
  6. * Manage all builtin AJAX calls
  7. *
  8. * @todo The calls should be refactored out to their own proper classes
  9. * @package dokuwiki
  10. */
  11. class Ajax {
  12. /**
  13. * Execute the given call
  14. *
  15. * @param string $call name of the ajax call
  16. */
  17. public function __construct($call) {
  18. $callfn = 'call' . ucfirst($call);
  19. if(method_exists($this, $callfn)) {
  20. $this->$callfn();
  21. } else {
  22. $evt = new Extension\Event('AJAX_CALL_UNKNOWN', $call);
  23. if($evt->advise_before()) {
  24. print "AJAX call '" . hsc($call) . "' unknown!\n";
  25. } else {
  26. $evt->advise_after();
  27. unset($evt);
  28. }
  29. }
  30. }
  31. /**
  32. * Searches for matching pagenames
  33. *
  34. * @author Andreas Gohr <andi@splitbrain.org>
  35. */
  36. protected function callQsearch() {
  37. global $lang;
  38. global $INPUT;
  39. $maxnumbersuggestions = 50;
  40. $query = $INPUT->post->str('q');
  41. if(empty($query)) $query = $INPUT->get->str('q');
  42. if(empty($query)) return;
  43. $query = urldecode($query);
  44. $data = ft_pageLookup($query, true, useHeading('navigation'));
  45. if(!count($data)) return;
  46. print '<strong>' . $lang['quickhits'] . '</strong>';
  47. print '<ul>';
  48. $counter = 0;
  49. foreach($data as $id => $title) {
  50. if(useHeading('navigation')) {
  51. $name = $title;
  52. } else {
  53. $ns = getNS($id);
  54. if($ns) {
  55. $name = noNS($id) . ' (' . $ns . ')';
  56. } else {
  57. $name = $id;
  58. }
  59. }
  60. echo '<li>' . html_wikilink(':' . $id, $name) . '</li>';
  61. $counter++;
  62. if($counter > $maxnumbersuggestions) {
  63. echo '<li>...</li>';
  64. break;
  65. }
  66. }
  67. print '</ul>';
  68. }
  69. /**
  70. * Support OpenSearch suggestions
  71. *
  72. * @link http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
  73. * @author Mike Frysinger <vapier@gentoo.org>
  74. */
  75. protected function callSuggestions() {
  76. global $INPUT;
  77. $query = cleanID($INPUT->post->str('q'));
  78. if(empty($query)) $query = cleanID($INPUT->get->str('q'));
  79. if(empty($query)) return;
  80. $data = ft_pageLookup($query);
  81. if(!count($data)) return;
  82. $data = array_keys($data);
  83. // limit results to 15 hits
  84. $data = array_slice($data, 0, 15);
  85. $data = array_map('trim', $data);
  86. $data = array_map('noNS', $data);
  87. $data = array_unique($data);
  88. Sort::sort($data);
  89. /* now construct a json */
  90. $suggestions = array(
  91. $query, // the original query
  92. $data, // some suggestions
  93. array(), // no description
  94. array() // no urls
  95. );
  96. header('Content-Type: application/x-suggestions+json');
  97. print json_encode($suggestions);
  98. }
  99. /**
  100. * Refresh a page lock and save draft
  101. *
  102. * Andreas Gohr <andi@splitbrain.org>
  103. */
  104. protected function callLock() {
  105. global $ID;
  106. global $INFO;
  107. global $INPUT;
  108. $ID = cleanID($INPUT->post->str('id'));
  109. if(empty($ID)) return;
  110. $INFO = pageinfo();
  111. $response = [
  112. 'errors' => [],
  113. 'lock' => '0',
  114. 'draft' => '',
  115. ];
  116. if(!$INFO['writable']) {
  117. $response['errors'][] = 'Permission to write this page has been denied.';
  118. echo json_encode($response);
  119. return;
  120. }
  121. if(!checklock($ID)) {
  122. lock($ID);
  123. $response['lock'] = '1';
  124. }
  125. $draft = new Draft($ID, $INFO['client']);
  126. if ($draft->saveDraft()) {
  127. $response['draft'] = $draft->getDraftMessage();
  128. } else {
  129. $response['errors'] = array_merge($response['errors'], $draft->getErrors());
  130. }
  131. echo json_encode($response);
  132. }
  133. /**
  134. * Delete a draft
  135. *
  136. * @author Andreas Gohr <andi@splitbrain.org>
  137. */
  138. protected function callDraftdel() {
  139. global $INPUT;
  140. $id = cleanID($INPUT->str('id'));
  141. if(empty($id)) return;
  142. $client = $INPUT->server->str('REMOTE_USER');
  143. if(!$client) $client = clientIP(true);
  144. $draft = new Draft($id, $client);
  145. if ($draft->isDraftAvailable() && checkSecurityToken()) {
  146. $draft->deleteDraft();
  147. }
  148. }
  149. /**
  150. * Return subnamespaces for the Mediamanager
  151. *
  152. * @author Andreas Gohr <andi@splitbrain.org>
  153. */
  154. protected function callMedians() {
  155. global $conf;
  156. global $INPUT;
  157. // wanted namespace
  158. $ns = cleanID($INPUT->post->str('ns'));
  159. $dir = utf8_encodeFN(str_replace(':', '/', $ns));
  160. $lvl = count(explode(':', $ns));
  161. $data = array();
  162. search($data, $conf['mediadir'], 'search_index', array('nofiles' => true), $dir);
  163. foreach(array_keys($data) as $item) {
  164. $data[$item]['level'] = $lvl + 1;
  165. }
  166. echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li');
  167. }
  168. /**
  169. * Return list of files for the Mediamanager
  170. *
  171. * @author Andreas Gohr <andi@splitbrain.org>
  172. */
  173. protected function callMedialist() {
  174. global $NS;
  175. global $INPUT;
  176. $NS = cleanID($INPUT->post->str('ns'));
  177. $sort = $INPUT->post->bool('recent') ? 'date' : 'natural';
  178. if($INPUT->post->str('do') == 'media') {
  179. tpl_mediaFileList();
  180. } else {
  181. tpl_mediaContent(true, $sort);
  182. }
  183. }
  184. /**
  185. * Return the content of the right column
  186. * (image details) for the Mediamanager
  187. *
  188. * @author Kate Arzamastseva <pshns@ukr.net>
  189. */
  190. protected function callMediadetails() {
  191. global $IMG, $JUMPTO, $REV, $fullscreen, $INPUT;
  192. $fullscreen = true;
  193. require_once(DOKU_INC . 'lib/exe/mediamanager.php');
  194. $image = '';
  195. if($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
  196. if(isset($IMG)) $image = $IMG;
  197. if(isset($JUMPTO)) $image = $JUMPTO;
  198. $rev = false;
  199. if(isset($REV) && !$JUMPTO) $rev = $REV;
  200. html_msgarea();
  201. tpl_mediaFileDetails($image, $rev);
  202. }
  203. /**
  204. * Returns image diff representation for mediamanager
  205. *
  206. * @author Kate Arzamastseva <pshns@ukr.net>
  207. */
  208. protected function callMediadiff() {
  209. global $INPUT;
  210. $image = '';
  211. if($INPUT->has('image')) $image = cleanID($INPUT->str('image'));
  212. (new Ui\MediaDiff($image))->preference('fromAjax', true)->show();
  213. }
  214. /**
  215. * Manages file uploads
  216. *
  217. * @author Kate Arzamastseva <pshns@ukr.net>
  218. */
  219. protected function callMediaupload() {
  220. global $NS, $MSG, $INPUT;
  221. $id = '';
  222. if(isset($_FILES['qqfile']['tmp_name'])) {
  223. $id = $INPUT->post->str('mediaid', $_FILES['qqfile']['name']);
  224. } elseif($INPUT->get->has('qqfile')) {
  225. $id = $INPUT->get->str('qqfile');
  226. }
  227. $id = cleanID($id);
  228. $NS = $INPUT->str('ns');
  229. $ns = $NS . ':' . getNS($id);
  230. $AUTH = auth_quickaclcheck("$ns:*");
  231. if($AUTH >= AUTH_UPLOAD) {
  232. io_createNamespace("$ns:xxx", 'media');
  233. }
  234. if(isset($_FILES['qqfile']['error']) && $_FILES['qqfile']['error']) unset($_FILES['qqfile']);
  235. $res = false;
  236. if(isset($_FILES['qqfile']['tmp_name'])) $res = media_upload($NS, $AUTH, $_FILES['qqfile']);
  237. if($INPUT->get->has('qqfile')) $res = media_upload_xhr($NS, $AUTH);
  238. if($res) {
  239. $result = array(
  240. 'success' => true,
  241. 'link' => media_managerURL(array('ns' => $ns, 'image' => $NS . ':' . $id), '&'),
  242. 'id' => $NS . ':' . $id,
  243. 'ns' => $NS
  244. );
  245. } else {
  246. $error = '';
  247. if(isset($MSG)) {
  248. foreach($MSG as $msg) {
  249. $error .= $msg['msg'];
  250. }
  251. }
  252. $result = array(
  253. 'error' => $error,
  254. 'ns' => $NS
  255. );
  256. }
  257. header('Content-Type: application/json');
  258. echo json_encode($result);
  259. }
  260. /**
  261. * Return sub index for index view
  262. *
  263. * @author Andreas Gohr <andi@splitbrain.org>
  264. */
  265. protected function callIndex() {
  266. global $conf;
  267. global $INPUT;
  268. // wanted namespace
  269. $ns = cleanID($INPUT->post->str('idx'));
  270. $dir = utf8_encodeFN(str_replace(':', '/', $ns));
  271. $lvl = count(explode(':', $ns));
  272. $data = array();
  273. search($data, $conf['datadir'], 'search_index', array('ns' => $ns), $dir);
  274. foreach (array_keys($data) as $item) {
  275. $data[$item]['level'] = $lvl + 1;
  276. }
  277. $idx = new Ui\Index;
  278. echo html_buildlist($data, 'idx', [$idx,'formatListItem'], [$idx,'tagListItem']);
  279. }
  280. /**
  281. * List matching namespaces and pages for the link wizard
  282. *
  283. * @author Andreas Gohr <gohr@cosmocode.de>
  284. */
  285. protected function callLinkwiz() {
  286. global $conf;
  287. global $lang;
  288. global $INPUT;
  289. $q = ltrim(trim($INPUT->post->str('q')), ':');
  290. $id = noNS($q);
  291. $ns = getNS($q);
  292. $ns = cleanID($ns);
  293. $id = cleanID($id);
  294. $nsd = utf8_encodeFN(str_replace(':', '/', $ns));
  295. $data = array();
  296. if($q !== '' && $ns === '') {
  297. // use index to lookup matching pages
  298. $pages = ft_pageLookup($id, true);
  299. // If 'useheading' option is 'always' or 'content',
  300. // search page titles with original query as well.
  301. if ($conf['useheading'] == '1' || $conf['useheading'] == 'content') {
  302. $pages = array_merge($pages, ft_pageLookup($q, true, true));
  303. asort($pages, SORT_STRING);
  304. }
  305. // result contains matches in pages and namespaces
  306. // we now extract the matching namespaces to show
  307. // them seperately
  308. $dirs = array();
  309. foreach($pages as $pid => $title) {
  310. if(strpos(getNS($pid), $id) !== false) {
  311. // match was in the namespace
  312. $dirs[getNS($pid)] = 1; // assoc array avoids dupes
  313. } else {
  314. // it is a matching page, add it to the result
  315. $data[] = array(
  316. 'id' => $pid,
  317. 'title' => $title,
  318. 'type' => 'f',
  319. );
  320. }
  321. unset($pages[$pid]);
  322. }
  323. foreach($dirs as $dir => $junk) {
  324. $data[] = array(
  325. 'id' => $dir,
  326. 'type' => 'd',
  327. );
  328. }
  329. } else {
  330. $opts = array(
  331. 'depth' => 1,
  332. 'listfiles' => true,
  333. 'listdirs' => true,
  334. 'pagesonly' => true,
  335. 'firsthead' => true,
  336. 'sneakyacl' => $conf['sneaky_index'],
  337. );
  338. if($id) $opts['filematch'] = '^.*\/' . $id;
  339. if($id) $opts['dirmatch'] = '^.*\/' . $id;
  340. search($data, $conf['datadir'], 'search_universal', $opts, $nsd);
  341. // add back to upper
  342. if($ns) {
  343. array_unshift(
  344. $data, array(
  345. 'id' => getNS($ns),
  346. 'type' => 'u',
  347. )
  348. );
  349. }
  350. }
  351. // fixme sort results in a useful way ?
  352. if(!count($data)) {
  353. echo $lang['nothingfound'];
  354. exit;
  355. }
  356. // output the found data
  357. $even = 1;
  358. foreach($data as $item) {
  359. $even *= -1; //zebra
  360. if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id'] !== '') $item['id'] .= ':';
  361. $link = wl($item['id']);
  362. echo '<div class="' . (($even > 0) ? 'even' : 'odd') . ' type_' . $item['type'] . '">';
  363. if($item['type'] == 'u') {
  364. $name = $lang['upperns'];
  365. } else {
  366. $name = hsc($item['id']);
  367. }
  368. echo '<a href="' . $link . '" title="' . hsc($item['id']) . '" class="wikilink1">' . $name . '</a>';
  369. if(!blank($item['title'])) {
  370. echo '<span>' . hsc($item['title']) . '</span>';
  371. }
  372. echo '</div>';
  373. }
  374. }
  375. }