source: extensions/oAuth/include/hybridauth/Hybrid/Providers/Google.php @ 20293

Last change on this file since 20293 was 20293, checked in by mistic100, 11 years ago

first commit of oAuth plugin, still in developpement

File size: 4.2 KB
Line 
1<?php
2/*!
3* HybridAuth
4* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
5* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
6*/
7
8/**
9 * Hybrid_Providers_Google provider adapter based on OAuth2 protocol
10 *
11 * http://hybridauth.sourceforge.net/userguide/IDProvider_info_Google.html
12 */
13class Hybrid_Providers_Google extends Hybrid_Provider_Model_OAuth2
14{
15        // default permissions
16        public $scope = "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.google.com/m8/feeds/";
17
18        /**
19        * IDp wrappers initializer
20        */
21        function initialize() 
22        {
23                parent::initialize();
24
25                // Provider api end-points
26                $this->api->authorize_url  = "https://accounts.google.com/o/oauth2/auth";
27                $this->api->token_url      = "https://accounts.google.com/o/oauth2/token";
28                $this->api->token_info_url = "https://www.googleapis.com/oauth2/v1/tokeninfo";
29        }
30
31        /**
32        * begin login step
33        */
34        function loginBegin()
35        {
36                $parameters = array("scope" => $this->scope, "access_type" => "offline");
37                $optionals  = array("scope", "access_type", "redirect_uri", "approval_prompt", "hd");
38
39                foreach ($optionals as $parameter){
40                        if( isset( $this->config[$parameter] ) && ! empty( $this->config[$parameter] ) ){
41                                $parameters[$parameter] = $this->config[$parameter];
42                        }
43                }
44
45                Hybrid_Auth::redirect( $this->api->authorizeUrl( $parameters ) ); 
46        }
47
48        /**
49        * load the user profile from the IDp api client
50        */
51        function getUserProfile()
52        {
53                // refresh tokens if needed
54                $this->refreshToken();
55
56                // ask google api for user infos
57                $response = $this->api->api( "https://www.googleapis.com/oauth2/v1/userinfo" ); 
58
59                if ( ! isset( $response->id ) || isset( $response->error ) ){
60                        throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 );
61                }
62
63                $this->user->profile->identifier    = (property_exists($response,'id'))?$response->id:"";
64                $this->user->profile->firstName     = (property_exists($response,'given_name'))?$response->given_name:"";
65                $this->user->profile->lastName      = (property_exists($response,'family_name'))?$response->family_name:"";
66                $this->user->profile->displayName   = (property_exists($response,'name'))?$response->name:"";
67                $this->user->profile->photoURL      = (property_exists($response,'picture'))?$response->picture:"";
68                $this->user->profile->profileURL    = "https://profiles.google.com/" . $this->user->profile->identifier;
69                $this->user->profile->gender        = (property_exists($response,'gender'))?$response->gender:""; 
70                $this->user->profile->email         = (property_exists($response,'email'))?$response->email:"";
71                $this->user->profile->emailVerified = (property_exists($response,'email'))?$response->email:"";
72                $this->user->profile->language      = (property_exists($response,'locale'))?$response->locale:"";
73
74                if( property_exists($response,'birthday') ){ 
75                        list($birthday_year, $birthday_month, $birthday_day) = explode( '-', $response->birthday );
76
77                        $this->user->profile->birthDay   = (int) $birthday_day;
78                        $this->user->profile->birthMonth = (int) $birthday_month;
79                        $this->user->profile->birthYear  = (int) $birthday_year;
80                }
81
82                return $this->user->profile;
83        }
84
85        /**
86        * load the user (Gmail) contacts
87        *  ..toComplete
88        */
89        function getUserContacts()
90        { 
91                // refresh tokens if needed
92                $this->refreshToken(); 
93
94                if( ! isset( $this->config['contacts_param'] ) ){
95                        $this->config['contacts_param'] = array( "max-results" => 500 );
96                }
97
98                $response = $this->api->api( "https://www.google.com/m8/feeds/contacts/default/full?" 
99                                                        . http_build_query( array_merge( array('alt' => 'json'), $this->config['contacts_param'] ) ) ); 
100
101                if( ! $response ){
102                        return ARRAY();
103                }
104 
105                $contacts = ARRAY(); 
106
107                foreach( $response->feed->entry as $idx => $entry ){
108                        $uc = new Hybrid_User_Contact();
109
110                        $uc->email        = isset($entry->{'gd$email'}[0]->address) ? (string) $entry->{'gd$email'}[0]->address : ''; 
111                        $uc->displayName  = isset($entry->title->{'$t'}) ? (string) $entry->title->{'$t'} : ''; 
112                        $uc->identifier   = $uc->email;
113
114                        $contacts[] = $uc;
115                } 
116
117                return $contacts;
118        }
119}
Note: See TracBrowser for help on using the repository browser.