function_core.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Discuz! Team
  4. * This is NOT a freeware, use is subject to license terms
  5. * https://license.discuz.vip
  6. */
  7. if(!defined('IN_DISCUZ') && !defined('IN_API')) {
  8. exit('Access Denied');
  9. }
  10. function DISCUZ_APP($app = '') {
  11. return DISCUZ_ROOT.'./source/app'.($app !== '' ? '/'.$app : '');
  12. }
  13. function appfile($filename, $app = '') {
  14. $app = $app ?: DISCUZ_APP;
  15. $p = strpos($filename, '/');
  16. if($p === false) {
  17. return '';
  18. }
  19. $folder = substr($filename, 0, $p);
  20. $apppath = '/source/app/'.$app;
  21. $path = $apppath.'/'.$filename;
  22. return preg_match('/^[\w\d\/_]+$/i', $path) ? realpath(DISCUZ_ROOT.$path.'.php') : false;
  23. }
  24. function setglobal($key, $value, $group = null) {
  25. global $_G;
  26. $key = explode('/', $group === null ? $key : $group.'/'.$key);
  27. $p = &$_G;
  28. foreach($key as $k) {
  29. if(!isset($p[$k]) || !is_array($p[$k])) {
  30. $p[$k] = [];
  31. }
  32. $p = &$p[$k];
  33. }
  34. $p = $value;
  35. return true;
  36. }
  37. function getglobal($key, $group = null) {
  38. global $_G;
  39. $key = explode('/', $group === null ? $key : $group.'/'.$key);
  40. $v = &$_G;
  41. foreach($key as $k) {
  42. if(!isset($v[$k])) {
  43. return null;
  44. }
  45. $v = &$v[$k];
  46. }
  47. return $v;
  48. }
  49. function getgpc($k, $type = 'GP') {
  50. $type = strtoupper($type);
  51. switch($type) {
  52. case 'G':
  53. $var = &$_GET;
  54. break;
  55. case 'P':
  56. $var = &$_POST;
  57. break;
  58. case 'C':
  59. $var = &$_COOKIE;
  60. break;
  61. default:
  62. if(isset($_GET[$k])) {
  63. $var = &$_GET;
  64. } else {
  65. $var = &$_POST;
  66. }
  67. break;
  68. }
  69. return $var[$k] ?? NULL;
  70. }
  71. function dhtmlspecialchars($string, $flags = null) {
  72. if(is_array($string)) {
  73. foreach($string as $key => $val) {
  74. $string[$key] = dhtmlspecialchars($val, $flags);
  75. }
  76. } else {
  77. if($flags === null) {
  78. $string = str_replace(['&', '"', '<', '>'], ['&amp;', '&quot;', '&lt;', '&gt;'], $string);
  79. } else {
  80. if(PHP_VERSION < '5.4.0') {
  81. $string = htmlspecialchars($string, $flags);
  82. } else {
  83. if(strtolower(CHARSET) == 'utf-8') {
  84. $charset = 'UTF-8';
  85. } else {
  86. $charset = 'ISO-8859-1';
  87. }
  88. $string = htmlspecialchars($string, $flags, $charset);
  89. }
  90. }
  91. }
  92. return $string;
  93. }
  94. function return_bytes($val) {
  95. $last = strtolower($val[strlen($val) - 1]);
  96. if(!is_numeric($val)) {
  97. $val = substr(trim($val), 0, -1);
  98. }
  99. switch($last) {
  100. case 'g':
  101. $val *= 1024;
  102. case 'm':
  103. $val *= 1024;
  104. case 'k':
  105. $val *= 1024;
  106. }
  107. return $val;
  108. }
  109. function checkrobot($useragent = '') {
  110. static $kw_spiders = ['bot', 'crawl', 'spider', 'slurp', 'sohu-search', 'lycos', 'robozilla'];
  111. static $kw_browsers = ['msie', 'netscape', 'opera', 'konqueror', 'mozilla'];
  112. $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
  113. if(dstrpos($useragent, $kw_spiders)) return true;
  114. if(!str_contains($useragent, 'http://') && dstrpos($useragent, $kw_browsers)) return false;
  115. return false;
  116. }
  117. function dstrpos($string, $arr, $returnvalue = false) {
  118. if(empty($string)) return false;
  119. foreach((array)$arr as $v) {
  120. if(str_contains($string, $v)) {
  121. $return = $returnvalue ? $v : true;
  122. return $return;
  123. }
  124. }
  125. return false;
  126. }
  127. function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $cachefile, $tpldir, $file) {
  128. static $tplrefresh, $timestamp, $targettplname;
  129. if($tplrefresh === null) {
  130. $tplrefresh = getglobal('config/output/tplrefresh');
  131. $timestamp = getglobal('timestamp');
  132. }
  133. if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($timestamp % $tplrefresh))) {
  134. if(empty($timecompare) || tplfile::filemtime($subtpl) > $timecompare) {
  135. require_once DISCUZ_ROOT.'/source/class/class_template.php';
  136. $template = new template();
  137. $template->parse_template($maintpl, $templateid, $tpldir, $file, $cachefile);
  138. return TRUE;
  139. }
  140. }
  141. return FALSE;
  142. }
  143. function fileext($filename) {
  144. return addslashes(strtolower(substr(strrchr($filename, '.'), 1, 10)));
  145. }
  146. function strexists($string, $find) {
  147. return !(!str_contains($string, $find));
  148. }
  149. function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl = '') {
  150. global $_G;
  151. if(str_starts_with($tpldir, 'source/app/')) {
  152. $tplfile = DISCUZ_ROOT.$tpldir.'/'.$file.'.php';
  153. } else {
  154. if(!$tpldir) {
  155. $tpldir = './template/default';
  156. }
  157. $tplfile = DISCUZ_TEMPLATE($tpldir).'/'.$file.'.htm';
  158. if(!tplfile::file_exists($tplfile)) {
  159. $tplfile = DISCUZ_TEMPLATE($tpldir).'/'.$file.'.php';
  160. }
  161. }
  162. $file == 'common/header' && defined('CURMODULE') && CURMODULE && $file = 'common/header_'.$_G['basescript'].'_'.CURMODULE;
  163. $cachefile = './template/'.$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';
  164. if($gettplfile) {
  165. return $tplfile;
  166. }
  167. checktplrefresh($tplfile, $tplfile, tplfile::filemtime(DISCUZ_DATA.$cachefile), $templateid, $cachefile, $tpldir, $file);
  168. return DISCUZ_DATA.$cachefile;
  169. }