forked from blamarche/openbookmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataUri.php
298 lines (267 loc) · 9.62 KB
/
DataUri.php
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* The MIT License (MIT)
* Copyright (c) 2015 FlyingTopHat ([email protected])
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* The DataUri class provides a convenient way to access and construct
* data URIs, but should not be relied upon for enforcing RFC 2397 standards.
*
* This class will not:
* - Validate the media-type provided/parsed
* - Validate the encoded data provided/parsed
*
* @author Lucas <http://www.flyingtophat.co.uk>
* @link http://www.flyingtophat.co.uk/blog/27/using-data-uris-in-php Examples
*/
class DataUri
{
/** @var Regular expression used for decomposition of data URI scheme */
private static $REGEX_URI = '/^data:(.+?){0,1}(?:(?:;(base64)\,){1}|\,)(.+){0,1}$/';
const DEFAULT_TYPE = 'text/plain;charset=US-ASCII';
const ENCODING_URL_ENCODED_OCTETS = 0;
const ENCODING_BASE64 = 1;
/** @var Keyword used in the data URI to signify base64 encoding */
const BASE64_KEYWORD = 'base64';
private $mediaType;
private $encoding;
private $encodedData;
/**
* Instantiates an instance of the DataURI class, initialised with the
* default values defined in RFC 2397. That is the media-type of
* text/plain;charset=US-ASCII and encoding type of URL encoded octets.
*
* @param string $mediaType Media-type
* @param string $data Unencoded data
* @param integer $encoding Class constant of either
* {@link DataUri::ENCODING_URL_ENCODED_OCTETS} or
* {@link DataUri::ENCODING_BASE64}
*
* @throws InvalidArgumentException
*/
public function __construct(
$mediaType = DataUri::DEFAULT_TYPE,
$data = '',
$encoding = DataUri::ENCODING_URL_ENCODED_OCTETS
) {
try {
$this->setMediaType($mediaType);
$this->setData($data, $encoding);
} catch (InvalidArgumentException $e) {
throw $e;
}
}
/**
* Returns the data URI's media-type. If none was provided then in
* accordance to RFC 2397 it will default to text/plain;charset=US-ASCII
*
* @return string Media-type
*/
public function getMediaType()
{
return empty($this->mediaType) === false
? $this->mediaType
: DataUri::DEFAULT_TYPE;
}
/**
* Sets the media-type.
*
* @param string $mediaType Media-type
*/
public function setMediaType($mediaType)
{
$this->mediaType = $mediaType;
}
/**
* Returns the method of encoding used for the data.
*
* @return int Class constant of either
* {@link DataUri::ENCODING_URL_ENCODED_OCTETS} or
* {@link DataUri::ENCODING_BASE64}
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* Returns the data in its encoded form.
*
* @return string Encoded data
*/
public function getEncodedData()
{
return $this->encodedData;
}
/**
* Sets the encoded data and the encoding scheme used to encode/decode it.
* Be aware that the data is not validated, so ensure that the correct
* encoding scheme is provided otherwise the method
* {@link DataUri::tryDecodeData($decodedData)} will fail.
*
* @param int $encoding Class constant of either
* {@link DataUri::ENCODING_URL_ENCODED_OCTETS} or
* {@link DataUri::ENCODING_BASE64}
* @param string $data Data encoded with the encoding scheme provided
*
* @throws InvalidArgumentException
*/
public function setEncodedData($encoding, $data)
{
if (
($encoding !== DataUri::ENCODING_URL_ENCODED_OCTETS) &&
($encoding !== DataUri::ENCODING_BASE64)
) {
throw new InvalidArgumentException('Unsupported encoding scheme');
}
$this->encoding = $encoding;
$this->encodedData = $data;
}
/**
* Sets the data for the data URI, which it stores in encoded form using
* the encoding scheme provided.
*
* @param string $data Data to encode then store
* @param int $encoding Class constant of either
* {@link DataUri::ENCODING_URL_ENCODED_OCTETS} or
* {@link DataUri::ENCODING_BASE64}
*
* @throws InvalidArgumentException
*/
public function setData($data, $encoding = DataUri::ENCODING_URL_ENCODED_OCTETS)
{
switch ($encoding) {
case DataUri::ENCODING_URL_ENCODED_OCTETS:
$this->encoding = DataUri::ENCODING_URL_ENCODED_OCTETS;
$this->encodedData = rawurlencode($data);
break;
case DataUri::ENCODING_BASE64:
$this->encoding = DataUri::ENCODING_BASE64;
$this->encodedData = base64_encode($data);
break;
default:
throw new InvalidArgumentException('Unsupported encoding scheme');
break;
}
}
/**
* Tries to decode the URI's data using the encoding scheme set.
*
* @param null $decodedData Stores the decoded data
*
* @return boolean <code>true</code> if data was output,
* else <code>false</code>
*/
public function tryDecodeData(&$decodedData)
{
$hasOutput = false;
switch ($this->getEncoding()) {
case DataUri::ENCODING_URL_ENCODED_OCTETS:
$decodedData = rawurldecode($this->getEncodedData());
$hasOutput = true;
break;
case DataUri::ENCODING_BASE64:
$b64Decoded = base64_decode($this->getEncodedData(), true);
if ($b64Decoded !== false) {
$decodedData = $b64Decoded;
$hasOutput = true;
}
break;
default:
// NOP
break;
}
return $hasOutput;
}
/**
* Generates a data URI string representation of the object.
*
* @return string
*/
public function toString()
{
$output = 'data:';
if (
($this->getMediaType() !== DataUri::DEFAULT_TYPE) ||
($this->getEncoding() !== DataUri::ENCODING_URL_ENCODED_OCTETS)
) {
$output .= $this->getMediaType();
if ($this->getEncoding() === DataUri::ENCODING_BASE64) {
$output .= ';' . DataUri::BASE64_KEYWORD;
}
}
$output .= ',' . $this->getEncodedData();
return $output;
}
public function __toString()
{
return $this->toString();
}
/**
* Determines whether a string is data URI with the components necessary for
* it to be parsed by the {@link DataUri::tryParse($s, &$out)} method.
*
* @param string $dataUriString Data URI
*
* @return boolean <code>true</code> if possible to parse,
* else <code>false</code>
*/
public static function isParsable($dataUriString)
{
return (preg_match(DataUri::$REGEX_URI, $dataUriString) === 1);
}
/**
* Parses a string data URI into an instance of a DataUri object.
*
* @param string $dataUriString Data URI to be parsed
* @param DataUri $out Output DataUri of the method
*
* @return boolean <code>true</code> if successful, else <code>false</code>
*/
public static function tryParse($dataUriString, &$out)
{
$hasOutput = false;
if (DataUri::isParsable($dataUriString)) {
$matches = null;
if (
preg_match_all(
DataUri::$REGEX_URI,
$dataUriString,
$matches,
PREG_SET_ORDER
) !== false
) {
$mediatype = isset($matches[0][1])
? $matches[0][1]
: DataUri::DEFAULT_TYPE;
$matchedEncoding = isset($matches[0][2]) ? $matches[0][2] : '';
$encoding = (strtolower($matchedEncoding) === DataUri::BASE64_KEYWORD)
? DataUri::ENCODING_BASE64
: DataUri::ENCODING_URL_ENCODED_OCTETS;
$data = isset($matches[0][3])
? $matches[0][3]
: '';
$dataUri = new DataUri();
$dataUri->setMediaType($mediatype);
$dataUri->setEncodedData($encoding, $data);
$out = $dataUri;
$hasOutput = true;
}
}
return $hasOutput;
}
}