1 | v<?php
|
---|
2 | /**
|
---|
3 | * Helper class for building multipart/form-data request body
|
---|
4 | *
|
---|
5 | * PHP version 5
|
---|
6 | *
|
---|
7 | * LICENSE:
|
---|
8 | *
|
---|
9 | * Copyright (c) 2008, Alexey Borzov <avb@php.net>
|
---|
10 | * All rights reserved.
|
---|
11 | *
|
---|
12 | * Redistribution and use in source and binary forms, with or without
|
---|
13 | * modification, are permitted provided that the following conditions
|
---|
14 | * are met:
|
---|
15 | *
|
---|
16 | * * Redistributions of source code must retain the above copyright
|
---|
17 | * notice, this list of conditions and the following disclaimer.
|
---|
18 | * * Redistributions in binary form must reproduce the above copyright
|
---|
19 | * notice, this list of conditions and the following disclaimer in the
|
---|
20 | * documentation and/or other materials provided with the distribution.
|
---|
21 | * * The names of the authors may not be used to endorse or promote products
|
---|
22 | * derived from this software without specific prior written permission.
|
---|
23 | *
|
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
---|
25 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
---|
26 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
---|
28 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
30 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
31 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
---|
32 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
35 | *
|
---|
36 | * @category HTTP
|
---|
37 | * @package HTTP_Request2
|
---|
38 | * @author Alexey Borzov <avb@php.net>
|
---|
39 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
---|
40 | * @version CVS: $Id: MultipartBody.php,v 1.3 2008/11/15 10:59:30 avb Exp $
|
---|
41 | * @link http://pear.php.net/package/HTTP_Request2
|
---|
42 | */
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Class for building multipart/form-data request body
|
---|
46 | *
|
---|
47 | * The class helps to reduce memory consumption by streaming large file uploads
|
---|
48 | * from disk, it also allows monitoring of upload progress (see request #7630)
|
---|
49 | *
|
---|
50 | * @category HTTP
|
---|
51 | * @package HTTP_Request2
|
---|
52 | * @author Alexey Borzov <avb@php.net>
|
---|
53 | * @version Release: 0.1.0
|
---|
54 | * @link http://tools.ietf.org/html/rfc1867
|
---|
55 | */
|
---|
56 | class net_php_pear_HTTP_Request2_MultipartBody
|
---|
57 | {
|
---|
58 | /**
|
---|
59 | * MIME boundary
|
---|
60 | * @var string
|
---|
61 | */
|
---|
62 | private $_boundary;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Form parameters added via {@link HTTP_Request2::addPostParameter()}
|
---|
66 | * @var array
|
---|
67 | */
|
---|
68 | private $_params = array();
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * File uploads added via {@link HTTP_Request2::addUpload()}
|
---|
72 | * @var array
|
---|
73 | */
|
---|
74 | private $_uploads = array();
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Header for parts with parameters
|
---|
78 | * @var string
|
---|
79 | */
|
---|
80 | private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n";
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Header for parts with uploads
|
---|
84 | * @var string
|
---|
85 | */
|
---|
86 | private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n";
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Current position in parameter and upload arrays
|
---|
90 | *
|
---|
91 | * First number is index of "current" part, second number is position within
|
---|
92 | * "current" part
|
---|
93 | *
|
---|
94 | * @var array
|
---|
95 | */
|
---|
96 | private $_pos = array(0, 0);
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Constructor. Sets the arrays with POST data.
|
---|
101 | *
|
---|
102 | * @param array values of form fields set via {@link HTTP_Request2::addPostParameter()}
|
---|
103 | * @param array file uploads set via {@link HTTP_Request2::addUpload()}
|
---|
104 | * @param bool whether to append brackets to array variable names
|
---|
105 | */
|
---|
106 | public function __construct($params, $uploads, $useBrackets = true)
|
---|
107 | {
|
---|
108 | $this->_params = self::_flattenArray('', $params, $useBrackets);
|
---|
109 | foreach ($uploads as $fieldName => $f) {
|
---|
110 | if (!is_array($f['fp'])) {
|
---|
111 | $this->_uploads[] = $f + array('name' => $fieldName);
|
---|
112 | } else {
|
---|
113 | for ($i = 0; $i < count($f['fp']); $i++) {
|
---|
114 | $upload = array(
|
---|
115 | 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName)
|
---|
116 | );
|
---|
117 | foreach (array('fp', 'filename', 'size', 'type') as $key) {
|
---|
118 | $upload[$key] = $f[$key][$i];
|
---|
119 | }
|
---|
120 | $this->_uploads[] = $upload;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Returns the length of the body to use in Content-Length header
|
---|
128 | *
|
---|
129 | * @return integer
|
---|
130 | */
|
---|
131 | public function getLength()
|
---|
132 | {
|
---|
133 | $boundaryLength = strlen($this->getBoundary());
|
---|
134 | $headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength;
|
---|
135 | $headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength;
|
---|
136 | $length = $boundaryLength + 6;
|
---|
137 | foreach ($this->_params as $p) {
|
---|
138 | $length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2;
|
---|
139 | }
|
---|
140 | foreach ($this->_uploads as $u) {
|
---|
141 | $length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) +
|
---|
142 | strlen($u['filename']) + $u['size'] + 2;
|
---|
143 | }
|
---|
144 | return $length;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Returns the boundary to use in Content-Type header
|
---|
149 | *
|
---|
150 | * @return string
|
---|
151 | */
|
---|
152 | public function getBoundary()
|
---|
153 | {
|
---|
154 | if (empty($this->_boundary)) {
|
---|
155 | $this->_boundary = 'PEAR-HTTP_Request2-' . md5(microtime());
|
---|
156 | }
|
---|
157 | return $this->_boundary;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns next chunk of request body
|
---|
162 | *
|
---|
163 | * @param integer Amount of bytes to read
|
---|
164 | * @return string Up to $length bytes of data, empty string if at end
|
---|
165 | */
|
---|
166 | public function read($length)
|
---|
167 | {
|
---|
168 | $ret = '';
|
---|
169 | $boundary = $this->getBoundary();
|
---|
170 | $paramCount = count($this->_params);
|
---|
171 | $uploadCount = count($this->_uploads);
|
---|
172 | while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) {
|
---|
173 | $oldLength = $length;
|
---|
174 | if ($this->_pos[0] < $paramCount) {
|
---|
175 | $param = sprintf($this->_headerParam, $boundary,
|
---|
176 | $this->_params[$this->_pos[0]][0]) .
|
---|
177 | $this->_params[$this->_pos[0]][1] . "\r\n";
|
---|
178 | $ret .= substr($param, $this->_pos[1], $length);
|
---|
179 | $length -= min(strlen($param) - $this->_pos[1], $length);
|
---|
180 |
|
---|
181 | } elseif ($this->_pos[0] < $paramCount + $uploadCount) {
|
---|
182 | $pos = $this->_pos[0] - $paramCount;
|
---|
183 | $header = sprintf($this->_headerUpload, $boundary,
|
---|
184 | $this->_uploads[$pos]['name'],
|
---|
185 | $this->_uploads[$pos]['filename'],
|
---|
186 | $this->_uploads[$pos]['type']);
|
---|
187 | if ($this->_pos[1] < strlen($header)) {
|
---|
188 | $ret .= substr($header, $this->_pos[1], $length);
|
---|
189 | $length -= min(strlen($header) - $this->_pos[1], $length);
|
---|
190 | }
|
---|
191 | $filePos = max(0, $this->_pos[1] - strlen($header));
|
---|
192 | if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) {
|
---|
193 | $ret .= fread($this->_uploads[$pos]['fp'], $length);
|
---|
194 | $length -= min($length, $this->_uploads[$pos]['size'] - $filePos);
|
---|
195 | }
|
---|
196 | if ($length > 0) {
|
---|
197 | $start = $this->_pos[1] + ($oldLength - $length) -
|
---|
198 | strlen($header) - $this->_uploads[$pos]['size'];
|
---|
199 | $ret .= substr("\r\n", $start, $length);
|
---|
200 | $length -= min(2 - $start, $length);
|
---|
201 | }
|
---|
202 |
|
---|
203 | } else {
|
---|
204 | $closing = '--' . $boundary . "--\r\n";
|
---|
205 | $ret .= substr($closing, $this->_pos[1], $length);
|
---|
206 | $length -= min(strlen($closing) - $this->_pos[1], $length);
|
---|
207 | }
|
---|
208 | if ($length > 0) {
|
---|
209 | $this->_pos = array($this->_pos[0] + 1, 0);
|
---|
210 | } else {
|
---|
211 | $this->_pos[1] += $oldLength;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | return $ret;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Sets the current position to the start of the body
|
---|
219 | *
|
---|
220 | * This allows reusing the same body in another request
|
---|
221 | */
|
---|
222 | public function rewind()
|
---|
223 | {
|
---|
224 | $this->_pos = array(0, 0);
|
---|
225 | foreach ($this->_uploads as $u) {
|
---|
226 | rewind($u['fp']);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Returns the body as string
|
---|
232 | *
|
---|
233 | * Note that it reads all file uploads into memory so it is a good idea not
|
---|
234 | * to use this method with large file uploads and rely on read() instead.
|
---|
235 | *
|
---|
236 | * @return string
|
---|
237 | */
|
---|
238 | public function __toString()
|
---|
239 | {
|
---|
240 | $this->rewind();
|
---|
241 | return $this->read($this->getLength());
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Helper function to change the (probably multidimensional) associative array
|
---|
247 | * into the simple one.
|
---|
248 | *
|
---|
249 | * @param string name for item
|
---|
250 | * @param mixed item's values
|
---|
251 | * @param bool whether to append [] to array variables' names
|
---|
252 | * @return array array with the following items: array('item name', 'item value');
|
---|
253 | */
|
---|
254 | private static function _flattenArray($name, $values, $useBrackets)
|
---|
255 | {
|
---|
256 | if (!is_array($values)) {
|
---|
257 | return array(array($name, $values));
|
---|
258 | } else {
|
---|
259 | $ret = array();
|
---|
260 | foreach ($values as $k => $v) {
|
---|
261 | if (empty($name)) {
|
---|
262 | $newName = $k;
|
---|
263 | } elseif ($useBrackets) {
|
---|
264 | $newName = $name . '[' . $k . ']';
|
---|
265 | } else {
|
---|
266 | $newName = $name;
|
---|
267 | }
|
---|
268 | $ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets));
|
---|
269 | }
|
---|
270 | return $ret;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 | ?>
|
---|