/cms_install/vendor/houdunwang/qq/src/build/Oauth.php

https://github.com/houdunwang/video · PHP · 128 lines · 80 code · 32 blank · 16 comment · 6 complexity · 1d571fc65b16b6a0c81858f5d0c256d7 MD5 · raw file

  1. <?php namespace houdunwang\qq\build;
  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( $config ) {
  19. $this->config( $config );
  20. $this->urlUtils = new URL();
  21. $this->error = new ErrorCase();
  22. }
  23. public function config( $config ) {
  24. $this->recorder = new Recorder( $config );
  25. }
  26. public function qq_login() {
  27. $appid = $this->recorder->readInc( "appid" );
  28. $callback = $this->recorder->readInc( "callback" );
  29. $scope = $this->recorder->readInc( "scope" );
  30. //-------生成唯一随机串防CSRF攻击
  31. $state = md5( uniqid( rand(), true ) );
  32. $this->recorder->write( 'state', $state );
  33. //-------构造请求参数列表
  34. $keysArr = [
  35. "response_type" => "code",
  36. "client_id" => $appid,
  37. "redirect_uri" => $callback,
  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 = [
  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 = [ ];
  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 = [
  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. }