1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Provides a stream wrapper and formatters appropriate for accessing and
|
6
|
* displaying YouTube videos.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* This is the rest point for the YouTube api.
|
11
|
*
|
12
|
* Avoid using the gdata api url when possible. Too many calls will result in
|
13
|
* throttling and 403 errors.
|
14
|
*/
|
15
|
define('MEDIA_YOUTUBE_REST_API', 'https://gdata.youtube.com/feeds/api/videos');
|
16
|
|
17
|
// Load all YouTube file formatters.
|
18
|
require_once dirname(__FILE__) . '/includes/media_youtube.formatters.inc';
|
19
|
|
20
|
/**
|
21
|
* Implements hook_media_internet_providers().
|
22
|
*/
|
23
|
function media_youtube_media_internet_providers() {
|
24
|
return array(
|
25
|
'MediaInternetYouTubeHandler' => array(
|
26
|
'title' => t('YouTube'),
|
27
|
),
|
28
|
);
|
29
|
}
|
30
|
|
31
|
/**
|
32
|
* Implements hook_stream_wrappers().
|
33
|
*/
|
34
|
function media_youtube_stream_wrappers() {
|
35
|
return array(
|
36
|
'youtube' => array(
|
37
|
'name' => t('YouTube videos'),
|
38
|
'class' => 'MediaYouTubeStreamWrapper',
|
39
|
'description' => t('Remote videos hosted on the YouTube video-sharing website.'),
|
40
|
'type' => STREAM_WRAPPERS_READ_VISIBLE,
|
41
|
),
|
42
|
);
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Implements hook_theme().
|
47
|
*/
|
48
|
function media_youtube_theme($existing, $type, $theme, $path) {
|
49
|
return array(
|
50
|
'media_youtube_video' => array(
|
51
|
'variables' => array('uri' => NULL, 'options' => array()),
|
52
|
'file' => 'media_youtube.theme.inc',
|
53
|
'path' => $path . '/themes',
|
54
|
'template' => 'media-youtube-video',
|
55
|
),
|
56
|
);
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* Implements hook_media_parse().
|
61
|
*
|
62
|
* @todo This hook should be deprecated. Refactor Media module to not call it
|
63
|
* any more, since media_internet should be able to automatically route to the
|
64
|
* appropriate handler.
|
65
|
*/
|
66
|
function media_youtube_media_parse($embed_code) {
|
67
|
$handler = new MediaInternetYouTubeHandler($embed_code);
|
68
|
return $handler->parse($embed_code);
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Implements hook_file_mimetype_mapping_alter().
|
73
|
*/
|
74
|
function media_youtube_file_mimetype_mapping_alter(&$mapping) {
|
75
|
$mapping['mimetypes'][] = 'video/youtube';
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Implements hook_ctools_plugin_api().
|
80
|
*/
|
81
|
function media_youtube_ctools_plugin_api($module, $api) {
|
82
|
if ($module == 'file_entity' && $api == 'file_default_displays') {
|
83
|
return array('version' => 1);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* YouTube search tab for the Media browser.
|
89
|
*/
|
90
|
|
91
|
/**
|
92
|
* Implements hook_media_browser_plugin_info().
|
93
|
*
|
94
|
* Commented out for release versions, active in dev versions. To enable the
|
95
|
* YouTube media browser tab, uncomment this function.
|
96
|
*/
|
97
|
function media_youtube_media_browser_plugin_info() {
|
98
|
$info['youtube'] = array(
|
99
|
'title' => t('YouTube'),
|
100
|
'class' => 'MediaYouTubeBrowser',
|
101
|
);
|
102
|
|
103
|
return $info;
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Provides a form for adding media items from YouTube search.
|
108
|
*/
|
109
|
function media_youtube_add($form, &$form_state = array()) {
|
110
|
module_load_include('inc', 'media', 'includes/media.browser');
|
111
|
|
112
|
// Our search term can come from the form, or from the pager.
|
113
|
$term = isset($form_state['input']['search']) ? $form_state['input']['search'] : (isset($_GET['search']) ? $_GET['search'] : '');
|
114
|
|
115
|
$form['search'] = array(
|
116
|
'#type' => 'textfield',
|
117
|
'#title' => t('Search'),
|
118
|
'#description' => t('Input a phrase or tags to search.'),
|
119
|
'#default_value' => $term,
|
120
|
);
|
121
|
$form['apply'] = array(
|
122
|
'#type' => 'button',
|
123
|
'#value' => t('Apply'),
|
124
|
);
|
125
|
|
126
|
// This is our half-assed pager.
|
127
|
$page = isset($_GET['page-yt']) ? $_GET['page-yt'] : 0;
|
128
|
if (isset($form_state['input']['search'])) {
|
129
|
// Reset the pager when we press apply.
|
130
|
$page = 0;
|
131
|
}
|
132
|
if (!empty($term)) {
|
133
|
$search = media_youtube_video_search(array('q' => $term, 'max-results' => 12, 'start-index' => $page * 12 + 1));
|
134
|
}
|
135
|
$form['videos']['#prefix'] = '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails">';
|
136
|
$form['videos']['#suffix'] = '</ul><div id="status"></div></div></div>';
|
137
|
|
138
|
$empty = FALSE;
|
139
|
$files = array();
|
140
|
if (!isset($search['entry'])) {
|
141
|
$empty = TRUE;
|
142
|
}
|
143
|
else {
|
144
|
// $search['entry'] is different depending on whether there is a single
|
145
|
// result or multiple results. So normalise it.
|
146
|
$videos = isset($search['entry']['id']) ? array($search['entry']) : $search['entry'];
|
147
|
foreach ($videos as $video) {
|
148
|
try {
|
149
|
$uri = media_parse_to_uri($video['link'][0]['@attributes']['href']);
|
150
|
}
|
151
|
catch (Exception $e) {
|
152
|
// Ignore invalid videos.
|
153
|
continue;
|
154
|
}
|
155
|
// Create a temporary file object for our retrieved video.
|
156
|
$file = file_uri_to_object($uri);
|
157
|
$file->type = 'video';
|
158
|
if (!isset($file->fid)) {
|
159
|
$file->fid = 0;
|
160
|
}
|
161
|
media_browser_build_media_item($file);
|
162
|
$attributes = array(
|
163
|
'data-uri' => $uri,
|
164
|
'class' => array('media-youtube-wrapper'),
|
165
|
);
|
166
|
$form['videos'][$uri] = array(
|
167
|
'#markup' => $file->preview,
|
168
|
'#prefix' => '<li' . drupal_attributes($attributes) . '>',
|
169
|
'#suffix' => '</li>',
|
170
|
);
|
171
|
$files[$uri] = $file;
|
172
|
}
|
173
|
}
|
174
|
|
175
|
if (!count($files)) {
|
176
|
$empty= TRUE;
|
177
|
}
|
178
|
if ($empty) {
|
179
|
$form['empty'] = array(
|
180
|
'#markup' => '<div class="empty-message">' . t('No videos match your search criteria. Please try again.') . '</div>',
|
181
|
);
|
182
|
}
|
183
|
|
184
|
$query = $_GET;
|
185
|
if ($term !== '') {
|
186
|
$query['search'] = $term;
|
187
|
}
|
188
|
|
189
|
$dest = $query['q'];
|
190
|
unset($query['q']);
|
191
|
$prev = $next = '';
|
192
|
if ($page) {
|
193
|
$query['page-yt'] = $page - 1;
|
194
|
$prev = l(t('previous'), $dest, array('query' => $query));
|
195
|
}
|
196
|
$query['page-yt'] = $page + 1;
|
197
|
if (!$empty) {
|
198
|
$next = l(t('next'), $dest, array('query' => $query));
|
199
|
}
|
200
|
|
201
|
$form['pager']= array(
|
202
|
'#markup' => $prev . ' ' . $next,
|
203
|
);
|
204
|
|
205
|
$form['submitted-video'] = array(
|
206
|
'#type' => 'hidden',
|
207
|
'#default_value' => FALSE,
|
208
|
);
|
209
|
|
210
|
// Add the files to JS so that they are accessible inside the browser
|
211
|
drupal_add_js(array('media' => array('files' => $files)), 'setting');
|
212
|
|
213
|
// Add media browser javascript and CSS.
|
214
|
drupal_add_js(drupal_get_path('module', 'media_youtube') . '/js/media-youtube.browser.js');
|
215
|
|
216
|
// @TODO: Remove deprecated library js and css. They're removed in Media,
|
217
|
// so let's comment out for now.
|
218
|
// drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.library.js');
|
219
|
// drupal_add_css(drupal_get_path('module', 'media') . '/js/plugins/media.library.css');
|
220
|
|
221
|
$form['actions'] = array('#type' => 'actions');
|
222
|
$form['actions']['submit'] = array(
|
223
|
'#type' => 'submit',
|
224
|
'#value' => t('Submit'),
|
225
|
);
|
226
|
return $form;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Allow stream wrappers to have their chance at validation.
|
231
|
*
|
232
|
* Any module that implements hook_media_parse will have an
|
233
|
* opportunity to validate this.
|
234
|
*
|
235
|
* @see media_parse_to_uri()
|
236
|
*/
|
237
|
function media_youtube_add_validate($form, &$form_state) {
|
238
|
if ($form_state['values']['op'] == t('Apply')) {
|
239
|
return;
|
240
|
}
|
241
|
$uri = $form_state['values']['submitted-video'];
|
242
|
try {
|
243
|
$file = file_uri_to_object($uri, TRUE);
|
244
|
}
|
245
|
catch (Exception $e) {
|
246
|
form_set_error('url', $e->getMessage());
|
247
|
return;
|
248
|
}
|
249
|
|
250
|
if (!$file->uri) {
|
251
|
form_set_error('url', t('Please select a video.'));
|
252
|
return;
|
253
|
}
|
254
|
|
255
|
$validators = isset($form['#validators']) ? $form['#validators'] : array();
|
256
|
if ($validators) {
|
257
|
// Check for errors. @see media_add_upload_validate calls file_save_upload().
|
258
|
// this code is ripped from file_save_upload because we just want the validation part.
|
259
|
// Call the validation functions specified by this function's caller.
|
260
|
$errors = file_validate($file, $validators);
|
261
|
|
262
|
if (!empty($errors)) {
|
263
|
$message = t('%uri could not be added.', array('%uri' => $uri));
|
264
|
if (count($errors) > 1) {
|
265
|
$message .= theme('item_list', array('items' => $errors));
|
266
|
}
|
267
|
else {
|
268
|
$message .= ' ' . array_pop($errors);
|
269
|
}
|
270
|
form_set_error('url', $message);
|
271
|
return FALSE;
|
272
|
}
|
273
|
}
|
274
|
// @TODO: Validate that if we have no $uri that this is a valid file to
|
275
|
// save. For instance, we may only be interested in images, and it would
|
276
|
// be helpful to let the user know they passed the HTML page containing
|
277
|
// the image accidentally. That would also save us from saving the file
|
278
|
// in the submit step.
|
279
|
|
280
|
// This is kinda a hack of the same.
|
281
|
|
282
|
// This should use the file_validate routines that the upload form users.
|
283
|
// We need to fix the media_parse_to_file routine to allow for a validation.
|
284
|
}
|
285
|
|
286
|
/**
|
287
|
* @TODO: Document this function.
|
288
|
*/
|
289
|
function media_youtube_add_submit($form, &$form_state) {
|
290
|
$uri = $form_state['values']['submitted-video'];
|
291
|
try {
|
292
|
// Save the remote file
|
293
|
$file = file_uri_to_object($uri, TRUE);
|
294
|
file_save($file);
|
295
|
}
|
296
|
catch (Exception $e) {
|
297
|
form_set_error('url', $e->getMessage());
|
298
|
return;
|
299
|
}
|
300
|
|
301
|
if (!$file->fid) {
|
302
|
form_set_error('url', t('The file %file could not be saved. An unknown error has occurred.', array('%file' => $uri)));
|
303
|
return;
|
304
|
}
|
305
|
else {
|
306
|
$form_state['file'] = $file;
|
307
|
}
|
308
|
|
309
|
// Redirect to the file edit page after submission.
|
310
|
if (media_youtube_access('update', $file)) {
|
311
|
$destination = array('destination' => 'admin/content/file');
|
312
|
if (isset($_GET['destination'])) {
|
313
|
$destination = drupal_get_destination();
|
314
|
unset($_GET['destination']);
|
315
|
}
|
316
|
$form_state['redirect'] = array('file/' . $file->fid . '/edit', array('query' => $destination));
|
317
|
}
|
318
|
else {
|
319
|
$form_state['redirect'] = 'admin/content/file';
|
320
|
}
|
321
|
}
|
322
|
|
323
|
/**
|
324
|
* Determine if a user may perform the given operation on the specified file.
|
325
|
*
|
326
|
* Enables compatibility with Media 1.x and 2.x by providing a wrapper around
|
327
|
* both media_access() and file_entity_access().
|
328
|
*
|
329
|
* @return boolean
|
330
|
* TRUE if the operation may be performed, FALSE otherwise.
|
331
|
*
|
332
|
* @see media_access()
|
333
|
* @see file_entity_access()
|
334
|
*/
|
335
|
function media_youtube_access($op, $file = NULL, $account = NULL) {
|
336
|
if (function_exists('file_entity_access')) {
|
337
|
$access = file_entity_access($op, $file, $account);
|
338
|
}
|
339
|
elseif (function_exists('media_access')) {
|
340
|
$access = media_access($op, $account);
|
341
|
}
|
342
|
else {
|
343
|
$access = FALSE;
|
344
|
}
|
345
|
|
346
|
return $access;
|
347
|
}
|
348
|
|
349
|
/**
|
350
|
* @TODO: Document this function.
|
351
|
*/
|
352
|
function media_youtube_video_search($options = array()) {
|
353
|
$options['v'] = 2;
|
354
|
|
355
|
$request = drupal_http_request(url(MEDIA_YOUTUBE_REST_API, array('query' => $options)));
|
356
|
if (!isset($request->error)) {
|
357
|
$entry = simplexml_load_string($request->data);
|
358
|
}
|
359
|
else {
|
360
|
throw new Exception("Error Processing Request. (Error: {$request->code}, {$request->error})");
|
361
|
|
362
|
//if request wasn't successful, create object for return to avoid errors
|
363
|
$entry = new SimpleXMLElement();
|
364
|
}
|
365
|
|
366
|
return media_youtube_unserialize_xml($entry);
|
367
|
}
|
368
|
|
369
|
/**
|
370
|
* Recursively converts a SimpleXMLElement object into an array.
|
371
|
*
|
372
|
* @param object $xml
|
373
|
* The original XML object.
|
374
|
*/
|
375
|
function media_youtube_unserialize_xml($xml) {
|
376
|
if ($xml instanceof SimpleXMLElement) {
|
377
|
$xml = (array) $xml;
|
378
|
}
|
379
|
if (is_array($xml)) {
|
380
|
foreach ($xml as $key => $item) {
|
381
|
$xml[$key] = media_youtube_unserialize_xml($item);
|
382
|
}
|
383
|
}
|
384
|
return $xml;
|
385
|
}
|
386
|
|
387
|
/**
|
388
|
* Check to ensure that a given id is valid.
|
389
|
*
|
390
|
* @param string $id
|
391
|
* The YouTube video id.
|
392
|
* @param boolean $refresh
|
393
|
* (Defaults to FALSE) If TRUE, then reset the value from the cache.
|
394
|
* @return boolean
|
395
|
* Returns TRUE if the video is valid.
|
396
|
*
|
397
|
* @TODO: How does this compare to MediaInternetYouTubeHandler's validId
|
398
|
* method, and can we refactor the code to rely on only one of them?
|
399
|
*/
|
400
|
function media_youtube_valid_id($id, $refresh = FALSE) {
|
401
|
$ids = &drupal_static(__FUNCTION__, array());
|
402
|
|
403
|
// Return our cached id if allowed, and it exists.
|
404
|
if (!$refresh && isset($ids[$id])) {
|
405
|
return $ids[$id];
|
406
|
}
|
407
|
elseif (!$refresh && !isset($ids[$id])) {
|
408
|
return $id;
|
409
|
}
|
410
|
elseif (!$refresh && $cache = cache_get('media_youtube:id:' . $id, 'cache_media_xml')) {
|
411
|
$ids[$id] = $cache->data;
|
412
|
return $ids[$id];
|
413
|
}
|
414
|
|
415
|
$url = url(MEDIA_YOUTUBE_REST_API . '/' . $id);
|
416
|
$response = drupal_http_request($url, array('method' => 'HEAD'));
|
417
|
$ids[$id] = ($response->code == 200);
|
418
|
cache_set('media_youtube:id:' . $id, $ids[$id], 'cache_media_xml', media_variable_get('xml_cache_expire', 3600));
|
419
|
return $ids[$id];
|
420
|
}
|