#352938 by sun: Fixed JS error (blank page) in IE when plugins are loaded.
[project/wysiwyg.git] / editors / tinymce.inc
1 <?php
2 // $Id$
3
4
5 /**
6 * Plugin implementation of hook_editor().
7 *
8 * - Function signature and returned editor name nust match include filename.
9 * - Returns an array of supported editor versions along with support files.
10 *
11 * @todo wysiwyg_<editor>_alter() to add/inject optional libraries like gzip.
12 */
13 function wysiwyg_tinymce_editor() {
14 $editor = array();
15 $editor['tinymce'] = array(
16 // Required properties
17 'title' => 'TinyMCE',
18 'vendor url' => 'http://tinymce.moxiecode.com',
19 'download url' => 'http://tinymce.moxiecode.com/download.php',
20 'library path' => wysiwyg_get_path('tinymce') . '/jscripts/tiny_mce',
21 'libraries' => array( // We cannot assume that all editors need just one js library.
22 '' => array( // Key may be used in wysiwyg_tinymce_settings() for exec mode.
23 'title' => 'Minified',
24 'files' => array('tiny_mce.js'),
25 ),
26 'src' => array(
27 'title' => 'Source',
28 'files' => array('tiny_mce_src.js'),
29 ),
30 ),
31 'version callback' => 'wysiwyg_tinymce_version',
32 'themes callback' => 'wysiwyg_tinymce_themes',
33 'settings callback' => 'wysiwyg_tinymce_settings',
34 'plugin callback' => 'wysiwyg_tinymce_plugins',
35 'plugin settings callback' => 'wysiwyg_tinymce_plugin_settings',
36 'versions' => array( // Each version can override global editor properties.
37 '2.1' => array(
38 // 'include files' => array('tinymce-2.inc'),
39 'js files' => array('tinymce-2.js'),
40 'css files' => array('tinymce-2.css'),
41 'download url' => 'http://sourceforge.net/project/showfiles.php?group_id=103281&package_id=111430&release_id=557383',
42 ),
43 '3.2' => array(
44 // 'include files' => array('tinymce-3.inc'),
45 'js files' => array('tinymce-3.js'),
46 'css files' => array('tinymce-3.css'),
47 // 'plugin callback' => 'wysiwyg_tinymce_3_plugins',
48 'libraries' => array(
49 '' => array(
50 'title' => 'Minified',
51 'files' => array(
52 'tiny_mce.js' => array('preprocess' => FALSE),
53 ),
54 ),
55 'jquery' => array(
56 'title' => 'jQuery',
57 'files' => array('tiny_mce_jquery.js'),
58 ),
59 'src' => array(
60 'title' => 'Source',
61 'files' => array('tiny_mce_src.js'),
62 ),
63 ),
64 ),
65 ),
66 // Optional properties
67 // 'editor path' => wysiwyg_get_path('tinymce'), // Assumed by default.
68 // 'js path' => wysiwyg_get_path('editors/js'), // Assumed by default.
69 // 'css path' => wysiwyg_get_path('editors/css'), // Assumed by default.
70 );
71 return $editor;
72 }
73
74 /**
75 * Detect editor version.
76 *
77 * @param $editor
78 * An array containing editor properties as returned from hook_editor().
79 *
80 * @return
81 * The installed editor version.
82 */
83 function wysiwyg_tinymce_version($editor) {
84 $script = $editor['library path'] .'/tiny_mce.js';
85 $script = fopen($script, 'r');
86 // Version is contained in the first 80 chars.
87 $line = fgets($script, 80);
88 // 2.x: this.majorVersion="2";this.minorVersion="1.3"
89 // 3.x: majorVersion:'3',minorVersion:'2.0.1'
90 if (preg_match('@majorVersion[=:]["\'](\d).+?minorVersion[=:]["\']([\d\.]+)@', $line, $version)) {
91 fclose($script);
92 return $version[1] .'.'. $version[2];
93 }
94 }
95
96 /**
97 * Return runtime editor settings for a given wysiwyg profile.
98 *
99 * @param $editor
100 * A processed hook_editor() array of editor properties.
101 * @param $config
102 * An array containing wysiwyg editor profile settings.
103 * @param $theme
104 * The name of a theme/GUI/skin to use.
105 *
106 * @return
107 * A settings array to be populated in
108 * Drupal.settings.wysiwyg.configs.{editor}
109 */
110 function wysiwyg_tinymce_settings($editor, $config, $theme) {
111 $init = array(
112 'button_tile_map' => TRUE, // @todo Add a setting for this.
113 'document_base_url' => base_path(),
114 'entity_encoding' => 'raw',
115 'mode' => 'none',
116 'plugins' => array(),
117 'relative_urls' => FALSE,
118 'theme' => $theme,
119 'width' => '100%',
120 'strict_loading_mode' => TRUE,
121 );
122 if (isset($config['apply_source_formatting'])) {
123 $init['apply_source_formatting'] = $config['apply_source_formatting'];
124 }
125 if (isset($config['convert_fonts_to_spans'])) {
126 $init['convert_fonts_to_spans'] = $config['convert_fonts_to_spans'];
127 }
128 if (isset($config['language'])) {
129 $init['language'] = $config['language'];
130 }
131 if (isset($config['paste_auto_cleanup_on_paste'])) {
132 $init['paste_auto_cleanup_on_paste'] = $config['paste_auto_cleanup_on_paste'];
133 }
134 if (isset($config['preformatted'])) {
135 $init['preformatted'] = $config['preformatted'];
136 }
137 if (isset($config['remove_linebreaks'])) {
138 $init['remove_linebreaks'] = $config['remove_linebreaks'];
139 }
140 if (isset($config['verify_html'])) {
141 $init['verify_html'] = $config['verify_html'];
142 }
143
144 if (!empty($config['css_classes'])) {
145 $init['theme_advanced_styles'] = implode(';', array_filter(explode("\n", str_replace("\r", '', $config['css_classes']))));
146 }
147
148 if (isset($config['css_setting'])) {
149 if ($config['css_setting'] == 'theme') {
150 $css = path_to_theme() .'/style.css';
151 if (file_exists($css)) {
152 $init['content_css'] = base_path() . $css;
153 }
154 }
155 else if ($config['css_setting'] == 'self' && isset($config['css_path'])) {
156 $init['content_css'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
157 }
158 }
159
160 // Find the enabled buttons and the button row they belong on.
161 // Also map the plugin metadata for each button.
162 // @todo What follows is a pain; needs a rewrite.
163 if (!empty($config['buttons']) && is_array($config['buttons'])) {
164 // $init['buttons'] are stacked into $init['theme_advanced_buttons1'] later.
165 // @todo Add a toolbar designer based on jQuery UI.
166 $init['buttons'] = array();
167 // Only array keys in $init['extensions'] matter; added to $init['plugins']
168 // later.
169 $init['extensions'] = array();
170 // $init['extended_valid_elements'] are just stacked, unique'd later, and
171 // transformed into a comma-separated string in wysiwyg_add_editor_settings().
172 // @todo Needs a complete plugin API redesign using arrays for
173 // tag => attributes definitions and array_merge_recursive().
174 $init['extended_valid_elements'] = array();
175
176 $plugins = wysiwyg_get_plugins($editor['name']);
177 foreach ($config['buttons'] as $plugin => $buttons) {
178 foreach ($buttons as $button => $enabled) {
179 // Iterate separately over buttons and extensions properties.
180 foreach (array('buttons', 'extensions') as $type) {
181 // Skip unavailable plugins.
182 if (!isset($plugins[$plugin][$type][$button])) {
183 continue;
184 }
185 // Add buttons.
186 if ($type == 'buttons') {
187 $init['buttons'][] = $button;
188 }
189 // Add external plugins to the list of extensions.
190 if ($type == 'buttons' && empty($plugins[$plugin]['internal'])) {
191 $init['extensions'][_wysiwyg_tinymce_plugin_name('add', $plugin)] = 1;
192 }
193 // Add internal buttons that also need to be loaded as extension.
194 else if ($type == 'buttons' && !empty($plugins[$plugin]['load'])) {
195 $init['extensions'][$plugin] = 1;
196 }
197 // Add plain extensions.
198 else if ($type == 'extensions' && !empty($plugins[$plugin]['load'])) {
199 $init['extensions'][$plugin] = 1;
200 }
201 // Allow plugins to add valid HTML elements.
202 if (!empty($plugins[$plugin]['extended_valid_elements'])) {
203 $init['extended_valid_elements'] = array_merge($init['extended_valid_elements'], $plugins[$plugin]['extended_valid_elements']);
204 }
205 // Allow plugins to add or override global configuration settings.
206 if (!empty($plugins[$plugin]['options'])) {
207 $init = array_merge($init, $plugins[$plugin]['options']);
208 }
209 }
210 }
211 }
212 // Clean-up.
213 $init['extended_valid_elements'] = array_unique($init['extended_valid_elements']);
214 if ($init['extensions']) {
215 $init['plugins'] = array_keys($init['extensions']);
216 unset($init['extensions']);
217 } else {
218 unset($init['extensions']);
219 }
220 }
221
222 // Add theme-specific settings.
223 switch ($theme) {
224 case 'advanced':
225 $init += array(
226 'theme_advanced_resize_horizontal' => FALSE,
227 'theme_advanced_resizing_use_cookie' => FALSE,
228 'theme_advanced_path_location' => isset($config['path_loc']) ? $config['path_loc'] : 'bottom',
229 'theme_advanced_resizing' => isset($config['resizing']) ? $config['resizing'] : 1,
230 'theme_advanced_toolbar_location' => isset($config['toolbar_loc']) ? $config['toolbar_loc'] : 'top',
231 'theme_advanced_toolbar_align' => isset($config['toolbar_align']) ? $config['toolbar_align'] : 'left',
232 );
233 if (isset($config['block_formats'])) {
234 $init['theme_advanced_blockformats'] = $config['block_formats'];
235 }
236 if (isset($init['buttons'])) {
237 // These rows explicitly need to be set to be empty, otherwise TinyMCE
238 // loads its default buttons of the advanced theme for each row.
239 $init += array(
240 'theme_advanced_buttons1' => array(),
241 'theme_advanced_buttons2' => array(),
242 'theme_advanced_buttons3' => array(),
243 );
244 // @todo Allow to sort/arrange editor buttons.
245 for ($i = 0; $i < count($init['buttons']); $i++) {
246 $init['theme_advanced_buttons1'][] = $init['buttons'][$i];
247 }
248 }
249 break;
250 }
251 unset($init['buttons']);
252
253 // Convert the config values into the form expected by TinyMCE.
254 foreach ($init as $key => $value) {
255 if (is_bool($value)) {
256 continue;
257 }
258 if (is_array($value)) {
259 $init[$key] = implode(',', $init[$key]);
260 }
261 }
262
263 return $init;
264 }
265
266 /**
267 * Determine available editor themes or check/reset a given one.
268 *
269 * @param $editor
270 * A processed hook_editor() array of editor properties.
271 * @param $profile
272 * A wysiwyg editor profile.
273 *
274 * @return
275 * An array of theme names. The first returned name should be the default
276 * theme name.
277 */
278 function wysiwyg_tinymce_themes($editor, $profile) {
279 /*
280 $themes = array();
281 $dir = $editor['library path'] . '/themes/';
282 if (is_dir($dir) && $dh = opendir($dir)) {
283 while (($file = readdir($dh)) !== FALSE) {
284 if (!in_array($file, array('.', '..', 'CVS', '.svn')) && is_dir($dir . $file)) {
285 $themes[$file] = $file;
286 }
287 }
288 closedir($dh);
289 asort($themes);
290 }
291 return $themes;
292 */
293 return array('advanced', 'simple');
294 }
295
296 /**
297 * Build a JS settings array of external plugins that need to be loaded separately.
298 *
299 * TinyMCE requires that external plugins (i.e. not residing in the editor's
300 * directory) are loaded (once) after the editor has been initialized.
301 */
302 function wysiwyg_tinymce_plugin_settings($editor, $profile, $plugins) {
303 $settings = array();
304 foreach ($plugins as $name => $plugin) {
305 if (empty($plugin['internal'])) {
306 $settings[$name] = base_path() . $plugin['path'];
307 }
308 }
309 return $settings;
310 }
311
312 /**
313 * Add or remove leading hiven to/of external plugin names.
314 *
315 * TinyMCE requires that external plugins, which should not be loaded from
316 * its own plugin repository are prefixed with a hiven in the name.
317 *
318 * @param string $op
319 * Operation to perform, 'add' or 'remove' (hiven).
320 * @param string $name
321 * A plugin name.
322 */
323 function _wysiwyg_tinymce_plugin_name($op, $name) {
324 if ($op == 'add') {
325 if (strpos($name, '-') !== 0) {
326 return '-'. $name;
327 }
328 return $name;
329 }
330 else if ($op == 'remove') {
331 if (strpos($name, '-') === 0) {
332 return substr($name, 1);
333 }
334 return $name;
335 }
336 }
337
338 /**
339 * Return internal plugins for TinyMCE; semi-implementation of hook_wysiwyg_plugin().
340 */
341 function wysiwyg_tinymce_plugins($editor) {
342 $plugins = array(
343 'default' => array(
344 'path' => $editor['library path'] .'/themes/advanced',
345 'buttons' => array(
346 'bold' => t('Bold'), 'italic' => t('Italic'), 'underline' => t('Underline'),
347 'strikethrough' => t('Strike-through'),
348 'justifyleft' => t('Align left'), 'justifycenter' => t('Align center'), 'justifyright' => t('Align right'), 'justifyfull' => t('Justify'),
349 'bullist' => t('Bullet list'), 'numlist' => t('Numbered list'),
350 'outdent' => t('Outdent'), 'indent' => t('Indent'),
351 'undo' => t('Undo'), 'redo' => t('Redo'),
352 'link' => t('Link'), 'unlink' => t('Unlink'), 'anchor' => t('Anchor'),
353 'image' => t('Image'),
354 'cleanup' => t('Clean-up'),
355 'forecolor' => t('Forecolor'), 'backcolor' => t('Backcolor'),
356 'sup' => t('Sup'), 'sub' => t('Sub'),
357 'blockquote' => t('Blockquote'), 'code' => t('Source code'),
358 'hr' => t('Horizontal rule'),
359 'cut' => t('Cut'), 'copy' => t('Copy'), 'paste' => t('Paste'),
360 'visualaid' => t('Visual aid'),
361 'removeformat' => t('Remove format'),
362 'charmap' => t('Character map'),
363 'help' => t('Help')),
364 'internal' => TRUE,
365 ),
366 'advhr' => array(
367 'path' => $editor['library path'] .'/plugins/advhr',
368 'buttons' => array('advhr' => t('Advanced horizontal rule')),
369 'extended_valid_elements' => array('hr[class|width|size|noshade]'),
370 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advhr',
371 'internal' => TRUE,
372 'load' => TRUE,
373 ),
374 'advimage' => array(
375 'path' => $editor['library path'] .'/plugins/advimage',
376 'extensions' => array('advimage' => t('Advanced image')),
377 'extended_valid_elements' => array('img[src|alt|title|align|width|height|hspace|vspace|border|style|class|onmouseover|onmouseout|id|name]'),
378 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advimage',
379 'internal' => TRUE,
380 'load' => TRUE,
381 ),
382 'advlink' => array(
383 'path' => $editor['library path'] .'/plugins/advlink',
384 'extensions' => array('advlink' => t('Advanced link')),
385 'extended_valid_elements' => array('a[name|href|target|title|class|onfocus|onblur|onclick|ondlbclick|onmousedown|onmouseup|onmouseover|onmouseout|onkeypress|onkeydown|onkeyup|id|style|rel]'),
386 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/advlink',
387 'internal' => TRUE,
388 'load' => TRUE,
389 ),
390 'autosave' => array(
391 'path' => $editor['library path'] .'/plugins/autosave',
392 'extensions' => array('autosave' => t('Auto save')),
393 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/autosave',
394 'internal' => TRUE,
395 'load' => TRUE,
396 ),
397 'contextmenu' => array(
398 'path' => $editor['library path'] .'/plugins/contextmenu',
399 'extensions' => array('contextmenu' => t('Context menu')),
400 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/contextmenu',
401 'internal' => TRUE,
402 'load' => TRUE,
403 ),
404 'directionality' => array(
405 'path' => $editor['library path'] .'/plugins/directionality',
406 'buttons' => array('ltr' => t('Left-to-right'), 'rtl' => t('Right-to-left')),
407 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/directionality',
408 'internal' => TRUE,
409 'load' => TRUE,
410 ),
411 'emotions' => array(
412 'path' => $editor['library path'] .'/plugins/emotions',
413 'buttons' => array('emotions' => t('Emotions')),
414 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/emotions',
415 'internal' => TRUE,
416 'load' => TRUE,
417 ),
418 'flash' => array(
419 'path' => $editor['library path'] .'/plugins/flash',
420 'buttons' => array('flash' => t('Flash')),
421 'extended_valid_elements' => array('img[class|src|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|obj|param|embed]'),
422 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/flash',
423 'internal' => TRUE,
424 'load' => TRUE,
425 ),
426 'font' => array(
427 'path' => $editor['library path'] .'/plugins/font',
428 'buttons' => array('formatselect' => t('HTML block format'), 'fontselect' => t('Font'), 'fontsizeselect' => t('Font size'), 'styleselect' => t('Font style')),
429 'extended_valid_elements' => array('font[face|size|color|style],span[class|align|style]'),
430 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/font',
431 'internal' => TRUE,
432 ),
433 'fullscreen' => array(
434 'path' => $editor['library path'] .'/plugins/fullscreen',
435 'buttons' => array('fullscreen' => t('Fullscreen')),
436 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/fullscreen',
437 'internal' => TRUE,
438 'load' => TRUE,
439 ),
440 'inlinepopups' => array(
441 'path' => $editor['library path'] .'/plugins/inlinepopups',
442 'extensions' => array('inlinepopups' => t('Inline popups')),
443 'options' => array(
444 'dialog_type' => array('modal'),
445 ),
446 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/inlinepopups',
447 'internal' => TRUE,
448 'load' => TRUE,
449 ),
450 'insertdatetime' => array(
451 'path' => $editor['library path'] .'/plugins/insertdatetime',
452 'buttons' => array('insertdate' => t('Insert date'), 'inserttime' => t('Insert time')),
453 'options' => array(
454 'plugin_insertdate_dateFormat' => array('%Y-%m-%d'),
455 'plugin_insertdate_timeFormat' => array('%H:%M:%S'),
456 ),
457 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/insertdatetime',
458 'internal' => TRUE,
459 'load' => TRUE,
460 ),
461 'layer' => array(
462 'path' => $editor['library path'] .'/plugins/layer',
463 'buttons' => array('insertlayer' => t('Insert layer'), 'moveforward' => t('Move forward'), 'movebackward' => t('Move backward'), 'absolute' => t('Absolute')),
464 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/layer',
465 'internal' => TRUE,
466 'load' => TRUE,
467 ),
468 'paste' => array(
469 'path' => $editor['library path'] .'/plugins/paste',
470 'buttons' => array('pastetext' => t('Paste text'), 'pasteword' => t('Paste from Word'), 'selectall' => t('Select all')),
471 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/paste',
472 'internal' => TRUE,
473 'load' => TRUE,
474 ),
475 'preview' => array(
476 'path' => $editor['library path'] .'/plugins/preview',
477 'buttons' => array('preview' => t('Preview')),
478 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/preview',
479 'internal' => TRUE,
480 'load' => TRUE,
481 ),
482 'print' => array(
483 'path' => $editor['library path'] .'/plugins/print',
484 'buttons' => array('print' => t('Print')),
485 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/print',
486 'internal' => TRUE,
487 'load' => TRUE,
488 ),
489 'searchreplace' => array(
490 'path' => $editor['library path'] .'/plugins/searchreplace',
491 'buttons' => array('search' => t('Search'), 'replace' => t('Replace')),
492 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/searchreplace',
493 'internal' => TRUE,
494 'load' => TRUE,
495 ),
496 'style' => array(
497 'path' => $editor['library path'] .'/plugins/style',
498 'buttons' => array('styleprops' => t('Style properties')),
499 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/style',
500 'internal' => TRUE,
501 'load' => TRUE,
502 ),
503 'table' => array(
504 'path' => $editor['library path'] .'/plugins/table',
505 'buttons' => array('tablecontrols' => t('Table')),
506 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/table',
507 'internal' => TRUE,
508 'load' => TRUE,
509 ),
510 );
511 if ($editor['installed version'] > 3) {
512 $plugins['safari'] = array(
513 'path' => $editor['library path'] .'/plugins/safari',
514 'extensions' => array('safari' => t('Safari compatibility')),
515 'url' => 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/safari',
516 'internal' => TRUE,
517 'load' => TRUE,
518 );
519 }
520 return $plugins;
521 }
522