Social.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /* -----------------------------------------------------------------------------------------
  3. IdiotMinds - http://idiotminds.com
  4. -----------------------------------------------------------------------------------------
  5. */
  6. //For Facebook
  7. require_once 'config.php';
  8. //For Google
  9. require_once 'lib/Google_Client.php';
  10. require_once 'lib/Google_Oauth2Service.php';
  11. class Social{
  12. public $_REDIRECT_URI;
  13. public $_GETURL;
  14. public function __construct($redirectUrl,$get = 0){
  15. $this->_REDIRECT_URI = $redirectUrl;
  16. $this->_GETURL = $get;
  17. }
  18. function google(){
  19. $client = new Google_Client();
  20. $client->setApplicationName("Idiot Minds Google Login Functionallity");
  21. $client->setClientId(CLIENT_ID);
  22. $client->setClientSecret(CLIENT_SECRET);
  23. $client->setRedirectUri($this->_REDIRECT_URI);
  24. $client->setApprovalPrompt(APPROVAL_PROMPT);
  25. $client->setAccessType(ACCESS_TYPE);
  26. $oauth2 = new Google_Oauth2Service($client);
  27. if (isset($_GET['code'])) {
  28. $client->authenticate($_GET['code']);
  29. $_SESSION['token'] = $client->getAccessToken();
  30. }
  31. if (isset($_SESSION['token'])) {
  32. $client->setAccessToken($_SESSION['token']);
  33. }
  34. if (isset($_REQUEST['error'])) {
  35. echo '<script type="text/javascript">window.close();</script>'; exit;
  36. }
  37. if(!$this->_GETURL){
  38. if ($client->getAccessToken()) {
  39. $user = $oauth2->userinfo->get();
  40. if($user['email']){
  41. //var_dump($user['email']);exit;
  42. return $user;
  43. }
  44. $_SESSION['User']=$user;
  45. $_SESSION['token'] = $client->getAccessToken();
  46. } else {
  47. $authUrl = $client->createAuthUrl();
  48. //header('Location: '.$authUrl);
  49. return $authUrl;
  50. }
  51. # 鑾峰彇 login url
  52. }else{
  53. $authUrl = $client->createAuthUrl();
  54. //header('Location: '.$authUrl);
  55. return $authUrl;
  56. }
  57. }
  58. }
  59. //$dd = new Social();exit;
  60. ?>