Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
MultipartStream.php
001 <?php
002
003 namespace GuzzleHttp\Psr7;
004
005 use Psr\Http\Message\StreamInterface;
006
007 /**
008 * Stream that when read returns bytes for a streaming multipart or
009 * multipart/form-data stream.
010 *
011 * @final
012 */
013 class MultipartStream implements StreamInterface
014 {
015 use StreamDecoratorTrait;
016
017 private $boundary;
018
019 /**
020 * @param array $elements Array of associative arrays, each containing a
021 * required "name" key mapping to the form field,
022 * name, a required "contents" key mapping to a
023 * StreamInterface/resource/string, an optional
024 * "headers" associative array of custom headers,
025 * and an optional "filename" key mapping to a
026 * string to send as the filename in the part.
027 * @param string $boundary You can optionally provide a specific boundary
028 *
029 * @throws \InvalidArgumentException
030 */
031 public function __construct(array $elements = [], $boundary = null)
032 {
033 $this->boundary = $boundary ?: sha1(uniqid('', true));
034 $this->stream = $this->createStream($elements);
035 }
036
037 /**
038 * Get the boundary
039 *
040 * @return string
041 */
042 public function getBoundary()
043 {
044 return $this->boundary;
045 }
046
047 public function isWritable()
048 {
049 return false;
050 }
051
052 /**
053 * Get the headers needed before transferring the content of a POST file
054 */
055 private function getHeaders(array $headers)
056 {
057 $str = '';
058 foreach ($headers as $key => $value) {
059 $str .= "{$key}: {$value}\r\n";
060 }
061
062 return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
063 }
064
065 /**
066 * Create the aggregate stream that will be used to upload the POST data
067 */
068 protected function createStream(array $elements)
069 {
070 $stream = new AppendStream();
071
072 foreach ($elements as $element) {
073 $this->addElement($stream, $element);
074 }
075
076 // Add the trailing boundary with CRLF
077 $stream->addStream(Utils::streamFor("--{$this->boundary}--\r\n"));
078
079 return $stream;
080 }
081
082 private function addElement(AppendStream $stream, array $element)
083 {
084 foreach (['contents', 'name'] as $key) {
085 if (!array_key_exists($key, $element)) {
086 throw new \InvalidArgumentException("A '{$key}' key is required");
087 }
088 }
089
090 $element['contents'] = Utils::streamFor($element['contents']);
091
092 if (empty($element['filename'])) {
093 $uri = $element['contents']->getMetadata('uri');
094 if (substr($uri, 0, 6) !== 'php://') {
095 $element['filename'] = $uri;
096 }
097 }
098
099 list($body, $headers) = $this->createElement(
100 $element['name'],
101 $element['contents'],
102 isset($element['filename']) ? $element['filename'] : null,
103 isset($element['headers']) ? $element['headers'] : []
104 );
105
106 $stream->addStream(Utils::streamFor($this->getHeaders($headers)));
107 $stream->addStream($body);
108 $stream->addStream(Utils::streamFor("\r\n"));
109 }
110
111 /**
112 * @return array
113 */
114 private function createElement($name, StreamInterface $stream, $filename, array $headers)
115 {
116 // Set a default content-disposition header if one was no provided
117 $disposition = $this->getHeader($headers, 'content-disposition');
118 if (!$disposition) {
119 $headers['Content-Disposition'] = ($filename === '0' || $filename)
120 ? sprintf(
121 'form-data; name="%s"; filename="%s"',
122 $name,
123 basename($filename)
124 )
125 : "form-data; name=\"{$name}\"";
126 }
127
128 // Set a default content-length header if one was no provided
129 $length = $this->getHeader($headers, 'content-length');
130 if (!$length) {
131 if ($length = $stream->getSize()) {
132 $headers['Content-Length'] = (string) $length;
133 }
134 }
135
136 // Set a default Content-Type if one was not supplied
137 $type = $this->getHeader($headers, 'content-type');
138 if (!$type && ($filename === '0' || $filename)) {
139 if ($type = MimeType::fromFilename($filename)) {
140 $headers['Content-Type'] = $type;
141 }
142 }
143
144 return [$stream, $headers];
145 }
146
147 private function getHeader(array $headers, $key)
148 {
149 $lowercaseHeader = strtolower($key);
150 foreach ($headers as $k => $v) {
151 if (strtolower($k) === $lowercaseHeader) {
152 return $v;
153 }
154 }
155
156 return null;
157 }
158 }
159