Projet

Général

Profil

Paste
Télécharger (50,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / includes / ckeditor.lib.inc @ 2e0f6994

1
<?php
2

    
3
/**
4
 * CKEditor - The text editor for the Internet - http://ckeditor.com
5
 * Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
6
 *
7
 * == BEGIN LICENSE ==
8
 *
9
 * Licensed under the terms of any of the following licenses of your
10
 * choice:
11
 *
12
 *  - GNU General Public License Version 2 or later (the "GPL")
13
 *    http://www.gnu.org/licenses/gpl.html
14
 *
15
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
16
 *    http://www.gnu.org/licenses/lgpl.html
17
 *
18
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
19
 *    http://www.mozilla.org/MPL/MPL-1.1.html
20
 *
21
 * == END LICENSE ==
22
 *
23
 * @file
24
 * CKEditor Module for Drupal 7.x
25
 *
26
 * This module allows Drupal to replace textarea fields with CKEditor.
27
 *
28
 * CKEditor is an online rich text editor that can be embedded inside web pages.
29
 * It is a WYSIWYG (What You See Is What You Get) editor which means that the
30
 * text edited in it looks as similar as possible to the results end users will
31
 * see after the document gets published. It brings to the Web popular editing
32
 * features found in desktop word processors such as Microsoft Word and
33
 * OpenOffice.org Writer. CKEditor is truly lightweight and does not require any
34
 * kind of installation on the client computer.
35
 */
36

    
37
/**
38
 * Guess the absolute server path to the document root
39
 * Usually it should return $_SERVER['DOCUMENT_ROOT']
40
 *
41
 * @todo Improve me!!
42
 * Returns absolute path or false on failure
43
 *
44
 * @return string|boolean
45
 */
46
function ckeditor_get_document_root_full_path() {
47
  $found = 0;
48
  $index_dir = realpath(dirname($_SERVER['SCRIPT_FILENAME'])); // {/dir1/dir2/home/drupal}/index.php
49
  if (getcwd() == $index_dir) {
50
    $found++;
51
  }
52
  $drupal_dir = base_path();
53
  $index_dir = str_replace('\\', "/", $index_dir);
54
  $drupal_dir = str_replace('\\', "/", $drupal_dir);
55
  $document_root_dir = $index_dir . '/' . str_repeat('../', substr_count($drupal_dir, '/') - 1);
56
  $document_root_dir = realpath($document_root_dir);
57
  $document_root_dir = rtrim($document_root_dir, '/\\');
58
  if ($document_root_dir == $_SERVER['DOCUMENT_ROOT']) {
59
    $found++;
60
  }
61
  $document_root_dir = str_replace('\\', '/', $document_root_dir);
62

    
63
  if ($document_root_dir != '') {
64
    $found++;
65
  }
66
  if (file_exists($document_root_dir)) {
67
    $found++;
68
  }
69
  if (file_exists($document_root_dir . base_path() . 'includes/bootstrap.inc')) {
70
    $found++;
71
  }
72

    
73
  if ($found >= 3) {
74
    return $document_root_dir;
75
  }
76
  else {
77
    return FALSE;
78
  }
79
}
80

    
81
/**
82
 * Emulates the asp Server.mapPath function.
83
 * Given an url path return the physical directory that it corresponds to.
84
 *
85
 * Returns absolute path or false on failure
86
 *
87
 * @param string $path
88
 * @return string|boolean
89
 */
90
function ckeditor_resolve_url($path) {
91
  if (function_exists('apache_lookup_uri')) {
92
    $info = @apache_lookup_uri($path);
93
    if (!$info) {
94
      return FALSE;
95
    }
96
    return $info->filename . $info->path_info;
97
  }
98

    
99
  $document_root = ckeditor_get_document_root_full_path();
100
  if ($document_root !== FALSE) {
101
    return $document_root . $path;
102
  }
103

    
104
  return FALSE;
105
}
106

    
107
/**
108
 * List of configured CKEditor toolbars
109
 *
110
 * @return array
111
 */
112
function ckeditor_load_toolbar_options() {
113
  $arr = array('Basic' => 'Basic', 'Full' => 'Full');
114
  $module_drupal_path = drupal_get_path('module', 'ckeditor');
115
  $editor_local_path = ckeditor_path('local');
116
  $ckconfig_js = $editor_local_path . '/config.js';
117
  $ckeditor_config_js = $module_drupal_path . '/ckeditor.config.js';
118
  if ($editor_local_path != '<URL>' && file_exists($ckconfig_js) && is_readable($ckconfig_js)) {
119
    $fp = @fopen($ckconfig_js, "r");
120
    if ($fp) {
121
      while (!feof($fp)) {
122
        $line = fgets($fp, 1024);
123
        $matches = array();
124
        if (preg_match('/config.toolbar_([a-z0-9_]+)/i', $line, $matches)) {
125
          $arr[$matches[1]] = drupal_ucfirst($matches[1]);
126
        }
127
      }
128
      fclose($fp);
129
    }
130
  }
131
  if (file_exists($ckeditor_config_js) && is_readable($ckeditor_config_js)) {
132
    $fp = @fopen($ckeditor_config_js, "r");
133
    if ($fp) {
134
      while (!feof($fp)) {
135
        $line = fgets($fp, 1024);
136
        $matches = array();
137
        if (preg_match('/config.toolbar_([a-z0-9_]+)/i', $line, $matches)) {
138
          $arr[$matches[1]] = drupal_ucfirst($matches[1]);
139
        }
140
      }
141
      fclose($fp);
142
    }
143
  }
144

    
145
  //oops, we have no information about toolbars, let's use hardcoded array
146
  if (empty($arr)) {
147
    $arr = array(
148
      'Basic' => 'Basic',
149
      'Default' => 'Default',
150
    );
151
  }
152
  asort($arr);
153

    
154
  return $arr;
155
}
156

    
157
/**
158
 * List of installed CKEditor skins
159
 *
160
 * @return array
161
 */
162
function ckeditor_load_skin_options() {
163
  $arr = array();
164
  $editor_local_path = ckeditor_path('local');
165
  $skin_dir = $editor_local_path . '/skins';
166
  if ($editor_local_path != '<URL>' && is_dir($skin_dir)) {
167
    $dh = @opendir($skin_dir);
168
    if (FALSE !== $dh) {
169
      while (($file = readdir($dh)) !== FALSE) {
170
        if (in_array($file, array(".", "..", "CVS", ".svn"))) {
171
          continue;
172
        }
173
        if (is_dir($skin_dir . DIRECTORY_SEPARATOR . $file)) {
174
          $arr[$file] = drupal_ucfirst($file);
175
        }
176
      }
177
      closedir($dh);
178
    }
179
  }
180

    
181
  // If we have no information about skins, use the default for this version.
182
  if (!$arr) {
183
    $version = explode('.', ckeditor_get_version());
184
    if ($version[0] == 3) {
185
      $arr = array('kama' => 'Kama');
186
    }
187
    elseif ($version[0] == 4) {
188
      $arr = ($version[1] <= 5) ? array('moono' => 'Moono') : array('moono-lisa' => 'Moono-lisa');
189
    }
190
  }
191
  asort($arr);
192

    
193
  return $arr;
194
}
195

    
196
/**
197
 * Return default skin for CKEditor
198
 *
199
 * @return string
200
 */
201
function ckeditor_default_skin() {
202
  // If there is no available list then bail.
203
  if (!$skin_options = ckeditor_load_skin_options()) {
204
    return '';
205
  }
206

    
207
  // Default skins in descending order
208
  $defaults = array(
209
    'moono-lisa' => 'Moono-lisa',
210
    'moono' => 'Moono',
211
    'kama' => 'Kama'
212
  );
213

    
214
  // If there is more than one skin to choose from, use the most current
215
  // default.
216
  if ($skin_options && ($default = array_intersect_key($defaults, $skin_options))) {
217
    return key($default);
218
  }
219

    
220
  // If there are no defaults, or only one skin, select the first from the list.
221
  return key($skin_options);
222
}
223

    
224
/**
225
 * List of installed CKEditor languages
226
 *
227
 * @return array
228
 */
229
function ckeditor_load_lang_options() {
230
  $arr = array();
231
  $editor_local_path = ckeditor_path('local');
232
  $lang_file = $editor_local_path . '/lang/_languages.js';
233
  if ($editor_local_path != '<URL>' && file_exists($lang_file)) {
234
    $f = fopen($lang_file, 'r');
235
    $file = fread($f, filesize($lang_file));
236
    $tmp = explode('{', $file);
237
    if (isset($tmp[2])) {
238
      $tmp = explode('}', $tmp[2]);
239
    }
240
    $langs = explode(',', $tmp[0]);
241
    foreach ($langs AS $key => $lang) {
242
      preg_match("/'?(\w+-?\w+)'?:'([\w\s\(\)]+)'/i", $lang, $matches);
243
      if (isset($matches[1]) && isset($matches[2]))
244
        $arr[$matches[1]] = $matches[2];
245
    }
246
  }
247
  //oops, we have no information about languages, let's use those available in CKEditor 2.4.3
248
  if (empty($arr)) {
249
    $arr = array(
250
      'af' => 'Afrikaans',
251
      'ar' => 'Arabic',
252
      'bg' => 'Bulgarian',
253
      'bn' => 'Bengali/Bangla',
254
      'bs' => 'Bosnian',
255
      'ca' => 'Catalan',
256
      'cs' => 'Czech',
257
      'da' => 'Danish',
258
      'de' => 'German',
259
      'el' => 'Greek',
260
      'en' => 'English',
261
      'en-au' => 'English (Australia)',
262
      'en-ca' => 'English (Canadian)',
263
      'en-uk' => 'English (United Kingdom)',
264
      'eo' => 'Esperanto',
265
      'es' => 'Spanish',
266
      'et' => 'Estonian',
267
      'eu' => 'Basque',
268
      'fa' => 'Persian',
269
      'fi' => 'Finnish',
270
      'fo' => 'Faroese',
271
      'fr' => 'French',
272
      'gl' => 'Galician',
273
      'he' => 'Hebrew',
274
      'hi' => 'Hindi',
275
      'hr' => 'Croatian',
276
      'hu' => 'Hungarian',
277
      'it' => 'Italian',
278
      'ja' => 'Japanese',
279
      'km' => 'Khmer',
280
      'ko' => 'Korean',
281
      'lt' => 'Lithuanian',
282
      'lv' => 'Latvian',
283
      'mn' => 'Mongolian',
284
      'ms' => 'Malay',
285
      'nb' => 'Norwegian Bokmal',
286
      'nl' => 'Dutch',
287
      'no' => 'Norwegian',
288
      'pl' => 'Polish',
289
      'pt' => 'Portuguese (Portugal)',
290
      'pt-br' => 'Portuguese (Brazil)',
291
      'ro' => 'Romanian',
292
      'ru' => 'Russian',
293
      'sk' => 'Slovak',
294
      'sl' => 'Slovenian',
295
      'sr' => 'Serbian (Cyrillic)',
296
      'sr-latn' => 'Serbian (Latin)',
297
      'sv' => 'Swedish',
298
      'th' => 'Thai',
299
      'tr' => 'Turkish',
300
      'uk' => 'Ukrainian',
301
      'vi' => 'Vietnamese',
302
      'zh' => 'Chinese Traditional',
303
      'zh-cn' => 'Chinese Simplified',
304
    );
305
  }
306
  asort($arr);
307
  return $arr;
308
}
309

    
310
/**
311
 * Get the language locale code supported by Scayt for the specified language
312
 */
313
function ckeditor_scayt_langcode($lang) {
314
  $scayt_langs = array(
315
    'en' => 'en_US',
316
    'es' => 'es_ES',
317
    'fr' => 'fr_FR',
318
    'de' => 'de_DE',
319
    'it' => 'it_IT',
320
    'el' => 'el_EL',
321
    'pt' => 'pt_PT',
322
    'da' => 'da_DA',
323
    'sv' => 'sv_SE',
324
    'nl' => 'nl_NL',
325
    'nb' => 'no_NO',
326
    'fi' => 'fi_FI',
327
  );
328
  if (array_key_exists($lang, $scayt_langs)) {
329
    return $scayt_langs[$lang];
330
  }
331

    
332
  $default = language_default();
333
  $default = $default->language;
334
  if (array_key_exists($default, $scayt_langs)) {
335
    return $scayt_langs[$default];
336
  }
337

    
338
  return 'en_US';
339
}
340

    
341
/**
342
 * List of CKEditor plugins.
343
 *
344
 * @param boolean $render
345
 *   Whether to generate plugin paths. Default to FALSE.
346
 * @return array
347
 */
348
function ckeditor_load_plugins($render = FALSE) {
349
  $plugins = module_invoke_all('ckeditor_plugin');
350

    
351
  // [#2159403] introduced a slight change in CKEditor API, expecting the path
352
  // to a plugin returned by hook_ckeditor_plugin() to be a full URL. This
353
  // breaks backward compatibility for external plugins, so we're fixing it
354
  // below.
355
  foreach ($plugins as $i => $plugin) {
356
    if (strpos($plugin['path'], '%') !== 0 && !preg_match('|^(http(s)?:)?\/?\/|i', $plugin['path'])) {
357
      $plugins[$i]['path'] = '%base_path%' . $plugin['path'];
358
    }
359
  }
360

    
361
  drupal_alter('ckeditor_plugin', $plugins);
362

    
363
  ksort($plugins);
364

    
365
  if ($render) {
366
    $plugins = ckeditor_plugins_render($plugins);
367
  }
368

    
369
  return $plugins;
370
}
371

    
372
/**
373
 * Render CKEditor plugins path
374
 */
375
function ckeditor_plugins_render($plugins) {
376
  $render = array();
377
  $render["%base_path%"] = ckeditor_base_path('relative') . '/';
378
  $render["%editor_path%"] = ckeditor_path('relative') . '/';
379
  $render["%module_path%"] = ckeditor_module_path('relative') . '/';
380
  $render["%plugin_dir%"] = $render["%module_path%"] . 'plugins/';
381
  $render["%plugin_dir_extra%"] = ckeditor_plugins_path('relative') . '/';
382

    
383
  foreach ((array) $plugins as $i => $plugin) {
384
    $plugins[$i]['path'] = str_replace(array_keys($render), array_values($render), $plugin['path']);
385
  }
386

    
387
  return $plugins;
388
}
389

    
390
/**
391
 * Get default ckeditor settings
392
 *
393
 * @return array
394
 */
395
function ckeditor_user_get_setting_default() {
396
  $default = array(
397
    'default' => 't',
398
    'show_toggle' => 't',
399
    'width' => '100%',
400
    'lang' => 'en',
401
    'auto_lang' => 't',
402
  );
403

    
404
  // Allow other modules to alter the default settings.
405
  drupal_alter('ckeditor_default_settings', $default);
406

    
407
  return $default;
408
}
409

    
410
/**
411
 * Return CKEditor settings
412
 *
413
 * @param object $user
414
 * @param object $profile
415
 * @param string $setting
416
 * @return array
417
 */
418
function ckeditor_user_get_setting($user, $profile, $setting) {
419
  $default = ckeditor_user_get_setting_default();
420

    
421
  if (user_access('customize ckeditor')) {
422
    $status = isset($user->data['ckeditor_' . $setting]) ? $user->data['ckeditor_' . $setting] : (isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting]);
423
  }
424
  else {
425
    $status = isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting];
426
  }
