public function index($ApiData = '') { if ($ApiData != '') { $_GET = $ApiData; } $this->ali = 0; if (isset($_GET['ali']) && intval($_GET['ali'])) { $this->ali = 1; } $this->siteUrl = C('site_url'); if (!class_exists('SimpleXMLElement')) { exit('SimpleXMLElement class not exist'); } if (!function_exists('dom_import_simplexml')) { exit('dom_import_simplexml function not exist'); } if (empty($_GET['token']) && isset($_GET['appid'])) { $appid = ltrim($_GET['appid'], '/'); $this->token = M('Wxuser')->where('appid=\'' . $appid . '\'')->getField('token'); } else { $this->token = htmlspecialchars($_GET['token']); } if ($appid == 'wx570bc396a51b8ff8') { $wxarr['type'] = 1; $wxarr['encode'] = 2; $oauth = new Wechat($appid, $wxarr); $apiOauth = new apiOauth(); $data = $oauth->request(); $openid = $data['FromUserName']; $ToUserName = $data['ToUserName']; switch ($data['MsgType']) { case 'text': if ($data['Content'] == 'TESTCOMPONENT_MSG_TYPE_TEXT') { $content = 'TESTCOMPONENT_MSG_TYPE_TEXT_callback'; $oauth->response($content); } else if (strstr($data['Content'], 'QUERY_AUTH_CODE')) { $auth_code = str_replace('QUERY_AUTH_CODE:', '', $data['Content']); $authorization_info = $apiOauth->get_authorization_info($auth_code); $access_token = $authorization_info['authorizer_access_token']; $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=' . $access_token; $content = $auth_code . '_from_api'; $call = '{' . "\r\n" . ' "touser":"******",' . "\r\n" . ' "msgtype":"text",' . "\r\n" . ' "text":' . "\r\n" . ' {' . "\r\n" . ' "content":"' . $content . '"' . "\r\n" . ' }' . "\r\n" . ' }'; $apiOauth->https_request($url, $call); } break; case 'event': $content = $data['Event'] . 'from_callback'; $oauth->response($content); break; } exit(); } if (($this->token == 'pigcms') || ($this->token == 'qcloud')) { $qcloudUser = M('Qcloud_user')->where(array('openid' => $_GET['openId']))->find(); $this->token = $qcloudUser['token']; } if (!preg_match('/^[0-9a-zA-Z]{3,42}$/', $this->token)) { exit('error token'); } if (!$this->ali) { $weixin = new Wechat($this->token, $this->wxuser); } $this->wxuser = S('wxuser_' . $this->token); if (!$this->wxuser || 1) { $this->wxuser = D('Wxuser')->where(array('token' => $this->token))->find(); if (C('agent_version') && intval($this->wxuser['agentid'])) { $thisAgent = M('Agent')->where(array('id' => $this->wxuser['agentid']))->find(); $this->siteUrl = $thisAgent['siteurl']; } S('wxuser_' . $this->token, $this->wxuser); } $this->user = M('Users')->where(array('id' => $this->wxuser['uid']))->find(); if (!$this->ali) { $data = $weixin->request(); $this->data = $weixin->request(); } $this->fans = S('fans_' . $this->token . '_' . $this->data['FromUserName']); if (!$this->fans || 1) { $this->fans = M('Userinfo')->where(array('token' => $this->token, 'wecha_id' => $this->data['FromUserName']))->find(); S('fans_' . $this->token . '_' . $this->data['FromUserName'], $this->fans); } $session_openid_name = 'token_openid_' . $this->token; $_SESSION[$session_openid_name] = $this->data['FromUserName']; $this->my = C('site_my'); $this->apiServer = apiServer::getServerUrl(); $open = M('Token_open')->where(array('token' => $this->token))->find(); $this->fun = $open['queryname']; if (!$this->ali) { list($content, $type) = $this->reply($data); $weixin->response($content, $type); } else { $data = array(); $data['Content'] = htmlspecialchars($_GET['keyword']); $data['FromUserName'] = htmlspecialchars($_GET['fromUserName']); $data['FromUserName'] = '******' . md5($data['FromUserName']); if (isset($_GET['eventType']) && $_GET['eventType']) { $data['Event'] = trim(htmlspecialchars($_GET['eventType'])); if ($data['Event'] == 'SCAN') { $data['EventKey'] = $_GET['EventKey']; $eventReplyClassName = 'SCANEventReply'; class_exists($eventReplyClassName); $SCANEventReply = new $eventReplyClassName($this->token, $data['FromUserName'], $data, $this->siteUrl, $this->ali); return $SCANEventReply->index(); } } $this->data = $data; return $this->reply($data); } }
public function oauth_back() { $ac_id = intval($_GET["ac_id"]); $auth_code = $_GET["auth_code"]; $expires_in = $_GET["expires_in"]; if (!empty($auth_code) && !empty($expires_in)) { $apiOauth = new apiOauth(); $authorization_info = $apiOauth->get_authorization_info($auth_code); $authorizer_info = $apiOauth->get_authorizer_info($authorization_info["authorizer_appid"]); $appid = $authorization_info["authorizer_appid"]; $where = array("uid" => session("uid")); if (!empty($ac_id)) { $where["id"] = $ac_id; } else { $where["appid"] = $appid; } $wxinfo = M("Wxuser")->where($where)->find(); if ($wxinfo) { $save = array(); $save["type"] = 1; $save["encode"] = 2; $save["wxid"] = $authorizer_info["user_name"]; $save["wxname"] = $authorizer_info["nick_name"]; $save["weixin"] = $authorizer_info["alias"]; $save["headerpic"] = empty($authorizer_info["head_img"]) ? "" : $authorizer_info["head_img"]; $service_type = $authorizer_info["service_type_info"]["id"]; $verify_type = $authorizer_info["verify_type_info"]["id"]; if (($service_type == 0 || $service_type == 1) && $verify_type == 0) { $res["winxintype"] = 4; } else { if ($service_type == 2 && $verify_type == 0) { $res["winxintype"] = 3; } else { if ($service_type == 2 && $verify_type == -1) { $save["winxintype"] = 2; } else { if (($service_type == 0 || $service_type == 1) && $verify_type == -1) { $save["winxintype"] = 1; } } } } $save["appid"] = $authorization_info["authorizer_appid"]; $save["authorizer_access_token"] = $authorization_info["authorizer_access_token"]; $save["authorizer_refresh_token"] = $authorization_info["authorizer_refresh_token"]; $save["authorizer_expires"] = $authorization_info["expires_in"] + time(); if (M("Wxuser")->where($where)->save($save)) { $update = array("appid" => $save["appid"] . "_no", "authorizer_access_token" => "", "authorizer_refresh_token" => "", "authorizer_expires" => 0); M("Wxuser")->where("appid = '{$save["appid"]}' AND id != {$wxinfo["id"]}")->save($update); $status = true; } } else { $status = $this->add_authorizer($authorizer_info, $authorization_info); } if ($status) { $this->success("公众号授权成功", U("Index/index")); } else { $this->error("公众号授权失败", U("Index/index")); } } else { $this->error("授权错误", U("Index/oauth")); } }
public function oauth_back() { $ac_id = intval($_GET['ac_id']); $auth_code = $_GET['auth_code']; $expires_in = $_GET['expires_in']; if (!empty($auth_code) && !empty($expires_in)) { $apiOauth = new apiOauth(); $authorization_info = $apiOauth->get_authorization_info($auth_code); $authorizer_info = $apiOauth->get_authorizer_info($authorization_info['authorizer_appid']); $appid = $authorization_info['authorizer_appid']; $where = array('uid' => session('uid')); if (!empty($ac_id)) { $where['id'] = $ac_id; } else { $where['appid'] = $appid; } //file_put_contents('authorization_info.txt',json_encode($authorization_info)); //file_put_contents('authorizer_info.txt',json_encode($authorizer_info)); $wxinfo = M('Wxuser')->where($where)->find(); if ($wxinfo) { $save = array(); $save['type'] = 1; $save['encode'] = 2; $save['wxid'] = $authorizer_info['user_name']; $save['wxname'] = $authorizer_info['nick_name']; $save['weixin'] = $authorizer_info['alias']; $save['headerpic'] = empty($authorizer_info['head_img']) ? '' : $authorizer_info['head_img']; $service_type = $authorizer_info['service_type_info']['id']; $verify_type = $authorizer_info['verify_type_info']['id']; if (($service_type == 0 || $service_type == 1) && $verify_type == 0) { $res['winxintype'] = 4; } else { if ($service_type == 2 && $verify_type == 0) { $res['winxintype'] = 3; } else { if ($service_type == 2 && $verify_type == -1) { $save['winxintype'] = 2; } else { if (($service_type == 0 || $service_type == 1) && $verify_type == -1) { $save['winxintype'] = 1; } } } } $save['appid'] = $authorization_info['authorizer_appid']; $save['authorizer_access_token'] = $authorization_info['authorizer_access_token']; $save['authorizer_refresh_token'] = $authorization_info['authorizer_refresh_token']; $save['authorizer_expires'] = $authorization_info['expires_in'] + time(); if (M('Wxuser')->where($where)->save($save)) { $update = array('appid' => $save['appid'] . '_no', 'authorizer_access_token' => '', 'authorizer_refresh_token' => '', 'authorizer_expires' => 0); M('Wxuser')->where("appid = '{$save['appid']}' AND id != {$wxinfo['id']}")->save($update); $status = true; } } else { $status = $this->add_authorizer($authorizer_info, $authorization_info); } if ($status) { $this->success('公众号授权成功', U('Index/index')); } else { $this->error('公众号授权失败', U('Index/index')); } } else { $this->error('授权错误', U('Index/oauth')); } }
public function index($ApiData = '') { if ($ApiData != "") { $_GET = $ApiData; } $this->ali = 0; if (isset($_GET["ali"]) && intval($_GET["ali"])) { $this->ali = 1; } $this->siteUrl = C("site_url"); if (!class_exists("SimpleXMLElement")) { exit("SimpleXMLElement class not exist"); } if (!function_exists("dom_import_simplexml")) { exit("dom_import_simplexml function not exist"); } if (empty($_GET["token"]) && isset($_GET["appid"])) { $appid = ltrim($_GET["appid"], "/"); $this->token = M("Wxuser")->where("appid='{$appid}'")->getField("token"); } else { $this->token = htmlspecialchars($_GET["token"]); } if ($appid == "wx570bc396a51b8ff8") { $wxarr["type"] = 1; $wxarr["encode"] = 2; $oauth = new Wechat($appid, $wxarr); $apiOauth = new apiOauth(); $data = $oauth->request(); $openid = $data["FromUserName"]; $ToUserName = $data["ToUserName"]; switch ($data["MsgType"]) { case "text": if ($data["Content"] == "TESTCOMPONENT_MSG_TYPE_TEXT") { $content = "TESTCOMPONENT_MSG_TYPE_TEXT_callback"; $oauth->response($content); } else { if (strstr($data["Content"], "QUERY_AUTH_CODE")) { $auth_code = str_replace("QUERY_AUTH_CODE:", "", $data["Content"]); $authorization_info = $apiOauth->get_authorization_info($auth_code); $access_token = $authorization_info["authorizer_access_token"]; $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token; $content = $auth_code . "_from_api"; $call = "{\r\n\t\t\t\t\t\t\t\t\t\"touser\":\"" . $openid . "\",\r\n\t\t\t\t\t\t\t\t\t\"msgtype\":\"text\",\r\n\t\t\t\t\t\t\t\t\t\"text\":\r\n\t\t\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\t\t\t \"content\":\"" . $content . "\"\r\n\t\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t\t}"; $apiOauth->https_request($url, $call); } } break; case "event": $content = $data["Event"] . "from_callback"; $oauth->response($content); break; } exit; } if ($this->token == "weimicms" || $this->token == "qcloud") { $qcloudUser = M("Qcloud_user")->where(array("openid" => $_GET["openId"]))->find(); $this->token = $qcloudUser["token"]; } if (!preg_match("/^[0-9a-zA-Z]{3,42}\$/", $this->token)) { exit("error token"); } if (!$this->ali) { $weixin = new Wechat($this->token, $this->wxuser); } $this->wxuser = S("wxuser_" . $this->token); if (!$this->wxuser || 1) { $this->wxuser = D("Wxuser")->where(array("token" => $this->token))->find(); if (C("agent_version") && intval($this->wxuser["agentid"])) { $thisAgent = M("Agent")->where(array("id" => $this->wxuser["agentid"]))->find(); $this->siteUrl = $thisAgent["siteurl"]; } S("wxuser_" . $this->token, $this->wxuser); } $this->user = M("Users")->where(array("id" => $this->wxuser["uid"]))->find(); if (!$this->ali) { $data = $weixin->request(); $this->data = $weixin->request(); } $this->mykey = trim(C('server_key')); $this->chatkey = trim(C('site_chatkey')); $weixin = new Wechat($this->token); $data = $weixin->request(); $this->data = $weixin->request(); if ($this->data) { //自定义机器人名字 $this->fans = S("fans_" . $this->token . "_" . $this->data["FromUserName"]); if (!$this->fans || 1) { $this->fans = M("Userinfo")->where(array("token" => $this->token, "wecha_id" => $this->data["FromUserName"]))->find(); S("fans_" . $this->token . "_" . $this->data["FromUserName"], $this->fans); } $session_openid_name = "token_openid_" . $this->token; $_SESSION[$session_openid_name] = $this->data["FromUserName"]; $this->my = C("site_my"); $this->apiServer = apiServer::getServerUrl(); $open = M("Token_open")->where(array("token" => $this->token))->find(); $this->fun = $open["queryname"]; if (!$this->ali) { list($content, $type) = $this->reply($data); $weixin->response($content, $type); } else { $data = array(); $data["Content"] = htmlspecialchars($_GET["keyword"]); $data["FromUserName"] = htmlspecialchars($_GET["fromUserName"]); $data["FromUserName"] = "******" . md5($data["FromUserName"]); if (isset($_GET["eventType"]) && $_GET["eventType"]) { $data["Event"] = trim(htmlspecialchars($_GET["eventType"])); if ($data["Event"] == "SCAN") { $data["EventKey"] = $_GET["EventKey"]; $eventReplyClassName = "SCANEventReply"; class_exists($eventReplyClassName); $SCANEventReply = new $eventReplyClassName($this->token, $data["FromUserName"], $data, $this->siteUrl, $this->ali); return $SCANEventReply->index(); } } $this->data = $data; return $this->reply($data); } } }