Projet

Général

Profil

Paste
Télécharger (49 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / includes / ckeditor.lib.inc @ 0ccfec7f

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
  //oops, we have no information about skins, let's use only default
181
  if (empty($arr)) {
182
    $arr = array(
183
      'moono' => 'Moono',
184
      'kama' => 'Kama'
185
    );
186
  }
187
  asort($arr);
188

    
189
  return $arr;
190
}
191

    
192
/**
193
 * Return default skin for CKEditor
194
 *
195
 * @return string
196
 */
197
function ckeditor_default_skin() {
198
  $skin_options = ckeditor_load_skin_options();
199
  if (array_key_exists('moono', $skin_options)) {
200
    return 'moono';
201
  }
202
  if (array_key_exists('kama', $skin_options)) {
203
    return 'kama';
204
  }
205
  reset($skin_options);
206
  //if any default theme not exists select first from the list
207
  return key($skin_options);
208
}
209

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

    
296
/**
297
 * Get the language locale code supported by Scayt for the specified language
298
 */
299
function ckeditor_scayt_langcode($lang) {
300
  $scayt_langs = array(
301
    'en' => 'en_US',
302
    'es' => 'es_ES',
303
    'fr' => 'fr_FR',
304
    'de' => 'de_DE',
305
    'it' => 'it_IT',
306
    'el' => 'el_EL',
307
    'pt' => 'pt_PT',
308
    'da' => 'da_DA',
309
    'sv' => 'sv_SE',
310
    'nl' => 'nl_NL',
311
    'nb' => 'no_NO',
312
    'fi' => 'fi_FI',
313
  );
314
  if (array_key_exists($lang, $scayt_langs)) {
315
    return $scayt_langs[$lang];
316
  }
317

    
318
  $default = language_default();
319
  $default = $default->language;
320
  if (array_key_exists($default, $scayt_langs)) {
321
    return $scayt_langs[$default];
322
  }
323

    
324
  return 'en_US';
325
}
326

    
327
/**
328
 * List of CKEditor plugins.
329
 *
330
 * @param boolean $render
331
 *   Whether to generate plugin paths. Default to FALSE.
332
 * @return array
333
 */
334
function ckeditor_load_plugins($render = FALSE) {
335
  $plugins = module_invoke_all('ckeditor_plugin');
336

    
337
  // [#2159403] introduced a slight change in CKEditor API, expecting the path
338
  // to a plugin returned by hook_ckeditor_plugin() to be a full URL. This
339
  // breaks backward compatibility for external plugins, so we're fixing it
340
  // below.
341
  foreach ($plugins as $i => $plugin) {
342
    if (strpos($plugin['path'], '%') !== 0 && !preg_match('|^(http(s)?:)?\/?\/|i', $plugin['path'])) {
343
      $plugins[$i]['path'] = '%base_path%' . $plugin['path'];
344
    }
345
  }
346

    
347
  drupal_alter('ckeditor_plugin', $plugins);
348

    
349
  ksort($plugins);
350

    
351
  if ($render) {
352
    $plugins = ckeditor_plugins_render($plugins);
353
  }
354

    
355
  return $plugins;
356
}
357

    
358
/**
359
 * Render CKEditor plugins path
360
 */
361
function ckeditor_plugins_render($plugins) {
362
  $render = array();
363
  $render["%base_path%"] = ckeditor_base_path('relative') . '/';
364
  $render["%editor_path%"] = ckeditor_path('relative') . '/';
365
  $render["%module_path%"] = ckeditor_module_path('relative') . '/';
366
  $render["%plugin_dir%"] = $render["%module_path%"] . 'plugins/';
367
  $render["%plugin_dir_extra%"] = ckeditor_plugins_path('relative') . '/';
368

    
369
  foreach ((array) $plugins as $i => $plugin) {
370
    $plugins[$i]['path'] = str_replace(array_keys($render), array_values($render), $plugin['path']);
371
  }
372

    
373
  return $plugins;
374
}
375

    
376
/**
377
 * Get default ckeditor settings
378
 *
379
 * @return array
380
 */
381
function ckeditor_user_get_setting_default() {
382
  $default = array(
383
    'default' => 't',
384
    'show_toggle' => 't',
385
    'width' => '100%',
386
    'lang' => 'en',
387
    'auto_lang' => 't',
388
  );
389

    
390
  // Allow other modules to alter the default settings.
391
  drupal_alter('ckeditor_default_settings', $default);
392

    
393
  return $default;
394
}
395

    
396
/**
397
 * Return CKEditor settings
398
 *
399
 * @param object $user
400
 * @param object $profile
401
 * @param string $setting
402
 * @return array
403
 */
404
function ckeditor_user_get_setting($user, $profile, $setting) {
405
  $default = ckeditor_user_get_setting_default();
406

    
407
  if (user_access('customize ckeditor')) {
408
    $status = isset($user->data['ckeditor_' . $setting]) ? $user->data['ckeditor_' . $setting] : (isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting]);
409
  }
410
  else {
411
    $status = isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting];
