-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathproxy.php
57 lines (46 loc) · 1.59 KB
/
proxy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
header('Access-Control-Max-Age:' . 5 * 60 * 1000);
header("Access-Control-Allow-Origin: *");
header('Access-Control-Request-Method: *');
header('Access-Control-Allow-Methods: OPTIONS, GET');
header('Access-Control-Allow-Headers: *');
header("Content-Type: application/javascript");
// Url params
$url = $_GET['url'];
$callback = $_GET['callback'];
// Retrieve file details
$file_details = get_url_details($url, 1, $callback);
if (!in_array($file_details["mime_type"], array("image/jpg", "image/jpeg", "image/png")))
{
print "error:Application error";
} else
{
$re_encoded_image = sprintf(
'data:%s;base64,%s', $file_details["mime_type"], base64_encode($file_details["data"])
);
print "{$callback}(" . json_encode($re_encoded_image) . ")";
}
function get_url_details($url, $attempt = 1, $callback = "")
{
$pathinfo = pathinfo($url);
$max_attempts = 10;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
//curl_setopt($ch, CURLOPT_PROXY, 'username:password@host:port');
$data = curl_exec($ch);
$error = curl_error($ch);
$mime_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if (!in_array($mime_type, array("image/jpg", "image/jpeg", "image/png")) && $max_attempts != $attempt)
{
return get_url_details($url, $attempt++, $callback);
}
return array(
"pathinfo" => $pathinfo,
"error" => $error,
"data" => $data,
"mime_type" => $mime_type
);
}