From 51ff8de3464c6497fbe5a9352b63f675d58c5f0b Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 7 Oct 2016 22:06:58 +0200 Subject: [PATCH 01/16] [Cache] Remove orphan function utf8_encode_deep --- caches/FileCache.php | 2 -- lib/Cache.php | 20 -------------------- 2 files changed, 22 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index a8ab88d7..eaf16f23 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -14,8 +14,6 @@ class FileCache extends CacheAbstract { public function saveData($datas){ $this->isPrepareCache(); - //Re-encode datas to UTF-8 - //$datas = Cache::utf8_encode_deep($datas); $writeStream = file_put_contents($this->getCacheFile(), serialize($datas)); if(!$writeStream) { diff --git a/lib/Cache.php b/lib/Cache.php index a1649a0b..192cb3bf 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -51,26 +51,6 @@ class Cache { return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache); } - - static public function utf8_encode_deep(&$input){ - if (is_string($input)){ - $input = utf8_encode($input); - } elseif(is_array($input)){ - foreach($input as &$value){ - Cache::utf8_encode_deep($value); - } - - unset($value); - } elseif(is_object($input)){ - $vars = array_keys(get_object_vars($input)); - - foreach($vars as $var){ - Cache::utf8_encode_deep($input->$var); - } - } - } - - static public function purge(){ $cacheTimeLimit = time() - 86400; // 86400 -> 24h $cachePath = 'cache'; From 9ac678aac50845674b8eb836e232fa236a2d8ca5 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 7 Oct 2016 22:33:45 +0200 Subject: [PATCH 02/16] [Cache] Move 'purge' function to implementations The purge function is cache specific and thus belongs to the specific implementation. --- caches/FileCache.php | 20 ++++++++++++++++++++ index.php | 4 ++-- lib/Cache.php | 21 --------------------- lib/CacheInterface.php | 1 + 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index eaf16f23..e11ce234 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -34,6 +34,26 @@ class FileCache extends CacheAbstract { return false; } + public function purgeCache(){ + $cacheTimeLimit = time() - 86400; // 86400 -> 24h + $cachePath = 'cache'; + if(file_exists($cachePath)){ + $cacheIterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($cachePath), + RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach($cacheIterator as $cacheFile){ + if(in_array($cacheFile->getBasename(), array('.', '..'))) + continue; + elseif($cacheFile->isFile()){ + if(filemtime($cacheFile->getPathname()) < $cacheTimeLimit) + unlink($cacheFile->getPathname()); + } + } + } + } + /** * Cache is prepared ? * Note : Cache name is based on request information, then cache must be prepare before use diff --git a/index.php b/index.php index d104caa4..43e777fe 100644 --- a/index.php +++ b/index.php @@ -84,8 +84,6 @@ if(!file_exists($whitelist_file)){ $whitelist_selection = explode("\n", file_get_contents($whitelist_file)); } -Cache::purge(); - try { Bridge::setDir(__DIR__ . '/bridges/'); @@ -120,6 +118,8 @@ try { $bridge = Bridge::create($bridge); $cache = Cache::create('FileCache'); + $cache->purgeCache(); + $bridge->setCache($cache); $noproxy = filter_input(INPUT_GET, '_noproxy', FILTER_VALIDATE_BOOLEAN); diff --git a/lib/Cache.php b/lib/Cache.php index 192cb3bf..414e6824 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -50,25 +50,4 @@ class Cache { static public function isValidNameCache($nameCache){ return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache); } - - static public function purge(){ - $cacheTimeLimit = time() - 86400; // 86400 -> 24h - $cachePath = 'cache'; - if(file_exists($cachePath)){ - $cacheIterator = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($cachePath), - RecursiveIteratorIterator::CHILD_FIRST - ); - - foreach($cacheIterator as $cacheFile){ - if(in_array($cacheFile->getBasename(), array('.', '..'))) - continue; - elseif($cacheFile->isFile()){ - if(filemtime($cacheFile->getPathname()) < $cacheTimeLimit) - unlink($cacheFile->getPathname()); - } - } - } - } - } diff --git a/lib/CacheInterface.php b/lib/CacheInterface.php index 4c59df30..ab1066e3 100644 --- a/lib/CacheInterface.php +++ b/lib/CacheInterface.php @@ -3,4 +3,5 @@ interface CacheInterface { public function loadData(); public function saveData($datas); public function getTime(); + public function purgeCache(); } From 5fdb3b2fd900f6ccf759307c174718113662e80e Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 7 Oct 2016 22:36:36 +0200 Subject: [PATCH 03/16] [FileCache] Build path using function instead of constant --- caches/FileCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index e11ce234..929d9026 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -36,7 +36,7 @@ class FileCache extends CacheAbstract { public function purgeCache(){ $cacheTimeLimit = time() - 86400; // 86400 -> 24h - $cachePath = 'cache'; + $cachePath = $this->getCachePath(); if(file_exists($cachePath)){ $cacheIterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($cachePath), From 45890d5969c0f615852c6ea28123d64e6907e043 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 7 Oct 2016 22:56:10 +0200 Subject: [PATCH 04/16] [FileCache] Don't store folder creation status The results of the function is_dir are already cached. See http://php.net/manual/en/function.is-dir.php --- caches/FileCache.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 929d9026..e3d86eea 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -3,7 +3,6 @@ * Cache with file system */ class FileCache extends CacheAbstract { - protected $cacheDirCreated; // boolean to avoid always chck dir cache existance public function loadData(){ $this->isPrepareCache(); @@ -75,9 +74,7 @@ class FileCache extends CacheAbstract { $cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ? // FIXME : implement recursive dir creation - if(is_null($this->cacheDirCreated) && !is_dir($cacheDir)){ - $this->cacheDirCreated = true; - + if(!is_dir($cacheDir)){ mkdir($cacheDir,0705); chmod($cacheDir,0705); } From ad825aa88a1b5e76421dd1253e4a5a8f5ae471a7 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 7 Oct 2016 23:16:33 +0200 Subject: [PATCH 05/16] [FileCache] Assign same permission to group as others It makes no sense for the group to get less access rights than anyone else --- caches/FileCache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index e3d86eea..5f74652d 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -75,8 +75,8 @@ class FileCache extends CacheAbstract { // FIXME : implement recursive dir creation if(!is_dir($cacheDir)){ - mkdir($cacheDir,0705); - chmod($cacheDir,0705); + mkdir($cacheDir,0755); + chmod($cacheDir,0755); } return $cacheDir; From 5f1c4e1c55dbf58f7972cd44dbf39e9305f2dbd4 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Fri, 7 Oct 2016 23:20:32 +0200 Subject: [PATCH 06/16] [FileCache] Implement recursive directory creation --- caches/FileCache.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 5f74652d..fc152b1a 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -73,10 +73,9 @@ class FileCache extends CacheAbstract { protected function getCachePath(){ $cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ? - // FIXME : implement recursive dir creation if(!is_dir($cacheDir)){ - mkdir($cacheDir,0755); - chmod($cacheDir,0755); + mkdir($cacheDir, 0755, true); + chmod($cacheDir, 0755); } return $cacheDir; From 0998cbde9d5edca546149320c5af2c7da17e2775 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 14:52:03 +0200 Subject: [PATCH 07/16] [cache] Directly implement CacheInterface in FileCache The function 'prepare' previously implemented in CacheAbstract is specifically required for FileCache and thus belongs to FileCache. Since this change removes all code from CacheAbstract, it can be removed completely. --- caches/FileCache.php | 10 +++++++++- lib/BridgeAbstract.php | 2 +- lib/CacheAbstract.php | 11 ----------- lib/RssBridge.php | 1 - 4 files changed, 10 insertions(+), 14 deletions(-) delete mode 100644 lib/CacheAbstract.php diff --git a/caches/FileCache.php b/caches/FileCache.php index fc152b1a..7878d348 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -2,7 +2,9 @@ /** * Cache with file system */ -class FileCache extends CacheAbstract { +class FileCache implements CacheInterface { + + protected $param; public function loadData(){ $this->isPrepareCache(); @@ -53,6 +55,12 @@ class FileCache extends CacheAbstract { } } + public function prepare(array $param){ + $this->param = $param; + + return $this; + } + /** * Cache is prepared ? * Note : Cache name is based on request information, then cache must be prepare before use diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index 2a648329..90e997c9 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -196,7 +196,7 @@ abstract class BridgeAbstract implements BridgeInterface { return static::URI; } - public function setCache(\CacheAbstract $cache){ + public function setCache(\CacheInterface $cache){ $this->cache = $cache; } } diff --git a/lib/CacheAbstract.php b/lib/CacheAbstract.php deleted file mode 100644 index e6c39d8c..00000000 --- a/lib/CacheAbstract.php +++ /dev/null @@ -1,11 +0,0 @@ -param = $param; - - return $this; - } -} diff --git a/lib/RssBridge.php b/lib/RssBridge.php index 0052abc4..37992d38 100644 --- a/lib/RssBridge.php +++ b/lib/RssBridge.php @@ -14,7 +14,6 @@ require __DIR__ . '/Bridge.php'; require __DIR__ . '/BridgeAbstract.php'; require __DIR__ . '/FeedExpander.php'; require __DIR__ . '/Cache.php'; -require __DIR__ . '/CacheAbstract.php'; require __DIR__ . '/validation.php'; require __DIR__ . '/html.php'; From 5ccde61a19cb61281928f9b89145efdbe1d6a0e0 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 15:04:14 +0200 Subject: [PATCH 08/16] [FileCache] Rename 'prepare' to 'setParameters' This is a cosmetic change to use the same naming convention for all methods. --- caches/FileCache.php | 8 ++++++-- lib/BridgeAbstract.php | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 7878d348..58b7928f 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -55,7 +55,11 @@ class FileCache implements CacheInterface { } } - public function prepare(array $param){ + /** + * Set HTTP GET parameters + * @return self + */ + public function setParameters(array $param){ $this->param = $param; return $this; @@ -68,7 +72,7 @@ class FileCache implements CacheInterface { */ protected function isPrepareCache(){ if(is_null($this->param)){ - throw new \Exception('Please feed "prepare" method before try to load'); + throw new \Exception('Please feed "setParameters" method before try to load'); } return true; diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index 90e997c9..8ac633dd 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -138,7 +138,7 @@ abstract class BridgeAbstract implements BridgeInterface { */ public function setDatas(array $inputs){ if(!is_null($this->cache)){ - $this->cache->prepare($inputs); + $this->cache->setParameters($inputs); $time = $this->cache->getTime(); if($time !== false && (time() - static::CACHE_TIMEOUT < $time) From ac0a9a90ad13049d503a4c4e222d7ca3f1bde6d3 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 15:17:08 +0200 Subject: [PATCH 09/16] [FileCache] Build file name solely on given parameters Previously the cache file name was build on the original request URI which also included the parameters. This could result in different file name for the same request (different format). Removing the format from the request is already done in index.php and could lead to issues in the future (if new parameters are introduced). --- caches/FileCache.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 58b7928f..261fe0c3 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -107,9 +107,6 @@ class FileCache implements CacheInterface { */ protected function getCacheName(){ $this->isPrepareCache(); - - $stringToEncode = $_SERVER['REQUEST_URI'] . http_build_query($this->param); - $stringToEncode = preg_replace('/(\?|&)format=[^&]*/i', '$1', $stringToEncode); - return hash('sha1', $stringToEncode) . '.cache'; + return hash('sha1', http_build_query($this->param)) . '.cache'; } } From d941fa41f6a353ad8eefa2e210c105cc545a1938 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 15:21:10 +0200 Subject: [PATCH 10/16] [FileCache] Remove 'isPrepareCache' There is no need to check the absense of the parameters in all functions. Instead 'getCacheName' is the only function actually using the parameters and thus should check the availability. --- caches/FileCache.php | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 261fe0c3..2ece7e2d 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -7,14 +7,11 @@ class FileCache implements CacheInterface { protected $param; public function loadData(){ - $this->isPrepareCache(); $datas = unserialize(file_get_contents($this->getCacheFile())); return $datas; } public function saveData($datas){ - $this->isPrepareCache(); - $writeStream = file_put_contents($this->getCacheFile(), serialize($datas)); if(!$writeStream) { @@ -25,8 +22,6 @@ class FileCache implements CacheInterface { } public function getTime(){ - $this->isPrepareCache(); - $cacheFile = $this->getCacheFile(); if(file_exists($cacheFile)){ return filemtime($cacheFile); @@ -65,19 +60,6 @@ class FileCache implements CacheInterface { return $this; } - /** - * Cache is prepared ? - * Note : Cache name is based on request information, then cache must be prepare before use - * @return \Exception|true - */ - protected function isPrepareCache(){ - if(is_null($this->param)){ - throw new \Exception('Please feed "setParameters" method before try to load'); - } - - return true; - } - /** * Return cache path (and create if not exist) * @return string Cache path @@ -106,7 +88,10 @@ class FileCache implements CacheInterface { * return string */ protected function getCacheName(){ - $this->isPrepareCache(); + if(is_null($this->param)){ + throw new \Exception('Call "setParameters" first!'); + } + return hash('sha1', http_build_query($this->param)) . '.cache'; } } From 5de4a59d412aabb68a41aabdfea89547c65434be Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 15:28:36 +0200 Subject: [PATCH 11/16] [index] Initialize cache before loading to bridge Previously BridgeAbstract needed to know which exact implementation of CacheInterface was used (since we only got one right now its not a problem). Initializing the cache in index.php instead allows to change cache types more easily. --- index.php | 13 ++++++++----- lib/BridgeAbstract.php | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/index.php b/index.php index 43e777fe..6f447d92 100644 --- a/index.php +++ b/index.php @@ -117,11 +117,6 @@ try { // Data retrieval $bridge = Bridge::create($bridge); - $cache = Cache::create('FileCache'); - $cache->purgeCache(); - - $bridge->setCache($cache); - $noproxy = filter_input(INPUT_GET, '_noproxy', FILTER_VALIDATE_BOOLEAN); if(defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy){ define('NOPROXY',true); @@ -132,6 +127,14 @@ try { unset($params['bridge']); unset($params['format']); unset($params['_noproxy']); + + // Initialize cache + $cache = Cache::create('FileCache'); + $cache->purgeCache(); + $cache->setParameters($params); + + // Load cache & data + $bridge->setCache($cache); $bridge->setDatas($params); // Data transformation diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index 8ac633dd..95170996 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -138,7 +138,6 @@ abstract class BridgeAbstract implements BridgeInterface { */ public function setDatas(array $inputs){ if(!is_null($this->cache)){ - $this->cache->setParameters($inputs); $time = $this->cache->getTime(); if($time !== false && (time() - static::CACHE_TIMEOUT < $time) From 5639b158e77479dd4e47548bdd0e0bdddd53e76a Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 15:34:17 +0200 Subject: [PATCH 12/16] [FileCache] Change parameters to lower-case This prevents creating multiple cache files for the same request. --- caches/FileCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 2ece7e2d..3691fe51 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -55,7 +55,7 @@ class FileCache implements CacheInterface { * @return self */ public function setParameters(array $param){ - $this->param = $param; + $this->param = array_map('strtolower', $param); return $this; } From 2d56b717cf85e9429a03690a645d237178325853 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 16:03:08 +0200 Subject: [PATCH 13/16] [FileCache] Add property to define cache folder --- caches/FileCache.php | 38 +++++++++++++++++++++++++++++--------- index.php | 1 + 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index 3691fe51..bffdff3f 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -4,6 +4,7 @@ */ class FileCache implements CacheInterface { + protected $path; protected $param; public function loadData(){ @@ -32,7 +33,7 @@ class FileCache implements CacheInterface { public function purgeCache(){ $cacheTimeLimit = time() - 86400; // 86400 -> 24h - $cachePath = $this->getCachePath(); + $cachePath = $this->getPath(); if(file_exists($cachePath)){ $cacheIterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($cachePath), @@ -50,6 +51,28 @@ class FileCache implements CacheInterface { } } + /** + * Set cache path + * @return self + */ + public function setPath($path){ + if(is_null($path) || !is_string($path)){ + throw new \Exception('The given path is invalid!'); + } + + $this->path = $path; + + // Make sure path ends with '/' or '\' + $lastchar = substr($this->path, -1, 1); + if($lastchar !== '/' && $lastchar !== '\\') + $this->path .= '/'; + + if(!is_dir($this->path)) + mkdir($this->path, 0755, true); + + return $this; + } + /** * Set HTTP GET parameters * @return self @@ -64,15 +87,12 @@ class FileCache implements CacheInterface { * Return cache path (and create if not exist) * @return string Cache path */ - protected function getCachePath(){ - $cacheDir = __DIR__ . '/../cache/'; // FIXME : configuration ? - - if(!is_dir($cacheDir)){ - mkdir($cacheDir, 0755, true); - chmod($cacheDir, 0755); + protected function getPath(){ + if(is_null($this->path)){ + throw new \Exception('Call "setPath" first!'); } - return $cacheDir; + return $this->path; } /** @@ -80,7 +100,7 @@ class FileCache implements CacheInterface { * @return string Path to the file cache */ protected function getCacheFile(){ - return $this->getCachePath() . $this->getCacheName(); + return $this->getPath() . $this->getCacheName(); } /** diff --git a/index.php b/index.php index 6f447d92..a20415d8 100644 --- a/index.php +++ b/index.php @@ -130,6 +130,7 @@ try { // Initialize cache $cache = Cache::create('FileCache'); + $cache->setPath(__DIR__ . '/cache'); $cache->purgeCache(); $cache->setParameters($params); From 5c309e93dc82e3af8b465944961b48fc4f685759 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 16:18:10 +0200 Subject: [PATCH 14/16] [cache] Specify cache duration for 'purgeCache' --- caches/FileCache.php | 5 ++--- index.php | 2 +- lib/CacheInterface.php | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/caches/FileCache.php b/caches/FileCache.php index bffdff3f..ce8394e2 100644 --- a/caches/FileCache.php +++ b/caches/FileCache.php @@ -31,8 +31,7 @@ class FileCache implements CacheInterface { return false; } - public function purgeCache(){ - $cacheTimeLimit = time() - 86400; // 86400 -> 24h + public function purgeCache($duration){ $cachePath = $this->getPath(); if(file_exists($cachePath)){ $cacheIterator = new RecursiveIteratorIterator( @@ -44,7 +43,7 @@ class FileCache implements CacheInterface { if(in_array($cacheFile->getBasename(), array('.', '..'))) continue; elseif($cacheFile->isFile()){ - if(filemtime($cacheFile->getPathname()) < $cacheTimeLimit) + if(filemtime($cacheFile->getPathname()) < time() - $duration) unlink($cacheFile->getPathname()); } } diff --git a/index.php b/index.php index a20415d8..599135a6 100644 --- a/index.php +++ b/index.php @@ -131,7 +131,7 @@ try { // Initialize cache $cache = Cache::create('FileCache'); $cache->setPath(__DIR__ . '/cache'); - $cache->purgeCache(); + $cache->purgeCache(86400); // 24 hours $cache->setParameters($params); // Load cache & data diff --git a/lib/CacheInterface.php b/lib/CacheInterface.php index ab1066e3..5753c0eb 100644 --- a/lib/CacheInterface.php +++ b/lib/CacheInterface.php @@ -3,5 +3,5 @@ interface CacheInterface { public function loadData(); public function saveData($datas); public function getTime(); - public function purgeCache(); + public function purgeCache($duration); } From 8fb4db8914db03b3c6a0589ba8fd807574fdb027 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 16:21:00 +0200 Subject: [PATCH 15/16] [index] Introduce CACHE_DIR --- index.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index 599135a6..fba21678 100644 --- a/index.php +++ b/index.php @@ -19,6 +19,9 @@ define('PROXY_NAME', 'Hidden Proxy Name'); date_default_timezone_set('UTC'); error_reporting(0); +// Specify directory for cached files (using FileCache) +define('CACHE_DIR', __DIR__ . '/cache'); + /* Create a file named 'DEBUG' for enabling debug mode. For further security, you may put whitelisted IP addresses @@ -130,7 +133,7 @@ try { // Initialize cache $cache = Cache::create('FileCache'); - $cache->setPath(__DIR__ . '/cache'); + $cache->setPath(CACHE_DIR); $cache->purgeCache(86400); // 24 hours $cache->setParameters($params); From b6feda2377e2ffe5ae5f9b8812a28a6a0203360d Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Sat, 8 Oct 2016 16:30:01 +0200 Subject: [PATCH 16/16] [contents] Use FileCache for getSimpleHTMLDOMCached --- lib/contents.php | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/lib/contents.php b/lib/contents.php index 747db5bf..470efeb3 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -103,30 +103,24 @@ function getSimpleHTMLDOMCached($url ){ debugMessage('Caching url ' . $url . ', duration ' . $duration); - $filepath = __DIR__ . '/../cache/pages/' . sha1($url) . '.cache'; - debugMessage('Cache file ' . $filepath); + // Initialize cache + $cache = Cache::create('FileCache'); + $cache->setPath(CACHE_DIR . '/pages'); + $cache->purgeCache(86400); // 24 hours (forced) - if(file_exists($filepath) && filectime($filepath) < time() - $duration){ - unlink ($filepath); - debugMessage('Cached file deleted: ' . $filepath); - } - - if(file_exists($filepath)){ - debugMessage('Loading cached file ' . $filepath); - touch($filepath); - $content = file_get_contents($filepath); - } else { - debugMessage('Caching ' . $url . ' to ' . $filepath); - $dir = substr($filepath, 0, strrpos($filepath, '/')); - - if(!is_dir($dir)){ - debugMessage('Creating directory ' . $dir); - mkdir($dir, 0777, true); - } + $params = [$url]; + $cache->setParameters($params); + // Determine if cached file is within duration + $time = $cache->getTime(); + if($time !== false + && (time() - $duration < $time) + && (!defined('DEBUG') || DEBUG !== true)){ // Contents within duration + $content = $cache->loadData(); + } else { // Content not within duration $content = getContents($url, $use_include_path, $context, $offset, $maxLen); if($content !== false){ - file_put_contents($filepath, $content); + $cache->saveData($content); } }