412
  }
413

    
414
  return $status;
415
}
416

    
417
/**
418
 * Return CKEditor profile by input format
419
 *
420
 * @param string $input_format
421
 * @return object|boolean
422
 */
423
function ckeditor_get_profile($input_format) {
424
  $select = db_select('ckeditor_settings', 's');
425
  $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
426
  $result = $select->fields('s', array("name"))->condition('f.format', $input_format)->condition('f.name', 'CKEditor Global Profile', '<>')->range(0, 1)->execute()->fetchAssoc();
427

    
428
  if ($result && $profile = ckeditor_profile_load($result['name'])) {
429
    return $profile;
430
  }
431

    
432
  return FALSE;
433
}
434

    
435
/**
436
 * Return CKEditor profile list
437
 */
438
function ckeditor_profile_input_formats() {
439
  $select = db_select('ckeditor_settings', 's');
440
  $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
441
  $result = $select->fields('s', array("name"))->fields('f', array("format"))->execute();
442

    
443
  $list = array();
444
  while ($row = $result->fetchAssoc()) {
445
    if (!isset($row['name'])) {
446
      $list[$row['name']] = array();
447
    }
448
    $list[$row['name']][] = $row['format'];
449
  }
450

    
451
  return $list;
452
}
453

    
454
/**
455
 * Search and return CKEditor plugin path
456
 *
457
 * @return string
458
 */
459
function _ckeditor_script_path() {
460
  $jspath = '';
461
  $module_path = drupal_get_path('module', 'ckeditor');
462

    
463
  if (file_exists($module_path . '/ckeditor/ckeditor.js')) {
464
    $jspath = '%m/ckeditor';
465
  }
466
  elseif (file_exists($module_path . '/ckeditor/ckeditor/ckeditor.js')) {
467
    $jspath = '%m/ckeditor/ckeditor';
468
  }
469
  elseif (file_exists(ckeditor_library_path('url') . '/ckeditor/ckeditor.js')) {
470
    $jspath = '%l/ckeditor';
471
  }
472
  return $jspath;
473
}
474

    
475
/**
476
 * Determines whether the CKEditor sources are present
477
 *
478
 * It checks if ckeditor.js is present.
479
 *
480
 * This function is used by ckeditor_requirements()
481
 *
482
 * @return boolean True if CKEditor is installed
483
 */
484
function _ckeditor_requirements_isinstalled() {
485
  $editor_path = ckeditor_path('local');
486
  if ($editor_path == '<URL>')
487
    return TRUE;
488
  $jspath = $editor_path . '/ckeditor.js';
489

    
490
  $jsp = file_exists($jspath);
491
  if (!$jsp && ($editor_path = _ckeditor_script_path())) {
492
    $result = db_select('ckeditor_settings', 's')->fields('s')->condition('name', 'CKEditor Global Profile')->execute()->fetchAssoc();
493
    if ($result) {
494
      $result['settings'] = unserialize($result['settings']);
495
      $result['settings']['ckeditor_path'] = $editor_path;
496
      $result['settings'] = serialize($result['settings']);
497
      db_update('ckeditor_settings')->fields(array("settings" => $result['settings']))->condition('name', 'CKEditor Global Profile')->execute();
498

    
499
      $jsp = TRUE;
500
      ckeditor_path('local', TRUE);
501
    }
502
  }
503
  return $jsp;
504
}
505

    
506
/**
507
 * Compile settings of all profiles at returns is as array
508
 *
509
 * @param string $input_format
510
 * @param boolean $clear
511
 * @return array
512
 */
513
function ckeditor_profiles_compile($input_format = FALSE, $clear = FALSE) {
514
  static $compiled = FALSE;
515
  static $_ckeditor_compiled = array();
516

    
517
  if ($clear !== FALSE && $compiled !== FALSE) {
518
    $compiled = FALSE;
519
  }
520

    
521
  if ($compiled === TRUE) {
522
    return ( $input_format === FALSE ) ? $_ckeditor_compiled : ( isset($_ckeditor_compiled[$input_format]) ? $_ckeditor_compiled[$input_format] : array() );
523
  }
524

    
525
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
526

    
527
  $profiles_list = ckeditor_profile_input_formats();
528

    
529
  foreach ($profiles_list AS $_profile => $_inputs) {
530
    $profile = ckeditor_profile_load($_profile);
531
    $setting = ckeditor_profile_settings_compile($global_profile, $profile);
532

    
533
    foreach ($_inputs AS $_input) {
534
      $_ckeditor_compiled[$_input] = $setting;
535
    }
536
  }
537

    
538
  $compiled = TRUE;
539

    
540
  return ( $input_format === FALSE ) ? $_ckeditor_compiled : $_ckeditor_compiled[$input_format];
541
}
542

    
543
/**
544
 * Compile settings of profile
545
 *
546
 * @param object $global_profile
547
 * @param object $profile
548
 * @return array
549
 */
