www.gusucode.com > HadSky PHP轻论坛系统源码 v2.4.2源码程序 > ytqlt_v2.4.2.0701/puyuetian/function.php

    <?php
if (!defined('puyuetian'))
	exit('Not Found puyuetian!Please contact QQ632827168');
//返回整数或小数数字,非数字返回0
function Cnum($str, $return = 0, $int = true) {
	if (is_numeric($str)) {
		if ($int)
			$str = (int)$str;
	} else {
		$str = $return;
	}
	return $str;
}

//返回符合条件的字符串,否则返回false
function Cstr($str, $return = false, $cstr = true, $minlen = 3, $maxlen = 15) {
	if (!$str) {
		return $return;
	}
	if ($cstr === true) {
		global $_G;
		$cstr = $_G['STRING']['UPPERCASE'] . $_G['STRING']['LOWERCASE'] . $_G['STRING']['NUMERICAL'] . '_';
	}
	$len = strlen($str);
	if ($cstr) {
		for ($i = 0; $i < $len; $i++) {
			$chk = strpos($cstr, substr($str, $i, 1));
			if ($chk === false) {
				return $return;
			}
		}
	}
	if ($minlen < $maxlen) {
		if ($len < $minlen) {
			return $return;
		}
		if ($len > $maxlen) {
			return $return;
		}
	} elseif ($minlen == $maxlen) {
		if ($len != $maxlen)
			return $return;
	}
	return $str;
}

//bbcode函数,若数据库设置了过滤标签则使用数据库的标签,否则使用系统默认标签
function BBcode($str, $marks = null, $attrs = null) {
	global $_G;
	if ($marks === null) {
		$_G['SET']['BBCODEMARKS'] ? $marks = $_G['SET']['BBCODEMARKS'] : $marks = $_G['STRING']['BBCODEMARKS'];
	}
	//第一次过滤
	$str = strip_tags($str, $marks);
	//第二次过滤
	if ($marks) {
		if (preg_match_all('/\<(.*?)\>/', $str, $match)) {
			foreach ($match[1] as $value2) {
				//获取当前标签名
				$bqn = substr($value2, 0, strpos($value2, ' '));
				//去除标签名
				$value2 = substr($value2, strlen($bqn));
				//处理前的初始化数据,转小写,去空格,去/
				$value2 = str_replace(' ', '', strtolower($value2));
				$value2 = str_replace('/', '', $value2);
				//检测是否为无元素标签
				if (strpos($marks, '<' . $value2 . '>') === FALSE) {
					if ($attrs === null) {
						$_G['SET']['BBCODEATTRS'] ? $attrs = $_G['SET']['BBCODEATTRS'] : $attrs = $_G['STRING']['BBCODEATTRS'];
					}
					$wms = explode(',', $attrs);
					$chkstr = $value2;
					//清除白名单数据
					foreach ($wms as $wm) {
						if ($wm == 'href') {
							//a标签特殊检测
							if (strpos($chkstr, 'href="javascript:') !== FALSE) {
								$str = htmlspecialchars($str);
								break 2;
							}
						}
						$chkstr = preg_replace('/' . $wm . '=".*?"/', '', $chkstr);
					}
					//处理后的检测
					if ($chkstr) {
						$str = htmlspecialchars($str);
						break;
					}
				}
			}
		}
	}
	return $str;
}

//html静态模板加载函数,文件名,返回结果或输出结果,需要输出的html代码,是否替换变量
function template($filename = false, $return = false, $htmlcode = false, $isreplace = true) {
	global $_G;
	if ($filename) {
		$filename = str_replace('\\', '/', $filename);
	}
	$_G['SYSTEM']['LOADTEMPLATENAME'] = $filename;
	$templatename = $_G['SET']['TEMPLATENAME'];
	if (!$htmlcode) {
		if (!$filename) {
			$filename = getcfilename();
		}
		$lj = $_G['SYSTEM']['PATH'];
		if (strpos($filename, '/') !== FALSE) {
			$htmlcode = file_get_contents("{$filename}");
		} elseif (strpos($filename, ':')) {
			$plugname = explode(':', $filename);
			$htmlcode = file_get_contents("{$lj}/phpscript/plug/{$plugname[0]}/template/{$plugname[1]}.html");
		} else {
			!file_exists("{$lj}/template/{$templatename}/{$filename}.html") ? $templatename2 = 'mb1' : $templatename2 = $_G['SET']['TEMPLATENAME'];
			@$htmlcode = file_get_contents("{$lj}/template/{$templatename2}/{$filename}.html");
			if (!$htmlcode) {
				return false;
			}
		}
	}
	if ($isreplace) {
		//模板内PHP脚本的执行
		if (preg_match_all('/<\?php[\s\S]*\?>/', $htmlcode, $match)) {
			foreach ($match as $value) {
				foreach ($value as $value2) {
					$bl = substr($value2, 5, strlen($value2) - 7);
					eval("global \$_G;{$bl}");
					//ECHO htmlspecialchars($bl,ENT_QUOTES);
					$htmlcode = str_replace($value2, '', $htmlcode);
				}
			}
		}
		//模板内变量的显示
		if (preg_match_all('#\{\$[A-Za-z0-9_\-\[\]\']+\}#', $htmlcode, $match)) {
			foreach ($match as $value) {
				foreach ($value as $value2) {
					$bl = substr($value2, 2, strlen($value2) - 3);
					if (strpos($bl, '[')) {//防止数组被global
						$globalbl = substr($bl, 0, strpos($bl, '['));
					} else {
						$globalbl = $bl;
					}
					eval("global \$" . $globalbl . ";");
					eval("\$bl=\$" . $bl . ";");
					//防止PHP函数执行漏洞
					$bl = str_replace('{', '&#123;', $bl);
					$bl = str_replace('}', '&#125;', $bl);
					$htmlcode = str_replace($value2, $bl, $htmlcode);
				}
			}
		}
		//模板内函数的显示
		if (preg_match_all('/\{\S+\}/', $htmlcode, $match)) {
			foreach ($match as $value) {
				foreach ($value as $value2) {
					$bl = substr($value2, 1, strlen($value2) - 2);
					if (fun_cunzai($bl)) {
						eval("\$bl=" . $bl . ";");
						$htmlcode = str_replace($value2, $bl, $htmlcode);
					}
				}
			}
		}
		//还原被过滤的字符
		$htmlcode = str_replace('&#123;', '{', $htmlcode);
		$htmlcode = str_replace('&#125;', '}', $htmlcode);
	}
	if ($return) {
		return $htmlcode;
	} else {
		echo $htmlcode;
	}
}

