Пример #1
0
 /**
  * create a new share
  * @param array $params
  * @return \OC_OCS_Result
  */
 public static function createShare($params)
 {
     $path = isset($_POST['path']) ? $_POST['path'] : null;
     if ($path === null) {
         return new \OC_OCS_Result(null, 400, "please specify a file or folder path");
     }
     $itemSource = self::getFileId($path);
     $itemType = self::getItemType($path);
     if ($itemSource === null) {
         return new \OC_OCS_Result(null, 404, "wrong path, file/folder doesn't exist.");
     }
     $shareWith = isset($_POST['shareWith']) ? $_POST['shareWith'] : null;
     $shareType = isset($_POST['shareType']) ? (int) $_POST['shareType'] : null;
     switch ($shareType) {
         case \OCP\Share::SHARE_TYPE_USER:
             $permissions = isset($_POST['permissions']) ? (int) $_POST['permissions'] : 31;
             break;
         case \OCP\Share::SHARE_TYPE_GROUP:
             $permissions = isset($_POST['permissions']) ? (int) $_POST['permissions'] : 31;
             break;
         case \OCP\Share::SHARE_TYPE_LINK:
             //allow password protection
             $shareWith = isset($_POST['password']) ? $_POST['password'] : null;
             //check public link share
             $publicUploadEnabled = \OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_public_upload', 'yes');
             if (isset($_POST['publicUpload']) && $publicUploadEnabled !== 'yes') {
                 return new \OC_OCS_Result(null, 403, "public upload disabled by the administrator");
             }
             $publicUpload = isset($_POST['publicUpload']) ? $_POST['publicUpload'] : 'false';
             // read, create, update (7) if public upload is enabled or
             // read (1) if public upload is disabled
             $permissions = $publicUpload === 'true' ? 7 : 1;
             break;
         default:
             return new \OC_OCS_Result(null, 400, "unknown share type");
     }
     try {
         $token = \OCP\Share::shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions);
     } catch (\Exception $e) {
         return new \OC_OCS_Result(null, 403, $e->getMessage());
     }
     if ($token) {
         $data = array();
         $data['id'] = 'unknown';
         $shares = \OCP\Share::getItemShared($itemType, $itemSource);
         if (is_string($token)) {
             //public link share
             foreach ($shares as $share) {
                 if ($share['token'] === $token) {
                     $data['id'] = $share['id'];
                     break;
                 }
             }
             $url = \OCP\Util::linkToPublic('files&t=' . $token);
             $data['url'] = $url;
             // '&' gets encoded to $amp;
             $data['token'] = $token;
         } else {
             foreach ($shares as $share) {
                 if ($share['share_with'] === $shareWith && $share['share_type'] === $shareType) {
                     $data['id'] = $share['id'];
                     break;
                 }
             }
         }
         return new \OC_OCS_Result($data);
     } else {
         return new \OC_OCS_Result(null, 404, "couldn't share file");
     }
 }