550
function ckeditor_profile_settings_compile($global_profile, $profile) {
551
  global $user, $language, $theme;
552

    
553
  $current_theme = variable_get('theme_default', $theme);
554

    
555
  $settings = array();
556
  $conf = $profile->settings;
557
  $profile_name = $profile->name;
558

    
559
  if (user_access('customize ckeditor')) {
560
    foreach (array('default', 'show_toggle', 'width', 'lang', 'auto_lang') as $setting) {
561
      $conf[$setting] = ckeditor_user_get_setting($user, $profile, $setting);
562
    }
563
  }
564

    
565
  if (!isset($conf['ss'])) {
566
    $conf['ss'] = 2;
567
  }
568

    
569
  $themepath = drupal_get_path('theme', $current_theme) . '/';
570
  $host = base_path();
571

    
572
  // setting some variables
573
  $module_drupal_path = ckeditor_module_path('relative');
574
  $module_drupal_local_path = ckeditor_module_path('local');
575
  $editor_path = ckeditor_path('relative');
576
  $editor_local_path = ckeditor_path('local');
577

    
578
  $toolbar = $conf['toolbar'];
579

    
580
  if (!empty($conf['theme_config_js']) && $conf['theme_config_js'] == 't' && file_exists($themepath . 'ckeditor.config.js')) {
581
    $ckeditor_config_path = $host . $themepath . 'ckeditor.config.js?' . @filemtime($themepath . 'ckeditor.config.js');
582
  }
583
  else {
584
    $ckeditor_config_path = $module_drupal_path . "/ckeditor.config.js?" . @filemtime($module_drupal_path . "/ckeditor.config.js");
585
  }
586

    
587
  $settings['customConfig'] = $ckeditor_config_path;
588
  $settings['defaultLanguage'] = $conf['lang'];
589
  $settings['toolbar'] = $toolbar;
590
  $settings['enterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['enter_mode']));
591
  $settings['shiftEnterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['shift_enter_mode']));
592
  $settings['toolbarStartupExpanded'] = ( $conf['expand'] == 't' );
593
  if ($conf['expand'] == 'f') {
594
    $settings['toolbarCanCollapse'] = true;
595
  }
596
  $settings['width'] = $conf['width'];
597
  //check if skin exists, if not select default one
598
  if (isset($global_profile->settings['skin']) && file_exists($editor_local_path . '/skins/' . $global_profile->settings['skin'])) {
599
    $settings['skin'] = $global_profile->settings['skin'];
600
  }
601
  else {
602
    $settings['skin'] = ckeditor_default_skin();
603
  }
604
  $settings['format_tags'] = $conf['font_format'];
605
  $settings['show_toggle'] = $conf['show_toggle'];
606
  $settings['default'] = $conf['default'];
607
  if (!empty($conf['allowed_content']) && $conf['allowed_content'] === 'f') {
608
    $settings['allowedContent'] = true;
609
  }
610
  elseif (!empty($conf['extraAllowedContent'])) {
611
    $settings['extraAllowedContent'] = $conf['extraAllowedContent'];
612
  }
613
  $settings['ss'] = $conf['ss'];
614

    
615
  if (isset($conf['language_direction'])) {
616
    switch ($conf['language_direction']) {
617
      case 'default':
618
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
619
          $settings['contentsLangDirection'] = 'rtl';
620
        }
621
        break;
622
      case 'ltr':
623
        $settings['contentsLangDirection'] = 'ltr';
624
        break;
625
      case 'rtl':
626
        $settings['contentsLangDirection'] = 'rtl';
627
        break;
628
    }
629
  }
630

    
631
  if (isset($conf['loadPlugins'])) {
632
    $settings['loadPlugins'] = ckeditor_plugins_render($conf['loadPlugins']);
633
  }
634
  else {
635
    $settings['loadPlugins'] = array();
636
  }
637

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

    
643
  if (isset($conf['html_entities']) && $conf['html_entities'] == 'f') {
644
    $settings['entities'] = FALSE;
645
    $settings['entities_greek'] = FALSE;
646
    $settings['entities_latin'] = FALSE;
647
  }
648
  if (isset($conf['scayt_autoStartup']) && $conf['scayt_autoStartup'] == 't') {
649
    $settings['scayt_autoStartup'] = TRUE;
650
  }
651
  else {
652
    $settings['scayt_autoStartup'] = FALSE;
653
  }
654

    
655
  if ($conf['auto_lang'] == "f") {
656
    $settings['language'] = $conf['lang'];
657
    //[#1473010]
658
    $settings['scayt_sLang'] = ckeditor_scayt_langcode($conf['lang']);
659
  }
660

    
661
  if (isset($conf['forcePasteAsPlainText']) && $conf['forcePasteAsPlainText'] == 't') {
662
    $settings['forcePasteAsPlainText'] = TRUE;
663
  }
664

    
665
  if (isset($conf['custom_formatting']) && $conf['custom_formatting'] == 't') {
666
    foreach ($conf['formatting']['custom_formatting_options'] as $k => $v) {
667
      if ($v === 0) {
668
        $conf['formatting']['custom_formatting_options'][$k] = FALSE;
669
      }
670
      else {
671
        $conf['formatting']['custom_formatting_options'][$k] = TRUE;
672
      }
673
    }
674
    $settings['output_pre_indent'] = $conf['formatting']['custom_formatting_options']['pre_indent'];
675
    unset($conf['formatting']['custom_formatting_options']['pre_indent']);
676
    $settings['custom_formatting'] = $conf['formatting']['custom_formatting_options'];
677
  }
678

    
679
  // add code for filebrowser for users that have access
680
  $filebrowser = !empty($conf['filebrowser']) ? $conf['filebrowser'] : 'none';
681
  $filebrowser_image = !empty($conf['filebrowser_image']) ? $conf['filebrowser_image'] : $filebrowser;
682
  $filebrowser_flash = !empty($conf['filebrowser_flash']) ? $conf['filebrowser_flash'] : $filebrowser;
683

    
684
  if ($filebrowser == 'imce' && !module_exists('imce')) {
685
    $filebrowser = 'none';
686
  }
687

    
688
  if ($filebrowser == 'elfinder' && !module_exists('elfinder')) {
689
    $filebrowser = 'none';
690
  }
691

    
692
  /* MODULES NOT PORTED TO D7
693
    if ($filebrowser == 'tinybrowser' && !module_exists('tinybrowser')) {
694
    $filebrowser = 'none';
695
    }
696

    
697
    if ($filebrowser == 'ib' && !module_exists('imagebrowser')) {
698
    $filebrowser = 'none';
699
    }
700
    if ($filebrowser == 'webfm' && !module_exists('webfm_popup')) {
701
    $filebrowser = 'none';
702
    }
703
   */
704
  if ($filebrowser_image != $filebrowser) {
705
    if ($filebrowser_image == 'imce' && !module_exists('imce')) {
706
      $filebrowser_image = $filebrowser;
707
    }
708

    
709
    if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
710
      $filebrowser_image = $filebrowser;
711
    }
712
    /* MODULES NOT PORTED TO D7
713
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
714
      $filebrowser_image = $filebrowser;
715
      }
716
      if ($filebrowser_image == 'ib' && !module_exists('imagebrowser')) {
717
      $filebrowser_image = $filebrowser;
718
      }
719
      if ($filebrowser_image == 'webfm' && !module_exists('webfm_popup')) {
720
      $filebrowser_image = $filebrowser;
721
      }
722
     */
723
  }
724

    
725
  if ($filebrowser_flash != $filebrowser) {
726
    if ($filebrowser_flash == 'imce' && !module_exists('imce')) {
727
      $filebrowser_flash = $filebrowser;
728
    }
729

    
730
    if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
731
      $filebrowser_flash = $filebrowser;
732
    }
733
    /* MODULES NOT PORTED TO D7
734
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
735
      $filebrowser_flash = $filebrowser;
736
      }
737
      if ($filebrowser_flash == 'ib' && !module_exists('imagebrowser')) {
738
      $filebrowser_flash = $filebrowser;
739
      }
740
      if ($filebrowser_flash == 'webfm' && !module_exists('webfm_popup')) {
741
      $filebrowser_flash = $filebrowser;
742
      }
743
     */
744
  }
