<?php
/**
 * @version $Id: validate.func.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

/**
 * Email 地址
 */
function isEMAIL($value)
{
    return preg_match('/^[A-Za-z0-9]+([._\-\+]*[A-Za-z0-9]+)*@([A-Za-z0-9]+[-A-Za-z0-9]*[A-Za-z0-9]+\.)+[A-Za-z0-9]+$/', $value) != 0;
}

/**
 * 时间日期（所有 GNU Date Input Formats，例如 yyyy/mm/dd、yyyy-mm-dd、hh:mm:ss）
 */
function isDateTime($value)
{
    return preg_match('/^[yY]-[mn]-[dj]$/is', $value) != 0;
}

/**
 * 分页号
 */
function isPage($page)
{
	return !empty($page) && preg_match ('/^([0-9]+)$/', $page);
}

/**
 * 域名
 */
function isDOMAIN($value)
{
    return preg_match('/[a-z0-9\.]/is', $value) != 0;
}

/**
 * 颜色
 */
function isColorCode($value)
{
	return preg_match('/^#[0-9a-f]{6}/i', $value) != 0;
}

/**
 * 整数
 */
function isINT($value)
{
    return strlen(intval($value)) == strlen($value) && is_numeric($value);
}

/**
 * HASH
 */
function isHash($value)
{
	return preg_match('/^[0-9a-zA-Z]{10}$/i', $value) != 0;
}

/**
 * 匿名用户昵称
 */
function checkAnonName($nickName)
{
	global $func_message, $pb_banwords;
	if (strlen($nickName) > 30)
	{
		return $func_message['nickname_long'];
	}
	$banwords = explode(',', $pb_banwords);
	if ($pb_banwords)
	{
		foreach ($banwords as $value)
		{
			if (strpos($nickName, $value) !== FALSE)
			{
				return $func_message['nickname_banword_exist'];
			}
		}
	}
}

/**
 * 用户名检测
 * 
 * @param String $username 用户名
 * @param Boolean $skip 是否忽略注册名限制
 */
function checkUserName($username, $skip = FALSE)
{
	global $reg_bannames,$func_message,$reg_minname,$reg_maxname;
	$banwords = array_filter(explode(',', $reg_bannames));
	$min = (int)$reg_minname;
	$max = (int)$reg_maxname;
	$len = strlen($username);
	$fc = array ("\\","&","'","\"","/","*",",","<",">","\r","\t","\n","#","$","(",")","%","@","+","?",";","^"," ");
	if (($len > $max) || ($len < $min))
	{
		return $func_message['username_length_error'];
	}
	if ($reg_bannames && !$skip)
	{
		foreach ($banwords as $value)
		{
			if (strpos($username, $value) !== FALSE)
			{
				return $func_message['username_banword_exist'];
			}
		}
	}
	foreach ($fc as $value)
	{
		if (strpos($username, $value) !== FALSE)
		{
			return $func_message['username_fchar_exist'];
		}
	}
}

/**
 * 检查用户密码
 * 
 * @param String $password 密码
 */
function checkPassword($password)
{
	global $func_message;
	$fc = array ("\r","\t","\n","'","\"","\\");
	if (!$password)
	{
		return $func_message['password_empty'];
	}
	if (strlen($password) < 6)
	{
		return $func_message['password_less_six'];
	}
	foreach ($fc as $value)
	{
		if (strpos($password, $value) !== FALSE)
		{
			return $func_message['p_fchar_exist'];
		}
	}
}

/**
 * 检查电子邮件
 * 
 * @param String $email 电子邮件地址
 */
function checkEmail($email)
{
	global $func_message;
	if (!$email)
	{
		return $func_message['email_empty'];
	}
	if (!preg_match('/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,3}$/i', $email))
	{
		return $func_message['email_format_error'];
	}
}

/**
 * 分类名称检测
 */
function checkCateName($value)
{
	global $a_message;
	$len = strlen($value);
	if ($len < 1)
	{
		return $a_message['cate_name_empty'];
	}
	elseif ($len > 50)
	{
		return $a_message['cate_name_long'];
	}
}

/**
 * 检查标签
 * 
 * @param String $tag 标签名称
 */
function checkTag($tag)
{
	global $pb_banwords, $func_message;
	$fc = array ("\\","&","'","\"","/","*","<",">","\r","\t","\n","#","$","(",")","%","@","+","?",";","^");
	foreach ($fc as $char)
	{
		if (strpos($tag, $char) !== FALSE)
		{
			return $func_message['tag_fchar_exist'];
		}
	}
	$banwords = explode(',', $pb_banwords);
	if ($pb_banwords)
	{
		foreach ($banwords as $value)
		{
			if (strpos($tag, $value) !== FALSE)
			{
				return $func_message['tag_banword_exist'];
			}
		}
	}
}

/**
 * 检查QQ是否合法
 */