427

    
428
  return $status;
429
}
430

    
431
/**
432
 * Return CKEditor profile by input format
433
 *
434
 * @param string $input_format
435
 * @return object|boolean
436
 */
437
function ckeditor_get_profile($input_format) {
438
  $select = db_select('ckeditor_settings', 's');
439
  $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
440
  $result = $select->fields('s', array("name"))->condition('f.format', $input_format)->condition('f.name', 'CKEditor Global Profile', '<>')->range(0, 1)->execute()->fetchAssoc();
441

    
442
  if ($result && $profile = ckeditor_profile_load($result['name'])) {
443
    return $profile;
444
  }
445

    
446
  return FALSE;
447
}
448

    
449
/**
450
 * Return CKEditor profile list
451
 */
452
function ckeditor_profile_input_formats() {
453
  $select = db_select('ckeditor_settings', 's');
454
  $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
455
  $result = $select->fields('s', array("name"))->fields('f', array("format"))->execute();
456

    
457
  $list = array();
458
  while ($row = $result->fetchAssoc()) {
459
    if (!isset($row['name'])) {
460
      $list[$row['name']] = array();
461
    }
462
    $list[$row['name']][] = $row['format'];
463
  }
464

    
465
  return $list;
466
}
467

    
468
/**
469
 * Search and return CKEditor plugin path
470
 *
471
 * @return string
472
 */
