2019-08-13 09:04:36 +00:00
|
|
|
<?php
|
2019-08-13 11:13:42 +00:00
|
|
|
class DiskCache {
|
2019-08-13 09:04:36 +00:00
|
|
|
private $dir;
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function __construct($dir) {
|
2019-08-13 09:04:36 +00:00
|
|
|
$this->dir = basename($dir);
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function getDir() {
|
2019-08-13 09:04:36 +00:00
|
|
|
return $this->dir;
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function isWritable() {
|
2019-08-13 09:15:43 +00:00
|
|
|
return is_dir($this->dir) && is_writable($this->dir);
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function exists($filename) {
|
2019-08-13 09:04:36 +00:00
|
|
|
return file_exists($this->getFullPath($filename));
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function getSize($filename) {
|
2019-08-13 09:04:36 +00:00
|
|
|
if ($this->exists($filename))
|
|
|
|
return filesize($this->getFullPath($filename));
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function getFullPath($filename) {
|
2019-08-13 09:04:36 +00:00
|
|
|
$filename = basename($filename);
|
|
|
|
|
|
|
|
return CACHE_DIR . "/" . $this->dir . "/" . $filename;
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function put($filename, $data) {
|
2019-08-13 09:04:36 +00:00
|
|
|
return file_put_contents($this->getFullPath($filename), $data);
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function touch($filename) {
|
2019-08-13 09:04:36 +00:00
|
|
|
return touch($this->getFullPath($filename));
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function get($filename) {
|
2019-08-13 09:04:36 +00:00
|
|
|
if ($this->exists($filename))
|
|
|
|
return file_get_contents($this->getFullPath($filename));
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function getMimeType($filename) {
|
2019-08-13 09:04:36 +00:00
|
|
|
if ($this->exists($filename))
|
|
|
|
return mime_content_type($this->getFullPath($filename));
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
2019-08-13 09:20:53 +00:00
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
public function send($filename) {
|
2019-08-13 09:20:53 +00:00
|
|
|
return send_local_file($this->getFullPath($filename));
|
|
|
|
}
|
2019-08-13 09:39:21 +00:00
|
|
|
|
2019-08-13 11:13:42 +00:00
|
|
|
static public function getUrl($filename) {
|
2019-08-13 09:39:21 +00:00
|
|
|
return get_self_url_prefix() . "/public.php?op=cached_url&file=" . $filename;
|
|
|
|
}
|
2019-08-13 09:46:57 +00:00
|
|
|
|
|
|
|
// check for locally cached (media) URLs and rewrite to local versions
|
|
|
|
// this is called separately after sanitize() and plugin render article hooks to allow
|
|
|
|
// plugins work on original source URLs used before caching
|
|
|
|
static public function rewriteUrls($str)
|
|
|
|
{
|
|
|
|
$res = trim($str);
|
|
|
|
if (!$res) return '';
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
|
|
|
if ($doc->loadHTML('<?xml encoding="UTF-8">' . $res)) {
|
|
|
|
$xpath = new DOMXPath($doc);
|
|
|
|
$cache = new DiskCache("images");
|
|
|
|
|
|
|
|
$entries = $xpath->query('(//img[@src]|//picture/source[@src]|//video[@poster]|//video/source[@src]|//audio/source[@src])');
|
|
|
|
|
|
|
|
$need_saving = false;
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
|
|
|
|
if ($entry->hasAttribute('src') || $entry->hasAttribute('poster')) {
|
|
|
|
|
|
|
|
// should be already absolutized because this is called after sanitize()
|
|
|
|
$src = $entry->hasAttribute('poster') ? $entry->getAttribute('poster') : $entry->getAttribute('src');
|
|
|
|
$cached_filename = sha1($src);
|
|
|
|
|
|
|
|
if ($cache->getSize($cached_filename) > 0) {
|
|
|
|
|
|
|
|
$src = DiskCache::getUrl(sha1($src));
|
|
|
|
|
|
|
|
if ($entry->hasAttribute('poster'))
|
|
|
|
$entry->setAttribute('poster', $src);
|
|
|
|
else
|
|
|
|
$entry->setAttribute('src', $src);
|
|
|
|
|
|
|
|
$need_saving = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($need_saving) {
|
|
|
|
$doc->removeChild($doc->firstChild); //remove doctype
|
|
|
|
$res = $doc->saveHTML();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
2019-08-13 11:13:42 +00:00
|
|
|
|
|
|
|
static function expire() {
|
|
|
|
$dirs = array_filter(glob(CACHE_DIR . "/*"), "is_dir");
|
|
|
|
|
|
|
|
foreach ($dirs as $cache_dir) {
|
|
|
|
$num_deleted = 0;
|
|
|
|
|
|
|
|
if (is_writable($cache_dir)) {
|
|
|
|
$files = glob("$cache_dir/*");
|
|
|
|
|
|
|
|
if ($files) {
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if (time() - filemtime($file) > 86400*CACHE_MAX_DAYS) {
|
|
|
|
unlink($file);
|
|
|
|
|
|
|
|
++$num_deleted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug::log("Expired $cache_dir: removed $num_deleted files.");
|
|
|
|
}
|
|
|
|
}
|
2019-08-13 09:04:36 +00:00
|
|
|
}
|