<?
require_once('../include/global.php');
require_once("../include/webhard_lib.php");
require_once("../include/file.php");
require_once('./conf.inc.php');


// 웹하드가 처음 생성되었을 땐 로딩 중단.
if (!is_numeric($_GET['wfd_key']))
	exit;


	// 공용웹하드 정보확인 및 권한제한
	$ws = webhard_get_share($_GET['wfd_key'], $ws_key=NULL, $limit=NULL);	


// sort
		$CFG['FILE_LIST_FIELD'] = array(
				'wf_name' => '이름',
				'wf_size' => '크기',
				'wf_type' => '종류',
				'wf_regdate' => '날짜'
				);
		if (!$_GET['s_key2'])
			$_GET['s_key2'] = 'ASC';
		$order_set = get_order(array_keys($CFG['FILE_LIST_FIELD']), 'wf_name', $_GET['s_key'], $_GET['s_key2']);
		$order_img = get_order_img(array_keys($CFG['FILE_LIST_FIELD']), $_GET['s_key'], $_GET['s_key2'], false, 'common_skin');		// array


// folder current
	$cg = new WebhardFolder;

	$cg_parents = $cg->getParents($_GET['wfd_key']);


	// 링크 걸기
	for ($i=0; $i<sizeof($cg_parents); $i++)
	{
		//$cg_parents[$i]['wfd_name'] = '<a href="'.$_SERVER['PHP_SELF'].'?wfd_key='.$cg_parents[$i]['wfd_key'].'">'.$cg_parents[$i]['wfd_name'].'</a>';
		if ($i+1 == sizeof($cg_parents))
		{
			$cg_parents[$i]['wfd_name'] = '<a href="JavaScript:parent.webhard_folder_click(\''.$cg_parents[$i]['wfd_key'].'\')"><b>'.$cg_parents[$i]['wfd_name'].'</b></a>';
		} else
		{
			$cg_parents[$i]['wfd_name'] = '<a href="JavaScript:parent.webhard_folder_click(\''.$cg_parents[$i]['wfd_key'].'\')">'.$cg_parents[$i]['wfd_name'].'</a>';
		}	// if()

		$f_icon = webhard_get_folder_icon($cg_parents[$i]['wfd_depth'], $cg_parents[$i]['ws_key']);
		$cg_parents[$i]['wfd_name'] = '<img src="img/'.$f_icon.'" align=absmiddle><span style="vertical-align:bottom; padding-left:2px;">'.$cg_parents[$i]['wfd_name'].'</span>';
	}	// for()

	$cg_parents_name = $cg->getParentsName($cg_parents, $split=' <img src="img/folder_split.gif" border="0" align="absmiddle"> ', '', '</span>');


// search
	$q_where = "";
	$is_search = false;

	if ($_GET['s_word'] != '')
	{
		set_q_search($_GET['s_word']);
		$q_where .= " AND wf_name LIKE '%" . $_GET['s_word'] . "%' ";

		$is_search = true;
	}	// if()


// folder list
	if ($is_search)
	{
		$cg_data = $cg->getRowsByParent($_GET['wfd_key'], 255, $first_depth=1, webhard_share_where($ws['ws_key']));
		$cg_data_cnt = (is_array($cg_data[$cg->field_key])) ? sizeof($cg_data[$cg->field_key]) : 0;

		if ($cg_data_cnt > 0)
		{
			$q_where .= " AND wfd_key IN (".$_GET['wfd_key'].",".implode(',', $cg_data['wfd_key']).") ";
		} else
		{
			$q_where .= " AND wfd_key='".$_GET['wfd_key']."' ";
		}	// if()

		$cg_data_cnt = 0;		// 검색결과에서 폴더목록은 보이지 않도록 숨김
	} else
	{
		$cg_data = $cg->getRowsByParent($_GET['wfd_key'], 1, $first_depth=1, webhard_share_where($ws['ws_key']));
		$cg_data_cnt = (is_array($cg_data[$cg->field_key])) ? sizeof($cg_data[$cg->field_key]) : 0;
		for ($i=0; $i<$cg_data_cnt; $i++)
		{
			$cg_data['wfd_memo'][$i] = han_substr(strip_tags($cg_data['wfd_memo'][$i]), 1024);
		}	// for()

		$q_where .= " AND wfd_key='".$_GET['wfd_key']."' ";
	}	// if()


