1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Extends the MediaInternetYouTubeHandler class to make it suitable for local testing.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* A test handler for YouTube videos.
|
10
|
*
|
11
|
* @see MediaInternetYouTubeHandler().
|
12
|
*/
|
13
|
class MediaYouTubeTestHandler extends MediaInternetYouTubeHandler {
|
14
|
public function getOEmbed() {
|
15
|
$uri = $this->parse($this->embedCode);
|
16
|
$external_url = file_create_url($uri);
|
17
|
$oembed_url = url('media-youtube-test/oembed', array('query' => array('url' => $external_url, 'format' => 'json'), 'absolute' => TRUE));
|
18
|
$response = drupal_http_request($oembed_url);
|
19
|
|
20
|
if (!isset($response->error)) {
|
21
|
return drupal_json_decode($response->data);
|
22
|
}
|
23
|
else {
|
24
|
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})");
|
25
|
return;
|
26
|
}
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Check if a YouTube video ID is valid.
|
31
|
*
|
32
|
* @return boolean
|
33
|
* TRUE if the video ID is valid, or throws a
|
34
|
* MediaInternetValidationException otherwise.
|
35
|
*/
|
36
|
public function validId($id, $type = 'v') {
|
37
|
$uri = file_stream_wrapper_uri_normalize('youtube://' . $type . '/' . check_plain($id));
|
38
|
$external_url = file_create_url($uri);
|
39
|
$oembed_url = url('media-youtube-test/oembed', array('query' => array('url' => $external_url, 'format' => 'json'), 'absolute' => TRUE));
|
40
|
$response = drupal_http_request($oembed_url, array('method' => 'HEAD'));
|
41
|
|
42
|
if ($response->code == 401) {
|
43
|
throw new MediaInternetValidationException('Embedding has been disabled for this YouTube video.');
|
44
|
}
|
45
|
elseif ($response->code != 200) {
|
46
|
throw new MediaInternetValidationException('The YouTube video ID is invalid or the video was deleted.');
|
47
|
}
|
48
|
|
49
|
return TRUE;
|
50
|
}
|
51
|
}
|