Facebook.php
| 1 | <?php |
| 2 | /** |
| 3 | * Facebook.php |
| 4 | * |
| 5 | * @package Embera |
| 6 | * @author Michael Pratt <yo@michael-pratt.com> |
| 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\Provider; |
| 14 | |
| 15 | use Embera\Url; |
| 16 | |
| 17 | /** |
| 18 | * Facebook Provider |
| 19 | * Connect with friends and the world around you on Facebook. |
| 20 | * This Provider Requires the use of an access_token provided by Facebook. |
| 21 | * Example: `$embera = new Embera([ 'facebook_access_token' => 'yourtokenforfacebook' ]);` |
| 22 | * |
| 23 | * @link https://facebook.com |
| 24 | * @see https://developers.facebook.com/docs/plugins/oembed-endpoints |
| 25 | */ |
| 26 | class Facebook extends ProviderAdapter implements ProviderInterface |
| 27 | { |
| 28 | /** inline {@inheritdoc} */ |
| 29 | protected $shouldSendRequest = false; |
| 30 | /** inline {@inheritdoc} */ |
| 31 | protected $endpoint = 'https://graph.facebook.com/v16.0/oembed_{type}'; |
| 32 | |
| 33 | /** inline {@inheritdoc} */ |
| 34 | protected static $hosts = [ |
| 35 | 'facebook.com' |
| 36 | ]; |
| 37 | |
| 38 | /** inline {@inheritdoc} */ |
| 39 | protected $allowedParams = [ 'maxwidth', 'maxheight', 'callback', 'omitscript', 'breaking_change', 'access_token', 'fields', 'locale' ]; |
| 40 | |
| 41 | /** inline {@inheritdoc} */ |
| 42 | protected $httpsSupport = true; |
| 43 | |
| 44 | /** inline {@inheritdoc} */ |
| 45 | protected $responsiveSupport = true; |
| 46 | |
| 47 | /** Patterns that match posts urls */ |
| 48 | protected $postPatterns = [ |
| 49 | /** |
| 50 | * https://www.facebook.com/{page-name}/posts/{post-id} |
| 51 | * https://www.facebook.com/{username}/posts/{post-id} |
| 52 | * https://www.facebook.com/{username}/activity/{activity-id} |
| 53 | * |
| 54 | * Undocumented: https://www.facebook.com/{username}/photos/{photo-id} |
| 55 | */ |
| 56 | '~facebook\.com/(?:[^/]+)/(?:posts|activity|photos)/(?:[^/]+)/?~i', |
| 57 | |
| 58 | /** |
| 59 | * https://www.facebook.com/notes/{username}/{note-url}/{note-id} |
| 60 | */ |
| 61 | '~facebook\.com/notes/(?:[^/]+)/(?:[^/]+)/(?:[^/]+)/?~i', |
| 62 | |
| 63 | /** |
| 64 | * https://www.facebook.com/photo.php?fbid={photo-id} |
| 65 | * https://www.facebook.com/permalink.php?story_fbid={post-id} |
| 66 | */ |
| 67 | '~facebook\.com/(?:photo|permalink)\.php\?(?:(story_)?fbid)=(?:[^ ]+)~i', |
| 68 | |
| 69 | /** |
| 70 | * https://www.facebook.com/photos/{photo-id} |
| 71 | * https://www.facebook.com/questions/{question-id} |
| 72 | */ |
| 73 | '~facebook\.com/(?:photos|questions)/(?:[^/ ]+)/?~i', |
| 74 | |
| 75 | /** |
| 76 | * NOTE: This url scheme is stated to be supported, however |
| 77 | * I havent found any example that work. I'm leaving it |
| 78 | * but I suspect that its not valid anymore.. we know how |
| 79 | * facebook is with API's :/ |
| 80 | * |
| 81 | * However in order to be really complaint with the documentation |
| 82 | * I'm leaving the pattern. |
| 83 | * |
| 84 | * https://www.facebook.com/media/set?set={set-id} |
| 85 | */ |
| 86 | '~facebook\.com/media/set/?\?set=(?:[^/ ]+)~i', |
| 87 | ]; |
| 88 | |
| 89 | /** Patterns that match video urls */ |
| 90 | protected $videoPatterns = [ |
| 91 | /** |
| 92 | * https://www.facebook.com/{page-name}/videos/{video-id}/ |
| 93 | * https://www.facebook.com/{username}/videos/{video-id}/ |
| 94 | */ |
| 95 | '~facebook\.com/(?:[^/]+)/videos/(?:[^/]+)/?~i', |
| 96 | |
| 97 | /** |
| 98 | * https://www.facebook.com/video.php?id={video-id} |
| 99 | * https://www.facebook.com/video.php?v={video-id} |
| 100 | */ |
| 101 | '~facebook\.com/video\.php\?(?:id|v)=(?:[^ ]+)~i', |
| 102 | |
| 103 | /** |
| 104 | * https://www.facebook.com/watch?v={video-id} |
| 105 | */ |
| 106 | '~facebook\.com/watch\?v=(?:[^ ]+)~i', |
| 107 | ]; |
| 108 | |
| 109 | /** Patterns that match page urls */ |
| 110 | protected $pagePatterns = array( |
| 111 | '~facebook\.com\/\S+[\/]?$~' |
| 112 | ); |
| 113 | |
| 114 | /** |
| 115 | * Checks if $url matches the given list of patterns |
| 116 | * |
| 117 | * @param string $url |
| 118 | * @param array $patternList |
| 119 | * @return bool |
| 120 | */ |
| 121 | protected function urlMatchesPattern($url, array $patternList) |
| 122 | { |
| 123 | foreach ($patternList as $p) { |
| 124 | if (preg_match($p, (string) $url)) { |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | /** inline {@inheritdoc} */ |
| 133 | public function validateUrl(Url $url) |
| 134 | { |
| 135 | return $this->urlMatchesPattern($url, array_merge($this->postPatterns, $this->videoPatterns, $this->pagePatterns)); |
| 136 | } |
| 137 | |
| 138 | /** inline {@inheritdoc} */ |
| 139 | public function getEndpoint() |
| 140 | { |
| 141 | if ($this->urlMatchesPattern($this->url, $this->videoPatterns)) { |
| 142 | $type = 'video'; |
| 143 | } elseif ($this->urlMatchesPattern($this->url, $this->postPatterns)) { |
| 144 | $type = 'post'; |
| 145 | } else { |
| 146 | $type = 'page'; |
| 147 | } |
| 148 | |
| 149 | return str_replace('{type}', $type, $this->endpoint); |
| 150 | } |
| 151 | |
| 152 | /** inline {@inheritdoc} */ |
| 153 | public function normalizeUrl(Url $url) |
| 154 | { |
| 155 | $url->convertToHttps(); |
| 156 | return $url; |
| 157 | } |
| 158 | |
| 159 | /** inline {@inheritdoc} */ |
| 160 | public function getStaticResponse() { |
| 161 | $response = []; |
| 162 | $params =$this->getParams(); |
| 163 | $embedUrl = 'https://www.facebook.com/plugins/post.php?href={url}&width={width}&height={height}&show_text=true'; |
| 164 | $height= 680; |
| 165 | $width = 500; |
| 166 | $attr = []; |
| 167 | $attr[] = 'class="embera-facebook-iframe-{md5}"'; |
| 168 | $attr[] = 'src="' . $embedUrl . '"'; |
| 169 | $attr[] = 'width="{width}"'; |
| 170 | $attr[] = 'height="{height}"'; |
| 171 | $attr[] = 'style="border:none;overflow:hidden"'; |
| 172 | $attr[] = 'scrolling="no"'; |
| 173 | $attr[] = 'frameborder="0"'; |
| 174 | $attr[] = 'allowTransparency="true"'; |
| 175 | |
| 176 | $iframe = '<iframe ' . implode(' ', $attr) . '></iframe>'; |
| 177 | if (!empty($params['maxheight'])) { |
| 178 | $height = $params['maxheight']; |
| 179 | } else { |
| 180 | if (!empty($params['maxwidth'])){ |
| 181 | $height = min(680, (int) ($params['maxwidth'] + 100)); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (!empty($params['maxwidth'])) { |
| 186 | $width = $params['maxwidth']; |
| 187 | } else { |
| 188 | if (!empty($params['maxheight'])){ |
| 189 | $width = min(500, (int) ($params['maxheight'] - 100)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | $table = array( |
| 194 | '{url}' => rawurlencode($this->url), |
| 195 | '{md5}' => substr(md5($this->url), 0, 5), |
| 196 | '{width}' => $width, |
| 197 | '{height}' => $height, |
| 198 | ); |
| 199 | |
| 200 | // Replace the html response |
| 201 | $response['html'] = str_replace(array_keys($table), array_values($table), $iframe); |
| 202 | |
| 203 | return $response; |
| 204 | } |
| 205 | |
| 206 | } |
| 207 |