ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
MultipartStream.php
Go to the documentation of this file.
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
5 
11 {
13 
14  private $boundary;
15 
28  public function __construct(array $elements = [], $boundary = null)
29  {
30  $this->boundary = $boundary ?: sha1(uniqid('', true));
31  $this->stream = $this->createStream($elements);
32  }
33 
39  public function getBoundary()
40  {
41  return $this->boundary;
42  }
43 
44  public function isWritable()
45  {
46  return false;
47  }
48 
52  private function getHeaders(array $headers)
53  {
54  $str = '';
55  foreach ($headers as $key => $value) {
56  $str .= "{$key}: {$value}\r\n";
57  }
58 
59  return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
60  }
61 
65  protected function createStream(array $elements)
66  {
67  $stream = new AppendStream();
68 
69  foreach ($elements as $element) {
70  $this->addElement($stream, $element);
71  }
72 
73  // Add the trailing boundary with CRLF
74  $stream->addStream(stream_for("--{$this->boundary}--\r\n"));
75 
76  return $stream;
77  }
78 
79  private function addElement(AppendStream $stream, array $element)
80  {
81  foreach (['contents', 'name'] as $key) {
82  if (!array_key_exists($key, $element)) {
83  throw new \InvalidArgumentException("A '{$key}' key is required");
84  }
85  }
86 
87  $element['contents'] = stream_for($element['contents']);
88 
89  if (empty($element['filename'])) {
90  $uri = $element['contents']->getMetadata('uri');
91  if (substr($uri, 0, 6) !== 'php://') {
92  $element['filename'] = $uri;
93  }
94  }
95 
96  list($body, $headers) = $this->createElement(
97  $element['name'],
98  $element['contents'],
99  isset($element['filename']) ? $element['filename'] : null,
100  isset($element['headers']) ? $element['headers'] : []
101  );
102 
103  $stream->addStream(stream_for($this->getHeaders($headers)));
104  $stream->addStream($body);
105  $stream->addStream(stream_for("\r\n"));
106  }
107 
111  private function createElement($name, StreamInterface $stream, $filename, array $headers)
112  {
113  // Set a default content-disposition header if one was no provided
114  $disposition = $this->getHeader($headers, 'content-disposition');
115  if (!$disposition) {
116  $headers['Content-Disposition'] = ($filename === '0' || $filename)
117  ? sprintf('form-data; name="%s"; filename="%s"',
118  $name,
119  basename($filename))
120  : "form-data; name=\"{$name}\"";
121  }
122 
123  // Set a default content-length header if one was no provided
124  $length = $this->getHeader($headers, 'content-length');
125  if (!$length) {
126  if ($length = $stream->getSize()) {
127  $headers['Content-Length'] = (string) $length;
128  }
129  }
130 
131  // Set a default Content-Type if one was not supplied
132  $type = $this->getHeader($headers, 'content-type');
133  if (!$type && ($filename === '0' || $filename)) {
135  $headers['Content-Type'] = $type;
136  }
137  }
138 
139  return [$stream, $headers];
140  }
141 
142  private function getHeader(array $headers, $key)
143  {
144  $lowercaseHeader = strtolower($key);
145  foreach ($headers as $k => $v) {
146  if (strtolower($k) === $lowercaseHeader) {
147  return $v;
148  }
149  }
150 
151  return null;
152  }
153 }
isWritable()
Returns whether or not the stream is writable.
getHeader(array $headers, $key)
$type
mimetype_from_filename($filename)
Determines the mimetype of a file by looking at its extension.
Definition: functions.php:620
$stream
PHP stream implementation.
stream_for($resource='', array $options=[])
Create a new stream based on the input type.
Definition: functions.php:78
getHeaders(array $headers)
Get the headers needed before transferring the content of a POST file.
__construct(array $elements=[], $boundary=null)
addStream(StreamInterface $stream)
Add a stream to the AppendStream.
getSize()
Get the size of the stream if known.
createElement($name, StreamInterface $stream, $filename, array $headers)
createStream(array $elements)
Create the aggregate stream that will be used to upload the POST data.
$filename
Definition: buildRTE.php:89
addElement(AppendStream $stream, array $element)
Reads from multiple streams, one after the other.
Stream that when read returns bytes for a streaming multipart or multipart/form-data stream...
$key
Definition: croninfo.php:18
Describes a data stream.