<?php
	/* 取自互联网: 提交功能 */
	function zhang($curl,$curlPost){
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,$curl);
		curl_setopt($ch,CURLOPT_HEADER,1);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
		$data=curl_exec($ch);
		curl_close($ch);
		return $data;
	}
	/* 取自互联网: 获取IP功能 */
	function ip(){
		if (getenv("HTTP_CLIENT_IP")&&strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown"))
		$ip = getenv("HTTP_CLIENT_IP");
		else if (getenv("HTTP_X_FORWARDED_FOR")&&strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
		else if (getenv("REMOTE_ADDR")&&strcasecmp(getenv("REMOTE_ADDR"),"unknown"))
		$ip = getenv("REMOTE_ADDR");
		else if (isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],"unknown"))
		$ip = $_SERVER['REMOTE_ADDR'];
		else
		$ip ='unknown';
		return($ip);
	}


	/* 取自互联网: 获取SID功能 */
	function newsid($qqhao,$qqmima){
		$post_data = array(
		'qq='.$qqhao,
		'pwd='.$qqmima,
		'bid_code=3GQQ',
		'toQQchat=true',
		'Login_url=http://pt.3g.qq.com/s?aid=nLoginnew&q_from=3GQQ',
		'modifySKey=0',
		'LoginType=1',
		'aid=nLoginHandle',
		);
		$post_data = implode('&',$post_data);
		$url='http://pt.3g.qq.com/psw3gqqLogin?r=777151794&vdata='.md5(time());
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL,$url);
		//传递一个作为HTTP“POST”操作的所有数据的字符串。
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		$out=curl_exec($ch);
		curl_close($ch);
		return $out;
	}
	/* 取自互联网: 删除数组中指定值, 参数: 数组 -> 欲删除的值, 返回: 新的数组 */
	function arr_delelement($arr,$val){
		$newarr = Array();
		for($i = 0;$i<count($arr);$i++){
			if($arr[$i] != $val){
				array_unshift($newarr,$arr[$i]);
			}
		}
		rsort($newarr);
		return $newarr;
	}

	/* 取自互联网: 取出左边文本, 参数: 全文本 -> 文本, 返回: 取出文本 */
	function text_lefttext ($str,$left){
		$a = explode($left,$str);
		return $a[0];
	}

	/* 取自互联网: 取出右边文本, 参数: 全文本 -> 文本, 返回: 取出文本 */
	function text_righttext ($str,$right){
		$a = explode($right,$str);
		return $a[count($a)-1];
	}

	/* 取自互联网: 取出中间文本, 参数: 全文本 -> 文本, 返回: 取出文本 */
	function text_midtext ($str,$left,$right){
		$a = explode($left,$str);
		$sz2 = $a[1];
		$sz1 = explode($right,$sz2);
		return $sz1[0];
	}

	/* 取自互联网: post访问网页, 参数: post地址 -> post数据 -> Cookie, 返回: 取出文本 */
	function post ($str1,$str2,$cookie){
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$str1);
		curl_setopt($ch,CURLOPT_HEADER,1);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$str2);
		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}

	/* 暗月挂Q原创功能: 发送邮件, 参数: 邮箱地址 -> 邮件主题 -> 邮件正文, 返回: 是否成功 */
	function Send_out_Mail ($zMail,$ztheme,$ztext,$SMTPurl='smtp.qq.com',$SMTPdk='465',$SMTPuser='fx@aygq.cc',$SMTPpass='AnYueGuaQ_88'){

		$path = (AnYue_Root==='AnYue_Root') ? '../' : AnYue_Root;

		require_once ($path.'Source/class/class.phpmailer.php');
		require_once ($path.'Source/class/class.smtp.php');
		$mail  = new PHPMailer();
		global $configure;

		if ($configure['SMTP-fwq'] && $configure['SMTP-dkh'] && $configure['SMTP-user'] && $configure['SMTP-pass']){

			$SMTPurl = $configure['SMTP-fwq'];
			$SMTPdk = $configure['SMTP-dkh'];
			$SMTPuser = $configure['SMTP-user'];
			$SMTPpass = $configure['SMTP-pass'];

		}

		$mail->CharSet    = 'UTF-8';                 //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置为 UTF-8
		$mail->IsSMTP(true);                            // 设定使用SMTP服务
		$mail->SMTPAuth   = true;                   // 启用 SMTP 验证功能
		$mail->SMTPSecure = 'ssl';                  // SMTP 安全协议
		$mail->Host       = $SMTPurl;       // SMTP 服务器
		$mail->Port       = $SMTPdk;                    // SMTP服务器的端口号
		$mail->Username   = $SMTPuser;  // SMTP服务器用户名
		$mail->Password   = $SMTPpass;        // SMTP服务器密码
		$mail->SetFrom($SMTPuser, '暗月挂Q网');    // 设置发件人地址和名称
		$mail->AddReplyTo($SMTPuser,"暗月挂Q网");
													// 设置邮件回复人地址和名称
		$mail->Subject    = $ztheme;                     // 设置邮件标题
		$mail->AltBody    = '为了查看该邮件，请切换到支持 HTML 的邮件客户端';
													// 可选项，向下兼容考虑
		$mail->MsgHTML($ztext);                         // 设置邮件内容
		$mail->AddAddress($zMail, '暗月挂Q科技用户');
		//$mail->AddAttachment("Images/phpmailer.gif"); // 附件
		if(!$mail->Send()){
			return 'No';
		} else {
			return 'Ok';
		}

	}

	/* 暗月挂Q原创功能: 生成验证码, 参数: key, 返回: 生成验证码 */
	function create_yzm ($text){
		return MD5 (MD5 ('aygq_'.substr ($text,5)));
	}

	/* 暗月挂Q原创功能: 生成随机数, 参数: 无, 返回: 生成随机数 */
	function Random_number (){
		$i = 1;
		while($i <= 20){
		  $temporary = $temporary . rand(0,9);
		  $i++;
		}
		  return $temporary;
	}

	/* 暗月挂Q原创功能: 生成随机密码, 参数: 密码最小长度 -> 密码最大长度 -> 是否启用特殊字符, 返回: 生成随机密码 */
	function random_passwd ($min_len = 8,$max_len = 16,$special = FALSE){
		$passwd_len = mt_rand($min_len,$max_len);
		$random_str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";	
		$special_str = '!@#$%^&*()_+';
		if($special != FALSE){	
			$random_str .= $special_str;
		}
		$random_passwd = '';
		while (strlen($random_passwd)<$passwd_len){
			$random_passwd .= substr($random_str, (rand()/strlen($random_str)),1);
		}
		return $random_passwd;
	}
	/* 暗月挂Q原创功能: 获取QQ昵称, 参数: QQ号码, 返回: QQ昵称 */
	function getQQNick ($qq){
		$data = text_midtext (file_get_contents ('http://sc41901.s136.cnaaa7.com/api/get_qq/?qq='.$qq),'"nickname":"','"');
		if ($data == Null){
			$data = $qq;
		}
		return htmlentities ($data,ENT_COMPAT,'UTF-8');
	}
	/* 暗月挂Q原创功能: 随机颜色站标, 参数: 无, 返回: 站标代码 */
	function colorhost (){
		$c = array('#000000','#333333','#999999','#CCCCCC','#FF0000','#0000FF','#FFFF00','#00FFFF','#FF00FF','#9933FF','#660000','#003300','#00FF00','#FFCC66','#CC99FF','#669933','#99CCFF','#CCCCCC','#660066');
		$s = count ($c)-1;
		for ($i=0;$i<=2;$i++){
			do {
				$k = $c[rand(0,$s)];
			}while(in_array ($k,$y));
			$y[$i] = $k;
		}
		$d .= '<b style="color: '.$y[0].';">暗</b><b style="color: '.$y[1].';">月</b><b style="color: '.$y[2].';">挂</b>';
		return $d.'<input width="60" height="60" type="image" name="logo" src="Style/Template/default/images/AYGQ.png">';
	}
	/* 暗月挂Q原创功能: 每日一语, 参数: 无, 返回: 文本 */
	function mryi (){
		$data = explode ('<ANYUE>',file_get_contents ('Data/mryi.db'));
		$s = count ($data) - 1;
		$i = rand (0,$s);
		return $data[$i];
	}

	/* 暗月挂Q原创功能: 增加访问次数, 参数: 无, 返回: 无返回数据 */
	function Add_f (){
		$row = mysql_fetch_array(mysql_query ("SELECT * FROM `storage` WHERE `type`='visit'"));
		$row['value']++;
		mysql_query ("UPDATE `storage` SET `value`='{$row['value']}' WHERE `type`='visit'");
	}

	/* 暗月挂Q原创功能: 计算内存使用值, 参数: 无, 返回: 内存使用值 */
	function memory_usage (){
		return (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024,2).' MB';
	}

	function promote ($id,$ip){
		if(!@eRegi($ip,file_get_contents('Data/ip.db'))){
			$adsmysql = mysql_query("SELECT * FROM `user` WHERE `id`='$id'");
			$adsuser = mysql_fetch_array($adsmysql);
			$user = $adsuser['user'];
			if ($user != Null){
				$fp = fopen('Data/ip.db','a+');
				fwrite($fp,$ip.'
');
				fclose($fp);
				$date = date ('Y-m-d H:i:s');
				$tui = $adsuser['tui']+1;
				mysql_query("update `user` set `tui`='$tui' where `id`='$id'");
				mysql_query("INSERT INTO `tui` (`user`,`date`,`ip`) VALUES ('$user','$date','$ip')");
			}
		}
	}

	function wall (){
		$result = mysql_query ('select * from `qqlist` where `qq` order by `id` desc limit 0,35');

		while($row = mysql_fetch_array ($result)){
			$y++;
			$c = 'true';
			$wall .= '
							<td width="39" height="39"><a target="_blank" href="QQ-'.$row['qq'].'.html"><img src="http://q4.qlogo.cn/headimg_dl?bs=qq&dst_uin='.$row['qq'].'&src_uin='.$row['qq'].'&fid='.$row['qq'].'&spec=40&url_enc=0&referer=bu_interface&term_type=PC" width="39px" height="39px" title="QQ：'.$row['qq'].'<br>添加时间：'.date('Y-m-d H:i:s',$row['time']).'" alt="'.$row['qq'].'"></a></td>';
			if ($y < 7){ $wall .= '&nbsp;'; }else{ $y = 0; }
		}

		if ($c != 'true'){
			$wall = '暂无挂Q记录！';
		}

		return $wall;
	}




	function prompt ($text='该页面发生错误！',$title='暗月科技 -&rsaquo; 错误',$code=''){
		echo <<<EOT
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>$title</title>
		$code
		<style type="text/css">
			html {
				background: #f1f1f1;
			}
			body {
				background: #fff;
				color: #444;
				font-family: "Open Sans", sans-serif;
				margin: 2em auto;
				padding: 1em 2em;
				max-width: 700px;
				-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
				box-shadow: 0 1px 3px rgba(0,0,0,0.13);
			}
			h1 {
				border-bottom: 1px solid #dadada;
				clear: both;
				color: #666;
				font: 24px "Open Sans", sans-serif;
				margin: 30px 0 0 0;
				padding: 0;
				padding-bottom: 7px;
			}
			#error-page {
				margin-top: 50px;
			}
			#error-page p {
				font-size: 14px;
				line-height: 1.5;
				margin: 25px 0 20px;
			}
			#error-page code {
				font-family: Consolas, Monaco, monospace;
			}
			ul li {
				margin-bottom: 10px;
				font-size: 14px ;
			}
			a {
				color: #21759B;
				text-decoration: none;
			}
			a:hover {
				color: #D54E21;
			}
			.button {
				background: #f7f7f7;
				border: 1px solid #cccccc;
				color: #555;
				display: inline-block;
				text-decoration: none;
				font-size: 13px;
				line-height: 26px;
				height: 28px;
				margin: 0;
				padding: 0 10px 1px;
				cursor: pointer;
				-webkit-border-radius: 3px;
				-webkit-appearance: none;
				border-radius: 3px;
				white-space: nowrap;
				-webkit-box-sizing: border-box;
				-moz-box-sizing:    border-box;
				box-sizing:         border-box;

				-webkit-box-shadow: inset 0 1px 0 #fff, 0 1px 0 rgba(0,0,0,.08);
				box-shadow: inset 0 1px 0 #fff, 0 1px 0 rgba(0,0,0,.08);
				vertical-align: top;
			}

			.button.button-large {
				height: 29px;
				line-height: 28px;
				padding: 0 12px;
			}

			.button:hover,
			.button:focus {
				background: #fafafa;
				border-color: #999;
				color: #222;
			}

			.button:focus  {
				-webkit-box-shadow: 1px 1px 1px rgba(0,0,0,.2);
				box-shadow: 1px 1px 1px rgba(0,0,0,.2);
			}

			.button:active {
				background: #eee;
				border-color: #999;
				color: #333;
				-webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
				box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
			}

		</style>
	</head>
	<body id="error-page">
		$text
	</body>
