View source
<?php
define('VIDEO_CCK_YOUTUBE_MAIN_URL', 'http://www.youtube.com/');
define('VIDEO_CCK_YOUTUBE_API_INFO', 'http://youtube.com/dev');
define('VIDEO_CCK_YOUTUBE_API_APPLICATION_URL', 'http://www.youtube.com/my_profile_dev');
define('VIDEO_CCK_YOUTUBE_REST_ENDPOINT', 'http://www.youtube.com/api2_rest');
define('VIDEO_CCK_YOUTUBE_COLOR1_DEFAULT', '#FFFFFF');
define('VIDEO_CCK_YOUTUBE_COLOR2_DEFAULT', '#CCCCCC');
function video_cck_youtube_info() {
$name = t('YouTube');
$features = array(
array(
t('Autoplay'),
t('Yes'),
'',
),
array(
t('RSS Attachment'),
t('Yes'),
'',
),
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.'),
),
array(
t('Thumbnails'),
t('Yes'),
t(''),
),
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.'),
),
);
return array(
'provider' => 'youtube',
'name' => $name,
'url' => VIDEO_CCK_YOUTUBE_MAIN_URL,
'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',
)),
)),
'supported_features' => $features,
);
}
function video_cck_youtube_settings() {
$form['youtube']['video_cck_youtube_show_related_videos'] = array(
'#type' => 'checkbox',
'#title' => t('Show related videos'),
'#default_value' => variable_get('video_cck_youtube_show_related_videos', 0),
'#description' => t('If checked, then when playing a video from YouTube, users may hover over the video to see thumbnails & links to related videos.'),
);
$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'),
));
$form['youtube']['api'] = array(
'#type' => 'fieldset',
'#title' => t('YouTube API'),
'#description' => $api_desc,
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['youtube']['colors'] = array(
'#type' => 'fieldset',
'#title' => t('Embedded Video Player Colors'),
'#description' => t('If allowed, these two colors, in hexidecimal form (#RRGGBB), will be used to skin the YouTube player.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['youtube']['colors']['video_cck_youtube_show_colors'] = array(
'#type' => 'checkbox',
'#title' => t('Override player colors'),
'#default_value' => variable_get('video_cck_youtube_show_colors', FALSE),
);
$form['youtube']['colors']['video_cck_youtube_show_border'] = array(
'#type' => 'checkbox',
'#title' => t('Display border around player'),
'#default_value' => variable_get('video_cck_youtube_show_border', FALSE),
);
$form['youtube']['colors']['video_cck_youtube_colors_color1'] = array(
'#type' => 'textfield',
'#title' => t('Color 1'),
'#default_value' => variable_get('video_cck_youtube_colors_color1', VIDEO_CCK_YOUTUBE_COLOR1_DEFAULT),
);
$form['youtube']['colors']['video_cck_youtube_colors_color2'] = array(
'#type' => 'textfield',
'#title' => t('Color 2'),
'#default_value' => variable_get('video_cck_youtube_colors_color2', VIDEO_CCK_YOUTUBE_COLOR2_DEFAULT),
);
$form['youtube']['player_options'] = array(
'#type' => 'fieldset',
'#title' => t('Embedded video player options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['youtube']['player_options']['emvideo_youtube_full_screen'] = array(
'#type' => 'checkbox',
'#title' => t('Allow fullscreen'),
'#default_value' => variable_get('emvideo_youtube_full_screen', 1),
'#description' => t('Allow users to view video using the entire computer screen.'),
);
$form['youtube']['player_options']['emvideo_youtube_high_quality'] = array(
'#type' => 'checkbox',
'#title' => t('Use YouTube high quality content'),
'#default_value' => variable_get('emvideo_youtube_high_quality', FALSE),
'#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."),
);
if (module_exists('colorpicker')) {
$form['youtube']['colors']['video_cck_youtube_colors_color1']['#type'] = 'colorpicker_textfield';
$form['youtube']['colors']['video_cck_youtube_colors_color1']['#colorpicker'] = 'colorpicker_1';
$form['youtube']['colors']['video_cck_youtube_colors_color2']['#type'] = 'colorpicker_textfield';
$form['youtube']['colors']['video_cck_youtube_colors_color2']['#colorpicker'] = 'colorpicker_2';
$form['youtube']['colors']['colorpicker_1'] = array(
'#type' => 'colorpicker',
'#title' => t('Color 1 picker'),
'#description' => t('Click in this textfield to start picking your color'),
);
$form['youtube']['colors']['colorpicker_2'] = array(
'#type' => 'colorpicker',
'#title' => t('Color 2 picker'),
'#description' => t('Click in this textfield to start picking your color'),
);
}
else {
$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'),
));
}
return $form;
}
function video_cck_youtube_data($field, $item) {
$data = array();
$data['video_cck_youtube_version'] = 1;
$data['thumbnail']['url'] = 'http://img.youtube.com/vi/' . $item['value'] . '/0.jpg';
$url = 'http://youtube.com/v/' . $item['value'];
$response = emfield_request_header('youtube', $url);
if ($response->code == 200) {
$data['flash']['url'] = $url;
$data['flash']['size'] = $response->headers['Content-Length'];
$data['flash']['mime'] = $response->headers['Content-Type'];
}
return $data;
}
function video_cck_youtube_rss($item, $teaser = NULL) {
if ($item['value']) {
if ($item['data']['video_cck_youtube_data_version'] >= 1) {
$data = $item['data'];
}
else {
$data = video_cck_youtube_data(NULL, $item);
}
$file = array();
if (is_array($data['flash'])) {
$file['filepath'] = $data['flash']['url'];
$file['filesize'] = $data['flash']['size'];
$file['filemime'] = $data['flash']['mime'];
}
$file['thumbnail']['filepath'] = $data['thumbnail']['url'];
return $file;
}
}
function video_cck_youtube_request($method, $args = array(), $cached = TRUE) {
$args['dev_id'] = trim(variable_get('video_cck_youtube_api_key', ''));
$args['method'] = $method;
$request = module_invoke('emfield', 'request_xml', 'youtube', VIDEO_CCK_YOUTUBE_REST_ENDPOINT, $args, $cached);
return $request;
}
function video_cck_youtube_extract($embed = '') {
return array(
'@youtube\\.com/v/([^"\\&]+)@i',
'@youtube\\.com/watch\\?v=([^"\\&]+)@i',
'@youtube\\.com/\\?v=([^"\\&]+)@i',
);
}
function video_cck_youtube_embedded_link($video_code) {
return 'http://www.youtube.com/watch?v=' . $video_code;
}
function video_cck_youtube_convert_color($color = NULL) {
if ($color[0] == '#') {
return substr($color, 1);
}
return $color;
}
function theme_video_cck_youtube_flash($embed, $width, $height, $autoplay, $options = array()) {
static $count;
if ($embed) {
$fullscreen = isset($options['fullscreen']) ? $options['fullscreen'] : variable_get('emvideo_youtube_full_screen', 1);
$fullscreen_value = $fullscreen ? "true" : "false";
$fs = $fullscreen ? "&fs={$fullscreen}" : "";
$related = isset($options['related']) ? $options['related'] : variable_get('video_cck_youtube_show_related_videos', 0);
$related = "rel={$related}";
$autoplay = isset($options['autoplay']) ? $options['autoplay'] : $autoplay;
$autoplay_value = $autoplay ? '&autoplay=1' : '';
$show_colors = isset($options['show_colors']) ? $options['show_colors'] : variable_get('video_cck_youtube_show_colors', FALSE);
if ($show_colors) {
$color1 = isset($options['color1']) ? $options['color1'] : variable_get('video_cck_youtube_colors_color1', VIDEO_CCK_YOUTUBE_COLOR1_DEFAULT);
$color2 = isset($options['color2']) ? $options['color2'] : variable_get('video_cck_youtube_colors_color2', VIDEO_CCK_YOUTUBE_COLOR2_DEFAULT);
$colors = '&color1=0x' . video_cck_youtube_convert_color($color1) . '&color2=0x' . video_cck_youtube_convert_color($color2);
}
$border = isset($options['border']) ? $options['border'] : variable_get('video_cck_youtube_show_border', FALSE);
$border = $border ? '&border=1' : '';
$enablejsapi = isset($options['enablejsapi']) ? $options['enablejsapi'] : variable_get('video_cck_youtube_enablejsapi', TRUE);
$enablejsapi = $enablejsapi ? '&enablejsapi=1&playerapiid=ytplayer' : '';
$allowScriptAcess = $enablejsapi ? 'always' : 'sameDomain';
$id = isset($options['id']) ? $options['id'] : 'video-cck-youtube-flash-' . ++$count;
$div_id = isset($options['div_id']) ? $options['div_id'] : 'video_cck_youtube_flash_wrapper_' . $count;
$high_quality = isset($options['high_quality']) ? $options['high_quality'] : variable_get('emvideo_youtube_high_quality', FALSE);
$high_quality = $high_quality ? '&ap=%2526fmt%3D18' : '';
$url = "http://www.youtube.com/v/{$embed}&{$related}{$autoplay_value}{$colors}{$border}{$high_quality}{$enablejsap}{$fs}";
if (variable_get('emfield_swfobject', FALSE) && (module_exists('swfobject_api') || variable_get('emfield_swfobject_location', ''))) {
if (module_exists('swfobject_api')) {
$params['width'] = $width;
$params['height'] = $height;
$params['div_id'] = $div_id;
$output .= theme('swfobject_api', $url, $params, $vars, $div_id);
}
else {
drupal_add_js(variable_get('emfield_swfobject_location', ''));
$output .= <<<FLASH
<div id="{<span class="php-variable">$div_id</span>}">
Sorry, you need to install flash to see this content.
</div>
<script language="JavaScript" type="text/javascript">
var so = new SWFObject("{<span class="php-variable">$url</span>}", "{<span class="php-variable">$div_id</span>}", "{<span class="php-variable">$width</span>}", "{<span class="php-variable">$height</span>}", "7");
so.write("{<span class="php-variable">$div_id</span>}");
</script>
FLASH;
}
}
else {
$output .= <<<FLASH
<div id="{<span class="php-variable">$div_id</span>}"><object type="application/x-shockwave-flash" height="{<span class="php-variable">$height</span>}" width="{<span class="php-variable">$width</span>}" data="{<span class="php-variable">$url</span>}" id="{<span class="php-variable">$id</span>}">
<param name="movie" value="{<span class="php-variable">$url</span>}" />
<param name="allowScriptAcess" value="sameDomain"/>
<param name="quality" value="best"/>
<param name="bgcolor" value="#FFFFFF"/>
<param name="scale" value="noScale"/>
<param name="salign" value="TL"/>
<param name="FlashVars" value="playerMode=embedded" />
<param name="wmode" value="transparent" />
</object></div>
FLASH;
}
}
return $output;
}
function video_cck_youtube_thumbnail($field, $item, $formatter, $node, $width, $height) {
$youtube_id = $item['value'];
if ($width > 130 || $height > 97) {
$tn = "http://img.youtube.com/vi/{$youtube_id}/0.jpg";
}
else {
$rand = rand(0, 2) + 1;
$tn = "http://img.youtube.com/vi/{$youtube_id}/{$rand}.jpg";
}
return $tn;
}
function video_cck_youtube_video($embed, $width, $height, $field, $item, $autoplay) {
$output = theme('video_cck_youtube_flash', $embed, $width, $height, $autoplay);
return $output;
}
function video_cck_youtube_preview($embed, $width, $height, $field, $item, $autoplay) {
$output = theme('video_cck_youtube_flash', $embed, $width, $height, $autoplay);
return $output;
}