| 1 |
<?php
|
| 2 |
// $Id: video_filter.module,v 1.13 2009/09/19 15:21:16 blackdog Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Video filter is a highly flexible and easy extendable filter module
|
| 7 |
* to embed any type of video in your site using a simple tag
|
| 8 |
*/
|
| 9 |
|
| 10 |
module_load_include('inc', 'video_filter', 'video_filter.codecs');
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implements hook_filter_info().
|
| 14 |
*/
|
| 15 |
function video_filter_filter_info() {
|
| 16 |
$filters = array();
|
| 17 |
$filters['video_filter'] = array(
|
| 18 |
'title' => t('Video Filter'),
|
| 19 |
'description' => t('Substitutes [video:URL] with embedded HTML.'),
|
| 20 |
'process callback' => '_video_filter_process',
|
| 21 |
'settings callback' => '_video_filter_settings',
|
| 22 |
'default settings' => array(
|
| 23 |
'video_filter_width' => '400',
|
| 24 |
'video_filter_height' => '400',
|
| 25 |
'video_filter_autoplay' => 1,
|
| 26 |
'video_filter_priority' => 'height',
|
| 27 |
'video_filter_related' => 1,
|
| 28 |
),
|
| 29 |
'tips callback' => '_video_filter_tips',
|
| 30 |
);
|
| 31 |
return $filters;
|
| 32 |
}
|
| 33 |
|
| 34 |
function _video_filter_settings($form, &$form_state, $filter, $defaults) {
|
| 35 |
$form['video_filter_width'] = array(
|
| 36 |
'#type' => 'textfield',
|
| 37 |
'#title' => t('Default width setting'),
|
| 38 |
'#default_value' => isset($filter->settings['video_filter_width']) ? $filter->settings['video_filter_width'] : $defaults['video_filter_width'],
|
| 39 |
'#maxlength' => 4,
|
| 40 |
);
|
| 41 |
$form['video_filter_height'] = array(
|
| 42 |
'#type' => 'textfield',
|
| 43 |
'#title' => t('Default height setting'),
|
| 44 |
'#default_value' => isset($filter->settings['video_filter_height']) ? $filter->settings['video_filter_height'] : $defaults['video_filter_height'],
|
| 45 |
'#maxlength' => 4,
|
| 46 |
);
|
| 47 |
$form['video_filter_autoplay'] = array(
|
| 48 |
'#type' => 'radios',
|
| 49 |
'#title' => t('Default autoplay setting'),
|
| 50 |
'#description' => t('Not all video formats support this setting.'),
|
| 51 |
'#default_value' => isset($filter->settings['video_filter_autoplay']) ? $filter->settings['video_filter_autoplay'] : $defaults['video_filter_autoplay'],
|
| 52 |
'#options' => array(
|
| 53 |
0 => t('No'),
|
| 54 |
1 => t('Yes'),
|
| 55 |
),
|
| 56 |
);
|
| 57 |
$form['video_filter_priority'] = array(
|
| 58 |
'#type' => 'radios',
|
| 59 |
'#title' => t('Default priority setting'),
|
| 60 |
'#description' => t('Should height or width take priority when videos are re-sized to fit?'),
|
| 61 |
'#default_value' => isset($filter->settings['video_filter_priority']) ? $filter->settings['video_filter_priority'] : $defaults['video_filter_priority'],
|
| 62 |
'#options' => array(
|
| 63 |
'height' => t('Height'),
|
| 64 |
'width' => t('Width'),
|
| 65 |
),
|
| 66 |
);
|
| 67 |
$form['video_filter_related'] = array(
|
| 68 |
'#type' => 'radios',
|
| 69 |
'#title' => t('Related videos setting'),
|
| 70 |
'#description' => t('Show "related videos"? Not all video formats support this setting.'),
|
| 71 |
'#default_value' => isset($filter->settings['video_filter_related']) ? $filter->settings['video_filter_related'] : $defaults['video_filter_related'],
|
| 72 |
'#options' => array(
|
| 73 |
0 => t('No'),
|
| 74 |
1 => t('Yes'),
|
| 75 |
),
|
| 76 |
);
|
| 77 |
|
| 78 |
return $form;
|
| 79 |
}
|
| 80 |
|
| 81 |
function _video_filter_tips($filter, $format, $long = FALSE) {
|
| 82 |
if ($long) {
|
| 83 |
$codecs = module_invoke_all('codec_info');
|
| 84 |
$supported = array();
|
| 85 |
$instructions = array();
|
| 86 |
foreach ($codecs AS $codec) {
|
| 87 |
$supported[] = $codec['name'];
|
| 88 |
$instructions[] = isset($codec['instructions']) ? '<li>' . $codec['name'] . ':<br/>' . $codec['instructions'] . '</li>' : '';
|
| 89 |
}
|
| 90 |
return t('
|
| 91 |
<p><strong>Video Filter</strong></p>
|
| 92 |
<p>You may insert videos from popular video sites by using a simple tag <code>[video:URL]</code>.</p>
|
| 93 |
<p>Examples:</p>
|
| 94 |
<ul>
|
| 95 |
<li>Single video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId]</code></li>
|
| 96 |
<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>
|
| 97 |
<li>Override default autoplay setting: <code>[video:http://www.youtube.com/watch?v=uN1qUeId autoplay:1]</code></li>
|
| 98 |
<li>Override default width and height:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId width:X height:Y]</code></li>
|
| 99 |
<li>Align the video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId align:right]</code></li>
|
| 100 |
</ul>
|
| 101 |
<p>Supported sites: @codecs.</p>
|
| 102 |
<p>Special instructions:</p>
|
| 103 |
<small>Some codecs need special input. You\'ll find those instructions here.</small>
|
| 104 |
<ul>!instructions</ul>', array('@codecs' => implode(', ', $supported), '!instructions' => implode('', $instructions)));
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
return t('You may insert videos with [video:URL]');
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
function _video_filter_process($text, $filter) {
|
| 112 |
if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
|
| 113 |
foreach ($matches_code[0] as $ci => $code) {
|
| 114 |
$video = array(
|
| 115 |
'source' => $matches_code[2][$ci],
|
| 116 |
'width' => $filter->settings['video_filter_width'],
|
| 117 |
'height' => $filter->settings['video_filter_height'],
|
| 118 |
'autoplay' => $filter->settings['video_filter_autoplay'],
|
| 119 |
'related' => $filter->settings['video_filter_related'],
|
| 120 |
);
|
| 121 |
|
| 122 |
// Pick random out of multiple sources separated by ','
|
| 123 |
if (strstr($video['source'], ',')) {
|
| 124 |
$sources = explode(',', $video['source']);
|
| 125 |
$random = array_rand($sources, 1);
|
| 126 |
$video['source'] = $sources[$random];
|
| 127 |
}
|
| 128 |
|
| 129 |
// Load all codecs
|
| 130 |
$codecs = module_invoke_all('codec_info');
|
| 131 |
|
| 132 |
// Find codec
|
| 133 |
foreach ($codecs as $codec) {
|
| 134 |
if (!is_array($codec['regexp'])) {
|
| 135 |
$codec['regexp'] = array($codec['regexp']);
|
| 136 |
}
|
| 137 |
|
| 138 |
// Try different regular expressions
|
| 139 |
foreach ($codec['regexp'] as $delta => $regexp) {
|
| 140 |
if (preg_match($regexp, $video['source'], $matches)) {
|
| 141 |
$video['codec'] = $codec;
|
| 142 |
$video['codec']['delta'] = $delta;
|
| 143 |
$video['codec']['matches'] = $matches;
|
| 144 |
break 2;
|
| 145 |
}
|
| 146 |
}
|
| 147 |
}
|
| 148 |
|
| 149 |
// Codec found
|
| 150 |
if ($video['codec']) {
|
| 151 |
// Override default attributes
|
| 152 |
if ($matches_code[3][$ci] && preg_match_all('/\s+([a-z]+)\:([^\s]+)/i', $matches_code[3][$ci], $matches_attributes)) {
|
| 153 |
foreach ($matches_attributes[0] as $ai => $attribute) {
|
| 154 |
$video[$matches_attributes[1][$ai]] = $matches_attributes[2][$ai];
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
// Resize within width and height to given ratio
|
| 159 |
if ($video['codec']['ratio']) {
|
| 160 |
// @todo - make this section prettier?
|
| 161 |
if ($filter->settings['video_filter_priority'] == 'height') {
|
| 162 |
if ($video['width'] * $video['codec']['ratio'] > $video['height']) {
|
| 163 |
$video['width'] = round($video['height'] * $video['codec']['ratio']);
|
| 164 |
$video['height'] = round($video['width'] / $video['codec']['ratio']);
|
| 165 |
}
|
| 166 |
else {
|
| 167 |
$video['height'] = round($video['width'] / $video['codec']['ratio']);
|
| 168 |
$video['width'] = round($video['height'] * $video['codec']['ratio']);
|
| 169 |
}
|
| 170 |
}
|
| 171 |
else {
|
| 172 |
if ($video['height'] * $video['codec']['ratio'] > $video['width']) {
|
| 173 |
$video['height'] = round($video['width'] / $video['codec']['ratio']);
|
| 174 |
$video['width'] = round($video['height'] * $video['codec']['ratio']);
|
| 175 |
}
|
| 176 |
else {
|
| 177 |
$video['width'] = round($video['height'] * $video['codec']['ratio']);
|
| 178 |
$video['height'] = round($video['width'] / $video['codec']['ratio']);
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
$video['autoplay'] = (bool) $video['autoplay'];
|
| 184 |
$video['align'] = in_array($video['align'], array('left', 'right')) ? $video['align'] : NULL;
|
| 185 |
|
| 186 |
$replacement = $video['codec']['callback']($video);
|
| 187 |
// Invalid format
|
| 188 |
}
|
| 189 |
else {
|
| 190 |
$replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
|
| 191 |
}
|
| 192 |
|
| 193 |
$text = str_replace($code, $replacement, $text);
|
| 194 |
}
|
| 195 |
}
|
| 196 |
|
| 197 |
return $text;
|
| 198 |
}
|
| 199 |
|
| 200 |
/**
|
| 201 |
* Wrapper that calls the theme function.
|
| 202 |
*/
|
| 203 |
function video_filter_flash($video, $params = array()) {
|
| 204 |
return theme('video_filter_flash', $video, $params);
|
| 205 |
}
|
| 206 |
|
| 207 |
/**
|
| 208 |
* Function that outputs the <object> element.
|
| 209 |
*
|
| 210 |
* @ingroup themeable
|
| 211 |
*/
|
| 212 |
function theme_video_filter_flash($video, $params) {
|
| 213 |
$output = '';
|
| 214 |
|
| 215 |
$output .= '<object type="application/x-shockwave-flash" ';
|
| 216 |
|
| 217 |
if ($video['align']) {
|
| 218 |
$output .= 'style="float:' . $video['align'] . '" ';
|
| 219 |
}
|
| 220 |
|
| 221 |
$output .= 'width="' . $video['width'] . '" height="' . $video['height'] . '" data="' . $video['source'] . '">' . "\n";
|
| 222 |
|
| 223 |
$defaults = array(
|
| 224 |
'movie' => $video['source'],
|
| 225 |
'wmode' => 'transparent',
|
| 226 |
'allowFullScreen' => 'true',
|
| 227 |
);
|
| 228 |
|
| 229 |
$params = array_merge($defaults, (is_array($params) && count($params)) ? $params : array());
|
| 230 |
|
| 231 |
foreach ($params as $name => $value) {
|
| 232 |
$output .= ' <param name="' . $name . '" value="' . $value . '" />' . "\n";
|
| 233 |
}
|
| 234 |
|
| 235 |
$output .= '</object>' . "\n";
|
| 236 |
|
| 237 |
return $output;
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Implements hook_theme().
|
| 242 |
*/
|
| 243 |
function video_filter_theme($existing, $type, $theme, $path) {
|
| 244 |
return array(
|
| 245 |
'video_filter_flash' => array(
|
| 246 |
'arguments' => array('video' => NULL, 'params' => array()),
|
| 247 |
),
|
| 248 |
);
|
| 249 |
}
|