<?php
/**
 * DokuWiki AJAX call handler
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Andreas Gohr <andi@splitbrain.org>
 * @author     Otto Vainio <plugins@valjakko.net>
 */

//fix for Opera XMLHttpRequests
if(!count($_POST) && $HTTP_RAW_POST_DATA){
  parse_str($HTTP_RAW_POST_DATA, $_POST);
}

if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../../').'/');
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/common.php');
require_once(DOKU_INC.'inc/pageutils.php');
require_once(DOKU_INC.'inc/auth.php');
if (!defined('DOKU_PLUGIN')) {
  define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
}
//close sesseion
session_write_close();

header('Content-Type: text/html; charset=utf-8');

//call the requested function
if (!isset($_POST['call'])) { return; }
$call = 'ajax_'.$_POST['call'];
if(function_exists($call)){
  $call();
}else{
  $call = $_POST['call'];
  $evt = new Doku_Event('AJAX_CALL_UNKNOWN', $call);
  if ($evt->advise_before()) {
    print "AJAX call '".htmlspecialchars($_POST['call'])."' unknown!\n";
  }
  $evt->advise_after();
  unset($evt);
}


/**
 * Return subnamespaces for the Mediamanager
 */
function ajax_linkpagens(){
  global $conf;
  require_once(DOKU_INC.'inc/search.php');
  require_once('../functions.php');

  // wanted namespace
  $ns  = cleanID($_POST['ns']);
  $dir  = utf8_encodeFN(str_replace(':','/',$ns));

  $lvl = count(explode(':',$ns));

  $data = array();
  search($data,$conf['datadir'],'search_index',array('nofiles' => true),$dir);
  foreach($data as $item){
    $item['level'] = $lvl+1;
    echo linkpage_nstree_li($item);
    echo linkpage_nstree_item($item);
    echo '</li>';
  }
}

/**
 * Return subnamespaces for the Mediamanager
 */
function ajax_linkpagelist(){
  global $conf;
  require_once('../functions.php');
  linkpage_filelist($_POST['ns']);
}

//Setup VIM: ex: et ts=2 enc=utf-8 :
?>
