<?php

/**
 *--------------------------------------
 * 文件浏览
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-17
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class FileExploreCtrlr extends ManageCtrlr
{
	public function list_file()
	{
		$o_upload = M('Option')->get_option('upload');

		$baseDir = APP_PATH.$o_upload['dir'];
		$currentDir = D_S.ltrim(str_replace(array('*', '@'), array(D_S, '.'), ARequest::get('dir')), D_S);
		if(D_S == $currentDir)
		{
			$currentDir = '';
		}

		$currentPath = $baseDir.$currentDir;

		$_FL['parent_dir'] = str_replace(array(D_S, '.'), array('*', '@'), dirname($currentDir));
		$_FL['current_dir'] = str_replace(array(D_S, '.'), array('*', '@'), $currentDir);

		if(is_dir($currentPath))
		{
			$fileSet = scandir($currentPath);
			foreach($fileSet as $fs)
			{
				if('.' == $fs or '..' == $fs or '.svn' == $fs)
				{
					continue;
				}

				if(is_dir($currentPath.D_S.$fs))
				{
					$dir['name'] = $fs;
					$_FL['list']['dir'][] = $dir;
				}
				else
				{
					$fileNameInfo = explode('.', $fs);
					$file['name'] = $fs;
					$file['type'] = $fileNameInfo[count($fileNameInfo) - 1];
					$file['type_info'] = $this->get_file_type($file['name']);
					$file['size'] = byte_format(filesize($currentPath.D_S.$fs));
					$file['edit_time'] = date("Y-n-d H:i:s", filemtime($currentPath.D_S.$fs));
					$_FL['list']['file'][] = $file;
				}
			}
		}
		$this->assign('_FL', $_FL);

		$this->display();
	}

	public function make_dir_do()
	{}

	public function delete_dir_do()
	{}

	public function upload_file_do()
	{}

	public function download_file_do()
	{
		$o_upload = M('Option')->get_option('upload');

		$baseDir = dirname(PFA_PATH).$o_upload['dir'];
		$file = $baseDir.D_S.ltrim(str_replace(array('*', '@'), array(D_S, '.'), ARequest::get('file')), D_S);

		if(!is_file($file))
		{
			M('AdminLog')->add_log(ASession::get('m_userid'), L('DOWNLOAD').L('FILE').': FILE['.ARequest::get('file').']'.L('FILE_NOT_EXSIT'), 0);
			$this->error(L('FILE_NOT_EXSIT'), Url::U('file_explore/list_file'));
		}
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DOWNLOAD').L('FILE').': FILE['.ARequest::get('file').']');

		$fileName = basename($file);
		$fileNameInfo = explode('.', $fileName);
		$fileType = $fileNameInfo[count($fileNameInfo) - 1];
		header('Content-type: application/x-'.$fileType);
		header('Content-Disposition: attachment; filename='.$fileName);
		header('Content-Description: PHP3 Generated Data');
		readfile($file);
		exit;
	}

	public function delete_file_do()
	{
		$o_upload = M('Option')->get_option('upload');

		$baseDir = APP_PATH.$o_upload['dir'];

		$_file = ARequest::get('file');
		$_L_ID = is_array($_file) ? implode(',', $_file) : $_file; /* 日志记录ID */

		if(is_array($_file))
		{
			foreach($_file as $_file)
			{
				$file = $baseDir.D_S.ltrim(str_replace(array('*', '@'), array(D_S, '.'), $_file), D_S);
				if(!is_file($file))
				{
					M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('FILE').': FILE['.$_file.']'.L('FILE_NOT_EXSIT'), 0);
					$this->error(L('FILE_NOT_EXSIT'), Url::U('file_explore/list_file'));
				}
				@unlink($file);
			}
		}
		else
		{
			$file = $baseDir.D_S.ltrim(str_replace(array('*', '@'), array(D_S, '.'), $_file), D_S);
			if(!is_file($file))
			{
				M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('FILE').': FILE['.$_file.']'.L('FILE_NOT_EXSIT'), 0);
				$this->error(L('FILE_NOT_EXSIT'), Url::U('file_explore/list_file'));
			}
			@unlink($file);
		}

		/* 记录日志 */
		M('AdminLog')->add_log(ASession::get('m_userid'), L('DELETE').L('FILE').': FILE['.$_L_ID.']');
		$this->success(L('DELETE_SUCCESS'), Url::U('file_explore/list_file'));
	}

	/* 获取文件介绍名称 */
	private function get_file_type($filename)
	{
		if(preg_match('/\.gif|\.jpg|\.jpeg|\.png|\.bmp/i', $filename))
		{
			return L('PICTURE_FILE');
		}
		if(preg_match('/\.css/i', $filename))
		{
			return L('STYLE_SHEET_FILE');
		}
		if(preg_match('/\.php|\.js/i', $filename))
		{
			return L('SCRIPT_FILE');
		}
		if(preg_match('/\.txt/i', $filename))
		{
			return L('TEXT_FILE');
		}
		if(preg_match('/\.html|\.htm/i', $filename))
		{
			return L('WEB_PAGE_FILE');
		}
		return L('CUSTOM_FILE');
	}
}

?>