<?

if ($sys['path'] == "") {
	$sys['path'] = "/";
}

if (!isCleanPath($sys['path'])) {
echo "Ilegal path";
exit();
}

$tmp['path'] = gluePath($permissions['path'],$sys['path']);
if (is_dir($tmp['path'])) {
	if (is_readable($tmp['path'])) {
		if (!is_writeable($tmp['path'])) {
			$tmp['cancelAction'] = true;
			$sys['path'] = "";
		}
	} else {
		$tmp['cancelAction'] = true;
		$sys['path'] = "";
	}
} else {
	$tmp['cancelAction'] = true;
	$sys['path'] = "";
}

$sys['fullPath'] = gluePath($permissions['path'],$sys['path']);

if (!is_dir($sys['fullPath'])) {
	if (!$sys['path']) {
		echo "Account misconfigured please contact the site admin.";
		die();
	}
	header("Location: index.php");
	exit();
}



if ($_GET['act'] == "simple_upload") {
	require_once($sys['root']."/system/includes/myfiles/actions/simple_upload.inc");
}




//get dir listing
$d = @dir($sys['fullPath']);
if (!$d) {
	$sys['fullPath'] = $permissions['path'];
	$d = dir($sys['fullPath']);
	if (!$d) {
		header("Location: ?logout=1&msg=3");
		exit();
	}
	echo "Cannot read folder contents";
	exit();
}

$i=0;
while ($file = @$d->read()) {
	//scoate din lista punctele si descrierile
	if ($file != "." && $file != ".." && !in_array($file, $ignoreFiles)) {
	$isdir = is_dir($sys['fullPath']."/".$file);
$i++;
			$files['name'][$i] = $file;
			$files['lcname'][$i] = strtolower($file);
		if ($isdir) {
			$files['isdir'][$i] = "1";
			if ($settings->config['compute_folder_size']) {
				$files['size'][$i] = $fm->getDirSize($sys['fullPath']."/".$file);
			} else {
				$files['size'][$i] = "0";
			}
			$files['ext'][$i] = "";
			$files['mdate'][$i] = filemtime($sys['fullPath']."/".$file);
			$files['filetypedescription'][] = $lang->get("Folder");
			$files['icon'][$i] = "folder.gif";
			$files['thumb_img'][$i] = "";
			$files['filetype'][$i] = $lang->get("Folder");
		} else {
			$files['isdir'][$i] = "0";
			$files['size'][$i] = filesize($sys['fullPath']."/".$file);
			$files['mdate'][$i] = filemtime($sys['fullPath']."/".$file);
			$extension = $fm->getExtension($file);
			$files['ext'][$i] = $extension;
			$fileinfo = $fm->fileTypeInfo($extension);
			if ($fileinfo['description'] == "") {
				$files['filetypedescription'][$i] = strtoupper($extension). " " . ucfirst("File", 2, "");
			} else {
				$files['filetypedescription'][$i] = $fileinfo['description'];
			}
			$files['thumb_img'][$i] = $img->icon($sys['fullPath']."/".$file, 1, 1, $sys['path']);
			$files['icon'][$i] = $fileinfo['icon'];
			if ($files['thumb_img'][$i] == $files['icon'][$i]) {
				$files['thumb_img'][$i] = "";
			}
			$files['filetype'][$i] = $fileinfo['type'];
		}
		$files['formatedFileSize'][$i] = $fm->formatFileSize($files['size'][$i], 2, "Empty");
		
}
}
$d->close();






/* Sorting icons */

if ($_SESSION['sortDir'] == "ASC") {
	$sys['sortDir'] = "ASC";
} else {
	$sys['sortDir'] = "DESC";
}


if ($_SESSION['detailSort'] == "filesize") {

$files = arrayMultiSort($files, array(
"isdir"=>"SORT_DESC", 
"size"=>"SORT_".$sys['sortDir'].""
));

} elseif ($_SESSION['detailSort'] == "date") {

$files = arrayMultiSort($files, array(
"isdir"=>"SORT_DESC", 
"mdate"=>"SORT_".$sys['sortDir'].""
));

} elseif ($_SESSION['detailSort'] == "filetype") {

$files = arrayMultiSort($files, array(
"isdir"=>"SORT_DESC", 
"filetype"=>"SORT_".$sys['sortDir']."", 
"ext"=>"SORT_".$sys['sortDir'].""
));

} else {

//filename sort
$files = arrayMultiSort($files, array(
"isdir"=>"SORT_DESC", 
"lcname"=>"SORT_".$sys['sortDir'].", SORT_STRING", 
"name"=>"SORT_".$sys['sortDir'].", SORT_STRING", 
));

}






