Repository navigation
Expand file tree
/
Copy pathFormData.php
More file actions
199 lines (187 loc) · 5.36 KB
/
Copy pathFormData.php
File metadata and controls
199 lines (187 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Network\Http;
use Cake\Network\Http\FormData\Part;
use Countable;
/**
* Provides an interface for building
* multipart/form-encoded message bodies.
*
* Used by Http\Client to upload POST/PUT data
* and files.
*
*/
class FormData implements Countable
{
/**
* Boundary marker.
*
* @var string
*/
protected $_boundary;
/**
* The parts in the form data.
*
* @var array
*/
protected $_parts = [];
/**
* Get the boundary marker
*
* @return string
*/
public function boundary()
{
if ($this->_boundary) {
return $this->_boundary;
}
$this->_boundary = md5(uniqid(time()));
return $this->_boundary;
}
/**
* Method for creating new instances of Part
*
* @param string $name The name of the part.
* @param string $value The value to add.
* @return \Cake\Network\Http\FormData\Part
*/
public function newPart($name, $value)
{
return new Part($name, $value);
}
/**
* Add a new part to the data.
*
* The value for a part can be a string, array, int,
* float, filehandle, or object implementing __toString()
*
* If the $value is an array, multiple parts will be added.
* Files will be read from their current position and saved in memory.
*
* @param string|\Cake\Network\Http\FormData $name The name of the part to add,
* or the part data object.
* @param mixed $value The value for the part.
* @return $this
*/
public function add($name, $value = null)
{
if (is_array($value)) {
$this->addRecursive($name, $value);
} elseif (is_resource($value)) {
$this->_parts[] = $this->addFile($name, $value);
} elseif (is_string($value) && strlen($value) && $value[0] === '@') {
trigger_error(
'Using the @ syntax for file uploads is not safe and is deprecated. ' .
'Instead you should use file handles.',
E_USER_DEPRECATED
);
$this->_parts[] = $this->addFile($name, $value);
} elseif ($name instanceof Part && $value === null) {
$this->_parts[] = $name;
} else {
$this->_parts[] = $this->newPart($name, $value);
}
return $this;
}
/**
* Add multiple parts at once.
*
* Iterates the parameter and adds all the key/values.
*
* @param array $data Array of data to add.
* @return $this
*/
public function addMany(array $data)
{
foreach ($data as $name => $value) {
$this->add($name, $value);
}
return $this;
}
/**
* Add either a file reference (string starting with @)
* or a file handle.
*
* @param string $name The name to use.
* @param mixed $value Either a string filename, or a filehandle.
* @return \Cake\Network\Http\FormData\Part
*/
public function addFile($name, $value)
{
$filename = false;
$contentType = 'application/octet-stream';
if (is_resource($value)) {
$content = stream_get_contents($value);
if (stream_is_local($value)) {
$finfo = new \finfo(FILEINFO_MIME);
$metadata = stream_get_meta_data($value);
$contentType = $finfo->file($metadata['uri']);
$filename = basename($metadata['uri']);
}
} else {
$finfo = new \finfo(FILEINFO_MIME);
$value = substr($value, 1);
$filename = basename($value);
$content = file_get_contents($value);
$contentType = $finfo->file($value);
}
$part = $this->newPart($name, $content);
$part->type($contentType);
if ($filename) {
$part->filename($filename);
}
return $part;
}
/**
* Recursively add data.
*
* @param string $name The name to use.
* @param mixed $value The value to add.
* @return void
*/
public function addRecursive($name, $value)
{
foreach ($value as $key => $value) {
$key = $name . '[' . $key . ']';
$this->add($key, $value);
}
}
/**
* Returns the count of parts inside this object.
*
* @return int
*/
public function count()
{
return count($this->_parts);
}
/**
* Converts the FormData and its parts into a string suitable
* for use in an HTTP request.
*
* @return string
*/
public function __toString()
{
$boundary = $this->boundary();
$out = '';
foreach ($this->_parts as $part) {
$out .= "--$boundary\r\n";
$out .= (string)$part;
$out .= "\r\n";
}
$out .= "--$boundary--\r\n\r\n";
return $out;
}
}