Exemplo n.º 1
0
 /**
  * 初始化参数对象
  * 解析外部参数
  * @param string $uri
  * @param array $config
  * @throws Exception
  * @throws MException
  * @return mix
  */
 public static function initMThumbnailBase($uri, $config = NULL)
 {
     $thumbnailBase = new MThumbnailBase();
     $format = "jpeg";
     $size = "small";
     if (isset($_REQUEST["format"])) {
         $format = strtolower($_REQUEST["format"]);
     }
     // 默认值format
     if ($format != "jpeg" && $format != "png") {
         $format = "jpeg";
     }
     // 默认值size
     if (isset($_REQUEST["size"])) {
         $size = $_REQUEST["size"];
     }
     $signature = $_REQUEST["signature"];
     // 解析文件路径,若返回false,则错误处理
     $urlManager = new MUrlManager();
     $path = $urlManager->parsePathFromUrl($uri);
     if ($path == false) {
         throw new MException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_404);
     }
     $parts = array_slice(explode('/', $uri), 3);
     $root = $parts[0];
     // 检索的根路径
     // 解析路径
     $path = "/" . $path;
     $path = MUtils::convertStandardPath($path);
     // 用户信息
     $user = MUserManager::getInstance()->getCurrentUser();
     if (!empty($_REQUEST["userId"]) && $_REQUEST["userId"] != 'undefined' && $user['user_name'] == 'admin') {
         $userId = $_REQUEST["userId"];
         $user = MiniUser::getInstance()->getUser($userId);
     }
     if (dirname($path) == "/") {
         $path = "/" . $user['id'] . $path;
     }
     $device = MUserManager::getInstance()->getCurrentDevice();
     $thumbnailBase->user_id = $user["user_id"];
     $thumbnailBase->user_nick = $user["user_name"];
     $thumbnailBase->user_device_id = $device["device_id"];
     $thumbnailBase->size = $size;
     $thumbnailBase->format = $format;
     $thumbnailBase->path = MUtils::convertStandardPath($path);
     $thumbnailBase->root = $root;
     $thumbnailBase->config = $config;
     // 检查共享
     $share_filter = MSharesFilter::init();
     if ($share_filter->handlerCheck($thumbnailBase->user_id, $path, true)) {
         $thumbnailBase->user_id = $share_filter->master;
         $thumbnailBase->path = $share_filter->_path;
     }
     return $thumbnailBase;
 }
Exemplo n.º 2
0
 /**
  * 控制器执行主逻辑函数
  *
  */
 public function invoke($uri = null)
 {
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     // 解析文件路径,若返回false,则错误处理
     $urlManager = new MUrlManager();
     $path = $urlManager->parsePathFromUrl($uri);
     $root = $urlManager->parseRootFromUrl($uri);
     if ($path == false || $root == false) {
         throw new MFilesException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411);
     }
     // 解析路径
     $path = MUtils::convertStandardPath($path);
     MiniFile::getInstance()->download($path);
 }
Exemplo n.º 3
0
 /**
  * 从url请求解析对应的action名称
  *  比如www.xx.cn/meta/service_upload_meta,返回的是service_upload_meta
  * @return string 返回的是对应action操作
  */
 public function parseActionFromUrl()
 {
     //
     // TODO: 针对不同的php server进行部署逻辑处理
     //
     $uri = $_SERVER['REQUEST_URI'];
     //
     // iis服务器,处理编码
     //
     if (isset($_SERVER['SERVER_SOFTWARE']) && strpos(strtolower($_SERVER['SERVER_SOFTWARE']), 'iis') !== false) {
         // iis  urlencode() 或者 rawurlencode(),二者的区别是前者把空格编码为 '+',而后者把空格编码为 '%20'
         $uri = rawurldecode($uri);
         $uri = mb_convert_encoding($uri, "UTF-8", "gbk");
     } else {
         $uri = urldecode($uri);
     }
     //
     // 姜松 20120419,前置假设用户不会将服务部署在/api.php/1/文件夹下
     // 也就是不会存在这种请求:api.php/1/api.php/1/info
     // 查找对应api,便于取出控制器
     //
     $index = strpos($uri, "api.php/1/");
     if ($index != FALSE) {
         $uri = substr($uri, $index + strlen("api.php/1/"));
         self::$API_VERSION = 1;
     }
     // 转换为标准格式的路径
     $uri = MUtils::convertStandardPath($uri);
     $parts = array_slice(explode('/', $uri), 1);
     //
     // 确保只有一个对应的方法
     //
     if (count($parts) < 1) {
         return false;
     }
     $action = $parts[0];
     if ($pos = strpos($action, '?')) {
         $action = substr($action, 0, $pos);
     }
     $array = array();
     $array["action"] = $action;
     $array["uri"] = $uri;
     return $array;
 }
Exemplo n.º 4
0
 /**
  * 控制器执行主逻辑函数
  * @param null $uri
  * @param null $absolutePath
  * @throws MAuthorizationException
  * @throws MFileopsException
  */
 public function invoke($uri = null, $absolutePath = null)
 {
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false || $params == null) {
         throw new MAuthorizationException(Yii::t('api', 'Invalid parameters'));
     }
     // 获取用户数据,如user_id
     $user = MUserManager::getInstance()->getCurrentUser();
     $this->userId = $user["user_id"];
     $includeDeleted = false;
     // 处理删除的同样也需要返回
     if (isset($params["include_deleted"])) {
         $includeDeleted = $params["include_deleted"];
     }
     $this->locale = "bytes";
     if (isset($params["locale"])) {
         $this->locale = $params["locale"];
     }
     $includeDeleted = MUtils::convertToBool($includeDeleted);
     $urlManager = new MUrlManager();
     $path = MUtils::convertStandardPath($urlManager->parsePathFromUrl($uri));
     $this->root = $urlManager->parseRootFromUrl($path);
     if ($path === false) {
         $path = "/";
     }
     $pathPart = explode('/', $path);
     // 根目录
     if (count($pathPart) <= 2) {
         $response = $this->handleRootPath($includeDeleted);
     } else {
         $response = $this->handleNotRootPath($path, $includeDeleted);
     }
     echo json_encode($response);
 }
