Révision 58344a8d
Ajouté par Assos Assos il y a presque 9 ans
drupal7/sites/all/modules/media_youtube/includes/MediaYouTubeBrowser.inc | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
/** |
|
4 |
* @file media_youtube/includes/MediaYouTubeBrowser.inc |
|
5 |
* |
|
6 |
* Definition of MediaYouTubeBrowser. |
|
7 |
*/ |
|
8 |
|
|
9 |
/** |
|
10 |
* Media browser plugin for displaying a specific view and display. |
|
11 |
*/ |
|
12 |
class MediaYouTubeBrowser extends MediaBrowserPlugin { |
|
13 |
/** |
|
14 |
* Implements MediaBrowserPluginInterface::access(). |
|
15 |
*/ |
|
16 |
public function access($account = NULL) { |
|
17 |
return media_internet_access($account); |
|
18 |
} |
|
19 |
|
|
20 |
/** |
|
21 |
* Implements MediaBrowserPlugin::view(). |
|
22 |
*/ |
|
23 |
public function view() { |
|
24 |
$build = array(); |
|
25 |
$params = $this->params; |
|
26 |
$build['form'] = drupal_get_form('media_youtube_add', $params); |
|
27 |
|
|
28 |
return $build; |
|
29 |
} |
|
30 |
} |
drupal7/sites/all/modules/media_youtube/includes/MediaYouTubeStreamWrapper.inc | ||
---|---|---|
18 | 18 |
|
19 | 19 |
function getOriginalThumbnailPath() { |
20 | 20 |
$parts = $this->get_parameters(); |
21 |
$v = check_plain($parts['v']);
|
|
22 |
// Attempt to pull a HD thumbnail from YouTube. If it exists pass it on
|
|
23 |
// otherwise pass on the smaller one.
|
|
24 |
$thumbname = drupal_tempnam('temporary://', 'youtube');
|
|
25 |
$response = drupal_http_request('http://img.youtube.com/vi/' . $v . '/maxresdefault.jpg'); |
|
21 |
$uri = file_stream_wrapper_uri_normalize('youtube://v/' . check_plain($parts['v']));
|
|
22 |
$external_url = file_create_url($uri);
|
|
23 |
$oembed_url = url('http://www.youtube.com/oembed', array('query' => array('url' => $external_url, 'format' => 'json')));
|
|
24 |
$response = drupal_http_request($oembed_url);
|
|
25 |
|
|
26 | 26 |
if (!isset($response->error)) { |
27 |
file_unmanaged_save_data($response->data, $thumbname, $replace = FILE_EXISTS_REPLACE); |
|
28 |
} |
|
29 |
if ((filesize($thumbname)) == 0) { |
|
30 |
return 'http://img.youtube.com/vi/' . $v . '/0.jpg'; |
|
27 |
$data = drupal_json_decode($response->data); |
|
28 |
return $data['thumbnail_url']; |
|
31 | 29 |
} |
32 | 30 |
else { |
33 |
return 'http://img.youtube.com/vi/' . $v . '/maxresdefault.jpg'; |
|
31 |
throw new Exception("Error Processing Request. (Error: {$response->code}, {$response->error})"); |
|
32 |
return; |
|
34 | 33 |
} |
35 | 34 |
} |
36 | 35 |
|
drupal7/sites/all/modules/media_youtube/js/media-youtube.browser.js | ||
---|---|---|
1 |
/** |
|
2 |
* @file |
|
3 |
* Handles the JS for the views file browser. Note that this does not currently |
|
4 |
* support multiple file selection |
|
5 |
*/ |
|
6 |
|
|
7 |
|
|
8 |
(function ($) { |
|
9 |
|
|
10 |
Drupal.behaviors.mediaYouTubeBrowser = { |
|
11 |
attach: function (context, settings) { |
|
12 |
|
|
13 |
// Container for the files that get passed back to the browser |
|
14 |
var files = {}; |
|
15 |
|
|
16 |
// Disable the links on media items list |
|
17 |
$('ul#media-browser-library-list a').click(function() { |
|
18 |
return false; |
|
19 |
}); |
|
20 |
|
|
21 |
// Catch the click on a media item |
|
22 |
$('#media-youtube-add .media-item').bind('click', function () { |
|
23 |
// Remove all currently selected files |
|
24 |
$('.media-item').removeClass('selected'); |
|
25 |
// Set the current item to active |
|
26 |
$(this).addClass('selected'); |
|
27 |
// Add this FID to the array of selected files |
|
28 |
var uri = $(this).parent('li.media-youtube-wrapper[data-uri]').attr('data-uri'); |
|
29 |
// Get the file from the settings which was stored in |
|
30 |
// template_preprocess_media_views_view_media_browser() |
|
31 |
var file = Drupal.settings.media.files[uri]; |
|
32 |
var files = new Array(); |
|
33 |
files.push(file); |
|
34 |
Drupal.media.browser.selectMedia(files); |
|
35 |
$("input[name='submitted-video']").val(uri); |
|
36 |
}); |
|
37 |
|
|
38 |
// $('.') |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
}(jQuery)); |
drupal7/sites/all/modules/media_youtube/js/media_youtube.fromurl.js | ||
---|---|---|
1 |
|
|
2 |
/** |
|
3 |
* @file |
|
4 |
* Create the 'YouTube' tab for the WYSIWYG plugins. |
|
5 |
*/ |
|
6 |
|
|
7 |
// (function ($) { |
|
8 |
// namespace('Drupal.media.browser.plugin'); |
|
9 |
// |
|
10 |
// Drupal.media.browser.plugin.media_youtube = function(mediaBrowser, options) { |
|
11 |
// return { |
|
12 |
// init: function() { |
|
13 |
// tabset = mediaBrowser.getTabset(); |
|
14 |
// tabset.tabs('add', '#media_youtube', 'YouTube'); |
|
15 |
// mediaBrowser.listen('tabs.show', function (e, id) { |
|
16 |
// if (id == 'media_youtube') { |
|
17 |
// // We only need to set this once. |
|
18 |
// // We probably could set it upon load. |
|
19 |
// if (mediaBrowser.getActivePanel().html() == '') { |
|
20 |
// mediaBrowser.getActivePanel().html(options.media_youtube); |
|
21 |
// } |
|
22 |
// } |
|
23 |
// }); |
|
24 |
// } |
|
25 |
// } |
|
26 |
// }; |
|
27 |
// |
|
28 |
// // For now, I guess self registration makes sense. |
|
29 |
// // Really though, we should be doing it via drupal_add_js and some settings |
|
30 |
// // from the drupal variable. |
|
31 |
// //@todo: needs a review. |
|
32 |
// Drupal.media.browser.register('media_youtube', Drupal.media.browser.plugin.media_youtube, {}); |
|
33 |
// })(jQuery); |
|
34 |
|
|
35 |
(function ($) { |
|
36 |
namespace('media.browser.plugin'); |
|
37 |
|
|
38 |
Drupal.media.browser.plugin.youtube_library = function(mediaBrowser, options) { |
|
39 |
|
|
40 |
return { |
|
41 |
mediaFiles: [], |
|
42 |
init: function() { |
|
43 |
tabset = mediaBrowser.getTabset(); |
|
44 |
tabset.tabs('add', '#youtube_library', 'YouTube'); |
|
45 |
var that = this; |
|
46 |
mediaBrowser.listen('tabs.show', function (e, id) { |
|
47 |
if (id == 'youtube_library') { |
|
48 |
// This is kinda rough, I'm not sure who should delegate what here. |
|
49 |
mediaBrowser.getActivePanel().addClass('throbber'); |
|
50 |
mediaBrowser.getActivePanel().html(''); |
|
51 |
//mediaBrowser.getActivePanel().addClass('throbber'); |
|
52 |
|
|
53 |
// Assumes we have to refresh everytime. |
|
54 |
// Remove any existing content |
|
55 |
mediaBrowser.getActivePanel().append('<ul></ul>'); |
|
56 |
that.browser = $('ul', mediaBrowser.getActivePanel()); |
|
57 |
that.browser.addClass('clearfix'); |
|
58 |
that.getMedia(); |
|
59 |
} |
|
60 |
}); |
|
61 |
}, |
|
62 |
|
|
63 |
getStreams: function () { |
|
64 |
return ['youtube://']; |
|
65 |
}, |
|
66 |
|
|
67 |
getConditions: function () { |
|
68 |
return {}; |
|
69 |
//return this.settings.conditions; |
|
70 |
}, |
|
71 |
|
|
72 |
getMedia: function() { |
|
73 |
var that = this; |
|
74 |
var callback = mediaBrowser.getCallbackUrl('getMedia'); |
|
75 |
var params = { |
|
76 |
conditions: JSON.stringify(this.getConditions()), |
|
77 |
streams: JSON.stringify(this.getStreams()) |
|
78 |
}; |
|
79 |
jQuery.get( |
|
80 |
callback, |
|
81 |
params, |
|
82 |
function(data, status) { |
|
83 |
that.mediaFiles = data.media; |
|
84 |
that.emptyMessage = data.empty; |
|
85 |
that.pager = data.pager; |
|
86 |
that.render(); |
|
87 |
}, |
|
88 |
'json' |
|
89 |
); |
|
90 |
}, |
|
91 |
|
|
92 |
render: function() { |
|
93 |
var that = this; |
|
94 |
mediaBrowser.getActivePanel().removeClass('throbber'); |
|
95 |
if (this.mediaFiles.length < 1) { |
|
96 |
jQuery('<div id="media-empty-message" class="media-empty-message"></div>').appendTo(this.browser) |
|
97 |
.html(this.emptyMessage); |
|
98 |
return; |
|
99 |
} |
|
100 |
|
|
101 |
for (var m in this.mediaFiles) { |
|
102 |
mediaFile = this.mediaFiles[m]; |
|
103 |
|
|
104 |
var listItem = jQuery('<li></li>').appendTo(this.browser) |
|
105 |
.attr('id', 'media-file-' + mediaFile.fid) |
|
106 |
.addClass('media-file'); |
|
107 |
|
|
108 |
var imgLink = jQuery('<a href="#"></a>').appendTo(listItem) |
|
109 |
.html(mediaFile.preview) |
|
110 |
.bind('click', mediaFile, function(e) { |
|
111 |
// Notify the main browser |
|
112 |
//this.selectedMedia = mediaFile; |
|
113 |
$('div.media-thumbnail img').removeClass('selected'); |
|
114 |
$('div.media-thumbnail img', $(this)).addClass('selected'); |
|
115 |
mediaBrowser.notify('mediaSelected', {mediaFiles: [e.data]}); |
|
116 |
//that.settings.onSelect(mediaFile); |
|
117 |
return false; |
|
118 |
}); |
|
119 |
} |
|
120 |
jQuery('<div id="media-pager" class="media-pager"></div>').appendTo(this.browser) |
|
121 |
.html(this.pager); |
|
122 |
} |
|
123 |
}; |
|
124 |
}; |
|
125 |
|
|
126 |
Drupal.media.browser.register('youtube_library', Drupal.media.browser.plugin.youtube_library); |
|
127 |
})(jQuery); |
drupal7/sites/all/modules/media_youtube/media_youtube.file.inc | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
/** |
|
4 |
* @file |
|
5 |
* File hooks implemented by the Media: YouTube module. |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Implements hook_file_operations(). |
|
10 |
*/ |
|
11 |
function media_youtube_file_operations() { |
|
12 |
$operations = array( |
|
13 |
'media_youtube_refresh' => array( |
|
14 |
'label' => t('Refresh YouTube information from source'), |
|
15 |
'callback' => 'media_youtube_cache_clear', |
|
16 |
), |
|
17 |
); |
|
18 |
|
|
19 |
return $operations; |
|
20 |
} |
|
21 |
|
|
22 |
/** |
|
23 |
* Clear the cached YouTube content for the selected files. |
|
24 |
*/ |
|
25 |
function media_youtube_cache_clear($fids) { |
|
26 |
$fids = array_keys($fids); |
|
27 |
|
|
28 |
$query = new EntityFieldQuery(); |
|
29 |
$results = $query |
|
30 |
->entityCondition('entity_type', 'file') |
|
31 |
->propertyCondition('uri', 'youtube:', 'STARTS_WITH') |
|
32 |
->propertyCondition('fid', $fids) |
|
33 |
->execute(); |
|
34 |
|
|
35 |
$files = file_load_multiple(array_keys($results['file'])); |
|
36 |
|
|
37 |
foreach ($files as $file) { |
|
38 |
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri); |
|
39 |
$local_path = $wrapper->getLocalThumbnailPath(); |
|
40 |
file_unmanaged_delete($local_path); |
|
41 |
} |
|
42 |
} |
drupal7/sites/all/modules/media_youtube/media_youtube.info | ||
---|---|---|
7 | 7 |
|
8 | 8 |
files[] = includes/MediaYouTubeStreamWrapper.inc |
9 | 9 |
files[] = includes/MediaInternetYouTubeHandler.inc |
10 |
files[] = includes/MediaYouTubeBrowser.inc |
|
11 | 10 |
|
12 |
; Information added by Drupal.org packaging script on 2015-03-23
|
|
13 |
version = "7.x-2.0-rc5"
|
|
11 |
; Information added by Drupal.org packaging script on 2015-05-28
|
|
12 |
version = "7.x-3.0"
|
|
14 | 13 |
core = "7.x" |
15 | 14 |
project = "media_youtube" |
16 |
datestamp = "1427094782"
|
|
15 |
datestamp = "1432800182"
|
|
17 | 16 |
|
drupal7/sites/all/modules/media_youtube/media_youtube.module | ||
---|---|---|
6 | 6 |
* displaying YouTube videos. |
7 | 7 |
*/ |
8 | 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 | 9 |
// Load all YouTube file formatters. |
18 | 10 |
require_once dirname(__FILE__) . '/includes/media_youtube.formatters.inc'; |
19 | 11 |
|
... | ... | |
83 | 75 |
return array('version' => 1); |
84 | 76 |
} |
85 | 77 |
} |
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 |
} |
Formats disponibles : Unified diff
Weekly update of contrib modules