Projet

Général

Profil

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

root / drupal7 / sites / all / modules / media_youtube / includes / MediaInternetYouTubeHandler.inc @ 5e632cae

1
<?php
2

    
3
/**
4
 * @file
5
 * Extends the MediaInternetBaseHandler class to handle YouTube videos.
6
 */
7

    
8
/**
9
 * Implementation of MediaInternetBaseHandler.
10
 *
11
 * @see hook_media_internet_providers().
12
 */
13
class MediaInternetYouTubeHandler extends MediaInternetBaseHandler {
14

    
15
  public function parse($embedCode) {
16
    $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
    // https://youtube.com/watch/*
29
    // https://youtube.com/embed/*
30
    // https://youtube.com/v/*
31
    // https://youtube.com/?v=*
32
    // https://youtu.be/*
33
    // https://gdata.youtube.com/feeds/api/videos/*
34
    $patterns = array(
35
      '@youtube\.com/watch[#\?].*?v=([^"#\& ]+).*&list=([^"#\& ]+)@i',
36
      '@youtu\.be/([^"#\&\? ]+)\?list=([^"#\& ]+)@i',
37
      '@youtube\.com/embed/([^"#\&\? ]+)\?list=([^"#\& ]+)@i',
38
      '@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

    
46
    foreach ($patterns as $pattern) {
47
      preg_match_all($pattern, $embedCode, $matches);
48
      // @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
      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
      }
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
    // Try to default the file name to the video's title.
71
    if (empty($file->fid) && $info = $this->getOEmbed()) {
72
      $file->filename = truncate_utf8($info['title'], 255);
73
    }
74

    
75
    return $file;
76
  }
77

    
78
  /**
79
   * Returns information about the media.
80
   *
81
   * See http://www.oembed.com.
82
   *
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('https://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
92
    $response = drupal_http_request($oembed_url);
93

    
94
    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

    
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
  public function validId($id, $type = 'v') {
111
    $uri = file_stream_wrapper_uri_normalize('youtube://' . $type . '/' . check_plain($id));
112
    $external_url = file_create_url($uri);
113
    $oembed_url = url('https://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
114
    $response = drupal_http_request($oembed_url);
115

    
116
    if (!isset($response->error)) {
117
      $data = drupal_json_decode($response->data);
118
      if (!empty($data)) {
119
        return TRUE;
120
      }
121
      else {
122
        $error_data = t('Unspecific');
123
        if (is_string($response->data)) {
124
          $error_data = $response->data;
125
        }
126
        throw new MediaInternetValidationException("The YouTube video ID is invalid, video was deleted or is disabled for embedding. Error: {$error_data}");
127
        return;
128
      }
129
    }
130
    else {
131
      throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
132
      return;
133
    }
134
  }
135
}