Exemplo n.º 5
0
 /**
  * 控制器执行主逻辑函数
  */
 public function invoke($uri = null)
 {
     $this->setAction(MConst::CREATE_FILE);
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $urlManager = new MUrlManager();
     $root = $urlManager->parseRootFromUrl($uri);
     if ($root == false) {
         //支持参数模式传递上传路径
         $path = MiniHttp::getParam("path", "");
         if (empty($path)) {
             throw new MFilesException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411);
         }
     }
     // 初始化创建文件公共类句柄
     $createFileHandler = MFilesCommon::initMFilesCommon();
     if (count($_FILES) == 0) {
         throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "5"), MConst::HTTP_CODE_400);
     }
     $keys = array_keys($_FILES);
     if (count($keys) != 1) {
         throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "6"), MConst::HTTP_CODE_400);
     }
     $key = $keys[0];
     // 检查请求参数$_FILES
     if (isset($_FILES[$key]) === false) {
         throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "7"), MConst::HTTP_CODE_400);
     }
     // 检查文件上传过程是否有错
     if ($_FILES[$key]["error"] != 0) {
         throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "8"), MConst::HTTP_CODE_400);
     }
     $fileName = $_FILES[$key]["name"];
     $type = MiniUtil::getMimeType($fileName);
     $size = $_FILES[$key]["size"];
     $tmpName = $_FILES[$key]["tmp_name"];
     // 验证文件是否已经上传成功
     if (file_exists($tmpName) === false) {
         throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500);
     }
     // 检查文件上传错误
     if (filesize($tmpName) != $size) {
         throw new MFilesException(Yii::t('api', "The file upload error!"), MConst::HTTP_CODE_400);
     }
     //断点文件上传
     if ($this->isBreakpointUpload()) {
         $filesController = new MFilePutController();
         $filesController->invoke($uri);
     } else {
         //完整文件上传
         $signature = MiniUtil::getFileHash($tmpName);
         // 解析路径
         $path = MiniHttp::getParam("path", "");
         $path = MiniUtil::specialWordReplace($path);
         $parentPath = dirname($path);
         $user = MUserManager::getInstance()->getCurrentUser();
         $parentFile = MiniFile::getInstance()->getByPath($parentPath);
         //如果目录存在,且该目录is_delete=1,则把目录状态删除状态修改为0
         if (!empty($parentFile) && $parentFile['is_deleted'] == 1) {
             $values = array();
             $values['is_deleted'] = false;
             MiniFile::getInstance()->updateByPath($parentPath, $values);
         } else {
             //如果是根目录,则不用新建目录
             //否则会创建文件名名称的文件夹出来,而且目标文件位于该文件夹的下面
             if (!MiniUtil::isRootPath($parentPath, $user["id"])) {
                 MiniFile::getInstance()->createFolder($parentPath, $user['id']);
             }
         }
         $createFileHandler->size = $size;
         $createFileHandler->parent_path = MUtils::convertStandardPath($parentPath);
         $createFileHandler->file_name = MiniUtil::specialWordReplace($fileName);
         $createFileHandler->root = $root;
         $createFileHandler->path = MUtils::convertStandardPath($path);
         $createFileHandler->type = $type;
         // 文件不存在,保存文件
         $createFileHandler->saveFile($tmpName, $signature, $size);
         // 保存文件meta
         $createFileHandler->saveFileMeta();
         // 处理不同端,不同返回值
         if (MUserManager::getInstance()->isWeb() === true) {
             $createFileHandler->buildWebResponse();
             return;
         }
         $createFileHandler->buildResult();
     }
 }
Exemplo n.º 6
0
 /**
  * 处理返回值
  */
 public function buildResult($query_db_file, $to_path = null)
 {
     // 处理不同端,不同返回值
     if (MUserManager::getInstance()->isWeb() === true) {
         $this->buildWebResponse();
         return;
     }
     if ($this->isEcho === false) {
         return;
     }
     $is_dir = true;
     $size = $query_db_file["file_size"];
     $response = array();
     if ($query_db_file["file_type"] == MConst::OBJECT_TYPE_FILE) {
         // 根据文件名后缀判断mime type
         $mime_type = MiniUtil::getMimeType($query_db_file["file_name"]);
         $is_dir = false;
         $response["mime_type"] = $mime_type;
         $response["thumb_exists"] = MUtils::isExistThumbnail($mime_type, (int) $query_db_file["file_size"]);
     }
     // 去除/{user_id}
     $path = CUtils::removeUserFromPath($query_db_file["file_path"]);
     $response["size"] = MUtils::getSizeByLocale($this->_locale, $size);
     $response["is_deleted"] = false;
     $response["bytes"] = intval($size);
     $response["modified"] = MUtils::formatIntTime($query_db_file["file_update_time"]);
     if ($to_path) {
         $path = $to_path;
     } else {
         $path = $query_db_file["file_path"];
     }
     $path_info = MUtils::pathinfo_utf($path);
     $path_info_out = MUtils::pathinfo_utf($this->to_share_filter->src_path);
     $path = MUtils::convertStandardPath($path_info_out['dirname'] . "/" . $path_info['basename']);
     $response["path"] = $path;
     $response["root"] = $this->_root;
     $response["is_dir"] = $is_dir;
     $response["rev"] = strval($query_db_file["version_id"]);
     $response["revision"] = intval($query_db_file["version_id"]);
     // 增加操作返回事件编码
     $response["event_uuid"] = $query_db_file["event_uuid"];
     echo json_encode($response);
 }
