cache.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. if(!defined('IN_DISCUZ')) {
  3. exit('Access Denied');
  4. }
  5. class serialize_cache
  6. {
  7. var $baseDir = PLUGIN_CACHE;
  8. var $pathLevel = 3;
  9. var $nameLen = 2;
  10. function file_cache() {
  11. }
  12. function adp_init($config=array()) {
  13. extract($config, EXTR_SKIP);
  14. if (isset($baseDir)){
  15. $this->baseDir = $baseDir;
  16. }
  17. if (isset($pathLevel)){
  18. $this->pathLevel = $pathLevel * 1 ==0 ? 3 : $pathLevel * 1;
  19. }
  20. if (isset($nameLen)){
  21. $this->nameLen = $nameLen * 1 ==0 ? 2 : $nameLen * 1;
  22. }
  23. }
  24. function get($key, $clearStaticKey=false){
  25. static $data;
  26. // 提供给 SET 进行通知,清除静态缓存数据
  27. if ($clearStaticKey){
  28. unset($data[$key]);
  29. return false;
  30. }
  31. $p = $this->_getSavePath($key);
  32. if (isset($data[$key]) && file_exists($p['p'])){
  33. return $data[$key];
  34. }
  35. if ( !file_exists($p['p']) ) {return false;}
  36. $content = IO::read($p['p']);
  37. $content = str_replace("<?php die('Permission denied');?>\n", "", $content);
  38. $d = unserialize($content);
  39. $d = $d[$key];
  40. if ( empty($d['ttl']) || $d['timeout'] > TIMESTAMP ){
  41. $data[$key] = is_array($d['data']) ? $d['data'] : rawurldecode($d['data']);
  42. return $data[$key];
  43. }
  44. return false;
  45. }
  46. function set($key, $value, $ttl = 0) {
  47. $vData = array($key => array('data' => $value, 'timeout'=> ( TIMESTAMP + $ttl), 'ttl' => $ttl));
  48. $formatData = "<?php die('Permission denied');?>\n" . serialize($vData);
  49. $p = $this->_getSavePath($key);
  50. //清除GET中的静态缓存数据
  51. $this->get($key, true);
  52. return IO::write($p['p'],$formatData);
  53. }
  54. function delete($key) {
  55. $p = $this->_getSavePath($key);
  56. if (file_exists($p['p'])){
  57. return IO::rm($p['p']);
  58. }
  59. return true;
  60. }
  61. function _getSavePath($key) {
  62. $sKey = $this->_getPriviteKey($key);
  63. $sArr = explode("\n",wordwrap(str_repeat($sKey,10), $this->nameLen, "\n", 1));
  64. $pArr = array_slice($sArr, 0,$this->pathLevel);
  65. $d = $this->baseDir.'/'.implode('/',$pArr);
  66. $f = $sKey.".cache.php";
  67. return array('f'=>$f , 'd'=>$d , 'p'=>$d.'/'.$f);
  68. }
  69. function _getPriviteKey($key){
  70. return md5($key);
  71. }
  72. }
  73. class fileIo
  74. {
  75. var $err = "";
  76. function file_io() {
  77. }
  78. function adp_init($config=array()) {
  79. }
  80. function write($file, $data, $append = false){
  81. if (!file_exists($file)){
  82. if (!$this->mkdir(dirname($file))) return false;
  83. }
  84. $len = false;
  85. $mode = $append ? 'ab' : 'wb';
  86. $fp = @fopen($file, $mode);
  87. if (!$fp) {
  88. LOGSTR('io', 'fopen file error,file:' . $file);
  89. exit("Can not open file $file !");
  90. }
  91. flock($fp, LOCK_EX);
  92. $len = @fwrite($fp, $data);
  93. flock($fp, LOCK_UN);
  94. @fclose($fp);
  95. return $len;
  96. }
  97. function read($file) {
  98. if (!file_exists($file)){
  99. return false;
  100. }
  101. if (!is_readable($file)) {
  102. LOGSTR('io', 'file can not be read,file:' . $file);
  103. return false;
  104. }
  105. if (function_exists('file_get_contents')){
  106. return file_get_contents($file);
  107. }else{
  108. return (($contents = file($file))) ? implode('', $contents) : false;
  109. }
  110. }
  111. /// get files and dirs not use recursion
  112. function ls($dir,$r=false,$info=false) {
  113. if (empty($dir)) $dir = '.';
  114. if(!file_exists($dir) || !is_dir($dir)){return false;}
  115. $fs = array();
  116. $ds = array($dir);
  117. while(count($ds)>0){
  118. foreach($ds as $i=>$d){
  119. unset($ds[$i]);
  120. $handle = opendir($d);
  121. while (false !== ($item = readdir($handle))) {
  122. if ($item == '.' || $item == '..') continue;
  123. $fp = ( $d=='.' || $d=='.\\' || $d=='./' ) ? $item : $d.DIRECTORY_SEPARATOR.$item;
  124. $t = is_file($fp) ? 'f' : (is_dir($fp) ? 'd' : 'o');
  125. if (is_dir($fp) && $r) { $ds[]=$fp; }
  126. $fs[] = ($info ? array($t,$fp,$this->info($fp)) : array($t,$fp));
  127. }
  128. }
  129. }
  130. return $fs;
  131. }
  132. function mkdir($path) {
  133. $rst = true;
  134. if (!file_exists($path)){
  135. $this->mkdir(dirname($path));
  136. $rst = @mkdir($path, 0777);
  137. }
  138. return $rst;
  139. }
  140. function rm($path){
  141. $path = rtrim($path,'/\\ ');
  142. if ( !is_dir($path) ){ return @unlink($path); }
  143. if ( !$handle= opendir($path) ){
  144. LOGSTR('io', 'opendir error,dir:' . $path);
  145. return false;
  146. }
  147. while( false !==($file=readdir($handle)) ){
  148. if($file=="." || $file=="..") continue ;
  149. $file=$path .DIRECTORY_SEPARATOR. $file;
  150. if(is_dir($file)){
  151. $this->rm($file);
  152. } else {
  153. if(!@unlink($file)){
  154. LOGSTR('io','delete file error when delete dir,file:'.$file);
  155. return false;
  156. }
  157. }
  158. }
  159. closedir($handle);
  160. if(!rmdir($path)){
  161. LOGSTR('io', 'delete dir error,dir:'. $path);
  162. return false;
  163. }
  164. return true;
  165. }
  166. function info($path=".",$key=false) {
  167. $path = realpath($path);
  168. if (!$path) false;
  169. $result = array(
  170. "name" => substr($path, strrpos($path, DIRECTORY_SEPARATOR)+1),
  171. "location" => $path,
  172. "type" => is_file($path) ? 1 : (is_dir($path) ? 0 : -1),
  173. "size" => filesize($path),
  174. "access" => fileatime($path),
  175. "modify" => filemtime($path),
  176. "change" => filectime($path),
  177. "read" => is_readable($path),
  178. "write" => is_writable($path)
  179. );
  180. clearstatcache();
  181. return $key ? $result[$key] : $result;
  182. }
  183. }
  184. class IO {
  185. function IO (){
  186. }
  187. function getInstance(){
  188. return new fileIo();
  189. }
  190. function &instance(){
  191. static $c;
  192. if(empty($c)) {
  193. $c = new fileIo();
  194. }
  195. return $c;
  196. }
  197. //------------------------------------------------------------------
  198. /**
  199. * IO::ls($path,$r=false,$info=false);
  200. * 获取某个目录的文件列表
  201. * @param $path 要处理的目录
  202. * @param $r 是否递归子目录
  203. * @param $info 是否获取每个文件的文件信息
  204. * @return 文件信息列表
  205. */
  206. function ls($path,$r=false,$info=false){
  207. $c = & IO::instance();
  208. return $c->ls($path,$r,$info);
  209. }
  210. //------------------------------------------------------------------
  211. /**
  212. * IO::write($file,$contents,$append=false);
  213. * 写入一个文件
  214. * @param $file 目标文件路径,如果目录结构不存在则自动创建
  215. * @param $contents 文件内容
  216. * @param $append 是否将内容追加到文件末尾,默认为 false 重写文件
  217. * @return 写入字节数 失败返回 false
  218. */
  219. function write($file,$contents,$append=false) {
  220. $c = & IO::instance();
  221. return $c->write($file,$contents,$append);
  222. }
  223. /**
  224. * IO::read($file);
  225. * @param $file 目标文件路径
  226. * @return 如果文件存在,返回内容 反之返回 false
  227. */
  228. function read($file) {
  229. $c = & IO::instance();
  230. return $c->read($file);
  231. }
  232. /**
  233. * IO::mkdir($path);
  234. * 生成目录结构,创建目录
  235. * @param $path 目录结构
  236. * @return 成功返回 true 失败返回 false
  237. */
  238. function mkdir($path) {
  239. $c = & IO::instance();
  240. return $c->mkdir($path);
  241. }
  242. /**
  243. * IO::rm($path);
  244. * 删除一个路径,如果是目录则删除它的子目录以及文件
  245. * @param $path 要删除的目标路径
  246. * @return 删除成功 返回 true 反之 返回 false
  247. */
  248. function rm($path) {
  249. $c = & IO::instance();
  250. return $c->rm($path);
  251. }
  252. /**
  253. * IO::info($path,$key=false);
  254. * 获取一个文件、目录的信息
  255. * @param $path 目标路径
  256. * @param $key 如果 $key 为空 返回所有文件信息 反之返回 文件信息中的 $key 项
  257. * @return 文件信息
  258. */
  259. function info($path,$key=false) {
  260. $c = & IO::instance();
  261. $info = $c->info($path);
  262. if(is_dir($path)){
  263. $file_arr = $c->ls($path, TRUE, TRUE);
  264. $size = 0;
  265. $file_list = array();
  266. foreach((array)$file_arr as $k => $v){
  267. if($v[2]['type'] != 1) continue;
  268. $size += $v[2]['size'];
  269. $ls[] = $v[2];
  270. }
  271. $info['size'] = $size;
  272. $info['ls'] = $ls;
  273. }
  274. return $info;
  275. }
  276. //------------------------------------------------------------------
  277. }
  278. function LOGSTR($type = '',$msg = ''){
  279. return $msg;
  280. }
  281. if(!function_exists('dir_clear')){
  282. function dir_clear($dir) {
  283. if($directory = @dir($dir)) {
  284. while($entry = $directory->read()) {
  285. if($entry == '.' || $entry == '..') {
  286. continue;
  287. }
  288. $filename = $dir.'/'.$entry;
  289. if(is_file($filename)) {
  290. @unlink($filename);
  291. } else {
  292. dir_clear($filename);
  293. }
  294. }
  295. $directory->close();
  296. @rmdir($dir);
  297. }
  298. }
  299. }
  300. ?>