<?PHP
/******************************************************************************
 @Author:     Björn Brockmann, Paul Eppner (paul@twomoons.de)
			  Sven Bräutigam, Jürgen Brändle
 @Homepage    http://www.der-dirigent.de
******************************************************************************/

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

include('inc/fnc.scan.php');
include('inc/class.filemanager.php');
$fm = &new filemanager();
$scanned_dirs_complete   = false;
$scanned_files_complete  = false;
$scanned_thumbs_complete = false;
$scan_active             = 0;
$scanned_completed       = 0;
$found_dirs              = 0;
$scanned_dirs            = 0;
$found_files             = 0;
$scanned_files           = 0;
$found_thumbs            = 0;
$scanned_thumbs          = 0;
$max_dirs                = $cfg_client['max_count_scandir'];
$max_files               = $cfg_client['max_count_scanfile'];
$max_thumbs              = $cfg_client['max_count_scanthumb'];
$extend_time             = $cfg_client['extend_time_scandir'];

/******************************************************************************
 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));
}

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

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

// Kopfbereich
$title  = $dedi_lang['area_upl'];
$fehler = ($fm->errno) ? $dedi_lang["err_$fm->errno"]: '';

if ($action == "10") {
	// check if directory exists ... avoid config errors in dedi_values
	$fehler = '';
	if ($iddirectory > 0) {
		$dir_data = $fm->get_directory( (int) $iddirectory, $client);
	} else {
		$dir_data['dirname'] = '';
		$dir_data['iddirectory'] = '0';
	}

	if (empty($dir_data)) {
		// Fehler Verzeichnis nicht in der Datenbank gefunden
		$fehler = $dedi_lang["err_1425"];
	} else {
		// Prüfe ob directory auf Festplatte existiert
		if (!is_dir($cfg_client['upl_path'] . $dir_data['dirname'])) {
			$fehler = $dedi_lang["err_1426"];
		}
	}
	
	// new scan - show property window
	$url = $sess->url('main.php?area=scan&action=scandir&iddirectory=' . $iddirectory . '&viewtype=' . $viewtype);
?>
	<html>
		<head>
			<link rel="stylesheet" type="text/css" href="tpl/<?PHP echo $cfg_dedi['skin'] ?>/css/styles.css">
			<meta http-equiv="content-type" content="text/html; charset=<?PHP echo $lang_charset ?>">
		</head>
		<body>
<?PHP
	if (empty($fehler))	{
?>
			<form action="<?PHP echo $url ?>" method="post" target="progresscontrol">
				<table width="100%" cellspacing="5" cellpadding="2" border="0">
					<caption><?PHP echo $dedi_lang['scan_options']; ?></caption>
					<tr>
						<td><?PHP echo $dedi_lang['scan_thumbs_checkbox']; ?></td>	
						<td><input type="checkbox" name="updatethumbs" value="1"></td>
					</tr>
					<tr>
						<td><?PHP echo $dedi_lang['scan_nosubdir_checkbox']; ?></td>	
						<td><input type="checkbox" name="nosubdirscan" value="1"></td>
					</tr>
					<tr>
						<td><input type="submit" name="submit" value="<?PHP echo $dedi_lang['scan_start_dirscan']; ?>"></td>	
						<td><input type="button" onClick="top.close()" name="" value="<?PHP echo $dedi_lang['upl_cancel']; ?>"></td>
					</tr>
				</table>
			</form>
<?PHP
	} else {
?>
			<table width="100%" cellspacing="5" cellpadding="2" border="0">
				<caption><?PHP echo $fehler; ?></caption>
				<tr>
					<td>&nbsp;</td>	
					<td>&nbsp;</td>
				</tr>
				<tr>
					<td>&nbsp;</td>	
					<td align="center"><a href="javascript:top.close()"><?PHP echo $dedi_lang['scan_close_now']; ?></a></td>
					<td>&nbsp;</td>
				</tr>
			</table>
<?PHP 
	}
?>
		</body>
	</html>
<?PHP
} else {
	// running scan
	$closescript = '';
	$urladdon    = (!empty($updatethumbs)) ? "&updatethumbs=1": "";
	$urladdon   .= (!empty($nosubdirscan)) ? "&nosubdirscan=1": "";
	$urladdon   .= '&viewtype=' . $viewtype;
	
	if (!$scanned_dirs_complete) {
		$url = $sess->url('main.php?area=scan&action=scandir&iddirectory=-1' . $urladdon);
		$arrFileErrors = (count($fm->error_dirs) > 0) ? '"'.implode( '<strong> (V)</strong>","', $fm->error_dirs ).'<strong> (V)</strong>"':'';
		$scan_active = 0;
	} elseif (!$scanned_files_complete) {
		if ($action == 'scandir') {
			$arrFileErrors = (count($fm->error_dirs) > 0) ? '"'.implode( '<strong> (V)</strong>","', $fm->error_dirs ).'<strong> (V)</strong>"':'';
		} else {
			$arrFileErrors = (count($fm->error_files) > 0) ? '"'.implode( '<strong> (D)</strong>","', $fm->error_files ).'<strong> (D)</strong>"':'';
		}
		$url = $sess->url('main.php?area=scan&action=scanfiles' . $urladdon);
		$scan_active = 1;
	} elseif (!$scanned_thumbs_complete) {
		$arrFileErrors = (count($fm->error_files) > 0) ? '"'.implode( '<strong> (D)</strong>","', $fm->error_files ).'<strong> (D)</strong>"':'';
		$url = $sess->url('main.php?area=scan&action=scanthumbs' . $urladdon);
		$scan_active = 2;
	} else {
		$arrFileErrors = '';
		$scan_active = 3;
		$closescript = "window.setTimeout('top.close()', 10000);";
	}
	if (empty($closescript)) $closescript = "window.document.location.href = '" . $url . "';";
?>
	<html>
		<head>
			<meta http-equiv="content-type" content="text/html; charset=<?PHP echo $lang_charset ?>">
		</head>
		<script language="JavaScript1.2">
			function update () {
				<?PHP
					echo "window.parent.setDirValues($found_dirs, $scanned_dirs);\n";
					echo "window.parent.setFileValues($found_files, $scanned_files );\n";
					echo "window.parent.setThumbValues($found_thumbs, $scanned_thumbs);\n";
					echo "window.parent.setMiscValues([ $arrFileErrors ], '$fehler', $scan_active );\n";
					echo "window.setTimeout('recall()', 250);";
				?>
				return true;
			}
			function recall() {
				<?PHP echo $closescript; ?>
			}
		</script>
		<body onload="update();">
		</body>
	</html>
<?PHP
}
?>