define('SMARTY_DIR', gluePath($sys['root'],"/system/class/smarty/"));
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = false;
$smarty->security = false;
$smarty->secure_dir = array(gluePath($sys['root'],"/customizable/private/templates/"));
$smarty->security_settings = array(
	"IF_FUNCS"=>array("count")
);
//$smarty->force_compile = true;
$smarty->compile_id = "custom_interfaces";
$smarty->compile_dir = gluePath($sys['root'], "system/data/temp/compiled_templates/");
if ($options['ui_type']) {
	$template = $options['ui_type'];
} else {
	$template = "default";
}

$smarty->register_block("translate", "smarty_block_translate", false); 
$smarty->register_block("trans", "smarty_block_translate", false); //just an alias

$smarty->assign("title", $settings->config['htmltitle']);


$customLogo = gluePath($sys['root'],"/customizable/public/logo.gif");
if (file_exists($customLogo)) {
	$logoImageSize = @getImageSize($customLogo);
	$smarty->assign("customLogo", array("HTMLSize"=>$logoImageSize[3]));
}


if (strlen($sys['path']) > 1) {
	$breadCrumbs[] = array("href"=>"?path=/", "title"=>"/");
	$p = explode("/", $str->fromHTML($sys['path']));
	for ($i = 1 ; $i < sizeof($p) ; $i++) {
		$f = gluePath($f, "/", $p[$i]);
		
		if (($i > 0) && ($i < sizeof($p))) {
			//echo " &raquo; ";
		}
		if ($i < sizeof($p)-1) {
			$breadCrumbs[] = array("href"=>"?path=".$str->forURL($f)."", "title"=>$p[$i]);
		} else {
			$breadCrumbs[] = array("href"=>"", "title"=>$p[$i]);
		}
	}
} else {
	$breadCrumbs[] = array("href"=>"", "title"=>"/");
}


//$GLOBALS['vlad']->debug($breadCrumbs);

$smarty->assign("breadCrumbs", $breadCrumbs);


if ($sys['path']){
	$smarty->assign("folderName", basename($str->fromHTML($sys['path'])));
}

$j=0;
$tmp = array();
for ($i = 0 ; $i < sizeof($files['name']) ; $i++) {
	$j++;
	$tmp[$j]['name'] = $files['name'][$i];
	$tmp[$j]['isdir'] = $files['isdir'][$i];
	$tmp[$j]['icon'] = $files['icon'][$i];
	$tmp[$j]['filetype'] = $files['filetype'][$i];
	$tmp[$j]['filetypeDescription'] = $files['filetypedescription'][$i];
	$tmp[$j]['formatedFileSize'] = $files['formatedFileSize'][$i];
	$tmp[$j]['path'] = gluePath($sys['path'], $tmp[$j]['name']);
	$tmp[$j]['thumb_img'] = $files['thumb_img'][$i];
}

$files = $tmp;

//$GLOBALS['vlad']->debug($files);

if (sizeof($files) > 0) {
	$smarty->assign("files", $files);
}

$smarty->assign("permissions", $permissions);
$smarty->assign("maxFileSize", ini_get("upload_max_filesize"));
$smarty->assign("path", $sys['path']);
$smarty->assign("upOneFolderPath", getUpOneDir($sys['path']));
$smarty->assign("currentUserInfo", $auth->currentUserInfo);
$smarty->assign("baseURL", $sys['baseURL']);



$smarty->register_function("funkyButton", "smarty_funkyButton");
function smarty_funkyButton($params) {
	global $tpl, $lang;
	return $tpl->btn($lang->get($params['title']), $params['onclick'], $width="", $forJS=false, $extra="", true);
}

$smarty->register_function("sortTitle", "smarty_sortTitle");
function smarty_sortTitle($params) {
	global $tpl, $str, $lang;
	ob_start();
	sortTitle($lang->get($params['title']), $params['sort'], "?path=".$str->forURL($_GET['path']), "images/interface/");
	return ob_get_clean();
}


$smarty->register_function("status2Popup", "smarty_status2Popup");
function smarty_status2Popup($params) {
	global $tpl, $statusBarMessage;
	ob_start();
	$tpl->status2Popup($statusBarMessage);
	return ob_get_clean();
}


function add_header_comment($tpl_source, &$smarty)
{
    return "<?php echo \"<!-- Webpage powered by Afian Document Manager (http://www.afian.com) -->\n\"; ?>\n".$tpl_source;
}
// register the postfilter
$smarty->register_postfilter("add_header_comment");

$smarty->display("file:".gluePath($sys['root'],"/customizable/private/templates/",$template.".html"), false, $templateInfo['main_tplid']);
?>