<?PHP
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
			      Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
  Last update : 2006-04-10       - escape output with htmlspecialchars
  Part of     : Update to Version 01.00.02
******************************************************************************/

/******************************************************************************
 1. Benötigte Funktionen und Klassen includieren
******************************************************************************/

include('inc/fnc.upl.php');
include('inc/class.filemanager.php');
$fm = &new filemanager();

/******************************************************************************
 2. Eventuelle Actions/ Funktionen abarbeiten
******************************************************************************/
$perm->check('area_upl');
if ($idclient != 0 && $idclient != (int) $client || !isset($idclient)) $idclient = (int) $client;

// if $action is named prepare a function call
if (!empty($action) && preg_match('/^\d/', $action) == 0) {
	eval( '$errno = upl_'.$action.'();' );

	// Event
	$errlog  = ($errno) ? 'Fehler:' . $errno: '';
	fire_event('upl'.$action, array('idupl' => $idupl, 'errlog' => $errlog));
	if (!empty($errno)) {
		switch($action) {
			case 'createdir':
				$action = 100;
				break;
			case 'editdir':
				$action = 110;
				break;
			default:
				break;
		}
	}
}


// actions:
// 1xx	directory actions to be confirmed
// 2xx	file actions to be confirmed
// named actions are confirmed to be done
//
// numbered actions
//
// 100	create directory
// 110	edit directory
// 120	delete directory
// 130	copy directory
// 140	move directory
// 150	change viewtype
// 160	reserved
// 170	reserved - download directory
// 180	scan directory
//
// 200	reserved
// 210	edit file
// 220	delete file
// 230	copy file
// 240	move file
// 250	export file
// 260	import file
// 270	download file
// 280	reserved
//
// named actions
//
// upl_createdir
// upl_editdir
// upl_deletedir
// upl_copydir
// upl_movedir
// upl_downloaddir
// upl_scandir
//
// upl_editfile
// upl_deletefile
// upl_copyfile
// upl_movefile
// upl_exportfile
// upl_importfile
// upl_downloadfile

/******************************************************************************
 3. Eventuelle Dateien zur Darstellung includieren
******************************************************************************/

include('inc/inc.header.php');

/******************************************************************************
 4. Bildschirmausgabe aufbereiten und ausgeben
******************************************************************************/

// Kopfbereich
$tmp['AREA_TITLE']    = $dedi_lang['area_upl'];
$tmp['ERR_MSG']       = ($errno) ? $dedi_lang["err_$errno"]: '';
$tmp['SUB_NAV_RIGHT'] = '';

// Tabellenformatierung
$tmp['TABLE_PADDING'] = $cellpadding;
$tmp['TABLE_SPACING'] = $cellspacing;
$tmp['TABLE_BORDER']  = $border;

// Templatedatei laden
$tpl->loadTemplatefile('filelist.tpl');

//
// headline
//
$tmp['FILENAME']    = $dedi_lang['upl_directoriesandfiles'];
$tmp['DESCRIPTION'] = $dedi_lang['upl_description'];
$tmp['ACTIONS']     = $dedi_lang['upl_action'];

//
// create idexpand from quicknav-value
//
if ($idexpandshort) {
	if ($action = 'createdir') $fm->use_cache = false;
	$idexpand = $fm->get_parent_directories($idexpandshort, $client);
	if ($action = 'createdir') $fm->use_cache = true;
}
//
// get list of expanded directories
//
if (empty($idexpand)) {
	$idopen = 0;
} else {
	$idopen = explode('_', $idexpand );
	$idopen = $idopen[0];
}
//
// get directory tree in a array
//
$fm_tree = $fm->get_directory_tree($client, $idexpand);
//
// do viewtype setting
//
if (empty($viewtype)) $viewtype = 'compact';
if (!empty($action) && $action == '150') {
	$viewtype = (!$viewtype || $viewtype == 'detail') ? 'compact': 'detail';
	$action   = 0;
}
//
// get directory list in a dropdown-list
//
$dir_list     = $fm->get_directory_dropdown($client, true);
$tmp['QNAV']  = '<form action="' . $sess->url("main.php") . '" method="post" style="display: inline;" name="qnav">';
$tmp['QNAV'] = $tmp['QNAV'] . '<input type="hidden" name="area" value="upl">';
$tmp['QNAV'] = $tmp['QNAV'] . '<input type="hidden" name="viewtype" value="' . $viewtype . '">';
$tmp['QNAV'] = $tmp['QNAV'] . '<select name="idexpandshort" style="font-size:11px;font-style:normal;vertical-align: 15%;" onChange="document.qnav.submit();">';
$tmp['QNAV'] = $tmp['QNAV'] . '<option selected value="">' . $dedi_lang['upl_showfilesindir'] . '</option>'.$dir_list.'</select></form>';

