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
2411if ( ! $ url )
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
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
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}
0 commit comments