<?php

class share_sxExe extends xIExecutor {

	public function main() {
		$msg = '';
		if(isset($_POST['checkbox']['folder'])) {
			// --- 分享文件夹 ---
			foreach($_POST['checkbox']['folder'] as $folderID) {
				$folder = new xFolder((int)$folderID);
				if($folder->isValid() && $folder->isOwner(xUser::$cur)) {
					$folderPath = str_replace(xUser::$cur->username,'',$folder->paths);
					if(($folderPath . $folder->name . '/' != '/AppData/') && (substr($folderPath,0,9) != '/AppData/')) {
						if(!$folder->containShare()) {
							try {
								xShare::create($folder, new xUser(0));
							} catch(xError $e) {
							}
						} else
							$msg .= '<br />' . $folderPath . $folder->name .' 目录内包含已分享的项目无法重复分享';
					} else {
						$msg .= '<br />' . $folderPath . $folder->name .' 禁止分享';
					}
				}
			}
		}
		if(isset($_POST['checkbox']['file'])) {
			// --- 分享文件 ---
			foreach($_POST['checkbox']['file'] as $fileID) {
				$file = new xFile((int)$fileID);
				if($file->isValid() && $file->isOwner(xUser::$cur)) {
					$filePath = str_replace(xUser::$cur->username,'',$file->paths);
					if(substr($filePath,0,9) != '/AppData/') {
						try {
							xShare::create($file, new xUser(0));
						} catch(xError $e) {
						}
					} else {
						$msg .= '<br />' . $folderPath . $folder->name .' 禁止分享';
					}
				}
			}
		}
		$echo = array(
			'result' => '1',
			'msg' => ($msg == '') ? '所选文件和文件夹都已成功分享' : '部分项目分享失败：<br />' . $msg
		);
		echo json_encode($echo);
	}

	public function onError($result) {

	}
}

