MediaInternetYouTubeHandler.inc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Implementation of MediaInternetBaseHandler.
  4. *
  5. * @see hook_media_internet_providers().
  6. */
  7. class MediaInternetYouTubeHandler extends MediaInternetBaseHandler {
  8. public function parse($embedCode) {
  9. $patterns = array(
  10. '@youtube\.com/watch[#\?].*?v=([^"\& ]+)@i',
  11. '@youtube\.com/embed/([^"\&\? ]+)@i',
  12. '@youtube\.com/v/([^"\&\? ]+)@i',
  13. '@youtube\.com/\?v=([^"\& ]+)@i',
  14. '@youtu.be/([^"\&\? ]+)@i',
  15. );
  16. foreach ($patterns as $pattern) {
  17. preg_match($pattern, $embedCode, $matches);
  18. if (isset($matches[1]) && $this->valid_id($matches[1])) {
  19. return file_stream_wrapper_uri_normalize('youtube://v/' . $matches[1]);
  20. }
  21. }
  22. }
  23. public function valid_id($id) {
  24. $url = 'http://gdata.youtube.com/feeds/api/videos/'. $id;
  25. $response = drupal_http_request($url, array('method' => 'HEAD'));
  26. if ($response->code != 200) {
  27. throw new MediaInternetValidationException(t('The YouTube video ID is invalid or the video was deleted.'));
  28. }
  29. return TRUE;
  30. }
  31. public function claim($embedCode) {
  32. if ($this->parse($embedCode)) {
  33. return TRUE;
  34. }
  35. }
  36. public function getFileObject() {
  37. $uri = $this->parse($this->embedCode);
  38. $file = file_uri_to_object($uri, TRUE);
  39. if (empty($file->fid) && $info = $this->getOEmbed()) {
  40. $file->filename = truncate_utf8($info['title'], 255);
  41. }
  42. return $file;
  43. }
  44. /**
  45. * Returns information about the media. See http://video.search.yahoo.com/mrss.
  46. *
  47. * @return
  48. * If ATOM+MRSS information is available, a SimpleXML element containing
  49. * ATOM and MRSS elements, as per those respective specifications.
  50. *
  51. * @todo Would be better for the return value to be an array rather than a
  52. * SimpleXML element, but media_retrieve_xml() needs to be upgraded to
  53. * handle namespaces first.
  54. */
  55. public function getMRSS() {
  56. $uri = $this->parse($this->embedCode);
  57. $video_id = arg(1, file_uri_target($uri));
  58. $rss_url = url('http://gdata.youtube.com/feeds/api/videos/' . $video_id, array('query' => array('v' => '2')));
  59. // @todo Use media_retrieve_xml() once it's upgraded to include elements
  60. // from all namespaces, not just the document default namespace.
  61. $entry = simplexml_load_file($rss_url);
  62. return $entry;
  63. }
  64. /**
  65. * Returns information about the media. See http://www.oembed.com/.
  66. *
  67. * @return
  68. * If oEmbed information is available, an array containing 'title', 'type',
  69. * 'url', and other information as specified by the oEmbed standard.
  70. * Otherwise, NULL.
  71. */
  72. public function getOEmbed() {
  73. $uri = $this->parse($this->embedCode);
  74. $external_url = drupal_realpath($uri);
  75. $oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
  76. $response = drupal_http_request($oembed_url);
  77. if (!isset($response->error)) {
  78. return drupal_json_decode($response->data);
  79. }
  80. }
  81. }