3 * phpGACL - Generic Access Control List - Hashed Directory Caching.
4 * Copyright (C) 2002 Mike Benoit
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * For questions, help, comments, discussion, etc., please join the
21 * phpGACL mailing list. http://sourceforge.net/mail/?group_id=57103
23 * You may contact the author of phpGACL by e-mail at:
26 * The latest version of phpGACL can be obtained from:
27 * http://phpgacl.sourceforge.net/
31 if ( !class_exists('Cache_Lite') ) {
32 require_once(dirname(__FILE__
) .'/Lite.php');
35 define('DIR_SEP',DIRECTORY_SEPARATOR
);
37 class Hashed_Cache_Lite
extends Cache_Lite
40 * Make a file name (with path)
42 * @param string $id cache id
43 * @param string $group name of the group
46 function _setFileName($id, $group)
48 // CRC32 with SUBSTR is still faster then MD5.
49 $encoded_id = substr(crc32($id),1);
50 // $encoded_id = md5($id);
52 // Generate just the directory, so it can be created.
53 // Groups will have their own top level directory, for quick/easy purging of an entire group.
54 $dir = $this->_cacheDir
.$group.'/'.substr($encoded_id,0,3);
55 $this->_create_dir_structure($dir);
57 $this->_file
= $dir.'/'.$encoded_id;
61 * Create full directory structure, Ripped straight from the Smarty Template engine.
63 * Copyright: 2001,2002 ispi of Lincoln, Inc.
65 * @param string $dir Full directory.
68 function _create_dir_structure($dir)
70 if (!@file_exists
($dir)) {
71 $dir_parts = preg_split('![\/]+!', $dir, -1, PREG_SPLIT_NO_EMPTY
);
72 $new_dir = ($dir{0} == DIR_SEP
) ? DIR_SEP
: '';
73 foreach ($dir_parts as $dir_part) {
74 $new_dir .= $dir_part;
75 if (!file_exists($new_dir) && !mkdir($new_dir, 0771)) {
76 Cache_Lite
::raiseError('Cache_Lite : problem creating directory \"$dir\" !', -3);
84 function _remove_dir_structure($dir,$remove_dir = false)
86 if (in_array(substr($dir,-1),array(DIR_SEP
,'/','\\'))) {
87 $dir = substr($dir,0,-1);
90 if (!($dh = opendir($dir))) {
91 $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
95 while ($file = readdir($dh)) {
96 if ($file == '.' ||
$file == '..') {
99 $file = $dir . DIR_SEP
. $file;
101 $this->_remove_dir_structure($file,true);
104 if (is_file($file)) {
105 if (!@unlink
($file)) {
107 $this->raiseError('Cache_Lite : Unable to remove cache !', -3);
119 $this->raiseError('Cache_Lite : Unable to remove cache directory !', -4);
130 * if no group is specified all cache files will be destroyed
131 * else only cache files of the specified group will be destroyed
133 * @param string $group name of the cache group
134 * @return boolean true if no problem
137 function clean($group = false)
140 $motif = $this->_cacheDir
.$group.'/';
142 if ($this->_memoryCaching
) {
143 foreach ($this->_memoryCachingArray
as $key => $value) {
144 if (strpos($key, $motif, 0)) {
145 unset($this->_memoryCachingArray
[$key]);
148 $this->_memoryCachingCounter
= count($this->_memoryCachingArray
);
149 if ($this->_onlyMemoryCaching
) {
154 return $this->_remove_dir_structure($motif);
157 if ($this->_memoryCaching
) {
158 $this->_memoryCachingArray
= array();
159 $this->_memoryCachingCounter
= 0;
160 if ($this->_onlyMemoryCaching
) {
165 if (!($dh = opendir($this->_cacheDir
))) {
166 $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
170 while ($file = readdir($dh)) {
171 if ($file == '.' ||
$file == '..') {
174 $file = $this->_cacheDir
. $file;
175 if (is_dir($file) && !$this->_remove_dir_structure($file,true)) {