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