//
// set javascript texts
//
$tmp['JSTEXT'] = '<script language="Javascript">' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . 'var pp_title        = "' . $dedi_lang['upl_js_texte_pp_title']        . '";' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . 'var pp_header_bild  = "' . $dedi_lang['upl_js_texte_pp_header_bild']  . '";' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . 'var pp_header_datei = "' . $dedi_lang['upl_js_texte_pp_header_datei'] . '";' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . 'var pp_created      = "' . $dedi_lang['upl_js_texte_pp_created']      . '";' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . 'var pp_modified     = "' . $dedi_lang['upl_js_texte_pp_modified']     . '";' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . 'var pp_author       = "' . $dedi_lang['upl_js_texte_pp_author']       . '";' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . 'var pp_size         = "' . $dedi_lang['upl_js_texte_pp_size']         . '";' . "\n";
$tmp['JSTEXT'] = $tmp['JSTEXT'] . '</script>' . "\n";

//
// do layout props
//
$tpl->setVariable($tmp);
unset($tmp);

//
// show directories and files
//
$tpl->setCurrentBlock('ENTRY');
showTree($fm_tree);

if ($idopen > 0 && $perm->have_perm(1, 'folder', $idopen)) show_files($idopen);

//
// generate file upload
// check upload permission for area first
if ($perm->have_perm(25, 'area_upl') || $perm->have_perm(25, 'folder', $idopen) ) {
	$tpl->setCurrentBlock('FILEIMPORTBULK');
	$tmp['FILEIMPORTTRIPLE_ACTION']  = $sess->url("main.php?area=upl&idexpand=$idexpand&viewtype=$viewtype");
	$tmp['FILEIMPORTTRIPLE_FUNC']    = 'uploadfile';
	$tmp['FILEIMPORTTRIPLE_CLIENT']  = $client;
	$tmp['FILEIMPORTTRIPLE_BGCOLOR'] = 'content2a';
	$tmp['FILEIMPORTTRIPLE_TEXT']    = $dedi_lang['upl_upload'];
	$tmp['FILEIMPORTTRIPLE_NAME']    = 'userfile[]';
	$tmp['FILEIMPORTTRIPLE_PICT']    = 'tpl/' . $cfg_dedi['skin'] . '/img/upl_upload.gif';
	$tmp['FILEIMPORTTRIPLE_HINT']    = $dedi_lang["upl_upload"];
	$tmp['FILEIMPORTTRIPLE_TO']      = $dedi_lang['upl_newplace'];
	$dir_select = '<option value="">' . $dedi_lang['upl_selectuploaddir'] . '</option>';
	if (extension_loaded('zip') && $idopen > 0) {
		$dir_select .= '<option value="-' . $idopen . '">' . $dedi_lang['upl_bulkupload'] . '</option>';
	}
	if ($idopen > 0 && file_exists($cfg_dedi['dedi_path']."external/pear/archive/tar.php")) {
		$dir_select .= '<option value="-' . $idopen . '">' . $dedi_lang['upl_tarupload'] . '</option>';
	}
	$tmp['FILEIMPORTTRIPLE_DIRECTORY']    = str_replace('"'.$idopen.'">', '"'.$idopen.'" selected>', $dir_select.$dir_list);
 	$tmp['FILEIMPORTTRIPLE_BUTTONWIDTH']  = '15';
	$tmp['FILEIMPORTTRIPLE_BUTTONHEIGHT'] = '15';

	$tmp['FILEIMPORTSCRIPT_CALL'] = '';
	if ($action == 'uploadfile' && $iddirectory < 0 && empty($errno)) {
		$idbulkdir = abs($iddirectory);
		$errfiles = implode("|", $fm->error_files);	// list files with errors
		$link = $sess->url('main.php?area=scancontrol&action=scandir&idexpand=' . $idbulkdir . '&iddirectory=' . $idbulkdir . '&viewtype=' . $viewtype);
		$tmp['FILEIMPORTSCRIPT_CALL']  = '<script language="JavaScript1.2">';
		$tmp['FILEIMPORTSCRIPT_CALL'] = $tmp['FILEIMPORTSCRIPT_CALL'] . "	window.open('$link', 'scanwin', 'width=400,height=400,scrollbars=0');";
		$tmp['FILEIMPORTSCRIPT_CALL'] = $tmp['FILEIMPORTSCRIPT_CALL'] . '	var error_files = "' . str_replace('"', '\"', $errfiles) . '";';
		$tmp['FILEIMPORTSCRIPT_CALL'] = $tmp['FILEIMPORTSCRIPT_CALL'] . '</script>';
	}
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
	unset($tmp);
}

//
// insert directory dropdown if action is move or copy dir
//
if (!empty($action) && ($action == 140 || $action == 130)) {
	$tmp_data = $tpl->get();
	$tmp_data = preg_replace ( '/%selectiondirlist%/i', str_replace('selected', '', $dir_list), $tmp_data );
	$tpl->setTemplate( $tmp_data, false, false );
}
//
// end of filemanager display
//