473
function _ckeditor_script_path() {
474
  $jspath = '';
475
  $module_path = drupal_get_path('module', 'ckeditor');
476

    
477
  if (file_exists($module_path . '/ckeditor/ckeditor.js')) {
478
    $jspath = '%m/ckeditor';
479
  }
480
  elseif (file_exists($module_path . '/ckeditor/ckeditor/ckeditor.js')) {
481
    $jspath = '%m/ckeditor/ckeditor';
482
  }
483
  elseif (file_exists(ckeditor_library_path('url') . '/ckeditor/ckeditor.js')) {
484
    $jspath = '%l/ckeditor';
485
  }
486
  return $jspath;
487
}
488

    
489
/**
490
 * Determines whether the CKEditor sources are present
491
 *
492
 * It checks if ckeditor.js is present.
493
 *
494
 * This function is used by ckeditor_requirements()
495
 *
496
 * @return boolean True if CKEditor is installed
497
 */
498
function _ckeditor_requirements_isinstalled() {
499
  $editor_path = ckeditor_path('local');
500
  if ($editor_path == '<URL>')
501
    return TRUE;
502
  $jspath = $editor_path . '/ckeditor.js';
503

    
504
  $jsp = file_exists($jspath);
505
  if (!$jsp && ($editor_path = _ckeditor_script_path())) {
506
    $result = db_select('ckeditor_settings', 's')->fields('s')->condition('name', 'CKEditor Global Profile')->execute()->fetchAssoc();
507
    if ($result) {
508
      $result['settings'] = unserialize($result['settings']);
509
      $result['settings']['ckeditor_path'] = $editor_path;
510
      $result['settings'] = serialize($result['settings']);
511
      db_update('ckeditor_settings')->fields(array("settings" => $result['settings']))->condition('name', 'CKEditor Global Profile')->execute();
512

    
513
      $jsp = TRUE;
514
      ckeditor_path('local', TRUE);
515
    }
516
  }
517
  return $jsp;
518
}
519

    
520
/**
521
 * Compile settings of all profiles at returns is as array
522
 *
523
 * @param string $input_format
524
 * @param boolean $clear
525
 * @return array
526
 */
527
function ckeditor_profiles_compile($input_format = FALSE, $clear = FALSE) {
528
  static $compiled = FALSE;
529
  static $_ckeditor_compiled = array();
530

    
531
  if ($clear !== FALSE && $compiled !== FALSE) {
532
    $compiled = FALSE;
533
  }
534

    
535
  if ($compiled === TRUE) {
536
    return ( $input_format === FALSE ) ? $_ckeditor_compiled : ( isset($_ckeditor_compiled[$input_format]) ? $_ckeditor_compiled[$input_format] : array() );
537
  }
538

    
539
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
540

    
541
  $profiles_list = ckeditor_profile_input_formats();
542

    
543
  foreach ($profiles_list AS $_profile => $_inputs) {
544
    $profile = ckeditor_profile_load($_profile);
545
    $setting = ckeditor_profile_settings_compile($global_profile, $profile);
546

    
547
    foreach ($_inputs AS $_input) {
548
      $_ckeditor_compiled[$_input] = $setting;
549
    }
550
  }
551

    
552
  $compiled = TRUE;
553

    
554
  return ( $input_format === FALSE ) ? $_ckeditor_compiled : $_ckeditor_compiled[$input_format];
555
}
556

    
557
/**
558
 * Compile settings of profile
559
 *
560
 * @param object $global_profile
561
 * @param object $profile
562
 * @return array
563
 */
