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 |
3acd948f
|
Assos Assos
|
'@youtube\.com/playlist[#\?].*?list=([^"#\& ]+)@i',
|
18 |
|
|
'@youtube\.com/view_play_list[#\?].*?p=([^"#\& ]+)@i',
|
19 |
18596a08
|
Assos Assos
|
);
|
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 |
639c8fcb
|
Assos Assos
|
// 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 |
85ad3d82
|
Assos Assos
|
$patterns = array(
|
35 |
3acd948f
|
Assos Assos
|
'@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 |
85ad3d82
|
Assos Assos
|
);
|
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 |
3acd948f
|
Assos Assos
|
}
|
55 |
18596a08
|
Assos Assos
|
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 |
639c8fcb
|
Assos Assos
|
$oembed_url = url('https://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
|
92 |
85ad3d82
|
Assos Assos
|
$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 |
639c8fcb
|
Assos Assos
|
$oembed_url = url('https://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
|
114 |
5e632cae
|
Assos Assos
|
$response = drupal_http_request($oembed_url);
|
115 |
70a4c29b
|
Assos Assos
|
|
116 |
5e632cae
|
Assos Assos
|
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 |
70a4c29b
|
Assos Assos
|
}
|
130 |
5e632cae
|
Assos Assos
|
else {
|
131 |
|
|
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
|
132 |
|
|
return;
|
133 |
70a4c29b
|
Assos Assos
|
}
|
134 |
|
|
}
|
135 |
85ad3d82
|
Assos Assos
|
} |