<?php
require 'inc.php';
require SYS_ROOT.'/cls_ctl/ctl.file.php';

$arr_file_ext=array("html","js","css");
if(!isset($get_folder_path)) $get_folder_path="";
if(!isset($get_filename)) $get_filename="";
$arr_x=array("root_path"=>"/".TEMPLATES_DIR."/temp".$get_folder_path);
$obj_ctl_file=new cls_ctl_file($arr_x);


if(!empty($get_filename)){
	$arr_x=explode(".",$get_filename);
	if(!in_array(end($arr_x),$arr_file_ext)) exit("文件名非法");
}
if($get_act=="save_folder"){
	if($get_folder_oldname==""){
		$return_msg=$obj_ctl_file->on_dir_create($get_folder_name);
	}else{
		$return_msg=$obj_ctl_file->on_dir_rename($get_folder_oldname,$get_folder_name);
	}
	if($return_msg!=""){
		$obj_msg->alert($return_msg);
	}
}
if($get_act=="del_folder" && !empty($get_folder_name)){
	$return_msg=$obj_ctl_file->on_dir_delete($get_folder_name);
}
if($get_act=="del_file" && !empty($get_filename)){
	$return_msg=$obj_ctl_file->on_file_delete($get_filename);
}
$page_footer_info=<<<HTML
<input type='hidden' name='folder_path' value='{$get_folder_path}'>
<input type='hidden' name='filename' value='{$get_filename}'>
<input type='hidden' name='folder_name' value=''>
<input type='hidden' name='folder_oldname' value=''>
HTML;

$str_path="";
$arr_x=explode("/",$get_folder_path);
for($i=0;$i<count($arr_x);$i++){
	if(empty($arr_x[$i])) continue;
	$str_path.="/".$arr_x[$i];
	$arr_x[$i]="<a href=\"javascript:meOpendir('".$str_path."');\">".$arr_x[$i]."</a>";
}
$this_folder_path="<a href=\"javascript:meOpendir('');\">根目录</a>".implode(" -> ",$arr_x);


switch($get_act)
{	
	case 'save':
	{
		if(strtolower(DB_CHARSET)=="gbk") $get_file_content=format_utf8_gbk($get_file_content);
		$arr_msg=$obj_ctl_file->on_file_create($get_filename,$get_file_content);
	    break;
	}
	case 'edit':
	{
		if(empty($get_filename)){
			$file_content="";
		}else{
			$file_content=$obj_ctl_file->get_file_content($get_filename);
		}
		include "tpl/templates.edit.tpl.php";
		break;
	}
	default:
	{
		$arr_dir=$obj_ctl_file->get_dirs();
		if(empty($get_folder_path)){
			$arr_x=$arr_dir;
			$arr_dir=array();
			foreach($arr_x as $item){
				if($item["name"]=="sys") continue;
				$arr_dir[]=$item;
			}
		}
		$arr_f=$obj_ctl_file->get_files();
		$arr_file=array();
		foreach($arr_f as $item){
			$arr_x=explode(".",$item["name"]);
			if(!in_array(strtolower(end($arr_x)),$arr_file_ext)) continue;
			$item["app"]=$arr_x[0];
			$arr_file[]=$item;
		}
		include "tpl/templates.tpl.php";

	}
}

?>