564
function ckeditor_profile_settings_compile($global_profile, $profile) {
565
  global $user, $language, $theme;
566

    
567
  $current_theme = variable_get('theme_default', $theme);
568

    
569
  $settings = array();
570
  $conf = $profile->settings;
571
  $profile_name = $profile->name;
572

    
573
  if (user_access('customize ckeditor')) {
574
    foreach (array('default', 'show_toggle', 'width', 'lang', 'auto_lang') as $setting) {
575
      $conf[$setting] = ckeditor_user_get_setting($user, $profile, $setting);
576
    }
577
  }
578

    
579
  if (!isset($conf['ss'])) {
580
    $conf['ss'] = 2;
581
  }
582

    
583
  $themepath = drupal_get_path('theme', $current_theme) . '/';
584
  $host = base_path();
585

    
586
  // setting some variables
587
  $module_drupal_path = ckeditor_module_path('relative');
588
  $module_drupal_local_path = ckeditor_module_path('local');
589
  $editor_path = ckeditor_path('relative');
590
  $editor_local_path = ckeditor_path('local');
591

    
592
  $toolbar = $conf['toolbar'];
593

    
594
  if (!empty($conf['theme_config_js']) && $conf['theme_config_js'] == 't' && file_exists($themepath . 'ckeditor.config.js')) {
595
    $ckeditor_config_path = $host . $themepath . 'ckeditor.config.js?' . @filemtime($themepath . 'ckeditor.config.js');
596
  }
597
  else {
598
    $ckeditor_config_path = $module_drupal_path . "/ckeditor.config.js?" . @filemtime($module_drupal_path . "/ckeditor.config.js");
599
  }
600

    
601
  $settings['customConfig'] = $ckeditor_config_path;
602
  $settings['defaultLanguage'] = $conf['lang'];
603
  $settings['toolbar'] = $toolbar;
604
  $settings['enterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['enter_mode']));
605
  $settings['shiftEnterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['shift_enter_mode']));
606
  $settings['toolbarStartupExpanded'] = ( $conf['expand'] == 't' );
607
  if ($conf['expand'] == 'f') {
608
    $settings['toolbarCanCollapse'] = true;
609
  }
610
  $settings['width'] = $conf['width'];
611
  //check if skin exists, if not select default one
612
  if (isset($global_profile->settings['skin']) && file_exists($editor_local_path . '/skins/' . $global_profile->settings['skin'])) {
613
    $settings['skin'] = $global_profile->settings['skin'];
614
  }
615
  else {
616
    $settings['skin'] = ckeditor_default_skin();
617
  }
618
  $settings['format_tags'] = $conf['font_format'];
619
  $settings['show_toggle'] = $conf['show_toggle'];
620
  $settings['default'] = $conf['default'];
621
  if (!empty($conf['allowed_content']) && $conf['allowed_content'] === 'f') {
622
    $settings['allowedContent'] = true;
623
  }
624
  elseif (!empty($conf['extraAllowedContent'])) {
625
    $settings['extraAllowedContent'] = $conf['extraAllowedContent'];
626
  }
627
  $settings['ss'] = $conf['ss'];
628

    
629
  if (isset($conf['language_direction'])) {
630
    switch ($conf['language_direction']) {
631
      case 'default':
632
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
633
          $settings['contentsLangDirection'] = 'rtl';
634
        }
635
        break;
636
      case 'ltr':
637
        $settings['contentsLangDirection'] = 'ltr';
638
        break;
639
      case 'rtl':
640
        $settings['contentsLangDirection'] = 'rtl';
641
        break;
642
    }
643
  }
644

    
645
  if (isset($conf['loadPlugins'])) {
646
    $settings['loadPlugins'] = ckeditor_plugins_render($conf['loadPlugins']);
647
  }
648
  else {
649
    $settings['loadPlugins'] = array();
650
  }
651

    
652
  //add support for divarea plugin from CKE4
653
  if (isset($conf['use_divarea']) && $conf['use_divarea'] == 't' && $editor_local_path != '<URL>' && file_exists($editor_local_path . '/plugins/divarea/plugin.js')) {
654
    $settings['loadPlugins']['divarea'] = array('name' => 'divarea', 'path' => $editor_path . '/plugins/divarea/', 'buttons' => FALSE, 'default' => 'f');
655
  }
656

    
657
  if (isset($conf['html_entities']) && $conf['html_entities'] == 'f') {
658
    $settings['entities'] = FALSE;
659
    $settings['entities_greek'] = FALSE;
660
    $settings['entities_latin'] = FALSE;
661
  }
662
  if (isset($conf['scayt_autoStartup']) && $conf['scayt_autoStartup'] == 't') {
663
    $settings['scayt_autoStartup'] = TRUE;
664
  }
665
  else {
666
    $settings['scayt_autoStartup'] = FALSE;
667
  }
668

    
669
  if ($conf['auto_lang'] == "f") {
670
    $settings['language'] = $conf['lang'];
671
    //[#1473010]
672
    $settings['scayt_sLang'] = ckeditor_scayt_langcode($conf['lang']);
673
  }
674

    
675
  if (isset($conf['forcePasteAsPlainText']) && $conf['forcePasteAsPlainText'] == 't') {
676
    $settings['forcePasteAsPlainText'] = TRUE;
677
  }
678

    
679
  if (isset($conf['custom_formatting']) && $conf['custom_formatting'] == 't') {
680
    foreach ($conf['formatting']['custom_formatting_options'] as $k => $v) {
681
      if ($v === 0) {
682
        $conf['formatting']['custom_formatting_options'][$k] = FALSE;
683
      }
684
      else {
685
        $conf['formatting']['custom_formatting_options'][$k] = TRUE;
686
      }
687
    }
688
    $settings['output_pre_indent'] = $conf['formatting']['custom_formatting_options']['pre_indent'];
689
    unset($conf['formatting']['custom_formatting_options']['pre_indent']);
690
    $settings['custom_formatting'] = $conf['formatting']['custom_formatting_options'];
691
  }
692

    
693
  // add code for filebrowser for users that have access
694
  $filebrowser = !empty($conf['filebrowser']) ? $conf['filebrowser'] : 'none';
695
  $filebrowser_image = !empty($conf['filebrowser_image']) ? $conf['filebrowser_image'] : $filebrowser;
696
  $filebrowser_flash = !empty($conf['filebrowser_flash']) ? $conf['filebrowser_flash'] : $filebrowser;
697

    
698
  if ($filebrowser == 'imce' && !module_exists('imce')) {
699
    $filebrowser = 'none';
700
  }
701

    
702
  if ($filebrowser == 'elfinder' && !module_exists('elfinder')) {
703
    $filebrowser = 'none';
704
  }
705

    
706
  /* MODULES NOT PORTED TO D7
707
    if ($filebrowser == 'tinybrowser' && !module_exists('tinybrowser')) {
708
    $filebrowser = 'none';
709
    }
710

    
711
    if ($filebrowser == 'ib' && !module_exists('imagebrowser')) {
712
    $filebrowser = 'none';
713
    }
714
    if ($filebrowser == 'webfm' && !module_exists('webfm_popup')) {
715
    $filebrowser = 'none';
716
    }
717
   */
718
  if ($filebrowser_image != $filebrowser) {
719
    if ($filebrowser_image == 'imce' && !module_exists('imce')) {
720
      $filebrowser_image = $filebrowser;
721
    }
722

    
723
    if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
724
      $filebrowser_image = $filebrowser;
725
    }
726
    /* MODULES NOT PORTED TO D7
727
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
728
      $filebrowser_image = $filebrowser;
729
      }
730
      if ($filebrowser_image == 'ib' && !module_exists('imagebrowser')) {
731
      $filebrowser_image = $filebrowser;
732
      }
733
      if ($filebrowser_image == 'webfm' && !module_exists('webfm_popup')) {
734
      $filebrowser_image = $filebrowser;
735
      }
736
     */
737
  }
738

    
739
  if ($filebrowser_flash != $filebrowser) {
740
    if ($filebrowser_flash == 'imce' && !module_exists('imce')) {
741
      $filebrowser_flash = $filebrowser;
742
    }
743

    
744
    if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
745
      $filebrowser_flash = $filebrowser;
746
    }
747
    /* MODULES NOT PORTED TO D7
748
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
749
      $filebrowser_flash = $filebrowser;
750
      }
751
      if ($filebrowser_flash == 'ib' && !module_exists('imagebrowser')) {
752
      $filebrowser_flash = $filebrowser;
753
      }
754
      if ($filebrowser_flash == 'webfm' && !module_exists('webfm_popup')) {
755
      $filebrowser_flash = $filebrowser;
756
      }
757
     */
758
  }
759

    
760
  if ($filebrowser == 'ckfinder' || $filebrowser_image == 'ckfinder' || $filebrowser_flash == 'ckfinder') {
761
    if (user_access('allow CKFinder file uploads')) {
762
      if (!empty($profile->settings['UserFilesPath'])) {
763
        $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = strtr($profile->settings['UserFilesPath'], array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => $host, "%n" => $user->name));
764
      }
765
      else {
766
        $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = strtr('%b%f/', array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => $host, "%n" => $user->name));
767
      }
