<?php

class upload_sxExe extends xIExecutor {

	public function main() {
		$bs = array('\\','/',':','*','?','"','<','>','|');
		$goOn = true;
		foreach($bs as $b) {
			if(strpos($_FILES['Filedata']['name'], $b)!==false) {
				$goOn = false;
				break;
			}
		}
		if($goOn) {
			$folder = new xFolder((int)$_POST['id'], xUser::$cur);
			$group = xUser::$cur->getGroup();
			if($folder->isValid() && $folder->isOwner(xUser::$cur)) {
				$folderPath = str_replace(xUser::$cur->username,'',$folder->paths);
				if(($folderPath.$folder->name.'/' != '/AppData/') && ($folderPath != '/AppData/')) {
					$serverFileLimit = str_replace('M','',get_cfg_var('upload_max_filesize')) * 1024 * 1024;
					$filesizeLimit = (($group->filesize_limit == 0) || ($group->filesize_limit > $serverFileLimit)) ? $serverFileLimit : $group->filesize_limit;
					if($_FILES['Filedata']['size'] <= $filesizeLimit) {
						if(xFile::verifyExt($_FILES['Filedata']['name'], $group->file_ext)) {
							if(xUser::$cur->occupySpace($_FILES['Filedata']['size'], 1)) {
								$vpath = date('Y');
								if(!is_dir(__ROOT__.'volume/'.$vpath)) {
									mkdir(__ROOT__.'volume/'.$vpath, 0777);
									chmod(__ROOT__.'volume/'.$vpath, 0777);
								}
								$vpath .= '/'.date('m');
								if(!is_dir(__ROOT__.'volume/'.$vpath)) {
									mkdir(__ROOT__.'volume/'.$vpath, 0777);
									chmod(__ROOT__.'volume/'.$vpath, 0777);
								}
								$vpath .= '/'.date('d');
								if(!is_dir(__ROOT__.'volume/'.$vpath)) {
									mkdir(__ROOT__.'volume/'.$vpath, 0777);
									chmod(__ROOT__.'volume/'.$vpath, 0777);
								}
								file_put_contents('volume/'.$vpath.'/index.php','');
								$vfilename = xStrUtils::getInst()->randomString(16, 'abcdefghijklmnopqrstuvwxyz0123456789').'.fvf';
								while(is_file(__ROOT__.'volume/'.$vpath.'/'.$vfilename)) {
									$vfilename = xStrUtils::getInst()->randomString(16, 'abcdefghijklmnopqrstuvwxyz0123456789').'.fvf';
								}
								if($vfile = xVFile::create((int)$_FILES['Filedata']['size'], 1)) {
									xGbl::$mysql->exec('INSERT INTO `'.TB_PREFIX.'volume_vfiles` (`id` ,`size` ,`path`) VALUES ("'.$vfile->id.'", "'.$vfile->size.'", "'.xGbl::$mysql->escape($vpath.'/'.$vfilename).'");');
									move_uploaded_file($_FILES['Filedata']['tmp_name'], __ROOT__.'volume/'.$vpath.'/'.$vfilename);
									$xf = false;
									try {
										$xf = xFile::create($_FILES['Filedata']['name'], xUser::$cur, $vfile, $folder);
										$vfile->uploadDone();
										xUser::$cur->select(xUser::$cur->id+0, 1);
										echo json_encode(array(
											'result' => '1',
											'diskUsageStr' => xFile::toStrSize(xUser::$cur->disk_usage),
											'diskMaxStr' => xFile::toStrSize(xUser::$cur->disk_max),
											'usagePer' => round(xUser::$cur->disk_usage/xUser::$cur->disk_max)
										));
									} catch(xError $e) {
										if($e->getCode() == xError::ERR_DUP) {
											// --- 重新编辑文件名 ---
											$i = 1;
											if(strpos($_FILES['Filedata']['name'],'.') !== false) {
												$oldFilename1 = substr($_FILES['Filedata']['name'], 0, strrpos($_FILES['Filedata']['name'], '.'));
												$oldFilename2 = substr($_FILES['Filedata']['name'], strrpos($_FILES['Filedata']['name'], '.') + 1);
											} else {
												$oldFilename1 = $_FILES['Filedata']['name'];
												$oldFilename2 = '';
											}
											while(true) {
												try {
													$xf = xFile::create($oldFilename1.'('.$i.').'.$oldFilename2, xUser::$cur, $vfile, $folder);
													$vfile->uploadDone();
													xUser::$cur->select(xUser::$cur->id+0, 1);
													echo json_encode(array(
														'result' => '1',
														'diskUsageStr' => xFile::toStrSize(xUser::$cur->disk_usage),
														'diskMaxStr' => xFile::toStrSize(xUser::$cur->disk_max),
														'usagePer' => round(xUser::$cur->disk_usage/xUser::$cur->disk_max*100)
													));
													break;
												} catch(xError $e) {
													if($e->getCode() == xError::ERR_DUP) {
														++$i;
													} else {
														echo json_encode(array(
															'result' => '0',
															'msg' => '未知错误，上传失败'
														));
														break;
													}
												}
											}
											
										}
									}
									if($xf !== false) {
										// --- 检测是不是自动共享 ---
										if($group->is(USR_R0_DEFAULTSHARE)) {
											// --- 检测这个文件是否已经共享，若未则共享 ---
											if(!xShare::access($xf, false)) {
												xShare::create($xf, new xUser(0));
											}
										}
									}
								} else {
									xUser::$cur->freeSpace((int)$_FILES['Filedata']['size'], 1);
									echo json_encode(array(
										'result' => '0',
										'msg' => '云卷已满，请联系管理员解决'
									));
								}
							} 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' => '文件超过单文件大小限制'
						));
					}
				} 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 />不允许包含：\\ / : * ? " < > |'
			));
		}
	}
	
	public function onError($result) {

	}

}

