GlisWeb framework
_osm.php
Vai alla documentazione di questo file.
1 <?php
2 
3  define( 'DIRECTORY_BASE', str_replace( '_src/_api' , NULL , dirname( __FILE__ ) ) );
4 
5  // die( __FILE__ );
6  // die( DIRECTORY_BASE );
7 
8  require DIRECTORY_BASE . '_src/_lib/_filesystem.tools.php';
9 
10  $ttl = 86400; // cache timeout in seconds
11 
12 # $ttl = 60 * 60 * 24 * ; // cache timeout in seconds
13 
14  $x = intval($_GET['x']);
15  $y = intval($_GET['y']);
16  $z = intval($_GET['z']);
17 # $r = strip_tags($_GET['r']);
18 
19  appendToFile( 'richiesta tela ' . $z . '/' . $x . '/' . $y . PHP_EOL, 'var/log/osm/cache.' . date( 'Ymd' ) . '.log' );
20 
21 # switch ($r)
22 # {
23 # case 'mapnik':
24 # default:
25  $r = 'mapnik';
26 # break;
27 # case 'osma':
28 # $r = 'osma';
29 # break;
30 # }
31 
32  $file = DIRECTORY_BASE . "var/cache/osm/tiles/$r/${z}_${x}_${y}.png";
33 
34  appendToFile( 'file: ' . $file . PHP_EOL, 'var/log/osm/cache.' . date( 'Ymd' ) . '.log' );
35 
36  checkFolder( "/var/cache/osm/tiles/$r/" );
37 
38  if (!is_file($file) || filemtime($file)<time()-(86400*30) || filesize($file)==0)
39  {
40  $server = array();
41  switch ($r)
42  {
43  case 'mapnik':
44  $server[] = 'a.tile.openstreetmap.org';
45  $server[] = 'b.tile.openstreetmap.org';
46  $server[] = 'c.tile.openstreetmap.org';
47 
48  $url = 'https://'.$server[array_rand($server)];
49  $url .= "/".$z."/".$x."/".$y.".png";
50  break;
51 
52  case 'osma':
53  default:
54  $server[] = 'a.tah.openstreetmap.org';
55  $server[] = 'b.tah.openstreetmap.org';
56  $server[] = 'c.tah.openstreetmap.org';
57 
58  $url = 'https://'.$server[array_rand($server)].'/Tiles/tile.php';
59  $url .= "/".$z."/".$x."/".$y.".png";
60  break;
61  }
62  $ch = curl_init($url);
63  $fp = fopen($file, "wb");
64 #1 curl_setopt($ch, CURLOPT_FILE, $fp);
65 #1 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
66 #1 curl_setopt($ch, CURLOPT_HEADER, 0);
67 #1 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
68 #1 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
69 #1 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
70 #1 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
71 curl_setopt($ch, CURLOPT_VERBOSE, 1);
72 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73 #curl_setopt($ch, CURLOPT_AUTOREFERER, false);
74 #curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
75 #curl_setopt($ch, CURLOPT_HEADER, 0);
76 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // <-- don't forget this
77 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // <-- and this
78 curl_setopt($ch, CURLOPT_USERAGENT,'GlisWeb/2020.1 contact produzione@istricesrl.it');
79  $e = curl_exec($ch);
80  if( $e === false ) { appendToFile( 'errore: ' . curl_error( $ch ) . PHP_EOL, 'var/log/osm/cache.' . date( 'Ymd' ) . '.log' ); }
81  curl_close($ch);
82 #1 fflush($fp);
83 fwrite($fp, $e);
84  fclose($fp);
85  appendToFile( 'url chiamato: ' . $url . PHP_EOL, 'var/log/osm/cache.' . date( 'Ymd' ) . '.log' );
86  }
87 
88 
89 // phpinfo();
90 
91 #error_reporting(E_ALL);
92 
93  $exp_gmt = gmdate("D, d M Y H:i:s", time() + $ttl * 60) ." GMT";
94  $mod_gmt = gmdate("D, d M Y H:i:s", filemtime($file)) ." GMT";
95  // for MSIE 5
96 # header("Cache-Control: pre-check=" . $ttl * 60, FALSE);
97 # header ('Content-Type: image/png');
98 # header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
99 
100 # header('Content-Length: ' . filesize($file));
101 # readfile($file);
102 # die();
103 
104 # ob_flush();
105 
106 #$handle = fopen($file, "rb");
107 #$contents = fread($handle, filesize($file));
108 #fclose($handle);
109 
110 #echo $contents;
111 
112 // die( $file );
113 $im = imagecreatefrompng($file);
114 header('Expires: ' . $exp_gmt);
115 header('Last-Modified: ' . $mod_gmt);
116 header('Cache-Control: public, max-age=' . $ttl * 60);
117 header('Content-Type: image/png');
118 #header('Content-Length: ' . filesize($file) );
119 // header('Content-Disposition: attachment; filename="'.basename($file).'";');
120 imagepng($im);
121 
122 #imagedestroy($im);
123 
124 #$im = imagecreatefromjpeg($file);
125 #header('Content-Type: image/jpeg');
126 #imagejpeg($im);
127 #imagedestroy($im);
$ttl
Definition: _osm.php:10
$x
Definition: _osm.php:14
appendToFile( $t, $f, &$e=NULL)
aggiunge una stringa a un file
$im
Definition: _osm.php:113
$z
Definition: _osm.php:16
$file
Definition: _osm.php:32
checkFolder( $p, &$e=NULL)
verifica l&#39;esistenza di un path di directory creando quelle mancanti
$r
Definition: _osm.php:25
$e
Definition: _slack.php:121
const DIRECTORY_BASE
Definition: _osm.php:3
$y
Definition: _osm.php:15
$mod_gmt
Definition: _osm.php:94
if(!is_file($file)||filemtime($file)< time() -(86400 *30)||filesize($file)==0 $exp_gmt)
Definition: _osm.php:93