1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) |
4
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
10
|
|
|
* @link https://cakephp.org CakePHP(tm) Project |
11
|
|
|
* @since 3.0.0 |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
namespace Cake\Http\Client; |
15
|
|
|
|
16
|
|
|
use Cake\Utility\Inflector; |
17
|
|
|
use Cake\Utility\Text; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Contains the data and behavior for a single |
21
|
|
|
* part in a Multipart FormData request body. |
22
|
|
|
* |
23
|
|
|
* Added to Cake\Http\Client\FormData when sending |
24
|
|
|
* data to a remote server. |
25
|
|
|
* |
26
|
|
|
* @internal |
27
|
|
|
*/ |
28
|
|
|
class FormDataPart |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Name of the value. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $_name; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Value to send. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $_value; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Content type to use |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $_type; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Disposition to send |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $_disposition; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Filename to send if using files. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $_filename; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The encoding used in this part. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $_transferEncoding; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The contentId for the part |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $_contentId; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The charset attribute for the Content-Disposition header fields |
81
|
|
|
* |
82
|
|
|
* @var string|null |
83
|
|
|
*/ |
84
|
|
|
protected $_charset; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Constructor |
88
|
|
|
* |
89
|
|
|
* @param string $name The name of the data. |
90
|
|
|
* @param string $value The value of the data. |
91
|
|
|
* @param string $disposition The type of disposition to use, defaults to form-data. |
92
|
|
|
* @param string|null $charset The charset of the data. |
93
|
|
|
*/ |
94
|
|
|
public function __construct($name, $value, $disposition = 'form-data', $charset = null) |
95
|
|
|
{ |
96
|
|
|
$this->_name = $name; |
97
|
|
|
$this->_value = $value; |
98
|
|
|
$this->_disposition = $disposition; |
99
|
|
|
$this->_charset = $charset; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get/set the disposition type |
104
|
|
|
* |
105
|
|
|
* By passing in `false` you can disable the disposition |
106
|
|
|
* header from being added. |
107
|
|
|
* |
108
|
|
|
* @param string|null $disposition Use null to get/string to set. |
109
|
|
|
* @return string|null |
110
|
|
|
*/ |
111
|
|
|
public function disposition($disposition = null) |
112
|
|
|
{ |
113
|
|
|
if ($disposition === null) { |
114
|
|
|
return $this->_disposition; |
115
|
|
|
} |
116
|
|
|
$this->_disposition = $disposition; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get/set the contentId for a part. |
121
|
|
|
* |
122
|
|
|
* @param string|null $id The content id. |
123
|
|
|
* @return string|null |
124
|
|
|
*/ |
125
|
|
|
public function contentId($id = null) |
126
|
|
|
{ |
127
|
|
|
if ($id === null) { |
128
|
|
|
return $this->_contentId; |
129
|
|
|
} |
130
|
|
|
$this->_contentId = $id; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get/set the filename. |
135
|
|
|
* |
136
|
|
|
* Setting the filename to `false` will exclude it from the |
137
|
|
|
* generated output. |
138
|
|
|
* |
139
|
|
|
* @param string|null $filename Use null to get/string to set. |
140
|
|
|
* @return string|null |
141
|
|
|
*/ |
142
|
|
|
public function filename($filename = null) |
143
|
|
|
{ |
144
|
|
|
if ($filename === null) { |
145
|
|
|
return $this->_filename; |
146
|
|
|
} |
147
|
|
|
$this->_filename = $filename; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get/set the content type. |
152
|
|
|
* |
153
|
|
|
* @param string|null $type Use null to get/string to set. |
154
|
|
|
* @return string|null |
155
|
|
|
*/ |
156
|
|
|
public function type($type) |
157
|
|
|
{ |
158
|
|
|
if ($type === null) { |
159
|
|
|
return $this->_type; |
160
|
|
|
} |
161
|
|
|
$this->_type = $type; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set the transfer-encoding for multipart. |
166
|
|
|
* |
167
|
|
|
* Useful when content bodies are in encodings like base64. |
168
|
|
|
* |
169
|
|
|
* @param string|null $type The type of encoding the value has. |
170
|
|
|
* @return string|null |
171
|
|
|
*/ |
172
|
|
|
public function transferEncoding($type) |
173
|
|
|
{ |
174
|
|
|
if ($type === null) { |
175
|
|
|
return $this->_transferEncoding; |
176
|
|
|
} |
177
|
|
|
$this->_transferEncoding = $type; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get the part name. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function name() |
186
|
|
|
{ |
187
|
|
|
return $this->_name; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the value. |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public function value() |
196
|
|
|
{ |
197
|
|
|
return $this->_value; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Convert the part into a string. |
202
|
|
|
* |
203
|
|
|
* Creates a string suitable for use in HTTP requests. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function __toString() |
208
|
|
|
{ |
209
|
|
|
$out = ''; |
210
|
|
|
if ($this->_disposition) { |
211
|
|
|
$out .= 'Content-Disposition: ' . $this->_disposition; |
212
|
|
|
if ($this->_name) { |
213
|
|
|
$out .= '; ' . $this->_headerParameterToString('name', $this->_name); |
214
|
|
|
} |
215
|
|
|
if ($this->_filename) { |
216
|
|
|
$out .= '; ' . $this->_headerParameterToString('filename', $this->_filename); |
217
|
|
|
} |
218
|
|
|
$out .= "\r\n"; |
219
|
|
|
} |
220
|
|
|
if ($this->_type) { |
221
|
|
|
$out .= 'Content-Type: ' . $this->_type . "\r\n"; |
222
|
|
|
} |
223
|
|
|
if ($this->_transferEncoding) { |
224
|
|
|
$out .= 'Content-Transfer-Encoding: ' . $this->_transferEncoding . "\r\n"; |
225
|
|
|
} |
226
|
|
|
if ($this->_contentId) { |
227
|
|
|
$out .= 'Content-ID: <' . $this->_contentId . ">\r\n"; |
228
|
|
|
} |
229
|
|
|
$out .= "\r\n"; |
230
|
|
|
$out .= (string)$this->_value; |
231
|
|
|
|
232
|
|
|
return $out; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get the string for the header parameter. |
237
|
|
|
* |
238
|
|
|
* If the value contains non-ASCII letters an additional header indicating |
239
|
|
|
* the charset encoding will be set. |
240
|
|
|
* |
241
|
|
|
* @param string $name The name of the header parameter |
242
|
|
|
* @param string $value The value of the header parameter |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
protected function _headerParameterToString($name, $value) |
246
|
|
|
{ |
247
|
|
|
$transliterated = Text::transliterate(str_replace('"', '', $value)); |
248
|
|
|
$return = sprintf('%s="%s"', $name, $transliterated); |
249
|
|
|
if ($this->_charset !== null && $value !== $transliterated) { |
250
|
|
|
$return .= sprintf("; %s*=%s''%s", $name, strtolower($this->_charset), rawurlencode($value)); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $return; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// @deprecated 3.4.0 Add backwards compat alias. |
258
|
|
|
class_alias('Cake\Http\Client\FormDataPart', 'Cake\Network\Http\FormData\Part'); |
259
|
|
|
|