//
// functions for filemanager display
//
//
// shows the directory tree
//
function showTree( $subtree, $level = -1, $expand = '' ) {
	global $fm_tree, $cfg_client, $perm, $dedi_lang, $idclient;

	if ($level == -1) {
		$treeroot = array( 0 => array( "iddirectory"=>0,
									 "dirname"=>$cfg_client['upl_path'],
									 "status"=>0,
									 "idclient"=>$idclient,
									 "parentid"=>0,
									 "name"=>$dedi_lang["upl_root_dir"],
									 "description"=>$cfg_client['upl_htmlpath'],
									 "author"=>0 ) );
		showDirectory( -1, $treeroot[0], '', $treeroot[0]['status'] );
		$level = 0;
	}
	if (empty($level)) {
		foreach($subtree as $key => $value) {
			if ($value[0]['parentid'] != 0) continue;
      if ($perm->have_perm(1, 'folder', $value[0]['iddirectory'])) {
			showDirectory( $level, $value[0], $value[0]['iddirectory'], $value[0]['status'] );
			if (is_array($value['_members_'])) {
				foreach($value['_members_'] as $membervalue) {
					showTree( $fm_tree[$membervalue], $level+1, $value[0]['iddirectory'] );
				}
			}
		}
		}
	} else {
    if ($perm->have_perm(1, 'folder', $subtree[0]['iddirectory'])) {
		showDirectory( $level, $subtree[0], $subtree[0]['iddirectory'].'_'.$expand, $subtree[0]['status'] );
		if (is_array($subtree['_members_'])) {
			foreach($subtree['_members_'] as $membervalue) {
				showTree( $fm_tree[$membervalue], $level+1, $subtree[0]['iddirectory'].'_'.$expand );
			}
		}
	}
	}
}

