2016-09-05 18:05:19 +02:00
|
|
|
<?php
|
|
|
|
require_once(__DIR__ . '/BridgeInterface.php');
|
|
|
|
/**
|
|
|
|
* Extension of BridgeAbstract allowing caching of files downloaded over http.
|
|
|
|
*/
|
|
|
|
abstract class HttpCachingBridgeAbstract extends BridgeAbstract {
|
|
|
|
/**
|
|
|
|
* Maintain locally cached versions of pages to download, to avoid multiple downloads.
|
|
|
|
* @param url url to cache
|
2016-09-09 22:14:49 +02:00
|
|
|
* @param duration duration of the cache file in seconds (default: 24h/86400s)
|
2016-09-05 18:05:19 +02:00
|
|
|
* @return content of the file as string
|
|
|
|
*/
|
2016-09-09 22:14:49 +02:00
|
|
|
public function get_cached($url, $duration = 86400){
|
2016-09-10 00:34:25 +02:00
|
|
|
$this->debugMessage('Caching url ' . $url . ', duration ' . $duration);
|
|
|
|
|
|
|
|
$filepath = __DIR__ . '/../cache/pages/' . sha1($url) . '.cache';
|
|
|
|
$this->debugMessage('Cache file ' . $filepath);
|
2016-09-05 18:05:19 +02:00
|
|
|
|
2016-09-09 22:14:49 +02:00
|
|
|
if(file_exists($filepath) && filectime($filepath) < time() - $duration){
|
|
|
|
unlink ($filepath);
|
|
|
|
$this->debugMessage('Cached file deleted: ' . $filepath);
|
|
|
|
}
|
|
|
|
|
2016-09-05 18:05:19 +02:00
|
|
|
if(file_exists($filepath)){
|
2016-09-10 00:34:25 +02:00
|
|
|
$this->debugMessage('Loading cached file ' . $filepath);
|
2016-09-10 00:00:10 +02:00
|
|
|
touch($filepath);
|
2016-09-05 18:05:19 +02:00
|
|
|
$content = file_get_contents($filepath);
|
|
|
|
} else {
|
2016-09-10 00:34:25 +02:00
|
|
|
$this->debugMessage('Caching ' . $url . ' to ' . $filepath);
|
2016-09-05 18:05:19 +02:00
|
|
|
$dir = substr($filepath, 0, strrpos($filepath, '/'));
|
|
|
|
|
|
|
|
if(!is_dir($dir)){
|
2016-09-10 00:34:25 +02:00
|
|
|
$this->debugMessage('Creating directory ' . $dir);
|
2016-09-05 18:05:19 +02:00
|
|
|
mkdir($dir, 0777, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $this->getContents($url);
|
|
|
|
if($content !== false){
|
|
|
|
file_put_contents($filepath, $content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str_get_html($content);
|
|
|
|
}
|
|
|
|
}
|