fckeditor_process_form

Versions
6
fckeditor_process_form(&$form)

Code

contrib/fckeditor/fckeditor.module, line 148

<?php
function fckeditor_process_form(&$form) {
  global $_fckeditor_configuration, $_fckeditor_js_ids;
  static $processed_textareas = array();
  static $found_textareas = array();

  //Skip if:
  // - we're not editing an element
  // - fckeditor is not enabled (configuration is empty)
  if (arg(1) == "add" || arg(1) == "reply" || !count($_fckeditor_configuration)) {
    return $form;
  }

  $fckeditor_filters = array();

  // Iterate over element children; resetting array keys to access last index.
  if (($children = array_values(element_children($form)))) {
    foreach ($children as $index => $item) {
      $element = &$form[$item];

      if (isset($element['#id']) && in_array($element['#id'], array_keys($_fckeditor_js_ids))) {
        $found_textareas[$element['#id']] = &$element;
      }

      // filter_form() always uses the key 'format'. We need a type-agnostic
      // match to prevent false positives. Also, there must have been at least
      // one element on this level.
      if ($item === 'format' && $index > 0) {

        // Make sure we either match a input format selector or input format
        // guidelines (displayed if user has access to one input format only).
        if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) {
          // The element before this element is the target form field.
          $field = &$form[$children[$index - 1]];
          $textarea_id = $field['#id'];
          $js_id = $_fckeditor_js_ids[$textarea_id];

          array_push($processed_textareas, $js_id);

          //search for checkxss1/2 class
          if (empty($field['#attributes']['class']) || strpos($field['#attributes']['class'], "checkxss") === FALSE) {
            continue;
          }

          // Determine the available input formats. The last child element is a
          // link to "More information about formatting options". When only one
          // input format is displayed, we also have to remove formatting
          // guidelines, stored in the child 'format'.
          $formats = element_children($element);

          foreach ($formats as $format_id) {
            $format = !empty($element[$format_id]['#default_value']) ? $element[$format_id]['#default_value'] : $element[$format_id]['#value'];
            break;
          }

          $enabled = filter_list_format($format);
          $fckeditor_filters = array();

          //loop through all enabled filters
          foreach ($enabled as $id => $filter) {
            //but use only that one selected in FCKeditor profile
            if (in_array($id, array_keys($_fckeditor_configuration[$textarea_id]['filters'])) && $_fckeditor_configuration[$textarea_id]['filters'][$id]) {
              if (!isset($fckeditor_filters[$js_id])) {
                $fckeditor_filters[$js_id] = array();
              }
              $fckeditor_filters[$js_id][] = $id ."/". $format;
            }
          }

          //No filters assigned, remove xss class
          if (empty($fckeditor_filters[$js_id])) {
            $field['#attributes']['class'] = preg_replace("/checkxss(1|2)/", "", $field['#attributes']['class']);
          }
          else {
            $field['#attributes']['class'] = strtr($field['#attributes']['class'], array("checkxss1" => "filterxss1", "checkxss2" => "filterxss2"));
          }

          array_pop($formats);
          unset($formats['format']);
        }
        // If this element is 'format', do not recurse further.
        continue;
      }
      // Recurse into children.
      fckeditor_process_form($element);
    }
  }

  //We're in a form
  if (isset($form['#action'])) {
    //some textareas associated with FCKeditor has not been processed
    if (count($processed_textareas) < count($_fckeditor_js_ids)) {
      //loop through all found textfields
      foreach (array_keys($found_textareas) as $id) {
        $element = &$found_textareas[$id];
        //if not processed yet (checkxss class is before final processing)
        if (strpos($element['#attributes']['class'], "checkxss") !== FALSE && !in_array($_fckeditor_js_ids[$element['#id']], $processed_textareas) && !empty($_fckeditor_configuration[$id]['filters']) && array_sum($_fckeditor_configuration[$id]['filters'])) {
          //assign default Filtered HTML to be safe on fields that do not have input format assigned, but only if at least one security filter is enabled in Security settings
          $js_id = $_fckeditor_js_ids[$element['#id']];
          $fckeditor_filters[$js_id][] = "filter/0/1";
          $element['#attributes']['class'] = strtr($element['#attributes']['class'], array("checkxss1" => "filterxss1", "checkxss2" => "filterxss2"));
        }
      }
    }
  }

  if (!empty($fckeditor_filters)) {
    drupal_add_js(array('fckeditor_filters' => $fckeditor_filters), 'setting');
  }

  return $form;
}
?>