PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/backupbuddy2/destinations/dropbox/lib/dropbuddy/pear_includes/HTTP/Request2/MultipartBody.php

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