MultipartBody.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Helper class for building multipart/form-data request body
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to BSD 3-Clause License that is bundled
  10. * with this package in the file LICENSE and available at the URL
  11. * https://raw.github.com/pear/HTTP_Request2/trunk/docs/LICENSE
  12. *
  13. * @category HTTP
  14. * @package HTTP_Request2
  15. * @author Alexey Borzov <avb@php.net>
  16. * @copyright 2008-2014 Alexey Borzov <avb@php.net>
  17. * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
  18. * @link http://pear.php.net/package/HTTP_Request2
  19. */
  20. /** Exception class for HTTP_Request2 package */
  21. require_once 'HTTP/Request2/Exception.php';
  22. /**
  23. * Class for building multipart/form-data request body
  24. *
  25. * The class helps to reduce memory consumption by streaming large file uploads
  26. * from disk, it also allows monitoring of upload progress (see request #7630)
  27. *
  28. * @category HTTP
  29. * @package HTTP_Request2
  30. * @author Alexey Borzov <avb@php.net>
  31. * @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
  32. * @version Release: 2.2.1
  33. * @link http://pear.php.net/package/HTTP_Request2
  34. * @link http://tools.ietf.org/html/rfc1867
  35. */
  36. class HTTP_Request2_MultipartBody
  37. {
  38. /**
  39. * MIME boundary
  40. * @var string
  41. */
  42. private $_boundary;
  43. /**
  44. * Form parameters added via {@link HTTP_Request2::addPostParameter()}
  45. * @var array
  46. */
  47. private $_params = array();
  48. /**
  49. * File uploads added via {@link HTTP_Request2::addUpload()}
  50. * @var array
  51. */
  52. private $_uploads = array();
  53. /**
  54. * Header for parts with parameters
  55. * @var string
  56. */
  57. private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n";
  58. /**
  59. * Header for parts with uploads
  60. * @var string
  61. */
  62. private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n";
  63. /**
  64. * Current position in parameter and upload arrays
  65. *
  66. * First number is index of "current" part, second number is position within
  67. * "current" part
  68. *
  69. * @var array
  70. */
  71. private $_pos = array(0, 0);
  72. /**
  73. * Constructor. Sets the arrays with POST data.
  74. *
  75. * @param array $params values of form fields set via
  76. * {@link HTTP_Request2::addPostParameter()}
  77. * @param array $uploads file uploads set via
  78. * {@link HTTP_Request2::addUpload()}
  79. * @param bool $useBrackets whether to append brackets to array variable names
  80. */
  81. public function __construct(array $params, array $uploads, $useBrackets = true)
  82. {
  83. $this->_params = self::_flattenArray('', $params, $useBrackets);
  84. foreach ($uploads as $fieldName => $f) {
  85. if (!is_array($f['fp'])) {
  86. $this->_uploads[] = $f + array('name' => $fieldName);
  87. } else {
  88. for ($i = 0; $i < count($f['fp']); $i++) {
  89. $upload = array(
  90. 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName)
  91. );
  92. foreach (array('fp', 'filename', 'size', 'type') as $key) {
  93. $upload[$key] = $f[$key][$i];
  94. }
  95. $this->_uploads[] = $upload;
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Returns the length of the body to use in Content-Length header
  102. *
  103. * @return integer
  104. */
  105. public function getLength()
  106. {
  107. $boundaryLength = strlen($this->getBoundary());
  108. $headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength;
  109. $headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength;
  110. $length = $boundaryLength + 6;
  111. foreach ($this->_params as $p) {
  112. $length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2;
  113. }
  114. foreach ($this->_uploads as $u) {
  115. $length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) +
  116. strlen($u['filename']) + $u['size'] + 2;
  117. }
  118. return $length;
  119. }
  120. /**
  121. * Returns the boundary to use in Content-Type header
  122. *
  123. * @return string
  124. */
  125. public function getBoundary()
  126. {
  127. if (empty($this->_boundary)) {
  128. $this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime());
  129. }
  130. return $this->_boundary;
  131. }
  132. /**
  133. * Returns next chunk of request body
  134. *
  135. * @param integer $length Number of bytes to read
  136. *
  137. * @return string Up to $length bytes of data, empty string if at end
  138. * @throws HTTP_Request2_LogicException
  139. */
  140. public function read($length)
  141. {
  142. $ret = '';
  143. $boundary = $this->getBoundary();
  144. $paramCount = count($this->_params);
  145. $uploadCount = count($this->_uploads);
  146. while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) {
  147. $oldLength = $length;
  148. if ($this->_pos[0] < $paramCount) {
  149. $param = sprintf(
  150. $this->_headerParam, $boundary, $this->_params[$this->_pos[0]][0]
  151. ) . $this->_params[$this->_pos[0]][1] . "\r\n";
  152. $ret .= substr($param, $this->_pos[1], $length);
  153. $length -= min(strlen($param) - $this->_pos[1], $length);
  154. } elseif ($this->_pos[0] < $paramCount + $uploadCount) {
  155. $pos = $this->_pos[0] - $paramCount;
  156. $header = sprintf(
  157. $this->_headerUpload, $boundary, $this->_uploads[$pos]['name'],
  158. $this->_uploads[$pos]['filename'], $this->_uploads[$pos]['type']
  159. );
  160. if ($this->_pos[1] < strlen($header)) {
  161. $ret .= substr($header, $this->_pos[1], $length);
  162. $length -= min(strlen($header) - $this->_pos[1], $length);
  163. }
  164. $filePos = max(0, $this->_pos[1] - strlen($header));
  165. if ($filePos < $this->_uploads[$pos]['size']) {
  166. while ($length > 0 && !feof($this->_uploads[$pos]['fp'])) {
  167. if (false === ($chunk = fread($this->_uploads[$pos]['fp'], $length))) {
  168. throw new HTTP_Request2_LogicException(
  169. 'Failed reading file upload', HTTP_Request2_Exception::READ_ERROR
  170. );
  171. }
  172. $ret .= $chunk;
  173. $length -= strlen($chunk);
  174. }
  175. }
  176. if ($length > 0) {
  177. $start = $this->_pos[1] + ($oldLength - $length) -
  178. strlen($header) - $this->_uploads[$pos]['size'];
  179. $ret .= substr("\r\n", $start, $length);
  180. $length -= min(2 - $start, $length);
  181. }
  182. } else {
  183. $closing = '--' . $boundary . "--\r\n";
  184. $ret .= substr($closing, $this->_pos[1], $length);
  185. $length -= min(strlen($closing) - $this->_pos[1], $length);
  186. }
  187. if ($length > 0) {
  188. $this->_pos = array($this->_pos[0] + 1, 0);
  189. } else {
  190. $this->_pos[1] += $oldLength;
  191. }
  192. }
  193. return $ret;
  194. }
  195. /**
  196. * Sets the current position to the start of the body
  197. *
  198. * This allows reusing the same body in another request
  199. */
  200. public function rewind()
  201. {
  202. $this->_pos = array(0, 0);
  203. foreach ($this->_uploads as $u) {
  204. rewind($u['fp']);
  205. }
  206. }
  207. /**
  208. * Returns the body as string
  209. *
  210. * Note that it reads all file uploads into memory so it is a good idea not
  211. * to use this method with large file uploads and rely on read() instead.
  212. *
  213. * @return string
  214. */
  215. public function __toString()
  216. {
  217. $this->rewind();
  218. return $this->read($this->getLength());
  219. }
  220. /**
  221. * Helper function to change the (probably multidimensional) associative array
  222. * into the simple one.
  223. *
  224. * @param string $name name for item
  225. * @param mixed $values item's values
  226. * @param bool $useBrackets whether to append [] to array variables' names
  227. *
  228. * @return array array with the following items: array('item name', 'item value');
  229. */
  230. private static function _flattenArray($name, $values, $useBrackets)
  231. {
  232. if (!is_array($values)) {
  233. return array(array($name, $values));
  234. } else {
  235. $ret = array();
  236. foreach ($values as $k => $v) {
  237. if (empty($name)) {
  238. $newName = $k;
  239. } elseif ($useBrackets) {
  240. $newName = $name . '[' . $k . ']';
  241. } else {
  242. $newName = $name;
  243. }
  244. $ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets));
  245. }
  246. return $ret;
  247. }
  248. }
  249. }
  250. ?>