You are here

function fckeditor_process_textarea in FCKeditor - WYSIWYG HTML editor 5.2

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

This function create the HTML objects required for the FCKeditor

Parameters

$element: A fully populated form elment to add the editor to

Return value

The same $element with extra FCKeditor markup and initialization

File

./fckeditor.module, line 806
FCKeditor - The text editor for Internet - http://www.fckeditor.net Copyright (C) 2003-2007 Frederico Caldeira Knabben

Code

function fckeditor_process_textarea($element) {
  static $is_running = FALSE;
  static $num = 1;
  static $processed_elements = array();
  global $user, $theme, $_fckeditor_configuration, $_fckeditor_js_ids;
  $processed = in_array($element['#id'], $processed_elements);
  if ($processed) {
    return $element;
  }

  //hack for module developers that want to disable FCKeditor on their textareas
  if (key_exists('#wysiwyg', $element) && !$element['#wysiwyg']) {
    return $element;
  }
  if (isset($element['#access']) && !$element['#access']) {
    return $element;
  }

  //skip this one, surely nobody wants WYSIWYG here
  switch ($element['#id']) {
    case 'edit-excl-list':
    case 'edit-simple-incl-list':
    case 'edit-simple-incl-paths':
    case 'edit-simple-incl-fields':
    case 'edit-log':
    case 'edit-excl-fields':
    case 'edit-excl-paths':
    case 'edit-js-conf':
      return $element;
      break;
  }
  $profile = fckeditor_user_get_profile($user);
  if (!$profile) {
    return $element;
  }
  $conf = array();
  $conf = $profile->settings;
  if ($conf['allow_user_conf'] == 't') {
    foreach (array(
      'default',
      'show_toggle',
      'popup',
      'skin',
      'toolbar',
      'expand',
      'width',
      'lang',
      'auto_lang',
    ) as $setting) {
      $conf[$setting] = fckeditor_user_get_setting($user, $profile, $setting);
    }
  }
  if ($conf["popup"] == "t" && $conf["show_toggle"] == "t") {
    $conf["show_toggle"] = "f";
  }

  //old profile info, assume Filtered HTML is enabled
  if (!isset($conf['ss'])) {
    $conf['ss'] = 2;
    $conf['filters']['filter/0'] = 1;
  }
  if (!isset($conf['filters'])) {
    $conf['filters'] = array();
  }
  $themepath = path_to_theme() . '/';
  $host = base_path();
  $enabled = fckeditor_is_enabled($conf['excl_mode'], $conf['excl_fields'], $conf['excl_paths'], $element['#id'], $_GET['q']);
  if ($enabled) {
    $global_profile = fckeditor_profile_load("FCKeditor Global Profile");
    $global_conf = $global_profile->settings;
    if ($global_conf) {
      $enabled = fckeditor_is_enabled($global_conf['excl_mode'], $global_conf['excl_fields'], $global_conf['excl_paths'], $element['#id'], $_GET['q']);
    }
  }
  if (($element['#rows'] > $conf['min_rows'] || $conf['min_rows'] <= 1 && empty($element['#rows'])) && $enabled) {

    // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
    if ($conf["popup"] == "f") {
      $element['#resizable'] = FALSE;
    }

    // only replace textarea when it has enough rows and it is enabled
    $js_id = 'oFCK_' . $num++;
    $fckeditor_on = $conf['default'] == 't' ? 1 : 0;
    $xss_check = 0;

    //it's not a problem when adding new content/comment
    if (arg(1) != "add" && arg(1) != "reply") {
      $_fckeditor_configuration[$element['#id']] = $conf;

      //let FCKeditor know when perform XSS checks auto/manual
      if ($conf['ss'] == 1) {
        $xss_class = 'checkxss1';
      }
      else {
        $xss_class = 'checkxss2';
      }
      if (!isset($element['#attributes']['class'])) {
        $element['#attributes']['class'] = '';
      }
      $element['#attributes']['class'] .= ' ' . $xss_class;
      $xss_check = 1;
    }
    $wysiwyg_link = '<div id="fck_' . $js_id . '"><textarea id="' . $js_id . '" cols="' . $element['#cols'] . '" rows="' . $element['#rows'] . '">' . htmlspecialchars($element['#value']) . '</textarea></div>' . "\n";
    $wysiwyg_link .= "<a href=\"javascript:Toggle('{$js_id}','{$element['#id']}','" . str_replace("'", "\\'", t("Switch to plain text editor")) . "','" . str_replace("'", "\\'", t("Switch to rich text editor")) . "'," . $xss_check . ");\" id=\"switch_{$js_id}\" " . ($fckeditor_on ? "style=\"display:none\"" : "") . ">";
    $wysiwyg_link .= $fckeditor_on ? t("Switch to plain text editor") : t("Switch to rich text editor");
    $wysiwyg_link .= "</a>";
    if (!isset($element['#suffix'])) {
      $element['#suffix'] = "";
    }

    //settings are saved as strings, not booleans
    if ($conf['show_toggle'] == 't') {

      // Make sure to append to #suffix so it isn't completely overwritten
      drupal_add_js('if (Drupal.jsEnabled) {$(document).ready(function() {CreateToggle("' . $element['#id'] . '","' . $js_id . '", ' . $fckeditor_on . ');});}', 'inline');
      $element['#suffix'] .= $wysiwyg_link;
    }

    // setting some variables
    $module_drupal_path = drupal_get_path('module', 'fckeditor');
    $module_full_path = base_path() . $module_drupal_path;

    // get the default drupal files path
    $files_path = base_path() . file_directory_path();

    // module_drupal_path:
    //  'modules/fckeditor' (length=17)
    // module_full_path:
    //  '/drupal5/modules/fckeditor' (length=26)
    // files_path:
    //  '/drupal5/files' (length=14)
    // configured in settings
    $width = $conf['width'];

    // sensible default for small toolbars
    $height = $element['#rows'] * 14 + 140;
    if (!$is_running) {
      drupal_add_js($module_drupal_path . '/fckeditor/fckeditor.js');
      drupal_add_js($module_drupal_path . '/fckeditor.utils.js');
      drupal_add_js(array(
        'basePath' => base_path(),
      ), 'setting');
      $is_running = TRUE;
    }
    $toolbar = $conf['toolbar'];

    //$height += 100; // for larger toolbars
    $force_simple_toolbar = fckeditor_is_enabled(1, $conf['simple_incl_fields'], $conf['simple_incl_paths'], $element['#id'], $_GET['q']);
    if (!$force_simple_toolbar) {
      $force_simple_toolbar = fckeditor_is_enabled(1, $global_conf['simple_incl_fields'], $global_conf['simple_incl_paths'], $element['#id'], $_GET['q']);
    }
    if ($force_simple_toolbar) {
      $toolbar = FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME;
    }
    $textarea_id = $conf['show_toggle'] == 't' ? $js_id : $element['#id'];
    $_fckeditor_js_ids[$element['#id']] = $textarea_id;
    if (!empty($conf['theme_config_js']) && $conf['theme_config_js'] == 't' && file_exists($themepath . 'fckeditor.config.js')) {
      $fckeditor_config_path = $host . $themepath . 'fckeditor.config.js?' . @filemtime($themepath . 'fckeditor.config.js');
    }
    else {
      $fckeditor_config_path = $module_full_path . "/fckeditor.config.js?" . @filemtime($module_drupal_path . "/fckeditor.config.js");
    }
    $js = $js_id . " = new FCKeditor( '" . $textarea_id . "' );\n" . $js_id . ".BasePath\t= '" . $module_full_path . "/fckeditor/';\n" . $js_id . ".Config['CustomConfigurationsPath'] = \"" . $fckeditor_config_path . "\";\n" . $js_id . ".Config['TextareaID'] = \"" . $element['#id'] . "\";";

    //if ($conf['appearance_conf'] == 'f') {
    $js .= "\n" . $js_id . ".ToolbarSet = \"" . $toolbar . "\";\n" . $js_id . ".Config['SkinPath'] = " . $js_id . ".BasePath + \"editor/skins/" . $conf['skin'] . "/\";\n" . $js_id . ".Config['DefaultLanguage'] = \"" . $conf['lang'] . "\";\n" . $js_id . ".Config['AutoDetectLanguage'] = " . ($conf['auto_lang'] == "t" ? "true" : "false") . ";\n" . $js_id . ".Height = \"" . $height . "\";\n" . $js_id . ".Config['ToolbarStartExpanded'] = " . ($conf['expand'] == "t" ? "true" : "false") . ";\n" . $js_id . ".Width = \"" . $width . "\";\n";

    //}

    //if ($conf['output_conf'] == 'f') {
    $js .= "\n" . $js_id . ".Config['EnterMode'] = '" . $conf['enter_mode'] . "';\n" . $js_id . ".Config['ShiftEnterMode'] = \"" . $conf['shift_enter_mode'] . "\";\n" . $js_id . ".Config['FontFormats'] = \"" . str_replace(",", ";", $conf['font_format']) . "\";\n" . $js_id . ".Config['FormatSource'] = " . ($conf['format_source'] == "t" ? "true" : "false") . ";\n" . $js_id . ".Config['FormatOutput'] = " . ($conf['format_output'] == "t" ? "true" : "false") . ";\n";

    //}
    if (function_exists('img_assist_perm')) {
      drupal_add_js("var fckImgAssistPath = '" . base_path() . drupal_get_path('module', 'img_assist') . "';", 'inline');
    }
    if (function_exists('linktocontent_node_menu')) {
      if (!empty($conf['linktoc']) && $conf['linktoc'] != 'p') {
        $js .= $js_id . ".Config['DrupalPathFilter'] = true;\n";
        if ($conf['linktoc'] == 'pn') {
          $js .= $js_id . ".Config['DrupalLinkToContentSelect'] = true;\n";
        }
      }
      $js .= $js_id . ".Config['DrupalPath'] = '" . base_path() . "';\n";
    }

    // integrate IMCE if it exists and is prefered
    if (function_exists('imce_integrate') && variable_get('imce_settings_fck', 0)) {
      imce_integrate('fck');
      $advanced_uploads = 0;
      $basic_uploads = 0;
    }
    else {
      $advanced_uploads = $conf['upload_advanced'] == "t";
      $basic_uploads = $conf['upload_basic'] == "t";
    }

    // add code for filebrowser for users that have access
    if (user_access('allow fckeditor file uploads') == 1) {
      $connector_path = $module_drupal_path . "/fckeditor/editor/filemanager/connectors/php/connector.php";
      if (file_exists($connector_path)) {

        //FCKeditor 2.5 and above
        $connector_path = $module_full_path . "/fckeditor/editor/filemanager/connectors/php/connector.php";
      }
      else {

        //FCKeditor 2.4.3-
        $connector_path = "connectors/php/connector.php";
      }
      $upload_path = $module_drupal_path . "/fckeditor/editor/filemanager/connectors/php/upload.php";
      if (file_exists($upload_path)) {

        //FCKeditor 2.5 and above
        $upload_path = $module_full_path . "/fckeditor/editor/filemanager/connectors/php/upload.php";
      }
      else {

        //FCKeditor 2.4.3-
        $upload_path = "/fckeditor/editor/filemanager/upload/php/upload.php";
      }
      if ($advanced_uploads) {
        $js .= $js_id . ".Config['LinkBrowserURL'] = \"" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Connector=" . $connector_path . "&ServerPath=" . $files_path . "\";\n  " . $js_id . ".Config['ImageBrowserURL'] = \"" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=" . $connector_path . "&ServerPath=" . $files_path . "\";\n  " . $js_id . ".Config['FlashBrowserURL'] = \"" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=" . $connector_path . "&ServerPath=" . $files_path . "\";\n";
      }
      else {
        $js .= $js_id . ".Config['LinkBrowser'] = false;\n" . $js_id . ".Config['ImageBrowser'] = false;\n" . $js_id . ".Config['FlashBrowser'] = false;\n";
      }
      if ($basic_uploads) {
        $js .= $js_id . ".Config['LinkUploadURL'] = \"" . $upload_path . "\";\n  " . $js_id . ".Config['ImageUploadURL'] = \"" . $upload_path . "?Type=Image\";\n  " . $js_id . ".Config['FlashUploadURL'] = \"" . $upload_path . "?Type=Flash\";\n";
      }
      else {
        $js .= $js_id . ".Config['LinkUpload'] = false;\n" . $js_id . ".Config['ImageUpload'] = false;\n" . $js_id . ".Config['FlashUpload'] = false;\n";
      }
      $_SESSION['FCKeditor']['UserFilesPath'] = strtr($profile->settings['UserFilesPath'], array(
        "%f" => file_directory_path(),
        "%u" => $user->uid,
        "%b" => base_path(),
      ));
      $_SESSION['FCKeditor']['UserFilesAbsolutePath'] = strtr($profile->settings['UserFilesAbsolutePath'], array(
        "%f" => file_directory_path(),
        "%u" => $user->uid,
        "%b" => base_path(),
        "%d" => $_SERVER['DOCUMENT_ROOT'],
      ));
      if (variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE) {
        $private_dir = isset($global_profile->settings['private_dir']) ? trim($global_profile->settings['private_dir'], '\\/') : '';
        if (!empty($private_dir)) {
          $private_dir = strtr($private_dir, array(
            '%u' => $user->uid,
          ));
          $_SESSION['FCKeditor']['UserFilesPath'] = url('system/files') . '/' . $private_dir . '/';
          $_SESSION['FCKeditor']['UserFilesAbsolutePath'] = realpath(file_directory_path()) . DIRECTORY_SEPARATOR . $private_dir . DIRECTORY_SEPARATOR;
        }
        else {
          $_SESSION['FCKeditor']['UserFilesPath'] = url('system/files') . '/';
          $_SESSION['FCKeditor']['UserFilesAbsolutePath'] = realpath(file_directory_path()) . DIRECTORY_SEPARATOR;
        }
      }
    }
    else {
      $js .= $js_id . ".Config['LinkBrowser'] = false;\n" . $js_id . ".Config['ImageBrowser'] = false;\n" . $js_id . ".Config['FlashBrowser'] = false;\n" . $js_id . ".Config['LinkUpload'] = false;\n" . $js_id . ".Config['ImageUpload'] = false;\n" . $js_id . ".Config['FlashUpload'] = false;\n";
    }
    if ($conf['js_conf']) {
      $lines = preg_split("/[\n\r]+/", $conf['js_conf']);
      foreach ($lines as $l) {
        if ($l && strlen($l) > 5) {
          $eqpos = strpos($l, "=");
          if (false !== $eqpos) {
            $option = str_replace("FCKConfig.", "", substr($l, 0, $eqpos));
            $js .= "\n" . $js_id . ".Config['" . trim($option) . "'] =" . substr($l, $eqpos + 1);
          }
        }
      }
    }

    // add custom xml stylesheet if it exists
    if ($conf['css_style'] == 'theme') {
      if (file_exists($themepath . 'fckstyles.xml')) {
        $styles_xml_path = $host . $themepath . 'fckstyles.xml';
        $js .= $js_id . ".Config['StylesXmlPath'] = \"" . $styles_xml_path . "\";\n";
      }
    }
    else {
      if ($conf['css_style'] == 'self') {
        $conf['styles_path'] = str_replace("%h%t", "%t", $conf['styles_path']);
        $js .= $js_id . ".Config['StylesXmlPath'] = \"" . str_replace(array(
          '%h',
          '%t',
          '%m',
        ), array(
          $host,
          $host . $themepath,
          $module_drupal_path,
        ), $conf['styles_path']) . "\";\n";
      }
    }

    // add custom stylesheet if configured
    // lets hope it exists but we'll leave that to the site admin
    if ($conf['css_mode'] == 'theme') {
      $css = $themepath . 'style.css';
      $editorcss = "\"";
      if (file_exists($css)) {
        $editorcss .= $host . $css . ",";
      }
      $color_path = variable_get('color_' . $theme . '_stylesheet', NULL);
      if (!empty($color_path)) {
        $editorcss .= $host . $color_path . ",";
      }
      $editorcss .= $module_full_path . "/fckeditor.css\";\n";
      $js .= $js_id . ".Config['EditorAreaCSS'] = " . $editorcss;
    }
    else {
      if ($conf['css_mode'] == 'self') {
        $conf['css_path'] = str_replace("%h%t", "%t", $conf['css_path']);
        $js .= $js_id . ".Config['EditorAreaCSS'] = \"" . str_replace(array(
          '%h',
          '%t',
        ), array(
          $host,
          $host . $themepath,
        ), $conf['css_path']) . "," . $module_full_path . "/fckeditor.css\";";
      }
    }
    drupal_add_js('var ' . $js_id . ';if (Drupal.jsEnabled) {$(document).ready(function() {' . $js . '});}', 'inline');
    if ($conf['popup'] == "t") {

      // Add the script file with the popup open function.
      drupal_add_js($module_drupal_path . '/fckeditor.popup.js');
      $element['#suffix'] .= " <span class=\"fckeditor_popuplink\">(<a href=\"#\" onclick=\"FCKeditor_OpenPopup('" . $module_full_path . "/fckeditor.popup.html?var=" . $js_id . "&el=" . $element['#id'] . "');return false;\">" . t('Open rich editor') . "</a>)</span>";
    }
    else {

      // if no popup mode, add the editor initialization to the footer
      // this obviously needs print($closure) in page.tpl.php
      if ($fckeditor_on) {
        drupal_add_js('if (Drupal.jsEnabled) {$(document).ready(function() {window.setTimeout("FCKeditorReplaceTextarea(\'' . $textarea_id . '\',' . $js_id . ',' . $xss_check . ');",100);});}', 'inline', 'footer');
      }
    }
  }

  // display the field id for administrators
  if (user_access('administer fckeditor')) {
    $element['#suffix'] .= '<div class="textarea-identifier description">' . t('The ID for !excluding this element is: @id - the path is: @path', array(
      '!excluding' => l(t("excluding or including"), 'admin/settings/fckeditor'),
      '@id' => $element['#id'],
      '@path' => $_GET['q'],
    )) . '</div>';
  }
  $processed_elements[] = $element['#id'];
  return $element;
}