<?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['imgfolders']);
$folderids_s = str_replace(' ', '', $_GET['imgfolderids']);
$filetypes_s = str_replace(' ', '', strtolower($_GET['imgfiletypes']));
$filetypes_s = ( empty($filetypes_s) || $filetypes_s == 'true' ) ? 'jpg,jpeg,gif,png': $filetypes_s;
$filetypeids_s = str_replace(' ', '', $_GET['imgfiletypeids']);
$subfolders = (trim(strtolower($_GET['imgsubfolders'])) == '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');
	};
}

$sql = "SELECT A.idupl idupl, A.filename filename, A.iddirectory iddirectory, A.titel titel, A.pictwidth width, A.pictheight height, B.filetype as filetype, C.dirname as directory FROM ".$dedi_db['upl']." AS A, ".$dedi_db['filetype']." AS B, ".$dedi_db['directory']." AS C WHERE A.idclient=".$client." AND C.idclient=".$client." AND A.idfiletype = B.idfiletype AND B.filetype IN ('".join($filetypes, "','")."') AND C.iddirectory = A.iddirectory ORDER BY filename";

$db -> query($sql);
$files = array();
while($db -> next_record()) {
	if($perm->have_perm( 17, 'file', $db->f('idupl'), $db->f('iddirectory'))) {
		$thumb = preg_replace("/\.".$db->f('filetype')."$/i", $cfg_client['thumbext'].'.'.$db->f('filetype'), $db->f('directory').$db->f('filename'));
		if(!file_exists($cfg_client['upl_path'].$thumb)) $thumb = $db->f('directory').$db->f('filename');
		$files[$db->f('iddirectory')][] = array(
			'ID'		=> $db->f('idupl'),
			'NAME'		=> $db->f('filename'),
			'TITLE'		=> $db->f('titel'),
			'TYPE'		=> $db->f('filetype'),
			'DIR'		=> $db->f('directory'),
			'WIDTH'		=> $db->f('width'),
			'HEIGHT'	=> $db->f('height'),
			'THUMB'		=> $thumb
		);
	}
}

?>
<!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>Bild 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']; ?>'; var mediapath = '<?PHP echo $cfg_client['upl_htmlpath']; ?>';</script>
	</head>
	<body onload="setfocus('src');" onunload="savesize('img');">
		<form action="#" onsubmit="return img_build();">
			<div id="left">
				<label for="src">Quelle:</label>
				<input type="text" class="text" id="src" name="src" size="30" />
				<label for="title">Titel:</label>
				<input type="text" class="text" id="title" name="title" size="30" />
				<label for="width">Breite (Pixel):</label>
				<input type="text" class="text" id="width" name="width" size="10" />
				<label for="height">Höhe (Pixel):</label>
				<input type="text" class="text" id="height" name="height" size="10" />
				<input type="submit" class="submit" value="OK" />
			</div>
			<a id="preview_link" title=" " href=" " onclick="return open_pic_popup(this.href, this.title);"><img id="preview" src=" " style="width: <?PHP echo $cfg_client['thumb_size']; ?>px;" title=" " alt=" "/></a>
		</form>
		<div id="right_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";

echo $html_out;

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:img_use($a[ID], \\'$a[TITLE]\\', \\'$a[THUMB]\\', \\'$a[WIDTH]\\', \\'$a[HEIGHT]\\', \\'$a[DIR]$a[NAME]\\', \\'".(empty($a['TITLE']) ? $a['NAME'] : $a['TITLE'])."\\')');\n";
        //$html_out .= "dtree.add($iid, $miid, '<span title=\\'$a[TITLE]\\'>$a[NAME]</span>', 'javascript:img_set($a[ID], \\'$a[THUMB]\\', \\'$a[WIDTH]\\', \\'$a[HEIGHT]\\', \\'$a[DIR]$a[NAME]\\', \\'".(empty($a['TITLE']) ? $a['NAME'] : $a['TITLE'])."\\')');\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;
  }
}

?>
document.write(dtree);
			</script>
		</div>
	</body>
</html>