Youtube.php
| 1 | <?php |
| 2 | /** |
| 3 | * Youtube.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 | * Youtube Provider |
| 19 | * Disfruta los videos y la música que te encantan, sube contenido original y compártelo con tus... |
| 20 | * |
| 21 | * @link https://youtube.com |
| 22 | * @see https://youtube-eng.googleblog.com/2009/10/oembed-support_9.html |
| 23 | */ |
| 24 | class Youtube extends ProviderAdapter implements ProviderInterface |
| 25 | { |
| 26 | /** inline {@inheritdoc} */ |
| 27 | protected $endpoint = 'https://www.youtube.com/oembed?format=json&scheme=https'; |
| 28 | |
| 29 | /** inline {@inheritdoc} */ |
| 30 | protected static $hosts = [ |
| 31 | 'm.youtube.com', 'youtube.com', 'youtu.be', |
| 32 | ]; |
| 33 | |
| 34 | /** inline {@inheritdoc} */ |
| 35 | protected $httpsSupport = true; |
| 36 | |
| 37 | /** inline {@inheritdoc} */ |
| 38 | public function validateUrl(Url $url) |
| 39 | { |
| 40 | return (bool) ( |
| 41 | preg_match('~v=(?:[a-z0-9_\-]+)~i', (string) $url) || |
| 42 | preg_match('~/shorts/(?:[a-z0-9_\-]+)~i', (string) $url) || |
| 43 | preg_match('~/playlist(.+)list=(?:[a-z0-9_\-]+)~i', (string) $url) || |
| 44 | preg_match('~/live/(?:[a-z0-9_\-]+)~i', (string) $url) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /** inline {@inheritdoc} */ |
| 49 | public function normalizeUrl(Url $url) |
| 50 | { |
| 51 | if (preg_match('~(?:v=|youtu\.be/|youtube\.com/embed/)([a-z0-9_\-]+)~i', (string) $url, $matches)) { |
| 52 | $url->overwrite('https://www.youtube.com/watch?v=' . $matches[1]); |
| 53 | } |
| 54 | |
| 55 | return $url; |
| 56 | } |
| 57 | |
| 58 | /** inline {@inheritdoc} */ |
| 59 | public function getFakeResponse() |
| 60 | { |
| 61 | if (preg_match('~v=([a-z0-9_\-]+)~i', (string) $this->url, $matches) || preg_match('~/shorts/([^/]+)~i', (string) $this->url, $matches)) { |
| 62 | $embedUrl = 'https://www.youtube.com/embed/' . $matches['1'] . '?feature=oembed'; |
| 63 | } else if (preg_match('~/playlist(.+)list=([a-z0-9_\-]+)~i', (string) $this->url, $matches)) { |
| 64 | $embedUrl = 'https://www.youtube.com/embed/videoseries?list=' . $matches['1']; |
| 65 | } else { |
| 66 | return array(); |
| 67 | } |
| 68 | |
| 69 | $attr = []; |
| 70 | $attr[] = 'width="{width}"'; |
| 71 | $attr[] = 'height="{height}"'; |
| 72 | $attr[] = 'src="' . $embedUrl . '"'; |
| 73 | $attr[] = 'frameborder="0"'; |
| 74 | $attr[] = 'allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"'; |
| 75 | $attr[] = 'referrerpolicy="strict-origin-when-cross-origin"'; |
| 76 | $attr[] = 'allowfullscreen'; |
| 77 | $attr[] = 'title=""'; |
| 78 | |
| 79 | return [ |
| 80 | 'type' => 'video', |
| 81 | 'provider_name' => 'Youtube', |
| 82 | 'provider_url' => 'https://www.youtube.com', |
| 83 | 'title' => 'Unknown title', |
| 84 | 'html' => '<iframe ' . implode(' ', $attr). '></iframe>', |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | } |
| 89 |