function fckeditor_process_textarea in FCKeditor - WYSIWYG HTML editor 6
Same name and namespace in other branches
- 5.2 fckeditor.module \fckeditor_process_textarea()
- 5 fckeditor.module \fckeditor_process_textarea()
- 6.2 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
1 string reference to 'fckeditor_process_textarea'
- fckeditor_elements in ./
fckeditor.module - Implementation of textarea Replace textarea with FCKeditor using callback function (fckeditor_process_textarea)
File
- ./
fckeditor.module, line 800 - FCKeditor - The text editor for Internet - http://www.fckeditor.net Copyright (C) 2003-2008 Frederico Caldeira Knabben
Code
function fckeditor_process_textarea($element) {
static $is_running = FALSE;
static $num = 1;
static $id2id = array();
static $processed_elements = array();
global $user, $language, $theme, $theme_info, $base_theme_info, $_fckeditor_configuration, $_fckeditor_js_ids;
$processed = in_array($element['#id'], $processed_elements);
//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':
case 'edit-teaser-js':
return $element;
break;
}
if (isset($element['#attributes']['disabled']) && $element['#attributes']['disabled'] == 'disabled') {
return $element;
}
$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(empty($conf['excl_mode']) ? "" : $conf['excl_mode'], empty($conf['excl_fields']) ? "" : $conf['excl_fields'], empty($conf['excl_paths']) ? "" : $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(empty($global_conf['excl_mode']) ? "" : $global_conf['excl_mode'], empty($global_conf['excl_fields']) ? "" : $global_conf['excl_fields'], empty($global_conf['excl_paths']) ? "" : $global_conf['excl_paths'], $element['#id'], $_GET['q']);
}
}
if (!isset($element['#suffix'])) {
$element['#suffix'] = "";
}
if (($element['#rows'] > $conf['min_rows'] || $conf['min_rows'] <= 1 && empty($element['#rows'])) && $enabled) {
// only replace textarea when it has enough rows and it is enabled
// Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
if ($conf["popup"] == "f") {
$element['#resizable'] = FALSE;
}
if (in_array($element['#id'], $processed_elements)) {
$js_id = $id2id[$element['#id']];
}
else {
$js_id = 'oFCK_' . $num++;
$id2id[$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';
}
if (!isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = '';
}
$element['#attributes']['class'] .= ' ' . $xss_class;
$xss_check = 1;
}
$content = "";
if (isset($element['#post']['teaser_js'])) {
$content .= $element['#post']['teaser_js'] . "<!--break-->";
}
$content .= $element['#value'];
$wysiwyg_link = '<div id="fck_' . $js_id . '"><textarea id="' . $js_id . '" cols="' . $element['#cols'] . '" rows="' . $element['#rows'] . '">' . htmlspecialchars($content) . '</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 ($conf['show_toggle'] == 't' && !$processed) {
drupal_add_js('if (Drupal.jsEnabled) {$(document).ready(function() {CreateToggle("' . $element['#id'] . '","' . $js_id . '", ' . $fckeditor_on . ');});}', 'inline');
}
//settings are saved as strings, not booleans
if ($conf['show_toggle'] == 't') {
// Make sure to append to #suffix so it isn't completely overwritten
$element['#suffix'] .= $wysiwyg_link;
}
// setting some variables
$module_drupal_path = drupal_get_path('module', 'fckeditor');
$module_full_path = $host . $module_drupal_path;
// 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 = $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');
$is_running = TRUE;
}
$toolbar = $conf['toolbar'];
//$height += 100; // for larger toolbars
$force_simple_toolbar = fckeditor_is_enabled(1, empty($conf['simple_incl_fields']) ? "" : $conf['simple_incl_fields'], empty($conf['simple_incl_paths']) ? "" : $conf['simple_incl_paths'], $element['#id'], $_GET['q']);
if (!$force_simple_toolbar) {
$force_simple_toolbar = fckeditor_is_enabled(1, empty($global_conf['simple_incl_fields']) ? "" : $global_conf['simple_incl_fields'], empty($global_conf['simple_incl_paths']) ? "" : $global_conf['simple_incl_paths'], $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");
}
$textarea_id = $conf['show_toggle'] == 't' ? $js_id : $element['#id'];
$_fckeditor_js_ids[$element['#id']] = $textarea_id;
$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')) {
//#275158
drupal_add_js("var fckImgAssistPath = '" . base_path() . drupal_get_path('module', 'img_assist') . "';", 'inline');
}
// 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') {
$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 (!empty($profile->settings['UserFilesPath'])) {
$_SESSION['FCKeditor']['UserFilesPath'] = strtr($profile->settings['UserFilesPath'], array(
"%f" => file_directory_path(),
"%u" => $user->uid,
"%b" => $host,
));
}
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'],
));
}
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;
}
}
}
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 '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'] = '" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Connector=" . $connector_path . "&ServerPath=" . $files_path . "';\n";
$js .= $js_id . ".Config['ImageBrowserURL'] = '" . $module_full_path . "/fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=" . $connector_path . "&ServerPath=" . $files_path . "';\n";
$js .= $js_id . ".Config['FlashBrowserURL'] = '" . $module_full_path . "/fckeditor/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;
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;
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 ($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 (!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 stylesheet if configured
// lets hope it exists but we'll leave that to the site admin
if ($conf['css_mode'] == 'theme') {
$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\";";
}
}
elseif ($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\";";
}
if (!$processed) {
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.
if (!$processed) {
drupal_add_js($module_drupal_path . '/fckeditor.popup.js');
}
if ($element['#id'] == 'edit-body') {
$teasersuffix = '&teaser=edit-teaser-js';
}
else {
$teasersuffix = '';
}
$element['#suffix'] .= '<span class="fckeditor_popuplink">(<a href="#" onclick="FCKeditor_OpenPopup(\'' . $module_full_path . '/fckeditor.popup.html?var=' . $js_id . '&el=' . $element['#id'] . $teasersuffix . '\');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 && !$processed) {
$str = "";
if ($element['#id'] == 'edit-body') {
$str = 'if ($("#edit-teaser-js").size() && $("#edit-teaser-js").val().length){
$("#edit-body").val($("#edit-teaser-js").val() + "<!--break-->" + $("#edit-body").val());
}';
}
drupal_add_js('if (Drupal.jsEnabled) {$(document).ready(function() {if (typeof (' . $js_id . ') != "undefined") { ' . $str . '
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;
}