@@ -13,958 +13,958 @@ discard block |
||
13 | 13 | */ |
14 | 14 | class RequestCore |
15 | 15 | { |
16 | - /** |
|
17 | - * The URL being requested. |
|
18 | - */ |
|
19 | - public $request_url; |
|
20 | - |
|
21 | - /** |
|
22 | - * The headers being sent in the request. |
|
23 | - */ |
|
24 | - public $request_headers; |
|
25 | - |
|
26 | - /** |
|
27 | - * The body being sent in the request. |
|
28 | - */ |
|
29 | - public $request_body; |
|
30 | - |
|
31 | - /** |
|
32 | - * The response returned by the request. |
|
33 | - */ |
|
34 | - public $response; |
|
35 | - |
|
36 | - /** |
|
37 | - * The headers returned by the request. |
|
38 | - */ |
|
39 | - public $response_headers; |
|
40 | - |
|
41 | - /** |
|
42 | - * The body returned by the request. |
|
43 | - */ |
|
44 | - public $response_body; |
|
45 | - |
|
46 | - /** |
|
47 | - * The HTTP status code returned by the request. |
|
48 | - */ |
|
49 | - public $response_code; |
|
50 | - |
|
51 | - /** |
|
52 | - * Additional response data. |
|
53 | - */ |
|
54 | - public $response_info; |
|
55 | - |
|
56 | - /** |
|
57 | - * The handle for the cURL object. |
|
58 | - */ |
|
59 | - public $curl_handle; |
|
60 | - |
|
61 | - /** |
|
62 | - * The method by which the request is being made. |
|
63 | - */ |
|
64 | - public $method; |
|
65 | - |
|
66 | - /** |
|
67 | - * Stores the proxy settings to use for the request. |
|
68 | - */ |
|
69 | - public $proxy = null; |
|
70 | - |
|
71 | - /** |
|
72 | - * The username to use for the request. |
|
73 | - */ |
|
74 | - public $username = null; |
|
75 | - |
|
76 | - /** |
|
77 | - * The password to use for the request. |
|
78 | - */ |
|
79 | - public $password = null; |
|
80 | - |
|
81 | - /** |
|
82 | - * Custom CURLOPT settings. |
|
83 | - */ |
|
84 | - public $curlopts = null; |
|
85 | - |
|
86 | - /** |
|
87 | - * The state of debug mode. |
|
88 | - */ |
|
89 | - public $debug_mode = false; |
|
90 | - |
|
91 | - /** |
|
92 | - * The default class to use for HTTP Requests (defaults to <RequestCore>). |
|
93 | - */ |
|
94 | - public $request_class = 'RequestCore'; |
|
95 | - |
|
96 | - /** |
|
97 | - * The default class to use for HTTP Responses (defaults to <ResponseCore>). |
|
98 | - */ |
|
99 | - public $response_class = 'ResponseCore'; |
|
100 | - |
|
101 | - /** |
|
102 | - * Default useragent string to use. |
|
103 | - */ |
|
104 | - public $useragent = 'RequestCore/1.4.3'; |
|
105 | - |
|
106 | - /** |
|
107 | - * File to read from while streaming up. |
|
108 | - */ |
|
109 | - public $read_file = null; |
|
110 | - |
|
111 | - /** |
|
112 | - * The resource to read from while streaming up. |
|
113 | - */ |
|
114 | - public $read_stream = null; |
|
115 | - |
|
116 | - /** |
|
117 | - * The size of the stream to read from. |
|
118 | - */ |
|
119 | - public $read_stream_size = null; |
|
120 | - |
|
121 | - /** |
|
122 | - * The length already read from the stream. |
|
123 | - */ |
|
124 | - public $read_stream_read = 0; |
|
125 | - |
|
126 | - /** |
|
127 | - * File to write to while streaming down. |
|
128 | - */ |
|
129 | - public $write_file = null; |
|
130 | - |
|
131 | - /** |
|
132 | - * The resource to write to while streaming down. |
|
133 | - */ |
|
134 | - public $write_stream = null; |
|
135 | - |
|
136 | - /** |
|
137 | - * Stores the intended starting seek position. |
|
138 | - */ |
|
139 | - public $seek_position = null; |
|
140 | - |
|
141 | - /** |
|
142 | - * The location of the cacert.pem file to use. |
|
143 | - */ |
|
144 | - public $cacert_location = false; |
|
145 | - |
|
146 | - /** |
|
147 | - * The state of SSL certificate verification. |
|
148 | - */ |
|
149 | - public $ssl_verification = true; |
|
150 | - |
|
151 | - /** |
|
152 | - * The user-defined callback function to call when a stream is read from. |
|
153 | - */ |
|
154 | - public $registered_streaming_read_callback = null; |
|
155 | - |
|
156 | - /** |
|
157 | - * The user-defined callback function to call when a stream is written to. |
|
158 | - */ |
|
159 | - public $registered_streaming_write_callback = null; |
|
160 | - |
|
161 | - /*%******************************************************************************************%*/ |
|
162 | - // CONSTANTS |
|
163 | - |
|
164 | - /** |
|
165 | - * GET HTTP Method |
|
166 | - */ |
|
167 | - const HTTP_GET = 'GET'; |
|
168 | - |
|
169 | - /** |
|
170 | - * POST HTTP Method |
|
171 | - */ |
|
172 | - const HTTP_POST = 'POST'; |
|
173 | - |
|
174 | - /** |
|
175 | - * PUT HTTP Method |
|
176 | - */ |
|
177 | - const HTTP_PUT = 'PUT'; |
|
178 | - |
|
179 | - /** |
|
180 | - * DELETE HTTP Method |
|
181 | - */ |
|
182 | - const HTTP_DELETE = 'DELETE'; |
|
183 | - |
|
184 | - /** |
|
185 | - * HEAD HTTP Method |
|
186 | - */ |
|
187 | - const HTTP_HEAD = 'HEAD'; |
|
188 | - |
|
189 | - |
|
190 | - /*%******************************************************************************************%*/ |
|
191 | - // CONSTRUCTOR/DESTRUCTOR |
|
192 | - |
|
193 | - /** |
|
194 | - * Constructs a new instance of this class. |
|
195 | - * |
|
196 | - * @param string $url (Optional) The URL to request or service endpoint to query. |
|
197 | - * @param string $proxy (Optional) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port` |
|
198 | - * @param array $helpers (Optional) An associative array of classnames to use for request, and response functionality. Gets passed in automatically by the calling class. |
|
199 | - * @return $this A reference to the current instance. |
|
200 | - */ |
|
201 | - public function __construct($url = null, $proxy = null, $helpers = null) |
|
202 | - { |
|
203 | - // Set some default values. |
|
204 | - $this->request_url = $url; |
|
205 | - $this->method = self::HTTP_GET; |
|
206 | - $this->request_headers = array(); |
|
207 | - $this->request_body = ''; |
|
208 | - |
|
209 | - // Set a new Request class if one was set. |
|
210 | - if (isset($helpers['request']) && !empty($helpers['request'])) |
|
211 | - { |
|
212 | - $this->request_class = $helpers['request']; |
|
213 | - } |
|
214 | - |
|
215 | - // Set a new Request class if one was set. |
|
216 | - if (isset($helpers['response']) && !empty($helpers['response'])) |
|
217 | - { |
|
218 | - $this->response_class = $helpers['response']; |
|
219 | - } |
|
220 | - |
|
221 | - if ($proxy) |
|
222 | - { |
|
223 | - $this->set_proxy($proxy); |
|
224 | - } |
|
225 | - |
|
226 | - return $this; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Destructs the instance. Closes opened file handles. |
|
231 | - * |
|
232 | - * @return $this A reference to the current instance. |
|
233 | - */ |
|
234 | - public function __destruct() |
|
235 | - { |
|
236 | - if (isset($this->read_file) && isset($this->read_stream)) |
|
237 | - { |
|
238 | - fclose($this->read_stream); |
|
239 | - } |
|
240 | - |
|
241 | - if (isset($this->write_file) && isset($this->write_stream)) |
|
242 | - { |
|
243 | - fclose($this->write_stream); |
|
244 | - } |
|
245 | - |
|
246 | - return $this; |
|
247 | - } |
|
248 | - |
|
249 | - |
|
250 | - /*%******************************************************************************************%*/ |
|
251 | - // REQUEST METHODS |
|
252 | - |
|
253 | - /** |
|
254 | - * Sets the credentials to use for authentication. |
|
255 | - * |
|
256 | - * @param string $user (Required) The username to authenticate with. |
|
257 | - * @param string $pass (Required) The password to authenticate with. |
|
258 | - * @return $this A reference to the current instance. |
|
259 | - */ |
|
260 | - public function set_credentials($user, $pass) |
|
261 | - { |
|
262 | - $this->username = $user; |
|
263 | - $this->password = $pass; |
|
264 | - return $this; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Adds a custom HTTP header to the cURL request. |
|
269 | - * |
|
270 | - * @param string $key (Required) The custom HTTP header to set. |
|
271 | - * @param mixed $value (Required) The value to assign to the custom HTTP header. |
|
272 | - * @return $this A reference to the current instance. |
|
273 | - */ |
|
274 | - public function add_header($key, $value) |
|
275 | - { |
|
276 | - $this->request_headers[$key] = $value; |
|
277 | - return $this; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Removes an HTTP header from the cURL request. |
|
282 | - * |
|
283 | - * @param string $key (Required) The custom HTTP header to set. |
|
284 | - * @return $this A reference to the current instance. |
|
285 | - */ |
|
286 | - public function remove_header($key) |
|
287 | - { |
|
288 | - if (isset($this->request_headers[$key])) |
|
289 | - { |
|
290 | - unset($this->request_headers[$key]); |
|
291 | - } |
|
292 | - return $this; |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Set the method type for the request. |
|
297 | - * |
|
298 | - * @param string $method (Required) One of the following constants: <HTTP_GET>, <HTTP_POST>, <HTTP_PUT>, <HTTP_HEAD>, <HTTP_DELETE>. |
|
299 | - * @return $this A reference to the current instance. |
|
300 | - */ |
|
301 | - public function set_method($method) |
|
302 | - { |
|
303 | - $this->method = strtoupper($method); |
|
304 | - return $this; |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Sets a custom useragent string for the class. |
|
309 | - * |
|
310 | - * @param string $ua (Required) The useragent string to use. |
|
311 | - * @return $this A reference to the current instance. |
|
312 | - */ |
|
313 | - public function set_useragent($ua) |
|
314 | - { |
|
315 | - $this->useragent = $ua; |
|
316 | - return $this; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Set the body to send in the request. |
|
321 | - * |
|
322 | - * @param string $body (Required) The textual content to send along in the body of the request. |
|
323 | - * @return $this A reference to the current instance. |
|
324 | - */ |
|
325 | - public function set_body($body) |
|
326 | - { |
|
327 | - $this->request_body = $body; |
|
328 | - return $this; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Set the URL to make the request to. |
|
333 | - * |
|
334 | - * @param string $url (Required) The URL to make the request to. |
|
335 | - * @return $this A reference to the current instance. |
|
336 | - */ |
|
337 | - public function set_request_url($url) |
|
338 | - { |
|
339 | - $this->request_url = $url; |
|
340 | - return $this; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Set additional CURLOPT settings. These will merge with the default settings, and override if |
|
345 | - * there is a duplicate. |
|
346 | - * |
|
347 | - * @param array $curlopts (Optional) A set of key-value pairs that set `CURLOPT` options. These will merge with the existing CURLOPTs, and ones passed here will override the defaults. Keys should be the `CURLOPT_*` constants, not strings. |
|
348 | - * @return $this A reference to the current instance. |
|
349 | - */ |
|
350 | - public function set_curlopts($curlopts) |
|
351 | - { |
|
352 | - $this->curlopts = $curlopts; |
|
353 | - return $this; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Sets the length in bytes to read from the stream while streaming up. |
|
358 | - * |
|
359 | - * @param integer $size (Required) The length in bytes to read from the stream. |
|
360 | - * @return $this A reference to the current instance. |
|
361 | - */ |
|
362 | - public function set_read_stream_size($size) |
|
363 | - { |
|
364 | - $this->read_stream_size = $size; |
|
365 | - |
|
366 | - return $this; |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Sets the resource to read from while streaming up. Reads the stream from its current position until |
|
371 | - * EOF or `$size` bytes have been read. If `$size` is not given it will be determined by <php:fstat()> and |
|
372 | - * <php:ftell()>. |
|
373 | - * |
|
374 | - * @param resource $resource (Required) The readable resource to read from. |
|
375 | - * @param integer $size (Optional) The size of the stream to read. |
|
376 | - * @return $this A reference to the current instance. |
|
377 | - */ |
|
378 | - public function set_read_stream($resource, $size = null) |
|
379 | - { |
|
380 | - if (!isset($size) || $size < 0) |
|
381 | - { |
|
382 | - $stats = fstat($resource); |
|
383 | - |
|
384 | - if ($stats && $stats['size'] >= 0) |
|
385 | - { |
|
386 | - $position = ftell($resource); |
|
387 | - |
|
388 | - if ($position !== false && $position >= 0) |
|
389 | - { |
|
390 | - $size = $stats['size'] - $position; |
|
391 | - } |
|
392 | - } |
|
393 | - } |
|
394 | - |
|
395 | - $this->read_stream = $resource; |
|
396 | - |
|
397 | - return $this->set_read_stream_size($size); |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Sets the file to read from while streaming up. |
|
402 | - * |
|
403 | - * @param string $location (Required) The readable location to read from. |
|
404 | - * @return $this A reference to the current instance. |
|
405 | - */ |
|
406 | - public function set_read_file($location) |
|
407 | - { |
|
408 | - $this->read_file = $location; |
|
409 | - $read_file_handle = fopen($location, 'r'); |
|
410 | - |
|
411 | - return $this->set_read_stream($read_file_handle); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Sets the resource to write to while streaming down. |
|
416 | - * |
|
417 | - * @param resource $resource (Required) The writeable resource to write to. |
|
418 | - * @return $this A reference to the current instance. |
|
419 | - */ |
|
420 | - public function set_write_stream($resource) |
|
421 | - { |
|
422 | - $this->write_stream = $resource; |
|
423 | - |
|
424 | - return $this; |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Sets the file to write to while streaming down. |
|
429 | - * |
|
430 | - * @param string $location (Required) The writeable location to write to. |
|
431 | - * @return $this A reference to the current instance. |
|
432 | - */ |
|
433 | - public function set_write_file($location) |
|
434 | - { |
|
435 | - $this->write_file = $location; |
|
436 | - $write_file_handle = fopen($location, 'w'); |
|
437 | - |
|
438 | - return $this->set_write_stream($write_file_handle); |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Set the proxy to use for making requests. |
|
443 | - * |
|
444 | - * @param string $proxy (Required) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port` |
|
445 | - * @return $this A reference to the current instance. |
|
446 | - */ |
|
447 | - public function set_proxy($proxy) |
|
448 | - { |
|
449 | - $proxy = parse_url($proxy); |
|
450 | - $proxy['user'] = isset($proxy['user']) ? $proxy['user'] : null; |
|
451 | - $proxy['pass'] = isset($proxy['pass']) ? $proxy['pass'] : null; |
|
452 | - $proxy['port'] = isset($proxy['port']) ? $proxy['port'] : null; |
|
453 | - $this->proxy = $proxy; |
|
454 | - return $this; |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Set the intended starting seek position. |
|
459 | - * |
|
460 | - * @param integer $position (Required) The byte-position of the stream to begin reading from. |
|
461 | - * @return $this A reference to the current instance. |
|
462 | - */ |
|
463 | - public function set_seek_position($position) |
|
464 | - { |
|
465 | - $this->seek_position = isset($position) ? (integer) $position : null; |
|
466 | - |
|
467 | - return $this; |
|
468 | - } |
|
469 | - |
|
470 | - /** |
|
471 | - * Register a callback function to execute whenever a data stream is read from using |
|
472 | - * <CFRequest::streaming_read_callback()>. |
|
473 | - * |
|
474 | - * The user-defined callback function should accept three arguments: |
|
475 | - * |
|
476 | - * <ul> |
|
477 | - * <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li> |
|
478 | - * <li><code>$file_handle</code> - <code>resource</code> - Required - The file handle resource that represents the file on the local file system.</li> |
|
479 | - * <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li> |
|
480 | - * </ul> |
|
481 | - * |
|
482 | - * @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul> |
|
483 | - * <li>The name of a global function to execute, passed as a string.</li> |
|
484 | - * <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li> |
|
485 | - * <li>An anonymous function (PHP 5.3+).</li></ul> |
|
486 | - * @return $this A reference to the current instance. |
|
487 | - */ |
|
488 | - public function register_streaming_read_callback($callback) |
|
489 | - { |
|
490 | - $this->registered_streaming_read_callback = $callback; |
|
491 | - |
|
492 | - return $this; |
|
493 | - } |
|
494 | - |
|
495 | - /** |
|
496 | - * Register a callback function to execute whenever a data stream is written to using |
|
497 | - * <CFRequest::streaming_write_callback()>. |
|
498 | - * |
|
499 | - * The user-defined callback function should accept two arguments: |
|
500 | - * |
|
501 | - * <ul> |
|
502 | - * <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li> |
|
503 | - * <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li> |
|
504 | - * </ul> |
|
505 | - * |
|
506 | - * @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul> |
|
507 | - * <li>The name of a global function to execute, passed as a string.</li> |
|
508 | - * <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li> |
|
509 | - * <li>An anonymous function (PHP 5.3+).</li></ul> |
|
510 | - * @return $this A reference to the current instance. |
|
511 | - */ |
|
512 | - public function register_streaming_write_callback($callback) |
|
513 | - { |
|
514 | - $this->registered_streaming_write_callback = $callback; |
|
515 | - |
|
516 | - return $this; |
|
517 | - } |
|
518 | - |
|
519 | - |
|
520 | - /*%******************************************************************************************%*/ |
|
521 | - // PREPARE, SEND, AND PROCESS REQUEST |
|
522 | - |
|
523 | - /** |
|
524 | - * A callback function that is invoked by cURL for streaming up. |
|
525 | - * |
|
526 | - * @param resource $curl_handle (Required) The cURL handle for the request. |
|
527 | - * @param resource $file_handle (Required) The open file handle resource. |
|
528 | - * @param integer $length (Required) The maximum number of bytes to read. |
|
529 | - * @return binary Binary data from a stream. |
|
530 | - */ |
|
531 | - public function streaming_read_callback($curl_handle, $file_handle, $length) |
|
532 | - { |
|
533 | - // Execute callback function |
|
534 | - if ($this->registered_streaming_read_callback) |
|
535 | - { |
|
536 | - return $this->registered_streaming_read_callback->streaming_read_callback($curl_handle, $file_handle, $length,$this->read_stream,$this->seek_position); |
|
537 | - } |
|
538 | - // Once we've sent as much as we're supposed to send... |
|
539 | - if ($this->read_stream_read >= $this->read_stream_size) |
|
540 | - { |
|
541 | - // Send EOF |
|
542 | - return ''; |
|
543 | - } |
|
544 | - |
|
545 | - // If we're at the beginning of an upload and need to seek... |
|
546 | - if ($this->read_stream_read == 0 && isset($this->seek_position) && $this->seek_position !== ftell($this->read_stream)) |
|
547 | - { |
|
548 | - if (fseek($this->read_stream, $this->seek_position) !== 0) |
|
549 | - { |
|
550 | - throw new RequestCore_Exception('The stream does not support seeking and is either not at the requested position or the position is unknown.'); |
|
551 | - } |
|
552 | - } |
|
553 | - |
|
554 | - $read = fread($this->read_stream, min($this->read_stream_size - $this->read_stream_read, $length)); // Remaining upload data or cURL's requested chunk size |
|
555 | - $this->read_stream_read += strlen($read); |
|
556 | - |
|
557 | - $out = $read === false ? '' : $read; |
|
558 | - |
|
559 | - return $out; |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * A callback function that is invoked by cURL for streaming down. |
|
564 | - * |
|
565 | - * @param resource $curl_handle (Required) The cURL handle for the request. |
|
566 | - * @param binary $data (Required) The data to write. |
|
567 | - * @return integer The number of bytes written. |
|
568 | - */ |
|
569 | - public function streaming_write_callback($curl_handle, $data) |
|
570 | - { |
|
571 | - if ($this->registered_streaming_write_callback){ |
|
572 | - return $this->registered_streaming_write_callback->streaming_write_callback($curl_handle,$data,$this->write_stream); |
|
573 | - } |
|
574 | - $length = strlen($data); |
|
575 | - $written_total = 0; |
|
576 | - $written_last = 0; |
|
577 | - |
|
578 | - while ($written_total < $length) |
|
579 | - { |
|
580 | - $written_last = fwrite($this->write_stream, substr($data, $written_total)); |
|
581 | - |
|
582 | - if ($written_last === false) |
|
583 | - { |
|
584 | - return $written_total; |
|
585 | - } |
|
586 | - |
|
587 | - $written_total += $written_last; |
|
588 | - } |
|
589 | - |
|
590 | - return $written_total; |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Prepares and adds the details of the cURL request. This can be passed along to a <php:curl_multi_exec()> |
|
595 | - * function. |
|
596 | - * |
|
597 | - * @return resource The handle for the cURL object. |
|
598 | - */ |
|
599 | - public function prep_request() |
|
600 | - { |
|
601 | - $curl_handle = curl_init(); |
|
602 | - |
|
603 | - // Set default options. |
|
604 | - curl_setopt($curl_handle, CURLOPT_URL, $this->request_url); |
|
605 | - curl_setopt($curl_handle, CURLOPT_FILETIME, true); |
|
606 | - curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, false); |
|
607 | - //为了兼容PHP 5.6,PHP 5.6把 CURLOPT_CLOSEPOLICY 这个变量删除了 |
|
608 | - //curl_setopt($curl_handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED); |
|
609 | - curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5); |
|
610 | - curl_setopt($curl_handle, CURLOPT_HEADER, true); |
|
611 | - curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); |
|
612 | - curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000); |
|
613 | - curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120); |
|
614 | - curl_setopt($curl_handle, CURLOPT_NOSIGNAL, true); |
|
615 | - curl_setopt($curl_handle, CURLOPT_REFERER, $this->request_url); |
|
616 | - curl_setopt($curl_handle, CURLOPT_USERAGENT, $this->useragent); |
|
617 | - curl_setopt($curl_handle, CURLOPT_READFUNCTION, array($this, 'streaming_read_callback')); |
|
618 | - |
|
619 | - // Verification of the SSL cert |
|
620 | - if ($this->ssl_verification) |
|
621 | - { |
|
622 | - curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, true); |
|
623 | - curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2); |
|
624 | - } |
|
625 | - else |
|
626 | - { |
|
627 | - curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); |
|
628 | - curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false); |
|
629 | - } |
|
630 | - |
|
631 | - // chmod the file as 0755 |
|
632 | - if ($this->cacert_location === true) |
|
633 | - { |
|
634 | - curl_setopt($curl_handle, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); |
|
635 | - } |
|
636 | - elseif (is_string($this->cacert_location)) |
|
637 | - { |
|
638 | - curl_setopt($curl_handle, CURLOPT_CAINFO, $this->cacert_location); |
|
639 | - } |
|
640 | - |
|
641 | - // Debug mode |
|
642 | - if ($this->debug_mode) |
|
643 | - { |
|
644 | - curl_setopt($curl_handle, CURLOPT_VERBOSE, true); |
|
645 | - } |
|
646 | - |
|
647 | - // Handle open_basedir & safe mode |
|
648 | - if (!ini_get('safe_mode') && !ini_get('open_basedir')) |
|
649 | - { |
|
650 | - curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, FALSE); |
|
651 | - } |
|
652 | - |
|
653 | - // Enable a proxy connection if requested. |
|
654 | - if ($this->proxy) |
|
655 | - { |
|
656 | - curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, true); |
|
657 | - |
|
658 | - $host = $this->proxy['host']; |
|
659 | - $host .= ($this->proxy['port']) ? ':' . $this->proxy['port'] : ''; |
|
660 | - curl_setopt($curl_handle, CURLOPT_PROXY, $host); |
|
661 | - |
|
662 | - if (isset($this->proxy['user']) && isset($this->proxy['pass'])) |
|
663 | - { |
|
664 | - curl_setopt($curl_handle, CURLOPT_PROXYUSERPWD, $this->proxy['user'] . ':' . $this->proxy['pass']); |
|
665 | - } |
|
666 | - } |
|
667 | - |
|
668 | - // Set credentials for HTTP Basic/Digest Authentication. |
|
669 | - if ($this->username && $this->password) |
|
670 | - { |
|
671 | - curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
|
672 | - curl_setopt($curl_handle, CURLOPT_USERPWD, $this->username . ':' . $this->password); |
|
673 | - } |
|
674 | - |
|
675 | - // Handle the encoding if we can. |
|
676 | - if (extension_loaded('zlib')) |
|
677 | - { |
|
678 | - curl_setopt($curl_handle, CURLOPT_ENCODING, ''); |
|
679 | - } |
|
680 | - |
|
681 | - // Process custom headers |
|
682 | - if (isset($this->request_headers) && count($this->request_headers)) |
|
683 | - { |
|
684 | - $temp_headers = array(); |
|
685 | - |
|
686 | - foreach ($this->request_headers as $k => $v) |
|
687 | - { |
|
688 | - $temp_headers[] = $k . ': ' . $v; |
|
689 | - } |
|
690 | - |
|
691 | - curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $temp_headers); |
|
692 | - } |
|
693 | - |
|
694 | - switch ($this->method) |
|
695 | - { |
|
696 | - case self::HTTP_PUT: |
|
697 | - //unset($this->read_stream); |
|
698 | - curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
699 | - if (isset($this->read_stream)) |
|
700 | - { |
|
701 | - if (!isset($this->read_stream_size) || $this->read_stream_size < 0) |
|
702 | - { |
|
703 | - throw new RequestCore_Exception('The stream size for the streaming upload cannot be determined.'); |
|
704 | - } |
|
705 | - |
|
706 | - curl_setopt($curl_handle, CURLOPT_INFILESIZE, $this->read_stream_size); |
|
707 | - curl_setopt($curl_handle, CURLOPT_UPLOAD, true); |
|
708 | - } |
|
709 | - else |
|
710 | - { |
|
711 | - curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body); |
|
712 | - } |
|
713 | - break; |
|
714 | - |
|
715 | - case self::HTTP_POST: |
|
716 | - curl_setopt($curl_handle, CURLOPT_POST, true); |
|
717 | - curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body); |
|
718 | - break; |
|
719 | - |
|
720 | - case self::HTTP_HEAD: |
|
721 | - curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, self::HTTP_HEAD); |
|
722 | - curl_setopt($curl_handle, CURLOPT_NOBODY, 1); |
|
723 | - break; |
|
724 | - |
|
725 | - default: // Assumed GET |
|
726 | - curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, $this->method); |
|
727 | - if (isset($this->write_stream)) |
|
728 | - { |
|
729 | - curl_setopt($curl_handle, CURLOPT_WRITEFUNCTION, array($this, 'streaming_write_callback')); |
|
730 | - curl_setopt($curl_handle, CURLOPT_HEADER, false); |
|
731 | - } |
|
732 | - else |
|
733 | - { |
|
734 | - curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body); |
|
735 | - } |
|
736 | - break; |
|
737 | - } |
|
738 | - |
|
739 | - // Merge in the CURLOPTs |
|
740 | - if (isset($this->curlopts) && sizeof($this->curlopts) > 0) |
|
741 | - { |
|
742 | - foreach ($this->curlopts as $k => $v) |
|
743 | - { |
|
744 | - curl_setopt($curl_handle, $k, $v); |
|
745 | - } |
|
746 | - } |
|
747 | - |
|
748 | - return $curl_handle; |
|
749 | - } |
|
750 | - |
|
751 | - /** |
|
752 | - * Take the post-processed cURL data and break it down into useful header/body/info chunks. Uses the |
|
753 | - * data stored in the `curl_handle` and `response` properties unless replacement data is passed in via |
|
754 | - * parameters. |
|
755 | - * |
|
756 | - * @param resource $curl_handle (Optional) The reference to the already executed cURL request. |
|
757 | - * @param string $response (Optional) The actual response content itself that needs to be parsed. |
|
758 | - * @return ResponseCore A <ResponseCore> object containing a parsed HTTP response. |
|
759 | - */ |
|
760 | - public function process_response($curl_handle = null, $response = null) |
|
761 | - { |
|
762 | - // Accept a custom one if it's passed. |
|
763 | - if ($curl_handle && $response) |
|
764 | - { |
|
765 | - $this->curl_handle = $curl_handle; |
|
766 | - $this->response = $response; |
|
767 | - } |
|
768 | - |
|
769 | - // As long as this came back as a valid resource... |
|
770 | - if (is_resource($this->curl_handle)) |
|
771 | - { |
|
772 | - // Determine what's what. |
|
773 | - $header_size = curl_getinfo($this->curl_handle, CURLINFO_HEADER_SIZE); |
|
774 | - $this->response_headers = substr($this->response, 0, $header_size); |
|
775 | - $this->response_body = substr($this->response, $header_size); |
|
776 | - $this->response_code = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE); |
|
777 | - $this->response_info = curl_getinfo($this->curl_handle); |
|
778 | - |
|
779 | - // Parse out the headers |
|
780 | - $this->response_headers = explode("\r\n\r\n", trim($this->response_headers)); |
|
781 | - $this->response_headers = array_pop($this->response_headers); |
|
782 | - $this->response_headers = explode("\r\n", $this->response_headers); |
|
783 | - array_shift($this->response_headers); |
|
784 | - |
|
785 | - // Loop through and split up the headers. |
|
786 | - $header_assoc = array(); |
|
787 | - foreach ($this->response_headers as $header) |
|
788 | - { |
|
789 | - $kv = explode(': ', $header); |
|
790 | - $header_assoc[strtolower($kv[0])] = isset($kv[1])?$kv[1]:''; |
|
791 | - } |
|
792 | - |
|
793 | - // Reset the headers to the appropriate property. |
|
794 | - $this->response_headers = $header_assoc; |
|
795 | - $this->response_headers['_info'] = $this->response_info; |
|
796 | - $this->response_headers['_info']['method'] = $this->method; |
|
797 | - |
|
798 | - if ($curl_handle && $response) |
|
799 | - { |
|
800 | - return new $this->response_class($this->response_headers, $this->response_body, $this->response_code, $this->curl_handle); |
|
801 | - } |
|
802 | - } |
|
803 | - |
|
804 | - // Return false |
|
805 | - return false; |
|
806 | - } |
|
807 | - |
|
808 | - /** |
|
809 | - * Sends the request, calling necessary utility functions to update built-in properties. |
|
810 | - * |
|
811 | - * @param boolean $parse (Optional) Whether to parse the response with ResponseCore or not. |
|
812 | - * @return string The resulting unparsed data from the request. |
|
813 | - */ |
|
814 | - public function send_request($parse = false) |
|
815 | - { |
|
816 | - set_time_limit(0); |
|
817 | - |
|
818 | - $curl_handle = $this->prep_request(); |
|
819 | - $this->response = curl_exec($curl_handle); |
|
820 | - |
|
821 | - if ($this->response === false) |
|
822 | - { |
|
823 | - throw new RequestCore_Exception('cURL resource: ' . (string) $curl_handle . '; cURL error: ' . curl_error($curl_handle) . ' (' . curl_errno($curl_handle) . ')'); |
|
824 | - } |
|
825 | - |
|
826 | - $parsed_response = $this->process_response($curl_handle, $this->response); |
|
827 | - |
|
828 | - curl_close($curl_handle); |
|
829 | - |
|
830 | - if ($parse) |
|
831 | - { |
|
832 | - return $parsed_response; |
|
833 | - } |
|
834 | - |
|
835 | - return $this->response; |
|
836 | - } |
|
837 | - |
|
838 | - /** |
|
839 | - * Sends the request using <php:curl_multi_exec()>, enabling parallel requests. Uses the "rolling" method. |
|
840 | - * |
|
841 | - * @param array $handles (Required) An indexed array of cURL handles to process simultaneously. |
|
842 | - * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul> |
|
843 | - * <li><code>callback</code> - <code>string|array</code> - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the <code>[0]</code> index is the class and the <code>[1]</code> index is the method name.</li> |
|
844 | - * <li><code>limit</code> - <code>integer</code> - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.</li></ul> |
|
845 | - * @return array Post-processed cURL responses. |
|
846 | - */ |
|
847 | - public function send_multi_request($handles, $opt = null) |
|
848 | - { |
|
849 | - set_time_limit(0); |
|
850 | - |
|
851 | - // Skip everything if there are no handles to process. |
|
852 | - if (count($handles) === 0) return array(); |
|
853 | - |
|
854 | - if (!$opt) $opt = array(); |
|
855 | - |
|
856 | - // Initialize any missing options |
|
857 | - $limit = isset($opt['limit']) ? $opt['limit'] : -1; |
|
858 | - |
|
859 | - // Initialize |
|
860 | - $handle_list = $handles; |
|
861 | - $http = new $this->request_class(); |
|
862 | - $multi_handle = curl_multi_init(); |
|
863 | - $handles_post = array(); |
|
864 | - $added = count($handles); |
|
865 | - $last_handle = null; |
|
866 | - $count = 0; |
|
867 | - $i = 0; |
|
868 | - |
|
869 | - // Loop through the cURL handles and add as many as it set by the limit parameter. |
|
870 | - while ($i < $added) |
|
871 | - { |
|
872 | - if ($limit > 0 && $i >= $limit) break; |
|
873 | - curl_multi_add_handle($multi_handle, array_shift($handles)); |
|
874 | - $i++; |
|
875 | - } |
|
876 | - |
|
877 | - do |
|
878 | - { |
|
879 | - $active = false; |
|
880 | - |
|
881 | - // Start executing and wait for a response. |
|
882 | - while (($status = curl_multi_exec($multi_handle, $active)) === CURLM_CALL_MULTI_PERFORM) |
|
883 | - { |
|
884 | - // Start looking for possible responses immediately when we have to add more handles |
|
885 | - if (count($handles) > 0) break; |
|
886 | - } |
|
887 | - |
|
888 | - // Figure out which requests finished. |
|
889 | - $to_process = array(); |
|
890 | - |
|
891 | - while ($done = curl_multi_info_read($multi_handle)) |
|
892 | - { |
|
893 | - // Since curl_errno() isn't reliable for handles that were in multirequests, we check the 'result' of the info read, which contains the curl error number, (listed here http://curl.haxx.se/libcurl/c/libcurl-errors.html ) |
|
894 | - if ($done['result'] > 0) |
|
895 | - { |
|
896 | - throw new RequestCore_Exception('cURL resource: ' . (string) $done['handle'] . '; cURL error: ' . curl_error($done['handle']) . ' (' . $done['result'] . ')'); |
|
897 | - } |
|
898 | - |
|
899 | - // Because curl_multi_info_read() might return more than one message about a request, we check to see if this request is already in our array of completed requests |
|
900 | - elseif (!isset($to_process[(int) $done['handle']])) |
|
901 | - { |
|
902 | - $to_process[(int) $done['handle']] = $done; |
|
903 | - } |
|
904 | - } |
|
905 | - |
|
906 | - // Actually deal with the request |
|
907 | - foreach ($to_process as $pkey => $done) |
|
908 | - { |
|
909 | - $response = $http->process_response($done['handle'], curl_multi_getcontent($done['handle'])); |
|
910 | - $key = array_search($done['handle'], $handle_list, true); |
|
911 | - $handles_post[$key] = $response; |
|
912 | - |
|
913 | - if (count($handles) > 0) |
|
914 | - { |
|
915 | - curl_multi_add_handle($multi_handle, array_shift($handles)); |
|
916 | - } |
|
917 | - |
|
918 | - curl_multi_remove_handle($multi_handle, $done['handle']); |
|
919 | - curl_close($done['handle']); |
|
920 | - } |
|
921 | - } |
|
922 | - while ($active || count($handles_post) < $added); |
|
923 | - |
|
924 | - curl_multi_close($multi_handle); |
|
925 | - |
|
926 | - ksort($handles_post, SORT_NUMERIC); |
|
927 | - return $handles_post; |
|
928 | - } |
|
929 | - |
|
930 | - |
|
931 | - /*%******************************************************************************************%*/ |
|
932 | - // RESPONSE METHODS |
|
933 | - |
|
934 | - /** |
|
935 | - * Get the HTTP response headers from the request. |
|
936 | - * |
|
937 | - * @param string $header (Optional) A specific header value to return. Defaults to all headers. |
|
938 | - * @return string|array All or selected header values. |
|
939 | - */ |
|
940 | - public function get_response_header($header = null) |
|
941 | - { |
|
942 | - if ($header) |
|
943 | - { |
|
944 | - return $this->response_headers[strtolower($header)]; |
|
945 | - } |
|
946 | - return $this->response_headers; |
|
947 | - } |
|
948 | - |
|
949 | - /** |
|
950 | - * Get the HTTP response body from the request. |
|
951 | - * |
|
952 | - * @return string The response body. |
|
953 | - */ |
|
954 | - public function get_response_body() |
|
955 | - { |
|
956 | - return $this->response_body; |
|
957 | - } |
|
958 | - |
|
959 | - /** |
|
960 | - * Get the HTTP response code from the request. |
|
961 | - * |
|
962 | - * @return string The HTTP response code. |
|
963 | - */ |
|
964 | - public function get_response_code() |
|
965 | - { |
|
966 | - return $this->response_code; |
|
967 | - } |
|
16 | + /** |
|
17 | + * The URL being requested. |
|
18 | + */ |
|
19 | + public $request_url; |
|
20 | + |
|
21 | + /** |
|
22 | + * The headers being sent in the request. |
|
23 | + */ |
|
24 | + public $request_headers; |
|
25 | + |
|
26 | + /** |
|
27 | + * The body being sent in the request. |
|
28 | + */ |
|
29 | + public $request_body; |
|
30 | + |
|
31 | + /** |
|
32 | + * The response returned by the request. |
|
33 | + */ |
|
34 | + public $response; |
|
35 | + |
|
36 | + /** |
|
37 | + * The headers returned by the request. |
|
38 | + */ |
|
39 | + public $response_headers; |
|
40 | + |
|
41 | + /** |
|
42 | + * The body returned by the request. |
|
43 | + */ |
|
44 | + public $response_body; |
|
45 | + |
|
46 | + /** |
|
47 | + * The HTTP status code returned by the request. |
|
48 | + */ |
|
49 | + public $response_code; |
|
50 | + |
|
51 | + /** |
|
52 | + * Additional response data. |
|
53 | + */ |
|
54 | + public $response_info; |
|
55 | + |
|
56 | + /** |
|
57 | + * The handle for the cURL object. |
|
58 | + */ |
|
59 | + public $curl_handle; |
|
60 | + |
|
61 | + /** |
|
62 | + * The method by which the request is being made. |
|
63 | + */ |
|
64 | + public $method; |
|
65 | + |
|
66 | + /** |
|
67 | + * Stores the proxy settings to use for the request. |
|
68 | + */ |
|
69 | + public $proxy = null; |
|
70 | + |
|
71 | + /** |
|
72 | + * The username to use for the request. |
|
73 | + */ |
|
74 | + public $username = null; |
|
75 | + |
|
76 | + /** |
|
77 | + * The password to use for the request. |
|
78 | + */ |
|
79 | + public $password = null; |
|
80 | + |
|
81 | + /** |
|
82 | + * Custom CURLOPT settings. |
|
83 | + */ |
|
84 | + public $curlopts = null; |
|
85 | + |
|
86 | + /** |
|
87 | + * The state of debug mode. |
|
88 | + */ |
|
89 | + public $debug_mode = false; |
|
90 | + |
|
91 | + /** |
|
92 | + * The default class to use for HTTP Requests (defaults to <RequestCore>). |
|
93 | + */ |
|
94 | + public $request_class = 'RequestCore'; |
|
95 | + |
|
96 | + /** |
|
97 | + * The default class to use for HTTP Responses (defaults to <ResponseCore>). |
|
98 | + */ |
|
99 | + public $response_class = 'ResponseCore'; |
|
100 | + |
|
101 | + /** |
|
102 | + * Default useragent string to use. |
|
103 | + */ |
|
104 | + public $useragent = 'RequestCore/1.4.3'; |
|
105 | + |
|
106 | + /** |
|
107 | + * File to read from while streaming up. |
|
108 | + */ |
|
109 | + public $read_file = null; |
|
110 | + |
|
111 | + /** |
|
112 | + * The resource to read from while streaming up. |
|
113 | + */ |
|
114 | + public $read_stream = null; |
|
115 | + |
|
116 | + /** |
|
117 | + * The size of the stream to read from. |
|
118 | + */ |
|
119 | + public $read_stream_size = null; |
|
120 | + |
|
121 | + /** |
|
122 | + * The length already read from the stream. |
|
123 | + */ |
|
124 | + public $read_stream_read = 0; |
|
125 | + |
|
126 | + /** |
|
127 | + * File to write to while streaming down. |
|
128 | + */ |
|
129 | + public $write_file = null; |
|
130 | + |
|
131 | + /** |
|
132 | + * The resource to write to while streaming down. |
|
133 | + */ |
|
134 | + public $write_stream = null; |
|
135 | + |
|
136 | + /** |
|
137 | + * Stores the intended starting seek position. |
|
138 | + */ |
|
139 | + public $seek_position = null; |
|
140 | + |
|
141 | + /** |
|
142 | + * The location of the cacert.pem file to use. |
|
143 | + */ |
|
144 | + public $cacert_location = false; |
|
145 | + |
|
146 | + /** |
|
147 | + * The state of SSL certificate verification. |
|
148 | + */ |
|
149 | + public $ssl_verification = true; |
|
150 | + |
|
151 | + /** |
|
152 | + * The user-defined callback function to call when a stream is read from. |
|
153 | + */ |
|
154 | + public $registered_streaming_read_callback = null; |
|
155 | + |
|
156 | + /** |
|
157 | + * The user-defined callback function to call when a stream is written to. |
|
158 | + */ |
|
159 | + public $registered_streaming_write_callback = null; |
|
160 | + |
|
161 | + /*%******************************************************************************************%*/ |
|
162 | + // CONSTANTS |
|
163 | + |
|
164 | + /** |
|
165 | + * GET HTTP Method |
|
166 | + */ |
|
167 | + const HTTP_GET = 'GET'; |
|
168 | + |
|
169 | + /** |
|
170 | + * POST HTTP Method |
|
171 | + */ |
|
172 | + const HTTP_POST = 'POST'; |
|
173 | + |
|
174 | + /** |
|
175 | + * PUT HTTP Method |
|
176 | + */ |
|
177 | + const HTTP_PUT = 'PUT'; |
|
178 | + |
|
179 | + /** |
|
180 | + * DELETE HTTP Method |
|
181 | + */ |
|
182 | + const HTTP_DELETE = 'DELETE'; |
|
183 | + |
|
184 | + /** |
|
185 | + * HEAD HTTP Method |
|
186 | + */ |
|
187 | + const HTTP_HEAD = 'HEAD'; |
|
188 | + |
|
189 | + |
|
190 | + /*%******************************************************************************************%*/ |
|
191 | + // CONSTRUCTOR/DESTRUCTOR |
|
192 | + |
|
193 | + /** |
|
194 | + * Constructs a new instance of this class. |
|
195 | + * |
|
196 | + * @param string $url (Optional) The URL to request or service endpoint to query. |
|
197 | + * @param string $proxy (Optional) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port` |
|
198 | + * @param array $helpers (Optional) An associative array of classnames to use for request, and response functionality. Gets passed in automatically by the calling class. |
|
199 | + * @return $this A reference to the current instance. |
|
200 | + */ |
|
201 | + public function __construct($url = null, $proxy = null, $helpers = null) |
|
202 | + { |
|
203 | + // Set some default values. |
|
204 | + $this->request_url = $url; |
|
205 | + $this->method = self::HTTP_GET; |
|
206 | + $this->request_headers = array(); |
|
207 | + $this->request_body = ''; |
|
208 | + |
|
209 | + // Set a new Request class if one was set. |
|
210 | + if (isset($helpers['request']) && !empty($helpers['request'])) |
|
211 | + { |
|
212 | + $this->request_class = $helpers['request']; |
|
213 | + } |
|
214 | + |
|
215 | + // Set a new Request class if one was set. |
|
216 | + if (isset($helpers['response']) && !empty($helpers['response'])) |
|
217 | + { |
|
218 | + $this->response_class = $helpers['response']; |
|
219 | + } |
|
220 | + |
|
221 | + if ($proxy) |
|
222 | + { |
|
223 | + $this->set_proxy($proxy); |
|
224 | + } |
|
225 | + |
|
226 | + return $this; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Destructs the instance. Closes opened file handles. |
|
231 | + * |
|
232 | + * @return $this A reference to the current instance. |
|
233 | + */ |
|
234 | + public function __destruct() |
|
235 | + { |
|
236 | + if (isset($this->read_file) && isset($this->read_stream)) |
|
237 | + { |
|
238 | + fclose($this->read_stream); |
|
239 | + } |
|
240 | + |
|
241 | + if (isset($this->write_file) && isset($this->write_stream)) |
|
242 | + { |
|
243 | + fclose($this->write_stream); |
|
244 | + } |
|
245 | + |
|
246 | + return $this; |
|
247 | + } |
|
248 | + |
|
249 | + |
|
250 | + /*%******************************************************************************************%*/ |
|
251 | + // REQUEST METHODS |
|
252 | + |
|
253 | + /** |
|
254 | + * Sets the credentials to use for authentication. |
|
255 | + * |
|
256 | + * @param string $user (Required) The username to authenticate with. |
|
257 | + * @param string $pass (Required) The password to authenticate with. |
|
258 | + * @return $this A reference to the current instance. |
|
259 | + */ |
|
260 | + public function set_credentials($user, $pass) |
|
261 | + { |
|
262 | + $this->username = $user; |
|
263 | + $this->password = $pass; |
|
264 | + return $this; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Adds a custom HTTP header to the cURL request. |
|
269 | + * |
|
270 | + * @param string $key (Required) The custom HTTP header to set. |
|
271 | + * @param mixed $value (Required) The value to assign to the custom HTTP header. |
|
272 | + * @return $this A reference to the current instance. |
|
273 | + */ |
|
274 | + public function add_header($key, $value) |
|
275 | + { |
|
276 | + $this->request_headers[$key] = $value; |
|
277 | + return $this; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Removes an HTTP header from the cURL request. |
|
282 | + * |
|
283 | + * @param string $key (Required) The custom HTTP header to set. |
|
284 | + * @return $this A reference to the current instance. |
|
285 | + */ |
|
286 | + public function remove_header($key) |
|
287 | + { |
|
288 | + if (isset($this->request_headers[$key])) |
|
289 | + { |
|
290 | + unset($this->request_headers[$key]); |
|
291 | + } |
|
292 | + return $this; |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Set the method type for the request. |
|
297 | + * |
|
298 | + * @param string $method (Required) One of the following constants: <HTTP_GET>, <HTTP_POST>, <HTTP_PUT>, <HTTP_HEAD>, <HTTP_DELETE>. |
|
299 | + * @return $this A reference to the current instance. |
|
300 | + */ |
|
301 | + public function set_method($method) |
|
302 | + { |
|
303 | + $this->method = strtoupper($method); |
|
304 | + return $this; |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Sets a custom useragent string for the class. |
|
309 | + * |
|
310 | + * @param string $ua (Required) The useragent string to use. |
|
311 | + * @return $this A reference to the current instance. |
|
312 | + */ |
|
313 | + public function set_useragent($ua) |
|
314 | + { |
|
315 | + $this->useragent = $ua; |
|
316 | + return $this; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Set the body to send in the request. |
|
321 | + * |
|
322 | + * @param string $body (Required) The textual content to send along in the body of the request. |
|
323 | + * @return $this A reference to the current instance. |
|
324 | + */ |
|
325 | + public function set_body($body) |
|
326 | + { |
|
327 | + $this->request_body = $body; |
|
328 | + return $this; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Set the URL to make the request to. |
|
333 | + * |
|
334 | + * @param string $url (Required) The URL to make the request to. |
|
335 | + * @return $this A reference to the current instance. |
|
336 | + */ |
|
337 | + public function set_request_url($url) |
|
338 | + { |
|
339 | + $this->request_url = $url; |
|
340 | + return $this; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Set additional CURLOPT settings. These will merge with the default settings, and override if |
|
345 | + * there is a duplicate. |
|
346 | + * |
|
347 | + * @param array $curlopts (Optional) A set of key-value pairs that set `CURLOPT` options. These will merge with the existing CURLOPTs, and ones passed here will override the defaults. Keys should be the `CURLOPT_*` constants, not strings. |
|
348 | + * @return $this A reference to the current instance. |
|
349 | + */ |
|
350 | + public function set_curlopts($curlopts) |
|
351 | + { |
|
352 | + $this->curlopts = $curlopts; |
|
353 | + return $this; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Sets the length in bytes to read from the stream while streaming up. |
|
358 | + * |
|
359 | + * @param integer $size (Required) The length in bytes to read from the stream. |
|
360 | + * @return $this A reference to the current instance. |
|
361 | + */ |
|
362 | + public function set_read_stream_size($size) |
|
363 | + { |
|
364 | + $this->read_stream_size = $size; |
|
365 | + |
|
366 | + return $this; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Sets the resource to read from while streaming up. Reads the stream from its current position until |
|
371 | + * EOF or `$size` bytes have been read. If `$size` is not given it will be determined by <php:fstat()> and |
|
372 | + * <php:ftell()>. |
|
373 | + * |
|
374 | + * @param resource $resource (Required) The readable resource to read from. |
|
375 | + * @param integer $size (Optional) The size of the stream to read. |
|
376 | + * @return $this A reference to the current instance. |
|
377 | + */ |
|
378 | + public function set_read_stream($resource, $size = null) |
|
379 | + { |
|
380 | + if (!isset($size) || $size < 0) |
|
381 | + { |
|
382 | + $stats = fstat($resource); |
|
383 | + |
|
384 | + if ($stats && $stats['size'] >= 0) |
|
385 | + { |
|
386 | + $position = ftell($resource); |
|
387 | + |
|
388 | + if ($position !== false && $position >= 0) |
|
389 | + { |
|
390 | + $size = $stats['size'] - $position; |
|
391 | + } |
|
392 | + } |
|
393 | + } |
|
394 | + |
|
395 | + $this->read_stream = $resource; |
|
396 | + |
|
397 | + return $this->set_read_stream_size($size); |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Sets the file to read from while streaming up. |
|
402 | + * |
|
403 | + * @param string $location (Required) The readable location to read from. |
|
404 | + * @return $this A reference to the current instance. |
|
405 | + */ |
|
406 | + public function set_read_file($location) |
|
407 | + { |
|
408 | + $this->read_file = $location; |
|
409 | + $read_file_handle = fopen($location, 'r'); |
|
410 | + |
|
411 | + return $this->set_read_stream($read_file_handle); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Sets the resource to write to while streaming down. |
|
416 | + * |
|
417 | + * @param resource $resource (Required) The writeable resource to write to. |
|
418 | + * @return $this A reference to the current instance. |
|
419 | + */ |
|
420 | + public function set_write_stream($resource) |
|
421 | + { |
|
422 | + $this->write_stream = $resource; |
|
423 | + |
|
424 | + return $this; |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Sets the file to write to while streaming down. |
|
429 | + * |
|
430 | + * @param string $location (Required) The writeable location to write to. |
|
431 | + * @return $this A reference to the current instance. |
|
432 | + */ |
|
433 | + public function set_write_file($location) |
|
434 | + { |
|
435 | + $this->write_file = $location; |
|
436 | + $write_file_handle = fopen($location, 'w'); |
|
437 | + |
|
438 | + return $this->set_write_stream($write_file_handle); |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Set the proxy to use for making requests. |
|
443 | + * |
|
444 | + * @param string $proxy (Required) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port` |
|
445 | + * @return $this A reference to the current instance. |
|
446 | + */ |
|
447 | + public function set_proxy($proxy) |
|
448 | + { |
|
449 | + $proxy = parse_url($proxy); |
|
450 | + $proxy['user'] = isset($proxy['user']) ? $proxy['user'] : null; |
|
451 | + $proxy['pass'] = isset($proxy['pass']) ? $proxy['pass'] : null; |
|
452 | + $proxy['port'] = isset($proxy['port']) ? $proxy['port'] : null; |
|
453 | + $this->proxy = $proxy; |
|
454 | + return $this; |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Set the intended starting seek position. |
|
459 | + * |
|
460 | + * @param integer $position (Required) The byte-position of the stream to begin reading from. |
|
461 | + * @return $this A reference to the current instance. |
|
462 | + */ |
|
463 | + public function set_seek_position($position) |
|
464 | + { |
|
465 | + $this->seek_position = isset($position) ? (integer) $position : null; |
|
466 | + |
|
467 | + return $this; |
|
468 | + } |
|
469 | + |
|
470 | + /** |
|
471 | + * Register a callback function to execute whenever a data stream is read from using |
|
472 | + * <CFRequest::streaming_read_callback()>. |
|
473 | + * |
|
474 | + * The user-defined callback function should accept three arguments: |
|
475 | + * |
|
476 | + * <ul> |
|
477 | + * <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li> |
|
478 | + * <li><code>$file_handle</code> - <code>resource</code> - Required - The file handle resource that represents the file on the local file system.</li> |
|
479 | + * <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li> |
|
480 | + * </ul> |
|
481 | + * |
|
482 | + * @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul> |
|
483 | + * <li>The name of a global function to execute, passed as a string.</li> |
|
484 | + * <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li> |
|
485 | + * <li>An anonymous function (PHP 5.3+).</li></ul> |
|
486 | + * @return $this A reference to the current instance. |
|
487 | + */ |
|
488 | + public function register_streaming_read_callback($callback) |
|
489 | + { |
|
490 | + $this->registered_streaming_read_callback = $callback; |
|
491 | + |
|
492 | + return $this; |
|
493 | + } |
|
494 | + |
|
495 | + /** |
|
496 | + * Register a callback function to execute whenever a data stream is written to using |
|
497 | + * <CFRequest::streaming_write_callback()>. |
|
498 | + * |
|
499 | + * The user-defined callback function should accept two arguments: |
|
500 | + * |
|
501 | + * <ul> |
|
502 | + * <li><code>$curl_handle</code> - <code>resource</code> - Required - The cURL handle resource that represents the in-progress transfer.</li> |
|
503 | + * <li><code>$length</code> - <code>integer</code> - Required - The length in kilobytes of the data chunk that was transferred.</li> |
|
504 | + * </ul> |
|
505 | + * |
|
506 | + * @param string|array|function $callback (Required) The callback function is called by <php:call_user_func()>, so you can pass the following values: <ul> |
|
507 | + * <li>The name of a global function to execute, passed as a string.</li> |
|
508 | + * <li>A method to execute, passed as <code>array('ClassName', 'MethodName')</code>.</li> |
|
509 | + * <li>An anonymous function (PHP 5.3+).</li></ul> |
|
510 | + * @return $this A reference to the current instance. |
|
511 | + */ |
|
512 | + public function register_streaming_write_callback($callback) |
|
513 | + { |
|
514 | + $this->registered_streaming_write_callback = $callback; |
|
515 | + |
|
516 | + return $this; |
|
517 | + } |
|
518 | + |
|
519 | + |
|
520 | + /*%******************************************************************************************%*/ |
|
521 | + // PREPARE, SEND, AND PROCESS REQUEST |
|
522 | + |
|
523 | + /** |
|
524 | + * A callback function that is invoked by cURL for streaming up. |
|
525 | + * |
|
526 | + * @param resource $curl_handle (Required) The cURL handle for the request. |
|
527 | + * @param resource $file_handle (Required) The open file handle resource. |
|
528 | + * @param integer $length (Required) The maximum number of bytes to read. |
|
529 | + * @return binary Binary data from a stream. |
|
530 | + */ |
|
531 | + public function streaming_read_callback($curl_handle, $file_handle, $length) |
|
532 | + { |
|
533 | + // Execute callback function |
|
534 | + if ($this->registered_streaming_read_callback) |
|
535 | + { |
|
536 | + return $this->registered_streaming_read_callback->streaming_read_callback($curl_handle, $file_handle, $length,$this->read_stream,$this->seek_position); |
|
537 | + } |
|
538 | + // Once we've sent as much as we're supposed to send... |
|
539 | + if ($this->read_stream_read >= $this->read_stream_size) |
|
540 | + { |
|
541 | + // Send EOF |
|
542 | + return ''; |
|
543 | + } |
|
544 | + |
|
545 | + // If we're at the beginning of an upload and need to seek... |
|
546 | + if ($this->read_stream_read == 0 && isset($this->seek_position) && $this->seek_position !== ftell($this->read_stream)) |
|
547 | + { |
|
548 | + if (fseek($this->read_stream, $this->seek_position) !== 0) |
|
549 | + { |
|
550 | + throw new RequestCore_Exception('The stream does not support seeking and is either not at the requested position or the position is unknown.'); |
|
551 | + } |
|
552 | + } |
|
553 | + |
|
554 | + $read = fread($this->read_stream, min($this->read_stream_size - $this->read_stream_read, $length)); // Remaining upload data or cURL's requested chunk size |
|
555 | + $this->read_stream_read += strlen($read); |
|
556 | + |
|
557 | + $out = $read === false ? '' : $read; |
|
558 | + |
|
559 | + return $out; |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * A callback function that is invoked by cURL for streaming down. |
|
564 | + * |
|
565 | + * @param resource $curl_handle (Required) The cURL handle for the request. |
|
566 | + * @param binary $data (Required) The data to write. |
|
567 | + * @return integer The number of bytes written. |
|
568 | + */ |
|
569 | + public function streaming_write_callback($curl_handle, $data) |
|
570 | + { |
|
571 | + if ($this->registered_streaming_write_callback){ |
|
572 | + return $this->registered_streaming_write_callback->streaming_write_callback($curl_handle,$data,$this->write_stream); |
|
573 | + } |
|
574 | + $length = strlen($data); |
|
575 | + $written_total = 0; |
|
576 | + $written_last = 0; |
|
577 | + |
|
578 | + while ($written_total < $length) |
|
579 | + { |
|
580 | + $written_last = fwrite($this->write_stream, substr($data, $written_total)); |
|
581 | + |
|
582 | + if ($written_last === false) |
|
583 | + { |
|
584 | + return $written_total; |
|
585 | + } |
|
586 | + |
|
587 | + $written_total += $written_last; |
|
588 | + } |
|
589 | + |
|
590 | + return $written_total; |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Prepares and adds the details of the cURL request. This can be passed along to a <php:curl_multi_exec()> |
|
595 | + * function. |
|
596 | + * |
|
597 | + * @return resource The handle for the cURL object. |
|
598 | + */ |
|
599 | + public function prep_request() |
|
600 | + { |
|
601 | + $curl_handle = curl_init(); |
|
602 | + |
|
603 | + // Set default options. |
|
604 | + curl_setopt($curl_handle, CURLOPT_URL, $this->request_url); |
|
605 | + curl_setopt($curl_handle, CURLOPT_FILETIME, true); |
|
606 | + curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, false); |
|
607 | + //为了兼容PHP 5.6,PHP 5.6把 CURLOPT_CLOSEPOLICY 这个变量删除了 |
|
608 | + //curl_setopt($curl_handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED); |
|
609 | + curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5); |
|
610 | + curl_setopt($curl_handle, CURLOPT_HEADER, true); |
|
611 | + curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); |
|
612 | + curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000); |
|
613 | + curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120); |
|
614 | + curl_setopt($curl_handle, CURLOPT_NOSIGNAL, true); |
|
615 | + curl_setopt($curl_handle, CURLOPT_REFERER, $this->request_url); |
|
616 | + curl_setopt($curl_handle, CURLOPT_USERAGENT, $this->useragent); |
|
617 | + curl_setopt($curl_handle, CURLOPT_READFUNCTION, array($this, 'streaming_read_callback')); |
|
618 | + |
|
619 | + // Verification of the SSL cert |
|
620 | + if ($this->ssl_verification) |
|
621 | + { |
|
622 | + curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, true); |
|
623 | + curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 2); |
|
624 | + } |
|
625 | + else |
|
626 | + { |
|
627 | + curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false); |
|
628 | + curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false); |
|
629 | + } |
|
630 | + |
|
631 | + // chmod the file as 0755 |
|
632 | + if ($this->cacert_location === true) |
|
633 | + { |
|
634 | + curl_setopt($curl_handle, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem'); |
|
635 | + } |
|
636 | + elseif (is_string($this->cacert_location)) |
|
637 | + { |
|
638 | + curl_setopt($curl_handle, CURLOPT_CAINFO, $this->cacert_location); |
|
639 | + } |
|
640 | + |
|
641 | + // Debug mode |
|
642 | + if ($this->debug_mode) |
|
643 | + { |
|
644 | + curl_setopt($curl_handle, CURLOPT_VERBOSE, true); |
|
645 | + } |
|
646 | + |
|
647 | + // Handle open_basedir & safe mode |
|
648 | + if (!ini_get('safe_mode') && !ini_get('open_basedir')) |
|
649 | + { |
|
650 | + curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, FALSE); |
|
651 | + } |
|
652 | + |
|
653 | + // Enable a proxy connection if requested. |
|
654 | + if ($this->proxy) |
|
655 | + { |
|
656 | + curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, true); |
|
657 | + |
|
658 | + $host = $this->proxy['host']; |
|
659 | + $host .= ($this->proxy['port']) ? ':' . $this->proxy['port'] : ''; |
|
660 | + curl_setopt($curl_handle, CURLOPT_PROXY, $host); |
|
661 | + |
|
662 | + if (isset($this->proxy['user']) && isset($this->proxy['pass'])) |
|
663 | + { |
|
664 | + curl_setopt($curl_handle, CURLOPT_PROXYUSERPWD, $this->proxy['user'] . ':' . $this->proxy['pass']); |
|
665 | + } |
|
666 | + } |
|
667 | + |
|
668 | + // Set credentials for HTTP Basic/Digest Authentication. |
|
669 | + if ($this->username && $this->password) |
|
670 | + { |
|
671 | + curl_setopt($curl_handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
|
672 | + curl_setopt($curl_handle, CURLOPT_USERPWD, $this->username . ':' . $this->password); |
|
673 | + } |
|
674 | + |
|
675 | + // Handle the encoding if we can. |
|
676 | + if (extension_loaded('zlib')) |
|
677 | + { |
|
678 | + curl_setopt($curl_handle, CURLOPT_ENCODING, ''); |
|
679 | + } |
|
680 | + |
|
681 | + // Process custom headers |
|
682 | + if (isset($this->request_headers) && count($this->request_headers)) |
|
683 | + { |
|
684 | + $temp_headers = array(); |
|
685 | + |
|
686 | + foreach ($this->request_headers as $k => $v) |
|
687 | + { |
|
688 | + $temp_headers[] = $k . ': ' . $v; |
|
689 | + } |
|
690 | + |
|
691 | + curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $temp_headers); |
|
692 | + } |
|
693 | + |
|
694 | + switch ($this->method) |
|
695 | + { |
|
696 | + case self::HTTP_PUT: |
|
697 | + //unset($this->read_stream); |
|
698 | + curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT'); |
|
699 | + if (isset($this->read_stream)) |
|
700 | + { |
|
701 | + if (!isset($this->read_stream_size) || $this->read_stream_size < 0) |
|
702 | + { |
|
703 | + throw new RequestCore_Exception('The stream size for the streaming upload cannot be determined.'); |
|
704 | + } |
|
705 | + |
|
706 | + curl_setopt($curl_handle, CURLOPT_INFILESIZE, $this->read_stream_size); |
|
707 | + curl_setopt($curl_handle, CURLOPT_UPLOAD, true); |
|
708 | + } |
|
709 | + else |
|
710 | + { |
|
711 | + curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body); |
|
712 | + } |
|
713 | + break; |
|
714 | + |
|
715 | + case self::HTTP_POST: |
|
716 | + curl_setopt($curl_handle, CURLOPT_POST, true); |
|
717 | + curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body); |
|
718 | + break; |
|
719 | + |
|
720 | + case self::HTTP_HEAD: |
|
721 | + curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, self::HTTP_HEAD); |
|
722 | + curl_setopt($curl_handle, CURLOPT_NOBODY, 1); |
|
723 | + break; |
|
724 | + |
|
725 | + default: // Assumed GET |
|
726 | + curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, $this->method); |
|
727 | + if (isset($this->write_stream)) |
|
728 | + { |
|
729 | + curl_setopt($curl_handle, CURLOPT_WRITEFUNCTION, array($this, 'streaming_write_callback')); |
|
730 | + curl_setopt($curl_handle, CURLOPT_HEADER, false); |
|
731 | + } |
|
732 | + else |
|
733 | + { |
|
734 | + curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $this->request_body); |
|
735 | + } |
|
736 | + break; |
|
737 | + } |
|
738 | + |
|
739 | + // Merge in the CURLOPTs |
|
740 | + if (isset($this->curlopts) && sizeof($this->curlopts) > 0) |
|
741 | + { |
|
742 | + foreach ($this->curlopts as $k => $v) |
|
743 | + { |
|
744 | + curl_setopt($curl_handle, $k, $v); |
|
745 | + } |
|
746 | + } |
|
747 | + |
|
748 | + return $curl_handle; |
|
749 | + } |
|
750 | + |
|
751 | + /** |
|
752 | + * Take the post-processed cURL data and break it down into useful header/body/info chunks. Uses the |
|
753 | + * data stored in the `curl_handle` and `response` properties unless replacement data is passed in via |
|
754 | + * parameters. |
|
755 | + * |
|
756 | + * @param resource $curl_handle (Optional) The reference to the already executed cURL request. |
|
757 | + * @param string $response (Optional) The actual response content itself that needs to be parsed. |
|
758 | + * @return ResponseCore A <ResponseCore> object containing a parsed HTTP response. |
|
759 | + */ |
|
760 | + public function process_response($curl_handle = null, $response = null) |
|
761 | + { |
|
762 | + // Accept a custom one if it's passed. |
|
763 | + if ($curl_handle && $response) |
|
764 | + { |
|
765 | + $this->curl_handle = $curl_handle; |
|
766 | + $this->response = $response; |
|
767 | + } |
|
768 | + |
|
769 | + // As long as this came back as a valid resource... |
|
770 | + if (is_resource($this->curl_handle)) |
|
771 | + { |
|
772 | + // Determine what's what. |
|
773 | + $header_size = curl_getinfo($this->curl_handle, CURLINFO_HEADER_SIZE); |
|
774 | + $this->response_headers = substr($this->response, 0, $header_size); |
|
775 | + $this->response_body = substr($this->response, $header_size); |
|
776 | + $this->response_code = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE); |
|
777 | + $this->response_info = curl_getinfo($this->curl_handle); |
|
778 | + |
|
779 | + // Parse out the headers |
|
780 | + $this->response_headers = explode("\r\n\r\n", trim($this->response_headers)); |
|
781 | + $this->response_headers = array_pop($this->response_headers); |
|
782 | + $this->response_headers = explode("\r\n", $this->response_headers); |
|
783 | + array_shift($this->response_headers); |
|
784 | + |
|
785 | + // Loop through and split up the headers. |
|
786 | + $header_assoc = array(); |
|
787 | + foreach ($this->response_headers as $header) |
|
788 | + { |
|
789 | + $kv = explode(': ', $header); |
|
790 | + $header_assoc[strtolower($kv[0])] = isset($kv[1])?$kv[1]:''; |
|
791 | + } |
|
792 | + |
|
793 | + // Reset the headers to the appropriate property. |
|
794 | + $this->response_headers = $header_assoc; |
|
795 | + $this->response_headers['_info'] = $this->response_info; |
|
796 | + $this->response_headers['_info']['method'] = $this->method; |
|
797 | + |
|
798 | + if ($curl_handle && $response) |
|
799 | + { |
|
800 | + return new $this->response_class($this->response_headers, $this->response_body, $this->response_code, $this->curl_handle); |
|
801 | + } |
|
802 | + } |
|
803 | + |
|
804 | + // Return false |
|
805 | + return false; |
|
806 | + } |
|
807 | + |
|
808 | + /** |
|
809 | + * Sends the request, calling necessary utility functions to update built-in properties. |
|
810 | + * |
|
811 | + * @param boolean $parse (Optional) Whether to parse the response with ResponseCore or not. |
|
812 | + * @return string The resulting unparsed data from the request. |
|
813 | + */ |
|
814 | + public function send_request($parse = false) |
|
815 | + { |
|
816 | + set_time_limit(0); |
|
817 | + |
|
818 | + $curl_handle = $this->prep_request(); |
|
819 | + $this->response = curl_exec($curl_handle); |
|
820 | + |
|
821 | + if ($this->response === false) |
|
822 | + { |
|
823 | + throw new RequestCore_Exception('cURL resource: ' . (string) $curl_handle . '; cURL error: ' . curl_error($curl_handle) . ' (' . curl_errno($curl_handle) . ')'); |
|
824 | + } |
|
825 | + |
|
826 | + $parsed_response = $this->process_response($curl_handle, $this->response); |
|
827 | + |
|
828 | + curl_close($curl_handle); |
|
829 | + |
|
830 | + if ($parse) |
|
831 | + { |
|
832 | + return $parsed_response; |
|
833 | + } |
|
834 | + |
|
835 | + return $this->response; |
|
836 | + } |
|
837 | + |
|
838 | + /** |
|
839 | + * Sends the request using <php:curl_multi_exec()>, enabling parallel requests. Uses the "rolling" method. |
|
840 | + * |
|
841 | + * @param array $handles (Required) An indexed array of cURL handles to process simultaneously. |
|
842 | + * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul> |
|
843 | + * <li><code>callback</code> - <code>string|array</code> - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the <code>[0]</code> index is the class and the <code>[1]</code> index is the method name.</li> |
|
844 | + * <li><code>limit</code> - <code>integer</code> - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.</li></ul> |
|
845 | + * @return array Post-processed cURL responses. |
|
846 | + */ |
|
847 | + public function send_multi_request($handles, $opt = null) |
|
848 | + { |
|
849 | + set_time_limit(0); |
|
850 | + |
|
851 | + // Skip everything if there are no handles to process. |
|
852 | + if (count($handles) === 0) return array(); |
|
853 | + |
|
854 | + if (!$opt) $opt = array(); |
|
855 | + |
|
856 | + // Initialize any missing options |
|
857 | + $limit = isset($opt['limit']) ? $opt['limit'] : -1; |
|
858 | + |
|
859 | + // Initialize |
|
860 | + $handle_list = $handles; |
|
861 | + $http = new $this->request_class(); |
|
862 | + $multi_handle = curl_multi_init(); |
|
863 | + $handles_post = array(); |
|
864 | + $added = count($handles); |
|
865 | + $last_handle = null; |
|
866 | + $count = 0; |
|
867 | + $i = 0; |
|
868 | + |
|
869 | + // Loop through the cURL handles and add as many as it set by the limit parameter. |
|
870 | + while ($i < $added) |
|
871 | + { |
|
872 | + if ($limit > 0 && $i >= $limit) break; |
|
873 | + curl_multi_add_handle($multi_handle, array_shift($handles)); |
|
874 | + $i++; |
|
875 | + } |
|
876 | + |
|
877 | + do |
|
878 | + { |
|
879 | + $active = false; |
|
880 | + |
|
881 | + // Start executing and wait for a response. |
|
882 | + while (($status = curl_multi_exec($multi_handle, $active)) === CURLM_CALL_MULTI_PERFORM) |
|
883 | + { |
|
884 | + // Start looking for possible responses immediately when we have to add more handles |
|
885 | + if (count($handles) > 0) break; |
|
886 | + } |
|
887 | + |
|
888 | + // Figure out which requests finished. |
|
889 | + $to_process = array(); |
|
890 | + |
|
891 | + while ($done = curl_multi_info_read($multi_handle)) |
|
892 | + { |
|
893 | + // Since curl_errno() isn't reliable for handles that were in multirequests, we check the 'result' of the info read, which contains the curl error number, (listed here http://curl.haxx.se/libcurl/c/libcurl-errors.html ) |
|
894 | + if ($done['result'] > 0) |
|
895 | + { |
|
896 | + throw new RequestCore_Exception('cURL resource: ' . (string) $done['handle'] . '; cURL error: ' . curl_error($done['handle']) . ' (' . $done['result'] . ')'); |
|
897 | + } |
|
898 | + |
|
899 | + // Because curl_multi_info_read() might return more than one message about a request, we check to see if this request is already in our array of completed requests |
|
900 | + elseif (!isset($to_process[(int) $done['handle']])) |
|
901 | + { |
|
902 | + $to_process[(int) $done['handle']] = $done; |
|
903 | + } |
|
904 | + } |
|
905 | + |
|
906 | + // Actually deal with the request |
|
907 | + foreach ($to_process as $pkey => $done) |
|
908 | + { |
|
909 | + $response = $http->process_response($done['handle'], curl_multi_getcontent($done['handle'])); |
|
910 | + $key = array_search($done['handle'], $handle_list, true); |
|
911 | + $handles_post[$key] = $response; |
|
912 | + |
|
913 | + if (count($handles) > 0) |
|
914 | + { |
|
915 | + curl_multi_add_handle($multi_handle, array_shift($handles)); |
|
916 | + } |
|
917 | + |
|
918 | + curl_multi_remove_handle($multi_handle, $done['handle']); |
|
919 | + curl_close($done['handle']); |
|
920 | + } |
|
921 | + } |
|
922 | + while ($active || count($handles_post) < $added); |
|
923 | + |
|
924 | + curl_multi_close($multi_handle); |
|
925 | + |
|
926 | + ksort($handles_post, SORT_NUMERIC); |
|
927 | + return $handles_post; |
|
928 | + } |
|
929 | + |
|
930 | + |
|
931 | + /*%******************************************************************************************%*/ |
|
932 | + // RESPONSE METHODS |
|
933 | + |
|
934 | + /** |
|
935 | + * Get the HTTP response headers from the request. |
|
936 | + * |
|
937 | + * @param string $header (Optional) A specific header value to return. Defaults to all headers. |
|
938 | + * @return string|array All or selected header values. |
|
939 | + */ |
|
940 | + public function get_response_header($header = null) |
|
941 | + { |
|
942 | + if ($header) |
|
943 | + { |
|
944 | + return $this->response_headers[strtolower($header)]; |
|
945 | + } |
|
946 | + return $this->response_headers; |
|
947 | + } |
|
948 | + |
|
949 | + /** |
|
950 | + * Get the HTTP response body from the request. |
|
951 | + * |
|
952 | + * @return string The response body. |
|
953 | + */ |
|
954 | + public function get_response_body() |
|
955 | + { |
|
956 | + return $this->response_body; |
|
957 | + } |
|
958 | + |
|
959 | + /** |
|
960 | + * Get the HTTP response code from the request. |
|
961 | + * |
|
962 | + * @return string The HTTP response code. |
|
963 | + */ |
|
964 | + public function get_response_code() |
|
965 | + { |
|
966 | + return $this->response_code; |
|
967 | + } |
|
968 | 968 | } |
969 | 969 | |
970 | 970 | |
@@ -973,53 +973,53 @@ discard block |
||
973 | 973 | */ |
974 | 974 | class ResponseCore |
975 | 975 | { |
976 | - /** |
|
977 | - * Stores the HTTP header information. |
|
978 | - */ |
|
979 | - public $header; |
|
980 | - |
|
981 | - /** |
|
982 | - * Stores the SimpleXML response. |
|
983 | - */ |
|
984 | - public $body; |
|
985 | - |
|
986 | - /** |
|
987 | - * Stores the HTTP response code. |
|
988 | - */ |
|
989 | - public $status; |
|
990 | - |
|
991 | - /** |
|
992 | - * Constructs a new instance of this class. |
|
993 | - * |
|
994 | - * @param array $header (Required) Associative array of HTTP headers (typically returned by <RequestCore::get_response_header()>). |
|
995 | - * @param string $body (Required) XML-formatted response from AWS. |
|
996 | - * @param integer $status (Optional) HTTP response status code from the request. |
|
997 | - * @return object Contains an <php:array> `header` property (HTTP headers as an associative array), a <php:SimpleXMLElement> or <php:string> `body` property, and an <php:integer> `status` code. |
|
998 | - */ |
|
999 | - public function __construct($header, $body, $status = null) |
|
1000 | - { |
|
1001 | - $this->header = $header; |
|
1002 | - $this->body = $body; |
|
1003 | - $this->status = $status; |
|
1004 | - |
|
1005 | - return $this; |
|
1006 | - } |
|
1007 | - |
|
1008 | - /** |
|
1009 | - * Did we receive the status code we expected? |
|
1010 | - * |
|
1011 | - * @param integer|array $codes (Optional) The status code(s) to expect. Pass an <php:integer> for a single acceptable value, or an <php:array> of integers for multiple acceptable values. |
|
1012 | - * @return boolean Whether we received the expected status code or not. |
|
1013 | - */ |
|
1014 | - public function isOK($codes = array(200, 201, 204, 206)) |
|
1015 | - { |
|
1016 | - if (is_array($codes)) |
|
1017 | - { |
|
1018 | - return in_array($this->status, $codes); |
|
1019 | - } |
|
1020 | - |
|
1021 | - return $this->status === $codes; |
|
1022 | - } |
|
976 | + /** |
|
977 | + * Stores the HTTP header information. |
|
978 | + */ |
|
979 | + public $header; |
|
980 | + |
|
981 | + /** |
|
982 | + * Stores the SimpleXML response. |
|
983 | + */ |
|
984 | + public $body; |
|
985 | + |
|
986 | + /** |
|
987 | + * Stores the HTTP response code. |
|
988 | + */ |
|
989 | + public $status; |
|
990 | + |
|
991 | + /** |
|
992 | + * Constructs a new instance of this class. |
|
993 | + * |
|
994 | + * @param array $header (Required) Associative array of HTTP headers (typically returned by <RequestCore::get_response_header()>). |
|
995 | + * @param string $body (Required) XML-formatted response from AWS. |
|
996 | + * @param integer $status (Optional) HTTP response status code from the request. |
|
997 | + * @return object Contains an <php:array> `header` property (HTTP headers as an associative array), a <php:SimpleXMLElement> or <php:string> `body` property, and an <php:integer> `status` code. |
|
998 | + */ |
|
999 | + public function __construct($header, $body, $status = null) |
|
1000 | + { |
|
1001 | + $this->header = $header; |
|
1002 | + $this->body = $body; |
|
1003 | + $this->status = $status; |
|
1004 | + |
|
1005 | + return $this; |
|
1006 | + } |
|
1007 | + |
|
1008 | + /** |
|
1009 | + * Did we receive the status code we expected? |
|
1010 | + * |
|
1011 | + * @param integer|array $codes (Optional) The status code(s) to expect. Pass an <php:integer> for a single acceptable value, or an <php:array> of integers for multiple acceptable values. |
|
1012 | + * @return boolean Whether we received the expected status code or not. |
|
1013 | + */ |
|
1014 | + public function isOK($codes = array(200, 201, 204, 206)) |
|
1015 | + { |
|
1016 | + if (is_array($codes)) |
|
1017 | + { |
|
1018 | + return in_array($this->status, $codes); |
|
1019 | + } |
|
1020 | + |
|
1021 | + return $this->status === $codes; |
|
1022 | + } |
|
1023 | 1023 | } |
1024 | 1024 | |
1025 | 1025 | /** |
@@ -19,111 +19,111 @@ |
||
19 | 19 | |
20 | 20 | |
21 | 21 | if(function_exists('get_loaded_extensions')){ |
22 | - //检测mcrypt,openssl扩展 |
|
23 | - $extensions = get_loaded_extensions(); |
|
24 | - if($extensions){ |
|
25 | - if(!in_array('mcrypt', $extensions)){ |
|
26 | - throw new Ks3ClientException("please install mcrypt extension"); |
|
27 | - } |
|
28 | - if(!in_array('openssl', $extensions)){ |
|
29 | - throw new Ks3ClientException("please install openssl extension"); |
|
30 | - } |
|
31 | - }else{ |
|
32 | - throw new Ks3ClientException("please install extensions"); |
|
33 | - } |
|
22 | + //检测mcrypt,openssl扩展 |
|
23 | + $extensions = get_loaded_extensions(); |
|
24 | + if($extensions){ |
|
25 | + if(!in_array('mcrypt', $extensions)){ |
|
26 | + throw new Ks3ClientException("please install mcrypt extension"); |
|
27 | + } |
|
28 | + if(!in_array('openssl', $extensions)){ |
|
29 | + throw new Ks3ClientException("please install openssl extension"); |
|
30 | + } |
|
31 | + }else{ |
|
32 | + throw new Ks3ClientException("please install extensions"); |
|
33 | + } |
|
34 | 34 | }else{ |
35 | - throw new Ks3ClientException(); |
|
35 | + throw new Ks3ClientException(); |
|
36 | 36 | } |
37 | 37 | class Ks3EncryptionClient extends Ks3Client{ |
38 | - private $encryptionHandler = NULL; |
|
39 | - //用户提供的主密钥,可以是对称秘钥或非对称秘钥(array,分别是公钥和私钥) |
|
40 | - private $encryptionMaterials = NULL; |
|
38 | + private $encryptionHandler = NULL; |
|
39 | + //用户提供的主密钥,可以是对称秘钥或非对称秘钥(array,分别是公钥和私钥) |
|
40 | + private $encryptionMaterials = NULL; |
|
41 | 41 | |
42 | - public function __construct($accessKey, $secretKey,$encryptionMaterials, $endpoint = NULL ){ |
|
43 | - parent::__construct($accessKey,$secretKey,$endpoint); |
|
44 | - if(is_array($encryptionMaterials)){ |
|
45 | - if(count($encryptionMaterials)==2){ |
|
46 | - $pk = openssl_pkey_get_public($encryptionMaterials[0]); |
|
47 | - $sk = openssl_pkey_get_private($encryptionMaterials[1]); |
|
48 | - if(!$pk) |
|
49 | - throw new Ks3ClientException("invalid RSA public key,you can generate key use openssl"); |
|
50 | - if(!$sk) |
|
51 | - throw new Ks3ClientException("invalid RSA private key,you can generate key use openssl"); |
|
52 | - $encryptionMaterials = array($pk,$sk); |
|
53 | - }else{ |
|
54 | - throw new Ks3ClientException("encryptionMaterials should be string or an array of size 2"); |
|
55 | - } |
|
56 | - } |
|
57 | - $ks3client = new Ks3Client($accessKey,$secretKey,$endpoint); |
|
58 | - $this->encryptionMaterials = $encryptionMaterials; |
|
59 | - if(ENCRYPTPTION_MODE == "EO"){ |
|
60 | - $this->encryptionHandler = new EncryptionEO($ks3client,$encryptionMaterials); |
|
61 | - }elseif (ENCRYPTPTION_MODE == "AE") { |
|
62 | - throw new Ks3ClientException("Authenticated encryption will be supported in the futher"); |
|
63 | - } |
|
64 | - else{ |
|
65 | - throw new Ks3ClientException("unsupported encryption mode :".ENCRYPTPTION_MODE); |
|
66 | - } |
|
67 | - if(ENCRYPTPTION_STORAGE_MODE != "ObjectMetadata"&&ENCRYPTPTION_STORAGE_MODE!="InstructionFile"){ |
|
68 | - throw new Ks3ClientException("unsupported encryption storage mode :".ENCRYPTPTION_STORAGE_MODE); |
|
69 | - } |
|
70 | - } |
|
42 | + public function __construct($accessKey, $secretKey,$encryptionMaterials, $endpoint = NULL ){ |
|
43 | + parent::__construct($accessKey,$secretKey,$endpoint); |
|
44 | + if(is_array($encryptionMaterials)){ |
|
45 | + if(count($encryptionMaterials)==2){ |
|
46 | + $pk = openssl_pkey_get_public($encryptionMaterials[0]); |
|
47 | + $sk = openssl_pkey_get_private($encryptionMaterials[1]); |
|
48 | + if(!$pk) |
|
49 | + throw new Ks3ClientException("invalid RSA public key,you can generate key use openssl"); |
|
50 | + if(!$sk) |
|
51 | + throw new Ks3ClientException("invalid RSA private key,you can generate key use openssl"); |
|
52 | + $encryptionMaterials = array($pk,$sk); |
|
53 | + }else{ |
|
54 | + throw new Ks3ClientException("encryptionMaterials should be string or an array of size 2"); |
|
55 | + } |
|
56 | + } |
|
57 | + $ks3client = new Ks3Client($accessKey,$secretKey,$endpoint); |
|
58 | + $this->encryptionMaterials = $encryptionMaterials; |
|
59 | + if(ENCRYPTPTION_MODE == "EO"){ |
|
60 | + $this->encryptionHandler = new EncryptionEO($ks3client,$encryptionMaterials); |
|
61 | + }elseif (ENCRYPTPTION_MODE == "AE") { |
|
62 | + throw new Ks3ClientException("Authenticated encryption will be supported in the futher"); |
|
63 | + } |
|
64 | + else{ |
|
65 | + throw new Ks3ClientException("unsupported encryption mode :".ENCRYPTPTION_MODE); |
|
66 | + } |
|
67 | + if(ENCRYPTPTION_STORAGE_MODE != "ObjectMetadata"&&ENCRYPTPTION_STORAGE_MODE!="InstructionFile"){ |
|
68 | + throw new Ks3ClientException("unsupported encryption storage mode :".ENCRYPTPTION_STORAGE_MODE); |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - public function putObjectByContent($args=array()){ |
|
73 | - return $this->encryptionHandler->putObjectByContentSecurely($args); |
|
74 | - } |
|
75 | - public function putObjectByFile($args=array()){ |
|
76 | - return $this->encryptionHandler->putObjectByFileSecurely($args); |
|
77 | - } |
|
78 | - public function getObject($args=array()){ |
|
79 | - return $this->encryptionHandler->getObjectSecurely($args); |
|
80 | - } |
|
81 | - public function initMultipartUpload($args=array()){ |
|
82 | - return $this->encryptionHandler->initMultipartUploadSecurely($args); |
|
83 | - } |
|
84 | - public function uploadPart($args=array()){ |
|
85 | - return $this->encryptionHandler->uploadPartSecurely($args); |
|
86 | - } |
|
87 | - public function abortMultipartUpload($args=array()){ |
|
88 | - return $this->encryptionHandler->abortMultipartUploadSecurely($args); |
|
89 | - } |
|
90 | - public function completeMultipartUpload($args=array()){ |
|
91 | - return $this->encryptionHandler->completeMultipartUploadSecurely($args); |
|
92 | - } |
|
93 | - public function deleteObject($args=array()){ |
|
94 | - $result = parent::deleteObject($args); |
|
95 | - $args["Key"] = $args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX; |
|
96 | - try { |
|
97 | - parent::deleteObject($args); |
|
98 | - } catch (Exception $e) { |
|
99 | - //do nothing |
|
100 | - } |
|
101 | - return $result; |
|
102 | - } |
|
103 | - public function copyObject($args=array()){ |
|
104 | - if(parent::objectExists(array( |
|
105 | - "Bucket"=>$args["Bucket"], |
|
106 | - "Key"=>$args["Key"] |
|
107 | - ))){ |
|
108 | - throw new Ks3ClientException("copy object faild,destination object exists"); |
|
109 | - } |
|
110 | - if(parent::objectExists(array( |
|
111 | - "Bucket"=>$args["CopySource"]["Bucket"], |
|
112 | - "Key"=>$args["CopySource"]["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX |
|
113 | - ))){ |
|
114 | - parent::copyObject( |
|
115 | - array( |
|
116 | - "Bucket"=>$args["Bucket"], |
|
117 | - "Key"=>$args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX, |
|
118 | - "CopySource"=>array( |
|
119 | - "Bucket"=>$args["CopySource"]["Bucket"], |
|
120 | - "Key"=>$args["CopySource"]["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX |
|
121 | - ) |
|
122 | - ) |
|
123 | - ); |
|
124 | - } |
|
125 | - return parent::copyObject($args); |
|
126 | - } |
|
72 | + public function putObjectByContent($args=array()){ |
|
73 | + return $this->encryptionHandler->putObjectByContentSecurely($args); |
|
74 | + } |
|
75 | + public function putObjectByFile($args=array()){ |
|
76 | + return $this->encryptionHandler->putObjectByFileSecurely($args); |
|
77 | + } |
|
78 | + public function getObject($args=array()){ |
|
79 | + return $this->encryptionHandler->getObjectSecurely($args); |
|
80 | + } |
|
81 | + public function initMultipartUpload($args=array()){ |
|
82 | + return $this->encryptionHandler->initMultipartUploadSecurely($args); |
|
83 | + } |
|
84 | + public function uploadPart($args=array()){ |
|
85 | + return $this->encryptionHandler->uploadPartSecurely($args); |
|
86 | + } |
|
87 | + public function abortMultipartUpload($args=array()){ |
|
88 | + return $this->encryptionHandler->abortMultipartUploadSecurely($args); |
|
89 | + } |
|
90 | + public function completeMultipartUpload($args=array()){ |
|
91 | + return $this->encryptionHandler->completeMultipartUploadSecurely($args); |
|
92 | + } |
|
93 | + public function deleteObject($args=array()){ |
|
94 | + $result = parent::deleteObject($args); |
|
95 | + $args["Key"] = $args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX; |
|
96 | + try { |
|
97 | + parent::deleteObject($args); |
|
98 | + } catch (Exception $e) { |
|
99 | + //do nothing |
|
100 | + } |
|
101 | + return $result; |
|
102 | + } |
|
103 | + public function copyObject($args=array()){ |
|
104 | + if(parent::objectExists(array( |
|
105 | + "Bucket"=>$args["Bucket"], |
|
106 | + "Key"=>$args["Key"] |
|
107 | + ))){ |
|
108 | + throw new Ks3ClientException("copy object faild,destination object exists"); |
|
109 | + } |
|
110 | + if(parent::objectExists(array( |
|
111 | + "Bucket"=>$args["CopySource"]["Bucket"], |
|
112 | + "Key"=>$args["CopySource"]["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX |
|
113 | + ))){ |
|
114 | + parent::copyObject( |
|
115 | + array( |
|
116 | + "Bucket"=>$args["Bucket"], |
|
117 | + "Key"=>$args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX, |
|
118 | + "CopySource"=>array( |
|
119 | + "Bucket"=>$args["CopySource"]["Bucket"], |
|
120 | + "Key"=>$args["CopySource"]["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX |
|
121 | + ) |
|
122 | + ) |
|
123 | + ); |
|
124 | + } |
|
125 | + return parent::copyObject($args); |
|
126 | + } |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | ?> |
130 | 130 | \ No newline at end of file |
@@ -16,14 +16,14 @@ |
||
16 | 16 | |
17 | 17 | //将所有能确定值的表单项都放在该数组中 |
18 | 18 | $postData = array( |
19 | - "key"=>$key, |
|
20 | - "success_action_redirect"=>$redirect, |
|
21 | - "Content-Type"=>"text/html", |
|
22 | - ); |
|
19 | + "key"=>$key, |
|
20 | + "success_action_redirect"=>$redirect, |
|
21 | + "Content-Type"=>"text/html", |
|
22 | + ); |
|
23 | 23 | //将不能确定值的表单项都放在该数组中 |
24 | 24 | $unknowData = array( |
25 | - "random" |
|
26 | - ); |
|
25 | + "random" |
|
26 | + ); |
|
27 | 27 | $result = $client->postObject($bucket_name,$postData,$unknowData); |
28 | 28 | print_r($result); |
29 | 29 |
@@ -16,37 +16,37 @@ discard block |
||
16 | 16 | multipartUpload($client,$bucket,$keyprefix); |
17 | 17 | |
18 | 18 | function putObjectByContentAndGetObjectUsingMeta($client,$bucket,$keyprefix){ |
19 | - for($i = 45 ;$i < 60;$i++){ |
|
19 | + for($i = 45 ;$i < 60;$i++){ |
|
20 | 20 | |
21 | - $content = EncryptionUtil::genereateOnceUsedKey($i); |
|
21 | + $content = EncryptionUtil::genereateOnceUsedKey($i); |
|
22 | 22 | |
23 | - $args = array( |
|
24 | - "Bucket"=>$bucket, |
|
25 | - "Key"=>$keyprefix."EOMeta", |
|
26 | - "ACL"=>"public-read", |
|
27 | - "Content"=>$content |
|
28 | - ); |
|
29 | - $client->putObjectByContent($args); |
|
30 | - rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOMeta", |
|
31 | - "D://testdown/down",base64_encode(md5($args["Content"]))); |
|
32 | - } |
|
23 | + $args = array( |
|
24 | + "Bucket"=>$bucket, |
|
25 | + "Key"=>$keyprefix."EOMeta", |
|
26 | + "ACL"=>"public-read", |
|
27 | + "Content"=>$content |
|
28 | + ); |
|
29 | + $client->putObjectByContent($args); |
|
30 | + rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOMeta", |
|
31 | + "D://testdown/down",base64_encode(md5($args["Content"]))); |
|
32 | + } |
|
33 | 33 | } |
34 | 34 | function putObjectByFileAndGetObjectUsingMeta($client,$bucket,$keyprefix){ |
35 | 35 | |
36 | - $args = array( |
|
37 | - "Bucket"=>$bucket, |
|
38 | - "Key"=>$keyprefix."EOMeta", |
|
39 | - "ACL"=>"public-read", |
|
40 | - "Content"=>array( |
|
41 | - "content"=>"D://IMG.jpg" |
|
42 | - ) |
|
43 | - ); |
|
44 | - $client->putObjectByFile($args); |
|
45 | - rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOMeta", |
|
46 | - "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
36 | + $args = array( |
|
37 | + "Bucket"=>$bucket, |
|
38 | + "Key"=>$keyprefix."EOMeta", |
|
39 | + "ACL"=>"public-read", |
|
40 | + "Content"=>array( |
|
41 | + "content"=>"D://IMG.jpg" |
|
42 | + ) |
|
43 | + ); |
|
44 | + $client->putObjectByFile($args); |
|
45 | + rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOMeta", |
|
46 | + "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
47 | 47 | } |
48 | 48 | function multipartUpload($client,$bucket,$keyprefix){ |
49 | - //初始化分开上传,获取uploadid |
|
49 | + //初始化分开上传,获取uploadid |
|
50 | 50 | $args = array( |
51 | 51 | "Bucket"=>$bucket, |
52 | 52 | "Key"=>$keyprefix."EOMeta", |
@@ -67,8 +67,8 @@ discard block |
||
67 | 67 | echo "upload".$i."\r\n"; |
68 | 68 | $args=array( |
69 | 69 | "Bucket"=>$bucket, |
70 | - "Key"=>$keyprefix."EOMeta", |
|
71 | - "LastPart"=>($i===$count-1), |
|
70 | + "Key"=>$keyprefix."EOMeta", |
|
71 | + "LastPart"=>($i===$count-1), |
|
72 | 72 | "Options"=>array( |
73 | 73 | "partNumber"=>$i+1, |
74 | 74 | "uploadId"=>$uploadid |
@@ -95,42 +95,42 @@ discard block |
||
95 | 95 | $result = $client->completeMultipartUpload($args); |
96 | 96 | |
97 | 97 | rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOMeta", |
98 | - "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
98 | + "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
99 | 99 | } |
100 | 100 | function rangeGetAndCheckMd5($client,$bucket,$key,$file,$expectedMd5){ |
101 | - $args = array("Bucket"=>$bucket,"Key"=>$key); |
|
102 | - $meta = $client->getObjectMeta($args); |
|
103 | - $contentlength = $meta["ObjectMeta"]["Content-Length"]; |
|
101 | + $args = array("Bucket"=>$bucket,"Key"=>$key); |
|
102 | + $meta = $client->getObjectMeta($args); |
|
103 | + $contentlength = $meta["ObjectMeta"]["Content-Length"]; |
|
104 | 104 | |
105 | - $filelist = array(); |
|
105 | + $filelist = array(); |
|
106 | 106 | |
107 | - for($begin = 0;$begin <$contentlength;){ |
|
108 | - $index = rand((int)($contentlength/20),(int)($contentlength/10)); |
|
109 | - $range = array("start"=>$begin,"end"=>$begin+$index); |
|
110 | - $destFile = $file.$begin."-".($begin+$index); |
|
111 | - array_push($filelist,$destFile); |
|
112 | - $begin += ($index+1); |
|
113 | - $args = array( |
|
114 | - "Bucket"=>$bucket, |
|
115 | - "Key"=>$key, |
|
116 | - "Range"=>$range, |
|
117 | - "WriteTo"=>$destFile |
|
118 | - ); |
|
119 | - $client->getObject($args); |
|
120 | - } |
|
107 | + for($begin = 0;$begin <$contentlength;){ |
|
108 | + $index = rand((int)($contentlength/20),(int)($contentlength/10)); |
|
109 | + $range = array("start"=>$begin,"end"=>$begin+$index); |
|
110 | + $destFile = $file.$begin."-".($begin+$index); |
|
111 | + array_push($filelist,$destFile); |
|
112 | + $begin += ($index+1); |
|
113 | + $args = array( |
|
114 | + "Bucket"=>$bucket, |
|
115 | + "Key"=>$key, |
|
116 | + "Range"=>$range, |
|
117 | + "WriteTo"=>$destFile |
|
118 | + ); |
|
119 | + $client->getObject($args); |
|
120 | + } |
|
121 | 121 | |
122 | - foreach ($filelist as $key => $value) { |
|
123 | - $handle = fopen($value,"r"); |
|
124 | - $size = filesize($value); |
|
125 | - if($size > 0){ |
|
126 | - $content = fread($handle,$size); |
|
127 | - file_put_contents($file,$content,FILE_APPEND); |
|
128 | - } |
|
129 | - fclose($handle); |
|
130 | - @unlink($value); |
|
131 | - } |
|
132 | - $md5 = base64_encode(md5_file($file)); |
|
133 | - if($md5 != $expectedMd5) |
|
134 | - throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
135 | - @unlink($file); |
|
122 | + foreach ($filelist as $key => $value) { |
|
123 | + $handle = fopen($value,"r"); |
|
124 | + $size = filesize($value); |
|
125 | + if($size > 0){ |
|
126 | + $content = fread($handle,$size); |
|
127 | + file_put_contents($file,$content,FILE_APPEND); |
|
128 | + } |
|
129 | + fclose($handle); |
|
130 | + @unlink($value); |
|
131 | + } |
|
132 | + $md5 = base64_encode(md5_file($file)); |
|
133 | + if($md5 != $expectedMd5) |
|
134 | + throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
135 | + @unlink($file); |
|
136 | 136 | } |
@@ -17,33 +17,33 @@ discard block |
||
17 | 17 | copyAnddeleteObject($client,$bucket,$keyprefix); |
18 | 18 | |
19 | 19 | function putObjectByContentAndGetObjectUsingInstruction($client,$bucket,$keyprefix){ |
20 | - $content = EncryptionUtil::genereateOnceUsedKey(rand(100,1000)); |
|
21 | - $args = array( |
|
22 | - "Bucket"=>$bucket, |
|
23 | - "Key"=>$keyprefix."EOFile", |
|
24 | - "ACL"=>"public-read", |
|
25 | - "Content"=>$content |
|
26 | - ); |
|
27 | - $client->putObjectByContent($args); |
|
28 | - rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
|
29 | - "D://testdown/down",base64_encode(md5($args["Content"]))); |
|
20 | + $content = EncryptionUtil::genereateOnceUsedKey(rand(100,1000)); |
|
21 | + $args = array( |
|
22 | + "Bucket"=>$bucket, |
|
23 | + "Key"=>$keyprefix."EOFile", |
|
24 | + "ACL"=>"public-read", |
|
25 | + "Content"=>$content |
|
26 | + ); |
|
27 | + $client->putObjectByContent($args); |
|
28 | + rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
|
29 | + "D://testdown/down",base64_encode(md5($args["Content"]))); |
|
30 | 30 | } |
31 | 31 | function putObjectByFileAndGetObjectUsingFile($client,$bucket,$keyprefix){ |
32 | 32 | |
33 | - $args = array( |
|
34 | - "Bucket"=>$bucket, |
|
35 | - "Key"=>$keyprefix."EOFile", |
|
36 | - "ACL"=>"public-read", |
|
37 | - "Content"=>array( |
|
38 | - "content"=>"D://IMG.jpg" |
|
39 | - ) |
|
40 | - ); |
|
41 | - $client->putObjectByFile($args); |
|
42 | - rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
|
43 | - "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
33 | + $args = array( |
|
34 | + "Bucket"=>$bucket, |
|
35 | + "Key"=>$keyprefix."EOFile", |
|
36 | + "ACL"=>"public-read", |
|
37 | + "Content"=>array( |
|
38 | + "content"=>"D://IMG.jpg" |
|
39 | + ) |
|
40 | + ); |
|
41 | + $client->putObjectByFile($args); |
|
42 | + rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
|
43 | + "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
44 | 44 | } |
45 | 45 | function multipartUpload($client,$bucket,$keyprefix){ |
46 | - //初始化分开上传,获取uploadid |
|
46 | + //初始化分开上传,获取uploadid |
|
47 | 47 | $args = array( |
48 | 48 | "Bucket"=>$bucket, |
49 | 49 | "Key"=>$keyprefix."EOFile", |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | echo "upload".$i."\r\n"; |
65 | 65 | $args=array( |
66 | 66 | "Bucket"=>$bucket, |
67 | - "Key"=>$keyprefix."EOFile", |
|
68 | - "LastPart"=>($i===$count-1), |
|
67 | + "Key"=>$keyprefix."EOFile", |
|
68 | + "LastPart"=>($i===$count-1), |
|
69 | 69 | "Options"=>array( |
70 | 70 | "partNumber"=>$i+1, |
71 | 71 | "uploadId"=>$uploadid |
@@ -92,79 +92,79 @@ discard block |
||
92 | 92 | $result = $client->completeMultipartUpload($args); |
93 | 93 | |
94 | 94 | rangeGetAndCheckMd5($client,$bucket,$keyprefix."EOFile", |
95 | - "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
95 | + "D://testdown/down",base64_encode(md5_file("D://IMG.jpg"))); |
|
96 | 96 | } |
97 | 97 | function copyAnddeleteObject($client,$bucket,$keyprefix){ |
98 | - $content = EncryptionUtil::genereateOnceUsedKey(rand(100,1000)); |
|
99 | - $args = array( |
|
100 | - "Bucket"=>$bucket, |
|
101 | - "Key"=>$keyprefix."EOFile", |
|
102 | - "ACL"=>"public-read", |
|
103 | - "Content"=>$content |
|
104 | - ); |
|
105 | - $client->putObjectByContent($args); |
|
98 | + $content = EncryptionUtil::genereateOnceUsedKey(rand(100,1000)); |
|
99 | + $args = array( |
|
100 | + "Bucket"=>$bucket, |
|
101 | + "Key"=>$keyprefix."EOFile", |
|
102 | + "ACL"=>"public-read", |
|
103 | + "Content"=>$content |
|
104 | + ); |
|
105 | + $client->putObjectByContent($args); |
|
106 | 106 | |
107 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
108 | - $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
109 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
110 | - $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction")); |
|
107 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
108 | + $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
109 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
110 | + $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction")); |
|
111 | 111 | |
112 | - $copyReq = array( |
|
113 | - "Bucket"=>$bucket, |
|
114 | - "Key"=>$keyprefix."EOFileCopy", |
|
115 | - "CopySource"=>array( |
|
116 | - "Bucket"=>$bucket, |
|
117 | - "Key"=>$keyprefix."EOFile" |
|
118 | - ) |
|
119 | - ); |
|
120 | - $client->copyObject($copyReq); |
|
112 | + $copyReq = array( |
|
113 | + "Bucket"=>$bucket, |
|
114 | + "Key"=>$keyprefix."EOFileCopy", |
|
115 | + "CopySource"=>array( |
|
116 | + "Bucket"=>$bucket, |
|
117 | + "Key"=>$keyprefix."EOFile" |
|
118 | + ) |
|
119 | + ); |
|
120 | + $client->copyObject($copyReq); |
|
121 | 121 | |
122 | - if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
123 | - throw new Exception("not found ".$keyprefix."EOFileCopy"); |
|
124 | - if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
125 | - throw new Exception("not found ".$keyprefix."EOFileCopy.instruction"); |
|
122 | + if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
123 | + throw new Exception("not found ".$keyprefix."EOFileCopy"); |
|
124 | + if(!$client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
125 | + throw new Exception("not found ".$keyprefix."EOFileCopy.instruction"); |
|
126 | 126 | |
127 | - $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
127 | + $client->deleteObject(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy")); |
|
128 | 128 | |
129 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
130 | - throw new Exception("found ".$keyprefix."EOFileCopy"); |
|
131 | - if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
132 | - throw new Exception("found ".$keyprefix."EOFileCopy.instruction"); |
|
129 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy"))) |
|
130 | + throw new Exception("found ".$keyprefix."EOFileCopy"); |
|
131 | + if($client->objectExists(array("Bucket"=>$bucket,"Key"=>$keyprefix."EOFileCopy.instruction"))) |
|
132 | + throw new Exception("found ".$keyprefix."EOFileCopy.instruction"); |
|
133 | 133 | } |
134 | 134 | function rangeGetAndCheckMd5($client,$bucket,$key,$file,$expectedMd5){ |
135 | - $args = array("Bucket"=>$bucket,"Key"=>$key); |
|
136 | - $meta = $client->getObjectMeta($args); |
|
137 | - $contentlength = $meta["ObjectMeta"]["Content-Length"]; |
|
135 | + $args = array("Bucket"=>$bucket,"Key"=>$key); |
|
136 | + $meta = $client->getObjectMeta($args); |
|
137 | + $contentlength = $meta["ObjectMeta"]["Content-Length"]; |
|
138 | 138 | |
139 | - $filelist = array(); |
|
139 | + $filelist = array(); |
|
140 | 140 | |
141 | - for($begin = 0;$begin <$contentlength;){ |
|
142 | - $index = rand((int)($contentlength/20),(int)($contentlength/10)); |
|
143 | - $range = array("start"=>$begin,"end"=>$begin+$index); |
|
144 | - $destFile = $file.$begin."-".($begin+$index); |
|
145 | - array_push($filelist,$destFile); |
|
146 | - $begin += ($index+1); |
|
147 | - $args = array( |
|
148 | - "Bucket"=>$bucket, |
|
149 | - "Key"=>$key, |
|
150 | - "Range"=>$range, |
|
151 | - "WriteTo"=>$destFile |
|
152 | - ); |
|
153 | - $client->getObject($args); |
|
154 | - } |
|
141 | + for($begin = 0;$begin <$contentlength;){ |
|
142 | + $index = rand((int)($contentlength/20),(int)($contentlength/10)); |
|
143 | + $range = array("start"=>$begin,"end"=>$begin+$index); |
|
144 | + $destFile = $file.$begin."-".($begin+$index); |
|
145 | + array_push($filelist,$destFile); |
|
146 | + $begin += ($index+1); |
|
147 | + $args = array( |
|
148 | + "Bucket"=>$bucket, |
|
149 | + "Key"=>$key, |
|
150 | + "Range"=>$range, |
|
151 | + "WriteTo"=>$destFile |
|
152 | + ); |
|
153 | + $client->getObject($args); |
|
154 | + } |
|
155 | 155 | |
156 | - foreach ($filelist as $key => $value) { |
|
157 | - $handle = fopen($value,"r"); |
|
158 | - $size = filesize($value); |
|
159 | - if($size > 0){ |
|
160 | - $content = fread($handle,$size); |
|
161 | - file_put_contents($file,$content,FILE_APPEND); |
|
162 | - } |
|
163 | - fclose($handle); |
|
164 | - @unlink($value); |
|
165 | - } |
|
166 | - $md5 = base64_encode(md5_file($file)); |
|
167 | - if($md5 != $expectedMd5) |
|
168 | - throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
169 | - @unlink($file); |
|
156 | + foreach ($filelist as $key => $value) { |
|
157 | + $handle = fopen($value,"r"); |
|
158 | + $size = filesize($value); |
|
159 | + if($size > 0){ |
|
160 | + $content = fread($handle,$size); |
|
161 | + file_put_contents($file,$content,FILE_APPEND); |
|
162 | + } |
|
163 | + fclose($handle); |
|
164 | + @unlink($value); |
|
165 | + } |
|
166 | + $md5 = base64_encode(md5_file($file)); |
|
167 | + if($md5 != $expectedMd5) |
|
168 | + throw new Exception("file md5 check error expected ".$expectedMd5." ,actual ".$md5, 1); |
|
169 | + @unlink($file); |
|
170 | 170 | } |
@@ -33,381 +33,381 @@ |
||
33 | 33 | //print_r(putAdp($client)); |
34 | 34 | //print_r(postObject($client)); |
35 | 35 | function listBuckets($client){ |
36 | - return $client->listBuckets(); |
|
36 | + return $client->listBuckets(); |
|
37 | 37 | } |
38 | 38 | function deleteBucket($client){ |
39 | - return $client->deleteBucket(array("Bucket"=>"ksc-scm")); |
|
39 | + return $client->deleteBucket(array("Bucket"=>"ksc-scm")); |
|
40 | 40 | } |
41 | 41 | function deleteBucketCORS($client){ |
42 | - return $client->deleteBucketCORS(array("Bucket"=>"ksc-scm")); |
|
42 | + return $client->deleteBucketCORS(array("Bucket"=>"ksc-scm")); |
|
43 | 43 | } |
44 | 44 | function listObjects($client){ |
45 | - $args = array( |
|
46 | - "Bucket"=>"lijunwei.test", |
|
47 | - "Options"=>array( |
|
48 | - //"prefix"=>"dir/", |
|
49 | - "max-keys"=>4, |
|
50 | - //"marker"=>"123.pdf", |
|
51 | - "delimiter"=>"/" |
|
52 | - ) |
|
53 | - ); |
|
54 | - return $client->listObjects($args); |
|
45 | + $args = array( |
|
46 | + "Bucket"=>"lijunwei.test", |
|
47 | + "Options"=>array( |
|
48 | + //"prefix"=>"dir/", |
|
49 | + "max-keys"=>4, |
|
50 | + //"marker"=>"123.pdf", |
|
51 | + "delimiter"=>"/" |
|
52 | + ) |
|
53 | + ); |
|
54 | + return $client->listObjects($args); |
|
55 | 55 | } |
56 | 56 | function getBucketAcl($client){ |
57 | - return $client->getBucketAcl(array("Bucket"=>"aaphp")); |
|
57 | + return $client->getBucketAcl(array("Bucket"=>"aaphp")); |
|
58 | 58 | } |
59 | 59 | function getBucketCORS($client){ |
60 | - return $client->getBucketCORS(array("Bucket"=>"ksc-scm")); |
|
60 | + return $client->getBucketCORS(array("Bucket"=>"ksc-scm")); |
|
61 | 61 | } |
62 | 62 | function getBucketLocation($client){ |
63 | - return $client->getBucketLocation(array("Bucket"=>"ksc-scm")); |
|
63 | + return $client->getBucketLocation(array("Bucket"=>"ksc-scm")); |
|
64 | 64 | } |
65 | 65 | function getBucketLogging($client){ |
66 | - return $client->getBucketLogging(array("Bucket"=>"ksc-scm")); |
|
66 | + return $client->getBucketLogging(array("Bucket"=>"ksc-scm")); |
|
67 | 67 | } |
68 | 68 | function bucketExists($client){ |
69 | - $args = array("Bucket"=>"ksc-scm"); |
|
70 | - return $client->bucketExists($args); |
|
69 | + $args = array("Bucket"=>"ksc-scm"); |
|
70 | + return $client->bucketExists($args); |
|
71 | 71 | } |
72 | 72 | function createBucket($client){ |
73 | - $args = array( |
|
74 | - "Bucket"=>"ksc-scm", |
|
75 | - "ACL"=>"private" |
|
76 | - ); |
|
77 | - return $client->createBucket($args); |
|
73 | + $args = array( |
|
74 | + "Bucket"=>"ksc-scm", |
|
75 | + "ACL"=>"private" |
|
76 | + ); |
|
77 | + return $client->createBucket($args); |
|
78 | 78 | } |
79 | 79 | function setBucketAcl($client){ |
80 | - $args = array( |
|
81 | - "Bucket"=>"ksc-scm", |
|
82 | - "ACL"=>"private" |
|
83 | - ); |
|
84 | - return $client->setBucketAcl($args); |
|
80 | + $args = array( |
|
81 | + "Bucket"=>"ksc-scm", |
|
82 | + "ACL"=>"private" |
|
83 | + ); |
|
84 | + return $client->setBucketAcl($args); |
|
85 | 85 | } |
86 | 86 | function setBucketCORS($client){ |
87 | - $args = array( |
|
88 | - "Bucket"=>"ksc-scm", |
|
89 | - "CORS"=>array( |
|
90 | - array( |
|
91 | - "AllowedMethod"=>array("GET","PUT"), |
|
92 | - "AllowedOrigin"=>array("http://www.kingsoft.com"), |
|
93 | - "AllowedHeader"=>array("*"), |
|
94 | - "ExposeHeader"=>array("*"), |
|
95 | - "MaxAgeSeconds"=>10 |
|
96 | - ), |
|
97 | - array( |
|
98 | - "AllowedMethod"=>array("GET","PUT"), |
|
99 | - "AllowedOrigin"=>array("*"), |
|
100 | - "AllowedHeader"=>array("*"), |
|
101 | - "ExposeHeader"=>array("*"), |
|
102 | - "MaxAgeSeconds"=>10 |
|
103 | - ) |
|
104 | - ) |
|
105 | - ); |
|
106 | - return $client->setBucketCORS($args); |
|
87 | + $args = array( |
|
88 | + "Bucket"=>"ksc-scm", |
|
89 | + "CORS"=>array( |
|
90 | + array( |
|
91 | + "AllowedMethod"=>array("GET","PUT"), |
|
92 | + "AllowedOrigin"=>array("http://www.kingsoft.com"), |
|
93 | + "AllowedHeader"=>array("*"), |
|
94 | + "ExposeHeader"=>array("*"), |
|
95 | + "MaxAgeSeconds"=>10 |
|
96 | + ), |
|
97 | + array( |
|
98 | + "AllowedMethod"=>array("GET","PUT"), |
|
99 | + "AllowedOrigin"=>array("*"), |
|
100 | + "AllowedHeader"=>array("*"), |
|
101 | + "ExposeHeader"=>array("*"), |
|
102 | + "MaxAgeSeconds"=>10 |
|
103 | + ) |
|
104 | + ) |
|
105 | + ); |
|
106 | + return $client->setBucketCORS($args); |
|
107 | 107 | } |
108 | 108 | function setBucketLogging($client){ |
109 | - $args = array( |
|
110 | - "Bucket"=>"ksc-scm", |
|
111 | - "BucketLogging"=>array( |
|
112 | - "Enable"=>TRUE, |
|
113 | - "TargetBucket"=>"ksc-scm", |
|
114 | - "TargetPrefix"=>"X-KSS" |
|
115 | - ) |
|
116 | - ); |
|
117 | - return $client->setBucketLogging($args); |
|
109 | + $args = array( |
|
110 | + "Bucket"=>"ksc-scm", |
|
111 | + "BucketLogging"=>array( |
|
112 | + "Enable"=>TRUE, |
|
113 | + "TargetBucket"=>"ksc-scm", |
|
114 | + "TargetPrefix"=>"X-KSS" |
|
115 | + ) |
|
116 | + ); |
|
117 | + return $client->setBucketLogging($args); |
|
118 | 118 | } |
119 | 119 | function deleteObject($client){ |
120 | - $args = array( |
|
121 | - "Bucket"=>"ksc-scm", |
|
122 | - "Key"=>"123.pdf" |
|
123 | - ); |
|
124 | - return $client->deleteObject($args); |
|
120 | + $args = array( |
|
121 | + "Bucket"=>"ksc-scm", |
|
122 | + "Key"=>"123.pdf" |
|
123 | + ); |
|
124 | + return $client->deleteObject($args); |
|
125 | 125 | } |
126 | 126 | function deleteObjects($client){ |
127 | - $args = array( |
|
128 | - "Bucket"=>"ksc-scm", |
|
129 | - "DeleteKeys"=>array("copy/test.zip","copy/123.doc") |
|
130 | - ); |
|
131 | - return $client->deleteObjects($args); |
|
127 | + $args = array( |
|
128 | + "Bucket"=>"ksc-scm", |
|
129 | + "DeleteKeys"=>array("copy/test.zip","copy/123.doc") |
|
130 | + ); |
|
131 | + return $client->deleteObjects($args); |
|
132 | 132 | } |
133 | 133 | function getObject($client){ |
134 | - $args = array( |
|
135 | - "Bucket"=>"aaphp", |
|
136 | - "Key"=>"multi.exe", |
|
137 | - "Range"=>array( |
|
138 | - "start"=>NULL, |
|
139 | - "end"=>4, |
|
140 | - ), |
|
141 | - "WriteTo"=>"D://test.zip" |
|
142 | - ); |
|
143 | - return $client->getObject($args); |
|
134 | + $args = array( |
|
135 | + "Bucket"=>"aaphp", |
|
136 | + "Key"=>"multi.exe", |
|
137 | + "Range"=>array( |
|
138 | + "start"=>NULL, |
|
139 | + "end"=>4, |
|
140 | + ), |
|
141 | + "WriteTo"=>"D://test.zip" |
|
142 | + ); |
|
143 | + return $client->getObject($args); |
|
144 | 144 | } |
145 | 145 | function getObjectAcl($client){ |
146 | - $args = array( |
|
147 | - "Bucket"=>"aaphp", |
|
148 | - "Key"=>"multi.exe" |
|
149 | - ); |
|
150 | - return $client->getObjectAcl($args); |
|
146 | + $args = array( |
|
147 | + "Bucket"=>"aaphp", |
|
148 | + "Key"=>"multi.exe" |
|
149 | + ); |
|
150 | + return $client->getObjectAcl($args); |
|
151 | 151 | } |
152 | 152 | function objectExists($client){ |
153 | - $args = array( |
|
154 | - "Bucket"=>"ksc-scm", |
|
155 | - "Key"=>"123.pdf" |
|
156 | - ); |
|
157 | - return $client->objectExists($args); |
|
153 | + $args = array( |
|
154 | + "Bucket"=>"ksc-scm", |
|
155 | + "Key"=>"123.pdf" |
|
156 | + ); |
|
157 | + return $client->objectExists($args); |
|
158 | 158 | } |
159 | 159 | function getObjectMeta($client){ |
160 | - $args = array( |
|
161 | - "Bucket"=>"aaphp", |
|
162 | - "Key"=>"test.zip" |
|
163 | - ); |
|
164 | - return $client->getObjectMeta($args); |
|
160 | + $args = array( |
|
161 | + "Bucket"=>"aaphp", |
|
162 | + "Key"=>"test.zip" |
|
163 | + ); |
|
164 | + return $client->getObjectMeta($args); |
|
165 | 165 | } |
166 | 166 | function setObjectAcl($client){ |
167 | - $args = array( |
|
168 | - "Bucket"=>"aaphp", |
|
169 | - "Key"=>"test.zip", |
|
170 | - "ACL"=>"private" |
|
171 | - ); |
|
172 | - return $client->setObjectAcl($args); |
|
167 | + $args = array( |
|
168 | + "Bucket"=>"aaphp", |
|
169 | + "Key"=>"test.zip", |
|
170 | + "ACL"=>"private" |
|
171 | + ); |
|
172 | + return $client->setObjectAcl($args); |
|
173 | 173 | } |
174 | 174 | function copyObject($client){ |
175 | - $args = array( |
|
176 | - "Bucket"=>"aaphp", |
|
177 | - "Key"=>"copy/test.zip", |
|
178 | - "CopySource"=>array( |
|
179 | - "Bucket"=>"aaphp", |
|
180 | - "Key"=>"test.zip" |
|
181 | - ) |
|
182 | - ); |
|
183 | - return $client->copyObject($args); |
|
175 | + $args = array( |
|
176 | + "Bucket"=>"aaphp", |
|
177 | + "Key"=>"copy/test.zip", |
|
178 | + "CopySource"=>array( |
|
179 | + "Bucket"=>"aaphp", |
|
180 | + "Key"=>"test.zip" |
|
181 | + ) |
|
182 | + ); |
|
183 | + return $client->copyObject($args); |
|
184 | 184 | } |
185 | 185 | function putObjectByFile($client){ |
186 | - $file = "D://phpput"; |
|
187 | - if(Utils::chk_chinese($file)){ |
|
188 | - $file = iconv('utf-8','gbk',$file); |
|
189 | - } |
|
190 | - $content = $file; |
|
191 | - $args = array( |
|
192 | - "Bucket"=>"aaphp", |
|
193 | - "Key"=>"stream_upload1.txt", |
|
194 | - "ACL"=>"public-read", |
|
195 | - "ObjectMeta"=>array( |
|
196 | - "Content-Type"=>"image/jpg",//只传0-10字节, |
|
197 | - ), |
|
198 | - "Content"=>array( |
|
199 | - "content"=>$file, |
|
200 | - "seek_position"=>0 |
|
201 | - ), |
|
202 | - ); |
|
203 | - return $client->putObjectByFile($args); |
|
186 | + $file = "D://phpput"; |
|
187 | + if(Utils::chk_chinese($file)){ |
|
188 | + $file = iconv('utf-8','gbk',$file); |
|
189 | + } |
|
190 | + $content = $file; |
|
191 | + $args = array( |
|
192 | + "Bucket"=>"aaphp", |
|
193 | + "Key"=>"stream_upload1.txt", |
|
194 | + "ACL"=>"public-read", |
|
195 | + "ObjectMeta"=>array( |
|
196 | + "Content-Type"=>"image/jpg",//只传0-10字节, |
|
197 | + ), |
|
198 | + "Content"=>array( |
|
199 | + "content"=>$file, |
|
200 | + "seek_position"=>0 |
|
201 | + ), |
|
202 | + ); |
|
203 | + return $client->putObjectByFile($args); |
|
204 | 204 | } |
205 | 205 | function multipartUpload($client){ |
206 | - $args = array( |
|
207 | - "Bucket"=>"aaphp", |
|
208 | - "Key"=>"multi.zip", |
|
209 | - "UserMeta"=>array( |
|
210 | - "x-kss-meta-test"=>"example" |
|
211 | - ), |
|
212 | - "ObjectMeta"=>array( |
|
213 | - "Content-Type"=>"text/plain" |
|
214 | - ) |
|
215 | - ); |
|
216 | - $uploadid = $client->initMultipartUpload($args); |
|
217 | - print_r($uploadid); |
|
218 | - $uploadid = $uploadid["UploadId"]; |
|
219 | - echo $uploadid."\r\n"; |
|
220 | - //开始上传 |
|
206 | + $args = array( |
|
207 | + "Bucket"=>"aaphp", |
|
208 | + "Key"=>"multi.zip", |
|
209 | + "UserMeta"=>array( |
|
210 | + "x-kss-meta-test"=>"example" |
|
211 | + ), |
|
212 | + "ObjectMeta"=>array( |
|
213 | + "Content-Type"=>"text/plain" |
|
214 | + ) |
|
215 | + ); |
|
216 | + $uploadid = $client->initMultipartUpload($args); |
|
217 | + print_r($uploadid); |
|
218 | + $uploadid = $uploadid["UploadId"]; |
|
219 | + echo $uploadid."\r\n"; |
|
220 | + //开始上传 |
|
221 | 221 | |
222 | - $file = "D://新建文件夹.rar"; |
|
223 | - if(Utils::chk_chinese($file)){ |
|
224 | - $file = iconv('utf-8','gbk',$file); |
|
225 | - } |
|
226 | - $total = Utils::getFileSize($file); |
|
227 | - $partsize = 1024*1024*5; |
|
228 | - $count = (int)(($total-1)/$partsize)+1; |
|
229 | - echo $count."\r\n"; |
|
230 | - for($i = 0;$i < $count;$i++){ |
|
231 | - echo "upload".$i."\r\n"; |
|
232 | - $args=array( |
|
233 | - "Bucket"=>"aaphp", |
|
234 | - "Key"=>"multi.zip", |
|
235 | - "Options"=>array( |
|
236 | - "partNumber"=>$i+1, |
|
237 | - "uploadId"=>$uploadid |
|
238 | - ), |
|
239 | - "ObjectMeta"=>array( |
|
240 | - "Content-Length"=>$partsize |
|
241 | - ), |
|
242 | - "Content"=>array( |
|
243 | - "content"=>$file, |
|
244 | - "seek_position"=>$partsize*$i |
|
245 | - ) |
|
246 | - ); |
|
247 | - $etag = $client->uploadPart($args); |
|
248 | - print_r($etag); |
|
249 | - $etag = $etag["ETag"]; |
|
250 | - } |
|
251 | - $parts = $client->listParts(array("Bucket"=>"aaphp","Key"=>"multi.zip","Options"=>array("uploadId"=>$uploadid))); |
|
252 | - print_r($parts); |
|
253 | - //结束上传 |
|
254 | - $args=array( |
|
255 | - "Bucket"=>"aaphp", |
|
256 | - "Key"=>"multi.zip", |
|
257 | - "Options"=>array("uploadId"=>$uploadid), |
|
258 | - "Parts"=>$parts["Parts"] |
|
259 | - ); |
|
260 | - $result = $client->completeMultipartUpload($args); |
|
261 | - print_r($result); |
|
222 | + $file = "D://新建文件夹.rar"; |
|
223 | + if(Utils::chk_chinese($file)){ |
|
224 | + $file = iconv('utf-8','gbk',$file); |
|
225 | + } |
|
226 | + $total = Utils::getFileSize($file); |
|
227 | + $partsize = 1024*1024*5; |
|
228 | + $count = (int)(($total-1)/$partsize)+1; |
|
229 | + echo $count."\r\n"; |
|
230 | + for($i = 0;$i < $count;$i++){ |
|
231 | + echo "upload".$i."\r\n"; |
|
232 | + $args=array( |
|
233 | + "Bucket"=>"aaphp", |
|
234 | + "Key"=>"multi.zip", |
|
235 | + "Options"=>array( |
|
236 | + "partNumber"=>$i+1, |
|
237 | + "uploadId"=>$uploadid |
|
238 | + ), |
|
239 | + "ObjectMeta"=>array( |
|
240 | + "Content-Length"=>$partsize |
|
241 | + ), |
|
242 | + "Content"=>array( |
|
243 | + "content"=>$file, |
|
244 | + "seek_position"=>$partsize*$i |
|
245 | + ) |
|
246 | + ); |
|
247 | + $etag = $client->uploadPart($args); |
|
248 | + print_r($etag); |
|
249 | + $etag = $etag["ETag"]; |
|
250 | + } |
|
251 | + $parts = $client->listParts(array("Bucket"=>"aaphp","Key"=>"multi.zip","Options"=>array("uploadId"=>$uploadid))); |
|
252 | + print_r($parts); |
|
253 | + //结束上传 |
|
254 | + $args=array( |
|
255 | + "Bucket"=>"aaphp", |
|
256 | + "Key"=>"multi.zip", |
|
257 | + "Options"=>array("uploadId"=>$uploadid), |
|
258 | + "Parts"=>$parts["Parts"] |
|
259 | + ); |
|
260 | + $result = $client->completeMultipartUpload($args); |
|
261 | + print_r($result); |
|
262 | 262 | } |
263 | 263 | function abortMultipartUpload($client){ |
264 | - $args=array( |
|
265 | - "Bucket"=>"aaphp", |
|
266 | - "Key"=>"multi.zip", |
|
267 | - "Options"=>array("uploadId"=>"1234") |
|
268 | - ); |
|
269 | - return $client->abortMultipartUpload($args); |
|
264 | + $args=array( |
|
265 | + "Bucket"=>"aaphp", |
|
266 | + "Key"=>"multi.zip", |
|
267 | + "Options"=>array("uploadId"=>"1234") |
|
268 | + ); |
|
269 | + return $client->abortMultipartUpload($args); |
|
270 | 270 | } |
271 | 271 | function generatePresignedUrl($client){ |
272 | - $args=array( |
|
273 | - "Bucket"=>"aaphp", |
|
274 | - "Key"=>"multi.zip", |
|
275 | - "Options"=>array( |
|
276 | - "Expires"=>60*60*24*10, |
|
277 | - "response-content-type"=>"application/xml" |
|
278 | - ) |
|
279 | - ); |
|
280 | - return $client->generatePresignedUrl($args); |
|
272 | + $args=array( |
|
273 | + "Bucket"=>"aaphp", |
|
274 | + "Key"=>"multi.zip", |
|
275 | + "Options"=>array( |
|
276 | + "Expires"=>60*60*24*10, |
|
277 | + "response-content-type"=>"application/xml" |
|
278 | + ) |
|
279 | + ); |
|
280 | + return $client->generatePresignedUrl($args); |
|
281 | 281 | } |
282 | 282 | function putObjectWithAdpAndCallBack($client){ |
283 | - $content = "D://野生动物.3gp"; |
|
284 | - $args = array( |
|
285 | - "Bucket"=>"aaphp", |
|
286 | - "Key"=>"野生动物.3gp", |
|
287 | - "ACL"=>"public-read", |
|
288 | - "Content"=>array( |
|
289 | - "content"=>$content |
|
290 | - ), |
|
291 | - "Adp"=>array( |
|
292 | - "NotifyURL"=>"http://10.4.2.38:19090/", |
|
293 | - "Adps"=>array( |
|
294 | - array( |
|
295 | - "Command"=>"tag=avop&f=mp4&res=1280x720&vbr=1000k&abr=128k", |
|
296 | - "Key"=>"野生动物-转码.3gp" |
|
297 | - ) |
|
298 | - ) |
|
299 | - ), |
|
300 | - "CallBack"=>array( |
|
301 | - "Url"=>"http://10.4.2.38:19090/", |
|
302 | - "BodyMagicVariables"=>array("bucket"=>"bucket","key"=>"key"), |
|
303 | - "BodyVariables"=>array("name"=>"lijunwei") |
|
304 | - ) |
|
305 | - ); |
|
306 | - return $client->putObjectByFile($args); |
|
283 | + $content = "D://野生动物.3gp"; |
|
284 | + $args = array( |
|
285 | + "Bucket"=>"aaphp", |
|
286 | + "Key"=>"野生动物.3gp", |
|
287 | + "ACL"=>"public-read", |
|
288 | + "Content"=>array( |
|
289 | + "content"=>$content |
|
290 | + ), |
|
291 | + "Adp"=>array( |
|
292 | + "NotifyURL"=>"http://10.4.2.38:19090/", |
|
293 | + "Adps"=>array( |
|
294 | + array( |
|
295 | + "Command"=>"tag=avop&f=mp4&res=1280x720&vbr=1000k&abr=128k", |
|
296 | + "Key"=>"野生动物-转码.3gp" |
|
297 | + ) |
|
298 | + ) |
|
299 | + ), |
|
300 | + "CallBack"=>array( |
|
301 | + "Url"=>"http://10.4.2.38:19090/", |
|
302 | + "BodyMagicVariables"=>array("bucket"=>"bucket","key"=>"key"), |
|
303 | + "BodyVariables"=>array("name"=>"lijunwei") |
|
304 | + ) |
|
305 | + ); |
|
306 | + return $client->putObjectByFile($args); |
|
307 | 307 | } |
308 | 308 | function multipartUploadWithAdpAndCallBack($client){ |
309 | - $args = array( |
|
310 | - "Bucket"=>"aaphp", |
|
311 | - "Key"=>"multi.zip", |
|
312 | - "UserMeta"=>array( |
|
313 | - "x-kss-meta-test"=>"example" |
|
314 | - ), |
|
315 | - "ObjectMeta"=>array( |
|
316 | - "Content-Type"=>"text/plain" |
|
317 | - ) |
|
318 | - ); |
|
319 | - $uploadid = $client->initMultipartUpload($args); |
|
320 | - print_r($uploadid); |
|
321 | - $uploadid = $uploadid["UploadId"]; |
|
322 | - echo $uploadid."\r\n"; |
|
323 | - //开始上传 |
|
309 | + $args = array( |
|
310 | + "Bucket"=>"aaphp", |
|
311 | + "Key"=>"multi.zip", |
|
312 | + "UserMeta"=>array( |
|
313 | + "x-kss-meta-test"=>"example" |
|
314 | + ), |
|
315 | + "ObjectMeta"=>array( |
|
316 | + "Content-Type"=>"text/plain" |
|
317 | + ) |
|
318 | + ); |
|
319 | + $uploadid = $client->initMultipartUpload($args); |
|
320 | + print_r($uploadid); |
|
321 | + $uploadid = $uploadid["UploadId"]; |
|
322 | + echo $uploadid."\r\n"; |
|
323 | + //开始上传 |
|
324 | 324 | |
325 | - $file = "D://野生动物.3gp"; |
|
326 | - if(Utils::chk_chinese($file)){ |
|
327 | - $file = iconv('utf-8','gbk',$file); |
|
328 | - } |
|
329 | - $partsize = 1024*1024*5; |
|
330 | - $total = Utils::getFileSize($file); |
|
331 | - $count = (int)(($total-1)/$partsize)+1; |
|
332 | - echo $count."\r\n"; |
|
333 | - for($i = 0;$i < $count;$i++){ |
|
334 | - echo "upload".$i."\r\n"; |
|
335 | - $args=array( |
|
336 | - "Bucket"=>"aaphp", |
|
337 | - "Key"=>"multi.zip", |
|
338 | - "Options"=>array( |
|
339 | - "partNumber"=>$i+1, |
|
340 | - "uploadId"=>$uploadid |
|
341 | - ), |
|
342 | - "ObjectMeta"=>array( |
|
343 | - "Content-Length"=>$partsize |
|
344 | - ), |
|
345 | - "Content"=>array( |
|
346 | - "content"=>$file, |
|
347 | - "seek_position"=>$partsize*$i |
|
348 | - ) |
|
349 | - ); |
|
350 | - $etag = $client->uploadPart($args); |
|
351 | - print_r($etag); |
|
352 | - $etag = $etag["ETag"]; |
|
353 | - } |
|
354 | - $parts = $client->listParts(array("Bucket"=>"aaphp","Key"=>"multi.zip","Options"=>array("uploadId"=>$uploadid))); |
|
355 | - print_r($parts); |
|
356 | - //结束上传 |
|
357 | - $args=array( |
|
358 | - "Bucket"=>"aaphp", |
|
359 | - "Key"=>"multi.zip", |
|
360 | - "Options"=>array("uploadId"=>$uploadid), |
|
361 | - "Parts"=>$parts["Parts"], |
|
362 | - "Adp"=>array( |
|
363 | - "NotifyURL"=>"http://10.4.2.38:19090/", |
|
364 | - "Adps"=>array( |
|
365 | - array( |
|
366 | - "Command"=>"tag=avop&f=mp4&res=1280x720&vbr=1000k&abr=128k", |
|
367 | - "Key"=>"野生动物-转码.3gp" |
|
368 | - ) |
|
369 | - ) |
|
370 | - ), |
|
371 | - "CallBack"=>array( |
|
372 | - "Url"=>"http://10.4.2.38:19090/", |
|
373 | - "BodyMagicVariables"=>array("bucket"=>"bucket","key"=>"key"), |
|
374 | - "BodyVariables"=>array("name"=>"lijunwei") |
|
375 | - ) |
|
376 | - ); |
|
377 | - $result = $client->completeMultipartUpload($args); |
|
378 | - print_r($result); |
|
379 | - $taskid = $result["TaskID"]; |
|
380 | - $task = $client->getAdp(array("TaskID"=>$taskid)); |
|
381 | - print_r($task); |
|
325 | + $file = "D://野生动物.3gp"; |
|
326 | + if(Utils::chk_chinese($file)){ |
|
327 | + $file = iconv('utf-8','gbk',$file); |
|
328 | + } |
|
329 | + $partsize = 1024*1024*5; |
|
330 | + $total = Utils::getFileSize($file); |
|
331 | + $count = (int)(($total-1)/$partsize)+1; |
|
332 | + echo $count."\r\n"; |
|
333 | + for($i = 0;$i < $count;$i++){ |
|
334 | + echo "upload".$i."\r\n"; |
|
335 | + $args=array( |
|
336 | + "Bucket"=>"aaphp", |
|
337 | + "Key"=>"multi.zip", |
|
338 | + "Options"=>array( |
|
339 | + "partNumber"=>$i+1, |
|
340 | + "uploadId"=>$uploadid |
|
341 | + ), |
|
342 | + "ObjectMeta"=>array( |
|
343 | + "Content-Length"=>$partsize |
|
344 | + ), |
|
345 | + "Content"=>array( |
|
346 | + "content"=>$file, |
|
347 | + "seek_position"=>$partsize*$i |
|
348 | + ) |
|
349 | + ); |
|
350 | + $etag = $client->uploadPart($args); |
|
351 | + print_r($etag); |
|
352 | + $etag = $etag["ETag"]; |
|
353 | + } |
|
354 | + $parts = $client->listParts(array("Bucket"=>"aaphp","Key"=>"multi.zip","Options"=>array("uploadId"=>$uploadid))); |
|
355 | + print_r($parts); |
|
356 | + //结束上传 |
|
357 | + $args=array( |
|
358 | + "Bucket"=>"aaphp", |
|
359 | + "Key"=>"multi.zip", |
|
360 | + "Options"=>array("uploadId"=>$uploadid), |
|
361 | + "Parts"=>$parts["Parts"], |
|
362 | + "Adp"=>array( |
|
363 | + "NotifyURL"=>"http://10.4.2.38:19090/", |
|
364 | + "Adps"=>array( |
|
365 | + array( |
|
366 | + "Command"=>"tag=avop&f=mp4&res=1280x720&vbr=1000k&abr=128k", |
|
367 | + "Key"=>"野生动物-转码.3gp" |
|
368 | + ) |
|
369 | + ) |
|
370 | + ), |
|
371 | + "CallBack"=>array( |
|
372 | + "Url"=>"http://10.4.2.38:19090/", |
|
373 | + "BodyMagicVariables"=>array("bucket"=>"bucket","key"=>"key"), |
|
374 | + "BodyVariables"=>array("name"=>"lijunwei") |
|
375 | + ) |
|
376 | + ); |
|
377 | + $result = $client->completeMultipartUpload($args); |
|
378 | + print_r($result); |
|
379 | + $taskid = $result["TaskID"]; |
|
380 | + $task = $client->getAdp(array("TaskID"=>$taskid)); |
|
381 | + print_r($task); |
|
382 | 382 | } |
383 | 383 | function putAdp($client){ |
384 | - $args=array( |
|
385 | - "Bucket"=>"aaphp", |
|
386 | - "Key"=>"multi.zip", |
|
387 | - "Adp"=>array( |
|
388 | - "NotifyURL"=>"http://10.4.2.38:19090/", |
|
389 | - "Adps"=>array( |
|
390 | - array( |
|
391 | - "Command"=>"tag=avop&f=mp4&res=1280x720&vbr=1000k&abr=128k", |
|
392 | - "Key"=>"野生动物-转码.3gp" |
|
393 | - ) |
|
394 | - ) |
|
395 | - ) |
|
396 | - ); |
|
397 | - $result = $client->putAdp($args); |
|
398 | - print_r($result); |
|
399 | - $taskid = $result["TaskID"]; |
|
400 | - $task = $client->getAdp(array("TaskID"=>$taskid)); |
|
401 | - print_r($task); |
|
384 | + $args=array( |
|
385 | + "Bucket"=>"aaphp", |
|
386 | + "Key"=>"multi.zip", |
|
387 | + "Adp"=>array( |
|
388 | + "NotifyURL"=>"http://10.4.2.38:19090/", |
|
389 | + "Adps"=>array( |
|
390 | + array( |
|
391 | + "Command"=>"tag=avop&f=mp4&res=1280x720&vbr=1000k&abr=128k", |
|
392 | + "Key"=>"野生动物-转码.3gp" |
|
393 | + ) |
|
394 | + ) |
|
395 | + ) |
|
396 | + ); |
|
397 | + $result = $client->putAdp($args); |
|
398 | + print_r($result); |
|
399 | + $taskid = $result["TaskID"]; |
|
400 | + $task = $client->getAdp(array("TaskID"=>$taskid)); |
|
401 | + print_r($task); |
|
402 | 402 | } |
403 | 403 | function postObject($client){ |
404 | 404 | |
405 | - $postData = array( |
|
406 | - "key"=>"~!@\\#$\%^&*()_+-=qw", |
|
407 | - "acl"=>"public-read" |
|
408 | - ); |
|
409 | - $unKnowData=array("122","334"); |
|
405 | + $postData = array( |
|
406 | + "key"=>"~!@\\#$\%^&*()_+-=qw", |
|
407 | + "acl"=>"public-read" |
|
408 | + ); |
|
409 | + $unKnowData=array("122","334"); |
|
410 | 410 | |
411 | - print_r($client->postObject("ksc-scm",$postData,$unKnowData)); |
|
411 | + print_r($client->postObject("ksc-scm",$postData,$unKnowData)); |
|
412 | 412 | } |
413 | 413 | ?> |
@@ -1,279 +1,279 @@ |
||
1 | 1 | <?php |
2 | 2 | class EncryptionUtil{ |
3 | - public static $INSTRUCTION_SUFFIX = ".instruction"; |
|
4 | - public static function genereateOnceUsedKey($length=32){ |
|
5 | - $randpwd = ""; |
|
6 | - for ($i = 0; $i < $length; $i++) |
|
7 | - { |
|
8 | - $randpwd .= chr(mt_rand(33, 126)); |
|
9 | - } |
|
10 | - return $randpwd; |
|
11 | - } |
|
12 | - public static function encode_AES_ECB($data,$secret_key){ |
|
13 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_ECB,''); |
|
3 | + public static $INSTRUCTION_SUFFIX = ".instruction"; |
|
4 | + public static function genereateOnceUsedKey($length=32){ |
|
5 | + $randpwd = ""; |
|
6 | + for ($i = 0; $i < $length; $i++) |
|
7 | + { |
|
8 | + $randpwd .= chr(mt_rand(33, 126)); |
|
9 | + } |
|
10 | + return $randpwd; |
|
11 | + } |
|
12 | + public static function encode_AES_ECB($data,$secret_key){ |
|
13 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_ECB,''); |
|
14 | 14 | |
15 | - $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB); |
|
16 | - $pad = $blocksize - (strlen($data) % $blocksize); |
|
17 | - $data = $data . str_repeat(chr($pad), $pad); |
|
15 | + $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB); |
|
16 | + $pad = $blocksize - (strlen($data) % $blocksize); |
|
17 | + $data = $data . str_repeat(chr($pad), $pad); |
|
18 | 18 | |
19 | - $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|
20 | - mcrypt_generic_init($td,$secret_key,$iv); |
|
21 | - $encrypted = mcrypt_generic($td,$data); |
|
22 | - mcrypt_generic_deinit($td); |
|
23 | - return $encrypted; |
|
24 | - } |
|
25 | - public static function decode_AES_ECB($data,$secret_key){ |
|
26 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_ECB,''); |
|
27 | - $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|
28 | - mcrypt_generic_init($td,$secret_key,$iv); |
|
29 | - $data = mdecrypt_generic($td,$data); |
|
30 | - mcrypt_generic_deinit($td); |
|
31 | - mcrypt_module_close($td); |
|
19 | + $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|
20 | + mcrypt_generic_init($td,$secret_key,$iv); |
|
21 | + $encrypted = mcrypt_generic($td,$data); |
|
22 | + mcrypt_generic_deinit($td); |
|
23 | + return $encrypted; |
|
24 | + } |
|
25 | + public static function decode_AES_ECB($data,$secret_key){ |
|
26 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_ECB,''); |
|
27 | + $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); |
|
28 | + mcrypt_generic_init($td,$secret_key,$iv); |
|
29 | + $data = mdecrypt_generic($td,$data); |
|
30 | + mcrypt_generic_deinit($td); |
|
31 | + mcrypt_module_close($td); |
|
32 | 32 | |
33 | - $dec_s = strlen($data); |
|
34 | - $padding = ord($data[$dec_s-1]); |
|
35 | - $data = substr($data, 0, -$padding); |
|
33 | + $dec_s = strlen($data); |
|
34 | + $padding = ord($data[$dec_s-1]); |
|
35 | + $data = substr($data, 0, -$padding); |
|
36 | 36 | |
37 | - return trim($data); |
|
38 | - } |
|
39 | - public static function getKeyEncryptionAlgm($encryptionMaterials){ |
|
40 | - if(is_array($encryptionMaterials)){ |
|
41 | - return "RSA"; |
|
42 | - }else{ |
|
43 | - return "AES"; |
|
44 | - } |
|
45 | - } |
|
46 | - public static function getAdjustedRange($range,$blocksize){ |
|
47 | - $a = $range["start"]; |
|
48 | - $b = $range["end"]; |
|
49 | - $a = $a - ($a%$blocksize)-$blocksize; |
|
50 | - if($a < 0) |
|
51 | - $a = 0; |
|
37 | + return trim($data); |
|
38 | + } |
|
39 | + public static function getKeyEncryptionAlgm($encryptionMaterials){ |
|
40 | + if(is_array($encryptionMaterials)){ |
|
41 | + return "RSA"; |
|
42 | + }else{ |
|
43 | + return "AES"; |
|
44 | + } |
|
45 | + } |
|
46 | + public static function getAdjustedRange($range,$blocksize){ |
|
47 | + $a = $range["start"]; |
|
48 | + $b = $range["end"]; |
|
49 | + $a = $a - ($a%$blocksize)-$blocksize; |
|
50 | + if($a < 0) |
|
51 | + $a = 0; |
|
52 | 52 | |
53 | - $b = $b+$blocksize-$b%$blocksize+$blocksize; |
|
53 | + $b = $b+$blocksize-$b%$blocksize+$blocksize; |
|
54 | 54 | |
55 | - return array("start"=>$a,"end"=>$b); |
|
56 | - } |
|
57 | - public static function encodeCek($encryptionMaterials,$cek){ |
|
58 | - $encrypKeyAlg = EncryptionUtil::getKeyEncryptionAlgm($encryptionMaterials); |
|
59 | - if($encrypKeyAlg === "AES"){ |
|
60 | - $secretKey = $encryptionMaterials; |
|
61 | - $encryptedSek = EncryptionUtil::encode_AES_ECB($cek,$secretKey); |
|
62 | - if(empty($encryptedSek)) |
|
63 | - throw new Ks3ClientException("can not encode cek useing AES"); |
|
64 | - }else if($encrypKeyAlg === "RSA"){ |
|
65 | - $encryptedSek = ""; |
|
66 | - openssl_public_encrypt($cek,$encryptedSek, $encryptionMaterials[0]); |
|
67 | - if(empty($encryptedSek)) |
|
68 | - throw new Ks3ClientException("can not encode cek useing RSA"); |
|
69 | - } |
|
70 | - return $encryptedSek; |
|
71 | - } |
|
72 | - public static function decodeCek($encryptionMaterials,$cekEncrypted){ |
|
73 | - $encrypKeyAlg = EncryptionUtil::getKeyEncryptionAlgm($encryptionMaterials); |
|
74 | - if($encrypKeyAlg === "AES"){ |
|
75 | - $secretKey = $encryptionMaterials; |
|
76 | - $cek = EncryptionUtil::decode_AES_ECB($cekEncrypted,$secretKey); |
|
77 | - if(empty($cek)) |
|
78 | - throw new Ks3ClientException("can not decode cek useing AES,secret key maybe not correct"); |
|
79 | - }else if($encrypKeyAlg === "RSA"){ |
|
80 | - $cek = ""; |
|
81 | - openssl_private_decrypt($cekEncrypted,$cek, $encryptionMaterials[1]); |
|
82 | - if(empty($cek)) |
|
83 | - throw new Ks3ClientException("can not decode cek useing RSA,public/private key pair maybe not correct"); |
|
84 | - } |
|
85 | - return $cek; |
|
86 | - } |
|
87 | - public static function getPKCS5EncrypedLength($length,$blocksize){ |
|
88 | - $pad = $blocksize - $length%$blocksize; |
|
89 | - return $length+$pad; |
|
90 | - } |
|
91 | - //pkcs5填充 |
|
92 | - public static function PKCS5Padding($data,$blocksize){ |
|
93 | - $pad = $blocksize - strlen($data)%$blocksize; |
|
94 | - for($i = 0;$i < $pad;$i++){ |
|
95 | - $data.= chr($pad); |
|
96 | - } |
|
97 | - return $data; |
|
98 | - } |
|
99 | - public static function updateContentMD5Header($req){ |
|
100 | - if(!is_array($req)) |
|
101 | - return $req; |
|
102 | - if(isset($req["ObjectMeta"])){ |
|
103 | - $meta = $req["ObjectMeta"]; |
|
104 | - }else{ |
|
105 | - return $req; |
|
106 | - } |
|
107 | - if(is_array($meta) && isset($meta["Content-MD5"])){ |
|
108 | - $md5 = $meta["Content-MD5"]; |
|
109 | - }else{ |
|
110 | - return $req; |
|
111 | - } |
|
112 | - if(empty($md5)){ |
|
113 | - return $req; |
|
114 | - }else{ |
|
115 | - $req["ObjectMeta"]["Content-MD5"] = NULL; |
|
116 | - $req["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-md5"] = $md5; |
|
117 | - } |
|
118 | - return $req; |
|
119 | - } |
|
120 | - public static function metaTextLength($req){ |
|
121 | - if(!is_array($req)) |
|
122 | - return -1; |
|
123 | - if(isset($req["ObjectMeta"])){ |
|
124 | - $meta = $req["ObjectMeta"]; |
|
125 | - }else{ |
|
126 | - return -1; |
|
127 | - } |
|
128 | - if(is_array($meta) && isset($meta["Content-Length"])){ |
|
129 | - $length = $meta["Content-Length"]; |
|
130 | - return $length; |
|
131 | - }else |
|
132 | - return -1; |
|
133 | - } |
|
134 | - public static function plainTextLength($args){ |
|
135 | - if(isset($args["Content"])){ |
|
136 | - if(is_array($args["Content"])){ |
|
137 | - $content = $args["Content"]["content"]; |
|
138 | - $seek_position = 0; |
|
139 | - $resourceLength = 0; |
|
140 | - $length = -1; |
|
141 | - $isFile = FALSE; |
|
55 | + return array("start"=>$a,"end"=>$b); |
|
56 | + } |
|
57 | + public static function encodeCek($encryptionMaterials,$cek){ |
|
58 | + $encrypKeyAlg = EncryptionUtil::getKeyEncryptionAlgm($encryptionMaterials); |
|
59 | + if($encrypKeyAlg === "AES"){ |
|
60 | + $secretKey = $encryptionMaterials; |
|
61 | + $encryptedSek = EncryptionUtil::encode_AES_ECB($cek,$secretKey); |
|
62 | + if(empty($encryptedSek)) |
|
63 | + throw new Ks3ClientException("can not encode cek useing AES"); |
|
64 | + }else if($encrypKeyAlg === "RSA"){ |
|
65 | + $encryptedSek = ""; |
|
66 | + openssl_public_encrypt($cek,$encryptedSek, $encryptionMaterials[0]); |
|
67 | + if(empty($encryptedSek)) |
|
68 | + throw new Ks3ClientException("can not encode cek useing RSA"); |
|
69 | + } |
|
70 | + return $encryptedSek; |
|
71 | + } |
|
72 | + public static function decodeCek($encryptionMaterials,$cekEncrypted){ |
|
73 | + $encrypKeyAlg = EncryptionUtil::getKeyEncryptionAlgm($encryptionMaterials); |
|
74 | + if($encrypKeyAlg === "AES"){ |
|
75 | + $secretKey = $encryptionMaterials; |
|
76 | + $cek = EncryptionUtil::decode_AES_ECB($cekEncrypted,$secretKey); |
|
77 | + if(empty($cek)) |
|
78 | + throw new Ks3ClientException("can not decode cek useing AES,secret key maybe not correct"); |
|
79 | + }else if($encrypKeyAlg === "RSA"){ |
|
80 | + $cek = ""; |
|
81 | + openssl_private_decrypt($cekEncrypted,$cek, $encryptionMaterials[1]); |
|
82 | + if(empty($cek)) |
|
83 | + throw new Ks3ClientException("can not decode cek useing RSA,public/private key pair maybe not correct"); |
|
84 | + } |
|
85 | + return $cek; |
|
86 | + } |
|
87 | + public static function getPKCS5EncrypedLength($length,$blocksize){ |
|
88 | + $pad = $blocksize - $length%$blocksize; |
|
89 | + return $length+$pad; |
|
90 | + } |
|
91 | + //pkcs5填充 |
|
92 | + public static function PKCS5Padding($data,$blocksize){ |
|
93 | + $pad = $blocksize - strlen($data)%$blocksize; |
|
94 | + for($i = 0;$i < $pad;$i++){ |
|
95 | + $data.= chr($pad); |
|
96 | + } |
|
97 | + return $data; |
|
98 | + } |
|
99 | + public static function updateContentMD5Header($req){ |
|
100 | + if(!is_array($req)) |
|
101 | + return $req; |
|
102 | + if(isset($req["ObjectMeta"])){ |
|
103 | + $meta = $req["ObjectMeta"]; |
|
104 | + }else{ |
|
105 | + return $req; |
|
106 | + } |
|
107 | + if(is_array($meta) && isset($meta["Content-MD5"])){ |
|
108 | + $md5 = $meta["Content-MD5"]; |
|
109 | + }else{ |
|
110 | + return $req; |
|
111 | + } |
|
112 | + if(empty($md5)){ |
|
113 | + return $req; |
|
114 | + }else{ |
|
115 | + $req["ObjectMeta"]["Content-MD5"] = NULL; |
|
116 | + $req["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-md5"] = $md5; |
|
117 | + } |
|
118 | + return $req; |
|
119 | + } |
|
120 | + public static function metaTextLength($req){ |
|
121 | + if(!is_array($req)) |
|
122 | + return -1; |
|
123 | + if(isset($req["ObjectMeta"])){ |
|
124 | + $meta = $req["ObjectMeta"]; |
|
125 | + }else{ |
|
126 | + return -1; |
|
127 | + } |
|
128 | + if(is_array($meta) && isset($meta["Content-Length"])){ |
|
129 | + $length = $meta["Content-Length"]; |
|
130 | + return $length; |
|
131 | + }else |
|
132 | + return -1; |
|
133 | + } |
|
134 | + public static function plainTextLength($args){ |
|
135 | + if(isset($args["Content"])){ |
|
136 | + if(is_array($args["Content"])){ |
|
137 | + $content = $args["Content"]["content"]; |
|
138 | + $seek_position = 0; |
|
139 | + $resourceLength = 0; |
|
140 | + $length = -1; |
|
141 | + $isFile = FALSE; |
|
142 | 142 | |
143 | - if (!is_resource($content)){ |
|
144 | - $isFile = TRUE; |
|
145 | - //如果之前用户已经转化为GBK则不转换 |
|
146 | - if(Utils::chk_chinese($content)&&!Utils::check_char($content)){ |
|
147 | - $content = iconv('utf-8','gbk',$content); |
|
148 | - } |
|
149 | - if(!file_exists($content)) |
|
150 | - throw new Ks3ClientException("the specified file does not exist "); |
|
151 | - $length = Utils::getFileSize($content); |
|
152 | - $content = fopen($content,"r"); |
|
153 | - }else{ |
|
154 | - $stats = fstat($content); |
|
155 | - if ($stats && $stats["size"] >= 0){ |
|
156 | - $length = $stats["size"]; |
|
157 | - } |
|
158 | - } |
|
159 | - $resourceLength = $length; |
|
160 | - //优先取用户设置seek_position,没有的话取ftell |
|
161 | - if(isset($args["Content"]["seek_position"])&&$args["Content"]["seek_position"]>0){ |
|
162 | - $seek_position = $args["Content"]["seek_position"]; |
|
163 | - }else if(!$isFile){ |
|
164 | - $seek_position = ftell($content); |
|
165 | - if($seek_position<0) |
|
166 | - $seek_position = 0; |
|
167 | - fseek($content,0); |
|
168 | - } |
|
143 | + if (!is_resource($content)){ |
|
144 | + $isFile = TRUE; |
|
145 | + //如果之前用户已经转化为GBK则不转换 |
|
146 | + if(Utils::chk_chinese($content)&&!Utils::check_char($content)){ |
|
147 | + $content = iconv('utf-8','gbk',$content); |
|
148 | + } |
|
149 | + if(!file_exists($content)) |
|
150 | + throw new Ks3ClientException("the specified file does not exist "); |
|
151 | + $length = Utils::getFileSize($content); |
|
152 | + $content = fopen($content,"r"); |
|
153 | + }else{ |
|
154 | + $stats = fstat($content); |
|
155 | + if ($stats && $stats["size"] >= 0){ |
|
156 | + $length = $stats["size"]; |
|
157 | + } |
|
158 | + } |
|
159 | + $resourceLength = $length; |
|
160 | + //优先取用户设置seek_position,没有的话取ftell |
|
161 | + if(isset($args["Content"]["seek_position"])&&$args["Content"]["seek_position"]>0){ |
|
162 | + $seek_position = $args["Content"]["seek_position"]; |
|
163 | + }else if(!$isFile){ |
|
164 | + $seek_position = ftell($content); |
|
165 | + if($seek_position<0) |
|
166 | + $seek_position = 0; |
|
167 | + fseek($content,0); |
|
168 | + } |
|
169 | 169 | |
170 | - $lengthInMeta = -1; |
|
171 | - if(isset($args["ObjectMeta"]["Content-Length"])){ |
|
172 | - $lengthInMeta = $args["ObjectMeta"]["Content-Length"]; |
|
173 | - } |
|
174 | - if($lengthInMeta > 0){ |
|
175 | - $length = $lengthInMeta; |
|
176 | - }else if($resourceLength > 0){ |
|
177 | - //根据seek_position计算实际长度 |
|
178 | - $length = $resourceLength - $seek_position; |
|
179 | - } |
|
180 | - if($length <= 0) |
|
181 | - throw new Ks3ClientException("calculate content length failed,unexpected contetn length ".$length); |
|
182 | - return $length; |
|
183 | - }else{ |
|
184 | - $content = $args["Content"]; |
|
185 | - $lengthInMeta = EncryptionUtil::metaTextLength($args); |
|
186 | - $length = strlen($content); |
|
187 | - if($length<$lengthInMeta||$lengthInMeta <= 0) |
|
188 | - return $length; |
|
189 | - else |
|
190 | - return $lengthInMeta; |
|
191 | - } |
|
192 | - } |
|
193 | - return -1; |
|
194 | - } |
|
195 | - public static function initMultipartUploadContext($initResult,$iv,$cek,$encryptedCek,$matdesc="{}"){ |
|
196 | - $cacheDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR; |
|
197 | - $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
198 | - if(!is_dir($cacheDir)) |
|
199 | - mkdir($cacheDir); |
|
200 | - if(!is_dir($encryptionDir)) |
|
201 | - mkdir($encryptionDir); |
|
202 | - if(is_array($matdesc)){ |
|
203 | - $matdesc = json_encode($matdesc); |
|
204 | - } |
|
205 | - $initResult["firstIv"] = base64_encode($iv); |
|
206 | - $initResult["nextIv"] = base64_encode($iv); |
|
207 | - $initResult["cek"] = base64_encode($cek); |
|
208 | - $initResult["encryptedCek"] = base64_encode($encryptedCek); |
|
209 | - $initResult["lastPart"] = FALSE; |
|
210 | - $initResult["matdesc"] = $matdesc; |
|
211 | - $json = json_encode($initResult); |
|
212 | - $file = EncryptionUtil::openfile($encryptionDir.$initResult["UploadId"], "w"); |
|
213 | - fwrite($file, $json); |
|
214 | - fclose($file); |
|
215 | - } |
|
216 | - public static function updateMultipartUploadContext($UploadId,$iv,$lastPart = FALSE){ |
|
217 | - $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
218 | - $content = EncryptionUtil::getMultipartUploadContext($UploadId); |
|
219 | - $content["nextIv"] = base64_encode($iv); |
|
220 | - $content["lastPart"] = $lastPart; |
|
221 | - $json = json_encode($content); |
|
222 | - $file = EncryptionUtil::openfile($encryptionDir.$UploadId, "w"); |
|
223 | - fwrite($file, $json); |
|
224 | - fclose($file); |
|
225 | - } |
|
226 | - public static function getMultipartUploadContext($UploadId){ |
|
227 | - $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
228 | - if(!EncryptionUtil::multipartUploadContextExists($UploadId)) |
|
229 | - throw new Ks3ClientException("can not found multipart upload context in cache dir"); |
|
230 | - $jsonString = file_get_contents($encryptionDir.$UploadId); |
|
231 | - $arry = json_decode($jsonString,TRUE); |
|
232 | - return $arry; |
|
233 | - } |
|
234 | - public static function deleteMultipartUploadContext($UploadId){ |
|
235 | - $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
236 | - @unlink($encryptionDir.$UploadId); |
|
237 | - } |
|
238 | - public static function multipartUploadContextExists($UploadId){ |
|
239 | - $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
240 | - return file_exists($encryptionDir.$UploadId); |
|
241 | - } |
|
242 | - public static function openfile($path,$mode){ |
|
243 | - $file = fopen($path, $mode); |
|
244 | - if($file) |
|
245 | - return $file; |
|
246 | - else |
|
247 | - throw new Ks3ClientException("open file ".$path." error"); |
|
248 | - } |
|
249 | - //matdesc为字符串或array数据类型。 |
|
250 | - public static function createInstructionFile($bucket,$key,$cek,$iv,$matdesc="{}"){ |
|
251 | - if(is_array($matdesc)){ |
|
252 | - $matdesc = json_encode($matdesc); |
|
253 | - } |
|
254 | - $key = $key.EncryptionUtil::$INSTRUCTION_SUFFIX; |
|
170 | + $lengthInMeta = -1; |
|
171 | + if(isset($args["ObjectMeta"]["Content-Length"])){ |
|
172 | + $lengthInMeta = $args["ObjectMeta"]["Content-Length"]; |
|
173 | + } |
|
174 | + if($lengthInMeta > 0){ |
|
175 | + $length = $lengthInMeta; |
|
176 | + }else if($resourceLength > 0){ |
|
177 | + //根据seek_position计算实际长度 |
|
178 | + $length = $resourceLength - $seek_position; |
|
179 | + } |
|
180 | + if($length <= 0) |
|
181 | + throw new Ks3ClientException("calculate content length failed,unexpected contetn length ".$length); |
|
182 | + return $length; |
|
183 | + }else{ |
|
184 | + $content = $args["Content"]; |
|
185 | + $lengthInMeta = EncryptionUtil::metaTextLength($args); |
|
186 | + $length = strlen($content); |
|
187 | + if($length<$lengthInMeta||$lengthInMeta <= 0) |
|
188 | + return $length; |
|
189 | + else |
|
190 | + return $lengthInMeta; |
|
191 | + } |
|
192 | + } |
|
193 | + return -1; |
|
194 | + } |
|
195 | + public static function initMultipartUploadContext($initResult,$iv,$cek,$encryptedCek,$matdesc="{}"){ |
|
196 | + $cacheDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR; |
|
197 | + $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
198 | + if(!is_dir($cacheDir)) |
|
199 | + mkdir($cacheDir); |
|
200 | + if(!is_dir($encryptionDir)) |
|
201 | + mkdir($encryptionDir); |
|
202 | + if(is_array($matdesc)){ |
|
203 | + $matdesc = json_encode($matdesc); |
|
204 | + } |
|
205 | + $initResult["firstIv"] = base64_encode($iv); |
|
206 | + $initResult["nextIv"] = base64_encode($iv); |
|
207 | + $initResult["cek"] = base64_encode($cek); |
|
208 | + $initResult["encryptedCek"] = base64_encode($encryptedCek); |
|
209 | + $initResult["lastPart"] = FALSE; |
|
210 | + $initResult["matdesc"] = $matdesc; |
|
211 | + $json = json_encode($initResult); |
|
212 | + $file = EncryptionUtil::openfile($encryptionDir.$initResult["UploadId"], "w"); |
|
213 | + fwrite($file, $json); |
|
214 | + fclose($file); |
|
215 | + } |
|
216 | + public static function updateMultipartUploadContext($UploadId,$iv,$lastPart = FALSE){ |
|
217 | + $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
218 | + $content = EncryptionUtil::getMultipartUploadContext($UploadId); |
|
219 | + $content["nextIv"] = base64_encode($iv); |
|
220 | + $content["lastPart"] = $lastPart; |
|
221 | + $json = json_encode($content); |
|
222 | + $file = EncryptionUtil::openfile($encryptionDir.$UploadId, "w"); |
|
223 | + fwrite($file, $json); |
|
224 | + fclose($file); |
|
225 | + } |
|
226 | + public static function getMultipartUploadContext($UploadId){ |
|
227 | + $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
228 | + if(!EncryptionUtil::multipartUploadContextExists($UploadId)) |
|
229 | + throw new Ks3ClientException("can not found multipart upload context in cache dir"); |
|
230 | + $jsonString = file_get_contents($encryptionDir.$UploadId); |
|
231 | + $arry = json_decode($jsonString,TRUE); |
|
232 | + return $arry; |
|
233 | + } |
|
234 | + public static function deleteMultipartUploadContext($UploadId){ |
|
235 | + $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
236 | + @unlink($encryptionDir.$UploadId); |
|
237 | + } |
|
238 | + public static function multipartUploadContextExists($UploadId){ |
|
239 | + $encryptionDir = KS3_API_PATH.DIRECTORY_SEPARATOR."cache".DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR; |
|
240 | + return file_exists($encryptionDir.$UploadId); |
|
241 | + } |
|
242 | + public static function openfile($path,$mode){ |
|
243 | + $file = fopen($path, $mode); |
|
244 | + if($file) |
|
245 | + return $file; |
|
246 | + else |
|
247 | + throw new Ks3ClientException("open file ".$path." error"); |
|
248 | + } |
|
249 | + //matdesc为字符串或array数据类型。 |
|
250 | + public static function createInstructionFile($bucket,$key,$cek,$iv,$matdesc="{}"){ |
|
251 | + if(is_array($matdesc)){ |
|
252 | + $matdesc = json_encode($matdesc); |
|
253 | + } |
|
254 | + $key = $key.EncryptionUtil::$INSTRUCTION_SUFFIX; |
|
255 | 255 | |
256 | - $instruction = json_encode(array( |
|
257 | - "x-kss-key"=>$cek, |
|
258 | - "x-kss-iv"=>$iv, |
|
259 | - "x-kss-matdesc"=>$matdesc |
|
260 | - )); |
|
256 | + $instruction = json_encode(array( |
|
257 | + "x-kss-key"=>$cek, |
|
258 | + "x-kss-iv"=>$iv, |
|
259 | + "x-kss-matdesc"=>$matdesc |
|
260 | + )); |
|
261 | 261 | |
262 | - $req = array( |
|
263 | - "Bucket"=>$bucket, |
|
264 | - "Key"=>$key, |
|
265 | - "Content"=>$instruction, |
|
266 | - "UserMeta"=>array( |
|
267 | - "x-kss-meta-x-kss-crypto-instr-file"=>base64_encode($key) |
|
268 | - ) |
|
269 | - ); |
|
270 | - return $req; |
|
271 | - } |
|
272 | - public static function isInstructionFile($s3Object){ |
|
273 | - $meta = $s3Object["Meta"]; |
|
274 | - if(isset($meta["UserMeta"]["x-kss-meta-x-kss-crypto-instr-file"])) |
|
275 | - return TRUE; |
|
276 | - return FALSE; |
|
277 | - } |
|
262 | + $req = array( |
|
263 | + "Bucket"=>$bucket, |
|
264 | + "Key"=>$key, |
|
265 | + "Content"=>$instruction, |
|
266 | + "UserMeta"=>array( |
|
267 | + "x-kss-meta-x-kss-crypto-instr-file"=>base64_encode($key) |
|
268 | + ) |
|
269 | + ); |
|
270 | + return $req; |
|
271 | + } |
|
272 | + public static function isInstructionFile($s3Object){ |
|
273 | + $meta = $s3Object["Meta"]; |
|
274 | + if(isset($meta["UserMeta"]["x-kss-meta-x-kss-crypto-instr-file"])) |
|
275 | + return TRUE; |
|
276 | + return FALSE; |
|
277 | + } |
|
278 | 278 | } |
279 | 279 | ?> |
280 | 280 | \ No newline at end of file |
@@ -3,356 +3,356 @@ |
||
3 | 3 | require_once KS3_API_PATH.DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR."EncryptionCallBack.php"; |
4 | 4 | require_once KS3_API_PATH.DIRECTORY_SEPARATOR."exceptions".DIRECTORY_SEPARATOR."Exceptions.php"; |
5 | 5 | interface EncryptionHandler{ |
6 | - public function putObjectByContentSecurely($args=array()); |
|
7 | - public function putObjectByFileSecurely($args=array()); |
|
8 | - public function getObjectSecurely($args=array()); |
|
9 | - public function initMultipartUploadSecurely($args=array()); |
|
10 | - public function uploadPartSecurely($args=array()); |
|
11 | - public function abortMultipartUploadSecurely($args=array()); |
|
12 | - public function completeMultipartUploadSecurely($args=array()); |
|
6 | + public function putObjectByContentSecurely($args=array()); |
|
7 | + public function putObjectByFileSecurely($args=array()); |
|
8 | + public function getObjectSecurely($args=array()); |
|
9 | + public function initMultipartUploadSecurely($args=array()); |
|
10 | + public function uploadPartSecurely($args=array()); |
|
11 | + public function abortMultipartUploadSecurely($args=array()); |
|
12 | + public function completeMultipartUploadSecurely($args=array()); |
|
13 | 13 | } |
14 | 14 | class EncryptionEO implements EncryptionHandler{ |
15 | - private $encryptionMaterials = NULL; |
|
16 | - private $ks3client = NULL; |
|
17 | - public function __construct($ks3client,$encryptionMaterials){ |
|
18 | - $this->encryptionMaterials = $encryptionMaterials; |
|
19 | - $this->ks3client = $ks3client; |
|
20 | - } |
|
21 | - public function putObjectByContentSecurely($args=array()){ |
|
22 | - $sek = EncryptionUtil::genereateOnceUsedKey(); |
|
23 | - $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
|
24 | - $content = $args["Content"]; |
|
25 | - if(empty($content)) |
|
26 | - throw new Ks3ClientException("please specifie Content in request args"); |
|
27 | - $metaContentLength = EncryptionUtil::metaTextLength($args); |
|
28 | - $plainTextLength = strlen($content); |
|
29 | - if($metaContentLength > 0 && $metaContentLength < $plainTextLength){ |
|
30 | - $plainTextLength = $metaContentLength; |
|
31 | - } |
|
32 | - if($plainTextLength > 0) |
|
33 | - $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
|
34 | - else |
|
35 | - throw new Ks3ClientException("unexpected content length ".$plainTextLength); |
|
15 | + private $encryptionMaterials = NULL; |
|
16 | + private $ks3client = NULL; |
|
17 | + public function __construct($ks3client,$encryptionMaterials){ |
|
18 | + $this->encryptionMaterials = $encryptionMaterials; |
|
19 | + $this->ks3client = $ks3client; |
|
20 | + } |
|
21 | + public function putObjectByContentSecurely($args=array()){ |
|
22 | + $sek = EncryptionUtil::genereateOnceUsedKey(); |
|
23 | + $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
|
24 | + $content = $args["Content"]; |
|
25 | + if(empty($content)) |
|
26 | + throw new Ks3ClientException("please specifie Content in request args"); |
|
27 | + $metaContentLength = EncryptionUtil::metaTextLength($args); |
|
28 | + $plainTextLength = strlen($content); |
|
29 | + if($metaContentLength > 0 && $metaContentLength < $plainTextLength){ |
|
30 | + $plainTextLength = $metaContentLength; |
|
31 | + } |
|
32 | + if($plainTextLength > 0) |
|
33 | + $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
|
34 | + else |
|
35 | + throw new Ks3ClientException("unexpected content length ".$plainTextLength); |
|
36 | 36 | |
37 | - $content = substr($content, 0,$plainTextLength); |
|
37 | + $content = substr($content, 0,$plainTextLength); |
|
38 | 38 | |
39 | 39 | |
40 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
41 | - $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
|
42 | - mcrypt_generic_init($td,$sek,$iv); |
|
43 | - //对content进行pkcs5填充 |
|
44 | - $content = EncryptionUtil::PKCS5Padding($content,mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC)); |
|
45 | - $encrypted = mcrypt_generic($td,$content); |
|
46 | - mcrypt_generic_deinit($td); |
|
40 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
41 | + $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
|
42 | + mcrypt_generic_init($td,$sek,$iv); |
|
43 | + //对content进行pkcs5填充 |
|
44 | + $content = EncryptionUtil::PKCS5Padding($content,mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC)); |
|
45 | + $encrypted = mcrypt_generic($td,$content); |
|
46 | + mcrypt_generic_deinit($td); |
|
47 | 47 | |
48 | - $args["ObjectMeta"]["Content-Length"] = strlen($encrypted); |
|
49 | - $args["Content"] = $encrypted; |
|
48 | + $args["ObjectMeta"]["Content-Length"] = strlen($encrypted); |
|
49 | + $args["Content"] = $encrypted; |
|
50 | 50 | |
51 | - $args = EncryptionUtil::updateContentMD5Header($args); |
|
51 | + $args = EncryptionUtil::updateContentMD5Header($args); |
|
52 | 52 | |
53 | - //TODO |
|
54 | - $matdesc = "{}"; |
|
55 | - if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
|
56 | - $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
|
57 | - $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
|
58 | - $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
|
59 | - } |
|
53 | + //TODO |
|
54 | + $matdesc = "{}"; |
|
55 | + if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
|
56 | + $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
|
57 | + $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
|
58 | + $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
|
59 | + } |
|
60 | 60 | |
61 | - $result = $this->ks3client->putObjectByContent($args); |
|
61 | + $result = $this->ks3client->putObjectByContent($args); |
|
62 | 62 | |
63 | - if(ENCRYPTPTION_STORAGE_MODE == "InstructionFile"){ |
|
64 | - $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
|
65 | - base64_encode($encryptedSek),base64_encode($iv),$matdesc); |
|
66 | - $this->ks3client->putObjectByContent($req); |
|
67 | - } |
|
63 | + if(ENCRYPTPTION_STORAGE_MODE == "InstructionFile"){ |
|
64 | + $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
|
65 | + base64_encode($encryptedSek),base64_encode($iv),$matdesc); |
|
66 | + $this->ks3client->putObjectByContent($req); |
|
67 | + } |
|
68 | 68 | |
69 | - return $result; |
|
70 | - } |
|
71 | - public function putObjectByFileSecurely($args=array()){ |
|
72 | - $sek = EncryptionUtil::genereateOnceUsedKey(); |
|
73 | - $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
|
74 | - if(!isset($args["Content"])||!is_array($args["Content"]) |
|
75 | - ||!isset($args["Content"]["content"]) |
|
76 | - ||empty($args["Content"]["content"])) |
|
77 | - throw new Ks3ClientException("please specifie file content in request args"); |
|
78 | - $content = $args["Content"]; |
|
79 | - $plainTextLength = EncryptionUtil::plainTextLength($args); |
|
80 | - if($plainTextLength <= 0){ |
|
81 | - throw new Ks3ClientException("get content length failed ,unexpected content length ".$plainTextLength); |
|
82 | - } |
|
83 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
84 | - $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
|
69 | + return $result; |
|
70 | + } |
|
71 | + public function putObjectByFileSecurely($args=array()){ |
|
72 | + $sek = EncryptionUtil::genereateOnceUsedKey(); |
|
73 | + $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
|
74 | + if(!isset($args["Content"])||!is_array($args["Content"]) |
|
75 | + ||!isset($args["Content"]["content"]) |
|
76 | + ||empty($args["Content"]["content"])) |
|
77 | + throw new Ks3ClientException("please specifie file content in request args"); |
|
78 | + $content = $args["Content"]; |
|
79 | + $plainTextLength = EncryptionUtil::plainTextLength($args); |
|
80 | + if($plainTextLength <= 0){ |
|
81 | + throw new Ks3ClientException("get content length failed ,unexpected content length ".$plainTextLength); |
|
82 | + } |
|
83 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
84 | + $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
|
85 | 85 | |
86 | - $args = EncryptionUtil::updateContentMD5Header($args); |
|
87 | - $encryptedLength = EncryptionUtil::getPKCS5EncrypedLength($plainTextLength,mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC)); |
|
86 | + $args = EncryptionUtil::updateContentMD5Header($args); |
|
87 | + $encryptedLength = EncryptionUtil::getPKCS5EncrypedLength($plainTextLength,mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC)); |
|
88 | 88 | |
89 | - $args["ObjectMeta"]["Content-Length"] = $encryptedLength; |
|
90 | - $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
|
89 | + $args["ObjectMeta"]["Content-Length"] = $encryptedLength; |
|
90 | + $args["UserMeta"]["x-kss-meta-x-kss-unencrypted-content-length"] = $plainTextLength; |
|
91 | 91 | |
92 | - $readCallBack = new AESCBCStreamReadCallBack(); |
|
93 | - $readCallBack->iv = $iv; |
|
94 | - $readCallBack->cek = $sek; |
|
95 | - $readCallBack->contentLength = $plainTextLength; |
|
96 | - $args["readCallBack"] = $readCallBack; |
|
92 | + $readCallBack = new AESCBCStreamReadCallBack(); |
|
93 | + $readCallBack->iv = $iv; |
|
94 | + $readCallBack->cek = $sek; |
|
95 | + $readCallBack->contentLength = $plainTextLength; |
|
96 | + $args["readCallBack"] = $readCallBack; |
|
97 | 97 | |
98 | - //TODO |
|
99 | - $matdesc = "{}"; |
|
100 | - if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
|
101 | - $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
|
102 | - $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
|
103 | - $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
|
104 | - } |
|
98 | + //TODO |
|
99 | + $matdesc = "{}"; |
|
100 | + if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
|
101 | + $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
|
102 | + $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
|
103 | + $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
|
104 | + } |
|
105 | 105 | |
106 | - $result = $this->ks3client->putObjectByFile($args); |
|
106 | + $result = $this->ks3client->putObjectByFile($args); |
|
107 | 107 | |
108 | - if(ENCRYPTPTION_STORAGE_MODE == "InstructionFile"){ |
|
109 | - $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
|
110 | - base64_encode($encryptedSek),base64_encode($iv),$matdesc); |
|
111 | - $this->ks3client->putObjectByContent($req); |
|
112 | - } |
|
108 | + if(ENCRYPTPTION_STORAGE_MODE == "InstructionFile"){ |
|
109 | + $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
|
110 | + base64_encode($encryptedSek),base64_encode($iv),$matdesc); |
|
111 | + $this->ks3client->putObjectByContent($req); |
|
112 | + } |
|
113 | 113 | |
114 | - return $result; |
|
115 | - } |
|
116 | - public function getObjectSecurely($args=array()){ |
|
117 | - $meta = $this->ks3client->getObjectMeta($args); |
|
118 | - if(isset($meta["UserMeta"]["x-kss-meta-x-kss-key"])&&isset($meta["UserMeta"]["x-kss-meta-x-kss-iv"])){ |
|
119 | - $encryptedInMeta = TRUE; |
|
120 | - }else{ |
|
121 | - $encryptedInMeta = FALSE; |
|
122 | - } |
|
123 | - $encrypted = TRUE; |
|
124 | - $encryptionInfo = array(); |
|
125 | - if($encryptedInMeta){ |
|
126 | - $encryptionInfo["iv"] = base64_decode($meta["UserMeta"]["x-kss-meta-x-kss-iv"]); |
|
127 | - $matdesc =$meta["UserMeta"]["x-kss-meta-x-kss-matdesc"]; |
|
128 | - $encryptionInfo["matdesc"] = $matdesc; |
|
129 | - $cekEncrypted = base64_decode($meta["UserMeta"]["x-kss-meta-x-kss-key"]); |
|
130 | - $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
|
131 | - }else{ |
|
132 | - if($this->ks3client->objectExists(array( |
|
133 | - "Bucket"=>$args["Bucket"], |
|
134 | - "Key"=>$args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX) |
|
135 | - ) |
|
136 | - ){ |
|
137 | - $insKey = $args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX; |
|
138 | - $getIns = array( |
|
139 | - "Bucket"=>$args["Bucket"], |
|
140 | - "Key"=>$insKey, |
|
141 | - ); |
|
142 | - $s3Object = $this->ks3client->getObject($getIns); |
|
143 | - if(!EncryptionUtil::isInstructionFile($s3Object)) |
|
144 | - throw new Ks3ClientException($insKey." is not an InstructionFile"); |
|
114 | + return $result; |
|
115 | + } |
|
116 | + public function getObjectSecurely($args=array()){ |
|
117 | + $meta = $this->ks3client->getObjectMeta($args); |
|
118 | + if(isset($meta["UserMeta"]["x-kss-meta-x-kss-key"])&&isset($meta["UserMeta"]["x-kss-meta-x-kss-iv"])){ |
|
119 | + $encryptedInMeta = TRUE; |
|
120 | + }else{ |
|
121 | + $encryptedInMeta = FALSE; |
|
122 | + } |
|
123 | + $encrypted = TRUE; |
|
124 | + $encryptionInfo = array(); |
|
125 | + if($encryptedInMeta){ |
|
126 | + $encryptionInfo["iv"] = base64_decode($meta["UserMeta"]["x-kss-meta-x-kss-iv"]); |
|
127 | + $matdesc =$meta["UserMeta"]["x-kss-meta-x-kss-matdesc"]; |
|
128 | + $encryptionInfo["matdesc"] = $matdesc; |
|
129 | + $cekEncrypted = base64_decode($meta["UserMeta"]["x-kss-meta-x-kss-key"]); |
|
130 | + $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
|
131 | + }else{ |
|
132 | + if($this->ks3client->objectExists(array( |
|
133 | + "Bucket"=>$args["Bucket"], |
|
134 | + "Key"=>$args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX) |
|
135 | + ) |
|
136 | + ){ |
|
137 | + $insKey = $args["Key"].EncryptionUtil::$INSTRUCTION_SUFFIX; |
|
138 | + $getIns = array( |
|
139 | + "Bucket"=>$args["Bucket"], |
|
140 | + "Key"=>$insKey, |
|
141 | + ); |
|
142 | + $s3Object = $this->ks3client->getObject($getIns); |
|
143 | + if(!EncryptionUtil::isInstructionFile($s3Object)) |
|
144 | + throw new Ks3ClientException($insKey." is not an InstructionFile"); |
|
145 | 145 | |
146 | - $content = $s3Object["Content"]; |
|
147 | - $content = json_decode($content,TRUE); |
|
148 | - $encryptionInfo["iv"] = base64_decode($content["x-kss-iv"]); |
|
149 | - $matdesc =$content["x-kss-matdesc"]; |
|
150 | - $encryptionInfo["matdesc"] = $matdesc; |
|
151 | - $cekEncrypted = base64_decode($content["x-kss-key"]); |
|
152 | - $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
|
153 | - }else{ |
|
154 | - $encrypted =FALSE; |
|
155 | - } |
|
156 | - } |
|
157 | - //是否为下载到文件中 |
|
158 | - $isWriteToFile=FALSE; |
|
159 | - if($encrypted) |
|
160 | - { |
|
161 | - $iv = $encryptionInfo["iv"]; |
|
162 | - $cek = $encryptionInfo["cek"]; |
|
146 | + $content = $s3Object["Content"]; |
|
147 | + $content = json_decode($content,TRUE); |
|
148 | + $encryptionInfo["iv"] = base64_decode($content["x-kss-iv"]); |
|
149 | + $matdesc =$content["x-kss-matdesc"]; |
|
150 | + $encryptionInfo["matdesc"] = $matdesc; |
|
151 | + $cekEncrypted = base64_decode($content["x-kss-key"]); |
|
152 | + $encryptionInfo["cek"] = $cek = EncryptionUtil::decodeCek($this->encryptionMaterials,$cekEncrypted); |
|
153 | + }else{ |
|
154 | + $encrypted =FALSE; |
|
155 | + } |
|
156 | + } |
|
157 | + //是否为下载到文件中 |
|
158 | + $isWriteToFile=FALSE; |
|
159 | + if($encrypted) |
|
160 | + { |
|
161 | + $iv = $encryptionInfo["iv"]; |
|
162 | + $cek = $encryptionInfo["cek"]; |
|
163 | 163 | |
164 | - if(empty($iv)) |
|
165 | - throw new Ks3ClientException("can not find iv in UserMeta or InstructionFile"); |
|
166 | - if(empty($cek)) |
|
167 | - throw new Ks3ClientException("can not find cek in UserMeta or InstructionFile"); |
|
164 | + if(empty($iv)) |
|
165 | + throw new Ks3ClientException("can not find iv in UserMeta or InstructionFile"); |
|
166 | + if(empty($cek)) |
|
167 | + throw new Ks3ClientException("can not find cek in UserMeta or InstructionFile"); |
|
168 | 168 | |
169 | - if(isset($args["Range"])){ |
|
170 | - $range = $args["Range"]; |
|
171 | - if(!is_array($range)){ |
|
172 | - if(preg_match('/^bytes=[0-9]*-[0-9]*$/', $range)){ |
|
173 | - $ranges = explode("-",substr($range,strlen("bytes="))); |
|
174 | - $a = $ranges[0]; |
|
175 | - $b = $ranges[1]; |
|
176 | - if($a > $b){ |
|
177 | - throw new Ks3ClientException("Invalid range ".$range); |
|
178 | - } |
|
179 | - $range = array("start"=>$a,"end"=>$b); |
|
180 | - }else{ |
|
181 | - throw new Ks3ClientException("Invalid range ".$range); |
|
182 | - } |
|
183 | - }else{ |
|
184 | - if(!isset($range["start"])||!isset($range["end"])){ |
|
185 | - throw new Ks3ClientException("Invalid range ".serialize($range)); |
|
186 | - } |
|
187 | - if($range["start"] > $range["end"]){ |
|
188 | - throw new Ks3ClientException("Invalid range ".serialize($range)); |
|
189 | - } |
|
190 | - } |
|
191 | - } |
|
169 | + if(isset($args["Range"])){ |
|
170 | + $range = $args["Range"]; |
|
171 | + if(!is_array($range)){ |
|
172 | + if(preg_match('/^bytes=[0-9]*-[0-9]*$/', $range)){ |
|
173 | + $ranges = explode("-",substr($range,strlen("bytes="))); |
|
174 | + $a = $ranges[0]; |
|
175 | + $b = $ranges[1]; |
|
176 | + if($a > $b){ |
|
177 | + throw new Ks3ClientException("Invalid range ".$range); |
|
178 | + } |
|
179 | + $range = array("start"=>$a,"end"=>$b); |
|
180 | + }else{ |
|
181 | + throw new Ks3ClientException("Invalid range ".$range); |
|
182 | + } |
|
183 | + }else{ |
|
184 | + if(!isset($range["start"])||!isset($range["end"])){ |
|
185 | + throw new Ks3ClientException("Invalid range ".serialize($range)); |
|
186 | + } |
|
187 | + if($range["start"] > $range["end"]){ |
|
188 | + throw new Ks3ClientException("Invalid range ".serialize($range)); |
|
189 | + } |
|
190 | + } |
|
191 | + } |
|
192 | 192 | |
193 | - $isWriteToFile = isset($args["WriteTo"]); |
|
194 | - $contentLength = $meta["ObjectMeta"]["Content-Length"]; |
|
195 | - if($isWriteToFile){ |
|
196 | - $writeCallBack = new AESCBCStreamWriteCallBack(); |
|
197 | - $writeCallBack->iv=$iv; |
|
198 | - $writeCallBack->cek=$cek; |
|
199 | - $writeCallBack->contentLength = $contentLength; |
|
200 | - if(isset($range)){ |
|
201 | - $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
202 | - $adjustedRange = EncryptionUtil::getAdjustedRange($range,$blocksize); |
|
203 | - $writeCallBack->expectedRange = $range; |
|
204 | - $writeCallBack->adjustedRange = $adjustedRange; |
|
205 | - $args["Range"]=$adjustedRange; |
|
206 | - } |
|
207 | - $args["writeCallBack"] = $writeCallBack; |
|
208 | - return $this->ks3client->getObject($args); |
|
209 | - }else{ |
|
210 | - $offset = 0; |
|
211 | - $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
212 | - if(isset($range)){ |
|
213 | - $adjustedRange = EncryptionUtil::getAdjustedRange($range,$blocksize); |
|
214 | - $args["Range"]=$adjustedRange; |
|
215 | - } |
|
216 | - $s3Object = $this->ks3client->getObject($args); |
|
217 | - $content = $s3Object["Content"]; |
|
193 | + $isWriteToFile = isset($args["WriteTo"]); |
|
194 | + $contentLength = $meta["ObjectMeta"]["Content-Length"]; |
|
195 | + if($isWriteToFile){ |
|
196 | + $writeCallBack = new AESCBCStreamWriteCallBack(); |
|
197 | + $writeCallBack->iv=$iv; |
|
198 | + $writeCallBack->cek=$cek; |
|
199 | + $writeCallBack->contentLength = $contentLength; |
|
200 | + if(isset($range)){ |
|
201 | + $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
202 | + $adjustedRange = EncryptionUtil::getAdjustedRange($range,$blocksize); |
|
203 | + $writeCallBack->expectedRange = $range; |
|
204 | + $writeCallBack->adjustedRange = $adjustedRange; |
|
205 | + $args["Range"]=$adjustedRange; |
|
206 | + } |
|
207 | + $args["writeCallBack"] = $writeCallBack; |
|
208 | + return $this->ks3client->getObject($args); |
|
209 | + }else{ |
|
210 | + $offset = 0; |
|
211 | + $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
212 | + if(isset($range)){ |
|
213 | + $adjustedRange = EncryptionUtil::getAdjustedRange($range,$blocksize); |
|
214 | + $args["Range"]=$adjustedRange; |
|
215 | + } |
|
216 | + $s3Object = $this->ks3client->getObject($args); |
|
217 | + $content = $s3Object["Content"]; |
|
218 | 218 | |
219 | - if(isset($range)){ |
|
220 | - if($adjustedRange["start"] > 0){ |
|
221 | - $iv = substr($content,0,$blocksize); |
|
222 | - $content = substr($content,$blocksize); |
|
223 | - $offset = $blocksize+$adjustedRange["start"]; |
|
224 | - } |
|
225 | - } |
|
226 | - if(!empty($content)){ |
|
227 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
228 | - mcrypt_generic_init($td,$cek,$iv); |
|
229 | - $decoded = mdecrypt_generic($td,$content); |
|
230 | - mcrypt_generic_deinit($td); |
|
231 | - mcrypt_module_close($td); |
|
232 | - }else{ |
|
233 | - $decoded = ""; |
|
234 | - } |
|
219 | + if(isset($range)){ |
|
220 | + if($adjustedRange["start"] > 0){ |
|
221 | + $iv = substr($content,0,$blocksize); |
|
222 | + $content = substr($content,$blocksize); |
|
223 | + $offset = $blocksize+$adjustedRange["start"]; |
|
224 | + } |
|
225 | + } |
|
226 | + if(!empty($content)){ |
|
227 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
228 | + mcrypt_generic_init($td,$cek,$iv); |
|
229 | + $decoded = mdecrypt_generic($td,$content); |
|
230 | + mcrypt_generic_deinit($td); |
|
231 | + mcrypt_module_close($td); |
|
232 | + }else{ |
|
233 | + $decoded = ""; |
|
234 | + } |
|
235 | 235 | |
236 | - //判断是否需要删除最后填充的字符,以及获取填充的字符 |
|
237 | - $needRemovePad = FALSE; |
|
238 | - $pad = NULL; |
|
239 | - if($offset+strlen($decoded) >=$contentLength){ |
|
240 | - $needRemovePad = TRUE; |
|
241 | - $pad = ord(substr($decoded,strlen($decoded)-1,1)); |
|
242 | - if($pad<=0||$pad>$blocksize) |
|
243 | - { |
|
244 | - //invalid pad |
|
245 | - $needRemovePad = FALSE; |
|
246 | - } |
|
247 | - } |
|
248 | - $endOffset = 0; |
|
249 | - if(isset($range)){ |
|
250 | - if($offset+strlen($decoded)>$range["end"]){ |
|
251 | - $preLength = strlen($decoded); |
|
252 | - $decoded = substr($decoded, 0,$range["end"]-$offset+1); |
|
253 | - $endOffset = $preLength-strlen($decoded); |
|
254 | - } |
|
255 | - if($offset<$range["start"]){ |
|
256 | - $decoded = substr($decoded,$range["start"] - $offset); |
|
257 | - } |
|
258 | - } |
|
259 | - //再次根据截取的长度判断是否需要删除最后填充的字符 |
|
260 | - if($needRemovePad&&$endOffset > $pad){ |
|
261 | - $needRemovePad = FALSE; |
|
262 | - } |
|
263 | - if($needRemovePad){ |
|
264 | - $padOffset = $pad-$endOffset; |
|
265 | - $actualWriteCount = strlen($decoded)-$padOffset; |
|
266 | - if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
|
267 | - $decoded = ""; |
|
268 | - else |
|
269 | - $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
270 | - } |
|
271 | - $s3Object["Content"] = $decoded; |
|
272 | - return $s3Object; |
|
273 | - } |
|
274 | - }else{ |
|
275 | - return $this->ks3client->getObject($args); |
|
276 | - } |
|
277 | - } |
|
278 | - public function initMultipartUploadSecurely($args=array()){ |
|
279 | - $sek = EncryptionUtil::genereateOnceUsedKey(); |
|
280 | - $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
|
281 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
282 | - $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
|
236 | + //判断是否需要删除最后填充的字符,以及获取填充的字符 |
|
237 | + $needRemovePad = FALSE; |
|
238 | + $pad = NULL; |
|
239 | + if($offset+strlen($decoded) >=$contentLength){ |
|
240 | + $needRemovePad = TRUE; |
|
241 | + $pad = ord(substr($decoded,strlen($decoded)-1,1)); |
|
242 | + if($pad<=0||$pad>$blocksize) |
|
243 | + { |
|
244 | + //invalid pad |
|
245 | + $needRemovePad = FALSE; |
|
246 | + } |
|
247 | + } |
|
248 | + $endOffset = 0; |
|
249 | + if(isset($range)){ |
|
250 | + if($offset+strlen($decoded)>$range["end"]){ |
|
251 | + $preLength = strlen($decoded); |
|
252 | + $decoded = substr($decoded, 0,$range["end"]-$offset+1); |
|
253 | + $endOffset = $preLength-strlen($decoded); |
|
254 | + } |
|
255 | + if($offset<$range["start"]){ |
|
256 | + $decoded = substr($decoded,$range["start"] - $offset); |
|
257 | + } |
|
258 | + } |
|
259 | + //再次根据截取的长度判断是否需要删除最后填充的字符 |
|
260 | + if($needRemovePad&&$endOffset > $pad){ |
|
261 | + $needRemovePad = FALSE; |
|
262 | + } |
|
263 | + if($needRemovePad){ |
|
264 | + $padOffset = $pad-$endOffset; |
|
265 | + $actualWriteCount = strlen($decoded)-$padOffset; |
|
266 | + if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
|
267 | + $decoded = ""; |
|
268 | + else |
|
269 | + $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
270 | + } |
|
271 | + $s3Object["Content"] = $decoded; |
|
272 | + return $s3Object; |
|
273 | + } |
|
274 | + }else{ |
|
275 | + return $this->ks3client->getObject($args); |
|
276 | + } |
|
277 | + } |
|
278 | + public function initMultipartUploadSecurely($args=array()){ |
|
279 | + $sek = EncryptionUtil::genereateOnceUsedKey(); |
|
280 | + $encryptedSek = EncryptionUtil::encodeCek($this->encryptionMaterials,$sek); |
|
281 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
282 | + $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND); |
|
283 | 283 | |
284 | - $matdesc = "{}"; |
|
285 | - if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
|
286 | - $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
|
287 | - $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
|
288 | - $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
|
289 | - } |
|
284 | + $matdesc = "{}"; |
|
285 | + if(ENCRYPTPTION_STORAGE_MODE == "ObjectMetadata"){ |
|
286 | + $args["UserMeta"]["x-kss-meta-x-kss-key"] = base64_encode($encryptedSek); |
|
287 | + $args["UserMeta"]["x-kss-meta-x-kss-iv"] = base64_encode($iv); |
|
288 | + $args["UserMeta"]["x-kss-meta-x-kss-matdesc"] = $matdesc; |
|
289 | + } |
|
290 | 290 | |
291 | - $initResult = $this->ks3client->initMultipartUpload($args); |
|
291 | + $initResult = $this->ks3client->initMultipartUpload($args); |
|
292 | 292 | |
293 | - EncryptionUtil::initMultipartUploadContext($initResult,$iv,$sek,$encryptedSek,$matdesc); |
|
293 | + EncryptionUtil::initMultipartUploadContext($initResult,$iv,$sek,$encryptedSek,$matdesc); |
|
294 | 294 | |
295 | - return $initResult; |
|
296 | - } |
|
297 | - public function uploadPartSecurely($args=array()){ |
|
298 | - $uploadId = $args["Options"]["uploadId"]; |
|
299 | - $isLastPart = FALSE; |
|
300 | - $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
301 | - if(isset($args["LastPart"])) |
|
302 | - $isLastPart = $args["LastPart"]; |
|
303 | - $exists = EncryptionUtil::multipartUploadContextExists($uploadId); |
|
304 | - if(!$exists){ |
|
305 | - throw new Ks3ClientException("no such upload in cache/encryption/"); |
|
306 | - } |
|
307 | - $context = EncryptionUtil::getMultipartUploadContext($uploadId); |
|
308 | - if($context["lastPart"]){ |
|
309 | - throw new Ks3ClientException("this upload with uploadId ".$uploadId," has been upload last part"); |
|
310 | - } |
|
311 | - $plainTextLength = EncryptionUtil::plainTextLength($args); |
|
312 | - if($plainTextLength <= 0){ |
|
313 | - throw new Ks3ClientException("get content length failed ,unexpected content length ".$plainTextLength); |
|
314 | - } |
|
315 | - if(!$isLastPart){ |
|
316 | - if($plainTextLength % $blocksize != 0) |
|
317 | - throw new Ks3ClientException("Invalid part size,part size (".$plainTextLength.") must be multiples of the block size ".$blocksize); |
|
318 | - }else{ |
|
319 | - $args["ObjectMeta"]["Content-Length"] = $plainTextLength + ($blocksize - $plainTextLength%$blocksize); |
|
320 | - } |
|
321 | - $readCallBack = new AESCBCStreamReadCallBack(); |
|
322 | - $readCallBack->iv = base64_decode($context["nextIv"]); |
|
323 | - $readCallBack->cek = base64_decode($context["cek"]); |
|
324 | - $readCallBack->contentLength = $plainTextLength; |
|
325 | - $readCallBack->mutipartUpload = TRUE; |
|
326 | - $readCallBack->isLastPart = $isLastPart; |
|
327 | - $args["readCallBack"] = $readCallBack; |
|
295 | + return $initResult; |
|
296 | + } |
|
297 | + public function uploadPartSecurely($args=array()){ |
|
298 | + $uploadId = $args["Options"]["uploadId"]; |
|
299 | + $isLastPart = FALSE; |
|
300 | + $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
301 | + if(isset($args["LastPart"])) |
|
302 | + $isLastPart = $args["LastPart"]; |
|
303 | + $exists = EncryptionUtil::multipartUploadContextExists($uploadId); |
|
304 | + if(!$exists){ |
|
305 | + throw new Ks3ClientException("no such upload in cache/encryption/"); |
|
306 | + } |
|
307 | + $context = EncryptionUtil::getMultipartUploadContext($uploadId); |
|
308 | + if($context["lastPart"]){ |
|
309 | + throw new Ks3ClientException("this upload with uploadId ".$uploadId," has been upload last part"); |
|
310 | + } |
|
311 | + $plainTextLength = EncryptionUtil::plainTextLength($args); |
|
312 | + if($plainTextLength <= 0){ |
|
313 | + throw new Ks3ClientException("get content length failed ,unexpected content length ".$plainTextLength); |
|
314 | + } |
|
315 | + if(!$isLastPart){ |
|
316 | + if($plainTextLength % $blocksize != 0) |
|
317 | + throw new Ks3ClientException("Invalid part size,part size (".$plainTextLength.") must be multiples of the block size ".$blocksize); |
|
318 | + }else{ |
|
319 | + $args["ObjectMeta"]["Content-Length"] = $plainTextLength + ($blocksize - $plainTextLength%$blocksize); |
|
320 | + } |
|
321 | + $readCallBack = new AESCBCStreamReadCallBack(); |
|
322 | + $readCallBack->iv = base64_decode($context["nextIv"]); |
|
323 | + $readCallBack->cek = base64_decode($context["cek"]); |
|
324 | + $readCallBack->contentLength = $plainTextLength; |
|
325 | + $readCallBack->mutipartUpload = TRUE; |
|
326 | + $readCallBack->isLastPart = $isLastPart; |
|
327 | + $args["readCallBack"] = $readCallBack; |
|
328 | 328 | |
329 | - $upResult = $this->ks3client->uploadPart($args); |
|
330 | - EncryptionUtil::updateMultipartUploadContext($uploadId,$readCallBack->iv,$isLastPart); |
|
331 | - return $upResult; |
|
332 | - } |
|
333 | - public function abortMultipartUploadSecurely($args=array()){ |
|
334 | - $uploadId = $args["Options"]["uploadId"]; |
|
335 | - EncryptionUtil::deleteMultipartUploadContext($uploadId); |
|
336 | - return $this->ks3client->abortMultipartUpload($args); |
|
337 | - } |
|
338 | - public function completeMultipartUploadSecurely($args=array()){ |
|
339 | - $uploadId = $args["Options"]["uploadId"]; |
|
340 | - $exists = EncryptionUtil::multipartUploadContextExists($uploadId); |
|
341 | - if(!$exists){ |
|
342 | - throw new Ks3ClientException("no such upload in cache/encryption/"); |
|
343 | - } |
|
344 | - $context = EncryptionUtil::getMultipartUploadContext($uploadId); |
|
345 | - if(!$context["lastPart"]){ |
|
346 | - throw new Ks3ClientException("Unable to complete an encrypted multipart upload without being told which part was the last. when upload part you can add item in args like args[\"LastPart\"]=TRUE"); |
|
347 | - } |
|
348 | - $result = $this->ks3client->completeMultipartUpload($args); |
|
349 | - if(ENCRYPTPTION_STORAGE_MODE=="InstructionFile"){ |
|
350 | - $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
|
351 | - $context["encryptedCek"],$context["firstIv"],$context["matdesc"]); |
|
352 | - $this->ks3client->putObjectByContent($req); |
|
353 | - } |
|
354 | - EncryptionUtil::deleteMultipartUploadContext($uploadId); |
|
355 | - return $result; |
|
356 | - } |
|
329 | + $upResult = $this->ks3client->uploadPart($args); |
|
330 | + EncryptionUtil::updateMultipartUploadContext($uploadId,$readCallBack->iv,$isLastPart); |
|
331 | + return $upResult; |
|
332 | + } |
|
333 | + public function abortMultipartUploadSecurely($args=array()){ |
|
334 | + $uploadId = $args["Options"]["uploadId"]; |
|
335 | + EncryptionUtil::deleteMultipartUploadContext($uploadId); |
|
336 | + return $this->ks3client->abortMultipartUpload($args); |
|
337 | + } |
|
338 | + public function completeMultipartUploadSecurely($args=array()){ |
|
339 | + $uploadId = $args["Options"]["uploadId"]; |
|
340 | + $exists = EncryptionUtil::multipartUploadContextExists($uploadId); |
|
341 | + if(!$exists){ |
|
342 | + throw new Ks3ClientException("no such upload in cache/encryption/"); |
|
343 | + } |
|
344 | + $context = EncryptionUtil::getMultipartUploadContext($uploadId); |
|
345 | + if(!$context["lastPart"]){ |
|
346 | + throw new Ks3ClientException("Unable to complete an encrypted multipart upload without being told which part was the last. when upload part you can add item in args like args[\"LastPart\"]=TRUE"); |
|
347 | + } |
|
348 | + $result = $this->ks3client->completeMultipartUpload($args); |
|
349 | + if(ENCRYPTPTION_STORAGE_MODE=="InstructionFile"){ |
|
350 | + $req = EncryptionUtil::createInstructionFile($args["Bucket"],$args["Key"], |
|
351 | + $context["encryptedCek"],$context["firstIv"],$context["matdesc"]); |
|
352 | + $this->ks3client->putObjectByContent($req); |
|
353 | + } |
|
354 | + EncryptionUtil::deleteMultipartUploadContext($uploadId); |
|
355 | + return $result; |
|
356 | + } |
|
357 | 357 | } |
358 | 358 | ?> |
359 | 359 | \ No newline at end of file |
@@ -2,226 +2,226 @@ |
||
2 | 2 | require_once KS3_API_PATH.DIRECTORY_SEPARATOR."encryption".DIRECTORY_SEPARATOR."EncryptionUtil.php"; |
3 | 3 | //下载 |
4 | 4 | class AESCBCStreamWriteCallBack{ |
5 | - private $iv; |
|
6 | - private $cek; |
|
7 | - private $contentLength; |
|
8 | - //数组,分别为上限和下限 |
|
9 | - private $expectedRange; |
|
10 | - //经过调整后的range |
|
11 | - private $adjustedRange; |
|
12 | - //当前指针位置 |
|
13 | - private $currentIndex; |
|
14 | - private $buffer;//上一次调用streaming_write_callback后,未解码的数据 |
|
15 | - private $firstWrite = TRUE; |
|
16 | - public function __set($property_name, $value){ |
|
17 | - $this->$property_name=$value; |
|
18 | - } |
|
19 | - public function __get($property_name){ |
|
20 | - if(isset($this->$property_name)) |
|
21 | - { |
|
22 | - return($this->$property_name); |
|
23 | - }else |
|
24 | - { |
|
25 | - return(NULL); |
|
26 | - } |
|
27 | - } |
|
28 | - //最后的数据大小肯定是blocksize的倍数,所以最后buffer中不会有未解密的内容。否则可以认为该文件是错误的 |
|
29 | - public function streaming_write_callback($curl_handle,$data,$write_stream){ |
|
30 | - $data = $this->buffer.$data; |
|
5 | + private $iv; |
|
6 | + private $cek; |
|
7 | + private $contentLength; |
|
8 | + //数组,分别为上限和下限 |
|
9 | + private $expectedRange; |
|
10 | + //经过调整后的range |
|
11 | + private $adjustedRange; |
|
12 | + //当前指针位置 |
|
13 | + private $currentIndex; |
|
14 | + private $buffer;//上一次调用streaming_write_callback后,未解码的数据 |
|
15 | + private $firstWrite = TRUE; |
|
16 | + public function __set($property_name, $value){ |
|
17 | + $this->$property_name=$value; |
|
18 | + } |
|
19 | + public function __get($property_name){ |
|
20 | + if(isset($this->$property_name)) |
|
21 | + { |
|
22 | + return($this->$property_name); |
|
23 | + }else |
|
24 | + { |
|
25 | + return(NULL); |
|
26 | + } |
|
27 | + } |
|
28 | + //最后的数据大小肯定是blocksize的倍数,所以最后buffer中不会有未解密的内容。否则可以认为该文件是错误的 |
|
29 | + public function streaming_write_callback($curl_handle,$data,$write_stream){ |
|
30 | + $data = $this->buffer.$data; |
|
31 | 31 | |
32 | - $length = strlen($data); |
|
33 | - //不能把上次的没读完的长度算在这次里,应该算在上次 |
|
34 | - $written_total = 0-strlen($this->buffer); |
|
35 | - $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
36 | - if($length<$blocksize) |
|
37 | - $this->buffer = $data; |
|
38 | - else{ |
|
39 | - //如果期望的范围之后还有数据,则认为数据已经接收完毕。不做任何处理 |
|
40 | - if($this->expectedRange["end"] < $this->expectedRange["start"]){ |
|
41 | - return $written_total+strlen($data); |
|
42 | - } |
|
43 | - $this->buffer = substr($data,$length - $length%$blocksize); |
|
44 | - $data = substr($data,0,$length - $length%$blocksize); |
|
32 | + $length = strlen($data); |
|
33 | + //不能把上次的没读完的长度算在这次里,应该算在上次 |
|
34 | + $written_total = 0-strlen($this->buffer); |
|
35 | + $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC); |
|
36 | + if($length<$blocksize) |
|
37 | + $this->buffer = $data; |
|
38 | + else{ |
|
39 | + //如果期望的范围之后还有数据,则认为数据已经接收完毕。不做任何处理 |
|
40 | + if($this->expectedRange["end"] < $this->expectedRange["start"]){ |
|
41 | + return $written_total+strlen($data); |
|
42 | + } |
|
43 | + $this->buffer = substr($data,$length - $length%$blocksize); |
|
44 | + $data = substr($data,0,$length - $length%$blocksize); |
|
45 | 45 | |
46 | - $ivoffset = 0; |
|
47 | - //range get时,如果不是从刚开始,则应该取加密后数据的前16个字节作为之后解密的iv |
|
48 | - if($this->firstWrite){ |
|
49 | - $this->firstWrite = FALSE; |
|
50 | - if(!$this->isBegin()){ |
|
51 | - $this->iv = substr($data,0,$blocksize); |
|
52 | - $data = substr($data,$blocksize); |
|
53 | - $ivoffset = $blocksize; |
|
54 | - } |
|
55 | - //初始化当前位置 |
|
56 | - if(isset($this->adjustedRange)) |
|
57 | - $this->currentIndex = $ivoffset+$this->adjustedRange["start"]; |
|
58 | - else |
|
59 | - $this->currentIndex = $ivoffset; |
|
60 | - } |
|
61 | - $written_total+=$ivoffset; |
|
62 | - if(strlen($data) == 0){ |
|
63 | - $decoded = ""; |
|
64 | - return $written_total; |
|
65 | - }else{ |
|
66 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
67 | - mcrypt_generic_init($td,$this->cek,$this->iv); |
|
68 | - $decoded = mdecrypt_generic($td,$data); |
|
69 | - mcrypt_generic_deinit($td); |
|
70 | - mcrypt_module_close($td); |
|
71 | - } |
|
46 | + $ivoffset = 0; |
|
47 | + //range get时,如果不是从刚开始,则应该取加密后数据的前16个字节作为之后解密的iv |
|
48 | + if($this->firstWrite){ |
|
49 | + $this->firstWrite = FALSE; |
|
50 | + if(!$this->isBegin()){ |
|
51 | + $this->iv = substr($data,0,$blocksize); |
|
52 | + $data = substr($data,$blocksize); |
|
53 | + $ivoffset = $blocksize; |
|
54 | + } |
|
55 | + //初始化当前位置 |
|
56 | + if(isset($this->adjustedRange)) |
|
57 | + $this->currentIndex = $ivoffset+$this->adjustedRange["start"]; |
|
58 | + else |
|
59 | + $this->currentIndex = $ivoffset; |
|
60 | + } |
|
61 | + $written_total+=$ivoffset; |
|
62 | + if(strlen($data) == 0){ |
|
63 | + $decoded = ""; |
|
64 | + return $written_total; |
|
65 | + }else{ |
|
66 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
67 | + mcrypt_generic_init($td,$this->cek,$this->iv); |
|
68 | + $decoded = mdecrypt_generic($td,$data); |
|
69 | + mcrypt_generic_deinit($td); |
|
70 | + mcrypt_module_close($td); |
|
71 | + } |
|
72 | 72 | |
73 | - $this->iv = substr($data,strlen($data)-$blocksize); |
|
74 | - //判断是否需要删除最后填充的字符,以及获取填充的字符 |
|
75 | - $needRemovePad = FALSE; |
|
76 | - $pad = NULL; |
|
73 | + $this->iv = substr($data,strlen($data)-$blocksize); |
|
74 | + //判断是否需要删除最后填充的字符,以及获取填充的字符 |
|
75 | + $needRemovePad = FALSE; |
|
76 | + $pad = NULL; |
|
77 | 77 | |
78 | - if($this->currentIndex+strlen($decoded) >=$this->contentLength){ |
|
79 | - $needRemovePad = TRUE; |
|
80 | - $pad = ord(substr($decoded,strlen($decoded)-1,1)); |
|
81 | - if($pad<=0||$pad>$blocksize) |
|
82 | - { |
|
83 | - //invalid pad |
|
84 | - $needRemovePad = FALSE; |
|
85 | - } |
|
86 | - } |
|
78 | + if($this->currentIndex+strlen($decoded) >=$this->contentLength){ |
|
79 | + $needRemovePad = TRUE; |
|
80 | + $pad = ord(substr($decoded,strlen($decoded)-1,1)); |
|
81 | + if($pad<=0||$pad>$blocksize) |
|
82 | + { |
|
83 | + //invalid pad |
|
84 | + $needRemovePad = FALSE; |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - //将解密后的数据截取到期望的长度 |
|
89 | - $startOffset = 0; |
|
90 | - $endOffset = 0; |
|
91 | - if(isset($this->expectedRange)){ |
|
92 | - $trueEnd = $expectedEnd = $this->expectedRange["end"]; |
|
88 | + //将解密后的数据截取到期望的长度 |
|
89 | + $startOffset = 0; |
|
90 | + $endOffset = 0; |
|
91 | + if(isset($this->expectedRange)){ |
|
92 | + $trueEnd = $expectedEnd = $this->expectedRange["end"]; |
|
93 | 93 | |
94 | - if($this->currentIndex+strlen($decoded)>$expectedEnd){ |
|
95 | - $preLength = strlen($decoded); |
|
96 | - $decoded = substr($decoded, 0,$expectedEnd-$this->currentIndex+1); |
|
97 | - $endOffset = $preLength-strlen($decoded); |
|
98 | - }else{ |
|
99 | - //因为range是开始结束都计算的,range=1-2。currentIndex=1,长度是2,end=currentIndex+2-1 |
|
100 | - $trueEnd = $this->currentIndex+strlen($decoded)-1; |
|
101 | - } |
|
102 | - $expectedStart = $this->expectedRange["start"]; |
|
103 | - if($this->currentIndex<$expectedStart){ |
|
104 | - $decoded = substr($decoded,$expectedStart - $this->currentIndex); |
|
105 | - $startOffset = $expectedStart - $this->currentIndex; |
|
106 | - } |
|
107 | - //调整下次期望的开始 |
|
108 | - $this->expectedRange["start"] = $trueEnd+1; |
|
109 | - } |
|
94 | + if($this->currentIndex+strlen($decoded)>$expectedEnd){ |
|
95 | + $preLength = strlen($decoded); |
|
96 | + $decoded = substr($decoded, 0,$expectedEnd-$this->currentIndex+1); |
|
97 | + $endOffset = $preLength-strlen($decoded); |
|
98 | + }else{ |
|
99 | + //因为range是开始结束都计算的,range=1-2。currentIndex=1,长度是2,end=currentIndex+2-1 |
|
100 | + $trueEnd = $this->currentIndex+strlen($decoded)-1; |
|
101 | + } |
|
102 | + $expectedStart = $this->expectedRange["start"]; |
|
103 | + if($this->currentIndex<$expectedStart){ |
|
104 | + $decoded = substr($decoded,$expectedStart - $this->currentIndex); |
|
105 | + $startOffset = $expectedStart - $this->currentIndex; |
|
106 | + } |
|
107 | + //调整下次期望的开始 |
|
108 | + $this->expectedRange["start"] = $trueEnd+1; |
|
109 | + } |
|
110 | 110 | |
111 | - $padOffset = 0; |
|
112 | - //再次根据截取的长度判断是否需要删除最后填充的字符 |
|
113 | - if($needRemovePad&&$endOffset > $pad){ |
|
114 | - $needRemovePad = FALSE; |
|
115 | - } |
|
116 | - $actualWriteCount = 0; |
|
117 | - if($needRemovePad){ |
|
118 | - $padOffset = $pad-$endOffset; |
|
119 | - $actualWriteCount = strlen($decoded)-$padOffset; |
|
120 | - if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
|
121 | - $decoded = ""; |
|
122 | - else |
|
123 | - $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
124 | - } |
|
125 | - $count = fwrite($write_stream, $decoded); |
|
126 | - if($count == 0) |
|
127 | - $count = $actualWriteCount; |
|
128 | - $count += $padOffset; |
|
129 | - $count += $startOffset; |
|
130 | - $count += $endOffset; |
|
131 | - $this->currentIndex += $count; |
|
132 | - $written_total+=$count; |
|
133 | - } |
|
134 | - //否则curl框架会报错 |
|
135 | - $written_total+=strlen($this->buffer); |
|
136 | - return $written_total; |
|
137 | - } |
|
138 | - //是的话则使用初始化IV |
|
139 | - private function isBegin(){ |
|
140 | - $beginIndex = 0; |
|
141 | - if(isset($this->adjustedRange["start"])) |
|
142 | - $beginIndex = $this->adjustedRange["start"]; |
|
143 | - if($beginIndex == 0) |
|
144 | - return TRUE; |
|
145 | - else |
|
146 | - return FALSE; |
|
147 | - } |
|
111 | + $padOffset = 0; |
|
112 | + //再次根据截取的长度判断是否需要删除最后填充的字符 |
|
113 | + if($needRemovePad&&$endOffset > $pad){ |
|
114 | + $needRemovePad = FALSE; |
|
115 | + } |
|
116 | + $actualWriteCount = 0; |
|
117 | + if($needRemovePad){ |
|
118 | + $padOffset = $pad-$endOffset; |
|
119 | + $actualWriteCount = strlen($decoded)-$padOffset; |
|
120 | + if($actualWriteCount <= 0)//负数的情况就是用户期望的range里全是填充的 |
|
121 | + $decoded = ""; |
|
122 | + else |
|
123 | + $decoded = substr($decoded,0,strlen($decoded)-$padOffset); |
|
124 | + } |
|
125 | + $count = fwrite($write_stream, $decoded); |
|
126 | + if($count == 0) |
|
127 | + $count = $actualWriteCount; |
|
128 | + $count += $padOffset; |
|
129 | + $count += $startOffset; |
|
130 | + $count += $endOffset; |
|
131 | + $this->currentIndex += $count; |
|
132 | + $written_total+=$count; |
|
133 | + } |
|
134 | + //否则curl框架会报错 |
|
135 | + $written_total+=strlen($this->buffer); |
|
136 | + return $written_total; |
|
137 | + } |
|
138 | + //是的话则使用初始化IV |
|
139 | + private function isBegin(){ |
|
140 | + $beginIndex = 0; |
|
141 | + if(isset($this->adjustedRange["start"])) |
|
142 | + $beginIndex = $this->adjustedRange["start"]; |
|
143 | + if($beginIndex == 0) |
|
144 | + return TRUE; |
|
145 | + else |
|
146 | + return FALSE; |
|
147 | + } |
|
148 | 148 | } |
149 | 149 | //上传 |
150 | 150 | class AESCBCStreamReadCallBack{ |
151 | - private $iv; |
|
152 | - private $cek; |
|
153 | - private $contentLength; |
|
154 | - private $buffer; |
|
155 | - private $hasread = 0; |
|
156 | - private $mutipartUpload =FALSE; |
|
157 | - private $isLastPart = FALSE; |
|
158 | - public function __set($property_name, $value){ |
|
159 | - $this->$property_name=$value; |
|
160 | - } |
|
161 | - public function __get($property_name){ |
|
162 | - if(isset($this->$property_name)) |
|
163 | - { |
|
164 | - return($this->$property_name); |
|
165 | - }else |
|
166 | - { |
|
167 | - return(NULL); |
|
168 | - } |
|
169 | - } |
|
170 | - public function streaming_read_callback($curl_handle,$file_handle,$length,$read_stream,$seek_position){ |
|
171 | - // Once we've sent as much as we're supposed to send... |
|
172 | - if ($this->hasread >= $this->contentLength) |
|
173 | - { |
|
174 | - // Send EOF |
|
175 | - return ''; |
|
176 | - } |
|
177 | - // If we're at the beginning of an upload and need to seek... |
|
178 | - if ($this->hasread == 0 && $seek_position>0 && $seek_position !== ftell($read_stream)) |
|
179 | - { |
|
180 | - if (fseek($read_stream, $seek_position) !== 0) |
|
181 | - { |
|
182 | - throw new RequestCore_Exception('The stream does not support seeking and is either not at the requested position or the position is unknown.'); |
|
183 | - } |
|
184 | - } |
|
151 | + private $iv; |
|
152 | + private $cek; |
|
153 | + private $contentLength; |
|
154 | + private $buffer; |
|
155 | + private $hasread = 0; |
|
156 | + private $mutipartUpload =FALSE; |
|
157 | + private $isLastPart = FALSE; |
|
158 | + public function __set($property_name, $value){ |
|
159 | + $this->$property_name=$value; |
|
160 | + } |
|
161 | + public function __get($property_name){ |
|
162 | + if(isset($this->$property_name)) |
|
163 | + { |
|
164 | + return($this->$property_name); |
|
165 | + }else |
|
166 | + { |
|
167 | + return(NULL); |
|
168 | + } |
|
169 | + } |
|
170 | + public function streaming_read_callback($curl_handle,$file_handle,$length,$read_stream,$seek_position){ |
|
171 | + // Once we've sent as much as we're supposed to send... |
|
172 | + if ($this->hasread >= $this->contentLength) |
|
173 | + { |
|
174 | + // Send EOF |
|
175 | + return ''; |
|
176 | + } |
|
177 | + // If we're at the beginning of an upload and need to seek... |
|
178 | + if ($this->hasread == 0 && $seek_position>0 && $seek_position !== ftell($read_stream)) |
|
179 | + { |
|
180 | + if (fseek($read_stream, $seek_position) !== 0) |
|
181 | + { |
|
182 | + throw new RequestCore_Exception('The stream does not support seeking and is either not at the requested position or the position is unknown.'); |
|
183 | + } |
|
184 | + } |
|
185 | 185 | |
186 | 186 | |
187 | - $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB); |
|
188 | - $needRead = min($this->contentLength - $this->hasread,$length); |
|
189 | - $read = fread($read_stream,$needRead); |
|
190 | - $this->hasread += strlen($read); |
|
191 | - $isLast = FALSE; |
|
192 | - if($this->hasread >= $this->contentLength){ |
|
193 | - $isLast = TRUE; |
|
194 | - } |
|
195 | - $data = $this->buffer.$read; |
|
187 | + $blocksize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_ECB); |
|
188 | + $needRead = min($this->contentLength - $this->hasread,$length); |
|
189 | + $read = fread($read_stream,$needRead); |
|
190 | + $this->hasread += strlen($read); |
|
191 | + $isLast = FALSE; |
|
192 | + if($this->hasread >= $this->contentLength){ |
|
193 | + $isLast = TRUE; |
|
194 | + } |
|
195 | + $data = $this->buffer.$read; |
|
196 | 196 | |
197 | - $dataLength = strlen($data); |
|
197 | + $dataLength = strlen($data); |
|
198 | 198 | |
199 | - if(!$isLast){ |
|
200 | - $this->buffer = substr($data,$dataLength-$dataLength%$blocksize); |
|
201 | - $data = substr($data, 0,$dataLength-$dataLength%$blocksize); |
|
202 | - }else{ |
|
203 | - //分块上传除最后一块外肯定是blocksize大小的倍数,所以不需要填充。 |
|
204 | - if($this->mutipartUpload){ |
|
205 | - if($this->isLastPart){ |
|
206 | - $this->buffer = NULL; |
|
207 | - $data = EncryptionUtil::PKCS5Padding($data,$blocksize); |
|
208 | - }else{ |
|
209 | - //donothing |
|
210 | - } |
|
211 | - }else{ |
|
212 | - $this->buffer = NULL; |
|
213 | - $data = EncryptionUtil::PKCS5Padding($data,$blocksize); |
|
214 | - } |
|
215 | - } |
|
216 | - $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
217 | - mcrypt_generic_init($td,$this->cek,$this->iv); |
|
218 | - $encrypted = mcrypt_generic($td,$data); |
|
219 | - mcrypt_generic_deinit($td); |
|
220 | - //去除自动填充的16个字节//php的当恰好为16的倍数时竟然不填充? |
|
221 | - //$encrypted = substr($encrypted,0,strlen($encrypted)-$blocksize); |
|
222 | - //取最后一个block作为下一次的iv |
|
223 | - $this->iv = substr($encrypted, strlen($encrypted)-$blocksize); |
|
224 | - return $encrypted; |
|
225 | - } |
|
199 | + if(!$isLast){ |
|
200 | + $this->buffer = substr($data,$dataLength-$dataLength%$blocksize); |
|
201 | + $data = substr($data, 0,$dataLength-$dataLength%$blocksize); |
|
202 | + }else{ |
|
203 | + //分块上传除最后一块外肯定是blocksize大小的倍数,所以不需要填充。 |
|
204 | + if($this->mutipartUpload){ |
|
205 | + if($this->isLastPart){ |
|
206 | + $this->buffer = NULL; |
|
207 | + $data = EncryptionUtil::PKCS5Padding($data,$blocksize); |
|
208 | + }else{ |
|
209 | + //donothing |
|
210 | + } |
|
211 | + }else{ |
|
212 | + $this->buffer = NULL; |
|
213 | + $data = EncryptionUtil::PKCS5Padding($data,$blocksize); |
|
214 | + } |
|
215 | + } |
|
216 | + $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'',MCRYPT_MODE_CBC,''); |
|
217 | + mcrypt_generic_init($td,$this->cek,$this->iv); |
|
218 | + $encrypted = mcrypt_generic($td,$data); |
|
219 | + mcrypt_generic_deinit($td); |
|
220 | + //去除自动填充的16个字节//php的当恰好为16的倍数时竟然不填充? |
|
221 | + //$encrypted = substr($encrypted,0,strlen($encrypted)-$blocksize); |
|
222 | + //取最后一个block作为下一次的iv |
|
223 | + $this->iv = substr($encrypted, strlen($encrypted)-$blocksize); |
|
224 | + return $encrypted; |
|
225 | + } |
|
226 | 226 | } |
227 | 227 | ?> |
228 | 228 | \ No newline at end of file |