Completed
Pull Request — master (#498)
by Dragonqos
02:30
created

Facebook::getApiVersionString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Exception\Exception;
6
use OAuth\OAuth2\Token\StdOAuth2Token;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
10
class Facebook extends AbstractService
11
{
12
    /**
13
     * Facebook www url - used to build dialog urls
14
     */
15
    const WWW_URL = 'https://www.facebook.com/';
16
17
    /**
18
     * Defined scopes
19
     *
20
     * If you don't think this is scary you should not be allowed on the web at all
21
     *
22
     * @link https://developers.facebook.com/docs/reference/login/
23
     * @link https://developers.facebook.com/tools/explorer For a list of permissions use 'Get Access Token'
24
     */
25
    // Default scope
26
    const SCOPE_PUBLIC_PROFILE                = 'public_profile';
27
    // Email scopes
28
    const SCOPE_EMAIL                         = 'email';
29
    // Extended permissions
30
    const SCOPE_READ_FRIENDLIST               = 'read_friendlists';
31
    const SCOPE_READ_INSIGHTS                 = 'read_insights';
32
    const SCOPE_READ_MAILBOX                  = 'read_mailbox';
33
    const SCOPE_READ_PAGE_MAILBOXES           = 'read_page_mailboxes';
34
    const SCOPE_READ_REQUESTS                 = 'read_requests';
35
    const SCOPE_READ_STREAM                   = 'read_stream';
36
    const SCOPE_VIDEO_UPLOAD                  = 'video_upload';
37
    const SCOPE_XMPP_LOGIN                    = 'xmpp_login';
38
    const SCOPE_USER_ONLINE_PRESENCE          = 'user_online_presence';
39
    const SCOPE_FRIENDS_ONLINE_PRESENCE       = 'friends_online_presence';
40
    const SCOPE_ADS_MANAGEMENT                = 'ads_management';
41
    const SCOPE_ADS_READ                      = 'ads_read';
42
    const SCOPE_CREATE_EVENT                  = 'create_event';
43
    const SCOPE_CREATE_NOTE                   = 'create_note';
44
    const SCOPE_EXPORT_STREAM                 = 'export_stream';
45
    const SCOPE_MANAGE_FRIENDLIST             = 'manage_friendlists';
46
    const SCOPE_MANAGE_NOTIFICATIONS          = 'manage_notifications';
47
    const SCOPE_PHOTO_UPLOAD                  = 'photo_upload';
48
    const SCOPE_PUBLISH_ACTIONS               = 'publish_actions';
49
    const SCOPE_PUBLISH_CHECKINS              = 'publish_checkins';
50
    const SCOPE_PUBLISH_STREAM                = 'publish_stream';
51
    const SCOPE_RSVP_EVENT                    = 'rsvp_event';
52
    const SCOPE_SHARE_ITEM                    = 'share_item';
53
    const SCOPE_SMS                           = 'sms';
54
    const SCOPE_STATUS_UPDATE                 = 'status_update';
55
    // Extended Profile Properties
56
    const SCOPE_USER_POSTS                    = 'user_posts';
57
    const SCOPE_USER_FRIENDS                  = 'user_friends';
58
    const SCOPE_USER_ABOUT                    = 'user_about_me';
59
    const SCOPE_USER_TAGGED_PLACES            = 'user_tagged_places';
60
    const SCOPE_FRIENDS_ABOUT                 = 'friends_about_me';
61
    const SCOPE_USER_ACTIVITIES               = 'user_activities';
62
    const SCOPE_FRIENDS_ACTIVITIES            = 'friends_activities';
63
    const SCOPE_USER_BIRTHDAY                 = 'user_birthday';
64
    const SCOPE_FRIENDS_BIRTHDAY              = 'friends_birthday';
65
    const SCOPE_USER_CHECKINS                 = 'user_checkins';
66
    const SCOPE_FRIENDS_CHECKINS              = 'friends_checkins';
67
    const SCOPE_USER_EDUCATION                = 'user_education_history';
68
    const SCOPE_FRIENDS_EDUCATION             = 'friends_education_history';
69
    const SCOPE_USER_EVENTS                   = 'user_events';
70
    const SCOPE_FRIENDS_EVENTS                = 'friends_events';
71
    const SCOPE_USER_GROUPS                   = 'user_groups';
72
    const SCOPE_USER_MANAGED_GROUPS           = 'user_managed_groups';
73
    const SCOPE_FRIENDS_GROUPS                = 'friends_groups';
74
    const SCOPE_USER_HOMETOWN                 = 'user_hometown';
75
    const SCOPE_FRIENDS_HOMETOWN              = 'friends_hometown';
76
    const SCOPE_USER_INTERESTS                = 'user_interests';
77
    const SCOPE_FRIEND_INTERESTS              = 'friends_interests';
78
    const SCOPE_USER_LIKES                    = 'user_likes';
79
    const SCOPE_FRIENDS_LIKES                 = 'friends_likes';
80
    const SCOPE_USER_LOCATION                 = 'user_location';
81
    const SCOPE_FRIENDS_LOCATION              = 'friends_location';
82
    const SCOPE_USER_NOTES                    = 'user_notes';
83
    const SCOPE_FRIENDS_NOTES                 = 'friends_notes';
84
    const SCOPE_USER_PHOTOS                   = 'user_photos';
85
    const SCOPE_USER_PHOTO_VIDEO_TAGS         = 'user_photo_video_tags';
86
    const SCOPE_FRIENDS_PHOTOS                = 'friends_photos';
87
    const SCOPE_FRIENDS_PHOTO_VIDEO_TAGS      = 'friends_photo_video_tags';
88
    const SCOPE_USER_QUESTIONS                = 'user_questions';
89
    const SCOPE_FRIENDS_QUESTIONS             = 'friends_questions';
90
    const SCOPE_USER_RELATIONSHIPS            = 'user_relationships';
91
    const SCOPE_FRIENDS_RELATIONSHIPS         = 'friends_relationships';
92
    const SCOPE_USER_RELATIONSHIPS_DETAILS    = 'user_relationship_details';
93
    const SCOPE_FRIENDS_RELATIONSHIPS_DETAILS = 'friends_relationship_details';
94
    const SCOPE_USER_RELIGION                 = 'user_religion_politics';
95
    const SCOPE_FRIENDS_RELIGION              = 'friends_religion_politics';
96
    const SCOPE_USER_STATUS                   = 'user_status';
97
    const SCOPE_FRIENDS_STATUS                = 'friends_status';
98
    const SCOPE_USER_SUBSCRIPTIONS            = 'user_subscriptions';
99
    const SCOPE_FRIENDS_SUBSCRIPTIONS         = 'friends_subscriptions';
100
    const SCOPE_USER_VIDEOS                   = 'user_videos';
101
    const SCOPE_FRIENDS_VIDEOS                = 'friends_videos';
102
    const SCOPE_USER_WEBSITE                  = 'user_website';
103
    const SCOPE_FRIENDS_WEBSITE               = 'friends_website';
104
    const SCOPE_USER_WORK                     = 'user_work_history';
105
    const SCOPE_FRIENDS_WORK                  = 'friends_work_history';
106
    // Open Graph Permissions
107
    const SCOPE_USER_MUSIC                    = 'user_actions.music';
108
    const SCOPE_FRIENDS_MUSIC                 = 'friends_actions.music';
109
    const SCOPE_USER_NEWS                     = 'user_actions.news';
110
    const SCOPE_FRIENDS_NEWS                  = 'friends_actions.news';
111
    const SCOPE_USER_VIDEO                    = 'user_actions.video';
112
    const SCOPE_FRIENDS_VIDEO                 = 'friends_actions.video';
113
    const SCOPE_USER_APP                      = 'user_actions:APP_NAMESPACE';
114
    const SCOPE_FRIENDS_APP                   = 'friends_actions:APP_NAMESPACE';
115
    const SCOPE_USER_GAMES                    = 'user_games_activity';
116
    const SCOPE_FRIENDS_GAMES                 = 'friends_games_activity';
117
    //Page Permissions
118
    const SCOPE_PAGES                         = 'manage_pages';
119
    const SCOPE_PUBLISH_PAGES                 = 'publish_pages';
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function init()
125
    {
126
        $this->stateParameterInAuthUrl = true;
127
128
        if( $this->baseApiUri === null ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
129
            $this->baseApiUri = new Uri('https://graph.facebook.com'.$this->getApiVersionString().'/');
130
        }
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getAuthorizationEndpoint()
137
    {
138
        return new Uri('https://www.facebook.com'.$this->getApiVersionString().'/dialog/oauth');
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function getAccessTokenEndpoint()
145
    {
146
        return new Uri('https://graph.facebook.com'.$this->getApiVersionString().'/oauth/access_token');
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    protected function parseAccessTokenResponse($responseBody)
153
    {
154
        $data = @json_decode($responseBody, true);
155
156
        // Facebook gives us a query string on old api (v2.0)
157
        if (!$data) {
158
            parse_str($responseBody, $data);
159
        }
160
161
        if (null === $data || !is_array($data)) {
162
            throw new TokenResponseException('Unable to parse response.');
163
        } elseif (isset($data['error'])) {
164
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
165
        }
166
167
        $token = new StdOAuth2Token();
168
        $token->setAccessToken($data['access_token']);
169
170
        if (isset($data['expires'])) {
171
            $token->setLifeTime($data['expires']);
172
        }
173
174
        if (isset($data['refresh_token'])) {
175
            $token->setRefreshToken($data['refresh_token']);
176
            unset($data['refresh_token']);
177
        }
178
179
        unset($data['access_token']);
180
        unset($data['expires']);
181
182
        $token->setExtraParams($data);
183
184
        return $token;
185
    }
186
187
    public function getDialogUri($dialogPath, array $parameters)
188
    {
189
        if (!isset($parameters['redirect_uri'])) {
190
            throw new Exception("Redirect uri is mandatory for this request");
191
        }
192
        $parameters['app_id'] = $this->credentials->getConsumerId();
193
        $baseUrl = self::WWW_URL .$this->getApiVersionString(). '/dialog/' . $dialogPath;
194
        $query = http_build_query($parameters);
195
        return new Uri($baseUrl . '?' . $query);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    protected function getApiVersionString()
202
    {
203
        return empty($this->apiVersion) ? '' : '/v' . $this->apiVersion;
204
    }
205
206
    /**
207
     * {@inheritdoc}
208
     */
209
    protected function getScopesDelimiter()
210
    {
211
        return ',';
212
    }
213
}
214