Skip to content

Commit 523d4c6

Browse files
committed
Refactored to use psr-4 class loading and PHP 7 features
1 parent b238013 commit 523d4c6

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"source": "https://github.com/Svish/php-cross-domain-proxy"
1010
},
1111
"require": {
12-
"php": ">=5.4.0",
12+
"php": "7.*",
1313
"lib-curl": ">=7.36"
1414
},
1515
"autoload": {
16-
"classmap": ["src/"]
16+
"psr-4": {"Geekality\\": "src/"}
1717
}
1818
}

composer.lock

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proxy.php

+17-36
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
<?php
22

3-
4-
if( ! isset($whitelist))
5-
$whitelist = [];
6-
7-
if( ! isset($curl_maxredirs))
8-
$curl_maxredirs = 10;
9-
10-
if( ! isset($curl_timeout))
11-
$curl_timeout = 30;
12-
13-
14-
15-
16-
173
// Get stuff
184
$headers = getallheaders();
19-
$method = __('REQUEST_METHOD', $_SERVER);
20-
$url = __('X-Proxy-Url', $headers);
21-
$cookie = __('X-Proxy-Cookie', $headers);
5+
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
6+
$url = $headers['X-Proxy-Url'] ?? null;
7+
$cookie = $headers['X-Proxy-Cookie'] ?? null;
8+
229

2310
// Check that we have a URL
2411
if( ! $url)
@@ -29,11 +16,11 @@
2916
http_response_code(403) and exit("Not an absolute URL: $url");
3017

3118
// Check referer hostname
32-
if( ! parse_url(__('Referer', $headers), PHP_URL_HOST) == $_SERVER['HTTP_HOST'])
19+
if( ! parse_url($headers['Referer'] ?? null, PHP_URL_HOST) == $_SERVER['HTTP_HOST'])
3320
http_response_code(403) and exit("Invalid referer");
3421

3522
// Check whitelist, if not empty
36-
if( ! empty($whitelist) and ! array_reduce($whitelist, 'whitelist', [$url, false]))
23+
if( ! array_reduce($whitelist ?? [], 'is_bad', [$url, false]))
3724
http_response_code(403) and exit("Not whitelisted: $url");
3825

3926

@@ -55,9 +42,9 @@
5542
CURLOPT_URL => $url,
5643
CURLOPT_HTTPHEADER => $headers,
5744
CURLOPT_HEADER => TRUE,
58-
CURLOPT_TIMEOUT => $curl_timeout,
45+
CURLOPT_TIMEOUT => $curl_timeout ?? 30,
5946
CURLOPT_FOLLOWLOCATION => TRUE,
60-
CURLOPT_MAXREDIRS => $curl_maxredirs,
47+
CURLOPT_MAXREDIRS => $curl_maxredirs ?? 10,
6148
]);
6249

6350
// Method specific options
@@ -112,13 +99,7 @@
11299

113100

114101

115-
// Helper functions
116-
function __($key, array $array, $default = null)
117-
{
118-
return array_key_exists($key, $array) ? $array[$key] : $default;
119-
}
120-
121-
function whitelist($carry, $item)
102+
function is_bad($carry, array $rule): bool
122103
{
123104
static $url;
124105
if(is_array($carry))
@@ -128,14 +109,14 @@ function whitelist($carry, $item)
128109
$carry = $carry[1];
129110
}
130111

131-
// Equals the full URL
132-
if(isset($item[0]))
133-
return $carry or $url['raw'] == $item[0];
112+
// Equals full URL
113+
if(isset($rule[0]))
114+
return $carry or $url['raw'] == $rule[0];
134115

135-
// Regex matches the full URL
136-
if(isset($item['regex']))
137-
return $carry or preg_match($item['regex'], $url['raw']);
116+
// Regex matches URL
117+
if(isset($rule['regex']))
118+
return $carry or preg_match($rule['regex'], $url['raw']);
138119

139-
// Select components matches same components in the URL
140-
return $carry or $item == array_intersect_key($url, $item);
120+
// Components in rule matches same components in URL
121+
return $carry or $rule == array_intersect_key($url, $rule);
141122
}

src/CrossOriginProxy.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
namespace Geekality;
4+
35
/**
46
* Cross-origin request proxy for client-side scripts.
57
*
@@ -14,7 +16,7 @@ class CrossOriginProxy
1416
* @param curl_timeout Timeout for the request.
1517
* @param curl_maxredirs Maximum number of allowed redirects.
1618
*/
17-
public static function proxy($whitelist = [], $curl_timeout = 30, $curl_maxredirs = 10)
19+
public static function proxy(array $whitelist = [], int $curl_timeout = 30, int $curl_maxredirs = 10)
1820
{
1921
require dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'proxy.php';
2022
}

test/proxy.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
require '../vendor/autoload.php';
44

5-
CrossOriginProxy::proxy([
5+
// Example of a whitelist
6+
$whitelist = [
7+
68
// Exact matching
79
['http://www.yr.no/place/Sweden/Stockholm/Stockholm/forecast.xml'],
810

@@ -12,5 +14,8 @@
1214

1315
// Regex matching
1416
['regex' => '%^http://www.yr.no/place/Norway/%'],
17+
];
18+
1519

16-
]);
20+
// Call/Use the proxy
21+
Geekality\CrossOriginProxy::proxy($whitelist);

0 commit comments

Comments
 (0)