forked from stellwerke-info/tile_cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.php
More file actions
156 lines (128 loc) · 4.89 KB
/
tile.php
File metadata and controls
156 lines (128 loc) · 4.89 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
// this file is based on: https://github.com/cyclestreets/tilecache/blob/master/index.php.
if ( ! file_exists( 'config.php' ) ) {
die( 'No config file' );
}
require_once( 'config.php' );
ini_set( 'display_errors', 'off' );
set_time_limit( 5 ); // set a rather restrictive time limit...
if ( strpos( TILECACHE_USER_AGENT, 'example.invalid' ) !== false || strpos( TILECACHE_USER_AGENT, 'example.invalid' ) !== false ) {
die( 'No user agent or referer specified! This is needed to successfully run your tile cache!' );
}
// Ensure the layer is supported
if ( ! isset( $_GET['layer'] ) || ! is_string( $_GET['layer'] ) || ! isset( TILECACHE_LAYERS[ $_GET['layer'] ] ) ) {
http_response_code( 400 );
die();
}
$layer = $_GET['layer'];
// Ensure the x/y/z parameters are present and numeric
$parameters = [ 'x', 'y', 'z' ];
$l = [];
foreach ( $parameters as $parameter ) {
if ( ! isset( $_GET[ $parameter ] ) || ! is_string( $_GET[ $parameter ] ) || ! ctype_digit( $_GET[ $parameter ] ) ) {
http_response_code( 400 );
die();
}
$l[ $parameter ] = (int) $_GET[ $parameter ];
}
final readonly class Loc {
public function __construct( public int $x, public int $y, public int $z ) {}
public function path() : string { return '/' . $this->z . '/' . $this->x . '/'; }
public function location() : string { return $this->path() . $this->y . '.png'; }
}
// Define the location
$loc = new Loc( $l['x'], $l['y'], $l['z'] );
// Get the tileserver URL for a specified layer
/** @param array<array{ 0: string, 1: int }> $layers */
function getTileserverUrl( array $layers, string $layer ) : array {
[ $tileserver, $maxz ] = $layers[ $layer ];
$serverLetter = chr( 97 + rand( 0, 2 ) ); // i.e. a, b, or c
$tileserver = str_replace( '{s}', $serverLetter, $tileserver );
return [ $tileserver, $maxz ];
}
// Retrieve a tile from a remote server.
function getTile( array $layers, string $layer, Loc $loc ) : string|false {
$headers = [
'User-Agent: ' . TILECACHE_USER_AGENT,
'Referer: ' . TILECACHE_REFERER,
];
[ $tileserver, $maxz ] = getTileserverUrl( $layers, $layer );
// Cut off at max allowed z.
if ( $loc->z > $maxz ) {
return false;
}
// If the tileserver URL has explicit x,y,z parameter placeholders, use that instead of the standard /{z}/{x}/{y}.png layout
if ( str_contains( $tileserver, '{x}' ) && ( str_contains( $tileserver, '{y}' ) || str_contains( $tileserver, '{-y}' ) ) && str_contains( $tileserver, '{z}' ) ) {
$url = str_replace( [ '{x}', '{y}', '{-y}', '{z}' ], [ $loc->x, $loc->y, $loc->y, $loc->z ], $tileserver );
} else {
$url = $tileserver . $loc->location();
}
$ch = \curl_init( $url );
\curl_setopt( $ch, \CURLOPT_HEADER, false );
\curl_setopt( $ch, \CURLOPT_HTTPHEADER, $headers );
\curl_setopt( $ch, \CURLOPT_RETURNTRANSFER, true );
\curl_setopt( $ch, \CURLOPT_FAILONERROR, true );
\curl_setopt( $ch, \CURLOPT_PROTOCOLS, \CURLPROTO_HTTPS );
\curl_setopt( $ch, \CURLOPT_FORBID_REUSE, true );
\curl_setopt( $ch, \CURLOPT_FRESH_CONNECT, true );
\curl_setopt( $ch, \CURLOPT_SSL_VERIFYPEER, true );
\curl_setopt( $ch, \CURLOPT_TIMEOUT, 2 );
\curl_setopt( $ch, \CURLOPT_CONNECTTIMEOUT, 2 );
$binary = \curl_exec( $ch );
$errno = \curl_errno( $ch );
// Get the tile
if ( $binary === false || $errno !== 0 ) {
error_log( "Remote tile failed {$url}" );
return false;
}
return $binary;
}
// Cache a tile on disk.
function cacheTile( string $binary, string $layer, Loc $loc ) : bool {
$cache = __DIR__ . '/';
$repo_safe = TILECACHE_REPOSITORY ?? '';
if ( ! \is_string( $repo_safe ) || ! \preg_match( '/^[a-z_]+$/', $repo_safe ) ) {
error_log( "Cannot write to cache $repo_safe" );
return false;
}
if ( $repo_safe != '' ) {
$cache .= $repo_safe . '/';
}
// Ensure the cache is writable
if ( ! is_dir( $cache ) || ! is_writable( $cache ) ) {
error_log( "Cannot write to cache $cache" );
return false;
}
// Ensure the directory for the file exists
$directory = $cache . $layer . $loc->path();
if ( ! is_dir( $directory ) ) {
mkdir( $directory, 0777, true );
}
// Ensure the directory is writable
if ( ! is_writable( $directory ) ) {
error_log( "Cannot write file to directory $directory" );
return false;
}
// Save the file to disk
$file = $cache . $layer . $loc->location();
file_put_contents( $file, $binary );
return true;
}
// Get the tile
$binary = getTile( TILECACHE_LAYERS, $layer, $loc );
// If no tile was retrieved, serve the null tile and end at this point
if ( ! $binary ) {
http_response_code( 404 );
die();
}
// Cache tile on disk.
if ( ! cacheTile( $binary, $layer, $loc ) ) {
http_response_code( 500 );
die();
}
// Send cache headers; see https://developers.google.com/speed/docs/best-practices/caching
header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', strtotime( '+' . TILECACHE_BROWSER_CACHE_DAYS . ' days' ) ) . ' GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) );
// Serve the file
header ( 'Content-Type: image/png' );
die( $binary );