Exemplo n.º 7
0
 /**
  * 控制器执行主逻辑函数, 复制文件或者文件夹
  */
 public function invoke($uri = null)
 {
     $this->setAction(MConst::COPY);
     $this->beforeInvoke();
     $this->beforecheck();
     $user = MUserManager::getInstance()->getCurrentUser();
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 11'), MConst::HTTP_CODE_400);
     }
     // 文件大小格式化参数
     $locale = "bytes";
     if (isset($params["root"]) === false || isset($params["from_path"]) === false || isset($params["to_path"]) === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 12'), MConst::HTTP_CODE_400);
     }
     if (isset($params["locale"])) {
         $locale = $params["locale"];
     }
     $root = $params["root"];
     $this->_from_path = $params["from_path"];
     $this->_to_path = $params["to_path"];
     if ($params['is_root']) {
         $this->_to_path = '/' . $user['id'] . $this->_to_path;
     }
     //
     // 检查文件名是否有效
     //
     $isInvalid = MUtils::checkNameInvalid(MUtils::get_basename($this->_to_path));
     if ($isInvalid) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 13'), MConst::HTTP_CODE_400);
     }
     //
     // 转换路径分隔符,便于以后跨平台,如:将 "\"=>"/"
     //
     $this->_from_path = MUtils::convertStandardPath($this->_from_path);
     $this->_to_path = MUtils::convertStandardPath($this->_to_path);
     if ($this->_from_path == "/" || $this->_to_path == "/" || $this->_from_path === false || $this->_to_path === false) {
         throw new MFileopsException(Yii::t('api', 'Bad Request 14'), MConst::HTTP_CODE_400);
     }
     if ($this->_to_path[strlen($this->_to_path) - 1] == "/") {
         // 目标文件无效,403 error
         throw new Exception(Yii::t('api', 'The file or folder name is invalid'), MConst::HTTP_CODE_403);
     }
     //
     // 检查共享
     //
     $from_share_filter = MSharesFilter::init();
     $this->to_share_filter = MSharesFilter::init();
     // 当从共享目录拷贝到其他目录时,源目录用户id设置为共享用户id
     //        if ($from_share_filter->handlerCheck($this->owner, $this->_from_path)) {
     //            $this->master = $from_share_filter->master;
     //            $this->_from_path = $from_share_filter->_path;
     //        }
     //
     //        // 当拷贝到共享目录的时候,目标目录的用户id设置为共享用户id
     //        if ($this->to_share_filter->handlerCheck($this->_user_id, $this->_to_path)) {
     //            $this->_user_id = $this->to_share_filter->master;
     //            $this->user_nick      = $this->to_share_filter->master_nick;
     //            $this->_to_path = $this->to_share_filter->_path;
     //        }
     //        if($this->_from_shared_path){
     //            $this->_from_path =  $this->_from_shared_path;
     //        }else{
     //            $this->_from_path = "/".$this->master.$this->_from_path;
     //        }
     //        if($this->_to_shared_path){
     //            $this->_to_path =  $this->_to_shared_path;
     //        }else{
     //            $this->_to_path   = "/".$this->_user_id.$this->_to_path;
     //        }
     //
     // 检查目标路径是否在复制目录下
     //
     if (strpos($this->_to_path, $this->_from_path . "/") === 0) {
         throw new MFileopsException(Yii::t('api', 'Can not be copied to the subdirectory'), MConst::HTTP_CODE_403);
     }
     $check = CUtils::removeUserFromPath($this->_to_path);
     if (empty($check) || $check == '/') {
         throw new MFileopsException(Yii::t('api', 'Can not be copied to the error directory'), MConst::HTTP_CODE_403);
     }
     //
     // 检查目标路径文件是否存在
     //
     $queryToPathDbFile = MFiles::queryAllFilesByPath($this->_to_path);
     $isUpdate = false;
     if ($queryToPathDbFile) {
         if ($queryToPathDbFile[0]["is_deleted"] == false) {
             // 已经存在,403 error
             throw new MFileopsException(Yii::t('api', 'There is already a item at the given destination'), MConst::HTTP_CODE_403);
         }
         $isUpdate = true;
     }
     //
     // 查询其信息
     //
     $fileName = MUtils::get_basename($this->_to_path);
     $queryFromPathDbFile = MFiles::queryFilesByPath($this->_from_path);
     $queryToPathDbFile = MFiles::queryFilesByPath(dirname($this->_to_path));
     if ($queryFromPathDbFile === false || empty($queryFromPathDbFile)) {
         throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404);
     }
     $fromArr = explode('/', $this->_from_path);
     $fromId = $fromArr[1];
     if ($params['root']) {
         $toArr = explode('/', $this->_to_path);
         $toId = $toArr[1];
     } else {
         $toId = $user['id'];
     }
     //权限判断
     //当属于共享目录时才进行权限控制(源路径)
     $fromFile = MiniFile::getInstance()->getByFilePath($this->_from_path);
     if ($fromId != $user['id']) {
         //判断文件重命名是否有权限操作
         $permissionArr = UserPermissionBiz::getInstance()->getPermission($this->_from_path, $user['id']);
         if (!isset($permissionArr)) {
             $permission = MConst::SUPREME_PERMISSION;
         } else {
             $permission = $permissionArr['permission'];
         }
         $miniPermission = new MiniPermission($permission);
         $canCopy = $miniPermission->canCopy($fromFile['file_type']);
         if (!$canCopy) {
             throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
         }
     }
     $isSharedPath = false;
     //主要用于判断是否为被共享文件
     //目标路径
     if ($toId != $user['id']) {
         $isSharedPath = true;
         //拷贝到 (目标路径的创建权限)  的判断
         //            if ($query_from_path_db_file[0]["file_type"] == 0){  //文件
         //                $this->to_share_filter->hasPermissionExecute($this->_to_path, MPrivilege::FILE_CREATE);
         //            } else {                                           //文件夹
         //                $this->to_share_filter->hasPermissionExecute($this->_to_path, MPrivilege::FOLDER_CREATE);
         //            }
     } else {
         $model = new GeneralFolderPermissionBiz($this->_to_path);
         if ($model->isParentShared($this->_to_path)) {
             //如果是父目录被共享
             $isSharedPath = true;
         }
     }
     if ($isSharedPath) {
         $permissionArr = UserPermissionBiz::getInstance()->getPermission(dirname($this->_to_path), $user['id']);
         if (!isset($permissionArr)) {
             $permission = MConst::SUPREME_PERMISSION;
         } else {
             $permission = $permissionArr['permission'];
             $privilegeModel = new PrivilegeBiz();
             $this->to_share_filter->slaves = $privilegeModel->getSlaveIdsByPath($permissionArr['share_root_path']);
             $this->to_share_filter->is_shared = true;
         }
         $miniPermission = new MiniPermission($permission);
         $toFile = MiniFile::getInstance()->getByFilePath(dirname($this->_to_path));
         $canCopy = $miniPermission->canCopy($toFile['file_type']);
         if (!$canCopy) {
             throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
         }
     }
     //
     // 查询目标路径父目录信息
     //
     $parentPath = dirname($this->_to_path);
     $createFolder = new MCreateFolderController();
     $createFolder->_user_device_id = $this->_user_device_id;
     $createFolder->_user_id = $this->_user_id;
     $createFolder->share_filter = $this->to_share_filter;
     $parentFileId = $createFolder->handlerParentFolder($parentPath);
     //
     // 组装对象信息
     //
     $fileDetail = new MFiles();
     $fileDetail->file_name = $fileName;
     $fileDetail->file_path = $this->_to_path;
     $this->assembleFileDetail($fileName, $parentFileId, $fileDetail, $queryFromPathDbFile[0]);
     //
     // 首先处理复制根目录操作
     //
     if ($isUpdate) {
         $fileDetail->event_uuid = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID);
         $updates = array();
         $updates["file_update_time"] = time();
         $updates["is_deleted"] = intval(false);
         $updates["event_uuid"] = $fileDetail->event_uuid;
         $updates["file_type"] = $fileDetail->file_type;
         $retValue = MFiles::updateFileDetailByPath($this->_to_path, $updates);
     } else {
         $retValue = MFiles::CreateFileDetail($fileDetail, $this->_user_id);
     }
     if ($retValue === false) {
         throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
     }
     //
     // 更新版本信息
     //
     $this->updateVerRef(array($fileDetail));
     $retValue = MiniEvent::getInstance()->createEvent($this->_user_id, $this->_user_device_id, $fileDetail->event_action, $fileDetail->file_path, $fileDetail->context, $fileDetail->event_uuid, $this->to_share_filter->type);
     if ($retValue === false) {
         throw new MFileopsException(Yii::t('api', 'There is already a item at the given destination'), MConst::HTTP_CODE_500);
     }
     $context = $fileDetail->context;
     if ($fileDetail->file_type == 0) {
         $context = unserialize($context);
     }
     $this->to_share_filter->handlerAction($fileDetail->event_action, $this->_user_device_id, $fileDetail->file_path, $context);
     //
     // 判断操作的是文件夹,还是文件
     //
     $createArray = array();
     $queryDbFile = MFiles::queryFilesByPath($this->_to_path);
     //
     // 查询其复制目录路径id
     //
     if ($queryDbFile === false || empty($queryDbFile)) {
         throw new MFileopsException(Yii::t('api', 'Not found the source files of the specified path'), MConst::HTTP_CODE_404);
     }
     if ($fileDetail->file_type != MConst::OBJECT_TYPE_FILE) {
         $fileDetail->id = $queryDbFile[0]["id"];
         $fileDetail->file_size = $queryDbFile[0]["file_size"];
         $this->handlerChildrenFile($fileDetail);
         //
         // 处理版本信息
         //
         $moveController = new MMoveController();
         $moveController->versions = array();
         $createArray = $moveController->handleChildrenVersions($createArray, $this->_user_id, $this->user_nick, $this->_from_path, $this->_to_path, $queryToPathDbFile[0]["id"], $this->_user_device_name, $queryFromPathDbFile[0]["file_size"]);
         $this->versions = $moveController->versions;
     } else {
         $fileMeta = new MFileMetas();
         $fileMeta->version_id = $queryFromPathDbFile[0]["version_id"];
         //
         // 查询其版本
         //
         $fileVersion = MFileMetas::queryFileMeta($this->_to_path, MConst::VERSION);
         $fileMeta->is_add = false;
         if ($fileVersion) {
             $metaValue = MUtils::getFileVersions($this->_user_device_name, $fileDetail->file_size, $fileMeta->version_id, MConst::CREATE_FILE, $this->_user_id, $this->user_nick, $fileVersion[0]["meta_value"]);
         } else {
             $metaValue = MUtils::getFileVersions($this->_user_device_name, $fileDetail->file_size, $fileMeta->version_id, MConst::CREATE_FILE, $this->_user_id, $this->user_nick);
             $fileMeta->is_add = true;
             // 不存在记录,需要添加
         }
         $fileMeta->meta_value = $metaValue;
         $fileMeta->file_path = $this->_to_path;
         $createArray[$queryFromPathDbFile[0]["file_path"]] = $fileMeta;
         //
         // 添加到需要更新的版本ref
         //
         array_push($this->versions, $fileMeta->version_id);
     }
     //
     // 创建版本信息
     //
     MFileMetas::batchCreateFileMetas($createArray, MConst::VERSION);
     //        if ($ret === false)
     //        {
     //            throw new MFileopsException(
     //                                        Yii::t('api','Internal Server Error'),
     //                                        MConst::HTTP_CODE_500);
     //        }
     //
     // 更新版本
     //
     foreach ($createArray as $key => $fileMeta) {
         if ($fileMeta->is_add === true) {
             // 不存在记录,不需要更新
             continue;
         }
         MFileMetas::updateFileMeta($fileMeta->file_path, MConst::VERSION, $fileMeta->meta_value);
     }
     //
     // 处理不同端,不同返回值
     //
     if (MUserManager::getInstance()->isWeb() === true) {
         $this->buildWebResponse();
         return;
     }
     $response = array();
     $isDir = true;
     if ($queryDbFile[0]["file_type"] == MConst::OBJECT_TYPE_FILE) {
         // TODO
         $mimeType = "text/plain";
         $response["mime_type"] = $mimeType;
         $isDir = false;
         $response["thumb_exists"] = MUtils::isExistThumbnail($mimeType, (int) $queryDbFile[0]["file_size"]);
     }
     $size = $queryDbFile[0]["file_size"];
     $response["size"] = MUtils::getSizeByLocale($locale, $size);
     $response["bytes"] = intval($size);
     $pathInfo = MUtils::pathinfo_utf($this->_to_path);
     $pathInfoOut = MUtils::pathinfo_utf($this->to_share_filter->src_path);
     $path = MUtils::convertStandardPath($pathInfoOut['dirname'] . "/" . $pathInfo['basename']);
     $response["path"] = $path;
     $response["root"] = $root;
     $response["is_dir"] = $isDir;
     $response["rev"] = strval($queryDbFile[0]["version_id"]);
     $response["revision"] = intval($queryDbFile[0]["version_id"]);
     $response["modified"] = MUtils::formatIntTime($queryDbFile[0]["file_update_time"]);
     //
     // 如果标记为不输出结果的话,直接返回$response
     //
     if (!$this->isOutput) {
         return $response;
     }
     echo json_encode($response);
 }
