aboutsummaryrefslogtreecommitdiff
path: root/types/video_youtube/video_youtube.module
blob: 30e46c89526e35ce350dcc69f93a482bb43381f4 (plain)
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
//$Id$
/**
 * @file
 * Enable Youtube support for video module.
 *
 * @author Fabio Varesano <fvaresano at yahoo dot it>
 * @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw at gmail dot com>
 * @todo
 */


// let's include apiclient logic
module_load_include('inc', 'video', 'includes/apiclient');

/**
 * Implementation of hook_menu
*/
function video_youtube_menu() {
  $items = array();
  $maycache=true;
  if($maycache) {
    $items['node/add/video/youtube'] = array(
      'title' => 'Youtube',
      'access arguments' => array('create video')
    );
    
    $items['admin/settings/video/youtube'] = array(
      'title' => 'Youtube',
      'description' => 'Configure various settings of the video Youtube plugin.',
      'access arguments' => array('administer site configuration'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('video_youtube_admin_settings'),
      'type' => MENU_NORMAL_ITEM,
    );
  }

  return $items;
}


/**
 * Setting form for video_upload
*/
function video_youtube_admin_settings() {
	$form = array();

  $form['video_youtube_auto_thumbnail'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable auto thumbnailing for youtube videos'),
    '#default_value' => variable_get('video_youtube_auto_thumbnail', false)
  );
  $form['video_youtube_related'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable related videos'),
    '#default_value' => variable_get('video_youtube_related', false),
    '#description' => t('If you enable related videos the Youtube player will display a list of related videos once the video completes playing.'),
  );
  $form['video_youtube_validation'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable validation'),
    '#default_value' => variable_get('video_youtube_validation', false),
    '#description' => t('If you enable validation, on each youtube video submission, you web server will contact Youtube to check that the inserted video is available and embeddable.'),
  );
  $form['video_youtube_api_key'] = array(
    '#type' => 'textfield',
    '#title' => t('Developer Key'),
    '#description' => t('Insert here the developer Key. You can get one from <a href="http://www.youtube.com/my_profile_dev">Youtube Development pages</a>.'),
    '#default_value' =>variable_get('video_youtube_api_key', ''),
  );
  // jlampton added: new youtube optional client id
  $form['video_youtube_client_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Client ID'),
    '#description' => t('Insert here the client ID. You can get one from <a href="http://www.youtube.com/my_profile_dev">Youtube Development pages</a>.'),
    '#default_value' =>variable_get('video_youtube_client_id', ''),
  );
  return system_settings_form($form);
}


/**
 * Validate settings
*/
function video_youtube_admin_settings_validate($form, &$form_state) {
  if ($form_state['values']['video_youtube_auto_thumbnail']) { // autothumbnailing is active
    // let's check we have a valid dev key
    if($form_state['values']['video_youtube_api_key'] == '') {
      form_set_error('video_youtube_api_key', t('You have to insert a valid Youtube Developer Key for auto thumbnailing to work'));
    }
  }
}


/**
 * Implementation of hook_v_help
*/
function video_youtube_v_help() {

  $help = array();
  $help['youtube']['data'] = '<b><a href="http://www.youtube.com">' . t('YouTube.com support') . '</a></b>';
  $help['youtube']['children'] = array(t('You can host videos on youtube.com and put them on your site. To do this, after you upload the video on youtube.com enter the video URL.'));

  return $help;
}


/**
 * Implementation of hook_v_info()
*/
function video_youtube_v_info() {
  $info['youtube'] = array(
    '#name' => 'Youtube Video',
    '#description' => t('Post a video available on !link to this website.', array('!link' => l(t('Youtube'), 'http://www.youtube.com'), NULL, NULL, NULL, TRUE)),
    '#autothumbable' => variable_get('video_youtube_auto_thumbnail', false),
    '#autoresolution' => true,
    '#autoplaytime' => true,
  );

  return $info;
}


