<?php
require_once("../config.php");
$lang = $_GET['lang'];
if(!preg_match('/[a-z]{2}/',$lang)) exit;
$sql = "SELECT * FROM langxref WHERE language = '$lang'";
$result = mysql_query($sql,$con) or die("<tree><root id='0'></root></tree>");
if(mysql_num_rows($result)==0){
// language root
$sql = "INSERT INTO node VALUES('',0,'root_$lang',0,0)";
$result = mysql_query($sql,$con) or die("<tree><root id='0'></root></tree>");
$lang_id = mysql_insert_id();
// A default node
$sql = "INSERT INTO node VALUES('',$lang_id,'$lang default node',0,0)";
$result = mysql_query($sql,$con) or die("<tree><root id='0'></root></tree>");
$default_id = mysql_insert_id();
// A default page
$sql = "INSERT INTO page VALUES($default_id,'')";
$result = mysql_query($sql,$con) or die("<tree><root id='0'></root></tree>");
$sql = "INSERT INTO langxref VALUES('',$lang_id,'$lang')";
$result = mysql_query($sql,$con) or die("<tree><root id='0'></root></tree>");
$id = $lang_id;
}else{
$id = mysql_result($result, 0, 'node_id');
}
$label = '';
$xml = "<tree>";
header("Content-Type: text/xml");
echo parseTree($id, $label, $xml, $con, false). "</tree>";
function parseTree($node, $label, $xml, $con, $child=true){
$sql = "SELECT * FROM node WHERE parent_id=$node ORDER BY node_position";
$result = mysql_query($sql,$con) or die("<tree><root id='0'></root></tree>");
$n = mysql_num_rows($result);
if($n==0){
$xml .= "<leaf label='" . htmlspecialchars( $label, ENT_QUOTES, 'UTF-8' ) . "' ref='$node' />";
}else{
// root needed for selection but not display
if($child) $xml .= "<folder label='" . htmlspecialchars( $label, ENT_QUOTES, 'UTF-8') . "' ref='$node'>";
for($f=0;$f<$n;$f++){
$nodex = mysql_result($result, $f, 'node_id');
$label = mysql_result($result, $f, 'label');
$xml = parseTree($nodex, $label, $xml, $con);
}
if($child) $xml .= "</folder>"; // not root
}
return $xml;
}
?>