745

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

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

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

    
907
  if (!empty($conf['js_conf'])) {
908
    preg_match_all('#config\.(\w+)[\s]*=[\s]*(.+?);[\s]*(?=config\.|$)#is', preg_replace("/[\n\r]+/", "", $conf['js_conf']), $matches);
909
    foreach ($matches[2] as $i => $match) {
910
      if (!empty($match)) {
911
        $value = trim($match, " ;\n\r\t\0\x0B");
912
        if (strcasecmp($value, 'true') == 0) {
913
          $value = TRUE;
914
        }
915
        if (strcasecmp($value, 'false') == 0) {
916
          $value = FALSE;
917
        }
918
        $settings["js_conf"][$matches[1][$i]] = $value;
919
      }
920
    }
921
  }
922

    
923
  $settings['stylesCombo_stylesSet'] = "drupal:" . $module_drupal_path . '/ckeditor.styles.js';
924
  if (!empty($conf['css_style'])) {
925
    if ($conf['css_style'] == 'theme' && file_exists($themepath . 'ckeditor.styles.js')) {
926
      $settings['stylesCombo_stylesSet'] = "drupal:" . $host . $themepath . 'ckeditor.styles.js';
927
    }
928
    elseif (!empty($conf['css_style']) && $conf['css_style'] == 'self') {
929
      $conf['styles_path'] = str_replace("%h%t", "%t", $conf['styles_path']);
930
      $settings['stylesCombo_stylesSet'] = "drupal:" . str_replace(array('%h', '%t', '%m'), array($host, $host . $themepath, $module_drupal_path), $conf['styles_path']);
931
    }
932
  }
933

    
934
  // add custom stylesheet if configured
935
  // lets hope it exists but we'll leave that to the site admin
936
  $query_string = '?' . variable_get('css_js_query_string', '0');
937
  $css_files = array();