/**
 * Implementation of hook_v_form()
*/
function video_youtube_v_form(&$node, &$form) {

  $form['video']['vidfile'] = array(
    '#type' => 'textfield',
    '#title' => t('Youtube Video URL'),
    '#default_value' => $node->vidfile,
    '#maxlength' => 700,
    '#required' => TRUE,
    '#weight' => -20,
    '#description' => t('Insert the URL to the youtube video. ') . l(t('More information.'), 'video/help', array('fragment' => 'videofile')));

  return $form;
}


/**
 * implementation of hook_v_validate
*/
function video_youtube_v_validate($node) {
  if(!preg_match("/^http:\/\/([a-z]{2,3}\.)?youtube\.com\/watch\?v=/", $node->vidfile)) {
    form_set_error('vidfile', t('The Youtube Video URL field must be similar to <em>http://youtube.com/watch?v=IICWFx7sKmw</em>, <em>http://www.youtube.com/watch?v=IICWFx7sKmw</em> or <em>http://it.youtube.com/watch?v=IICWFx7sKmw</em>'));
  }
  else if(variable_get('video_youtube_validation', false)){
    // we have a good URL. Let's check that the video is available on Youtube and that it is embeddable.
    // the approach used here is to return errors only if Youtube explicitely says "an error has occurred"
    $id = _video_youtube_get_id($node->vidfile);
    // jlampton changed the youtube validation url
    $response = _video_apiclient_youtube_request('gdata.youtube.com/feeds/api/videos', array('video_id' => $id));
    if(isset($response['ERROR'])) {
      form_set_error('vidfile', t('The Youtube Video URL validation has failed for some reason. Please check the URL and try again.<br />If the error persists please contact %site_name administrators.', array('%site_name' => variable_get('site_name', 'Drupal'))));
      if(isset($response['ERROR']['DESCRIPTION'][0])) {
        drupal_set_message(t('The Youtube validation service reported the following error: %error', array('%error'=>$response['ERROR']['DESCRIPTION'][0])), 'error');
      }
    }
    else if(isset($response['VIDEO_DETAILS']['EMBED_STATUS'][0])
    && $response['VIDEO_DETAILS']['EMBED_STATUS'][0] != 'ok') {
      // embedding has been disabled. we let the video pass but we warn the user
      drupal_set_message(t('The video authors have disabled embedding on Youtube. This means that this video will only be playable directly on Youtube.'));
    }
    else { // if youtube did not explicetely said "an error has occurred" we accept the video
      ;
    }
  }
}

/**
 * Implementation of hook_v_play
*/
function video_youtube_v_play($node) {
  return theme('video_youtube_play', $node);
}


/** AUTOTHUMBNAILING LOGIC */

define('VIDEO_YOUTUBE_API_INFO', 'http://youtube.com/dev');
define('VIDEO_YOUTUBE_API_APPLICATION_URL', 'http://www.youtube.com/my_profile_dev');
define('VIDEO_YOUTUBE_REST_ENDPOINT', 'http://www.youtube.com/api2_rest');


/**
* this is a wrapper for _video_apiclient_request_xml that includes youtube's api key
*/
function _video_apiclient_youtube_request($method, $args = array(), $cacheable = TRUE) {
 $args['dev_id'] = trim(variable_get('video_youtube_api_key', ''));
 $args['method'] = $method;

 return _video_apiclient_request_xml('youtube', VIDEO_YOUTUBE_REST_ENDPOINT, $args, $cacheable);
}

/**
* returns the external url for a thumbnail of a specific video
*  @param $id
*    the youtube id of the specific video
*  @return
*    a URL pointing to the thumbnail
*/
function _video_apiclient_youtube_get_thumbnail_url($id) {
  $response = _video_apiclient_youtube_request('youtube.videos.get_details', array('video_id' => $id));
  
  if(isset($response['THUMBNAIL_URL'][0]) && $response['THUMBNAIL_URL'][0] != '') {
   return $response['THUMBNAIL_URL'][0];
  }
  return false;
}


