1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file media_youtube/includes/MediaInterenetYouTubeHandler.inc
|
5
|
*
|
6
|
* Contains MediaInternetYouTubeHandler.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Implementation of MediaInternetBaseHandler.
|
11
|
*
|
12
|
* @see hook_media_internet_providers().
|
13
|
*/
|
14
|
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
|
public function parse($embedCode) {
|
37
|
$patterns = array(
|
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
|
foreach ($patterns as $pattern) {
|
46
|
preg_match($pattern, $embedCode, $matches);
|
47
|
// @TODO: Parse is called often. Refactor so that valid ID is checked
|
48
|
// when a video is added, but not every time the embedCode is parsed.
|
49
|
if (isset($matches[1]) && self::validId($matches[1])) {
|
50
|
return file_stream_wrapper_uri_normalize('youtube://v/' . $matches[1]);
|
51
|
}
|
52
|
}
|
53
|
}
|
54
|
|
55
|
public function claim($embedCode) {
|
56
|
if ($this->parse($embedCode)) {
|
57
|
return TRUE;
|
58
|
}
|
59
|
}
|
60
|
|
61
|
public function getFileObject() {
|
62
|
$uri = $this->parse($this->embedCode);
|
63
|
$file = file_uri_to_object($uri, TRUE);
|
64
|
|
65
|
if (empty($file->fid) && $info = $this->getOEmbed()) {
|
66
|
$file->filename = truncate_utf8($info['title'], 255);
|
67
|
}
|
68
|
|
69
|
return $file;
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Returns information about the media. See http://video.search.yahoo.com/mrss.
|
74
|
*
|
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/.
|
104
|
*
|
105
|
* @return
|
106
|
* If oEmbed information is available, an array containing 'title', 'type',
|
107
|
* 'url', and other information as specified by the oEmbed standard.
|
108
|
* Otherwise, NULL.
|
109
|
*/
|
110
|
public function getOEmbed() {
|
111
|
$uri = $this->parse($this->embedCode);
|
112
|
$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);
|
115
|
if (!isset($response->error)) {
|
116
|
return drupal_json_decode($response->data);
|
117
|
}
|
118
|
else {
|
119
|
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
|
120
|
return;
|
121
|
}
|
122
|
}
|
123
|
}
|