UCHome中关于公共函数(function_common.php)页面的代码分析(一)
/*
[UCenter Home] (C) 2007-2008 Comsenz Inc.
$Id: function_common.php 2009-10-20 21:12:00
@author ymaozi
@copyright http://www.codedesign.cn
@uchome源码交流QQ群:83400263
*/
if(!defined('IN_UCHOME')) {
exit('Access Denied');
}
/**
* SQL ADDSLASHES 对sql的一些字符进行转义
* @param string or array $string
* @return string or array
*/
function saddslashes($string) {
if(is_array($string)) { //如果转入的是数组则对数组中的value进行递归转义
foreach($string as $key => $val) {
$string[$key] = saddslashes($val);
}
} else {
$string = addslashes($string); //对单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符),进行转义
}
return $string;
}
/**
* 取消HTML代码
* @param string or array $string
* @return string or array
*/
function shtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = shtmlspecialchars($val);
}
} else {
$string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string));//将传入的html中的&,",<,>,进行替换
}
return $string;
}
/**
* 清空cookie与一些判断用户登录的信息
*/
function clearcookie() {
global $_SGLOBAL;
obclean(); //清除缓存
ssetcookie('auth', '', -86400 * 365); //设置cookie名为auth的过期
$_SGLOBAL['supe_uid'] = 0;
$_SGLOBAL['supe_username'] = '';
$_SGLOBAL['member'] = array(); //将这些全局变量清空
}
//cookie设置
/**
* 设置cookie
* @param string cookie名
* @param string cookie值
* @param int cookie存储时间
* @return void
*/
function ssetcookie($var, $value, $life=0) {
global $_SGLOBAL, $_SC, $_SERVER;
setcookie($_SC['cookiepre'].$var, $value, $life?($_SGLOBAL['timestamp']+$life):0, $_SC['cookiepath'], $_SC['cookiedomain'], $_SERVER['SERVER_PORT']==443?1:0);
}
//
/**
* 创建数据库连接对象
*/
function dbconnect() {
global $_SGLOBAL, $_SC;
include_once(S_ROOT.'./source/class_mysql.php'); //引入数据库操作类
if(empty($_SGLOBAL['db'])) { //如果没有创建数据库对象,则创建
$_SGLOBAL['db'] = new dbstuff;
$_SGLOBAL['db']->charset = $_SC['dbcharset'];
$_SGLOBAL['db']->connect($_SC['dbhost'], $_SC['dbuser'], $_SC['dbpw'], $_SC['dbname'], $_SC['pconnect']);
}
}
//获取在线IP
function getonlineip($format=0) {
global $_SGLOBAL;
if(empty($_SGLOBAL['onlineip'])) {
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
//如果存在客户端ip,并通过strcasecmp(),比较不等于unknown,则获取客户端ip
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
//如果存在代理ip,则获取代理ip
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
//代理服务器 IP
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
//通过正则检验,是否是ip地址的格式
$_SGLOBAL['onlineip'] = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
}
if($format) {
$ips = explode('.', $_SGLOBAL['onlineip']); //将ip地址,以.为分隔存入到数组
for($i=0;$i<3;$i++) {
$ips[$i] = intval($ips[$i]);
}
return sprintf('%03d%03d%13d', $ips[0], $ips[1], $ips[2]);//返回ip地十的前三段,03d:三位整数,如果不足刚以0填充
} else {
return $_SGLOBAL['onlineip'];
}
}
//
/**
* 判断当前用户登录状态
*/
function checkauth() {
global $_SGLOBAL, $_SC, $_SCONFIG, $_SCOOKIE, $_SN;
if($_SGLOBAL['mobile'] && $_GET['m_auth']) $_SCOOKIE['auth'] = $_GET['m_auth'];
if($_SCOOKIE['auth']) { //如果设置了名了auth的cookie
@list($password, $uid) = explode("\t", authcode($_SCOOKIE['auth'], 'DECODE')); //通过authcode()函数将加密过的auth进行解密,将解密的信息分别存在$password与$uid中
$_SGLOBAL['supe_uid'] = intval($uid); //将$uid设置给全局的supe_uid
if($password && $_SGLOBAL['supe_uid']) { //如果密码与uid都存在,则判断用户信息的正确性
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('session')." WHERE uid='$_SGLOBAL[supe_uid]'");
if($member = $_SGLOBAL['db']->fetch_array($query)) {
if($member['password'] == $password) {
$_SGLOBAL['supe_username'] = addslashes($member['username']);
$_SGLOBAL['session'] = $member;
} else {
$_SGLOBAL['supe_uid'] = 0;
}
} else {//如果用户表中不存在该用户,则到用户表中查找
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('member')." WHERE uid='$_SGLOBAL[supe_uid]'");
if($member = $_SGLOBAL['db']->fetch_array($query)) {
if($member['password'] == $password) {
$_SGLOBAL['supe_username'] = addslashes($member['username']);
$session = array('uid' => $_SGLOBAL['supe_uid'], 'username' => $_SGLOBAL['supe_username'], 'password' => $password);
include_once(S_ROOT.'./source/function_space.php');
insertsession($session);//将信息插入到session表中
} else {
$_SGLOBAL['supe_uid'] = 0;
}
} else {
$_SGLOBAL['supe_uid'] = 0;
}
}
}
}
if(empty($_SGLOBAL['supe_uid'])) {
//如果supe_uid为空,则清除cookie
clearcookie();
} else {
$_SGLOBAL['username'] = $member['username'];
}
}
[UCenter Home] (C) 2007-2008 Comsenz Inc.
$Id: function_common.php 2009-10-20 21:12:00
@author ymaozi
@copyright http://www.codedesign.cn
@uchome源码交流QQ群:83400263
*/
if(!defined('IN_UCHOME')) {
exit('Access Denied');
}
/**
* SQL ADDSLASHES 对sql的一些字符进行转义
* @param string or array $string
* @return string or array
*/
function saddslashes($string) {
if(is_array($string)) { //如果转入的是数组则对数组中的value进行递归转义
foreach($string as $key => $val) {
$string[$key] = saddslashes($val);
}
} else {
$string = addslashes($string); //对单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符),进行转义
}
return $string;
}
/**
* 取消HTML代码
* @param string or array $string
* @return string or array
*/
function shtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = shtmlspecialchars($val);
}
} else {
$string = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1',
str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string));//将传入的html中的&,",<,>,进行替换
}
return $string;
}
/**
* 清空cookie与一些判断用户登录的信息
*/
function clearcookie() {
global $_SGLOBAL;
obclean(); //清除缓存
ssetcookie('auth', '', -86400 * 365); //设置cookie名为auth的过期
$_SGLOBAL['supe_uid'] = 0;
$_SGLOBAL['supe_username'] = '';
$_SGLOBAL['member'] = array(); //将这些全局变量清空
}
//cookie设置
/**
* 设置cookie
* @param string cookie名
* @param string cookie值
* @param int cookie存储时间
* @return void
*/
function ssetcookie($var, $value, $life=0) {
global $_SGLOBAL, $_SC, $_SERVER;
setcookie($_SC['cookiepre'].$var, $value, $life?($_SGLOBAL['timestamp']+$life):0, $_SC['cookiepath'], $_SC['cookiedomain'], $_SERVER['SERVER_PORT']==443?1:0);
}
//
/**
* 创建数据库连接对象
*/
function dbconnect() {
global $_SGLOBAL, $_SC;
include_once(S_ROOT.'./source/class_mysql.php'); //引入数据库操作类
if(empty($_SGLOBAL['db'])) { //如果没有创建数据库对象,则创建
$_SGLOBAL['db'] = new dbstuff;
$_SGLOBAL['db']->charset = $_SC['dbcharset'];
$_SGLOBAL['db']->connect($_SC['dbhost'], $_SC['dbuser'], $_SC['dbpw'], $_SC['dbname'], $_SC['pconnect']);
}
}
//获取在线IP
function getonlineip($format=0) {
global $_SGLOBAL;
if(empty($_SGLOBAL['onlineip'])) {
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
//如果存在客户端ip,并通过strcasecmp(),比较不等于unknown,则获取客户端ip
$onlineip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
//如果存在代理ip,则获取代理ip
$onlineip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
//代理服务器 IP
$onlineip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$onlineip = $_SERVER['REMOTE_ADDR'];
}
preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
//通过正则检验,是否是ip地址的格式
$_SGLOBAL['onlineip'] = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
}
if($format) {
$ips = explode('.', $_SGLOBAL['onlineip']); //将ip地址,以.为分隔存入到数组
for($i=0;$i<3;$i++) {
$ips[$i] = intval($ips[$i]);
}
return sprintf('%03d%03d%13d', $ips[0], $ips[1], $ips[2]);//返回ip地十的前三段,03d:三位整数,如果不足刚以0填充
} else {
return $_SGLOBAL['onlineip'];
}
}
//
/**
* 判断当前用户登录状态
*/
function checkauth() {
global $_SGLOBAL, $_SC, $_SCONFIG, $_SCOOKIE, $_SN;
if($_SGLOBAL['mobile'] && $_GET['m_auth']) $_SCOOKIE['auth'] = $_GET['m_auth'];
if($_SCOOKIE['auth']) { //如果设置了名了auth的cookie
@list($password, $uid) = explode("\t", authcode($_SCOOKIE['auth'], 'DECODE')); //通过authcode()函数将加密过的auth进行解密,将解密的信息分别存在$password与$uid中
$_SGLOBAL['supe_uid'] = intval($uid); //将$uid设置给全局的supe_uid
if($password && $_SGLOBAL['supe_uid']) { //如果密码与uid都存在,则判断用户信息的正确性
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('session')." WHERE uid='$_SGLOBAL[supe_uid]'");
if($member = $_SGLOBAL['db']->fetch_array($query)) {
if($member['password'] == $password) {
$_SGLOBAL['supe_username'] = addslashes($member['username']);
$_SGLOBAL['session'] = $member;
} else {
$_SGLOBAL['supe_uid'] = 0;
}
} else {//如果用户表中不存在该用户,则到用户表中查找
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('member')." WHERE uid='$_SGLOBAL[supe_uid]'");
if($member = $_SGLOBAL['db']->fetch_array($query)) {
if($member['password'] == $password) {
$_SGLOBAL['supe_username'] = addslashes($member['username']);
$session = array('uid' => $_SGLOBAL['supe_uid'], 'username' => $_SGLOBAL['supe_username'], 'password' => $password);
include_once(S_ROOT.'./source/function_space.php');
insertsession($session);//将信息插入到session表中
} else {
$_SGLOBAL['supe_uid'] = 0;
}
} else {
$_SGLOBAL['supe_uid'] = 0;
}
}
}
}
if(empty($_SGLOBAL['supe_uid'])) {
//如果supe_uid为空,则清除cookie
clearcookie();
} else {
$_SGLOBAL['username'] = $member['username'];
}
}
没事写点uchome的代码分析,希望有人和我一起学习呀
浙公网安备 33010602011771号