| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- if (! defined('IS_INITPHP'))
- exit('Access Denied!');
- /**
- * 微信基础类
- *
- * @author Chen Deben
- * @copyright Meitu
- */
- class mpInit {
- /**
- *
- * @var 接收到的数据
- */
- public $mpobj;
- public function __construct() {
- // $this->valid();
- $this->parseMp();
- }
- public function valid() {
- if ($this->checkSignature()) {
- $echoStr = $_GET['echostr'];
- if ($echoStr != "") {
- echo $echoStr;
- exit();
- }
- } else {
- exit("非法访问1!");
- }
- }
- public function parseMp() {
- $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
- if (! empty($postStr)) {
- libxml_disable_entity_loader(true);
- $this->mpobj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
- } else {
- exit("非法访问2");
- }
- }
- /**
- * 获取用户发过来的指令keyword
- */
- public function getKeyword() {
- return InitPHP::output($this->mpobj->Content);
- }
- /**
- * 获取消息类型
- */
- public function getType() {
- return $this->mpobj->MsgType;
- }
- /**
- * 回复文本消息
- *
- * @param string $msg
- * 消息内容
- */
- public function responseText($msg) {
- $textTpl = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Content><![CDATA[%s]]></Content>
- <FuncFlag>0</FuncFlag>
- </xml>";
- $resultStr = sprintf($textTpl, $this->mpobj->FromUserName, $this->mpobj->ToUserName, time(), "text", $msg);
- echo $resultStr;
- die();
- }
- /**
- * 校验授权
- *
- * @throws TOKEN未定义异常
- * @return boolean
- */
- private function checkSignature() {
- if (! defined("TOKEN")) {
- throw new Exception('TOKEN未定义!');
- }
- $signature = $_GET["signature"];
- $timestamp = $_GET["timestamp"];
- $nonce = $_GET["nonce"];
- $token = TOKEN;
- $tmpArr = array(
- $token,
- $timestamp,
- $nonce
- );
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode($tmpArr);
- $tmpStr = sha1($tmpStr);
- if ($tmpStr == $signature) {
- return true;
- } else {
- return false;
- }
- }
- }
|