You are here

function _swftools_filter_process_text in SWF Tools 6.3

Same name and namespace in other branches
  1. 5 swftools.module \_swftools_filter_process_text()
  2. 6 swftools.module \_swftools_filter_process_text()
  3. 6.2 swftools.module \_swftools_filter_process_text()

Processes text obtained from the input filter.

This function processes the filter text that the user has added to the text area. If the filter is wrapped in <p></p> then these are stripped as part of the processing This eliminates a validation error in the resulting mark up if SWF Tools filter is being used in conjunction with other HTML filters that correct line breaks. It won't work in EVERY case, but it will work in MOST cases. Filters that are embedded in-line with text will continue to fail validation.

1 call to _swftools_filter_process_text()
swftools_filter in ./swftools.module
Implementation of hook_filter().

File

./swftools.module, line 1007
The primary component of SWF Tools that enables comprehensive media handling.

Code

function _swftools_filter_process_text($text) {
  $endl = chr(13);
  if (preg_match_all('@(?:<p>)?\\[(swflist|swf)\\s*(.*?)\\](?:</p>)?@s', $text, $match)) {

    // $match[0][#] .... fully matched string <swf|swflist parm_0="value_0" parm_1="value_1" parm_2="value_2">
    // $match[1][#] .... matched tag type ( swf | swflist )
    // $match[2][#] .... full params string until the closing '>'
    $swftools_parameters = array(
      'file',
      'params',
      'flashvars',
      'othervars',
      'methods',
      'files',
    );
    $match_vars = array();

    // Initialise an array to hold playlist arrays
    $files = array();
    foreach ($match[2] as $key => $passed_parameters) {

      //preg_match_all('/(\w*)=\"(.*?)\"/', $passed_parameters, $match_vars[$key]);
      preg_match_all('/(\\w*)=(?:\\"|&quot;)(.*?)(?:\\"|&quot;)/', $passed_parameters, $match_vars[$key]);

      // $match_vars[0][#] .... fully matched string
      // $match_vars[1][#] .... matched parameter, eg flashvars, params
      // $match_vars[2][#] .... value after the '='
      // Process the parameters onto the $prepared array.
      // Search for standard parameters, parse the values out onto the array.
      foreach ($match_vars[$key][1] as $vars_key => $vars_name) {

        // Switch to swf or swflist, based on file or files
        // Need to tidy up this line, probably use switch/case
        if ($vars_name == 'file') {
          $match[1][$key] = 'swf';
        }
        else {
          if ($vars_name == 'files') {
            $match[1][$key] = 'swflist';
          }
        }
        if ($vars_name == 'file') {
          $prepared[$key][$vars_name] = $match_vars[$key][2][$vars_key];
          unset($match_vars[$key][1][$vars_key]);
        }
        elseif (in_array($vars_name, $swftools_parameters)) {
          $prepared[$key][$vars_name] = swftools_parse_str(str_replace(array(
            '&amp;&amp;',
            '&&',
          ), '&', $match_vars[$key][2][$vars_key]));
          unset($match_vars[$key][1][$vars_key]);
        }
        else {
          $prepared[$key]['othervars'][$vars_name] = $match_vars[$key][2][$vars_key];
        }
      }

      // Search for remaining parameters, map them as elements of the standard parameters.
      if (isset($prepared[$key]['methods']['player'])) {
        $player = strtolower($prepared[$key]['methods']['player']);
      }
      else {
        $player_key = array_search('player', $match_vars[$key][1]);
        if ($player_key !== FALSE) {
          $player = strtolower($match_vars[$key][2][$player_key]);
        }
        else {
          $player = FALSE;
        }
      }
      $prepared[$key]['methods']['player'] = $player;
      if (count($match_vars[$key][1])) {

        // Find out if a player has been set.
        foreach ($match_vars[$key][1] as $vars_key => $vars_name) {
          if ($parent = swftools_get_filter_alias($vars_name, $player)) {
            if ($parent) {
              $prepared[$key][$parent][$vars_name] = $match_vars[$key][2][$vars_key];
            }
          }
        }
      }

      // Just assigning parameters as false if not already set on the $prepared array.
      // Really just to avoid declaration warnings when we call swf and swf_list
      if (count($prepared[$key])) {
        foreach ($swftools_parameters as $swfparameter) {
          if (!isset($prepared[$key][$swfparameter])) {
            $prepared[$key][$swfparameter] = array();
          }
        }
      }

      // Assemble in to an array of options ready to pass
      $options = array();
      $options['params'] = $prepared[$key]['params'];
      $options['flashvars'] = $prepared[$key]['flashvars'];
      $options['othervars'] = $prepared[$key]['othervars'];
      $options['methods'] = $prepared[$key]['methods'];

      // Do not allow override of alternate HTML unless setteings allow it
      if (isset($options['othervars']['html_alt']) && !variable_get('swftools_override_html_alt', 0)) {
        $options['othervars']['html_alt'] = variable_get('swftools_html_alt', SWFTOOLS_DEFAULT_HTML_ALT);
      }

      // Process the filter
      switch ($match[1][$key]) {
        case 'swf':
          $replace = swf($prepared[$key]['file'], $options);
          break;
        case 'swflist':

          // If this filter contains a key called files
          if ($prepared[$key]['files']) {

            // Iterate over the
            foreach ($prepared[$key]['files'] as $name => $filename) {
              if (!$filename) {
                $prepared[$key]['files'][$name] = $name;
              }

              // Put in to proper format for new swftools_prepare_playlist()
              $files[$key][$name]['filepath'] = $prepared[$key]['files'][$name];
            }

            // Get playlist data, but don't determine action if the user specified a player
            $replace = swf($files[$key], $options);
          }
          else {
            $replace = '<!-- No files passed to the playlist -->';
          }
          break;
      }
      $matched[] = $match[0][$key];
      $rewrite[] = $replace;
    }
    return str_replace($matched, $rewrite, $text);
  }
  return $text;
}