Exemplo n.º 8
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);
 }
Exemplo n.º 9
0
 /**
  * 控制器执行主逻辑函数
  *
  * @return mixed $value 返回最终需要执行完的结果
  */
 public function invoke($uri = null)
 {
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false || $params == null) {
         throw new Exception(Yii::t('api', 'Invalid parameters'));
     }
     // 文件大小格式化参数
     $locale = "bytes";
     if (isset($params["locale"])) {
         $locale = $params["locale"];
     }
     $url = $uri;
     $url_manager = new MUrlManager();
     $this->_root = $url_manager->parseRootFromUrl($uri);
     $path = $url_manager->parsePathFromUrl($uri);
     $path = MUtils::convertStandardPath($path);
     $originalPath = $path;
     // 检查共享
     $share_filter = MSharesFilter::init();
     //        $share_filter
     //
     // 获取用户数据,如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"];
     $rev = $params["rev"];
     $rev = intval($rev);
     $path = "/{$this->_user_id}{$path}";
     //
     // 该文件是否具有此版本
     //
     $file_meta = MFileMetas::queryFileMeta($path, MConst::VERSION);
     if ($file_meta == false || empty($file_meta)) {
         throw new MFileopsException(Yii::t('api', '    Unable to find the revision at that path'), MConst::HTTP_CODE_404);
     }
     if (MUtils::isExistReversion($rev, $file_meta[0]["meta_value"]) == false) {
         throw new MFileopsException(Yii::t('api', '    Unable to find the revision at that path'), MConst::HTTP_CODE_404);
     }
     //
     // 查询版本信息
     //
     $version = MiniVersion::getInstance()->getVersion($rev);
     if ($version == null) {
         throw new MFileopsException(Yii::t('api', '    Unable to find the revision at that path'), MConst::HTTP_CODE_404);
     }
     $size = $version["file_size"];
     $file_hash = $version["file_signature"];
     //
     // 查询文件信息
     //
     $query_db_file = MFiles::queryFilesByPath($path);
     if ($query_db_file === false || empty($query_db_file)) {
         throw new MFileopsException(Yii::t('api', 'not existed'), MConst::HTTP_CODE_404);
     }
     if ($query_db_file[0]["file_type"] == MConst::OBJECT_TYPE_DIRECTORY) {
         // 文件夹不需要版本
         throw new MFileopsException(Yii::t('api', 'folder not existed version'), MConst::HTTP_CODE_403);
     }
     if ($rev !== $query_db_file[0]["version_id"]) {
         //
         // 更新文件版本
         //
         $updates = array();
         $updates["version_id"] = $rev;
         $updates["file_update_time"] = time();
         $updates["file_size"] = $size;
         $updates["event_uuid"] = MiniUtil::getEventRandomString(MConst::LEN_EVENT_UUID);
         $ret = MFiles::updateFileDetailById($query_db_file[0]["id"], $updates);
         if ($ret === false) {
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
         }
         $file_detail = new MFiles();
         $file_detail->file_name = $query_db_file[0]["file_name"];
         //
         // 保存事件
         //
         $context = array("hash" => $file_hash, "rev" => (int) $rev, "bytes" => (int) $size);
         //
         // 增加修改事件
         //
         $ret = MiniEvent::getInstance()->createEvent($this->_user_id, $user_device_id, MConst::MODIFY_FILE, $path, serialize($context), $updates["event_uuid"]);
         if ($ret === false) {
             throw new MFileopsException(Yii::t('api', 'Internal Server Error'), MConst::HTTP_CODE_500);
         }
         $this->handleFileMeta($path, $rev, $user_nick, $this->_user_device_name, $query_db_file[0]["file_size"]);
     }
     // TODO
     $mime_type = $version["mime_type"];
     $response = array();
     $response["size"] = MUtils::getSizeByLocale($locale, $size);
     $response["is_deleted"] = true;
     $response["bytes"] = intval($size);
     $response["thumb_exists"] = MUtils::isExistThumbnail($mime_type, $size);
     $response["path"] = $originalPath;
     $response["root"] = $this->_root;
     $response["is_dir"] = false;
     $response["mime_type"] = $mime_type;
     $response["modified"] = MUtils::formatIntTime(time());
     $response["rev"] = strval($rev);
     $response["revision"] = $rev;
     echo json_encode($response);
 }
