Gfycat.php
| 1 | <?php |
| 2 | /** |
| 3 | * Gfycat.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 | * Gfycat Provider |
| 19 | * @link https://gfycat.com |
| 20 | */ |
| 21 | class Gfycat extends ProviderAdapter implements ProviderInterface |
| 22 | { |
| 23 | /** inline {@inheritdoc} */ |
| 24 | protected $endpoint = 'https://api.gfycat.com/v1/oembed?format=json'; |
| 25 | |
| 26 | /** inline {@inheritdoc} */ |
| 27 | protected static $hosts = [ |
| 28 | 'gfycat.com' |
| 29 | ]; |
| 30 | |
| 31 | /** inline {@inheritdoc} */ |
| 32 | protected $allowedParams = [ 'maxwidth', 'maxheight' ]; |
| 33 | |
| 34 | /** inline {@inheritdoc} */ |
| 35 | protected $httpsSupport = true; |
| 36 | |
| 37 | /** inline {@inheritdoc} */ |
| 38 | protected $responsiveSupport = true; |
| 39 | |
| 40 | /** inline {@inheritdoc} */ |
| 41 | public function validateUrl(Url $url) |
| 42 | { |
| 43 | $invalid = [ |
| 44 | 'api', 'terms', 'privacy', 'about', 'dmca', 'search', |
| 45 | 'popular-gifs', 'gaming', 'stickers', 'sound-gifs', 'discover', |
| 46 | 'upload', 'create', 'login', 'signup', 'jobs', 'partners', 'blog', 'faq', |
| 47 | 'support', 'gifbrewery', 'slack', 'pro', |
| 48 | ]; |
| 49 | |
| 50 | if (preg_match('~gfycat\.com/(?:' . implode('|', $invalid). ')$~', (string) $url)) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | return (bool) (preg_match('~gfycat\.com/([^/]+)$~i', (string) $url)); |
| 55 | } |
| 56 | |
| 57 | /** inline {@inheritdoc} */ |
| 58 | public function normalizeUrl(Url $url) |
| 59 | { |
| 60 | $url->convertToHttps(); |
| 61 | $url->removeQueryString(); |
| 62 | $url->removeLastSlash(); |
| 63 | $url->removeWWW(); |
| 64 | |
| 65 | return $url; |
| 66 | } |
| 67 | |
| 68 | /** inline {@inheritdoc} */ |
| 69 | public function getFakeResponse() |
| 70 | { |
| 71 | preg_match('~gfycat\.com/([^/\-]+)~i', (string) $this->url, $matches); |
| 72 | |
| 73 | $embedUrl = 'https://gfycat.com/ifr/' . $matches['1']; |
| 74 | |
| 75 | $attr = []; |
| 76 | $attr[] = 'src="' . $embedUrl . '"'; |
| 77 | $attr[] = 'frameborder="0"'; |
| 78 | $attr[] = 'scrolling="no"'; |
| 79 | $attr[] = 'width="100%"'; |
| 80 | $attr[] = 'height="100%"'; |
| 81 | $attr[] = 'style="position:absolute;top:0;left:0;"'; |
| 82 | $attr[] = 'allowfullscreen'; |
| 83 | |
| 84 | $html = []; |
| 85 | $html[] = '<div style="position:relative;padding-bottom: calc(56.25% + 44px)"">'; |
| 86 | $html[] = '<iframe ' . implode(' ', $attr). '></iframe>'; |
| 87 | $html[] = '</div>'; |
| 88 | |
| 89 | return [ |
| 90 | 'type' => 'video', |
| 91 | 'provider_name' => 'Gfycat', |
| 92 | 'provider_url' => 'https://gfycat.com', |
| 93 | 'title' => 'Unknown title', |
| 94 | 'html' => implode('', $html), |
| 95 | ]; |
| 96 | } |
| 97 | |
| 98 | } |
| 99 |