938
  switch ($conf['css_mode']) {
939
    case 'theme':
940
      global $language, $base_theme_info;
941
      $themes = list_themes();
942
      $theme_info = $themes[$current_theme];
943
      if (!empty($theme_info->stylesheets)) {
944
        $editorcss = "\"";
945
        foreach ($base_theme_info as $base) { // Grab stylesheets from base theme
946
          if (!empty($base->stylesheets)) { // may be empty when the base theme reference in the info file is invalid
947
            foreach ($base->stylesheets as $type => $stylesheets) {
948
              if ($type != "print") {
949
                foreach ($stylesheets as $name => $path) {
950
                  if (file_exists($path)) {
951
                    $css_files[$name] = $host . $path . $query_string;
952
                    // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
953
                    if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
954
                      $rtl_path = substr($path, 0, -4) . "-rtl.css";
955
                      if (file_exists($rtl_path)) {
956
                        $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
957
                      }
958
                    }
959
                  }
960
                }
961
              }
962
            }
963
          }
964
        }
965
        if (!empty($theme_info->stylesheets)) { // Grab stylesheets from current theme
966
          foreach ($theme_info->stylesheets as $type => $stylesheets) {
967
            if ($type != "print") {
968
              foreach ($stylesheets as $name => $path) {
969
                // Checks if less module exists...
970
                if (strstr($path, '.less') && module_exists('less')) {
971
                  $path = 'sites/default/files/less/' . $path; // append the less file path
972
                  $path = str_replace('.less', '', $path); // remove the .less
973
                }
974
                if (file_exists($path)) {
975
                  $css_files[$name] = $host . $path . $query_string;
976
                  // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
977
                  if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
978
                    $rtl_path = substr($path, 0, -4) . "-rtl.css";
979
                    if (file_exists($rtl_path)) {
980
                      $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
981
                    }
982
                  }
983
                }
984
                elseif (!empty($css_files[$name])) {
985
                  unset($css_files[$name]);
986
                }
987
              }
988
            }
989
          }
990
        }
991
        // Grab stylesheets local.css and local-rtl.css if they exist (fusion based themes)
992
        if (file_exists($themepath . 'css/local.css')) {
993
          $css_files[] = $host . $themepath . 'css/local.css' . $query_string;
994
        }
995
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && file_exists($themepath . 'css/local-rtl.css')) {
996
          $css_files[] = $host . $themepath . 'css/local-rtl.css' . $query_string;
997
        }
998

    
999
        // Grab stylesheets from color module
1000
        $color_paths = variable_get('color_' . $current_theme . '_stylesheets', array());
1001
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
1002
          if (!empty($color_paths[1])) {
1003
            $css_files[] = $host . $color_paths[1] . $query_string;
1004
          }
1005
        }
1006
        elseif (!empty($color_paths[0])) {
1007
          $css_files[] = $host . $color_paths[0] . $query_string;
1008
        }
1009
      }
1010
      else {
1011
        if (!file_exists($themepath . 'ckeditor.css') && file_exists($themepath . 'style.css')) {
1012
          $css_files[] = $host . $themepath . 'style.css' . $query_string;
1013
        }
1014
      }
1015
      if (file_exists($module_drupal_local_path . '/css/ckeditor.css')) {
1016
        $css_files[] = $module_drupal_path . '/css/ckeditor.css' . $query_string;
1017
      }
1018
      if (file_exists($themepath . 'ckeditor.css')) {
1019
        $css_files[] = $host . $themepath . 'ckeditor.css' . $query_string;
1020
      }
1021
      break;
1022

    
1023
    case 'self':
1024
      if (file_exists($module_drupal_local_path . '/css/ckeditor.css')) {
1025
        $css_files[] = $module_drupal_path . '/css/ckeditor.css' . $query_string;
1026
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
1027
          if (file_exists($module_drupal_local_path . '/css/ckeditor-rtl.css')) {
1028
            $css_files[] = $module_drupal_path . '/css/ckeditor-rtl.css' . $query_string;
1029
          }
1030
        }
1031
      }
1032
      foreach (explode(',', $conf['css_path']) as $css_path) {
1033
        $css_path = trim(str_replace("%h%t", "%t", $css_path));
1034
        $css_files[] = str_replace(array('%h', '%t'), array($host, $host . $themepath), $css_path) . $query_string;
1035
      }
1036
      break;
1037

    
1038
    case 'none':
1039
      if (file_exists($module_drupal_local_path . '/css/ckeditor.css')) {
1040
        $css_files[] = $module_drupal_path . '/css/ckeditor.css' . $query_string;
1041
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
1042
          if (file_exists($module_drupal_local_path . '/css/ckeditor-rtl.css')) {
1043
            $css_files[] = $module_drupal_path . '/css/ckeditor-rtl.css' . $query_string;
1044
          }
1045
        }
1046
      }
1047
      if ($editor_local_path != '<URL>') {
1048
        if (file_exists($editor_local_path . '/contents.css')) {
1049
          $css_files[] = $editor_path . '/contents.css' . $query_string;
1050
        }
1051
      }