//
// shows the files in an open directory
//
function show_files($idopen) {
	global $tpl, $idupl, $action, $dedi_lang, $newname, $sess, $perm, $filelevel, $fileexpand, $cfg_client, $cfg_dedi, $fm, $dir_list, $client, $viewtype, $deb;

	$is_detailview = ($viewtype == 'detail');	// unterscheidung für die anzeige
	$details_okay  = false;						// no files to display
	$indent        = ($filelevel+1)*21+12;

	// get file list and set display template
	$db       = $fm->get_files_in_directory($idopen, $client);
	$tpl_name = ($is_detailview) ? 'DETAILFM1': 'DETAIL';
	$tpl->setCurrentBlock($tpl_name);

	while($db->next_record()) {
		// found files to display
		$details_okay = true;
		// create basis link and get file id
		$idfile = $db->f('idupl');
	    // prüfe ob Datei gezeigt werden darf ...
		if ($perm->have_perm(17, 'file', $idfile, $idopen)) {
		$link = 'main.php?area=upl&action=%s&idexpand=' . $fileexpand . '&iddirectory=' . $idopen . '&idupl=' . $idfile . '&viewtype=' . $viewtype;
		// get thumbnail size
		$thumbsize = $fm->get_thumbnail_size($db);
		// get file information
		$file_info = get_dateinfo( array($db->f('created'), $db->f('lastmodified'), $db->f('vorname'), $db->f('nachname')) );

		// create view of file // Datei in Originalgröße anzeigen
		$popup_link      = '<a class="action" href="javascript:void(0)" onMouseOut="nd();" ';
		$popup_link     .= $is_detailview ? ' title="' . $dedi_lang['upl_openfileinnewwindow'] . '" ': '';
		$popup_link     .= 'onClick="new_imagepopup(\'' . $cfg_client['upl_htmlpath'] . $db->f('dirname') . $db->f('filename');
		$popup_link     .= "','','" . $dedi_lang['upl_popupclose'] . "','','" . $db->f('pictwidth') . "','" . $db->f('pictheight') . "','true');";
		$str_titel       = $db->f('titel')       ? htmlspecialchars($db->f('titel'))      : '';
		$str_description = $db->f('description') ? htmlspecialchars($db->f('description')): '';
		$file_size_info  = ( $db->f('filesize') > 1024) ? sprintf( "%01.2f", $db->f('filesize')/1024) . '&nbsp;kByte': $db->f('filesize') . '&nbsp;Byte';
		$file_size_info .= $tmp['DETAILFM1_FILESIZE'] . '&nbsp;-&nbsp;' . $db->f('pictwidth') . 'x' . $db->f("pictheight") . 'px';
		if ($is_detailview) {
			$tmp['DETAILFM1BLOCKEDIT']    = 'fileblock';
			$tmp['DETAILFM1_NAMESTART']	  = $popup_link . " window.event.cancelBubble = true;\">";
			$tmp['DETAILFM1_NAMEEND']     = "</a>";
			$tmp['DETAILFM1_NAME']    	  = $db->f('filename')."<br><br>";
			$tmp['DETAILFM1_PICTSRC']     = $thumbsize['thumbnail'];
			$tmp['DETAILFM1_PICTWIDTH']   = intval($thumbsize['width']);
			$tmp['DETAILFM1_PICTHEIGHT']  = intval($thumbsize['height']);
			$tmp['DETAILFM1_FILESIZE']    = $file_size_info . '<br>' . implode( "/", $file_info );
			$tmp['DETAILFM1_DESCRIPTION'] = '<strong>' . $dedi_lang['upl_titel']       . ':</strong><br>' . $str_titel . '<br>';
			$tmp['DETAILFM1_DESCRIPTION'] = $tmp['DETAILFM1_DESCRIPTION']  . '<strong>' . $dedi_lang['upl_description'] . ':</strong><br>' . $str_description;
			// create new function array
			$fm_func = array();
		} else {
			$tmp['DETAIL_NAME']        = $popup_link . '" onMouseOver="previewPict2(\'' . $thumbsize['thumbnail'] . "', '" . intval($thumbsize['width']);
			$tmp['DETAIL_NAME']        = $tmp['DETAIL_NAME'] . "', '" . intval($thumbsize['height']) . "', '" . $file_info['created'] . "', '" . $file_info['changed'];
			$tmp['DETAIL_NAME']        = $tmp['DETAIL_NAME'] . "', '" . $file_info['author'] . "', true, '" . $db->f('filename') . "', '" . $file_size_info . "');\" >" . $db->f('filename') . "</a>";
			$tmp['DETAIL_BGCOLOR']     = '#DBE3EF';
			$tmp['DETAIL_BGCOLOROVER'] = '#C7D5EB';
			$tmp['DETAIL_STYLE']       = ' style="padding-left: '.$indent.'px;"';
			$tmp['DETAIL_DESCRIPTION'] = ($str_titel ? '<strong>' . $str_titel . '</strong><br>': '') . $str_description;
			// buttons and actions
			$tmp['DETAIL_DOWNLOAD']    = make_image_link2 ( '#', 'space.gif', '', 15, 15, '', '', '', 'action', '', '', '', 'action' );
			$tmp['DETAIL_EDIT']        = make_image_link2 ( '#', 'space.gif', '', 18, 14, '', '', '', 'action', '', '', '', 'action' );
			$tmp['DETAIL_DELBUT']      = make_image_link2 ( '#', 'space.gif', '', 13, 13, '', '', '', 'action', '', '', '', 'action' );
			$tmp['DETAIL_DUPLICATE']   = make_image_link2 ( '#', 'space.gif', '', 18, 14, '', '', '', 'action', '', '', '', 'action' );
			$tmp['DETAIL_MOVE']        = make_image_link2 ( '#', 'space.gif', '', 16, 16, '', '', '', 'action', '', '', '', 'action' );
		}
		// buttons and actions
		// check permission for file download
  		if ($perm->have_perm(24, 'file', $idfile, $idopen)) {
			$str_link = sprintf($link, 'downloadfile');
			if ($is_detailview) $fm_func["DOWNLOAD"]    = "[ '" . $dedi_lang['upl_download'] . "', '" . $str_link . "' ]";
			else                $tmp['DETAIL_DOWNLOAD'] = make_image_link2 ( $str_link, 'but_download.gif', $dedi_lang['upl_downloadfile'], 15, 15, '', '', '', 'action' );
		}
		// check permission for file modifications
  		if ($perm->have_perm(19, 'file', $idfile, $idopen)) {
			$str_link = sprintf($link, '210');
			if ($is_detailview) $fm_func["EDIT"]    = "[ '" . $dedi_lang['upl_edit'] . "' , '" . $str_link . "#fileworkplace'   ]";
			else                $tmp['DETAIL_EDIT'] = make_image_link2 ( $str_link, 'but_edit.gif', $dedi_lang['upl_editfile'], 18, 14, '', '', '', 'action', 'fileworkplace', '', '', 'action' );
			$str_link = sprintf($link, '230');
			if ($is_detailview) $fm_func["DUPLICATE"]    = "[ '" . $dedi_lang['upl_copy'] . "'   , '" . $str_link . "#fileworkplace'   ]";
			else                $tmp['DETAIL_DUPLICATE'] = make_image_link2 ( $str_link, 'but_duplicate.gif', $dedi_lang['upl_copyfiletodir'], 18, 14, '', '', '', 'action', 'fileworkplace', '', '', 'action' );
			$str_link = sprintf($link, '240');
			if ($is_detailview) $fm_func["MOVE"]    = "[ '" . $dedi_lang['upl_move'] . "', '" . $str_link . "#fileworkplace'   ]";
			else                $tmp['DETAIL_MOVE'] = make_image_link2 ( $str_link, 'but_moveup.gif', $dedi_lang['upl_movefiletodir'], 16, 16, '', '', '', 'action', 'fileworkplace', '', '', 'action' );
		}
		// check permission for file delete
  		if ($perm->have_perm(21, 'file', $idfile, $idopen)) {
			$str_link = sprintf($link, '220');
			if ($is_detailview) $fm_func["DELBUT"]    = "[ '" . $dedi_lang['upl_del'] . "'    , '" . $str_link . "#fileworkplace' ]";
			else                $tmp['DETAIL_DELBUT'] = make_image_link2 ( $str_link, 'but_delete.gif', $dedi_lang["upl_deletefile"], 13, 13, '', '', '', 'action', 'fileworkplace', '', '', 'action' );
		}
		if ($is_detailview) {
			$tmp['DETAILFM1_AKTIONEN'] = '<img src="tpl/' . $cfg_dedi['skin'] . '/img/but_edit.gif" onMouseOver="showmenu(event, [\'' . $dedi_lang['upl_editfile'] . "', [ " . implode(", ", $fm_func) . ' ] ])" onMouseOut="delayhidemenu();" border="0" align="left">';
		}
		// handle called actions
		if ($idupl == $db->f('idupl')) {
			switch ($action) {
				// edit file
				case 210:
					$actionname     = 'editfile';
					$linkokay       = 'javascript:document.fmedit.submit()';
					$str_filename   = $dedi_lang['upl_copyfilename'] . ':<br><input type="text" name="newfilename" value="' . htmlspecialchars(($newfilename)?$newfilename:$db->f('filename')) . '" size="30" maxlength="255">';
					$str_titel      = $dedi_lang['upl_titel'] . ':<br><input type="text" name="newtitle" value="' . htmlspecialchars(($newtitle)?$newtitle:$db->f('titel')) . '" size="30" maxlength="200">';
					$str_descrption = $dedi_lang['upl_description'] . ':<br><textarea name="newdescription" rows="3" cols="50">' . htmlspecialchars(($newdescription)?$newdescription:$db->f('description')) . '</textarea>';

					$tmp[$tpl_name.'_NAME']        = $str_filename;
					$tmp[$tpl_name.'_DESCRIPTION'] = $str_titel . '<br>' . $str_descrption . '<br>';
					if ($is_detailview) {
						$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . $str_filename . '<br><br>';
					}
					// check for permission to change user rights
  					if ($perm->have_perm(22, 'file', $idfile, $idopen)) {
						$panel = $perm->get_right_panel('file', $idfile, array( 'formname'=>'fmedit' ), 'img', false, false, $idopen);
						if (!empty($panel)) {
							$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . $panel['rights'];
							$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . $panel['call'];
							$tmp[$tpl_name.'_EDITSCRIPT']  = $panel['scripts'];
						}
					}
					// add buttons
					$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . make_image_link4 ('but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang["upl_editfile"], $dedi_lang['upl_cancel'], 'action1', 'action1', 'main.php?area=upl&viewtype=' . $viewtype . '&idexpand=' .$fileexpand );
					break;

				// delete file
				case 220:
					$actionname  = 'deletefile';
					$linkokay    = $link;
					$but_confirm = make_image_link3 ('main.php?area=upl&action=deletefile&viewtype=' . $viewtype . '&idupl=' . $idupl . '&idexpand=' . $fileexpand, 'but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang["upl_deletefile"], $dedi_lang['upl_cancel'], 'action1', 'fileworkplace' );
					$tmp[$tpl_name.'_DESCRIPTION']  = $dedi_lang['upl_confirm_delete'] . '<br><br>' . $but_confirm;
					break;
				// duplicate file
				case 230:
					$actionname = 'copyfile';
					$linkokay   = 'javascript:document.fmedit.submit()';
					$tmp[$tpl_name.'_DESCRIPTION'] = $dedi_lang["upl_copyfilename"] . '<br><input type="text" name="newfilename" value="' . htmlspecialchars(($newfilename)?$newfilename:$db->f('filename')) . '" size="30" maxlength="255"><br>';
					$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . $dedi_lang['upl_newplace'] . ':<br><select name="movetargetid"><option value="">' . $dedi_lang["upl_copyfiletodir"] . ' ...</option>' . str_replace('selected', '', $dir_list) . '</select><br><br>';
					$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . make_image_link4 ('but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang["upl_copyfiletodir"], $dedi_lang['upl_cancel'], 'action1', 'action1', 'main.php?area=upl&viewtype=' . $viewtype . '&idexpand=' . $fileexpand);
					break;
				// move file
				case 240:
					$actionname = 'movefile';
					$linkokay   = 'javascript:document.fmedit.submit()';
					$tmp[$tpl_name.'_DESCRIPTION'] = $dedi_lang['upl_copyfilename'] . '<br><input type="text" name="newfilename" value="' . htmlspecialchars(($newfilename)?$newfilename:$db->f('filename')) . '" size="30" maxlength="255"><br>';
					$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . $dedi_lang['upl_newplace'] . ':<br><select name="movetargetid"><option value="">' . $dedi_lang["upl_movefiletodir"] . ' ...</option>';
					$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . str_replace('selected', '', $dir_list) . '</select><br><br>';
					$tmp[$tpl_name.'_DESCRIPTION'] = $tmp[$tpl_name.'_DESCRIPTION'] . make_image_link4 ('but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang["upl_movefiletodir"], $dedi_lang['upl_cancel'], 'action1', 'action1', 'main.php?area=upl&viewtype='.$viewtype.'&idexpand='.$fileexpand);
					// $tmp['DETAIL_DESCRIPTION']     = $tmp['DETAILFM1_DESCRIPTION'];
					break;
				default:
					break;
			}
			if ($actionname) {
				if ($is_detailview) {
					$tmp['DETAILFM1_NAME']		= '';
					$tmp['DETAILFM1_FILESIZE']	= '';
					$tmp['DETAILFM1_AKTIONEN']	= '';
					$tmp['DETAILFM1ACTIVE']		= '<a name="#fileworkplace"></a>';
					$tmp['DETAILFM1BLOCKEDIT']	= 'fileblockactive';
				}
				makeRowEdit($tmp, array('url'     =>'main.php',
										'enctype' =>'text/plain',
										'block'   =>$tpl_name.'ROWEDIT',
										'hidden'  => array( 'area'		 => 'upl',
															'idexpand'	 => $fileexpand,
															'idupl'		 => $db->f('idupl'),
															'action'	 => $actionname,
															'filename'	 => $db->f('filename'),
															'description'=> $db->f('description'),
															'iddirectory'=> $db->f('iddirectory'),
															'idfiletype' => $db->f('idfiletype'),
															'viewtype'	 => $viewtype
															)
										)
							);
			}
		}
		$tpl->setVariable($tmp);
		$tpl->parseCurrentBlock();
		unset($tmp);
	}
	}

	// create detail box if in detail view and files where found
	if ($details_okay && $is_detailview) {
		// open detail block
		$tpl->setCurrentBlock('DETAILFM');
		$tmp = array();
		$tmp['DETAILFM_BGCOLOR']		= '#A8BADE';
		$tmp['DETAILFM_BGCOLOROVER']	= '#C7D5EB';
		// $tmp['DETAILFM_STYLE']		= ' style="padding:5px 0px 5px 0px;"';
		$tpl->setVariable($tmp);
		$tpl->parseCurrentBlock();
		unset($tmp);
		// close detail block
		$tpl->setCurrentBlock('DETAILFM2');
		$tmp['DETAILFM2_FAKE'] = ' ';
		$tpl->setVariable($tmp);
		$tpl->parseCurrentBlock();
		unset($tmp);
	}
}

function showDirectory( $level, $line_array, $prefixexpand, $status ) {
	global $tpl, $idopen, $client, $iddirectory, $action, $dedi_lang, $newname, $sess, $perm, $filelevel, $fileexpand;
	global $newdirname, $newdirdescription, $idexpand, $cfg_client, $cfg_dedi, $viewtype;

	$named_place	= '<a name="dirworkplace"></a>';
	$iddir			= $line_array['iddirectory'];
	$selected		= '';
	$class			= '#C7D5EB';
	$class2			= '#A8BADE';
	$hint			= $dedi_lang['upl_opendir'];
	$pict			= 'but_folder_off2.gif';
	$expand			= $iddir . '_' . ($level) ? $prefixexpand :'';
	$actionexpand	= $idexpand;
	$indent			= ($level == -1) ? 4: ($level*21+4);

	if ($idopen == $iddir) {
		$class		= '#A8BADE';
		$class2		= '#A8BADE';
		$hint		= $dedi_lang['upl_closedir'];
		$pict		= 'but_folder.gif';
		$selected	= ' selected';
		$expand		= ($level) ? '-'.$expand: '';
	}

	// directory informations
	$link						= 'main.php?area=upl&action=%s&idexpand=' . $actionexpand . '&iddirectory=' . $iddir . '&viewtype=' . $viewtype;

	$tmp['ENTRY_SHOWDETAIL']	= ($level < 0) ?  $line_array['name']: make_image_link2 ('main.php?area=upl&viewtype=' . $viewtype . '&idexpand=' .$expand, $pict, $hint, '', '', '', '', '', 'action', '', '', $line_array['name'], 'action' );

	$tmp['ENTRY_BGCOLOR']		= $class;
	$tmp['ENTRY_BGCOLOROVER']	= $class2;
	$tmp['ENTRY_STYLE']			= ' style="padding-left: ' . $indent . 'px;" ';
	$tmp['ENTRY_DESCRIPTION']	= htmlspecialchars($line_array['description']);
	// buttons and actions
	$tmp['ENTRY_SCAN']			= make_image_link2 ( '#', 'space.gif', '', 20, 17, '', '', '', 'action', '', '', '', 'action' );
	$tmp['ENTRY_NEW']			= make_image_link2 ( '#', 'space.gif', '', 14, 14, '', '', '', 'action', '', '', '', 'action' );
	$tmp['ENTRY_EDIT']			= make_image_link2 ( '#', 'space.gif', '', 18, 14, '', '', '', 'action', '', '', '', 'action' );
	$tmp['ENTRY_DELBUT']		= make_image_link2 ( '#', 'space.gif', '', 13, 13, '', '', '', 'action', '', '', '', 'action' );

	// check if root directory
	if ($level < 0) {
		// button for changing the view
    // Auswahl für Detail- oder Compact-Modus
		if ($viewtype == 'compact')
			$tmp['ENTRY_SPECIAL'] = make_image_link2 ( sprintf($link, 150), 'but_viewdetail.gif', $dedi_lang['upl_changeviewdetail'], 18, 18, '', '', '', 'action1', '', '', '', 'action' );
		else
			$tmp['ENTRY_SPECIAL'] = make_image_link2 ( sprintf($link, 150), 'but_viewcompact.gif', $dedi_lang['upl_changeviewcompact'], 18, 18, '', '', '', 'action1', '', '', '', 'action' );
		// clear dir name
		$line_array['dirname'] = '';
	} else {

	}
	// buttons for functions
	// check permission for create directory
	if ($perm->have_perm(2, 'folder', $iddir)) {
		$tmp['ENTRY_NEW'] = make_image_link2 ( sprintf($link, 100), 'but_newcat.gif', $dedi_lang['upl_createdir'], 14, 14, '', '', '', 'action1', 'dirworkplace', '', '', 'action' );
	}
	// check permission for edit directory
	if ($perm->have_perm(3, 'folder', $iddir)) {
		if ($level >= 0) {
			$tmp['ENTRY_EDIT'] = make_image_link2 ( sprintf($link, 110), 'but_edit.gif', $dedi_lang['upl_editdir'], 18, 14, '', '', '', 'action1', 'dirworkplace', '', '', 'action' );
		}
	}
	// check permission for delete directory
	if ($perm->have_perm(5, 'folder', $iddir)) {
		if ($level >= 0) {
			$tmp['ENTRY_DELBUT'] = make_image_link2 ( sprintf($link, 120), 'but_delete.gif', $dedi_lang['upl_deletedir'], 13, 13, '', '', '', 'action1', 'dirworkplace', '', '', 'action' );
		}
	}
	// check permission for directory scan
	if (($perm->have_perm(11, 'area_upl') && $level == -1) || $perm->have_perm(11, 'folder', $iddir)) {
		$link = 'main.php?area=scancontrol&action=%s&idexpand=' . $actionexpand . '&iddirectory=' . $iddir . '&viewtype=' . $viewtype;
		$tmp['ENTRY_SCAN'] = make_image_link5 ( sprintf($link, 'scandir'), 'but_folder_scan.gif', (($level == -1) ? $dedi_lang['upl_scandir_root']: $dedi_lang['upl_scandir']), 20, 17, 'scanwin', 'width=400,height=400,scrollbars=0', '', '', 'action', '', '', '', 'action' );
	}

	// handle called actions
	// functions will override the description or add something to the name
	if ($iddirectory == $line_array['iddirectory']) {
		switch ($action) {
			// create new directory
			case 100:
				$actionname = 'createdir';
				// show directory that will contain the new directory
				$tmp['ENTRY_SHOWDETAIL'] = $tmp['ENTRY_SHOWDETAIL'] . $named_place;
				$tpl->setVariable($tmp);
				$tpl->parseCurrentBlock();
				unset($tmp);
				// create line for new directory
				quoteSaveText($newdirname);
				quoteSaveText($newdirdescription);
				$tmp['ENTRY_SHOWDETAIL']  = '<input type="text" name="newdirname" value="' . $newdirname . '" size="30" maxlength="255">';
				$tmp['ENTRY_BGCOLOR']	  = $class;
				$tmp['ENTRY_BGCOLOROVER'] = $class2;
				$tmp['ENTRY_STYLE']		  = ' style="padding-left: '.($indent+3).'px;" ';
				$tmp['ENTRY_DESCRIPTION'] = '<input type="text" name="newdirdescription" value="' . $newdirdescription . '" size="30" maxlength="255"> ';
				$tmp['ENTRY_DESCRIPTION'] = $tmp['ENTRY_DESCRIPTION'] . make_image_link4 ('but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang['upl_createdir'], $dedi_lang['upl_cancel'], 'action', 'action', 'main.php?area=upl&viewtype=' . $viewtype . '&idexpand=' . $actionexpand . '&iddirectory=' . $iddir, '14', '14');
				break;
			// edit existing directory
			case 110:
				$actionname = 'editdir';
				quoteSaveText($newdirname);
				quoteSaveText($newdirdescription);
				$tmp['ENTRY_SHOWDETAIL']	 = $named_place . make_image_link2 ('main.php?area=upl&idexpand=' . $expand, $pict, $hint, 15, 15, '', '', '', 'action', '', '', '', 'action' );
				$tmp['ENTRY_NAME']			 = '<input type="text" name="newdirname" value="' . (($newdirname)?$newdirname:htmlspecialchars($line_array['name'])) . '" size="30" maxlength="255">';
				$tmp['ENTRY_DESCRIPTION']	 = '<input type="text" name="newdirdescription" value="' . (($newdirdescription)?$newdirdescription:htmlspecialchars($line_array['description'])) . '" size="30" maxlength="255"> ';
				if ($perm->have_perm(6, 'folder', $iddirectory)) {
					$panel = $perm->get_right_panel('folder', $iddirectory, array( 'formname'=>'fmedit' ), 'img' );
					if (!empty($panel)) {
						$tmp['ENTRY_DESCRIPTION'] = $tmp['ENTRY_DESCRIPTION'] . implode("", $panel);
					}
				}
				$tmp['ENTRY_DESCRIPTION'] = $tmp['ENTRY_DESCRIPTION'] . make_image_link4 ('but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang['upl_editdir'], $dedi_lang['upl_cancel'], 'action', 'action', 'main.php?area=upl&viewtype=' . $viewtype . '&idexpand=' . $actionexpand . '&iddirectory=' . $iddir, '14', '14');
				break;
			// delete directory
			case 120:
				$tmp['ENTRY_DELETE'] = make_image_link3 ('main.php?area=upl&action=deletedir&viewtype=' . $viewtype . '&iddirectory=' . $line_array['iddirectory'] . '&idclient=' . $client . '&idexpand=' . $expand, 'but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang['upl_deletedir'], $dedi_lang['upl_cancel'], 'action1', 'dirworkplace' );
				break;
			// to do for version 1.x
			// 130: copy directory
			// 140: move directory
			// 170: download directory
			default:
				break;
		}
		if ($actionname) {
			makeRowEdit($tmp, array('url'		=>'main.php?action=' . $actionname,
									'enctype'	=>'text/plain',
									'block'		=>'ROWEDIT',
									'hidden'	=>array('area'				=>'upl',
														'idexpand'			=>$actionexpand,
														'iddirectory'		=>$line_array['iddirectory'],
														'parentid'			=>$line_array['iddirectory'],
														'parentdirname'		=>$line_array['dirname'],
														'dirdescription'	=>$line_array['iddirectory'],
														'dirname'			=>$line_array['dirname'],
														'viewtype'			=>$viewtype
														)
									));
		}
	}
	$tpl->setVariable($tmp);
	$tpl->parseCurrentBlock();
	unset($tmp);

	if ($idopen == $iddir) {
		$tpl->setCurrentBlock('PASTENTRY');
		$filelevel = $level;
		$fileexpand = str_replace('-', '', $actionexpand);
	}
}

function get_dateinfo( $infos ) {
	global $cfg_dedi;
	$date_format = $cfg_dedi['FormatDate'] . ' ' . $cfg_dedi['FormatTime'];
	$file_info   = array( 'created'=>date($date_format, $infos[0]),
						  'changed'=>date($date_format, $infos[1]),
						  'author' =>$infos[2] . " " . $infos[3] );
	return $file_info;
}

function makeRowEdit( &$tmp, $in ) {
	global $sess;
	$tmp[$in['block']]       = '<form name="fmedit" class="filemanageredit" action="' . $sess->url($in['url']) . '" method="post">';
	foreach($in['hidden'] as $key => $value) {
		$tmp[$in['block']] = $tmp[$in['block']] . '<input type="hidden" name="' . htmlspecialchars($key) . '" value="' . htmlspecialchars($value) . '">';
	}
	$tmp[$in['block'].'END'] = '</form>';
}

function makeStatusEdit( &$tmp, $in ) {
	global $dedi_lang;
	$tmp[$in['block']] = $tmp[$in['block']] . make_image_link4 ('but_confirm.gif', 'but_cancel_delete.gif', $dedi_lang[$in['func']], $dedi_lang['upl_cancel'], 'action', 'action', $in['back'], '14', '14');
}

function quoteSaveText(&$code) {
	remove_magic_quotes_gpc($code);
	$code = htmlspecialchars($code);
}
?>