+++ /dev/null
-<?php\r
-/**\r
- * Helper class for building multipart/form-data request body\r
- *\r
- * PHP version 5\r
- *\r
- * LICENSE:\r
- *\r
- * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- *\r
- * * Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * * Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * * The names of the authors may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * @category HTTP\r
- * @package HTTP_Request2\r
- * @author Alexey Borzov <avb@php.net>\r
- * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
- * @version CVS: $Id: MultipartBody.php 287306 2009-08-14 15:22:52Z avb $\r
- * @link http://pear.php.net/package/HTTP_Request2\r
- */\r
-\r
-/**\r
- * Class for building multipart/form-data request body\r
- *\r
- * The class helps to reduce memory consumption by streaming large file uploads\r
- * from disk, it also allows monitoring of upload progress (see request #7630)\r
- *\r
- * @category HTTP\r
- * @package HTTP_Request2\r
- * @author Alexey Borzov <avb@php.net>\r
- * @version Release: 0.4.1\r
- * @link http://tools.ietf.org/html/rfc1867\r
- */\r
-class HTTP_Request2_MultipartBody\r
-{\r
- /**\r
- * MIME boundary\r
- * @var string\r
- */\r
- private $_boundary;\r
-\r
- /**\r
- * Form parameters added via {@link HTTP_Request2::addPostParameter()}\r
- * @var array\r
- */\r
- private $_params = array();\r
-\r
- /**\r
- * File uploads added via {@link HTTP_Request2::addUpload()}\r
- * @var array\r
- */\r
- private $_uploads = array();\r
-\r
- /**\r
- * Header for parts with parameters\r
- * @var string\r
- */\r
- private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n";\r
-\r
- /**\r
- * Header for parts with uploads\r
- * @var string\r
- */\r
- private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n";\r
-\r
- /**\r
- * Current position in parameter and upload arrays\r
- *\r
- * First number is index of "current" part, second number is position within\r
- * "current" part\r
- *\r
- * @var array\r
- */\r
- private $_pos = array(0, 0);\r
-\r
-\r
- /**\r
- * Constructor. Sets the arrays with POST data.\r
- *\r
- * @param array values of form fields set via {@link HTTP_Request2::addPostParameter()}\r
- * @param array file uploads set via {@link HTTP_Request2::addUpload()}\r
- * @param bool whether to append brackets to array variable names\r
- */\r
- public function __construct(array $params, array $uploads, $useBrackets = true)\r
- {\r
- $this->_params = self::_flattenArray('', $params, $useBrackets);\r
- foreach ($uploads as $fieldName => $f) {\r
- if (!is_array($f['fp'])) {\r
- $this->_uploads[] = $f + array('name' => $fieldName);\r
- } else {\r
- for ($i = 0; $i < count($f['fp']); $i++) {\r
- $upload = array(\r
- 'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName)\r
- );\r
- foreach (array('fp', 'filename', 'size', 'type') as $key) {\r
- $upload[$key] = $f[$key][$i];\r
- }\r
- $this->_uploads[] = $upload;\r
- }\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Returns the length of the body to use in Content-Length header\r
- *\r
- * @return integer\r
- */\r
- public function getLength()\r
- {\r
- $boundaryLength = strlen($this->getBoundary());\r
- $headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength;\r
- $headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength;\r
- $length = $boundaryLength + 6;\r
- foreach ($this->_params as $p) {\r
- $length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2;\r
- }\r
- foreach ($this->_uploads as $u) {\r
- $length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) +\r
- strlen($u['filename']) + $u['size'] + 2;\r
- }\r
- return $length;\r
- }\r
-\r
- /**\r
- * Returns the boundary to use in Content-Type header\r
- *\r
- * @return string\r
- */\r
- public function getBoundary()\r
- {\r
- if (empty($this->_boundary)) {\r
- $this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime());\r
- }\r
- return $this->_boundary;\r
- }\r
-\r
- /**\r
- * Returns next chunk of request body\r
- *\r
- * @param integer Amount of bytes to read\r
- * @return string Up to $length bytes of data, empty string if at end\r
- */\r
- public function read($length)\r
- {\r
- $ret = '';\r
- $boundary = $this->getBoundary();\r
- $paramCount = count($this->_params);\r
- $uploadCount = count($this->_uploads);\r
- while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) {\r
- $oldLength = $length;\r
- if ($this->_pos[0] < $paramCount) {\r
- $param = sprintf($this->_headerParam, $boundary, \r
- $this->_params[$this->_pos[0]][0]) .\r
- $this->_params[$this->_pos[0]][1] . "\r\n";\r
- $ret .= substr($param, $this->_pos[1], $length);\r
- $length -= min(strlen($param) - $this->_pos[1], $length);\r
-\r
- } elseif ($this->_pos[0] < $paramCount + $uploadCount) {\r
- $pos = $this->_pos[0] - $paramCount;\r
- $header = sprintf($this->_headerUpload, $boundary,\r
- $this->_uploads[$pos]['name'],\r
- $this->_uploads[$pos]['filename'],\r
- $this->_uploads[$pos]['type']);\r
- if ($this->_pos[1] < strlen($header)) {\r
- $ret .= substr($header, $this->_pos[1], $length);\r
- $length -= min(strlen($header) - $this->_pos[1], $length);\r
- }\r
- $filePos = max(0, $this->_pos[1] - strlen($header));\r
- if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) {\r
- $ret .= fread($this->_uploads[$pos]['fp'], $length);\r
- $length -= min($length, $this->_uploads[$pos]['size'] - $filePos);\r
- }\r
- if ($length > 0) {\r
- $start = $this->_pos[1] + ($oldLength - $length) -\r
- strlen($header) - $this->_uploads[$pos]['size'];\r
- $ret .= substr("\r\n", $start, $length);\r
- $length -= min(2 - $start, $length);\r
- }\r
-\r
- } else {\r
- $closing = '--' . $boundary . "--\r\n";\r
- $ret .= substr($closing, $this->_pos[1], $length);\r
- $length -= min(strlen($closing) - $this->_pos[1], $length);\r
- }\r
- if ($length > 0) {\r
- $this->_pos = array($this->_pos[0] + 1, 0);\r
- } else {\r
- $this->_pos[1] += $oldLength;\r
- }\r
- }\r
- return $ret;\r
- }\r
-\r
- /**\r
- * Sets the current position to the start of the body\r
- *\r
- * This allows reusing the same body in another request\r
- */\r
- public function rewind()\r
- {\r
- $this->_pos = array(0, 0);\r
- foreach ($this->_uploads as $u) {\r
- rewind($u['fp']);\r
- }\r
- }\r
-\r
- /**\r
- * Returns the body as string\r
- *\r
- * Note that it reads all file uploads into memory so it is a good idea not\r
- * to use this method with large file uploads and rely on read() instead.\r
- *\r
- * @return string\r
- */\r
- public function __toString()\r
- {\r
- $this->rewind();\r
- return $this->read($this->getLength());\r
- }\r
-\r
-\r
- /**\r
- * Helper function to change the (probably multidimensional) associative array\r
- * into the simple one.\r
- *\r
- * @param string name for item\r
- * @param mixed item's values\r
- * @param bool whether to append [] to array variables' names\r
- * @return array array with the following items: array('item name', 'item value');\r
- */\r
- private static function _flattenArray($name, $values, $useBrackets)\r
- {\r
- if (!is_array($values)) {\r
- return array(array($name, $values));\r
- } else {\r
- $ret = array();\r
- foreach ($values as $k => $v) {\r
- if (empty($name)) {\r
- $newName = $k;\r
- } elseif ($useBrackets) {\r
- $newName = $name . '[' . $k . ']';\r
- } else {\r
- $newName = $name;\r
- }\r
- $ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets));\r
- }\r
- return $ret;\r
- }\r
- }\r
-}\r
-?>\r