fckeditor_process_textarea

Versions
6
fckeditor_process_textarea($element)

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

Code

contrib/fckeditor/fckeditor.module, line 456

<?php
function fckeditor_process_textarea($element) {
  static $is_running = FALSE;
  static $num = 1;
  global $user, $language, $_fckeditor_configuration, $_fckeditor_js_ids;
  $enabled = TRUE;

  //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-log':
      return $element;
      break;
  }

  if (isset($element['#attributes']['disabled']) && $element['#attributes']['disabled'] == 'disabled') {
    return $element;
  }


  $global_profile = fckeditor_profile_load('FCKeditor Global Profile');
  if ($global_profile) {
    $global_conf = $global_profile->settings;
    if ($global_conf) {
      $enabled = fckeditor_is_enabled(empty($global_conf['excl_mode']) ? '0' : $global_conf['excl_mode'], empty($global_conf['excl_regex']) ? '' : $global_conf['excl_regex'], $element['#id'], $_GET['q']);
    }
  }

  if ($enabled) {
    $profile = fckeditor_user_get_profile($user, $element['#id']);
    if ($profile) {
      $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';
      }
    }
    else {
      $enabled = FALSE;
    }
  }

  //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 = fckeditor_path_to_theme() .'/';
  $host = base_path();

  if (!isset($element['#suffix'])) {
    $element['#suffix'] = '';
  }

  // only replace textarea when it has enough rows and it is enabled
  if ($enabled && (($element['#rows'] > $conf['min_rows']) || ($conf['min_rows'] <= 1 && empty($element['#rows'])))) {
    $textarea_id = $element['#id'];

    if (!isset($element['#attributes'])) {
      $element['#attributes'] = array();
    }
    if (!isset($element['#attributes']['class'])) {
      $element['#attributes']['class'] = 'fckeditor';
    }
    else {
      $element['#attributes']['class'] .= ' fckeditor';
    }

    $js_id = 'oFCK_'. $num++;
    $_fckeditor_js_ids[$element['#id']] = $js_id;
    $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';
      }

      $element['#attributes']['class'] .= ' '. $xss_class;
      $xss_check = 1;
    }

    //settings are saved as strings, not booleans
    if ($conf['show_toggle'] == 't') {
      $content = '';
      if (isset($element['#post']['teaser_js'])) {
        $content .= $element['#post']['teaser_js'] .'<!--break-->';
      }
      $content .= $element['#value'];
      $wysiwyg_link = '';
      $wysiwyg_link .= "<a href=\"javascript:Toggle('{$textarea_id}','". str_replace("'", "\\'", t('Switch to plain text editor')) ."','". str_replace("'", "\\'", t('Switch to rich text editor')) ."',". $xss_check .");\" id=\"switch_{$textarea_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>';

      // Make sure to append to #suffix so it isn't completely overwritten
      $element['#suffix'] .= $wysiwyg_link;
    }

    //convert contents to HTML if necessary
    if ($conf['autofixplaintext'] == 't') {
      module_load_include('lib.inc', 'fckeditor');
      if (fckeditor_is_plaintext($element['#value'])) {
        $element['#value'] = _filter_autop($element['#value']);
      }
    }

    // setting some variables
    $module_drupal_path = drupal_get_path('module', 'fckeditor');
    $module_full_path   = $host . $module_drupal_path;
    $editor_path        = fckeditor_path(FALSE);
    $editor_local_path  = fckeditor_path(TRUE);
    // get the default drupal files path
    $files_path         = $host . 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 = intval($element['#rows']) * 14 + 140;

    if (!$is_running) {
      drupal_add_js($module_drupal_path .'/fckeditor.utils.js');
      /* In D6 drupal_add_js() can't add external JS, in D7 use drupal_add_js(...,'external') */
      drupal_set_html_head('<script type="text/javascript" src="'. $editor_path .'/fckeditor.js?I"></script>');
      $is_running = TRUE;
    }

    $toolbar = $conf['toolbar'];
    //$height += 100; // for larger toolbars

    $force_simple_toolbar = fckeditor_is_enabled('1', empty($conf['simple_incl_regex']) ? '' : $conf['simple_incl_regex'], $element['#id'], $_GET['q']);
    if (!$force_simple_toolbar) {
      $force_simple_toolbar = fckeditor_is_enabled('1', empty($global_conf['simple_incl_regex']) ? '' : $global_conf['simple_incl_regex'], $element['#id'], $_GET['q']);
    }
    if ($force_simple_toolbar) {
      $toolbar = FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME;
    }

    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 ."' );
