MediaInternetVimeoHandler.inc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Implementation of MediaInternetBaseHandler.
  4. *
  5. * @see hook_media_internet_providers().
  6. */
  7. class MediaInternetVimeoHandler extends MediaInternetBaseHandler {
  8. public function parse($embedCode) {
  9. // Vimeo has a few URL formats:
  10. // http://vimeo.com/*
  11. // http://vimeo.com/video/*
  12. // http://vimeo.com/groups/*/videos/*
  13. // http://vimeo.com/channels/*#$ID
  14. $patterns = array(
  15. '@vimeo\.com/(\d+)@i',
  16. '@vimeo\.com/video/(\d+)@i',
  17. '@vimeo\.com/groups/.+/videos/(\d+)@i',
  18. '@vimeo\.com/channels/.+#(\d+)@i',
  19. );
  20. foreach ($patterns as $pattern) {
  21. preg_match($pattern, $embedCode, $matches);
  22. if (isset($matches[1])) {
  23. return file_stream_wrapper_uri_normalize('vimeo://v/' . $matches[1]);
  24. }
  25. }
  26. }
  27. public function claim($embedCode) {
  28. if ($this->parse($embedCode)) {
  29. return TRUE;
  30. }
  31. }
  32. public function save() {
  33. $file = $this->getFileObject();
  34. file_save($file);
  35. return $file;
  36. }
  37. public function getFileObject() {
  38. $uri = $this->parse($this->embedCode);
  39. //@todo: this is terribly broken in some ways because the function is really
  40. // made for local files which are 'real'
  41. $file = file_uri_to_object($uri, TRUE);
  42. // Try to default the file name to the video's title.
  43. if (empty($file->fid) && $info = $this->getOEmbed()) {
  44. $file->filename = truncate_utf8($info['title'], 255);
  45. }
  46. return $file;
  47. }
  48. /**
  49. * Returns information about the media.
  50. *
  51. * See http://video.search.yahoo.com/mrss
  52. *
  53. * @return
  54. * If ATOM+MRSS information is available, a SimpleXML element containing
  55. * ATOM and MRSS elements, as per those respective specifications.
  56. *
  57. * @todo Would be better for the return value to be an array rather than a
  58. * SimpleXML element, but media_retrieve_xml() needs to be upgraded to
  59. * handle namespaces first.
  60. */
  61. public function getMRSS() {
  62. $uri = $this->parse($this->embedCode);
  63. $video_id = arg(1, file_uri_target($uri));
  64. $rss_url = url('http://gdata.vimeo.com/feeds/api/videos/' . $video_id, array('query' => array('v' => '2')));
  65. // @todo Use media_retrieve_xml() once it's upgraded to include elements
  66. // from all namespaces, not just the document default namespace.
  67. $entry = simplexml_load_file($rss_url);
  68. return $entry;
  69. }
  70. /**
  71. * Returns information about the media.
  72. *
  73. * See http://www.oembed.com/ and https://vimeo.com/api/docs/oembed
  74. *
  75. * @return
  76. * If oEmbed information is available, an array containing 'title', 'type',
  77. * 'url', and other information as specified by the oEmbed standard.
  78. * Otherwise, NULL.
  79. */
  80. public function getOEmbed() {
  81. $uri = $this->parse($this->embedCode);
  82. $external_url = drupal_realpath($uri);
  83. $oembed_url = url('http://vimeo.com/api/oembed.json', array(
  84. 'query' => array('url' => $external_url))
  85. );
  86. $response = drupal_http_request($oembed_url);
  87. if (!isset($response->error)) {
  88. return drupal_json_decode($response->data);
  89. }
  90. }
  91. }