function checkQQ($qq)
{
	return preg_match('/^[1-9][0-9]{4,8}$/i', $qq) != 0;
}

/**
 * 检查MSN是否合法
 */
function checkMSN($msn)
{
	return preg_match('/^[_\.0-9a-zA-Z-]+@[\w\.]*?$/i', $msn) != 0;
}

/**
 * 检查URL链接
 */
function checkURL($url)
{
	global $func_message, $pb_banlinks;
	$fc = array ("\\","'","\"","*","<",">","\r","\t","\n","$","(",")");
	if (!preg_match('/^(https?|ftp|gopher|news|telnet|mms|rtsp):\/\//i', $url))
	{
		return $func_message['source_illegal_linkurl'];
	}
	foreach ($fc as $value)
	{
		if (strpos($url, $value) !== FALSE)
		{
			return $func_message['source_fchar_exist'];
		}
	}
	if ($pb_banlinks)
	{
		preg_match('#^(https?|ftp|gopher|news|telnet|mms|rtsp):\/\/([^\/]*)#i', $url, $linkhost);
		$pb_banlinks = explode('|', $pb_banlinks);
		foreach ($pb_banlinks as $l)
		{
			if (strpos($linkhost[2], $l) !== FALSE)
			{
				return $func_message['source_banlink_exist'];
			}
		}
	}
}

/**
 * 检查来自信息
 */
function checkLocation($location)
{
	global $pb_banwords, $func_message;
	if (strlen($location) > 30) return $func_message['location_len_limit'];
	$banwords = explode(',', $pb_banwords);
	$fc = array ("\\","&","'","\"","/","*",",","<",">","\r","\t","\n","#","$","(",")","%","@","+","?",";","^"," ");
	foreach ($fc as $value)
	{
		if (strpos($location, $value) !== FALSE)
		{
			return $func_message['location_fchar_exist'];
		}
	}
	if ($pb_banwords)
	{
		foreach ($banwords as $value)
		{
			if (strpos($location, $value) !== FALSE)
			{
				return $func_message['location_banword_exist'];
			}
		}
	}
}

/**
 * 检查评论
 * 
 * @param String $comment 评论内容
 */
function checkComment($comment)
{
	global $pb_banwords, $pb_commentlen, $func_message;
	$banwords = explode(',', $pb_banwords);
	list ($pb_commentlenmin, $pb_commentlenmax) = explode("\t", $pb_commentlen);
	$len = strlen($comment);
	if ($len < $pb_commentlenmin || $len > $pb_commentlenmax)
	{
		return sprintf($func_message['comment_len_limit'], $pb_commentlenmin, $pb_commentlenmax);
	}
	if ($pb_banwords)
	{
		foreach ($banwords as $value)
		{
			if (strpos($comment, $value) !== FALSE)
			{
				return $func_message['comment_banword_exist'];
			}
		}
	}
}

/**
 * 检查短信息
 */
function checkMsg($msg)
{
	global $pb_banwords;
	$banwords = explode(',', $pb_banwords);
	if ($pb_banwords)
	{
		foreach ($banwords as $value)
		{
			if (strpos($msg, $value) !== FALSE)
			{
				return FALSE;
			}
		}
	}
	return TRUE;
}

/**
 * 检查头像链接
 */
function checkAvatar($url)
{
	global $func_message;
	$fc = array ("\\"," ","'","\"","*",",","<",">","\r","\t","\n","$","(",")","+","^");
	if (!preg_match('/^https?:\/\/[^\s]+?\.(gif|jpeg|png|jpg)/i', $url))
	{
		return $func_message['avatar_illegal_linkurl'];
	}
	foreach ($fc as $value)
	{
		if (strpos($url, $value) !== FALSE)
		{
			return $func_message['avatar_fchar_exist'];
		}
	}
}

/**
 * 检查标题
 */
function checkTitle($var)
{
	global $pb_titlelen, $pb_banwords, $func_message;
	list ($min, $max) = explode("\t", $pb_titlelen);
	$banwords = explode(',', $pb_banwords);
	if (strlen($var) > $max || strlen($var) < $min)
	{
		return sprintf($func_message['title_limit'], $min, $max);
	}
	if ($pb_banwords)
	{
		foreach ($banwords as $value)
		{
			if (strpos($var, $value) !== FALSE)
			{
				return $func_message['title_banword_exist'];
			}
		}
	}
}

/**
 * 检查主题内容
 */
function checkContent($var)
{
	global $pb_contentlen, $pb_banwords, $func_message;
	list ($min, $max) = explode("\t", $pb_contentlen);
	$banwords = explode(',', $pb_banwords);
	if (strlen($var) > $max || strlen($var) < $min)
	{
		return sprintf($func_message['content_limit'], $min, $max);
	}
	if ($pb_banwords)
	{
		foreach ($banwords as $value)
		{
			if (strpos($var, $value) !== FALSE)
			{
				return $func_message['content_banword_exist'];
			}
		}
	}
}

?>