". $js_id .".defaultState = ". (($fckeditor_on && $conf['popup'] == 'f') ? 1 : 0) .";
". $js_id .".BasePath = '". $editor_path ."/';
". $js_id .".DrupalId = '". $js_id ."';
". $js_id .".Config['PluginsPath'] = '". $module_full_path ."/plugins/';
". $js_id .".Config['CustomConfigurationsPath'] = \"". $fckeditor_config_path ."\";
". $js_id .".Config['TextareaID'] = \"". $element['#id'] ."\";
". $js_id .".Config['BodyId'] = \"". $element['#id'] ."\";";

    //if ($conf['appearance_conf'] == 'f') {
    $js .= "\n". $js_id .".ToolbarSet = \"". $toolbar ."\";
". $js_id .".Config['SkinPath'] = ". $js_id .".BasePath + \"editor/skins/". $conf['skin'] ."/\";
". $js_id .".Config['DefaultLanguage'] = \"". $conf['lang'] ."\";
". $js_id .".Config['AutoDetectLanguage'] = ". ($conf['auto_lang']=="t"?"true":"false") .";
". $js_id .".Height = \"". $height ."\";
". $js_id .".Config['ToolbarStartExpanded'] = ". ($conf['expand']=="t"?"true":"false") .";
". $js_id .".Width = \"". $width ."\";\n";
    //}
    //if ($conf['output_conf'] == 'f') {
    $js .= "\n". $js_id .".Config['EnterMode'] = '". $conf['enter_mode'] ."';
