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('SWFTOOLS_SWF_DISPLAY', 'swftools_swf_display');
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_INSTALLED', TRUE);
define('SWFTOOLS_DEFAULT_HTML_ALT', '<p>You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialise correctly.</p>');
define('SWFTOOLS_PLAYLIST_PATH', 'playlists');
define('SWFTOOLS_PLAYER_PATH', '');
define('SWFTOOLS_GRANT_ACCESS_TO_PRIVATE_FILES', FALSE);
define('SWFTOOLS_GRANT_ACCESS_EXTENSIONS', 'swf flv xml mp3 jpg jpeg png');
define('SWFTOOLS_ALWAYS_ADD_JS', TRUE);
function swftools_init() {
if (variable_get('swftools_always_add_js', SWFTOOLS_ALWAYS_ADD_JS)) {
swftools_push_js();
}
}
function swftools_menu() {
$items = array();
$swf_admin = array(
'administer flash',
);
$items['admin/settings/swftools'] = array(
'title' => 'SWF Tools',
'description' => 'Settings to control how SWF Tools integrates with Adobe Flash related methods and tools like video players, MP3 players and image viewers.',
'access arguments' => $swf_admin,
'page callback' => 'system_admin_menu_block_page',
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/settings/swftools/handling'] = array(
'title' => 'File handling',
'description' => 'Configure how SWF Tools should handle different types of file.',
'access arguments' => $swf_admin,
'weight' => -1,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'swftools_admin_handling_form',
),
'file' => 'swftools.admin.inc',
);
$items['admin/settings/swftools/embed'] = array(
'title' => 'Embedding settings',
'description' => 'Set the embedding method that SWF Tools should use, and configure embedding defaults.',
'access arguments' => $swf_admin,
'weight' => -2,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'swftools_admin_embed_form',
),
'file' => 'swftools.admin.inc',
);
$items['admin/reports/swftools'] = array(
'title' => 'SWF Tools status',
'description' => 'Get an overview of the status of the SWF Tools module and its supporting files.',
'page callback' => 'swftools_status',
'access arguments' => $swf_admin,
'file' => 'swftools.admin.status.inc',
'weight' => 9,
);
$items = array_merge($items, genericplayers_menu());
return $items;
}
function swftools_perm() {
return array(
'administer flash',
);
}
function swf_list($playlist_data, $options = array()) {
$params = $options['params'];
$flashvars = $options['flashvars'];
$othervars = $options['othervars'];
$methods = $options['methods'];
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 = '';
}
$options['methods'] = $methods;
$options['othervars'] = $othervars;
return swf($playlist, $options);
}
else {
}
}
function swf($file, $options = array()) {
$options += array(
'params' => SWFDEFAULT,
'flashvars' => SWFDEFAULT,
'othervars' => SWFDEFAULT,
'methods' => SWFDEFAULT,
);
$params = $options['params'];
$flashvars = $options['flashvars'];
$othervars = $options['othervars'];
$methods = $options['methods'];
$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', SWFTOOLS_DEFAULT_HTML_ALT);
$resolved_methods = new stdClass();
$player = isset($methods['player']) ? $methods['player'] : FALSE;
if (!$player) {
$player = swftools_get_player($action);
if (!$player) {
$descriptions = array(
SWFTOOLS_IMAGE_DISPLAY_LIST => 'a series of images',
SWFTOOLS_FLV_DISPLAY => 'a single flv file',
SWFTOOLS_FLV_DISPLAY_LIST => 'a series of flv files',
SWFTOOLS_MP3_DISPLAY => 'a single mp3 file',
SWFTOOLS_MP3_DISPLAY_LIST => 'a series of mp3 files',
SWFTOOLS_MEDIA_DISPLAY_LIST => 'a series mixed media files',
);
if (isset($descriptions[$action])) {
drupal_set_message(t('No player is configured to play ' . $descriptions[$action] . '. Check the SWF Tools file handling settings on the configuration page.'), 'error');
}
else {
drupal_set_message(t('No player is configured for the action %action. Check the SWF Tools file handling settings on the configuration page.', array(
'%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(t('Could not find the %player file for embedding.', array(
'%player' => $player,
)), 'error', FALSE);
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 = !empty($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(t('Unable to create playlist.'), 'error');
return $html_alt;
}
}
$nocache = '';
if (variable_get('swftools_playlist_caching', 'here') == 'always') {
$nocache = '?nc=' . time();
}
if (!valid_url($file, TRUE)) {
if (empty($playlist)) {
if (swftools_get_media_path()) {
$file = file_create_path($file);
}
}
$file_url = swftools_get_media_url($file);
if (!$file_url) {
return $html_alt;
}
$file_url = $file_url;
}
else {
$file_url = $file;
}
$vars->othervars['file_url'] = $file_url;
switch ($player) {
case SWFTOOLS_SWF:
$vars->params['src_path'] = $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 (empty($vars->flashvars['width']) && empty($vars->flashvars['height'])) {
if (!empty($vars->params['width']) && !empty($vars->params['height'])) {
$vars->flashvars['width'] = $vars->params['width'];
$vars->flashvars['height'] = $vars->params['height'];
}
}
if (empty($vars->params['width']) && empty($vars->params['height'])) {
if (!empty($vars->flashvars['width']) && !empty($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);
$preview = NULL;
return theme('swftools_embed', $output, $action, $resolved_methods, $vars, $preview);
}
function theme_swftools_embed($embed_code, $action, $methods, $vars, $html_alt) {
$id = !empty($vars->othervars['id']) ? ' id="swf-' . $vars->othervars['id'] . '"' : '';
$classes[] = 'swftools-wrapper';
$classes[] = str_replace('_', '-', $methods->player['name']);
if (!empty($vars->othervars['class'])) {
$classes[] = $vars->othervars['class'];
}
return '<div' . $id . ' class="' . implode(' ', $classes) . '">' . $embed_code . '</div>';
}
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('Direct embedding - do not 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) {
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(t('%dir does not exist, or is not writeable.', array(
'%dir' => $dir,
)), 'error', FALSE);
}
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 = trim(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(t('Could not display the flash because %path does not appear to exist.', array(
'%path' => $path,
)), '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() {
$defaults = array(
'swliveconnect' => variable_get('swftools_params_swliveconnect', 'default'),
'play' => variable_get('swftools_params_play', TRUE),
'loop' => variable_get('swftools_params_loop', TRUE),
'menu' => variable_get('swftools_params_menu', FALSE),
'quality' => variable_get('swftools_params_quality', 'autohigh'),
'scale' => variable_get('swftools_params_scale', 'showall'),
'align' => variable_get('swftools_params_align', 'l'),
'salign' => variable_get('swftools_params_salign', 'tl'),
'wmode' => variable_get('swftools_params_wmode', 'opaque'),
'bgcolor' => variable_get('swftools_params_bgcolor', '#FFFFFF'),
'version' => variable_get('swftools_params_version', '7'),
'allowfullscreen' => variable_get('swftools_params_allowfullscreen', TRUE),
);
$defaults['menu'] = _swftools_tf($defaults['menu']);
$defaults['play'] = _swftools_tf($defaults['play']);
$defaults['loop'] = _swftools_tf($defaults['loop']);
$defaults['allowfullscreen'] = _swftools_tf($defaults['allowfullscreen']);
return $defaults;
}
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(t('@title module does not support xml playlists.', array(
'@title' => $method->player['title'],
)), 'error');
}
if (!($handle = fopen($playlist_name, 'a'))) {
drupal_set_message(t('An error occurred trying to create file %playlist_name.', array(
'%playlist_name' => $playlist_name,
)), 'error');
return FALSE;
}
if (fwrite($handle, $playlist) === FALSE) {
drupal_set_message(t('An error occurred trying to create file %playlist_name.', array(
'%playlist_name' => $playlist_name,
)), 'error');
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="swftools_filter">SWF Tools Filter</h3>
<p>The basic syntax for embedding a flash file (.swf), flash movie (.flv) or audio file (.mp3) is:</p>
<blockquote><code><swf file="filename.swf"></code></blockquote>
<p>If you would like to override SWF Tools and flash player default settings,
you can specify additional parameters. For example:</p>
<blockquote><code><swf file="song.mp3" flashvars="backcolor=#AABBCC&&forecolor=#11AA11"></code></blockquote>
<p>If you would like to output a list of files then the format is:</p>
<blockquote><code><swf files="image1.jpg&&image2.jpg&&..."></code></blockquote>
SWF Tools Filter will accept following:
<ul>
<li><b>params</b> : You can specify values for parameters to be passed to Flash
to control the appearance of the output. Typical values are
bgcolor and wmode. Example: <code>params="wmode=true&&bgcolor="#00FF00"</code>
Alternatively you can supply each parameter individually without using
<code>params</code>. Example <code>wmode="true" bgcolor="#00FF00"</code></li>
<li><b>flashvars</b> : You can specify values for output as flashvars, which
become available to the Flash movie that is playing. This is often done
to control a media 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>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 default specified on 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 !swftools_filter_help to display Flash files inline', array(
"!swftools_filter_help" => l('<swf file="song.mp3">', "filter/tips/{$format}", array(
'query' => 'swftools_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 <swf 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);
}
}
function _swftools_filter_process_text($text) {
$endl = chr(13);
if (preg_match_all('@(?:<p>)?\\[(swflist|swf) (.*?)\\](?:</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('/\\s+?(\\w*)=[\\"](.*?)[\\"]#?/', $passed_parameters, $match_vars[$key]);
foreach ($match_vars[$key][1] as $vars_key => $vars_name) {
if ($vars_name == 'file') {
$match[1][$key] = 'swf';
}
else {
if ($vars_name == 'files') {
$match[1][$key] = 'swflist';
}
}
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]);
}
else {
$prepared[$key]['othervars'][$vars_name] = $match_vars[$key][2][$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;
}
}
}
$options = array();
$options['params'] = $prepared[$key]['params'];
$options['flashvars'] = $prepared[$key]['flashvars'];
$options['othervars'] = $prepared[$key]['othervars'];
$options['methods'] = $prepared[$key]['methods'];
switch ($match[1][$key]) {
case 'swf':
$replace = swf($prepared[$key]['file'], $options);
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'], '', !$player);
$replace = swf_list($playlist_data, $options);
}
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',
'swliveconnect' => 'params',
'play' => 'params',
'loop' => 'params',
'menu' => 'params',
'quality' => 'params',
'scale' => 'params',
'align' => 'params',
'salign' => 'params',
'wmode' => 'params',
'bgcolor' => 'params',
'base' => 'params',
'version' => 'params',
'allowfullscreen' => '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_theme() {
return array(
'swftools_embed' => array(
'arguments' => array(
'embed_code' => NULL,
'action' => NULL,
'methods' => NULL,
'vars' => array(),
'html_alt' => NULL,
),
),
);
}
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);
$extensions = variable_get('swftools_grant_access_extensions', SWFTOOLS_GRANT_ACCESS_EXTENSIONS);
global $user;
if ($user->uid != 1) {
$regex = '/\\.(' . ereg_replace(' +', '|', preg_quote($extensions)) . ')$/i';
if (!preg_match($regex, $file)) {
return;
}
}
$mime_types = array(
'swf' => 'application/x-shockwave-flash',
'flv' => 'application/octet-stream',
'xml' => 'text/xml',
'mp3' => 'audio/mpeg',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/gif',
);
if ($mime_types[$extension]) {
return array(
'Content-Type: ' . $mime_types[$extension],
'Content-Length: ' . filesize(file_create_path($file)),
);
}
}
}
function swftools_swftools_embed($action = 'add_js', $methods = FALSE, $vars = FALSE, $html_alt = '') {
if ($action == 'add_js') {
return;
}
$wijering_fix = '';
static $unique_id = 0;
$unique_id++;
$id = time() . $unique_id;
$wijering_fix = ' id="swf' . $id . '"';
$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']);
$flashvars = str_replace('&', '&', $P['flashvars']);
$id = !empty($vars->othervars['id']) ? ' id="' . $vars->othervars['id'] . '"' : '';
$name = !empty($vars->othervars['id']) ? ' name="' . $vars->othervars['id'] . '"' : '';
$swliveconnect = $P['swliveconnect'] ? ' swliveconnect="' . $P['swliveconnect'] . '"' : '';
$params = $id;
$params .= '<param name="allowScriptAccess" value="sameDomain" />' . "\n";
$params .= '<param name="wmode" value="' . $P['wmode'] . '" />' . "\n";
$params .= '<param name="bgcolor" value="' . $P['bgcolor'] . '" />' . "\n";
$params .= '<param name="scale" value="' . $P['scale'] . '" />' . "\n";
$params .= '<param name="quality" value="' . $P['quality'] . '" />' . "\n";
$params .= '<param name="align" value="' . $P['align'] . '" />' . "\n";
$params .= '<param name="allowfullscreen" value="' . $P['allowfullscreen'] . '" />' . "\n";
$params .= '<param name="base" value="' . $P['base'] . '" />' . "\n";
$params .= '<param name="play" value="' . $play . '" />' . "\n";
$params .= '<param name="menu" value="' . $menu . '" />' . "\n";
$params .= '<param name="loop" value="' . $loop . '" />' . "\n";
$params .= $flashvars ? '<param name="flashvars" value="' . $flashvars . '" />' . "\n" : '';
$html = '<div class="swftools">' . "\n";
$html .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' . $width_attr . $height_attr . $wijering_fix . '>' . "\n";
$html .= '<param name="movie" value="' . $P['src'] . '" />' . "\n";
$html .= $params;
$html .= '<!--[if gte IE 7]>-->' . "\n";
$html .= '<object type="application/x-shockwave-flash" data="' . $P['src'] . '"' . $width_attr . $height_attr . '>' . "\n";
$html .= $params;
$html .= '<!--<![endif]-->' . "\n";
$html .= $html_alt . "\n";
$html .= '<!--[if gte IE 7]>-->' . "\n";
$html .= '</object>' . "\n";
$html .= '<!--<![endif]-->' . "\n";
$html .= '</object>' . "\n";
$html .= '</div>' . "\n";
return $html;
}
function swftools_admin_form_submit($form, &$form_state) {
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
$saved = array();
foreach ($form_state['values'] as $player => $settings) {
if ($op == t('Reset to defaults')) {
variable_del('swftools_' . $player);
}
else {
$flat[$player] = array();
if (is_array($settings)) {
foreach ($settings as $category => $vars) {
$flat[$player] = array_merge($flat[$player], $vars);
}
variable_set('swftools_' . $player, $flat[$player]);
}
}
}
if ($op == t('Reset to defaults')) {
drupal_set_message(t('The configuration options have been reset to their default values.'));
}
else {
drupal_set_message(t('The configuration options have been saved.'));
}
drupal_flush_all_caches();
}