From 9ac9153f04b18ef6f1d4a1d23063c990228eebfd Mon Sep 17 00:00:00 2001 From: michal Date: Wed, 24 Aug 2011 11:06:21 +0200 Subject: [PATCH 3/3] Add configuration option to move CKEditor's plugins. --- ckeditor.module | 77 +++++++++++++++++++++++++++++++++++++- includes/ckeditor.admin.inc | 57 +++++++++++++++++++++++++++- includes/ckeditor.lib.inc | 88 ++++++++++++++++++++++++++++++------------- 3 files changed, 193 insertions(+), 29 deletions(-) diff --git a/ckeditor.module b/ckeditor.module index 6524eda..6ddc4de 100644 --- a/ckeditor.module +++ b/ckeditor.module @@ -297,6 +297,7 @@ function ckeditor_path($local = FALSE, $refresh = FALSE) { if ($refresh || (!$cke_path)) { $mod_path = drupal_get_path('module', 'ckeditor'); + $lib_path = 'sites/all/libraries'; $global_profile = ckeditor_profile_load('CKEditor Global Profile', $refresh); //default: path to ckeditor subdirectory in the ckeditor module directory (starting from the document root) @@ -312,7 +313,8 @@ function ckeditor_path($local = FALSE, $refresh = FALSE) { if (isset($gs['ckeditor_path'])) { $tmp_path = $gs['ckeditor_path']; - $tmp_path = strtr($tmp_path, array("%m" => $mod_path, "%l" => 'sites/all/libraries')); +// $tmp_path = strtr($tmp_path, array("%m" => $mod_path, "%l" => 'sites/all/libraries')); + $tmp_path = strtr($tmp_path, array("%b" => base_path(), "%m" => base_path() . $mod_path, "%l" => base_path() . $lib_path)); $tmp_path = str_replace('\\', '/', $tmp_path); $tmp_path = str_replace('//', '/', $tmp_path); $cke_path = $tmp_path; @@ -325,6 +327,9 @@ function ckeditor_path($local = FALSE, $refresh = FALSE) { if (FALSE !== strpos($gs['ckeditor_path'], "%l")) { $gs['ckeditor_local_path'] = strtr($gs['ckeditor_path'], array("%l" => 'sites/all/libraries')); } + /* if (FALSE !== strpos($gs['ckeditor_path'], "%l")) { + $gs['ckeditor_local_path'] = strtr($gs['ckeditor_path'], array("%l" => "sites/all/libraries")); + }*/ } } @@ -364,4 +369,74 @@ function ckeditor_features_api() { 'file' => drupal_get_path('module', 'ckeditor') .'/includes/ckeditor.features.inc', ) ); +} + +/** + * Read CKEditor plugins path from Global profile + * + * @return + * path to CKEditor plugins folder + */ +function ckeditor_plugins_path($local = FALSE, $refresh = FALSE) { + static $cke_plugins_path; + static $cke_plugins_local_path; + + if ($refresh || (!$cke_plugins_path)) { + $mod_path = drupal_get_path('module', 'ckeditor'); + $lib_path = 'sites/all/libraries'; + $global_profile = ckeditor_profile_load('CKEditor Global Profile', $refresh); + + //default: path to ckeditor subdirectory in the ckeditor module directory (starting from the document root) + //e.g. for http://example.com/drupal it will be /drupal/sites/all/modules/ckeditor/ckeditor + $cke_plugins_path = base_path() . $mod_path .'/plugins'; + + if ($global_profile) { + $gs = $global_profile->settings; + + if (isset($gs['ckeditor_plugins_path'])) { + $tmp_path = $gs['ckeditor_plugins_path']; + $tmp_path = strtr($tmp_path, array("%b" => base_path(), "%m" => base_path() . $mod_path, "%l" => base_path() . $lib_path)); + $tmp_path = str_replace('\\', '/', $tmp_path); + $tmp_path = str_replace('//', '/', $tmp_path); + $tmp_path = rtrim($tmp_path, ' \/'); + if (substr($tmp_path, 0, 1) != '/') { + $tmp_path = '/'. $tmp_path; //starts with '/' + } + $cke_plugins_path = $tmp_path; + + if (empty($gs['ckeditor_plugins_local_path'])) { + //fortunately wildcards are used, we can easily get the right server path + if (FALSE !== strpos($gs['ckeditor_plugins_path'], "%m")) { + $gs['ckeditor_plugins_local_path'] = strtr($gs['ckeditor_plugins_path'], array("%m" => $mod_path)); + } + if (FALSE !== strpos($gs['ckeditor_plugins_path'], "%b")) { + $gs['ckeditor_plugins_local_path'] = strtr($gs['ckeditor_plugins_path'], array("%b" => ".")); + } + if (FALSE !== strpos($gs['ckeditor_plugins_path'], "%l")) { + $gs['ckeditor_plugins_local_path'] = strtr($gs['ckeditor_plugins_path'], array("%l" => "sites/all/libraries")); + } + } + } + + //ckeditor_plugins_path is defined, but wildcards are not used, we need to try to find out where is + //the document root located and append ckeditor_plugins_path to it. + if (!empty($gs['ckeditor_plugins_local_path'])) { + $cke_plugins_local_path = $gs['ckeditor_plugins_local_path']; + } + elseif (!empty($gs['ckeditor_plugins_path'])) { + module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib'); + $local_path = ckeditor_resolve_url( $gs['ckeditor_plugins_path'] ."/" ); + if (FALSE !== $local_path) { + $cke_plugins_local_path = $local_path; + } + } + } + } + + if ($local) { + return $cke_plugins_local_path; + } + else { + return $cke_plugins_path; + } } \ No newline at end of file diff --git a/includes/ckeditor.admin.inc b/includes/ckeditor.admin.inc index c72cb90..88137f0 100644 --- a/includes/ckeditor.admin.inc +++ b/includes/ckeditor.admin.inc @@ -39,6 +39,7 @@ function ckeditor_admin_main() { $editor_path = ckeditor_path(TRUE); $ckconfig_file = $editor_path . '/config.js'; + //check if CKEditor plugin is installed if (!_ckeditor_requirements_isinstalled()) { drupal_set_message(t('Checking for %filename or %file.', array('%filename' => $ckconfig_file, '%file' => 'sites/all/libraries/ckeditor/ckeditor.js'))); @@ -158,12 +159,15 @@ function ckeditor_admin_global_profile_form($form, $form_state, $mode = 'add') { '#maxlength' => 128, '#description' => t('Path to CKEditor (the HTML editor, downloaded from ckeditor.com) relative to the document root.') . '
' . - t('Available placeholders:!m - path where CKEditor module is stored (!files).!l - path to libraries dir (sites/all/libraries)
Current path: !path', + //t('Available placeholders:!m - path where CKEditor module is stored (!files).!l - path to libraries dir (sites/all/libraries)
Current path: !path', + t('Available placeholders:!b - base URL path of the Drupal installation (!base).!m - base URL path where CKEditor module is stored (!files).!l - base URL path to libraries dir (!library).
Current path: !path', + array( '!m' => '
%m', '!l' => '
%l', '!path' => ckeditor_path(FALSE), '!files' => $module_drupal_path, + '!library' => base_path() . 'sites/all/libraries/', '!ckeditorcom' => 'http://ckeditor.com/download' ) ), @@ -178,6 +182,36 @@ function ckeditor_admin_global_profile_form($form, $form_state, $mode = 'add') { '#maxlength' => 128, '#description' => t('The path to the local directory (on the server) which points to the path defined above. Enter either an absolute server path or path relative to "index.php". If empty, CKEditor module will try to find the right path.
Current path: !path', array('!path' => ckeditor_path(TRUE))), ); + + $form['ckeditor_advanced_settings']['ckeditor_plugins_path'] = array( + '#type' => 'textfield', + '#title' => t('Path to CKEditor plugins dir'), + '#default_value' => !empty($profile->settings['ckeditor_plugins_path']) ? $profile->settings['ckeditor_plugins_path'] : '%m/plugins', + '#size' => 40, + '#maxlength' => 128, + '#description' => t('Path to CKEditor plugins dir relative to the document root.') . + '
'. + t('Available placeholders:!b - base URL path of the Drupal installation (!base).!m - base URL path where CKEditor module is stored (!files).!l - base URL path to libraries dir (!library).
Current path: !path', + array( + '!b' => '
%b', + '!m' => '
%m', + '!l' => '
%l', + '!path' => ckeditor_plugins_path(), + '!base' => base_path(), + '!files' => base_path() . $module_drupal_path , + '!library' => base_path() . 'sites/all/libraries/' + )) + ); + + $form['ckeditor_advanced_settings']['ckeditor_plugins_local_path'] = array( + '#type' => 'textfield', + '#title' => t('Local path to CKEditor plugins dir'), + '#default_value' => isset($profile->settings['ckeditor_plugins_local_path'])?$profile->settings['ckeditor_plugins_local_path']:'', + '#size' => 40, + '#maxlength' => 128, + '#description' => t('The path to the local directory (on the server) which points to the path defined above. Enter either an absolute server path or path relative to "index.php". If empty, CKEditor module will try to find the right path.
Current path: !path', array('!path' => ckeditor_plugins_path(TRUE))), + ); + //@todo DOWNLOAD API $current_private_dir = !empty($profile->settings['private_dir']) ? $profile->settings['private_dir'] : ''; $form['ckeditor_advanced_settings']['private_dir'] = array( @@ -244,6 +278,25 @@ function ckeditor_admin_global_profile_form_submit($form, &$form_state) { } //no slash at the end $edit['ckeditor_local_path'] = trim(rtrim($edit['ckeditor_local_path'], "/")); + //strip whitespaces + if (empty($edit['ckeditor_plugins_local_path'])) { + $edit['ckeditor_plugins_local_path'] = ''; + } + else { + $edit['ckeditor_plugins_local_path'] = trim($edit['ckeditor_plugins_local_path']); + } + + //strip slash from the end + if (empty($edit['ckeditor_plugins_path'])) { + $edit['ckeditor_plugins_path'] = ''; + } + $edit['ckeditor_plugins_path'] = trim(rtrim($edit['ckeditor_plugins_path'], "/")); + if ($edit['ckeditor_plugins_path'] && 0 !== strpos($edit['ckeditor_plugins_path'], "/") && 0 !== strpos($edit['ckeditor_plugins_path'], "%")) { + //ensure that slash is at the beginning + $edit['ckeditor_plugins_path'] = "/". $edit['ckeditor_plugins_path']; + } + //no slash at the end + $edit['ckeditor_plugins_path'] = trim(rtrim($edit['ckeditor_plugins_path'], "/")); $settings = ckeditor_admin_values_to_settings($edit); db_insert('ckeditor_settings') @@ -478,7 +531,7 @@ function ckeditor_admin_profile_form($form, $form_state, $profile = NULL) { drupal_add_js(array('ckeditor_uicolor' => $color_palette), 'setting'); } - $editor_path = ckeditor_path(FALSE); + $editor_path = ckeditor_path(TRUE); $module_drupal_path = drupal_get_path('module', 'ckeditor'); drupal_add_js('window.CKEDITOR_BASEPATH = "' . base_path() . $editor_path . '/"', array('type' => 'inline', 'weight' => -100)); drupal_add_js($editor_path . '/ckeditor.js', array('type' => 'file', 'preprocess' => FALSE)); diff --git a/includes/ckeditor.lib.inc b/includes/ckeditor.lib.inc index 938c0ef..8142955 100644 --- a/includes/ckeditor.lib.inc +++ b/includes/ckeditor.lib.inc @@ -318,6 +318,7 @@ function ckeditor_load_plugins($render = FALSE) { $editor_path = '%editor_path%'; $ckeditor_path = '%ckeditor_path%'; $plugin_dir = '%plugin_dir%'; + $plugin_dir_additional = ckeditor_plugins_path(TRUE, TRUE) . '/'; /* * External plugins @@ -330,7 +331,7 @@ function ckeditor_load_plugins($render = FALSE) { 'default' => 't' ); } - + if (module_exists('ckeditor_link') && file_exists(drupal_get_path('module', 'ckeditor_link') . '/plugins/link/plugin.js')) { $arr['ckeditor_link'] = array( 'name' => 'drupal_path', @@ -352,7 +353,7 @@ function ckeditor_load_plugins($render = FALSE) { /* * CKEditor build-in plugins */ - $_editor_path = ckeditor_path(FALSE) . '/'; + $_editor_path = ckeditor_path(TRUE) . '/'; if (file_exists($_editor_path . 'plugins/tableresize/plugin.js')) { $arr['tableresize'] = array( 'name' => 'tableresize', @@ -370,12 +371,12 @@ function ckeditor_load_plugins($render = FALSE) { 'default' => 'f' ); } - /* * CKEditor module plugins */ $_plugin_dir = $_editor_path . 'plugins/'; - if ($handle = opendir($_plugin_dir)) { + //if ($handle = opendir($_plugin_dir)) { + if (is_dir($plugin_dir) && $handle = opendir($plugin_dir)) { while (false !== ($file = readdir($handle))) { if (is_dir($_plugin_dir . $file) && file_exists($_plugin_dir . $file . '/plugin.js')){ $source = file_get_contents($_plugin_dir . $file . '/plugin.js'); @@ -407,6 +408,42 @@ function ckeditor_load_plugins($render = FALSE) { closedir($handle); } + /* + * CKEditor module plugins - additional directory + */ + if ($plugin_dir != $plugin_dir_additional && is_dir($plugin_dir_additional) && $handle = opendir($plugin_dir_additional)) { + while (false !== ($file = readdir($handle))) { + if (is_dir($plugin_dir_additional . $file) && file_exists($plugin_dir_additional . $file . '/plugin.js')){ + $source = file_get_contents($plugin_dir_additional . $file . '/plugin.js'); + $buttons = false; + if (preg_match_all('#\.addButton\([\s]*\'(.*?)\'#', $source, $matches)){ + $buttons = implode(', ', $matches[1]); + } + if (preg_match('#@file ([^\n\r]*)#', $source, $matches)) { + $arr[$file] = array( + 'name' => $file, + 'desc' => t($matches[1]), + 'path' => base_path() . $plugin_dir_additional . $file . '/', + 'default' => 'f' + ); + } + else { + $arr[$file] = array( + 'name' => $file, + 'desc' => t('Plugin file: ' . $file), + 'path' => base_path() . $plugin_dir_additional . $file . '/', + 'default' => 'f' + ); + } + if ($buttons){ + $arr[$file]['desc'] .= ' (buttons: ' . $buttons . ')'; + } + } + } + + closedir($handle); + } + /* * CKEditor plugins registered by hook */ @@ -434,11 +471,10 @@ function ckeditor_load_plugins($render = FALSE) { $arr = array_merge($arr, $plugins); ksort($arr); - + if ($render === TRUE){ $arr = ckeditor_plugins_render($arr); } - return $arr; } @@ -448,14 +484,15 @@ function ckeditor_load_plugins($render = FALSE) { function ckeditor_plugins_render($plugins) { $render = array(); $render["%base_path%"] = base_path(); - $render["%editor_path%"] = ckeditor_path(FALSE) . '/'; + $render["%editor_path%"] = ckeditor_path(TRUE) . '/'; $render["%ckeditor_path%"] = drupal_get_path('module', 'ckeditor'); $render["%plugin_dir%"] = $render["%editor_path%"] . 'plugins/'; + $render["%plugin_dir_extra%"] = ckeditor_plugins_path(TRUE) . '/'; foreach ((array) $plugins as $i => $plugin){ $plugins[$i]['path'] = str_replace(array_keys($render), array_values($render), $plugin['path']); + } - return $plugins; } @@ -511,7 +548,7 @@ function ckeditor_get_profile($input_format) { $select = db_select('ckeditor_settings', 's'); $select->join('ckeditor_input_format', 'f', 'f.name = s.name'); $result = $select->fields('s', array("name"))->condition('f.format', $input_format)->condition('f.name', 'CKEditor Global Profile', '<>')->range(0, 1)->execute()->fetchAssoc(); - + if ($result && $profile = ckeditor_profile_load($result['name'])) { return $profile; } @@ -635,8 +672,8 @@ function ckeditor_profiles_compile( $input_format = FALSE, $clear = FALSE ) { function ckeditor_profile_settings_compile( $global_profile, $profile ) { global $user, $language, $theme; - $currentTheme = variable_get('theme_default', $theme); - + $currentTheme = variable_get('theme_default', $theme); + $settings = array(); $conf = array(); $conf = $profile->settings; @@ -670,7 +707,6 @@ function ckeditor_profile_settings_compile( $global_profile, $profile ) { $module_full_path = $host . $module_drupal_path; $editor_path = ckeditor_path(FALSE); $editor_local_path = ckeditor_path(TRUE); - // get the default drupal files path $files_path = $host . variable_get('file_private_path', conf_path() . '/files'); @@ -690,11 +726,11 @@ function ckeditor_profile_settings_compile( $global_profile, $profile ) { $settings['shiftEnterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['shift_enter_mode'])); $settings['toolbarStartupExpanded'] = ( $conf['expand'] == 't' ); $settings['width'] = $conf['width']; - $settings['skin'] = $conf['skin']; + $settings['skin'] = $conf['skin']; $settings['format_tags'] = $conf['font_format']; //$settings['popup'] = $conf['popup']; $settings['show_toggle'] = $conf['show_toggle']; - + if (isset($conf['language_direction'])) { switch ($conf['language_direction']) { @@ -711,10 +747,10 @@ function ckeditor_profile_settings_compile( $global_profile, $profile ) { break; } } - + if (isset($conf['loadPlugins'])) { $settings['loadPlugins'] = ckeditor_plugins_render($conf['loadPlugins']); - + if (array_key_exists('media', $settings['loadPlugins'])){ module_load_include('inc', 'media', 'includes/media.browser'); $javascript = media_browser_js(); @@ -925,8 +961,8 @@ function ckeditor_profile_settings_compile( $global_profile, $profile ) { $settings['filebrowserImageBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|'))); break; case 'elfinder': - $settings['filebrowserImageBrowseUrl'] = $host ."index.php?q=elfinder&app=ckeditor"; - break; + $settings['filebrowserImageBrowseUrl'] = $host ."index.php?q=elfinder&app=ckeditor"; + break; /* MODULES NOT PORTED TO D7 case 'webfm': if (user_access('access webfm')) { @@ -961,8 +997,8 @@ function ckeditor_profile_settings_compile( $global_profile, $profile ) { $settings['filebrowserFlashBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|'))); break; case 'elfinder': - $settings['filebrowserFlashBrowseUrl'] = $host ."index.php?q=elfinder&app=ckeditor"; - break; + $settings['filebrowserFlashBrowseUrl'] = $host ."index.php?q=elfinder&app=ckeditor"; + break; /* MODULES NOT PORTED TO D7 case 'webfm': if (user_access('access webfm')) { @@ -1147,7 +1183,6 @@ function ckeditor_profile_settings_compile( $global_profile, $profile ) { } } } - return $settings; } @@ -1162,12 +1197,13 @@ function ckeditor_profile_settings_compile( $global_profile, $profile ) { * */ function ckeditor_load_by_field( $field, $format, $show_toggle = TRUE, $add_fields_to_toggle = FALSE ) { - global $theme; + + global $theme; static $processed_ids= array(); static $is_running = FALSE; $enabled = TRUE; $suffix = ''; - + if (!isset($field['#id'])) { return $field; } @@ -1191,7 +1227,7 @@ function ckeditor_load_by_field( $field, $format, $show_toggle = TRUE, $add_fiel if (isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled') { return $field; } - + if (!isset($processed_ids[$field['#id']])) { $processed_ids[$field['#id']] = array(); } @@ -1259,7 +1295,7 @@ function ckeditor_load_by_field( $field, $format, $show_toggle = TRUE, $add_fiel $editor_local_path = ckeditor_path(TRUE); // get the default drupal files path $files_path = $host . variable_get('file_private_path', conf_path() . '/files'); - + if (!$is_running) { drupal_add_js($module_drupal_path . '/includes/ckeditor.utils.js', array('type' => 'file', 'scope' => 'footer') ); //if ($settings['popup'] != 't') { @@ -1277,7 +1313,7 @@ function ckeditor_load_by_field( $field, $format, $show_toggle = TRUE, $add_fiel else { drupal_add_js($editor_path . '/ckeditor_basic.js', 'file'); }*/ - drupal_add_js(array('ckeditor' => array('module_path' => $module_full_path, 'editor_path' => base_path() . $editor_path . '/')), 'setting'); + drupal_add_js(array('ckeditor' => array('module_path' => $module_full_path, 'editor_path' => base_path() . $editor_local_path . '/')), 'setting'); /*if ($settings['popup'] == 't') { drupal_add_js(array('ckeditor' => array('editor_path' => $editor_path)), 'setting'); }*/ -- 1.7.4.msysgit.0