768
      if (!empty($profile->settings['UserFilesAbsolutePath'])) {
769
        $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = strtr($profile->settings['UserFilesAbsolutePath'], array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => base_path(), "%d" => ckeditor_get_document_root_full_path(), "%n" => $user->name));
770
      }
771
      else {
772
        $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = strtr('%d%b%f/', array("%f" => variable_get('file_public_path', conf_path() . '/files'), "%u" => $user->uid, "%b" => base_path(), "%d" => ckeditor_get_document_root_full_path(), "%n" => $user->name));
773
      }
774
      if (variable_get('file_default_scheme', '') == 'private') {
775
        $private_dir = isset($global_profile->settings['private_dir']) ? trim($global_profile->settings['private_dir'], '/') : '';
776
        if (!empty($private_dir)) {
777
          $private_dir = strtr($private_dir, array('%u' => $user->uid, '%n' => $user->name));
778
          $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = url('system/files') . '/' . $private_dir . '/';
779
          $private_upload_path = file_uri_target('private://' . variable_get('file_private_path', '')) . '/' . $private_dir;
780
        }
781
        else {
782
          $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = url('system/files') . '/';
783
          $private_upload_path =  file_uri_target('private://' . variable_get('file_private_path', ''));
784
        }
785
        //add '/' to beginning of path if necessary
786
        if (strpos(variable_get('file_private_path', ''), '/') === 0 && $private_upload_path[0] != '/') {
787
          $private_upload_path = '/' . $private_upload_path;
788
        }
789
        //check if CKEditor private dir exists and create it if not
790
        if ($private_dir && !is_dir($private_upload_path)) {
791
          mkdir($private_upload_path, 0755, TRUE);
792
        }
793
        $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = drupal_realpath($private_upload_path) . '/';
794
      }
795
    }
796
  }
797
  /* MODULES NOT PORTED TO D7
798
    if (in_array('tinybrowser', array($filebrowser, $filebrowser_image, $filebrowser_flash))) {
799
    $popup_win_size = variable_get('tinybrowser_popup_window_size', '770x480');
800
    if (!preg_match('#\d+x\d+#is', $popup_win_size)) {
801
    $popup_win_size = '770x480';
802
    }
803
    $popup_win_size = trim($popup_win_size);
804
    $popup_win_size = strtolower($popup_win_size);
805
    $win_size = split('x', $popup_win_size);
806
    }
807
   */
