-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia_youtube.admin.inc
310 lines (282 loc) · 9.74 KB
/
media_youtube.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
// $Id: media_youtube.admin.inc,v 1.1.2.7 2010/04/21 14:59:59 aaron Exp $
/**
* @file
* Administrative page callbacks for Media: YouTube.
*/
/**
* Callback for /admin/config/media/media_youtube.
*/
function media_youtube_settings_form() {
$form = array();
$form['api'] = array(
'#type' => 'fieldset',
'#title' => t('YouTube API'),
'#description' => t('You will first need to obtain an <a href="@youtube">API Developer Key</a>.', array('@youtube' => 'http://code.google.com/apis/youtube/dashboard/')),
'#collapsible' => TRUE,
'#collapsed' => (variable_get('media_youtube_api_key', '') != ''),
);
$form['api']['media_youtube_api_key'] = array(
'#type' => 'textfield',
'#title' => t('YouTube API Key'),
'#default_value' => variable_get('media_youtube_api_key', ''),
'#description' => t('Please enter your YouTube Developer Key here.'),
);
$form['api']['media_youtube_username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('media_youtube_username', ''),
'#description' => t('Your YouTube username'),
);
$form['api']['media_youtube_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#default_value' => variable_get('media_youtube_password', ''),
'#description' => t('Your YouTube password'),
);
return system_settings_form($form);
}
/**
* Callback for /media/add/media_youtube/upload
*/
function media_youtube_upload() {
if(isset($_GET['status']) && $_GET['status'] == '200' && isset($_GET['id']) && strlen($_GET['id']) > 0) {
$uri = 'youtube://' . 'v/' . $_GET['id'];
// Check to see if this a duplicate of an existing file
$files = file_load_multiple(NULL, array('uri' => $uri));
if ($files) {
// This is ugly.
$file = array_shift($files);
}
else {
// @TODO: This won't work for YouTube and many other streams.
// copy($url, $destination);
$file = file_uri_to_object($uri);
file_save($file);
}
// field_attach_insert('media', $file);
if ($file) {
field_attach_submit('media', $file, $form, $form_state);
field_attach_insert('media', $file);
drupal_goto('media/' . $file->fid . '/edit');
}
else {
drupal_set_message(t('An error occurred. The file was uploaded but not saved into the CMS.'), 'error');
drupal_goto('media/add/media_youtube');
}
}
else {
return theme('media_youtube_upload', array('url' => $_GET['url'], 'token' => $_GET['token']));
}
}
/**
* Callback for /media/add/media_youtube and
* /admin/content/media/add/media_youtube.
*/
function media_youtube_add($form, &$form_state = array(), $redirect = NULL) {
global $user;
$form = array();
$form['youtube'] = array(
'#type' => 'vertical_tabs',
);
$form['youtube']['all'] = array(
'#type' => 'fieldset',
'#title' => t('All YouTube videos'),
);
// Get all youtube files for this user
$results = db_query("SELECT fid FROM {file_managed} WHERE uid = :uid AND uri LIKE :uri", array(
':uid' => $user->uid,
':uri' => 'youtube%%'
))->fetchAll();
foreach ($results as $result) {
$file = file_load($result->fid);
$output = theme('image', array(
'path' => 'http://img.youtube.com/vi/' . pathinfo($file->uri, PATHINFO_FILENAME) . '/0.jpg',
'title' => 'title',
'alt' => 'alt',
'attributes' => array('width' => 150),
'getsize' => FALSE,
));
$form['youtube']['all'][$file->fid] = array(
'#markup' => $output,
);
}
/* $form['youtube']['all']['test'] = array(
'#type' => 'checkbox',
'#title' => 'test',
);*/
$form['youtube']['add_from_url'] = array(
'#type' => 'fieldset',
'#title' => t('Add from URL'),
);
$form['youtube']['add_from_url']['url'] = array(
'#type' => 'textfield',
'#title' => 'URL',
'#description' => 'Input the URL of the desired YouTube video page.',
);
$form['youtube']['browser_upload'] = array(
'#type' => 'fieldset',
'#title' => t('Upload'),
);
$form['youtube']['browser_upload']['title'] = array(
'#type' => 'textfield',
'#title' => t('Title'),
'#required' => true,
);
$form['youtube']['browser_upload']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#required' => true,
);
$form['youtube']['browser_upload']['category'] = array(
'#type' => 'textfield',
'#title' => t('Category'),
'#required' => true,
);
$form['youtube']['browser_upload']['tags'] = array(
'#type' => 'textfield',
'#title' => t('Tags'),
'#required' => true,
);
$form['redirect'] = array(
'#type' => 'value',
'#value' => $redirect,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
/**
* Validation for media_youtube_add().
*/
function media_youtube_add_validate($form, &$form_state) {
if($form_state['values']['youtube__active_tab'] != 'edit-browser-upload') {
if (!preg_match('@youtube\.com/watch\?v=([^"\& ]+)@i', $form_state['values']['url'], $matches)) {
form_set_error('url', t('Please submit a valid YouTube video URL.'));
}
}
else {
if(empty($form_state['values']['title'])) {
form_set_error('title', t('Please submit a title'));
}
if(empty($form_state['values']['description'])) {
form_set_error('description', t('Please submit a description'));
}
if(empty($form_state['values']['category'])) {
form_set_error('category', t('Please submit a category'));
}
if(empty($form_state['values']['tags'])) {
form_set_error('tags', t('Please submit at least one tag'));
}
}
}
/**
* Submission for media_youtube_add().
*
* This will create a file object for the YouTube video.
*/
function media_youtube_add_submit($form, &$form_state) {
if(!empty($form_state['values']['youtube__active_tab']) == 'edit-browser-upload') {
// get authentication token
$url_auth = 'https://www.google.com/youtube/accounts/ClientLogin';
$url_token = 'http://gdata.youtube.com/action/GetUploadToken';
$username = variable_get('media_youtube_username', '');
$password = variable_get('media_youtube_password', '');
$developer_key = variable_get('media_youtube_api_key', '');
$ch = curl_init($url_auth);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=$username&Passwd=$password&service=youtube&source=DrupalMedia");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($status == 200) {
// Parse Google's response
$goog_resp = array();
foreach (explode("\n", $response) as $l) {
$l = chop($l);
if ($l) {
list($key, $val) = explode('=', chop($l), 2);
$goog_resp[$key] = $val;
}
}
$API_XML_Request =
<<<EOT
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<media:group>
<media:title type="plain">{$form_state['values']['title']}</media:title>
<media:description type="plain">{$form_state['values']['description']}</media:description>
<media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">{$form_state['values']['category']}</media:category>
<media:keywords>{$form_state['values']['tags']}</media:keywords>
</media:group>
</entry>
EOT;
$headers = array(
'Authorization: GoogleLogin auth=' . $goog_resp['Auth'],
'GData-Version: 2',
'X-GData-Key: key=' . $developer_key . '',
'Content-Length: ' . mb_strlen($API_XML_Request, 'utf8'),
'Content-Type: application/atom+xml; charset=UTF-8',
);
$ch = curl_init($url_token);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $API_XML_Request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($status == 200) {
$xml = new SimpleXMLElement($response);
$form_state['redirect'] = array('media/add/media_youtube/upload', array(
'query' => array(
'url' => $xml->url,
'token' => $xml->token,
)
));
}
else {
$form_state['rebuild'] = TRUE;
$xml = new SimpleXMLElement($response);
drupal_set_message('Error: ' . $xml->error[0]->code . ' : ' . $xml->error[0]->location, 'error');
}
}
else {
drupal_set_message(t('Error authenticating with YouTube, check your settings'), 'error');
}
}
else {
$defaults = array (
'display' => TRUE,
);
$uri = media_youtube_media_parse($form_state['values']['url']);
// Check to see if this a duplicate of an existing file
$files = file_load_multiple(NULL, array('uri' => $uri));
if ($files) {
// This is ugly.
$file = array_shift($files);
}
else {
// @TODO: This won't work for YouTube and many other streams.
// copy($url, $destination);
$file = file_uri_to_object($uri);
file_save($file);
}
// field_attach_insert('media', $file);
if ($file) {
$form_state['redirect'] = 'media/' . $file->fid . '/edit';
field_attach_submit('media', $file, $form, $form_state);
field_attach_insert('media', $file);
}
else {
drupal_set_message(t('An error occurred and no file was saved.'), 'error');
}
$form_state['redirect'] = !empty($form_state['values']['redirect']) ? $form_state['values']['redirect'] : 'media/' . $file->fid . '/edit';
}
}