From e5f9febd1bf52130b12d50f71c259c058197cd6d Mon Sep 17 00:00:00 2001 From: Geolim4 Date: Tue, 2 Feb 2016 19:55:37 +0100 Subject: [PATCH] ### Issue #182 Global changes: - Removed exception catch on chmod() as it doesn't trigger any error - Replaced SQl Exceptions with custom Exceptions: - phpfastcacheCoreException - phpfastcacheDriverException - Removed use of aliases, e.g: is_writeable() to is_writable() - Cleaned some indentation on sqlite.php --- phpfastcache/3.0.0/abstract.php | 20 +++--- phpfastcache/3.0.0/drivers/files.php | 12 ++-- phpfastcache/3.0.0/drivers/sqlite.php | 72 +++++++++---------- .../exceptions/phpfastcacheCoreException.php | 11 +++ .../phpfastcacheDriverException.php | 11 +++ phpfastcache/3.0.0/phpfastcache.php | 24 +++---- 6 files changed, 80 insertions(+), 70 deletions(-) create mode 100644 phpfastcache/3.0.0/exceptions/phpfastcacheCoreException.php create mode 100644 phpfastcache/3.0.0/exceptions/phpfastcacheDriverException.php diff --git a/phpfastcache/3.0.0/abstract.php b/phpfastcache/3.0.0/abstract.php index 1f3381132..3fadf9bcf 100644 --- a/phpfastcache/3.0.0/abstract.php +++ b/phpfastcache/3.0.0/abstract.php @@ -157,14 +157,14 @@ public function isExisting($keyword) * todo: search * @param $query * @return mixed - * @throws \Exception + * @throws \phpfastcacheCoreException */ public function search($query) { if (method_exists($this, "driver_search")) { return $this->driver_search($query); } - throw new Exception('Search method is not supported by this driver.'); + throw new phpfastcacheCoreException('Search method is not supported by this driver.'); } /** @@ -376,7 +376,7 @@ public function __get($name) * @param $name * @param $v * @return bool|null - * @throws \Exception + * @throws \phpfastcacheCoreException */ public function __set($name, $v) { @@ -384,7 +384,7 @@ public function __set($name, $v) return $this->set($name, $v[ 0 ], $v[ 1 ], isset($v[ 2 ]) ? $v[ 2 ] : array()); } else { - throw new Exception("Example ->$name = array('VALUE', 300);", 98); + throw new phpfastcacheCoreException("Example ->$name = array('VALUE', 300);", 98); } } @@ -424,7 +424,7 @@ protected function required_extension($name) /** * @param $file * @return string - * @throws \Exception + * @throws \phpfastcacheCoreException */ protected function readfile($file) { @@ -435,7 +435,7 @@ protected function readfile($file) $file_handle = @fopen($file, "r"); if (!$file_handle) { - throw new Exception("Can't Read File", 96); + throw new phpfastcacheCoreException("Can't Read File", 96); } while (!feof($file_handle)) { @@ -488,7 +488,7 @@ protected function decode($value) /** * Auto Create .htaccess to protect cache folder * @param string $path - * @throws \Exception + * @throws \phpfastcacheCoreException */ protected function htaccessGen($path = "") { @@ -502,7 +502,7 @@ protected function htaccessGen($path = "") $f = @fopen($path . "/.htaccess", "w+"); if (!$f) { - throw new Exception("Can't create .htaccess", 97); + throw new phpfastcacheCoreException("Can't create .htaccess", 97); } fwrite($f, $html); fclose($f); @@ -526,7 +526,7 @@ protected function isPHPModule() /** * return System Information * @return mixed - * @throws \Exception + * @throws \phpfastcacheCoreException */ public function systemInfo() { @@ -536,7 +536,7 @@ public function systemInfo() $this->option[ 'system' ][ 'drivers' ] = array(); $dir = @opendir(dirname(__FILE__) . "/drivers/"); if (!$dir) { - throw new Exception("Can't open file dir ext", 100); + throw new phpfastcacheCoreException("Can't open file dir ext", 100); } while ($file = @readdir($dir)) { diff --git a/phpfastcache/3.0.0/drivers/files.php b/phpfastcache/3.0.0/drivers/files.php index 8894cf094..7b8be842b 100644 --- a/phpfastcache/3.0.0/drivers/files.php +++ b/phpfastcache/3.0.0/drivers/files.php @@ -18,7 +18,7 @@ public function __construct($config = array()) $this->getPath(); // force create path if (!$this->checkdriver() && !isset($config[ 'skipError' ])) { - throw new Exception("Can't use this driver for your website!"); + throw new phpfastcacheDriverException("Can't use this driver for your website!"); } } @@ -78,7 +78,7 @@ private function getFilePath($keyword, $skip = false) if($skip == false) { if(!file_exists($path)) { if(!mkdir($path,$this->__setChmodAuto())) { - throw new Exception("PLEASE CHMOD ".$this->getPath()." - 0777 OR ANY WRITABLE PERMISSION!",92); + throw new phpfastcacheDriverException("PLEASE CHMOD ".$this->getPath()." - 0777 OR ANY WRITABLE PERMISSION!",92); } } } @@ -192,7 +192,7 @@ public function driver_stats($option = array()) $path = $this->getPath(); $dir = @opendir($path); if (!$dir) { - throw new Exception("Can't read PATH:" . $path, 94); + throw new phpfastcacheDriverException("Can't read PATH:" . $path, 94); } $total = 0; @@ -202,7 +202,7 @@ public function driver_stats($option = array()) // read sub dir $subdir = opendir($path."/".$file); if(!$subdir) { - throw new Exception("Can't read path:".$path."/".$file,93); + throw new phpfastcacheDriverException("Can't read path:".$path."/".$file,93); } while($f = readdir($subdir)) { @@ -264,7 +264,7 @@ public function driver_clean($option = array()) $path = $this->getPath(); $dir = @opendir($path); if (!$dir) { - throw new Exception("Can't read PATH:" . $path, 94); + throw new phpfastcacheDriverException("Can't read PATH:" . $path, 94); } while($file=readdir($dir)) { @@ -272,7 +272,7 @@ public function driver_clean($option = array()) // read sub dir $subdir = @opendir($path . "/" . $file); if (!$subdir) { - throw new Exception("Can't read path:" . $path . "/" . $file, + throw new phpfastcacheDriverException("Can't read path:" . $path . "/" . $file, 93); } diff --git a/phpfastcache/3.0.0/drivers/sqlite.php b/phpfastcache/3.0.0/drivers/sqlite.php index 718725a4f..3a139568c 100644 --- a/phpfastcache/3.0.0/drivers/sqlite.php +++ b/phpfastcache/3.0.0/drivers/sqlite.php @@ -86,9 +86,9 @@ public function initIndexing(PDO $db) // delete everything before reset indexing $dir = opendir($this->path); - while($file = readdir($dir)) { - if($file != "." && $file!=".." && $file != "indexing" && $file!="dbfastcache") { - unlink($this->path."/".$file); + while ($file = readdir($dir)) { + if ($file != "." && $file != ".." && $file != "indexing" && $file != "dbfastcache") { + unlink($this->path . "/" . $file); } } @@ -109,7 +109,7 @@ public function indexing($keyword) { if ($this->indexing == null) { $createTable = false; - if(!file_exists($this->path."/indexing")) { + if (!file_exists($this->path . "/indexing")) { $createTable = true; } @@ -136,8 +136,8 @@ public function indexing($keyword) // check file size - $size = file_exists($this->path."/db".$db) ? filesize($this->path."/db".$db) : 1; - $size = round($size / 1024 / 1024,1); + $size = file_exists($this->path . "/db" . $db) ? filesize($this->path . "/db" . $db) : 1; + $size = round($size / 1024 / 1024, 1); if ($size > $this->max_size) { @@ -177,18 +177,18 @@ public function indexing($keyword) */ public function db($keyword, $reset = false) { - /* + /** * Default is fastcache */ $instant = $this->indexing($keyword); - /* + /** * init instant */ if (!isset($this->instant[ $instant ])) { // check DB Files ready or not $createTable = false; - if(!file_exists($this->path."/db".$instant) || $reset == true) { + if (!file_exists($this->path . "/db" . $instant) || $reset == true) { $createTable = true; } $PDO = new PDO("sqlite:" . $this->path . "/db" . $instant); @@ -233,7 +233,7 @@ public function driver_set( $time = 300, $option = array() ) { - $skipExisting = isset($option['skipExisting']) ? $option['skipExisting'] : false; + $skipExisting = isset($option[ 'skipExisting' ]) ? $option[ 'skipExisting' ] : false; $toWrite = true; // check in cache first @@ -271,14 +271,9 @@ public function driver_set( } catch (PDOException $e) { return false; } - } - - } - return false; - } /** @@ -393,30 +388,29 @@ public function driver_stats($option = array()) $optimized = 0; $dir = opendir($this->path); - while($file = readdir($dir)) { - if($file!="." && $file!="..") { - $file_path = $this->path."/".$file; + while ($file = readdir($dir)) { + if ($file != "." && $file != "..") { + $file_path = $this->path . "/" . $file; $size = filesize($file_path); $total = $total + $size; - try { - $PDO = new PDO("sqlite:".$file_path); - $PDO->setAttribute(PDO::ATTR_ERRMODE, - PDO::ERRMODE_EXCEPTION); - - $stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U"); - $stm->execute(array( - ":U" => date("U"), - )); + try { + $PDO = new PDO("sqlite:" . $file_path); + $PDO->setAttribute(PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION); - $PDO->exec("VACUUM;"); - $size = filesize($file_path); - $optimized = $optimized + $size; - } catch (PDOException $e) { - $size = 0; - $optimized = 0; - } + $stm = $PDO->prepare("DELETE FROM `caching` WHERE `exp` <= :U"); + $stm->execute(array( + ":U" => date("U"), + )); + $PDO->exec("VACUUM;"); + $size = filesize($file_path); + $optimized = $optimized + $size; + } catch (PDOException $e) { + $size = 0; + $optimized = 0; + } } @@ -438,13 +432,13 @@ public function driver_clean($option = array()) // close connection $this->instant = array(); - $this->indexing = NULL; - + $this->indexing = null; + // delete everything before reset indexing $dir = opendir($this->path); - while($file = readdir($dir)) { - if($file != "." && $file!="..") { - unlink($this->path."/".$file); + while ($file = readdir($dir)) { + if ($file != "." && $file != "..") { + unlink($this->path . "/" . $file); } } } diff --git a/phpfastcache/3.0.0/exceptions/phpfastcacheCoreException.php b/phpfastcache/3.0.0/exceptions/phpfastcacheCoreException.php new file mode 100644 index 000000000..f01f7203c --- /dev/null +++ b/phpfastcache/3.0.0/exceptions/phpfastcacheCoreException.php @@ -0,0 +1,11 @@ + http://www.phpfastcache.com + * @author Georges.L (Geolim4) + */ +class phpfastcacheCoreException extends Exception +{ + +} \ No newline at end of file diff --git a/phpfastcache/3.0.0/exceptions/phpfastcacheDriverException.php b/phpfastcache/3.0.0/exceptions/phpfastcacheDriverException.php new file mode 100644 index 000000000..87cd2ba62 --- /dev/null +++ b/phpfastcache/3.0.0/exceptions/phpfastcacheDriverException.php @@ -0,0 +1,11 @@ + http://www.phpfastcache.com + * @author Georges.L (Geolim4) + */ +class phpfastcacheDriverException extends Exception +{ + +} \ No newline at end of file diff --git a/phpfastcache/3.0.0/phpfastcache.php b/phpfastcache/3.0.0/phpfastcache.php index 6ab7a4bd2..4a66ae0d5 100644 --- a/phpfastcache/3.0.0/phpfastcache.php +++ b/phpfastcache/3.0.0/phpfastcache.php @@ -7,6 +7,8 @@ require_once(dirname(__FILE__) . "/abstract.php"); require_once(dirname(__FILE__) . "/driver.php"); +require_once(dirname(__FILE__) . "/exceptions/phpfastcacheCoreException.php"); +require_once(dirname(__FILE__) . "/exceptions/phpfastcacheDriverException.php"); /** * Short function @@ -203,8 +205,7 @@ public static function getPath($skip_create_path = false, $config) $tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir(); $path = $tmp_dir; } else { - $path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? rtrim($_SERVER[ 'DOCUMENT_ROOT' ], - "/") . "/../" : rtrim(dirname(__FILE__), "/") . "/"; + $path = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? rtrim($_SERVER[ 'DOCUMENT_ROOT' ], "/") . "/../" : rtrim(dirname(__FILE__), "/") . "/"; } if (self::$config[ 'path' ] != "") { @@ -244,8 +245,7 @@ public static function getPath($skip_create_path = false, $config) @chmod($full_path, self::__setChmodAuto($config)); } if (!@file_exists($full_path) || !@is_writable($full_path)) { - throw new Exception("PLEASE CREATE OR CHMOD " . $full_path . " - 0777 OR ANY WRITABLE PERMISSION!", - 92); + throw new phpfastcacheCoreException("PLEASE CREATE OR CHMOD " . $full_path . " - 0777 OR ANY WRITABLE PERMISSION!", 92); } } @@ -325,16 +325,13 @@ protected static function htaccessGen($path, $create = true) { if ($create == true) { - if (!is_writeable($path)) { - try { - chmod($path, 0777); - } catch (Exception $e) { - throw new Exception("PLEASE CHMOD " . $path . " - 0777 OR ANY WRITABLE PERMISSION!", - 92); + if (!is_writable($path)) { + if (!chmod($path, 0777)) { + throw new phpfastcacheCoreException("PLEASE CHMOD " . $path . " - 0777 OR ANY WRITABLE PERMISSION!", 92); } } - if(!file_exists($path."/.htaccess")) { + if (!file_exists($path . "/.htaccess")) { // echo "write me"; $html = "order deny, allow \r\n deny from all \r\n @@ -342,13 +339,10 @@ protected static function htaccessGen($path, $create = true) $f = @fopen($path . "/.htaccess", "w+"); if (!$f) { - throw new Exception("PLEASE CHMOD " . $path . " - 0777 OR ANY WRITABLE PERMISSION!", - 92); + throw new phpfastcacheCoreException("PLEASE CHMOD " . $path . " - 0777 OR ANY WRITABLE PERMISSION!", 92); } fwrite($f, $html); fclose($f); - - } }