<?php
/**
 * AJAX call handler for Dokutranslate plugin
 * Based on built-in ACL plugin
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Martin Doucha <next_ghost@quick.cz>
 * @author     Andreas Gohr <andi@splitbrain.org>
 */

//fix for Opera XMLHttpRequests
if(!count($_POST) && !empty($HTTP_RAW_POST_DATA)){
	parse_str($HTTP_RAW_POST_DATA, $_POST);
}

if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../../');
require_once(DOKU_INC.'inc/init.php');
//close session
session_write_close();

if(!auth_isadmin()) die('for admins only');
if(!checkSecurityToken()) die('CRSF Attack');

$ID    = getID();

$dokutranslate = plugin_load('admin','dokutranslate');
$dokutranslate->handle();

$ajax = $_REQUEST['ajax'];
header('Content-Type: text/html; charset=utf-8');

if($ajax == 'modform'){
	$dokutranslate->_html_modform();
}elseif($ajax == 'tree'){
	global $conf;
	global $ID;

	$dir = $conf['datadir'];
	$ns  = $_REQUEST['ns'];

	if($ns == '*'){
		$ns ='';
	}

	$ns  = cleanID($ns);
	$lvl = count(explode(':',$ns));
	$ns  = utf8_encodeFN(str_replace(':','/',$ns));

	$data = $dokutranslate->_get_tree($ns,$ns);

	foreach(array_keys($data) as $item){
		$data[$item]['level'] = $lvl+1;
	}

	echo html_buildlist($data, 'dokutranslate', array($dokutranslate, '_html_list_acl'), array($dokutranslate, '_html_li_acl'));
}