". $js_id .".Config['ShiftEnterMode'] = \"". $conf['shift_enter_mode'] ."\";
". $js_id .".Config['FontFormats'] = \"". str_replace(",", ";", $conf['font_format']) ."\";
". $js_id .".Config['FormatSource'] = ". ($conf['format_source']=="t"?"true":"false") .";
". $js_id .".Config['FormatOutput'] = ". ($conf['format_output']=="t"?"true":"false") .";\n";
    //}

    if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
      $js .= $js_id .".Config['ContentLangDirection'] = 'rtl';\n";
    }

    // add code for filebrowser for users that have access
    if (user_access('allow fckeditor file uploads')==1) {
      $filebrowser = !empty($conf['filebrowser']) ? $conf['filebrowser'] : 'none';
      if ($filebrowser == 'imce' && !module_exists('imce')) {
        $filebrowser = 'none';
      }
      if ($filebrowser == 'ib' && !module_exists('imagebrowser')) {
        $filebrowser = 'none';
      }
      if ($filebrowser == 'webfm' && !module_exists('webfm_popup')) {
        $filebrowser = 'none';
      }
      $quickupload = (!empty($conf['quickupload']) && $conf['quickupload'] == 't');

      // load variables used by both quick upload and filebrowser
      // and assure that the $_SESSION variables are loaded
      if ($quickupload || $filebrowser == 'builtin') {
        if (file_exists($editor_local_path ."/editor/filemanager/connectors/php/connector.php")) {
          $connector_path = $editor_path ."/editor/filemanager/connectors/php/connector.php" ;
        }
        elseif (file_exists($editor_local_path ."/editor/filemanager/upload/php/connector.php")) {
          $connector_path = $editor_path ."/editor/filemanager/upload/php/connector.php";
        }

        if (file_exists($editor_local_path ."/editor/filemanager/connectors/php/upload.php")) {
          $upload_path = $editor_path ."/editor/filemanager/connectors/php/upload.php" ;
        }
        elseif (file_exists($editor_local_path ."/editor/filemanager/upload/php/upload.php")) {
          $upload_path = $editor_path ."/editor/filemanager/upload/php/upload.php";
        }

        if (!empty($profile->settings['UserFilesPath'])) $_SESSION['FCKeditor']['UserFilesPath'] = strtr($profile->settings['UserFilesPath'], array("%f" => file_directory_path(), "%u" => $user->uid, "%b" => $host, "%n" => $user->name));
        if (!empty($profile->settings['UserFilesAbsolutePath'])) $_SESSION['FCKeditor']['UserFilesAbsolutePath'] = strtr($profile->settings['UserFilesAbsolutePath'], array("%f" => file_directory_path(), "%u" => $user->uid, "%b" => base_path(), "%d" => $_SERVER['DOCUMENT_ROOT'], "%n" => $user->name));
        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, '%n' => $user->name));
            $_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;
          }
        }
      }

      if ($quickupload) {
        $js .= $js_id .".Config['LinkUpload'] = true;\n";
        $js .= $js_id .".Config['ImageUpload'] = true;\n";
        $js .= $js_id .".Config['FlashUpload'] = true;\n";
        $js .= $js_id .".Config['LinkUploadURL'] = '". $upload_path ."';\n";
        $js .= $js_id .".Config['ImageUploadURL'] = '". $upload_path ."?Type=Image';\n";
        $js .= $js_id .".Config['FlashUploadURL'] = '". $upload_path ."?Type=Flash';\n";
      }
      else {
        $js .= $js_id .".Config['LinkUpload'] = false;\n";
        $js .= $js_id .".Config['ImageUpload'] = false;\n";
        $js .= $js_id .".Config['FlashUpload'] = false;\n";
      }

      switch ($filebrowser) {
        case 'imce':
          $js .= $js_id .".Config['LinkBrowser']= true;\n";
          $js .= $js_id .".Config['ImageBrowser']= true;\n";
          $js .= $js_id .".Config['FlashBrowser']= true;\n";
          $js .= $js_id .".Config['LinkBrowserURL']= '". $host ."index.php?q=imce&app=FCKEditor|url@txtLnkUrl,txtUrl';\n";
          $js .= $js_id .".Config['ImageBrowserURL']= '". $host ."index.php?q=imce&app=FCKEditor|url@txtUrl|width@txtWidth|height@txtHeight';\n";
          $js .= $js_id .".Config['FlashBrowserURL']= '". $host ."index.php?q=imce&app=FCKEditor|url@txtUrl';\n";
          break;

        case 'webfm':
          $js .= $js_id .".Config['LinkBrowser']= true;\n";
          $js .= $js_id .".Config['ImageBrowser']= true;\n";
          $js .= $js_id .".Config['FlashBrowser']= true;\n";
          $js .= $js_id .".Config['ImageDlgHideLink']= true;\n";
          $js .= $js_id .".Config['LinkBrowserURL']= '". $host ."index.php?q=webfm_popup&url=txtUrl';\n";
          $js .= $js_id .".Config['ImageBrowserURL']= '". $host ."index.php?q=webfm_popup&url=txtUrl';\n";
          $js .= $js_id .".Config['FlashBrowserURL']= '". $host ."index.php?q=webfm_popup&url=txtUrl';\n";
          break;

        case 'builtin':
          $js .= $js_id .".Config['LinkBrowser'] = true;\n";
          $js .= $js_id .".Config['ImageBrowser'] = true;\n";
          $js .= $js_id .".Config['FlashBrowser'] = true;\n";
          $js .= $js_id .".Config['LinkBrowserURL'] = '". $editor_path ."/editor/filemanager/browser/default/browser.html?Connector=". $connector_path ."&ServerPath=". $files_path ."';\n";
          $js .= $js_id .".Config['ImageBrowserURL'] = '". $editor_path ."/editor/filemanager/browser/default/browser.html?Type=Image&Connector=". $connector_path ."&ServerPath=". $files_path ."';\n";
          $js .= $js_id .".Config['FlashBrowserURL'] = '". $editor_path ."/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=". $connector_path ."&ServerPath=". $files_path ."';\n";
          break;

        case 'ib':
          $js .= $js_id .".Config['ImageBrowser']= true;\n";
          $js .= $js_id .".Config['LinkBrowser']= true;\n";
          $js .= $js_id .".Config['FlashBrowser']= false;\n";
          $js .= $js_id .".Config['ImageBrowserURL']= '". $host ."index.php?q=imagebrowser/view/browser&app=FCKEditor';\n";
          $js .= $js_id .".Config['LinkBrowserURL']= '". $host ."index.php?q=imagebrowser/view/browser&app=FCKEditor';\n";
          $js .= $js_id .".Config['ImageBrowserWindowWidth']= '680';";
          $js .= $js_id .".Config['ImageBrowserWindowHeight'] = '439';";
          $js .= $js_id .".Config['LinkBrowserWindowWidth']= '680';";
          $js .= $js_id .".Config['LinkBrowserWindowHeight'] = '439';";
          break;

        default:
        case 'none':
          $js .= $js_id .".Config['LinkBrowser'] = false;\n";
          $js .= $js_id .".Config['ImageBrowser'] = false;\n";
          $js .= $js_id .".Config['FlashBrowser'] = false;\n";
          break;
      }
    }
    else {
      $js .= $js_id .".Config['LinkBrowser'] = false;\n";
      $js .= $js_id .".Config['ImageBrowser'] = false;\n";
      $js .= $js_id .".Config['FlashBrowser'] = false;\n";
      $js .= $js_id .".Config['LinkUpload'] = false;\n";
      $js .= $js_id .".Config['ImageUpload'] = false;\n";
      $js .= $js_id .".Config['FlashUpload'] = false;\n";
    }

    if (!empty($conf['js_conf'])) {
      $lines = preg_split("/[\n\r]+/", $conf['js_conf']);
      foreach ($lines as $l) {
        if (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 (!empty($conf['css_style']) && $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";
      }
    }
    elseif (!empty($conf['css_style']) && $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 xml templae if it exists
    if (!empty($conf['templatefile_mode']) && $conf['templatefile_mode'] == 'theme') {
      if (file_exists($themepath .'fcktemplates.xml')) {
        $styles_xml_path = $host . $themepath .'fcktemplates.xml';
        $js .= $js_id .".Config['TemplatesXmlPath'] = \"". $styles_xml_path ."\";\n";
      }
    }
    elseif (!empty($conf['templatefile_mode']) && $conf['templatefile_mode'] == 'self') {
      $conf['templatefile_path'] = str_replace("%h%t", "%t", $conf['templatefile_path']);
      $js .=  $js_id .".Config['TemplatesXmlPath'] = \"". str_replace(array('%h', '%t', '%m'), array($host, $host . $themepath, $module_drupal_path), $conf['templatefile_path']) ."\";\n";
    }

    // add custom stylesheet if configured
    // lets hope it exists but we'll leave that to the site admin
    $cssfiles = array($module_full_path .'/fckeditor.css');
    switch ($conf['css_mode']) {
      case 'theme':
        global $language, $theme, $theme_info, $base_theme_info;

        $style_css = $themepath .'style.css';
        if (!empty($theme_info->stylesheets)) {
          $css_files = array();
          $editorcss = "\"";
          foreach ($base_theme_info as $base) { // Grab stylesheets from base theme
            if (!empty($base->stylesheets)) { // may be empty when the base theme reference in the info file is invalid
              foreach ($base->stylesheets as $type => $stylesheets) {
                if ($type != "print") {
                  foreach ($stylesheets as $name => $path) {
                    if (file_exists($path)) {
                      $css_files[$name] = $host . $path;
                    }
                  }
                }
              }
            }
          }
          if (!empty($theme_info->stylesheets)) { // Grab stylesheets from current theme
            foreach ($theme_info->stylesheets as $type => $stylesheets) {
              if ($type != "print") {
                foreach ($stylesheets as $name => $path) {
                  if (file_exists($path)) {
                    $css_files[$name] = $host . $path;
                  }
                  elseif (!empty($css_files[$name])) {
                    unset($css_files[$name]);
                  }
                }
              }
            }
          }
          if (!empty($css_files)) {
            $editorcss .= implode(",", $css_files) .",";
          }
          // Grab stylesheets from color module
          $color_paths = variable_get('color_'. $theme .'_stylesheets', array());
          if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
            if (!empty($color_paths[1])) {
              $editorcss .= $host . $color_paths[1] .",";
            }
          }
          elseif (!empty($color_paths[0])) {
            $editorcss .= $host . $color_paths[0] .",";
          }
          $editorcss .= $module_full_path ."/fckeditor.css\";\n";
          $js .=  $js_id .".Config['EditorAreaCSS'] = ". $editorcss;
        }
        elseif (file_exists($style_css)) {
          $js .=  $js_id .".Config['EditorAreaCSS'] = \"". $host . $style_css .",". $module_full_path ."/fckeditor.css\";";
        }
        else {
          $js .=  $js_id .".Config['EditorAreaCSS'] = \"". $module_full_path ."/fckeditor.css\";";
        }
        break;

      case 'self':
        $conf['css_path'] = str_replace("%h%t", "%t", $conf['css_path']);
        $cssfiles[] = str_replace(array('%h', '%t'), array($host, $host . $themepath), $conf['css_path']);
        $js .=  $js_id .".Config['EditorAreaCSS'] = '". implode(',', $cssfiles) ."';\n";
        break;

      case 'none':
        $js .=  $js_id .".Config['EditorAreaCSS'] = ". $js_id .".BasePath + 'editor/css/fck_editorarea.css,' + '". implode(',', $cssfiles) ."';\n";
        break;
    }
    if ($num == 2) {
      $js .= 'var fckInstances = {};';
    }
    $js .= 'fckInstances[\''. $textarea_id .'\'] = '. $js_id .";\n";

    drupal_add_js('var '. $js_id .';if (Drupal.jsEnabled) {'. $js .'}', 'inline');

    if ($conf['popup'] == 't') {
      $element['#suffix'] .= ' <span class="fckeditor_popuplink">(<a href="#" onclick="FCKeditor_OpenPopup(\''. $module_full_path .'/fckeditor.popup.html\', \''. $js_id .'\', \''. $element['#id'] .'\', \''. $conf['width'] .'\'); return false;">'. t('Open rich text editor') ."</a>)</span>";
    }
  }

  // display the field id for administrators
  if (user_access('administer fckeditor') && (!isset($global_conf['show_fieldnamehint']) || $global_conf['show_fieldnamehint'] == 't')) {
    module_load_include('admin.inc', 'fckeditor');

    $element['#suffix'] .= '<div class="textarea-identifier description">'. t('The ID for !excludingorincludinglink this element is %fieldname.', array('!excludingorincludinglink' => l(t('excluding or including'), 'admin/settings/fckeditor'), '%fieldname' => fckeditor_rule_to_string(fckeditor_rule_create(fckeditor_get_nodetype($_GET['q']), $_GET['q'], $element['#id'])))) .'</div>';
  }

  return $element;
}
?>