web/drupal/modules/fckeditor/fckeditor.module
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 <?php
       
     2 // $Id: fckeditor.module,v 1.20.2.99 2009/03/11 14:29:55 wwalc Exp $
       
     3 /**
       
     4  * FCKeditor - The text editor for Internet - http://www.fckeditor.net
       
     5  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
       
     6  *
       
     7  * == BEGIN LICENSE ==
       
     8  *
       
     9  * Licensed under the terms of any of the following licenses at your
       
    10  * choice:
       
    11  *
       
    12  *  - GNU General Public License Version 2 or later (the "GPL")
       
    13  *    http://www.gnu.org/licenses/gpl.html
       
    14  *
       
    15  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
       
    16  *    http://www.gnu.org/licenses/lgpl.html
       
    17  *
       
    18  *  - Mozilla Public License Version 1.1 or later (the "MPL")
       
    19  *    http://www.mozilla.org/MPL/MPL-1.1.html
       
    20  *
       
    21  * == END LICENSE ==
       
    22  *
       
    23  * @file
       
    24  * FCKeditor Module for Drupal 6.x
       
    25  *
       
    26  * This module allows Drupal to replace textarea fields with FCKeditor.
       
    27  *
       
    28  * This HTML text editor brings to the web many of the powerful functionalities
       
    29  * of known desktop editors like Word. It's really  lightweight and doesn't
       
    30  * require any kind of installation on the client computer.
       
    31  */
       
    32 
       
    33 /**
       
    34  * The name of simplified toolbar which should be forced
       
    35  * Be sure that this toolbar is defined in fckeditor.config.js or fckconfig.js
       
    36  */
       
    37 define('FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME', 'DrupalBasic') ;
       
    38 
       
    39 global $_fckeditor_configuration;
       
    40 global $_fckeditor_js_ids;
       
    41 
       
    42 $_fckeditor_configuration = array();
       
    43 $_fckeditor_js_ids = array();
       
    44 
       
    45 /**
       
    46  * Implementation of hook_help
       
    47  */
       
    48 function fckeditor_help($path, $arg) {
       
    49   switch ($path) {
       
    50     case 'admin/settings/help#description':
       
    51       $output = t("Enables the usage of FCKeditor (WYSIWYG editor) instead of plain text fields.");
       
    52       break;
       
    53     case 'admin/settings/fckeditor':
       
    54       if (!empty($arg[3]) && in_array($arg[3], array("addg", "editg"))) {
       
    55         $output = t("<p>The Global Profile allows you to define settings that are common for all profiles. Values defined in other profiles will be appended to the global configuration. This way you can avoid repeating some of the settings that are usually the same in each profile.</p>");
       
    56         break;
       
    57       }
       
    58       else if (!empty($arg[3]) && in_array($arg[3], array("add", "edit"))) {
       
    59         $output = t("<p>Note: FCKeditor is highly configurable. The most commonly used features are listed below. If you want to take a look at all available settings, open <code>!fckconfig</code> and then customize <code>!fckeditor_config</code> to your needs. This is also the only way to define new toolbar sets. It is advised to not edit <code>fckconfig.js</code> because you may overwrite it accidentally when you update the editor.</p>", array('!fckconfig' => drupal_get_path('module', 'fckeditor') ."/fckeditor/fckconfig.js", '!fckeditor_config' => drupal_get_path('module', 'fckeditor') ."/fckeditor.config.js"));
       
    60         break;
       
    61       }
       
    62       else if (!empty($arg[3]) && in_array($arg[3], array("delete", "deleteg"))) {
       
    63         break;
       
    64       }
       
    65       $output = t("<p>The FCKeditor module allows Drupal to replace textarea fields with a rich text or <acronym title=\"What You See Is What You Get\">WYSIWYG</acronym> editor. This editor brings many of the powerful functionalities of known desktop editors like Word to the web. It's relatively lightweight and doesn't require any kind of installation on the client computer.</p><p>More information about the editor is located at the !fckeditorlink. A small user guide is located at !userguidelink.</p>",
       
    66       array(
       
    67       '!fckeditorlink' => l(t('FCKeditor homepage'), 'http://www.fckeditor.net'),
       
    68       '!userguidelink' => l(t('FCKeditor userguide'), 'http://docs.fckeditor.net/FCKeditor/Users_Guide'))
       
    69       );
       
    70       $output .= t('<p>Profiles can be defined based on user roles. A FCKeditor profile can define which pages receive this FCKeditor capability, what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor functions. It is possible also to define the Global Profile that will hold values that will be appended to all other profiles.</p><p>Lastly, only users with the <code>!access1</code> !permission will be able to use FCKeditor. </p>', array('!permission' => l(t('permission'), 'admin/user/permissions'), '!access1' => t('access fckeditor')));
       
    71       break;
       
    72     case 'admin/help#fckeditor':
       
    73       $output = t("<p>The FCKeditor module allows Drupal to replace textarea fields with a rich text or <acronym title=\"What You See Is What You Get\">WYSIWYG</acronym> editor. This editor brings many of the powerful functionalities of known desktop editors like Word to the web. It's relatively lightweight and doesn't require any kind of installation on the client computer.</p><p>More information is located at the !fckeditorlink. A small user guide is located at !userguidelink.</p>",
       
    74       array(
       
    75       '!fckeditorlink' => l(t('FCKeditor homepage'), 'http://www.fckeditor.net'),
       
    76       '!userguidelink' => l(t('FCKeditor userguide'), 'http://docs.fckeditor.net/FCKeditor/Users_Guide'))
       
    77       );
       
    78       $output .= t('<h3>Configuration</h3><ol><li>Go to the !fckeditorlink and download the latest version of FCKeditor. Then uncompress the contents of the "fckeditor" directory of the downloaded file to %fckeditordir.</li><li>Enable the module as usual from Drupal\'s admin pages.</li><li>Grant permissions for use of FCKeditor in <code>!path2</code><br />Note: to enable the file browser, read also the <i>How to enable the file browser</i> section.</li><li>Under <code>!path1</code>, adjust the fckeditor profiles. In each profile you can choose which textareas will be replaced by FCKeditor, select default toolbar and configure some more advanced settings.</li><li>For the Rich Text Editing to work you also need to configure your !filterlink for the users that may access Rich Text Editing. Either grant those users Full HTML access or use the following: <br /><code>!filter</code>. </li><li>To have a better control over line breaks, you may disable <code>Line break converter</code> in the chosen filter (recommended).</li><li>Modify the fckeditor.config.js file to custom your needs (optional).<br />You may copy the needed configuration lines from the default FCKeditor configuration settings (!fckconfig), the lines in fckeditor.config.js will override most settings.</li></ol>',
       
    79       array(
       
    80       '!fckeditorlink' => l(t('FCKeditor homepage'), 'http://www.fckeditor.net/download'),
       
    81       '%fckeditordir' => base_path() . drupal_get_path('module', 'fckeditor') .'/fckeditor/',
       
    82       '!path1' => l(t('Administer > Site configuration > FCKeditor'), 'admin/settings/fckeditor'),
       
    83       '!path2' => l(t('Administer > User Management > Permissions'), 'admin/user/permissions'),
       
    84       '!filter' => htmlentities('<a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area>
       
    85       <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em>
       
    86       <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote>
       
    87       <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>'),
       
    88       '!fckconfig' => base_path() . drupal_get_path('module', 'fckeditor') .'/fckeditor/fckconfig.js',
       
    89       '!moduledir' => base_path() . drupal_get_path('module', 'fckeditor') .'/fckeditor',
       
    90       '!filterlink' => l(t('filters'), 'admin/settings/filters'))
       
    91       );
       
    92       $output .= t('<h3>Installation troubleshooting</h3><p>If your FCKeditor does not show you must check if all files are extracted correctly. The directory %fckeditordir should have the following files: <code>fckeditor.js, fckconfig.js, fckstyles.xml, fcktemplates.xml</code> and a directory named <code>editor</code>.</p>',
       
    93       array(
       
    94       '%fckeditordir' => base_path() . drupal_get_path('module', 'fckeditor') .'/fckeditor/')
       
    95       );
       
    96       $output .= t('The correct directory structure is as follows: <blockquote><pre>!structure</pre></blockquote>', array(
       
    97       '!structure' => "modules\n   fckeditor\n      <em>fckeditor.module</em>\n      fckeditor\n         _samples\n         editor\n         <em>COPY_HERE.txt</em>\n         <em>fckconfig.js</em>\n         ..."
       
    98       ));
       
    99       $output .= t("<h3>Plugins: Teaser break and Pagebreak</h3><p>By default, FCKeditor module comes with two plugins that can handle teaser break (&lt;!--break--&gt;) and pagebreak (&lt;!--pagebreak--&gt;). You can enable any (or even both) of them.<ol><li>Open <code>!fckeditor.config.js</code> and uncomment these three lines: <pre>!code</pre></li><li>The second step is to add buttons to the toolbar (in the same file). The button names are: <code>DrupalBreak, DrupalPageBreak</code>. For example if you have a toolbar with an array of buttons defined as follows: <pre>!buttons1</pre> simply add those two buttons at the end of array: <pre>!buttons2</pre> (remember about single quotes).</li><li>Note that the &lt;--pagebreak--&gt; tag is not supported by default in Drupal. You should install the <a href=\"!paging\" target=\"_blank\">Paging</a> module to enable the &lt;!--pagebreak--&gt; tag support. Please refer to the Paging module documentation for detailed installation instructions.</li></ol></p>",
       
   100       array(
       
   101       '!fckeditor.config.js' => base_path() . drupal_get_path('module', 'fckeditor') .'/fckeditor.config.js',
       
   102       '!code' => "
       
   103       FCKConfig.PluginsPath = '../../plugins/' ;
       
   104       FCKConfig.Plugins.Add( 'drupalbreak' ) ;
       
   105       FCKConfig.Plugins.Add( 'drupalpagebreak' ) ;
       
   106       ",
       
   107       "!paging" => "http://drupal.org/project/paging",
       
   108       '!buttons1' => "['Image','Flash','Table','Rule','SpecialChar']",
       
   109       '!buttons2' => "['Image','Flash','Table','Rule','SpecialChar', 'DrupalBreak', 'DrupalPageBreak']",
       
   110       ));
       
   111       $output .= t('<h3>Uploading images and files</h3><p>There are three ways of uploading files: by using the built-in file browser, by using modules like !imce, !ib, !webfm or by using the core upload module.</p>',
       
   112       array(
       
   113       '!imce' => l(t('IMCE'), 'http://drupal.org/project/imce'),
       
   114       '!ib' => l(t('Image Browser'), 'http://drupal.org/project/imagebrowser'),
       
   115       '!webfm' => l(t('Web File Manager'), 'http://drupal.org/project/webfm'),
       
   116       )
       
   117       );
       
   118       // the rest is untranslated for the moment
       
   119       $output .= t("<h3>How to enable the file browser</h3><p>The editor gives the end user the flexibility to create a custom file browser that can be integrated on it. The included file browser allows users to view the content of a specific directory on the server and add new content to that directory (create folders and upload files).</p><p><ol><li>To enable file browsing you need to edit the connector configuration file in your fckeditor module directory, the file should be in:<blockquote><code>!config3</code> <br /> (FCKeditor 2.5+)<br /><br /> or <br /><br /><code>!config1</code><br /> and <br /><code>!config2</code> <br /> (FCKeditor 2.3.x - 2.4.x)</blockquote></p><p>In this file(s) you will need to enable the file browser by adding one line that includes file with the special authentication function for Drupal (<code>filemanager.config.php</code>). Add this code: <blockquote><code>!code1</code><br /> (FCKeditor 2.5+)</blockquote> or <blockquote><code>!code2</code> <br /> (FCKeditor 2.3.x - 2.4.x)</blockquote> straight below this line: <blockquote><code>!code3</code></blockquote> The config.php file also holds some other important settings, please take a look at it and adjust it to your needs (optional).</p></li>",
       
   120       array('!config1' => base_path() . drupal_get_path('module', 'fckeditor') ."/fckeditor/editor/filemanager/browser/default/connectors/php/config.php",
       
   121       '!config2' => base_path() . drupal_get_path('module', 'fckeditor') ."/fckeditor/editor/filemanager/upload/php/config.php",
       
   122       '!config3' => base_path() . drupal_get_path('module', 'fckeditor') ."/fckeditor/editor/filemanager/connectors/php/config.php",
       
   123       '!filesdir' => file_directory_path(),
       
   124       '!code1' => 'require_once "../../../../../filemanager.config.php";', //2.5
       
   125       '!code2' => 'require_once "'. str_replace("\\", "\\\\", dirname(__FILE__) . DIRECTORY_SEPARATOR .'filemanager.config.php"'), //2.4
       
   126       '!code3' => "\$Config['UserFilesAbsolutePath'] = '' ;",
       
   127       )
       
   128       );
       
   129       $output .= t("<li>As of Drupal 5.2, additional step is required: locate file named <code>settings.php</code> inside your drupal directory (usually <code>sites/default/settings.php</code>) and set <strong><code>&#36;cookie_domain</code></strong> variable to the appropiate domain (remember to uncomment that line). If you not do this, FCKeditor will claim that file browser is disabled</li>");
       
   130       $output .= t('<li>Enabling file uploads is <strong>a security risk</strong>. That\'s why you have to grant a !link to enable the file browser to certain groups (assign the &quot;!allowupload&quot; permissions).</li>', array('!link' => l(t('separate permission'), 'admin/user/permissions'), "!allowupload" => t("allow fckeditor file uploads")));
       
   131       $output .= t('<li>Lastly, adjust the !fb for each !profile.</li></ol>', array('!fb' => t('File browser settings'), '!profile' => l(t('profile'), 'admin/settings/fckeditor')));
       
   132       $output .= t("<h3>Modules: Image Assist</h3><p>Image Assist can be integrated with FCKeditor. To do this, simply copy the <code>!iaf1</code> file to <code>!iaf2</code>.</p>", array("!iaf1" => drupal_get_path('module', 'fckeditor') ."/img_assist_fckeditor.js", "!iaf2" => drupal_get_path('module', 'img_assist') ."/img_assist_fckeditor.js"));
       
   133 
       
   134       break;
       
   135   }
       
   136   return !empty($output) ? $output : "";
       
   137 }
       
   138 
       
   139 /**
       
   140  * Implementation of hook_perm
       
   141  * Administer -> User management -> Permissions
       
   142  */
       
   143 function fckeditor_perm() {
       
   144   return array('administer fckeditor', 'access fckeditor', 'allow fckeditor file uploads');
       
   145 }
       
   146 
       
   147 
       
   148 /**
       
   149  * Implementation of textarea
       
   150  * Replace textarea with FCKeditor using callback function (fckeditor_process_textarea)
       
   151  */
       
   152 function fckeditor_elements() {
       
   153   $type = array();
       
   154   $type['textfield'] = array(
       
   155     '#process' => array(
       
   156     'fckeditor_process_input'
       
   157   ),
       
   158   );
       
   159   if (user_access('access fckeditor')) {
       
   160     // only roles with permission get the fckeditor
       
   161     if (fckeditor_is_compatible_client()) {
       
   162       // it would be useless to dig deeper if we're not able or allowed to
       
   163       $type['textarea'] = array('#process' => array('fckeditor_process_textarea'));
       
   164       $type['form'] = array('#after_build' => array('fckeditor_process_form'));
       
   165     }
       
   166   }
       
   167   return $type;
       
   168 }
       
   169 
       
   170 function fckeditor_process_form(&$form) {
       
   171   global $_fckeditor_configuration, $_fckeditor_js_ids;
       
   172   static $processed_textareas = array();
       
   173   static $found_textareas = array();
       
   174 
       
   175   //Skip if:
       
   176   // - we're not editing an element
       
   177   // - fckeditor is not enabled (configuration is empty)
       
   178   if (arg(1) == "add" || arg(1) == "reply" || !count($_fckeditor_configuration)) {
       
   179     return $form;
       
   180   }
       
   181 
       
   182   $fckeditor_filters = array();
       
   183 
       
   184   // Iterate over element children; resetting array keys to access last index.
       
   185   if ($children = array_values(element_children($form))) {
       
   186     foreach ($children as $index => $item) {
       
   187       $element = &$form[$item];
       
   188 
       
   189       if (isset($element['#id']) && in_array($element['#id'], array_keys($_fckeditor_js_ids))) {
       
   190         $found_textareas[$element['#id']] = &$element;
       
   191       }
       
   192 
       
   193       // filter_form() always uses the key 'format'. We need a type-agnostic
       
   194       // match to prevent false positives. Also, there must have been at least
       
   195       // one element on this level.
       
   196       if ($item === 'format' && $index > 0) {
       
   197 
       
   198         // Make sure we either match a input format selector or input format
       
   199         // guidelines (displayed if user has access to one input format only).
       
   200         if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) {
       
   201           // The element before this element is the target form field.
       
   202           $field = &$form[$children[$index - 1]];
       
   203           $textarea_id = $field['#id'];
       
   204           $js_id = $_fckeditor_js_ids[$textarea_id];
       
   205 
       
   206           array_push($processed_textareas, $js_id);
       
   207 
       
   208           //search for checkxss1/2 class
       
   209           if (empty($field['#attributes']['class']) || strpos($field['#attributes']['class'], "checkxss") === FALSE) {
       
   210             continue;
       
   211           }
       
   212 
       
   213           // Determine the available input formats. The last child element is a
       
   214           // link to "More information about formatting options". When only one
       
   215           // input format is displayed, we also have to remove formatting
       
   216           // guidelines, stored in the child 'format'.
       
   217           $formats = element_children($element);
       
   218 
       
   219           foreach ($formats as $format_id) {
       
   220             $format = !empty($element[$format_id]['#default_value']) ? $element[$format_id]['#default_value'] : $element[$format_id]['#value'];
       
   221             break;
       
   222           }
       
   223 
       
   224           $enabled = filter_list_format($format);
       
   225           $fckeditor_filters = array();
       
   226 
       
   227           //loop through all enabled filters
       
   228           foreach ($enabled as $id => $filter) {
       
   229             //but use only that one selected in FCKeditor profile
       
   230             if (in_array($id, array_keys($_fckeditor_configuration[$textarea_id]['filters']))) {
       
   231               if (!isset($fckeditor_filters[$js_id])) {
       
   232                 $fckeditor_filters[$js_id] = array();
       
   233               }
       
   234               $fckeditor_filters[$js_id][] = $id ."/". $format;
       
   235             }
       
   236           }
       
   237 
       
   238           //No filters assigned, remove xss class
       
   239           if (empty($fckeditor_filters[$js_id])) {
       
   240             $field['#attributes']['class'] = preg_replace("/checkxss(1|2)/", "", $field['#attributes']['class']);
       
   241           }
       
   242           else {
       
   243             $field['#attributes']['class'] = strtr($field['#attributes']['class'], array("checkxss1" => "filterxss1", "checkxss2" => "filterxss2"));
       
   244           }
       
   245 
       
   246           array_pop($formats);
       
   247           unset($formats['format']);
       
   248         }
       
   249         // If this element is 'format', do not recurse further.
       
   250         continue;
       
   251       }
       
   252       // Recurse into children.
       
   253       fckeditor_process_form($element);
       
   254     }
       
   255   }
       
   256 
       
   257   //We're in a form
       
   258   if (isset($form['#action'])) {
       
   259     //some textareas associated with FCKeditor has not been processed
       
   260     if (count($processed_textareas) < count($_fckeditor_js_ids)) {
       
   261       //loop through all found textfields
       
   262       foreach (array_keys($found_textareas) as $id) {
       
   263         $element = &$found_textareas[$id];
       
   264         //if not processed yet (checkxss class is before final processing)
       
   265         if (strpos($element['#attributes']['class'], "checkxss") !== FALSE && !in_array($_fckeditor_js_ids[$element['#id']], $processed_textareas) && !empty($_fckeditor_configuration[$id]['filters'])) {
       
   266           //assign default Filtered HTML to be safe on fields that do not have input format assigned, but only if at least one security filter is enabled in Security settings
       
   267           $js_id = $_fckeditor_js_ids[$element['#id']];
       
   268           $fckeditor_filters[$js_id][] = "filter/0/1";
       
   269           $element['#attributes']['class'] = strtr($element['#attributes']['class'], array("checkxss1" => "filterxss1", "checkxss2" => "filterxss2"));
       
   270         }
       
   271       }
       
   272     }
       
   273   }
       
   274 
       
   275   if (!empty($fckeditor_filters)) {
       
   276     drupal_add_js(array('fckeditor_filters' => $fckeditor_filters), 'setting');
       
   277   }
       
   278 
       
   279   return $form;
       
   280 }
       
   281 
       
   282 /**
       
   283  * Allow more than 255 chars in Allowed HTML tags textfield
       
   284  *
       
   285  */
       
   286 function fckeditor_process_input($element) {
       
   287   if ($element['#id']=='edit-allowed-html-1') {
       
   288     $element['#maxlength'] = max($element['#maxlength'], 1024);
       
   289   }
       
   290   return $element;
       
   291 }
       
   292 
       
   293 /**
       
   294  * Add link to FCKeditor configuration in "Administer ->  Site configuration" section
       
   295  *
       
   296  */
       
   297 function fckeditor_menu() {
       
   298 
       
   299   $items = array();
       
   300 
       
   301   $items['fckeditor/xss'] = array(
       
   302     'title' => 'XSS Filter',
       
   303     'description' => 'XSS Filter.',
       
   304     'page callback' => 'fckeditor_filter_xss',
       
   305     'access arguments' => array('access fckeditor'),
       
   306     'type' => MENU_CALLBACK,
       
   307   );
       
   308 
       
   309   $items['admin/settings/fckeditor'] = array(
       
   310   'title' => 'FCKeditor',
       
   311   'description' => 'Configure the rich editor.',
       
   312   'page callback' => 'fckeditor_admin',
       
   313   'access arguments' => array('administer fckeditor'),
       
   314   'type' => MENU_NORMAL_ITEM,
       
   315   );
       
   316 
       
   317   return $items;
       
   318 }
       
   319 
       
   320 /**
       
   321  * AJAX callback - XSS filter
       
   322  */
       
   323 function fckeditor_filter_xss() {
       
   324   $GLOBALS['devel_shutdown'] = FALSE;
       
   325 
       
   326   if (!isset($_POST['text']) || !is_string($_POST['text']) || !is_array($_POST['filters'])) {
       
   327     exit;
       
   328   }
       
   329 
       
   330   $text = $_POST['text'];
       
   331   $text = strtr($text, array('<!--' => '__COMMENT__START__', '-->' => '__COMMENT__END__'));
       
   332 
       
   333   foreach ($_POST['filters'] as $module_delta) {
       
   334     $module = strtok($module_delta, "/");
       
   335     $delta = strtok("/");
       
   336     $format = strtok("/");
       
   337 
       
   338     if (!module_hook($module, 'filter')) {
       
   339       continue;
       
   340     }
       
   341 
       
   342     //built-in filter module, a special case where we would like to strip XSS and nothing more
       
   343     if ($module == 'filter' && $delta == 0) {
       
   344       preg_match_all("|</?([a-z][a-z0-9]*)(?:\b[^>]*)>|i", $text, $matches);
       
   345       if ($matches[1]) {
       
   346         $tags = array_unique($matches[1]);
       
   347         $text = filter_xss($text, $tags);
       
   348       }
       
   349     }
       
   350     else {
       
   351       $text = module_invoke($module, 'filter', 'process', $delta, $format, $text);
       
   352     }
       
   353   }
       
   354 
       
   355   $text = strtr($text, array('__COMMENT__START__' => '<!--', '__COMMENT__END__' => '-->'));
       
   356 
       
   357   echo $text;
       
   358   exit;
       
   359 }
       
   360 
       
   361 //Remove a profile from the database.
       
   362 function fckeditor_profile_delete($name) {
       
   363   db_query("DELETE FROM {fckeditor_settings} WHERE name = '%s'", $name);
       
   364   db_query("DELETE FROM {fckeditor_role} WHERE name = '%s'", $name);
       
   365 }
       
   366 
       
   367 /**
       
   368  * Profile validation.
       
   369  */
       
   370 function fckeditor_profile_validate($edit) {
       
   371   $errors = array();
       
   372 
       
   373   //include mode and all other fields are empty, invalid
       
   374   if ($edit['excl_mode'] == 1 && !$edit['excl_fields'] && !$edit['excl_paths']) {
       
   375     $errors['excl_mode'] = t('Include mode selected, but no fields/paths given. Enter at least one path or field where FCKeditor should appear.');
       
   376   }
       
   377 
       
   378   if (!preg_match("/^\d+$/", trim($edit['min_rows']))) {
       
   379     $errors['min_rows'] = t('Minimum rows must be a valid number');
       
   380   }
       
   381 
       
   382   if ($edit['default'] == 't' && $edit['popup'] == 't') {
       
   383     $errors['popup'] = t('If FCKeditor is enabled by default, popup window must be disabled.');
       
   384   }
       
   385 
       
   386   if ($edit['show_toggle'] == 't' && $edit['popup'] == 't') {
       
   387     $errors['popup'] = t('If toggle is enabled, popup window must be disabled.');
       
   388   }
       
   389 
       
   390   if (!$edit['name']) {
       
   391     $errors['name'] = t('You must give a profile name.');
       
   392   }
       
   393 
       
   394   if (!preg_match("/^\d+%?$/", $edit['width'])) {
       
   395     $errors['width'] = t('Enter valid width. Ex: 400 or 100%');
       
   396   }
       
   397 
       
   398   if (!empty($edit['css_path'])) {
       
   399     if ($edit['css_mode'] != 'self') {
       
   400       $errors['css_path'] = t('CSS path is not empty. Please set the "Editor CSS" option to "define css" mode.');
       
   401     }
       
   402     else if (false !== strpos($edit['css_path'], '"')) {
       
   403       $errors['css_path'] = t('Double quotes are not allowed in CSS path.');
       
   404     }
       
   405     else if (substr($edit['css_path'], 0, 1) == "'" && substr($edit['css_path'], -1) == "'") {
       
   406       $errors['css_path'] = t('Enter valid path, do not surround it with quotes.');
       
   407     }
       
   408   }
       
   409 
       
   410   if (!empty($edit['styles_path'])) {
       
   411     if ($edit['css_style'] != 'self') {
       
   412       $errors['styles_path'] = t('Path to predefined styles is not empty. Please set the "Predefined styles" option to "define path to fckstyles.xml" mode.');
       
   413     }
       
   414     else if (false !== strpos($edit['styles_path'], '"')) {
       
   415       $errors['styles_path'] = t('Double quotes are not allowed in path.');
       
   416     }
       
   417     else if (substr($edit['styles_path'], 0, 1) == "'" && substr($edit['styles_path'], -1) == "'") {
       
   418       $errors['styles_path'] = t('Enter valid path, do not surround it with quotes.');
       
   419     }
       
   420   }
       
   421 
       
   422   if (!empty($edit['font_format'])) {
       
   423     if (!preg_match("/^((p|div|pre|address|h1|h2|h3|h4|h5|h6);)*(p|div|pre|address|h1|h2|h3|h4|h5|h6)$/", $edit['font_format'])) {
       
   424       $errors['font_format'] = t('Enter valid, semicolon separated, list of HTML font formats (no semicolon at the end of list expected).');
       
   425     }
       
   426   }
       
   427 
       
   428   //validate fields
       
   429   $fields = preg_split("/[\s,]+/", strip_tags($edit['excl_fields']));
       
   430   foreach ($fields as $field) {
       
   431     if ($field && !preg_match("/^[a-z]+(\-{1,2}[[:alnum:]]+|\\*|\-\\*)+$/i", $field)) {
       
   432       $errors['excl_fields'] = t("Invalid field specified: %1", array("%1" => $field));
       
   433       break;
       
   434     }
       
   435   }
       
   436 
       
   437   $fields = preg_split("/[\s,]+/", strip_tags($edit['simple_incl_fields']));
       
   438   foreach ($fields as $field) {
       
   439     if ($field && !preg_match("/^[a-z]+(\-{1,2}[[:alnum:]]+|\\*|\-\\*)+$/i", $field)) {
       
   440       $errors['simple_incl_fields'] = t("Invalid field specified: %1", array("%1" => $field));
       
   441       break;
       
   442     }
       
   443   }
       
   444 
       
   445   //validate paths
       
   446   $paths = preg_split("/[\s,]+/", strip_tags($edit['excl_paths']));
       
   447   foreach ($paths as $path) {
       
   448     if ($path && !preg_match("|^[_a-z0-9-\*/]*$|i", $path)) {
       
   449       $errors['excl_paths'] = t("Invalid path specified: %1", array("%1" => $path));
       
   450       break;
       
   451     }
       
   452   }
       
   453 
       
   454   $paths = preg_split("/[\s,]+/", strip_tags($edit['simple_incl_paths']));
       
   455   foreach ($paths as $path) {
       
   456     if ($path && !preg_match("|^[_a-z0-9-\*/]*$|i", $path)) {
       
   457       $errors['simple_incl_paths'] = t("Invalid path specified: %1", array("%1" => $path));
       
   458       break;
       
   459     }
       
   460   }
       
   461 
       
   462   if (variable_get('file_downloads', '') !== FILE_DOWNLOADS_PRIVATE) {
       
   463     if (!empty($edit['UserFilesAbsolutePath']) && empty($edit['UserFilesPath'])) {
       
   464       $errors['UserFilesPath'] = t("Path to uploaded files is required.");
       
   465     }
       
   466     if (!empty($edit['UserFilesPath']) && empty($edit['UserFilesAbsolutePath'])) {
       
   467       $errors['UserFilesPath'] = t("Absolute path to uploaded files is required.");
       
   468     }
       
   469   }
       
   470 
       
   471   foreach ($errors as $name => $message) {
       
   472     form_set_error($name, $message);
       
   473   }
       
   474 
       
   475   return count($errors) == 0;
       
   476 }
       
   477 
       
   478 /**
       
   479  * Global profile validation.
       
   480  */
       
   481 function fckeditor_global_profile_validate($edit) {
       
   482   $errors = array();
       
   483 
       
   484   //include mode and all other fields are empty, invalid
       
   485   if ($edit['excl_mode'] == 1 && !$edit['excl_fields'] && !$edit['excl_paths']) {
       
   486     $errors['excl_mode'] = t('Include mode selected, but no fields/paths given. Enter at least one path or field where FCKeditor should appear.');
       
   487   }
       
   488 
       
   489   //validate fields
       
   490   $fields = preg_split("/[\s,]+/", strip_tags($edit['excl_fields']));
       
   491   foreach ($fields as $field) {
       
   492     if ($field && !preg_match("/^[a-z]+(\-{1,2}[[:alnum:]]+|\\*|\-\\*)+$/i", $field)) {
       
   493       $errors['excl_fields'] = t("Invalid field specified: %1", array("%1" => $field));
       
   494       break;
       
   495     }
       
   496   }
       
   497 
       
   498   $fields = preg_split("/[\s,]+/", strip_tags($edit['simple_incl_fields']));
       
   499   foreach ($fields as $field) {
       
   500     if ($field && !preg_match("/^[a-z]+(\-{1,2}[[:alnum:]]+|\\*|\-\\*)+$/i", $field)) {
       
   501       $errors['simple_incl_fields'] = t("Invalid field specified: %1", array("%1" => $field));
       
   502       break;
       
   503     }
       
   504   }
       
   505 
       
   506   //validate paths
       
   507   $paths = preg_split("/[\s,]+/", strip_tags($edit['excl_paths']));
       
   508   foreach ($paths as $path) {
       
   509     if ($path && !preg_match("|^[_a-z0-9-\*/]*$|i", $path)) {
       
   510       $errors['excl_paths'] = t("Invalid path specified: %1", array("%1" => $path));
       
   511       break;
       
   512     }
       
   513   }
       
   514 
       
   515   $paths = preg_split("/[\s,]+/", strip_tags($edit['simple_incl_paths']));
       
   516   foreach ($paths as $path) {
       
   517     if ($path && !preg_match("|^[_a-z0-9-\*/]*$|i", $path)) {
       
   518       $errors['simple_incl_paths'] = t("Invalid path specified: %1", array("%1" => $path));
       
   519       break;
       
   520     }
       
   521   }
       
   522 
       
   523   foreach ($errors as $name => $message) {
       
   524     form_set_error($name, $message);
       
   525   }
       
   526 
       
   527   return count($errors) == 0;
       
   528 }
       
   529 
       
   530 /**
       
   531  * Controller for FCKeditor administrative settings.
       
   532  */
       
   533 function fckeditor_admin($arg = NULL) {
       
   534 
       
   535   $module_drupal_path = drupal_get_path('module', 'fckeditor');
       
   536   $fckconfig_file = $module_drupal_path .'/fckeditor/fckconfig.js';
       
   537   if (!file_exists($fckconfig_file)) {
       
   538     drupal_set_message(t('checking for %filename', array('%filename' => $fckconfig_file)));
       
   539     drupal_set_message(
       
   540     t('The FCKeditor component is not installed correctly. Please go to the !fckeditorlink to download the latest version. After that you must extract the files to %modulepath and make sure that the directory %modulesubdir and the file %modulefile exist. Refer to the !readme for more information.',
       
   541     array(
       
   542     '!fckeditorlink' => l(t('FCKeditor homepage'), 'http://www.fckeditor.net/download'),
       
   543     '!readme' => l('readme.txt', 'admin/help/fckeditor'),
       
   544     '%modulepath' => base_path() . $module_drupal_path .'/fckeditor/',
       
   545     '%modulesubdir' => base_path() . $module_drupal_path .'/fckeditor/editor',
       
   546     '%modulefile' => base_path() . $module_drupal_path .'/fckeditor/fckeditor.js')),
       
   547     'error');
       
   548     return FALSE;
       
   549   }
       
   550 
       
   551   $edit = $_POST;
       
   552   $op = isset($_POST['op']) ? $_POST['op'] : "";
       
   553 
       
   554   $op = $arg && !$op ? $arg : $op;
       
   555 
       
   556   switch ($op) {
       
   557     case 'add':
       
   558       $output = fckeditor_profile_form($edit);
       
   559       break;
       
   560 
       
   561     case 'addg':
       
   562       $output = fckeditor_global_profile_form($edit);
       
   563       break;
       
   564 
       
   565     case 'edit':
       
   566       drupal_set_title(t('Edit FCKeditor profile'));
       
   567       $output = fckeditor_profile_form(fckeditor_profile_load(urldecode(arg(4))));
       
   568       break;
       
   569 
       
   570     case 'editg':
       
   571       drupal_set_title(t('Edit FCKeditor profile'));
       
   572       $output = fckeditor_global_profile_form(fckeditor_profile_load("FCKeditor Global Profile"));
       
   573       break;
       
   574 
       
   575     case 'deleteg':
       
   576       $output = fckeditor_ask_delete_confirmation(TRUE);
       
   577       break;
       
   578 
       
   579     case 'delete':
       
   580       $output = fckeditor_ask_delete_confirmation(FALSE, urldecode(arg(4)));
       
   581       break;
       
   582 
       
   583     case 'deleteconfirmed':
       
   584       fckeditor_profile_delete(urldecode(arg(4)));
       
   585       drupal_set_message(t('Deleted profile'));
       
   586       drupal_goto('admin/settings/fckeditor');
       
   587       break;
       
   588 
       
   589     case 'deletegconfirmed':
       
   590       fckeditor_profile_delete("FCKeditor Global Profile");
       
   591       drupal_set_message(t('Deleted Global profile'));
       
   592       drupal_goto('admin/settings/fckeditor');
       
   593       break;
       
   594 
       
   595     case t('Create profile');
       
   596     case t('Update profile');
       
   597     if (fckeditor_profile_validate($edit)) {
       
   598       fckeditor_profile_save($edit);
       
   599       !empty($edit['old_name']) ? drupal_set_message(t('Your FCKeditor profile has been updated.')) : drupal_set_message(t('Your FCKeditor profile has been created.'));
       
   600       drupal_goto('admin/settings/fckeditor');
       
   601     }
       
   602     else {
       
   603       $output = fckeditor_profile_form($edit);
       
   604     }
       
   605     break;
       
   606 
       
   607     case t('Create global profile');
       
   608     case t('Update global profile');
       
   609     if (fckeditor_global_profile_validate($edit)) {
       
   610       $edit['name'] = 'FCKeditor Global Profile';
       
   611       fckeditor_global_profile_save($edit);
       
   612       drupal_set_message(t('FCKeditor global profile has been saved.'));
       
   613       drupal_goto('admin/settings/fckeditor');
       
   614     }
       
   615     else {
       
   616       $output = fckeditor_global_profile_form($edit);
       
   617     }
       
   618     break;
       
   619 
       
   620     default:
       
   621       drupal_set_title(t('FCKeditor settings'));
       
   622       //Check if FCKeditor is installed.
       
   623       $fckeditor_loc = drupal_get_path('module', 'fckeditor') .'/fckeditor/';
       
   624       if (!is_dir($fckeditor_loc)) {
       
   625         drupal_set_message(t('Could not find the FCKeditor engine installed at <strong>!fckeditor-directory</strong>. Please !download, uncompress it and copy the folder into !fckeditor-path.', array('!fckeditor-path' => drupal_get_path('module', 'fckeditor'), '!fckeditor-directory' => $fckeditor_loc, '!download' => l(t("download FCKeditor"), "http://www.fckeditor.net/download"))), 'error');
       
   626       }
       
   627 
       
   628       $access_fckeditor_roles = user_roles(FALSE, 'access fckeditor');
       
   629       if (!$access_fckeditor_roles) {
       
   630         drupal_set_message(t('There is currently no role with the <strong>!access</strong> permission. Visit !acl administration section.',
       
   631         array("!access" => t("access fckeditor"), "!acl" => l(t("Permissions"), "admin/user/permissions"))), "warning");
       
   632       }
       
   633       else {
       
   634         $result = db_query_range("SELECT name FROM {fckeditor_settings} WHERE name<>'FCKeditor Global Profile'", 0, 1);
       
   635         $has_profiles = FALSE;
       
   636         //find profile other than Global
       
   637         if ($obj = db_fetch_object($result)) {
       
   638           $has_profiles = TRUE;
       
   639         }
       
   640 
       
   641         //find roles with profiles
       
   642         $result = db_query("SELECT rid FROM {fckeditor_role}");
       
   643         $rids = array();
       
   644         while ($obj = db_fetch_object($result)) {
       
   645           $rids[] = $obj->rid;
       
   646         }
       
   647         $rids = array_unique($rids);
       
   648         if (!$has_profiles) {
       
   649           drupal_set_message(t("No FCKeditor profiles found. At this moment, nobody is able to use FCKeditor. Create new profile below."), "error");
       
   650         }
       
   651         else {
       
   652           //not all roles with access fckeditor has their FCKeditor profile assigned
       
   653           $diff = array_diff(array_keys($access_fckeditor_roles), $rids);
       
   654           if ($diff) {
       
   655             $list = "<ul>";
       
   656             foreach ($diff as $rid) {
       
   657               $list .= "<li>". $access_fckeditor_roles[$rid] ."</li>";
       
   658             }
       
   659             $list .= "</ul>";
       
   660             drupal_set_message(t("Not all roles with <strong>!access</strong> permission are associated with FCKeditor profiles. As a result, users having the following roles may be unable to use FCKeditor: !list Create new or edit FCKeditor profiles below and in the <strong>Basic setup</strong> section, check &quot;Roles allowed to use this profile&quot;.", array("!access" => l(t("access fckeditor"), "admin/user/permissions"), "!list" => $list)), "warning");
       
   661           }
       
   662         }
       
   663       }
       
   664 
       
   665       $output = fckeditor_profile_overview();
       
   666   }
       
   667 
       
   668   return $output;
       
   669 }
       
   670 
       
   671 /**
       
   672  * Save a profile to the database.
       
   673  * @todo add more entries to array in the user_save line
       
   674  */
       
   675 function fckeditor_profile_save($edit) {
       
   676   db_query("DELETE FROM {fckeditor_settings} WHERE name = '%s' or name = '%s'", $edit['name'], empty($edit['old_name']) ? "" : $edit['old_name']);
       
   677   db_query("DELETE FROM {fckeditor_role} WHERE name = '%s' or name = '%s'", $edit['name'], empty($edit['old_name']) ? "" : $edit['old_name']);
       
   678   db_query("INSERT INTO {fckeditor_settings} (name, settings) VALUES ('%s', '%s')", $edit['name'], serialize($edit));
       
   679   if (!empty($edit['rids']))
       
   680   foreach ($edit['rids'] as $rid => $value) {
       
   681     db_query("INSERT INTO {fckeditor_role} (name, rid) VALUES ('%s', %d)", $edit['name'], $rid);
       
   682   }
       
   683 
       
   684   // if users can't set their own defaults, make sure to remove $user->fckeditor_status so their default doesn't override the main default
       
   685   if (!empty($edit['user_choose']) && $edit['user_choose'] == 'false') {
       
   686     global $user;
       
   687     user_save($user, array('fckeditor_status' => NULL));
       
   688   }
       
   689 }
       
   690 
       
   691 function fckeditor_global_profile_save($edit) {
       
   692   if (isset($edit['rank'])) {
       
   693     $edit['rank'] = explode('>', str_replace(' ', '', $edit['rank']));
       
   694   }
       
   695 
       
   696   db_query("DELETE FROM {fckeditor_settings} WHERE name = '%s' or name = '%s'", $edit['name'], empty($edit['old_name']) ? "" : $edit['old_name']);
       
   697   db_query("DELETE FROM {fckeditor_role} WHERE name = '%s' or name = '%s'", $edit['name'], empty($edit['old_name']) ? "" : $edit['old_name']);
       
   698   db_query("INSERT INTO {fckeditor_settings} (name, settings) VALUES ('%s', '%s')", $edit['name'], serialize($edit));
       
   699 }
       
   700 
       
   701 /**
       
   702  * Controller for fckeditor profiles.
       
   703  */
       
   704 function fckeditor_profile_overview() {
       
   705   $output = '';
       
   706 
       
   707   $profiles = fckeditor_profile_load();
       
   708   if ($profiles) {
       
   709     $roles = user_roles();
       
   710     $access_fckeditor_roles = user_roles(FALSE, 'access fckeditor');
       
   711     $header = array(t('Profile'), t('Roles'), t('Operations'));
       
   712     foreach ($profiles as $p) {
       
   713       $rids = $p->rids;
       
   714       if ($p->name !== "FCKeditor Global Profile") {
       
   715         foreach ($p->rids as $rid => $name) {
       
   716           if (!isset($access_fckeditor_roles[$rid])) {
       
   717             unset($rids[$rid]);
       
   718           }
       
   719         }
       
   720         $rows[] = array(array('data' => $p->name, 'valign' => 'top'), array('data' => implode("<br />\n", $rids)), array('data' => l(t('edit'), 'admin/settings/fckeditor/edit/'. urlencode($p->name)) .' '. l(t('delete'), 'admin/settings/fckeditor/delete/'. urlencode($p->name)), 'valign' => 'top'));
       
   721       }
       
   722     }
       
   723     $output .= "<h3>". t("Profiles") ."</h3>";
       
   724     $output .= theme('table', $header, $rows);
       
   725     $output .= '<p>'. l(t('Create new profile'), 'admin/settings/fckeditor/add') .'</p>';
       
   726   }
       
   727   else {
       
   728     drupal_set_message(t('No profiles found. Click here to !create.', array('!create' => l(t("create a new profile"), 'admin/settings/fckeditor/add'))));
       
   729   }
       
   730 
       
   731   $rows = array();
       
   732   if (!isset($profiles['FCKeditor Global Profile'])) {
       
   733     drupal_set_message(t('Global Profile not found. Click here to !create.', array('!create' => l(t("create the global profile"), 'admin/settings/fckeditor/addg'))));
       
   734   }
       
   735   else {
       
   736     $output .= "<h3>". t("Global Settings") ."</h3>";
       
   737     $rows[] = array(array('data' => t('FCKeditor Global Profile'), 'valign' => 'top'), array('data' => l(t('edit'), 'admin/settings/fckeditor/editg') ." ". l(t('delete'), 'admin/settings/fckeditor/deleteg'), 'valign' => 'top'));
       
   738     $output .= theme('table', array(t('Profile'), t('Operations')), $rows);
       
   739   }
       
   740 
       
   741   return $output;
       
   742 }
       
   743 
       
   744 /**
       
   745  * Load all profiles. Just load one profile if $name is passed in.
       
   746  */
       
   747 function fckeditor_profile_load($name = '') {
       
   748   static $profiles = array();
       
   749 
       
   750   if (!$profiles) {
       
   751     $roles = user_roles();
       
   752     $result = db_query('SELECT * FROM {fckeditor_settings}');
       
   753     while ($data = db_fetch_object($result)) {
       
   754       $data->settings = unserialize($data->settings);
       
   755       $result2 = db_query("SELECT rid FROM {fckeditor_role} WHERE name = '%s'", $data->name);
       
   756       $role = array();
       
   757       while ($r = db_fetch_object($result2)) {
       
   758         $role[$r->rid] = $roles[$r->rid];
       
   759       }
       
   760       $data->rids = $role;
       
   761 
       
   762       $profiles[$data->name] = $data;
       
   763     }
       
   764   }
       
   765 
       
   766   return ($name ? $profiles[$name] : $profiles);
       
   767 }
       
   768 
       
   769 /**
       
   770  * @param int $excl_mode 1/include, exclude otherwise
       
   771  * @param string $excl_fields fields (HTML IDs)
       
   772  * @param string $excl_paths paths (drupal paths)
       
   773  * @param string $element_id current ID
       
   774  * @param string $get_q current path
       
   775  *
       
   776  * @return boolean
       
   777  *    returns true if FCKeditor is enabled
       
   778  */
       
   779 function fckeditor_is_enabled($excl_mode, $excl_fields, $excl_paths, $element_id, $get_q) {
       
   780   $arr_excl_fields = preg_split("/[\s,]+/", strip_tags($excl_fields));
       
   781   $field_found = fckeditor_idsearch($element_id, $arr_excl_fields);
       
   782 
       
   783   $path = drupal_get_path_alias($get_q);
       
   784   $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($excl_paths, '/')) .')$/';
       
   785   $path_found = preg_match($regexp, $path);
       
   786 
       
   787   $found = $field_found || $path_found;
       
   788 
       
   789   $result =  ($excl_mode == 1) ? $found : !$found;
       
   790   return $result;
       
   791 }
       
   792 
       
   793 /**
       
   794  * This function create the HTML objects required for the FCKeditor
       
   795  *
       
   796  * @param $element
       
   797  *   A fully populated form elment to add the editor to
       
   798  * @return
       
   799  *   The same $element with extra FCKeditor markup and initialization
       
   800  */
       
   801 function fckeditor_process_textarea($element) {
       
   802   static $is_running = FALSE;
       
   803   static $num = 1;
       
   804   static $id2id = array();
       
   805   static $processed_elements = array();
       
   806   global $user, $language, $theme, $theme_info, $base_theme_info, $_fckeditor_configuration, $_fckeditor_js_ids;
       
   807 
       
   808   $processed = in_array($element['#id'], $processed_elements);
       
   809 
       
   810   //hack for module developers that want to disable FCKeditor on their textareas
       
   811   if (key_exists('#wysiwyg', $element) && !$element['#wysiwyg']) {
       
   812     return $element;
       
   813   }
       
   814 
       
   815   //skip this one, surely nobody wants WYSIWYG here
       
   816   switch ($element['#id']) {
       
   817     case 'edit-excl-list':
       
   818     case 'edit-simple-incl-list':
       
   819     case 'edit-simple-incl-paths':
       
   820     case 'edit-simple-incl-fields':
       
   821     case 'edit-log':
       
   822     case 'edit-excl-fields':
       
   823     case 'edit-excl-paths':
       
   824     case 'edit-js-conf':
       
   825     case 'edit-teaser-js':
       
   826       return $element;
       
   827       break;
       
   828   }
       
   829 
       
   830   if (isset($element['#attributes']['disabled']) && $element['#attributes']['disabled'] == 'disabled') {
       
   831     return $element;
       
   832   }
       
   833 
       
   834   $profile = fckeditor_user_get_profile($user);
       
   835   if (!$profile) {
       
   836     return $element;
       
   837   }
       
   838 
       
   839   $conf = array();
       
   840   $conf = $profile->settings;
       
   841 
       
   842   if ($conf['allow_user_conf']=='t') {
       
   843     foreach (array('default', 'show_toggle', 'popup', 'skin', 'toolbar', 'expand', 'width', 'lang', 'auto_lang') as $setting) {
       
   844       $conf[$setting] = fckeditor_user_get_setting($user, $profile, $setting);
       
   845     }
       
   846   }
       
   847   if ($conf["popup"]=="t" && $conf["show_toggle"]=="t") {
       
   848     $conf["show_toggle"]="f";
       
   849   }
       
   850 
       
   851   //old profile info, assume Filtered HTML is enabled
       
   852   if (!isset($conf['ss'])) {
       
   853     $conf['ss'] = 2;
       
   854     $conf['filters']['filter/0'] = 1;
       
   855   }
       
   856   if (!isset($conf['filters'])) {
       
   857     $conf['filters'] = array();
       
   858   }
       
   859 
       
   860   $themepath = path_to_theme() .'/';
       
   861   $host = base_path();
       
   862 
       
   863   $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']);
       
   864   if ($enabled) {
       
   865     $global_profile = fckeditor_profile_load("FCKeditor Global Profile");
       
   866     $global_conf = $global_profile->settings;
       
   867     if ($global_conf) {
       
   868       $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']);
       
   869     }
       
   870   }
       
   871   if (!isset($element['#suffix'])) {
       
   872     $element['#suffix'] = "";
       
   873   }
       
   874 
       
   875   if ((($element['#rows'] > $conf['min_rows']) || ($conf['min_rows'] <= 1 && empty($element['#rows']))) && $enabled) {
       
   876     // only replace textarea when it has enough rows and it is enabled
       
   877 
       
   878     // Set resizable to false to avoid drupal.js resizable function from taking control of the textarea
       
   879     if ($conf["popup"]=="f") {
       
   880       $element['#resizable'] = FALSE;
       
   881     }
       
   882 
       
   883     if (in_array($element['#id'], $processed_elements)) {
       
   884       $js_id = $id2id[$element['#id']];
       
   885     }
       
   886     else {
       
   887       $js_id = 'oFCK_'. $num++;
       
   888       $id2id[$element['#id']] = $js_id;
       
   889     }
       
   890     $fckeditor_on = ($conf['default']=='t') ? 1 : 0 ;
       
   891 
       
   892     $xss_check = 0;
       
   893     //it's not a problem when adding new content/comment
       
   894     if (arg(1) != "add" && arg(1) != "reply") {
       
   895       $_fckeditor_configuration[$element['#id']] = $conf;
       
   896 
       
   897       //let FCKeditor know when perform XSS checks auto/manual
       
   898       if ($conf['ss'] == 1) {
       
   899         $xss_class = 'checkxss1';
       
   900       }
       
   901       else {
       
   902         $xss_class = 'checkxss2';
       
   903       }
       
   904 
       
   905       if (!isset($element['#attributes']['class'])) {
       
   906         $element['#attributes']['class'] = '';
       
   907       }
       
   908 
       
   909       $element['#attributes']['class'] .= ' '. $xss_class;
       
   910       $xss_check = 1;
       
   911     }
       
   912 
       
   913     $content = "";
       
   914     if (isset($element['#post']['teaser_js'])) {
       
   915       $content .= $element['#post']['teaser_js'] ."<!--break-->";
       
   916     }
       
   917     $content .= $element['#value'];
       
   918     $wysiwyg_link = "<div id=\"fck_{$js_id}\"><textarea id=\"{$js_id}\">". htmlspecialchars($content) ."</textarea></div>\n";
       
   919     $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\"":"") .">";
       
   920     $wysiwyg_link .= $fckeditor_on ? t("Switch to plain text editor") : t("Switch to rich text editor");
       
   921     $wysiwyg_link .= "</a>";
       
   922     if ($conf['show_toggle'] == 't' && !$processed) {
       
   923       drupal_add_js('if (Drupal.jsEnabled) {$(document).ready(function() {CreateToggle("'. $element['#id'] .'","'. $js_id .'", '. $fckeditor_on .');});}', 'inline');
       
   924     }
       
   925     //settings are saved as strings, not booleans
       
   926     if ($conf['show_toggle'] == 't') {
       
   927       // Make sure to append to #suffix so it isn't completely overwritten
       
   928       $element['#suffix'] .= $wysiwyg_link;
       
   929     }
       
   930     // setting some variables
       
   931     $module_drupal_path = drupal_get_path('module', 'fckeditor');
       
   932     $module_full_path = $host . $module_drupal_path;
       
   933     // get the default drupal files path
       
   934     $files_path = $host . file_directory_path();
       
   935     // module_drupal_path:
       
   936     //  'modules/fckeditor' (length=17)
       
   937     // module_full_path:
       
   938     //  '/drupal5/modules/fckeditor' (length=26)
       
   939     // files_path:
       
   940     //  '/drupal5/files' (length=14)
       
   941     // configured in settings
       
   942     $width = $conf['width'];
       
   943 
       
   944     // sensible default for small toolbars
       
   945     $height = $element['#rows'] * 14 + 140;
       
   946 
       
   947     if (!$is_running) {
       
   948       drupal_add_js($module_drupal_path .'/fckeditor/fckeditor.js');
       
   949       drupal_add_js($module_drupal_path .'/fckeditor.utils.js');
       
   950       $is_running = TRUE;
       
   951     }
       
   952 
       
   953     $toolbar = $conf['toolbar'];
       
   954     //$height += 100; // for larger toolbars
       
   955 
       
   956     $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']);
       
   957     if (!$force_simple_toolbar) {
       
   958       $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']);
       
   959     }
       
   960     if ($force_simple_toolbar) {
       
   961       $toolbar = FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME;
       
   962     }
       
   963 
       
   964     if (!empty($conf['theme_config_js']) && $conf['theme_config_js'] == 't' && file_exists($themepath .'fckeditor.config.js')) {
       
   965       $fckeditor_config_path = $host . $themepath .'fckeditor.config.js?'. @filemtime($themepath .'fckeditor.config.js');
       
   966     }
       
   967     else {
       
   968       $fckeditor_config_path = $module_full_path ."/fckeditor.config.js?". @filemtime($module_drupal_path ."/fckeditor.config.js");
       
   969     }
       
   970 
       
   971     $textarea_id = $conf['show_toggle'] == 't' ? $js_id : $element['#id'];
       
   972     $_fckeditor_js_ids[$element['#id']] = $textarea_id;
       
   973     $js = $js_id ." = new FCKeditor( '". $textarea_id ."' );
       
   974 ". $js_id .".BasePath	= '". $module_full_path ."/fckeditor/';
       
   975 ". $js_id .".Config['CustomConfigurationsPath'] = \"". $fckeditor_config_path ."\";
       
   976 ". $js_id .".Config['TextareaID'] = \"". $element['#id'] ."\";";
       
   977 
       
   978     //if ($conf['appearance_conf'] == 'f') {
       
   979     $js .= "\n". $js_id .".ToolbarSet = \"". $toolbar ."\";
       
   980 ". $js_id .".Config['SkinPath'] = ". $js_id .".BasePath + \"editor/skins/". $conf['skin'] ."/\";
       
   981 ". $js_id .".Config['DefaultLanguage'] = \"". $conf['lang'] ."\";
       
   982 ". $js_id .".Config['AutoDetectLanguage'] = ". ($conf['auto_lang']=="t"?"true":"false") .";
       
   983 ". $js_id .".Height = \"". $height ."\";
       
   984 ". $js_id .".Config['ToolbarStartExpanded'] = ". ($conf['expand']=="t"?"true":"false") .";
       
   985 ". $js_id .".Width = \"". $width ."\";\n";
       
   986     //}
       
   987     //if ($conf['output_conf'] == 'f') {
       
   988     $js .= "\n". $js_id .".Config['EnterMode'] = '". $conf['enter_mode'] ."';
       
   989 ". $js_id .".Config['ShiftEnterMode'] = \"". $conf['shift_enter_mode'] ."\";
       
   990 ". $js_id .".Config['FontFormats'] = \"". str_replace(",", ";", $conf['font_format']) ."\";
       
   991 ". $js_id .".Config['FormatSource'] = ". ($conf['format_source']=="t"?"true":"false") .";
       
   992 ". $js_id .".Config['FormatOutput'] = ". ($conf['format_output']=="t"?"true":"false") .";\n";
       
   993     //}
       
   994 
       
   995     if (function_exists('img_assist_perm')) { //#275158
       
   996       drupal_add_js("var fckImgAssistPath = '". base_path() . drupal_get_path('module', 'img_assist') ."';", 'inline');
       
   997     }
       
   998     // add code for filebrowser for users that have access
       
   999     if (user_access('allow fckeditor file uploads')==1) {
       
  1000       $filebrowser = !empty($conf['filebrowser']) ? $conf['filebrowser'] : 'none';
       
  1001       if ($filebrowser == 'imce' && !module_exists('imce')) {
       
  1002         $filebrowser = 'none';
       
  1003       }
       
  1004       if ($filebrowser == 'ib' && !module_exists('imagebrowser')) {
       
  1005         $filebrowser = 'none';
       
  1006       }
       
  1007       if ($filebrowser == 'webfm' && !module_exists('webfm_popup')) {
       
  1008         $filebrowser = 'none';
       
  1009       }
       
  1010       $quickupload = (!empty($conf['quickupload']) && $conf['quickupload'] == 't');
       
  1011 
       
  1012       // load variables used by both quick upload and filebrowser
       
  1013       // and assure that the $_SESSION variables are loaded
       
  1014       if ($quickupload || $filebrowser == 'builtin') {
       
  1015 
       
  1016         $connector_path = $module_drupal_path ."/fckeditor/editor/filemanager/connectors/php/connector.php";
       
  1017         if (file_exists($connector_path)) {
       
  1018           //FCKeditor 2.5 and above
       
  1019           $connector_path = $module_full_path ."/fckeditor/editor/filemanager/connectors/php/connector.php";
       
  1020         }
       
  1021         else {
       
  1022           //FCKeditor 2.4.3-
       
  1023           $connector_path = "connectors/php/connector.php";
       
  1024         }
       
  1025         $upload_path = $module_drupal_path ."/fckeditor/editor/filemanager/connectors/php/upload.php";
       
  1026         if (file_exists($upload_path)) {
       
  1027           //FCKeditor 2.5 and above
       
  1028           $upload_path = $module_full_path ."/fckeditor/editor/filemanager/connectors/php/upload.php";
       
  1029         }
       
  1030         else {
       
  1031           //FCKeditor 2.4.3-
       
  1032           $upload_path = "/fckeditor/editor/filemanager/upload/php/upload.php";
       
  1033         }
       
  1034 
       
  1035         if (!empty($profile->settings['UserFilesPath'])) $_SESSION['FCKeditor']['UserFilesPath'] = strtr($profile->settings['UserFilesPath'], array("%f" => file_directory_path(), "%u" => $user->uid, "%b" => $host));
       
  1036         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']));
       
  1037         if (variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE) {
       
  1038           $private_dir = trim(isset($global_profile->settings['private_dir']) ? $global_profile->settings['private_dir'] : "", "/\\");
       
  1039           if (strlen($private_dir)) {
       
  1040             $_SESSION['FCKeditor']['UserFilesPath'] = url('system/files') .'/'. $private_dir .'/';
       
  1041             $_SESSION['FCKeditor']['UserFilesAbsolutePath'] = realpath(file_directory_path()) . DIRECTORY_SEPARATOR . $private_dir . DIRECTORY_SEPARATOR;
       
  1042           }
       
  1043           else {
       
  1044             $_SESSION['FCKeditor']['UserFilesPath'] = url('system/files') .'/';
       
  1045             $_SESSION['FCKeditor']['UserFilesAbsolutePath'] = realpath(file_directory_path()) . DIRECTORY_SEPARATOR;
       
  1046           }
       
  1047         }
       
  1048       }
       
  1049 
       
  1050       if ($quickupload) {
       
  1051         $js .= $js_id .".Config['LinkUpload'] = true;\n";
       
  1052         $js .= $js_id .".Config['ImageUpload'] = true;\n";
       
  1053         $js .= $js_id .".Config['FlashUpload'] = true;\n";
       
  1054         $js .= $js_id .".Config['LinkUploadURL'] = '". $upload_path ."';\n";
       
  1055         $js .= $js_id .".Config['ImageUploadURL'] = '". $upload_path ."?Type=Image';\n";
       
  1056         $js .= $js_id .".Config['FlashUploadURL'] = '". $upload_path ."?Type=Flash';\n";
       
  1057       }
       
  1058       else {
       
  1059         $js .= $js_id .".Config['LinkUpload'] = false;\n";
       
  1060         $js .= $js_id .".Config['ImageUpload'] = false;\n";
       
  1061         $js .= $js_id .".Config['FlashUpload'] = false;\n";
       
  1062       }
       
  1063 
       
  1064       switch ($filebrowser) {
       
  1065         case 'imce':
       
  1066           $js .= $js_id .".Config['LinkBrowser']= true;\n";
       
  1067           $js .= $js_id .".Config['ImageBrowser']= true;\n";
       
  1068           $js .= $js_id .".Config['FlashBrowser']= true;\n";
       
  1069           $js .= $js_id .".Config['LinkBrowserURL']= '". $host ."index.php?q=imce&app=FCKEditor|url@txtUrl';\n";
       
  1070           $js .= $js_id .".Config['ImageBrowserURL']= '". $host ."index.php?q=imce&app=FCKEditor|url@txtUrl|width@txtWidth|height@txtHeight';\n";
       
  1071           $js .= $js_id .".Config['FlashBrowserURL']= '". $host ."index.php?q=imce&app=FCKEditor|url@txtUrl';\n";
       
  1072           break;
       
  1073         case 'builtin':
       
  1074           $js .= $js_id .".Config['LinkBrowser'] = true;\n";
       
  1075           $js .= $js_id .".Config['ImageBrowser'] = true;\n";
       
  1076           $js .= $js_id .".Config['FlashBrowser'] = true;\n";
       
  1077           $js .= $js_id .".Config['LinkBrowserURL'] = '". $module_full_path ."/fckeditor/editor/filemanager/browser/default/browser.html?Connector=". $connector_path ."&ServerPath=". $files_path ."';\n";
       
  1078           $js .= $js_id .".Config['ImageBrowserURL'] = '". $module_full_path ."/fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=". $connector_path ."&ServerPath=". $files_path ."';\n";
       
  1079           $js .= $js_id .".Config['FlashBrowserURL'] = '". $module_full_path ."/fckeditor/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=". $connector_path ."&ServerPath=". $files_path ."';\n";
       
  1080           break;
       
  1081         case 'ib':
       
  1082           $js .= $js_id .".Config['ImageBrowser']= true;\n";
       
  1083           $js .= $js_id .".Config['LinkBrowser']= true;\n";
       
  1084           $js .= $js_id .".Config['FlashBrowser']= false;\n";
       
  1085           $js .= $js_id .".Config['ImageBrowserURL']= '". $host ."index.php?q=imagebrowser/view/browser&app=FCKEditor';\n";
       
  1086           $js .= $js_id .".Config['LinkBrowserURL']= '". $host ."index.php?q=imagebrowser/view/browser&app=FCKEditor';\n";
       
  1087           $js .= $js_id .".Config['ImageBrowserWindowWidth']= '680';";
       
  1088           $js .= $js_id .".Config['ImageBrowserWindowHeight'] = '439';";
       
  1089           $js .= $js_id .".Config['LinkBrowserWindowWidth']= '680';";
       
  1090           $js .= $js_id .".Config['LinkBrowserWindowHeight'] = '439';";
       
  1091           break;
       
  1092         case 'webfm':
       
  1093           $js .= $js_id .".Config['LinkBrowser']= true;\n";
       
  1094           $js .= $js_id .".Config['ImageBrowser']= true;\n";
       
  1095           $js .= $js_id .".Config['FlashBrowser']= true;\n";
       
  1096           $js .= $js_id .".Config['ImageDlgHideLink']= true;\n";
       
  1097           $js .= $js_id .".Config['LinkBrowserURL']= '". $host ."index.php?q=webfm_popup&url=txtUrl';\n";
       
  1098           $js .= $js_id .".Config['ImageBrowserURL']= '". $host ."index.php?q=webfm_popup&url=txtUrl';\n";
       
  1099           $js .= $js_id .".Config['FlashBrowserURL']= '". $host ."index.php?q=webfm_popup&url=txtUrl';\n";
       
  1100           break;
       
  1101         default:
       
  1102         case 'none':
       
  1103           $js .= $js_id .".Config['LinkBrowser'] = false;\n";
       
  1104           $js .= $js_id .".Config['ImageBrowser'] = false;\n";
       
  1105           $js .= $js_id .".Config['FlashBrowser'] = false;\n";
       
  1106           break;
       
  1107       }
       
  1108     }
       
  1109     else {
       
  1110       $js .= $js_id .".Config['LinkBrowser'] = false;\n";
       
  1111       $js .= $js_id .".Config['ImageBrowser'] = false;\n";
       
  1112       $js .= $js_id .".Config['FlashBrowser'] = false;\n";
       
  1113       $js .= $js_id .".Config['LinkUpload'] = false;\n";
       
  1114       $js .= $js_id .".Config['ImageUpload'] = false;\n";
       
  1115       $js .= $js_id .".Config['FlashUpload'] = false;\n";
       
  1116     }
       
  1117 
       
  1118     if (!empty($conf['js_conf'])) {
       
  1119       $lines = preg_split("/[\n\r]+/", $conf['js_conf']);
       
  1120       foreach ($lines as $l)
       
  1121       if ($l && strlen($l) > 5) {
       
  1122         $eqpos = strpos($l, "=");
       
  1123         if (false !== $eqpos) {
       
  1124           $option = str_replace("FCKConfig.", "", substr($l, 0, $eqpos));
       
  1125           $js .= "\n". $js_id .".Config['". trim($option) ."'] =". substr($l, $eqpos + 1);
       
  1126         }
       
  1127       }
       
  1128     }
       
  1129 
       
  1130     // add custom xml stylesheet if it exists
       
  1131     if (!empty($conf['css_style']) && $conf['css_style'] == 'theme') {
       
  1132       if (file_exists($themepath .'fckstyles.xml')) {
       
  1133         $styles_xml_path = $host . $themepath .'fckstyles.xml';
       
  1134         $js .= $js_id .".Config['StylesXmlPath'] = \"". $styles_xml_path ."\";\n";
       
  1135       }
       
  1136     }
       
  1137     else if (!empty($conf['css_style']) && $conf['css_style'] == 'self') {
       
  1138       $conf['styles_path'] = str_replace("%h%t", "%t", $conf['styles_path']);
       
  1139       $js .=  $js_id .".Config['StylesXmlPath'] = \"". str_replace(array('%h', '%t', '%m'), array($host, $host . $themepath, $module_drupal_path), $conf['styles_path']) ."\";\n";
       
  1140     }
       
  1141     // add custom stylesheet if configured
       
  1142     // lets hope it exists but we'll leave that to the site admin
       
  1143     if ($conf['css_mode'] == 'theme') {
       
  1144       $style_css = $themepath .'style.css';
       
  1145       if (!empty($theme_info->stylesheets)) {
       
  1146         $css_files = array();
       
  1147         $editorcss = "\"";
       
  1148         foreach ($base_theme_info as $base) { // Grab stylesheets from base theme
       
  1149           foreach ($base->stylesheets as $type => $stylesheets) {
       
  1150             if ($type != "print") {
       
  1151               foreach ($stylesheets as $name => $path) {
       
  1152                 if (file_exists($path)) {
       
  1153                   $css_files[$name] = $host . $path;
       
  1154                 }
       
  1155               }
       
  1156             }
       
  1157           }
       
  1158         }
       
  1159         if (!empty($theme_info->stylesheets)) { // Grab stylesheets from current theme
       
  1160           foreach ($theme_info->stylesheets as $type => $stylesheets) {
       
  1161             if ($type != "print") {
       
  1162               foreach ($stylesheets as $name => $path) {
       
  1163                 if (file_exists($path)) {
       
  1164                   $css_files[$name] = $host . $path;
       
  1165                 }
       
  1166                 else if (!empty($css_files[$name])) {
       
  1167                   unset($css_files[$name]);
       
  1168                 }
       
  1169               }
       
  1170             }
       
  1171           }
       
  1172         }
       
  1173         if (!empty($css_files)) {
       
  1174           $editorcss .= implode(",", $css_files) .",";
       
  1175         }
       
  1176         // Grab stylesheets from color module
       
  1177         $color_paths = variable_get('color_'. $theme .'_stylesheets', array());
       
  1178         if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
       
  1179           if (!empty($color_paths[1])) {
       
  1180             $editorcss .= $host . $color_paths[1] .",";
       
  1181           }
       
  1182         }
       
  1183         else if (!empty($color_paths[0])) {
       
  1184           $editorcss .= $host . $color_paths[0] .",";
       
  1185         }
       
  1186         $editorcss .= $module_full_path ."/fckeditor.css\";\n";
       
  1187         $js .=  $js_id .".Config['EditorAreaCSS'] = ". $editorcss;
       
  1188       }
       
  1189       else if (file_exists($style_css)) {
       
  1190         $js .=  $js_id .".Config['EditorAreaCSS'] = \"". $host . $style_css .",". $module_full_path ."/fckeditor.css\";";
       
  1191       }
       
  1192       else {
       
  1193         $js .=  $js_id .".Config['EditorAreaCSS'] = \"". $module_full_path ."/fckeditor.css\";";
       
  1194       }
       
  1195     }
       
  1196     else if ($conf['css_mode'] == 'self') {
       
  1197       $conf['css_path'] = str_replace("%h%t", "%t", $conf['css_path']);
       
  1198       $js .=  $js_id .".Config['EditorAreaCSS'] = \"". str_replace(array('%h', '%t'), array($host, $host . $themepath), $conf['css_path']) .",". $module_full_path ."/fckeditor.css\";";
       
  1199     }
       
  1200 
       
  1201     if (!$processed) {
       
  1202       drupal_add_js('var '. $js_id .';if (Drupal.jsEnabled) {$(document).ready(function() {'. $js .'});}', 'inline');
       
  1203     }
       
  1204     if ($conf['popup']=="t") {
       
  1205       // Add the script file with the popup open function.
       
  1206       if (!$processed) {
       
  1207         drupal_add_js($module_drupal_path .'/fckeditor.popup.js');
       
  1208       }
       
  1209       $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>";
       
  1210     }
       
  1211     else {
       
  1212       // if no popup mode, add the editor initialization to the footer
       
  1213       // this obviously needs print($closure) in page.tpl.php
       
  1214       if ($fckeditor_on && !$processed) {
       
  1215         $str = "";
       
  1216         if ($element['#id'] == 'edit-body') {
       
  1217           $str = 'if ($("#edit-teaser-js").size() && $("#edit-teaser-js").val().length){
       
  1218             $("#edit-body").val($("#edit-teaser-js").val() + "<!--break-->" + $("#edit-body").val());
       
  1219 	        }';
       
  1220         }
       
  1221         drupal_add_js('if (Drupal.jsEnabled) {$(document).ready(function() {if (typeof ('. $js_id .') != "undefined") { '. $str .'
       
  1222 	        window.setTimeout("FCKeditorReplaceTextarea(\''. $textarea_id .'\','. $js_id .','. $xss_check .');",100);}});}', 'inline', 'footer');
       
  1223       }
       
  1224     }
       
  1225   }
       
  1226 
       
  1227   // display the field id for administrators
       
  1228   if (user_access('administer fckeditor')) {
       
  1229     $element['#suffix'] .= '<div class="textarea-identifier description">'. t('The ID for !excluding this element is: !id - the path is: !path', array(
       
  1230     '!excluding' => l(t("excluding or including"), 'admin/settings/fckeditor'),
       
  1231     '!id' => $element['#id'],
       
  1232     '!path' => $_GET['q'],
       
  1233     )) .'</div>';
       
  1234   }
       
  1235 
       
  1236   $processed_elements[] = $element['#id'];
       
  1237 
       
  1238   return $element;
       
  1239 }
       
  1240 
       
  1241 /**
       
  1242  * Implementation of hook_user().
       
  1243  */
       
  1244 function fckeditor_user($type, &$edit, &$user, $category = NULL) {
       
  1245   if ($type == 'form' && $category == 'account' && user_access('access fckeditor')) {
       
  1246     $profile = fckeditor_user_get_profile($user);
       
  1247     $toolbar_options = fckeditor_load_toolbar_options();
       
  1248     $skin_options = fckeditor_load_skin_options();
       
  1249     $lang_options = fckeditor_load_lang_options();
       
  1250 
       
  1251     // because the settings are saved as strings we need to test for the string 'true'
       
  1252     if ($profile->settings['allow_user_conf'] == 't') {
       
  1253       $form['fckeditor'] = array(
       
  1254       '#type' => 'fieldset',
       
  1255       '#title' => t('Rich Text Editor settings'),
       
  1256       '#weight' => 10,
       
  1257       '#collapsible' => TRUE,
       
  1258       '#collapsed' => TRUE
       
  1259       );
       
  1260 
       
  1261       $form['fckeditor']['fckeditor_default'] = array(
       
  1262       '#type' => 'select',
       
  1263       '#title' => t('Default state'),
       
  1264       '#default_value' => isset($user->fckeditor_default) ? $user->fckeditor_default : (isset($profile->settings['default']) ? $profile->settings['default'] : 'f'),
       
  1265       '#options' => array('t' => t('enabled'), 'f' => t('disabled')),
       
  1266       '#description' => t('Should rich-text editing be enabled or disabled by default in textarea fields? If disabled, rich text editor may still be enabled using toggle or popup window.'),
       
  1267       );
       
  1268 
       
  1269       $form['fckeditor']['fckeditor_show_toggle'] = array(
       
  1270       '#type' => 'select',
       
  1271       '#title' => t('Show disable/enable rich text editor toggle'),
       
  1272       '#default_value' => isset($user->fckeditor_show_toggle) ? $user->fckeditor_show_toggle : (isset($profile->settings['show_toggle']) ? $profile->settings['show_toggle'] : 't'),
       
  1273       '#options' => array('t' => t('true'), 'f' => t('false')),
       
  1274       '#description' => t('Whether or not to show the disable/enable rich text editor toggle below the textarea. Works only if FCKeditor is not running a popup window (see below).'),
       
  1275       );
       
  1276 
       
  1277       $form['fckeditor']['fckeditor_popup'] = array(
       
  1278       '#type' => 'select',
       
  1279       '#title' => t('Use FCKeditor in a popup window'),
       
  1280       '#default_value' => isset($user->fckeditor_popup) ? $user->fckeditor_popup : (isset($profile->settings['popup']) ? $profile->settings['popup'] : 'f'),
       
  1281       '#options' => array('f' => t('false'), 't' => t('true')),
       
  1282       '#description' => t('If this option is enabled a link to a popup window will be used instead of a textarea replace.'),
       
  1283       );
       
  1284 
       
  1285       $form['fckeditor']['fckeditor_skin'] = array(
       
  1286       '#type' => 'select',
       
  1287       '#title' => t('Skin'),
       
  1288       '#default_value' => isset($user->fckeditor_skin) ? $user->fckeditor_skin : (isset($profile->settings['skin']) ? $profile->settings['skin'] : 'default'),
       
  1289       '#options' => $skin_options,
       
  1290       '#description' => t('Choose a FCKeditor skin.'),
       
  1291       );
       
  1292 
       
  1293       $form['fckeditor']['fckeditor_toolbar'] = array(
       
  1294       '#type' => 'select',
       
  1295       '#title' => t('Toolbar'),
       
  1296       '#default_value' => isset($user->fckeditor_toolbar) ? $user->fckeditor_toolbar : (isset($profile->settings['toolbar']) ? $profile->settings['toolbar'] : 'default'),
       
  1297       '#options' => $toolbar_options,
       
  1298       '#description' => t('Choose a FCKeditor toolbar set.'),
       
  1299       );
       
  1300 
       
  1301       $form['fckeditor']['fckeditor_expand'] = array(
       
  1302       '#type' => 'select',
       
  1303       '#title' => t('Start the toolbar expanded'),
       
  1304       '#default_value' => isset($user->fckeditor_expand) ? $user->fckeditor_expand : (isset($profile->settings['expand']) ? $profile->settings['expand'] : 't'),
       
  1305       '#options' => array('t' => t('enabled'), 'f' => t('disabled')),
       
  1306       '#description' => t('The toolbar start expanded or collapsed.'),
       
  1307       );
       
  1308 
       
  1309       $form['fckeditor']['fckeditor_width'] = array(
       
  1310       '#type' => 'textfield',
       
  1311       '#title' => t('Width'),
       
  1312       '#default_value' => isset($user->fckeditor_width) ? $user->fckeditor_width : (isset($profile->settings['width']) ? $profile->settings['width'] : '100%'),
       
  1313       '#description' => t("Width in pixels or percent. Ex: 400 or 100%"),
       
  1314       '#size' => 40,
       
  1315       '#maxlength' => 128,
       
  1316       );
       
  1317 
       
  1318       $form['fckeditor']['fckeditor_lang'] = array(
       
  1319       '#type' => 'select',
       
  1320       '#title' => t('Language'),
       
  1321       '#default_value' => isset($user->fckeditor_lang) ? $user->fckeditor_lang : (isset($profile->settings['lang']) ? $profile->settings['lang'] : 'en'),
       
  1322       '#options' => $lang_options,
       
  1323       '#description' => t('The language for the FCKeditor interface.')
       
  1324       );
       
  1325 
       
  1326       $form['fckeditor']['fckeditor_auto_lang'] = array(
       
  1327       '#type' => 'select',
       
  1328       '#title' => t('Auto-detect language'),
       
  1329       '#default_value' => isset($user->fckeditor_auto_lang) ? $user->fckeditor_auto_lang : (isset($profile->settings['auto_lang']) ? $profile->settings['auto_lang'] : 't'),
       
  1330       '#options' => array('t' => t('true'), 'f' => t('false')),
       
  1331       '#description' => t('Use auto detect user language feature.')
       
  1332       );
       
  1333 
       
  1334       return array('fckeditor' => $form);
       
  1335     }
       
  1336   }
       
  1337 
       
  1338   if ($type == 'validate') {
       
  1339     if (isset($edit['fckeditor_default'], $edit['fckeditor_popup']) && $edit['fckeditor_default'] == 't' && $edit['fckeditor_popup'] == 't') {
       
  1340       form_set_error('fckeditor_popup', t('If FCKeditor is enabled by default, popup window must be disabled.'));
       
  1341     }
       
  1342 
       
  1343     if (isset($edit['fckeditor_show_toggle'], $edit['fckeditor_popup']) && $edit['fckeditor_show_toggle'] == 't' && $edit['fckeditor_popup'] == 't') {
       
  1344       form_set_error('fckeditor_popup', t('If toggle is enabled, popup window must be disabled.'));
       
  1345     }
       
  1346 
       
  1347     if (isset($edit['fckeditor_width']) && !preg_match('/^\d+%?$/', $edit['fckeditor_width'])) {
       
  1348       form_set_error('fckeditor_width', t('Enter valid width. Example: 400 or 100%.'));
       
  1349     }
       
  1350   }
       
  1351 }
       
  1352 
       
  1353 /**
       
  1354  * Return an HTML form for profile configuration.
       
  1355  */
       
  1356 function fckeditor_profile_form($edit) {
       
  1357 
       
  1358   $output = drupal_get_form('fckeditor_profile_form_build', $edit);
       
  1359 
       
  1360   return $output;
       
  1361 }
       
  1362 
       
  1363 /**
       
  1364  * Return an HTML form for global profile configuration.
       
  1365  */
       
  1366 function fckeditor_global_profile_form($edit) {
       
  1367 
       
  1368   $output = drupal_get_form('fckeditor_global_profile_form_build', $edit);
       
  1369 
       
  1370   return $output;
       
  1371 }
       
  1372 
       
  1373 function fckeditor_load_toolbar_options() {
       
  1374   $arr = array();
       
  1375   $module_drupal_path = drupal_get_path('module', 'fckeditor');
       
  1376   $fckconfig_js = $module_drupal_path .'/fckeditor/fckconfig.js';
       
  1377   $fckeditor_config_js = $module_drupal_path .'/fckeditor.config.js';
       
  1378   if (file_exists($fckconfig_js) && is_readable($fckconfig_js)) {
       
  1379     $fp = @fopen($fckconfig_js, "r");
       
  1380     if ($fp) {
       
  1381       while (!feof($fp)) {
       
  1382         $line = fgets($fp, 1024);
       
  1383         if (preg_match("/FCKConfig\.ToolbarSets\[(\"|')(.*?)\\1\]/i", $line, $matches)) {
       
  1384           $arr[$matches[2]] = ucfirst($matches[2]);
       
  1385         }
       
  1386       }
       
  1387       fclose($fp);
       
  1388     }
       
  1389   }
       
  1390   if (file_exists($fckeditor_config_js) && is_readable($fckeditor_config_js)) {
       
  1391     $fp = @fopen($fckeditor_config_js, "r");
       
  1392     if ($fp) {
       
  1393       while (!feof($fp)) {
       
  1394         $line = fgets($fp, 1024);
       
  1395         if (preg_match("/FCKConfig\.ToolbarSets\[(\"|')(.*?)\\1\]/i", $line, $matches)) {
       
  1396           $arr[$matches[2]] = ucfirst($matches[2]);
       
  1397         }
       
  1398       }
       
  1399       fclose($fp);
       
  1400     }
       
  1401   }
       
  1402 
       
  1403   //oops, we have no information about toolbars, let's use hardcoded array
       
  1404   if (empty($arr)) {
       
  1405     $arr = array(
       
  1406     'Basic' => 'Basic',
       
  1407     'Default' => 'Default',
       
  1408     );
       
  1409   }
       
  1410   asort($arr);
       
  1411 
       
  1412   return $arr;
       
  1413 }
       
  1414 
       
  1415 function fckeditor_load_skin_options() {
       
  1416   $arr = array();
       
  1417   $module_drupal_path = drupal_get_path('module', 'fckeditor');
       
  1418   $skin_dir = $module_drupal_path .'/fckeditor/editor/skins';
       
  1419   if (is_dir($skin_dir)) {
       
  1420     $dh = @opendir($skin_dir);
       
  1421     if (FALSE !== $dh) {
       
  1422       while (($file = readdir($dh)) !== FALSE ) {
       
  1423         if (in_array($file, array(".", "..", "CVS", ".svn"))) {
       
  1424           continue;
       
  1425         }
       
  1426         if (is_dir($skin_dir . DIRECTORY_SEPARATOR . $file)) {
       
  1427           $arr[$file] = ucfirst($file);
       
  1428         }
       
  1429       }
       
  1430       closedir( $dh );
       
  1431     }
       
  1432   }
       
  1433 
       
  1434   //oops, we have no information about skins, let's use only default
       
  1435   if (empty($arr)) {
       
  1436     $arr = array(
       
  1437     'default' => 'Default',
       
  1438     );
       
  1439   }
       
  1440   asort($arr);
       
  1441 
       
  1442   return $arr;
       
  1443 }
       
  1444 
       
  1445 function fckeditor_load_lang_options() {
       
  1446   $arr = array();
       
  1447   $module_drupal_path = drupal_get_path('module', 'fckeditor');
       
  1448   $lang_dir = $module_drupal_path .'/fckeditor/editor/lang';
       
  1449   if (is_dir($lang_dir)) {
       
  1450     $dh = @opendir($lang_dir);
       
  1451     if (false !== $dh ) {
       
  1452       while (($file = readdir($dh)) !== FALSE) {
       
  1453         if (in_array($file, array(".", "..", "CVS", ".svn"))) {
       
  1454           continue;
       
  1455         }
       
  1456         if (is_file($lang_dir . DIRECTORY_SEPARATOR . $file) && preg_match("/^(.*?)\.js$/", $file, $matches)) {
       
  1457           $lang = $matches[1];
       
  1458           $arr[$lang] = strtoupper($lang);
       
  1459         }
       
  1460       }
       
  1461       closedir( $dh );
       
  1462     }
       
  1463   }
       
  1464 
       
  1465   //oops, we have no information about languages, let's use those available in FCKeditor 2.4.3
       
  1466   if (empty($arr)) {
       
  1467     $arr = array(
       
  1468     'af' => 'Afrikaans',
       
  1469     'ar' => 'Arabic',
       
  1470     'bg' => 'Bulgarian',
       
  1471     'bn' => 'Bengali/Bangla',
       
  1472     'bs' => 'Bosnian',
       
  1473     'ca' => 'Catalan',
       
  1474     'cs' => 'Czech',
       
  1475     'da' => 'Danish',
       
  1476     'de' => 'German',
       
  1477     'el' => 'Greek',
       
  1478     'en' => 'English',
       
  1479     'en-au' => 'English (Australia)',
       
  1480     'en-ca' => 'English (Canadian)',
       
  1481     'en-uk' => 'English (United Kingdom)',
       
  1482     'eo' => 'Esperanto',
       
  1483     'es' => 'Spanish',
       
  1484     'et' => 'Estonian',
       
  1485     'eu' => 'Basque',
       
  1486     'fa' => 'Persian',
       
  1487     'fi' => 'Finnish',
       
  1488     'fo' => 'Faroese',
       
  1489     'fr' => 'French',
       
  1490     'gl' => 'Galician',
       
  1491     'he' => 'Hebrew',
       
  1492     'hi' => 'Hindi',
       
  1493     'hr' => 'Croatian',
       
  1494     'hu' => 'Hungarian',
       
  1495     'it' => 'Italian',
       
  1496     'ja' => 'Japanese',
       
  1497     'km' => 'Khmer',
       
  1498     'ko' => 'Korean',
       
  1499     'lt' => 'Lithuanian',
       
  1500     'lv' => 'Latvian',
       
  1501     'mn' => 'Mongolian',
       
  1502     'ms' => 'Malay',
       
  1503     'nb' => 'Norwegian Bokmal',
       
  1504     'nl' => 'Dutch',
       
  1505     'no' => 'Norwegian',
       
  1506     'pl' => 'Polish',
       
  1507     'pt' => 'Portuguese (Portugal)',
       
  1508     'pt-br' => 'Portuguese (Brazil)',
       
  1509     'ro' => 'Romanian',
       
  1510     'ru' => 'Russian',
       
  1511     'sk' => 'Slovak',
       
  1512     'sl' => 'Slovenian',
       
  1513     'sr' => 'Serbian (Cyrillic)',
       
  1514     'sr-latn' => 'Serbian (Latin)',
       
  1515     'sv' => 'Swedish',
       
  1516     'th' => 'Thai',
       
  1517     'tr' => 'Turkish',
       
  1518     'uk' => 'Ukrainian',
       
  1519     'vi' => 'Vietnamese',
       
  1520     'zh' => 'Chinese Traditional',
       
  1521     'zh-cn' => 'Chinese Simplified',
       
  1522     );
       
  1523   }
       
  1524 
       
  1525   asort($arr);
       
  1526 
       
  1527   return $arr;
       
  1528 }
       
  1529 
       
  1530 /**
       
  1531  * sort roles according to precedence settings. previously sorted roles are followed by latest added roles.
       
  1532  */
       
  1533 function fckeditor_sorted_roles() {
       
  1534   static $order;
       
  1535   if (isset($order)) {
       
  1536     return $order;
       
  1537   }
       
  1538   $order = array();
       
  1539   $roles = user_roles(0, 'access fckeditor');
       
  1540 
       
  1541   $result = db_query("SELECT settings FROM {fckeditor_settings} WHERE name='FCKeditor Global Profile'");
       
  1542   $data = db_fetch_object($result);
       
  1543   if (!empty($data->settings)) {
       
  1544     $settings = unserialize($data->settings);
       
  1545     if (isset($settings['rank']) && !empty($settings['rank']))
       
  1546     foreach ($settings['rank'] as $rid) {
       
  1547       if (isset($roles[$rid])) {
       
  1548         $order[$rid] = $roles[$rid];
       
  1549         unset($roles[$rid]);
       
  1550       }
       
  1551     }
       
  1552   }
       
  1553   krsort($roles);//sort the remaining unsorted roles by id, descending.
       
  1554   $order += $roles;
       
  1555   return $order;
       
  1556 }
       
  1557 
       
  1558 function fckeditor_global_profile_form_build($sth, $edit) {
       
  1559   $edit = (object) $edit;
       
  1560 
       
  1561   if (arg(3) == 'addg') {
       
  1562     drupal_set_breadcrumb(array(l(t('administer'), 'admin'), l(t('fckeditor'), 'admin/settings/fckeditor'), l(t('Add new FCKeditor Global Profile'), 'admin/settings/fckeditor/addg')));
       
  1563 
       
  1564     $result = db_query("SELECT DISTINCT(rid) FROM {fckeditor_role} WHERE name='FCKeditor Global Profile'");
       
  1565     $data = db_fetch_object($result);
       
  1566 
       
  1567     if (!empty($data->rid)) {
       
  1568       drupal_set_message(t("Global profile already exist. Only one global profile is allowed."), "error");
       
  1569       return array();
       
  1570     }
       
  1571 
       
  1572     $btn = t('Create global profile');
       
  1573   }
       
  1574   else {
       
  1575     $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name);
       
  1576     $btn = t('Update global profile');
       
  1577   }
       
  1578 
       
  1579   $form['common'] = array(
       
  1580   '#type' => 'fieldset',
       
  1581   '#title' => t('Main setup'),
       
  1582   '#collapsible' => TRUE,
       
  1583   '#collapsed' => TRUE
       
  1584   );
       
  1585 
       
  1586   $roles = fckeditor_sorted_roles();
       
  1587   $rids = $rtext = array();
       
  1588   foreach ($roles as $rid => $name) {
       
  1589     $rids[] = $rid;
       
  1590     $rtext[] = '<strong>'. $rid .' - </strong>'. $name;
       
  1591   }
       
  1592   $form['common']['rank'] = array('#type' => 'textfield',
       
  1593   '#title' => t('Role precedence'),
       
  1594   '#default_value' => implode('>', $rids),
       
  1595   '#description' => t('A user having <strong>multiple roles</strong> gets the permissions of the highest one. Sort role IDs according to their <strong>precedence from higher to lower</strong> by putting > in between.<br />'),
       
  1596   );
       
  1597   if ($rids) {
       
  1598     $form['common']['rank']['#description'] .= t('Here is the id-name pairs of roles having access to FCKeditor:') .'<div>'. implode('<br />', $rtext) .'</div>';
       
  1599   }
       
  1600   else {
       
  1601     $form['common']['rank']['#description'] .= t('You haven\'t assigned the <code>!access1</code> !permissions yet.', array('!access1' => t('access fckeditor'), '!permissions' => l(t('permissions'), 'admin/user/permissions')));
       
  1602   }
       
  1603 
       
  1604   $form['fckeditor_exclude_settings'] = array(
       
  1605   '#type' => 'fieldset',
       
  1606   '#title' => t('Visibility settings'),
       
  1607   '#collapsible' => TRUE,
       
  1608   '#collapsed' => TRUE,
       
  1609   );
       
  1610 
       
  1611   $form['fckeditor_exclude_settings']['excl_mode'] = array(
       
  1612   '#type' => 'select',
       
  1613   '#title' => t('Use inclusion or exclusion mode'),
       
  1614   '#default_value' => (empty($edit->settings['excl_mode']) || in_array($edit->settings['excl_mode'], array(0, 2))) ? 0 : 1,
       
  1615   '#options' => array('0' => t('exclude'), '1' => t('include')),
       
  1616   '#description' => t('Choose the way of disabling/enabling FCKeditor on selected fields/paths (see below). Use exclude to disable FCKeditor on selected fields/paths. Use include if you want to load FCKeditor only on selected paths/fields.'),
       
  1617   );
       
  1618   /**
       
  1619    * get excluded fields - so we can have normal textareas too
       
  1620    * split the phrase by any number of commas or space characters,
       
  1621    * which include " ", \r, \t, \n and \f
       
  1622    */
       
  1623   $form['fckeditor_exclude_settings']['excl_fields'] = array(
       
  1624   '#type' => 'textarea',
       
  1625   '#title' => t('Fields to exclude/include'),
       
  1626   '#cols' => 60,
       
  1627   '#rows' => 5,
       
  1628   '#prefix' => '<div style="margin-left:20px">',
       
  1629   '#suffix' => '</div>',
       
  1630   '#default_value' => !empty($edit->settings['excl_fields']) ? $edit->settings['excl_fields'] : '',
       
  1631   '#description' => t("Enter names (HTML ID's) of fields that may or may not have an FCKeditor, depending on the chosen option for the inclusion/exclusion mode.<br />You may separate the different entries by commas, spaces or newlines.<br />You may also use * as a wildcard character."),
       
  1632   );
       
  1633 
       
  1634   /**
       
  1635    * get excluded paths - so we can have normal textareas too
       
  1636    * split the phrase by any number of commas or space characters,
       
  1637    * which include " ", \r, \t, \n and \f
       
  1638    */
       
  1639   $form['fckeditor_exclude_settings']['excl_paths'] = array(
       
  1640   '#type' => 'textarea',
       
  1641   '#title' => t('Paths to exclude/include'),
       
  1642   '#prefix' => '<div style="margin-left:20px">',
       
  1643   '#suffix' => '</div>',
       
  1644   '#cols' => 60,
       
  1645   '#rows' => 5,
       
  1646   '#default_value' => !empty($edit->settings['excl_paths']) ? $edit->settings['excl_paths'] : '',
       
  1647   '#description' => t("Enter drupal paths here, depending on the chosen option for the inclusion/exclusion mode.<br />Paths may be used the same way as in the drupal blocks configuration.<br />You may separate the different entries by commas, spaces or newlines. <br />You may also use * as a wildcard character (for example <code>comment/*</code>)."),
       
  1648   );
       
  1649 
       
  1650   $form['fckeditor_exclude_settings']['simple_incl_fields'] = array(
       
  1651   '#type' => 'textarea',
       
  1652   '#title' => t('Force simplified toolbar on the following fields'),
       
  1653   '#cols' => 60,
       
  1654   '#rows' => 5,
       
  1655   '#default_value' => !empty($edit->settings['simple_incl_fields']) ? $edit->settings['simple_incl_fields'] : '',
       
  1656   '#description' => t("Enter names (HTML ID's) of fields that should have the simplified toolbar (!name).<br />If you don't want to use this feature, simply leave this field empty.<br />You may separate the different entries by commas, spaces or newlines.", array("!name" => FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME)),
       
  1657   );
       
  1658 
       
  1659   $form['fckeditor_exclude_settings']['simple_incl_paths'] = array(
       
  1660   '#type' => 'textarea',
       
  1661   '#title' => t('Force simplified toolbar on the following paths'),
       
  1662   '#cols' => 60,
       
  1663   '#rows' => 5,
       
  1664   '#default_value' => !empty($edit->settings['simple_incl_paths']) ? $edit->settings['simple_incl_paths'] : '',
       
  1665   '#description' => t("Enter drupal paths that should have the simplified toolbar (!name).<br />If you don't want to use this feature, simply leave this field empty.<br />Paths may be used the same way as in the drupal blocks configuration.<br />You may separate the different entries by commas, spaces or newlines.<br />You may also use * as a wildcard character (for example <code>comment/*</code>).", array("!name" => FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME)),
       
  1666   );
       
  1667 
       
  1668   if (variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE) {
       
  1669     $form['fckeditor_advanced_settings'] = array(
       
  1670     '#type' => 'fieldset',
       
  1671     '#title' => t('Advanced settings'),
       
  1672     '#collapsible' => TRUE,
       
  1673     '#collapsed' => TRUE,
       
  1674     );
       
  1675 
       
  1676     $current_private_dir = !isset($edit->settings['private_dir']) ? "" : $edit->settings['private_dir'];
       
  1677     $form['fckeditor_advanced_settings']['private_dir'] = array(
       
  1678       '#type' => 'textfield',
       
  1679       '#title' => t('Location of files uploaded with FCKeditor in the private folder'),
       
  1680       '#default_value' => $current_private_dir,
       
  1681       '#size' => 40,
       
  1682       '#maxlength' => 255,
       
  1683       '#description' => t('The path relative to the location of the private directory where FCKeditor should store uploaded files.') .'<br />'. t('<strong>Warning:</strong> FCKeditor does not implement any kind of access protection on files available in this location. All files stored in the directory defined above might be accessible by unathenticated users if there is no information about the file in the Drupal\'s database.') .'<br />'. t('System path to the private folder is: !system_path.', array('!system_path' => realpath(file_directory_path()) . DIRECTORY_SEPARATOR)) .'<br />'. t('Available wildcard characters:<br /><strong>%u</strong> - User ID.') .'<br />'. t('Current path: !path', array('!path' => $current_private_dir .' ('. file_create_path($current_private_dir) .')')),
       
  1684     );
       
  1685   }
       
  1686 
       
  1687   $form['submit'] = array(
       
  1688   '#type' => 'submit',
       
  1689   '#value' => $btn
       
  1690   );
       
  1691 
       
  1692   return $form;
       
  1693 }
       
  1694 
       
  1695 /**
       
  1696  * Return an HTML form for profile configuration.
       
  1697  */
       
  1698 function fckeditor_profile_form_build($sth, $edit) {
       
  1699   $edit = (object) $edit;
       
  1700 
       
  1701   $toolbar_options = fckeditor_load_toolbar_options();
       
  1702   $skin_options = fckeditor_load_skin_options();
       
  1703   $lang_options = fckeditor_load_lang_options();
       
  1704 
       
  1705   // Only display the roles that currently don't have a fckeditor profile. One
       
  1706   // profile per role.
       
  1707   $orig_roles = user_roles(FALSE, 'access fckeditor');
       
  1708   $roles = $orig_roles;
       
  1709 
       
  1710   if (!empty($edit->rids) && !user_roles(false, 'access fckeditor')) {
       
  1711     drupal_set_message(t('You haven\'t assigned <code>!access1</code> !permissions yet.<br />It is recommended to assign the <code>!access1</code> !permissions before updating FCKeditor profiles.',
       
  1712     array(
       
  1713     '!access1' => t('access fckeditor'),
       
  1714     '!permissions' => l(t('permissions'), 'admin/user/permissions'))), 'warning');
       
  1715   }
       
  1716 
       
  1717   if (arg(3) == 'add') {
       
  1718     drupal_set_breadcrumb(array(l(t('administer'), 'admin'), l(t('fckeditor'), 'admin/settings/fckeditor'), l(t('Add new FCKeditor profile'), 'admin/settings/fckeditor/add')));
       
  1719 
       
  1720     $result = db_query('SELECT DISTINCT(rid) FROM {fckeditor_role}');
       
  1721     while ($data = db_fetch_object($result)) {
       
  1722       if ((empty($edit->rids) || !in_array($data->rid, array_keys((array) $edit->rids))) && !form_get_errors()) {
       
  1723         unset($roles[$data->rid]);
       
  1724       }
       
  1725     }
       
  1726     if (count($orig_roles) != count($roles)) {
       
  1727       drupal_set_message(t('Not all user roles are shown since they already have fckeditor profiles. You must first unassign profiles in order to add them to a new one.'));
       
  1728     }
       
  1729     $btn = t('Create profile');
       
  1730   }
       
  1731   else {
       
  1732     $form['old_name'] = array('#type' => 'hidden', '#value' => $edit->name);
       
  1733     $btn = t('Update profile');
       
  1734   }
       
  1735 
       
  1736   $form['basic'] = array(
       
  1737   '#type' => 'fieldset',
       
  1738   '#title' => t('Basic setup'),
       
  1739   '#collapsible' => TRUE,
       
  1740   '#collapsed' => TRUE
       
  1741   );
       
  1742 
       
  1743   $form['basic']['name'] = array(
       
  1744   '#type' => 'textfield',
       
  1745   '#title' => t('Profile name'),
       
  1746   '#default_value' => !empty($edit->name) ? $edit->name : "",
       
  1747   '#size' => 40,
       
  1748   '#maxlength' => 128,
       
  1749   '#description' => t('Enter a name for this profile. This name is only visible within the fckeditor administration page.'),
       
  1750   '#required' => TRUE
       
  1751   );
       
  1752 
       
  1753   $form['basic']['rids'] = array(
       
  1754   '#type' => 'checkboxes',
       
  1755   '#title' => t('Roles allowed to use this profile'),
       
  1756   '#default_value' => !empty($edit->rids) ? array_keys((array) $edit->rids) : array(),
       
  1757   '#options' => $roles,
       
  1758   '#description' =>  t('Only roles with \'!access1\' permission will be shown here. If no role is available, make sure that you have assigned the \'!access1\' !permission.', array('!access1' => t('access fckeditor'), '!permission' => l(t("permission"), "admin/user/permissions"))),
       
  1759   '#required' => TRUE
       
  1760   );
       
  1761 
       
  1762   $form['basic']['allow_user_conf'] = array(
       
  1763   '#type' => 'select',
       
  1764   '#title' => t('Allow users to customize FCKeditor appearance'),
       
  1765   '#default_value' => !empty($edit->settings['allow_user_conf']) ? $edit->settings['allow_user_conf'] : 'f',
       
  1766   '#options' => array('f' => t('false'), 't' => t('true')),
       
  1767   '#description' => t('If allowed, users will be able to override <code>Editor appearance</code> by visiting their profile page.'),
       
  1768   );
       
  1769 
       
  1770   $form['security'] = array(
       
  1771     '#type' => 'fieldset',
       
  1772     '#title' => t('Security'),
       
  1773     '#description' => '<p>'. t("When Drupal saves user data input through a textarea, it's saved in the database in unmodified form. That's why all untrusted textarea input should be run through an input format filter before outputting it to the screen.") .'</p>'.'<p>'. t("Drupal will not, however, filter data for content editor's editing a textarea. Normally, there is no security risk because the unmodified code is displayed as text and will not be rendered as HTML. But with FCKeditor installed, this is not the case, and content editor's are subject to having raw, untrusted code running inside their browsers.") .'</p>'.'<p>'. t("To address this issue, you should select a security filters below to prevent FCKeditor from rendering malicious code. Note that if a textarea's input format is set to \"Full HTML\" (or if the input format of the node doesn't include the filter), FCKeditor will properly ignore the setting below and will not run the code through the security filter.") .'</p>'.'<p>'. t("If any textareas on your site are accessible to unwanted users, we recommend checking the \"HTML Filter\". You may have other modules installed that provide other kinds of security filters and you may use those as long as you trust them to properly filter out malicious code. Note that not all the filters below are security filters and will provide no protection.") .'</p>',
       
  1774     '#collapsible' => TRUE,
       
  1775     '#collapsed' => TRUE
       
  1776   );
       
  1777 
       
  1778   $all = filter_list_all();
       
  1779 
       
  1780   $form['security']['filters'] = array(
       
  1781     '#type' => 'fieldset',
       
  1782     '#title' => t('Security filters'),
       
  1783     '#description' => t('Please choose carefully all filters that protect your content (probably not all filters listed below are security filters).'),
       
  1784     '#tree' => TRUE,
       
  1785   );
       
  1786 
       
  1787   //don't bother administrator with filters that definitely are not security filters
       
  1788   $modules_with_filters_to_skip = array('amazon_filter', 'asy', 'bbcode', 'biblio', 'blockquote', 'bookpost', 'chessboard', 'citation_filter', 'codefilter', 'collapse_text', 'contextlinks', 'coolfilter', 'dialectic', 'dript', 'dme', 'drutex', 'embedfilter', 'ext_link_page', 'extlink', 'elf', 'flickr', 'flickrstickr', 'footnotes', 'formdefaults', 'freelinking', 'gallery', 'geogebra', 'geshifilter', 'gotwo', 'googtube', 'gotcha', 'gtspam', 'hidden_content', 'img_assist', 'image_filter', 'inlinetags', 'insert_view', 'insertframe', 'insertnode', 'interwiki', 'jlightbox', 'jsmath', 'language_sections', 'link_node', 'lootz', 'markdown', 'marksmarty', 'mobile_codes', 'mykml', 'nofollowlist', 'oagwt', 'paging', 'pathfilter', 'pearwiki_filter', 'php', 'pirate', 'reptag', 'scrippet', 'scripturefilter', 'signwriter', 'slideshowpro', 'smartlinebreakconverter', 'smartypants', 'smileys', 'spamspan', 'spam_tokens', 'spoiler', 'table_altrow', 'tablemanager', 'tableofcontents', 'textile', 'tooltips', 'twikifilter', 'typogrify', 'unwrap', 'urlclass', 'urlicon', 'url_replace_filter', 'username_highlighter', 'video_filter', 'quote');
       
  1789 
       
  1790   if (!isset($edit->settings['ss'])) {
       
  1791     $edit->settings['filters']['filter/0'] = 1;
       
  1792   }
       
  1793   foreach ($all as $id => $filter) {
       
  1794     if (in_array(strtolower($filter->module), $modules_with_filters_to_skip)) {
       
  1795       continue;
       
  1796     }
       
  1797     //skip line break converter and email -> link
       
  1798     if ($filter->module == 'filter' && in_array($filter->delta, array(1, 2))) {
       
  1799       continue;
       
  1800     }
       
  1801     $form['security']['filters'][$id] = array(
       
  1802       '#type' => 'checkbox',
       
  1803       '#title' => $filter->name,
       
  1804       '#default_value' => !empty($edit->settings['filters'][$id]),
       
  1805       '#description' => module_invoke($filter->module, 'filter', 'description', $filter->delta),
       
  1806     );
       
  1807   }
       
  1808 
       
  1809   $form['security']['ss'] = array(
       
  1810     '#type' => 'radios',
       
  1811     '#title' => t('Security settings'),
       
  1812     '#default_value' => isset($edit->settings['ss']) ? $edit->settings['ss'] : '2',
       
  1813     '#options' => array(
       
  1814       '2' => t('Always run security filters for FCKeditor.'),
       
  1815       '1' => t('Run security filters only when FCKeditor is set to start automatically.'),
       
  1816     ),
       
  1817     '#description' => t('There are two ways of starting FCKeditor: automatically and manually (via toggle or in a popup). If you decide to apply security filters only when FCKeditor starts automatically, you\'ll not be protected when toggling manually from plain textarea to FCKeditor or when using FCKeditor in a popup mode. So choose this option only, if you can detect various attacks (mainly XSS) by yourself just by looking at the HTML code.'),
       
  1818   );
       
  1819 
       
  1820   $form['fckeditor_exclude_settings'] = array(
       
  1821   '#type' => 'fieldset',
       
  1822   '#title' => t('Visibility settings'),
       
  1823   '#collapsible' => TRUE,
       
  1824   '#collapsed' => TRUE,
       
  1825   );
       
  1826 
       
  1827   $form['fckeditor_exclude_settings']['min_rows'] = array(
       
  1828   '#type' => 'textfield',
       
  1829   '#title' => t('Minimum rows'),
       
  1830   '#default_value' => !empty($edit->settings['min_rows']) ? $edit->settings['min_rows'] : '5',
       
  1831   '#description' => t("FCKeditor will be triggered if the textarea has more rows than entered here. Enter '1' if you do not want to use this feature."),
       
  1832   );
       
  1833 
       
  1834   $form['fckeditor_exclude_settings']['excl_mode'] = array(
       
  1835   '#type' => 'select',
       
  1836   '#title' => t('Use inclusion or exclusion mode'),
       
  1837   '#default_value' => (empty($edit->settings['excl_mode']) || in_array($edit->settings['excl_mode'], array(0, 2))) ? 0 : 1,
       
  1838   '#options' => array('0' => t('exclude'), '1' => t('include')),
       
  1839   '#description' => t('Choose the way of disabling/enabling FCKeditor on selected fields/paths (see below). Use exclude to disable FCKeditor on selected fields/paths. Use include if you want to load FCKeditor only on selected paths/fields.'),
       
  1840   );
       
  1841 
       
  1842   /**
       
  1843    * get excluded fields - so we can have normal textareas too
       
  1844    * split the phrase by any number of commas or space characters,
       
  1845    * which include " ", \r, \t, \n and \f
       
  1846    */
       
  1847   $form['fckeditor_exclude_settings']['excl_fields'] = array(
       
  1848   '#type' => 'textarea',
       
  1849   '#title' => t('Fields to exclude/include'),
       
  1850   '#cols' => 60,
       
  1851   '#rows' => 5,
       
  1852   '#prefix' => '<div style="margin-left:20px">',
       
  1853   '#suffix' => '</div>',
       
  1854   '#default_value' => !empty($edit->settings['excl_fields']) ? $edit->settings['excl_fields'] : '',
       
  1855   '#description' => t("Enter names (HTML ID's) of fields that may or may not have an FCKeditor, depending on the chosen option for the inclusion/exclusion mode.<br />You may separate the different entries by commas, spaces or newlines.<br />You may also use * as a wildcard character."),
       
  1856   );
       
  1857 
       
  1858   /**
       
  1859    * get excluded paths - so we can have normal textareas too
       
  1860    * split the phrase by any number of commas or space characters,
       
  1861    * which include " ", \r, \t, \n and \f
       
  1862    */
       
  1863   $form['fckeditor_exclude_settings']['excl_paths'] = array(
       
  1864   '#type' => 'textarea',
       
  1865   '#title' => t('Paths to exclude/include'),
       
  1866   '#prefix' => '<div style="margin-left:20px">',
       
  1867   '#suffix' => '</div>',
       
  1868   '#cols' => 60,
       
  1869   '#rows' => 5,
       
  1870   '#default_value' => !empty($edit->settings['excl_paths']) ? $edit->settings['excl_paths'] : '',
       
  1871   '#description' => t("Enter drupal paths here, depending on the chosen option for the inclusion/exclusion mode.<br />Paths may be used the same way as in the drupal blocks configuration.<br />You may separate the different entries by commas, spaces or newlines. <br />You may also use * as a wildcard character (for example <code>comment/*</code>)."),
       
  1872   );
       
  1873 
       
  1874   $form['fckeditor_exclude_settings']['simple_incl_fields'] = array(
       
  1875   '#type' => 'textarea',
       
  1876   '#title' => t('Force simplified toolbar on the following fields'),
       
  1877   '#cols' => 60,
       
  1878   '#rows' => 5,
       
  1879   //'#prefix' => t('Here you can define where FCKeditor should force the <code>!simple</code> toolbar.<br />Useful for smaller textareas where we usually don\'t use very complicated HTML code, like in signatures.', array('!simple' => FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME)),
       
  1880   '#default_value' => !empty($edit->settings['simple_incl_fields']) ? $edit->settings['simple_incl_fields'] : '',
       
  1881   '#description' => t("Enter names (HTML ID's) of fields that should have the simplified toolbar (!name).<br />If you don't want to use this feature, simply leave this field empty.<br />You may separate the different entries by commas, spaces or newlines.", array("!name" => FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME)),
       
  1882   );
       
  1883 
       
  1884   $form['fckeditor_exclude_settings']['simple_incl_paths'] = array(
       
  1885   '#type' => 'textarea',
       
  1886   '#title' => t('Force simplified toolbar on the following paths'),
       
  1887   '#cols' => 60,
       
  1888   '#rows' => 5,
       
  1889   //'#prefix' => t('Here you can define where FCKeditor should force the <code>!simple</code> toolbar.<br />Useful for smaller textareas where we usually don\'t use very complicated HTML code, like in signatures.', array('!simple' => FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME)),
       
  1890   '#default_value' => !empty($edit->settings['simple_incl_paths']) ? $edit->settings['simple_incl_paths'] : '',
       
  1891   '#description' => t("Enter drupal paths that should have the simplified toolbar (!name).<br />If you don't want to use this feature, simply leave this field empty.<br />Paths may be used the same way as in the drupal blocks configuration.<br />You may separate the different entries by commas, spaces or newlines.<br />You may also use * as a wildcard character (for example <code>comment/*</code>).", array("!name" => FCKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME)),
       
  1892   );
       
  1893 
       
  1894   $form['appearance'] = array(
       
  1895   '#type' => 'fieldset',
       
  1896   '#title' => t('Editor appearance'),
       
  1897   '#collapsible' => TRUE,
       
  1898   '#collapsed' => TRUE,
       
  1899   );
       
  1900 
       
  1901   $form['appearance']['default'] = array(
       
  1902   '#type' => 'select',
       
  1903   '#title' => t('Default state'),
       
  1904   '#default_value' => !empty($edit->settings['default']) ? $edit->settings['default'] : 't',
       
  1905   '#options' => array('t' => t('enabled'), 'f' => t('disabled')),
       
  1906   '#description' => t('Default editor state. If disabled, rich text editor may still be enabled using toggle or popup window.'),
       
  1907   );
       
  1908 
       
  1909   $form['appearance']['show_toggle'] = array(
       
  1910   '#type' => 'select',
       
  1911   '#title' => t('Show disable/enable rich text editor toggle'),
       
  1912   '#default_value' => !empty($edit->settings['show_toggle']) ? $edit->settings['show_toggle'] : 't',
       
  1913   '#options' => array('t' => t('true'), 'f' => t('false')),
       
  1914   '#description' => t('Whether or not to show the disable/enable rich text editor toggle below the textarea. Works only if FCKeditor is not running in a popup window (see below).'),
       
  1915   );
       
  1916 
       
  1917   $form['appearance']['popup'] = array(
       
  1918   '#type' => 'select',
       
  1919   '#title' => t('Use FCKeditor in a popup window'),
       
  1920   '#default_value' => !empty($edit->settings['popup']) ? $edit->settings['popup'] : 'f',
       
  1921   '#options' => array('f' => t('false'), 't' => t('true')),
       
  1922   '#description' => t('If this option is enabled a link to a popup window will be used instead of a textarea replace.'),
       
  1923   );
       
  1924 
       
  1925   $form['appearance']['skin'] = array(
       
  1926   '#type' => 'select',
       
  1927   '#title' => t('Skin'),
       
  1928   '#default_value' => !empty($edit->settings['skin']) ? $edit->settings['skin'] : 'default',
       
  1929   '#options' => $skin_options,
       
  1930   '#description' => t('Choose a default skin.'),
       
  1931   );
       
  1932 
       
  1933   $form['appearance']['toolbar'] = array(
       
  1934   '#type' => 'select',
       
  1935   '#title' => t('Toolbar'),
       
  1936   '#default_value' => !empty($edit->settings['toolbar']) ? $edit->settings['toolbar'] : 'default',
       
  1937   '#options' => $toolbar_options,
       
  1938   '#description' => t('Choose a default toolbar set. To define new toolbar, edit <code>fckeditor.config.js</code> located in !module_path.', array('!module_path' => drupal_get_path('module', 'fckeditor'))),
       
  1939   );
       
  1940 
       
  1941   $form['appearance']['expand'] = array(
       
  1942   '#type' => 'select',
       
  1943   '#title' => t('Start the toolbar expanded'),
       
  1944   '#default_value' => !empty($edit->settings['expand']) ? $edit->settings['expand'] : 't',
       
  1945   '#options' => array('t' => t('enabled'), 'f' => t('disabled')),
       
  1946   '#description' => t('The toolbar start expanded or collapsed.'),
       
  1947   );
       
  1948 
       
  1949   $form['appearance']['width'] = array(
       
  1950   '#type' => 'textfield',
       
  1951   '#title' => t('Width'),
       
  1952   '#default_value' => !empty($edit->settings['width']) ? $edit->settings['width'] : '100%',
       
  1953   '#description' => t("Width in pixels or percent. Ex: 400 or 100%"),
       
  1954   '#size' => 40,
       
  1955   '#maxlength' => 128,
       
  1956   );
       
  1957 
       
  1958   $form['appearance']['lang'] = array(
       
  1959   '#type' => 'select',
       
  1960   '#title' => t('Language'),
       
  1961   '#default_value' => !empty($edit->settings['lang']) ? $edit->settings['lang'] : 'en',
       
  1962   '#options' => $lang_options,
       
  1963   '#description' => t('The language for the FCKeditor interface.')
       
  1964   );
       
  1965 
       
  1966   $form['appearance']['auto_lang'] = array(
       
  1967   '#type' => 'select',
       
  1968   '#title' => t('Auto-detect language'),
       
  1969   '#default_value' => !empty($edit->settings['auto_lang']) ? $edit->settings['auto_lang'] : 't',
       
  1970   '#options' => array('t' => t('true'), 'f' => t('false')),
       
  1971   '#description' => t('Use auto detect user language feature.')
       
  1972   );
       
  1973 
       
  1974   /*
       
  1975   $form['appearance']['appearance_conf'] = array(
       
  1976   '#type' => 'select',
       
  1977   '#title' => t('Ignore this section, use default settings defined in config files'),
       
  1978   '#default_value' => $edit->settings['appearance_conf'] ? $edit->settings['appearance_conf'] : 'f',
       
  1979   '#options' => array('f' => t('false'), 't' => t('true')),
       
  1980   '#description' => t('Although it is less handy, defining settings only in config files (<code>fckconfig.js</code> and <code>fckeditor.config.js</code>) will slightly leverage your traffic and improve load time of your site. <br />Warning: if set to true, all changes made in <code>Editor appearance</code> will have no affect on FCKeditor\'s behaviour.'),
       
  1981   );
       
  1982   */
       
  1983 
       
  1984   $form['output'] = array(
       
  1985   '#type' => 'fieldset',
       
  1986   '#title' => t('Cleanup and output'),
       
  1987   '#collapsible' => TRUE,
       
  1988   '#collapsed' => TRUE,
       
  1989   );
       
  1990 
       
  1991   $form['output']['enter_mode'] = array(
       
  1992   '#type' => 'select',
       
  1993   '#title' => t('Enter mode'),
       
  1994   '#default_value' => !empty($edit->settings['enter_mode']) ? $edit->settings['enter_mode'] : 'p',
       
  1995   '#options' => array('p' => '<p>', 'br' => '<br>', 'div' => '<div>'),
       
  1996   '#description' => t('Set which tag FCKeditor should use when [Enter] key is pressed.')
       
  1997   );
       
  1998 
       
  1999   $form['output']['shift_enter_mode'] = array(
       
  2000   '#type' => 'select',
       
  2001   '#title' => t('Shift + Enter mode'),
       
  2002   '#default_value' => !empty($edit->settings['shift_enter_mode']) ? $edit->settings['shift_enter_mode'] : 'br',
       
  2003   '#options' => array('p' => '<p>', 'br' => '<br>', 'div' => '<div>'),
       
  2004   '#description' => t('Set which tag FCKeditor should use when [Shift] + [Enter] is pressed.')
       
  2005   );
       
  2006 
       
  2007   $form['output']['font_format'] = array(
       
  2008   '#type' => 'textfield',
       
  2009   '#title' => t('Font formats'),
       
  2010   '#default_value' => !empty($edit->settings['font_format']) ? $edit->settings['font_format'] : 'p;div;pre;address;h1;h2;h3;h4;h5;h6',
       
  2011   '#size' => 40,
       
  2012   '#maxlength' => 250,
       
  2013   '#description' => t('Semicolon separated list of HTML font formats. Allowed values are: p;div;pre;address;h1;h2;h3;h4;h5;h6'),
       
  2014   );
       
  2015 
       
  2016   $form['output']['format_source'] = array(
       
  2017   '#type' => 'select',
       
  2018   '#title' => t('Apply source formatting'),
       
  2019   '#default_value' => !empty($edit->settings['format_source']) ? $edit->settings['format_source'] : 't',
       
  2020   '#options' => array('t' => t('true'), 'f' => ('false')),
       
  2021   '#description' => t('When set to "true" the editor will format the XHTML when switching from WYSIWYG view to Source view, by inserting line breaks on some tags endings and indenting paragraphs, tables and lists.'),
       
  2022   );
       
  2023 
       
  2024   $form['output']['format_output'] = array(
       
  2025   '#type' => 'select',
       
  2026   '#title' => t('Format output'),
       
  2027   '#default_value' => !empty($edit->settings['format_output']) ? $edit->settings['format_output'] : 't',
       
  2028   '#options' => array('t' => t('true'), 'f' => t('false')),
       
  2029   '#description' => t('When set to "true" the editor will format the XHTML output by inserting line breaks on some tags endings and indenting paragraphs, tables and lists.'),
       
  2030   );
       
  2031 
       
  2032   /*
       
  2033   $form['output']['output_conf'] = array(
       
  2034   '#type' => 'select',
       
  2035   '#title' => t('Ignore this section, use default settings defined in config files'),
       
  2036   '#default_value' => $edit->settings['output_conf'] ? $edit->settings['output_conf'] : 'f',
       
  2037   '#options' => array('f' => t('false'), 't' => t('true')),
       
  2038   '#description' => t('Although it is less handy, defining settings only in config files (<code>fckconfig.js</code> and <code>fckeditor.config.js</code>) will slightly leverage your traffic and improve load time of your site. <br />Warning: if set to true, all changes made in <code>Cleanup and output</code> will have no affect on FCKeditor\'s behaviour.'),
       
  2039   );
       
  2040   */
       
  2041 
       
  2042   $form['css'] = array(
       
  2043   '#type' => 'fieldset',
       
  2044   '#title' => t('CSS'),
       
  2045   '#collapsible' => TRUE,
       
  2046   '#collapsed' => TRUE
       
  2047   );
       
  2048 
       
  2049   $form['css']['css_mode'] = array(
       
  2050   '#type' => 'select',
       
  2051   '#title' => t('Editor CSS'),
       
  2052   '#default_value' => !empty($edit->settings['css_mode']) ? $edit->settings['css_mode'] : 'theme',
       
  2053   '#options' => array('theme' => t('use theme css'), 'self' => t('define css'), 'none' => t('FCKeditor default')),
       
  2054   '#description' => t('Defines the CSS to be used in the editor area.<br />use theme css - load style.css from current site theme.<br />define css - enter path for css file below.<br />FCKeditor default - uses default CSS from editor.')
       
  2055   );
       
  2056 
       
  2057   $form['css']['css_path'] = array(
       
  2058   '#type' => 'textfield',
       
  2059   '#title' => t('CSS path'),
       
  2060   '#default_value' => !empty($edit->settings['css_path']) ? $edit->settings['css_path'] : "",
       
  2061   '#size' => 40,
       
  2062   '#maxlength' => 255,
       
  2063   '#description' => t('Enter path to CSS file (<em>example: css/editor.css</em>) or a list of css files seperated by a comma (<em>example: /themes/garland/style.css,http://example.com/style.css</em>).<br />Macros: %h (host name: !host), %t (path to theme: !theme)<br />Be sure to select "define css" above.', array('!host' => base_path(), '!theme' => base_path() . path_to_theme() .'/'))
       
  2064   );
       
  2065 
       
  2066   $form['css']['css_style'] = array(
       
  2067   '#type' => 'select',
       
  2068   '#title' => t('Predefined styles'),
       
  2069   '#default_value' => !empty($edit->settings['css_style']) ? $edit->settings['css_style'] : 'theme',
       
  2070   '#options' => array('theme' => t('use theme fckstyles.xml'), 'self' => t('define path to fckstyles.xml'), 'default' => t('FCKeditor default')),
       
  2071   '#description' => t('Define the location of <code>fckstyles.xml</code> file. It is used by the &quot;Style&quot; dropdown list available in the Default toolbar.<br />Copy !fckstyles.xml inside your theme directory (<code>!theme</code>) and adjust it to your needs.', array('!fckstyles.xml' => drupal_get_path('module', 'fckeditor') .'/fckeditor/fckstyles.xml', '!theme' => path_to_theme() .'/fckstyles.xml'))
       
  2072   );
       
  2073 
       
  2074   $form['css']['styles_path'] = array(
       
  2075   '#type' => 'textfield',
       
  2076   '#title' => t('Predefined styles path'),
       
  2077   '#default_value' => !empty($edit->settings['styles_path']) ? $edit->settings['styles_path'] : "",
       
  2078   '#size' => 40,
       
  2079   '#maxlength' => 255,
       
  2080   '#description' => t('Enter path to XML file with predefined styles (<em>example: /fckstyles.xml</em>).<br />Macros: %h (host name: !host), %t (path to theme: !theme), %m (path to FCKeditor module: !module)<br />Be sure to select "define path to fckstyles.xml" above.', array('!host' => base_path(), '!theme' => base_path() . path_to_theme() .'/', '!module' => drupal_get_path('module', 'fckeditor')))
       
  2081   );
       
  2082 
       
  2083   $form['fckeditor_upload_settings'] = array(
       
  2084   '#type' => 'fieldset',
       
  2085   '#title' => t('File browser settings'),
       
  2086   '#collapsible' => TRUE,
       
  2087   '#collapsed' => TRUE,
       
  2088   '#description' => t('Set file browser settings. A file browser will allow you to explore the files contained on the server and embed them as links, images or flash movies.') .' '. t('Besides the built-in FCKeditor file browser, you can also use a contributed module like !imce, !ib or !webfm.', array(
       
  2089     '!imce' => l(t('IMCE'), 'http://drupal.org/project/imce'),
       
  2090     '!webfm' => l(t('Web File Manager'), 'http://drupal.org/project/webfm'),
       
  2091     '!ib' => l(t('Image Browser'), 'http://drupal.org/project/imagebrowser'))) .' '. t('The quick upload setting controls whether images, flash movies and files can be uploaded using the Upload tab of the respective dialogs.') .' '. t('Please note that these options require manual configuration, check !readme for more information.',
       
  2092     array(
       
  2093     '!readme' => l('readme.txt', 'admin/help/fckeditor'),
       
  2094     )
       
  2095     ) .'<br />'
       
  2096   );
       
  2097 
       
  2098   $filebrowsers = array(
       
  2099     'none' => t('None'),
       
  2100     'builtin' => t('Built-in filebrowser'),
       
  2101   );
       
  2102 
       
  2103   if (module_exists('imce')) {
       
  2104     $filebrowsers['imce'] = t('IMCE');
       
  2105   }
       
  2106 
       
  2107   if (module_exists('imagebrowser')) {
       
  2108     $filebrowsers['ib'] = t('Image Browser');
       
  2109   }
       
  2110 
       
  2111   if (module_exists('webfm_popup')) {
       
  2112     $filebrowsers['webfm'] = t('Web File Manager');
       
  2113   }
       
  2114 
       
  2115   $form['fckeditor_upload_settings']['filebrowser'] = array(
       
  2116     '#type' => 'select',
       
  2117     '#options' => $filebrowsers,
       
  2118     '#title' => t('File browser type'),
       
  2119     '#default_value' => !empty($edit->settings['filebrowser']) ? $edit->settings['filebrowser'] : 'none',
       
  2120     '#description' => t('Select the file browser that you would like to use to upload files, images and flash movies.'),
       
  2121   );
       
  2122 
       
  2123   $form['fckeditor_upload_settings']['quickupload'] = array(
       
  2124     '#type' => 'select',
       
  2125     '#options' => array('f' => t('false'), 't' => t('true')),
       
  2126     '#title' => t('Allow quick uploads'),
       
  2127     '#default_value' => !empty($edit->settings['quickupload']) ? $edit->settings['quickupload'] : 'f',
       
  2128     '#description' => t('The quick upload functionality can be disabled and enabled independently of the file browser. It will always use the settings below. To enable quick uploads you must follow the same configuration procedure as when enabling the built-in file browser.'),
       
  2129   );
       
  2130 
       
  2131   $current_user_files_path = empty($edit->settings['UserFilesPath']) ? "" : strtr($edit->settings['UserFilesPath'], array("%f" => file_directory_path(), "%u" => "UID", "%b" => base_path()));
       
  2132   $current_user_files_absolute_path = empty($edit->settings['UserFilesAbsolutePath']) ? "" : strtr($edit->settings['UserFilesAbsolutePath'], array("%f" => file_directory_path(), "%u" => "UID", "%b" => base_path(), "%d" => $_SERVER['DOCUMENT_ROOT']));
       
  2133 
       
  2134   $form['fckeditor_upload_settings']['UserFilesPath'] = array(
       
  2135     '#type' => 'textfield',
       
  2136     '#title' => t('Path to uploaded files'),
       
  2137     '#default_value' => !empty($edit->settings['UserFilesPath']) ? $edit->settings['UserFilesPath'] : "%b%f/",
       
  2138     '#size' => 40,
       
  2139     '#maxlength' => 255,
       
  2140     '#description' => t('Path to uploaded files relative to the document root.<br />Available wildcard characters:<br /><strong>%b</strong> - base URL path of the Drupal installation (!base).<br /><strong>%f</strong> - Drupal file system path where the files are stored (!files).<br /><strong>%u</strong> - User ID.<br />Current path: !path', array('!path' => $current_user_files_path, '!files' => file_directory_path(), '!base' => base_path())),
       
  2141   );
       
  2142 
       
  2143   $form['fckeditor_upload_settings']['UserFilesAbsolutePath'] = array(
       
  2144     '#type' => 'textfield',
       
  2145     '#title' => t('Absolute path to uploaded files'),
       
  2146     '#default_value' => !empty($edit->settings['UserFilesAbsolutePath']) ? $edit->settings['UserFilesAbsolutePath'] : "%d%b%f/",
       
  2147     '#size' => 40,
       
  2148     '#maxlength' => 255,
       
  2149     '#description' => t('The path to the local directory (in the server) which points to the path defined above. If empty, FCKeditor will try to discover the right path.<br />Available wildcard characters:<br /><strong>%d</strong> - server path to document root (!root).<br /><strong>%b</strong> - base URL path of the Drupal installation (!base).<br /><strong>%f</strong> - Drupal file system path where the files are stored (!files).<br /><strong>%u</strong> - User ID.<br />Current path: !path', array('!path' => $current_user_files_absolute_path, '!files' => file_directory_path(), '!base' => base_path(), '!root' => $_SERVER['DOCUMENT_ROOT'])),
       
  2150   );
       
  2151 
       
  2152   if (variable_get('file_downloads', '') == FILE_DOWNLOADS_PRIVATE) {
       
  2153     $form['fckeditor_upload_settings']['UserFilesPath']['#description'] = t('Setting relative path to uploaded files has been disabled because private downloads are enabled and this path is calculated automatically. To change the location of uploaded files in the private file system, edit the <a href="!url">FCKeditor Global Profile</a>.', array('!url' => url('admin/settings/fckeditor/editg')));
       
  2154     $form['fckeditor_upload_settings']['UserFilesPath']['#disabled'] = TRUE;
       
  2155     $form['fckeditor_upload_settings']['UserFilesAbsolutePath']['#description'] = t('Setting path to uploaded files has been disabled because private downloads are enabled and this path is calculated automatically.To change the location of uploaded files in the private file system, edit the <a href="!url">FCKeditor Global Profile</a>.', array('!global' => url('admin/settings/fckeditor/editg')));
       
  2156     $form['fckeditor_upload_settings']['UserFilesAbsolutePath']['#disabled'] = TRUE;
       
  2157   }
       
  2158 
       
  2159   $form['advanced'] = array(
       
  2160   '#type' => 'fieldset',
       
  2161   '#title' => t('Advanced options'),
       
  2162   '#collapsible' => TRUE,
       
  2163   '#collapsed' => TRUE,
       
  2164   );
       
  2165   $form['advanced']['theme_config_js'] = array(
       
  2166     '#title' => t('Load fckeditor.config.js from theme path'),
       
  2167     '#default_value' => !empty($edit->settings['theme_config_js']) ? $edit->settings['theme_config_js'] : 'f',
       
  2168     '#type' => 'select',
       
  2169     '#options' => array('f' => t('false'), 't' => t('true')),
       
  2170     '#description' => t('When set to "true" the editor will try to load the fckeditor.config.js file from theme directory.'),
       
  2171   );
       
  2172   $form['advanced']['js_conf'] = array(
       
  2173   '#type' => 'textarea',
       
  2174   '#title' => t('Custom javascript configuration'),
       
  2175   '#default_value' => !empty($edit->settings['js_conf']) ? $edit->settings['js_conf'] : "",
       
  2176   '#cols' => 60,
       
  2177   '#rows' => 5,
       
  2178   '#description' => t('Warning: to change FCKeditor configuration globally, you should modify the config file: <code>!fckeditor_config</code>.<br />Sometimes it is required to change the FCKeditor configuration for selected profile. Use this box to define settings that are uniqe for this profile.<br />Available options are listed in the !docs.<br />Warning: if you make something wrong here, FCKeditor may fail to load.<br />For example to disable some advanced tabs in dialog windows in FCKeditor, add the following: !example',
       
  2179   array(
       
  2180   '!fckeditor_config' => drupal_get_path('module', 'fckeditor') ."/fckeditor.config.js",
       
  2181   '!docs' => l(t("FCKeditor documentation"), "http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/Configuration/Configuration_Options"),
       
  2182   "!example" => "<pre>LinkDlgHideTarget = true ;
       
  2183 LinkDlgHideAdvanced = true ;
       
  2184 ImageDlgHideLink = true ;
       
  2185 ImageDlgHideAdvanced = true ;
       
  2186 FlashDlgHideAdvanced = true ;</pre>")
       
  2187 ));
       
  2188 
       
  2189   $form['submit'] = array(
       
  2190   '#type' => 'submit',
       
  2191   '#value' => $btn
       
  2192   );
       
  2193 
       
  2194 return $form;
       
  2195 }
       
  2196 
       
  2197 /**
       
  2198  * Search the field id for matches in array of matches
       
  2199  *
       
  2200  * @param $search
       
  2201  *   A string representing a form field id
       
  2202  * @ param $array
       
  2203  *   An $array with strings to match the $search parameter against
       
  2204  *
       
  2205  * @return
       
  2206  *   TRUE on match, FALSE on no match
       
  2207  */
       
  2208 function fckeditor_idsearch($search, $array) {
       
  2209   foreach ($array as $key => $value) {
       
  2210     if (!empty($value) && preg_match('/^'. str_replace('*', '.*', addslashes($value)) .'$/i', $search)) {
       
  2211       // on any first match we know we're done here so return positive
       
  2212       return TRUE;
       
  2213     }
       
  2214   }
       
  2215   return FALSE;
       
  2216 }
       
  2217 
       
  2218 /**
       
  2219  * Test if client can render the FCKeditor
       
  2220  * Use built-in test method in fckeditor.php
       
  2221  * If fckeditor.php is not found, return false (probably in such case fckeditor is not installed correctly)
       
  2222  *
       
  2223  * @return
       
  2224  *   TRUE if the browser is reasonably capable
       
  2225  */
       
  2226 function fckeditor_is_compatible_client() {
       
  2227   $fckeditor_main_file = drupal_get_path('module', 'fckeditor') .'/fckeditor/fckeditor.php';
       
  2228   if (!function_exists('version_compare') || version_compare(phpversion(), '5', '<')) {
       
  2229     $fckeditor_target_file = drupal_get_path('module', 'fckeditor') .'/fckeditor/fckeditor_php4.php';
       
  2230   }
       
  2231   else {
       
  2232     $fckeditor_target_file = drupal_get_path('module', 'fckeditor') .'/fckeditor/fckeditor_php5.php';
       
  2233   }
       
  2234 
       
  2235   if (file_exists($fckeditor_target_file)) {
       
  2236     include_once $fckeditor_target_file;
       
  2237     //FCKeditor 2.6.1+
       
  2238     if (function_exists('FCKeditor_IsCompatibleBrowser')) {
       
  2239       return FCKeditor_IsCompatibleBrowser();
       
  2240     }
       
  2241     else if (class_exists('FCKeditor')) {
       
  2242       //FCKeditor 2.6 with definition of FCKeditor_IsCompatibleBrowser() in fckeditor.php
       
  2243       if (filesize($fckeditor_main_file) > 1500) {
       
  2244         include_once $fckeditor_main_file;
       
  2245       }
       
  2246       //FCKeditor 2.5.1 and earlier
       
  2247       $fck = new FCKeditor('fake');
       
  2248       return $fck->IsCompatible();
       
  2249     }
       
  2250   }
       
  2251 
       
  2252   return FALSE;
       
  2253 }
       
  2254 
       
  2255 function fckeditor_user_get_setting($user, $profile, $setting) {
       
  2256   $default = array(
       
  2257   'default' => 't',
       
  2258   'show_toggle' => 't',
       
  2259   'popup' => 'f',
       
  2260   'skin' => 'default',
       
  2261   'toolbar' => 'default',
       
  2262   'expand' => 't',
       
  2263   'width' => '100%',
       
  2264   'lang' => 'en',
       
  2265   'auto_lang' => 't',
       
  2266   );
       
  2267   $settings = $profile->settings;
       
  2268 
       
  2269   if ($settings['allow_user_conf']) {
       
  2270     $status = isset($user->{"fckeditor_". $setting}) ? $user->{"fckeditor_". $setting} : (isset($settings[$setting]) ? $settings[$setting] : $default[$setting]);
       
  2271   }
       
  2272   else {
       
  2273     $status = isset($settings[$setting]) ? $settings[$setting] : $default[$setting];
       
  2274   }
       
  2275 
       
  2276   return $status;
       
  2277 }
       
  2278 
       
  2279 function fckeditor_user_get_profile($user) {
       
  2280   static $profile_name;
       
  2281 
       
  2282   // Since fckeditor_profile_load() makes a db hit, only call it when we're pretty sure
       
  2283   // we're gonna render fckeditor.
       
  2284   if (!isset($profile_name[$user->uid])) {
       
  2285     $sorted_roles = fckeditor_sorted_roles();
       
  2286     foreach ($sorted_roles as $rid => $name) {
       
  2287       if (isset($user->roles[$rid])) {
       
  2288         break;
       
  2289       }
       
  2290     }
       
  2291 
       
  2292     if (isset($rid) && isset($user->roles[$rid])) {
       
  2293       $profile_name[$user->uid] = db_result(db_query("SELECT s.name FROM {fckeditor_settings} s INNER JOIN {fckeditor_role} r ON r.name = s.name WHERE r.rid='%s'", $rid));
       
  2294     }
       
  2295     else if ($user->uid == "1") {
       
  2296       $profile_name[$user->uid] = db_result(db_query_range("SELECT s.name FROM {fckeditor_settings} s INNER JOIN {fckeditor_role} r ON r.name = s.name ORDER BY r.rid DESC", 1));
       
  2297     }
       
  2298   }
       
  2299 
       
  2300   if (isset($profile_name[$user->uid]) && $profile_name[$user->uid]) {
       
  2301     $profile = fckeditor_profile_load($profile_name[$user->uid]);
       
  2302     return $profile;
       
  2303   }
       
  2304 
       
  2305   return FALSE;
       
  2306 }
       
  2307 
       
  2308 function fckeditor_init() {
       
  2309   drupal_add_css(drupal_get_path('module', 'fckeditor') .'/fckeditor.css');
       
  2310 }
       
  2311 
       
  2312 function fckeditor_ask_delete_confirmation($is_global, $profile = "") {
       
  2313   if (!$is_global) {
       
  2314     $delete_link = l(t('Yes, delete!'), 'admin/settings/fckeditor/deleteconfirmed/'. urlencode($profile));
       
  2315     $profile_name = t('!profile profile', array('!profile' => $profile));
       
  2316   }
       
  2317   else {
       
  2318     $delete_link = l(t('Yes, delete!'), 'admin/settings/fckeditor/deletegconfirmed');
       
  2319     $profile_name = t('Global Profile');
       
  2320   }
       
  2321 
       
  2322   drupal_set_title(t('Confirm profile deletion'));
       
  2323   drupal_set_message(t("You're about to delete the FCKeditor profile, read the question below carefully."), "warning");
       
  2324 
       
  2325   return t("<p>Are you sure that you want to delete the !profile?</p><p>!yes !no</p>",
       
  2326     array('!profile' => $profile_name,
       
  2327           '!yes' => $delete_link,
       
  2328           '!no' => l(t('Cancel'), 'admin/settings/fckeditor', array('attributes' => array('style' => 'margin-left:40px'))),
       
  2329           ));
       
  2330 }
       
  2331 
       
  2332 /**
       
  2333  * Implementation of hook_file_download().
       
  2334  * Support for private downloads.
       
  2335  * FCKeditor does not implement any kind of potection on private files.
       
  2336  */
       
  2337 function fckeditor_file_download($file) {
       
  2338   if ($path = file_create_path($file)) {
       
  2339     $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $path);
       
  2340     if (db_fetch_object($result)) {
       
  2341         return NULL;
       
  2342     }
       
  2343 
       
  2344     //No info in DB? Probably a file uploaded with FCKeditor
       
  2345     $global_profile = fckeditor_profile_load("FCKeditor Global Profile");
       
  2346 
       
  2347     //Assume that files inside of fckeditor directory belong to the FCKeditor. If private directory is set, let the decision about protection to the user.
       
  2348     $private_dir = isset($global_profile->settings['private_dir']) ? $global_profile->settings['private_dir'] : "/";
       
  2349 
       
  2350     //If path to the file points to the FCKeditor private directory, allow downloading
       
  2351     if (strpos($path, file_directory_path() ."/". trim($private_dir, "/\\")) === 0) {
       
  2352       $ctype = ($info = @getimagesize($path)) ? $info['mime'] : (function_exists('mime_content_type') ? mime_content_type($path) : 'application/x-download');
       
  2353       return array('Content-type: '. $ctype);
       
  2354     }
       
  2355   }
       
  2356 }