Def.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. if( !defined('BASEPATH') )
  3. {
  4. exit( 'No direct script access allowed' );
  5. }
  6. class Denglu
  7. {
  8. public function __construct()
  9. {
  10. $this->redirect_uri = links("user", "open/callback");
  11. $this->ci =& get_instance();
  12. }
  13. public function callback($ac, $log_state = "")
  14. {
  15. $mode = $ac . "_callback";
  16. return $this->$mode($log_state);
  17. }
  18. public function qq_login($log_state = "")
  19. {
  20. if( Qq_Log == 0 )
  21. {
  22. exit( "QQ登录为关闭状态~" );
  23. }
  24. $scope = "get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo";
  25. $login_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" . Qq_Appid . "&redirect_uri=" . $this->redirect_uri . "&state=" . $log_state . "&scope=" . $scope;
  26. header("Location:" . $login_url);
  27. }
  28. public function weixin_login($log_state = "")
  29. {
  30. if( Wx_Log == 0 )
  31. {
  32. exit( "微信登录为关闭状态~" );
  33. }
  34. $login_url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . Wx_Appid . "&redirect_uri=" . urlencode($this->redirect_uri) . "&response_type=code&scope=snsapi_login&state=" . $log_state . "#wechat_redirect";
  35. header("Location:" . $login_url);
  36. }
  37. public function qq_callback($log_state = "")
  38. {
  39. $state = $this->ci->input->get_post("state", true, true);
  40. $code = $this->ci->input->get("code", true);
  41. if( empty($state) || empty($code) )
  42. {
  43. msg_url("登录失败,返回参数错误~!", links("user", "login"));
  44. }
  45. if( $state != $log_state )
  46. {
  47. msg_url("非法登录~!", links("user", "login"));
  48. }
  49. $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&" . "client_id=" . Qq_Appid . "&redirect_uri=" . urlencode($this->redirect_uri) . "&client_secret=" . Qq_Appkey . "&code=" . $code;
  50. $response = $this->get_url_contents($token_url);
  51. if( strpos($response, "callback") !== false )
  52. {
  53. msg_url("登入失败,没获取到access_token!", links("user", "login"));
  54. }
  55. $params = array( );
  56. parse_str($response, $params);
  57. $data["access_token"] = $params["access_token"];
  58. $data["refresh_token"] = $params["refresh_token"];
  59. $data["expire_in"] = $params["expires_in"];
  60. $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $data["access_token"];
  61. $str = $this->get_url_contents($graph_url);
  62. if( strpos($str, "callback") !== false )
  63. {
  64. $lpos = strpos($str, "(");
  65. $rpos = strrpos($str, ")");
  66. $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
  67. }
  68. $user = json_decode($str);
  69. if( isset($user->error) )
  70. {
  71. msg_url("获取openid失败!", links("user", "login"));
  72. }
  73. $qqid = $user->openid;
  74. $get_user_info = "https://graph.qq.com/user/get_user_info?" . "access_token=" . $data["access_token"] . "&oauth_consumer_key=" . CS_Qqid . "&openid=" . $qqid . "&format=json";
  75. $info = $this->get_url_contents($get_user_info);
  76. $arr = json_decode($info, true);
  77. $data["nichen"] = $arr["nickname"];
  78. $data["pic"] = $arr["figureurl_2"];
  79. $data["uid"] = $qqid;
  80. return $data;
  81. }
  82. public function weixin_callback($log_state = "")
  83. {
  84. $state = $this->ci->input->get_post("state", true, true);
  85. $code = $this->ci->input->get("code", true);
  86. if( empty($state) || empty($code) )
  87. {
  88. msg_url("登录失败,返回参数错误~!", links("user", "login"));
  89. }
  90. if( $state != $log_state )
  91. {
  92. msg_url("非法登录~!", links("user", "login"));
  93. }
  94. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . Wx_Appid . "&secret=" . Wx_Appkey . "&code=" . $code . "&grant_type=authorization_code";
  95. $json = $this->get_url_contents($url);
  96. $arr = json_decode($json, true);
  97. $token = $arr["access_token"];
  98. $openid = $arr["openid"];
  99. if( empty($openid) )
  100. {
  101. exit( "获取用户信息失败!" );
  102. }
  103. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $token . "&openid=" . $openid;
  104. $json = $this->get_url_contents($url);
  105. $arr = json_decode($json, true);
  106. $data["nichen"] = $arr["nickname"];
  107. $data["pic"] = $arr["headimgurl"];
  108. $data["uid"] = $openid;
  109. return $data;
  110. }
  111. public function get_url_contents($url, $post = "", $type = "get")
  112. {
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  115. curl_setopt($ch, CURLOPT_URL, $url);
  116. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  117. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  118. if( $type == "post" )
  119. {
  120. curl_setopt($ch, CURLOPT_POST, 1);
  121. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  122. }
  123. else
  124. {
  125. curl_setopt($ch, CURLOPT_POST, 0);
  126. }
  127. curl_setopt($ch, CURLOPT_USERAGENT, "cscms");
  128. $result = curl_exec($ch);
  129. curl_close($ch);
  130. return $result;
  131. }
  132. }