<?php

class addFolder_sxExe extends xIExecutor {

	public function main() {
		$_POST['title'] = trim($_POST['title']);
		$_POST['title'] = str_replace('　','',$_POST['title']);
		if($_POST['title'] != '') {
			$bs = array('\\','/',':','*','?','"','<','>','|');
			$goOn = true;
			foreach($bs as $b) {
				if(strpos($_POST['title'], $b)!==false) {
					$goOn = false;
					break;
				}
			}
			if($goOn) {
				// --- 不许在 AppData 目录下新建 ---
				$parentFolderID = (int)$_POST['parent'];
				$folder = new xFolder($parentFolderID, xUser::$cur);
				if($folder->isValid() && $folder->isOwner(xUser::$cur)) {
					$folderPath = str_replace(xUser::$cur->username,'',$folder->paths);
					if(($folderPath.$folder->name.'/' != '/AppData/') && ($folderPath != '/AppData/')) {
						try {
							xFolder::create($_POST['title'], xUser::$cur, $folder);
							echo json_encode(array(
								'result' => '1'
							));
						} catch(xError $e) {
							if($e->getCode() == xError::ERR_DUP) {
								echo json_encode(array(
									'result' => '0',
									'msg' => '此位置已经包含了同名文件夹，无法新建'
								));
							}
						}
					} else {
						echo json_encode(array(
							'result' => '0',
							'msg' => 'AppData 目录下不允许新建任何文件夹'
						));
					}
				} else {
					echo json_encode(array(
						'result' => '0',
						'msg' => '上级文件夹不存在'
					));
				}
			} else {
				echo json_encode(array(
					'result' => '0',
					'msg' => '文件夹名不许包含不合法的字符。<br />不允许包含：\\ / : * ? " < > |'
				));
			}
		} else {
			echo json_encode(array(
				'result' => '0',
				'msg' => '文件夹名不许为空'
			));
		}
	}

	public function onError($result) {

	}
}

