You are here

function ckeditor_process_textarea in CKEditor - WYSIWYG HTML editor 6

This function creates the HTML objects required for CKEditor.

Parameters

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

Return value

The same $element with extra CKEditor markup and initialization.

2 calls to ckeditor_process_textarea()
ckeditor_get_settings in ./ckeditor.module
ckeditor_process_hidden in ./ckeditor.module
1 string reference to 'ckeditor_process_textarea'
ckeditor_elements in ./ckeditor.module
Implementation of hook_elements(). Replace the textarea with CKEditor using a callback function (ckeditor_process_textarea)

File

./ckeditor.module, line 491
CKEditor - The text editor for the Internet - http://ckeditor.com Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.

Code

function ckeditor_process_textarea($element) {
  static $is_running = FALSE;
  static $num = 1;
  static $processed_ids = array();
  global $user, $theme, $language, $_ckeditor_configuration, $_ckeditor_ids;
  $settings = array();
  $enabled = TRUE;
  $suffix = "";
  $class = "";

  //hack for module developers that want to disable ckeditor 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;
  }
  if (isset($processed_ids[$element['#id']])) {

    //statement for node preview purpose, when second textarea element with the same id is processing to add class which ckeditor behavior must process
    if (empty($element['#attributes']['class'])) {
      $element['#attributes']['class'] = $processed_ids[$element['#id']]['class'];
    }
    else {
      $element['#attributes']['class'] .= " " . $processed_ids[$element['#id']]['class'];
    }
    if (empty($element['#suffix'])) {
      $element['#suffix'] = $processed_ids[$element['#id']]['suffix'];
    }
    else {
      $element['#suffix'] .= $processed_ids[$element['#id']]['suffix'];
    }
    return $element;
  }
  else {
    $processed_ids[$element['#id']] = array();
  }
  module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
  if ($global_profile) {
    $global_conf = $global_profile->settings;
    if ($global_conf) {
      $enabled = ckeditor_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 = ckeditor_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',
          'width',
          'lang',
          'auto_lang',
        ) as $setting) {
          $conf[$setting] = ckeditor_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 = path_to_theme() . '/';
  $host = base_path();
  if (!isset($element['#rows'])) {
    $element['#rows'] = 5;
  }

  // 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'];
    $class = 'ckeditor-mod';
    $_ckeditor_ids[] = $textarea_id;
    $ckeditor_on = $conf['default'] == 't' ? 1 : 0;
    if (!empty($conf['default_state_exception_regex'])) {
      $match = ckeditor_is_enabled(1, $conf['default_state_exception_regex'], $element['#id'], $_GET['q']) xor 1;
      $ckeditor_on = ($ckeditor_on xor $match) ? 1 : 0;
    }
    $xss_check = 0;

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

      //let ckeditor know when perform XSS checks auto/manual
      if ($conf['ss'] == 1) {
        $xss_class = 'checkxss1';
      }
      else {
        $xss_class = 'checkxss2';
      }
      $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-->';
      }
      if (isset($element['#value'])) {
        $content .= $element['#value'];
      }
      $wysiwyg_link = '';
      $wysiwyg_link .= "<a class=\"ckeditor_links\" style=\"display:none\" href=\"javascript:void(0);\" onclick=\"javascript:Drupal.ckeditorToggle('{$textarea_id}','" . str_replace("'", "\\'", t('Switch to plain text editor')) . "','" . str_replace("'", "\\'", t('Switch to rich text editor')) . "'," . $xss_check . ");\" id=\"switch_{$textarea_id}\">";
      $wysiwyg_link .= $ckeditor_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
      $suffix .= $wysiwyg_link;
    }

    // setting some variables
    $module_drupal_path = drupal_get_path('module', 'ckeditor');
    $module_full_path = $host . $module_drupal_path;
    $editor_path = ckeditor_path(FALSE);
    $lib_path = dirname($editor_path);
    $editor_local_path = ckeditor_path(TRUE);
    $lib_local_path = dirname($editor_local_path);

    // get the default drupal files path
    $files_path = $host . file_directory_path();
    drupal_set_html_head('<script type="text/javascript">window.CKEDITOR_BASEPATH = "' . $editor_path . '/";</script>');

    // sensible default for small toolbars
    if (isset($element['#rows'])) {
      $height = intval($element['#rows']) * 14 + 140;
    }
    else {
      $height = 400;
    }
    if (!$is_running) {
      $preprocess = FALSE;
      if (isset($global_profile->settings['ckeditor_aggregate']) && $global_profile->settings['ckeditor_aggregate'] == 't') {
        $preprocess = TRUE;
      }
      drupal_add_js($module_drupal_path . '/includes/ckeditor.utils.js', 'module', variable_get('preprocess_js', FALSE) ? 'header' : 'footer');

      /* In D6 drupal_add_js() can't add external JS, in D7 use drupal_add_js(...,'external') */
      if ($conf['popup'] != 't') {
        if (isset($conf['ckeditor_load_method']) && file_exists($editor_local_path . '/' . $conf['ckeditor_load_method'])) {
          drupal_add_js($editor_local_path . '/' . $conf['ckeditor_load_method'], 'module', 'header', FALSE, TRUE, $preprocess);
          if ($conf['ckeditor_load_method'] == 'ckeditor_basic.js') {
            drupal_add_js('CKEDITOR.loadFullCoreTimeout = ' . $conf['ckeditor_load_time_out'] . ';', 'inline');
            drupal_add_js(array(
              'ckeditor' => array(
                'load_timeout' => TRUE,
              ),
            ), 'setting');
          }
        }
        else {
          drupal_add_js($editor_local_path . '/ckeditor.js', 'module', 'header', FALSE, TRUE, $preprocess);
        }
      }
      else {
        if (file_exists($editor_local_path . '/ckeditor_basic.js')) {
          drupal_add_js($editor_local_path . '/ckeditor_basic.js', 'module', 'header', FALSE, TRUE, $preprocess);
        }
        else {
          drupal_add_js($editor_local_path . '/ckeditor.js', 'module', 'header', FALSE, TRUE, $preprocess);
        }
      }
      drupal_add_js(array(
        'ckeditor' => array(
          'module_path' => $module_full_path,
          'editor_path' => $editor_path,
        ),
      ), 'setting');
      drupal_add_js(array(
        'ckeditor' => array(
          'ajaxToken' => drupal_get_token('ckeditorAjaxCall'),
          'xss_url' => url('ckeditor/xss'),
        ),
      ), 'setting');
      drupal_add_js(array(
        'ckeditor' => array(
          'query' => $_GET['q'],
        ),
      ), 'setting');
      drupal_add_js(array(
        'ckeditor' => array(
          'default_input_format' => variable_get('filter_default_format', 1),
        ),
      ), 'setting');
      $is_running = TRUE;
    }
    $toolbar = $conf['toolbar'];

    //$height += 100; // for larger toolbars
    $force_simple_toolbar = ckeditor_is_enabled('1', empty($conf['simple_incl_regex']) ? '' : $conf['simple_incl_regex'], $element['#id'], $_GET['q']);
    if (!$force_simple_toolbar) {
      $force_simple_toolbar = ckeditor_is_enabled('1', empty($global_conf['simple_incl_regex']) ? '' : $global_conf['simple_incl_regex'], $element['#id'], $_GET['q']);
    }
    if ($force_simple_toolbar) {
      if (!isset($global_conf['simple_toolbar'])) {
        $toolbar = "[ [ 'Format', 'Bold', 'Italic', '-', 'NumberedList','BulletedList', '-', 'Link', 'Unlink', 'Image' ] ]";
      }
      else {
        $toolbar = $global_conf['simple_toolbar'];
      }
    }
    if (!empty($conf['theme_config_js']) && $conf['theme_config_js'] == 't' && file_exists($themepath . 'ckeditor.config.js')) {
      $ckeditor_config_path = $host . $themepath . 'ckeditor.config.js?' . @filemtime($themepath . 'ckeditor.config.js');
    }
    else {
      $ckeditor_config_path = $module_full_path . "/ckeditor.config.js?" . @filemtime($module_drupal_path . "/ckeditor.config.js");
    }
    $settings[$textarea_id]['customConfig'] = $ckeditor_config_path;
    $settings[$textarea_id]['defaultLanguage'] = $conf['lang'];
    $settings[$textarea_id]['toolbar'] = $toolbar;
    $settings[$textarea_id]['enterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['enter_mode']));
    $settings[$textarea_id]['shiftEnterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['shift_enter_mode']));
    $settings[$textarea_id]['toolbarStartupExpanded'] = $conf['expand'] == 't';
    $settings[$textarea_id]['customConfig'] = $ckeditor_config_path;
    $settings[$textarea_id]['width'] = $conf['width'];
    $settings[$textarea_id]['height'] = $height;

    //check if skin exists, if not select default one
    if (file_exists($editor_local_path . '/skins/' . $conf['skin'])) {
      $settings[$textarea_id]['skin'] = $conf['skin'];
    }
    else {
      $settings[$textarea_id]['skin'] = ckeditor_default_skin();
    }
    $settings[$textarea_id]['format_tags'] = $conf['font_format'];
    if (!empty($conf['allowed_content']) && $conf['allowed_content'] === 'f') {
      $settings[$textarea_id]['allowedContent'] = true;
    }
    if (!empty($conf['extraAllowedContent'])) {
      $settings[$textarea_id]['extraAllowedContent'] = $conf['extraAllowedContent'];
    }
    if (isset($conf['language_direction'])) {
      switch ($conf['language_direction']) {
        case 'default':
          if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
            $settings[$textarea_id]['contentsLangDirection'] = 'rtl';
          }
          break;
        case 'ltr':
          $settings[$textarea_id]['contentsLangDirection'] = 'ltr';
          break;
        case 'rtl':
          $settings[$textarea_id]['contentsLangDirection'] = 'rtl';
          break;
      }
    }
    if (isset($conf['loadPlugins'])) {
      $settings[$textarea_id]['loadPlugins'] = ckeditor_plugins_render($conf['loadPlugins']);
    }
    else {
      $settings[$textarea_id]['loadPlugins'] = array();
    }

    //add support for divarea plugin from CKE4
    if (isset($conf['use_divarea']) && $conf['use_divarea'] == 't' && file_exists($editor_local_path . '/plugins/divarea/plugin.js')) {
      $settings[$textarea_id]['loadPlugins']['divarea'] = array(
        'name' => 'divarea',
        'path' => $editor_path . '/plugins/divarea/',
        'buttons' => FALSE,
        'default' => 'f',
      );
    }
    if (isset($conf['html_entities']) && $conf['html_entities'] == 'f') {
      $settings[$textarea_id]['entities'] = FALSE;
      $settings[$textarea_id]['entities_greek'] = FALSE;
      $settings[$textarea_id]['entities_latin'] = FALSE;
    }
    if (isset($conf['scayt_autoStartup']) && $conf['scayt_autoStartup'] == 't') {
      $settings[$textarea_id]['scayt_autoStartup'] = TRUE;
    }
    else {
      $settings[$textarea_id]['scayt_autoStartup'] = FALSE;
    }
    if ($conf['auto_lang'] == "f") {
      $settings[$textarea_id]['language'] = $conf['lang'];
    }
    if (isset($element['#scayt_language'])) {
      $settings[$textarea_id]['scayt_sLang'] = $element['#scayt_language'];
    }
    if (isset($conf['forcePasteAsPlainText']) && $conf['forcePasteAsPlainText'] == 't') {
      $settings[$textarea_id]['forcePasteAsPlainText'] = TRUE;
    }
    if (isset($conf['custom_formatting']) && $conf['custom_formatting'] == 't') {
      foreach ($conf['formatting']['custom_formatting_options'] as $k => $v) {
        if ($v === 0) {
          $conf['formatting']['custom_formatting_options'][$k] = FALSE;
        }
        else {
          $conf['formatting']['custom_formatting_options'][$k] = TRUE;
        }
      }
      $settings[$textarea_id]['output_pre_indent'] = $conf['formatting']['custom_formatting_options']['pre_indent'];
      unset($conf['formatting']['custom_formatting_options']['pre_indent']);
      $settings[$textarea_id]['custom_formatting'] = $conf['formatting']['custom_formatting_options'];
    }

    // add code for filebrowser for users that have access
    $filebrowser = !empty($conf['filebrowser']) ? $conf['filebrowser'] : 'none';
    $filebrowser_image = !empty($conf['filebrowser_image']) ? $conf['filebrowser_image'] : $filebrowser;
    $filebrowser_flash = !empty($conf['filebrowser_flash']) ? $conf['filebrowser_flash'] : $filebrowser;
    if ($filebrowser == 'imce' && !module_exists('imce')) {
      $filebrowser = 'none';
    }
    if ($filebrowser == 'tinybrowser' && !module_exists('tinybrowser')) {
      $filebrowser = 'none';
    }
    if ($filebrowser == 'ib' && !module_exists('imagebrowser')) {
      $filebrowser = 'none';
    }
    if ($filebrowser == 'webfm' && !module_exists('webfm_popup')) {
      $filebrowser = 'none';
    }
    if ($filebrowser == 'elfinder' && !module_exists('elfinder')) {
      $filebrowser = 'none';
    }
    if ($filebrowser_image != $filebrowser) {
      if ($filebrowser_image == 'imce' && !module_exists('imce')) {
        $filebrowser_image = $filebrowser;
      }
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
        $filebrowser_image = $filebrowser;
      }
      if ($filebrowser_image == 'ib' && !module_exists('imagebrowser')) {
        $filebrowser_image = $filebrowser;
      }
      if ($filebrowser_image == 'webfm' && !module_exists('webfm_popup')) {
        $filebrowser_image = $filebrowser;
      }
      if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
        $filebrowser_image = $filebrowser;
      }
    }
    if ($filebrowser_flash != $filebrowser) {
      if ($filebrowser_flash == 'imce' && !module_exists('imce')) {
        $filebrowser_flash = $filebrowser;
      }
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
        $filebrowser_flash = $filebrowser;
      }
      if ($filebrowser_flash == 'ib' && !module_exists('imagebrowser')) {
        $filebrowser_flash = $filebrowser;
      }
      if ($filebrowser_flash == 'webfm' && !module_exists('webfm_popup')) {
        $filebrowser_flash = $filebrowser;
      }
      if ($filebrowser_flash == 'elfinder' && !module_exists('elfinder')) {
        $filebrowser_flash = $filebrowser;
      }
    }
    if ($filebrowser == 'ckfinder' || $filebrowser_image == 'ckfinder' || $filebrowser_flash == 'ckfinder') {
      if (user_access('allow CKFinder file uploads')) {
        if (!empty($profile->settings['UserFilesPath'])) {
          $_SESSION['ckeditor']['UserFilesPath'] = strtr($profile->settings['UserFilesPath'], array(
            "%f" => file_directory_path(),
            "%u" => $user->uid,
            "%b" => $host,
            "%n" => $user->name,
          ));
        }
        else {
          $_SESSION['ckeditor']['UserFilesPath'] = strtr('%b%f/', array(
            "%f" => file_directory_path(),
            "%u" => $user->uid,
            "%b" => $host,
            "%n" => $user->name,
          ));
        }
        if (!empty($profile->settings['UserFilesAbsolutePath'])) {
          $_SESSION['ckeditor']['UserFilesAbsolutePath'] = strtr($profile->settings['UserFilesAbsolutePath'], array(
            "%f" => file_directory_path(),
            "%u" => $user->uid,
            "%b" => base_path(),
            "%d" => $_SERVER['DOCUMENT_ROOT'],
            "%n" => $user->name,
          ));
        }
        else {
          $_SESSION['ckeditor']['UserFilesAbsolutePath'] = strtr('%d%b%f/', 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['ckeditor']['UserFilesPath'] = url('system/files') . '/' . $private_dir . '/';
            $_SESSION['ckeditor']['UserFilesAbsolutePath'] = realpath(file_directory_path()) . DIRECTORY_SEPARATOR . $private_dir . DIRECTORY_SEPARATOR;
          }
          else {
            $_SESSION['ckeditor']['UserFilesPath'] = url('system/files') . '/';
            $_SESSION['ckeditor']['UserFilesAbsolutePath'] = realpath(file_directory_path()) . DIRECTORY_SEPARATOR;
          }
        }
      }
    }
    if (in_array('tinybrowser', array(
      $filebrowser,
      $filebrowser_image,
      $filebrowser_flash,
    ))) {
      $popup_win_size = variable_get('tinybrowser_popup_window_size', '770x480');
      if (!preg_match('#\\d+x\\d+#is', $popup_win_size)) {
        $popup_win_size = '770x480';
      }
      $popup_win_size = trim($popup_win_size);
      $popup_win_size = strtolower($popup_win_size);
      $win_size = split('x', $popup_win_size);
    }
    switch ($filebrowser) {
      case 'ckfinder':
        if (user_access('allow CKFinder file uploads')) {
          $ckfinder_full_path = base_path() . ckfinder_path();
          $settings[$textarea_id]['filebrowserBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html';
          $settings[$textarea_id]['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images';
          $settings[$textarea_id]['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Flash';
          $settings[$textarea_id]['filebrowserUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Files';
          $settings[$textarea_id]['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images';
          $settings[$textarea_id]['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Flash';
        }
        break;
      case 'imce':
        $settings[$textarea_id]['filebrowserBrowseUrl'] = $host . "index.php?q=imce&app=ckeditor|sendto@ckeditor_fileUrl|";
        break;
      case 'webfm':
        if (user_access('access webfm')) {
          $settings[$textarea_id]['filebrowserBrowseUrl'] = $host . "index.php?q=webfm_popup&caller=ckeditor";
        }
        break;
      case 'ib':
        if (user_access('browse own images')) {
          $settings[$textarea_id]['filebrowserBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
          $settings[$textarea_id]['filebrowserWindowWidth'] = 700;
          $settings[$textarea_id]['filebrowserWindowHeight'] = 520;
        }
        break;
      case 'tinybrowser':
        $settings[$textarea_id]['filebrowserBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=file";
        $settings[$textarea_id]['filebrowserWindowWidth'] = (int) $win_size[0] + 15;
        $settings[$textarea_id]['filebrowserWindowHeight'] = (int) $win_size[1] + 15;
        break;
      case 'elfinder':
        $settings[$textarea_id]['filebrowserBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
        break;
    }
    if ($filebrowser_image != $filebrowser) {
      switch ($filebrowser_image) {
        case 'ckfinder':
          if (user_access('allow CKFinder file uploads')) {
            $ckfinder_full_path = base_path() . ckfinder_path();
            $settings[$textarea_id]['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images';
            $settings[$textarea_id]['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images';
          }
          break;
        case 'imce':
          $settings[$textarea_id]['filebrowserImageBrowseUrl'] = $host . "index.php?q=imce&app=ckeditor|sendto@ckeditor_fileUrl|";
          break;
        case 'webfm':
          if (user_access('access webfm')) {
            $settings[$textarea_id]['filebrowserImageBrowseUrl'] = $host . "index.php?q=webfm_popup&caller=ckeditor";
          }
          break;
        case 'ib':
          if (user_access('browse own images')) {
            $settings[$textarea_id]['filebrowserImageBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
            $settings[$textarea_id]['filebrowserImageWindowWidth'] = 680;
            $settings[$textarea_id]['filebrowserImageWindowHeight'] = 439;
          }
          break;
        case 'tinybrowser':
          $settings[$textarea_id]['filebrowserImageBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=image";
          $settings[$textarea_id]['filebrowserImageWindowWidth'] = (int) $win_size[0] + 15;
          $settings[$textarea_id]['filebrowserImageWindowHeight'] = (int) $win_size[1] + 15;
          break;
        case 'elfinder':
          $settings[$textarea_id]['filebrowserImageBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
          break;
      }
    }
    if ($filebrowser_flash != $filebrowser) {
      switch ($filebrowser_flash) {
        case 'ckfinder':
          if (user_access('allow CKFinder file uploads')) {
            $ckfinder_full_path = base_path() . ckfinder_path();
            $settings[$textarea_id]['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images';
            $settings[$textarea_id]['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images';
          }
          break;
        case 'imce':
          $settings[$textarea_id]['filebrowserFlashBrowseUrl'] = $host . "index.php?q=imce&app=ckeditor|sendto@ckeditor_fileUrl|";
          break;
        case 'webfm':
          if (user_access('access webfm')) {
            $settings[$textarea_id]['filebrowserFlashBrowseUrl'] = $host . "index.php?q=webfm_popup&caller=ckeditor";
          }
          break;
        case 'ib':
          if (user_access('browse own images')) {
            $settings[$textarea_id]['filebrowserFlashBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
            $settings[$textarea_id]['filebrowserFlashWindowWidth'] = 680;
            $settings[$textarea_id]['filebrowserFlashWindowHeight'] = 439;
          }
          break;
        case 'tinybrowser':
          $settings[$textarea_id]['filebrowserFlashBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=media";
          $settings[$textarea_id]['filebrowserFlashWindowWidth'] = (int) $win_size[0] + 15;
          $settings[$textarea_id]['filebrowserFlashWindowHeight'] = (int) $win_size[1] + 15;
          break;
        case 'elfinder':
          $settings[$textarea_id]['filebrowserFlashBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
          break;
      }
    }
    if (!empty($conf['js_conf'])) {

      //parsing lines with custom configuration
      preg_match_all('#config\\.(\\w+)[\\s]*=[\\s]*(.+?);[\\s]*(?=config\\.|$)#is', preg_replace("/[\n\r]+/", "", $conf['js_conf']), $matches);
      foreach ($matches[2] as $i => $match) {
        if (!empty($match)) {
          $value = trim($match, " ;\n\r\t\0\v");
          if (strcasecmp($value, 'true') == 0) {
            $value = TRUE;
          }
          if (strcasecmp($value, 'false') == 0) {
            $value = FALSE;
          }
          $settings[$textarea_id]["js_conf"][$matches[1][$i]] = $value;
        }
      }
    }
    $settings[$textarea_id]['stylesCombo_stylesSet'] = "drupal:" . $module_full_path . '/ckeditor.styles.js';
    if (!empty($conf['css_style'])) {
      if ($conf['css_style'] == 'theme' && file_exists($themepath . 'ckeditor.styles.js')) {
        $settings[$textarea_id]['stylesCombo_stylesSet'] = "drupal:" . $host . $themepath . 'ckeditor.styles.js';
      }
      elseif (!empty($conf['css_style']) && $conf['css_style'] == 'self') {
        $conf['styles_path'] = str_replace("%h%t", "%t", $conf['styles_path']);
        $settings[$textarea_id]['stylesCombo_stylesSet'] = "drupal:" . str_replace(array(
          '%h',
          '%t',
          '%m',
        ), array(
          $host,
          $host . $themepath,
          $module_drupal_path,
        ), $conf['styles_path']);
      }
    }

    // add custom stylesheet if configured
    // lets hope it exists but we'll leave that to the site admin
    $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
    $css_files = array();
    switch ($conf['css_mode']) {
      case 'theme':
        global $language, $theme_info, $base_theme_info;
        if (!empty($theme_info->stylesheets)) {
          $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 . $query_string;

                      // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
                      if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
                        $rtl_path = substr($path, 0, -4) . "-rtl.css";
                        if (file_exists($rtl_path)) {
                          $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
                        }
                      }
                    }
                  }
                }
              }
            }
          }
          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) {

                  // Checks if less module exists...
                  if (strstr($path, '.less') && module_exists('less')) {
                    $path = 'sites/default/files/less/' . $path;

                    // append the less file path
                    $path = str_replace('.less', '', $path);

                    // remove the .less
                  }
                  if (file_exists($path)) {
                    $css_files[$name] = $host . $path . $query_string;

                    // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
                    if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
                      $rtl_path = substr($path, 0, -4) . "-rtl.css";
                      if (file_exists($rtl_path)) {
                        $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
                      }
                    }
                  }
                  elseif (!empty($css_files[$name])) {
                    unset($css_files[$name]);
                  }
                }
              }
            }
          }

          // Grab stylesheets local.css and local-rtl.css if they exist (fusion based themes)
          if (file_exists($themepath . 'css/local.css')) {
            $css_files[] = $host . $themepath . 'css/local.css' . $query_string;
          }
          if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && file_exists($themepath . 'css/local-rtl.css')) {
            $css_files[] = $host . $themepath . 'css/local-rtl.css' . $query_string;
          }

          // 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])) {
              $css_files[] = $host . $color_paths[1] . $query_string;
            }
          }
          elseif (!empty($color_paths[0])) {
            $css_files[] = $host . $color_paths[0] . $query_string;
          }
        }
        else {
          if (file_exists($themepath . 'style.css')) {
            $css_files[] = $host . $themepath . 'style.css' . $query_string;
          }
        }
        if (file_exists($module_drupal_path . '/ckeditor.css')) {
          $css_files[] = $module_full_path . '/ckeditor.css' . $query_string;
        }
        if (file_exists($themepath . 'ckeditor.css')) {
          $css_files[] = $host . $themepath . 'ckeditor.css' . $query_string;
        }
        break;
      case 'self':
        if (file_exists($module_drupal_path . '/ckeditor.css')) {
          $css_files[] = $module_full_path . '/ckeditor.css' . $query_string;
          if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
            if (file_exists($module_drupal_path . '/ckeditor-rtl.css')) {
              $css_files[] = $module_full_path . '/ckeditor-rtl.css' . $query_string;
            }
          }
        }
        foreach (explode(',', $conf['css_path']) as $css_path) {
          $css_path = trim(str_replace("%h%t", "%t", $css_path));
          $css_files[] = str_replace(array(
            '%h',
            '%t',
          ), array(
            $host,
            $host . $themepath,
          ), $css_path) . $query_string;
        }
        break;
      case 'none':
        if (file_exists($module_drupal_path . '/ckeditor.css')) {
          $css_files[] = $module_full_path . '/ckeditor.css' . $query_string;
          if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
            if (file_exists($module_drupal_path . '/ckeditor-rtl.css')) {
              $css_files[] = $module_full_path . '/ckeditor-rtl.css' . $query_string;
            }
          }
        }
        $css_files[] = $editor_path . '/contents.css' . $query_string;
        break;
    }
    if (isset($conf['ckeditor_load_method']) && $conf['ckeditor_load_method'] == 'ckeditor_source.js') {
      foreach ($css_files as $k => $v) {
        $css_files[$k] = $v . '&t=' . time();
      }
    }
    $settings[$textarea_id]['contentsCss'] = array_values($css_files);
    if ($ckeditor_on) {
      $autostart[$textarea_id] = TRUE;
    }
    if (!empty($conf['uicolor']) && $conf['uicolor'] == "custom" && !empty($conf['uicolor_user'])) {
      $settings[$textarea_id]['uiColor'] = $conf['uicolor_user'];
    }
    if (!empty($conf['uicolor']) && strpos($conf['uicolor'], "color_") === 0) {
      if (function_exists('color_get_palette')) {
        $palette = @color_get_palette($theme, FALSE);

        //[#652274]
        $color = str_replace("color_", "", $conf['uicolor']);
        if (!empty($palette[$color])) {
          $settings[$textarea_id]['uiColor'] = $palette[$color];
        }
      }
    }
    drupal_add_js(array(
      'ckeditor' => array(
        'theme' => $theme,
      ),
    ), 'setting');
    if (!empty($settings)) {
      $_SESSION['cke_get_settings'] = $settings;
      drupal_add_js(array(
        'ckeditor' => array(
          'settings' => $settings,
        ),
      ), 'setting');
    }
    if (!empty($autostart)) {
      drupal_add_js(array(
        'ckeditor' => array(
          'autostart' => $autostart,
        ),
      ), 'setting');
    }
    if ($conf['popup'] == 't') {
      $suffix .= ' <span style="display:none" class="ckeditor_popuplink ckeditor_links">(<a href="#" onclick="return ckeditorOpenPopup(\'' . $textarea_id . '\', \'' . $element['#id'] . '\', \'' . $conf['width'] . '\');">' . t('Open rich text editor') . "</a>)</span>";
    }
  }

  // display the field id for administrators
  if (user_access('administer ckeditor') && (!isset($global_conf['show_fieldnamehint']) || $global_conf['show_fieldnamehint'] == 't')) {
    module_load_include('inc', 'ckeditor', 'includes/ckeditor.admin');
    $suffix .= '<div class="textarea-identifier description">' . t('CKEditor: the ID for <a href="!excluding">excluding or including</a> this element is %fieldname.', array(
      '!excluding' => url('admin/settings/ckeditor'),
      '%fieldname' => ckeditor_rule_to_string(ckeditor_rule_create(ckeditor_get_nodetype($_GET['q']), $_GET['q'], $element['#id'])),
    )) . '</div>';
  }

  // Remember extra information and reuse it during "Preview"
  $processed_ids[$element['#id']]['suffix'] = $suffix;
  $processed_ids[$element['#id']]['class'] = $class;
  if (empty($element['#suffix'])) {
    $element['#suffix'] = $suffix;
  }
  else {
    $element['#suffix'] .= $suffix;
  }
  if (empty($element['#attributes']['class'])) {
    $element['#attributes']['class'] = $class;
  }
  else {
    $element['#attributes']['class'] .= ' ' . $class;
  }

  //hack with patch jquery-ui dialog
  return $element;
}