Parent Directory
|
Revision Log
|
Revision Graph
Enhanced support for CCK filefields - multiple filefields will can be converted to a playlist using the 'SWF Tools - playlist' formatter.
| 1 | <?php |
| 2 | // $Id: swftools.module,v 1.36 2009/03/18 23:27:43 stuartgreenfield Exp $ |
| 3 | |
| 4 | // Include the generic player module for basic mp3 and flv support |
| 5 | include_once(drupal_get_path('module', 'swftools') .'/genericplayers.module'); |
| 6 | |
| 7 | // The following list defines all the actions that SWF Tools understands |
| 8 | // Component modules report to SWF Tools what actions they support via hook_swftools_methods() |
| 9 | define('SWFTOOLS_EMBED', 'swftools_embed'); // Provides an embedding method |
| 10 | define('SWFTOOLS_IMAGE_DISPLAY_LIST', 'swftools_image_display_list'); // Can display list of images |
| 11 | define('SWFTOOLS_FLV_DISPLAY', 'swftools_flv_display'); // Can display .flv format file |
| 12 | define('SWFTOOLS_FLV_DISPLAY_LIST', 'swftools_flv_display_list'); // Can display list of .flv format files |
| 13 | define('SWFTOOLS_MP3_DISPLAY', 'swftools_mp3_display'); // Can display .mp3 format file |
| 14 | define('SWFTOOLS_MP3_DISPLAY_LIST', 'swftools_mp3_display_list'); // Can display list of .mp3 format files |
| 15 | define('SWFTOOLS_MEDIA_DISPLAY', 'swftools_media_display'); // Can display an unspecified file format |
| 16 | define('SWFTOOLS_MEDIA_DISPLAY_LIST', 'swftools_media_display_list'); // Can display list of mixed files |
| 17 | define('SWFTOOLS_SWF_DISPLAY_DIRECT', 'swftools_swf_display_direct'); // Can display .swf format file, not inside any player |
| 18 | define('SWFTOOLS_SWF_DISPLAY', 'swftools_swf_display'); // Can display .swf format inside a player |
| 19 | |
| 20 | // These are for user convenience, most common parameters for casual users using the api. |
| 21 | define('SWFDEFAULT', FALSE); |
| 22 | define('SWFTOOLS_SWF', 'swftools_swf'); // This is a 'player' method implemented by swftools.module itself. |
| 23 | define('SWFTOOLS_CUSTOM', 'swftools_custom'); // An unknown media/file player. |
| 24 | define('SWFTOOLS_NOJAVASCRIPT', 'swftools_nojavascript'); // This is an 'embed' method implemented by swftools.module itself. |
| 25 | //define('SWFTOOLS_DEFAULT_BG', $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'swftools') .'/shared/swftools-default.jpg'); // A generic image for use in certain contexts. |
| 26 | define('SWFTOOLS_DEFAULT_BG', url(drupal_get_path('module', 'swftools') . '/shared/swftools-default.jpg', array('absolute' => TRUE))); // A generic image for use in certain contexts. |
| 27 | |
| 28 | // Let other modules know SWF Tools is available |
| 29 | define('SWFTOOLS_INSTALLED', TRUE); |
| 30 | |
| 31 | // Configure some other defaults |
| 32 | 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 initialize correctly.</p>'); |
| 33 | define('SWFTOOLS_PLAYLIST_PATH', 'playlists'); |
| 34 | define('SWFTOOLS_PLAYER_PATH', ''); |
| 35 | define('SWFTOOLS_GRANT_ACCESS_TO_PRIVATE_FILES', FALSE); |
| 36 | define('SWFTOOLS_GRANT_ACCESS_EXTENSIONS', 'swf flv xml mp3 jpg jpeg png'); |
| 37 | define('SWFTOOLS_ALWAYS_ADD_JS', TRUE); |
| 38 | |
| 39 | /** |
| 40 | * Implementation of hook_init(), used to force embedding JavaScript on to all pages |
| 41 | */ |
| 42 | function swftools_init() { |
| 43 | if (variable_get('swftools_always_add_js', SWFTOOLS_ALWAYS_ADD_JS)) { |
| 44 | swftools_push_js(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Implementation of hook_menu(). |
| 50 | */ |
| 51 | function swftools_menu() { |
| 52 | |
| 53 | // Initialise array |
| 54 | $items = array(); |
| 55 | |
| 56 | // Should this be administer swf tools? |
| 57 | $swf_admin = array('administer flash'); |
| 58 | |
| 59 | $items['admin/settings/swftools'] = array( |
| 60 | 'title' => 'SWF Tools', |
| 61 | 'description' => 'Settings to control how SWF Tools integrates with Adobe Flash related methods and tools like video players, MP3 players and image viewers.', |
| 62 | 'access arguments' => $swf_admin, |
| 63 | 'page callback' => 'system_admin_menu_block_page', |
| 64 | 'file' => 'system.admin.inc', |
| 65 | 'file path' => drupal_get_path('module', 'system'), |
| 66 | ); |
| 67 | |
| 68 | $items['admin/settings/swftools/handling'] = array( |
| 69 | 'title' => 'File handling', |
| 70 | 'description' => 'Configure how SWF Tools should handle different types of file.', |
| 71 | 'access arguments' => $swf_admin, |
| 72 | 'weight' => -1, |
| 73 | 'page callback' => 'drupal_get_form', |
| 74 | 'page arguments' => array('swftools_admin_handling_form'), |
| 75 | 'file' => 'swftools.admin.inc', |
| 76 | ); |
| 77 | |
| 78 | $items['admin/settings/swftools/embed'] = array( |
| 79 | 'title' => 'Embedding settings', |
| 80 | 'description' => 'Set the embedding method that SWF Tools should use, and configure embedding defaults.', |
| 81 | 'access arguments' => $swf_admin, |
| 82 | 'weight' => -2, |
| 83 | 'page callback' => 'drupal_get_form', |
| 84 | 'page arguments' => array('swftools_admin_embed_form'), |
| 85 | 'file' => 'swftools.admin.inc', |
| 86 | ); |
| 87 | |
| 88 | $items['admin/reports/swftools'] = array( |
| 89 | 'title' => 'SWF Tools status', |
| 90 | 'description' => 'Get an overview of the status of the SWF Tools module and its supporting files.', |
| 91 | 'page callback' => 'swftools_status', |
| 92 | 'access arguments' => $swf_admin, |
| 93 | 'file' => 'swftools.admin.status.inc', |
| 94 | 'weight' => 9, |
| 95 | ); |
| 96 | |
| 97 | $items = array_merge($items, genericplayers_menu()); |
| 98 | |
| 99 | return $items; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Implementation of hook_perm(). |
| 105 | */ |
| 106 | function swftools_perm() { |
| 107 | return array( |
| 108 | 'administer flash', |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * Take an array of play list data and options, and return a markup string. |
| 115 | * |
| 116 | * @param $playlist_data |
| 117 | * A formatted array of data to be used to create the playlist. An appropriate |
| 118 | * array can be created from an array of filenames by calling swftools_prepare_playlist_data. |
| 119 | * @param $options |
| 120 | * An array of options to pass to the call to swf(). |
| 121 | * @return |
| 122 | * A string of markup to produce the playlist in a flash media player. |
| 123 | */ |
| 124 | function swf_list($playlist_data, $options = array()) { |
| 125 | |
| 126 | // Populate methods and othervars with playlist data |
| 127 | if (is_array($playlist_data)) { |
| 128 | |
| 129 | // If action isn't set then set it |
| 130 | if (empty($options['methods']['action']) && isset($playlist_data['action'])) { |
| 131 | $options['methods']['action'] = $playlist_data['action']; |
| 132 | } |
| 133 | |
| 134 | // If playlist_data isn't set then set it |
| 135 | if (empty($options['othervars']['playlist_data'])) { |
| 136 | $options['othervars']['playlist_data'] = $playlist_data; |
| 137 | } |
| 138 | |
| 139 | // If playlist filename is set |
| 140 | if (isset($playlist_data['filename'])) { |
| 141 | $playlist = $playlist_data['filename']; |
| 142 | } |
| 143 | else { |
| 144 | $playlist = ''; |
| 145 | } |
| 146 | |
| 147 | // Produce markup |
| 148 | return swf($playlist, $options); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Return output, which might be embed markup, or pre-flash markup, that |
| 154 | * includes the appropriate JavaScript added to the head of the page. |
| 155 | * |
| 156 | * @param $file |
| 157 | * The file to be played. If it is a SWF file it will usually be embedded directly. |
| 158 | * Use a full URL, a path relative to webroot, or a path relative to the configured files directory. |
| 159 | * If an array is passed then the array will be converted to a playlist automatically. |
| 160 | * If the file string is a complete url then SWF Tools will pass it along unaltered. If the string |
| 161 | * is a partial path then it will either be resolved to the local file system, or to a remote host, |
| 162 | * depending whether the swftools_media_url variable is set. |
| 163 | * @param $options=>$params |
| 164 | * An associative array of <param> variables to set.eg. array('bgcolor'=>'FF00FF') |
| 165 | * To set height and width: array('width'=>'200', 'height'=>'120'). However, |
| 166 | * as a convenient alternative for the common requirement of just height and width |
| 167 | * you can also pass a text string like '200x100'. |
| 168 | * If you pass nothing, and the file to play is a .swf, swftools will try and |
| 169 | * establish a natural width and height from the actual .swf file that you've |
| 170 | * passed into $file. |
| 171 | * @param $options=>$flashvars |
| 172 | * An associative array of flashvar variables to set. eg. array('playlist'=>'files/my_playlist.xml') |
| 173 | * @param $options=>$othervars |
| 174 | * An associative array of variables that might be required by the $player or $embed technique. |
| 175 | * These values are not output as params or flashvars. |
| 176 | * @param $options=>$methods |
| 177 | * Explicitly declare an action, player or embedding method by passing an array of |
| 178 | * the form: array('action'=>'dosomething', 'player'=>'withsomething', 'embed'=>'withthisjavascript'). |
| 179 | * These usually default to the administration settings and also you will |
| 180 | * usually use a CONSTANT which will be documented further at a later stage. |
| 181 | */ |
| 182 | function swf($file, $options = array()) { |
| 183 | |
| 184 | // Initialise any $options array elements that weren't passed by the caller |
| 185 | $options += array( |
| 186 | 'params' => array(), |
| 187 | 'flashvars' => array(), |
| 188 | 'othervars' => array(), |
| 189 | 'methods' => array(), |
| 190 | ); |
| 191 | |
| 192 | // If swf() was called with an array of files, make a playlist and call swf_list() for processing |
| 193 | if (is_array($file)) { |
| 194 | |
| 195 | // Determine if this is streamed content |
| 196 | $stream = FALSE; |
| 197 | if (isset($options['othervars']['stream'])) { |
| 198 | $stream = TRUE; |
| 199 | } |
| 200 | |
| 201 | // Is a specific action being set? |
| 202 | $get_action = (isset($options['methods']['action'])) ? FALSE : TRUE; |
| 203 | |
| 204 | // Turn the array in to a playlist |
| 205 | $playlist_data = swftools_prepare_playlist_data($file, '', $get_action, array(), $stream); |
| 206 | |
| 207 | // Call swf_list to process the playlist and create the markup |
| 208 | return swf_list($playlist_data, $options); |
| 209 | } |
| 210 | |
| 211 | // Get all the actions, players and embedding options available to us |
| 212 | $methods = swftools_methods_available(); |
| 213 | |
| 214 | |
| 215 | // ACTION |
| 216 | // Work out what SWF Tools should do with this file |
| 217 | |
| 218 | // Was an explicit action set in $options['methods']['action'] |
| 219 | $action = (isset($options['methods']['action'])) ? $options['methods']['action'] : FALSE; |
| 220 | |
| 221 | // If an explicit action wasn't set then try to determine an appropriate one using the filename |
| 222 | if (!$action) { |
| 223 | $action = swftools_get_action($file); |
| 224 | } |
| 225 | |
| 226 | // HTML ALTERNATIVE |
| 227 | // If an explicit value wasn't set in $options['othervars']['html_alt'] use a default |
| 228 | $html_alt = (isset($options['othervars']['html_alt'])) ? $options['othervars']['html_alt'] : variable_get('swftools_html_alt', SWFTOOLS_DEFAULT_HTML_ALT); |
| 229 | |
| 230 | |
| 231 | // RESOLVE PLAYER AND EMBEDDING |
| 232 | // 'resolved' refers to the fact that these are the methods we now intend to use, not /all/ methods available. |
| 233 | $resolved_methods = new stdClass(); |
| 234 | |
| 235 | |
| 236 | // PLAYER |
| 237 | // Work out what player SWF Tools will need to use for this action |
| 238 | |
| 239 | // Was an explicit player set in $options['methods']['player'] |
| 240 | $player = (isset($options['methods']['player'])) ? $options['methods']['player'] : FALSE; |
| 241 | |
| 242 | // If an explicit player wasn't set then find out what player is configured for the current action |
| 243 | if (!$player) { |
| 244 | |
| 245 | // Find out what player is assigned to handle the current action |
| 246 | $player = swftools_get_player($action); |
| 247 | |
| 248 | // If still no player assignment then we don't know what to do with this action |
| 249 | if (!$player) { |
| 250 | |
| 251 | // Build an array of descriptions for each possible action |
| 252 | $descriptions = array( |
| 253 | SWFTOOLS_IMAGE_DISPLAY_LIST=> 'a series of images', |
| 254 | SWFTOOLS_FLV_DISPLAY=> 'a single flv file', |
| 255 | SWFTOOLS_FLV_DISPLAY_LIST=> 'a series of flv files', |
| 256 | SWFTOOLS_MP3_DISPLAY=> 'a single mp3 file', |
| 257 | SWFTOOLS_MP3_DISPLAY_LIST=> 'a series of mp3 files', |
| 258 | SWFTOOLS_MEDIA_DISPLAY_LIST=> 'a series mixed media files', |
| 259 | ); |
| 260 | |
| 261 | // If we have a matching description for the specified action, create a meaningful message |
| 262 | if (isset($descriptions[$action])) { |
| 263 | drupal_set_message(t('No player is configured to play ' . $descriptions[$action] . '. Check the SWF Tools file handling settings on the configuration page.'), 'error'); |
| 264 | } |
| 265 | |
| 266 | // Otherwise we have an action that SWF Tools doesn't understand, so create a fallback message |
| 267 | else { |
| 268 | 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'); |
| 269 | } |
| 270 | |
| 271 | // We couldn't find a player for this content, so fallback to the alternate markup and return |
| 272 | return $html_alt; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Check that the action / player combination is valid by checking for the [action][player] keys |
| 277 | if (isset($methods[$action][$player])) { |
| 278 | |
| 279 | // If the combination was found, place player information in to $resolved_methods |
| 280 | $resolved_methods->player = $methods[$action][$player]; |
| 281 | |
| 282 | } |
| 283 | |
| 284 | // If the action / player combination doesn't appear then we need to do something else |
| 285 | else { |
| 286 | |
| 287 | // If the action is display an swf directly then assume we have a custom player |
| 288 | if ($action == SWFTOOLS_SWF_DISPLAY_DIRECT) { |
| 289 | |
| 290 | // Assign SWFTOOLS_CUSTOM data in to $resolved_methods |
| 291 | $resolved_methods->player = $methods[$action][SWFTOOLS_CUSTOM]; |
| 292 | $resolved_methods->player['shared_file'] = $player; |
| 293 | } |
| 294 | |
| 295 | // This player and action combination is not valid |
| 296 | else { |
| 297 | drupal_set_message(t('%player cannot perform the action %action.', array('%player' => $player, '%action' => $action)), 'error', FALSE); |
| 298 | return $html_alt; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | |
| 303 | // EMBED |
| 304 | // Work out what embedding method SWF Tools should use for this content |
| 305 | |
| 306 | // Was an explicit embedding method set in $options['methods']['embed'] |
| 307 | $embed = (isset($options['methods']['embed'])) ? $options['methods']['embed'] : FALSE; |
| 308 | |
| 309 | // If an explicit embedding method wasn't set then find get the current default |
| 310 | if (!$embed) { |
| 311 | $embed = variable_get('swftools_embed_method', SWFTOOLS_NOJAVASCRIPT); |
| 312 | } |
| 313 | |
| 314 | // Place the embedding method information in to $resolved_methods |
| 315 | $resolved_methods->embed = $methods[SWFTOOLS_EMBED][$embed]; |
| 316 | |
| 317 | |
| 318 | // VARIABLES AND PARAMETERS |
| 319 | // Put all the variables on a simple object to make internal function calls simpler |
| 320 | $vars = new stdClass(); |
| 321 | |
| 322 | |
| 323 | // OTHERVARS |
| 324 | // If $options['othervars'] were supplied, add to $vars->othervars |
| 325 | $vars->othervars = (is_array($options['othervars'])) ? $options['othervars'] : array(); |
| 326 | |
| 327 | |
| 328 | // PARAMS |
| 329 | // $options['params'] could be an associative array, or in 'WIDTHxHEIGHT' format. |
| 330 | |
| 331 | // If $options were passed to the swf() function then process them |
| 332 | if ($options['params']) { |
| 333 | |
| 334 | // If $options['params'] is an array then just add it to $vars |
| 335 | if (is_array($options['params'])) { |
| 336 | $vars->params = $options['params']; |
| 337 | } |
| 338 | |
| 339 | // Otherwise see if we can explode the string in to a height and width |
| 340 | else { |
| 341 | $dimensions = explode('x', $options['params']); |
| 342 | if (count($dimensions) == 2) { |
| 343 | $vars->params = array( |
| 344 | 'width' => $dimensions[0], |
| 345 | 'height' => $dimensions[1], |
| 346 | ); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
| 352 | // FLASHVARS |
| 353 | // Flashvars could be passed as an associative array, or as a string in 'a=1&b=2' format |
| 354 | |
| 355 | // If the flashvars have been passed as an array simply add to $varsa |
| 356 | if (is_array($options['flashvars'])) { |
| 357 | $vars->flashvars = $options['flashvars']; |
| 358 | } |
| 359 | |
| 360 | // Otherwise parse the flashvars string in to an array |
| 361 | else { |
| 362 | |
| 363 | // Parse the string as if in 'a=1&b=2' format. |
| 364 | parse_str($options['flashvars'], $vars->flashvars); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | // BASE |
| 369 | // Determine a reasonable 'base' directory, if a remote url is set, use that |
| 370 | // If file is local, set to the file directory |
| 371 | |
| 372 | // Was an explicit base path set in $options['params']['base'] |
| 373 | $base = (!empty($vars->params['base'])) ? $vars->params['base'] : ''; |
| 374 | |
| 375 | // If the base path isn't set, or the path is not valid try to find a reasonable alternative |
| 376 | if (!$base || !valid_url($base)) { |
| 377 | |
| 378 | // Use swftools_get_media_url() to obtain either the local path, or the remote media path |
| 379 | $base = swftools_get_media_url('', FALSE); |
| 380 | } |
| 381 | |
| 382 | // Strip $base_root if this is a local base path |
| 383 | $base = swftools_strip_base_root($base); |
| 384 | |
| 385 | // Assign the resulting base path in to $vars->params['base'] |
| 386 | $vars->params['base'] = $base; |
| 387 | |
| 388 | |
| 389 | // PLAYLIST |
| 390 | // Determine if we trying to generate a playlist |
| 391 | |
| 392 | // If $options['othervars']['playlist_data'] is set then we are processing a playlist |
| 393 | if (isset($options['othervars']['playlist_data'])) { |
| 394 | |
| 395 | // Flag that a playlist is being generated |
| 396 | $playlist = TRUE; |
| 397 | |
| 398 | // Generate a playlist in the files directory |
| 399 | $file = swftools_generate_playlist($options['othervars']['playlist_data'], '', $resolved_methods, $vars); |
| 400 | |
| 401 | // If a file wasn't generated by swftools_generate_playlist then set an error and return alternate markup |
| 402 | if (!$file) { |
| 403 | drupal_set_message(t('Unable to create playlist.'), 'error'); |
| 404 | return $html_alt; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | |
| 409 | // CACHING |
| 410 | // To try and prevent the xml files from being cached append the time to the filename to try and force it to reload |
| 411 | if (variable_get('swftools_playlist_caching', 'here') == 'always') { |
| 412 | $nocache = '?nc='. time(); |
| 413 | } |
| 414 | else { |
| 415 | $nocache = ''; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | // FILE |
| 420 | // Make sure that the file path is $file is valid - we skip this section if $file is already a full url |
| 421 | // Otherwise we try to expand it to a full url to the local file system or the remote media directory |
| 422 | |
| 423 | // If the file isn't a stream and isn't already a url, then turn it in to a url |
| 424 | if (!isset($vars->othervars['stream']) && !valid_url($file, TRUE)) { |
| 425 | |
| 426 | // If file is local, make sure it points to the file directory |
| 427 | if (swftools_get_media_path()) { |
| 428 | $file = file_create_path($file); |
| 429 | } |
| 430 | |
| 431 | // Process $file to resolve ensure it resolves to a full url |
| 432 | $file_url = swftools_create_url($file); |
| 433 | |
| 434 | // If $file_url was not generated then file doesn't exist so return $html_alt |
| 435 | if (!$file_url) { |
| 436 | return $html_alt; |
| 437 | } |
| 438 | |
| 439 | } |
| 440 | |
| 441 | // If this is a media stream simply set $file_url to $file |
| 442 | else { |
| 443 | $file_url = $file; |
| 444 | } |
| 445 | |
| 446 | // Attach file_url to othervars so player modules have access if required |
| 447 | $vars->othervars['file_url'] = $file_url; |
| 448 | |
| 449 | // SRC |
| 450 | // Determine the "src" attribute of the embed (also applies to the 'movie' attribute). |
| 451 | // Usually this is the Flash Player, but it can also be a swf file, or a custom file |
| 452 | // passed on othervars['shared_file']. |
| 453 | switch ($player) { |
| 454 | case SWFTOOLS_SWF: |
| 455 | $vars->params['src_path'] = $file; |
| 456 | $vars->params['src'] = $file_url; |
| 457 | break; |
| 458 | case SWFTOOLS_CUSTOM: |
| 459 | $vars->params['src_path'] = $resolved_methods->player['shared_file']; |
| 460 | $vars->params['src'] = swftools_create_url($vars->params['src_path']); |
| 461 | break; |
| 462 | default: |
| 463 | $vars->params['src_path'] = swftools_get_player_path() . '/' . $resolved_methods->player['shared_file']; |
| 464 | $vars->params['src'] = $GLOBALS['base_url'] . '/' . $vars->params['src_path']; |
| 465 | } |
| 466 | |
| 467 | // Try to strip $base_root if this is a local path |
| 468 | $vars->params['src'] = swftools_strip_base_root($vars->params['src']); |
| 469 | |
| 470 | // Merge default and user defined "params". |
| 471 | $vars->params = array_merge(_swftools_params(), $vars->params); |
| 472 | |
| 473 | // Ask the module implementing the player what flashvars are required, pass |
| 474 | // all existing values by reference to allow optional override at the player.module level. |
| 475 | if (module_hook($resolved_methods->player['module'], 'swftools_flashvars')) { |
| 476 | |
| 477 | // Get player flashvars |
| 478 | $player_flashvars = module_invoke($resolved_methods->player['module'], 'swftools_flashvars', $action, $resolved_methods, $vars); |
| 479 | |
| 480 | // Merge player flashvars with existing flashvars |
| 481 | if (is_array($player_flashvars)) { |
| 482 | $vars->flashvars = array_merge($vars->flashvars, $player_flashvars); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // If the player made a flashvar assignment for the playlist, add it to the flashvars |
| 487 | if (!empty($resolved_methods->player['file'])) { |
| 488 | $vars->flashvars[$resolved_methods->player['file']] = $vars->othervars['file_url']; |
| 489 | } |
| 490 | |
| 491 | // If the player requires a specific minimum flash version then assign it to the params |
| 492 | if (isset($resolved_methods->player['version'])) { |
| 493 | $vars->params['version'] = $resolved_methods->player['version']; |
| 494 | } |
| 495 | |
| 496 | // Call function to set the size of the content |
| 497 | swftools_set_size($vars, $resolved_methods->player); |
| 498 | |
| 499 | // Build a string out of the flashvars array. |
| 500 | $vars->params['flashvars'] = _swftools_get_flashvars_string($vars->flashvars); |
| 501 | |
| 502 | // Call the embedding code to get the HTML and set the JavaScript if necessary. |
| 503 | $embed_markup = module_invoke($resolved_methods->embed['module'], 'swftools_embed', $action, $resolved_methods, $vars, $html_alt); |
| 504 | |
| 505 | // Call theme function to return completed markup, e.g. add containing div |
| 506 | return theme('swftools_embed', $embed_markup, $action, $resolved_methods, $vars, $html_alt); |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /** |
| 511 | * Produce finished markup ready for inserting on the page |
| 512 | * |
| 513 | * @param $embed_markup |
| 514 | * The markup needed to add the swf content to the page |
| 515 | * @param $action |
| 516 | * The action that is being used, in case the themer wants it |
| 517 | * @param $methods |
| 518 | * The player and embedding methods being used, in case the themer wants it |
| 519 | * @param $vars |
| 520 | * The array of othervars, params and flashvars in case the themer wants it |
| 521 | * @param $html_alt |
| 522 | * The alternate HTML content, in case the themer wants it |
| 523 | * @return |
| 524 | * An HTML string that generates the output |
| 525 | */ |
| 526 | function theme_swftools_embed($embed_markup, $action, $methods, $vars, $html_alt) { |
| 527 | |
| 528 | // Generate a css id if an id was supplied in $vars->othervars |
| 529 | $id = (!empty($vars->othervars['id'])) ? ' id="swf-'. $vars->othervars['id'] .'"' : ''; |
| 530 | |
| 531 | // Prepare an array of classes to include in the wrapper div |
| 532 | $classes[] = 'swftools-wrapper'; |
| 533 | $classes[] = str_replace('_', '-', $methods->player['name']); |
| 534 | |
| 535 | // If the user provided class data already then don't over-rule it |
| 536 | if (!empty($vars->othervars['class'])) { |
| 537 | $classes[] = $vars->othervars['class']; |
| 538 | } |
| 539 | |
| 540 | // Return completed markup |
| 541 | return '<div' . $id . ' class="' . implode(' ', $classes) . '">' . $embed_markup . '</div>'; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | /** |
| 546 | * Collect information from all modules about the methods (players and embedding) |
| 547 | * that are available. |
| 548 | * |
| 549 | * @param $action |
| 550 | * Optional parameter to retrieve data only about a specific action. |
| 551 | * @param $reset |
| 552 | * Optional parameter which if TRUE will reset the methods cache and rebuild it. |
| 553 | * @return |
| 554 | * An array of data describing the available methods. |
| 555 | */ |
| 556 | function swftools_methods_available($action = NULL, $reset = FALSE) { |
| 557 | |
| 558 | // Cache methods array as it may be called several times |
| 559 | static $methods = array(); |
| 560 | |
| 561 | // If user has requested the cache to be reset then reset it |
| 562 | if ($reset) { |
| 563 | $methods = array(); |
| 564 | } |
| 565 | |
| 566 | // If the cache isn't populated then build it by calling hook_swftools_methods |
| 567 | if (!count($methods)) { |
| 568 | $methods = module_invoke_all('swftools_methods'); |
| 569 | } |
| 570 | |
| 571 | // In case no module is presenting support for the required action then the |
| 572 | // following line avoids a notice error |
| 573 | if ($action) { |
| 574 | $methods += array( |
| 575 | $action => NULL, |
| 576 | ); |
| 577 | } |
| 578 | |
| 579 | // Return methods - either for the specific action, or all actions |
| 580 | return ($action) ? $methods[$action] : $methods; |
| 581 | |
| 582 | } |
| 583 | |
| 584 | |
| 585 | /** |
| 586 | * Convert an array of parameters in to JSON and assign to an object |
| 587 | * |
| 588 | * @param $params |
| 589 | * An array of parameters to process. |
| 590 | * @param $attr |
| 591 | * Optional name of object to assign the JSON string to, default is swftools. |
| 592 | * @return |
| 593 | * A string defining the object in JSON. |
| 594 | */ |
| 595 | function swftools_json_params($params, $object = 'swftools') { |
| 596 | return $object . "='" . drupal_to_js($params) . "'"; |
| 597 | } |
| 598 | |
| 599 | |
| 600 | /** |
| 601 | * Returns 'true' or 'false' for JavaScript based the supplied value $bool. |
| 602 | * |
| 603 | * @param $bool |
| 604 | * The value that should be cast to true or false. |
| 605 | * @return |
| 606 | * The string 'true' or 'false' depending on the supplied value. |
| 607 | */ |
| 608 | function _swftools_tf($bool) { |
| 609 | |
| 610 | // String 'false' is treated as TRUE in PHP logic, so force to FALSE |
| 611 | if (strtolower($bool) == 'false') { |
| 612 | $bool = FALSE; |
| 613 | } |
| 614 | |
| 615 | // Return 'true' or 'false' now we are sure of result |
| 616 | return $bool ? 'true' : 'false'; |
| 617 | |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /** |
| 622 | * Identify the most likely SWF Tools action for a file, based on its extension. |
| 623 | * |
| 624 | * @param $file |
| 625 | * The name of the file for which the action is required. |
| 626 | * @return |
| 627 | * A string describing an SWF Tools action. |
| 628 | */ |
| 629 | function swftools_get_action($file) { |
| 630 | |
| 631 | // Get the path information for this file |
| 632 | $path_parts = pathinfo($file); |
| 633 | |
| 634 | // Select an action based on the file extension |
| 635 | switch (strtolower($path_parts['extension'])) { |
| 636 | case 'swf': |
| 637 | return SWFTOOLS_SWF_DISPLAY_DIRECT; |
| 638 | case 'flv': |
| 639 | return SWFTOOLS_FLV_DISPLAY; |
| 640 | case 'mp3': |
| 641 | return SWFTOOLS_MP3_DISPLAY; |
| 642 | case 'jpg': case 'gif': case 'png': case 'jpeg': case 'img': |
| 643 | return SWFTOOLS_IMAGE_DISPLAY_LIST; |
| 644 | default: |
| 645 | // Assume that this is mixed media and let the media player try to handle it |
| 646 | return SWFTOOLS_MEDIA_DISPLAY_LIST; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
| 651 | /** |
| 652 | * Identify the currently configured player for the specified action. |
| 653 | * |
| 654 | * @param $action |
| 655 | * The SWF Tools action to be performed. |
| 656 | * @return |
| 657 | * The name of the currently configured player for this action. |
| 658 | */ |
| 659 | function swftools_get_player($action) { |
| 660 | |
| 661 | switch ($action) { |
| 662 | case SWFTOOLS_SWF_DISPLAY_DIRECT: |
| 663 | return variable_get(SWFTOOLS_SWF_DISPLAY_DIRECT, SWFTOOLS_SWF); |
| 664 | case SWFTOOLS_FLV_DISPLAY: |
| 665 | return variable_get(SWFTOOLS_FLV_DISPLAY, GENERIC_FLV); |
| 666 | case SWFTOOLS_MP3_DISPLAY: |
| 667 | return variable_get(SWFTOOLS_MP3_DISPLAY, GENERIC_MP3); |
| 668 | |
| 669 | // For all other actions try to get the setting from the database, otherwise |
| 670 | // return FALSE to indicate no player is available to handle this action. |
| 671 | default: |
| 672 | return variable_get($action, FALSE); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Returns the playlist path relative to webroot. |
| 678 | * This path needs to be writeable, so it is fitting to limit valid |
| 679 | * locations to the files directory. |
| 680 | * |
| 681 | */ |
| 682 | /** |
| 683 | * Returns the playlist path, relative to the webroot |
| 684 | * |
| 685 | * @param $dir |
| 686 | * If FALSE the function returns the current playlist directory. |
| 687 | * Otherwise the function tries to create the named directory in the file system. |
| 688 | * @return |
| 689 | * The path to the playlist directory, either the one stored in the database or |
| 690 | * the one that was created, otherwise returns FALSE to indicate failure. |
| 691 | */ |
| 692 | function swftools_get_playlist_path($dir = FALSE) { |
| 693 | |
| 694 | // If no directory specified, get the default |
| 695 | if (!$dir) { |
| 696 | $dir = variable_get('swftools_playlist_path', SWFTOOLS_PLAYLIST_PATH); |
| 697 | } |
| 698 | |
| 699 | // Ensure we have a path in the file system |
| 700 | $dir = file_create_path($dir); |
| 701 | |
| 702 | // Create playlist directory if necessary |
| 703 | if (!file_check_directory($dir, FILE_CREATE_DIRECTORY)) { |
| 704 | drupal_set_message(t('%dir does not exist, or is not writeable.', array('%dir' => $dir)), 'error', FALSE); |
| 705 | } |
| 706 | |
| 707 | // Return path to the playlist directory |
| 708 | return $dir; |
| 709 | |
| 710 | } |
| 711 | |
| 712 | |
| 713 | /** |
| 714 | * Returns a flash player path relative to webroot. |
| 715 | * The default path is in the modules/swftools/shared directory. |
| 716 | * It may suit some sites to store flash players in an alternative location, but |
| 717 | * the assumption is the location will be on the same server. |
| 718 | * If the path starts with '/', then we can assume is relative to the webroot. |
| 719 | * Otherwise we assume it's in the files directory. |
| 720 | * |
| 721 | * @param $dir |
| 722 | * Optional parameter that gives the location of flash media players. |
| 723 | * @return |
| 724 | * String with the path to the media players. |
| 725 | */ |
| 726 | function swftools_get_player_path($dir = FALSE) { |
| 727 | |
| 728 | // If a directory parameter wasn't set then return the configured value |
| 729 | if (!$dir) { |
| 730 | $dir = variable_get('swftools_player_path', SWFTOOLS_PLAYER_PATH); |
| 731 | |
| 732 | // If the swftools_player_path variable isn't set return the default path |
| 733 | if (!$dir) { |
| 734 | $dir = drupal_get_path('module', 'swftools') .'/shared'; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | // If $dir starts with / then assume we provided a full path from the web root |
| 739 | elseif (substr($dir, 0, 1) == '/') { |
| 740 | $dir = ltrim($dir, '/'); |
| 741 | } |
| 742 | |
| 743 | // Otherwise assume the path is in the files directory and build that path |
| 744 | else { |
| 745 | $dir = file_create_path($dir); |
| 746 | } |
| 747 | |
| 748 | // Return the resulting directory |
| 749 | return $dir; |
| 750 | } |
| 751 | |
| 752 | |
| 753 | /** |
| 754 | * Returns the media path relative to webroot. |
| 755 | * There is a setting called 'swftools_media_url'. If this is set, we assume the |
| 756 | * media is on a different server. |
| 757 | * |
| 758 | * @return |
| 759 | * A string containing the path to the local files, or empty if the files are remote. |
| 760 | */ |
| 761 | function swftools_get_media_path() { |
| 762 | |
| 763 | // Retrieve the media url setting |
| 764 | $media_url = trim(variable_get('swftools_media_url', '')); |
| 765 | |
| 766 | // If no media url is set then return the path to local files |
| 767 | if (!$media_url || $media_url == '') { |
| 768 | return file_create_path('') . '/'; |
| 769 | } |
| 770 | |
| 771 | // If a media url is set then assume this is a remote path and so we don't know anything |
| 772 | // about the path between the base url and the file. Return an empty string. |
| 773 | return ''; |
| 774 | } |
| 775 | |
| 776 | |
| 777 | /** |
| 778 | * Resolve a path to a full url, either on the local file system, or at a remote address |
| 779 | * if the swftools_media_url variable has been set. If the path describes a file, is local |
| 780 | * and the swftools_check_media variable is set then check if the file exists. |
| 781 | * The path must be relative to the webroot. |
| 782 | * |
| 783 | * @param $path |
| 784 | * The file path to check. |
| 785 | * @param $is_file |
| 786 | * Optional flag to indicate that the path points to a file in which case local files can be tested to see if |
| 787 | * they exist (defaults to TRUE). If set to FALSE then it indicates the path doesn't refer to a file and it won't be tested. |
| 788 | * @return |
| 789 | * A string with the complete url to the file, either locally or using the remote path, or FALSE if the local file doesn't exist |
| 790 | */ |
| 791 | function swftools_get_media_url($path, $is_file = TRUE) { |
| 792 | |
| 793 | // Retrieve the media url setting - this is set if the files are remote |
| 794 | static $media_url; |
| 795 | if (!isset($media_url)) { |
| 796 | $media_url = trim(variable_get('swftools_media_url', '')); |
| 797 | } |
| 798 | |
| 799 | // If a remote path is set simply build the appropriate path and return |
| 800 | if ($media_url) { |
| 801 | return $media_url . '/' . $path; |
| 802 | } |
| 803 | |
| 804 | // If media checking is active, and the path is a file, check to see if it actually exists |
| 805 | if (variable_get('swftools_check_media', TRUE) && $is_file) { |
| 806 | |
| 807 | // If the file doesn't exist, set an error message and return FALSE to indicate failure |
| 808 | if (!file_exists($path)) { |
| 809 | drupal_set_message(t('SWF Tools error - %path does not appear to exist.', array('%path' => $path)), 'error'); |
| 810 | return FALSE; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // Return the path |
| 815 | return file_create_url($path); |
| 816 | |
| 817 | } |
| 818 | |
| 819 | |
| 820 | /** |
| 821 | * "flashvars" is a parameter like height and width, which are |
| 822 | * passed into the flash player as a=1&b=2&... |
| 823 | * |
| 824 | */ |
| 825 | function _swftools_get_flashvars_string(&$flashvars) { |
| 826 | |
| 827 | foreach ($flashvars AS $var => $value) { |
| 828 | $flashvars[$var] = str_replace(array('&', '=', '?'), array('%26', '%3D', '%3F'), $value); |
| 829 | } |
| 830 | $encoded = drupal_query_string_encode($flashvars); |
| 831 | |
| 832 | // '#' seems to encode as %2523, reverse this, using a more robust hex prefix.. |
| 833 | $encoded = str_replace('%2523', '0x', $encoded); |
| 834 | |
| 835 | // Fix encoding per #181998#comment-882293 |
| 836 | $encoded = str_replace('%3A', ':', $encoded); |
| 837 | $encoded = str_replace('%252F', '/', $encoded); |
| 838 | |
| 839 | return $encoded; |
| 840 | } |
| 841 | |
| 842 | |
| 843 | /** |
| 844 | * Return an array of default values to use as the swf parameters. |
| 845 | * Parameters are described in the Adobe knowledge base TechNote 12701 |
| 846 | * http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_12701 |
| 847 | * |
| 848 | * @return |
| 849 | * An array of key/value pairs |
| 850 | */ |
| 851 | function _swftools_params() { |
| 852 | |
| 853 | $defaults = array( |
| 854 | 'swliveconnect' => variable_get('swftools_params_swliveconnect', 'default'), |
| 855 | 'play' => variable_get('swftools_params_play', TRUE), |
| 856 | 'loop' => variable_get('swftools_params_loop', TRUE), |
| 857 | 'menu' => variable_get('swftools_params_menu', FALSE), |
| 858 | 'quality' => variable_get('swftools_params_quality', 'autohigh'), |
| 859 | 'scale' => variable_get('swftools_params_scale', 'showall'), |
| 860 | 'align' => variable_get('swftools_params_align', 'l'), |
| 861 | 'salign' => variable_get('swftools_params_salign', 'tl'), |
| 862 | 'wmode' => variable_get('swftools_params_wmode', 'opaque'), |
| 863 | 'bgcolor' => variable_get('swftools_params_bgcolor', '#FFFFFF'), |
| 864 | 'version' => variable_get('swftools_params_version', '7'), |
| 865 | 'allowfullscreen' => variable_get('swftools_params_allowfullscreen', TRUE), |
| 866 | 'allowscriptaccess' => variable_get('swftools_params_allowscriptaccess', 'sameDomain'), |
| 867 | ); |
| 868 | |
| 869 | // Ensure that boolean parameters are set to strings 'true' or 'false' |
| 870 | $defaults['menu'] = _swftools_tf($defaults['menu']); |
| 871 | $defaults['play'] = _swftools_tf($defaults['play']); |
| 872 | $defaults['loop'] = _swftools_tf($defaults['loop']); |
| 873 | $defaults['allowfullscreen'] = _swftools_tf($defaults['allowfullscreen']); |
| 874 | |
| 875 | // Return the defaults |
| 876 | return $defaults; |
| 877 | } |
| 878 | |
| 879 | |
| 880 | /** |
| 881 | * Attempt to return information for the specified file |
| 882 | * Supply the path to the file to be processed, and it return FALSE if no data |
| 883 | * was obtained. The return variable, if successful, is an array that may |
| 884 | * include width, height, extension, file_size, mime_type. |
| 885 | * |
| 886 | */ |
| 887 | function swftools_get_info($file) { |
| 888 | |
| 889 | // Only check the file if it is local, or it is a media player |
| 890 | //if (trim(variable_get('swftools_media_url', '')) == '') { |
| 891 | if ((trim(variable_get('swftools_media_url', '')) == '') or (strpos($file, swftools_get_player_path()) === 0)) { |
| 892 | $info = image_get_info($file); |
| 893 | return $info; |
| 894 | } |
| 895 | return FALSE; |
| 896 | } |
| 897 | |
| 898 | |
| 899 | /** |
| 900 | * Saves a playlist (xml file) to the playlist directory ready for the swf player to use. |
| 901 | * |
| 902 | * @param &$playlist_data |
| 903 | * A formatted array of playlist data that will be converted to an xml file. NEED TO DOCUMENT THE STRUCTURE. |
| 904 | * @param $playlist_name |
| 905 | * An optional name for the playlist. If not specified a filename will be created. |
| 906 | * @param $method |
| 907 | * An array describing the selected player method. |
| 908 | * @param &$vars |
| 909 | * Array of variables. Not used by this function, but will be passed to the playlist generator functions. |
| 910 | * @return |
| 911 | * A string containing the path to the playlist, or FALSE if the playlist generation failed. |
| 912 | * Note that $playlist_data and $vars can be manipulated by this function. |
| 913 | */ |
| 914 | function swftools_generate_playlist(&$playlist_data, $playlist_name, &$method, &$vars) { |
| 915 | |
| 916 | // If no filename is supplied |
| 917 | if (!$playlist_name) { |
| 918 | |
| 919 | // Initialise a string |
| 920 | $prename = ''; |
| 921 | |
| 922 | // Iterate through each element of $playlist_data['playlist'] |
| 923 | foreach ($playlist_data['playlist'] AS $data) { |
| 924 | |
| 925 | // Build a string using all the filenames |
| 926 | $prename .= $data['filename']; |
| 927 | } |
| 928 | |
| 929 | // Hash the resulting string of names and use as the filename |
| 930 | $playlist_name = md5($method->player['name'] . $prename) . '.xml'; |
| 931 | } |
| 932 | |
| 933 | // Turn the playlist name in to a full path |
| 934 | $playlist_name = swftools_get_playlist_path() . '/' . $playlist_name; |
| 935 | |
| 936 | // If caching of xml playlist files has been disabled then delete the current playlist by this name |
| 937 | if (variable_get('swftools_playlist_caching', 'here') == 'always') { |
| 938 | file_delete($playlist_name); |
| 939 | } |
| 940 | |
| 941 | // If the playlist already exists then return the path to it |
| 942 | elseif (is_file($playlist_name)) { |
| 943 | |
| 944 | // Return path to the file |
| 945 | return file_create_url($playlist_name); |
| 946 | } |
| 947 | |
| 948 | // Determine the name of the relevant hook to output the playlist in the appropriate format |
| 949 | $hook = $method->player['name'] . '_swftools_playlist'; |
| 950 | |
| 951 | // Check that the module implements this hook before trying to call it |
| 952 | if (module_hook($method->player['module'], $hook)) { |
| 953 | $playlist = module_invoke($method->player['module'], $hook, $playlist_data, $method, $vars); |
| 954 | } |
| 955 | |
| 956 | // If the hook doesn't exist then the player doesn't support playlists |
| 957 | else { |
| 958 | drupal_set_message(t('@title module does not support xml playlists.', array('@title' => $method->player['title'])), 'error'); |
| 959 | } |
| 960 | |
| 961 | // Try to open the playlist file ready to store the xml playlist |
| 962 | if (!$handle = fopen($playlist_name, 'a')) { |
| 963 | drupal_set_message(t('An error occurred trying to create file %playlist_name.', array('%playlist_name' => $playlist_name)), 'error'); |
| 964 | return FALSE; |
| 965 | } |
| 966 | |
| 967 | // If the file was opened then try to write the playlist data to it |
| 968 | if (fwrite($handle, $playlist) === FALSE) { |
| 969 | drupal_set_message(t('An error occurred trying to create file %playlist_name.', array('%playlist_name' => $playlist_name)), 'error'); |
| 970 | return FALSE; |
| 971 | } |
| 972 | |
| 973 | // Close the file |
| 974 | fclose($handle); |
| 975 | |
| 976 | // Return a url to the playlist |
| 977 | return file_create_url($playlist_name); |
| 978 | } |
| 979 | |
| 980 | |
| 981 | /** |
| 982 | * Add to the page any supporting files required by a given embedding method. |
| 983 | * |
| 984 | * @param $embed |
| 985 | * Optional parameter - if not supplied the files for the current method will be added. |
| 986 | * If supplied then the files for the named method will be added. |
| 987 | * @return |
| 988 | * Nothing |
| 989 | */ |
| 990 | function swftools_push_js($embed = SWFDEFAULT) { |
| 991 | |
| 992 | // Get the list of all the the currently available methods |
| 993 | $methods = swftools_methods_available(); |
| 994 | |
| 995 | // If no specific embedding method was named then get the current default |
| 996 | if (!$embed) { |
| 997 | $embed = variable_get('swftools_embed_method', SWFTOOLS_NOJAVASCRIPT); |
| 998 | } |
| 999 | |
| 1000 | // Call the module responsible to output the js. Don't pass any additional |
| 1001 | // parameters - as we don't want the module to try and return the in-body |
| 1002 | // html placeholder for the flash content. |
| 1003 | $output = module_invoke($methods[SWFTOOLS_EMBED][$embed]['module'], 'swftools_embed'); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | /** |
| 1008 | * Take an array of filenames and prepare them to be used as a playlist |
| 1009 | * |
| 1010 | * @param $files |
| 1011 | * An array of files that will be added to the playlist. |
| 1012 | * @param $title |
| 1013 | * Optional playlist title |
| 1014 | * @param $get_action |
| 1015 | * Optional parameter indicating if the function should determine an appropriate action. Default is TRUE. |
| 1016 | * @param $type_filter |
| 1017 | * Optional parameter - an array of file extensions that are permitted |
| 1018 | * @param $stream |
| 1019 | * Option parameter to indicate if this is going to be a streamed playlist, in which case checks for the |
| 1020 | * existence of files should be skipped |
| 1021 | * @return unknown_type |
| 1022 | */ |
| 1023 | function swftools_prepare_playlist_data($files, $title = '', $get_action = TRUE, $type_filter = array(), $stream = FALSE) { |
| 1024 | |
| 1025 | // Initialise an array to return the results in |
| 1026 | $playlist_data = array(); |
| 1027 | |
| 1028 | // Set a title for the playlist |
| 1029 | $playlist_data['header']['title'] = $title; |
| 1030 | |
| 1031 | // Initialize an array for the results |
| 1032 | $processed_files = array(); |
| 1033 | |
| 1034 | // Iterate through the array of files that have been passed |
| 1035 | foreach ($files AS $key => $data) { |
| 1036 | |
| 1037 | // If $data is an object then recast it to an array |
| 1038 | if (is_object($data)) { |
| 1039 | $temp = array($data); |
| 1040 | } |
| 1041 | |
| 1042 | // If $data is already an array simply put a copy in $temp |
| 1043 | elseif (is_array($data)) { |
| 1044 | $temp = $data; |
| 1045 | } |
| 1046 | |
| 1047 | // Otherwise turn a single piece of data in to an array |
| 1048 | else { |
| 1049 | |
| 1050 | // Initialise array |
| 1051 | $temp = array(); |
| 1052 | |
| 1053 | // Use the data string as the filepath |
| 1054 | $temp['filepath'] = $data; |
| 1055 | |
| 1056 | // If the $key isn't numeric then take it to be a filename |
| 1057 | if (!is_numeric($key)) { |
| 1058 | $temp['filename'] = $key; |
| 1059 | } |
| 1060 | |
| 1061 | // Otherwise use the data string as the filename |
| 1062 | else { |
| 1063 | $temp['filename'] = $data; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // Suppress errors in case the array is missing keys |
| 1068 | $temp += array( |
| 1069 | 'filename' => '', |
| 1070 | 'filepath' => '', |
| 1071 | ); |
| 1072 | |
| 1073 | // We should now have an item called $temp, with some known keys, and |
| 1074 | // also any other keys that were passed (if originally an array or object) |
| 1075 | // filepath holds either a file path, or the name of the item sent as a string |
| 1076 | // filename contains either the name of the key from original array, of if this |
| 1077 | // was not set, it will be the same as filepath |
| 1078 | |
| 1079 | // We know $temp array contains keys filename and filepath |
| 1080 | $file['filename'] = $temp['filename']; |
| 1081 | $file['filepath'] = $temp['filepath']; |
| 1082 | |
| 1083 | // Do we need the playlist generator to do this stuff with the urls? |
| 1084 | // I suppose it avoids repetition in later modules. |
| 1085 | |
| 1086 | // If the fileurl isn't set in the original array, then try to work it out |
| 1087 | if (!isset($temp['fileurl'])) { |
| 1088 | |
| 1089 | // If the filepath is a valid url, use that |
| 1090 | if (valid_url($file['filepath'], TRUE)) { |
| 1091 | $file['fileurl'] = $file['filepath']; |
| 1092 | $file['filepath'] = FALSE; // Flag that we don't know the server path. |
| 1093 | } |
| 1094 | |
| 1095 | // If fid is set then these are files from the upload module, so make a file path |
| 1096 | elseif (isset($temp['fid'])) { |
| 1097 | $file['fileurl'] = swftools_strip_base_root(swftools_get_media_url($temp['filepath'], FALSE)); |
| 1098 | } |
| 1099 | |
| 1100 | else { |
| 1101 | // Build a fileurl, or just assign the filepath if this is to be streamed |
| 1102 | if (!$stream) { |
| 1103 | |
| 1104 | // Then check if files are being sourced locally, and if they are build a file path |
| 1105 | if (swftools_get_media_path()) { |
| 1106 | $filepath = file_create_path($file['filepath']); |
| 1107 | } |
| 1108 | else { |
| 1109 | $filepath = $file['filepath']; |
| 1110 | } |
| 1111 | |
| 1112 | $file['filepath'] = $filepath; |
| 1113 | $file['fileurl'] = swftools_strip_base_root(swftools_get_media_url($filepath)); |
| 1114 | } |
| 1115 | else { |
| 1116 | $file['fileurl'] = $temp['filepath']; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | // If the filename isn't set then try to extract it from the url |
| 1122 | if (!isset($file['filename'])) { |
| 1123 | $path_parts = pathinfo($file['fileurl']); |
| 1124 | $file['filename'] = $path_parts['basename']; |
| 1125 | } |
| 1126 | |
| 1127 | // If a title for the file isn't set then create one form the filename |
| 1128 | if (!isset($temp['title'])) { |
| 1129 | $file['title'] = $file['filename']; |
| 1130 | } |
| 1131 | |
| 1132 | // Otherwise use the supplied one |
| 1133 | else { |
| 1134 | $file['title'] = $temp['title']; |
| 1135 | } |
| 1136 | |
| 1137 | // Store the prepared file data in the $processed_files array |
| 1138 | // We now have a consistent array with keys filename, filepath, fileurl and title |
| 1139 | $processed_files[] = $file; |
| 1140 | } |
| 1141 | |
| 1142 | // Add the finished array of processed files to the playlist data |
| 1143 | $playlist_data['playlist'] = $processed_files; |
| 1144 | |
| 1145 | // Note, we want to exit quickly if the caller did not want us to |
| 1146 | // dynamically determine the display action by passing $get_action = FALSE. |
| 1147 | if (!$get_action) { |
| 1148 | // The caller knows what swftools action to set, so exit here. |
| 1149 | return $playlist_data; |
| 1150 | } |
| 1151 | |
| 1152 | // Try to work out the action from the files passed. |
| 1153 | $first_valid_file_type = FALSE; |
| 1154 | $mixed_media = FALSE; |
| 1155 | |
| 1156 | // Process the files attached to the node to determine the correct action. |
| 1157 | foreach ($playlist_data['playlist'] AS $key => $data) { |
| 1158 | |
| 1159 | // fileurl is always set, irrespective of what we are passing, so use this to determine extension |
| 1160 | $path_parts = pathinfo($data['fileurl']); |
| 1161 | |
| 1162 | // Get the extension for the file |
| 1163 | $extension = strtolower($path_parts['extension']); |
| 1164 | |
| 1165 | // Only try to determine actions if there's an extension to work with |
| 1166 | if ($extension) { |
| 1167 | |
| 1168 | // Determine if this is an image type |
| 1169 | if (strpos('|jpg|jpeg|gif|png|', $extension)) { |
| 1170 | // Treat all types of images as the same file type |
| 1171 | $extension = 'img'; |
| 1172 | } |
| 1173 | |
| 1174 | // Only process the file if $type_filter is empty (ie. no filter) |
| 1175 | // or if the extension is declared in the $type_filter array. |
| 1176 | if (!count($type_filter) || in_array($extension, $type_filter)) { |
| 1177 | // $first_valid_file_type stores the file type of the first valid file. |
| 1178 | // This will be compared to subsequent files and if two files |
| 1179 | // have different types, the action will be defined as SWFTOOLS_MEDIA_DISPLAY_LIST |
| 1180 | // in order to utilize a flexible media player. |
| 1181 | if (!$first_valid_file_type) { |
| 1182 | $first_valid_file_type = $extension; |
| 1183 | } |
| 1184 | else { |
| 1185 | if ($first_valid_file_type != $extension) { |
| 1186 | $mixed_media = TRUE; |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | else { |
| 1191 | // This file is not desired so remove it |
| 1192 | unset($playlist_data['playlist'][$key]); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | // Make a decision based on analysing the file array. |
| 1198 | if ($first_valid_file_type == '') { |
| 1199 | // No files passed our test. |
| 1200 | return FALSE; |
| 1201 | } |
| 1202 | |
| 1203 | // Determine the required action. |
| 1204 | if ($mixed_media) { |
| 1205 | // We have files of multiple types. |
| 1206 | $action = SWFTOOLS_MEDIA_DISPLAY_LIST; |
| 1207 | } |
| 1208 | else { |
| 1209 | // We only had one file type, so discover the action for that type by |
| 1210 | // calling swftools_get_action() with a dummy filename |
| 1211 | $action = swftools_get_action('dummy.' . $first_valid_file_type); |
| 1212 | } |
| 1213 | |
| 1214 | // Pluralize the action for multiple files if not already pluralized |
| 1215 | if (count($playlist_data['playlist']) > 1 && substr($action, -5, 5) != '_list') { |
| 1216 | $action = $action . '_list'; |
| 1217 | } |
| 1218 | |
| 1219 | // Assign the action to the playlist data ready for return |
| 1220 | $playlist_data['action'] = $action; |
| 1221 | |
| 1222 | // Return the resulting playlist data |
| 1223 | return $playlist_data; |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | /** |
| 1228 | * Next three filter related code ported from flash_filter. |
| 1229 | * |
| 1230 | */ |
| 1231 | |
| 1232 | /* |
| 1233 | * Implementation of hook_filter_tips |
| 1234 | * |
| 1235 | */ |
| 1236 | function swftools_filter_tips($delta, $format, $long = false) { |
| 1237 | if ($long) { |
| 1238 | return t(' |
| 1239 | <h3 id="swftools_filter">SWF Tools Filter</h3> |
| 1240 | <p>The basic syntax for embedding a flash file (.swf), flash movie (.flv) or audio file (.mp3) is:</p> |
| 1241 | <blockquote><code><swf file="filename.swf"></code></blockquote> |
| 1242 | |
| 1243 | <p>If you would like to override SWF Tools and flash player default settings, |
| 1244 | you can specify additional parameters. For example:</p> |
| 1245 | <blockquote><code><swf file="song.mp3" flashvars="backcolor=#AABBCC&&forecolor=#11AA11"></code></blockquote> |
| 1246 | <p>If you would like to output a list of files then the format is:</p> |
| 1247 | <blockquote><code><swf files="image1.jpg&&image2.jpg&&..."></code></blockquote> |
| 1248 | SWF Tools Filter will accept following: |
| 1249 | <ul> |
| 1250 | <li><b>params</b> : You can specify values for parameters to be passed to Flash |
| 1251 | to control the appearance of the output. Typical values are |
| 1252 | bgcolor and wmode. Example: <code>params="wmode=true&&bgcolor="#00FF00"</code> |
| 1253 | Alternatively you can supply each parameter individually without using |
| 1254 | <code>params</code>. Example <code>wmode="true" bgcolor="#00FF00"</code></li> |
| 1255 | <li><b>flashvars</b> : You can specify values for output as flashvars, which |
| 1256 | become available to the Flash movie that is playing. This is often done |
| 1257 | to control a media player. Refer to the documentation of the flash player |
| 1258 | you are using to know what flashvar options are available. |
| 1259 | Example: <code>flashvars="autostart=true&&volume=80"</code></li> |
| 1260 | <li><b>methods</b> : Optional information about how to display the file. The most |
| 1261 | common usage is to specify a particular media player and |
| 1262 | thus override the default specified on the settings page. |
| 1263 | Example: <code>methods="player=onepixelout_mp3"</code></li> |
| 1264 | </ul> |
| 1265 | <p><strong>WARNING</strong>: with params, flashvars and othervars, pass multiple values |
| 1266 | separated by <strong>&&</strong>.</p>'); |
| 1267 | } |
| 1268 | else { |
| 1269 | 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')))); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * Implementation of hook_filter |
| 1275 | * |
| 1276 | */ |
| 1277 | function swftools_filter($op, $delta = 0, $format = -1, $text = '') { |
| 1278 | switch ($op) { |
| 1279 | case 'list': |
| 1280 | return array(0 => t('SWF Tools filter')); |
| 1281 | case 'no cache': |
| 1282 | return FALSE; |
| 1283 | case 'description': |
| 1284 | return t('Substitutes <swf file="filename.flv"> or <swf files="file1.jpg&&file2.jpg"> with embedding code.'); |
| 1285 | case 'prepare': |
| 1286 | // replace <swf > with [swf ] to prevent other filters stripping |
| 1287 | return (preg_replace('/\<(swflist|swf)\s*(.*)>/sU', '[\1 \2]', $text)); |
| 1288 | case 'process': |
| 1289 | return _swftools_filter_process_text($text); |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * This function processes the filter text that the user has added to the text area. |
| 1295 | * If the filter is wrapped in <p></p> then these are stripped as part of the processing |
| 1296 | * This eliminates a validation error in the resulting mark up if SWF Tools filter is |
| 1297 | * being used in conjunction with other HTML filters that correct line breaks. |
| 1298 | * It won't work in EVERY case, but it will work in MOST cases. |
| 1299 | * Filters that are embedded in-line with text will continue to fail validation. |
| 1300 | */ |
| 1301 | function _swftools_filter_process_text($text) { |
| 1302 | $endl = chr(13) ; |
| 1303 | if (preg_match_all('@(?:<p>)?\[(swflist|swf)\s*(.*?)\](?:</p>)?@s', $text, $match)) { |
| 1304 | // $match[0][#] .... fully matched string <swf|swflist parm_0="value_0" parm_1="value_1" parm_2="value_2"> |
| 1305 | // $match[1][#] .... matched tag type ( swf | swflist ) |
| 1306 | // $match[2][#] .... full params string until the closing '>' |
| 1307 | |
| 1308 | $swftools_parameters = array('file', 'params', 'flashvars', 'othervars', 'methods', 'files'); |
| 1309 | $match_vars = array(); |
| 1310 | foreach ($match[2] as $key => $passed_parameters) { |
| 1311 | preg_match_all('/(\w*)=\"(.*?)\"/', $passed_parameters, $match_vars[$key]); |
| 1312 | // $match_vars[0][#] .... fully matched string |
| 1313 | // $match_vars[1][#] .... matched parameter, eg flashvars, params |
| 1314 | // $match_vars[2][#] .... value after the '=' |
| 1315 | |
| 1316 | // Process the parameters onto the $prepared array. |
| 1317 | // Search for standard parameters, parse the values out onto the array. |
| 1318 | foreach ($match_vars[$key][1] as $vars_key => $vars_name) { |
| 1319 | |
| 1320 | // Switch to swf or swflist, based on file or files |
| 1321 | // Need to tidy up this line, probably use switch/case |
| 1322 | if ($vars_name == 'file') { |
| 1323 | $match[1][$key] = 'swf'; |
| 1324 | } |
| 1325 | else { |
| 1326 | if ($vars_name == 'files') { |
| 1327 | $match[1][$key] = 'swflist'; |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | if ($vars_name == 'file') { |
| 1332 | $prepared[$key][$vars_name] = $match_vars[$key][2][$vars_key]; |
| 1333 | unset ($match_vars[$key][1][$vars_key]); |
| 1334 | } |
| 1335 | elseif (in_array($vars_name, $swftools_parameters)) { |
| 1336 | $prepared[$key][$vars_name] = swftools_url_parse(str_replace(array('&&', '&&'), '&', $match_vars[$key][2][$vars_key])); |
| 1337 | unset ($match_vars[$key][1][$vars_key]); |
| 1338 | } |
| 1339 | else |