/**
 * Implementation of hook_v_auto_thumbnail
*/
function video_youtube_v_auto_thumbnail($node) {
  if (count($_POST)) {
    if ($_POST['vidfile'] == $node->vidfile) {
      _video_image_thumbnail_debug(t('No new video to thumbnail'));
      return NULL;
    }
    if ($_POST['tempimage']['fids']['_original']) {
      _video_image_thumbnail_debug(t('Video already thumbnailed'));
      return NULL;
    }
    $vidfile = $_POST['vidfile'];
  } else {
    $vidfile = $node->vidfile;
  }
  
  //get the video id
  $id = _video_youtube_get_id($vidfile);
  // get thumbnail url
  $thumbnail_url = _video_apiclient_youtube_get_thumbnail_url($id);

  return _video_image_get_thumb_file_object($thumbnail_url, $id);
}


/**
 * Implementation of hook_v_auto_resolution
*/
function video_youtube_v_auto_resolution(&$node) {
  // we set youtube videos to 425x350 by default
  return array(425, 350);
}


/**
 * Implementation of hook_v_auto_playtime
*/
function video_youtube_v_auto_playtime(&$node) {
  $id = _video_youtube_get_id($node->vidfile);
  $response = _video_apiclient_youtube_request('youtube.videos.get_details', array('video_id' => $id)); // NOTE: here we already passed validation so we expect a valid response
  
  return $response['VIDEO_DETAILS']['LENGTH_SECONDS'][0]; // return the lenght in seconds
}


/** THEMEABLE FUNCTIONS */

/**
 * Play videos hosted on youtube.com
 * Allows users to host videos on youtube.com and then use the video ID to post it in the module.
 * In the future it could also use the youtube developer API to get info and comments of the video.
 *
 * @param $node
 *   object with node information
 *
 * @return
 *   string of content to display
 */
function theme_video_youtube_play($node) {
  $width = ($node->video_scaled_x ? $node->video_scaled_x : '425');
  $height = ($node->video_scaled_y ? $node->video_scaled_y : '350');

  $id = _video_youtube_get_id(check_plain($node->vidfile));
  
  // related video setting
  $rel = variable_get('video_youtube_related', false) ? '1' : '0';
  
  // this will be executed by not Internet Explorer browsers
  $output = '<!--[if !IE]> <-->
<object type="application/x-shockwave-flash" width="'. $width .'" height="'. $height .'"
data="http://www.youtube.com/v/' . $id . '&rel='.$rel.'">
<!--> <![endif]-->' . "\n";

  // this will be executed by Internet Explorer
  $output .= '<!--[if IE]>
<object type="application/x-shockwave-flash" width="'. $width .'" height="'. $height .'"
classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<![endif]-->' . "\n";

  // params will be passed to both IE or not IE browsers
  $output .= '<param name="movie" value="http://www.youtube.com/v/' . $id . '&rel='.$rel.'" />' . "\n"
  . '<param name="wmode" value="transparent" />' . "\n"
  . _video_get_parameters($node) .
  '<p>'. t('Your browser is not able to display this multimedia content.') .'</p>
</object>';


  $output = theme('video_format_play', $output, t('http://www.google.com/support/youtube'), t('Link to youtube.com'), t('youtube.com'));
  return $output;
}


/** HELPER FUNCTIONS */

/**
 * Get the id from an URL
*/
function _video_youtube_get_id($url) {
  $parsed_url = parse_url($url);
  parse_str($parsed_url['query'], $parsed_query);;
  return $parsed_query['v'];
}

/**
 * Implementation of hook_theme().
 */
function video_youtube_theme() {
  return array(
    'video_youtube_play' => array(
      'arguments' => array('node' => NULL),
    ),
  );
}