summaryrefslogtreecommitdiffstats
path: root/fckeditor.module
blob: 92d3bc8f397e7581843aadf1a0243c3ef2346935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
// $Id$
/**
 * @file
 * FCKeditor Module for Drupal 5.x
 *
 * This module allows Drupal to replace textarea fields with FCKeditor.
 *
 * This HTML text editor brings to the web many of the powerful functionalities
 * of known desktop editors like Word. It's really  lightweight and doesn't
 * require any kind of installation on the client computer.
 *
 * ----------------------------------------------------------------------------
 *
 * FCKeditor - The text editor for internet
 * Copyright (C) 2003-2006 Frederico Caldeira Knabben
 *
 * Licensed under the terms of the GNU Lesser General Public License:
 * 		http://www.opensource.org/licenses/lgpl-license.php
 *
 * For further information visit:
 * 		http://www.fckeditor.net/
 *
 * ----------------------------------------------------------------------------
 *
 * @version 1.0
 * @author  LatPro Inc (George)
 * @version 1.1
 * @author  Frederico Caldeira Knabben (www.fckeditor.net)
 * @version 1.2
 * @author Ontwerpwerk (www.ontwerpwerk.nl)
 */


/**
 * Implementation of hook_help
 */
function fckeditor_help($section = '') {
  switch ($section) {
    case 'admin/settings/help#description':
      $output = t("Enables the usage of FCKeditor (WYSIWYG editor) instead of plain text fields.");
      break;
    case 'admin/settings/fckeditor':
      $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>", array('!fckeditorlink'=>l(t('FCKeditor homepage'), 'http://www.fckeditor.net'), '!userguidelink'=>l(t('FCKeditor userguide'), 'http://wiki.fckeditor.net/UsersGuide')));
      $output .= "<div class=\"more-link\">".l('more help...','admin/help/fckeditor')."</div>";
      break;
    case 'admin/help#fckeditor':
      $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>", array('!fckeditorlink'=>l(t('FCKeditor homepage'), 'http://www.fckeditor.net'), '!userguidelink'=>l(t('FCKeditor userguide'), 'http://wiki.fckeditor.net/UsersGuide')));
      $output .= t('<h3>Configuration</h3>
<p>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: <code>!filter</code>. Users that need access to tables might be better of using Full HTML.</p>', array('!filter'=>htmlentities('<a> <em> <strong> <small> <sup> <sub> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <img> <br> <br /> <p> <div> <span> <b> <i>'), '!filterlink'=> l(t('filters'),'admin/filters')));
      $output .= t('<h3>Security</h3>
<p>Note that enabling file uploads is a security risk, please take care about to who and when you will grant access.</p>');
      // the rest is untranslated for the moment
      $output .= "<h3>How to enable the file browser (in FCKeditor 2.3.x)</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>To enable file browsing you need to edit the connector configuration file in your fckeditor module directory, the file should be in:<br /><code>/fckeditor/editor/filemanager/browser/default/connectors/php/config.php</code></p>
<p>In this file you will need to enable the file browser:<br /><code>&#36;Config['Enabled'] = true;</code></p>
<p>To use the drupal files directory you also need to comment out the following line in the connector configuration:<br /><code>//&#36;Config['UserFilesPath'] = '/UserFiles/';</code></p>
<p>Furthermore, you will need to create a <em>'File'</em>, <em>'Image'</em>, <em>'Flash'</em> and <em>'Media'</em> subdirectory in your drupal files directory ('".file_directory_path()."'). These directories must have the same privileges as the drupal files directory. In some cases these directories must be world writable (chmod 0777).</p>";
      break;
  }
  return $output;
}

/**
 * Implementation of hook_perm
 */
function fckeditor_perm() {
  return array('use default fckeditor', 'use advanced fckeditor', 'allow fckeditor file uploads');
}


/**
 * Implementation of textarea
 */
function fckeditor_elements() {
  $type = array();
  if (user_access('use advanced fckeditor') || user_access('use default fckeditor')) {
    // only roles with permission get the fckeditor
    if (fckeditor_is_compatible_client()) {
      // it would be useless to dig deeper if we're not able or allowed to
      $type['textarea'] = array(
        '#process' => array(
          'fckeditor_process_textarea' => array()
        ),
      );
    }
  }
  return $type;
}

function fckeditor_menu($may_cache) {
  if ($may_cache) {
  $items[] = array(
    'path' => 'admin/settings/fckeditor',
    'title' => t('FCKeditor'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array('fckeditor_settings_form'),
    'access' => user_access('administer site configuration'),
    'description' => t("Enables the usage of FCKeditor (WYSIWYG editor) instead of plain text fields."),
   );
  }
  return $items;
}

function fckeditor_settings_form() {
  // Settings form
  $toolbar_options = array(
    'Default' => 'Default',
    'DrupalFull' => 'Drupal Full',
    'DrupalBasic' => 'Drupal Basic',
    'Basic' => 'Basic',
  );

  // Generate the form - settings applying to all patterns first
  $form['fckeditor_settings'] = array(
    '#type' => 'fieldset',
    '#weight' => -20,
    '#title' => t('Basic settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['fckeditor_settings']['fckeditor_popup'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use FCKeditor in a popup window'),
    '#default_value' => variable_get('fckeditor_popup', '0'),
    '#description' => t('If this option is selected a link to a popup window will be used instead of a textarea replace.'),
  );
  $form['fckeditor_settings']['fckeditor_default_toolbar'] = array(
    '#type' => 'select',
    '#title' => t('Default Toolbar'),
    '#default_value' => variable_get('fckeditor_default_toolbar', 'DrupalBasic'),
    '#options' => $toolbar_options,
    '#description' => t('Choose a default toolbar set.'),
  );
  $form['fckeditor_settings']['fckeditor_advanced_toolbar'] = array(
    '#type' => 'select',
    '#title' => t('Advanced Toolbar'),
    '#default_value' => variable_get('fckeditor_advanced_toolbar', 'DrupalFull'),
    '#options' => $toolbar_options,
    '#description' => t('Choose a toolbar set for administrators.'),
  );

  $form['fckeditor_exclude_settings'] = array(
    '#type' => 'fieldset',
    '#weight' => -15,
    '#title' => t('Exclusion settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['fckeditor_exclude_settings']['fckeditor_minimum_rows'] = array(
    '#type' => 'textfield',
    '#title' => t('Minimum rows'),
    '#default_value' => variable_get('fckeditor_minimum_rows', 5),
    '#description' => t("Textareas must have a minimum of rows before FCKeditor will be triggered. Enter '1' if you do not want to use this feature."),
  );
  /**
   * get excluded fields - so we can have normal textareas too
   * split the phrase by any number of commas or space characters,
   * which include " ", \r, \t, \n and \f
   */
  $form['fckeditor_exclude_settings']['fckeditor_exclude'] = array(
    '#type' => 'textarea',
    '#title' => t('Exclude fields'),
    '#cols' => 60,
    '#rows' => 5,
    '#default_value' => variable_get("fckeditor_exclude", ''),
    '#description' => t("Names (HTML ID's) of fields that may not have an FCKeditor (separated by commas, spaces or newlines. You may also use * for a wildcard)"),
  );

  $form['fckeditor_special_settings'] = array(
    '#type' => 'fieldset',
    '#weight' => -10,
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['fckeditor_special_settings']['fckeditor_toolbar_start_expanded'] = array(
    '#type' => 'checkbox',
    '#title' => t('Start the toolbar expanded'),
    '#default_value' => variable_get('fckeditor_toolbar_start_expanded', '1'),
    '#description' => t('The toolbar start expanded or collapsed.'),
  );
  $form['fckeditor_special_settings']['fckeditor_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Width'),
    '#default_value' => variable_get("fckeditor_width", "100%"),
    '#description' => t("Width in pixels or percent. Ex: 400 or 100%"),
  );

  return system_settings_form($form);
}


/**
 * This function create the HTML objects required for the FCKeditor
 *
 * @param $element
 *   A fully populated form elment to add the editor to
 * @return
 *   The same $element with extra FCKeditor markup and initialization
 */
function fckeditor_process_textarea($element) {
  $exclude = preg_split("/[\s,]+/", strip_tags(variable_get("fckeditor_exclude", '')));

  if (($element['#rows'] > variable_get('fckeditor_minimum_rows', 5))
                  && !fckeditor_idsearch($element['#id'], $exclude)) {
    // only replace textarea when it has enough rows and it is not in the exclusion list

    // setting some variables
    $module_drupal_path	= drupal_get_path('module', 'fckeditor');
    $module_full_path	= base_path() . $module_drupal_path;
    // get the default drupal files path
    $files_path = file_directory_path();
    // '-' in a javascript id will not work
    $js_id = 'oFCKeditor_' . str_replace('-', '_', $element['#id']);

    // configured in settings
    $width = variable_get("fckeditor_width", '100%');

    // sensible default for small toolbars
    $height = $element['#rows'] * 14 + 100;

    // nessecary because FCKeditor interferes with resize script
    $element['#resizable'] = FALSE;

    drupal_add_js($module_drupal_path . '/fckeditor/fckeditor.js');

    if (user_access('use advanced fckeditor')) {
      $toolbar = variable_get("fckeditor_advanced_toolbar", 'DrupalFull');
      $height += 100; // sensible default for admin toolbars toolbars
    }
    else {
      $toolbar = variable_get("fckeditor_default_toolbar", 'DrupalBasic');
    }

  	$element['#suffix'] .= "\n<script type=\"text/javascript\">
var ".$js_id." = new FCKeditor( '".$element['#id']."' );
".$js_id.".BasePath	= '".$module_full_path."/fckeditor/';
".$js_id.".Config['CustomConfigurationsPath'] = '".$module_full_path."/fckeditor.config.js';
".$js_id.".ToolbarSet = '".$toolbar."';
".$js_id.".Height = '".$height."';\n";

    // add code for filebrowser for users that have access
    if (user_access('allow fckeditor file uploads')) {
      $element['#suffix'] .= $js_id.".Config['LinkBrowserURL'] = '".$module_full_path."/fckeditor/editor/filemanager/browser/default/browser.html?Connector=connectors/php/connector.php&ServerPath=/".$files_path."';
".$js_id.".Config['ImageBrowserURL'] = '".$module_full_path."/fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php&ServerPath=/".$files_path."';
".$js_id.".Config['FlashBrowserURL'] = '".$module_full_path."/fckeditor/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/php/connector.php&ServerPath=/".$files_path."';
".$js_id.".Config['LinkUpload'] = false;
".$js_id.".Config['ImageUpload'] = false;
".$js_id.".Config['FlashUpload'] = false;\n";
    } else {
      $element['#suffix'] .= $js_id.".Config['LinkBrowser'] = false;
".$js_id.".Config['ImageBrowser'] = false;
".$js_id.".Config['FlashBrowser'] = false;
".$js_id.".Config['LinkUpload'] = false;
".$js_id.".Config['ImageUpload'] = false;
".$js_id.".Config['FlashUpload'] = false;\n";
    }

    $element['#suffix'] .= "</script>\n";

  	if (variable_get('fckeditor_popup', '0')) {
  		// Add the script file with the popup open function.
      drupal_add_js($module_drupal_path . '/fckeditor.popup.js');
  		$element['#title'] .= " <span class=\"fckeditor_popuplink\">(<a href=\"#\" onclick=\"FCKeditor_OpenPopup('".$module_full_path."/fckeditor.popup.html?var=".$js_id."&el=".$element['#id']."');return false;\">" . t('Open rich editor') . "</a>)</span>";
  	}
    else {
      // if no popup mode, add the editor
   		$element['#suffix'] .="<script type=\"text/javascript\">
  ".$js_id.".ReplaceTextarea();
</script>\n";
    }
  }

  return $element;
}

/**
 * Search the field id for matches in array of matches
 *
 * @param $search
 *   A string representing a form field id
 * @ param $array
 *   An $array with strings to match the $search parameter against
 *
 * @return
 *   TRUE on match, FALSE on no match
 */
function fckeditor_idsearch($search, $array) {
  foreach ($array as $key => $value) {
    if (!empty($value) && preg_match('/^'.str_replace('*','.*',$value).'$/i', $search)) {
      // on any first match we know we're done here so return positive
      return true;
    }
  }
  return false;
}

/**
 * Test if client can render the FCKeditor
 *
 * Check the user agent for a matching browser,
 * using HTTP_USER_AGENT because the browsers that are known to support FCKeditor are limited
 *
 * @return
 *   TRUE if the browser is reasonably capable
 */
function fckeditor_is_compatible_client() {
  $useragent = $_SERVER['HTTP_USER_AGENT'];

  if (strpos($useragent, 'MSIE') !== false && strpos($useragent, 'mac') === false && strpos($useragent, 'Opera') === false) {
    $browserversion = (int)substr($useragent, strpos($useragent, 'MSIE') + 5, 3);
    return ($browserversion >= 5.5);
  }
  elseif (strpos($useragent, 'Gecko') !== false) {
    $browserversion = (int)substr($useragent, strpos($useragent, 'Gecko/') + 6, 8);
    return ($browserversion >= 20030210);
  }
  else
    return false;
}