summaryrefslogtreecommitdiffstats
path: root/system/database/DB_cache.php
diff options
context:
space:
mode:
Diffstat (limited to 'system/database/DB_cache.php')
-rw-r--r--system/database/DB_cache.php165
1 files changed, 96 insertions, 69 deletions
diff --git a/system/database/DB_cache.php b/system/database/DB_cache.php
index e6945950b..d05ebb219 100644
--- a/system/database/DB_cache.php
+++ b/system/database/DB_cache.php
@@ -1,45 +1,85 @@
-<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+<?php
/**
* CodeIgniter
*
- * An open source application development framework for PHP 5.1.6 or newer
+ * An open source application development framework for PHP
*
- * @package CodeIgniter
- * @author ExpressionEngine Dev Team
- * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
- * @license http://codeigniter.com/user_guide/license.html
- * @link http://codeigniter.com
- * @since Version 1.0
+ * This content is released under the MIT License (MIT)
+ *
+ * Copyright (c) 2019 - 2022, CodeIgniter Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * @package CodeIgniter
+ * @author EllisLab Dev Team
+ * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
+ * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
+ * @copyright Copyright (c) 2019 - 2022, CodeIgniter Foundation (https://codeigniter.com/)
+ * @license https://opensource.org/licenses/MIT MIT License
+ * @link https://codeigniter.com
+ * @since Version 1.0.0
* @filesource
*/
-
-// ------------------------------------------------------------------------
+defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Database Cache Class
*
* @category Database
- * @author ExpressionEngine Dev Team
- * @link http://codeigniter.com/user_guide/database/
+ * @author EllisLab Dev Team
+ * @link https://codeigniter.com/userguide3/database/
*/
class CI_DB_Cache {
- var $CI;
- var $db; // allows passing of db object so that multiple database connections and returned db objects can be supported
+ /**
+ * CI Singleton
+ *
+ * @var object
+ */
+ public $CI;
/**
- * Constructor
+ * Database object
*
- * Grabs the CI super object instance so we can access it.
+ * Allows passing of DB object so that multiple database connections
+ * and returned DB objects can be supported.
*
+ * @var object
*/
- function __construct(&$db)
+ public $db;
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Constructor
+ *
+ * @param object &$db
+ * @return void
+ */
+ public function __construct(&$db)
{
- // Assign the main CI object to $this->CI
- // and load the file helper since we use it a lot
+ // Assign the main CI object to $this->CI and load the file helper since we use it a lot
$this->CI =& get_instance();
$this->db =& $db;
$this->CI->load->helper('file');
+
+ $this->check_path();
}
// --------------------------------------------------------------------
@@ -47,15 +87,14 @@ class CI_DB_Cache {
/**
* Set Cache Directory Path
*
- * @access public
- * @param string the path to the cache directory
+ * @param string $path Path to the cache directory
* @return bool
*/
- function check_path($path = '')
+ public function check_path($path = '')
{
- if ($path == '')
+ if ($path === '')
{
- if ($this->db->cachedir == '')
+ if ($this->db->cachedir === '')
{
return $this->db->cache_off();
}
@@ -64,14 +103,26 @@ class CI_DB_Cache {
}
// Add a trailing slash to the path if needed
- $path = preg_replace("/(.+?)\/*$/", "\\1/", $path);
+ $path = realpath($path)
+ ? rtrim(realpath($path), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR
+ : rtrim($path, '/').'/';
- if ( ! is_dir($path) OR ! is_really_writable($path))
+ if ( ! is_dir($path))
{
+ log_message('debug', 'DB cache path error: '.$path);
+
// If the path is wrong we'll turn off caching
return $this->db->cache_off();
}
+ if ( ! is_really_writable($path))
+ {
+ log_message('debug', 'DB cache dir not writable: '.$path);
+
+ // If the path is not really writable we'll turn off caching
+ return $this->db->cache_off();
+ }
+
$this->db->cachedir = $path;
return TRUE;
}
@@ -82,25 +133,18 @@ class CI_DB_Cache {
* Retrieve a cached query
*
* The URI being requested will become the name of the cache sub-folder.
- * An MD5 hash of the SQL statement will become the cache file name
+ * An MD5 hash of the SQL statement will become the cache file name.
*
- * @access public
+ * @param string $sql
* @return string
*/
- function read($sql)
+ public function read($sql)
{
- if ( ! $this->check_path())
- {
- return $this->db->cache_off();
- }
-
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
-
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
-
$filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);
- if (FALSE === ($cachedata = read_file($filepath)))
+ if ( ! is_file($filepath) OR FALSE === ($cachedata = file_get_contents($filepath)))
{
return FALSE;
}
@@ -113,32 +157,20 @@ class CI_DB_Cache {
/**
* Write a query to a cache file
*
- * @access public
+ * @param string $sql
+ * @param object $object
* @return bool
*/
- function write($sql, $object)
+ public function write($sql, $object)
{
- if ( ! $this->check_path())
- {
- return $this->db->cache_off();
- }
-
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
-
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
-
$dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
-
$filename = md5($sql);
- if ( ! @is_dir($dir_path))
+ if ( ! is_dir($dir_path) && ! @mkdir($dir_path, 0750))
{
- if ( ! @mkdir($dir_path, DIR_WRITE_MODE))
- {
- return FALSE;
- }
-
- @chmod($dir_path, DIR_WRITE_MODE);
+ return FALSE;
}
if (write_file($dir_path.$filename, serialize($object)) === FALSE)
@@ -146,7 +178,7 @@ class CI_DB_Cache {
return FALSE;
}
- @chmod($dir_path.$filename, FILE_WRITE_MODE);
+ chmod($dir_path.$filename, 0640);
return TRUE;
}
@@ -155,23 +187,23 @@ class CI_DB_Cache {
/**
* Delete cache files within a particular directory
*
- * @access public
- * @return bool
+ * @param string $segment_one
+ * @param string $segment_two
+ * @return void
*/
- function delete($segment_one = '', $segment_two = '')
+ public function delete($segment_one = '', $segment_two = '')
{
- if ($segment_one == '')
+ if ($segment_one === '')
{
$segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);
}
- if ($segment_two == '')
+ if ($segment_two === '')
{
$segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);
}
$dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';
-
delete_files($dir_path, TRUE);
}
@@ -180,16 +212,11 @@ class CI_DB_Cache {
/**
* Delete all existing cache files
*
- * @access public
- * @return bool
+ * @return void
*/
- function delete_all()
+ public function delete_all()
{
- delete_files($this->db->cachedir, TRUE);
+ delete_files($this->db->cachedir, TRUE, TRUE);
}
}
-
-
-/* End of file DB_cache.php */
-/* Location: ./system/database/DB_cache.php */ \ No newline at end of file