808
  switch ($filebrowser) {
809
    case 'ckfinder':
810
      if (user_access('allow CKFinder file uploads')) {
811
        $ckfinder_full_path = ckfinder_path('relative');
812
        $settings['filebrowserBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?id=' . $profile_name;
813
        $settings['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
814
        $settings['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Flash&id=' . $profile_name;
815
        $settings['filebrowserUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Files&id=' . $profile_name;
816
        $settings['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
817
        $settings['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Flash&id=' . $profile_name;
818
      }
819
      break;
820
    case 'imce':
821
      $settings['filebrowserBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
822
      break;
823
    case 'elfinder':
824
      $settings['filebrowserBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
825
      break;
826
    /* MODULES NOT PORTED TO D7
827
      case 'webfm':
828
      if (user_access('access webfm')) {
829
      $settings['filebrowserBrowseUrl'] = $host . "index.php?q=webfm_popup";
830
      }
831
      break;
832
      case 'ib':
833
      if (user_access('browse own images')) {
834
      $settings['filebrowserBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
835
      $settings['filebrowserWindowWidth'] = 700;
836
      $settings['filebrowserWindowHeight'] = 520;
837
      }
838
      break;
839
      case 'tinybrowser':
840
      $settings['filebrowserBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=file";
841
      $settings['filebrowserWindowWidth'] = (int) $win_size[0] + 15;
842
      $settings['filebrowserWindowHeight'] = (int) $win_size[1] + 15;
843
      break;
844
     */
845
  }
846

    
847
  if ($filebrowser_image != $filebrowser) {
848
    switch ($filebrowser_image) {
849
      case 'ckfinder':
850
        if (user_access('allow CKFinder file uploads')) {
851
          $ckfinder_full_path = ckfinder_path('relative');
852
          $settings['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
853
          $settings['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
854
        }
855
        break;
856
      case 'imce':
857
        $settings['filebrowserImageBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
858
        break;
859
      case 'elfinder':
860
        $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
861
        break;
862
      /* MODULES NOT PORTED TO D7
863
        case 'webfm':
864
        if (user_access('access webfm')) {
865
        $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=webfm_popup";
866
        }
867
        break;
868
        case 'ib':
869
        if (user_access('browse own images')) {
870
        $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
871
        $settings['filebrowserImageWindowWidth'] = 680;
872
        $settings['filebrowserImageWindowHeight'] = 439;
873
        }
874
        break;
875
        case 'tinybrowser':
876
        $settings['filebrowserImageBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=image";
877
        $settings['filebrowserImageWindowWidth'] = (int) $win_size[0] + 15;
878
        $settings['filebrowserImageWindowHeight'] = (int) $win_size[1] + 15;
879
        break;
880
       */
881
    }
882
  }
883

    
884
  if ($filebrowser_flash != $filebrowser) {
885
    switch ($filebrowser_flash) {
886
      case 'ckfinder':
887
        if (user_access('allow CKFinder file uploads')) {
888
          $ckfinder_full_path = ckfinder_path('relative');
889
          $settings['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
890
          $settings['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
891
        }
892
        break;
893
      case 'imce':
894
        $settings['filebrowserFlashBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
895
        break;
896
      case 'elfinder':
897
        $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
898
        break;
899
      /* MODULES NOT PORTED TO D7
900
        case 'webfm':
901
        if (user_access('access webfm')) {
902
        $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=webfm_popup";
903
        }
904
        break;
905
        case 'ib':
906
        if (user_access('browse own images')) {
907
        $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
908
        $settings['filebrowserFlashWindowWidth'] = 680;
909
        $settings['filebrowserFlashWindowHeight'] = 439;
910
        }
911
        break;
912
        case 'tinybrowser':
913
        $settings['filebrowserFlashBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=media";
914
        $settings['filebrowserFlashWindowWidth'] = (int) $win_size[0] + 15;
915
        $settings['filebrowserFlashWindowHeight'] = (int) $win_size[1] + 15;
916
        break;
917
       */
918
    }
919
  }
920

    
921
  if (!empty($conf['js_conf'])) {
922
    preg_match_all('#config\.(\w+)[\s]*=[\s]*(.+?);[\s]*(?=config\.|$)#is', preg_replace("/[\n\r]+/", "", $conf['js_conf']), $matches);
923
    foreach ($matches[2] as $i => $match) {
924
      if (!empty($match)) {
925
        $value = trim($match, " ;\n\r\t\0\x0B");
926
        if (strcasecmp($value, 'true') == 0) {
927
          $value = TRUE;
928
        }
929
        if (strcasecmp($value, 'false') == 0) {
930
          $value = FALSE;
931
        }
932
        $settings["js_conf"][$matches[1][$i]] = $value;
933
      }
934
    }
935
  }
936

    
937
  $settings['stylesCombo_stylesSet'] = "drupal:" . $module_drupal_path . '/ckeditor.styles.js';
938
  if (!empty($conf['css_style'])) {
939
    if ($conf['css_style'] == 'theme' && file_exists($themepath . 'ckeditor.styles.js')) {
940
      $settings['stylesCombo_stylesSet'] = "drupal:" . $host . $themepath . 'ckeditor.styles.js';
941
    }
942
    elseif (!empty($conf['css_style']) && $conf['css_style'] == 'self') {
943
      $conf['styles_path'] = str_replace("%h%t", "%t", $conf['styles_path']);
944
      $settings['stylesCombo_stylesSet'] = "drupal:" . str_replace(array('%h', '%t', '%m'), array($host, $host . $themepath, $module_drupal_path), $conf['styles_path']);
945
    }
946
  }
947

    
948
  // add custom stylesheet if configured
949
  // lets hope it exists but we'll leave that to the site admin
950
  $query_string = '?' . variable_get('css_js_query_string', '0');
951
  $css_files = array();
952
  switch ($conf['css_mode']) {
953
    case 'theme':
954
      _ckeditor_add_css_from_theme($current_theme, $css_files);
955
      break;
956

    
957
    case 'self':
958
      if (file_exists($module_drupal_local_path . '/css/ckeditor.css')) {
959
        $css_files[] = $module_drupal_path . '/css/ckeditor.css' . $query_string;
960
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
961
          if (file_exists($module_drupal_local_path . '/css/ckeditor-rtl.css')) {
962
            $css_files[] = $module_drupal_path . '/css/ckeditor-rtl.css' . $query_string;
963
          }
964
        }
965
      }
966
      foreach (explode(',', $conf['css_path']) as $css_path) {
967
        $css_path = trim(str_replace("%h%t", "%t", $css_path));
968
        $css_files[] = str_replace(array('%h', '%t'), array($host, $host . $themepath), $css_path) . $query_string;
969
      }
970
      break;
971

    
972
    case 'none':
973
      if (file_exists($module_drupal_local_path . '/css/ckeditor.css')) {
974
        $css_files[] = $module_drupal_path . '/css/ckeditor.css' . $query_string;
975
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
976
          if (file_exists($module_drupal_local_path . '/css/ckeditor-rtl.css')) {
977
            $css_files[] = $module_drupal_path . '/css/ckeditor-rtl.css' . $query_string;
978
          }
979
        }
980
      }
981
      if ($editor_local_path != '<URL>') {
982
        if (file_exists($editor_local_path . '/contents.css')) {
983
          $css_files[] = $editor_path . '/contents.css' . $query_string;
984
        }
985
      }
986
      else {
987
        $editor_url_path = ckeditor_path('url');
988
        $css_files[] = $editor_url_path . '/contents.css' . $query_string;
989
      }
990
      break;
991
  }
992

    
993
  if (isset($conf['ckeditor_load_method']) && $conf['ckeditor_load_method'] == 'ckeditor_source.js') {
994
    foreach ($css_files as $k => $v) {
995
      $css_files[$k] = $v . '&t=' . time();
996
    }
997
  }
998

    
999
  $settings['contentsCss'] = array_values($css_files);
1000

    
1001
  if (!empty($conf['uicolor']) && $conf['uicolor'] == "custom" && !empty($conf['uicolor_user'])) {
1002
    $settings['uiColor'] = $conf['uicolor_user'];
1003
  }
1004

    
1005
  if (!empty($conf['uicolor']) && strpos($conf['uicolor'], "color_") === 0) {
1006
    if (function_exists('color_get_palette')) {
1007
      $palette = @color_get_palette($current_theme, FALSE); //[#652274]
1008
      $color = str_replace("color_", "", $conf['uicolor']);
1009
      if (!empty($palette[$color])) {
1010
        $settings['uiColor'] = $palette[$color];
1011
      }
1012
    }
1013
  }
1014

    
1015
  if (!empty($settings['loadPlugins'])) {
1016
    // Remove orphaned plugins (coming from disabled modules).
1017
    $available_plugins = array_keys(ckeditor_load_plugins());
1018
    $settings['loadPlugins'] = array_intersect_key($settings['loadPlugins'], array_flip($available_plugins));
1019
  }
1020

    
1021
  // Allow modules to modify the settings.
1022
  drupal_alter('ckeditor_settings', $settings, $conf);
1023

    
1024
  return $settings;
1025
}
1026

    
1027
function _ckeditor_add_css_from_theme($current_theme, &$css_files) {
1028
  global $language, $base_theme_info;
1029

    
1030
  $themes = list_themes();
1031
  $theme_info = $themes[$current_theme];
1032
  if (!empty($theme_info->base_theme)) {
1033
    _ckeditor_add_css_from_theme($theme_info->base_theme, $css_files);
1034
  }
1035

    
1036
  $query_string = '?' . variable_get('css_js_query_string', '0');
1037
  $host = base_path();
1038
  $themepath = drupal_get_path('theme', $current_theme) . '/';
1039
  $module_drupal_local_path = ckeditor_module_path('local');
1040
  $module_drupal_path = drupal_get_path('module', 'ckeditor');
1041
  if (!empty($theme_info->stylesheets)) {
1042
    $editorcss = "\"";
1043
    foreach ($base_theme_info as $base) { // Grab stylesheets from base theme
1044
      if (!empty($base->stylesheets)) { // may be empty when the base theme reference in the info file is invalid
1045
        foreach ($base->stylesheets as $type => $stylesheets) {
1046
          if ($type != "print") {
1047
            foreach ($stylesheets as $name => $path) {
1048
              if (file_exists($path)) {
1049
                $css_files[$name] = $host . $path . $query_string;
1050
                // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
1051
                if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
1052
                  $rtl_path = substr($path, 0, -4) . "-rtl.css";
1053
                  if (file_exists($rtl_path)) {
1054
                    $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
1055
                  }
1056
                }
1057
              }
1058
            }
1059
          }
1060
        }
1061
      }
1062
    }
1063
    if (!empty($theme_info->stylesheets)) { // Grab stylesheets from current theme
1064
      foreach ($theme_info->stylesheets as $type => $stylesheets) {
1065
        if ($type != "print") {
1066
          foreach ($stylesheets as $name => $path) {
1067
            // Checks if less module exists...
1068
            if (strstr($path, '.less') && module_exists('less')) {
1069
              $path = 'sites/default/files/less/' . $path; // append the less file path
1070
              $path = str_replace('.less', '', $path); // remove the .less
1071
            }
1072
            if (file_exists($path)) {
1073
              $css_files[$name] = $host . $path . $query_string;
1074
              // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
1075
              if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
1076
                $rtl_path = substr($path, 0, -4) . "-rtl.css";
1077
                if (file_exists($rtl_path)) {
1078
                  $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
1079
                }
1080
              }
1081
            }
1082
            elseif (!empty($css_files[$name])) {
1083
              unset($css_files[$name]);
1084
            }
1085
          }
1086
        }
1087
      }
1088
    }
1089
    // Grab stylesheets local.css and local-rtl.css if they exist (fusion based themes)
1090
    if (file_exists($themepath . 'css/local.css')) {
1091
      $css_files[] = $host . $themepath . 'css/local.css' . $query_string;
1092
    }
1093
    if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && file_exists($themepath . 'css/local-rtl.css')) {
1094
      $css_files[] = $host . $themepath . 'css/local-rtl.css' . $query_string;
1095
    }
1096

    
1097
    // Grab stylesheets from color module
1098
    $color_paths = variable_get('color_' . $current_theme . '_stylesheets', array());
1099
    if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
1100
      if (!empty($color_paths[1])) {
1101
        if (substr($color_paths[0], 0, 9) == 'public://') {
1102
          $css_files[] = file_create_url($color_paths[1]) . $query_string;
1103
        }
1104
        else {
1105
          $css_files[] = $host . $color_paths[1] . $query_string;
1106
        }
1107
      }
1108
    }
1109
    elseif (!empty($color_paths[0])) {
1110
      if (substr($color_paths[0], 0, 9) == 'public://') {
1111
        $css_files[] = file_create_url($color_paths[0]) . $query_string;
1112
      }
1113
      else {
1114
        $css_files[] = $host . $color_paths[0] . $query_string;
1115
      }
1116
    }
1117
  }
1118
  else {
1119
    if (!file_exists($themepath . 'ckeditor.css') && file_exists($themepath . 'style.css')) {
1120
      $css_files[] = $host . $themepath . 'style.css' . $query_string;
1121
    }
1122
  }
1123
  if (file_exists($module_drupal_local_path . '/css/ckeditor.css')) {
1124
    $css_files[] = $host . $module_drupal_path . '/css/ckeditor.css' . $query_string;
1125
  }
1126
  if (file_exists($themepath . 'ckeditor.css')) {
1127
    $css_files[] = $host . $themepath . 'ckeditor.css' . $query_string;
1128
  }
1129
}
1130

    
1131
/**
1132
 * Load CKEditor for field ID
1133
 *
1134
 * @param object $field
1135
 * @param string $format
1136
 *
1137
 * @return object
1138
 *
1139
 */
1140
function ckeditor_load_by_field($field, $format, $show_toggle = TRUE, $add_fields_to_toggle = FALSE) {
1141
  global $user, $theme;
1142
  static $processed_ids = array();
1143
  static $is_running = FALSE;
1144
  $use_ckeditor = FALSE;
1145
  $format_arr = FALSE;
1146
  $suffix = '';
1147

    
1148
  if (is_array($format)) {
1149
    $format_arr = $format;
1150
    $format = isset($format_arr['#value']) ? $format_arr['#value'] : $format_arr['#default_value'];
1151
  }
1152

    
1153
  if (!isset($field['#id'])) {
1154
    return $field;
1155
  }
1156

    
1157
  if (isset($processed_ids[$field['#id']])) {
1158
    return $field;
1159
  }
1160

    
1161
  if (key_exists('#wysiwyg', $field) && !$field['#wysiwyg']) {
1162
    return $field;
1163
  }
1164

    
1165
  if (isset($field['#access']) && !$field['#access']) {
1166
    return $field;
1167
  }
1168

    
1169
  if ($field['#id'] == "edit-log") {
1170
    return $field;
1171
  }
1172

    
1173
  if (isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled') {
1174
    return $field;
1175
  }
1176

    
1177
  drupal_add_js(array('ckeditor' => array('textarea_default_format' => array($field['#id'] => $format))), 'setting');
1178

    
1179
  if (!isset($processed_ids[$field['#id']])) {
1180
    $processed_ids[$field['#id']] = array();
1181
  }
1182

    
1183
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
1184
  $profile = ckeditor_get_profile($format);
1185
  $host = base_path();
1186

    
1187
  if ($profile === FALSE) {
1188
    $ckeditor_in_default_format = FALSE;
1189
    foreach ((array) $format_arr['#options'] as $key => $val) {
1190
      if ($key == $format)
1191
        continue;
1192
      if ($profile = ckeditor_get_profile($key)) {
1193
        $use_ckeditor = $key;
1194
        break;
1195
      }
1196
    }
1197
    if ($use_ckeditor === FALSE) {
1198
      return $field;
1199
    }
1200
  }
1201
  else {
1202
    $ckeditor_in_default_format = TRUE;
1203
  }
1204

    
1205
  if ($settings = ckeditor_profiles_compile($format)) {
1206
    $ckeditor_on = ($settings['default'] == 't' && $profile->settings['default'] == 't') ? TRUE : FALSE;
1207
  }
1208
  elseif ($settings = ckeditor_profiles_compile($use_ckeditor)) {
1209
    $ckeditor_on = FALSE;
1210
  }
1211
  else {
1212
    return $field;
1213
  }
1214

    
1215
  // Attach the editor css.
1216
  $field['#attached']['css'][] = drupal_get_path('module', 'ckeditor') . '/css/ckeditor.editor.css';
1217

    
1218
  if ($settings) {
1219
    $textarea_id = $field['#id'];
1220
    $class[] = 'ckeditor-mod';
1221
    $_ckeditor_ids[] = $textarea_id;
1222

    
1223
    //settings are saved as strings, not booleans
1224
    if ($settings['show_toggle'] == 't' && $show_toggle) {
1225

    
1226
      if ($add_fields_to_toggle !== FALSE) {
1227
        if (is_array($add_fields_to_toggle)) {
1228
          $toggle_fields = "['" . $textarea_id . "','" . implode("','", $add_fields_to_toggle) . "']";
1229
        }
1230
        else {
1231
          $toggle_fields = "['" . $textarea_id . "','" . $add_fields_to_toggle . "']";
1232
        }
1233
      }
1234
      else {
1235
        $toggle_fields = "['{$textarea_id}']";
1236
      }
1237

    
1238
      $wysiwyg_link = '';
1239
      $wysiwyg_link .= "<a class=\"ckeditor_links\" style=\"display:none\" href=\"javascript:void(0);\" onclick=\"javascript:Drupal.ckeditorToggle({$toggle_fields},'" . str_replace("'", "\\'", t('Switch to plain text editor')) . "','" . str_replace("'", "\\'", t('Switch to rich text editor')) . "');\" id=\"switch_{$textarea_id}\">";
1240
      $wysiwyg_link .= $ckeditor_on ? t('Switch to plain text editor') : t('Switch to rich text editor');
1241
      $wysiwyg_link .= '</a>';
1242

    
1243
      // Make sure to append to #suffix so it isn't completely overwritten
1244
      $suffix .= $wysiwyg_link;
1245
    }
1246

    
1247
    $editor_local_path = ckeditor_path('local');
1248
    $editor_url_path = ckeditor_path('url');
1249

    
1250
    if (!$is_running) {
1251
      // By default sessions are not started automatically for anonymous users.
1252
      // Start one for editing content so that we had a consistent token that is used in XSS filter.
1253
      if (isset($field['#entity']) && !empty($field['#entity']->created) && empty($user->uid)) {
1254
        drupal_session_start();
1255
        $_SESSION['ckeditor_anonymous_user'] = true;
1256
        drupal_page_is_cacheable(FALSE);
1257
      }
1258

    
1259
      if (!$ckeditor_in_default_format) {
1260
        $load_method = 'ckeditor_basic.js';
1261
        $load_time_out = 0;
1262
      }
1263
      elseif (isset($profile->settings['ckeditor_load_method'])) {
1264
        $load_method = $profile->settings['ckeditor_load_method'];
1265
        $load_time_out = $profile->settings['ckeditor_load_time_out'];
1266
      }
1267
      if ($editor_local_path != '<URL>') {
1268
        drupal_add_js('window.CKEDITOR_BASEPATH = "' . ckeditor_path('relative') . '/"', array('type' => 'inline', 'weight' => -100));
1269
      }
1270
      drupal_add_js(ckeditor_module_path('url') . '/includes/ckeditor.utils.js', array('type' => 'file', 'scope' => 'footer'));
1271

    
1272
      $preprocess = FALSE;
1273
      if (isset($global_profile->settings['ckeditor_aggregate']) && $global_profile->settings['ckeditor_aggregate'] == 't') {
1274
        $preprocess = TRUE;
1275
      }
1276

    
1277
      if ($editor_local_path == '<URL>') {
1278
        drupal_add_js($editor_url_path . '/ckeditor.js', array('type' => 'external', 'scope' => 'footer'));
1279
      }
1280
      else if (isset($load_method) && file_exists($editor_local_path . '/' . $load_method)) {
1281
        drupal_add_js($editor_url_path . '/' . $load_method, array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
1282
        if ($load_method == 'ckeditor_basic.js') {
1283
          drupal_add_js('CKEDITOR.loadFullCoreTimeout = ' . $load_time_out . ';', array('type' => 'inline', 'scope' => 'footer'));
1284
          drupal_add_js(array('ckeditor' => array('load_timeout' => TRUE)), 'setting');
1285
        }
1286
      }
1287
      else {
1288
        drupal_add_js($editor_url_path . '/ckeditor.js', array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
1289
      }
1290
      $ckeditor_url = ckeditor_path('relative');
1291
      if ($ckeditor_url == '<URL>') {
1292
        $ckeditor_url = ckeditor_path('url');
1293
      }
1294
      $ckeditor_url .= '/';
1295
      drupal_add_js(array('ckeditor' => array('module_path' => ckeditor_module_path('relative'), 'editor_path' => $ckeditor_url)), 'setting');
1296
      if (module_exists('paging')) {
1297
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1298
      }
1299
      if (module_exists('linktocontent_node')) {
1300
        drupal_add_js(array('ckeditor' => array('linktocontent_node' => TRUE)), 'setting');
1301
      }
1302
      if (module_exists('linktocontent_menu')) {
1303
        drupal_add_js(array('ckeditor' => array('linktocontent_menu' => TRUE)), 'setting');
1304
      }
1305
      if (module_exists('pagebreak')) {
1306
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1307
      }
1308
      if (module_exists('smart_paging')) {
1309
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1310
      }
1311
      drupal_add_js(array('ckeditor' => array('ajaxToken' => drupal_get_token('ckeditorAjaxCall'), 'xss_url' => url('ckeditor/xss'))), 'setting');
1312
      $is_running = TRUE;
1313
    }
1314

    
1315
    drupal_add_js(array('ckeditor' => array('theme' => $theme)), 'setting');
1316
    if (!empty($settings)) {
1317
      drupal_add_js(array('ckeditor' => array('elements' => array($textarea_id => $format))), 'setting');
1318
    }
1319
    if (!empty($ckeditor_on)) {
1320
      drupal_add_js(array('ckeditor' => array('autostart' => array($textarea_id => $ckeditor_on))), 'setting');
1321
    }
1322
    //[#1473010]
1323
    if (isset($settings['scayt_sLang'])) {
1324
      drupal_add_js(array('ckeditor' => array('scayt_language' => $settings['scayt_sLang'])), 'setting');
1325
    }
1326
    elseif (!empty($field["#language"]) && $field["#language"] != LANGUAGE_NONE) {
1327
      drupal_add_js(array('ckeditor' => array('scayt_language' => ckeditor_scayt_langcode($field["#language"]))), 'setting');
1328
    }
1329

    
1330
    // Pass Drupal's JS cache-busting string via settings along to CKEditor.
1331
    drupal_add_js(array('ckeditor' => array('timestamp' => variable_get('css_js_query_string', '0'))), 'setting');
1332

    
1333
    // Remember extra information and reuse it during "Preview"
1334
    $processed_ids[$field['#id']]['suffix'] = $suffix;
1335
    $processed_ids[$field['#id']]['class'] = $class;
1336

    
1337
    if (empty($field['#suffix'])) {
1338
      $field['#suffix'] = $suffix;
1339
    }
1340
    else {
1341
      $field['#suffix'] .= $suffix;
1342
    }
1343

    
1344
    if (empty($field['#attributes']['class'])) {
1345
      $field['#attributes']['class'] = $class;
1346
    }
1347
    else {
1348
      $field['#attributes']['class'] = array_merge($field['#attributes']['class'], $class);
1349
    }
1350
  }
1351

    
1352
  return $field;
1353
}
1354

    
1355
/**
1356
 * Return all modules that provide security filters.
1357
 */
1358
function ckeditor_security_filters() {
1359
  $security_filters = array();
1360

    
1361
  $security_filters['modules'] = array(
1362
    'htmLawed' => array(
1363
      'title' => 'htmLawed',
1364
      'project_page' => 'http://drupal.org/project/htmLawed',
1365
      'weight' => 0,
1366
      'installed' => FALSE,
1367
      'filters' => array()
1368
    ),
1369
    'htmltidy' => array(
1370
      'title' => 'Htmltidy',
1371
      'project_page' => 'http://drupal.org/project/htmltidy',
1372
      'weight' => 0,
1373
      'installed' => FALSE,
1374
      'filters' => array()
1375
    ),
1376
    'htmlpurifier' => array(
1377
      'title' => 'HTML Purifier',
1378
      'project_page' => 'http://drupal.org/project/htmlpurifier',
1379
      'weight' => 0,
1380
      'installed' => FALSE,
1381
      'filters' => array()
1382
    ),
1383
    'wysiwyg_filter' => array(
1384
      'title' => 'WYSIWYG Filter',
1385
      'project_page' => 'http://drupal.org/project/wysiwyg_filter',
1386
      'weight' => 0,
1387
      'installed' => FALSE,
1388
      'filters' => array()
1389
    )
1390
  );
1391

    
1392
  $security_filters['filters'] = array();
1393

    
1394
  foreach ($security_filters['modules'] as $module_name => $module_conf) {
1395
    if (module_exists($module_name)) {
1396
      $security_filters['modules'][$module_name]['installed'] = TRUE;
1397
      $module_filters = module_invoke($module_name, 'filter_info');
1398
      foreach ($module_filters as $module_filter_name => $module_filter_conf) {
1399
        $security_filters['modules'][$module_name]['filters'][$module_filter_name] = $module_filter_conf;
1400
        $security_filters['filters'][$module_filter_name] = TRUE;
1401
      }
1402
    }
1403
  }
1404

    
1405
  //add filters from Drupal core
1406
  $security_filters['modules']['__drupal'] = array(
1407
    'title' => 'Drupal core',
1408
    'project_page' => FALSE,
1409
    'weight' => -1,
1410
    'installed' => TRUE,
1411
    'filters' => array(
1412
      'filter_html' => array(
1413
        'title' => 'Limit allowed HTML tags',
1414
        'description' => 'Removes the attributes that the built-in "Limit allowed HTML tags"-filter does not allow inside HTML elements/tags'
1415
      )
1416
    )
1417
  );
1418
  $security_filters['filters']['filter_html'] = TRUE;
1419

    
1420
  //load security filters added by API
1421
  $external_module_filters = module_invoke_all('ckeditor_security_filter');
1422
  if (count($external_module_filters) > 0) {
1423
    $security_filters['modules']['__external'] = array(
1424
      'title' => 'External filters',
1425
      'project_page' => FALSE,
1426
      'weight' => 1,
1427
      'installed' => TRUE,
1428
      'filters' => array()
1429
    );
1430
    foreach ($external_module_filters as $module_filter_name => $module_filter_conf) {
1431
      $security_filters['modules']['__external']['filters'][$module_filter_name] = $module_filter_conf;
1432
      $security_filters['filters'][$module_filter_name] = TRUE;
1433
    }
1434
  }
1435
  drupal_alter('ckeditor_security_filter', $security_filters);
1436

    
1437
  return $security_filters;
1438
}