/[drupal]/contributions/modules/emfield/contrib/video_cck/providers/youtube.inc
ViewVC logotype

Contents of /contributions/modules/emfield/contrib/video_cck/providers/youtube.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.20 - (show annotations) (download) (as text)
Wed May 20 20:11:03 2009 UTC (6 months, 1 week ago) by aaron
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +2 -2 lines
File MIME type: text/x-php
* Fix youtube div id for swfobject (aaron).
1 <?php
2 // $Id: youtube.inc,v 1.19 2009/02/05 03:37:16 aaron Exp $
3
4 define('VIDEO_CCK_YOUTUBE_MAIN_URL', 'http://www.youtube.com/');
5 define('VIDEO_CCK_YOUTUBE_API_INFO', 'http://youtube.com/dev');
6 define('VIDEO_CCK_YOUTUBE_API_APPLICATION_URL', 'http://www.youtube.com/my_profile_dev');
7 define('VIDEO_CCK_YOUTUBE_REST_ENDPOINT', 'http://www.youtube.com/api2_rest');
8 define('VIDEO_CCK_YOUTUBE_COLOR1_DEFAULT', '#FFFFFF');
9 define('VIDEO_CCK_YOUTUBE_COLOR2_DEFAULT', '#CCCCCC');
10
11 /**
12 * hook video_cck_PROVIDER_info
13 * this returns information relevant to a specific 3rd party video provider
14 * @return
15 * an array of strings requested by various admin and other forms
16 * 'name' => the translated name of the provider
17 * 'url' => the url to the main page for the provider
18 * 'settings_description' => a description of the provider that will be posted in the admin settings form
19 * 'supported_features' => an array of rows describing the state of certain supported features by the provider.
20 * These will be rendered in a table, with the columns being 'Feature', 'Supported', 'Notes'.
21 */
22 function video_cck_youtube_info() {
23 $name = t('YouTube');
24 $features = array(
25 array(t('Autoplay'), t('Yes'), ''),
26 array(t('RSS Attachment'), t('Yes'), ''),
27 array(t('Show related videos'), t('Yes'), t('This is embedded in the video itself when enabled; currently not available with other providers. Set the feature above.')),
28 array(t('Thumbnails'), t('Yes'), t('')),
29 array(t('Custom player colors'), t('Yes'), t('You may customize the player\'s skin by choosing your own colors, and/or border in that setting field set.')),
30 );
31 return array(
32 'provider' => 'youtube',
33 'name' => $name,
34 'url' => VIDEO_CCK_YOUTUBE_MAIN_URL,
35 'settings_description' => t('These settings specifically affect videos displayed from !youtube. You can learn more about its !api here.', array('!youtube' => l($name, VIDEO_CCK_YOUTUBE_MAIN_URL, array('target' => '_blank')), '!api' => l(t('API'), VIDEO_CCK_YOUTUBE_API_INFO, array('target' => '_blank')))),
36 'supported_features' => $features,
37 );
38 }
39
40 /**
41 * hook video_cck_PROVIDER_settings
42 * this should return a subform to be added to the video_cck_settings() admin settings page.
43 * note that a form field will already be provided, at $form['PROVIDER'] (such as $form['youtube'])
44 * so if you want specific provider settings within that field, you can add the elements to that form field.
45 */
46 function video_cck_youtube_settings() {
47 $form['youtube']['video_cck_youtube_show_related_videos'] = array(
48 '#type' => 'checkbox',
49 '#title' => t('Show related videos'),
50 '#default_value' => variable_get('video_cck_youtube_show_related_videos', 0),
51 '#description' => t('If checked, then when playing a video from YouTube, users may hover over the video to see thumbnails & links to related videos.'),
52 );
53 $api_desc = module_exists('youtube_api') ? t('Please make sure to enter your YouTube API key here.') : t('You will have access to more advanced features if you install and enable the !youtube_api module.', array('!youtube_api' => l(t('YouTube API'), 'http://drupal.org/project/youtube_api')));
54 $form['youtube']['api'] = array(
55 '#type' => 'fieldset',
56 '#title' => t('YouTube API'),
57 '#description' => $api_desc,
58 '#collapsible' => TRUE,
59 '#collapsed' => FALSE,
60 );
61 // $form['youtube']['api'] = youtube_api_admin_form();
62 // $form['youtube']['api'] = array(
63 // '#type' => 'fieldset',
64 // '#title' => t('YouTube API'),
65 // '#description' => t('The API is no longer required. However, there may be future features requiring it (such as the ability to display otherwise private videos). You will first need to apply for an API Developer Key from the !youtube. Note that you do not need this key to display YouTube videos or their thumbnails.', array('!youtube' => l('YouTube Developer Profile page', VIDEO_CCK_YOUTUBE_API_APPLICATION_URL, array('target' => '_blank')))),
66 // '#collapsible' => TRUE,
67 // '#collapsed' => TRUE,
68 // );
69 // $form['youtube']['api']['video_cck_youtube_api_key'] = array(
70 // '#type' => 'textfield',
71 // '#title' => t('YouTube API Key'),
72 // '#default_value' => variable_get('video_cck_youtube_api_key', ''),
73 // '#description' => t('Please enter your YouTube Developer Key here.'),
74 // );
75 // $form['youtube']['api']['video_cck_youtube_api_secret'] = array(
76 // '#type' => 'textfield',
77 // '#title' => t('YouTube API Secret'),
78 // '#default_value' => variable_get('video_cck_youtube_api_secret', ''),
79 // '#description' => t('If you have a secret for the YouTube API, enter it here.'),
80 // );
81 $form['youtube']['colors'] = array(
82 '#type' => 'fieldset',
83 '#title' => t('Embedded Video Player Colors'),
84 '#description' => t('If allowed, these two colors, in hexidecimal form (#RRGGBB), will be used to skin the YouTube player.'),
85 '#collapsible' => TRUE,
86 '#collapsed' => TRUE,
87 );
88 $form['youtube']['colors']['video_cck_youtube_show_colors'] = array(
89 '#type' => 'checkbox',
90 '#title' => t('Override player colors'),
91 '#default_value' => variable_get('video_cck_youtube_show_colors', FALSE),
92 );
93 $form['youtube']['colors']['video_cck_youtube_show_border'] = array(
94 '#type' => 'checkbox',
95 '#title' => t('Display border around player'),
96 '#default_value' => variable_get('video_cck_youtube_show_border', FALSE),
97 );
98 $form['youtube']['colors']['video_cck_youtube_colors_color1'] = array(
99 '#type' => 'textfield',
100 '#title' => t('Color 1'),
101 '#default_value' => variable_get('video_cck_youtube_colors_color1', VIDEO_CCK_YOUTUBE_COLOR1_DEFAULT),
102 );
103 $form['youtube']['colors']['video_cck_youtube_colors_color2'] = array(
104 '#type' => 'textfield',
105 '#title' => t('Color 2'),
106 '#default_value' => variable_get('video_cck_youtube_colors_color2', VIDEO_CCK_YOUTUBE_COLOR2_DEFAULT),
107 );
108
109 $form['youtube']['player_options'] = array(
110 '#type' => 'fieldset',
111 '#title' => t('Embedded video player options'),
112 '#collapsible' => TRUE,
113 '#collapsed' => TRUE,
114 );
115 $form['youtube']['player_options']['emvideo_youtube_full_screen'] = array(
116 '#type' => 'checkbox',
117 '#title' => t('Allow fullscreen'),
118 '#default_value' => variable_get('emvideo_youtube_full_screen', 1),
119 '#description' => t('Allow users to view video using the entire computer screen.'),
120 );
121 $form['youtube']['player_options']['emvideo_youtube_high_quality'] = array(
122 '#type' => 'checkbox',
123 '#title' => t('Use YouTube high quality content'),
124 '#default_value' => variable_get('emvideo_youtube_high_quality', FALSE),
125 '#description' => t("If checked, then a parameter will be set to request high quality content. Note: Not all videos from youtube are available in high quality. Those that aren't will play in normal quality."),
126 );
127
128 if (module_exists('colorpicker')) {
129 $form['youtube']['colors']['video_cck_youtube_colors_color1']['#type'] = 'colorpicker_textfield';
130 $form['youtube']['colors']['video_cck_youtube_colors_color1']['#colorpicker'] = 'colorpicker_1';
131 $form['youtube']['colors']['video_cck_youtube_colors_color2']['#type'] = 'colorpicker_textfield';
132 $form['youtube']['colors']['video_cck_youtube_colors_color2']['#colorpicker'] = 'colorpicker_2';
133 $form['youtube']['colors']['colorpicker_1'] = array(
134 '#type' => 'colorpicker',
135 '#title' => t('Color 1 picker'),
136 '#description' => t('Click in this textfield to start picking your color'),
137 );
138 $form['youtube']['colors']['colorpicker_2'] = array(
139 '#type' => 'colorpicker',
140 '#title' => t('Color 2 picker'),
141 '#description' => t('Click in this textfield to start picking your color'),
142 );
143 }
144 else {
145 $form['youtube']['colors']['#description'] .= t(' The !colorpicker, if active, gives an easy way to select these colors.', array('!colorpicker' => l(t('Colorpicker module'), 'http://drupal.org/project/colorpicker')));
146 }
147 return $form;
148 }
149
150 /**
151 * hook emfield_PROVIDER_data
152 *
153 * provides an array to be serialised and made available with $item elsewhere
154 */
155 function video_cck_youtube_data($field, $item) {
156 $data = array();
157 // create some 'field' version control
158 $data['video_cck_youtube_version'] = 1;
159
160 // be nice to make this an array for changing media:thumbnail?
161 $data['thumbnail']['url'] = 'http://img.youtube.com/vi/'. $item['value'] .'/0.jpg';
162
163 // gather info about the item
164 // RSS / MRSS feeds with the item would have enough info
165 // alternative try getting the minimum from an http get
166 $url = 'http://youtube.com/v/'. $item['value'];
167 $response = emfield_request_header('youtube', $url);
168
169 if ($response->code == 200) {
170 // probably shouldn't give the 303 path
171 $data['flash']['url'] = $url;
172 $data['flash']['size'] = $response->headers['Content-Length'];
173 $data['flash']['mime'] = $response->headers['Content-Type'];
174 }
175
176 return $data;
177 }
178
179 /**
180 *
181 */
182 function video_cck_youtube_rss($item, $teaser = NULL) {
183 if ($item['value']) {
184 if ($item['data']['video_cck_youtube_data_version'] >= 1) {
185 $data = $item['data'];
186 }
187 else {
188 $data = video_cck_youtube_data(NULL, $item);
189 }
190
191 $file = array();
192 if (is_array($data['flash'])) {
193 $file['filepath'] = $data['flash']['url'];
194 $file['filesize'] = $data['flash']['size'];
195 $file['filemime'] = $data['flash']['mime'];
196 }
197 $file['thumbnail']['filepath'] = $data['thumbnail']['url'];
198
199 return $file;
200 }
201 }
202
203 /**
204 * this is a wrapper for video_cck_request_xml that includes youtube's api key
205 */
206 function video_cck_youtube_request($method, $args = array(), $cached = TRUE) {
207 $args['dev_id'] = trim(variable_get('video_cck_youtube_api_key', ''));
208 $args['method'] = $method;
209
210 // if we've got a secret sign the arguments
211 // TODO: doesn't seem to matter
212 // if ($secret = trim(variable_get('video_cck_youtube_api_secret', ''))) {
213 // $args['api_sig'] = md5($secret . $arghash);
214 // }
215
216 $request = module_invoke('emfield', 'request_xml', 'youtube', VIDEO_CCK_YOUTUBE_REST_ENDPOINT, $args, $cached);
217 return $request;
218 }
219
220 /**
221 * hook video_cck_PROVIDER_extract
222 * this is called to extract the video code from a pasted URL or embed code.
223 * @param $embed
224 * an optional string with the pasted URL or embed code
225 * @return
226 * either an array of regex expressions to be tested, or a string with the video code to be used
227 * if the hook tests the code itself, it should return either the string of the video code (if matched), or an empty array.
228 * otherwise, the calling function will handle testing the embed code against each regex string in the returned array.
229 */
230 function video_cck_youtube_extract($embed = '') {
231 // src="http://www.youtube.com/v/nvbQQnvxXDk"
232 // http://youtube.com/watch?v=nvbQQnvxXDk
233 // http://www.youtube.com/watch?v=YzFCA-xUc8w&feature=dir
234 return array(
235 '@youtube\.com/v/([^"\&]+)@i',
236 '@youtube\.com/watch\?v=([^"\&]+)@i',
237 '@youtube\.com/\?v=([^"\&]+)@i',
238 );
239 }
240
241 /**
242 * hook video_cck_PROVIDER_embedded_link($video_code)
243 * returns a link to view the video at the provider's site
244 * @param $video_code
245 * the string containing the video to watch
246 * @return
247 * a string containing the URL to view the video at the original provider's site
248 */
249 function video_cck_youtube_embedded_link($video_code) {
250 return 'http://www.youtube.com/watch?v='. $video_code;
251 }
252
253 function video_cck_youtube_convert_color($color = NULL) {
254 if ($color{0} == '#') {
255 return substr($color, 1);
256 }
257 return $color;
258 }
259
260 /**
261 * The embedded flash displaying the youtube video.
262 */
263 function theme_video_cck_youtube_flash($embed, $width, $height, $autoplay, $options = array()) {
264 static $count;
265 if ($embed) {
266 $fullscreen = isset($options['fullscreen']) ? $options['fullscreen'] : variable_get('emvideo_youtube_full_screen', 1);
267 $fullscreen_value = $fullscreen ? "true" : "false";
268 $fs = $fullscreen ? "&amp;fs=$fullscreen" : "";
269 $related = isset($options['related']) ? $options['related'] : variable_get('video_cck_youtube_show_related_videos', 0);
270 $related = "rel=$related";
271 $autoplay = isset($options['autoplay']) ? $options['autoplay'] : $autoplay;
272 $autoplay_value = $autoplay ? '&autoplay=1' : '';
273 $show_colors = isset($options['show_colors']) ? $options['show_colors'] : variable_get('video_cck_youtube_show_colors', FALSE);
274 if ($show_colors) {
275 $color1 = isset($options['color1']) ? $options['color1'] : variable_get('video_cck_youtube_colors_color1', VIDEO_CCK_YOUTUBE_COLOR1_DEFAULT);
276 $color2 = isset($options['color2']) ? $options['color2'] : variable_get('video_cck_youtube_colors_color2', VIDEO_CCK_YOUTUBE_COLOR2_DEFAULT);
277 $colors='&color1=0x'. video_cck_youtube_convert_color($color1) .'&color2=0x'. video_cck_youtube_convert_color($color2);
278 }
279 $border = isset($options['border']) ? $options['border'] : variable_get('video_cck_youtube_show_border', FALSE);
280 $border = $border ? '&border=1' : '';
281 $enablejsapi = isset($options['enablejsapi']) ? $options['enablejsapi'] : variable_get('video_cck_youtube_enablejsapi', TRUE);
282 $enablejsapi = $enablejsapi ? '&enablejsapi=1&playerapiid=ytplayer' : '';
283 $allowScriptAcess = $enablejsapi ? 'always' : 'sameDomain';
284 $id = isset($options['id']) ? $options['id'] : 'video-cck-youtube-flash-'. (++$count);
285 $div_id = isset($options['div_id']) ? $options['div_id'] : 'video-cck-youtube-flash-wrapper-'. $count;
286 $high_quality = isset($options['high_quality']) ? $options['high_quality'] : variable_get('emvideo_youtube_high_quality', FALSE);
287 $high_quality = $high_quality ? '&amp;ap=%2526fmt%3D18' : '';
288 $url = "http://www.youtube.com/v/$embed&amp;$related$autoplay_value$colors$border$high_quality$enablejsap$fs";
289 if (variable_get('emfield_swfobject', FALSE) && (module_exists('swfobject_api') || variable_get('emfield_swfobject_location', ''))) {
290 if (module_exists('swfobject_api')) {
291 $params['width'] = $width;
292 $params['height'] = $height;
293 $params['div_id'] = $div_id;
294 $output .= theme('swfobject_api', $url, $params, $vars, $id);
295 }
296 else {
297 drupal_add_js(variable_get('emfield_swfobject_location', ''));
298 $output .= <<<FLASH
299 <div id="$div_id">
300 Sorry, you need to install flash to see this content.
301 </div>
302 <script language="JavaScript" type="text/javascript">
303 var so = new SWFObject("$url", "$id", "$width", "$height", "7");
304 so.write("$div_id");
305 </script>
306 FLASH;
307 }
308 }
309 else {
310 $output .= <<<FLASH
311 <div id="$div_id"><object type="application/x-shockwave-flash" height="$height" width="$width" data="$url" id="$id">
312 <param name="movie" value="$url" />
313 <param name="allowScriptAcess" value="sameDomain"/>
314 <param name="quality" value="best"/>
315 <param name="bgcolor" value="#FFFFFF"/>
316 <param name="scale" value="noScale"/>
317 <param name="salign" value="TL"/>
318 <param name="FlashVars" value="playerMode=embedded" />
319 <param name="wmode" value="transparent" />
320 </object></div>
321 FLASH;
322 }
323 }
324 return $output;
325 }
326
327 /**
328 * hook video_cck_PROVIDER_thumbnail
329 * returns the external url for a thumbnail of a specific video
330 * TODO: make the args: ($embed, $field, $item), with $field/$item provided if we need it, but otherwise simplifying things
331 * @param $field
332 * the field of the requesting node
333 * @param $item
334 * the actual content of the field from the requesting node
335 * @return
336 * a URL pointing to the thumbnail
337 */
338 function video_cck_youtube_thumbnail($field, $item, $formatter, $node, $width, $height) {
339 $youtube_id = $item['value'];
340 // old code to grab thumbnail via api
341 // $request = video_cck_youtube_request('youtube.videos.get_details', array('video_id' => $youtube_id));
342 // $tn = $request['THUMBNAIL_URL'][0];
343
344 // if we have a large thumbnail size, then get the larger version available.
345 if ($width > 130 || $height > 97) {
346 $tn = "http://img.youtube.com/vi/$youtube_id/0.jpg";
347 }
348 else {
349 // youtube offers 3 thumbnails. select one randomly.
350 $rand = rand(0, 2) + 1;
351 $tn = "http://img.youtube.com/vi/$youtube_id/$rand.jpg";
352 }
353 return $tn;
354 }
355
356 /**
357 * hook video_cck_PROVIDER_video
358 * this actually displays the full/normal-sized video we want, usually on the default page view
359 * @param $embed
360 * the video code for the video to embed
361 * @param $width
362 * the width to display the video
363 * @param $height
364 * the height to display the video
365 * @param $field
366 * the field info from the requesting node
367 * @param $item
368 * the actual content from the field
369 * @return
370 * the html of the embedded video
371 */
372 function video_cck_youtube_video($embed, $width, $height, $field, $item, $autoplay) {
373 $output = theme('video_cck_youtube_flash', $embed, $width, $height, $autoplay);
374 return $output;
375 }
376
377 /**
378 * hook video_cck_PROVIDER_video
379 * this actually displays the preview-sized video we want, commonly for the teaser
380 * @param $embed
381 * the video code for the video to embed
382 * @param $width
383 * the width to display the video
384 * @param $height
385 * the height to display the video
386 * @param $field
387 * the field info from the requesting node
388 * @param $item
389 * the actual content from the field
390 * @return
391 * the html of the embedded video
392 */
393 function video_cck_youtube_preview($embed, $width, $height, $field, $item, $autoplay) {
394 $output = theme('video_cck_youtube_flash', $embed, $width, $height, $autoplay);
395 return $output;
396 }

  ViewVC Help
Powered by ViewVC 1.1.2