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 |
|
|
public function parse($embedCode) {
|
15 |
70a4c29b
|
Assos Assos
|
// 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 |
85ad3d82
|
Assos Assos
|
$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 |
70a4c29b
|
Assos Assos
|
|
30 |
85ad3d82
|
Assos Assos
|
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 |
70a4c29b
|
Assos Assos
|
// Try to default the file name to the video's title.
|
51 |
85ad3d82
|
Assos Assos
|
if (empty($file->fid) && $info = $this->getOEmbed()) {
|
52 |
|
|
$file->filename = truncate_utf8($info['title'], 255);
|
53 |
|
|
}
|
54 |
|
|
|
55 |
|
|
return $file;
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
/**
|
59 |
70a4c29b
|
Assos Assos
|
* Returns information about the media.
|
60 |
85ad3d82
|
Assos Assos
|
*
|
61 |
70a4c29b
|
Assos Assos
|
* See http://www.oembed.com.
|
62 |
85ad3d82
|
Assos Assos
|
*
|
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 |
70a4c29b
|
Assos Assos
|
|
74 |
85ad3d82
|
Assos Assos
|
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 |
70a4c29b
|
Assos Assos
|
|
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 |
85ad3d82
|
Assos Assos
|
} |