<?php ini_set ( \'date.timezone\', \'Asia/Shanghai\' ); define ( \'IN_ASK2\', TRUE ); $http_type = ((isset ( $_SERVER [\'HTTPS\'] ) && $_SERVER [\'HTTPS\'] == \'on\') || (isset ( $_SERVER [\'HTTP_X_FORWARDED_PROTO\'] ) && $_SERVER [\'HTTP_X_FORWARDED_PROTO\'] == \'https\')) ? \'https://\' : \'https://\'; if (is_https ()) { define ( \'SITE_URL\', \'https://\' . $_SERVER [\'HTTP_HOST\'] . \'/\' ); } else { define ( \'SITE_URL\', \'http://\' . $_SERVER [\'HTTP_HOST\'] . \'/\' ); } define ( \'ENVIRONMENT\', isset ( $_SERVER [\'CI_ENV\'] ) ? $_SERVER [\'CI_ENV\'] : \'production\' ); define ( \'FC_PATH\', dirname ( dirname ( dirname ( __FILE__ ) ) ) . DIRECTORY_SEPARATOR ); define ( \'BASEPATH\', FC_PATH . \'system\' ); require FC_PATH . \'lib/db_mysqli.php\'; $wechatObj = new wechatCallbackapiTest (); if (! isset ( $_GET [\'echostr\'] )) { $wechatObj->responseMsg (); } else { $wechatObj->valid (); } //判断是否是https function is_https() { if (! empty ( $_SERVER [\'HTTPS\'] ) && strtolower ( $_SERVER [\'HTTPS\'] ) !== \'off\') { return TRUE; } elseif (isset ( $_SERVER [\'HTTP_X_FORWARDED_PROTO\'] ) && $_SERVER [\'HTTP_X_FORWARDED_PROTO\'] === \'https\') { return TRUE; } elseif (! empty ( $_SERVER [\'HTTP_FRONT_END_HTTPS\'] ) && strtolower ( $_SERVER [\'HTTP_FRONT_END_HTTPS\'] ) !== \'off\') { return TRUE; } return FALSE; } class wechatCallbackapiTest { var $db; var $token; function wechatCallbackapiTest() { $this->init_db (); $this->token = $this->getoken (); } function init_db() { require FC_PATH . \'application\' . DIRECTORY_SEPARATOR . \'config\' . DIRECTORY_SEPARATOR . \'database.php\'; $dbconfig = $db [\'default\']; $db = new db (); define ( \'DB_TABLEPRE\', $dbconfig [\'dbprefix\'] ); $config = array (); $config [\'hostname\'] = $dbconfig [\'hostname\']; $config [\'username\'] = $dbconfig [\'username\']; $config [\'password\'] = $dbconfig [\'password\']; $config [\'database\'] = $dbconfig [\'database\']; $config [\'charset\'] = $dbconfig [\'char_set\']; $config [\'autoconnect\'] = 1; $config [\'dbport\'] = 3306; $config [\'debug\'] = true; $db->open ( $config ); $this->db = $db; } function getoken() { $wxtoken = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "setting where k=\'wxtoken\' limit 0,1" ); return trim ( $wxtoken [\'v\'] ); } //验证签名 public function valid() { $echoStr = $_GET ["echostr"]; $signature = $_GET ["signature"]; $timestamp = $_GET ["timestamp"]; $nonce = $_GET ["nonce"]; $token = $this->token; $tmpArr = array ($token, $timestamp, $nonce ); sort ( $tmpArr, SORT_STRING ); $tmpStr = implode ( $tmpArr ); $tmpStr = sha1 ( $tmpStr ); if ($tmpStr == $signature) { echo $echoStr; exit (); } } //响应消息 public function responseMsg() { $postStr = $GLOBALS ["HTTP_RAW_POST_DATA"]? $GLOBALS ["HTTP_RAW_POST_DATA"]:file_get_contents(\'php://input\'); if (! empty ( $postStr )) { $this->logger ( "R \r\n" . $postStr ); $postObj = simplexml_load_string ( $postStr, \'SimpleXMLElement\', LIBXML_NOCDATA ); $RX_TYPE = trim ( $postObj->MsgType ); if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")) { //过滤关注和取消关注事件 } else { } //消息类型分离 switch ($RX_TYPE) { case "event" : $result = $this->receiveEvent ( $postObj ); break; case "text" : $result = $this->receiveText ( $postObj ); break; case "image" : $result = $this->receiveImage ( $postObj ); break; case "location" : $result = $this->receiveLocation ( $postObj ); break; case "voice" : $result = $this->receiveVoice ( $postObj ); break; case "video" : $result = $this->receiveVideo ( $postObj ); break; case "link" : $result = $this->receiveLink ( $postObj ); break; default : $result = "unknown msg type: " . $RX_TYPE; break; } $this->logger ( "T \r\n" . $result ); echo $result; } else { echo ""; exit (); } } //接收事件消息 private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe" : $site = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "weixin_info limit 0,1" ); $content = $site [\'msg\']; $content .= (! empty ( $object->EventKey )) ? ("\n来自二维码场景 " . str_replace ( "qrscene_", "", $object->EventKey )) : ""; break; case "unsubscribe" : $content = "取消关注"; break; case "CLICK" : $content = $this->getcontent ( $object->EventKey ); break; case "VIEW" : $content = "跳转链接 " . $object->EventKey; break; case "SCAN" : $content = "扫描场景 " . $object->EventKey; break; case "LOCATION" : // $content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude; break; case "scancode_waitmsg" : if ($object->ScanCodeInfo->ScanType == "qrcode") { $content = "扫码带提示:类型 二维码 结果:" . $object->ScanCodeInfo->ScanResult; } else if ($object->ScanCodeInfo->ScanType == "barcode") { $codeinfo = explode ( ",", strval ( $object->ScanCodeInfo->ScanResult ) ); $codeValue = $codeinfo [1]; $content = "扫码带提示:类型 条形码 结果:" . $codeValue; } else { $content = "扫码带提示:类型 " . $object->ScanCodeInfo->ScanType . " 结果:" . $object->ScanCodeInfo->ScanResult; } break; case "scancode_push" : $content = "扫码推事件"; break; case "pic_sysphoto" : $content = "系统拍照"; break; case "pic_weixin" : $content = "相册发图:数量 " . $object->SendPicsInfo->Count; break; case "pic_photo_or_album" : $content = "拍照或者相册:数量 " . $object->SendPicsInfo->Count; break; case "location_select" : $content = "发送位置:标签 " . $object->SendLocationInfo->Label; break; default : // $content = "receive a new event: ".$object->Event; break; } if($object->Event==\'subscribe\'||$object->Event==\'SCAN\'){ $myopenid = sprintf($object->FromUserName); $mytoken = sprintf($object->EventKey); $user = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "user where openid=\'$myopenid\' limit 0,1" ); if($object->Event==\'SCAN\'){ $type=1; $content = \'\'; }else{ $content = $site [\'msg\']; $type=0; $mytoken = substr($mytoken,-9); } if(empty($user)){ $uid=0; $content .= "请先注册 "; }else{ $uid=$user[\'uid\']; if(empty($user[\'phone\'])){ $content .= "请验证手机号"; }else{ $content .= "欢迎登陆 "; } } $time = time(); $this->db->query ( "INSERT INTO " . DB_TABLEPRE . "user_loginjilu(openid,uid,token,type,time) values (\'$myopenid\',\'$uid\',\'$mytoken\',\'$type\',$time)" ); $infoid = $this->db->insert_id (); // file_put_contents("text.txt", var_export($infoid,true)."\r\n",FILE_APPEND); } if (is_array ( $content )) { if (isset ( $content [0] [\'PicUrl\'] )) { $result = $this->transmitNews ( $object, $content ); } else if (isset ( $content [\'MusicUrl\'] )) { $result = $this->transmitMusic ( $object, $content ); } } else { if ($content == "") { $site = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "weixin_info limit 0,1" ); $content = $site [\'unword\']; } $result = $this->transmitText ( $object, $content ); } return $result; } private function getcontent($keyword, $object = null) { // runlog ( \'test\', $keyword ); $keys = array (); $kcontent = array (); $content = \'\'; $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords order by id desc LIMIT 0,1000" ); while ( $key = $this->db->fetch_array ( $query ) ) { //精准匹配 if ($key [\'showtype\'] == 1) { if ($keyword == $key [\'txtname\']) { //系统关键词 if ($key [\'txttype\'] == 1) { switch (trim ( $key [\'txtcontent\'] )) { case \'#最新问题#\' : $content = $this->newquestion (); if (count ( $content ) <= 0) { $content = "没有最新问题推荐哟"; } break; case \'#热门问题#\' : $content = $this->hotquestion (); if (count ( $content ) <= 0) { $content = "没有热门问题推荐哟"; } break; case \'#最新文章#\' : $content = $this->newblog (); if (count ( $content ) <= 0) { $content = "没有最新文章推荐哟"; } break; case \'#站长推荐#\' : $content = $this->hotblog (); if (count ( $content ) <= 0) { $content = "没有站长推荐的文章哟"; } break; case \'#附近的人#\' : break; case \'#附近的问题#\' : break; } } else { if (! empty ( $key [\'title\'] ) && $key [\'title\'] != \'\') { $sql = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords where txtname=\'$keyword\' order by id desc LIMIT 0,9" ); while ( $topic = $this->db->fetch_array ( $sql ) ) { if (strstr ( $topic [\'wburl\'], \'http:\' )) { $kcontent [] = array ("Title" => $topic [\'title\'], "Description" => $topic [\'txtcontent\'], "PicUrl" => SITE_URL . $topic [\'fmtu\'], "Url" => $topic [\'wburl\'] ); } else { $kcontent [] = array ("Title" => $topic [\'title\'], "Description" => $topic [\'txtcontent\'], "PicUrl" => SITE_URL . $topic [\'fmtu\'], "Url" => SITE_URL . \'index.php?topic/getone/\' . $topic [\'wzid\'] . \'.html\' ); } } } else { $content = $key [\'txtcontent\']; } } if (count ( $kcontent ) > 0) { $content = $kcontent; } break; } } else { //模糊匹配 if (strstr ( $keyword, $key [\'txtname\'] )) { if ($key [\'txttype\'] == 1) { switch (trim ( $key [\'txtcontent\'] )) { case \'#最新问题#\' : $content = $this->newquestion (); if (count ( $content ) <= 0) { $content = "没有最新问题推荐哟"; } break; case \'#热门问题#\' : $content = $this->hotquestion (); if (count ( $content ) <= 0) { $content = "没有最新问题推荐哟"; } break; case \'#最新文章#\' : $content = $this->newblog (); if (count ( $content ) <= 0) { $content = "没有最新文章推荐哟"; } break; case \'#站长推荐#\' : $content = $this->hotblog (); if (count ( $content ) <= 0) { $content = "没有站长推荐的文章哟"; } break; case \'#附近的人#\' : break; case \'#附近的问题#\' : break; } } else { if (! empty ( $key [\'title\'] ) && $key [\'title\'] != \'\') { $sql = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "weixin_keywords where txtname=\'$keyword\' order by id desc LIMIT 0,9" ); while ( $topic = $this->db->fetch_array ( $sql ) ) { if (strstr ( $topic [\'wburl\'], \'http:\' )) { $kcontent [] = array ("Title" => $topic [\'title\'], "Description" => $topic [\'txtcontent\'], "PicUrl" => SITE_URL . $topic [\'fmtu\'], "Url" => $topic [\'wburl\'] ); } else { $kcontent [] = array ("Title" => $topic [\'title\'], "Description" => $topic [\'txtcontent\'], "PicUrl" => SITE_URL . $topic [\'fmtu\'], "Url" => SITE_URL . \'index.php?topic/getone/\' . $topic [\'wzid\'] . \'.html\' ); } } } else { $content = $key [\'txtcontent\']; } } if (count ( $kcontent ) > 0) { $content = $kcontent; } break; } } } if ($keyword == "签到" || $keyword == "打卡") { $content = "签到记录已经收到"; } if ($keyword == "账号绑定" || $content == "账号绑定") { $openid = $object->FromUserName; $getone = $this->db->fetch_first ( "SELECT * FROM " . DB_TABLEPRE . "user where openid=\'$openid\' limit 0,1" ); if ($getone == null) { $url = SITE_URL . "index.php?account/bind/$openid"; $content = "<a href=\'$url\'>" . $getone [\'username\'] . "点击进入账号绑定</a>"; } else { $content = $getone [\'username\'] . "您已经绑定账号了!"; } } if ($keyword == \'openid\') { $content = "您的openid:" . $object->FromUserName; } return $content; } //接收文本消息 private function receiveText($object) { $keyword = trim ( $object->Content ); $content = ""; $content = $this->getcontent ( $keyword, $object ); $type = \'\'; $firststr = substr ( $keyword, 0, 1 ); $laststr = substr ( $keyword, strlen ( $keyword ) - 1, 1 ); if ($laststr == "#" && $firststr == "#") { $type = \'topic\'; //表示文章检索 } if ($laststr == "$" && $firststr == "$") { $type = \'question\'; //表示文章检索 } // //检索内容 switch ($type) { case \'topic\' : $topickeyword = trim ( $keyword, "#" ); $content = array (); $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "topic where title like \'%$topickeyword%\' order by id desc LIMIT 0,4" ); while ( $topic = $this->db->fetch_array ( $query ) ) { //$topic[\'viewtime\'] = tdate($topic[\'viewtime\']); $index = strpos ( $topic [\'image\'], \'http\' ); if ($index == 0) { $content [] = array ("Title" => $topic [\'title\'], "Description" => "", "PicUrl" => $topic [\'image\'], "Url" => SITE_URL . \'index.php?topic/getone/\' . $topic [\'id\'] ); } else { $content [] = array ("Title" => $topic [\'title\'], "Description" => "", "PicUrl" => SITE_URL . $topic [\'image\'], "Url" => SITE_URL . \'index.php?topic/getone/\' . $topic [\'id\'] ); } } break; case \'question\' : $topickeyword = trim ( $keyword, "$" ); $content = array (); $query = $this->db->query ( "SELECT * FROM " . DB_TABLEPRE . "question where title like \'%$topickeyword%\' order by id desc LIMIT 0,4" ); while ( $question = $this->db->fetch_array ( $query ) ) { //$topic[\'viewtime\'] = tdate($topic[\'viewtime\']); $imgsrc =
全部评论
请发表评论