// file list
	$q = "
		SELECT
			*
		FROM
			".$TB['WEBHARD_FILE']."
		WHERE
			1=1
			".$q_where."
		ORDER BY
			" . $order_set['query'] . "
		";
	$wf = db_get_rows($q);
	$wf_cnt = sizeof($wf);

	$wf_size_sum = 0;
	for ($i=0; $i<$wf_cnt; $i++)
	{
		// 윈도우에서 지원하지 않는 문자는 파일명에서 자동 치환하기
		$wf[$i]['wf_name'] = file_name_char_limit($wf[$i]['wf_name']);

		//$wf[$i]['wf_size_view'] = filesize_human($wf[$i]['wf_size']);
		$wf[$i]['wf_size_view'] = number_format(@ceil($wf[$i]['wf_size']/1024)).'KB';
		$wf[$i]['wf_type'] = strtoupper($wf[$i]['wf_type']);
		$wf[$i]['wf_memo'] = han_substr(strip_tags($wf[$i]['wf_memo']), 1024);

		// 검색결과에서는 파일명앞에 자신의 폴더명을 붙여서 보여줌.
		if ($is_search)
		{
			// [2008-07-01] 폴더가 없을때 검색시 발생하는 에러 수정
			$as = @array_search($wf[$i]['wfd_key'], $cg_data['wfd_key']);
			if ($as === false)
			{
				$wfd_name = '내 웹하드';
			} else 
			{
				$wfd_name = $cg_data['wfd_name'][$as];
			}	// if()
			$wf[$i]['wf_name_view'] = $wfd_name.' > '.$wf[$i]['wf_name'];
		} else 
		{
			$wf[$i]['wf_name_view'] = $wf[$i]['wf_name'];
		}	// if()

		$wf_size_sum += $wf[$i]['wf_size'];
	}	// for()

	$wf_size_sum = filesize_human($wf_size_sum);


// 사용용량
	$DEBUG['wfd_size']['start'] = getmicrotime();
	if ($ws['ws_key'] > 0)		// 공용웹하드
	{
		$m_webhard_quota = $ws['ws_quota']*1024;
		$m_webhard_quota_view = filesize_human($m_webhard_quota);

		$ms_webhard = $ws['ws_quota_use'];
		$ms_webhard_view = filesize_human($ms_webhard);
		$webhard_free_size = $m_webhard_quota - $ms_webhard;
		if ($webhard_free_size < 0)
			$webhard_free_size = 0;

		$m_webhard_quota_rate = @ceil($ms_webhard / $m_webhard_quota * 100);


		// 권한정보
			if ($ws['ws_mg_no'] != '')
			{
				$q = "SELECT mg_name FROM ".$TB['MEMBER_GROUP']." WHERE d_no='".$ws['d_no']."' AND mg_no IN (".implode(',', db_str_explode('|', $ws['ws_mg_no'])).") ORDER BY mg_name";
				$mg = db_get_rows($q, true);
				// [2009-12-09] 회원 조직에 지정된 조직이 삭제되고난 후 에러 메세지 해결
				//$ws['ws_mg_no'] = implode(',', $mg['mg_name']);								
				$ws['ws_mg_no'] = @implode(',', $mg['mg_name']);								
			}	// if()		

			$ws['ws_perm_view'] = '';

			// [2008-02-21] 관리자일 경우 모든 권한 부여
			if ($ws['ws_admin_id'] != '' && strpos($ws['ws_admin_id'], '|'.$AUTH['auth_m_id'].'|') !== false)
				$ws['ws_perm'] = '|W|R|D|';

			foreach ($G_SYS['WEBHARD_WS_PERM'] as $key=>$val)
			{
				if (strpos($ws['ws_perm'], $key) !== false)
				{
					$ws['ws_perm_view'] .= '<font color=blue>'.$val.'</font>';
				} else
				{
					$ws['ws_perm_view'] .= '<font color=red><strike>'.$val.'</strike></font>';
				}	// if()
				if ($key != 'D')
					$ws['ws_perm_view'] .= ',';
			}	// foreach()

			$ws['ws_guest_id'] = implode(',', db_str_explode('|', $ws['ws_guest_id']));
			$ws['ws_admin_id'] = implode(',', db_str_explode('|', $ws['ws_admin_id']));

	} else		// 내 웹하드
	{
		$q = "SELECT m_webhard_quota FROM ".$TB['MEMBER']." WHERE m_no='".$AUTH['auth_m_no']."'";
		$m_webhard_quota = db_get_one($q);
		$m_webhard_quota = $m_webhard_quota*1024;
		$m_webhard_quota_view = filesize_human($m_webhard_quota);

		$q = "SELECT ms_webhard FROM ".$TB['MEMBER_SIZE']." WHERE m_no='".$AUTH['auth_m_no']."'";
		$ms_webhard = db_get_one($q);
		$ms_webhard_view = filesize_human($ms_webhard);

		$webhard_free_size = $m_webhard_quota - $ms_webhard;
		if ($webhard_free_size < 0)
			$webhard_free_size = 0;

		$m_webhard_quota_rate = @ceil($ms_webhard / $m_webhard_quota * 100);

		// 내 웹하드는 모든 권한 부여
		$ws['ws_perm'] = '|W|R|D|';
	}	// if()
	$DEBUG['wfd_size']['exec'] += getmicrotime() - $DEBUG['wfd_size']['start'];


// skin view
	nskinset('webhard', 'file_list.html', 'iframe');
	include($NSKIN['view_layout']);

require_once('include/global_end.php');
?>