forked from ifgi-webteam/Campusplan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiles.php
82 lines (68 loc) · 2.05 KB
/
tiles.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
<?php
/* Original source http://wiki.openstreetmap.org/wiki/ProxySimplePHP
* Modified to use directory structure matching the OSM urls and retries on a failure
*/
$ttl = 604800; //cache timeout in seconds - 1 week
$x = intval($_GET['x']);
$y = intval($_GET['y']);
$z = intval($_GET['z']);
if (isset($_GET['r'])) {
$r = strip_tags($_GET['r']);
} else {
$r = 'mapnik';
}
switch ($r) {
case 'mapnik':
$r = 'mapnik';
break;
case 'osma':
default:
$r = 'osma';
break;
}
$file = "tiles/$r/$z/$x/$y.png";
$img = null;
$tries = 0;
if (!is_file($file) || filemtime($file) < time()-(86400*30)) {
do {
$server = array();
switch ($r) {
case 'mapnik':
$server[] = 'a.tile.openstreetmap.org';
$server[] = 'b.tile.openstreetmap.org';
$server[] = 'c.tile.openstreetmap.org';
$url = 'http://'.$server[array_rand($server)];
$url .= "/".$z."/".$x."/".$y.".png";
break;
case 'osma':
default:
$server[] = 'a.tah.openstreetmap.org';
$server[] = 'b.tah.openstreetmap.org';
$server[] = 'c.tah.openstreetmap.org';
$url = 'http://'.$server[array_rand($server)].'/Tiles/tile.php';
$url .= "/".$z."/".$x."/".$y.".png";
break;
}
@mkdir(dirname($file), 0755, true);
$img = file_get_contents($url);
if ($img) {
$fp = fopen($file, "w");
fwrite($fp, $img);
fclose($fp);
}
if ($tries++ > 5) exit(); // Give up after five tries
} while (!$img); // If curl has returned a broken file, then try downloading again
} else {
$img = file_get_contents($file);
}
$exp_gmt = gmdate("D, d M Y H:i:s", time() + $ttl * 60) ." GMT";
$mod_gmt = gmdate("D, d M Y H:i:s", filemtime($file)) ." GMT";
header("Expires: " . $exp_gmt);
header("Last-Modified: " . $mod_gmt);
header("Cache-Control: public, max-age=" . $ttl * 60);
// for MSIE 5
header("Cache-Control: pre-check=" . $ttl * 60, FALSE);
header ('Content-Type: image/png');
//readfile($file);
echo $img;
?>