<?PHP
if (!defined('IS_DEDI')) die();
include_once($cfg_dedi['dedi_path'].'inc/inc.init_external.php');
echo '<?xml version="1.0" encoding="iso-8859-1"?>';

$folders_s = str_replace(' ', '', $_GET['filefolders']);
$folderids_s = str_replace(' ', '', $_GET['filefolderids']);
$filetypes_s = str_replace(' ', '', strtolower($_GET['filefiletypes']));
$filetypeids_s = str_replace(' ', '', $_GET['filefiletypeids']);
$subfolders = (trim(strtolower($_GET['filesubfolders'])) == '1');

$folders = array_merge(
	empty($folders_s) ? array() : explode(',', $folders_s),
	empty($folderids_s) ? array() : explode(',', $folderids_s)
);

$filetypes = array_merge(
	empty($filetypes_s) ? array() : explode(',', $filetypes_s),
	empty($filetypeids_s) ? array() : explode(',', $filetypeids_s)
);
$sql = "SELECT iddirectory, parentid, name FROM ".$dedi_db['directory']." WHERE idclient=".$client." AND dirname NOT LIKE('cms/%') ORDER BY dirname";
$db -> query($sql);

$tree = array();
while($db -> next_record()) {
	if($perm->have_perm(1, 'folder', $db->f('iddirectory'))) {
		$tree[$db->f('parentid')][] = $db->f('iddirectory');
		$folder[$db->f('iddirectory')] = $db->f('name');
	};
}


if((!empty($filetypes_s) && $filetypes_s != 'true'))
$sql = "SELECT A.idupl idupl, A.filename filename, A.iddirectory iddirectory, A.titel titel FROM ".$dedi_db['upl']." AS A, ".$dedi_db['filetype']." AS B WHERE idclient=".$client." AND A.idfiletype = B.idfiletype AND B.filetype IN ('".join($filetypes, "','")."') ORDER BY filename";
else
$sql = "SELECT idupl, filename, iddirectory, titel FROM ".$dedi_db['upl']." WHERE idclient=".$client." ORDER BY filename";;

$db -> query($sql);
$files = array();
while($db -> next_record()) {
	if($perm->have_perm( 17, 'file', $db->f('idupl'), $db->f('iddirectory'))) {
		$files[$db->f('iddirectory')][] = array(
			'ID'		=> $db->f('idupl'),
			'NAME'		=> $db->f('filename'),
			'TITLE'		=> $db->f('titel')
		);
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Dateilink einfügen</title>

		<base href="<?PHP echo $cfg_dedi['dedi_html_path'] . 'external/bbcode/'; ?>" />

		<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
		<link rel="stylesheet" type="text/css" href="form.css" />

		<script type="text/javascript" src="dtree.js"></script>
		<script type="text/javascript" src="form.js"></script>
		<script type="text/javascript">var formname = '<?PHP echo $_GET['formname']; ?>';</script>
	</head>
	<body onunload="savesize('filelink');">
		<div id="dtree">
			<script type="text/javascript">
dtree = new dTree('dtree');
dtree.config.useCookies = true;
dtree.config.inOrder = true;
dtree.config.folderLinks = false;
dtree.icon.folder		= 'img/cfolder.gif';
dtree.icon.folderOpen	= 'img/ofolder.gif';
dtree.icon.node			= 'img/blank.gif';
dtree.add(0, -1, 'DeDi-Dateistruktur');
<?PHP

$html_out = '';
$iid = 0;
$ok = false;
if(count($folders) == 0) {
  if(count($tree) >= 0)
    foreach($tree[0] as $a) {
	  $html_out .= print_folder($a, 0, $folder, $tree, $files, $iid, $subfolders);
	  $ok = true;
	}
} else {
  foreach($folders as $n => $v) {
    if(in_array($n, $folder)) {
      $html_out .= print_folder($n, 0, $folder, $tree, $files, $iid, $subfolders);
      $ok = true;
    }
  }
}

if(!$ok) $html_out .= "dtree.add(1, 0, '<span class=\"greyed\">Sie haben ungenügende Rechte</span>');\n";

function print_folder($id, $ipid, &$folder, &$tree, &$files, &$iid, &$subfolders) {
  global $cfg_dedi;
  if(isset($folder[$id])) {
    $iid++;
    $html_out .= "dtree.add($iid, $ipid, '$folder[$id]');\n";
    $miid = $iid;
    if(is_array($files[$id])) {
      foreach($files[$id] as $a) {
        $iid++;
        $html_out .= "dtree.add($iid, $miid, '<span title=\\'$a[TITLE]\\'>$a[NAME]</span>', 'javascript:filelink_use($a[ID])');\n";
      }
    }
    if(is_array($tree[$id]) && $subfolders) {
      foreach($tree[$id] as $a) {
        $html_out .= print_folder($a, $miid, $folder, $tree, $files, $iid, $subfolders);
      }
    }
    return $html_out;
  }
}

echo $html_out;
?>
document.write(dtree);
			</script>
		</div>
<?PHP if($_GET['notext'] == '1')
	echo "\t\t" . '<div id="dtree_foot"><input type="checkbox" class="fixedcheckbox" value="1" id="notext" name="notext" /><label for="notext" class="checkbox">Kein Linktext</label></div>';
?>
		</div>
	</body>
</html>