<?php
/* versionbeta:name
 * versionbeta:number
 * versionbeta:site
 * versionbeta:pubtime
 */
include_once dirname(__FILE__) . "/weixin/wxBizMsgCrypt.php";
class cls_weixin_open {
	static function save_notice($arr) {
		$arr_configv = cls_config::get("","weixin",'','');
		$arr_config = cls_config::get("","weixin.open",'','');
		$cont = file_get_contents("php://input");
		// 第三方发送消息给公众平台
		$timeStamp = $_GET['timestamp'];
		$nonce = $_GET['nonce'];
		$sign = $_GET['msg_signature'];
		$xml = file_get_contents("php://input");
		$pc = new WXBizMsgCrypt($arr_config['token'], $arr_config['aeskey'], $arr_config['appid']);
		$msg = '';
		/*
		$errCode = $pc->decryptMsg($sign, $timeStamp, $nonce, $xml, $msg);
		if ($errCode == 0) {
			$request = simplexml_load_string($msg , 'SimpleXmlElement' , LIBXML_NOCDATA);
			$arr = fun_format::toarray($request);
			cls_cache::set($arr , "verify_ticket" , "weixin/open");
		} else {
			
		}
		*/
		$arr = array(
			"timeStamp" => $timeStamp,
			"nonce" => $nonce,
			"xml" => $xml,
			"sign" => $sign,
		);
		$arrx = fun_base::post("http://aliai.kjcms.cn/wxopen/index.php",$arr);
		if($arrx['code']==0 && !empty($arrx['cont'])) {
			$request = simplexml_load_string($arrx['cont'] , 'SimpleXmlElement' , LIBXML_NOCDATA);
			$arr_msg = fun_format::toarray($request);
			$appid = isset($arr_msg['AuthorizerAppid']) ? $arr_msg['AuthorizerAppid'] : $arr_configv['appid'];
			if($arr_msg['InfoType']=='component_verify_ticket') {
				cls_cache::set($arr_msg , "verify_ticket_" . $appid , "weixin/open",1);
			} else if($arr_msg['InfoType']=='unauthorized') {
				cls_cache::set($arr_msg , "unauthorized_" . $appid , "weixin/open",1);
			} else if($arr_msg['InfoType']=='authorized') {
				cls_cache::set($arr_msg , "authorized_" . $appid , "weixin/open",1);
			} else if($arr_msg['InfoType']=='updateauthorized') {
				cls_cache::set($arr_msg , "updateauthorized_" . $appid , "weixin/open",1);
			} else {
				cls_cache::set($arrx , "verify_ticket2" , "tmp");
				cls_cache::set($arr , "verify_ticket" , "tmp");
			}
		} else {
			cls_cache::set($arrx , "verify_ticket2" , "tmp");
			cls_cache::set($arr , "verify_ticket" , "tmp");
		}
	}
	static function get_ticket() {
		$arr_configv = cls_config::get("","weixin",'','');
		$arr = cls_cache::get("verify_ticket_" . $arr_configv['appid'] , "weixin/open",0);
		$val = isset($arr['ComponentVerifyTicket']) ? $arr['ComponentVerifyTicket'] : "";
		return $val;
	}
	static function get_access_token() {
		$arr_configv = cls_config::get("","weixin",'','');
		$access_token = cls_cache::get("access_token_" . $arr_configv['appid'] , "weixin/open",90);
		if(empty($access_token)) {
			$arr_config = cls_config::get("","weixin.open",'','');
			$url = "https://api.weixin.qq.com/cgi-bin/component/api_component_token";
			$ticket = self::get_ticket();
			$arr_val = array(
				"component_appid" => $arr_config['appid'],
				"component_appsecret" => $arr_config["secret"],
				"component_verify_ticket" => $ticket
			);
			$arr = fun_base::post($url,json_encode($arr_val));
			if($arr['code']==0 && !empty($arr['cont'])) {
				$arr = (array)json_decode($arr['cont']);
				if(isset($arr['component_access_token'])) $access_token = $arr['component_access_token'];
				if(!empty($access_token)) cls_cache::set($access_token , "access_token_" . $arr_configv['appid'] , "weixin/open");
			}
		}
		return $access_token;
	}
	static function get_auth_code() {
		$arr_configv = cls_config::get("","weixin",'','');
		$arr_config = cls_config::get("","weixin.open",'','');
		$code = cls_cache::get("auth_code_" . $arr_configv['appid'] , "weixin/open",8);
		if(empty($code)) {
			$token = self::get_access_token();
			$url = "https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" . $token;
			$arr_val = array("component_appid"=>$arr_config['appid']);
			$arr = fun_base::post($url,json_encode($arr_val));
			if($arr['code']==0 && !empty($arr['cont'])) {
				$arr = (array)json_decode($arr['cont']);
				if(isset($arr['pre_auth_code'])) $code = $arr['pre_auth_code'];
				if(!empty($code)) cls_cache::set($code , "auth_code_" . $arr_configv['appid'] , "weixin/open");
			}
		}
		return $code;
	}
	function get_authorizer_token(){
		$arr_configv = cls_config::get("","weixin",'','');
		$arr_cache = cls_cache::get("authorizer_token_" . $arr_configv['appid'] , "weixin/open",0);
		$arr_config = cls_config::get("","weixin.open",'','');
		if(empty($arr_cache)) {
			$token = self::get_access_token();
			$url = "https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=" . $token;
			$code = cls_cache::get( "authCode_" . $arr_configv['appid'] , "weixin/open",0);
			$arr_val = array("component_appid"=>$arr_config['appid'],'authorization_code'=>$code);
			$arr = fun_base::post($url,json_encode($arr_val));
			if($arr['code']==0 && !empty($arr['cont'])) {
				$arr = fun_format::toarray(json_decode($arr['cont']));
				if(isset($arr['authorization_info']) && isset($arr['authorization_info']['authorizer_access_token'])) $arr = $arr['authorization_info'];
				if(isset($arr['authorizer_access_token'])) {
					$code = $arr['authorizer_access_token'];
					$arr['overtime'] = TIME+(int)$arr['expires_in'];
					cls_cache::set($arr , "authorizer_token_" . $arr_configv['appid'] , "weixin/open",1);
				}
			}
		} else if($arr_cache['overtime']<TIME) {
			$token = self::get_access_token();
			$url = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=" . $token;
			$arr_val = array(
				"component_appid"=>$arr_config['appid'],
				'authorizer_appid'=>$arr_configv['appid'],
				'authorizer_refresh_token'=>$arr_cache['authorizer_refresh_token']
			);
			$arr = fun_base::post($url,json_encode($arr_val));
			if($arr['code']==0 && !empty($arr['cont'])) {
				$arr = (array)json_decode($arr['cont']);
				if(isset($arr['authorizer_access_token'])) {
					$arr_cache['authorizer_access_token'] = $arr['authorizer_access_token'];
					$arr_cache['authorizer_refresh_token'] = $arr['authorizer_refresh_token'];
					$arr_cache['overtime'] = TIME + (int)$arr['expires_in'];
					$code = $arr['authorizer_access_token'];
					cls_cache::set($arr_cache , "authorizer_token_" . $arr_configv['appid'] , "weixin/open",1);
					cls_cache::set($arr , "authorizer_token_" . $arr_configv['appid'] . "_back" , "weixin/open",1);
				}
			}
		} else {
			$code = $arr_cache['authorizer_access_token'];
		}
		return $code;
	}
	static function get_jsapi_ticket(){
		$arr_configv = cls_config::get("","weixin",'','');
		$jsapi_ticket = cls_cache::get("jsapi_ticket_" . $arr_configv['appid'] , 'weixin/open' , 100);
		if(empty($jsapi_ticket)) {
			$access_token = self::get_authorizer_token();
			if(empty($access_token)) return $jsapi_ticket;
			$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $access_token . "&type=jsapi";
			$arr = fun_base::post($url , array() , array() , 'GET');
			if($arr['code'] != 0 || empty($arr['cont']) ) return $jsapi_ticket;
			$cont = $arr['cont'];
			$arr_json = fun_format::toarray($cont);
			if(isset($arr_json['ticket'])) {
				$jsapi_ticket = $arr_json['ticket'];
				cls_cache::set($jsapi_ticket , "jsapi_ticket_" . $arr_configv['appid'] , 'weixin/open');
			}
		}
		return $jsapi_ticket;
	}
	static function sign_js($url) {
		$ticket = self::get_jsapi_ticket();
		$arr = array(
			'jsapi_ticket' => $ticket,
			'timestamp' => TIME,
			'noncestr' => TIME . rand(10000,99999),
			'url' => $url,
		);
		ksort($arr);
		$arr_x = array();
		foreach($arr as $item=>$key) {
			if(empty($key)) continue;
			$arr_x[] = $item . "=" . $key;
		}
		$str = implode("&" , $arr_x);
		$arr['sign'] = sha1($str);
		$arr["appid"] = cls_weixin::get_perms("appid");
		$arr['code'] = 0;
		return $arr;
	}
}