From 85055ac97fa57ca339924cef719115d85bdf6c2e Mon Sep 17 00:00:00 2001 From: David M Date: Thu, 3 Jan 2008 17:00:40 +0000 Subject: oh boy... - Migrate code base to PHP 5.1+ git-svn-id: file:///svn/phpbb/trunk@8295 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 43 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 775e8d4495..fa6e79260b 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -22,18 +22,18 @@ if (!defined('IN_PHPBB')) */ class acm { - var $vars = array(); - var $var_expires = array(); - var $is_modified = false; + private $vars = array(); + private $var_expires = array(); + private $is_modified = false; - var $sql_rowset = array(); - var $sql_row_pointer = array(); - var $cache_dir = ''; + public $sql_rowset = array(); + private $sql_row_pointer = array(); + public $cache_dir = ''; /** * Set cache path */ - function acm() + function __construct() { global $phpbb_root_path; $this->cache_dir = $phpbb_root_path . 'cache/'; @@ -42,25 +42,24 @@ class acm /** * Load global cache */ - function load() + private function load() { global $phpEx; + + // grab the global cache if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) { @include($this->cache_dir . 'data_global.' . $phpEx); - } - else - { - return false; + return true; } - return true; + return false; } /** * Unload cache object */ - function unload() + public function unload() { $this->save(); unset($this->vars); @@ -77,7 +76,7 @@ class acm /** * Save modified objects */ - function save() + private function save() { if (!$this->is_modified) { @@ -112,7 +111,7 @@ class acm /** * Tidy cache */ - function tidy() + public function tidy() { global $phpEx; @@ -161,9 +160,9 @@ class acm /** * Get saved cache object */ - function get($var_name) + public function get($var_name) { - if ($var_name[0] == '_') + if ($var_name[0] === '_') { global $phpEx; @@ -186,7 +185,7 @@ class acm */ function put($var_name, $var, $ttl = 31536000) { - if ($var_name[0] == '_') + if ($var_name[0] === '_') { global $phpEx; @@ -252,7 +251,7 @@ class acm { global $phpEx; - if ($var_name == 'sql' && !empty($table)) + if ($var_name === 'sql' && !empty($table)) { if (!is_array($table)) { @@ -310,7 +309,7 @@ class acm return; } - if ($var_name[0] == '_') + if ($var_name[0] === '_') { $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx"); } @@ -330,7 +329,7 @@ class acm */ function _exists($var_name) { - if ($var_name[0] == '_') + if ($var_name[0] === '_') { global $phpEx; return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx"); -- cgit v1.2.1 From af738dbc2a48713f59779410955282aa5760b741 Mon Sep 17 00:00:00 2001 From: David M Date: Fri, 4 Jan 2008 18:35:49 +0000 Subject: Ch-ch-ch-changes - Made us more DB independent by making many queries capability based instead of DB specific - Finished PHP5ifying of the acm_file class, now with some (hopefully) enhancements to its performance - Sped up viewforum considerably (also goes towards mcp_forum) I really hope I didn't explode CVS... git-svn-id: file:///svn/phpbb/trunk@8301 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index fa6e79260b..3be4483774 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -88,7 +88,7 @@ class acm if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb')) { @flock($fp, LOCK_EX); - fwrite($fp, "vars = " . var_export($this->vars, true) . ";\n\n\$this->var_expires = " . var_export($this->var_expires, true) . "\n?>"); + fwrite($fp, "vars = unserialize('" . serialize($this->vars) . "');\n\$this->var_expires = unserialize('" . serialize($this->var_expires) . "');"); @flock($fp, LOCK_UN); fclose($fp); @@ -192,7 +192,7 @@ class acm if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb')) { @flock($fp, LOCK_EX); - fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\n\$data = " . var_export($var, true) . ";\n?>"); + fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($var) ? "unserialize('" . serialize($var) . "');" : 'array();')); @flock($fp, LOCK_UN); fclose($fp); @@ -210,7 +210,7 @@ class acm /** * Purge cache data */ - function purge() + public function purge() { // Purge all phpbb cache files $dir = @opendir($this->cache_dir); @@ -247,7 +247,7 @@ class acm /** * Destroy cache data */ - function destroy($var_name, $table = '') + public function destroy($var_name, $table = '') { global $phpEx; @@ -327,7 +327,7 @@ class acm /** * Check if a given cache entry exist */ - function _exists($var_name) + private function _exists($var_name) { if ($var_name[0] === '_') { @@ -353,7 +353,7 @@ class acm /** * Load cached sql query */ - function sql_load($query) + public function sql_load($query) { global $phpEx; @@ -386,7 +386,7 @@ class acm /** * Save sql query */ - function sql_save($query, &$query_result, $ttl) + public function sql_save($query, &$query_result, $ttl) { global $db, $phpEx; @@ -408,10 +408,10 @@ class acm } $db->sql_freeresult($query_result); - $file = " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n"; - fwrite($fp, $file . "\n\$this->sql_rowset[\$query_id] = " . var_export($this->sql_rowset[$query_id], true) . ";\n?>"); + fwrite($fp, $file . "\$this->sql_rowset[\$query_id] = " . (sizeof($this->sql_rowset[$query_id]) ? "unserialize('" . serialize($this->sql_rowset[$query_id]) . "');" : 'array();')); @flock($fp, LOCK_UN); fclose($fp); @@ -421,18 +421,10 @@ class acm } } - /** - * Ceck if a given sql query exist in cache - */ - function sql_exists($query_id) - { - return isset($this->sql_rowset[$query_id]); - } - /** * Fetch row from cache (database) */ - function sql_fetchrow($query_id) + public function sql_fetchrow($query_id) { if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) { @@ -445,7 +437,7 @@ class acm /** * Fetch a field from the current row of a cached database result (database) */ - function sql_fetchfield($query_id, $field) + public function sql_fetchfield($query_id, $field) { if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) { @@ -458,7 +450,7 @@ class acm /** * Seek a specific row in an a cached database result (database) */ - function sql_rowseek($rownum, $query_id) + public function sql_rowseek($rownum, $query_id) { if ($rownum >= sizeof($this->sql_rowset[$query_id])) { @@ -472,7 +464,7 @@ class acm /** * Free memory used for a cached database result (database) */ - function sql_freeresult($query_id) + public function sql_freeresult($query_id) { if (!isset($this->sql_rowset[$query_id])) { @@ -488,7 +480,7 @@ class acm /** * Removes/unlinks file */ - function remove_file($filename) + private function remove_file($filename) { if (!@unlink($filename)) { -- cgit v1.2.1 From 7b262babcd5c862c3068b08da8bba5f48bd5f36a Mon Sep 17 00:00:00 2001 From: David M Date: Sun, 3 Feb 2008 10:19:04 +0000 Subject: Alright, this should give some improved performance :) This is the end of random seek access to rows. If you have a compelling reason as to why they should stay, contact me. Else, they are gone forevermore... The following API calls are deprecated: acm::sql_rowseek() -> no replacement $db->sql_fetchfield($field, $rownum = false, $query_id = false) -> $db->sql_fetchfield($field, $query_id = false) Initial tests show that phpBB3 over four percent of memory against phpBB3.1 on an empty board. So far so good :) Other cool things: db2, MS SQL ODBC and MS SQL 2005 all use less memory because they do not need to reference the last executed query to handle random access seeks :) P.S. The crazy people using SVN: please report any issues with the new way we itterate through caches, I do not want to miss anything :) git-svn-id: file:///svn/phpbb/trunk@8372 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 3be4483774..f123f1383d 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -27,7 +27,6 @@ class acm private $is_modified = false; public $sql_rowset = array(); - private $sql_row_pointer = array(); public $cache_dir = ''; /** @@ -65,12 +64,10 @@ class acm unset($this->vars); unset($this->var_expires); unset($this->sql_rowset); - unset($this->sql_row_pointer); $this->vars = array(); $this->var_expires = array(); $this->sql_rowset = array(); - $this->sql_row_pointer = array(); } /** @@ -234,12 +231,10 @@ class acm unset($this->vars); unset($this->var_expires); unset($this->sql_rowset); - unset($this->sql_row_pointer); $this->vars = array(); $this->var_expires = array(); $this->sql_rowset = array(); - $this->sql_row_pointer = array(); $this->is_modified = false; } @@ -378,7 +373,6 @@ class acm return false; } - $this->sql_row_pointer[$query_id] = 0; return $query_id; } @@ -400,7 +394,6 @@ class acm $query_id = sizeof($this->sql_rowset); $this->sql_rowset[$query_id] = array(); - $this->sql_row_pointer[$query_id] = 0; while ($row = $db->sql_fetchrow($query_result)) { @@ -426,12 +419,9 @@ class acm */ public function sql_fetchrow($query_id) { - if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) - { - return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++]; - } + list(, $row) = each($this->sql_rowset[$query_id]); - return false; + return ($row !== NULL) ? $row : false; } /** @@ -439,26 +429,9 @@ class acm */ public function sql_fetchfield($query_id, $field) { - if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) - { - return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false; - } - - return false; - } - - /** - * Seek a specific row in an a cached database result (database) - */ - public function sql_rowseek($rownum, $query_id) - { - if ($rownum >= sizeof($this->sql_rowset[$query_id])) - { - return false; - } + $row = current($this->sql_rowset[$query_id]); - $this->sql_row_pointer[$query_id] = $rownum; - return true; + return ($row !== false && isset($row[$field])) ? $row[$field] : false; } /** @@ -472,7 +445,6 @@ class acm } unset($this->sql_rowset[$query_id]); - unset($this->sql_row_pointer[$query_id]); return true; } -- cgit v1.2.1 From 2cedbbac0944da85a01f3a3afbac65756610f29d Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sat, 23 Feb 2008 14:23:34 +0000 Subject: merge revisions #r8384, #r8387, #r8388, #r8389 and #r8390 git-svn-id: file:///svn/phpbb/trunk@8391 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index f123f1383d..f952b372c6 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -306,7 +306,7 @@ class acm if ($var_name[0] === '_') { - $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx"); + $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true); } else if (isset($this->vars[$var_name])) { @@ -369,7 +369,7 @@ class acm } else if ($expired) { - $this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"); + $this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx", true); return false; } @@ -452,13 +452,15 @@ class acm /** * Removes/unlinks file */ - private function remove_file($filename) + private function remove_file($filename, $check = false) { - if (!@unlink($filename)) + if ($check && !@is_writeable($this->cache_dir)) { // E_USER_ERROR - not using language entry - intended. trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); } + + return @unlink($filename); } } -- cgit v1.2.1 From 2f4a618900e2c3b6ea14c68cbeb5897cd2ac1a04 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Thu, 29 May 2008 12:25:56 +0000 Subject: ok... i hope i haven't messed too much with the code and everything is still working. Changes: - Ascraeus now uses constants for the phpbb root path and the php extension. This ensures more security for external applications and modifications (no more overwriting of root path and extension possible through insecure mods and register globals enabled) as well as no more globalizing needed. - A second change implemented here is an additional short-hand-notation for append_sid(). It is allowed to omit the root path and extension now (for example calling append_sid('memberlist')) - in this case the root path and extension get added automatically. The hook is called after these are added. git-svn-id: file:///svn/phpbb/trunk@8572 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 50 ++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 33 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index f952b372c6..9376549cba 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -34,8 +34,7 @@ class acm */ function __construct() { - global $phpbb_root_path; - $this->cache_dir = $phpbb_root_path . 'cache/'; + $this->cache_dir = PHPBB_ROOT_PATH . 'cache/'; } /** @@ -43,12 +42,10 @@ class acm */ private function load() { - global $phpEx; - // grab the global cache - if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) + if (file_exists($this->cache_dir . 'data_global.' . PHP_EXT)) { - @include($this->cache_dir . 'data_global.' . $phpEx); + @include($this->cache_dir . 'data_global.' . PHP_EXT); return true; } @@ -80,16 +77,14 @@ class acm return; } - global $phpEx; - - if ($fp = @fopen($this->cache_dir . 'data_global.' . $phpEx, 'wb')) + if ($fp = @fopen($this->cache_dir . 'data_global.' . PHP_EXT, 'wb')) { @flock($fp, LOCK_EX); fwrite($fp, "vars = unserialize('" . serialize($this->vars) . "');\n\$this->var_expires = unserialize('" . serialize($this->var_expires) . "');"); @flock($fp, LOCK_UN); fclose($fp); - @chmod($this->cache_dir . 'data_global.' . $phpEx, 0666); + @chmod($this->cache_dir . 'data_global.' . PHP_EXT, 0666); } else { @@ -99,7 +94,7 @@ class acm trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR); } - trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx, E_USER_ERROR); + trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . PHP_EXT, E_USER_ERROR); } $this->is_modified = false; @@ -110,8 +105,6 @@ class acm */ public function tidy() { - global $phpEx; - $dir = @opendir($this->cache_dir); if (!$dir) @@ -135,7 +128,7 @@ class acm } closedir($dir); - if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) + if (file_exists($this->cache_dir . 'data_global.' . PHP_EXT)) { if (!sizeof($this->vars)) { @@ -161,14 +154,12 @@ class acm { if ($var_name[0] === '_') { - global $phpEx; - if (!$this->_exists($var_name)) { return false; } - @include($this->cache_dir . "data{$var_name}.$phpEx"); + @include($this->cache_dir . "data{$var_name}." . PHP_EXT); return (isset($data)) ? $data : false; } else @@ -184,16 +175,14 @@ class acm { if ($var_name[0] === '_') { - global $phpEx; - - if ($fp = @fopen($this->cache_dir . "data{$var_name}.$phpEx", 'wb')) + if ($fp = @fopen($this->cache_dir . "data{$var_name}." . PHP_EXT, 'wb')) { @flock($fp, LOCK_EX); fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($var) ? "unserialize('" . serialize($var) . "');" : 'array();')); @flock($fp, LOCK_UN); fclose($fp); - @chmod($this->cache_dir . "data{$var_name}.$phpEx", 0666); + @chmod($this->cache_dir . "data{$var_name}." . PHP_EXT, 0666); } } else @@ -244,8 +233,6 @@ class acm */ public function destroy($var_name, $table = '') { - global $phpEx; - if ($var_name === 'sql' && !empty($table)) { if (!is_array($table)) @@ -306,7 +293,7 @@ class acm if ($var_name[0] === '_') { - $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true); + $this->remove_file($this->cache_dir . 'data' . $var_name . '.' . PHP_EXT, true); } else if (isset($this->vars[$var_name])) { @@ -326,8 +313,7 @@ class acm { if ($var_name[0] === '_') { - global $phpEx; - return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx"); + return file_exists($this->cache_dir . 'data' . $var_name . '.' . PHP_EXT); } else { @@ -350,18 +336,16 @@ class acm */ public function sql_load($query) { - global $phpEx; - // Remove extra spaces and tabs $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); $query_id = sizeof($this->sql_rowset); - if (!file_exists($this->cache_dir . 'sql_' . md5($query) . ".$phpEx")) + if (!file_exists($this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT)) { return false; } - @include($this->cache_dir . 'sql_' . md5($query) . ".$phpEx"); + @include($this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT); if (!isset($expired)) { @@ -369,7 +353,7 @@ class acm } else if ($expired) { - $this->remove_file($this->cache_dir . 'sql_' . md5($query) . ".$phpEx", true); + $this->remove_file($this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT, true); return false; } @@ -382,11 +366,11 @@ class acm */ public function sql_save($query, &$query_result, $ttl) { - global $db, $phpEx; + global $db; // Remove extra spaces and tabs $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); - $filename = $this->cache_dir . 'sql_' . md5($query) . '.' . $phpEx; + $filename = $this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT; if ($fp = @fopen($filename, 'wb')) { -- cgit v1.2.1 From e0799b79e2e1b9223af2d838f31966d25418af24 Mon Sep 17 00:00:00 2001 From: "Marek A. R" Date: Sun, 13 Jul 2008 19:08:30 +0000 Subject: Fix file ACM to work with cached single-quotes and other characters that could need escaping. git-svn-id: file:///svn/phpbb/trunk@8681 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 9376549cba..d8477376ea 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -80,7 +80,7 @@ class acm if ($fp = @fopen($this->cache_dir . 'data_global.' . PHP_EXT, 'wb')) { @flock($fp, LOCK_EX); - fwrite($fp, "vars = unserialize('" . serialize($this->vars) . "');\n\$this->var_expires = unserialize('" . serialize($this->var_expires) . "');"); + fwrite($fp, "vars = unserialize(" . var_export(serialize($this->vars), true) . ");\n\$this->var_expires = unserialize(" . var_export(serialize($this->var_expires), true) . ");"); @flock($fp, LOCK_UN); fclose($fp); @@ -178,7 +178,7 @@ class acm if ($fp = @fopen($this->cache_dir . "data{$var_name}." . PHP_EXT, 'wb')) { @flock($fp, LOCK_EX); - fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($var) ? "unserialize('" . serialize($var) . "');" : 'array();')); + fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($var) ? "unserialize(" . var_export(serialize($var), true) . ");" : 'array();')); @flock($fp, LOCK_UN); fclose($fp); @@ -388,7 +388,7 @@ class acm $file = " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n"; - fwrite($fp, $file . "\$this->sql_rowset[\$query_id] = " . (sizeof($this->sql_rowset[$query_id]) ? "unserialize('" . serialize($this->sql_rowset[$query_id]) . "');" : 'array();')); + fwrite($fp, $file . "\$this->sql_rowset[\$query_id] = " . (sizeof($this->sql_rowset[$query_id]) ? "unserialize(" . var_export(serialize($this->sql_rowset[$query_id]), true) . ");" : 'array();')); @flock($fp, LOCK_UN); fclose($fp); -- cgit v1.2.1 From 15e04808fc3a6699ac857c2252cce70090df80ba Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sat, 16 Aug 2008 13:11:50 +0000 Subject: writeable -> writable merge git-svn-id: file:///svn/phpbb/trunk@8762 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index d8477376ea..1093ee1fa2 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -143,7 +143,7 @@ class acm } } } - + set_config('cache_last_gc', time(), true); } @@ -438,7 +438,7 @@ class acm */ private function remove_file($filename, $check = false) { - if ($check && !@is_writeable($this->cache_dir)) + if ($check && !@is_writable($this->cache_dir)) { // E_USER_ERROR - not using language entry - intended. trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); -- cgit v1.2.1 From 8174462e895966ce18c336b60dda93e562f4096f Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Fri, 22 Aug 2008 13:32:34 +0000 Subject: Merge chmod changes into trunk git-svn-id: file:///svn/phpbb/trunk@8781 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 1093ee1fa2..62d30f8018 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -84,7 +84,7 @@ class acm @flock($fp, LOCK_UN); fclose($fp); - @chmod($this->cache_dir . 'data_global.' . PHP_EXT, 0666); + phpbb_chmod($this->cache_dir . 'data_global.' . PHP_EXT, CHMOD_WRITE); } else { @@ -182,7 +182,7 @@ class acm @flock($fp, LOCK_UN); fclose($fp); - @chmod($this->cache_dir . "data{$var_name}." . PHP_EXT, 0666); + phpbb_chmod($this->cache_dir . "data{$var_name}." . PHP_EXT, CHMOD_WRITE); } } else @@ -392,7 +392,7 @@ class acm @flock($fp, LOCK_UN); fclose($fp); - @chmod($filename, 0666); + phpbb_chmod($filename, CHMOD_WRITE); $query_result = $query_id; } -- cgit v1.2.1 From d46e8e6f98054fc58a126c865184369198d9a2ed Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sat, 22 Nov 2008 19:38:25 +0000 Subject: merge revisions i missed... hopefully not breaking things - did not check every change. git-svn-id: file:///svn/phpbb/trunk@9077 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 62d30f8018..e0e99d4153 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -84,6 +84,11 @@ class acm @flock($fp, LOCK_UN); fclose($fp); + if (!function_exists('phpbb_chmod')) + { + include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); + } + phpbb_chmod($this->cache_dir . 'data_global.' . PHP_EXT, CHMOD_WRITE); } else @@ -182,6 +187,11 @@ class acm @flock($fp, LOCK_UN); fclose($fp); + if (!function_exists('phpbb_chmod')) + { + include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); + } + phpbb_chmod($this->cache_dir . "data{$var_name}." . PHP_EXT, CHMOD_WRITE); } } @@ -392,6 +402,11 @@ class acm @flock($fp, LOCK_UN); fclose($fp); + if (!function_exists('phpbb_chmod')) + { + include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); + } + phpbb_chmod($filename, CHMOD_WRITE); $query_result = $query_id; -- cgit v1.2.1 From 25725c98503c3a9ba8b5de4b91c2528b6c974a03 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sat, 27 Dec 2008 12:18:04 +0000 Subject: replace constants with class constants. ACL_YES, ACL_NO, ACL_NEVER, USER_NORMAL, USER_IGNORE, USER_INACTIVE, USER_FOUNDER git-svn-id: file:///svn/phpbb/trunk@9233 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index e0e99d4153..23183d1865 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -89,7 +89,7 @@ class acm include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); } - phpbb_chmod($this->cache_dir . 'data_global.' . PHP_EXT, CHMOD_WRITE); + phpbb_chmod($this->cache_dir . 'data_global.' . PHP_EXT, phpbb::CHMOD_WRITE); } else { @@ -192,7 +192,7 @@ class acm include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); } - phpbb_chmod($this->cache_dir . "data{$var_name}." . PHP_EXT, CHMOD_WRITE); + phpbb_chmod($this->cache_dir . "data{$var_name}." . PHP_EXT, phpbb::CHMOD_WRITE); } } else @@ -407,7 +407,7 @@ class acm include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); } - phpbb_chmod($filename, CHMOD_WRITE); + phpbb_chmod($filename, phpbb::CHMOD_WRITE); $query_result = $query_id; } -- cgit v1.2.1 From 889fa871402814874d5d4f01b2f1c829d8206eb9 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sun, 28 Dec 2008 13:27:58 +0000 Subject: implement new phpbb::$acm object, replacing $cache global git-svn-id: file:///svn/phpbb/trunk@9240 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 493 +++++++++++++++------------------------- 1 file changed, 189 insertions(+), 304 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 23183d1865..e05977d158 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -17,67 +17,155 @@ if (!defined('IN_PHPBB')) } /** -* ACM File Based Caching +* Define file-based cache. * @package acm */ -class acm +class phpbb_acm_file extends phpbb_acm_abstract { - private $vars = array(); - private $var_expires = array(); - private $is_modified = false; - - public $sql_rowset = array(); + /** + * @var string The cache directory to use + */ public $cache_dir = ''; /** - * Set cache path + * @var array|bool The cache types this class supports. True indicates support for all types. + */ + public $supported = true; + + /** + * Set cache directory + * + * @param string $cache_prefix The cache prefix the instance is responsible for + * @access public */ - function __construct() + public function __construct($cache_prefix) { $this->cache_dir = PHPBB_ROOT_PATH . 'cache/'; + $this->cache_prefix = $cache_prefix; } /** - * Load global cache + * {@link phpbb_acm_abstract::get() get()} */ - private function load() + public function get($var_name) { - // grab the global cache - if (file_exists($this->cache_dir . 'data_global.' . PHP_EXT)) + if ($var_name[0] === '#') { - @include($this->cache_dir . 'data_global.' . PHP_EXT); - return true; + $var_name = substr($var_name, 1); + return $this->get_global($var_name); } - return false; + if (!$this->exists($var_name)) + { + return false; + } + + @include($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT); + + // If no data there, then the file expired... + if ($expired) + { + // Destroy + $this->destroy($var_name); + return false; + } + + return $data; } /** - * Unload cache object + * {@link phpbb_acm_abstract::put() put()} */ - public function unload() + public function put($var_name, $data, $ttl = 31536000) + { + if ($var_name[0] === '#') + { + $var_name = substr($var_name, 1); + return $this->put_global($var_name, $data, $ttl); + } + + $filename = $this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT; + + if ($fp = @fopen($filename, 'wb')) + { + @flock($fp, LOCK_EX); + fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($data) ? "unserialize(" . var_export(serialize($data), true) . ");" : 'array();')); + @flock($fp, LOCK_UN); + fclose($fp); + + if (!function_exists('phpbb_chmod')) + { + include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); + } + + phpbb_chmod($filename, phpbb::CHMOD_WRITE); + } + + return $data; + } + + + /** + * {@link phpbb_acm_abstract::exists() exists()} + */ + public function exists($var_name) + { + if ($var_name[0] === '#') + { + $var_name = substr($var_name, 1); + return $this->exists_global($var_name); + } + + return file_exists($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT); + } + + /** + * {@link phpbb_acm_abstract::destroy() destroy()} + */ + public function destroy($var_name) + { + if ($var_name[0] === '#') + { + $var_name = substr($var_name, 1); + $this->destroy_global($var_name); + } + + if (!$this->exists($var_name)) + { + return false; + } + + $this->remove_file($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT, true); + } + + /** + * {@link phpbb_acm_abstract::load() load()} + */ + public function load() { - $this->save(); - unset($this->vars); - unset($this->var_expires); - unset($this->sql_rowset); - - $this->vars = array(); - $this->var_expires = array(); - $this->sql_rowset = array(); + // grab the global cache + if (file_exists($this->cache_dir . $this->cache_prefix . '_global.' . PHP_EXT)) + { + @include($this->cache_dir . $this->cache_prefix . '_global.' . PHP_EXT); + return true; + } + + return false; } /** - * Save modified objects + * {@link phpbb_acm_abstract::unload() unload()} */ - private function save() + public function unload() { if (!$this->is_modified) { return; } - if ($fp = @fopen($this->cache_dir . 'data_global.' . PHP_EXT, 'wb')) + $filename = $this->cache_dir . $this->cache_prefix . '_global.' . PHP_EXT; + + if ($fp = @fopen($filename, 'wb')) { @flock($fp, LOCK_EX); fwrite($fp, "vars = unserialize(" . var_export(serialize($this->vars), true) . ");\n\$this->var_expires = unserialize(" . var_export(serialize($this->var_expires), true) . ");"); @@ -89,7 +177,7 @@ class acm include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); } - phpbb_chmod($this->cache_dir . 'data_global.' . PHP_EXT, phpbb::CHMOD_WRITE); + phpbb_chmod($filename, phpbb::CHMOD_WRITE); } else { @@ -99,16 +187,20 @@ class acm trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR); } - trigger_error('Not able to open ' . $this->cache_dir . 'data_global.' . PHP_EXT, E_USER_ERROR); + trigger_error('Not able to open ' . $filename, E_USER_ERROR); } $this->is_modified = false; + + // To reset the global vars + $this->vars = $this->var_expires = array(); } /** - * Tidy cache + * Tidy local cache data. Also see {@link phpbb_acm_abstract::tidy() tidy()} + * @access protected */ - public function tidy() + protected function tidy_local() { $dir = @opendir($this->cache_dir); @@ -119,96 +211,28 @@ class acm while (($entry = readdir($dir)) !== false) { - if (!preg_match('/^(sql_|data_(?!global))/', $entry)) + if (strpos($entry, $this->cache_prefix . '_') !== 0 || strpos($entry, $this->cache_prefix . '_global') === 0) { continue; } $expired = true; @include($this->cache_dir . $entry); + if ($expired) { $this->remove_file($this->cache_dir . $entry); } } closedir($dir); - - if (file_exists($this->cache_dir . 'data_global.' . PHP_EXT)) - { - if (!sizeof($this->vars)) - { - $this->load(); - } - - foreach ($this->var_expires as $var_name => $expires) - { - if (time() > $expires) - { - $this->destroy($var_name); - } - } - } - - set_config('cache_last_gc', time(), true); - } - - /** - * Get saved cache object - */ - public function get($var_name) - { - if ($var_name[0] === '_') - { - if (!$this->_exists($var_name)) - { - return false; - } - - @include($this->cache_dir . "data{$var_name}." . PHP_EXT); - return (isset($data)) ? $data : false; - } - else - { - return ($this->_exists($var_name)) ? $this->vars[$var_name] : false; - } } /** - * Put data into cache + * Purge local cache data. Also see {@link phpbb_acm_abstract::purge() purge()} + * @access protected */ - function put($var_name, $var, $ttl = 31536000) + protected function purge_local() { - if ($var_name[0] === '_') - { - if ($fp = @fopen($this->cache_dir . "data{$var_name}." . PHP_EXT, 'wb')) - { - @flock($fp, LOCK_EX); - fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($var) ? "unserialize(" . var_export(serialize($var), true) . ");" : 'array();')); - @flock($fp, LOCK_UN); - fclose($fp); - - if (!function_exists('phpbb_chmod')) - { - include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); - } - - phpbb_chmod($this->cache_dir . "data{$var_name}." . PHP_EXT, phpbb::CHMOD_WRITE); - } - } - else - { - $this->vars[$var_name] = $var; - $this->var_expires[$var_name] = time() + $ttl; - $this->is_modified = true; - } - } - - /** - * Purge cache data - */ - public function purge() - { - // Purge all phpbb cache files $dir = @opendir($this->cache_dir); if (!$dir) @@ -218,7 +242,7 @@ class acm while (($entry = readdir($dir)) !== false) { - if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) + if (strpos($entry, $this->cache_prefix . '_') !== 0 || strpos($entry, $this->cache_prefix . '_global') === 0) { continue; } @@ -226,240 +250,101 @@ class acm $this->remove_file($this->cache_dir . $entry); } closedir($dir); - - unset($this->vars); - unset($this->var_expires); - unset($this->sql_rowset); - - $this->vars = array(); - $this->var_expires = array(); - $this->sql_rowset = array(); - - $this->is_modified = false; } /** - * Destroy cache data + * Get modified date for cache entry + * + * @param string $var_name The cache variable name + * @access public */ - public function destroy($var_name, $table = '') + public function get_modified_date($var_name) { - if ($var_name === 'sql' && !empty($table)) - { - if (!is_array($table)) - { - $table = array($table); - } - - $dir = @opendir($this->cache_dir); - - if (!$dir) - { - return; - } - - while (($entry = readdir($dir)) !== false) - { - if (strpos($entry, 'sql_') !== 0) - { - continue; - } - - // The following method is more failproof than simply assuming the query is on line 3 (which it should be) - $check_line = @file_get_contents($this->cache_dir . $entry); - - if (empty($check_line)) - { - continue; - } - - // Now get the contents between /* and */ - $check_line = substr($check_line, strpos($check_line, '/* ') + 3, strpos($check_line, ' */') - strpos($check_line, '/* ') - 3); - - $found = false; - foreach ($table as $check_table) - { - // Better catch partial table names than no table names. ;) - if (strpos($check_line, $check_table) !== false) - { - $found = true; - break; - } - } - - if ($found) - { - $this->remove_file($this->cache_dir . $entry); - } - } - closedir($dir); - - return; - } - - if (!$this->_exists($var_name)) - { - return; - } - - if ($var_name[0] === '_') - { - $this->remove_file($this->cache_dir . 'data' . $var_name . '.' . PHP_EXT, true); - } - else if (isset($this->vars[$var_name])) - { - $this->is_modified = true; - unset($this->vars[$var_name]); - unset($this->var_expires[$var_name]); - - // We save here to let the following cache hits succeed - $this->save(); - } + return @filemtime($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT); } /** - * Check if a given cache entry exist + * Removes/unlinks file + * + * @param string $filename The filename to remove + * @param bool $check If true the cache directory is checked for correct directory permissions. + * @access protected */ - private function _exists($var_name) + protected function remove_file($filename, $check = false) { - if ($var_name[0] === '_') + if ($check && !@is_writable($this->cache_dir)) { - return file_exists($this->cache_dir . 'data' . $var_name . '.' . PHP_EXT); + // E_USER_ERROR - not using language entry - intended. + trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); } - else - { - if (!sizeof($this->vars)) - { - $this->load(); - } - - if (!isset($this->var_expires[$var_name])) - { - return false; - } - return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]); - } + return @unlink($filename); } +} +/** +* Special implementation for cache type 'sql' +* @package acm +*/ +class phpbb_acm_file_sql extends phpbb_acm_file +{ /** - * Load cached sql query + * {@link phpbb_acm_abstract::destroy() destroy()} */ - public function sql_load($query) + public function destroy($var_name) { - // Remove extra spaces and tabs - $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); - $query_id = sizeof($this->sql_rowset); - - if (!file_exists($this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT)) + if ($var_name[0] === '#') { - return false; + $var_name = substr($var_name, 1); + $this->destroy_global($var_name); } - @include($this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT); + $table = (!is_array($var_name)) ? array($var_name) : $var_name; + $dir = @opendir($this->cache_dir); - if (!isset($expired)) - { - return false; - } - else if ($expired) + if (!$dir) { - $this->remove_file($this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT, true); - return false; + return; } - - return $query_id; - } - - /** - * Save sql query - */ - public function sql_save($query, &$query_result, $ttl) - { - global $db; - - // Remove extra spaces and tabs - $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); - $filename = $this->cache_dir . 'sql_' . md5($query) . '.' . PHP_EXT; - - if ($fp = @fopen($filename, 'wb')) + while (($entry = readdir($dir)) !== false) { - @flock($fp, LOCK_EX); - - $query_id = sizeof($this->sql_rowset); - $this->sql_rowset[$query_id] = array(); - - while ($row = $db->sql_fetchrow($query_result)) + if (strpos($entry, $this->cache_prefix . '_') !== 0) { - $this->sql_rowset[$query_id][] = $row; + continue; } - $db->sql_freeresult($query_result); - $file = " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n"; - - fwrite($fp, $file . "\$this->sql_rowset[\$query_id] = " . (sizeof($this->sql_rowset[$query_id]) ? "unserialize(" . var_export(serialize($this->sql_rowset[$query_id]), true) . ");" : 'array();')); - @flock($fp, LOCK_UN); - fclose($fp); + // The following method is more failproof than simply assuming the query is on line 3 (which it should be) + @include($this->cache_dir . $entry); - if (!function_exists('phpbb_chmod')) + if (empty($data)) { - include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); + $this->remove_file($this->cache_dir . $entry); + continue; } - phpbb_chmod($filename, phpbb::CHMOD_WRITE); - - $query_result = $query_id; - } - } - - /** - * Fetch row from cache (database) - */ - public function sql_fetchrow($query_id) - { - list(, $row) = each($this->sql_rowset[$query_id]); - - return ($row !== NULL) ? $row : false; - } - - /** - * Fetch a field from the current row of a cached database result (database) - */ - public function sql_fetchfield($query_id, $field) - { - $row = current($this->sql_rowset[$query_id]); - - return ($row !== false && isset($row[$field])) ? $row[$field] : false; - } - - /** - * Free memory used for a cached database result (database) - */ - public function sql_freeresult($query_id) - { - if (!isset($this->sql_rowset[$query_id])) - { - return false; - } + // Get the query + $data = $data['query']; - unset($this->sql_rowset[$query_id]); - - return true; - } + $found = false; + foreach ($table as $check_table) + { + // Better catch partial table names than no table names. ;) + if (strpos($data, $check_table) !== false) + { + $found = true; + break; + } + } - /** - * Removes/unlinks file - */ - private function remove_file($filename, $check = false) - { - if ($check && !@is_writable($this->cache_dir)) - { - // E_USER_ERROR - not using language entry - intended. - trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); + if ($found) + { + $this->remove_file($this->cache_dir . $entry); + } } + closedir($dir); - return @unlink($filename); + return; } } -- cgit v1.2.1 From 950842de5c30908b8cab3227774d6414cb37a333 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Thu, 22 Jan 2009 15:59:00 +0000 Subject: add some properties change phpbb_chmod to phpbb::$system->chmod() also changed chmod behaviour to the most failsafe method. If we are not able to tell the exact outcome, we simply do not mess with it. git-svn-id: file:///svn/phpbb/trunk@9296 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index e05977d158..a6733245bf 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -93,12 +93,7 @@ class phpbb_acm_file extends phpbb_acm_abstract @flock($fp, LOCK_UN); fclose($fp); - if (!function_exists('phpbb_chmod')) - { - include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); - } - - phpbb_chmod($filename, phpbb::CHMOD_WRITE); + phpbb::$system->chmod($filename, phpbb::CHMOD_READ | phpbb::CHMOD_WRITE); } return $data; @@ -172,12 +167,7 @@ class phpbb_acm_file extends phpbb_acm_abstract @flock($fp, LOCK_UN); fclose($fp); - if (!function_exists('phpbb_chmod')) - { - include(PHPBB_ROOT_PATH . 'includes/functions.' . PHP_EXT); - } - - phpbb_chmod($filename, phpbb::CHMOD_WRITE); + phpbb::$system->chmod($filename, phpbb::CHMOD_READ | phpbb::CHMOD_WRITE); } else { -- cgit v1.2.1 From 4cbf6bc703bdadf716197b68a89b3438247ff022 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sun, 22 Mar 2009 16:34:26 +0000 Subject: Merge most changes from 3.0.x branch since the 25th december. (Captcha changes for refreshing captcha image not included) git-svn-id: file:///svn/phpbb/trunk@9404 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index a6733245bf..e073230d49 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -89,7 +89,7 @@ class phpbb_acm_file extends phpbb_acm_abstract if ($fp = @fopen($filename, 'wb')) { @flock($fp, LOCK_EX); - fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($data) ? "unserialize(" . var_export(serialize($data), true) . ");" : 'array();')); + fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($data) ? "unserialize(" . var_export(serialize($data), true) . ");" : 'array();')); @flock($fp, LOCK_UN); fclose($fp); @@ -163,7 +163,7 @@ class phpbb_acm_file extends phpbb_acm_abstract if ($fp = @fopen($filename, 'wb')) { @flock($fp, LOCK_EX); - fwrite($fp, "vars = unserialize(" . var_export(serialize($this->vars), true) . ");\n\$this->var_expires = unserialize(" . var_export(serialize($this->var_expires), true) . ");"); + fwrite($fp, "vars = unserialize(" . var_export(serialize($this->vars), true) . ");\n\$this->var_expires = unserialize(" . var_export(serialize($this->var_expires), true) . ");"); @flock($fp, LOCK_UN); fclose($fp); @@ -174,10 +174,13 @@ class phpbb_acm_file extends phpbb_acm_abstract // Now, this occurred how often? ... phew, just tell the user then... if (!@is_writable($this->cache_dir)) { - trigger_error($this->cache_dir . ' is NOT writable.', E_USER_ERROR); + // We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload()) + die($this->cache_dir . ' is NOT writable.'); + exit; } - trigger_error('Not able to open ' . $filename, E_USER_ERROR); + die('Not able to open ' . $filename); + exit; } $this->is_modified = false; -- cgit v1.2.1 From bf8ac19eaa8d74f9dfd6d597190f5664e7339382 Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sun, 4 Oct 2009 18:13:59 +0000 Subject: Move trunk/phpBB to old_trunk/phpBB git-svn-id: file:///svn/phpbb/trunk@10210 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 344 ---------------------------------------- 1 file changed, 344 deletions(-) delete mode 100644 phpBB/includes/acm/acm_file.php (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php deleted file mode 100644 index e073230d49..0000000000 --- a/phpBB/includes/acm/acm_file.php +++ /dev/null @@ -1,344 +0,0 @@ -cache_dir = PHPBB_ROOT_PATH . 'cache/'; - $this->cache_prefix = $cache_prefix; - } - - /** - * {@link phpbb_acm_abstract::get() get()} - */ - public function get($var_name) - { - if ($var_name[0] === '#') - { - $var_name = substr($var_name, 1); - return $this->get_global($var_name); - } - - if (!$this->exists($var_name)) - { - return false; - } - - @include($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT); - - // If no data there, then the file expired... - if ($expired) - { - // Destroy - $this->destroy($var_name); - return false; - } - - return $data; - } - - /** - * {@link phpbb_acm_abstract::put() put()} - */ - public function put($var_name, $data, $ttl = 31536000) - { - if ($var_name[0] === '#') - { - $var_name = substr($var_name, 1); - return $this->put_global($var_name, $data, $ttl); - } - - $filename = $this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT; - - if ($fp = @fopen($filename, 'wb')) - { - @flock($fp, LOCK_EX); - fwrite($fp, " " . (time() + $ttl) . ") ? true : false;\nif (\$expired) { return; }\n\$data = " . (sizeof($data) ? "unserialize(" . var_export(serialize($data), true) . ");" : 'array();')); - @flock($fp, LOCK_UN); - fclose($fp); - - phpbb::$system->chmod($filename, phpbb::CHMOD_READ | phpbb::CHMOD_WRITE); - } - - return $data; - } - - - /** - * {@link phpbb_acm_abstract::exists() exists()} - */ - public function exists($var_name) - { - if ($var_name[0] === '#') - { - $var_name = substr($var_name, 1); - return $this->exists_global($var_name); - } - - return file_exists($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT); - } - - /** - * {@link phpbb_acm_abstract::destroy() destroy()} - */ - public function destroy($var_name) - { - if ($var_name[0] === '#') - { - $var_name = substr($var_name, 1); - $this->destroy_global($var_name); - } - - if (!$this->exists($var_name)) - { - return false; - } - - $this->remove_file($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT, true); - } - - /** - * {@link phpbb_acm_abstract::load() load()} - */ - public function load() - { - // grab the global cache - if (file_exists($this->cache_dir . $this->cache_prefix . '_global.' . PHP_EXT)) - { - @include($this->cache_dir . $this->cache_prefix . '_global.' . PHP_EXT); - return true; - } - - return false; - } - - /** - * {@link phpbb_acm_abstract::unload() unload()} - */ - public function unload() - { - if (!$this->is_modified) - { - return; - } - - $filename = $this->cache_dir . $this->cache_prefix . '_global.' . PHP_EXT; - - if ($fp = @fopen($filename, 'wb')) - { - @flock($fp, LOCK_EX); - fwrite($fp, "vars = unserialize(" . var_export(serialize($this->vars), true) . ");\n\$this->var_expires = unserialize(" . var_export(serialize($this->var_expires), true) . ");"); - @flock($fp, LOCK_UN); - fclose($fp); - - phpbb::$system->chmod($filename, phpbb::CHMOD_READ | phpbb::CHMOD_WRITE); - } - else - { - // Now, this occurred how often? ... phew, just tell the user then... - if (!@is_writable($this->cache_dir)) - { - // We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload()) - die($this->cache_dir . ' is NOT writable.'); - exit; - } - - die('Not able to open ' . $filename); - exit; - } - - $this->is_modified = false; - - // To reset the global vars - $this->vars = $this->var_expires = array(); - } - - /** - * Tidy local cache data. Also see {@link phpbb_acm_abstract::tidy() tidy()} - * @access protected - */ - protected function tidy_local() - { - $dir = @opendir($this->cache_dir); - - if (!$dir) - { - return; - } - - while (($entry = readdir($dir)) !== false) - { - if (strpos($entry, $this->cache_prefix . '_') !== 0 || strpos($entry, $this->cache_prefix . '_global') === 0) - { - continue; - } - - $expired = true; - @include($this->cache_dir . $entry); - - if ($expired) - { - $this->remove_file($this->cache_dir . $entry); - } - } - closedir($dir); - } - - /** - * Purge local cache data. Also see {@link phpbb_acm_abstract::purge() purge()} - * @access protected - */ - protected function purge_local() - { - $dir = @opendir($this->cache_dir); - - if (!$dir) - { - return; - } - - while (($entry = readdir($dir)) !== false) - { - if (strpos($entry, $this->cache_prefix . '_') !== 0 || strpos($entry, $this->cache_prefix . '_global') === 0) - { - continue; - } - - $this->remove_file($this->cache_dir . $entry); - } - closedir($dir); - } - - /** - * Get modified date for cache entry - * - * @param string $var_name The cache variable name - * @access public - */ - public function get_modified_date($var_name) - { - return @filemtime($this->cache_dir . $this->cache_prefix . '_' . $var_name . '.' . PHP_EXT); - } - - /** - * Removes/unlinks file - * - * @param string $filename The filename to remove - * @param bool $check If true the cache directory is checked for correct directory permissions. - * @access protected - */ - protected function remove_file($filename, $check = false) - { - if ($check && !@is_writable($this->cache_dir)) - { - // E_USER_ERROR - not using language entry - intended. - trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); - } - - return @unlink($filename); - } -} - -/** -* Special implementation for cache type 'sql' -* @package acm -*/ -class phpbb_acm_file_sql extends phpbb_acm_file -{ - /** - * {@link phpbb_acm_abstract::destroy() destroy()} - */ - public function destroy($var_name) - { - if ($var_name[0] === '#') - { - $var_name = substr($var_name, 1); - $this->destroy_global($var_name); - } - - $table = (!is_array($var_name)) ? array($var_name) : $var_name; - $dir = @opendir($this->cache_dir); - - if (!$dir) - { - return; - } - - while (($entry = readdir($dir)) !== false) - { - if (strpos($entry, $this->cache_prefix . '_') !== 0) - { - continue; - } - - // The following method is more failproof than simply assuming the query is on line 3 (which it should be) - @include($this->cache_dir . $entry); - - if (empty($data)) - { - $this->remove_file($this->cache_dir . $entry); - continue; - } - - // Get the query - $data = $data['query']; - - $found = false; - foreach ($table as $check_table) - { - // Better catch partial table names than no table names. ;) - if (strpos($data, $check_table) !== false) - { - $found = true; - break; - } - } - - if ($found) - { - $this->remove_file($this->cache_dir . $entry); - } - } - closedir($dir); - - return; - } -} - -?> \ No newline at end of file -- cgit v1.2.1 From 2e17e448deed073f8614bb555a8ef20c57291c2a Mon Sep 17 00:00:00 2001 From: Meik Sievertsen Date: Sun, 4 Oct 2009 18:14:59 +0000 Subject: Copy 3.0.x branch to trunk git-svn-id: file:///svn/phpbb/trunk@10211 89ea8834-ac86-4346-8a33-228a782c2dd0 --- phpBB/includes/acm/acm_file.php | 720 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 720 insertions(+) create mode 100644 phpBB/includes/acm/acm_file.php (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php new file mode 100644 index 0000000000..234be5c5d1 --- /dev/null +++ b/phpBB/includes/acm/acm_file.php @@ -0,0 +1,720 @@ +cache_dir = $phpbb_root_path . 'cache/'; + } + + /** + * Load global cache + */ + function load() + { + return $this->_read('data_global'); + } + + /** + * Unload cache object + */ + function unload() + { + $this->save(); + unset($this->vars); + unset($this->var_expires); + unset($this->sql_rowset); + unset($this->sql_row_pointer); + + $this->vars = array(); + $this->var_expires = array(); + $this->sql_rowset = array(); + $this->sql_row_pointer = array(); + } + + /** + * Save modified objects + */ + function save() + { + if (!$this->is_modified) + { + return; + } + + global $phpEx; + + if (!$this->_write('data_global')) + { + // Now, this occurred how often? ... phew, just tell the user then... + if (!@is_writable($this->cache_dir)) + { + // We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload()) + die($this->cache_dir . ' is NOT writable.'); + exit; + } + + die('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx); + exit; + } + + $this->is_modified = false; + } + + /** + * Tidy cache + */ + function tidy() + { + global $phpEx; + + $dir = @opendir($this->cache_dir); + + if (!$dir) + { + return; + } + + $time = time(); + + while (($entry = readdir($dir)) !== false) + { + if (!preg_match('/^(sql_|data_(?!global))/', $entry)) + { + continue; + } + + if (!($handle = @fopen($this->cache_dir . $entry, 'rb'))) + { + continue; + } + + // Skip the PHP header + fgets($handle); + + // Skip expiration + $expires = (int) fgets($handle); + + fclose($handle); + + if ($time >= $expires) + { + $this->remove_file($this->cache_dir . $entry); + } + } + closedir($dir); + + if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) + { + if (!sizeof($this->vars)) + { + $this->load(); + } + + foreach ($this->var_expires as $var_name => $expires) + { + if ($time >= $expires) + { + $this->destroy($var_name); + } + } + } + + set_config('cache_last_gc', time(), true); + } + + /** + * Get saved cache object + */ + function get($var_name) + { + if ($var_name[0] == '_') + { + global $phpEx; + + if (!$this->_exists($var_name)) + { + return false; + } + + return $this->_read('data' . $var_name); + } + else + { + return ($this->_exists($var_name)) ? $this->vars[$var_name] : false; + } + } + + /** + * Put data into cache + */ + function put($var_name, $var, $ttl = 31536000) + { + if ($var_name[0] == '_') + { + $this->_write('data' . $var_name, $var, time() + $ttl); + } + else + { + $this->vars[$var_name] = $var; + $this->var_expires[$var_name] = time() + $ttl; + $this->is_modified = true; + } + } + + /** + * Purge cache data + */ + function purge() + { + // Purge all phpbb cache files + $dir = @opendir($this->cache_dir); + + if (!$dir) + { + return; + } + + while (($entry = readdir($dir)) !== false) + { + if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) + { + continue; + } + + $this->remove_file($this->cache_dir . $entry); + } + closedir($dir); + + unset($this->vars); + unset($this->var_expires); + unset($this->sql_rowset); + unset($this->sql_row_pointer); + + $this->vars = array(); + $this->var_expires = array(); + $this->sql_rowset = array(); + $this->sql_row_pointer = array(); + + $this->is_modified = false; + } + + /** + * Destroy cache data + */ + function destroy($var_name, $table = '') + { + global $phpEx; + + if ($var_name == 'sql' && !empty($table)) + { + if (!is_array($table)) + { + $table = array($table); + } + + $dir = @opendir($this->cache_dir); + + if (!$dir) + { + return; + } + + while (($entry = readdir($dir)) !== false) + { + if (strpos($entry, 'sql_') !== 0) + { + continue; + } + + if (!($handle = @fopen($this->cache_dir . $entry, 'rb'))) + { + continue; + } + + // Skip the PHP header + fgets($handle); + + // Skip expiration + fgets($handle); + + // Grab the query, remove the LF + $query = substr(fgets($handle), 0, -1); + + fclose($handle); + + foreach ($table as $check_table) + { + // Better catch partial table names than no table names. ;) + if (strpos($query, $check_table) !== false) + { + $this->remove_file($this->cache_dir . $entry); + break; + } + } + } + closedir($dir); + + return; + } + + if (!$this->_exists($var_name)) + { + return; + } + + if ($var_name[0] == '_') + { + $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true); + } + else if (isset($this->vars[$var_name])) + { + $this->is_modified = true; + unset($this->vars[$var_name]); + unset($this->var_expires[$var_name]); + + // We save here to let the following cache hits succeed + $this->save(); + } + } + + /** + * Check if a given cache entry exist + */ + function _exists($var_name) + { + if ($var_name[0] == '_') + { + global $phpEx; + return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx"); + } + else + { + if (!sizeof($this->vars)) + { + $this->load(); + } + + if (!isset($this->var_expires[$var_name])) + { + return false; + } + + return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]); + } + } + + /** + * Load cached sql query + */ + function sql_load($query) + { + // Remove extra spaces and tabs + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); + + if (($rowset = $this->_read('sql_' . md5($query))) === false) + { + return false; + } + + $query_id = sizeof($this->sql_rowset); + $this->sql_rowset[$query_id] = $rowset; + $this->sql_row_pointer[$query_id] = 0; + + return $query_id; + } + + /** + * Save sql query + */ + function sql_save($query, &$query_result, $ttl) + { + global $db; + + // Remove extra spaces and tabs + $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); + + $query_id = sizeof($this->sql_rowset); + $this->sql_rowset[$query_id] = array(); + $this->sql_row_pointer[$query_id] = 0; + + while ($row = $db->sql_fetchrow($query_result)) + { + $this->sql_rowset[$query_id][] = $row; + } + $db->sql_freeresult($query_result); + + if ($this->_write('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl + time(), $query)) + { + $query_result = $query_id; + } + } + + /** + * Ceck if a given sql query exist in cache + */ + function sql_exists($query_id) + { + return isset($this->sql_rowset[$query_id]); + } + + /** + * Fetch row from cache (database) + */ + function sql_fetchrow($query_id) + { + if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) + { + return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++]; + } + + return false; + } + + /** + * Fetch a field from the current row of a cached database result (database) + */ + function sql_fetchfield($query_id, $field) + { + if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) + { + return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field] : false; + } + + return false; + } + + /** + * Seek a specific row in an a cached database result (database) + */ + function sql_rowseek($rownum, $query_id) + { + if ($rownum >= sizeof($this->sql_rowset[$query_id])) + { + return false; + } + + $this->sql_row_pointer[$query_id] = $rownum; + return true; + } + + /** + * Free memory used for a cached database result (database) + */ + function sql_freeresult($query_id) + { + if (!isset($this->sql_rowset[$query_id])) + { + return false; + } + + unset($this->sql_rowset[$query_id]); + unset($this->sql_row_pointer[$query_id]); + + return true; + } + + /** + * Read cached data from a specified file + * + * @access private + * @param string $filename Filename to write + * @return mixed False if an error was encountered, otherwise the data type of the cached data + */ + function _read($filename) + { + global $phpEx; + + $file = "{$this->cache_dir}$filename.$phpEx"; + + $type = substr($filename, 0, strpos($filename, '_')); + + if (!file_exists($file)) + { + return false; + } + + if (!($handle = @fopen($file, 'rb'))) + { + return false; + } + + // Skip the PHP header + fgets($handle); + + if ($filename == 'data_global') + { + $this->vars = $this->var_expires = array(); + + $time = time(); + + while (($expires = (int) fgets($handle)) && !feof($handle)) + { + // Number of bytes of data + $bytes = substr(fgets($handle), 0, -1); + + if (!is_numeric($bytes) || ($bytes = (int) $bytes) === 0) + { + // We cannot process the file without a valid number of bytes + // so we discard it + fclose($handle); + + $this->vars = $this->var_expires = array(); + $this->is_modified = false; + + $this->remove_file($file); + + return false; + } + + if ($time >= $expires) + { + fseek($handle, $bytes, SEEK_CUR); + + continue; + } + + $var_name = substr(fgets($handle), 0, -1); + + // Read the length of bytes that consists of data. + $data = fread($handle, $bytes - strlen($var_name)); + $data = @unserialize($data); + + // Don't use the data if it was invalid + if ($data !== false) + { + $this->vars[$var_name] = $data; + $this->var_expires[$var_name] = $expires; + } + + // Absorb the LF + fgets($handle); + } + + fclose($handle); + + $this->is_modified = false; + + return true; + } + else + { + $data = false; + $line = 0; + + while (($buffer = fgets($handle)) && !feof($handle)) + { + $buffer = substr($buffer, 0, -1); // Remove the LF + + // $buffer is only used to read integers + // if it is non numeric we have an invalid + // cache file, which we will now remove. + if (!is_numeric($buffer)) + { + break; + } + + if ($line == 0) + { + $expires = (int) $buffer; + + if (time() >= $expires) + { + break; + } + + if ($type == 'sql') + { + // Skip the query + fgets($handle); + } + } + else if ($line == 1) + { + $bytes = (int) $buffer; + + // Never should have 0 bytes + if (!$bytes) + { + break; + } + + // Grab the serialized data + $data = fread($handle, $bytes); + + // Read 1 byte, to trigger EOF + fread($handle, 1); + + if (!feof($handle)) + { + // Somebody tampered with our data + $data = false; + } + break; + } + else + { + // Something went wrong + break; + } + $line++; + } + fclose($handle); + + // unserialize if we got some data + $data = ($data !== false) ? @unserialize($data) : $data; + + if ($data === false) + { + $this->remove_file($file); + return false; + } + + return $data; + } + } + + /** + * Write cache data to a specified file + * + * 'data_global' is a special case and the generated format is different for this file: + * + * + * (expiration) + * (length of var and serialised data) + * (var) + * (serialised data) + * ... (repeat) + * + * + * The other files have a similar format: + * + * + * (expiration) + * (query) [SQL files only] + * (length of serialised data) + * (serialised data) + * + * + * @access private + * @param string $filename Filename to write + * @param mixed $data Data to store + * @param int $expires Timestamp when the data expires + * @param string $query Query when caching SQL queries + * @return bool True if the file was successfully created, otherwise false + */ + function _write($filename, $data = null, $expires = 0, $query = '') + { + global $phpEx; + + $file = "{$this->cache_dir}$filename.$phpEx"; + + if ($handle = @fopen($file, 'wb')) + { + @flock($handle, LOCK_EX); + + // File header + fwrite($handle, '<' . '?php exit; ?' . '>'); + + if ($filename == 'data_global') + { + // Global data is a different format + foreach ($this->vars as $var => $data) + { + if (strpos($var, "\r") !== false || strpos($var, "\n") !== false) + { + // CR/LF would cause fgets() to read the cache file incorrectly + // do not cache test entries, they probably won't be read back + // the cache keys should really be alphanumeric with a few symbols. + continue; + } + $data = serialize($data); + + // Write out the expiration time + fwrite($handle, "\n" . $this->var_expires[$var] . "\n"); + + // Length of the remaining data for this var (ignoring two LF's) + fwrite($handle, strlen($data . $var) . "\n"); + fwrite($handle, $var . "\n"); + fwrite($handle, $data); + } + } + else + { + fwrite($handle, "\n" . $expires . "\n"); + + if (strpos($filename, 'sql_') === 0) + { + fwrite($handle, $query . "\n"); + } + $data = serialize($data); + + fwrite($handle, strlen($data) . "\n"); + fwrite($handle, $data); + } + + @flock($handle, LOCK_UN); + fclose($handle); + + if (!function_exists('phpbb_chmod')) + { + global $phpbb_root_path; + include($phpbb_root_path . 'includes/functions.' . $phpEx); + } + + phpbb_chmod($file, CHMOD_READ | CHMOD_WRITE); + + return true; + } + + return false; + } + + /** + * Removes/unlinks file + */ + function remove_file($filename, $check = false) + { + if ($check && !@is_writable($this->cache_dir)) + { + // E_USER_ERROR - not using language entry - intended. + trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); + } + + return @unlink($filename); + } +} + +?> \ No newline at end of file -- cgit v1.2.1 From af5b9a96409d788733fcb1ff367e0c7fb0583702 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Tue, 9 Nov 2010 08:59:25 +0100 Subject: [ticket/9556] Drop php closing tags, add trailing newline Closing tags converted using Oleg's script. remove-php-end-tags.py -a . Trailing newlines added using the following where $ext is file extension. find . -type f -name "*.$ext" -print | xargs printf "e %s\nw\n" | ed -s; Extensions: php, css, html, js, xml. PHPBB3-9556 --- phpBB/includes/acm/acm_file.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php index 5c1876d006..0e60cd6940 100644 --- a/phpBB/includes/acm/acm_file.php +++ b/phpBB/includes/acm/acm_file.php @@ -728,5 +728,3 @@ class acm return @unlink($filename); } } - -?> \ No newline at end of file -- cgit v1.2.1 From 9329b16ab13f3a4caf107df358c3c58bda2dcd8a Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 3 Nov 2010 18:35:31 +0100 Subject: [task/acm-refactor] Refactor the ACM classes to have a common interface. They are now refered to as cache drivers rather than ACM classes. The additional utility functions from the original cache class have been moved to the cache_service. The class loader is now instantiated without a cache instance and passed one as soon as it is constructed to allow autoloading the cache classes. PHPBB3-9983 --- phpBB/includes/acm/acm_file.php | 730 ---------------------------------------- 1 file changed, 730 deletions(-) delete mode 100644 phpBB/includes/acm/acm_file.php (limited to 'phpBB/includes/acm/acm_file.php') diff --git a/phpBB/includes/acm/acm_file.php b/phpBB/includes/acm/acm_file.php deleted file mode 100644 index 0e60cd6940..0000000000 --- a/phpBB/includes/acm/acm_file.php +++ /dev/null @@ -1,730 +0,0 @@ -cache_dir = $phpbb_root_path . 'cache/'; - } - - /** - * Load global cache - */ - function load() - { - return $this->_read('data_global'); - } - - /** - * Unload cache object - */ - function unload() - { - $this->save(); - unset($this->vars); - unset($this->var_expires); - unset($this->sql_rowset); - unset($this->sql_row_pointer); - - $this->vars = array(); - $this->var_expires = array(); - $this->sql_rowset = array(); - $this->sql_row_pointer = array(); - } - - /** - * Save modified objects - */ - function save() - { - if (!$this->is_modified) - { - return; - } - - global $phpEx; - - if (!$this->_write('data_global')) - { - if (!function_exists('phpbb_is_writable')) - { - global $phpbb_root_path; - include($phpbb_root_path . 'includes/functions.' . $phpEx); - } - - // Now, this occurred how often? ... phew, just tell the user then... - if (!phpbb_is_writable($this->cache_dir)) - { - // We need to use die() here, because else we may encounter an infinite loop (the message handler calls $cache->unload()) - die($this->cache_dir . ' is NOT writable.'); - exit; - } - - die('Not able to open ' . $this->cache_dir . 'data_global.' . $phpEx); - exit; - } - - $this->is_modified = false; - } - - /** - * Tidy cache - */ - function tidy() - { - global $phpEx; - - $dir = @opendir($this->cache_dir); - - if (!$dir) - { - return; - } - - $time = time(); - - while (($entry = readdir($dir)) !== false) - { - if (!preg_match('/^(sql_|data_(?!global))/', $entry)) - { - continue; - } - - if (!($handle = @fopen($this->cache_dir . $entry, 'rb'))) - { - continue; - } - - // Skip the PHP header - fgets($handle); - - // Skip expiration - $expires = (int) fgets($handle); - - fclose($handle); - - if ($time >= $expires) - { - $this->remove_file($this->cache_dir . $entry); - } - } - closedir($dir); - - if (file_exists($this->cache_dir . 'data_global.' . $phpEx)) - { - if (!sizeof($this->vars)) - { - $this->load(); - } - - foreach ($this->var_expires as $var_name => $expires) - { - if ($time >= $expires) - { - $this->destroy($var_name); - } - } - } - - set_config('cache_last_gc', time(), true); - } - - /** - * Get saved cache object - */ - function get($var_name) - { - if ($var_name[0] == '_') - { - global $phpEx; - - if (!$this->_exists($var_name)) - { - return false; - } - - return $this->_read('data' . $var_name); - } - else - { - return ($this->_exists($var_name)) ? $this->vars[$var_name] : false; - } - } - - /** - * Put data into cache - */ - function put($var_name, $var, $ttl = 31536000) - { - if ($var_name[0] == '_') - { - $this->_write('data' . $var_name, $var, time() + $ttl); - } - else - { - $this->vars[$var_name] = $var; - $this->var_expires[$var_name] = time() + $ttl; - $this->is_modified = true; - } - } - - /** - * Purge cache data - */ - function purge() - { - // Purge all phpbb cache files - $dir = @opendir($this->cache_dir); - - if (!$dir) - { - return; - } - - while (($entry = readdir($dir)) !== false) - { - if (strpos($entry, 'sql_') !== 0 && strpos($entry, 'data_') !== 0 && strpos($entry, 'ctpl_') !== 0 && strpos($entry, 'tpl_') !== 0) - { - continue; - } - - $this->remove_file($this->cache_dir . $entry); - } - closedir($dir); - - unset($this->vars); - unset($this->var_expires); - unset($this->sql_rowset); - unset($this->sql_row_pointer); - - $this->vars = array(); - $this->var_expires = array(); - $this->sql_rowset = array(); - $this->sql_row_pointer = array(); - - $this->is_modified = false; - } - - /** - * Destroy cache data - */ - function destroy($var_name, $table = '') - { - global $phpEx; - - if ($var_name == 'sql' && !empty($table)) - { - if (!is_array($table)) - { - $table = array($table); - } - - $dir = @opendir($this->cache_dir); - - if (!$dir) - { - return; - } - - while (($entry = readdir($dir)) !== false) - { - if (strpos($entry, 'sql_') !== 0) - { - continue; - } - - if (!($handle = @fopen($this->cache_dir . $entry, 'rb'))) - { - continue; - } - - // Skip the PHP header - fgets($handle); - - // Skip expiration - fgets($handle); - - // Grab the query, remove the LF - $query = substr(fgets($handle), 0, -1); - - fclose($handle); - - foreach ($table as $check_table) - { - // Better catch partial table names than no table names. ;) - if (strpos($query, $check_table) !== false) - { - $this->remove_file($this->cache_dir . $entry); - break; - } - } - } - closedir($dir); - - return; - } - - if (!$this->_exists($var_name)) - { - return; - } - - if ($var_name[0] == '_') - { - $this->remove_file($this->cache_dir . 'data' . $var_name . ".$phpEx", true); - } - else if (isset($this->vars[$var_name])) - { - $this->is_modified = true; - unset($this->vars[$var_name]); - unset($this->var_expires[$var_name]); - - // We save here to let the following cache hits succeed - $this->save(); - } - } - - /** - * Check if a given cache entry exist - */ - function _exists($var_name) - { - if ($var_name[0] == '_') - { - global $phpEx; - return file_exists($this->cache_dir . 'data' . $var_name . ".$phpEx"); - } - else - { - if (!sizeof($this->vars)) - { - $this->load(); - } - - if (!isset($this->var_expires[$var_name])) - { - return false; - } - - return (time() > $this->var_expires[$var_name]) ? false : isset($this->vars[$var_name]); - } - } - - /** - * Load cached sql query - */ - function sql_load($query) - { - // Remove extra spaces and tabs - $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); - - if (($rowset = $this->_read('sql_' . md5($query))) === false) - { - return false; - } - - $query_id = sizeof($this->sql_rowset); - $this->sql_rowset[$query_id] = $rowset; - $this->sql_row_pointer[$query_id] = 0; - - return $query_id; - } - - /** - * Save sql query - */ - function sql_save($query, &$query_result, $ttl) - { - global $db; - - // Remove extra spaces and tabs - $query = preg_replace('/[\n\r\s\t]+/', ' ', $query); - - $query_id = sizeof($this->sql_rowset); - $this->sql_rowset[$query_id] = array(); - $this->sql_row_pointer[$query_id] = 0; - - while ($row = $db->sql_fetchrow($query_result)) - { - $this->sql_rowset[$query_id][] = $row; - } - $db->sql_freeresult($query_result); - - if ($this->_write('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl + time(), $query)) - { - $query_result = $query_id; - } - } - - /** - * Ceck if a given sql query exist in cache - */ - function sql_exists($query_id) - { - return isset($this->sql_rowset[$query_id]); - } - - /** - * Fetch row from cache (database) - */ - function sql_fetchrow($query_id) - { - if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) - { - return $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++]; - } - - return false; - } - - /** - * Fetch a field from the current row of a cached database result (database) - */ - function sql_fetchfield($query_id, $field) - { - if ($this->sql_row_pointer[$query_id] < sizeof($this->sql_rowset[$query_id])) - { - return (isset($this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]][$field])) ? $this->sql_rowset[$query_id][$this->sql_row_pointer[$query_id]++][$field] : false; - } - - return false; - } - - /** - * Seek a specific row in an a cached database result (database) - */ - function sql_rowseek($rownum, $query_id) - { - if ($rownum >= sizeof($this->sql_rowset[$query_id])) - { - return false; - } - - $this->sql_row_pointer[$query_id] = $rownum; - return true; - } - - /** - * Free memory used for a cached database result (database) - */ - function sql_freeresult($query_id) - { - if (!isset($this->sql_rowset[$query_id])) - { - return false; - } - - unset($this->sql_rowset[$query_id]); - unset($this->sql_row_pointer[$query_id]); - - return true; - } - - /** - * Read cached data from a specified file - * - * @access private - * @param string $filename Filename to write - * @return mixed False if an error was encountered, otherwise the data type of the cached data - */ - function _read($filename) - { - global $phpEx; - - $file = "{$this->cache_dir}$filename.$phpEx"; - - $type = substr($filename, 0, strpos($filename, '_')); - - if (!file_exists($file)) - { - return false; - } - - if (!($handle = @fopen($file, 'rb'))) - { - return false; - } - - // Skip the PHP header - fgets($handle); - - if ($filename == 'data_global') - { - $this->vars = $this->var_expires = array(); - - $time = time(); - - while (($expires = (int) fgets($handle)) && !feof($handle)) - { - // Number of bytes of data - $bytes = substr(fgets($handle), 0, -1); - - if (!is_numeric($bytes) || ($bytes = (int) $bytes) === 0) - { - // We cannot process the file without a valid number of bytes - // so we discard it - fclose($handle); - - $this->vars = $this->var_expires = array(); - $this->is_modified = false; - - $this->remove_file($file); - - return false; - } - - if ($time >= $expires) - { - fseek($handle, $bytes, SEEK_CUR); - - continue; - } - - $var_name = substr(fgets($handle), 0, -1); - - // Read the length of bytes that consists of data. - $data = fread($handle, $bytes - strlen($var_name)); - $data = @unserialize($data); - - // Don't use the data if it was invalid - if ($data !== false) - { - $this->vars[$var_name] = $data; - $this->var_expires[$var_name] = $expires; - } - - // Absorb the LF - fgets($handle); - } - - fclose($handle); - - $this->is_modified = false; - - return true; - } - else - { - $data = false; - $line = 0; - - while (($buffer = fgets($handle)) && !feof($handle)) - { - $buffer = substr($buffer, 0, -1); // Remove the LF - - // $buffer is only used to read integers - // if it is non numeric we have an invalid - // cache file, which we will now remove. - if (!is_numeric($buffer)) - { - break; - } - - if ($line == 0) - { - $expires = (int) $buffer; - - if (time() >= $expires) - { - break; - } - - if ($type == 'sql') - { - // Skip the query - fgets($handle); - } - } - else if ($line == 1) - { - $bytes = (int) $buffer; - - // Never should have 0 bytes - if (!$bytes) - { - break; - } - - // Grab the serialized data - $data = fread($handle, $bytes); - - // Read 1 byte, to trigger EOF - fread($handle, 1); - - if (!feof($handle)) - { - // Somebody tampered with our data - $data = false; - } - break; - } - else - { - // Something went wrong - break; - } - $line++; - } - fclose($handle); - - // unserialize if we got some data - $data = ($data !== false) ? @unserialize($data) : $data; - - if ($data === false) - { - $this->remove_file($file); - return false; - } - - return $data; - } - } - - /** - * Write cache data to a specified file - * - * 'data_global' is a special case and the generated format is different for this file: - * - * - * (expiration) - * (length of var and serialised data) - * (var) - * (serialised data) - * ... (repeat) - * - * - * The other files have a similar format: - * - * - * (expiration) - * (query) [SQL files only] - * (length of serialised data) - * (serialised data) - * - * - * @access private - * @param string $filename Filename to write - * @param mixed $data Data to store - * @param int $expires Timestamp when the data expires - * @param string $query Query when caching SQL queries - * @return bool True if the file was successfully created, otherwise false - */ - function _write($filename, $data = null, $expires = 0, $query = '') - { - global $phpEx; - - $file = "{$this->cache_dir}$filename.$phpEx"; - - if ($handle = @fopen($file, 'wb')) - { - @flock($handle, LOCK_EX); - - // File header - fwrite($handle, '<' . '?php exit; ?' . '>'); - - if ($filename == 'data_global') - { - // Global data is a different format - foreach ($this->vars as $var => $data) - { - if (strpos($var, "\r") !== false || strpos($var, "\n") !== false) - { - // CR/LF would cause fgets() to read the cache file incorrectly - // do not cache test entries, they probably won't be read back - // the cache keys should really be alphanumeric with a few symbols. - continue; - } - $data = serialize($data); - - // Write out the expiration time - fwrite($handle, "\n" . $this->var_expires[$var] . "\n"); - - // Length of the remaining data for this var (ignoring two LF's) - fwrite($handle, strlen($data . $var) . "\n"); - fwrite($handle, $var . "\n"); - fwrite($handle, $data); - } - } - else - { - fwrite($handle, "\n" . $expires . "\n"); - - if (strpos($filename, 'sql_') === 0) - { - fwrite($handle, $query . "\n"); - } - $data = serialize($data); - - fwrite($handle, strlen($data) . "\n"); - fwrite($handle, $data); - } - - @flock($handle, LOCK_UN); - fclose($handle); - - if (!function_exists('phpbb_chmod')) - { - global $phpbb_root_path; - include($phpbb_root_path . 'includes/functions.' . $phpEx); - } - - phpbb_chmod($file, CHMOD_READ | CHMOD_WRITE); - - return true; - } - - return false; - } - - /** - * Removes/unlinks file - */ - function remove_file($filename, $check = false) - { - if (!function_exists('phpbb_is_writable')) - { - global $phpbb_root_path, $phpEx; - include($phpbb_root_path . 'includes/functions.' . $phpEx); - } - - if ($check && !phpbb_is_writable($this->cache_dir)) - { - // E_USER_ERROR - not using language entry - intended. - trigger_error('Unable to remove files within ' . $this->cache_dir . '. Please check directory permissions.', E_USER_ERROR); - } - - return @unlink($filename); - } -} -- cgit v1.2.1