ckeditor.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * @file
  4. * Editor integration functions for CKEditor.
  5. */
  6. /**
  7. * Plugin implementation of hook_editor().
  8. */
  9. function wysiwyg_ckeditor_editor() {
  10. $editor['ckeditor'] = array(
  11. 'title' => 'CKEditor',
  12. 'vendor url' => 'http://ckeditor.com',
  13. 'download url' => 'http://ckeditor.com/download',
  14. 'libraries' => array(
  15. '' => array(
  16. 'title' => 'Default',
  17. 'files' => array(
  18. 'ckeditor.js' => array('preprocess' => FALSE),
  19. ),
  20. ),
  21. 'src' => array(
  22. 'title' => 'Source',
  23. 'files' => array(
  24. 'ckeditor_source.js' => array('preprocess' => FALSE),
  25. ),
  26. ),
  27. ),
  28. 'version callback' => 'wysiwyg_ckeditor_version',
  29. 'themes callback' => 'wysiwyg_ckeditor_themes',
  30. 'settings callback' => 'wysiwyg_ckeditor_settings',
  31. 'plugin callback' => 'wysiwyg_ckeditor_plugins',
  32. 'plugin settings callback' => 'wysiwyg_ckeditor_plugin_settings',
  33. 'proxy plugin' => array(
  34. 'drupal' => array(
  35. 'load' => TRUE,
  36. 'proxy' => TRUE,
  37. ),
  38. ),
  39. 'proxy plugin settings callback' => 'wysiwyg_ckeditor_proxy_plugin_settings',
  40. 'versions' => array(
  41. '3.0.0.3665' => array(
  42. 'js files' => array('ckeditor-3.0.js'),
  43. ),
  44. ),
  45. );
  46. return $editor;
  47. }
  48. /**
  49. * Detect editor version.
  50. *
  51. * @param $editor
  52. * An array containing editor properties as returned from hook_editor().
  53. *
  54. * @return
  55. * The installed editor version.
  56. */
  57. function wysiwyg_ckeditor_version($editor) {
  58. $library = $editor['library path'] . '/ckeditor.js';
  59. if (!file_exists($library)) {
  60. return;
  61. }
  62. $library = fopen($library, 'r');
  63. $max_lines = 8;
  64. while ($max_lines && $line = fgets($library, 500)) {
  65. // version:'CKEditor 3.0 SVN',revision:'3665'
  66. // version:'3.0 RC',revision:'3753'
  67. // version:'3.0.1',revision:'4391'
  68. if (preg_match('@version:\'(?:CKEditor )?([\d\.]+)(?:.+revision:\'([\d]+))?@', $line, $version)) {
  69. fclose($library);
  70. // Version numbers need to have three parts since 3.0.1.
  71. $version[1] = preg_replace('/^(\d+)\.(\d+)$/', '${1}.${2}.0', $version[1]);
  72. return $version[1] . '.' . $version[2];
  73. }
  74. $max_lines--;
  75. }
  76. fclose($library);
  77. }
  78. /**
  79. * Determine available editor themes or check/reset a given one.
  80. *
  81. * @param $editor
  82. * A processed hook_editor() array of editor properties.
  83. * @param $profile
  84. * A wysiwyg editor profile.
  85. *
  86. * @return
  87. * An array of theme names. The first returned name should be the default
  88. * theme name.
  89. */
  90. function wysiwyg_ckeditor_themes($editor, $profile) {
  91. // @todo Skins are not themes but this will do for now.
  92. $path = $editor['library path'] . '/skins/';
  93. if (file_exists($path) && ($dir_handle = opendir($path))) {
  94. $themes = array();
  95. while ($file = readdir($dir_handle)) {
  96. if (is_dir($path . $file) && substr($file, 0, 1) != '.' && $file != 'CVS') {
  97. $themes[] = $file;
  98. }
  99. }
  100. closedir($dir_handle);
  101. natcasesort($themes);
  102. $themes = array_values($themes);
  103. return !empty($themes) ? $themes : array('default');
  104. }
  105. else {
  106. return array('default');
  107. }
  108. }
  109. /**
  110. * Return runtime editor settings for a given wysiwyg profile.
  111. *
  112. * @param $editor
  113. * A processed hook_editor() array of editor properties.
  114. * @param $config
  115. * An array containing wysiwyg editor profile settings.
  116. * @param $theme
  117. * The name of a theme/GUI/skin to use.
  118. *
  119. * @return
  120. * A settings array to be populated in
  121. * Drupal.settings.wysiwyg.configs.{editor}
  122. */
  123. function wysiwyg_ckeditor_settings($editor, $config, $theme) {
  124. $settings = array(
  125. 'baseHref' => $GLOBALS['base_url'] . '/',
  126. 'width' => '100%',
  127. // For better compatibility with smaller textareas.
  128. 'resize_minWidth' => 450,
  129. 'height' => 420,
  130. // @todo Do not use skins as themes and add separate skin handling.
  131. 'theme' => 'default',
  132. 'skin' => !empty($theme) ? $theme : 'kama',
  133. // By default, CKEditor converts most characters into HTML entities. Since
  134. // it does not support a custom definition, but Drupal supports Unicode, we
  135. // disable at least the additional character sets. CKEditor always converts
  136. // XML default characters '&', '<', '>'.
  137. // @todo Check whether completely disabling ProcessHTMLEntities is an option.
  138. 'entities_latin' => FALSE,
  139. 'entities_greek' => FALSE,
  140. );
  141. // Add HTML block format settings; common block formats are already predefined
  142. // by CKEditor.
  143. if (isset($config['block_formats'])) {
  144. $block_formats = explode(',', drupal_strtolower($config['block_formats']));
  145. $predefined_formats = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'pre', 'address', 'div');
  146. foreach (array_diff($block_formats, $predefined_formats) as $tag) {
  147. $tag = trim($tag);
  148. $settings["format_$tag"] = array('element' => $tag);
  149. }
  150. $settings['format_tags'] = implode(';', $block_formats);
  151. }
  152. if (isset($config['apply_source_formatting'])) {
  153. $settings['apply_source_formatting'] = $config['apply_source_formatting'];
  154. }
  155. if (isset($config['css_setting'])) {
  156. // Versions below 3.0.1 could only handle one stylesheet.
  157. if (version_compare($editor['installed version'], '3.0.1.4391', '<')) {
  158. if ($config['css_setting'] == 'theme') {
  159. $settings['contentsCss'] = reset(wysiwyg_get_css());
  160. }
  161. elseif ($config['css_setting'] == 'self' && isset($config['css_path'])) {
  162. $settings['contentsCss'] = strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme()));
  163. }
  164. }
  165. else {
  166. if ($config['css_setting'] == 'theme') {
  167. $settings['contentsCss'] = wysiwyg_get_css();
  168. }
  169. elseif ($config['css_setting'] == 'self' && isset($config['css_path'])) {
  170. $settings['contentsCss'] = explode(',', strtr($config['css_path'], array('%b' => base_path(), '%t' => path_to_theme())));
  171. }
  172. }
  173. }
  174. if (isset($config['language'])) {
  175. $settings['language'] = $config['language'];
  176. }
  177. if (isset($config['resizing'])) {
  178. // CKEditor tests "!== false", so ensure it is a Boolean.
  179. $settings['resize_enabled'] = (bool) $config['resizing'];
  180. }
  181. if (isset($config['toolbar_loc'])) {
  182. $settings['toolbarLocation'] = $config['toolbar_loc'];
  183. }
  184. $settings['toolbar'] = array();
  185. if (!empty($config['buttons'])) {
  186. $extra_plugins = array();
  187. $plugins = wysiwyg_get_plugins($editor['name']);
  188. foreach ($config['buttons'] as $plugin => $buttons) {
  189. foreach ($buttons as $button => $enabled) {
  190. // Iterate separately over buttons and extensions properties.
  191. foreach (array('buttons', 'extensions') as $type) {
  192. // Skip unavailable plugins.
  193. if (!isset($plugins[$plugin][$type][$button])) {
  194. continue;
  195. }
  196. // Add buttons.
  197. if ($type == 'buttons') {
  198. $settings['toolbar'][] = $button;
  199. }
  200. // Add external Drupal plugins to the list of extensions.
  201. if ($type == 'buttons' && !empty($plugins[$plugin]['proxy'])) {
  202. $extra_plugins[] = $button;
  203. }
  204. // Add external plugins to the list of extensions.
  205. elseif ($type == 'buttons' && empty($plugins[$plugin]['internal'])) {
  206. $extra_plugins[] = $plugin;
  207. }
  208. // Add internal buttons that also need to be loaded as extension.
  209. elseif ($type == 'buttons' && !empty($plugins[$plugin]['load'])) {
  210. $extra_plugins[] = $plugin;
  211. }
  212. // Add plain extensions.
  213. elseif ($type == 'extensions' && !empty($plugins[$plugin]['load'])) {
  214. $extra_plugins[] = $plugin;
  215. }
  216. // Allow plugins to add or override global configuration settings.
  217. if (!empty($plugins[$plugin]['options'])) {
  218. $settings = array_merge($settings, $plugins[$plugin]['options']);
  219. }
  220. }
  221. }
  222. }
  223. if (!empty($extra_plugins)) {
  224. $settings['extraPlugins'] = implode(',', $extra_plugins);
  225. }
  226. }
  227. // For now, all buttons are placed into one row.
  228. $settings['toolbar'] = array($settings['toolbar']);
  229. return $settings;
  230. }
  231. /**
  232. * Build a JS settings array of native external plugins that need to be loaded separately.
  233. */
  234. function wysiwyg_ckeditor_plugin_settings($editor, $profile, $plugins) {
  235. $settings = array();
  236. foreach ($plugins as $name => $plugin) {
  237. // Register all plugins that need to be loaded.
  238. if (!empty($plugin['load'])) {
  239. $settings[$name] = array();
  240. // Add path for native external plugins.
  241. if (empty($plugin['internal']) && isset($plugin['path'])) {
  242. $settings[$name]['path'] = base_path() . $plugin['path'] . '/';
  243. }
  244. // Force native internal plugins to use the standard path.
  245. else {
  246. $settings[$name]['path'] = base_path() . $editor['library path'] . '/plugins/' . $name . '/';
  247. }
  248. // CKEditor defaults to 'plugin.js' on its own when filename is not set.
  249. if (!empty($plugin['filename'])) {
  250. $settings[$name]['fileName'] = $plugin['filename'];
  251. }
  252. }
  253. }
  254. return $settings;
  255. }
  256. /**
  257. * Build a JS settings array for Drupal plugins loaded via the proxy plugin.
  258. */
  259. function wysiwyg_ckeditor_proxy_plugin_settings($editor, $profile, $plugins) {
  260. $settings = array();
  261. foreach ($plugins as $name => $plugin) {
  262. // Populate required plugin settings.
  263. $settings[$name] = $plugin['dialog settings'] + array(
  264. 'title' => $plugin['title'],
  265. 'icon' => base_path() . $plugin['icon path'] . '/' . $plugin['icon file'],
  266. 'iconTitle' => $plugin['icon title'],
  267. // @todo These should only be set if the plugin defined them.
  268. 'css' => base_path() . $plugin['css path'] . '/' . $plugin['css file'],
  269. );
  270. }
  271. return $settings;
  272. }
  273. /**
  274. * Return internal plugins for this editor; semi-implementation of hook_wysiwyg_plugin().
  275. */
  276. function wysiwyg_ckeditor_plugins($editor) {
  277. $plugins = array(
  278. 'default' => array(
  279. 'buttons' => array(
  280. 'Bold' => t('Bold'), 'Italic' => t('Italic'), 'Underline' => t('Underline'),
  281. 'Strike' => t('Strike-through'),
  282. 'JustifyLeft' => t('Align left'), 'JustifyCenter' => t('Align center'), 'JustifyRight' => t('Align right'), 'JustifyBlock' => t('Justify'),
  283. 'BulletedList' => t('Bullet list'), 'NumberedList' => t('Numbered list'),
  284. 'Outdent' => t('Outdent'), 'Indent' => t('Indent'),
  285. 'Undo' => t('Undo'), 'Redo' => t('Redo'),
  286. 'Link' => t('Link'), 'Unlink' => t('Unlink'), 'Anchor' => t('Anchor'),
  287. 'Image' => t('Image'),
  288. 'TextColor' => t('Forecolor'), 'BGColor' => t('Backcolor'),
  289. 'Superscript' => t('Superscript'), 'Subscript' => t('Subscript'),
  290. 'Blockquote' => t('Blockquote'), 'Source' => t('Source code'),
  291. 'HorizontalRule' => t('Horizontal rule'),
  292. 'Cut' => t('Cut'), 'Copy' => t('Copy'), 'Paste' => t('Paste'),
  293. 'PasteText' => t('Paste Text'), 'PasteFromWord' => t('Paste from Word'),
  294. 'ShowBlocks' => t('Show blocks'),
  295. 'RemoveFormat' => t('Remove format'),
  296. 'SpecialChar' => t('Character map'),
  297. 'Format' => t('HTML block format'), 'Font' => t('Font'), 'FontSize' => t('Font size'), 'Styles' => t('Font style'),
  298. 'Table' => t('Table'),
  299. 'SelectAll' => t('Select all'), 'Find' => t('Search'), 'Replace' => t('Replace'),
  300. 'Flash' => t('Flash'), 'Smiley' => t('Smiley'),
  301. 'CreateDiv' => t('Div container'),
  302. 'Iframe' => t('iFrame'),
  303. 'Maximize' => t('Maximize'),
  304. 'SpellChecker' => t('Check spelling'), 'Scayt' => t('Check spelling as you type'),
  305. 'About' => t('About'),
  306. ),
  307. 'internal' => TRUE,
  308. ),
  309. );
  310. if (version_compare($editor['installed version'], '3.1.0.4885', '<')) {
  311. unset($plugins['default']['buttons']['CreateDiv']);
  312. }
  313. if (version_compare($editor['installed version'], '3.5.0.6260', '<')) {
  314. unset($plugins['default']['buttons']['Iframe']);
  315. }
  316. return $plugins;
  317. }