//mysql数据库转义,待过滤字符串,是否添加'',''两边添加的字符,是否强制添加''(false数字不添加)
function mysqlstr($str, $quto = true, $bwf = '', $must = false) {
	if (get_magic_quotes_gpc()) {
		$str = stripcslashes($str);
		//去掉默认开启magic_quotes_gpc所添加的转义
	}
	$str = mysql_real_escape_string($str);
	if ($quto && !is_numeric($str) || $must) {
		$str = "'{$bwf}{$str}{$bwf}'";
	}
	return $str;
}

//获取当前文件的名称
function getcfilename($hz = false) {
	$url = $_SERVER['SCRIPT_NAME'];
	$filename = end(explode('/', $url));
	if (!$hz) {
		$pos = strripos($filename, '.');
		$filename = substr($filename, 0, $pos);
	}
	return $filename;
}

function fun_cunzai($funname) {
	$pos = strpos($funname, "(");
	if ($pos) {
		$name = substr($funname, 0, $pos);
		if (function_exists($name)) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function getClientInfos($info = 'all') {
	if ($info == 'all') {
		$infos = '浏览器标示:' . $_SERVER['HTTP_USER_AGENT'] . ' <br>
';
		$infos .= '客户端语言:' . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . '
<br>
';
		$infos .= '客户端IP地址:' . $_SERVER['REMOTE_ADDR'];
	} elseif ($info == 'ip') {
		$infos = $_SERVER['REMOTE_ADDR'];
	} else {
		$infos = $_SERVER[$info];
	}
	return $infos;
}

function chkuploadfilesuffix($suffix) {
	if (!$suffix)
		return FALSE;
	global $_G;
	return in_array($suffix, explode("|", $_G['SET']['UPLOADFILETYPES']));
}

function ReWriteURL($name, $parmas, $addparmas = '', $delimiter = '-', $suffix = '.html') {
	global $_G;
	if ($_G['SET']['REWRITEURL'] && $_G['USER']['ID'] == 2) {
		if ($parmas) {
			$parmas = explode('&', $parmas);
			$url = $name;
			foreach ($parmas as $value) {
				$value = explode('=', $value);
				$url .= $delimiter . $value[1];
			}
		} else {
			$url = $name;
		}
		$addparmas ? $url .= "{$suffix}?{$addparmas}" : $url .= $suffix;
	} else {
		$addparmas ? $url = "index.php?c={$name}&{$parmas}&{$addparmas}" : $url .= "index.php?c={$name}&{$parmas}";
	}
	return $url;
}

function __unset() {
	foreach ($GLOBALS as $key => $value) {
		if (substr($key, 0, 2) == "__") {
			unset($GLOBALS[$key]);
		}
	}
}

function getShuXiang($datetime) {
	$year = date('Y', $datetime);
	if ($year) {
		//1900年是鼠年
		$data = array('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪');
		$index = ($year - 1900) % 12;
		return $data[$index];
	} else {
		return FALSE;
	}
}

function getXingZuo($datetime) {
	$date = (int)date('nd', $datetime);
	if ($date) {
		switch ($date) {
			case (120<=$date&&218>=$date) :
				$xz = "水瓶";
				break;
			case (219<=$date&&320>=$date) :
				$xz = "双鱼";
				break;
			case (321<=$date&&419>=$date) :
				$xz = "白羊";
				break;
			case (420<=$date&&520>=$date) :
				$xz = "金牛";
				break;
			case (521<=$date&&621>=$date) :
				$xz = "双子";
				break;
			case (622<=$date&&722>=$date) :
				$xz = "巨蟹";
				break;
			case (723<=$date&&822>=$date) :
				$xz = "狮子";
				break;
			case (823<=$date&&922>=$date) :
				$xz = "处女";
				break;
			case (923<=$date&&1023>=$date) :
				$xz = "天秤";
				break;
			case (1024<=$date&&1121>=$date) :
				$xz = "天蝎";
				break;
			case (1122<=$date&&1221>=$date) :
				$xz = "射手";
				break;
			default :
				$xz = "摩羯";
				break;
		}
		return $xz;
	} else {
		return FALSE;
	}
}

function getNianLing($datetime) {
	return (date('Y', time()) - date('Y', $datetime));
}

function ShellPHP($code, $return = false) {
	eval($code . ';');
	if ($return) {
		return $return;
	}
}

function ArrayData($data, $key, $value = NULL) {
	$__data = unserialize($data);
	if ($value !== NULL) {
		//数据更新
		$__data[$key] = $value;
		$data = serialize($__data);
		return $data;
	} else {
		//数据读取
		return $__data[$key];
	}
}