Exemplo n.º 10
0
 /**
  * 控制器执行主逻辑函数
  */
 public function invoke($uri = null)
 {
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false || $params == null) {
         throw new MAuthorizationException(Yii::t('api', 'Invalid parameters'));
     }
     $url_manager = new MUrlManager();
     $path = MUtils::convertStandardPath($url_manager->parsePathFromUrl($uri));
     $root = $url_manager->parseRootFromUrl($uri);
     // 去掉问号后面参数
     if ($pos = strpos($this->_root, '?')) {
         $this->_root = substr($this->_root, 0, $pos);
     }
     if ($pos = strpos($path, '?')) {
         $path = substr($path, 0, $pos);
     }
     // 获取用户数据,如user_id
     $user = MUserManager::getInstance()->getCurrentUser();
     $this->_user_id = $user["user_id"];
     $query = "";
     if (isset($params["query"])) {
         $query = $params["query"];
     }
     if ($query === "") {
         throw new MFileopsException(Yii::t('api', 'bad request 15'), MConst::HTTP_CODE_400);
     }
     $file_limit = 10000;
     if (isset($params["file_limit"])) {
         $file_limit = $params["file_limit"];
     }
     $include_deleted = false;
     // 处理删除的同样也需要返回
     //        if(isset($params["include_deleted"])) {
     //            $include_deleted = $params["include_deleted"];
     //            if(is_string($include_deleted) === true) {
     //                if(strtolower($include_deleted) === "false") {
     //                    $include_deleted = false;
     //                } elseif(strtolower($include_deleted) === "true") {
     //                    $include_deleted = true;
     //                }
     //            }
     //        }
     $this->_locale = "bytes";
     if (isset($params["locale"])) {
         $this->_locale = $params["locale"];
     }
     $callback = null;
     if (isset($params["callback"])) {
         $callback = $params["callback"];
     }
     if (empty($path) || "/" . $this->_user_id == $path) {
         $path = '/' . $this->_user_id;
         $userFiles = array();
         $query_db_file = MFiles::searchFilesByPath($path, $query, $this->_user_id, $include_deleted);
         foreach ($query_db_file as $db_file) {
             // if(($db_file['parent_file_id'] == 0) and ($db_file['file_type'] != 4) and ($db_file['file_type'] != 2)){
             $userFiles[] = $db_file;
             // }
         }
         $retval = $this->handleSearchRoot($path, $query);
         $files = array_merge($retval, $userFiles);
     } else {
         //            $files = array();
         $includeDeleted = false;
         $currentFile = MiniFile::getInstance()->getByPath($path);
         //            if (empty($currentFile)){
         //                throw new MFileopsException(Yii::t('api','not existed'),MConst::HTTP_CODE_404);
         //            }
         //            //查询文件类型
         //            $version = MiniVersion::getInstance()->getVersion($currentFile["version_id"]);
         //            $mimeType = null;
         //            if ($version != NULL)
         //            {
         //                $currentFile["signature"] = $version["file_signature"];
         //                $mimeType = $version["mime_type"];
         //            }
         //            // 组装子文件数据
         $childrenFiles = MiniFile::getInstance()->getChildrenByFileID($parentFileId = $currentFile['id'], $includeDeleted);
         $currentFileParts = explode('/', $path);
         $currentFileUserId = $currentFileParts[1];
         $files = $childrenFiles;
         //            $query = str_replace("%", "\\%", $query);
         //            $sql = ' file_name like "%' . $query . '%"';
         //            $sql = '';
         //            $files = array();
         //            foreach($childrenFiles as $childrenFile){
         //                $condition = $sql . 'file_path="' . $childrenFile['file_path'] . '" ';
         //                $file = MFiles::findAll($condition);
         //                $files = array_merge($files,$file);
         //            }
         //            $contents = array();
         //            if(!empty($childrenFiles)){
         //                foreach($childrenFiles as $childrenFile){
         //                    $content = array();
         //                    $version = MiniVersion::getInstance()->getVersion($childrenFile["version_id"]);
         //                    $mimeType = null;
         //                    if ($version != NULL){
         //                        $mimeType = $version["mime_type"];
         //                        $childrenFile["signature"] = $version["file_signature"];
         //                    }
         //                    $content = $this->assembleResponse($content, $childrenFile, $mimeType);
         //                    if(!empty($content) && $childrenFile['is_deleted'] == 0){
         //                        array_push($contents, $content);
         //                    }
         //                }
         //            }
         //            $response['contents'] = $contents;
     }
     $result = array();
     $query = str_replace("%", "\\%", $query);
     $sql = ' file_name like "%' . $query . '%"';
     foreach ($files as $file) {
         $condition = $sql . 'and file_path like"' . $file['file_path'] . '%" ';
         $file = MFiles::findAll($condition);
         if (empty($file)) {
             continue;
         }
         $result = array_merge($result, $file);
     }
     //        $path        = "/{$this->_user_id}{$path}";
     //        $path        = MUtils::convertStandardPath($path) . "/";
     // 查询其 信息
     //        $operator = $this->_user_id;
     //
     //        $sharefilter = MSharesFilter::init();
     //        $sharefilter->handlerCheck($this->_user_id, CUtils::removeUserFromPath($path));
     //        if($sharefilter->is_shared) {
     //            $operator = $sharefilter->master;
     //            $qpath = '/' . $sharefilter->master . $sharefilter->_path;
     //            $query_db_file = MFiles::searchFilesByPath($qpath, $query, $sharefilter->master, $include_deleted);
     //
     //            // 判断搜索出来的文件是否有权限访问
     //            foreach($query_db_file as $index => $file) {
     //                // 列表权限,如果没有列表权限,则不进行显示
     //                try {
     //                    $sharefilter->hasPermissionExecute($file['file_path'], MPrivilege::RESOURCE_READ);
     //                }catch(Exception $e) {
     //                    unset($query_db_file[$index]);
     //                    continue;
     //                }
     //            }
     //        } else {
     //            $query_db_file = MFiles::searchFilesByPath($path, $query, $this->_user_id, $include_deleted);
     //        }
     //
     // 查询根目录
     //
     //        $retval = $this->handleSearchRoot($path, $query);
     //        $query_db_file = array_merge($query_db_file, $retval);
     // if (count($query_db_file) > $file_limit)
     // {
     // throw new MFileopsException(
     // Yii::t('api','Too many file entries to return'),
     // MConst::HTTP_CODE_406);
     // }
     //        $keys = array();
     $response = array();
     $filePaths = array();
     if (!empty($result)) {
         foreach ($result as $file) {
             if (in_array($file['file_path'], $filePaths)) {
                 continue;
             }
             array_push($filePaths, $file['file_path']);
             $item = array();
             $version = MiniVersion::getInstance()->getVersion($file["version_id"]);
             $mimeType = null;
             $signature = null;
             if ($version != NULL) {
                 $mimeType = $version["mime_type"];
                 $signature = $version["file_signature"];
                 $file["signature"] = $signature;
             }
             $item = $this->assembleResponse($item, $file, $mimeType);
             if (!empty($item)) {
                 array_push($response, $item);
             }
         }
     }
     //        foreach($query_db_file as $key => $db_file) {
     //            if ($key >= $file_limit)
     //                break;
     //            $file_array = array();
     //            $mime_type = null;
     //            if($db_file["file_type"] == MConst::OBJECT_TYPE_FILE) {
     //                $version = MiniVersion::getInstance()->getVersion($db_file["version_id"]);
     //                if($version) {
     //                    $mime_type = $version["mime_type"];
     //                }
     //            }
     //            $file_array = $this->assembleResponse($file_array, $db_file, $mime_type);
     //            #这里对数据进行了二次过滤,如果路径是一致的,则过滤掉
     //            #TODO 这里的冗余数据初步分析是权限导致的,二次重构要去掉这个代码
     //            $path = $file_array["path"];
     //            if(!array_key_exists($path, $keys)){
     //                $keys[$path] = $file_array;
     //                array_push($response, $file_array);
     //            }
     //        }
     echo json_encode($response);
 }
