| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- /**
- * [Discuz!] (C)2001-2099 Discuz! Team
- * This is NOT a freeware, use is subject to license terms
- * https://license.discuz.vip
- */
- if(!defined('IN_DISCUZ') && !defined('IN_API')) {
- exit('Access Denied');
- }
- function DISCUZ_APP($app = '') {
- return DISCUZ_ROOT.'./source/app'.($app !== '' ? '/'.$app : '');
- }
- function appfile($filename, $app = '') {
- $app = $app ?: DISCUZ_APP;
- $p = strpos($filename, '/');
- if($p === false) {
- return '';
- }
- $folder = substr($filename, 0, $p);
- $apppath = '/source/app/'.$app;
- $path = $apppath.'/'.$filename;
- return preg_match('/^[\w\d\/_]+$/i', $path) ? realpath(DISCUZ_ROOT.$path.'.php') : false;
- }
- function setglobal($key, $value, $group = null) {
- global $_G;
- $key = explode('/', $group === null ? $key : $group.'/'.$key);
- $p = &$_G;
- foreach($key as $k) {
- if(!isset($p[$k]) || !is_array($p[$k])) {
- $p[$k] = [];
- }
- $p = &$p[$k];
- }
- $p = $value;
- return true;
- }
- function getglobal($key, $group = null) {
- global $_G;
- $key = explode('/', $group === null ? $key : $group.'/'.$key);
- $v = &$_G;
- foreach($key as $k) {
- if(!isset($v[$k])) {
- return null;
- }
- $v = &$v[$k];
- }
- return $v;
- }
- function getgpc($k, $type = 'GP') {
- $type = strtoupper($type);
- switch($type) {
- case 'G':
- $var = &$_GET;
- break;
- case 'P':
- $var = &$_POST;
- break;
- case 'C':
- $var = &$_COOKIE;
- break;
- default:
- if(isset($_GET[$k])) {
- $var = &$_GET;
- } else {
- $var = &$_POST;
- }
- break;
- }
- return $var[$k] ?? NULL;
- }
- function dhtmlspecialchars($string, $flags = null) {
- if(is_array($string)) {
- foreach($string as $key => $val) {
- $string[$key] = dhtmlspecialchars($val, $flags);
- }
- } else {
- if($flags === null) {
- $string = str_replace(['&', '"', '<', '>'], ['&', '"', '<', '>'], $string);
- } else {
- if(PHP_VERSION < '5.4.0') {
- $string = htmlspecialchars($string, $flags);
- } else {
- if(strtolower(CHARSET) == 'utf-8') {
- $charset = 'UTF-8';
- } else {
- $charset = 'ISO-8859-1';
- }
- $string = htmlspecialchars($string, $flags, $charset);
- }
- }
- }
- return $string;
- }
- function return_bytes($val) {
- $last = strtolower($val[strlen($val) - 1]);
- if(!is_numeric($val)) {
- $val = substr(trim($val), 0, -1);
- }
- switch($last) {
- case 'g':
- $val *= 1024;
- case 'm':
- $val *= 1024;
- case 'k':
- $val *= 1024;
- }
- return $val;
- }
- function checkrobot($useragent = '') {
- static $kw_spiders = ['bot', 'crawl', 'spider', 'slurp', 'sohu-search', 'lycos', 'robozilla'];
- static $kw_browsers = ['msie', 'netscape', 'opera', 'konqueror', 'mozilla'];
- $useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
- if(dstrpos($useragent, $kw_spiders)) return true;
- if(!str_contains($useragent, 'http://') && dstrpos($useragent, $kw_browsers)) return false;
- return false;
- }
- function dstrpos($string, $arr, $returnvalue = false) {
- if(empty($string)) return false;
- foreach((array)$arr as $v) {
- if(str_contains($string, $v)) {
- $return = $returnvalue ? $v : true;
- return $return;
- }
- }
- return false;
- }
- function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $cachefile, $tpldir, $file) {
- static $tplrefresh, $timestamp, $targettplname;
- if($tplrefresh === null) {
- $tplrefresh = getglobal('config/output/tplrefresh');
- $timestamp = getglobal('timestamp');
- }
- if(empty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($timestamp % $tplrefresh))) {
- if(empty($timecompare) || tplfile::filemtime($subtpl) > $timecompare) {
- require_once DISCUZ_ROOT.'/source/class/class_template.php';
- $template = new template();
- $template->parse_template($maintpl, $templateid, $tpldir, $file, $cachefile);
- return TRUE;
- }
- }
- return FALSE;
- }
- function fileext($filename) {
- return addslashes(strtolower(substr(strrchr($filename, '.'), 1, 10)));
- }
- function strexists($string, $find) {
- return !(!str_contains($string, $find));
- }
- function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl = '') {
- global $_G;
- if(str_starts_with($tpldir, 'source/app/')) {
- $tplfile = DISCUZ_ROOT.$tpldir.'/'.$file.'.php';
- } else {
- if(!$tpldir) {
- $tpldir = './template/default';
- }
- $tplfile = DISCUZ_TEMPLATE($tpldir).'/'.$file.'.htm';
- if(!tplfile::file_exists($tplfile)) {
- $tplfile = DISCUZ_TEMPLATE($tpldir).'/'.$file.'.php';
- }
- }
- $file == 'common/header' && defined('CURMODULE') && CURMODULE && $file = 'common/header_'.$_G['basescript'].'_'.CURMODULE;
- $cachefile = './template/'.$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';
- if($gettplfile) {
- return $tplfile;
- }
- checktplrefresh($tplfile, $tplfile, tplfile::filemtime(DISCUZ_DATA.$cachefile), $templateid, $cachefile, $tpldir, $file);
- return DISCUZ_DATA.$cachefile;
- }
|