From 8ceacaf175f2a5580d77ecf48e367a1cf572d3c7 Mon Sep 17 00:00:00 2001 From: franzz Date: Wed, 17 Oct 2018 18:05:10 +0200 Subject: [PATCH] add tile api --- .gitignore | 1 + inc/cacher.php | 103 ++++++++++++++++++++++++++++++++++++++++++++ inc/spot.php | 29 +++++++++++++ index.php | 9 +++- masks/messages.html | 5 ++- 5 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 inc/cacher.php diff --git a/.gitignore b/.gitignore index f5ed1c8..c885887 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ /files/**/*.JPEG /files/**/*.png /files/**/*.PNG +/cache/*.tile \ No newline at end of file diff --git a/inc/cacher.php b/inc/cacher.php new file mode 100644 index 0000000..3738481 --- /dev/null +++ b/inc/cacher.php @@ -0,0 +1,103 @@ +setPattern($sPattern); + $this->setId($sId); + $this->iX = 0; + $this->iY = 0; + $this->iZ = 0; + $this->sToken = ''; + $this->asDomains = array(); + } + + public function setId($sId) { + $this->sId = $sId; + } + + public function setPattern($sPattern) { + $this->sPattern = $sPattern; + } + + public function setToken($sToken) { + $this->sToken = $sToken; + } + + public function setDomains($asDomains) { + $this->asDomains = $asDomains; + } + + public function setGeoPos($iX, $iY, $iZ) { + $this->iX = $iX; + $this->iY = $iY; + $this->iZ = $iZ; + } + + public function pushTile($iX, $iY, $iZ) { + $this->setGeoPos($iX, $iY, $iZ); + $sTilePath = $this->getFilePath(); + if(!$this->isFileAvailable($sTilePath)) { + $oCurl = curl_init(); + curl_setopt($oCurl, CURLOPT_URL, $this->getUrl()); + curl_setopt($oCurl, CURLOPT_HEADER, false); + curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($oCurl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); + curl_setopt($oCurl, CURLOPT_REFERER, 'http://spot.lutran.fr'); + $sContent = curl_exec($oCurl); + curl_close($oCurl); + + file_put_contents($sTilePath, $sContent); + } + else $sContent = file_get_contents($sTilePath); + + header('Content-type: '.mime_content_type($sTilePath)); + return $sContent; + } + + public function getUrl() { + $asParams = $this->getUrlParams(); + $sUrl = $this->sPattern; + foreach($asParams as $sParam=>$sValue) { + $sUrl = str_replace('{'.$sParam.'}', $sValue, $sUrl); + } + return $sUrl; + } + + private function isFileAvailable($sTilePath) { + if(!file_exists($sTilePath)) return false; + else return (time() - filemtime($sTilePath) < self::DATA_RETENTION); + } + + private function getFilePath() { + $asParams = $this->getUrlParams(); + return self::CACHE_FOLDER.'/'.$asParams['id'].'_'.$asParams['x'].'_'.$asParams['y'].'_'.$asParams['z'].'.tile'; + } + + private function getUrlParams() { + return array( + 'id' => $this->sId, + 'x' => $this->iX, + 'y' => $this->iY, + 'z' => $this->iZ, + 'token' => $this->sToken, + 's' => empty($this->asDomains)?'':$this->asDomains[array_rand($this->asDomains)] + ); + } +} + diff --git a/inc/spot.php b/inc/spot.php index 2477822..c297bfe 100755 --- a/inc/spot.php +++ b/inc/spot.php @@ -28,6 +28,7 @@ class Spot extends Main public function __construct($oClassManagement, $sProcessPage) { parent::__construct($oClassManagement, $sProcessPage); + $oClassManagement->incClass('cacher', true); } protected function install() @@ -332,6 +333,34 @@ class Spot extends Main return $oUploader->getBody(); } + public function getTile($sMapId, $iX, $iY, $iZ) + { + if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == $this->asContext['serv_name']) { + $asDomains = array(); + switch($sMapId) { + case 'mapbox.satellite': + case 'mapbox.streets': + $sPattern = 'https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}@2x.png?access_token={token}'; + $sToken = Settings::MAPBOX_KEY; + break; + case 'linz': + $sPattern = 'http://tiles-{s}.data-cdn.linz.govt.nz/services;key={token}/tiles/v4/layer=50767/EPSG:3857/{z}/{x}/{y}.png'; + $sToken = Settings::LINZ_KEY; + $asDomains = array('a', 'b', 'c', 'd'); + break; + } + $oCacher = new Cacher($sPattern, $sMapId); + $oCacher->setToken($sToken); + $oCacher->setDomains($asDomains); + + return $oCacher->pushTile($iX, $iY, $iZ); + } + else { + header('HTTP/1.1 403 Forbidden'); + exit; + } + } + public static function getPicInfo($sPicPath) { $iPicTimeStamp = $iPicTakenTimeStamp = $iPicFileTimeStamp = 0; diff --git a/index.php b/index.php index c08c72c..a50dbba 100755 --- a/index.php +++ b/index.php @@ -20,7 +20,11 @@ $sAction = isset($_REQUEST['a'])?$_REQUEST['a']:''; $sPage = isset($_GET['p'])?$_GET['p']:'index'; $sName = isset($_GET['name'])?$_GET['name']:''; $sContent = isset($_GET['content'])?$_GET['content']:''; -$iChunk = isset($_GET['chunk'])?$_GET['chunk']:0; +$iChunk = isset($_GET['chunk'])?$_GET['chunk']:0; +$sId = isset($_REQUEST['id'])?$_REQUEST['id']:''; +$iX = isset($_GET['x'])?$_GET['x']:0; +$iY = isset($_GET['y'])?$_GET['y']:0; +$iZ = isset($_GET['z'])?$_GET['z']:0; //Initiate class $oSpot = new Spot($oClassManagement, __FILE__); @@ -42,6 +46,9 @@ if($sAction!='') case 'add_post': $sResult = $oSpot->addPost($sName, $sContent); break; + case 'tile': + $sResult = $oSpot->getTile($sId, $iX, $iY, $iZ); + break; default: $sResult = Spot::getJsonResult(false, Spot::NOT_FOUND); } diff --git a/masks/messages.html b/masks/messages.html index 13e572f..bdf9957 100755 --- a/masks/messages.html +++ b/masks/messages.html @@ -44,6 +44,7 @@ oSpot.pageInit = function(asHash) self.tmp('simple-bar').getScrollElement().addEventListener('scroll', onFeedScroll); self.tmp('infowindow', 'boolean'); self.tmp('map_offset', -0.3); + self.tmp('tile_api', '?a=tile&id={id}&z={z}&x={x}&y={y}'); //Centering map var agCenter = {lat:0, lng:0}; @@ -84,10 +85,10 @@ oSpot.pageInit = function(asHash) } //Tile layers - var oMapBoxSat = L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}@2x.png?access_token='+self.vars('mapbox_key'), {id: 'mapbox.satellite', minZoom: 0, maxZoom: 19}), + var oMapBoxSat = L.tileLayer(self.tmp('tile_api'), {id: 'mapbox.satellite', minZoom: 0, maxZoom: 19}), //oOpenTopoMap = L.tileLayer('https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png', {id: 'OpenTopoMap', minZoom: 2, maxZoom: 19}); //oMapBoxStreet = L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token='+self.vars('mapbox_key'), {id: 'mapbox.streets'}), - oLinz = L.tileLayer('http://tiles-{s}.data-cdn.linz.govt.nz/services;key=92193c0af382409a98694d3e4aa8bd46/tiles/v4/layer=50767/EPSG:3857/{z}/{x}/{y}.png', {id: 'Linz', maxZoom: 17, continuousWorld: true, attribution: 'Sourced from LINZ. CC BY 4.0', subdomains:'abcd'}); + oLinz = L.tileLayer(self.tmp('tile_api'), {id: 'linz', maxZoom: 17, continuousWorld: true, attribution: 'Sourced from LINZ. CC BY 4.0'}); //oGoogleSatellite = L.tileLayer('https://mt.google.com/vt/lyrs=y&x={x}&y={y}&z={z}', {id: 'GoogleSatellite', minZoom: 1, maxZoom: 22}); //Map