GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 58a54a...a4de62 )
by Michael
03:34
created

Facebook::modifyResponse()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 8.8571
cc 3
eloc 15
nc 3
nop 1
crap 12
1
<?php
2
/**
3
 * Facebook.php
4
 *
5
 * @package Providers
6
 * @author Michael Pratt <[email protected]>
7
 * @link   http://www.michael-pratt.com/
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Embera\Providers;
14
15
/**
16
 * The Facebook Provider (Public posts and videos)
17
 * @link https://www.facebook.com
18
 * @link https://developers.facebook.com/docs/plugins/oembed-endpoints
19
 */
20
class Facebook extends \Embera\Adapters\Service
21
{
22
    /**
23
     * inline {@inheritdoc}
24
     * This Provider is kind of special, because it uses different oembed endpoints
25
     * based on the given url. The default value of the endpoint is null and is set
26
     * during the process of getting the url information.
27
     */
28
    protected $apiUrl = null;
29
30
    /** Patterns that match posts urls */
31
    protected $postPatterns = array(
32
        /**
33
         * https://www.facebook.com/{page-name}/posts/{post-id}
34
         * https://www.facebook.com/{username}/posts/{post-id}
35
         * https://www.facebook.com/{username}/activity/{activity-id}
36
         *
37
         * Undocumented: https://www.facebook.com/{username}/photos/{photo-id}
38
         */
39
        '~facebook\.com/(?:[^/]+)/(?:posts|activity|photos)/(?:[^/]+)/?~i',
40
41
        /**
42
         * https://www.facebook.com/notes/{username}/{note-url}/{note-id}
43
         */
44
        '~facebook\.com/notes/(?:[^/]+)/(?:[^/]+)/(?:[^/]+)/?~i',
45
46
        /**
47
         * https://www.facebook.com/photo.php?fbid={photo-id}
48
         * https://www.facebook.com/permalink.php?story_fbid={post-id}
49
         */
50
        '~facebook\.com/(?:photo|permalink)\.php\?(?:(story_)?fbid)=(?:[^ ]+)~i',
51
52
        /**
53
         * https://www.facebook.com/photos/{photo-id}
54
         * https://www.facebook.com/questions/{question-id}
55
         */
56
        '~facebook\.com/(?:photos|questions)/(?:[^/ ]+)/?~i',
57
58
        /**
59
         * NOTE: This url scheme is stated to be supported, however
60
         * I havent found any example that work. I'm leaving it
61
         * but I suspect that its not valid anymore.. we know how
62
         * facebook is with API's :/
63
         *
64
         * However in order to be really complaint with the documentation
65
         * I'm leaving the pattern.
66
         *
67
         * https://www.facebook.com/media/set?set={set-id}
68
         */
69
         '~facebook\.com/media/set/?\?set=(?:[^/ ]+)~i',
70
    );
71
72
73
    /** Patterns that match video urls */
74
    protected $videoPatterns = array(
75
        /**
76
         * https://www.facebook.com/{page-name}/videos/{video-id}/
77
         * https://www.facebook.com/{username}/videos/{video-id}/
78
         */
79
        '~facebook\.com/(?:[^/]+)/videos/(?:[^/]+)/?~i',
80
81
        /**
82
`        * https://www.facebook.com/video.php?id={video-id}
83
         * https://www.facebook.com/video.php?v={video-id}
84
         */
85
        '~facebook\.com/video\.php\?(?:id|v)=(?:[^ ]+)~i',
86
    );
87
88
    /** inline {@inheritdoc} */
89 2
    protected function validateUrl()
90 2
    {
91 2
        $this->url->convertToHttps();
92 2
        return ($this->urlMatchesPattern(array_merge($this->postPatterns, $this->videoPatterns)));
93
    }
94
95
    /**
96
     * Checks if $this->url matches the given list of patterns
97
     *
98
     * @param array $patternList Array with regex
99
     * @return bool
100
     */
101 2
    protected function urlMatchesPattern(array $patternList)
102
    {
103 2
        foreach ($patternList as $p) {
104 2
            if (preg_match($p, $this->url)) {
105 2
                return true;
106
            }
107 2
        }
108
109 2
        return false;
110
    }
111
112
    /**
113
     * inline {@inheritdoc}
114
     *
115
     * Im overriding this method because I need to set the
116
     * endpoint based on the given url. By default we're always assuming
117
     * it is a post url unless we have a specific video match.
118
     *
119
     * Why? Because we already did url validation and We dont want
120
     * to loop over both sets of patterns all over again right? So
121
     * we just need to loop over the smaller one ;)
122
     */
123 2
    public function getInfo()
124
    {
125 2
        $this->apiUrl = 'https://www.facebook.com/plugins/post/oembed.json/';
126 2
        if ($this->urlMatchesPattern($this->videoPatterns)) {
127
            $this->apiUrl = 'https://www.facebook.com/plugins/video/oembed.json/';
128
        }
129
130 2
        return parent::getInfo();
131
    }
132
133
    /**
134
     * inline {@inheritdoc}
135
     *
136
     * Need to modify the html response, to use the iframe instead.
137
     * The html returned by facebook always adds the javascript code
138
     * and the famous <div id="fb-root"></div>.
139
     *
140
     * When embedding multiple links, the code seems to conflict and doesnt
141
     * embed properly
142
     */
143
    protected function modifyResponse(array $response = array())
144
    {
145
        if (!empty($response['html'])) {
146
            $iframe = '<iframe id="embera-iframe-{md5}" class="embera-facebook-iframe" src="https://www.facebook.com/plugins/post.php?href={url}&width={width}&height={height}&show_text=true&appId" width="{width}" height="{height}"" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>';
147
148
            if (!empty($response['height'])) {
149
                $height = $response['height'];
150
            } else {
151
                $height = min(680, (int) ($response['width'] + 100));
152
            }
153
154
            $table = array(
155
                '{url}' => rawurlencode($this->url),
156
                '{md5}' => substr(md5($this->url), 0, 5),
157
                '{width}' => $response['width'],
158
                '{height}' => $height,
159
            );
160
161
            // Backup the real response
162
            $response['raw']['html'] = $response['html'];
163
164
            // Replace the html response
165
            $response['html'] = str_replace(array_keys($table), array_values($table), $iframe);
166
        }
167
168
        return $response;
169
    }
170
}
171
172
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
173