<?php
defined('PP_PATH') or exit;
class mmisc extends model {
	//邮件发送
	public function sendmail($username, $email, $subject, $message) {
		$mailconf = $this->kv->get('mail_cfg');
	
		if( empty($mailconf) ) return '未获取到邮件配置信息';
		
		if($mailconf['sendtype'] == 0) {
			$subject = iconv('UTF-8', 'GBK', $subject);
			$message = iconv('UTF-8', 'GBK', $message);
			mail($email, $subject, $message, NULL, NULL);			
		} elseif($mailconf['sendtype'] == 1) {			
			// $key = array_rand($mailconf['smtplist']);			
			// $smtp = $mailconf['smtplist'][$key];
			$smtp = $mailconf['smtplist'];
				
			$message = str_replace("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />", "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\" />", $message);
			$subject = iconv('UTF-8', 'GBK', $subject);
			$message = iconv('UTF-8', 'GBK', $message);
			$username = iconv('UTF-8', 'GBK', $username);
			return pp_mail::send($smtp, $username, $email, $subject, $message);				
		}
	}
	//百度推送
	public function baidup($urls,$api){
		//$api = 'http://data.zz.baidu.com/urls?site=www.baidu.com&token=tiKWEQWPjFn8VIj4';
		if(empty($api)) return false;
		$ch = curl_init();
		$options =  array(
			CURLOPT_URL => $api,
			CURLOPT_POST => true,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_POSTFIELDS => implode("\n", $urls),
			CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
		);
		curl_setopt_array($ch, $options);
		$result = curl_exec($ch);
		return json_decode($result,true);
	}
}
