Skip to content

Commit 89b4427

Browse files
committed
Revised for clarity and conciseness
1 parent 6045974 commit 89b4427

File tree

3 files changed

+60
-55
lines changed

3 files changed

+60
-55
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,16 @@ html2canvas(document.getElementById('container'), {
8484

8585
Method | Description
8686
--- | ---
87-
`setOptions(string $key, mixed $value): void` | Setup generic options
87+
`setDrivers(array $drivers): void` | Set drivers used to download the resource
88+
`setOptions(string $key, mixed $value): void` | Set generic options
8889
`getOptions([string $key]): mixed` | Get generic options
89-
`urls([array $urls]): array` | Get or redefine allowed URLs
90-
`setDrivers(array $drivers): void` | Sets drivers used for download resource
91-
`addAllowedType(string $type, bool $binary): void` | Add Content-Type to the allowed list
92-
`removeAllowedType(string $type): void` | Remove content-type from the allowed list
90+
`setAllowUrls(array $urls): void` | Set allowed URLs
91+
`addAllowType(string $type, bool $binary): void` | Add content-type to the allowed list
92+
`removeAllowType(string $type): void` | Remove content-type from the allowed list
9393
`setTemporary(string $path): void` | Sets temporary handle path, eg.: `/mnt/storage/`, `php://temp`, `php://memory`
9494
`getTemporary(): resource` | Get temporary stream
9595
`download(string $url[, bool $ignoreDownloadError]): void` | Perform download
96-
`setResponseCacheTime(int $seconds): void` | Enable or disable cache for Proxy::respose() or Proxy::jsonp()
96+
`setResponseCacheTime(int $seconds): void` | Enable or disable cache for `Proxy::respose()` or `Proxy::jsonp()`
9797
`response(): void` | Dump response to output
9898
`jsonp(string $callback): void` | Output JSONP callback with URL or data URI content
9999
`getContents([int $length[, int $offset]]): string` | If last download was successful, contents will be returned
@@ -156,7 +156,7 @@ $proxy->setOptions('stream', [
156156
]);
157157
```
158158

159-
## Allowed Content-Type
159+
## Allow Content-Type
160160

161161
When executing the download() method a content-type validation will be performed, by default the following Content-Types are allowed:
162162

@@ -174,16 +174,16 @@ Content-Type | `Proxy::jsonp()`
174174
You can define another allowed content-type, example:
175175

176176
```php
177-
$proxy->addAllowedType('image/x-icon', true);
178-
$proxy->addAllowedType('image/vnd.microsoft.icon', true);
177+
$proxy->addAllowType('image/x-icon', true);
178+
$proxy->addAllowType('image/vnd.microsoft.icon', true);
179179
```
180180

181181
Second parameter of the method specifies whether the `Proxy::jsonp()` should use URL encoding or Base64 encoding in the data URI scheme.
182182

183-
To remove an allowed Content-Type use the `Proxy::removeAllowedType()` method, example:
183+
To remove an allowed Content-Type use the `Proxy::removeAllowType()` method, example:
184184

185185
```php
186-
$proxy->removeAllowedType('image/apng');
186+
$proxy->removeAllowType('image/apng');
187187
```
188188

189189
## How to use

proxy.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@
1818
$proxy = new Proxy();
1919

2020
// Set allowed URLs
21-
// $proxy->urls([ 'https://*.domain.io/', 'https://cdn.foobar.io/' ]);
21+
/*
22+
$proxy->setAllowUrls([
23+
'https://*.domain.io/',
24+
'https://cdn.foobar.io/'
25+
]);
26+
*/
2227

2328
// Set allowed content-types
24-
// $proxy->addAllowedType('image/ico', true);
29+
// $proxy->addAllowType('image/ico', true);
2530

26-
// Extra configs for CURL
31+
// Extra configs for CurlDriver
2732
// $proxy->setOptions('curl', [ CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_3 ]);
2833

29-
// Extra configs for stream, see details in: https://www.php.net/manual/en/context.php
34+
// Extra configs for StreamDriver, see details in: https://www.php.net/manual/en/context.php
3035
/*
3136
$proxy->setOptions('stream', [
3237
'http' => [

src/Proxy.php

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class Proxy
2121
];
2222
private $driver;
2323
private $drivers = [];
24-
private $allowedUrls = ['*'];
25-
private $allowedTypes = [
24+
private $allowUrls = [];
25+
private $allowUrlsRegEx;
26+
private $allowTypes = [
2627
'image/apng' => true,
2728
'image/png' => true,
2829
'image/avif' => true,
@@ -48,7 +49,18 @@ public function __construct()
4849
}
4950

5051
/**
51-
* Setup generic options
52+
* Set drivers used to download the resource
53+
*
54+
* @param array $drivers Set drivers
55+
* @return void
56+
*/
57+
public function setDrivers(array $drivers)
58+
{
59+
$this->drivers = $drivers;
60+
}
61+
62+
/**
63+
* Set generic options
5264
*
5365
* @param string $key
5466
* @param mixed $value
@@ -85,31 +97,15 @@ public function getOptions($key = null)
8597
}
8698

8799
/**
88-
* Get or redefine allowed URLs
100+
* Set allowed URLs
89101
*
90-
* @param array $urls Optional. Redefine allowed URLs
91-
* @return array Return current allowed URLs
92-
*/
93-
public function urls(array $urls = [])
94-
{
95-
if (empty($urls)) {
96-
return $this->allowedUrls;
97-
}
98-
99-
$current = $this->allowedUrls;
100-
$this->allowedUrls = $urls;
101-
return $current;
102-
}
103-
104-
/**
105-
* Set drivers used for download resource
106-
*
107-
* @param array $drivers Set drivers
102+
* @param array $urls
108103
* @return void
109104
*/
110-
public function setDrivers(array $drivers)
105+
public function setAllowUrls(array $urls)
111106
{
112-
$this->drivers = $drivers;
107+
$this->allowUrls = $urls;
108+
$this->allowUrlsRegEx = null;
113109
}
114110

115111
/**
@@ -119,9 +115,9 @@ public function setDrivers(array $drivers)
119115
* @param string $binary
120116
* @return void
121117
*/
122-
public function addAllowedType($type, $binary)
118+
public function addAllowType($type, $binary)
123119
{
124-
$this->allowedTypes[$type] = $binary;
120+
$this->allowTypes[$type] = $binary;
125121
}
126122

127123
/**
@@ -130,9 +126,9 @@ public function addAllowedType($type, $binary)
130126
* @param string $type
131127
* @return void
132128
*/
133-
public function removeAllowedType($type)
129+
public function removeAllowType($type)
134130
{
135-
unset($this->allowedTypes[$type]);
131+
unset($this->allowTypes[$type]);
136132
}
137133

138134
/**
@@ -222,7 +218,7 @@ public function download($url, $ignoreDownloadError = false)
222218
$contentType = trim($contentType);
223219
}
224220

225-
if (array_key_exists($contentType, $this->allowedTypes) === false) {
221+
if (array_key_exists($contentType, $this->allowTypes) === false) {
226222
$this->raise('Not allowed Content-type: ' . $contentType);
227223
}
228224

@@ -305,7 +301,7 @@ public function jsonp($callback)
305301
list($contentType, $extra) = $extract;
306302
}
307303

308-
$binary = $this->allowedTypes[$contentType];
304+
$binary = $this->allowTypes[$contentType];
309305

310306
if ($binary) {
311307
$contentType .= ';base64';
@@ -433,17 +429,21 @@ private function httpCache()
433429

434430
private function validateUrl($url)
435431
{
436-
$alloweds = $this->allowedUrls;
437-
438-
if (in_array('*', $alloweds) === false) {
439-
$re = implode('|', $alloweds);
440-
$re = preg_quote($re, '#');
441-
$re = strtr($re, array(
442-
'\\*' => '\\w+',
443-
'\\|' => '|'
444-
));
432+
$urlList = $this->allowUrls;
433+
434+
if ($urlList) {
435+
if ($this->allowUrlsRegEx === null) {
436+
$regex = implode('|', $urlList);
437+
$regex = preg_quote($regex, '#');
438+
$regex = strtr($regex, array(
439+
'\\*' => '\\w+',
440+
'\\|' => '|'
441+
));
442+
443+
$this->allowUrlsRegEx = '#^(' . $regex . ')#';
444+
}
445445

446-
if (!preg_match('#^(' . $re . ')#', $url)) {
446+
if (!preg_match($this->allowUrlsRegEx, $url)) {
447447
return false;
448448
}
449449
}

0 commit comments

Comments
 (0)