Guest User

google_oauth.php

a guest
Dec 22nd, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. <?php
  2. /*
  3. UserSpice 4
  4. An Open Source PHP User Management System
  5. by the UserSpice Team at http://UserSpice.com
  6.  
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. ?>
  21. <?php
  22.  
  23. $value=null;
  24. $gender=null;
  25. $link=null;
  26. if($settings->glogin==1 && !$user->isLoggedIn()){
  27. require_once $abs_us_root.$us_url_root.'users/helpers/google_helpers.php';
  28. if(isset($_REQUEST['code'])){
  29. $gClient->authenticate();
  30. $_SESSION['token'] = $gClient->getAccessToken();
  31. header('Location: ' . filter_var($redirectUrl, FILTER_SANITIZE_URL));
  32. }
  33. $gClient->setAccessType('online');
  34. $gClient->setApprovalPrompt('auto') ;
  35. if (isset($_SESSION['token'])) {
  36. $gClient->setAccessToken($_SESSION['token']);
  37. }
  38.  
  39. if ($gClient->getAccessToken()) {
  40. $userProfile = $google_oauthV2->userinfo->get();
  41. //User Authenticated by Google
  42.  
  43. $gUser = new User();
  44. $_SESSION["user"]=$value;
  45. //Deal with a user having an account but no google creds
  46. $findExistingUS=$db->query("SELECT * FROM users WHERE email = ?",array($userProfile['email']));
  47. $feusc = $findExistingUS->count();
  48. if($feusc>0){$feusr = $findExistingUS->first();}
  49. if($feusc == 1){
  50. $fields=array('gpluslink'=>'https://plus.google.com/'.$userProfile['id'],'picture'=>$userProfile['picture'],'locale'=>$userProfile['locale'],'gender'=>'unknown','oauth_provider'=>"google",'oauth_uid'=>$userProfile['id']);
  51. $db->update('users',$feusr->id,$fields);
  52. }
  53. $gUser->checkUser('google',$userProfile['id'],$userProfile['given_name'],$userProfile['family_name'],$userProfile['email'],$gender,$userProfile['locale'],$link,$userProfile['picture']);
  54. //Add UserSpice info to session
  55. $_SESSION["user"]=$feusr->id;
  56. //Add Google info to the session
  57. $_SESSION['google_data'] = $userProfile;
  58.  
  59. $_SESSION['token'] = $gClient->getAccessToken();
  60.  
  61.  
  62. } else {
  63. $authUrl = $gClient->createAuthUrl();
  64.  
  65. }
  66. }
  67. // if(isset($authUrl)) {
  68. // echo '<a href="'.$authUrl.'"><img src="'
  69. // .$us_url_root.'/users/images/google.png" alt=""/></a>';
  70. // } else {
  71. // echo '<a href="users/logout.php?logout">Logout</a>';
  72. // }
  73. ?>
Add Comment
Please, Sign In to add comment