Exemplo n.º 11
0
 /**
  * (non-PHPdoc)
  * @see MIController::invoke()
  */
 public function invoke($uri = NULL)
 {
     $this->setAction(MConst::CREATE_FILE);
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $hash = @$_REQUEST['hash'];
     if (empty($hash)) {
         $hash = MiniHttp::getParam("signature", "");
     }
     // 接收参数
     if (empty($hash)) {
         throw new MFilesException(Yii::t('api', MConst::PARAMS_ERROR . "Missing parameter 'hash'."), MConst::HTTP_CODE_400);
     }
     // 解析文件路径,若返回false,则错误处理
     $urlManager = new MUrlManager();
     $path = $urlManager->parsePathFromUrl($uri);
     $root = $urlManager->parseRootFromUrl($uri);
     if ($path == false || $root == false) {
         //支持参数模式传递上传路径
         $path = MiniHttp::getParam("path", "");
         $root = "miniyun";
         $this->isNewVersion = true;
         if (empty($path)) {
             throw new MFilesException(Yii::t('api', MConst::PATH_ERROR), MConst::HTTP_CODE_411);
         }
     }
     $path = MiniUtil::specialWordReplace($path);
     $pathInfo = MUtils::pathinfo_utf($path);
     $fileName = $pathInfo["basename"];
     $parentPath = $pathInfo["dirname"];
     // 检查是否在共享文件夹内, 如果在共享文件夹内,则进行权限检查
     $user = MUserManager::getInstance()->getCurrentUser();
     $userId = $user["user_id"];
     $shareFilter = MSharesFilter::init();
     if ($shareFilter->handlerCheck($userId, $path)) {
         $userId = $shareFilter->master;
         $path = $shareFilter->_path;
         $filePath = "/" . $userId . $path;
         $shareFilter->hasPermissionExecute($filePath, MPrivilege::FILE_CREATE);
     }
     // 检查版本是否存在
     if ($this->handleCheckFileVersion($hash, $fileName) == FALSE) {
         return;
     }
     $this->handler = MFilesCommon::initMFilesCommon();
     $this->handler->parent_path = MUtils::convertStandardPath($parentPath);
     $this->handler->file_name = $fileName;
     $this->handler->root = $root;
     $this->handler->path = MUtils::convertStandardPath($path);
     $this->handler->type = MiniUtil::getMimeType($fileName);
     $this->handler->size = $this->size;
     $this->handler->file_hash = $hash;
     $this->handler->version_id = $this->version_id;
     // 保存文件meta
     $this->handler->saveFileMeta();
     if (MUserManager::getInstance()->isWeb() === true) {
         $this->handler->buildWebResponse();
         return;
     }
     $this->handler->buildResult();
 }
