PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/new88li/openid/qq/callback.php

http://phpfor.googlecode.com/
PHP | 77 lines | 56 code | 11 blank | 10 comment | 8 complexity | 3c1aea1055c5ad7dab76b35b1999f870 MD5 | raw file
  1. <?php
  2. require_once("client.php");
  3. //var_dump($_SESSION);exit;
  4. $targetUrl = !empty($_COOKIE['S']['OPENID_REFFERURL'])?$_COOKIE['S']['OPENID_REFFERURL']:$system->base_url();
  5. if($_REQUEST['state'] == $_SESSION['state']){
  6. $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"
  7. . "client_id=" . APP_KEY. "&redirect_uri=" . urlencode(CALLBACK)
  8. . "&client_secret=" . APP_SECRET. "&code=" . $_REQUEST["code"];
  9. $response = file_get_contents($token_url);
  10. if (strpos($response, "callback") !== false){
  11. $lpos = strpos($response, "(");
  12. $rpos = strrpos($response, ")");
  13. $response = substr($response, $lpos + 1, $rpos - $lpos -1);
  14. $msg = json_decode($response);
  15. if (isset($msg->error))
  16. {
  17. echo "<h3>error:</h3>" . $msg->error;
  18. echo "<h3>msg :</h3>" . $msg->error_description;
  19. exit;
  20. }
  21. }
  22. $params = array();
  23. parse_str($response, $params);
  24. //$_SESSION["access_token"] = $params["access_token"];
  25. //get_openid
  26. $graph_url = "https://graph.qq.com/oauth2.0/me?access_token="
  27. . $params["access_token"];
  28. $str = file_get_contents($graph_url);
  29. if (strpos($str, "callback") !== false)
  30. {
  31. $lpos = strpos($str, "(");
  32. $rpos = strrpos($str, ")");
  33. $str = substr($str, $lpos + 1, $rpos - $lpos -1);
  34. }
  35. $user = json_decode($str);
  36. //var_dump($user->openid);exit('get_openid()');
  37. if (isset($user->error))
  38. {
  39. echo "<h3>error:</h3>" . $user->error;
  40. echo "<h3>msg :</h3>" . $user->error_description;
  41. exit;
  42. }
  43. //echo("Hello " . $user->openid);
  44. $info = get_user_info($user->openid,$params["access_token"]);
  45. /*var_dump($info);exit;
  46. array(7) { ["ret"]=> int(0) ["msg"]=> string(0) "" ["nickname"]=> string(18) "??????" ["figureurl"]=> string(73) "http://qzapp.qlogo.cn/qzapp/100233567/865BD58E9F54F5B5134F392AB3FAC4EE/30" ["figureurl_1"]=> string(73) "http://qzapp.qlogo.cn/qzapp/100233567/865BD58E9F54F5B5134F392AB3FAC4EE/50" ["figureurl_2"]=> string(74) "http://qzapp.qlogo.cn/qzapp/100233567/865BD58E9F54F5B5134F392AB3FAC4EE/100" ["gender"]=> string(3) "?" }
  47. */
  48. $row = array(
  49. 'refer_id' => $user->openid,
  50. 'open_id' => $info['nickname'],
  51. //'email' => $info['qmail'],
  52. 'token' => serialize($params["access_token"]),
  53. 'open_type' => 'qq',
  54. );
  55. //var_dump($row);exit;
  56. $account = $system->loadModel('member/account');
  57. $user = $account->createotherlogin($row);
  58. if($user['member_refer'] != 'local'){
  59. $targetUrl = $system->mkUrl('passport','bind',array(base64_encode($targetUrl)));;
  60. }
  61. }else{
  62. echo("The state does not match. You may be a victim of CSRF.");
  63. }
  64. echo "<script>window.location=decodeURIComponent('".$targetUrl."');</script>";
  65. exit;
  66. ?>