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
|
public function parse($embedCode) {
|
15
|
// http://youtube.com/watch/*
|
16
|
// http://youtube.com/embed/*
|
17
|
// http://youtube.com/v/*
|
18
|
// http://youtube.com/?v=*
|
19
|
// http://youtu.be/*
|
20
|
// http://gdata.youtube.com/feeds/api/videos/*
|
21
|
$patterns = array(
|
22
|
'@youtube\.com/watch[#\?].*?v=([^"\& ]+)@i',
|
23
|
'@youtube\.com/embed/([^"\&\? ]+)@i',
|
24
|
'@youtube\.com/v/([^"\&\? ]+)@i',
|
25
|
'@youtube\.com/\?v=([^"\& ]+)@i',
|
26
|
'@youtu\.be/([^"\&\? ]+)@i',
|
27
|
'@gdata\.youtube\.com/feeds/api/videos/([^"\&\? ]+)@i',
|
28
|
);
|
29
|
|
30
|
foreach ($patterns as $pattern) {
|
31
|
preg_match($pattern, $embedCode, $matches);
|
32
|
// @TODO: Parse is called often. Refactor so that valid ID is checked
|
33
|
// 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]);
|
36
|
}
|
37
|
}
|
38
|
}
|
39
|
|
40
|
public function claim($embedCode) {
|
41
|
if ($this->parse($embedCode)) {
|
42
|
return TRUE;
|
43
|
}
|
44
|
}
|
45
|
|
46
|
public function getFileObject() {
|
47
|
$uri = $this->parse($this->embedCode);
|
48
|
$file = file_uri_to_object($uri, TRUE);
|
49
|
|
50
|
// Try to default the file name to the video's title.
|
51
|
if (empty($file->fid) && $info = $this->getOEmbed()) {
|
52
|
$file->filename = truncate_utf8($info['title'], 255);
|
53
|
}
|
54
|
|
55
|
return $file;
|
56
|
}
|
57
|
|
58
|
/**
|
59
|
* Returns information about the media.
|
60
|
*
|
61
|
* See http://www.oembed.com.
|
62
|
*
|
63
|
* @return
|
64
|
* If oEmbed information is available, an array containing 'title', 'type',
|
65
|
* 'url', and other information as specified by the oEmbed standard.
|
66
|
* Otherwise, NULL.
|
67
|
*/
|
68
|
public function getOEmbed() {
|
69
|
$uri = $this->parse($this->embedCode);
|
70
|
$external_url = file_create_url($uri);
|
71
|
$oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
|
72
|
$response = drupal_http_request($oembed_url);
|
73
|
|
74
|
if (!isset($response->error)) {
|
75
|
return drupal_json_decode($response->data);
|
76
|
}
|
77
|
else {
|
78
|
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
|
79
|
return;
|
80
|
}
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Check if a YouTube video ID is valid.
|
85
|
*
|
86
|
* @return boolean
|
87
|
* TRUE if the video ID is valid, or throws a
|
88
|
* MediaInternetValidationException otherwise.
|
89
|
*/
|
90
|
static public function validId($id) {
|
91
|
$uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($id));
|
92
|
$external_url = file_create_url($uri);
|
93
|
$oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
|
94
|
$response = drupal_http_request($oembed_url, array('method' => 'HEAD'));
|
95
|
|
96
|
if ($response->code == 401) {
|
97
|
throw new MediaInternetValidationException('Embedding has been disabled for this YouTube video.');
|
98
|
}
|
99
|
elseif ($response->code != 200) {
|
100
|
throw new MediaInternetValidationException('The YouTube video ID is invalid or the video was deleted.');
|
101
|
}
|
102
|
|
103
|
return TRUE;
|
104
|
}
|
105
|
}
|