View source
<?php
include_once drupal_get_path('module', 'swftools') . '/genericplayers.module';
define('SWFTOOLS_EMBED_METHOD', 'swftools_embed_method');
define('SWFTOOLS_IMAGE_DISPLAY_LIST', 'swftools_image_display_list');
define('SWFTOOLS_FLV_DISPLAY', 'swftools_flv_display');
define('SWFTOOLS_FLV_DISPLAY_LIST', 'swftools_flv_display_list');
define('SWFTOOLS_MP3_DISPLAY', 'swftools_mp3_display');
define('SWFTOOLS_MP3_DISPLAY_LIST', 'swftools_mp3_display_list');
define('SWFTOOLS_MEDIA_DISPLAY', 'swftools_media_display');
define('SWFTOOLS_MEDIA_DISPLAY_LIST', 'swftools_media_display_list');
define('SWFTOOLS_SWF_DISPLAY_DIRECT', 'swftools_swf_display_direct');
define('SWFDEFAULT', FALSE);
define('SWFTOOLS_SWF', 'swftools_swf');
define('SWFTOOLS_CUSTOM', 'swftools_custom');
define('SWFTOOLS_NOJAVASCRIPT', 'swftools_nojavascript');
define('SWFTOOLS_DEFAULT_BG', $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'swftools') . '/shared/swftools-default.jpg');
define('SWFTOOLS_PLAYLIST_PATH', 'playlists');
define('SWFTOOLS_PLAYER_PATH', '');
define('SWFTOOLS_GRANT_ACCESS_TO_PRIVATE_FILES', TRUE);
function swftools_menu($may_cache) {
if (variable_get('swftools_always_add_js', FALSE)) {
swftools_push_js();
}
$items = array();
$swf_admin = user_access('administer flash');
if ($may_cache) {
$items[] = array(
'path' => 'admin/media/swf',
'title' => t('SWF Tools'),
'callback' => 'swftools_admin',
'access' => $swf_admin,
'position' => 'left',
'description' => t('SWF Tools provide integration with Macromedia Flash related methods and tools like Video Players, MP3 Players and Image Viewers'),
);
$items[] = array(
'path' => 'admin/media/swf/embed',
'title' => t('Embedding settings'),
'description' => t('Settings specific to Flash embedding. Object and embed attributes can be set here.'),
'weight' => -2,
);
$items[] = array(
'path' => 'admin/media/swf/handling',
'title' => t('File handling'),
'description' => t('Flash players and handling for various file types.'),
'weight' => -1,
);
$items = array_merge($items, genericplayers_menu(TRUE));
}
return $items;
}
function swftools_admin() {
cache_clear_all();
cache_clear_all('*', 'cache_filter', TRUE);
if ($page = arg(3)) {
return drupal_get_form('swftools_admin_' . $page . '_form');
}
else {
return system_admin_menu_block_page();
}
}
function swftools_perm() {
return array(
'administer flash',
);
}
function swftools_admin_embed_form() {
include_once drupal_get_path('module', 'swftools') . '/swftools.admin.inc';
$form = _swftools_admin_embed_form();
cache_clear_all();
cache_clear_all('*', 'cache_filter', TRUE);
return system_settings_form($form);
}
function swftools_admin_handling_form() {
include_once drupal_get_path('module', 'swftools') . '/swftools.admin.inc';
$form = _swftools_admin_handling_form();
cache_clear_all();
cache_clear_all('*', 'cache_filter', TRUE);
return system_settings_form($form);
}
function swf_list($playlist_data, $params = SWFDEFAULT, $flashvars = SWFDEFAULT, $othervars = SWFDEFAULT, $methods = SWFDEFAULT) {
if (is_array($playlist_data)) {
if (!$methods) {
$methods = array();
}
$methods['action'] = $playlist_data['action'];
if (!$othervars) {
$othervars = array();
}
$othervars['playlist_data'] = $playlist_data;
if (isset($playlist_data['filename'])) {
$playlist = $playlist_data['filename'];
}
else {
$playlist = '';
}
return swf($playlist, $params, $flashvars, $othervars, $methods);
}
else {
}
}
function swf($file, $params = SWFDEFAULT, $flashvars = SWFDEFAULT, $othervars = SWFDEFAULT, $methods = SWFDEFAULT) {
$all_methods = swftools_methods_available();
$action = isset($methods['action']) ? $methods['action'] : FALSE;
if (!$action) {
$action = swftools_get_action($file);
}
$html_alt = $othervars['html_alt'] ? $othervars['html_alt'] : variable_get('swftools_html_alt', '<p>Sorry, flash is not available.</p>');
$resolved_methods = new stdClass();
$player = isset($methods['player']) ? $methods['player'] : FALSE;
if (!$player) {
$player = swftools_get_player($action);
if (!$player) {
drupal_set_message("No player is configured for the action '{$action}'", 'error');
return $html_alt;
}
}
if (isset($all_methods[$action][$player])) {
$resolved_methods->player = $all_methods[$action][$player];
}
else {
if ($action == SWFTOOLS_SWF_DISPLAY_DIRECT) {
$resolved_methods->player = $all_methods[$action][SWFTOOLS_CUSTOM];
$resolved_methods->player['shared_file'] = $player;
}
else {
drupal_set_message("Could not find the '{$player}' file for embedding.", 'error');
return $html_alt;
}
}
$embed = isset($methods['embed']) ? $methods['embed'] : FALSE;
if (!$embed) {
$embed = variable_get(SWFTOOLS_EMBED_METHOD, SWFTOOLS_NOJAVASCRIPT);
}
$resolved_methods->embed = $all_methods[SWFTOOLS_EMBED_METHOD][$embed];
$vars = new stdClass();
$vars->othervars = is_array($othervars) ? $othervars : array();
if ($params) {
if (is_array($params)) {
$vars->params = $params;
}
else {
$dimensions = explode('x', $params);
if (count($dimensions) == 2) {
$vars->params = array(
'width' => $dimensions[0],
'height' => $dimensions[1],
);
}
}
}
else {
$vars->params = array();
}
if ($flashvars) {
if (is_array($flashvars)) {
$vars->flashvars = $flashvars;
}
else {
parse_str($flashvars, $vars->flashvars);
}
}
else {
$vars->flashvars = array();
}
$base = $vars->params['base'] ? $vars->params['base'] : '';
if (!$base || !valid_url($base)) {
$base = swftools_get_media_url('', FALSE);
}
$vars->params['base'] = $base;
if (isset($othervars['playlist_data'])) {
$playlist = TRUE;
$file = swftools_generate_playlist($othervars['playlist_data'], $playlist_name, $resolved_methods, $vars, FALSE);
if (!$file) {
drupal_set_message("Unable to create playlist.");
return $html_alt;
}
}
$nocache = '';
if (variable_get('swftools_playlist_caching', 'here') == 'always') {
}
$orig_file = file_create_path($file);
if (!valid_url($file, TRUE)) {
if (!$playlist) {
$file = swftools_get_media_path() . $file;
}
$file_url = swftools_get_media_url($file) . $nocache;
if (!$file_url) {
return $html_alt;
}
}
else {
$file_url = $file;
}
$vars->othervars['file_url'] = $file_url;
switch ($player) {
case SWFTOOLS_SWF:
$vars->params['src_path'] = $orig_file;
$vars->params['src'] = $file_url;
break;
case SWFTOOLS_CUSTOM:
$vars->params['src_path'] = $resolved_methods->player['shared_file'];
$vars->params['src'] = swftools_get_media_url($resolved_methods->player['shared_file']);
break;
default:
$vars->params['src_path'] = swftools_get_player_path() . '/' . $resolved_methods->player['shared_file'];
$vars->params['src'] = $GLOBALS['base_url'] . '/' . swftools_get_player_path() . '/' . $resolved_methods->player['shared_file'];
}
$vars->params = array_merge(_swftools_params(), $vars->params);
$player_flashvars = call_user_func_array($resolved_methods->player['module'] . '_swftools_flashvars', array(
$action,
&$resolved_methods,
&$vars,
));
$vars->flashvars = array_merge($vars->flashvars, $player_flashvars);
if (isset($resolved_methods->player['file'])) {
$vars->flashvars[$resolved_methods->player['file']] = $file_url;
}
if (isset($resolved_methods->player['version'])) {
$vars->params['version'] = $resolved_methods->player['version'];
}
if (!$vars->flashvars['width'] && !$vars->flashvars['height']) {
if ($vars->params['width'] && $vars->params['height']) {
$vars->flashvars['width'] = $vars->params['width'];
$vars->flashvars['height'] = $vars->params['height'];
}
}
if (!$vars->params['width'] && !$vars->params['height']) {
if ($vars->flashvars['width'] && $vars->flashvars['height']) {
$vars->params['width'] = $vars->flashvars['width'];
$vars->params['height'] = $vars->flashvars['height'];
}
}
if (empty($vars->params['height']) && empty($vars->params['width'])) {
$info = swftools_get_info($vars->params['src_path']);
if ($info) {
$vars->params['height'] = $info['height'];
$vars->params['width'] = $info['width'];
}
}
$vars->params['flashvars'] = _swftools_get_flashvars_string($vars->flashvars);
$output = module_invoke($resolved_methods->embed['module'], 'swftools_embed', $action, $resolved_methods, $vars, $html_alt);
return theme('swftools_embed', $output, $action, $resolved_methods, $vars, $preview);
}
function theme_swftools_embed($embed_code, $action, $methods, $vars, $html_alt) {
$id = $vars->othervars['id'] ? ' id="swf-' . $vars->othervars['id'] . '"' : '';
$classes[] = 'swftools-wrapper';
$classes[] = str_replace('_', '-', $methods->player['name']);
$classes[] = $vars->othervars['class'];
return '<div' . $id . ' class="' . implode(' ', $classes) . '">' . $embed_code . '</div>';
}
function swftools_swftools_embed($action = 'add_js', $methods = FALSE, $vars = FALSE, $preview = 'NA') {
if ($action == 'add_js') {
return;
}
$P = $vars->params;
$width_attr = $P['width'] ? ' width="' . $P['width'] . '"' : '';
$height_attr = $P['height'] ? ' height="' . $P['height'] . '"' : '';
$loop = _swftools_tf($P['loop']);
$menu = _swftools_tf($P['menu']);
$play = _swftools_tf($P['play']);
$fullscreen = _swftools_tf($P['allowfullscreen']);
$id = $vars->othervars['id'] ? ' id="' . $vars->othervars['id'] . '"' : '';
$name = $vars->othervars['id'] ? ' name="' . $vars->othervars['id'] . '"' : '';
$swliveconnect = $P['swliveconnect'] ? ' swliveconnect="' . $P['swliveconnect'] . '"' : '';
$html = '<div class="swftools"><object' . $width_attr . $height_attr . $id . ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' . $vars->params['version'] . ',0,0,0">' . "\n";
$html .= '<param name="allowScriptAccess" value="sameDomain" />' . "\n";
$html .= '<param name="movie" value="' . $P['src'] . '" />' . "\n";
$html .= '<param name="wmode" value="' . $P['wmode'] . '" />' . "\n";
$html .= '<param name="bgcolor" value="' . $P['bgcolor'] . '" />' . "\n";
$html .= '<param name="scale" value="' . $P['scale'] . '" />' . "\n";
$html .= '<param name="quality" value="' . $P['quality'] . '" />' . "\n";
$html .= '<param name="align" value="' . $P['align'] . '" />' . "\n";
$html .= '<param name="allowFullScreen" value="' . $P['allowfullscreen'] . '" />' . "\n";
$html .= '<param name="base" value="' . $P['base'] . '" />' . "\n";
$html .= '<param name="play" value="' . $play . '" />' . "\n";
$html .= '<param name="menu" value="' . $menu . '" />' . "\n";
$html .= '<param name="loop" value="' . $loop . '" />' . "\n";
$html .= $P['flashvars'] ? '<param name="FlashVars" value="' . $P['flashvars'] . '" />' . "\n" : '';
$html .= '<embed' . $width_attr . $height_attr . $name . $swliveconnect . ' src="' . $P['src'] . '"' . ' wmode="' . $P['wmode'] . '"' . ' bgcolor="' . $P['bgcolor'] . '"' . ' scale="' . $P['scale'] . '"' . ' quality="' . $P['quality'] . '"' . ' loop="' . $loop . '"' . ' menu="' . $menu . '"' . ' play="' . $play . '"' . ' name="' . $P['name'] . '"' . ' base="' . $P['base'] . '"' . ' allowFullScreen="' . $P['allowfullscreen'] . '"' . ($P['salign'] ? ' salign="' . $P['salign'] . '"' : '') . ($P['flashvars'] ? ' FlashVars="' . $P['flashvars'] . '"' : '') . ' allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"' . ' />' . "\n";
$html .= '</object></div>' . "\n";
return $html;
}
function swftools_methods_available($action = NULL, $reset = FALSE) {
static $methods = array();
if ($reset) {
$methods = array();
}
elseif (count($methods)) {
return $action ? $methods[$action] : $methods;
}
foreach (module_implements('swftools_methods') as $mod) {
$methods = array_merge_recursive($methods, call_user_func($mod . '_swftools_methods'));
}
$methods = array_merge_recursive($methods, genericplayers_swftools_methods());
$methods[SWFTOOLS_EMBED_METHOD][SWFTOOLS_NOJAVASCRIPT] = array(
'name' => SWFTOOLS_NOJAVASCRIPT,
'module' => 'swftools',
'shared_file' => '',
'title' => t('Embed Flash Directly, don\'t use JavaScript replacement.'),
);
$methods[SWFTOOLS_SWF_DISPLAY_DIRECT][SWFTOOLS_SWF] = array(
'name' => SWFTOOLS_SWF,
'module' => 'swftools',
'shared_file' => '',
'title' => t('Use SWF file directly, no streaming through another SWF.'),
);
$methods[SWFTOOLS_SWF_DISPLAY_DIRECT][SWFTOOLS_CUSTOM] = array(
'name' => SWFTOOLS_CUSTOM,
'module' => 'swftools',
'shared_file' => '',
'title' => t('Use custom SWF file.'),
);
return $action ? $methods[$action] : $methods;
}
function swftools_json_params(&$params, $attr = 'swftools') {
return $attr . "='" . drupal_to_js($params) . "'";
}
function swftools_swftools_flashvars($action, &$methods, &$vars) {
return $vars->flashvars;
}
function _swftools_tf($bool) {
if (strtolower($bool) == 'false') {
$bool = FALSE;
}
return $bool ? 'true' : 'false';
}
function swftools_get_action($file) {
$path_parts = pathinfo($file);
switch (strtolower($path_parts['extension'])) {
case 'flv':
return SWFTOOLS_FLV_DISPLAY;
case 'swf':
return SWFTOOLS_SWF_DISPLAY_DIRECT;
case 'mp3':
return SWFTOOLS_MP3_DISPLAY;
case 'jpg':
case 'gif':
case 'png':
case 'jpeg':
case 'img':
return SWFTOOLS_IMAGE_DISPLAY_LIST;
default:
return SWFTOOLS_MEDIA_DISPLAY_LIST;
}
}
function swftools_get_player($action, $part = FALSE) {
$methods = swftools_methods_available($action);
switch ($action) {
case SWFTOOLS_FLV_DISPLAY:
return variable_get(SWFTOOLS_FLV_DISPLAY, GENERIC_FLV);
case SWFTOOLS_MP3_DISPLAY:
return variable_get(SWFTOOLS_MP3_DISPLAY, GENERIC_MP3);
case SWFTOOLS_SWF_DISPLAY_DIRECT:
return variable_get(SWFTOOLS_SWF_DISPLAY_DIRECT, SWFTOOLS_SWF);
default:
return variable_get($action, FALSE);
}
}
function swftools_get_playlist_path($dir = FALSE) {
if (!$dir) {
$dir = variable_get('swftools_playlist_path', SWFTOOLS_PLAYLIST_PATH);
}
$dir = file_create_path($dir);
if (!file_check_directory($dir, FILE_CREATE_DIRECTORY)) {
drupal_set_message("{$dir} does not exist, or is not writeable.");
}
return $dir;
}
function swftools_get_player_path($dir = FALSE) {
if (!$dir) {
$dir = variable_get('swftools_player_path', SWFTOOLS_PLAYER_PATH);
if (!$dir) {
$dir = drupal_get_path('module', 'swftools') . '/shared';
}
}
elseif (substr($dir, 0, 1) == '/') {
$dir = ltrim($dir, '/');
}
else {
$dir = file_create_path($dir);
}
return $dir;
}
function swftools_get_media_path() {
$url = trim(variable_get('swftools_media_url', ''));
if (!$url || $url == '') {
return file_create_path('') . '/';
}
return '';
}
function swftools_get_media_url($path, $is_file = TRUE) {
$media_url = variable_get('swftools_media_url', '');
if ($media_url) {
return $media_url . '/' . $path;
}
if (variable_get('swftools_check_media', TRUE) && $is_file) {
if (file_exists($path)) {
return file_create_url($path);
}
else {
drupal_set_message("Could not display the flash because \"{$path}\" does not appear to exist.", 'error');
return FALSE;
}
}
else {
return file_create_url($path);
}
}
function _swftools_get_flashvars_string(&$flashvars) {
foreach ($flashvars as $var => $value) {
$flashvars[$var] = str_replace(array(
'&',
'=',
'?',
), array(
'%26',
'%3D',
'%3F',
), $value);
}
$encoded = drupal_query_string_encode($flashvars);
$encoded = str_replace('%2523', '0x', $encoded);
$encoded = str_replace('%3A', ':', $encoded);
$encoded = str_replace('%252F', '/', $encoded);
return $encoded;
}
function _swftools_params() {
$swf_options = _swftools_options();
$defaults = array(
'wmode' => variable_get('swftools_params_wmode', 'opaque'),
'bgcolor' => variable_get('swftools_params_bgcolor', '#FFFFFF'),
'menu' => variable_get('swftools_params_menu', FALSE),
'play' => variable_get('swftools_params_play', TRUE),
'loop' => variable_get('swftools_params_loop', FALSE),
'quality' => variable_get('swftools_params_quality', 'autohigh'),
'align' => variable_get('swftools_params_align', 'l'),
'salign' => variable_get('swftools_params_salign', 'tl'),
'scale' => variable_get('swftools_params_scale', 'showall'),
'swliveconnect' => variable_get('swftools_params_swliveconnect', 'default'),
'version' => variable_get('swftools_params_version', '7'),
'allowfullscreen' => variable_get('swftools_params_allowfullscreen', TRUE),
);
$defaults['play'] = _swftools_tf($defaults['play']);
$defaults['loop'] = _swftools_tf($defaults['loop']);
$defaults['menu'] = _swftools_tf($defaults['menu']);
$defaults['allowfullscreen'] = _swftools_tf($defaults['allowfullscreen']);
return $defaults;
}
function _swftools_options() {
static $swf_options = array();
if (!count($swf_options)) {
$swf_options['quality'] = array(
'low' => 'low',
'autolow' => 'autolow',
'medium' => 'medium',
'high' => 'high',
'autohigh' => 'autohigh',
'best' => 'best',
);
$swf_options['wmode'] = array(
'window' => 'window',
'opaque' => 'opaque',
'transparent' => 'transparent',
);
$swf_options['scale'] = array(
'showall' => 'showall',
'noborder' => 'noborder',
'exactfit' => 'exactfit',
);
$swf_options['align'] = array(
'default' => 'centered',
'l' => 'left',
'r' => 'right',
't' => 'top',
'b' => 'bottom',
);
$swf_options['salign'] = array(
'l' => 'left',
'r' => 'right',
't' => 'top',
'b' => 'bottom',
'tl' => 'top left',
'tr' => 'top right',
'bl' => 'bottom left',
'br' => 'bottom right',
);
$swf_options['bool'] = array(
'default' => 'default',
'true' => 'true',
'false' => 'false',
);
}
return $swf_options;
}
function swftools_get_info($file) {
if (trim(variable_get('swftools_media_url', '')) == '' or strpos($file, swftools_get_player_path()) === 0) {
$info = image_get_info($file);
return $info;
}
return FALSE;
}
function swftools_generate_playlist(&$playlist_data, $playlist_name, &$method, &$vars) {
if (!$playlist_name) {
$prename = '';
foreach ($playlist_data['playlist'] as $data) {
$prename .= $data['filename'];
}
$playlist_name = md5($method->player['name'] . $prename) . '.xml';
}
$playlist_name = swftools_get_playlist_path() . "/{$playlist_name}";
if (variable_get('swftools_playlist_caching', 'here') == 'always') {
file_delete($playlist_name);
}
elseif (is_file($playlist_name)) {
return file_create_url($playlist_name);
}
$func = 'swftools_' . $method->player['name'] . '_playlist';
if (function_exists($func)) {
$playlist = call_user_func($func, $playlist_data, $method, $vars);
}
else {
drupal_set_message('Error with xml generation by the ' . $method->player['name'] . ' module.', 'error');
}
if (!($handle = fopen($playlist_name, 'a'))) {
drupal_set_message("An error occurred trying to create file {$playlist_name}");
return FALSE;
}
if (fwrite($handle, $playlist) === FALSE) {
drupal_set_message("An error occurred trying to create file {$playlist_name}");
return FALSE;
}
fclose($handle);
return file_create_url($playlist_name);
}
function swftools_push_js($embed = SWFDEFAULT) {
$all_methods = swftools_methods_available();
if (!$embed) {
$embed = variable_get(SWFTOOLS_EMBED_METHOD, SWFTOOLS_NOJAVASCRIPT);
}
$embed = $all_methods[SWFTOOLS_EMBED_METHOD][$embed];
$output = module_invoke($embed['module'], 'swftools_embed');
}
function swftools_prepare_playlist_data($files, $title = '', $get_action = TRUE, $type_filter = array()) {
$playlist_data = array();
$playlist_data['header']['title'] = $title;
$id = 0;
foreach ($files as $key => $data) {
while (array_key_exists($id, $files)) {
$id++;
}
if (is_object($data)) {
$files[$key] = (array) $data;
}
elseif (!is_array($data)) {
$files[$id]['filepath'] = $data;
if (!is_numeric($key)) {
$files[$id]['filename'] = $key;
}
else {
$files[$id]['filename'] = $data;
}
unset($files[$key]);
}
}
$playlist_data['playlist'] = $files;
foreach ($files as $key => $data) {
if (!isset($data['fileurl'])) {
if (valid_url($data['filepath'], TRUE)) {
$playlist_data['playlist'][$key]['filepath'] = FALSE;
$playlist_data['playlist'][$key]['fileurl'] = $data['filepath'];
}
elseif (isset($data['fid'])) {
$playlist_data['playlist'][$key]['filename'] = $data['filename'];
$playlist_data['playlist'][$key]['fileurl'] = swftools_get_media_url($playlist_data['playlist'][$key]['filepath'], FALSE);
}
else {
$playlist_data['playlist'][$key]['filename'] = $data['filename'];
$playlist_data['playlist'][$key]['filepath'] = swftools_get_media_path() . $data['filepath'];
$playlist_data['playlist'][$key]['fileurl'] = swftools_get_media_url($playlist_data['playlist'][$key]['filepath']);
}
}
if (!isset($data['filename'])) {
$path_parts = pathinfo($playlist_data['playlist'][$key]['fileurl']);
$playlist_data['playlist'][$key]['filename'] = $path_parts['basename'];
}
if (!isset($data['title'])) {
$playlist_data['playlist'][$key]['title'] = $playlist_data['playlist'][$key]['filename'];
}
}
if (!$get_action) {
return $playlist_data;
}
else {
$first_valid_file_type = FALSE;
$mixed_media = FALSE;
$fids = array();
foreach ($playlist_data['playlist'] as $key => $data) {
$path_parts = pathinfo($data['fileurl']);
$extension = strtolower($path_parts['extension']);
if (strpos('|jpg|jpeg|gif|png|', $extension)) {
$extension = 'img';
}
if (!count($type_filter) || in_array($extension, $type_filter)) {
if (!$first_valid_file_type) {
$first_valid_file_type = $extension;
}
else {
if ($first_valid_file_type != $extension) {
$mixed_media = TRUE;
}
}
}
else {
unset($playlist_data['playlist'][$key]);
}
}
if ($first_valid_file_type == '') {
return FALSE;
}
if ($mixed_media) {
$action = SWFTOOLS_MEDIA_DISPLAY_LIST;
}
else {
$action = swftools_get_action('dummy.' . $first_valid_file_type);
}
if (count($playlist_data['playlist']) > 1 && substr($action, -5, 5) != '_list') {
$action = $action . '_list';
}
$playlist_data['action'] = $action;
return $playlist_data;
}
}
function swftools_filter_tips($delta, $format, $long = false) {
if ($long) {
return t('
<h3 id="filter-flash_filter">Flash Filter</h3>
<p>The basic syntax for embedding a flash file (.swf), flash movie (.flv) or audio file (.mp3) is:</p>
<blockquote><pre>[flash:filename.swf]</pre></blockquote>
<p>If you would like to override SWF Tools and flash player default settings,
you can specify additional parameters. For example:</p>
<blockquote><pre>[swf file="song.mp3" flashvars="backcolor=#AABBCC&&forecolor=#11AA11"]</pre></blockquote>
Flash Filter will accept following parameters:
<ul>
<li><b>params</b> : You can specify values for output inside <param>
tags with the <embed> html. Typical values are
bgcolor and wmode. Example: <code>params="wmode=true&&bgcolor=#00FF00"</code></li>
<li><b>flashvars</b> : You can specify values for output as flashvars, which
become available to the flash player. Refer to the
documentation of the flash player you are using to
know what flashvar options are available.
Example: <code>flashvars="autostart=true&&volume=80"</code></li>
<li><b>files</b> : Optional list of files to be passed, you\'ll normally define
files relative to your Drupal files directory.
Example: <code>files="name1=image.jpg&&name2=movie.flv"</code></li>
<li><b>methods</b> : Optional information about how to display the file. The most
common usage is to specify a particular media player and
thus override the settings page.
Example: <code>methods="player=onepixelout_mp3"</code></li>
</ul>
<p><strong>WARNING</strong>: with params, flashvars and othervars, pass multiple values
separated by <strong>&&</strong>.</p>');
}
else {
return t('You may use !flash_filter_help to display Flash files inline', array(
"!flash_filter_help" => l('[swf file="song.mp3"]', "filter/tips/{$format}", NULL, 'filter-flash_filter'),
));
}
}
function swftools_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('SWF Tools Filter'),
);
case 'no cache':
return FALSE;
case 'description':
return t('Substitutes [swf file="filename.flv"] or [swflist files="file1.jpg&&file2.jpg"] with embedding code.');
case 'prepare':
return preg_replace('/\\<(swflist|swf)\\s*(.*)>/sU', '[\\1 \\2]', $text);
case 'process':
return _swftools_filter_process_text($text);
default:
return $text;
}
}
function _swftools_filter_process_text($text) {
$endl = chr(13);
if (preg_match_all('@(?:<p>)?\\[(swflist|swf)\\s*(.*?)\\](?:</p>)?@s', $text, $match)) {
$swftools_parameters = array(
'file',
'params',
'flashvars',
'othervars',
'methods',
'files',
);
$match_vars = array();
foreach ($match[2] as $key => $passed_parameters) {
preg_match_all('/(\\w*)=(?:\\"|")(.*?)(?:\\"|")/', $passed_parameters, $match_vars[$key]);
foreach ($match_vars[$key][1] as $vars_key => $vars_name) {
if ($vars_name == 'file') {
$prepared[$key][$vars_name] = $match_vars[$key][2][$vars_key];
unset($match_vars[$key][1][$vars_key]);
}
elseif (in_array($vars_name, $swftools_parameters)) {
$prepared[$key][$vars_name] = swftools_url_parse(str_replace(array(
'&&',
'&&',
), '&', $match_vars[$key][2][$vars_key]));
unset($match_vars[$key][1][$vars_key]);
}
}
if (isset($prepared[$key]['methods']['player'])) {
$player = strtolower($prepared[$key]['methods']['player']);
}
else {
$player_key = array_search('player', $match_vars[$key][1]);
if ($player_key !== FALSE) {
$player = strtolower($match_vars[$key][2][$player_key]);
}
else {
$player = FALSE;
}
}
$prepared[$key]['methods']['player'] = $player;
if (count($match_vars[$key][1])) {
foreach ($match_vars[$key][1] as $vars_key => $vars_name) {
if ($parent = swftools_get_filter_alias($vars_name, $player)) {
if ($parent) {
$prepared[$key][$parent][$vars_name] = $match_vars[$key][2][$vars_key];
}
}
}
}
if (count($prepared[$key])) {
foreach ($swftools_parameters as $swfparameter) {
if (!isset($prepared[$key][$swfparameter])) {
$prepared[$key][$swfparameter] = FALSE;
}
}
}
switch ($match[1][$key]) {
case 'swf':
$replace = swf($prepared[$key]['file'], $prepared[$key]['params'], $prepared[$key]['flashvars'], $prepared[$key]['othervars'], $prepared[$key]['methods']);
break;
case 'swflist':
if ($prepared[$key]['files']) {
foreach ($prepared[$key]['files'] as $name => $filename) {
if (!$filename) {
$prepared[$key]['files'][$name] = $name;
}
}
$playlist_data = swftools_prepare_playlist_data($prepared[$key]['files']);
$replace = swf_list($playlist_data, $prepared[$key]['params'], $prepared[$key]['flashvars'], $prepared[$key]['othervars'], $prepared[$key]['methods']);
}
else {
$replace = '<!-- No files passed to the playlist -->';
}
break;
}
$matched[] = $match[0][$key];
$rewrite[] = $replace;
}
return str_replace($matched, $rewrite, $text);
}
return $text;
}
function swftools_get_filter_alias($var, $player = FALSE) {
static $general_mapping = array();
static $player_mapping = array();
if (!count($general_mapping)) {
$general_mapping = array(
'action' => 'methods',
'embed' => 'methods',
'width' => 'params',
'height' => 'params',
'bgcolor' => 'params',
);
if (!count($player_mapping)) {
$player_mapping = module_invoke_all('swftools_variable_mapping');
}
$combined = array();
if (count($player_mapping)) {
foreach ($player_mapping as $mapping) {
$combined = array_merge($combined, $mapping);
}
$general_mapping = array_merge($combined, $general_mapping);
}
}
if ($player && isset($player_mapping[$player][$var])) {
return $player_mapping[$player][$var];
}
else {
return isset($general_mapping[$var]) ? $general_mapping[$var] : FALSE;
}
}
function swftools_url_parse($string) {
$return = array();
$pairs = split("&", $string);
foreach ($pairs as $pair) {
$splitpair = split("=", $pair);
if (!$splitpair[1] || array_key_exists($splitpair[0], $return)) {
$return[] = $splitpair[0];
}
else {
$return[$splitpair[0]] = $splitpair[1];
}
}
return $return;
}
function swftools_file_download($file) {
$playlist_path = preg_quote(variable_get('swftools_playlist_path', SWFTOOLS_PLAYLIST_PATH));
if (preg_match('/^' . $playlist_path . '/', $file)) {
return array(
'Content-Type: ' . $mime_types[$extension],
'Content-Length: ' . filesize(file_create_path($file)),
);
}
if (variable_get('swftools_grant_access_to_private_files', SWFTOOLS_GRANT_ACCESS_TO_PRIVATE_FILES)) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
$mime_types = array(
'swf' => 'application/x-shockwave-flash',
'flv' => 'application/octet-stream',
'xml' => 'text/xml',
'mp3' => 'audio/mpeg',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
);
if ($mime_types[$extension]) {
return array(
'Content-Type: ' . $mime_types[$extension],
'Content-Length: ' . filesize(file_create_path($file)),
);
}
}
}