Oauth.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /* PHP SDK
  3. * @version 2.0.0
  4. * @author connect@qq.com
  5. * @copyright © 2013, Tencent Corporation. All rights reserved.
  6. */
  7. require_once(CLASS_PATH."Recorder.class.php");
  8. require_once(CLASS_PATH."URL.class.php");
  9. require_once(CLASS_PATH."ErrorCase.class.php");
  10. class Oauth{
  11. const VERSION = "2.0";
  12. const GET_AUTH_CODE_URL = "https://graph.qq.com/oauth2.0/authorize";
  13. const GET_ACCESS_TOKEN_URL = "https://graph.qq.com/oauth2.0/token";
  14. const GET_OPENID_URL = "https://graph.qq.com/oauth2.0/me";
  15. protected $recorder;
  16. public $urlUtils;
  17. protected $error;
  18. function __construct(){
  19. $this->recorder = new Recorder();
  20. $this->urlUtils = new URL();
  21. $this->error = new ErrorCase();
  22. }
  23. public function qq_login($ref,$type='pc'){
  24. $appid = $this->recorder->readInc("appid");
  25. $callback = $this->recorder->readInc("callback");
  26. if($type == 'touch'){
  27. $callback = $this->recorder->readInc("callback_touch");
  28. }
  29. $scope = $this->recorder->readInc("scope");
  30. //-------生成唯一随机串防CSRF攻击
  31. $state = md5(uniqid(rand(), TRUE));
  32. $this->recorder->write('state',$state);
  33. //-------构造请求参数列表
  34. $keysArr = array(
  35. "response_type" => "code",
  36. "client_id" => $appid,
  37. "redirect_uri" => $callback."?ref=".$ref,
  38. "state" => $state,
  39. "scope" => $scope
  40. );
  41. $login_url = $this->urlUtils->combineURL(self::GET_AUTH_CODE_URL, $keysArr);
  42. header("Location:$login_url");
  43. }
  44. public function qq_callback(){
  45. $state = $this->recorder->read("state");
  46. //--------验证state防止CSRF攻击
  47. if($_GET['state'] != $state){
  48. $this->error->showError("30001");
  49. }
  50. //-------请求参数列表
  51. $keysArr = array(
  52. "grant_type" => "authorization_code",
  53. "client_id" => $this->recorder->readInc("appid"),
  54. "redirect_uri" => urlencode($this->recorder->readInc("callback")),
  55. "client_secret" => $this->recorder->readInc("appkey"),
  56. "code" => $_GET['code']
  57. );
  58. //------构造请求access_token的url
  59. $token_url = $this->urlUtils->combineURL(self::GET_ACCESS_TOKEN_URL, $keysArr);
  60. $response = $this->urlUtils->get_contents($token_url);
  61. if(strpos($response, "callback") !== false){
  62. $lpos = strpos($response, "(");
  63. $rpos = strrpos($response, ")");
  64. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  65. $msg = json_decode($response);
  66. if(isset($msg->error)){
  67. $this->error->showError($msg->error, $msg->error_description);
  68. }
  69. }
  70. $params = array();
  71. parse_str($response, $params);
  72. $this->recorder->write("access_token", $params["access_token"]);
  73. return $params["access_token"];
  74. }
  75. public function get_openid(){
  76. //-------请求参数列表
  77. $keysArr = array(
  78. "access_token" => $this->recorder->read("access_token")
  79. );
  80. $graph_url = $this->urlUtils->combineURL(self::GET_OPENID_URL, $keysArr);
  81. $response = $this->urlUtils->get_contents($graph_url);
  82. //--------检测错误是否发生
  83. if(strpos($response, "callback") !== false){
  84. $lpos = strpos($response, "(");
  85. $rpos = strrpos($response, ")");
  86. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  87. }
  88. $user = json_decode($response);
  89. if(isset($user->error)){
  90. $this->error->showError($user->error, $user->error_description);
  91. }
  92. //------记录openid
  93. $this->recorder->write("openid", $user->openid);
  94. return $user->openid;
  95. }
  96. }