You are here

ckeditor_swf.module in CKEditor SWF - Enhanced Flash embedding plugin 6

Same filename and directory in other branches
  1. 6.2 ckeditor_swf.module
  2. 7 ckeditor_swf.module

24.01.2010 Written by Henri MEDOT <henri.medot[AT]absyx[DOT]fr> http://www.absyx.fr

File

ckeditor_swf.module
View source
<?php

/**
 * @file
 * 24.01.2010
 * Written by Henri MEDOT <henri.medot[AT]absyx[DOT]fr>
 * http://www.absyx.fr
 */

/**
 * Implementation of hook_menu().
 */
function ckeditor_swf_menu() {
  $items['ckeditor_swf/getinfo'] = array(
    'page callback' => 'ckeditor_swf_getinfo',
    'access arguments' => array(
      'access ckeditor',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}
function ckeditor_swf_getinfo() {
  $info = @getimagesize(check_url($_REQUEST['src']));
  drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  if ($info) {
    print '{mime:"' . $info['mime'] . '", width:' . $info[0] . ', height:' . $info[1] . '}';
  }
  else {
    print 'null';
  }
  exit;
}

/**
 * Implementation of hook_elements().
 */
function ckeditor_swf_elements() {
  $type = array();
  if (user_access('access ckeditor')) {
    $type['textarea'] = array(
      '#process' => array(
        'ckeditor_swf_process_textarea',
      ),
    );
  }
  return $type;
}
function ckeditor_swf_process_textarea($element) {
  static $added = FALSE;
  if (!$added) {
    $js = drupal_add_js(NULL, 'setting');
    foreach ($js['setting'] as $array) {
      if (isset($array['ckeditor'])) {
        drupal_add_js(array(
          'ckeditor_swf' => array(
            'module_path' => base_path() . drupal_get_path('module', 'ckeditor_swf'),
            'getinfo_path' => url('ckeditor_swf/getinfo'),
          ),
        ), 'setting');
        $added = TRUE;
        break;
      }
    }
  }
  return $element;
}

/**
 * Implementation of hook_filter().
 */
function ckeditor_swf_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
  switch ($op) {
    case 'list':
      return array(
        0 => t('CKEditor SWF Filter'),
      );
    case 'description':
      return t('Converts SWF content markup generated by CKEditor into standards compliant markup.');
    case 'no cache':
      return FALSE;
    case 'prepare':
      return $text;
    case 'process':
      return _ckeditor_swf_filter_process($text);
    default:
      return $text;
  }
}
function _ckeditor_swf_filter_process($text) {

  // Get object tags.
  unset($matches);
  if (!preg_match_all('`</?object\\b.*?>`is', $text, $matches, PREG_OFFSET_CAPTURE)) {
    return $text;
  }

  // Get object elements, ignoring nested ones.
  $elements = array();
  $i = -1;
  $depth = 0;
  foreach ($matches[0] as $match) {
    if (substr($match[0], 0, 2) != '</') {
      if ($depth == 0) {
        $elements[++$i]['start'] = $match;
      }
      $depth++;
    }
    elseif ($depth > 0) {
      $depth--;
      if ($depth == 0) {
        $elements[$i]['end'] = $match;
        $start = $elements[$i]['start'][1] + strlen($elements[$i]['start'][0]);
        $elements[$i]['inner_html'] = substr($text, $start, $match[1] - $start);
      }
    }
  }

  // Parse object elements.
  foreach ($elements as $i => $element) {
    $elements[$i]['attributes'] = ckeditor_swf_parse_attributes($element['start'][0]);

    // Parse param elements.
    $params = array();
    unset($matches);
    if (preg_match_all('`<param\\b.*?>`is', $element['inner_html'], $matches)) {
      foreach ($matches[0] as $match) {
        $attributes = ckeditor_swf_parse_attributes($match);
        $name = strtolower(@$attributes['name']);
        if (!isset($params[$name])) {
          $params[$name] = @$attributes['value'];
        }
      }
    }
    $elements[$i]['params'] = $params;

    // Get alternative content.
    $alt = preg_replace('`</?(?:object|param|embed)\\b.*?>`is', '', $element['inner_html']);
    $alt = preg_replace('`<!--.*?-->`s', '', $alt);
    $alt = ckeditor_swf_trim($alt);
    $elements[$i]['alt'] = $alt;
  }

  // Render standards compliant object elements.
  foreach ($elements as $i => $element) {
    $attributes = $element['attributes'];
    $params = $element['params'];
    unset($attributes['codebase']);
    $output = '<object' . ckeditor_swf_attributes($attributes) . '>' . "\n";
    $output .= ckeditor_swf_params($params);
    $attributes['type'] = 'application/x-shockwave-flash';
    $attributes['data'] = @$params['movie'];
    unset($attributes['classid']);
    unset($attributes['id']);
    unset($params['movie']);
    $output .= '<!--[if !IE]>-->' . "\n";
    $output .= '<object' . ckeditor_swf_attributes($attributes) . '>' . "\n";
    $output .= ckeditor_swf_params($params);
    $output .= '<!--<![endif]-->' . "\n";
    $output .= $element['alt'] . "\n";
    $output .= '<!--[if !IE]>-->' . "\n";
    $output .= '</object>' . "\n";
    $output .= '<!--<![endif]-->' . "\n";
    $output .= '</object>';
    $elements[$i]['output'] = $output;
  }

  // Replace text.
  $start = 0;
  $output = '';
  foreach ($elements as $element) {
    $output .= substr($text, $start, $element['start'][1] - $start);
    $output .= $element['output'];
    $start = $element['end'][1] + strlen($element['end'][0]);
  }
  $output .= substr($text, $start);
  return $output;
}
function ckeditor_swf_parse_attributes($tag) {
  $attributes = array();
  unset($matches);
  if (preg_match_all('`\\b([a-z]+)="(.*?)"`is', $tag, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
      $attributes[strtolower($match[1])] = $match[2];
    }
  }
  return $attributes;
}
function ckeditor_swf_attributes($attributes) {
  $output = '';
  foreach ($attributes as $name => $value) {
    $output .= ' ' . $name . '="' . $value . '"';
  }
  return $output;
}
function ckeditor_swf_params($params) {
  $output = '';
  foreach ($params as $name => $value) {
    $output .= '<param name="' . $name . '" value="' . $value . '" />' . "\n";
  }
  return $output;
}
function ckeditor_swf_trim($str) {
  return trim(preg_replace('`\\s+`', ' ', $str));
}