</html>
EOT;
	}

	function add_user ($username,$password,$email,$regip){
		$date = date('Y-m-d H:i:s');
		mysql_query ("INSERT INTO `user` (`user`,`pass`,`email`,`regdate`,`regip`) VALUES ('$username','$password','$email','$date','$regip')");
		send_reg_mail ($username,$password,$regip,$date,$email);
	}

	function send_reg_mail ($username,$password,$regip,$date,$email){
		require_once AnYue_Root.'Source/tool/Mdata.php';
		Send_out_Mail ($email,$username.'！注册成功，请查看您的登陆密码！',$text);
	}

	function reg_testing ($user,$Mail,$action){

		if (($user == Null) || ($Mail == Null)){
			return '<p><b>注册失败：</b>用户名或邮箱不能为空，</p><p>5秒后 <a href="index.php?act=reg">跳转页面</a> ！</p><meta http-equiv="refresh" content="5;url=./index.php?act=reg">';
		}

		if ($action){
			session_start();
			require_once (AnYue_Root.'Source/class/Rcs/Captcha.class.php');
			if (Captcha::Verify($_POST['captcha'])){}else{
				return '<p><b>注册失败：</b>验证码输入错误，</p><p>5秒后 <a href="index.php?act=reg">跳转页面</a> ！</p><meta http-equiv="refresh" content="5;url=./index.php?act=reg">';
			}
		}

		if (!@ereg ('^[0-9a-zA-Z\_]*$',$user)){
			return '<p><b>注册失败：</b>用户账户必须为数字或英文字符，</p><p>5秒后 <a href="index.php?act=reg">跳转页面</a> ！</p><meta http-equiv="refresh" content="5;url=./index.php?act=reg">';
		}


		if(!@ereg ('^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*$',$Mail)){
			return '<p><b>注册失败：</b>您输入的邮箱有误，请输入正确邮箱，</p><p>5秒后 <a href="index.php?act=reg">跳转页面</a> ！</p><meta http-equiv="refresh" content="5;url=./index.php?act=reg">';
		}

		if((strlen ($user) < 3) || (strlen ($user) > 15)){
			return '<p><b>注册失败：</b>账号长度必须在3位至15位之间，</p><p>5秒后 <a href="index.php?act=reg">跳转页面</a> ！</p><meta http-equiv="refresh" content="5;url=./index.php?act=reg">';
		}

		$sql = mysql_fetch_array (mysql_query ("SELECT * FROM `user` WHERE `user`='$user'"));
		if ($sql['user'] == $user){
			return '<p><b>注册失败：</b>该账号已注册，</p><p>5秒后 <a href="index.php?act=reg">跳转页面</a> ！</p><meta http-equiv="refresh" content="5;url=./index.php?act=reg">';
		}

		$sql = mysql_fetch_array (mysql_query ("SELECT * FROM `user` WHERE `email`='$Mail'"));
		if($sql['email'] == $Mail){
			return '<p><b>注册失败：</b>该邮箱已注册，</p><p>5秒后 <a href="index.php?act=reg">跳转页面</a> ！</p><meta http-equiv="refresh" content="5;url=./index.php?act=reg">';
		}

	}