View source
<?php
define('EMVIDEO_YOUTUBE_MAIN_URL', 'http://youtube.com/');
define('EMVIDEO_YOUTUBE_API_INFO', 'http://code.google.com/apis/youtube/getting_started.html');
define('EMVIDEO_YOUTUBE_API_APPLICATION_URL', 'http://code.google.com/apis/youtube/dashboard/');
define('EMVIDEO_YOUTUBE_REST_ENDPOINT', 'http://gdata.youtube.com/feeds/api/videos');
define('EMVIDEO_YOUTUBE_COLOR1_DEFAULT', '#FFFFFF');
define('EMVIDEO_YOUTUBE_COLOR2_DEFAULT', '#CCCCCC');
define('EMVIDEO_YOUTUBE_DATA_VERSION', 3);
function emvideo_youtube_info() {
$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('Duration'),
t('Yes'),
'',
),
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."),
),
array(
t('Full screen mode'),
t('Yes'),
t('You may customize the player to enable or disable full screen playback. Full screen mode is enabled by default.'),
),
array(
t('Use JW FLV Media Player'),
t('Yes'),
t("You may opt to use the !flvplayer to play YouTube videos if it's installed on your server.", array(
'!flvplayer' => l(t('JW FLV Media Player'), 'http://www.longtailvideo.com/players/jw-flv-player/'),
)),
),
);
return array(
'provider' => 'youtube',
'name' => t('YouTube'),
'url' => EMVIDEO_YOUTUBE_MAIN_URL,
'settings_description' => t('These settings specifically affect videos displayed from <a href="@youtube" target="_blank">YouTube</a>. You can learn more about its <a href="@api" target="_blank">API</a> here.', array(
'@youtube' => EMVIDEO_YOUTUBE_MAIN_URL,
'@api' => EMVIDEO_YOUTUBE_API_INFO,
)),
'supported_features' => $features,
);
}
function emvideo_youtube_settings() {
$form['emvideo_youtube_show_related_videos'] = array(
'#type' => 'checkbox',
'#title' => t('Show related videos'),
'#default_value' => variable_get('emvideo_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.'),
);
$form['api'] = array(
'#type' => 'fieldset',
'#title' => t('YouTube API'),
'#description' => t('This is not currently required. At some point, you may need to apply for an API Developer Key from the <a href="@youtube" target="_blank">YouTube Developer Profile page</a>. Note that you do not need this key to display YouTube videos or their thumbnails.', array(
'@youtube' => EMVIDEO_YOUTUBE_API_APPLICATION_URL,
)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['api']['emvideo_youtube_api_key'] = array(
'#type' => 'textfield',
'#title' => t('YouTube Developer Key'),
'#default_value' => variable_get('emvideo_youtube_api_key', ''),
'#description' => t('Please enter your YouTube Developer Key here.'),
);
$form['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['colors']['emvideo_youtube_show_colors'] = array(
'#type' => 'checkbox',
'#title' => t('Override player colors'),
'#default_value' => variable_get('emvideo_youtube_show_colors', FALSE),
);
$form['colors']['emvideo_youtube_show_border'] = array(
'#type' => 'checkbox',
'#title' => t('Display border around player'),
'#default_value' => variable_get('emvideo_youtube_show_border', FALSE),
);
$form['colors']['emvideo_youtube_colors_color1'] = array(
'#type' => 'textfield',
'#title' => t('Color 1'),
'#default_value' => variable_get('emvideo_youtube_colors_color1', EMVIDEO_YOUTUBE_COLOR1_DEFAULT),
);
$form['colors']['emvideo_youtube_colors_color2'] = array(
'#type' => 'textfield',
'#title' => t('Color 2'),
'#default_value' => variable_get('emvideo_youtube_colors_color2', EMVIDEO_YOUTUBE_COLOR2_DEFAULT),
);
$form['player_options'] = array(
'#type' => 'fieldset',
'#title' => t('Embedded video player options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['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['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."),
);
$form['player_options']['emvideo_youtube_display_info'] = array(
'#type' => 'checkbox',
'#title' => t('Remove titles and ratings on videos'),
'#default_value' => variable_get('emvideo_youtube_display_info', FALSE),
'#description' => t("If checked, additional info, such as title and rating, will NOT be displayed on the YouTube player."),
);
if (module_exists('colorpicker')) {
$form['colors']['emvideo_youtube_colors_color1']['#type'] = 'colorpicker_textfield';
$form['colors']['emvideo_youtube_colors_color1']['#colorpicker'] = 'colorpicker_1';
$form['colors']['emvideo_youtube_colors_color2']['#type'] = 'colorpicker_textfield';
$form['colors']['emvideo_youtube_colors_color2']['#colorpicker'] = 'colorpicker_2';
$form['colors']['colorpicker_1'] = array(
'#type' => 'colorpicker',
'#title' => t('Color 1 picker'),
'#description' => t('Click in this textfield to start picking your color'),
);
$form['colors']['colorpicker_2'] = array(
'#type' => 'colorpicker',
'#title' => t('Color 2 picker'),
'#description' => t('Click in this textfield to start picking your color'),
);
}
else {
$form['colors']['#description'] .= ' ' . t('The <a href="@colorpicker">Colorpicker module</a>, if active, gives an easy way to select these colors.', array(
'@colorpicker' => 'http://drupal.org/project/colorpicker',
));
}
$form['jwflv'] = array(
'#type' => 'fieldset',
'#title' => t('JW FLV Media Player Options'),
'#collapsible' => TRUE,
'#collapsed' => !($flv_path = emfield_flvmediaplayer_url()),
);
$form['jwflv']['emvideo_youtube_use_jw_flv'] = array(
'#type' => 'checkbox',
'#title' => t('Use JW FLV Media Player'),
'#description' => t('If checked, and the server is properly configured, YouTube videos will be played using the !flvplayer.', array(
'!flvplayer' => l(t('JW FLV Media Player'), 'http://www.longtailvideo.com/players/jw-flv-player/'),
)),
'#default_value' => variable_get('emvideo_youtube_use_jw_flv', FALSE),
);
if (!$flv_path) {
$form['jwflv']['#description'] = t('You must first install the !flvplayer, preferably in your /sites/all/plugins folder.', array(
'!flvplayer' => l(t('JW FLV Media Player'), 'http://www.longtailvideo.com/players/jw-flv-player/'),
));
}
if ($flv_path && !_emvideo_youtube_check_flv_player_setup()) {
$form['jwflv']['#description'] = t('You must have the yt.swf file installed in the same folder as your !flvplayer.', array(
'!flvplayer' => l(t('JW FLV Media Player'), 'http://www.longtailvideo.com/players/jw-flv-player/'),
));
}
$form['jwflv']['#description'] = $form['jwflv']['#description'] ? $form['jwflv']['#description'] : t('!flvplayer is properly configured on your server, so may be used to play YouTube videos if desired.', array(
'!flvplayer' => l(t('JW FLV Media Player'), 'http://www.longtailvideo.com/players/jw-flv-player/'),
));
return $form;
}
function _emvideo_youtube_check_flv_player_setup() {
static $check;
if (is_null($check)) {
$flv_path = emfield_flvmediaplayer_url();
if (!$flv_path || !file_exists($flv_path)) {
$check = FALSE;
}
else {
$path = dirname($flv_path);
$check = file_exists($path . '/yt.swf');
}
}
return $check;
}
function emvideo_youtube_data_version() {
return EMVIDEO_YOUTUBE_DATA_VERSION;
}
function emvideo_youtube_data($field, $item) {
$data = array();
$data['emvideo_youtube_version'] = $data['emvideo_data_version'] = EMVIDEO_YOUTUBE_DATA_VERSION;
$data['raw'] = emvideo_youtube_request($item['value']);
$data['duration'] = intval($data['raw']['MEDIA:GROUP']['YT:DURATION'][1]['SECONDS']);
if (strpos($item['value'], 'PLAYLIST_') === 0) {
$playlist_id = substr($item['value'], 9);
$url = 'http://youtube.com/p/' . $playlist_id;
$data['playlist'] = 1;
$api_url = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist_id;
$result = drupal_http_request($api_url);
if ($result->code == 200) {
$parser = drupal_xml_parser_create($result->data);
$vals = array();
$index = array();
xml_parse_into_struct($parser, $result->data, $vals, $index);
xml_parser_free($parser);
if (count($vals)) {
foreach ($vals as $val) {
if ($val['tag'] == 'MEDIA:THUMBNAIL' && $val['attributes']['HEIGHT'] == '240') {
$data['thumbnail']['url'] = $val['attributes']['URL'];
break;
}
}
}
}
}
else {
$url = 'http://youtube.com/v/' . $item['value'];
$data['playlist'] = 0;
$data['thumbnail']['url'] = 'http://img.youtube.com/vi/' . $item['value'] . '/0.jpg';
}
$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 emvideo_youtube_rss($item, $teaser = NULL) {
if ($item['value']) {
if (!empty($item['data']['emvideo_youtube_data_version']) && $item['data']['emvideo_youtube_data_version'] >= 1) {
$data = $item['data'];
}
else {
$data = emvideo_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 emvideo_youtube_request($code, $cached = TRUE) {
$args['dev_id'] = trim(variable_get('emvideo_youtube_api_key', ''));
$args['method'] = $method;
$request = module_invoke('emfield', 'request_xml', 'youtube', EMVIDEO_YOUTUBE_REST_ENDPOINT . '/' . $code, array(
'video' => $code,
), $cached);
return $request;
}
function emvideo_youtube_extract($code = '') {
$playlist_regex = array(
'@youtube\\.com/p/([^"\\& ]+)@i',
'@youtube\\.com/view_play_list\\?p=([^"\\& ]+)@i',
);
foreach ($playlist_regex as $regex) {
if (preg_match($regex, trim($code), $matches)) {
return 'PLAYLIST_' . $matches[1];
}
}
return array(
'@youtube\\.com/v/([^\\?"\\& ]+)@i',
'@youtube\\.com/watch\\?v=([^"\\& ]+)@i',
'@youtube\\.com/\\?v=([^"\\& ]+)@i',
);
}
function emvideo_youtube_embedded_link($video_code) {
if (strpos($video_code, 'PLAYLIST_') === 0) {
return 'http://youtube.com/p/' . substr($video_code, 9);
}
return 'http://www.youtube.com/watch?v=' . $video_code;
}
function emvideo_youtube_duration($item) {
if (!isset($item['data']['emvideo_youtube_version']) || $item['data']['emvideo_youtube_version'] < 3) {
$item['data'] = emvideo_youtube_data(NULL, $item);
}
return isset($item['data']['duration']) ? $item['data']['duration'] : 0;
}
function emvideo_youtube_convert_color($color = NULL) {
if ($color[0] == '#') {
return drupal_substr($color, 1);
}
return $color;
}
function theme_emvideo_youtube_flash($code, $width, $height, $autoplay, $options = array()) {
static $count;
$output = '';
if ($code = check_plain($code)) {
$width = intval($width);
$height = intval($height);
$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('emvideo_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('emvideo_youtube_show_colors', FALSE);
$colors = '';
if ($show_colors) {
$color1 = isset($options['color1']) ? $options['color1'] : variable_get('emvideo_youtube_colors_color1', emvideo_YOUTUBE_COLOR1_DEFAULT);
$color2 = isset($options['color2']) ? $options['color2'] : variable_get('emvideo_youtube_colors_color2', emvideo_YOUTUBE_COLOR2_DEFAULT);
$colors = '&color1=0x' . emvideo_youtube_convert_color($color1) . '&color2=0x' . emvideo_youtube_convert_color($color2);
}
$border = isset($options['border']) ? $options['border'] : variable_get('emvideo_youtube_show_border', FALSE);
$border = $border ? '&border=1' : '';
$enablejsapi = isset($options['enablejsapi']) ? $options['enablejsapi'] : variable_get('emvideo_youtube_enablejsapi', TRUE);
$enablejsapi = $enablejsapi ? '&enablejsapi=1&playerapiid=ytplayer' : '';
$id = check_plain(isset($options['id']) ? $options['id'] : 'emvideo-youtube-flash-' . ++$count);
$div_id = check_plain(isset($options['div_id']) ? $options['div_id'] : 'emvideo-youtube-flash-wrapper-' . $count);
$high_quality = isset($options['high_quality']) ? $options['high_quality'] : variable_get('emvideo_youtube_high_quality', FALSE);
$high_quality = $high_quality ? '&hd=1' : '';
$display_info = isset($options['display_info']) ? $options['display_info'] : variable_get('emvideo_youtube_display_info', FALSE);
$display_info = $display_info ? '&showinfo=0' : '';
if ($options['item']['data']['playlist']) {
$code = substr($code, 9);
$url = check_plain("http://www.youtube.com/p/{$code}&{$related}{$autoplay_value}{$colors}{$border}{$high_quality}{$display_info}{$enablejsapi}{$fs}");
}
else {
$url = check_plain("http://www.youtube.com/v/{$code}&{$related}{$autoplay_value}{$colors}{$border}{$high_quality}{$display_info}{$enablejsapi}{$fs}");
}
$path = emfield_flvmediaplayer_url();
$use_flv = isset($options['use_flv']) ? $options['use_flv'] : variable_get('emvideo_youtube_use_jw_flv', FALSE);
if ($use_flv && ($flv_path = emfield_flvmediaplayer_url()) && _emvideo_youtube_check_flv_player_setup()) {
global $base_path;
$tn_options = array(
'width' => $width,
'height' => $height,
'return_url' => TRUE,
);
$flashvars = array();
$flashvars['image'] = theme('emvideo_video_thumbnail', NULL, $options['item'], 'emvideo_thumbnail', $options['node'], TRUE, $tn_options);
$flashvars['type'] = 'youtube';
$flashvars['file'] = $url;
$flashvars['autostart'] = $autoplay ? 'true' : 'false';
$url = $base_path . $flv_path;
}
else {
$use_flv = FALSE;
}
if ($use_flv && module_exists('flvmediaplayer')) {
$params['width'] = $width;
$params['height'] = $height;
$params['div_id'] = $id;
$params['allowFullScreen'] = $fullscreen_value;
$flv_profile = isset($options['flv_profile']) ? $options['flv_profile'] : variable_get('emfield_flv_profile', '');
if ($flv_profile) {
$data = flvmediaplayer_build_player_data($options['node'], $flv_profile, array(
'file' => $flashvars['file'],
));
$data['params']['width'] = $width;
$data['params']['height'] = $height;
$data['flashvars']['autostart'] = $flashvars['autostart'];
$data['params']['allowFullScreen'] = $fullscreen_value;
$data['params']['wmode'] = 'transparent';
$data['flashvars']['image'] = $flashvars['image'];
$output = theme('flvmediaplayer_render_player', $url, $data['params'], $data['flashvars']);
}
else {
$output = theme('flvmediaplayer_render_player', $url, $params, $flashvars);
}
}
else {
if (variable_get('emfield_swfobject', FALSE) && (module_exists('swfobject_api') || variable_get('emfield_swfobject_location', ''))) {
$params['width'] = $width;
$params['height'] = $height;
$params['div_id'] = $id;
$params['allowFullScreen'] = $fullscreen_value;
$params['wmode'] = 'transparent';
$output = theme('emfield_swfobject', $url, $params, $flashvars, $id);
}
else {
if ($use_flv) {
unset($tn_options['return_url']);
$noembed = theme('emvideo_video_thumbnail', NULL, $options['item'], 'emvideo_thumbnail', $options['node'], TRUE, $tn_options);
$flv_path = url($flv_path);
$flashvars = check_plain(drupal_query_string_encode($flashvars));
$output = <<<FLV
<div id="{<span class="php-variable">$div_id</span>}">
<object
type="application/x-shockwave-flash" data="{<span class="php-variable">$flv_path</span>}"
width="{<span class="php-variable">$width</span>}" height="{<span class="php-variable">$height</span>}" id="{<span class="php-variable">$id</span>}">
<param name="movie" value="{<span class="php-variable">$flv_path</span>}" />
<param name="flashvars" value="{<span class="php-variable">$flashvars</span>}" />
<param name="wmode" value="transparent" />
{<span class="php-variable">$noembed</span>}
</object>
</div>
FLV;
}
else {
$flashvars = check_plain(drupal_query_string_encode(array(
'playerMode' => 'embedded',
)));
$output = theme('emvideo_youtube_default_external', $height, $width, $url, $id, $div_id, $fullscreen_value, $flashvars);
}
}
}
}
return '<div class="emfield-emvideo emfield-emvideo-youtube">' . $output . '</div>';
}
function theme_emvideo_youtube_default_external($height, $width, $url, $id, $div_id, $fullscreen_value, $flashvars) {
$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="allowScriptAccess" value="sameDomain"/>
<param name="quality" value="best"/>
<param name="allowFullScreen" value="{<span class="php-variable">$fullscreen_value</span>}"/>
<param name="bgcolor" value="#FFFFFF"/>
<param name="scale" value="noScale"/>
<param name="salign" value="TL"/>
<param name="FlashVars" value="{<span class="php-variable">$flashvars</span>}" />
<param name="wmode" value="transparent" />
</object></div>
FLASH;
return $output;
}
function emvideo_youtube_thumbnail($field, $item, $formatter, $node, $width, $height, $options = array()) {
if ($item['data']['thumbnail']['url']) {
return $item['data']['thumbnail']['url'];
}
$tn = "http://img.youtube.com/vi/{$item['value']}/0.jpg";
return $tn;
}
function emvideo_youtube_video($code, $width, $height, $field, $item, $node, $autoplay, $options = array()) {
$options['item'] = $item;
$options['node'] = $node;
$output = theme('emvideo_youtube_flash', $code, $width, $height, $autoplay, $options);
return $output;
}
function emvideo_youtube_preview($code, $width, $height, $field, $item, $node, $autoplay, $options = array()) {
$options['item'] = $item;
$options['node'] = $node;
$output = theme('emvideo_youtube_flash', $code, $width, $height, $autoplay, $options);
return $output;
}
function emvideo_youtube_emfield_subtheme() {
return array(
'emvideo_youtube_flash' => array(
'arguments' => array(
'embed' => NULL,
'width' => NULL,
'height' => NULL,
'autoplay' => NULL,
'options' => array(),
),
'file' => 'providers/youtube.inc',
),
'emvideo_youtube_default_external' => array(
'arguments' => array(
'height' => NULL,
'width' => NULL,
'url' => NULL,
'id' => NULL,
'div_id' => NULL,
'fullscreen_value' => NULL,
'flashvars' => NULL,
),
),
);
}
function emvideo_youtube_content_generate() {
return array(
'http://www.youtube.com/watch?v=-jubiv7QUco',
'http://www.youtube.com/watch?v=VG_ss5QT03Y',
'http://www.youtube.com/watch?v=LrUMJgyQVE8',
'http://www.youtube.com/watch?v=H2eI02rK9_U',
'http://www.youtube.com/watch?v=B3qxki6H8Fk',
'http://www.youtube.com/watch?v=Rsaw5NuFIto',
'http://www.youtube.com/watch?v=J3CaN-g5mQQ',
'http://www.youtube.com/watch?v=L2pXcNu4GRo',
);
}