1052
      else {
1053
        $editor_url_path = ckeditor_path('url');
1054
        $css_files[] = $editor_url_path . '/contents.css' . $query_string;
1055
      }
1056
      break;
1057
  }
1058

    
1059
  if (isset($conf['ckeditor_load_method']) && $conf['ckeditor_load_method'] == 'ckeditor_source.js') {
1060
    foreach ($css_files as $k => $v) {
1061
      $css_files[$k] = $v . '&t=' . time();
1062
    }
1063
  }
1064

    
1065
  $settings['contentsCss'] = array_values($css_files);
1066

    
1067
  if (!empty($conf['uicolor']) && $conf['uicolor'] == "custom" && !empty($conf['uicolor_user'])) {
1068
    $settings['uiColor'] = $conf['uicolor_user'];
1069
  }
1070

    
1071
  if (!empty($conf['uicolor']) && strpos($conf['uicolor'], "color_") === 0) {
1072
    if (function_exists('color_get_palette')) {
1073
      $palette = @color_get_palette($current_theme, FALSE); //[#652274]
1074
      $color = str_replace("color_", "", $conf['uicolor']);
1075
      if (!empty($palette[$color])) {
1076
        $settings['uiColor'] = $palette[$color];
1077
      }
1078
    }
1079
  }
1080

    
1081
  if (!empty($settings['loadPlugins'])) {
1082
    // Remove orphaned plugins (coming from disabled modules).
1083
    $available_plugins = array_keys(ckeditor_load_plugins());
1084
    $settings['loadPlugins'] = array_intersect_key($settings['loadPlugins'], array_flip($available_plugins));
1085
  }
1086

    
1087
  // Allow modules to modify the settings.
1088
  drupal_alter('ckeditor_settings', $settings, $conf);
1089

    
1090
  return $settings;
1091
}
1092

    
1093
/**
1094
 * Load CKEditor for field ID
1095
 *
1096
 * @param object $field
1097
 * @param string $format
1098
 *
1099
 * @return object
1100
 *
1101
 */