Exemplo n.º 12
0
 /**
  * 当参数overwrite=false时,执行文件重命名,再创建文件
  */
 private function renameFile()
 {
     // overwrite=true,不执行之后操作
     if ($this->overwrite == true) {
         return;
     }
     $children = MFiles::queryChildrenByParentId($this->user_id, $this->parent_file_id);
     if ($children === false) {
         throw new MFilesException(Yii::t('api', MConst::INTERNAL_SERVER_ERROR), MConst::HTTP_CODE_500);
     }
     $names = array();
     foreach ($children as $k => $v) {
         $names[strtolower($v["file_name"])] = $v["file_name"];
     }
     //
     // 执行重命名文件操作
     //
     $this->file_name = MUtils::getConflictName($this->file_name, $names);
     $this->path = MUtils::convertStandardPath($this->parent_path . "/" . $this->file_name);
     $this->file_path = "/" . $this->user_id . $this->path;
     $this->create_file = true;
 }
Exemplo n.º 13
0
 /**
  * 控制器执行主逻辑函数
  *
  */
 public function invoke($uri = null)
 {
     $this->setAction(MConst::CREATE_DIRECTORY);
     // 调用父类初始化函数,注册自定义的异常和错误处理逻辑
     parent::init();
     $params = $_REQUEST;
     // 检查参数
     if (isset($params) === false || $params == null) {
         throw new MFileopsException(Yii::t('api', 'Bad Request'), MConst::HTTP_CODE_400);
     }
     // 获取用户数据,如user_id
     $user = MUserManager::getInstance()->getCurrentUser();
     $device = MUserManager::getInstance()->getCurrentDevice();
     $this->_user_id = $user["user_id"];
     $this->_user_device_id = $device["device_id"];
     // 文件大小格式化参数
     $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'), MConst::HTTP_CODE_400);
     }
     $root = $params["root"];
     // dataserver 增加创建返回path,用于导航
     // by Kindac
     // since 2013/06/25
     $path = $params["path"];
     $fileName = MUtils::get_basename($path);
     if ($fileName === false) {
         throw new MFileopsException(Yii::t('api', 'The folder name is invalid'), MConst::HTTP_CODE_400);
     }
     // 检查文件名是否有效
     $isInvalid = MUtils::checkNameInvalid($fileName);
     if ($isInvalid) {
         throw new MFileopsException(Yii::t('api', 'The folder name is invalid'), MConst::HTTP_CODE_400);
     }
     // 转换路径分隔符,便于以后跨平台,如:将 "\"=>"/"
     $path = MUtils::convertStandardPath($path);
     if ($path == false) {
         throw new MFileopsException(Yii::t('api', 'The folder name is invalid'), MConst::HTTP_CODE_400);
     }
     // 检查是否在共享目录
     $this->share_filter = MSharesFilter::init();
     if ($this->share_filter->handlerCheck($this->_user_id, $path, MConst::CREATE_DIRECTORY)) {
         $this->_user_id = $this->share_filter->master;
         $path = $this->share_filter->_path;
     }
     if ($params['is_root'] == "/") {
         $path = "/" . $this->_user_id . $path;
     }
     $item = explode("/", $path);
     if (!preg_match("/^[0-9]+\$/", $item[1])) {
         $path = "/" . $user["user_id"] . $path;
     }
     $parentPath = dirname($path);
     $isSharedPath = false;
     //主要用于判断是否为被共享文件
     if (dirname(MiniUtil::getRelativePath($path)) == "/" . $this->_user_id) {
         $permission = MConst::SUPREME_PERMISSION;
     } else {
         $pathArr = explode('/', $path);
         $masterId = $pathArr[1];
         if ($masterId != $this->_user_id) {
             $isSharedPath = true;
         } else {
             $model = new GeneralFolderPermissionBiz($parentPath);
             if ($model->isParentShared($parentPath)) {
                 //如果是父目录被共享
                 $isSharedPath = true;
             }
         }
         if ($isSharedPath) {
             $permissionArr = UserPermissionBiz::getInstance()->getPermission($parentPath, $this->_user_id);
             if (!isset($permissionArr)) {
                 $permission = MConst::SUPREME_PERMISSION;
             } else {
                 $permission = $permissionArr['permission'];
                 $privilegeModel = new PrivilegeBiz();
                 $this->share_filter->slaves = $privilegeModel->getSlaveIdsByPath($permissionArr['share_root_path']);
                 $this->share_filter->is_shared = true;
             }
         } else {
             $permission = MConst::SUPREME_PERMISSION;
         }
     }
     $miniPermission = new MiniPermission($permission);
     $canCreateFolder = $miniPermission->canCreateFolder();
     if (!$canCreateFolder) {
         throw new MFileopsException(Yii::t('api', 'no permission'), MConst::HTTP_CODE_409);
     }
     // 查询其是否存在 信息
     $file = MiniFile::getInstance()->getByPath($path);
     // 是否存在相同文件路径, 且被删除的记录
     $hadFileDelete = false;
     if (isset($file)) {
         if ($file["is_deleted"] == false) {
             $code = $file["file_type"] == MConst::OBJECT_TYPE_FILE ? MConst::HTTP_CODE_402 : MConst::HTTP_CODE_403;
             if (MUserManager::getInstance()->isWeb() === true) {
                 throw new MFileopsException(Yii::t('api', 'There is already a item at the given destination'), $code);
             }
             $uuid = $file["event_uuid"];
             // 已经存在,403 error
             throw new MFileopsException($code);
         }
         $hadFileDelete = true;
     }
     $this->_parentFilePath = "/{$this->_user_id}";
     // 检查父目录
     $parentFileId = $this->handlerParentFolder($parentPath);
     $fileDetail = $this->createFile($path, $parentFileId, $hadFileDelete);
     // 处理不同端,不同返回值
     if (MUserManager::getInstance()->isWeb() === true) {
         if ($this->isOutput) {
             $this->buildWebResponse($fileName, $path);
         }
         return;
     }
     $response = array();
     $response["size"] = "0";
     $response["thumb_exists"] = false;
     $response["bytes"] = 0;
     $response["modified"] = MUtils::formatIntTime($fileDetail["file_update_time"]);
     $path = CUtils::removeUserFromPath("{$this->_parentFilePath}/{$fileName}");
     if ($this->share_filter->is_shared) {
         $path = $this->share_filter->src_path;
         $path_info = MUtils::pathinfo_utf($path);
         $path = MUtils::convertStandardPath($path_info['dirname'] . "/" . $fileName);
     }
     $response["path"] = $this->_parentFilePath . "/" . $fileName;
     $response["is_dir"] = true;
     $response["icon"] = "folder";
     $response["root"] = $root;
     $response["revision"] = 0;
     // 版本
     // 增加返回事件uuid,便于客户端进行事件对比逻辑
     $response["event_uuid"] = $fileDetail["event_uuid"];
     echo json_encode($response);
 }