File size: 4,251 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

namespace MauticPlugin\MauticSocialBundle\Integration;

use MauticPlugin\MauticSocialBundle\Form\Type\FacebookType;

class FacebookIntegration extends SocialIntegration
{
    public function getName(): string
    {
        return 'Facebook';
    }

    /**
     * @return string[]
     */
    public function getIdentifierFields(): array
    {
        return [
            'facebook',
        ];
    }

    public function getSupportedFeatures(): array
    {
        return [
            'share_button',
            'login_button',
            'public_profile',
        ];
    }

    public function getAuthenticationUrl(): string
    {
        return 'https://www.facebook.com/dialog/oauth';
    }

    public function getAccessTokenUrl(): string
    {
        return 'https://graph.facebook.com/oauth/access_token';
    }

    public function getAuthScope(): string
    {
        return 'email';
    }

    /**
     * @param string $data
     * @param bool   $postAuthorization
     *
     * @return mixed
     */
    public function parseCallbackResponse($data, $postAuthorization = false)
    {
        // Facebook is inconsistent in that it returns errors as json and data as parameter list
        $values = parent::parseCallbackResponse($data, $postAuthorization);

        if (null === $values) {
            parse_str($data, $values);

            $this->session->set($this->getName().'_tokenResponse', $values);
        }

        return $values;
    }

    public function getApiUrl($endpoint): string
    {
        return "https://graph.facebook.com/$endpoint";
    }

    /**
     * Get public data.
     *
     * @return array
     */
    public function getUserData($identifier, &$socialCache)
    {
        $this->persistNewLead = false;
        $accessToken          = $this->getContactAccessToken($socialCache);

        if (!isset($accessToken['access_token'])) {
            return;
        }

        $url    = $this->getApiUrl('v2.8/me');
        $fields = array_keys($this->getAvailableLeadFields());

        $parameters = [
            'access_token' => $accessToken['access_token'],
            'fields'       => implode(',', $fields),
        ];

        $data = $this->makeRequest($url, $parameters, 'GET', ['auth_type' => 'rest']);

        if (is_object($data) && isset($data->id)) {
            $info = $this->matchUpData($data);

            if (isset($data->username)) {
                $info['profileHandle'] = $data->username;
            } elseif (isset($data->link)) {
                if (preg_match("/www.facebook.com\/(app_scoped_user_id\/)?(.*?)($|\/)/", $data->link, $matches)) {
                    $info['profileHandle'] = $matches[2];
                }
            } else {
                $info['profileHandle'] = $data->id;
            }
            $info['profileImage'] = "https://graph.facebook.com/{$data->id}/picture?type=large";

            $socialCache['id']          = $data->id;
            $socialCache['profile']     = $info;
            $socialCache['lastRefresh'] = new \DateTime();
            $socialCache['accessToken'] = $this->encryptApiKeys($accessToken);

            $this->getMauticLead($info, $this->persistNewLead, $socialCache, $identifier);

            return $data;
        }

        return null;
    }

    public function getAvailableLeadFields($settings = []): array
    {
        return [
            'about'       => ['type' => 'string'],
            'birthday'    => ['type' => 'string'],
            'email'       => ['type' => 'string'],
            'first_name'  => ['type' => 'string'],
            'gender'      => ['type' => 'string'],
            'last_name'   => ['type' => 'string'],
            'link'        => ['type' => 'string'],
            'locale'      => ['type' => 'string'],
            'middle_name' => ['type' => 'string'],
            'name'        => ['type' => 'string'],
            'political'   => ['type' => 'string'],
            'quotes'      => ['type' => 'string'],
            'religion'    => ['type' => 'string'],
            'timezone'    => ['type' => 'string'],
            'website'     => ['type' => 'string'],
        ];
    }

    public function getFormType(): string
    {
        return FacebookType::class;
    }
}