#85051 initial update for 5.x functions
[project/fckeditor.git] / fckeditor.module
1 <?php
2 // $Id$
3 /**
4 * @file
5 * FCKeditor Module for Drupal 5.x
6 *
7 * This module allows Drupal to replace textarea fields with FCKeditor.
8 *
9 * This HTML text editor brings to the web many of the powerful functionalities
10 * of known desktop editors like Word. It's really lightweight and doesn't
11 * require any kind of installation on the client computer.
12 *
13 * ----------------------------------------------------------------------------
14 *
15 * FCKeditor - The text editor for internet
16 * Copyright (C) 2003-2006 Frederico Caldeira Knabben
17 *
18 * Licensed under the terms of the GNU Lesser General Public License:
19 * http://www.opensource.org/licenses/lgpl-license.php
20 *
21 * For further information visit:
22 * http://www.fckeditor.net/
23 *
24 * ----------------------------------------------------------------------------
25 *
26 * @version 1.0
27 * @author LatPro Inc (George)
28 * @version 1.1
29 * @author Frederico Caldeira Knabben (www.fckeditor.net)
30 * @version 1.2
31 * @author Ontwerpwerk (www.ontwerpwerk.nl)
32 */
33
34
35 /**
36 * Implementation of hook_help
37 */
38 function fckeditor_help($section = '') {
39 switch ($section) {
40 case 'admin/settings/help#description':
41 $output = t("Enables the usage of FCKeditor (WYSIWYG editor) instead of plain text fields.");
42 break;
43 case 'admin/settings/fckeditor':
44 $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>
45 <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')));
46 $output .= "<div class=\"more-link\">".l('more help...','admin/help/fckeditor')."</div>";
47 break;
48 case 'admin/help#fckeditor':
49 $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>
50 <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')));
51 $output .= t('<h3>Configuration</h3>
52 <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')));
53 $output .= t('<h3>Security</h3>
54 <p>Note that enabling file uploads is a security risk, please take care about to who and when you will grant access.</p>');
55 // the rest is untranslated for the moment
56 $output .= "<h3>How to enable the file browser (in FCKeditor 2.3.x)</h3>
57 <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>
58 <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>
59 <p>In this file you will need to enable the file browser:<br /><code>&#36;Config['Enabled'] = true;</code></p>
60 <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>
61 <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>";
62 break;
63 }
64 return $output;
65 }
66
67 /**
68 * Implementation of hook_perm
69 */
70 function fckeditor_perm() {
71 return array('use default fckeditor', 'use advanced fckeditor', 'allow fckeditor file uploads');
72 }
73
74
75 /**
76 * Implementation of textarea
77 */
78 function fckeditor_elements() {
79 $type = array();
80 if (user_access('use advanced fckeditor') || user_access('use default fckeditor')) {
81 // only roles with permission get the fckeditor
82 if (fckeditor_is_compatible_client()) {
83 // it would be useless to dig deeper if we're not able or allowed to
84 $type['textarea'] = array(
85 '#process' => array(
86 'fckeditor_process_textarea' => array()
87 ),
88 );
89 }
90 }
91 return $type;
92 }
93
94 function fckeditor_menu($may_cache) {
95 if ($may_cache) {
96 $items[] = array(
97 'path' => 'admin/settings/fckeditor',
98 'title' => t('FCKeditor'),
99 'callback' => 'drupal_get_form',
100 'callback arguments' => array('fckeditor_settings_form'),
101 'access' => user_access('administer site configuration'),
102 'description' => t("Enables the usage of FCKeditor (WYSIWYG editor) instead of plain text fields."),
103 );
104 }
105 return $items;
106 }
107
108 function fckeditor_settings_form() {
109 // Settings form
110 $toolbar_options = array(
111 'Default' => 'Default',
112 'DrupalFull' => 'Drupal Full',
113 'DrupalBasic' => 'Drupal Basic',
114 'Basic' => 'Basic',
115 );
116
117 // Generate the form - settings applying to all patterns first
118 $form['fckeditor_settings'] = array(
119 '#type' => 'fieldset',
120 '#weight' => -20,
121 '#title' => t('Basic settings'),
122 '#collapsible' => TRUE,
123 '#collapsed' => FALSE,
124 );
125 $form['fckeditor_settings']['fckeditor_popup'] = array(
126 '#type' => 'checkbox',
127 '#title' => t('Use FCKeditor in a popup window'),
128 '#default_value' => variable_get('fckeditor_popup', '0'),
129 '#description' => t('If this option is selected a link to a popup window will be used instead of a textarea replace.'),
130 );
131 $form['fckeditor_settings']['fckeditor_default_toolbar'] = array(
132 '#type' => 'select',
133 '#title' => t('Default Toolbar'),
134 '#default_value' => variable_get('fckeditor_default_toolbar', 'DrupalBasic'),
135 '#options' => $toolbar_options,
136 '#description' => t('Choose a default toolbar set.'),
137 );
138 $form['fckeditor_settings']['fckeditor_advanced_toolbar'] = array(
139 '#type' => 'select',
140 '#title' => t('Advanced Toolbar'),
141 '#default_value' => variable_get('fckeditor_advanced_toolbar', 'DrupalFull'),
142 '#options' => $toolbar_options,
143 '#description' => t('Choose a toolbar set for administrators.'),
144 );
145
146 $form['fckeditor_exclude_settings'] = array(
147 '#type' => 'fieldset',
148 '#weight' => -15,
149 '#title' => t('Exclusion settings'),
150 '#collapsible' => TRUE,
151 '#collapsed' => TRUE,
152 );
153 $form['fckeditor_exclude_settings']['fckeditor_minimum_rows'] = array(
154 '#type' => 'textfield',
155 '#title' => t('Minimum rows'),
156 '#default_value' => variable_get('fckeditor_minimum_rows', 5),
157 '#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."),
158 );
159 /**
160 * get excluded fields - so we can have normal textareas too
161 * split the phrase by any number of commas or space characters,
162 * which include " ", \r, \t, \n and \f
163 */
164 $form['fckeditor_exclude_settings']['fckeditor_exclude'] = array(
165 '#type' => 'textarea',
166 '#title' => t('Exclude fields'),
167 '#cols' => 60,
168 '#rows' => 5,
169 '#default_value' => variable_get("fckeditor_exclude", ''),
170 '#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)"),
171 );
172
173 $form['fckeditor_special_settings'] = array(
174 '#type' => 'fieldset',
175 '#weight' => -10,
176 '#title' => t('Advanced settings'),
177 '#collapsible' => TRUE,
178 '#collapsed' => TRUE,
179 );
180 $form['fckeditor_special_settings']['fckeditor_toolbar_start_expanded'] = array(
181 '#type' => 'checkbox',
182 '#title' => t('Start the toolbar expanded'),
183 '#default_value' => variable_get('fckeditor_toolbar_start_expanded', '1'),
184 '#description' => t('The toolbar start expanded or collapsed.'),
185 );
186 $form['fckeditor_special_settings']['fckeditor_width'] = array(
187 '#type' => 'textfield',
188 '#title' => t('Width'),
189 '#default_value' => variable_get("fckeditor_width", "100%"),
190 '#description' => t("Width in pixels or percent. Ex: 400 or 100%"),
191 );
192
193 return system_settings_form($form);
194 }
195
196
197 /**
198 * This function create the HTML objects required for the FCKeditor
199 *
200 * @param $element
201 * A fully populated form elment to add the editor to
202 * @return
203 * The same $element with extra FCKeditor markup and initialization
204 */
205 function fckeditor_process_textarea($element) {
206 $exclude = preg_split("/[\s,]+/", strip_tags(variable_get("fckeditor_exclude", '')));
207
208 if (($element['#rows'] > variable_get('fckeditor_minimum_rows', 5))
209 && !fckeditor_idsearch($element['#id'], $exclude)) {
210 // only replace textarea when it has enough rows and it is not in the exclusion list
211
212 // setting some variables
213 $module_drupal_path = drupal_get_path('module', 'fckeditor');
214 $module_full_path = base_path() . $module_drupal_path;
215 // get the default drupal files path
216 $files_path = file_directory_path();
217 // '-' in a javascript id will not work
218 $js_id = 'oFCKeditor_' . str_replace('-', '_', $element['#id']);
219
220 // configured in settings
221 $width = variable_get("fckeditor_width", '100%');
222
223 // sensible default for small toolbars
224 $height = $element['#rows'] * 14 + 100;
225
226 // nessecary because FCKeditor interferes with resize script
227 $element['#resizable'] = FALSE;
228
229 drupal_add_js($module_drupal_path . '/fckeditor/fckeditor.js');
230
231 if (user_access('use advanced fckeditor')) {
232 $toolbar = variable_get("fckeditor_advanced_toolbar", 'DrupalFull');
233 $height += 100; // sensible default for admin toolbars toolbars
234 }
235 else {
236 $toolbar = variable_get("fckeditor_default_toolbar", 'DrupalBasic');
237 }
238
239 $element['#suffix'] .= "\n<script type=\"text/javascript\">
240 var ".$js_id." = new FCKeditor( '".$element['#id']."' );
241 ".$js_id.".BasePath = '".$module_full_path."/fckeditor/';
242 ".$js_id.".Config['CustomConfigurationsPath'] = '".$module_full_path."/fckeditor.config.js';
243 ".$js_id.".ToolbarSet = '".$toolbar."';
244 ".$js_id.".Height = '".$height."';\n";
245
246 // add code for filebrowser for users that have access
247 if (user_access('allow fckeditor file uploads')) {
248 $element['#suffix'] .= $js_id.".Config['LinkBrowserURL'] = '".$module_full_path."/fckeditor/editor/filemanager/browser/default/browser.html?Connector=connectors/php/connector.php&ServerPath=/".$files_path."';
249 ".$js_id.".Config['ImageBrowserURL'] = '".$module_full_path."/fckeditor/editor/filemanager/browser/default/browser.html?Type=Image&Connector=connectors/php/connector.php&ServerPath=/".$files_path."';
250 ".$js_id.".Config['FlashBrowserURL'] = '".$module_full_path."/fckeditor/editor/filemanager/browser/default/browser.html?Type=Flash&Connector=connectors/php/connector.php&ServerPath=/".$files_path."';
251 ".$js_id.".Config['LinkUpload'] = false;
252 ".$js_id.".Config['ImageUpload'] = false;
253 ".$js_id.".Config['FlashUpload'] = false;\n";
254 } else {
255 $element['#suffix'] .= $js_id.".Config['LinkBrowser'] = false;
256 ".$js_id.".Config['ImageBrowser'] = false;
257 ".$js_id.".Config['FlashBrowser'] = false;
258 ".$js_id.".Config['LinkUpload'] = false;
259 ".$js_id.".Config['ImageUpload'] = false;
260 ".$js_id.".Config['FlashUpload'] = false;\n";
261 }
262
263 $element['#suffix'] .= "</script>\n";
264
265 if (variable_get('fckeditor_popup', '0')) {
266 // Add the script file with the popup open function.
267 drupal_add_js($module_drupal_path . '/fckeditor.popup.js');
268 $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>";
269 }
270 else {
271 // if no popup mode, add the editor
272 $element['#suffix'] .="<script type=\"text/javascript\">
273 ".$js_id.".ReplaceTextarea();
274 </script>\n";
275 }
276 }
277
278 return $element;
279 }
280
281 /**
282 * Search the field id for matches in array of matches
283 *
284 * @param $search
285 * A string representing a form field id
286 * @ param $array
287 * An $array with strings to match the $search parameter against
288 *
289 * @return
290 * TRUE on match, FALSE on no match
291 */
292 function fckeditor_idsearch($search, $array) {
293 foreach ($array as $key => $value) {
294 if (!empty($value) && preg_match('/^'.str_replace('*','.*',$value).'$/i', $search)) {
295 // on any first match we know we're done here so return positive
296 return true;
297 }
298 }
299 return false;
300 }
301
302 /**
303 * Test if client can render the FCKeditor
304 *
305 * Check the user agent for a matching browser,
306 * using HTTP_USER_AGENT because the browsers that are known to support FCKeditor are limited
307 *
308 * @return
309 * TRUE if the browser is reasonably capable
310 */
311 function fckeditor_is_compatible_client() {
312 $useragent = $_SERVER['HTTP_USER_AGENT'];
313
314 if (strpos($useragent, 'MSIE') !== false && strpos($useragent, 'mac') === false && strpos($useragent, 'Opera') === false) {
315 $browserversion = (int)substr($useragent, strpos($useragent, 'MSIE') + 5, 3);
316 return ($browserversion >= 5.5);
317 }
318 elseif (strpos($useragent, 'Gecko') !== false) {
319 $browserversion = (int)substr($useragent, strpos($useragent, 'Gecko/') + 6, 8);
320 return ($browserversion >= 20030210);
321 }
322 else
323 return false;
324 }