Take two:
[www-register-wizard.git] / database / DB_cache.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\r
2 /**\r
3  * CodeIgniter\r
4  *\r
5  * An open source application development framework for PHP 4.3.2 or newer\r
6  *\r
7  * @package             CodeIgniter\r
8  * @author              ExpressionEngine Dev Team\r
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.\r
10  * @license             http://codeigniter.com/user_guide/license.html\r
11  * @link                http://codeigniter.com\r
12  * @since               Version 1.0\r
13  * @filesource\r
14  */\r
15 \r
16 // ------------------------------------------------------------------------\r
17 \r
18 /**\r
19  * Database Cache Class\r
20  *\r
21  * @category    Database\r
22  * @author              ExpressionEngine Dev Team\r
23  * @link                http://codeigniter.com/user_guide/database/\r
24  */\r
25 class CI_DB_Cache {\r
26 \r
27         var $CI;\r
28         var $db;        // allows passing of db object so that multiple database connections and returned db objects can be supported\r
29 \r
30         /**\r
31          * Constructor\r
32          *\r
33          * Grabs the CI super object instance so we can access it.\r
34          *\r
35          */     \r
36         function CI_DB_Cache(&$db)\r
37         {\r
38                 // Assign the main CI object to $this->CI\r
39                 // and load the file helper since we use it a lot\r
40                 $this->CI =& get_instance();\r
41                 $this->db =& $db;\r
42                 $this->CI->load->helper('file');        \r
43         }\r
44 \r
45         // --------------------------------------------------------------------\r
46 \r
47         /**\r
48          * Set Cache Directory Path\r
49          *\r
50          * @access      public\r
51          * @param       string  the path to the cache directory\r
52          * @return      bool\r
53          */             \r
54         function check_path($path = '')\r
55         {\r
56                 if ($path == '')\r
57                 {\r
58                         if ($this->db->cachedir == '')\r
59                         {\r
60                                 return $this->db->cache_off();\r
61                         }\r
62                 \r
63                         $path = $this->db->cachedir;\r
64                 }\r
65         \r
66                 // Add a trailing slash to the path if needed\r
67                 $path = preg_replace("/(.+?)\/*$/", "\\1/",  $path);\r
68 \r
69                 if ( ! is_dir($path) OR ! is_really_writable($path))\r
70                 {\r
71                         // If the path is wrong we'll turn off caching\r
72                         return $this->db->cache_off();\r
73                 }\r
74                 \r
75                 $this->db->cachedir = $path;\r
76                 return TRUE;\r
77         }\r
78         \r
79         // --------------------------------------------------------------------\r
80 \r
81         /**\r
82          * Retrieve a cached query\r
83          *\r
84          * The URI being requested will become the name of the cache sub-folder.\r
85          * An MD5 hash of the SQL statement will become the cache file name\r
86          *\r
87          * @access      public\r
88          * @return      string\r
89          */\r
90         function read($sql)\r
91         {\r
92                 if ( ! $this->check_path())\r
93                 {\r
94                         return $this->db->cache_off();\r
95                 }\r
96 \r
97                 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);\r
98                 \r
99                 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);\r
100         \r
101                 $filepath = $this->db->cachedir.$segment_one.'+'.$segment_two.'/'.md5($sql);            \r
102                 \r
103                 if (FALSE === ($cachedata = read_file($filepath)))\r
104                 {       \r
105                         return FALSE;\r
106                 }\r
107                 \r
108                 return unserialize($cachedata);                 \r
109         }       \r
110 \r
111         // --------------------------------------------------------------------\r
112 \r
113         /**\r
114          * Write a query to a cache file\r
115          *\r
116          * @access      public\r
117          * @return      bool\r
118          */\r
119         function write($sql, $object)\r
120         {\r
121                 if ( ! $this->check_path())\r
122                 {\r
123                         return $this->db->cache_off();\r
124                 }\r
125 \r
126                 $segment_one = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);\r
127                 \r
128                 $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);\r
129         \r
130                 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';\r
131                 \r
132                 $filename = md5($sql);\r
133         \r
134                 if ( ! @is_dir($dir_path))\r
135                 {\r
136                         if ( ! @mkdir($dir_path, DIR_WRITE_MODE))\r
137                         {\r
138                                 return FALSE;\r
139                         }\r
140                         \r
141                         @chmod($dir_path, DIR_WRITE_MODE);                      \r
142                 }\r
143                 \r
144                 if (write_file($dir_path.$filename, serialize($object)) === FALSE)\r
145                 {\r
146                         return FALSE;\r
147                 }\r
148                 \r
149                 @chmod($dir_path.$filename, DIR_WRITE_MODE);\r
150                 return TRUE;\r
151         }\r
152 \r
153         // --------------------------------------------------------------------\r
154 \r
155         /**\r
156          * Delete cache files within a particular directory\r
157          *\r
158          * @access      public\r
159          * @return      bool\r
160          */\r
161         function delete($segment_one = '', $segment_two = '')\r
162         {       \r
163                 if ($segment_one == '')\r
164                 {\r
165                         $segment_one  = ($this->CI->uri->segment(1) == FALSE) ? 'default' : $this->CI->uri->segment(1);\r
166                 }\r
167                 \r
168                 if ($segment_two == '')\r
169                 {\r
170                         $segment_two = ($this->CI->uri->segment(2) == FALSE) ? 'index' : $this->CI->uri->segment(2);\r
171                 }\r
172                 \r
173                 $dir_path = $this->db->cachedir.$segment_one.'+'.$segment_two.'/';\r
174                 \r
175                 delete_files($dir_path, TRUE);\r
176         }\r
177 \r
178         // --------------------------------------------------------------------\r
179 \r
180         /**\r
181          * Delete all existing cache files\r
182          *\r
183          * @access      public\r
184          * @return      bool\r
185          */\r
186         function delete_all()\r
187         {\r
188                 delete_files($this->db->cachedir, TRUE);\r
189         }\r
190 \r
191 }\r
192 \r
193 \r
194 /* End of file DB_cache.php */\r
195 /* Location: ./system/database/DB_cache.php */