| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Freyo\Flysystem\QcloudCOSv4\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | date_default_timezone_set('PRC'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class Cosapi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     //计算sign签名的时间参数 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     const EXPIRED_SECONDS = 180; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     //1M | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const SLICE_SIZE_1M = 1048576; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     //20M 大于20M的文件需要进行分片传输 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const MAX_UNSLICE_FILE_SIZE = 20971520; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     //失败尝试次数 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const MAX_RETRY_TIMES = 3; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     //HTTP请求超时时间 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private static $timeout = 60; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private static $region = 'gz'; // default region is guangzou | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * 设置HTTP请求超时时间. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param int $timeout 超时时长 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public static function setTimeout($timeout = 60) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         if (!is_int($timeout) || $timeout < 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         self::$timeout = $timeout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public static function setRegion($region) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         self::$region = $region; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * 上传文件,自动判断文件大小,如果小于20M则使用普通文件上传,大于20M则使用分片上传. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $bucket     bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param string $srcPath    本地文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $dstPath    上传的文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $bizAttr    文件属性 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string $slicesize  分片大小(512k,1m,2m,3m),默认:1m | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param string $insertOnly 同名文件是否覆盖 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return [type] [description] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 4 |  |     public static function upload( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $bucket, $srcPath, $dstPath, $bizAttr = null, $sliceSize = null, $insertOnly = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 58 | 4 | View Code Duplication |         if (!file_exists($srcPath)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 'message' => 'file '.$srcPath.' not exists', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 'data'    => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 4 |  |         $dstPath = self::normalizerPath($dstPath, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         //文件大于20M则使用分片传输 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |         if (filesize($srcPath) < self::MAX_UNSLICE_FILE_SIZE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |             return self::uploadFile($bucket, $srcPath, $dstPath, $bizAttr, $insertOnly); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $sliceSize = self::getSliceSize($sliceSize); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return self::uploadBySlicing($bucket, $srcPath, $dstPath, $bizAttr, $sliceSize, $insertOnly); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * 创建目录 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param  string  $bucket bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param  string  $folder       目录路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param  string  $bizAttr    目录属性 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |     public static function createFolder($bucket, $folder, $bizAttr = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |         if (!self::isValidPath($folder)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 'message' => 'folder '.$folder.' is not a valid folder name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'data'    => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |         $folder = self::normalizerPath($folder, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |         $folder = self::cosUrlEncode($folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |         $expired = time() + self::EXPIRED_SECONDS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 2 |  |         $url = self::generateResUrl($bucket, $folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         $signature = Auth::createReusableSignature($expired, $bucket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |             'op'       => 'create', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |             'biz_attr' => (isset($bizAttr) ? $bizAttr : ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 2 |  |         $data = json_encode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $req = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |             'url'     => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |             'method'  => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |             'timeout' => self::$timeout, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |             'data'    => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             'header'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |                 'Authorization: '.$signature, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |                 'Content-Type: application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 2 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * 目录列表 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param  string  $bucket bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param  string  $path     目录路径,sdk会补齐末尾的 '/' | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param  int     $num      拉取的总数 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @param  string  $pattern  eListBoth,ListDirOnly,eListFileOnly  默认both | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param  int     $order    默认正序(=0), 填1为反序, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param  string  $offset   透传字段,用于翻页,前端不需理解,需要往前/往后翻页则透传回来 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 130 | 1 | View Code Duplication |     public static function listFolder( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $bucket, $folder, $num = 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $pattern = 'eListBoth', $order = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $context = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |         $folder = self::normalizerPath($folder, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |         return self::listBase($bucket, $folder, $num, $pattern, $order, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * 目录列表(前缀搜索) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @param  string  $bucket bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param  string  $prefix   列出含此前缀的所有文件 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param  int     $num      拉取的总数 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param  string  $pattern  eListBoth(默认),ListDirOnly,eListFileOnly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @param  int     $order    默认正序(=0), 填1为反序, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param  string  $offset   透传字段,用于翻页,前端不需理解,需要往前/往后翻页则透传回来 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 149 |  | View Code Duplication |     public static function prefixSearch( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $bucket, $prefix, $num = 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $pattern = 'eListBoth', $order = 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $context = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $path = self::normalizerPath($prefix); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         return self::listBase($bucket, $prefix, $num, $pattern, $order, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * 目录更新 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param  string  $bucket bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param  string  $folder      文件夹路径,SDK会补齐末尾的 '/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param  string  $bizAttr   目录属性 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public static function updateFolder($bucket, $folder, $bizAttr = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $folder = self::normalizerPath($folder, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         return self::updateBase($bucket, $folder, $bizAttr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |       * 查询目录信息 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |       * @param  string  $bucket bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |       * @param  string  $folder       目录路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |       */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     public static function statFolder($bucket, $folder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         $folder = self::normalizerPath($folder, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         return self::statBase($bucket, $folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      * 删除目录 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |      * @param  string  $bucket bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |      * @param  string  $folder       目录路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |      *  注意不能删除bucket下根目录/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 190 | 2 | View Code Duplication |     public static function delFolder($bucket, $folder) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 2 |  |         if (empty($bucket) || empty($folder)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                 'message' => 'bucket or path is empty', ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 2 |  |         $folder = self::normalizerPath($folder, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 2 |  |         return self::delBase($bucket, $folder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * 更新文件 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      * @param  string  $bucket  bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * @param  string  $path        文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      * @param  string  $authority:  eInvalid(继承Bucket的读写权限)/eWRPrivate(私有读写)/eWPrivateRPublic(公有读私有写) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      * @param  array   $customer_headers_array 携带的用户自定义头域,包括 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |      * 'Cache-Control' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |      * 'Content-Type' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |      * 'Content-Disposition' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |      * 'Content-Language' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |      * 'x-cos-meta-自定义内容' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 | 4 |  |     public static function update($bucket, $path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |                                   $bizAttr = null, $authority = null, $customer_headers_array = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 4 |  |         $path = self::normalizerPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 4 |  |         return self::updateBase($bucket, $path, $bizAttr, $authority, $customer_headers_array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      * 查询文件信息 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      * @param  string  $bucket  bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |      * @param  string  $path        文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 7 |  |     public static function stat($bucket, $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 | 7 |  |         $path = self::normalizerPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 | 7 |  |         return self::statBase($bucket, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * 删除文件 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @param  string  $bucket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      * @param  string  $path      文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 240 | 2 | View Code Duplication |     public static function delFile($bucket, $path) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 | 2 |  |         if (empty($bucket) || empty($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |                 'message' => 'path is empty', ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 | 2 |  |         $path = self::normalizerPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 | 2 |  |         return self::delBase($bucket, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |      * 内部方法, 上传文件. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |      * @param string $bucket     bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |      * @param string $srcPath    本地文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |      * @param string $dstPath    上传的文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |      * @param string $bizAttr    文件属性 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |      * @param int    $insertOnly 是否覆盖同名文件:0 覆盖,1:不覆盖 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |      * @return [type] [description] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 | 4 |  |     private static function uploadFile($bucket, $srcPath, $dstPath, $bizAttr = null, $insertOnly = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 | 4 |  |         $srcPath = realpath($srcPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 | 4 |  |         $dstPath = self::cosUrlEncode($dstPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 269 | 4 | View Code Duplication |         if (filesize($srcPath) >= self::MAX_UNSLICE_FILE_SIZE) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |                 'message' => 'file '.$srcPath.' larger then 20M, please use uploadBySlicing interface', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |                 'data'    => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 | 4 |  |         $expired = time() + self::EXPIRED_SECONDS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 | 4 |  |         $url = self::generateResUrl($bucket, $dstPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 | 4 |  |         $signature = Auth::createReusableSignature($expired, $bucket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 | 4 |  |         $fileSha = hash_file('sha1', $srcPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 | 4 |  |             'op'       => 'upload', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 | 4 |  |             'sha'      => $fileSha, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 | 4 |  |             'biz_attr' => (isset($bizAttr) ? $bizAttr : ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 | 4 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 | 4 |  |         $data['filecontent'] = file_get_contents($srcPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 | 4 |  |         if (isset($insertOnly) && strlen($insertOnly) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 | 2 |  |             $data['insertOnly'] = (($insertOnly == 0 || $insertOnly == '0') ? 0 : 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |         $req = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 | 4 |  |             'url'     => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 | 4 |  |             'method'  => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 4 |  |             'timeout' => self::$timeout, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 | 4 |  |             'data'    => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |             'header'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 4 |  |                 'Authorization: '.$signature, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 4 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 | 4 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 4 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |      * 内部方法,上传文件. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |      * @param string $bucket     bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |      * @param string $srcPath    本地文件路径 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |      * @param string $dstPath    上传的文件路径 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |      * @param string $bizAttr    文件属性 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |      * @param string $sliceSize  分片大小 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |      * @param int    $insertOnly 是否覆盖同名文件:0 覆盖,1:不覆盖 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |      * @return [type] [description] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |     private static function uploadBySlicing( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |         $bucket, $srcFpath, $dstFpath, $bizAttr = null, $sliceSize = null, $insertOnly = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |         $srcFpath = realpath($srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |         $fileSize = filesize($srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |         $dstFpath = self::cosUrlEncode($dstFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |         $url = self::generateResUrl($bucket, $dstFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |         $sliceCount = ceil($fileSize / $sliceSize); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |         // expiration seconds for one slice mutiply by slice count | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         // will be the expired seconds for whole file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |         $expiration = time() + (self::EXPIRED_SECONDS * $sliceCount); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |         if ($expiration >= (time() + 10 * 24 * 60 * 60)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |             $expiration = time() + 10 * 24 * 60 * 60; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |         $signature = Auth::createReusableSignature($expiration, $bucket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |         $sliceUploading = new SliceUploading(self::$timeout * 1000, self::MAX_RETRY_TIMES); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |         for ($tryCount = 0; $tryCount < self::MAX_RETRY_TIMES; ++$tryCount) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  |             if ($sliceUploading->initUploading( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  |                 $signature, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |                 $srcFpath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |                 $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |                 $fileSize, $sliceSize, $bizAttr, $insertOnly) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |             $errorCode = $sliceUploading->getLastErrorCode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |             if ($errorCode === -4019) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |                 // Delete broken file and retry again on _ERROR_FILE_NOT_FINISH_UPLOAD error. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |                 self::delFile($bucket, $dstFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 353 |  | View Code Duplication |             if ($tryCount === self::MAX_RETRY_TIMES - 1) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |                 return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |                     'code'       => $sliceUploading->getLastErrorCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |                     'message'    => $sliceUploading->getLastErrorMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |                     'request_id' => $sliceUploading->getRequestId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 362 |  | View Code Duplication |         if (!$sliceUploading->performUploading()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |                 'code'       => $sliceUploading->getLastErrorCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |                 'message'    => $sliceUploading->getLastErrorMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |                 'request_id' => $sliceUploading->getRequestId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 370 |  | View Code Duplication |         if (!$sliceUploading->finishUploading()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |                 'code'       => $sliceUploading->getLastErrorCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |                 'message'    => $sliceUploading->getLastErrorMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |                 'request_id' => $sliceUploading->getRequestId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |             'code'       => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |             'message'    => 'success', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |             'request_id' => $sliceUploading->getRequestId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |             'data'       => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |                 'access_url'    => $sliceUploading->getAccessUrl(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |                 'resource_path' => $sliceUploading->getResourcePath(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |                 'source_url'    => $sliceUploading->getSourceUrl(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |      * 内部公共函数 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |      * @param  string  $bucket bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |      * @param  string  $path       文件夹路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |      * @param  int     $num        拉取的总数 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |      * @param  string  $pattern    eListBoth(默认),ListDirOnly,eListFileOnly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |      * @param  int     $order      默认正序(=0), 填1为反序, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |      * @param  string  $context    在翻页查询时候用到 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 398 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 399 | 1 |  |     private static function listBase( | 
            
                                                                        
                            
            
                                    
            
            
                | 400 |  |  |         $bucket, $path, $num = 20, $pattern = 'eListBoth', $order = 0, $context = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 401 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 402 | 1 |  |         $path = self::cosUrlEncode($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 403 | 1 |  |         $expired = time() + self::EXPIRED_SECONDS; | 
            
                                                                        
                            
            
                                    
            
            
                | 404 | 1 |  |         $url = self::generateResUrl($bucket, $path); | 
            
                                                                        
                            
            
                                    
            
            
                | 405 | 1 |  |         $signature = Auth::createReusableSignature($expired, $bucket); | 
            
                                                                        
                            
            
                                    
            
            
                | 406 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 407 |  |  |         $data = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 408 | 1 |  |             'op' => 'list', | 
            
                                                                        
                            
            
                                    
            
            
                | 409 | 1 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 410 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 411 | 1 |  |         if (self::isPatternValid($pattern) == false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 412 |  |  |             return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 413 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                        
                            
            
                                    
            
            
                | 414 |  |  |                 'message' => 'parameter pattern invalid', | 
            
                                                                        
                            
            
                                    
            
            
                | 415 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 416 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 417 | 1 |  |         $data['pattern'] = $pattern; | 
            
                                                                        
                            
            
                                    
            
            
                | 418 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 419 | 1 | View Code Duplication |         if ($order != 0 && $order != 1) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 420 |  |  |             return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 421 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                        
                            
            
                                    
            
            
                | 422 |  |  |                 'message' => 'parameter order invalid', | 
            
                                                                        
                            
            
                                    
            
            
                | 423 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 424 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 425 | 1 |  |         $data['order'] = $order; | 
            
                                                                        
                            
            
                                    
            
            
                | 426 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 427 | 1 | View Code Duplication |         if ($num < 0 || $num > 199) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 428 |  |  |             return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 429 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                        
                            
            
                                    
            
            
                | 430 |  |  |                 'message' => 'parameter num invalid, num need less then 200', | 
            
                                                                        
                            
            
                                    
            
            
                | 431 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 432 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 433 | 1 |  |         $data['num'] = $num; | 
            
                                                                        
                            
            
                                    
            
            
                | 434 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 435 | 1 |  |         if (isset($context)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 436 |  |  |             $data['context'] = $context; | 
            
                                                                        
                            
            
                                    
            
            
                | 437 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 438 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 439 | 1 |  |         $url = $url.'?'.http_build_query($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 440 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 441 |  |  |         $req = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 442 | 1 |  |             'url'     => $url, | 
            
                                                                        
                            
            
                                    
            
            
                | 443 | 1 |  |             'method'  => 'get', | 
            
                                                                        
                            
            
                                    
            
            
                | 444 | 1 |  |             'timeout' => self::$timeout, | 
            
                                                                        
                            
            
                                    
            
            
                | 445 |  |  |             'header'  => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 446 | 1 |  |                 'Authorization: '.$signature, | 
            
                                                                        
                            
            
                                    
            
            
                | 447 | 1 |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 448 | 1 |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 449 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 450 | 1 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 451 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |      * 内部公共方法(更新文件和更新文件夹) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  |      * @param  string  $bucket  bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  |      * @param  string  $path        路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |      * @param  string  $bizAttr     文件/目录属性 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |      * @param  string  $authority:  eInvalid/eWRPrivate(私有)/eWPrivateRPublic(公有读写) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |      * @param  array   $customer_headers_array 携带的用户自定义头域,包括 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |      * 'Cache-Control' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  |      * 'Content-Type' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |      * 'Content-Disposition' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  |      * 'Content-Language' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |      * 'x-cos-meta-自定义内容' => '*' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 | 4 |  |     private static function updateBase( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |         $bucket, $path, $bizAttr = null, $authority = null, $custom_headers_array = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 | 4 |  |         $path = self::cosUrlEncode($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 | 4 |  |         $expired = time() + self::EXPIRED_SECONDS; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 | 4 |  |         $url = self::generateResUrl($bucket, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 | 4 |  |         $signature = Auth::createNonreusableSignature($bucket, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 | 4 |  |         $data = ['op' => 'update']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 | 4 |  |         if (isset($bizAttr)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  |             $data['biz_attr'] = $bizAttr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 | 4 |  |         if (isset($authority) && strlen($authority) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 | 1 |  |             if (self::isAuthorityValid($authority) == false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |                 return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |                     'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |                     'message' => 'parameter authority invalid', ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 | 1 |  |             $data['authority'] = $authority; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 | 4 |  |         if (isset($custom_headers_array)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 | 3 |  |             $data['custom_headers'] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 | 3 |  |             self::add_customer_header($data['custom_headers'], $custom_headers_array); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 | 4 |  |         $data = json_encode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |         $req = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 | 4 |  |             'url'     => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 | 4 |  |             'method'  => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 | 4 |  |             'timeout' => self::$timeout, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 | 4 |  |             'data'    => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |             'header'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 | 4 |  |                 'Authorization: '.$signature, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 | 4 |  |                 'Content-Type: application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 | 4 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 | 4 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 | 4 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |      * 内部方法 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |      * @param  string  $bucket  bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |      * @param  string  $path        文件/目录路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 | 7 |  |     private static function statBase($bucket, $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 | 7 |  |         $path = self::cosUrlEncode($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 | 7 |  |         $expired = time() + self::EXPIRED_SECONDS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 | 7 |  |         $url = self::generateResUrl($bucket, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 | 7 |  |         $signature = Auth::createReusableSignature($expired, $bucket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 | 7 |  |         $data = ['op' => 'stat']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 | 7 |  |         $url = $url.'?'.http_build_query($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |         $req = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 | 7 |  |             'url'     => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 | 7 |  |             'method'  => 'get', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 | 7 |  |             'timeout' => self::$timeout, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |             'header'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 | 7 |  |                 'Authorization: '.$signature, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 | 7 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 | 7 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 | 7 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |      * 内部私有方法 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |      * @param  string  $bucket  bucket名称 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |      * @param  string  $path        文件/目录路径路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 | 4 |  |     private static function delBase($bucket, $path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 | 4 |  |         if ($path == '/') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  |                 'code'    => ErrorCode::COSAPI_PARAMS_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  |                 'message' => 'can not delete bucket using api! go to '. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  |                     'http://console.qcloud.com/cos to operate bucket', ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 | 4 |  |         $path = self::cosUrlEncode($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 | 4 |  |         $expired = time() + self::EXPIRED_SECONDS; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 | 4 |  |         $url = self::generateResUrl($bucket, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 | 4 |  |         $signature = Auth::createNonreusableSignature($bucket, $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 | 4 |  |         $data = ['op' => 'delete']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 | 4 |  |         $data = json_encode($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  |         $req = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 563 | 4 |  |             'url'     => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 564 | 4 |  |             'method'  => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 565 | 4 |  |             'timeout' => self::$timeout, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 566 | 4 |  |             'data'    => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 567 |  |  |             'header'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 568 | 4 |  |                 'Authorization: '.$signature, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 569 | 4 |  |                 'Content-Type: application/json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 570 | 4 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 571 | 4 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 572 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 573 | 4 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 574 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 575 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 576 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 577 |  |  |      * 内部公共方法, 路径编码 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 578 |  |  |      * @param  string  $path 待编码路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 579 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 580 | 18 |  |     private static function cosUrlEncode($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 581 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 582 | 18 |  |         return str_replace('%2F', '/', rawurlencode($path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 583 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 584 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 585 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 586 |  |  |      * 内部公共方法, 构造URL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 587 |  |  |      * @param  string  $bucket | 
            
                                                                                                            
                            
            
                                    
            
            
                | 588 |  |  |      * @param  string  $dstPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 589 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 590 | 22 |  |     private static function generateResUrl($bucket, $dstPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 591 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 592 | 22 |  |         $endPoint = Conf::API_COSAPI_END_POINT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 593 | 22 |  |         $endPoint = str_replace('region', self::$region, $endPoint); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 594 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 595 | 22 |  |         return $endPoint.Conf::getAppId().'/'.$bucket.$dstPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 596 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 597 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 598 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 599 |  |  |      * 内部公共方法, 发送消息 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 600 |  |  |      * @param  string  $req | 
            
                                                                                                            
                            
            
                                    
            
            
                | 601 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 602 | 22 |  |     private static function sendRequest($req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 603 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 604 | 22 |  |         $rsp = HttpClient::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 605 | 22 |  |         if ($rsp === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 606 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 607 |  |  |                 'code'    => ErrorCode::COSAPI_NETWORK_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 608 |  |  |                 'message' => 'network error', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 609 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 610 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 611 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 612 | 22 |  |         $info = HttpClient::info(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 613 | 22 |  |         $ret = json_decode($rsp, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 614 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 615 | 22 |  |         if ($ret === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 616 |  |  |             return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 617 |  |  |                 'code'    => ErrorCode::COSAPI_NETWORK_ERROR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 618 |  |  |                 'message' => $rsp, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 619 |  |  |                 'data'    => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 620 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 621 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 622 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 623 | 22 |  |         return $ret; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 624 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 625 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 626 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 627 |  |  |      * Get slice size. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 628 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 629 |  |  |     private static function getSliceSize($sliceSize) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 630 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 631 |  |  |         // Fix slice size to 1MB. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 632 |  |  |         return self::SLICE_SIZE_1M; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 633 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 634 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 635 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 636 |  |  |      * 内部方法, 规整文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 637 |  |  |      * @param  string  $path      文件路径 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 638 |  |  |      * @param  string  $isfolder  是否为文件夹 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 639 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 640 | 22 |  |     private static function normalizerPath($path, $isfolder = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 641 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 642 | 22 |  |         if (preg_match('/^\//', $path) == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 643 | 22 |  |             $path = '/'.$path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 644 | 22 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 645 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 646 | 22 |  |         if ($isfolder == true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 647 | 5 |  |             if (preg_match('/\/$/', $path) == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 648 | 5 |  |                 $path = $path.'/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 649 | 5 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 650 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 651 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 652 |  |  |         // Remove unnecessary slashes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 653 | 22 |  |         $path = preg_replace('#/+#', '/', $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 654 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 655 | 22 |  |         return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 656 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 657 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 658 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 659 |  |  |      * 判断authority值是否正确. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 660 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 661 |  |  |      * @param string $authority | 
            
                                                                                                            
                            
            
                                    
            
            
                | 662 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 663 |  |  |      * @return [type] bool | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 664 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 665 | 1 |  |     private static function isAuthorityValid($authority) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 666 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 667 | 1 |  |         if ($authority == 'eInvalid' || $authority == 'eWRPrivate' || $authority == 'eWPrivateRPublic') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 668 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 669 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 670 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 671 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 672 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 673 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 674 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 675 |  |  |      * 判断pattern值是否正确. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 676 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 677 |  |  |      * @param string $authority | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 678 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 679 |  |  |      * @return [type] bool | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 680 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 681 | 1 |  |     private static function isPatternValid($pattern) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 682 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 683 | 1 |  |         if ($pattern == 'eListBoth' || $pattern == 'eListDirOnly' || $pattern == 'eListFileOnly') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 684 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 685 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 686 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 687 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 688 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 689 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 690 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 691 |  |  |      * 判断是否符合自定义属性. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 692 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 693 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 694 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 695 |  |  |      * @return [type] bool | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 696 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 697 | 3 |  |     private static function isCustomer_header($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 698 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 699 | 3 |  |         if ($key == 'Cache-Control' || $key == 'Content-Type' || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 700 |  |  |             $key == 'Content-Disposition' || $key == 'Content-Language' || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 701 |  |  |             $key == 'Content-Encoding' || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 702 |  |  |             substr($key, 0, strlen('x-cos-meta-')) == 'x-cos-meta-' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 703 | 3 |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 704 | 3 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 705 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 706 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 707 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 708 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 709 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 710 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 711 |  |  |      * 增加自定义属性到data中. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 712 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 713 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 714 |  |  |      * @param array $customer_headers_array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 715 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 716 |  |  |      * @return [type] void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 717 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 718 | 3 |  |     private static function add_customer_header(&$data, &$customer_headers_array) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 719 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 720 | 3 |  |         if (count($customer_headers_array) < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 721 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 722 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 723 | 3 |  |         foreach ($customer_headers_array as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 724 | 3 |  |             if (self::isCustomer_header($key)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 725 | 3 |  |                 $data[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 726 | 3 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 727 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 728 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 729 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 730 |  |  |     // Check |$path| is a valid file path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 731 |  |  |     // Return true on success, otherwise return false. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 732 | 2 |  |     private static function isValidPath($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 733 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 734 | 2 |  |         if (strpos($path, '?') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 735 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 736 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 737 | 2 |  |         if (strpos($path, '*') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 738 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 739 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 740 | 2 |  |         if (strpos($path, ':') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 741 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 742 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 743 | 2 |  |         if (strpos($path, '|') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 744 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 745 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 746 | 2 |  |         if (strpos($path, '\\') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 747 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 748 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 749 | 2 |  |         if (strpos($path, '<') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 750 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 751 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 752 | 2 |  |         if (strpos($path, '>') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 753 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 754 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 755 | 2 |  |         if (strpos($path, '"') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 756 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 757 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 758 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 759 | 2 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 760 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 761 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 762 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 763 |  |  |      * Copy a file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 764 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 765 |  |  |      * @param $bucket    bucket name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 766 |  |  |      * @param $srcFpath  source file path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 767 |  |  |      * @param $dstFpath  destination file path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 768 |  |  |      * @param $overwrite if the destination location is occupied, overwrite it or not? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 769 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 770 |  |  |      * @return array|mixed. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 771 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 772 | 2 | View Code Duplication |     public static function copyFile($bucket, $srcFpath, $dstFpath, $overwrite = false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 773 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 774 | 2 |  |         $srcFpath = self::normalizerPath($srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 775 | 2 |  |         $dstFpath = self::normalizerPath($dstFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 776 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 777 | 2 |  |         $url = self::generateResUrl($bucket, $srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 778 | 2 |  |         $sign = Auth::createNonreusableSignature($bucket, $srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 779 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 780 | 2 |  |             'op'            => 'copy', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 781 | 2 |  |             'dest_fileid'   => $dstFpath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 782 | 2 |  |             'to_over_write' => $overwrite ? 1 : 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 783 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 784 |  |  |         $req = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 785 | 2 |  |             'url'     => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 786 | 2 |  |             'method'  => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 787 | 2 |  |             'timeout' => self::$timeout, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 788 | 2 |  |             'data'    => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 789 |  |  |             'header'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 790 | 2 |  |                 'Authorization: '.$sign, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 791 | 2 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 792 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 793 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 794 | 2 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 795 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 796 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 797 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 798 |  |  |      * Move a file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 799 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 800 |  |  |      * @param $bucket    bucket name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 801 |  |  |      * @param $srcFpath  source file path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 802 |  |  |      * @param $dstFpath  destination file path. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 803 |  |  |      * @param $overwrite if the destination location is occupied, overwrite it or not? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 804 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 805 |  |  |      * @return array|mixed. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 806 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 807 | 2 | View Code Duplication |     public static function moveFile($bucket, $srcFpath, $dstFpath, $overwrite = false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 808 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 809 | 2 |  |         $srcFpath = self::normalizerPath($srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 810 | 2 |  |         $dstFpath = self::normalizerPath($dstFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 811 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 812 | 2 |  |         $url = self::generateResUrl($bucket, $srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 813 | 2 |  |         $sign = Auth::createNonreusableSignature($bucket, $srcFpath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 814 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 815 | 2 |  |             'op'            => 'move', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 816 | 2 |  |             'dest_fileid'   => $dstFpath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 817 | 2 |  |             'to_over_write' => $overwrite ? 1 : 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 818 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 819 |  |  |         $req = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 820 | 2 |  |             'url'     => $url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 821 | 2 |  |             'method'  => 'post', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 822 | 2 |  |             'timeout' => self::$timeout, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 823 | 2 |  |             'data'    => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 824 |  |  |             'header'  => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 825 | 2 |  |                 'Authorization: '.$sign, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 826 | 2 |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 827 | 2 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 828 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 829 | 2 |  |         return self::sendRequest($req); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 830 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 831 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 832 |  |  |  | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.