<?php

class list_sxExe extends xIExecutor {

	public function main() {
		$parent = new xFolder((int)$_POST['parent'], xUser::$cur);
		// --- 构建 ---
		if($parent->isOwner(xUser::$cur)) {
			$list = array('files'=>array());
			$FILEM_SHARED = $parent->is(FILEM_SHARED_R0) ? true : false;
			if($FILEM_SHARED == false) $FILEM_SHARED = xShare::access($parent, false) ? true : false;
			if(!isset($_POST['onlyShowFolders']) || ($_POST['onlyShowFolders']!='1')) {
				if($_POST['filter'] == '*') $list['files'] = $parent->listFiles('DESC', 'id', 999);
				else {
					$filters = array(
						'picture' => '"jpg","gif","png"',
						'document' => '"txt","doc","docx","ppt","xls"',
						'video' => '"avi","3gp","rmvb","mkv","mp4"',
						'torrent' => '"torrent"',
						'music' => '"mp3","wav"'
					);
					$list['files'] = $parent->listFiles('DESC', 'id', 999, 0, 0, $filters[$_POST['filter']]);
				}
			} else {
				$list['files'] = array();
			}
			if(!isset($_POST['onlyShowFiles']) || ($_POST['onlyShowFiles']!='1')) {
				$list['folders'] = $parent->listFolders('DESC', 'id', 999);
			} else {
				$list['folders'] = array();
			}
			foreach($list['folders'] as &$folder) {
				$folder['size'] = xFile::toStrSize($folder['size']);
				$folder['added'] = date('Y-m-d H:i', $folder['added']);
				$folder['paths'] = substr($folder['paths'], strlen(xUser::$cur->username)) . $folder['name'] . '/';
				// --- 权限 ---
				$folder['FILEM_SHARED'] = $FILEM_SHARED ? true : (($folder['mr0'] & FILEM_SHARED_R0) ? true : false);
				$folder['FILEM_SHARED_SUB'] = $FILEM_SHARED ? 'Sub' : '';
			}
			foreach($list['files'] as &$file) {
				$file['size'] = xFile::toStrSize($file['size']);
				$file['added'] = date('Y-m-d H:i', $file['added']);
				// --- 权限 ---
				$file['FILEM_SHARED'] = $FILEM_SHARED ? true : (($file['mr0'] & FILEM_SHARED_R0) ? true : false);
				$file['FILEM_SHARED_SUB'] = $FILEM_SHARED ? 'Sub' : '';
			}
			if($parent->id!=0) {
				$list['path'] = str_replace(xUser::$cur->username,'',$parent->paths) . $parent->name;
				$list['doubleParent'] = $parent->parent;
			} else if(isset($_POST['filter']) && ($_POST['filter'] != '*')) {
				$list['path'] = $_POST['filter'] . '/';
			} else $list['path'] = '/';
			$list['parent'] = $parent->id;
			$list['filter'] = isset($_POST['filter']) ? $_POST['filter'] : '*';
			echo json_encode($list);
		}
	}

	public function onError($result) {

	}
}

