-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcache.php
115 lines (90 loc) · 3.27 KB
/
tcache.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?PHP
/*
http://tiles.strabospot.org/v4/mapbox.satellite/19/119885/201168.png?access_token=pk.eyJ1Ijoic3RyYWJvLWdlb2xvZ3kiLCJhIjoiY2lpYzdhbzEwMDA1ZnZhbTEzcTV3Z3ZnOSJ9.myyChr6lmmHfP8LYwhH5Sg
http://tiles.strabospot.org/v4/mapbox.outdoors/19/119886/201171.png?access_token=pk.eyJ1Ijoic3RyYWJvLWdlb2xvZ3kiLCJhIjoiY2lpYzdhbzEwMDA1ZnZhbTEzcTV3Z3ZnOSJ9.myyChr6lmmHfP8LYwhH5Sg
http://b.tiles.mapbox.com/v4/mapbox.satellite/19/119885/201168.png?access_token=pk.eyJ1Ijoic3RyYWJvLWdlb2xvZ3kiLCJhIjoiY2lpYzdhbzEwMDA1ZnZhbTEzcTV3Z3ZnOSJ9.myyChr6lmmHfP8LYwhH5Sg
http://b.tiles.mapbox.com/v4/mapbox.outdoors/19/119886/201171.png?access_token=pk.eyJ1Ijoic3RyYWJvLWdlb2xvZ3kiLCJhIjoiY2lpYzdhbzEwMDA1ZnZhbTEzcTV3Z3ZnOSJ9.myyChr6lmmHfP8LYwhH5Sg
*/
function dumpVar($var){
echo "<pre>";
print_r($var);
echo "</pre>";
}
//dumpVar($_GET);
$uri = $_SERVER['REQUEST_URI'];
$access_token="pk.eyJ1Ijoic3RyYWJvLWdlb2xvZ3kiLCJhIjoiY2lpYzdhbzEwMDA1ZnZhbTEzcTV3Z3ZnOSJ9.myyChr6lmmHfP8LYwhH5Sg";
$moreget = explode("?",$uri)[1];
if($moreget!=""){
$parts = explode("=",$moreget);
if($parts[0]=="access_token"){
$access_token=$parts[1];
}
}
$cruft = $_GET['cruft'];
$parts=explode("/",$cruft);
$valid_layers = array("mapbox.satellite","mapbox.outdoors");
$dir1 = $parts[0];
$dir2 = $parts[1];
$dir3 = $parts[2];
$filename = $parts[3];
$fileextension=explode(".",$filename)[1];
switch( $fileextension ) {
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpeg"; break;
default:
}
if(in_array($dir1,$valid_layers)){
if(!file_exists("cache/$dir1")){
mkdir("cache/$dir1");
}
if(!file_exists("cache/$dir1/$dir2")){
mkdir("cache/$dir1/$dir2");
}
if(!file_exists("cache/$dir1/$dir2/$dir3")){
mkdir("cache/$dir1/$dir2/$dir3");
}
//echo "ctype: $ctype";exit();
if(!file_exists("cache/$dir1/$dir2/$dir3/$filename" )){
$url = "http://api.tiles.mapbox.com/v4/$dir1/$dir2/$dir3/$filename?access_token=$access_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$fsize=(curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD));
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
//save file and send to browser
//header("Access-Control-Allow-Origin: *");
header("Content-Type: $ctype");
header('Content-Length: ' . $fsize);
echo $data;
file_put_contents("cache/$dir1/$dir2/$dir3/$filename",$data);
}else{
http_response_code($httpcode);
//header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
echo $data;
}
}else{
//read file and send to browser
//header("Access-Control-Allow-Origin: *");
header("Content-Type: $ctype");
header('Content-Length: ' . filesize("cache/$dir1/$dir2/$dir3/$filename"));
readfile("cache/$dir1/$dir2/$dir3/$filename");
}
}else{
http_response_code(404);
echo "Error. Layer $dir1 is not valid. Valid layers are: ";
$vadelim="";
foreach($valid_layers as $va){
echo $vadelim.$va;$vadelim=", ";
}
echo ".";
}
?>