|
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
|
|
|
protected function validateUrl() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->url->convertToHttps(); |
|
92
|
|
|
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
|
|
|
protected function urlMatchesPattern(array $patternList) |
|
102
|
|
|
{ |
|
103
|
|
|
foreach ($patternList as $p) { |
|
104
|
|
|
if (preg_match($p, $this->url)) { |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
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
|
|
|
public function getInfo() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->apiUrl = 'https://www.facebook.com/plugins/post/oembed.json/'; |
|
126
|
|
|
if ($this->urlMatchesPattern($this->videoPatterns)) { |
|
127
|
|
|
$this->apiUrl = 'https://www.facebook.com/plugins/video/oembed.json/'; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return parent::getInfo(); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
?> |
|
|
|
|
|
|
135
|
|
|
|
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.