xref: /dokuwiki/lib/exe/ajax.php (revision 92cac9a97ee63d9c3c9bb8b0da1e3eb0604ba04f)
1f62ea8a1Sandi<?php
2f62ea8a1Sandi/**
3f62ea8a1Sandi * DokuWiki AJAX call handler
4f62ea8a1Sandi *
5f62ea8a1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6f62ea8a1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7f62ea8a1Sandi */
8f62ea8a1Sandi
9f62ea8a1Sandi//fix for Opera XMLHttpRequests
10339bd727SChristopher Smithif(!count($_POST) && !empty($HTTP_RAW_POST_DATA)){
11f62ea8a1Sandi    parse_str($HTTP_RAW_POST_DATA, $_POST);
12f62ea8a1Sandi}
13f62ea8a1Sandi
14d0a27cb0SAndreas Gohrif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
15f62ea8a1Sandirequire_once(DOKU_INC.'inc/init.php');
1624b3cb1aSAndreas Gohr//close session
178746e727Sandisession_write_close();
18f62ea8a1Sandi
1995657bc6Sandiheader('Content-Type: text/html; charset=utf-8');
2095657bc6Sandi
2195657bc6Sandi
22f62ea8a1Sandi//call the requested function
23d0b9cae1SAndreas Gohrif(isset($_POST['call'])){
24850c2e7cSAndreas Gohr    $call = $_POST['call'];
25d0b9cae1SAndreas Gohr}else if(isset($_GET['call'])){
26850c2e7cSAndreas Gohr    $call = $_GET['call'];
27d0b9cae1SAndreas Gohr}else{
28a06884abSAndreas Gohr    exit;
29d0b9cae1SAndreas Gohr}
30850c2e7cSAndreas Gohr$callfn = 'ajax_'.$call;
31850c2e7cSAndreas Gohr
32850c2e7cSAndreas Gohrif(function_exists($callfn)){
33850c2e7cSAndreas Gohr    $callfn();
34f62ea8a1Sandi}else{
353cb4b39fSBen Coburn    $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
363cb4b39fSBen Coburn    if ($evt->advise_before()) {
37850c2e7cSAndreas Gohr        print "AJAX call '".htmlspecialchars($call)."' unknown!\n";
38850c2e7cSAndreas Gohr        exit;
393cb4b39fSBen Coburn    }
403cb4b39fSBen Coburn    $evt->advise_after();
413cb4b39fSBen Coburn    unset($evt);
42f62ea8a1Sandi}
43f62ea8a1Sandi
44f62ea8a1Sandi/**
45f62ea8a1Sandi * Searches for matching pagenames
46f62ea8a1Sandi *
47f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org>
48f62ea8a1Sandi */
49f62ea8a1Sandifunction ajax_qsearch(){
50f62ea8a1Sandi    global $conf;
51f62ea8a1Sandi    global $lang;
52f62ea8a1Sandi
5380423ab6SAdrian Lang    $query = $_POST['q'];
5480423ab6SAdrian Lang    if(empty($query)) $query = $_GET['q'];
55f62ea8a1Sandi    if(empty($query)) return;
56f62ea8a1Sandi
578d22f1e9SAndreas Gohr    $data = ft_pageLookup($query, true, useHeading('navigation'));
58f62ea8a1Sandi
59f62ea8a1Sandi    if(!count($data)) return;
60f62ea8a1Sandi
6196331712SAnika Henke    print '<strong>'.$lang['quickhits'].'</strong>';
62506fa893SAndreas Gohr    print '<ul>';
6380423ab6SAdrian Lang    foreach($data as $id => $title){
6480423ab6SAdrian Lang        if (useHeading('navigation')) {
6580423ab6SAdrian Lang            $name = $title;
6680423ab6SAdrian Lang        } else {
67bd2f6c2fSAndreas Gohr            $ns = getNS($id);
68bd2f6c2fSAndreas Gohr            if($ns){
69d83e78edSAndreas Gohr                $name = noNS($id).' ('.$ns.')';
70bd2f6c2fSAndreas Gohr            }else{
71bd2f6c2fSAndreas Gohr                $name = $id;
72bd2f6c2fSAndreas Gohr            }
7380423ab6SAdrian Lang        }
7480423ab6SAdrian Lang        echo '<li>' . html_wikilink(':'.$id,$name) . '</li>';
75506fa893SAndreas Gohr    }
76506fa893SAndreas Gohr    print '</ul>';
77f62ea8a1Sandi}
78f62ea8a1Sandi
796035eb33SAndreas Gohr/**
80da96af35SAndreas Gohr * Support OpenSearch suggestions
81da96af35SAndreas Gohr *
82da96af35SAndreas Gohr * @link   http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.0
83da96af35SAndreas Gohr * @author Mike Frysinger <vapier@gentoo.org>
84da96af35SAndreas Gohr */
85da96af35SAndreas Gohrfunction ajax_suggestions() {
86da96af35SAndreas Gohr    global $conf;
87da96af35SAndreas Gohr    global $lang;
88da96af35SAndreas Gohr
89da96af35SAndreas Gohr    $query = cleanID($_POST['q']);
90da96af35SAndreas Gohr    if(empty($query)) $query = cleanID($_GET['q']);
91da96af35SAndreas Gohr    if(empty($query)) return;
92da96af35SAndreas Gohr
93da96af35SAndreas Gohr    $data = array();
94da96af35SAndreas Gohr    $data = ft_pageLookup($query);
95da96af35SAndreas Gohr    if(!count($data)) return;
968d22f1e9SAndreas Gohr    $data = array_keys($data);
97da96af35SAndreas Gohr
98da96af35SAndreas Gohr    // limit results to 15 hits
99da96af35SAndreas Gohr    $data = array_slice($data, 0, 15);
100da96af35SAndreas Gohr    $data = array_map('trim',$data);
101da96af35SAndreas Gohr    $data = array_map('noNS',$data);
102da96af35SAndreas Gohr    $data = array_unique($data);
103da96af35SAndreas Gohr    sort($data);
104da96af35SAndreas Gohr
105da96af35SAndreas Gohr    /* now construct a json */
106da96af35SAndreas Gohr    $suggestions = array(
107da96af35SAndreas Gohr                        $query,  // the original query
108da96af35SAndreas Gohr                        $data,   // some suggestions
109da96af35SAndreas Gohr                        array(), // no description
110da96af35SAndreas Gohr                        array()  // no urls
111da96af35SAndreas Gohr                   );
112da96af35SAndreas Gohr    $json = new JSON();
113da96af35SAndreas Gohr
114da96af35SAndreas Gohr    header('Content-Type: application/x-suggestions+json');
115da96af35SAndreas Gohr    print $json->encode($suggestions);
116da96af35SAndreas Gohr}
117da96af35SAndreas Gohr
118da96af35SAndreas Gohr/**
119ee4c4a1bSAndreas Gohr * Refresh a page lock and save draft
1206035eb33SAndreas Gohr *
1216035eb33SAndreas Gohr * Andreas Gohr <andi@splitbrain.org>
1226035eb33SAndreas Gohr */
1236035eb33SAndreas Gohrfunction ajax_lock(){
124ee4c4a1bSAndreas Gohr    global $conf;
125ee4c4a1bSAndreas Gohr    global $lang;
12631bc8f11SMichael Hamann    global $ID;
12731bc8f11SMichael Hamann    global $INFO;
1286035eb33SAndreas Gohr
12931bc8f11SMichael Hamann    $ID = cleanID($_POST['id']);
13031bc8f11SMichael Hamann    if(empty($ID)) return;
13131bc8f11SMichael Hamann
13231bc8f11SMichael Hamann    $INFO = pageinfo();
13331bc8f11SMichael Hamann
13431bc8f11SMichael Hamann    if (!$INFO['writable']) {
13531bc8f11SMichael Hamann        echo 'Permission denied';
13631bc8f11SMichael Hamann        return;
13731bc8f11SMichael Hamann    }
13831bc8f11SMichael Hamann
13931bc8f11SMichael Hamann    if(!checklock($ID)){
14031bc8f11SMichael Hamann        lock($ID);
141ee4c4a1bSAndreas Gohr        echo 1;
1426035eb33SAndreas Gohr    }
143ee4c4a1bSAndreas Gohr
144ee4c4a1bSAndreas Gohr    if($conf['usedraft'] && $_POST['wikitext']){
145ee4c4a1bSAndreas Gohr        $client = $_SERVER['REMOTE_USER'];
146ee4c4a1bSAndreas Gohr        if(!$client) $client = clientIP(true);
147ee4c4a1bSAndreas Gohr
14831bc8f11SMichael Hamann        $draft = array('id'     => $ID,
14942de51b1SAdrian Lang                'prefix' => substr($_POST['prefix'], 0, -1),
150ee4c4a1bSAndreas Gohr                'text'   => $_POST['wikitext'],
151ee4c4a1bSAndreas Gohr                'suffix' => $_POST['suffix'],
152d259f728SAndreas Gohr                'date'   => (int) $_POST['date'],
153ee4c4a1bSAndreas Gohr                'client' => $client,
154ee4c4a1bSAndreas Gohr                );
15531bc8f11SMichael Hamann        $cname = getCacheName($draft['client'].$ID,'.draft');
156ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
157f2263577SAndreas Gohr            echo $lang['draftdate'].' '.dformat();
158ee4c4a1bSAndreas Gohr        }
159ee4c4a1bSAndreas Gohr    }
160ee4c4a1bSAndreas Gohr
161ee4c4a1bSAndreas Gohr}
162ee4c4a1bSAndreas Gohr
163ee4c4a1bSAndreas Gohr/**
164ee4c4a1bSAndreas Gohr * Delete a draft
1653df72098SAndreas Gohr *
1663df72098SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
167ee4c4a1bSAndreas Gohr */
168ee4c4a1bSAndreas Gohrfunction ajax_draftdel(){
16940f3c0b5SGina Haeussge    $id = cleanID($_REQUEST['id']);
170ee4c4a1bSAndreas Gohr    if(empty($id)) return;
171ee4c4a1bSAndreas Gohr
172ee4c4a1bSAndreas Gohr    $client = $_SERVER['REMOTE_USER'];
173ee4c4a1bSAndreas Gohr    if(!$client) $client = clientIP(true);
174ee4c4a1bSAndreas Gohr
175ee4c4a1bSAndreas Gohr    $cname = getCacheName($client.$id,'.draft');
176ee4c4a1bSAndreas Gohr    @unlink($cname);
1776035eb33SAndreas Gohr}
1786035eb33SAndreas Gohr
1793df72098SAndreas Gohr/**
1803df72098SAndreas Gohr * Return subnamespaces for the Mediamanager
181a06884abSAndreas Gohr *
182a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
1833df72098SAndreas Gohr */
1843df72098SAndreas Gohrfunction ajax_medians(){
1853df72098SAndreas Gohr    global $conf;
1863df72098SAndreas Gohr
1873df72098SAndreas Gohr    // wanted namespace
1883df72098SAndreas Gohr    $ns  = cleanID($_POST['ns']);
1893df72098SAndreas Gohr    $dir  = utf8_encodeFN(str_replace(':','/',$ns));
1903df72098SAndreas Gohr
1913df72098SAndreas Gohr    $lvl = count(explode(':',$ns));
1923df72098SAndreas Gohr
1933df72098SAndreas Gohr    $data = array();
194ee7b5a62SAndreas Gohr    search($data,$conf['mediadir'],'search_index',array('nofiles' => true),$dir);
195a1dee2b9SAdrian Lang    foreach(array_keys($data) as $item){
196a1dee2b9SAdrian Lang        $data[$item]['level'] = $lvl+1;
1973df72098SAndreas Gohr    }
198a1dee2b9SAdrian Lang    echo html_buildlist($data, 'idx', 'media_nstree_item', 'media_nstree_li');
1993df72098SAndreas Gohr}
2003df72098SAndreas Gohr
2013df72098SAndreas Gohr/**
202a06884abSAndreas Gohr * Return list of files for the Mediamanager
203a06884abSAndreas Gohr *
204a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2053df72098SAndreas Gohr */
2063df72098SAndreas Gohrfunction ajax_medialist(){
2073df72098SAndreas Gohr    global $conf;
208c182313eSAndreas Gohr    global $NS;
2093df72098SAndreas Gohr
210c182313eSAndreas Gohr    $NS = $_POST['ns'];
21123846a98SKate Arzamastseva    if ($_POST['do'] == 'media') {
212ed69a2aeSKate Arzamastseva        tpl_fileList();
21323846a98SKate Arzamastseva    } else {
214c182313eSAndreas Gohr        tpl_mediaContent(true);
2153df72098SAndreas Gohr    }
21623846a98SKate Arzamastseva}
21723846a98SKate Arzamastseva
21823846a98SKate Arzamastseva/**
21923846a98SKate Arzamastseva * Return the content of the right column
22023846a98SKate Arzamastseva * (image details) for the Mediamanager
22123846a98SKate Arzamastseva *
22223846a98SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
22323846a98SKate Arzamastseva */
22423846a98SKate Arzamastsevafunction ajax_mediadetails(){
225*92cac9a9SKate Arzamastseva    global $DEL, $NS, $IMG, $AUTH, $JUMPTO, $REV, $lang, $fullscreen, $conf;
2267d7ab775SKate Arzamastseva    $fullscreen = true;
2277d7ab775SKate Arzamastseva    require_once(DOKU_INC.'lib/exe/mediamanager.php');
22823846a98SKate Arzamastseva
2297d7ab775SKate Arzamastseva    if ($_REQUEST['image']) $image = cleanID($_REQUEST['image']);
2307d7ab775SKate Arzamastseva    if (isset($IMG)) $image = $IMG;
2317d7ab775SKate Arzamastseva    if (isset($JUMPTO)) $image = $JUMPTO;
2327d7ab775SKate Arzamastseva    if (isset($REV) && !$JUMPTO) $rev = $REV;
2337d7ab775SKate Arzamastseva
2347d7ab775SKate Arzamastseva    html_msgarea();
2357d7ab775SKate Arzamastseva    tpl_fileDetails($image, $rev);
23623846a98SKate Arzamastseva}
2373df72098SAndreas Gohr
238a06884abSAndreas Gohr/**
2394ee15585SKate Arzamastseva * Returns image diff representation for mediamanager
2404ee15585SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
2414ee15585SKate Arzamastseva */
2424ee15585SKate Arzamastsevafunction ajax_mediadiff(){
2434ee15585SKate Arzamastseva    global $NS;
2444ee15585SKate Arzamastseva
2454ee15585SKate Arzamastseva    if ($_REQUEST['image']) $image = cleanID($_REQUEST['image']);
2464ee15585SKate Arzamastseva    $NS = $_POST['ns'];
2474ee15585SKate Arzamastseva    $auth = auth_quickaclcheck("$ns:*");
2484ee15585SKate Arzamastseva    media_diff($image, $NS, $auth);
2494ee15585SKate Arzamastseva}
2504ee15585SKate Arzamastseva
2514ee15585SKate Arzamastseva/**
252a06884abSAndreas Gohr * Return sub index for index view
253a06884abSAndreas Gohr *
254a06884abSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
255a06884abSAndreas Gohr */
256a06884abSAndreas Gohrfunction ajax_index(){
257a06884abSAndreas Gohr    global $conf;
258a06884abSAndreas Gohr
259a06884abSAndreas Gohr    // wanted namespace
260a06884abSAndreas Gohr    $ns  = cleanID($_POST['idx']);
261a06884abSAndreas Gohr    $dir  = utf8_encodeFN(str_replace(':','/',$ns));
262a06884abSAndreas Gohr
263a06884abSAndreas Gohr    $lvl = count(explode(':',$ns));
264a06884abSAndreas Gohr
265a06884abSAndreas Gohr    $data = array();
266a06884abSAndreas Gohr    search($data,$conf['datadir'],'search_index',array('ns' => $ns),$dir);
267a1dee2b9SAdrian Lang    foreach(array_keys($data) as $item){
268a1dee2b9SAdrian Lang        $data[$item]['level'] = $lvl+1;
269a06884abSAndreas Gohr    }
270a1dee2b9SAdrian Lang    echo html_buildlist($data, 'idx', 'html_list_index', 'html_li_index');
271a06884abSAndreas Gohr}
272a06884abSAndreas Gohr
27356dfcc12SAndreas Gohr/**
27456dfcc12SAndreas Gohr * List matching namespaces and pages for the link wizard
2753e23cbfdSAndreas Gohr *
2763e23cbfdSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
27756dfcc12SAndreas Gohr */
27856dfcc12SAndreas Gohrfunction ajax_linkwiz(){
27956dfcc12SAndreas Gohr    global $conf;
28056dfcc12SAndreas Gohr    global $lang;
28156dfcc12SAndreas Gohr
282c6497d39SAndreas Gohr    $q  = ltrim(trim($_POST['q']),':');
28356dfcc12SAndreas Gohr    $id = noNS($q);
28456dfcc12SAndreas Gohr    $ns = getNS($q);
28556dfcc12SAndreas Gohr
28656dfcc12SAndreas Gohr    $ns = cleanID($ns);
28756dfcc12SAndreas Gohr    $id = cleanID($id);
28856dfcc12SAndreas Gohr
28956dfcc12SAndreas Gohr    $nsd  = utf8_encodeFN(str_replace(':','/',$ns));
29056dfcc12SAndreas Gohr    $idd  = utf8_encodeFN(str_replace(':','/',$id));
29156dfcc12SAndreas Gohr
29256dfcc12SAndreas Gohr    $data = array();
29356dfcc12SAndreas Gohr    if($q && !$ns){
29456dfcc12SAndreas Gohr
29556dfcc12SAndreas Gohr        // use index to lookup matching pages
29656dfcc12SAndreas Gohr        $pages = array();
2978d22f1e9SAndreas Gohr        $pages = ft_pageLookup($id,true);
29856dfcc12SAndreas Gohr
29956dfcc12SAndreas Gohr        // result contains matches in pages and namespaces
30056dfcc12SAndreas Gohr        // we now extract the matching namespaces to show
30156dfcc12SAndreas Gohr        // them seperately
30256dfcc12SAndreas Gohr        $dirs  = array();
3038d22f1e9SAndreas Gohr
3048d22f1e9SAndreas Gohr        foreach($pages as $pid => $title){
3058d22f1e9SAndreas Gohr            if(strpos(noNS($pid),$id) === false){
30656dfcc12SAndreas Gohr                // match was in the namespace
3078d22f1e9SAndreas Gohr                $dirs[getNS($pid)] = 1; // assoc array avoids dupes
30856dfcc12SAndreas Gohr            }else{
30956dfcc12SAndreas Gohr                // it is a matching page, add it to the result
31056dfcc12SAndreas Gohr                $data[] = array(
3118d22f1e9SAndreas Gohr                        'id'    => $pid,
3128d22f1e9SAndreas Gohr                        'title' => $title,
31356dfcc12SAndreas Gohr                        'type'  => 'f',
31456dfcc12SAndreas Gohr                        );
31556dfcc12SAndreas Gohr            }
3168d22f1e9SAndreas Gohr            unset($pages[$pid]);
31756dfcc12SAndreas Gohr        }
31856dfcc12SAndreas Gohr        foreach($dirs as $dir => $junk){
31956dfcc12SAndreas Gohr            $data[] = array(
32056dfcc12SAndreas Gohr                    'id'   => $dir,
32156dfcc12SAndreas Gohr                    'type' => 'd',
32256dfcc12SAndreas Gohr                    );
32356dfcc12SAndreas Gohr        }
32456dfcc12SAndreas Gohr
32556dfcc12SAndreas Gohr    }else{
32656dfcc12SAndreas Gohr
32756dfcc12SAndreas Gohr        $opts = array(
32856dfcc12SAndreas Gohr                'depth' => 1,
32956dfcc12SAndreas Gohr                'listfiles' => true,
33056dfcc12SAndreas Gohr                'listdirs'  => true,
33156dfcc12SAndreas Gohr                'pagesonly' => true,
33256dfcc12SAndreas Gohr                'firsthead' => true,
3339dde1138SAndreas Gohr                'sneakyacl' => $conf['sneaky_index'],
33456dfcc12SAndreas Gohr                );
33556dfcc12SAndreas Gohr        if($id) $opts['filematch'] = '^.*\/'.$id;
33656dfcc12SAndreas Gohr        if($id) $opts['dirmatch']  = '^.*\/'.$id;
33756dfcc12SAndreas Gohr        search($data,$conf['datadir'],'search_universal',$opts,$nsd);
33856dfcc12SAndreas Gohr
33956dfcc12SAndreas Gohr        // add back to upper
34056dfcc12SAndreas Gohr        if($ns){
34156dfcc12SAndreas Gohr            array_unshift($data,array(
34256dfcc12SAndreas Gohr                        'id'   => getNS($ns),
34356dfcc12SAndreas Gohr                        'type' => 'u',
34456dfcc12SAndreas Gohr                        ));
34556dfcc12SAndreas Gohr        }
34656dfcc12SAndreas Gohr    }
34756dfcc12SAndreas Gohr
34856dfcc12SAndreas Gohr    // fixme sort results in a useful way ?
34956dfcc12SAndreas Gohr
35056dfcc12SAndreas Gohr    if(!count($data)){
35156dfcc12SAndreas Gohr        echo $lang['nothingfound'];
35256dfcc12SAndreas Gohr        exit;
35356dfcc12SAndreas Gohr    }
35456dfcc12SAndreas Gohr
35556dfcc12SAndreas Gohr    // output the found data
35656dfcc12SAndreas Gohr    $even = 1;
35756dfcc12SAndreas Gohr    foreach($data as $item){
35856dfcc12SAndreas Gohr        $even *= -1; //zebra
35956dfcc12SAndreas Gohr
36056dfcc12SAndreas Gohr        if(($item['type'] == 'd' || $item['type'] == 'u') && $item['id']) $item['id'] .= ':';
36156dfcc12SAndreas Gohr        $link = wl($item['id']);
36256dfcc12SAndreas Gohr
36356dfcc12SAndreas Gohr        echo '<div class="'.(($even > 0)?'even':'odd').' type_'.$item['type'].'">';
36456dfcc12SAndreas Gohr
36556dfcc12SAndreas Gohr        if($item['type'] == 'u'){
3663e23cbfdSAndreas Gohr            $name = $lang['upperns'];
36756dfcc12SAndreas Gohr        }else{
36856dfcc12SAndreas Gohr            $name = htmlspecialchars($item['id']);
36956dfcc12SAndreas Gohr        }
37056dfcc12SAndreas Gohr
37156dfcc12SAndreas Gohr        echo '<a href="'.$link.'" title="'.htmlspecialchars($item['id']).'" class="wikilink1">'.$name.'</a>';
37256dfcc12SAndreas Gohr
37356dfcc12SAndreas Gohr        if($item['title']){
37456dfcc12SAndreas Gohr            echo '<span>'.htmlspecialchars($item['title']).'</span>';
37556dfcc12SAndreas Gohr        }
37656dfcc12SAndreas Gohr        echo '</div>';
37756dfcc12SAndreas Gohr    }
37856dfcc12SAndreas Gohr
37956dfcc12SAndreas Gohr}
38056dfcc12SAndreas Gohr
381e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
382