Révision 18596a08
Ajouté par Assos Assos il y a presque 7 ans
drupal7/sites/all/modules/media_youtube/includes/MediaInternetYouTubeHandler.inc | ||
---|---|---|
11 | 11 |
* @see hook_media_internet_providers(). |
12 | 12 |
*/ |
13 | 13 |
class MediaInternetYouTubeHandler extends MediaInternetBaseHandler { |
14 |
|
|
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 |
} |
|
15 | 28 |
// http://youtube.com/watch/* |
16 | 29 |
// http://youtube.com/embed/* |
17 | 30 |
// http://youtube.com/v/* |
... | ... | |
19 | 32 |
// http://youtu.be/* |
20 | 33 |
// http://gdata.youtube.com/feeds/api/videos/* |
21 | 34 |
$patterns = array( |
35 |
'@youtube\.com/watch[#\?].*?v=([^"\& ]+).*&list=([^"\& ]+)@i', |
|
36 |
'@youtu\.be/([^"\&\? ]+)\?list=([^"\& ]+)@i', |
|
37 |
'@youtube\.com/embed/([^"\&\? ]+)\?list=([^"\& ]+)@i', |
|
22 | 38 |
'@youtube\.com/watch[#\?].*?v=([^"\& ]+)@i', |
23 | 39 |
'@youtube\.com/embed/([^"\&\? ]+)@i', |
24 | 40 |
'@youtube\.com/v/([^"\&\? ]+)@i', |
... | ... | |
28 | 44 |
); |
29 | 45 |
|
30 | 46 |
foreach ($patterns as $pattern) { |
31 |
preg_match($pattern, $embedCode, $matches); |
|
47 |
preg_match_all($pattern, $embedCode, $matches);
|
|
32 | 48 |
// @TODO: Parse is called often. Refactor so that valid ID is checked |
33 | 49 |
// when a video is added, but not every time the embedCode is parsed. |
34 |
if (isset($matches[1]) && self::validId($matches[1])) { |
|
35 |
return file_stream_wrapper_uri_normalize('youtube://v/' . $matches[1]); |
|
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); |
|
36 | 56 |
} |
37 | 57 |
} |
38 | 58 |
} |
... | ... | |
87 | 107 |
* TRUE if the video ID is valid, or throws a |
88 | 108 |
* MediaInternetValidationException otherwise. |
89 | 109 |
*/ |
90 |
static public function validId($id) {
|
|
91 |
$uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($id));
|
|
110 |
public function validId($id, $type = 'v') {
|
|
111 |
$uri = file_stream_wrapper_uri_normalize('youtube://' . $type . '/' . check_plain($id));
|
|
92 | 112 |
$external_url = file_create_url($uri); |
93 | 113 |
$oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json'))); |
94 | 114 |
$response = drupal_http_request($oembed_url, array('method' => 'HEAD')); |
Formats disponibles : Unified diff
Weekly update of contrib modules