1102
function ckeditor_load_by_field($field, $format, $show_toggle = TRUE, $add_fields_to_toggle = FALSE) {
1103
  global $user, $theme;
1104
  static $processed_ids = array();
1105
  static $is_running = FALSE;
1106
  $use_ckeditor = FALSE;
1107
  $format_arr = FALSE;
1108
  $suffix = '';
1109

    
1110
  if (is_array($format)) {
1111
    $format_arr = $format;
1112
    $format = isset($format_arr['#value']) ? $format_arr['#value'] : $format_arr['#default_value'];
1113
  }
1114

    
1115
  if (!isset($field['#id'])) {
1116
    return $field;
1117
  }
1118

    
1119
  if (isset($processed_ids[$field['#id']])) {
1120
    return $field;
1121
  }
1122

    
1123
  if (key_exists('#wysiwyg', $field) && !$field['#wysiwyg']) {
1124
    return $field;
1125
  }
1126

    
1127
  if (isset($field['#access']) && !$field['#access']) {
1128
    return $field;
1129
  }
1130

    
1131
  if ($field['#id'] == "edit-log") {
1132
    return $field;
1133
  }
1134

    
1135
  if (isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled') {
1136
    return $field;
1137
  }
1138

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

    
1141
  if (!isset($processed_ids[$field['#id']])) {
1142
    $processed_ids[$field['#id']] = array();
1143
  }
1144

    
1145
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
1146
  $profile = ckeditor_get_profile($format);
1147
  $host = base_path();
1148

    
1149
  if ($profile === FALSE) {
1150
    $ckeditor_in_default_format = FALSE;
1151
    foreach ((array) $format_arr['#options'] as $key => $val) {
1152
      if ($key == $format)
1153
        continue;
1154
      if ($profile = ckeditor_get_profile($key)) {
1155
        $use_ckeditor = $key;
1156
        break;
1157
      }
1158
    }
1159
    if ($use_ckeditor === FALSE) {
1160
      return $field;
1161
    }
1162
  }
1163
  else {
1164
    $ckeditor_in_default_format = TRUE;
1165
  }
1166

    
1167
  if ($settings = ckeditor_profiles_compile($format)) {
1168
    $ckeditor_on = ($settings['default'] == 't' && $profile->settings['default'] == 't') ? TRUE : FALSE;
1169
  }
1170
  elseif ($settings = ckeditor_profiles_compile($use_ckeditor)) {
1171
    $ckeditor_on = FALSE;
1172
  }
1173
  else {
1174
    return $field;
1175
  }
1176

    
1177
  // Attach the editor css.
1178
  $field['#attached']['css'][] = drupal_get_path('module', 'ckeditor') . '/css/ckeditor.editor.css';
1179

    
1180
  if ($settings) {
1181
    $textarea_id = $field['#id'];
1182
    $class[] = 'ckeditor-mod';
1183
    $_ckeditor_ids[] = $textarea_id;
1184

    
1185
    //settings are saved as strings, not booleans
1186
    if ($settings['show_toggle'] == 't' && $show_toggle) {
1187

    
1188
      if ($add_fields_to_toggle !== FALSE) {
1189
        if (is_array($add_fields_to_toggle)) {
1190
          $toggle_fields = "['" . $textarea_id . "','" . implode("','", $add_fields_to_toggle) . "']";
1191
        }
1192
        else {
1193
          $toggle_fields = "['" . $textarea_id . "','" . $add_fields_to_toggle . "']";
1194
        }
1195
      }
1196
      else {
1197
        $toggle_fields = "['{$textarea_id}']";
1198
      }
1199

    
1200
      $wysiwyg_link = '';
1201
      $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}\">";
1202
      $wysiwyg_link .= $ckeditor_on ? t('Switch to plain text editor') : t('Switch to rich text editor');
1203
      $wysiwyg_link .= '</a>';
1204

    
1205
      // Make sure to append to #suffix so it isn't completely overwritten
1206
      $suffix .= $wysiwyg_link;
1207
    }
1208

    
1209
    $editor_local_path = ckeditor_path('local');
1210
    $editor_url_path = ckeditor_path('url');
1211

    
1212
    if (!$is_running) {
1213
      // By default sessions are not started automatically for anonymous users.
1214
      // Start one for editing content so that we had a consistent token that is used in XSS filter.
1215
      if (isset($field['#entity']) && !empty($field['#entity']->created) && empty($user->uid)) {
1216
        drupal_session_start();
1217
        $_SESSION['ckeditor_anonymous_user'] = true;
1218
        drupal_page_is_cacheable(FALSE);
1219
      }
1220

    
1221
      if (!$ckeditor_in_default_format) {
1222
        $load_method = 'ckeditor_basic.js';
1223
        $load_time_out = 0;
1224
      }
1225
      elseif (isset($profile->settings['ckeditor_load_method'])) {
1226
        $load_method = $profile->settings['ckeditor_load_method'];
1227
        $load_time_out = $profile->settings['ckeditor_load_time_out'];
1228
      }
1229
      if ($editor_local_path != '<URL>') {
1230
        drupal_add_js('window.CKEDITOR_BASEPATH = "' . ckeditor_path('relative') . '/"', array('type' => 'inline', 'weight' => -100));
1231
      }
1232
      drupal_add_js(ckeditor_module_path('url') . '/includes/ckeditor.utils.js', array('type' => 'file', 'scope' => 'footer'));
1233

    
1234
      $preprocess = FALSE;
1235
      if (isset($global_profile->settings['ckeditor_aggregate']) && $global_profile->settings['ckeditor_aggregate'] == 't') {
1236
        $preprocess = TRUE;
1237
      }
1238

    
1239
      if ($editor_local_path == '<URL>') {
1240
        drupal_add_js($editor_url_path . '/ckeditor.js', array('type' => 'external', 'scope' => 'footer'));
1241
      }
1242
      else if (isset($load_method) && file_exists($editor_local_path . '/' . $load_method)) {
1243
        drupal_add_js($editor_url_path . '/' . $load_method, array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
1244
        if ($load_method == 'ckeditor_basic.js') {
1245
          drupal_add_js('CKEDITOR.loadFullCoreTimeout = ' . $load_time_out . ';', array('type' => 'inline', 'scope' => 'footer'));
1246
          drupal_add_js(array('ckeditor' => array('load_timeout' => TRUE)), 'setting');
1247
        }
1248
      }
1249
      else {
1250
        drupal_add_js($editor_url_path . '/ckeditor.js', array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
1251
      }
1252
      $ckeditor_url = ckeditor_path('relative');
1253
      if ($ckeditor_url == '<URL>') {
1254
        $ckeditor_url = ckeditor_path('url');
1255
      }
1256
      $ckeditor_url .= '/';
1257
      drupal_add_js(array('ckeditor' => array('module_path' => ckeditor_module_path('relative'), 'editor_path' => $ckeditor_url)), 'setting');
1258
      if (module_exists('paging')) {
1259
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1260
      }
1261
      if (module_exists('linktocontent_node')) {
1262
        drupal_add_js(array('ckeditor' => array('linktocontent_node' => TRUE)), 'setting');
1263
      }
1264
      if (module_exists('linktocontent_menu')) {
1265
        drupal_add_js(array('ckeditor' => array('linktocontent_menu' => TRUE)), 'setting');
1266
      }
1267
      if (module_exists('pagebreak')) {
1268
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1269
      }
1270
      if (module_exists('smart_paging')) {
1271
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1272
      }
1273
      drupal_add_js(array('ckeditor' => array('ajaxToken' => drupal_get_token('ckeditorAjaxCall'), 'xss_url' => url('ckeditor/xss'))), 'setting');
1274
      $is_running = TRUE;
1275
    }
1276

    
1277
    drupal_add_js(array('ckeditor' => array('theme' => $theme)), 'setting');
1278
    if (!empty($settings)) {
1279
      drupal_add_js(array('ckeditor' => array('elements' => array($textarea_id => $format))), 'setting');
1280
    }
1281
    if (!empty($ckeditor_on)) {
1282
      drupal_add_js(array('ckeditor' => array('autostart' => array($textarea_id => $ckeditor_on))), 'setting');
1283
    }
1284
    //[#1473010]
1285
    if (isset($settings['scayt_sLang'])) {
1286
      drupal_add_js(array('ckeditor' => array('scayt_language' => $settings['scayt_sLang'])), 'setting');
1287
    }
1288
    elseif (!empty($field["#language"]) && $field["#language"] != LANGUAGE_NONE) {
1289
      drupal_add_js(array('ckeditor' => array('scayt_language' => ckeditor_scayt_langcode($field["#language"]))), 'setting');
1290
    }
1291

    
1292
    // Remember extra information and reuse it during "Preview"
1293
    $processed_ids[$field['#id']]['suffix'] = $suffix;
1294
    $processed_ids[$field['#id']]['class'] = $class;
1295

    
1296
    if (empty($field['#suffix'])) {
1297
      $field['#suffix'] = $suffix;
1298
    }
1299
    else {
1300
      $field['#suffix'] .= $suffix;
1301
    }
1302

    
1303
    if (empty($field['#attributes']['class'])) {
1304
      $field['#attributes']['class'] = $class;
1305
    }
1306
    else {
1307
      $field['#attributes']['class'] = array_merge($field['#attributes']['class'], $class);
1308
    }
1309
  }
1310

    
1311
  return $field;
1312
}
1313

    
1314
/**
1315
 * Return all modules that provide security filters.
1316
 */
1317
function ckeditor_security_filters() {
1318
  $security_filters = array();
1319

    
1320
  $security_filters['modules'] = array(
1321
    'htmLawed' => array(
1322
      'title' => 'htmLawed',
1323
      'project_page' => 'http://drupal.org/project/htmLawed',
1324
      'weight' => 0,
1325
      'installed' => FALSE,
1326
      'filters' => array()
1327
    ),
1328
    'htmltidy' => array(
1329
      'title' => 'Htmltidy',
1330
      'project_page' => 'http://drupal.org/project/htmltidy',
1331
      'weight' => 0,
1332
      'installed' => FALSE,
1333
      'filters' => array()
1334
    ),
1335
    'htmlpurifier' => array(
1336
      'title' => 'HTML Purifier',
1337
      'project_page' => 'http://drupal.org/project/htmlpurifier',
1338
      'weight' => 0,
1339
      'installed' => FALSE,
1340
      'filters' => array()
1341
    ),
1342
    'wysiwyg_filter' => array(
1343
      'title' => 'WYSIWYG Filter',
1344
      'project_page' => 'http://drupal.org/project/wysiwyg_filter',
1345
      'weight' => 0,
1346
      'installed' => FALSE,
1347
      'filters' => array()
1348
    )
1349
  );
1350

    
1351
  $security_filters['filters'] = array();
1352

    
1353
  foreach ($security_filters['modules'] as $module_name => $module_conf) {
1354
    if (module_exists($module_name)) {
1355
      $security_filters['modules'][$module_name]['installed'] = TRUE;
1356
      $module_filters = module_invoke($module_name, 'filter_info');
1357
      foreach ($module_filters as $module_filter_name => $module_filter_conf) {
1358
        $security_filters['modules'][$module_name]['filters'][$module_filter_name] = $module_filter_conf;
1359
        $security_filters['filters'][$module_filter_name] = TRUE;
1360
      }
1361
    }
1362
  }
1363

    
1364
  //add filters from Drupal core
1365
  $security_filters['modules']['__drupal'] = array(
1366
    'title' => 'Drupal core',
1367
    'project_page' => FALSE,
1368
    'weight' => -1,
1369
    'installed' => TRUE,
1370
    'filters' => array(
1371
      'filter_html' => array(
1372
        'title' => 'Limit allowed HTML tags',
1373
        'description' => 'Removes the attributes that the built-in "Limit allowed HTML tags"-filter does not allow inside HTML elements/tags'
1374
      )
1375
    )
1376
  );
1377
  $security_filters['filters']['filter_html'] = TRUE;
1378

    
1379
  //load security filters added by API
1380
  $external_module_filters = module_invoke_all('ckeditor_security_filter');
1381
  if (count($external_module_filters) > 0) {
1382
    $security_filters['modules']['__external'] = array(
1383
      'title' => 'External filters',
1384
      'project_page' => FALSE,
1385
      'weight' => 1,
1386
      'installed' => TRUE,
1387
      'filters' => array()
1388
    );
1389
    foreach ($external_module_filters as $module_filter_name => $module_filter_conf) {
1390
      $security_filters['modules']['__external']['filters'][$module_filter_name] = $module_filter_conf;
1391
      $security_filters['filters'][$module_filter_name] = TRUE;
1392
    }
1393
  }
1394
  drupal_alter('ckeditor_security_filter', $security_filters);
1395

    
1396
  return $security_filters;
1397
}