Projet

Général

Profil

Paste
Télécharger (4,01 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / media_youtube / includes / MediaInternetYouTubeHandler.inc @ 31a5a6d6

1 85ad3d82 Assos Assos
<?php
2
3
/**
4 70a4c29b Assos Assos
 * @file
5
 * Extends the MediaInternetBaseHandler class to handle YouTube videos.
6 85ad3d82 Assos Assos
 */
7
8
/**
9
 * Implementation of MediaInternetBaseHandler.
10
 *
11
 * @see hook_media_internet_providers().
12
 */
13
class MediaInternetYouTubeHandler extends MediaInternetBaseHandler {
14 18596a08 Assos Assos
15 85ad3d82 Assos Assos
  public function parse($embedCode) {
16 18596a08 Assos Assos
    $list_patterns = array(
17
      '@youtube\.com/playlist[#\?].*?list=([^"\& ]+)@i',
18
      '@youtube\.com/view_play_list[#\?].*?p=([^"\& ]+)@i',
19
    );
20
21
    foreach ($list_patterns as $pattern) {
22
      preg_match($pattern, $embedCode, $matches);
23
24
      if (isset($matches[1]) && $this->validId($matches[1], 'l')) {
25
        return file_stream_wrapper_uri_normalize('youtube://l/' . $matches[1]);
26
      }
27
    }
28 70a4c29b Assos Assos
    // http://youtube.com/watch/*
29
    // http://youtube.com/embed/*
30
    // http://youtube.com/v/*
31
    // http://youtube.com/?v=*
32
    // http://youtu.be/*
33
    // http://gdata.youtube.com/feeds/api/videos/*
34 85ad3d82 Assos Assos
    $patterns = array(
35 18596a08 Assos Assos
      '@youtube\.com/watch[#\?].*?v=([^"\& ]+).*&list=([^"\& ]+)@i',
36
      '@youtu\.be/([^"\&\? ]+)\?list=([^"\& ]+)@i',
37
      '@youtube\.com/embed/([^"\&\? ]+)\?list=([^"\& ]+)@i',
38 85ad3d82 Assos Assos
      '@youtube\.com/watch[#\?].*?v=([^"\& ]+)@i',
39
      '@youtube\.com/embed/([^"\&\? ]+)@i',
40
      '@youtube\.com/v/([^"\&\? ]+)@i',
41
      '@youtube\.com/\?v=([^"\& ]+)@i',
42
      '@youtu\.be/([^"\&\? ]+)@i',
43
      '@gdata\.youtube\.com/feeds/api/videos/([^"\&\? ]+)@i',
44
    );
45 70a4c29b Assos Assos
46 85ad3d82 Assos Assos
    foreach ($patterns as $pattern) {
47 18596a08 Assos Assos
      preg_match_all($pattern, $embedCode, $matches);
48 85ad3d82 Assos Assos
      // @TODO: Parse is called often. Refactor so that valid ID is checked
49
      // when a video is added, but not every time the embedCode is parsed.
50 18596a08 Assos Assos
      if (isset($matches[1][0]) && $this->validId($matches[1][0])) {
51
        $uri = 'youtube://v/' . $matches[1][0];
52
        if (isset($matches[2][0]) && $this->validId($matches[2][0], 'l')) {
53
          $uri .= '/l/' . $matches[2][0];
54
         }
55
        return file_stream_wrapper_uri_normalize($uri);
56 85ad3d82 Assos Assos
      }
57
    }
58
  }
59
60
  public function claim($embedCode) {
61
    if ($this->parse($embedCode)) {
62
      return TRUE;
63
    }
64
  }
65
66
  public function getFileObject() {
67
    $uri = $this->parse($this->embedCode);
68
    $file = file_uri_to_object($uri, TRUE);
69
70 70a4c29b Assos Assos
    // Try to default the file name to the video's title.
71 85ad3d82 Assos Assos
    if (empty($file->fid) && $info = $this->getOEmbed()) {
72
      $file->filename = truncate_utf8($info['title'], 255);
73
    }
74
75
    return $file;
76
  }
77
78
  /**
79 70a4c29b Assos Assos
   * Returns information about the media.
80 85ad3d82 Assos Assos
   *
81 70a4c29b Assos Assos
   * See http://www.oembed.com.
82 85ad3d82 Assos Assos
   *
83
   * @return
84
   *   If oEmbed information is available, an array containing 'title', 'type',
85
   *   'url', and other information as specified by the oEmbed standard.
86
   *   Otherwise, NULL.
87
   */
88
  public function getOEmbed() {
89
    $uri = $this->parse($this->embedCode);
90
    $external_url = file_create_url($uri);
91
    $oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
92
    $response = drupal_http_request($oembed_url);
93 70a4c29b Assos Assos
94 85ad3d82 Assos Assos
    if (!isset($response->error)) {
95
      return drupal_json_decode($response->data);
96
    }
97
    else {
98
      throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
99
      return;
100
    }
101
  }
102 70a4c29b Assos Assos
103
  /**
104
   * Check if a YouTube video ID is valid.
105
   *
106
   * @return boolean
107
   *   TRUE if the video ID is valid, or throws a
108
   *   MediaInternetValidationException otherwise.
109
   */
110 18596a08 Assos Assos
  public function validId($id, $type = 'v') {
111
    $uri = file_stream_wrapper_uri_normalize('youtube://' . $type . '/' . check_plain($id));
112 70a4c29b Assos Assos
    $external_url = file_create_url($uri);
113
    $oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
114
    $response = drupal_http_request($oembed_url, array('method' => 'HEAD'));
115
116
    if ($response->code == 401) {
117
      throw new MediaInternetValidationException('Embedding has been disabled for this YouTube video.');
118
    }
119
    elseif ($response->code != 200) {
120
      throw new MediaInternetValidationException('The YouTube video ID is invalid or the video was deleted.');
121
    }
122
123
    return TRUE;
124
  }
125 85ad3d82 Assos Assos
}