@@ -21,8 +21,9 @@ class Proxy
21
21
];
22
22
private $ driver ;
23
23
private $ drivers = [];
24
- private $ allowedUrls = ['* ' ];
25
- private $ allowedTypes = [
24
+ private $ allowUrls = [];
25
+ private $ allowUrlsRegEx ;
26
+ private $ allowTypes = [
26
27
'image/apng ' => true ,
27
28
'image/png ' => true ,
28
29
'image/avif ' => true ,
@@ -48,7 +49,18 @@ public function __construct()
48
49
}
49
50
50
51
/**
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
52
64
*
53
65
* @param string $key
54
66
* @param mixed $value
@@ -85,31 +97,15 @@ public function getOptions($key = null)
85
97
}
86
98
87
99
/**
88
- * Get or redefine allowed URLs
100
+ * Set allowed URLs
89
101
*
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
108
103
* @return void
109
104
*/
110
- public function setDrivers (array $ drivers )
105
+ public function setAllowUrls (array $ urls )
111
106
{
112
- $ this ->drivers = $ drivers ;
107
+ $ this ->allowUrls = $ urls ;
108
+ $ this ->allowUrlsRegEx = null ;
113
109
}
114
110
115
111
/**
@@ -119,9 +115,9 @@ public function setDrivers(array $drivers)
119
115
* @param string $binary
120
116
* @return void
121
117
*/
122
- public function addAllowedType ($ type , $ binary )
118
+ public function addAllowType ($ type , $ binary )
123
119
{
124
- $ this ->allowedTypes [$ type ] = $ binary ;
120
+ $ this ->allowTypes [$ type ] = $ binary ;
125
121
}
126
122
127
123
/**
@@ -130,9 +126,9 @@ public function addAllowedType($type, $binary)
130
126
* @param string $type
131
127
* @return void
132
128
*/
133
- public function removeAllowedType ($ type )
129
+ public function removeAllowType ($ type )
134
130
{
135
- unset($ this ->allowedTypes [$ type ]);
131
+ unset($ this ->allowTypes [$ type ]);
136
132
}
137
133
138
134
/**
@@ -222,7 +218,7 @@ public function download($url, $ignoreDownloadError = false)
222
218
$ contentType = trim ($ contentType );
223
219
}
224
220
225
- if (array_key_exists ($ contentType , $ this ->allowedTypes ) === false ) {
221
+ if (array_key_exists ($ contentType , $ this ->allowTypes ) === false ) {
226
222
$ this ->raise ('Not allowed Content-type: ' . $ contentType );
227
223
}
228
224
@@ -305,7 +301,7 @@ public function jsonp($callback)
305
301
list ($ contentType , $ extra ) = $ extract ;
306
302
}
307
303
308
- $ binary = $ this ->allowedTypes [$ contentType ];
304
+ $ binary = $ this ->allowTypes [$ contentType ];
309
305
310
306
if ($ binary ) {
311
307
$ contentType .= ';base64 ' ;
@@ -433,17 +429,21 @@ private function httpCache()
433
429
434
430
private function validateUrl ($ url )
435
431
{
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
+ }
445
445
446
- if (!preg_match (' #^( ' . $ re . ' )# ' , $ url )) {
446
+ if (!preg_match ($ this -> allowUrlsRegEx , $ url )) {
447
447
return false ;
448
448
}
449
449
}
0 commit comments