PageRenderTime 82ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/source/function/function_cache.php

https://github.com/jinbo51/DiscuzX
PHP | 170 lines | 154 code | 10 blank | 6 comment | 27 complexity | 989b7175e103f31bfd83c2489a710d40 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: function_cache.php 31691 2012-09-21 05:35:18Z monkey $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. function updatecache($cachename = '') {
  12. $updatelist = empty($cachename) ? array() : (is_array($cachename) ? $cachename : array($cachename));
  13. if(!$updatelist) {
  14. @include_once libfile('cache/setting', 'function');
  15. build_cache_setting();
  16. $cachedir = DISCUZ_ROOT.'./source/function/cache';
  17. $cachedirhandle = dir($cachedir);
  18. while($entry = $cachedirhandle->read()) {
  19. if(!in_array($entry, array('.', '..')) && preg_match("/^cache\_([\_\w]+)\.php$/", $entry, $entryr) && $entryr[1] != 'setting' && substr($entry, -4) == '.php' && is_file($cachedir.'/'.$entry)) {
  20. @include_once libfile('cache/'.$entryr[1], 'function');
  21. call_user_func('build_cache_'.$entryr[1]);
  22. }
  23. }
  24. foreach(C::t('common_plugin')->fetch_all_data(1) as $plugin) {
  25. $dir = substr($plugin['directory'], 0, -1);
  26. $cachedir = DISCUZ_ROOT.'./source/plugin/'.$dir.'/cache';
  27. if(file_exists($cachedir)) {
  28. $cachedirhandle = dir($cachedir);
  29. while($entry = $cachedirhandle->read()) {
  30. if(!in_array($entry, array('.', '..')) && preg_match("/^cache\_([\_\w]+)\.php$/", $entry, $entryr) && substr($entry, -4) == '.php' && is_file($cachedir.'/'.$entry)) {
  31. @include_once libfile('cache/'.$entryr[1], 'plugin/'.$dir);
  32. call_user_func('build_cache_plugin_'.$entryr[1]);
  33. }
  34. }
  35. }
  36. }
  37. } else {
  38. foreach($updatelist as $entry) {
  39. $entrys = explode(':', $entry);
  40. if(count($entrys) == 1) {
  41. @include_once libfile('cache/'.$entry, 'function');
  42. call_user_func('build_cache_'.$entry);
  43. } else {
  44. @include_once libfile('cache/'.$entrys[1], 'plugin/'.$entrys[0]);
  45. call_user_func('build_cache_'.$entrys[1]);
  46. }
  47. }
  48. }
  49. }
  50. function writetocache($script, $cachedata, $prefix = 'cache_') {
  51. global $_G;
  52. $dir = DISCUZ_ROOT.'./data/sysdata/';
  53. if(!is_dir($dir)) {
  54. dmkdir($dir, 0777);
  55. }
  56. if($fp = @fopen("$dir$prefix$script.php", 'wb')) {
  57. fwrite($fp, "<?php\n//Discuz! cache file, DO NOT modify me!\n//Identify: ".md5($prefix.$script.'.php'.$cachedata.$_G['config']['security']['authkey'])."\n\n$cachedata?>");
  58. fclose($fp);
  59. } else {
  60. exit('Can not write to cache files, please check directory ./data/ and ./data/sysdata/ .');
  61. }
  62. }
  63. function getcachevars($data, $type = 'VAR') {
  64. $evaluate = '';
  65. foreach($data as $key => $val) {
  66. if(!preg_match("/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/", $key)) {
  67. continue;
  68. }
  69. if(is_array($val)) {
  70. $evaluate .= "\$$key = ".arrayeval($val).";\n";
  71. } else {
  72. $val = addcslashes($val, '\'\\');
  73. $evaluate .= $type == 'VAR' ? "\$$key = '$val';\n" : "define('".strtoupper($key)."', '$val');\n";
  74. }
  75. }
  76. return $evaluate;
  77. }
  78. function smthumb($size, $smthumb = 50) {
  79. if($size[0] <= $smthumb && $size[1] <= $smthumb) {
  80. return array('w' => $size[0], 'h' => $size[1]);
  81. }
  82. $sm = array();
  83. $x_ratio = $smthumb / $size[0];
  84. $y_ratio = $smthumb / $size[1];
  85. if(($x_ratio * $size[1]) < $smthumb) {
  86. $sm['h'] = ceil($x_ratio * $size[1]);
  87. $sm['w'] = $smthumb;
  88. } else {
  89. $sm['w'] = ceil($y_ratio * $size[0]);
  90. $sm['h'] = $smthumb;
  91. }
  92. return $sm;
  93. }
  94. function arrayeval($array, $level = 0) {
  95. if(!is_array($array)) {
  96. return "'".$array."'";
  97. }
  98. if(is_array($array) && function_exists('var_export')) {
  99. return var_export($array, true);
  100. }
  101. $space = '';
  102. for($i = 0; $i <= $level; $i++) {
  103. $space .= "\t";
  104. }
  105. $evaluate = "Array\n$space(\n";
  106. $comma = $space;
  107. if(is_array($array)) {
  108. foreach($array as $key => $val) {
  109. $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
  110. $val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
  111. if(is_array($val)) {
  112. $evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
  113. } else {
  114. $evaluate .= "$comma$key => $val";
  115. }
  116. $comma = ",\n$space";
  117. }
  118. }
  119. $evaluate .= "\n$space)";
  120. return $evaluate;
  121. }
  122. function pluginsettingvalue($type) {
  123. $pluginsetting = $pluginvalue = array();
  124. @include DISCUZ_ROOT.'./data/sysdata/cache_pluginsetting.php';
  125. $pluginsetting = isset($pluginsetting[$type]) ? $pluginsetting[$type] : array();
  126. $varids = $pluginids = array();
  127. foreach($pluginsetting as $pluginid => $v) {
  128. foreach($v['setting'] as $varid => $var) {
  129. $varids[] = $varid;
  130. $pluginids[$varid] = $pluginid;
  131. }
  132. }
  133. if($varids) {
  134. foreach(C::t('common_pluginvar')->fetch_all($varids) as $plugin) {
  135. $values = (array)dunserialize($plugin['value']);
  136. foreach($values as $id => $value) {
  137. $pluginvalue[$id][$pluginids[$plugin['pluginvarid']]][$plugin['variable']] = $value;
  138. }
  139. }
  140. }
  141. return $pluginvalue;
  142. }
  143. function cleartemplatecache() {
  144. $tpl = dir(DISCUZ_ROOT.'./data/template');
  145. while($entry = $tpl->read()) {
  146. if(preg_match("/\.tpl\.php$/", $entry)) {
  147. @unlink(DISCUZ_ROOT.'./data/template/'.$entry);
  148. }
  149. }
  150. $tpl->close();
  151. }
  152. ?>