<?php
namespace lib;

class MessageSend{
	static public function abnor_sms($qqarr,$qqs,$type=0){
		global $DB,$date,$conf;
		$phone = array();
		foreach ($qqs as $qq){
			if($qqarr[$qq]['phone']!=''){
				$phone[] = $qqarr[$qq]['phone'];
			}
		}
		$phone = array_unique($phone);//去重
		foreach ($phone as $p){
			$ret = send_sms($p,'您的挂机QQ号');
		}
	}
	static public function end_notice($msgarr){//到期预通知
		global $DB,$date;
		foreach($msgarr as $qq => $v){
			$msg = '<html lang="zh-cmn">
	<head><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="format-detection" content="telephone=no"></head>
	<body>
	    <style type="text/css">
	        .qmeml a:link{color:#1d91e5;text-decoration: none;}.qmeml a:hover{text-decoration:none;}.qmeml{background:#ececec;padding:35px 0px;}.qmeml_wrap{width:864px;}
	        p {font-size:14px;line-height:28px;color:#313131;}
	        @media (max-width:884px) {.qmeml_wrap{width:100%;min-width:200px;max-width:864px;font-size:16px;}.qmeml_header{padding:10px 10px 6px;}.qmeml_content{margin:0px!important;}}
	    </style>
	    <div class="qmeml">
	        <table cellpadding="0" align="center" style="overflow:hidden;background:#fff;margin:0 auto;text-align:left;position:relative;font-size:14px;line-height:1.5;box-shadow:0 0 3px #ccc;border:1px solid #ccc;border-radius:5px;border-collapse:collapse;" class="qmeml_wrap">
	            <tr>
	                <th class="qmeml_header" valign="middle" style="height:38px;color:#fff; font-size:14px;line-height:38px; font-weight:bold;text-align:left;padding:10px 24px 6px; border-bottom:1px solid #467ec3;background:#518bcb;border-radius:5px 5px 0 0;">云端QQ机器人</th>
	            </tr>
	            <tr>
	                <td>
	                    <div class="qmeml_content" style="padding:20px 35px 40px;">
	                        <h2 style="text-align:center;">业务即将到期</h2>
	                        <h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">
	                            尊敬的用户：'.$qq.'
	                        </h2>
	                        <h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">
	                            您好!
	                        </h2>
	                        <p style="padding-left:2em;margin:0">'.$v.'</p>
	                        <p style="margin:0;padding:0;margin-top:20px;display:inline-block;width:100%; text-align:right;"><span style="border-bottom:1px dashed #ccc;">'.$date.'</span></p>
	                    </div>
	                </td>
	            </tr>
	        </table>
	    </div>
	</body>
	</html>';
			$isu = send_mail($qq.'@qq.com','即将到期',$msg);
			$times = strtotime('+1day');
			$DB->exec("UPDATE pre_qq SET end_time='{$times}' WHERE qq='{$qq}'");
		}//foreach1
	}

	static public function notice($qqarr,$type=0){
		global $DB,$date,$conf;
		$time = time();
		$rs=$DB->query("SELECT * FROM `pre_qq` WHERE qq in(".implode(',',$qqarr).")");
		while($res = $rs->fetch()){
			$arr = explode('-',$res['out_notify']);
			$count = intval($arr[1]);
			if($time > intval($arr[0]) && 3 > $count){
				if($type){
					$type = '掉线通知';
				}else{
					$type = 'cookie异常通知';
				}
				$content = '检测到您在本站的QQ出现异常<br><br>
				QQ账号：'.$res['qq'].'<br>
				为了您的正常使用请及时更新<br>';

				$msg = '<html lang="zh-cmn">
		<head><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="format-detection" content="telephone=no"></head>
		<body>
		    <style type="text/css">
		        .qmeml a:link{color:#1d91e5;text-decoration: none;}.qmeml a:hover{text-decoration:none;}.qmeml{background:#ececec;padding:35px 0px;}.qmeml_wrap{width:864px;}
		        p {font-size:14px;line-height:28px;color:#313131;}
		        @media (max-width:884px) {.qmeml_wrap{width:100%;min-width:200px;max-width:864px;font-size:16px;}.qmeml_header{padding:10px 10px 6px;}.qmeml_content{margin:0px!important;}}
		    </style>
		    <div class="qmeml">
		        <table cellpadding="0" align="center" style="overflow:hidden;background:#fff;margin:0 auto;text-align:left;position:relative;font-size:14px;line-height:1.5;box-shadow:0 0 3px #ccc;border:1px solid #ccc;border-radius:5px;border-collapse:collapse;" class="qmeml_wrap">
		            <tr>
		                <th class="qmeml_header" valign="middle" style="height:38px;color:#fff; font-size:14px;line-height:38px; font-weight:bold;text-align:left;padding:10px 24px 6px; border-bottom:1px solid #467ec3;background:#518bcb;border-radius:5px 5px 0 0;">'.$conf['sitename'].'</th>
		            </tr>
		            <tr>
		                <td>
		                    <div class="qmeml_content" style="padding:20px 35px 40px;">
		                        <h2 style="text-align:center;">QQ：'.$res['qq'].' '.$type.'</h2>
		                        <h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">
		                            尊敬的用户：'.$res['qq'].'
		                        </h2>
		                        <h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">
		                            您好!
		                        </h2>
		                        <p style="padding-left:2em;margin:0">'.$content.'</p>
		                        <p style="margin:0;padding:0;margin-top:20px;display:inline-block;width:100%; text-align:right;"><span style="border-bottom:1px dashed #ccc;">'.$date.'</span></p>
		                    </div>
		                </td>
		            </tr>
		        </table>
		    </div>
		</body>
		</html>';
				$isu = send_mail($res['qq'].'@qq.com',$type,$msg);
				$times = strtotime('+10minute');
				$count++;
				$out_notify = $times.'-'.$count;
				$DB->exec("UPDATE pre_qq SET out_notify='{$out_notify}' WHERE qq='{$res['qq']}'");
			}
		}//while
	}//public

	static public function frame($sub, $content){
		global $date,$conf;
		$msg = '<html lang="zh-cmn">
<head><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="format-detection" content="telephone=no"></head>
<body>
    <style type="text/css">
        .qmeml a:link{color:#1d91e5;text-decoration: none;}.qmeml a:hover{text-decoration:none;}.qmeml{background:#ececec;padding:35px 0px;}.qmeml_wrap{width:864px;}
        p {font-size:14px;line-height:28px;color:#313131;}
        @media (max-width:884px) {.qmeml_wrap{width:100%;min-width:200px;max-width:864px;font-size:16px;}.qmeml_header{padding:10px 10px 6px;}.qmeml_content{margin:0px!important;}}
    </style>
    <div class="qmeml">
        <table cellpadding="0" align="center" style="overflow:hidden;background:#fff;margin:0 auto;text-align:left;position:relative;font-size:14px;line-height:1.5;box-shadow:0 0 3px #ccc;border:1px solid #ccc;border-radius:5px;border-collapse:collapse;" class="qmeml_wrap">
            <tr>
                <th class="qmeml_header" valign="middle" style="height:38px;color:#fff; font-size:14px;line-height:38px; font-weight:bold;text-align:left;padding:10px 24px 6px; border-bottom:1px solid #467ec3;background:#518bcb;border-radius:5px 5px 0 0;">'.$conf['sitename'].'</th>
            </tr>
            <tr>
                <td>
                    <div class="qmeml_content" style="padding:20px 35px 40px;">
                        <h2 style="text-align:center;">框架异常通知</h2><br>
                        <h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">
                            尊敬的站长
                        </h2>
                        <h2 style="font-weight:bold;margin-bottom:5px;font-size:14px;">
                            您好!
                        </h2>
                        <p style="padding-left:2em;margin:0">[content]</p>
                        <p style="margin:0;padding:0;margin-top:20px;display:inline-block;width:100%; text-align:right;"><span style="border-bottom:1px dashed #ccc;">'.$date.'</span></p>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>';
		$msg = str_replace("[content]",$content,$msg);
		$isu = send_mail($conf['mail_recv'], '框架掉线或假死', $msg);
	}

	static public function send($sendobj, $sub, $sub_nr, $content, $sendname=0){
		global $date,$conf;
		if($sendname == 0)$sendname=$sub;
		$msg = '<html lang="zh-cmn">
<head><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="format-detection" content="telephone=no"></head>
<body>
    <style type="text/css">
        .qmeml a:link{color:#1d91e5;text-decoration: none;}.qmeml a:hover{text-decoration:none;}.qmeml{background:#ececec;padding:35px 0px;}.qmeml_wrap{width:864px;}
        p {font-size:14px;line-height:28px;color:#313131;}
        @media (max-width:884px) {.qmeml_wrap{width:100%;min-width:200px;max-width:864px;font-size:16px;}.qmeml_header{padding:10px 10px 6px;}.qmeml_content{margin:0px!important;}}
    </style>
    <div class="qmeml">
        <table cellpadding="0" align="center" style="overflow:hidden;background:#fff;margin:0 auto;text-align:left;position:relative;font-size:14px;line-height:1.5;box-shadow:0 0 3px #ccc;border:1px solid #ccc;border-radius:5px;border-collapse:collapse;" class="qmeml_wrap">
            <tr>
                <th class="qmeml_header" valign="middle" style="height:38px;color:#fff; font-size:22px;line-height:38px; font-weight:bold;text-align:left;padding:10px 24px 6px; border-bottom:1px solid #467ec3;background:#518bcb;border-radius:5px 5px 0 0; text-align:center;">'.$sub_nr.'</th>
            </tr>
            <tr>
                <td>
                    <div class="qmeml_content" style="padding:15px;">
                        <p style="padding-left:2em;margin:0">[content]</p>
                        <p style="margin:0;padding:0;margin-top:20px;display:inline-block;width:100%; text-align:right;"><span style="border-bottom:1px dashed #ccc;">'.$date.'</span></p>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</body>
</html>';
		$msg = str_replace("[content]",$content,$msg);
		$ret = send_mail($sendobj, $sub, $msg, $sendname);
		if(isset($ret['code']) && $ret['code'] == 1){
			return true;
		}else{
			return false;
		}
	}
}
