PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/source/function/function_cache.php

https://github.com/kuaileshike/upload
PHP | 151 lines | 135 code | 10 blank | 6 comment | 18 complexity | ca69aa199a4958aafb8c6ecea42ad6f0 MD5 | raw file
  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 27617 2012-02-07 08:24:14Z 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. } else {
  25. foreach($updatelist as $entry) {
  26. @include_once libfile('cache/'.$entry, 'function');
  27. call_user_func('build_cache_'.$entry);
  28. }
  29. }
  30. }
  31. function writetocache($script, $cachedata, $prefix = 'cache_') {
  32. global $_G;
  33. $dir = DISCUZ_ROOT.'./data/sysdata/';
  34. if(!is_dir($dir)) {
  35. dmkdir($dir, 0777);
  36. }
  37. if($fp = @fopen("$dir$prefix$script.php", 'wb')) {
  38. 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?>");
  39. fclose($fp);
  40. } else {
  41. exit('Can not write to cache files, please check directory ./data/ and ./data/sysdata/ .');
  42. }
  43. }
  44. function getcachevars($data, $type = 'VAR') {
  45. $evaluate = '';
  46. foreach($data as $key => $val) {
  47. if(!preg_match("/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/", $key)) {
  48. continue;
  49. }
  50. if(is_array($val)) {
  51. $evaluate .= "\$$key = ".arrayeval($val).";\n";
  52. } else {
  53. $val = addcslashes($val, '\'\\');
  54. $evaluate .= $type == 'VAR' ? "\$$key = '$val';\n" : "define('".strtoupper($key)."', '$val');\n";
  55. }
  56. }
  57. return $evaluate;
  58. }
  59. function smthumb($size, $smthumb = 50) {
  60. if($size[0] <= $smthumb && $size[1] <= $smthumb) {
  61. return array('w' => $size[0], 'h' => $size[1]);
  62. }
  63. $sm = array();
  64. $x_ratio = $smthumb / $size[0];
  65. $y_ratio = $smthumb / $size[1];
  66. if(($x_ratio * $size[1]) < $smthumb) {
  67. $sm['h'] = ceil($x_ratio * $size[1]);
  68. $sm['w'] = $smthumb;
  69. } else {
  70. $sm['w'] = ceil($y_ratio * $size[0]);
  71. $sm['h'] = $smthumb;
  72. }
  73. return $sm;
  74. }
  75. function arrayeval($array, $level = 0) {
  76. if(!is_array($array)) {
  77. return "'".$array."'";
  78. }
  79. if(is_array($array) && function_exists('var_export')) {
  80. return var_export($array, true);
  81. }
  82. $space = '';
  83. for($i = 0; $i <= $level; $i++) {
  84. $space .= "\t";
  85. }
  86. $evaluate = "Array\n$space(\n";
  87. $comma = $space;
  88. if(is_array($array)) {
  89. foreach($array as $key => $val) {
  90. $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
  91. $val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
  92. if(is_array($val)) {
  93. $evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
  94. } else {
  95. $evaluate .= "$comma$key => $val";
  96. }
  97. $comma = ",\n$space";
  98. }
  99. }
  100. $evaluate .= "\n$space)";
  101. return $evaluate;
  102. }
  103. function pluginsettingvalue($type) {
  104. $pluginsetting = $pluginvalue = array();
  105. @include DISCUZ_ROOT.'./data/sysdata/cache_pluginsetting.php';
  106. $pluginsetting = isset($pluginsetting[$type]) ? $pluginsetting[$type] : array();
  107. $varids = $pluginids = array();
  108. foreach($pluginsetting as $pluginid => $v) {
  109. foreach($v['setting'] as $varid => $var) {
  110. $varids[] = $varid;
  111. $pluginids[$varid] = $pluginid;
  112. }
  113. }
  114. if($varids) {
  115. foreach(C::t('common_pluginvar')->fetch_all($varids) as $plugin) {
  116. $values = (array)dunserialize($plugin['value']);
  117. foreach($values as $id => $value) {
  118. $pluginvalue[$id][$pluginids[$plugin['pluginvarid']]][$plugin['variable']] = $value;
  119. }
  120. }
  121. }
  122. return $pluginvalue;
  123. }
  124. function cleartemplatecache() {
  125. $tpl = dir(DISCUZ_ROOT.'./data/template');
  126. while($entry = $tpl->read()) {
  127. if(preg_match("/\.tpl\.php$/", $entry)) {
  128. @unlink(DISCUZ_ROOT.'./data/template/'.$entry);
  129. }
  130. }
  131. $tpl->close();
  132. }
  133. ?>