Project

General

Profile

Revision 70a4c29b

Added by Assos Assos over 9 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/media_youtube/includes/MediaInternetYouTubeHandler.inc
1 1
<?php
2 2

  
3 3
/**
4
 * @file media_youtube/includes/MediaInterenetYouTubeHandler.inc
5
 *
6
 * Contains MediaInternetYouTubeHandler.
4
 * @file
5
 * Extends the MediaInternetBaseHandler class to handle YouTube videos.
7 6
 */
8 7

  
9 8
/**
......
12 11
 * @see hook_media_internet_providers().
13 12
 */
14 13
class MediaInternetYouTubeHandler extends MediaInternetBaseHandler {
15
  /**
16
   * Check if a YouTube video id is valid.
17
   *
18
   * Check against the oembed stream instead of the gdata api site to
19
   * avoid "yt:quota too_many_recent_calls" errors.
20
   *
21
   * @return
22
   *   Boolean.
23
   */
24
  static public function validId($id) {
25
    $url = 'http://www.youtube.com/oembed?url=http%3A//youtube.com/watch%3Fv%3D'. $id;
26
    $response = drupal_http_request($url, array('method' => 'HEAD'));
27
    if ($response->code == 401) {
28
      throw new MediaInternetValidationException("Embedding has been disabled for this video.");
29
    }
30
    elseif ($response->code != 200) {
31
      throw new MediaInternetValidationException("The YouTube video ID is invalid or the video was deleted.");
32
    }
33
    return TRUE;
34
  }
35

  
36 14
  public function parse($embedCode) {
15
    // http://youtube.com/watch/*
16
    // http://youtube.com/embed/*
17
    // http://youtube.com/v/*
18
    // http://youtube.com/?v=*
19
    // http://youtu.be/*
20
    // http://gdata.youtube.com/feeds/api/videos/*
37 21
    $patterns = array(
38 22
      '@youtube\.com/watch[#\?].*?v=([^"\& ]+)@i',
39 23
      '@youtube\.com/embed/([^"\&\? ]+)@i',
......
42 26
      '@youtu\.be/([^"\&\? ]+)@i',
43 27
      '@gdata\.youtube\.com/feeds/api/videos/([^"\&\? ]+)@i',
44 28
    );
29

  
45 30
    foreach ($patterns as $pattern) {
46 31
      preg_match($pattern, $embedCode, $matches);
47 32
      // @TODO: Parse is called often. Refactor so that valid ID is checked
......
62 47
    $uri = $this->parse($this->embedCode);
63 48
    $file = file_uri_to_object($uri, TRUE);
64 49

  
50
    // Try to default the file name to the video's title.
65 51
    if (empty($file->fid) && $info = $this->getOEmbed()) {
66 52
      $file->filename = truncate_utf8($info['title'], 255);
67 53
    }
......
70 56
  }
71 57

  
72 58
  /**
73
   * Returns information about the media. See http://video.search.yahoo.com/mrss.
59
   * Returns information about the media.
74 60
   *
75
   * @return
76
   *   If ATOM+MRSS information is available, a SimpleXML element containing
77
   *   ATOM and MRSS elements, as per those respective specifications.
78
   *
79
   * @todo Would be better for the return value to be an array rather than a
80
   *   SimpleXML element, but media_retrieve_xml() needs to be upgraded to
81
   *   handle namespaces first.
82
   */
83
  public function getMRSS() {
84
    $uri = $this->parse($this->embedCode);
85
    $video_id = arg(1, file_uri_target($uri));
86
    $rss_url = url('http://gdata.youtube.com/feeds/api/videos/' . $video_id, array('query' => array('v' => '2')));
87
    // @todo Use media_retrieve_xml() once it's upgraded to include elements
88
    //   from all namespaces, not just the document default namespace.
89
    $request = drupal_http_request($rss_url);
90
    if (!isset($request->error)) {
91
      $entry = simplexml_load_string($request->data);
92
    }
93
    else {
94
      throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
95

  
96
      //if request wasn't successful, create object for return to avoid errors
97
      $entry = new SimpleXMLElement();
98
    }
99
    return $entry;
100
  }
101

  
102
  /**
103
   * Returns information about the media. See http://www.oembed.com/.
61
   * See http://www.oembed.com.
104 62
   *
105 63
   * @return
106 64
   *   If oEmbed information is available, an array containing 'title', 'type',
......
112 70
    $external_url = file_create_url($uri);
113 71
    $oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
114 72
    $response = drupal_http_request($oembed_url);
73

  
115 74
    if (!isset($response->error)) {
116 75
      return drupal_json_decode($response->data);
117 76
    }
......
120 79
      return;
121 80
    }
122 81
  }
82

  
83
  /**
84
   * Check if a YouTube video ID is valid.
85
   *
86
   * @return boolean
87
   *   TRUE if the video ID is valid, or throws a
88
   *   MediaInternetValidationException otherwise.
89
   */
90
  static public function validId($id) {
91
    $uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($id));
92
    $external_url = file_create_url($uri);
93
    $oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
94
    $response = drupal_http_request($oembed_url, array('method' => 'HEAD'));
95

  
96
    if ($response->code == 401) {
97
      throw new MediaInternetValidationException('Embedding has been disabled for this YouTube video.');
98
    }
99
    elseif ($response->code != 200) {
100
      throw new MediaInternetValidationException('The YouTube video ID is invalid or the video was deleted.');
101
    }
102

  
103
    return TRUE;
104
  }
123 105
}

Also available in: Unified diff