Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 30... Línea 30...
30
     *                         string to send as the filename in the part.
30
     *                         string to send as the filename in the part.
31
     * @param string $boundary You can optionally provide a specific boundary
31
     * @param string $boundary You can optionally provide a specific boundary
32
     *
32
     *
33
     * @throws \InvalidArgumentException
33
     * @throws \InvalidArgumentException
34
     */
34
     */
35
    public function __construct(array $elements = [], string $boundary = null)
35
    public function __construct(array $elements = [], ?string $boundary = null)
36
    {
36
    {
37
        $this->boundary = $boundary ?: bin2hex(random_bytes(20));
37
        $this->boundary = $boundary ?: bin2hex(random_bytes(20));
38
        $this->stream = $this->createStream($elements);
38
        $this->stream = $this->createStream($elements);
39
    }
39
    }
Línea 49... Línea 49...
49
    }
49
    }
Línea 50... Línea 50...
50
 
50
 
51
    /**
51
    /**
52
     * Get the headers needed before transferring the content of a POST file
52
     * Get the headers needed before transferring the content of a POST file
53
     *
53
     *
54
     * @param array<string, string> $headers
54
     * @param string[] $headers
55
     */
55
     */
56
    private function getHeaders(array $headers): string
56
    private function getHeaders(array $headers): string
57
    {
57
    {
58
        $str = '';
58
        $str = '';
59
        foreach ($headers as $key => $value) {
59
        foreach ($headers as $key => $value) {
60
            $str .= "{$key}: {$value}\r\n";
60
            $str .= "{$key}: {$value}\r\n";
Línea 61... Línea 61...
61
        }
61
        }
62
 
62
 
Línea 63... Línea 63...
63
        return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
63
        return "--{$this->boundary}\r\n".trim($str)."\r\n\r\n";
64
    }
64
    }
65
 
65
 
Línea 70... Línea 70...
70
    {
70
    {
71
        $stream = new AppendStream();
71
        $stream = new AppendStream();
Línea 72... Línea 72...
72
 
72
 
73
        foreach ($elements as $element) {
73
        foreach ($elements as $element) {
74
            if (!is_array($element)) {
74
            if (!is_array($element)) {
75
                throw new \UnexpectedValueException("An array is expected");
75
                throw new \UnexpectedValueException('An array is expected');
76
            }
76
            }
77
            $this->addElement($stream, $element);
77
            $this->addElement($stream, $element);
Línea 78... Línea 78...
78
        }
78
        }
Línea 110... Línea 110...
110
        $stream->addStream(Utils::streamFor($this->getHeaders($headers)));
110
        $stream->addStream(Utils::streamFor($this->getHeaders($headers)));
111
        $stream->addStream($body);
111
        $stream->addStream($body);
112
        $stream->addStream(Utils::streamFor("\r\n"));
112
        $stream->addStream(Utils::streamFor("\r\n"));
113
    }
113
    }
Línea -... Línea 114...
-
 
114
 
-
 
115
    /**
-
 
116
     * @param string[] $headers
-
 
117
     *
-
 
118
     * @return array{0: StreamInterface, 1: string[]}
114
 
119
     */
115
    private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers): array
120
    private function createElement(string $name, StreamInterface $stream, ?string $filename, array $headers): array
116
    {
121
    {
117
        // Set a default content-disposition header if one was no provided
122
        // Set a default content-disposition header if one was no provided
118
        $disposition = $this->getHeader($headers, 'content-disposition');
123
        $disposition = self::getHeader($headers, 'content-disposition');
119
        if (!$disposition) {
124
        if (!$disposition) {
120
            $headers['Content-Disposition'] = ($filename === '0' || $filename)
125
            $headers['Content-Disposition'] = ($filename === '0' || $filename)
121
                ? sprintf(
126
                ? sprintf(
122
                    'form-data; name="%s"; filename="%s"',
127
                    'form-data; name="%s"; filename="%s"',
Línea 125... Línea 130...
125
                )
130
                )
126
                : "form-data; name=\"{$name}\"";
131
                : "form-data; name=\"{$name}\"";
127
        }
132
        }
Línea 128... Línea 133...
128
 
133
 
129
        // Set a default content-length header if one was no provided
134
        // Set a default content-length header if one was no provided
130
        $length = $this->getHeader($headers, 'content-length');
135
        $length = self::getHeader($headers, 'content-length');
131
        if (!$length) {
136
        if (!$length) {
132
            if ($length = $stream->getSize()) {
137
            if ($length = $stream->getSize()) {
133
                $headers['Content-Length'] = (string) $length;
138
                $headers['Content-Length'] = (string) $length;
134
            }
139
            }
Línea 135... Línea 140...
135
        }
140
        }
136
 
141
 
137
        // Set a default Content-Type if one was not supplied
142
        // Set a default Content-Type if one was not supplied
138
        $type = $this->getHeader($headers, 'content-type');
143
        $type = self::getHeader($headers, 'content-type');
139
        if (!$type && ($filename === '0' || $filename)) {
-
 
140
            if ($type = MimeType::fromFilename($filename)) {
-
 
141
                $headers['Content-Type'] = $type;
144
        if (!$type && ($filename === '0' || $filename)) {
Línea 142... Línea 145...
142
            }
145
            $headers['Content-Type'] = MimeType::fromFilename($filename) ?? 'application/octet-stream';
143
        }
146
        }
Línea -... Línea 147...
-
 
147
 
-
 
148
        return [$stream, $headers];
-
 
149
    }
144
 
150
 
145
        return [$stream, $headers];
151
    /**
146
    }
152
     * @param string[] $headers
147
 
153
     */
148
    private function getHeader(array $headers, string $key)
154
    private static function getHeader(array $headers, string $key): ?string
149
    {
155
    {
150
        $lowercaseHeader = strtolower($key);
156
        $lowercaseHeader = strtolower($key);
151
        foreach ($headers as $k => $v) {
157
        foreach ($headers as $k => $v) {
Línea 152... Línea 158...
152
            if (strtolower($k) === $lowercaseHeader) {
158
            if (strtolower((string) $k) === $lowercaseHeader) {