very minor mod to previous Direct Messaging update commit
[openemr.git] / gacl / Cache_Lite / Hashed_Cache_Lite.php
blob894952f8e0b40a5651c7d94388405d62cc5ba4ff
1 <?php
2 /*
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:
24 * ipso@snappymail.ca
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
39 /**
40 * Make a file name (with path)
42 * @param string $id cache id
43 * @param string $group name of the group
44 * @access private
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;
60 /**
61 * Create full directory structure, Ripped straight from the Smarty Template engine.
62 * Version: 2.3.0
63 * Copyright: 2001,2002 ispi of Lincoln, Inc.
65 * @param string $dir Full directory.
66 * @access private
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);
77 return false;
79 $new_dir .= DIR_SEP;
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);
92 return false;
95 while ($file = readdir($dh)) {
96 if ($file == '.' || $file == '..') {
97 continue;
99 $file = $dir . DIR_SEP . $file;
100 if (is_dir($file)) {
101 $this->_remove_dir_structure($file,true);
102 continue;
104 if (is_file($file)) {
105 if (!@unlink($file)) {
106 closedir($dh);
107 $this->raiseError('Cache_Lite : Unable to remove cache !', -3);
108 return false;
110 continue;
114 closedir($dh);
116 if ($remove_dir) {
117 clearstatcache();
118 if (!@rmdir($dir)) {
119 $this->raiseError('Cache_Lite : Unable to remove cache directory !', -4);
120 return false;
124 return true;
128 * Clean the cache
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
135 * @access public
137 function clean($group = false)
139 if ($group) {
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) {
150 return true;
154 return $this->_remove_dir_structure($motif);
157 if ($this->_memoryCaching) {
158 $this->_memoryCachingArray = array();
159 $this->_memoryCachingCounter = 0;
160 if ($this->_onlyMemoryCaching) {
161 return true;
165 if (!($dh = opendir($this->_cacheDir))) {
166 $this->raiseError('Cache_Lite : Unable to open cache directory !', -4);
167 return false;
170 while ($file = readdir($dh)) {
171 if ($file == '.' || $file == '..') {
172 continue;
174 $file = $this->_cacheDir . $file;
175 if (is_dir($file) && !$this->_remove_dir_structure($file,true)) {
176 return false;
180 return true;
184 // end of script