1
1
<?php
2
2
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
-
17
3
// Get stuff
18
4
$ 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
+
22
9
23
10
// Check that we have a URL
24
11
if ( ! $ url )
29
16
http_response_code (403 ) and exit ("Not an absolute URL: $ url " );
30
17
31
18
// 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 ' ])
33
20
http_response_code (403 ) and exit ("Invalid referer " );
34
21
35
22
// 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 ]))
37
24
http_response_code (403 ) and exit ("Not whitelisted: $ url " );
38
25
39
26
55
42
CURLOPT_URL => $ url ,
56
43
CURLOPT_HTTPHEADER => $ headers ,
57
44
CURLOPT_HEADER => TRUE ,
58
- CURLOPT_TIMEOUT => $ curl_timeout ,
45
+ CURLOPT_TIMEOUT => $ curl_timeout ?? 30 ,
59
46
CURLOPT_FOLLOWLOCATION => TRUE ,
60
- CURLOPT_MAXREDIRS => $ curl_maxredirs ,
47
+ CURLOPT_MAXREDIRS => $ curl_maxredirs ?? 10 ,
61
48
]);
62
49
63
50
// Method specific options
112
99
113
100
114
101
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
122
103
{
123
104
static $ url ;
124
105
if (is_array ($ carry ))
@@ -128,14 +109,14 @@ function whitelist($carry, $item)
128
109
$ carry = $ carry [1 ];
129
110
}
130
111
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 ];
134
115
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 ' ]);
138
119
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 );
141
122
}
0 commit comments