View source
<?php
module_load_include('inc', 'video_filter', 'video_filter.codecs');
function video_filter_menu() {
$items = array();
$items['admin/video_filter/dashboard/%'] = array(
'title' => 'Videofilter',
'page callback' => 'video_filter_dashboard_page',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'page arguments' => array(
3,
),
);
return $items;
}
function video_filter_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['#after_build'])) {
if (count($form['#after_build'])) {
foreach ($form['#after_build'] as $after_build) {
if ($after_build == "ckeditor_process_form") {
_video_filter_add_settings('ckeditor');
}
}
}
}
}
function template_preprocess_video_filter_dashboard(&$variables) {
$head_title = array(
strip_tags(drupal_get_title()),
variable_get('site_name', 'Drupal'),
);
$variables['head_title'] = implode(' | ', $head_title);
$variables['base_path'] = base_path();
$variables['front_page'] = url();
$variables['head'] = drupal_get_html_head();
$variables['help'] = theme('help');
$variables['language'] = $GLOBALS['language'];
$variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
$variables['messages'] = isset($variables['show_messages']) ? theme('status_messages') : '';
$variables['styles'] = drupal_get_css();
$variables['scripts'] = drupal_get_js();
$variables['title'] = t('Video filter');
}
function video_filter_dashboard_page($editor) {
module_invoke('admin_menu', 'suppress');
drupal_add_css(drupal_get_path('module', 'video_filter') . '/video_filter.css');
switch ($editor) {
case 'wysiwyg_tinymce':
drupal_add_js(wysiwyg_get_path('tinymce') . '/jscripts/tiny_mce/tiny_mce_popup.js');
drupal_add_js(drupal_get_path('module', 'video_filter') . '/editors/tinymce/video_filter.js');
break;
case 'ckeditor':
case 'wysiwyg_ckeditor':
drupal_add_js(drupal_get_path('module', 'video_filter') . '/editors/ckeditor/video_filter_dialog.js');
break;
case 'fckeditor':
case 'wysiwyg_fckeditor':
drupal_add_js(drupal_get_path('module', 'video_filter') . '/editors/fckeditor/video_filter/video_filter_dialog.js');
break;
}
print theme('video_filter_dashboard', drupal_get_form('_video_filter_form'));
exit;
}
function _video_filter_form() {
$form['video_filter'] = array(
'#type' => 'fieldset',
'#title' => t('Insert Video'),
'#weight' => 0,
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#attributes' => array(
'class' => 'clear-block',
),
);
$form['video_filter']['file_url'] = array(
'#type' => 'textfield',
'#title' => t('Video URL'),
'#maxlength' => 255,
'#size' => 80,
'#default_value' => '',
'#weight' => 1,
);
$form['video_filter']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#maxlength' => 255,
'#size' => 80,
'#default_value' => '',
'#weight' => 2,
);
$form['video_filter']['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#maxlength' => 255,
'#size' => 80,
'#default_value' => '',
'#weight' => 3,
);
$form['video_filter']['align'] = array(
'#type' => 'select',
'#title' => t('Align'),
'#default_value' => 'none',
'#options' => array(
'none' => t('None'),
'left' => t('Left'),
'right' => t('Right'),
'center' => t('Center'),
),
'#weight' => 4,
);
$form['video_filter']['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#weight' => 5,
);
$form['instructions'] = array(
'#type' => 'fieldset',
'#title' => t('Instructions'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array(
'class' => 'clear-block',
),
'#weight' => 97,
);
$text = '<p>' . t('Insert a 3rd party video from one of the following providers.') . '</p>';
$text .= _video_filter_instructions();
$form['instructions']['text'] = array(
'#value' => $text,
);
$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#weight' => 98,
);
$form['insert'] = array(
'#type' => 'button',
'#value' => t('Insert'),
'#weight' => 99,
);
return $form;
}
function video_filter_wysiwyg_plugin($editor, $version) {
_video_filter_add_settings('wysiwyg_' . $editor);
$plugins = array();
switch ($editor) {
case 'tinymce':
$plugins['video_filter'] = array(
'path' => drupal_get_path('module', 'video_filter') . '/editors/tinymce/editor_plugin.js',
'buttons' => array(
'video_filter' => t('Video filter'),
),
'url' => 'http://drupal.org/project/video_filter',
'load' => TRUE,
);
break;
case 'ckeditor':
$plugins['video_filter'] = array(
'path' => drupal_get_path('module', 'video_filter') . '/editors/ckeditor/',
'buttons' => array(
'video_filter' => t('Video filter'),
),
'url' => 'http://drupal.org/project/video_filter',
'load' => TRUE,
);
break;
case 'fckeditor':
$plugins['video_filter'] = array(
'path' => drupal_get_path('module', 'video_filter') . '/editors/fckeditor/',
'buttons' => array(
'video_filter' => t('Video filter'),
),
'url' => 'http://drupal.org/project/video_filter',
'load' => TRUE,
);
break;
}
return $plugins;
}
function _video_filter_add_settings($editor) {
static $editor_settings_added = array();
if (!isset($editor_settings_added[$editor])) {
$editor_settings_added[$editor] = TRUE;
$settings = array(
'video_filter' => array(
'url' => array(
$editor => url('admin/video_filter/dashboard/' . $editor),
),
),
);
drupal_add_js($settings, 'setting');
}
}
function _video_filter_instructions() {
module_load_include('inc', 'video_filter', 'video_filter.codecs');
$codecs = video_filter_get_codec_info();
$output = '<ul>';
foreach ($codecs as $codec) {
$output .= '<li><strong>' . $codec['name'] . '</strong><br />' . $codec['sample_url'] . '</li>';
}
$output .= '</ul>';
return $output;
}
function video_filter_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Video Filter'),
);
case 'description':
return t('Substitutes [video:URL] with embedded HTML.');
case 'process':
return video_filter_process($text, $format);
case 'settings':
return video_filter_settings($format);
default:
return $text;
}
}
function video_filter_filter_tips($delta, $format, $long = FALSE) {
if ($long) {
$codecs = video_filter_get_codec_info();
$supported = array();
$instructions = array();
foreach ($codecs as $codec) {
$supported[] = $codec['name'];
$instructions[] = $codec['instructions'] != '' ? '<li>' . $codec['name'] . ':<br/>' . $codec['instructions'] . '</li>' : '';
}
return t('
<p><strong>Video Filter</strong></p>
<p>You may insert videos from popular video sites by using a simple tag <code>[video:URL]</code>.</p>
<p>Examples:</p>
<ul>
<li>Single video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId]</code></li>
<li>Random video out of multiple:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId1,http://www.youtube.com/watch?v=uN1qUeId2]</code></li>
<li>Override default autoplay setting: <code>[video:http://www.youtube.com/watch?v=uN1qUeId autoplay:1]</code></li>
<li>Override default width and height:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId width:X height:Y]</code></li>
<li>Override default aspect ratio:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId ratio:4/3]</code></li>
<li>Align the video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId align:right]</code></li>
</ul>
<p>Supported sites: @codecs.</p>
<p>Special instructions:</p>
<small>Some codecs need special input. You\'ll find those instructions here.</small>
<ul>!instructions</ul>
', array(
'@codecs' => implode(', ', $supported),
'!instructions' => implode('', $instructions),
));
}
else {
return t('You may insert videos with [video:URL]');
}
}
function video_filter_process($text, $format = -1) {
if (preg_match_all('/\\[video(\\:(.+))?( .+)?\\]/isU', $text, $matches_code)) {
foreach ($matches_code[0] as $ci => $code) {
$video = array(
'source' => $matches_code[2][$ci],
'autoplay' => variable_get('video_filter_autoplay_' . $format, 0),
'related' => variable_get('video_filter_related_' . $format, 1),
);
if (strstr($video['source'], ',')) {
$sources = explode(',', $video['source']);
$random = array_rand($sources, 1);
$video['source'] = $sources[$random];
}
$codecs = video_filter_get_codec_info();
foreach ($codecs as $codec_name => $codec) {
if (!is_array($codec['regexp'])) {
$codec['regexp'] = array(
$codec['regexp'],
);
}
foreach ($codec['regexp'] as $delta => $regexp) {
if (preg_match($regexp, $video['source'], $matches)) {
$video['codec'] = $codec;
$video['codec']['delta'] = $delta;
$video['codec']['matches'] = $matches;
$video['codec']['codec_name'] = $codec_name;
break 2;
}
}
}
if (isset($video['codec'])) {
if ($matches_code[3][$ci] && preg_match_all('/\\s+([a-zA-Z_]+)\\:(\\s+)?([0-9a-zA-Z\\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
foreach ($matches_attributes[0] as $ai => $attribute) {
$video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
}
}
$ratio = 1;
if (isset($video['ratio']) && preg_match('/(\\d+)\\/(\\d+)/', $video['ratio'], $tratio)) {
$ratio = $tratio[1] / $tratio[2];
}
else {
$ratio = $video['codec']['ratio'];
}
if (isset($video['width']) && !isset($video['height'])) {
$video['height'] = variable_get('video_filter_height_' . $format, 400);
}
elseif (isset($video['height']) && !isset($video['width'])) {
$video['width'] = $video['height'] * $ratio;
}
elseif (isset($video['height']) && isset($video['width'])) {
$video['width'] = $video['width'];
$video['height'] = $video['height'];
}
elseif (!isset($video['height']) && !isset($video['width'])) {
$video['width'] = variable_get('video_filter_width_' . $format, 400) != '' ? variable_get('video_filter_width_' . $format, 400) : 400;
$video['height'] = variable_get('video_filter_height_' . $format, 400) != '' ? variable_get('video_filter_height_' . $format, 400) : 400;
}
$control_bar_height = 0;
if (isset($video['control_bar_height'])) {
$control_bar_height = $video['control_bar_height'];
}
elseif (isset($video['codec']['control_bar_height'])) {
$control_bar_height = $video['codec']['control_bar_height'];
}
if ($ratio) {
$scale_factor = min(array(
$video['height'] - $control_bar_height,
$video['width'] / $ratio,
));
$video['height'] = round($scale_factor + $control_bar_height);
$video['width'] = round($scale_factor * $ratio);
}
$video['autoplay'] = (bool) $video['autoplay'];
$video['align'] = isset($video['align']) && in_array($video['align'], array(
'left',
'right',
'center',
)) ? $video['align'] : NULL;
drupal_alter('video_filter_video', $video);
if (variable_get('video_filter_html5_' . $format, 1) == 1 && is_callable($video['codec']['html5_callback'], FALSE)) {
$replacement = call_user_func($video['codec']['html5_callback'], $video);
}
elseif (is_callable($video['codec']['callback'], FALSE)) {
$replacement = call_user_func($video['codec']['callback'], $video);
}
else {
$replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
}
}
else {
$replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
}
$text = str_replace($code, $replacement, $text);
}
}
return $text;
}
function video_filter_settings($format) {
$form['video_filter'] = array(
'#type' => 'fieldset',
'#title' => t('Video filter'),
'#collapsible' => TRUE,
);
$form['video_filter']['video_filter_width_' . $format] = array(
'#type' => 'textfield',
'#title' => t('Default width setting'),
'#default_value' => variable_get('video_filter_width_' . $format, 400),
'#maxlength' => 4,
);
$form['video_filter']['video_filter_height_' . $format] = array(
'#type' => 'textfield',
'#title' => t('Default height setting'),
'#default_value' => variable_get('video_filter_height_' . $format, 400),
'#maxlength' => 4,
);
$form['video_filter']['video_filter_autoplay_' . $format] = array(
'#type' => 'radios',
'#title' => t('Default autoplay setting'),
'#description' => t('Not all video formats support this setting.'),
'#default_value' => variable_get('video_filter_autoplay_' . $format, 1),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$form['video_filter']['video_filter_related_' . $format] = array(
'#type' => 'radios',
'#title' => t('Related videos setting'),
'#description' => t('Show "related videos"? Not all video formats support this setting.'),
'#default_value' => variable_get('video_filter_related_' . $format, 1),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$form['video_filter']['video_filter_html5_' . $format] = array(
'#type' => 'radios',
'#title' => t('Use HTML5'),
'#description' => t('Use HTML5 if the codec provides it. Makes your videos more device agnostic.'),
'#default_value' => variable_get('video_filter_html5_' . $format, 1),
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
return $form;
}
function video_filter_init() {
drupal_add_css(drupal_get_path('module', 'video_filter') . '/video_filter.css');
}
function video_filter_theme($existing, $type, $theme, $path) {
return array(
'video_filter_dashboard' => array(
'arguments' => array(
'content' => NULL,
),
'template' => 'video_filter_dashboard',
),
'video_filter_flash' => array(
'arguments' => array(
'video' => NULL,
'params' => array(),
),
),
'video_filter_iframe' => array(
'arguments' => array(
'video' => NULL,
),
),
);
}
function video_filter_flash($video, $params = array()) {
return theme('video_filter_flash', $video, $params);
}
function video_filter_iframe($video) {
return theme('video_filter_iframe', $video);
}
function video_filter_get_codec_info() {
static $codecs;
if (!isset($codecs)) {
$codecs = module_invoke_all('codec_info');
drupal_alter('video_filter_codec_info', $codecs);
}
return $codecs;
}
function theme_video_filter_flash($video, $params = array()) {
$output = '';
$classes = video_filter_get_classes($video);
$output .= '<object class="' . implode(' ', $classes) . '" type="application/x-shockwave-flash" ';
$output .= 'width="' . $video['width'] . '" height="' . $video['height'] . '" data="' . $video['source'] . '">' . "\n";
$defaults = array(
'movie' => $video['source'],
'wmode' => 'transparent',
'allowFullScreen' => 'true',
);
$params = array_merge($defaults, is_array($params) && count($params) ? $params : array());
foreach ($params as $name => $value) {
$output .= ' <param name="' . $name . '" value="' . $value . '" />' . "\n";
}
$output .= '</object>' . "\n";
return $output;
}
function theme_video_filter_iframe($video) {
$classes = video_filter_get_classes($video);
$output = '<iframe src="' . $video['source'] . '" width="' . $video['width'] . '" height="' . $video['height'] . '" class="' . implode(' ', $classes) . '" frameborder="0"></iframe>';
return $output;
}
function video_filter_get_classes($video) {
$classes = array(
'video-filter',
'video-' . $video['codec']['codec_name'],
);
if (isset($video['align'])) {
$classes[] = 'video-' . $video['align'];
}
unset($video['codec']['matches'][0]);
foreach ($video['codec']['matches'] as $match) {
$classes[] = 'vf-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $match));
}
return $classes;
}
function video_filter_oembed_request($endpoint, array $arguments) {
$result = drupal_http_request(url($endpoint, array(
'query' => $arguments,
)));
if ($data = json_decode($result->data)) {
return (array) $data;
}
else {
return FALSE;
}
}