Exemplo n.º 1
0
 /**
  * 控制器执行主逻辑函数, 处理删除文件或者文件夹
  */
 public function invoke($uri = null)
 {
     $this->setAction(MConst::DELETE);
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 8'), MConst::HTTP_CODE_400);
     }
     //
     // 获取用户数据,如user_id
     $user = MUserManager::getInstance()->getCurrentUser();
     $device = MUserManager::getInstance()->getCurrentDevice();
     $this->_user_id = $user["user_id"];
     $user_nick = $user["user_name"];
     $user_device_id = $device["device_id"];
     $this->_user_device_name = $device["user_device_name"];
     // 文件大小格式化参数
     $locale = "bytes";
     if (isset($params["locale"])) {
         $locale = $params["locale"];
     }
     if (isset($params["root"]) === false || isset($params["path"]) === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 9'), MConst::HTTP_CODE_400);
     }
     $root = $params["root"];
     $path = $params["path"];
     $isDir = $params["is_dir"];
     $pathArr = explode('/', $path);
     if ($path == '/' || empty($pathArr[2]) || empty($pathArr[1])) {
         return;
     }
     if ($isDir) {
         //避免人为添加删除目录
         $arr = explode('/', $path);
         $isRoot = false;
         $isMine = false;
         if (count($arr) == 3) {
             $isRoot = true;
         }
         $fileOwnerId = $arr[1];
         $currentUserId = $this->_user_id;
         if ($fileOwnerId == $currentUserId) {
             $isMine = true;
         }
         if ($isRoot && !$isMine) {
             //如果是在根目录下且不是自己的目录 则后台控制不准取消共享
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_409);
         }
     }
     //
     // 转换路径分隔符,便于以后跨平台,如:将 "\"=>"/"
     //
     $path = MUtils::convertStandardPath($path);
     if ($path == "" || $path == "/" || $path === false) {
         throw new MFileopsException(Yii::t('api', 'Bad request 10'), MConst::HTTP_CODE_400);
     }
     // 检查是否是共享目录
     $share_filter = MSharesFilter::init();
     if ($share_filter->handlerCheck($this->_user_id, $path)) {
         $this->_user_id = $share_filter->master;
         $path = $share_filter->_path;
     }
     //
     // 如果删除的是共享目录,则转到ShareManager处理
     //
     if ($share_filter->_is_shared_path && $share_filter->operator != $share_filter->master) {
         $file = MFiles::queryFilesByPath("/" . $share_filter->operator . $share_filter->src_path);
         if (!$file) {
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
         }
         $id = $file[0]["id"];
         $handler = new ShareManager();
         $handler->_userId = $share_filter->operator;
         $handler->_id = $id;
         try {
             $handler->invoke(ShareManager::CANCEL_SHARED);
         } catch (Exception $e) {
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
         }
         // 输出返回值
         $path = MUtils::convertStandardPath($share_filter->src_path);
         $this->buildResult($root, $path, $handler->_file["version_id"], $handler->_file["file_update_time"], true);
         return;
     }
     if ($share_filter->_is_shared_path && $share_filter->operator != $share_filter->master) {
         throw new MException(Yii::t('api', 'You do not have permission to perform the delete operation.'), MConst::HTTP_CODE_409);
     }
     //
     // 组装对象信息
     //
     $file_name = MUtils::get_basename($path);
     $file_detail = new MFiles();
     $file_detail->file_name = $file_name;
     $file_detail->file_path = $path;
     //
     // 查询其目录信息,是否存在
     //
     $query_db_file = MFiles::queryFilesByPath($file_detail->file_path);
     //数据已不存在
     if (count($query_db_file) <= 0) {
         throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404);
     }
     $data = array("obj" => $this, "share_filter" => $share_filter, "query_db_file" => $query_db_file[0]);
     //在共享文件夹中进行删除权限判断
     if ($share_filter->is_shared && $query_db_file[0]["file_type"] != MConst::OBJECT_TYPE_BESHARED) {
         if ($query_db_file[0]["file_type"] == 0) {
             //文件删除
             $share_filter->hasPermissionExecute($query_db_file[0]["file_path"], MPrivilege::FILE_DELETE);
         } else {
             //文件夹删除
             $share_filter->hasPermissionExecute($query_db_file[0]["file_path"], MPrivilege::FOLDER_DELETE);
         }
     }
     //
     // 可以删除包含子文件的目录
     // 检查其是否为文件夹
     //
     $files = array();
     $file_detail->is_dir = false;
     $file_detail->id = $query_db_file[0]["id"];
     $file_detail->file_size = $query_db_file[0]["file_size"];
     $file_detail->file_type = $query_db_file[0]["file_type"];
     if ($query_db_file[0]["file_type"] > MConst::OBJECT_TYPE_FILE) {
         $file_detail->is_dir = true;
         $files = $this->handleChildrenFile($file_detail->file_path, $files);
     } else {
         // 处理加入版本历史
         $this->handleFileMeta($file_detail->file_path, $query_db_file[0]["version_id"], $this->_user_id, $user_nick, $this->_user_device_name, $file_detail->file_size);
     }
     $isSharedPath = false;
     $pathArr = explode('/', $file_detail->file_path);
     $masterId = $pathArr[1];
     if ($masterId != $this->_user_id) {
         $isSharedPath = true;
     } else {
         $model = new GeneralFolderPermissionBiz($file_detail->file_path);
         if ($model->isParentShared($file_detail->file_path)) {
             //如果是父目录被共享
             $isSharedPath = true;
         }
     }
     if ($isSharedPath) {
         $permissionArr = UserPermissionBiz::getInstance()->getPermission($file_detail->file_path, $user["user_id"]);
         $permission = $permissionArr['permission'];
         if (!empty($permission)) {
             $privilegeModel = new PrivilegeBiz();
             $share_filter->slaves = $privilegeModel->getSlaveIdsByPath($permissionArr['share_root_path']);
             $share_filter->is_shared = true;
             if ($file_detail->file_type == 0) {
                 //删除文件
                 $can_file_delete = substr($permission, 7, 1);
                 if ($can_file_delete == 0) {
                     throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
                 }
             }
             if ($file_detail->file_type == 1 || $file_detail->file_type == 2 || $file_detail->file_type == 4) {
                 $can_folder_delete = substr($permission, 3, 1);
                 if ($can_folder_delete == 0) {
                     throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
                 }
             }
         }
     }
     //
     // 更新文件元数据的为删除数据
     //
     $this->assembleFileDetail($file_detail, $query_db_file[0]);
     $ret_value = MFiles::updateRemoveFileDetail($file_detail);
     if ($ret_value === false) {
         throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
     }
     //
     // 将删除目录加入数组
     //
     array_push($files, $file_detail);
     //
     // 保存事件
     //
     $ret_value = MiniEvent::getInstance()->createEvents($this->_user_id, $user_device_id, $files, $share_filter->type);
     if ($ret_value === false) {
         throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
     }
     //
     //
     //
     if ($share_filter->is_shared) {
         foreach ($files as $file) {
             $share_filter->handlerAction($file->event_action, $user_device_id, $file->from_path, $file->context);
         }
     }
     //
     // 删除共享目录(删除共享目录,对应的权限也一起删除)
     //
     //首先判断用户有无删除权限
     $userPrivilegeList = MiniUserPrivilege::getInstance()->getPrivilegeList($file_detail->file_path);
     $groupPrivilegeList = MiniGroupPrivilege::getInstance()->getPrivilegeList($file_detail->file_path);
     if (!empty($userPrivilegeList)) {
         MiniUserPrivilege::getInstance()->deleteByFilePath($file_detail->file_path);
     }
     if (!empty($groupPrivilegeList)) {
         MiniGroupPrivilege::getInstance()->deleteByFilePath($file_detail->file_path);
     }
     //并且将file_type改为1
     if ($file_detail->file_type == 0) {
         MiniFile::getInstance()->togetherShareFile($file_detail->file_path, Mconst::OBJECT_TYPE_FILE);
     } else {
         MiniFile::getInstance()->togetherShareFile($file_detail->file_path, Mconst::OBJECT_TYPE_DIRECTORY);
     }
     if ($filter !== true && $share_filter->_is_shared_path && $share_filter->operator == $share_filter->master) {
         $file = MFiles::queryFilesByPath("/" . $share_filter->operator . $path, true);
         if (!$file) {
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
         }
         $id = $file[0]["id"];
         $handler = new ShareManager();
         $handler->_userId = $share_filter->operator;
         $handler->_id = $id;
         try {
             $handler->invoke(ShareManager::CANCEL_SHARED);
         } catch (Exception $e) {
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
         }
     }
     // 如果彻底删除,则调用回收站
     if ($this->completely_remove) {
         $trash = new Trash();
         $trash->_userId = $this->_user_id;
         $trash->fromIds = $file_detail->id;
         try {
             $trash->invoke(Trash::DELETE);
         } catch (Exception $e) {
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
         }
         //执行的额外操作
         $this->extend($share_filter, $query_db_file, $file_detail);
         return;
     }
     $path = CUtils::removeUserFromPath($query_db_file[0]["file_path"]);
     $path_info = MUtils::pathinfo_utf($path);
     $path_info_out = MUtils::pathinfo_utf($share_filter->src_path);
     $path = MUtils::convertStandardPath($path_info_out['dirname'] . "/" . $path_info['basename']);
     //执行的额外操作
     $this->extend($share_filter, $query_db_file, $file_detail);
     $this->buildResult($root, $path, $query_db_file[0]["version_id"], $query_db_file[0]["file_update_time"], $file_detail->is_dir);
 }