<?php

class rename_sxExe extends xIExecutor {

	public function main() {
		if($_POST['type'] == 'folder') {
			// --- 重命名文件夹 ---
			$folder = new xFolder((int)$_POST['id']);
			if($folder->isValid() && $folder->isOwner(xUser::$cur)) {
				$folderPath = substr($folder->paths, strlen(xUser::$cur->username));
				if($folderPath . $folder->name . '/' != '/AppData/') {
					// --- 不是 AppData，那是否在 AppData 内部 ---
					if(substr($folderPath,0,9) != '/AppData/') {
						$bs = array('\\','/',':','*','?','"','<','>','|');
						$goOn = true;
						foreach($bs as $b) {
							if(strpos($_POST['name'], $b)!==false) {
								$goOn = false;
								break;
							}
						}
						if($goOn) {
							if($folder->move(new xFolder((int)$folder->parent, xUser::$cur), $_POST['name'])) {
								echo json_encode(array(
									'result' => '1'
								));
							} 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' => 'AppData 下的所有文件夹禁止重命名'
						));
					}
				} else {
					echo json_encode(array(
						'result' => '0',
						'msg' => 'AppData 目录禁止重命名'
					));
				}
			} else {
				echo json_encode(array(
					'result' => '0',
					'msg' => '目录不存在'
				));
			}
		} else if($_POST['type'] == 'file') {
			// --- 重命名文件 ---
			$file = new xFile((int)$_POST['id']);
			if($file->isValid() && $file->isOwner(xUser::$cur)) {
				$filePath = str_replace(xUser::$cur->username,'',$file->paths);
				if(substr($filePath,0,9) != '/AppData/') {
					$bs = array('\\','/',':','*','?','"','<','>','|');
					$goOn = true;
					foreach($bs as $b) {
						if(strpos($_POST['name'], $b)!==false) {
							$goOn = false;
							break;
						}
					}
					if($goOn) {
						$group = xUser::$cur->getGroup();
						if(xFile::verifyExt($_POST['name'], $group->file_ext)) {
							if($file->move(new xFolder((int)$file->parent, xUser::$cur), $_POST['name'])) {
								echo json_encode(array(
									'result' => '1'
								));
							} else {
								echo json_encode(array(
									'result' => '0',
									'msg' => '此位置已经包含了同名文件，无法重命名'
								));
							}
						} else {
							echo json_encode(array(
								'result' => '0',
								'msg' => '您只能将文件扩展名修改为 '.$group->file_ext
							));
						}
					} else {
						echo json_encode(array(
							'result' => '0',
							'msg' => '文件名不许包含不合法的字符。<br />不允许包含：\\ / : * ? " < > |'
						));
					}
				} else {
					echo json_encode(array(
						'result' => '0',
						'msg' => 'AppData 下的所有文件禁止重命名'
					));
				}
			} else {
				echo json_encode(array(
					'result' => '0',
					'msg' => '文件不存在'
				));
			}
		}
	}

	public function onError($result) {

	}
}

