104 lines
2.4 KiB
PHP
104 lines
2.4 KiB
PHP
<?php
|
|
|
|
class Cacher extends PhpObject
|
|
{
|
|
const CACHE_FOLDER = 'cache/';
|
|
const DATA_RETENTION = 60 * 60 * 24 * 90; //3 months
|
|
|
|
private $sPattern;
|
|
|
|
//Params
|
|
private $sId;
|
|
private $iX;
|
|
private $iY;
|
|
private $iZ;
|
|
private $sToken;
|
|
private $asDomains;
|
|
|
|
public function __construct($sPattern, $sId='')
|
|
{
|
|
parent::__construct(__CLASS__, Settings::DEBUG);
|
|
$this->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)]
|
|
);
|
|
}
|
|
}
|
|
|