View source
<?php
function flashnode_help($section) {
switch ($section) {
case 'admin/settings/flashnode':
return t('Flash node lets you create nodes that store a piece of Flash animation. You can use it in a basic mode, where the Flash item is always displayed at the start of the node, or you can enable the flash filter to incorporate Flash content in to other nodes.');
case 'admin/help#flashnode':
return t('
<p>Flash node lets you create nodes that store a piece of <a href="!flashurl">Flash</a> animation. You can use it in a basic mode, where the Flash item is always displayed at the start of the node, but you can choose whether to have the Flash element displayed in both the teaser and the body, just the teaser, or just the body. You can define the height and width of the Flash element, or you can let the node use the Flash content\'s original settings.</p>
<p>The module also defines a new input filter called <strong>flash</strong> that you can use. This lets you re-use Flash content in other nodes by using the format <strong>[flash|nid=<nid>]</strong> in the body of a node. You can pass optional parameters to manipulate the display of the Flash content by including them in the macro. Allowable parameters are:
<ul><li>
<strong>width</strong> - set a specific width, in pixels
</li><li>
<strong>height</strong> - set a specific height, in pixels
</li><li>
<strong>scale</strong> - scale both width and height to a multiple of the original size
</li><li>
<strong>xscale</strong> - scale just the width to a multiple of the original size
</li><li>
<strong>yscale</strong> - scale just the height to a multiple of the original size
</li><li>
<strong>scalewidth</strong> - set a specific width, in pixels, and automatically adjust the height to maintain the aspect ratio
</li><li>
<strong>scaleheight</strong> - set a specific height, in pixels, and automatically adjust the width to maintain the aspect ratio
</li><li>
<strong>class</strong> - create the container div with a specific CSS class (the default class is <em>flash</em>)
</li></ul>
For example, to use Flash content from node 10, scaled to 50% of its original size, and with CSS class flash-left you would use <strong>[flash|nid=10|scale=0.5|class=flash-left]</strong></p>
<p>Flash content is inserted using the method that you specify in SWFTools.');
}
}
function flashnode_node_info() {
return array(
'flashnode' => array(
'name' => t('Flash'),
'module' => 'flashnode',
'description' => t('Allows you to easily upload and display a Flash file. You can choose whether the animation appears in the teaser, the body, or both.'),
),
);
}
function flashnode_perm() {
return array(
'create flash nodes',
'edit own flash nodes',
'administer flash node',
'use display options',
'use basic options',
'use advanced options',
'import flash',
);
}
function flashnode_access($op, $node) {
global $user;
if ($op == 'create' && user_access('create flash nodes')) {
return TRUE;
}
if ($op == 'update' || $op == 'delete') {
if (user_access('edit own flash nodes') && $user->uid == $node->uid) {
return TRUE;
}
}
}
function flashnode_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'node/add/flashnode',
'title' => t('Flash'),
'access' => user_access('create flash nodes'),
);
$items[] = array(
'path' => 'admin/settings/flashnode',
'title' => t('Flash node'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'flashnode_admin_settings',
),
'access' => user_access('administer flash node'),
'type' => MENU_NORMAL_ITEM,
'description' => t('Set the directory where flash files are uploaded to.'),
);
$items[] = array(
'path' => 'admin/content/flashnode',
'title' => t('Import Flash'),
'callback' => 'flashnode_import',
'access' => user_access('import flash'),
'description' => t('Import and create nodes from files that have been uploaded directly to the server.'),
);
}
return $items;
}
function flashnode_cron() {
$path = variable_get('flashnode_default_path', 'flash') . '/temp';
$files = file_scan_directory(file_create_path($path), '.*');
foreach ($files as $file => $info) {
if (time() - filemtime($file) > 60 * 60 * 6) {
file_delete($file);
}
}
}
function flashnode_prepare(&$node) {
$field_name = 'flashfile';
if ($file = file_check_upload($field_name)) {
$swf_path_fix = _flashnode_filename($file->filename, TRUE);
$file = file_save_upload($field_name, $swf_path_fix);
if ($file) {
$file_mime = strtolower($file->filemime);
$file_ext = strtolower(substr(strrchr($file->filename, '.'), 1));
if (strpos(trim(variable_get('flashnode_allowable_types', 'swf flv mp3')), $file_ext) === false) {
watchdog($field_name, t('Flash node was given %type for upload.', array(
'%type' => $file_ext,
)));
form_set_error($field_name, t('The specified file is not an allowed format.'));
file_delete($file->filepath);
return;
}
}
else {
return;
}
$node->flashnode['_flashnode'] = $swf_path_fix;
$node->flashnode['filemime'] = $file->filemime;
$node->new_file = TRUE;
if ($file_ext != 'swf') {
drupal_set_message(t('Remember you might have to set the movie size for flv or mp3 files as flash node cannot always automatically determine the player size!'), 'warning');
}
}
$info = image_get_info(file_create_path($node->flashnode['_flashnode']));
$node->flashnode['_height'] = $info['height'];
$node->flashnode['_width'] = $info['width'];
global $user;
if (!user_access('use basic options') && $user->uid != 1) {
unset($node->flashnode['height']);
unset($node->flashnode['width']);
}
}
function flashnode_validate(&$node, $form) {
if (empty($form['flashnode']['_flashnode']['#value'])) {
form_set_error('flashfile', t('You must specify a Flash file to upload.'));
}
if (!is_numeric($form['flashnode']['options']['height']['#value']) && !empty($form['flashnode']['options']['height']['#value'])) {
form_set_error('flashnode][height', t('You must enter a valid height.'));
}
if (!is_numeric($form['flashnode']['options']['width']['#value']) && !empty($form['flashnode']['options']['width']['#value'])) {
form_set_error('flashnode][width', t('You must enter a valid width.'));
}
}
function flashnode_form(&$node, &$param) {
global $user;
_flashnode_check_settings();
$form['#attributes'] = array(
"enctype" => "multipart/form-data",
);
$form['flashnode']['#tree'] = TRUE;
if ($node->new_file) {
$form['new_file'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
if ($node->new_file) {
$form['flashnode']['_flashnode'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['_flashnode'],
);
$form['flashnode']['filemime'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['filemime'],
);
$form['flashnode']['_height'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['_height'],
);
$form['flashnode']['_width'] = array(
'#type' => 'hidden',
'#value' => $node->flashnode['_width'],
);
}
else {
$form['flashnode']['_flashnode'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['_flashnode'],
);
$form['flashnode']['filemime'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['filemime'],
);
$form['flashnode']['_height'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['_height'],
);
$form['flashnode']['_width'] = array(
'#type' => 'hidden',
'#default_value' => $node->flashnode['_width'],
);
}
$type = node_get_types('type', $node);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#weight' => -5,
);
}
if ($type->has_body) {
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => check_plain($type->body_label),
'#default_value' => $node->body,
'#rows' => 20,
'#required' => $type->min_word_count > 0,
);
$form['body_filter']['format'] = filter_form($node->format);
}
$form['flashnode']['flashfile'] = array(
'#type' => 'file',
'#title' => t('Flash file'),
'#description' => t('Click "Browse..." to select a swf, flv or mp3 file to upload.'),
'#tree' => FALSE,
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options1'] = array(
'#type' => 'fieldset',
'#title' => t('Basic flash node options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
'#access' => user_access('use basic options') || user_access('use display options') || $user->uid == 1,
);
$form['flashnode']['options1']['display'] = array(
'#type' => 'radios',
'#title' => t('Display in'),
'#default_value' => isset($node->flashnode['display']) ? $node->flashnode['display'] : variable_get('flashnode_default_display', 0),
'#options' => array(
0 => t('Teaser and body'),
1 => t('Teaser only'),
2 => t('Body only'),
3 => t('Do not display'),
),
'#parents' => array(
'flashnode',
'display',
),
'#access' => user_access('use display options') || $user->uid == 1,
);
$form['flashnode']['options1']['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => $node->flashnode['width'],
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The width of the movie, in pixels. Leave blank to use the file\'s own settings.'),
'#parents' => array(
'flashnode',
'width',
),
'#access' => user_access('use basic options') || $user->uid == 1,
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options1']['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => $node->flashnode['height'],
'#size' => 5,
'#maxlength' => 5,
'#description' => t('The height of the movie, in pixels. Leave blank to use the file\'s own settings.'),
'#parents' => array(
'flashnode',
'height',
),
'#access' => user_access('use basic options') || $user->uid == 1,
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options2'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced flash node options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#tree' => FALSE,
'#access' => user_access('use advanced options') || $user->uid == 1,
);
$form['flashnode']['options2']['substitution'] = array(
'#type' => 'textarea',
'#title' => t('Substitution content'),
'#rows' => 5,
'#default_value' => $node->flashnode['substitution'],
'#parents' => array(
'flashnode',
'substitution',
),
'#description' => t('If a javascript method is used to embed flash then this is the content that users will see if they are unable to, or choose not to, display the flash content. This content uses the same input format as the body. The default content may be used by entering @default.', array(
'@default' => '!default',
)),
'#after_build' => array(
'_flashnode_form_after_build',
),
);
$form['flashnode']['options2']['flashvars'] = array(
'#type' => 'textarea',
'#title' => t('Flashvars'),
'#rows' => 5,
'#default_value' => $node->flashnode['flashvars'],
'#parents' => array(
'flashnode',
'flashvars',
),
'#description' => t('Specify any flashvars that need to be passed to the movie. If the input format allows PHP code you may use PHP to create a dynamic flashvars string.'),
);
$form['flashnode']['options2']['base'] = array(
'#type' => 'textfield',
'#title' => t('Base'),
'#default_value' => $node->flashnode['base'] ? $node->flashnode['base'] : variable_get('flashnode_default_base', base_path() . file_directory_path()),
'#parents' => array(
'flashnode',
'base',
),
'#description' => t('Over-ride the default setting with a different base path here if necessary, e.g. if migrating existing content. This setting is only needed for movies that use the %loadmovie command with relative paths.', array(
'%loadmovie' => 'loadMovie()',
)),
);
$form['flashnode']['#after_build'] = array(
'_flashnode_form_after_build',
);
return $form;
}
function flashnode_load(&$node) {
$result = db_query("SELECT filepath FROM {files} WHERE nid=%d AND filename='%s'", $node->nid, '_flashnode');
$node->flashnode['_flashnode'] = db_result($result);
$result = db_query("SELECT height, width, display, substitution, flashvars, base FROM {flashnode} WHERE nid=%d", $node->nid);
$settings = db_fetch_object($result);
foreach ($settings as $parameter => $value) {
$node->flashnode[$parameter] = $value;
}
}
function flashnode_insert($node) {
if (!$node->flashnode['import']) {
$fid = _flashnode_insert($node->nid, file_create_path($node->flashnode['_flashnode']), $node->flashnode['filemime'], $node->flashnode['import']);
}
if ($fid) {
db_query("INSERT INTO {flashnode} (nid, vid, height, width, display, substitution, flashvars, base, fid) VALUES (%d, %d, %d, %d, %d, '%s', '%s', '%s', %d)", $node->nid, $node->vid, $node->flashnode['height'], $node->flashnode['width'], $node->flashnode['display'], $node->flashnode['substitution'], $node->flashnode['flashvars'], $node->flashnode['base'], $fid);
}
}
function flashnode_update($node) {
$old_path = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $node->nid));
if ($old_path != $node->flashnode['_flashnode']) {
file_delete(file_create_path($old_path));
db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $node->nid);
db_query("DELETE FROM {flashnode} WHERE nid=%d", $node->nid);
flashnode_insert($node);
}
else {
db_query("UPDATE {flashnode} SET height=%d, width=%d, display=%d, substitution='%s', flashvars='%s', base='%s', vid=%d WHERE nid=%d", $node->flashnode['height'], $node->flashnode['width'], $node->flashnode['display'], $node->flashnode['substitution'], $node->flashnode['flashvars'], $node->flashnode['base'], $node->vid, $node->nid);
}
cache_clear_all('*', 'cache_filter', true);
}
function flashnode_delete($node) {
file_delete(file_create_path($node->flashnode['_flashnode']));
db_query("DELETE FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $node->nid);
db_query("DELETE FROM {flashnode} WHERE nid=%d", $node->nid);
}
function flashnode_view($node, $teaser, $page = 0) {
$node = node_prepare($node, $teaser);
$node->flashnode['substitution'] = check_markup($node->flashnode['substitution'], $node->format, FALSE);
$node->flashnode['flashvars'] = _flashnode_php_flashvars($node->flashnode['flashvars'], $node->format);
$flash = theme('flashnode', $node->flashnode, $teaser);
if ($node->flashnode['display'] != 2 && $node->flashnode['display'] != 3 && $teaser) {
$node->content['body']['#value'] = $flash . $node->content['body']['#value'];
}
if ($node->flashnode['display'] != 1 && $node->flashnode['display'] != 3 && !$teaser) {
$node->content['body']['#value'] = $flash . $node->content['body']['#value'];
}
return $node;
}
function flashnode_content($args = array(), $format = FILTER_FORMAT_DEFAULT) {
if (!is_numeric($args['nid'])) {
return;
}
$flash['_flashnode'] = db_result(db_query("SELECT filepath FROM {files} WHERE filename='%s' AND nid=%d", '_flashnode', $args['nid']));
if (!$flash['_flashnode']) {
return;
}
$result = db_query("SELECT height, width, display, substitution, flashvars, base FROM {flashnode} WHERE nid=%d", $args['nid']);
$result = db_fetch_object($result);
foreach ($result as $parameter => $value) {
$flash[$parameter] = $value;
}
$parent_format = db_result(db_query('SELECT r.format FROM {node_revisions} r INNER JOIN {node} n ON n.vid = r.vid WHERE n.nid=%d', $args['nid']));
$flash['flashvars'] = _flashnode_php_flashvars($flash['flashvars'], $parent_format);
unset($args['nid']);
$xscale = $yscale = 1;
$options = array();
if ($args) {
foreach ($args as $parameter => $value) {
switch ($parameter) {
case 'scalewidth':
if (is_numeric($value) && $flash['width'] > 0) {
$xscale = $yscale = $value / $flash['width'];
}
break;
case 'scaleheight':
if (is_numeric($value) && $flash['height'] > 0) {
$xscale = $yscale = $value / $flash['height'];
}
break;
case 'xscale':
if (is_numeric($value)) {
$xscale = $value;
}
break;
case 'yscale':
if (is_numeric($value)) {
$yscale = $value;
}
break;
case 'scale':
if (is_numeric($value)) {
$xscale = $yscale = $value;
}
break;
case 'height':
case 'width':
if (is_numeric($value)) {
$flash[$parameter] = $value;
}
break;
case 'class':
$flash['class'] = $value;
break;
case 'flashvars':
$value = str_replace('&', '&', $value);
$flash['flashvars'] = $value;
$flash['flashvars'] = _flashnode_php_flashvars($flash['flashvars'], $format);
break;
case 'substitution':
$flash['substitution'] = $value;
break;
default:
$options[$parameter] = $value;
}
}
}
$flash['height'] = $flash['height'] * $yscale;
$flash['width'] = $flash['width'] * $xscale;
$flash['substitution'] = check_markup($flash['substitution'], $format, FALSE);
return theme('flashnode', $flash, FALSE, $options);
}
function flashnode_get_macros($text) {
$m = array();
preg_match_all('/ \\[ ([^\\[\\]]+)* \\] /x', $text, $matches);
$tag_match = (array) array_unique($matches[1]);
foreach ($tag_match as $macro) {
$current_macro = '[' . $macro . ']';
$param = array_map('trim', explode('|', $macro));
$func_name = array_shift($param);
if ($func_name == 'flashnode') {
$vars = array();
foreach ($param as $p) {
$pos = strpos($p, '=');
$varname = substr($p, 0, $pos);
$varvalue = substr($p, $pos + 1);
$vars[$varname] = $varvalue;
}
$m[$current_macro] = $vars;
}
}
return $m;
}
function flashnode_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('Flash node filter'),
);
case 'description':
return t('Add Flash from a flash node to your posts using a flash node macro.');
case 'process':
foreach (flashnode_get_macros($text) as $unexpanded_macro => $macro) {
$expanded_macro = flashnode_content($macro, $format);
$text = str_replace($unexpanded_macro, $expanded_macro, $text);
}
return $text;
default:
return $text;
}
}
function flashnode_filter_tips($delta, $format, $long = false) {
return t('Flash node macros can be added to this post.');
}
function flashnode_admin_settings() {
_flashnode_check_settings();
cache_clear_all('*', 'cache_filter', true);
$form['flashnode_updated'] = array(
'#type' => 'hidden',
'#value' => time(),
);
$form['flashnode_default_path'] = array(
'#type' => 'textfield',
'#title' => t('Default flash path'),
'#default_value' => variable_get('flashnode_default_path', 'flash'),
'#description' => t('Subdirectory in the directory "%dir" where Flash files will be stored. Do not include a leading or trailing slash.', array(
'%dir' => variable_get('file_directory_path', 'files'),
)),
);
$form['flashnode_allowable_types'] = array(
'#type' => 'textfield',
'#title' => t('Allowable file types'),
'#default_value' => variable_get('flashnode_allowable_types', 'swf flv mp3'),
'#description' => t('You can limit the types of file that users are able to upload by entering a list of allowable file extensions separated by spaces. Do not include the leading dot.'),
);
$form['flashnode_default_display'] = array(
'#type' => 'radios',
'#title' => t('Default display setting'),
'#default_value' => variable_get('flashnode_default_display', 0),
'#description' => t('The default display setting that will be used when a new flash node is created.'),
'#options' => array(
0 => t('Teaser and body'),
1 => t('Teaser only'),
2 => t('Body only'),
3 => t('Do not display'),
),
);
$form['flashnode_default_html_alt'] = array(
'#type' => 'textarea',
'#rows' => 5,
'#title' => t('Default substitution content'),
'#default_value' => variable_get('flashnode_default_html_alt', 'You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialise correctly.'),
'#description' => t('If you are using a javascript method to embed flash then this is the content that users will see if they are unable to, or choose not to, display the flash content. Use this content in a node by entering %default in the substitution field when creating a flash node. Note that this content is NOT filtered when it is displayed in a node so you may use mark-up that would not otherwise be allowed.', array(
'%default' => '!default',
)),
);
$form['flashnode_default_base'] = array(
'#type' => 'textfield',
'#title' => t('Default base parameter'),
'#default_value' => variable_get('flashnode_default_base', base_path() . file_directory_path()),
'#description' => t('If you use the ActionScript command %loadmovie or similar then you may need to use this setting to tell the Flash player where to find supporting movies. The base path is used for any relative paths that occur in the movie file. If you upload files using Drupal\'s upload module then the default setting of %base should be used. Only change it if you are uploading files to a different directory using something like FTP. This setting can be over-ridden when a node is created.', array(
'%loadmovie' => 'loadMovie()',
'%base' => base_path() . file_directory_path(),
)),
);
$form['flashnode_max_width'] = array(
'#type' => 'textfield',
'#title' => t('Maximum displayed width'),
'#default_value' => variable_get('flashnode_max_width', 0),
'#description' => t('The maximum displayed width of a flash movie can be limited by entering a non-zero value here. If the movie width is greater than this width then the movie will be scaled down when it is displayed. A value of zero means that no scaling will occur. This setting can be useful to ensure that the page layout is not disrupted by a large flash movie.'),
);
$form['flashnode_max_height'] = array(
'#type' => 'textfield',
'#title' => t('Maximum displayed height'),
'#default_value' => variable_get('flashnode_max_height', 0),
'#description' => t('The maximum displayed height of a flash movie can be limited by entering a non-zero value here. If the movie height is greater than this height then the movie will be scaled down when it is displayed. A value of zero means that no scaling will occur. This setting can be useful to ensure that the page layout is not disrupted by a large flash movie.'),
);
return system_settings_form($form);
}
function flashnode_admin_settings_validate($form_id, $form_values) {
if (!is_numeric($form_values['flashnode_max_width']) || $form_values['flashnode_max_width'] < 0) {
form_set_error('flashnode_max_width', t('The maximum displayed width must be zero or a positive number.'));
}
if (!is_numeric($form_values['flashnode_max_height']) || $form_values['flashnode_max_height'] < 0) {
form_set_error('flashnode_max_height', t('The maximum displayed height must be zero or a positive number.'));
}
}
function _flashnode_check_settings() {
$flashnode_path = file_create_path(variable_get('flashnode_default_path', 'flash'));
$temp_path = $flashnode_path . '/temp';
file_check_directory($flashnode_path, FILE_CREATE_DIRECTORY, 'flashnode_default_path');
file_check_directory($temp_path, FILE_CREATE_DIRECTORY, 'flashnode_default_path');
}
function _flashnode_filename($filename, $temp = FALSE) {
$filename = str_replace(' ', '_', $filename);
$result = variable_get('flashnode_default_path', 'flash') . '/';
if ($temp) {
$result .= 'temp/';
}
$result .= $filename;
return $result;
}
function _flashnode_insert($nid, $flash, $mime) {
$dest = _flashnode_filename(basename($flash));
if (file_move($flash, $dest)) {
$file->filename = '_flashnode';
$file->filepath = _flashnode_filename(basename($flash));
$file->filemime = $mime;
$file->filesize = filesize(file_create_path($dest));
$fid = db_next_id('{files}_fid');
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $fid, $nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
return $fid;
}
}
function _flashnode_form_after_build($form, $form_values) {
if (!empty($form_values['flashnode']['_flashnode']) && $form['#parents'][0] == 'flashfile') {
$form['#description'] = t('Current file is %filename. Click "Browse..." to upload a different file.', array(
'%filename' => basename($form_values['flashnode']['_flashnode']),
));
}
if (empty($form_values['flashnode']['width']) && $form['#parents'][1] == 'width') {
$form['#value'] = $form_values['flashnode']['_width'];
form_set_value($form, $form_values['flashnode']['_width']);
}
if (empty($form_values['flashnode']['height']) && $form['#parents'][1] == 'height') {
$form['#value'] = $form_values['flashnode']['_height'];
form_set_value($form, $form_values['flashnode']['_height']);
}
if (empty($form_values['flashnode']['_flashnode']) && $form['#parents'][1] == 'substitution' && empty($form_values['flashnode']['substitution'])) {
$form['#value'] = '!default';
form_set_value($form, '!default');
}
return $form;
}
function _flashnode_php_flashvars($flashvars, $format = -1) {
$filters = filter_list_format($format);
foreach ($filters as $filter) {
if ($filter->module == 'filter' && $filter->delta == 1) {
$flashvars = module_invoke($filter->module, 'filter', 'process', $filter->delta, $format, $flashvars);
}
}
$flashvars = check_plain($flashvars);
$flashvars = str_replace('&', '&', $flashvars);
return $flashvars;
}
function theme_flashnode($flashnode, $teaser = FALSE, $options = array()) {
$max_width = variable_get('flashnode_max_width', 0);
$max_height = variable_get('flashnode_max_height', 0);
if ($max_width) {
if ($flashnode['width'] > $max_width) {
$scale = $max_width / $flashnode['width'];
$flashnode['width'] = $flashnode['width'] * $scale;
$flashnode['height'] = $flashnode['height'] * $scale;
}
}
if ($max_height) {
if ($flashnode['height'] > $max_height) {
$scale = $max_height / $flashnode['height'];
$flashnode['width'] = $flashnode['width'] * $scale;
$flashnode['height'] = $flashnode['height'] * $scale;
}
}
$output .= theme('flashnode_markup', $flashnode, $options);
return $output;
}
function theme_flashnode_markup($flashnode, $options = array()) {
if (function_exists('swf')) {
$params = array(
'width' => round($flashnode['width']),
'height' => round($flashnode['height']),
'base' => $flashnode['base'],
);
$preview = t($flashnode['substitution'], array(
'!default' => variable_get('flashnode_default_html_alt', 'You are missing some Flash content that should appear here! Perhaps your browser cannot display it, or maybe it did not initialise correctly.'),
));
$options = array_merge($options, array(
'html_alt' => $preview,
));
$file = $flashnode['_flashnode'];
if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
$file = str_replace(' ', '%20', $file);
}
$output .= swf(file_create_url($file), $params, $flashnode['flashvars'], $options, SWFDEFAULT, 0);
return $output;
}
static $markup_error_shown = false;
if (preg_match('@flv|mp3$@i', $flashnode['_flashnode'])) {
if (!$markup_error_shown) {
drupal_set_message(t('Flash node needs !swftools in order to play mp3 or flv files.', array(
'!swftools' => l('SWF Tools', 'http://drupal.org/project/swftools'),
)), 'warning');
$markup_error_shown = true;
}
return;
}
$output = t('<div class="flashnode"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="!width" height="!height" id="myMovieName"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="true" /><param name="movie" value="!filepath" /><param name="quality" value="high" /><param name="flashvars" value="!flashvars" /><param name="base" value="!base" /><embed src="!filepath" allowScriptAccess="sameDomain" allowFullScreen="true" quality="high" width="!width" height="!height" flashvars="!flashvars" name="myMovieName" align="" type="application/x-shockwave-flash" base="!base" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object></div>', array(
'!height' => $flashnode['height'],
'!width' => $flashnode['width'],
'!filepath' => file_create_url($flashnode['_flashnode']),
'!flashvars' => $flashnode['flashvars'],
'!base' => $flashnode['base'],
));
return $output;
}
function flashnode_import() {
if ($_POST['op'] == t('Import') && $_POST['files']) {
return drupal_get_form('flashnode_import_confirm');
}
$output .= drupal_get_form('flashnode_import_form');
return $output;
}
function flashnode_import_form() {
$form['help'] = array(
'#value' => t('
<p>This feature can be used to import files directly and create Flash nodes from them. This can be useful for importing batches of files that have been uploaded to the server, or to import files that are too large to be uploaded via the node creation form. Note that files that are imported do not respect file size limitations that would apply to files uploaded via the node form. Nodes that are created by this import function are set to be unpublished.</p>
<p>The import function will scan the %directory directory and sub-directories to locate files for import.</p>', array(
'%directory' => base_path() . file_create_path(variable_get('flashnode_default_path', 'flash')),
)),
);
$filesnotindb = _flashnode_filesnotindb();
if ($filesnotindb) {
$form['count'] = array(
'#value' => format_plural(count($filesnotindb), '1 file found.', '@count files found.') . t(' Select the file(s) you want to import, then click \'Import\'.'),
);
}
else {
$form['count'] = array(
'#value' => t('No files were found for import.'),
);
}
$files = array();
foreach ($filesnotindb as $file) {
$files[$file] = '';
$form['file'][$file] = array(
'#value' => l(str_replace(file_create_path(variable_get('flashnode_default_path', 'flash')) . '/', '', $file), $GLOBALS['base_url'] . '/' . $file),
);
}
$form['files'] = array(
'#type' => 'checkboxes',
'#options' => $files,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Import'),
);
return $form;
}
function _flashnode_filesnotindb() {
$filesnotindb = array();
$result = db_query('SELECT filepath FROM {files} ORDER BY filepath ASC');
$filesindb = array();
while ($file = db_fetch_object($result)) {
$filesindb[] = file_create_path($file->filepath);
}
$filesonserver = _flashnode_directorytoarray(realpath(file_create_path(variable_get('flashnode_default_path', 'flash'))), TRUE);
asort($filesonserver);
$root = realpath('.');
foreach ($filesonserver as $file) {
$file = preg_replace('@' . preg_quote($root) . '.@', '', $file);
$file = str_replace("\\", "/", $file);
if (!file_check_directory($file)) {
if (!in_array($file, $filesindb)) {
$filesnotindb[] = $file;
}
}
}
return $filesnotindb;
}
function _flashnode_directorytoarray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory . "/" . $file) && $file != 'temp') {
if ($recursive) {
$array_items = array_merge($array_items, _flashnode_directorytoarray($directory . "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\\/\\//si", "/", $file);
}
else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\\/\\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
function theme_flashnode_import_form($form) {
$output .= drupal_render($form['help']);
$output .= drupal_render($form['count']);
if (isset($form['file']) && is_array($form['file'])) {
$header = array(
theme('table_select_header_cell'),
t('File'),
);
foreach (element_children($form['file']) as $key) {
$row = array();
$row[] = drupal_render($form['files'][$key]);
$row[] = drupal_render($form['file'][$key]);
$rows[] = $row;
}
$output .= theme('table', $header, $rows);
$output .= drupal_render($form['submit']);
}
return $output;
}
function flashnode_import_confirm() {
$edit = $_POST;
$form['files'] = array(
'#prefix' => '<ul>',
'#suffix' => '</ul>',
'#tree' => TRUE,
);
foreach (array_filter($edit['files']) as $file) {
$form['files'][$file] = array(
'#type' => 'hidden',
'#value' => $file,
'#prefix' => '<li>',
'#suffix' => check_plain(str_replace(file_create_path(variable_get('flashnode_default_path', 'flash')) . '/', '', $file)) . "</li>\n",
);
}
$form['operation'] = array(
'#type' => 'hidden',
'#value' => 'import',
);
return confirm_form($form, t('Are you sure you want to import these items?'), 'admin/content/flashnode', '', t('Import'), t('Cancel'));
}
function flashnode_import_confirm_submit($form_id, $form_values) {
if ($form_values['confirm']) {
foreach ($form_values['files'] as $file) {
flashnode_import_file($file);
}
}
return 'admin/content/node';
}
function flashnode_import_file($file_to_import) {
global $user;
$node = new stdClass();
$file = new stdClass();
$node->title = check_plain(basename($file_to_import));
$node->uid = $user->uid;
$node->type = 'flashnode';
$node->status = 0;
$info = image_get_info(realpath($file_to_import));
$node->flashnode['height'] = $info['height'];
$node->flashnode['width'] = $info['width'];
$file->filemime = $info['mime_type'];
$node->flashnode['display'] = variable_get('flashnode_default_display', 0);
$node->flashnode['substitution'] = '!default';
$node->flashnode['base'] = variable_get('flashnode_default_base', base_path() . file_directory_path());
$node->flashnode['import'] = TRUE;
$file->filename = basename($file_to_import);
$file->filepath = str_replace(file_create_path() . '/', '', $file_to_import);
$file->filesize = filesize(realpath($file_to_import));
if (!$file->filemime) {
if (preg_match('@swf|flv|mp3$@i', $file->filename, $matches)) {
switch (strtolower($matches[0])) {
case 'mp3':
$file->filemime = 'audio/mpeg';
break;
case 'flv':
default:
$file->filemime = 'application/octet-stream';
}
}
}
node_save($node);
$fid = db_next_id('{files}_fid');
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')", $fid, $node->nid, '_flashnode', $file->filepath, $file->filemime, $file->filesize);
db_query("INSERT INTO {flashnode} (nid, vid, height, width, display, substitution, flashvars, base, fid) VALUES (%d, %d, %d, %d, %d, '%s', '%s', '%s', %d)", $node->nid, $node->vid, $node->flashnode['height'], $node->flashnode['width'], $node->flashnode['display'], $node->flashnode['substitution'], $node->flashnode['flashvars'], $node->flashnode['base'], $fid);
$file_to_import = str_replace(file_create_path(variable_get('flashnode_default_path', 'flash')) . '/', '', $file_to_import);
if ($node->nid) {
drupal_set_message('Imported ' . $file_to_import);
watchdog('content', t('@type: imported %title.', array(
'@type' => t($node->type),
'%title' => $node->title,
)), WATCHDOG_NOTICE, l(t('view'), "node/{$node->nid}"));
}
else {
drupal_set_message('Failed to import ' . $file_to_import, 'warning');
}
return;
}
function flashnode_file_download($file) {
$result = db_query("SELECT fi.*, fl.nid FROM {files} fi INNER JOIN {flashnode} fl ON fi.fid = fl.fid WHERE filepath = '%s'", $file);
if ($file = db_fetch_object($result)) {
$node = node_load($file->nid);
if (node_access('view', $node)) {
return array(
'Content-Type: ' . $file->filemime,
'Content-Length: ' . $file->filesize,
);
}
else {
return -1;
}
}
}
if (module_exists('views')) {
include drupal_get_path('module', 'flashnode') . '/flashnode.views.inc';
}