Projet

Général

Profil

Paste
Télécharger (54,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / includes / ckeditor.lib.inc @ 5a7e6170

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 (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 (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
  //oops, we have no information about skins, let's use only default
182
  if (empty($arr)) {
183
    $arr = array(
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
  //if any default theme not exists select first from the list
206
  return key(reset($skin_options));
207
}
208

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

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

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

    
323
  return 'en_US';
324
}
325

    
326
/**
327
 * List of CKEditor plugins;
328
 *
329
 * @return array
330
 */
331
function ckeditor_load_plugins($render = FALSE) {
332
  $arr = array();
333
  $base_path = '%base_path%';
334
  $editor_path = '%editor_path%';
335
  $ckeditor_path = '%module_path%';
336
  $plugin_dir = '%plugin_dir%';
337
  $plugin_dir_additional = '%plugin_dir_extra%';
338
  $pattern = '#\.addButton\([\s]*[\'"](.*?)[\'"][\s]*\,[\s]*\{[\s]*(.*?)[\s]*\}#s';
339

    
340
  /*
341
   * External plugins
342
   */
343
  if (module_exists('ckeditor_swf') && file_exists(drupal_get_path('module', 'ckeditor_swf') . '/plugins/swf/plugin.js')) {
344
    $arr['ckeditor_swf'] = array(
345
      'name' => 'swf',
346
      'desc' => t('Support for the CKEditor SWF module'),
347
      'path' => $base_path . drupal_get_path('module', 'ckeditor_swf') . '/plugins/swf/',
348
      'buttons' => FALSE,
349
      'default' => 't'
350
    );
351
  }
352

    
353
  if (module_exists('ckeditor_link') && file_exists(drupal_get_path('module', 'ckeditor_link') . '/plugins/link/plugin.js')) {
354
    $arr['ckeditor_link'] = array(
355
      'name' => 'drupal_path',
356
      'desc' => t('Support for the CKEditor Link module'),
357
      'path' => $base_path . drupal_get_path('module', 'ckeditor_link') . '/plugins/link/',
358
      'buttons' => FALSE,
359
      'default' => 't'
360
    );
361
  }
362

    
363
  if (module_exists('linkit') && file_exists(drupal_get_path('module', 'linkit') . '/editors/ckeditor/plugin.js')) {
364
    $arr['linkit'] = array(
365
      'name' => 'Linkit',
366
      'desc' => t('Support for the Linkit module <em>(buttons: Linkit)</em>'),
367
      'path' => $base_path . drupal_get_path('module', 'linkit') . '/editors/ckeditor/',
368
      'buttons' => array(
369
        'Linkit' => array(
370
          'title' => 'Linkit',
371
          'icon' => $base_path . drupal_get_path('module', 'linkit') . '/editors/ckeditor/linkit.png'
372
        )
373
      ),
374
      'default' => 't'
375
    );
376
  }
377

    
378
  /*
379
   * CKEditor build-in plugins
380
   */
381
  $_editor_path = ckeditor_path('local') . '/';
382
  if (file_exists($_editor_path . 'plugins/tableresize/plugin.js')) {
383
    $arr['tableresize'] = array(
384
      'name' => 'tableresize',
385
      'desc' => t('Table Resize plugin'),
386
      'path' => $editor_path . 'plugins/tableresize/',
387
      'buttons' => FALSE,
388
      'default' => 't'
389
    );
390
  }
391

    
392
  if (file_exists($_editor_path . 'plugins/autogrow/plugin.js')) {
393
    $arr['autogrow'] = array(
394
      'name' => 'autogrow',
395
      'desc' => t('Auto Grow plugin'),
396
      'path' => $editor_path . 'plugins/autogrow/',
397
      'buttons' => FALSE,
398
      'default' => 'f'
399
    );
400
  }
401

    
402
  if (file_exists($_editor_path . 'plugins/stylesheetparser/plugin.js')) {
403
    $arr['stylesheetparser'] = array(
404
      'name' => 'stylesheetparser',
405
      'desc' => t('Stylesheet Parser plugin'),
406
      'path' => $editor_path . 'plugins/stylesheetparser/',
407
      'buttons' => FALSE,
408
      'default' => 'f'
409
    );
410
  }
411

    
412
  /*
413
   * CKEditor module plugins
414
   */
415
  $_plugin_dir = ckeditor_module_path('local') . '/plugins/';
416
  if ($handle = opendir($_plugin_dir)) {
417
    while (false !== ($file = readdir($handle))) {
418
      if (is_dir($_plugin_dir . $file) && file_exists($_plugin_dir . $file . '/plugin.js')) {
419
        $source = file_get_contents($_plugin_dir . $file . '/plugin.js');
420
        $buttons = array();
421
        if (preg_match_all($pattern, $source, $matches)) {
422
          foreach ($matches[1] as $i => $button_name) {
423
            if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$i], $matches2)) {
424
              $buttons[$button_name] = array();
425
              $buttons[$button_name]['label'] = $button_name;
426
              $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]);
427
              $buttons[$button_name]['icon'] = trim($matches2[2]);
428
            }
429
          }
430
        }
431
        if (preg_match('#@file ([^\n\r]*)#', $source, $matches)) {
432
          $arr[$file] = array(
433
            'name' => $file,
434
            'desc' => t($matches[1]),
435
            'path' => $plugin_dir . $file . '/',
436
            'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
437
            'default' => 'f'
438
          );
439
        }
440
        else {
441
          $arr[$file] = array(
442
            'name' => $file,
443
            'desc' => t('Plugin file: ' . $file),
444
            'path' => $plugin_dir . $file . '/',
445
            'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
446
            'default' => 'f'
447
          );
448
        }
449
      }
450
    }
451
    closedir($handle);
452
  }
453

    
454
  /*
455
   * CKEditor module plugins - additional directory
456
   */
457
  $_plugin_dir_additional = ckeditor_plugins_path('local') . '/';
458
  if ($_plugin_dir != $_plugin_dir_additional && is_dir($_plugin_dir_additional) && $handle = opendir($_plugin_dir_additional)) {
459
    while (false !== ($file = readdir($handle))) {
460
      if (is_dir($_plugin_dir_additional . $file) && file_exists($_plugin_dir_additional . $file . '/plugin.js')) {
461
        $source = file_get_contents($_plugin_dir_additional . $file . '/plugin.js');
462
        $buttons = array();
463
        if (preg_match_all($pattern, $source, $matches)) {
464
          foreach ($matches[1] as $i => $button_name) {
465
            if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$i], $matches2)) {
466
              $buttons[$button_name] = array();
467
              $buttons[$button_name]['label'] = $button_name;
468
              $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]);
469
              $buttons[$button_name]['icon'] = trim($matches2[2]);
470
            }
471
          }
472
        }
473
        if (preg_match('#@file ([^\n\r]*)#', $source, $matches)) {
474
          $arr[$file] = array(
475
            'name' => $file,
476
            'desc' => t($matches[1]),
477
            'path' => $plugin_dir_additional . $file . '/',
478
            'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
479
            'default' => 'f'
480
          );
481
        }
482
        else {
483
          $arr[$file] = array(
484
            'name' => $file,
485
            'desc' => t('Plugin file: ' . $file),
486
            'path' => $plugin_dir_additional . $file . '/',
487
            'buttons' => (count($buttons) > 0) ? $buttons : FALSE,
488
            'default' => 'f'
489
          );
490
        }
491
      }
492
    }
493

    
494
    closedir($handle);
495
  }
496

    
497
  /*
498
   * CKEditor plugins registered by hook
499
   */
500
  $plugins = module_invoke_all('ckeditor_plugin');
501

    
502
  foreach ($plugins as $i => $plugin) {
503
    if (file_exists($plugin['path'] . 'plugin.js')) {
504
      $source = file_get_contents($plugin['path'] . 'plugin.js');
505
      $plugins[$i]['path'] = $base_path . $plugin['path'];
506
      if (!isset($plugin['buttons']) || count($plugin['buttons']) == 0) {
507
        $buttons = array();
508
        if (preg_match_all($pattern, $source, $matches)) {
509
          foreach ($matches[1] as $j => $button_name) {
510
            if (preg_match('#(icon)[\s]*\:[\s]*([^\,\n]*)#', $matches[2][$j], $matches2)) {
511
              $buttons[$button_name] = array();
512
              $buttons[$button_name]['label'] = $button_name;
513
              $matches2[2] = str_replace(array('this.path', '+', '\'', '"'), array('', '', '', ''), $matches2[2]);
514
              $buttons[$button_name]['icon'] = trim($matches2[2]);
515
            }
516
          }
517
        }
518
        $plugins[$i]['buttons'] = (count($buttons) > 0) ? $buttons : FALSE;
519
      }
520
    }
521
    else {
522
      unset($plugins[$i]);
523
    }
524
  }
525
  $arr = array_merge($arr, $plugins);
526

    
527
  if (isset($arr['media']) && module_exists('media') == FALSE) {
528
    unset($arr['media']);
529
  }
530

    
531
  if (isset($arr['imce']) && module_exists('imce') == FALSE) {
532
    unset($arr['imce']);
533
  }
534
  //remove page break button if there is no module to do this
535
  if (isset($arr['drupalbreaks']['buttons']['DrupalPageBreak']) && !module_exists('paging') && !module_exists('pagebreak')) {
536
    unset($arr['drupalbreaks']['buttons']['DrupalPageBreak']);
537
  }
538

    
539
  if (isset($arr['drupalbreaks'])) {
540
    $arr['drupalbreaks']['default'] = 't';
541
  }
542

    
543
  ksort($arr);
544
  if ($render === TRUE) {
545
    $arr = ckeditor_plugins_render($arr);
546
  }
547
  return $arr;
548
}
549

    
550
/**
551
 * Render CKEditor plugins path
552
 */
553
function ckeditor_plugins_render($plugins) {
554
  $render = array();
555
  $render["%base_path%"] = ckeditor_base_path('relative') . '/';
556
  $render["%editor_path%"] = ckeditor_path('relative') . '/';
557
  $render["%module_path%"] = ckeditor_module_path('relative') . '/';
558
  $render["%plugin_dir%"] = $render["%module_path%"] . 'plugins/';
559
  $render["%plugin_dir_extra%"] = ckeditor_plugins_path('relative') . '/';
560

    
561
  foreach ((array) $plugins as $i => $plugin) {
562
    $plugins[$i]['path'] = str_replace(array_keys($render), array_values($render), $plugin['path']);
563
  }
564

    
565
  return $plugins;
566
}
567

    
568
/**
569
 * Get default ckeditor settings
570
 *
571
 * @return array
572
 */
573
function ckeditor_user_get_setting_default() {
574
  $default = array(
575
    'default' => 't',
576
    'show_toggle' => 't',
577
    'width' => '100%',
578
    'lang' => 'en',
579
    'auto_lang' => 't',
580
  );
581

    
582
  return $default;
583
}
584

    
585
/**
586
 * Return CKEditor settings
587
 *
588
 * @param object $user
589
 * @param object $profile
590
 * @param string $setting
591
 * @return array
592
 */
593
function ckeditor_user_get_setting($user, $profile, $setting) {
594
  $default = ckeditor_user_get_setting_default();
595

    
596
  if (user_access('customize ckeditor')) {
597
    $status = isset($user->data['ckeditor_' . $setting]) ? $user->data['ckeditor_' . $setting] : (isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting]);
598
  }
599
  else {
600
    $status = isset($profile->settings[$setting]) ? $profile->settings[$setting] : $default[$setting];
601
  }
602

    
603
  return $status;
604
}
605

    
606
/**
607
 * Return CKEditor profile by input format
608
 *
609
 * @param string $input_format
610
 * @return object|boolean
611
 */
612
function ckeditor_get_profile($input_format) {
613
  $select = db_select('ckeditor_settings', 's');
614
  $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
615
  $result = $select->fields('s', array("name"))->condition('f.format', $input_format)->condition('f.name', 'CKEditor Global Profile', '<>')->range(0, 1)->execute()->fetchAssoc();
616

    
617
  if ($result && $profile = ckeditor_profile_load($result['name'])) {
618
    return $profile;
619
  }
620

    
621
  return FALSE;
622
}
623

    
624
/**
625
 * Return CKEditor profile list
626
 */
627
function ckeditor_profile_input_formats() {
628
  $select = db_select('ckeditor_settings', 's');
629
  $select->join('ckeditor_input_format', 'f', 'f.name = s.name');
630
  $result = $select->fields('s', array("name"))->fields('f', array("format"))->execute();
631

    
632
  $list = array();
633
  while ($row = $result->fetchAssoc()) {
634
    if (!isset($row['name'])) {
635
      $list[$row['name']] = array();
636
    }
637
    $list[$row['name']][] = $row['format'];
638
  }
639

    
640
  return $list;
641
}
642

    
643
/**
644
 * Search and return CKEditor plugin path
645
 *
646
 * @return string
647
 */
648
function _ckeditor_script_path() {
649
  $jspath = FALSE;
650
  $module_path = drupal_get_path('module', 'ckeditor');
651

    
652
  if (file_exists($module_path . '/ckeditor/ckeditor.js')) {
653
    $jspath = '%m/ckeditor';
654
  }
655
  elseif (file_exists($module_path . '/ckeditor/ckeditor/ckeditor.js')) {
656
    $jspath = '%m/ckeditor/ckeditor';
657
  }
658
  elseif (file_exists(ckeditor_library_path('url') . '/ckeditor/ckeditor.js')) {
659
    $jspath = '%l/ckeditor';
660
  }
661
  return $jspath;
662
}
663

    
664
/**
665
 * Determines whether the CKEditor sources are present
666
 *
667
 * It checks if ckeditor.js is present.
668
 *
669
 * This function is used by ckeditor_requirements()
670
 *
671
 * @return boolean True if CKEditor is installed
672
 */
673
function _ckeditor_requirements_isinstalled() {
674
  $editor_path = ckeditor_path('local');
675
  $jspath = $editor_path . '/ckeditor.js';
676

    
677
  $jsp = file_exists($jspath);
678
  if (!$jsp && ($editor_path = _ckeditor_script_path())) {
679
    $result = db_select('ckeditor_settings', 's')->fields('s')->condition('name', 'CKEditor Global Profile')->execute()->fetchAssoc();
680
    if ($result) {
681
      $result['settings'] = unserialize($result['settings']);
682
      $result['settings']['ckeditor_path'] = $editor_path;
683
      $result['settings'] = serialize($result['settings']);
684
      db_update('ckeditor_settings')->fields(array("settings" => $result['settings']))->condition('name', 'CKEditor Global Profile')->execute();
685

    
686
      $jsp = TRUE;
687
      ckeditor_path('local', TRUE);
688
    }
689
  }
690
  return $jsp;
691
}
692

    
693
/**
694
 * Compile settings of all profiles at returns is as array
695
 *
696
 * @param string $input_format
697
 * @param boolean $clear
698
 * @return array
699
 */
700
function ckeditor_profiles_compile($input_format = FALSE, $clear = FALSE) {
701
  static $compiled = FALSE;
702
  static $_ckeditor_compiled = array();
703

    
704
  if ($clear !== FALSE && $compiled !== FALSE) {
705
    $compiled = FALSE;
706
  }
707

    
708
  if ($compiled === TRUE) {
709
    return ( $input_format === FALSE ) ? $_ckeditor_compiled : ( isset($_ckeditor_compiled[$input_format]) ? $_ckeditor_compiled[$input_format] : array() );
710
  }
711

    
712
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
713

    
714
  $profiles_list = ckeditor_profile_input_formats();
715

    
716
  foreach ($profiles_list AS $_profile => $_inputs) {
717
    $profile = ckeditor_profile_load($_profile);
718
    $setting = ckeditor_profile_settings_compile($global_profile, $profile);
719

    
720
    foreach ($_inputs AS $_input) {
721
      $_ckeditor_compiled[$_input] = $setting;
722
    }
723
  }
724

    
725
  $compiled = TRUE;
726

    
727
  return ( $input_format === FALSE ) ? $_ckeditor_compiled : $_ckeditor_compiled[$input_format];
728
}
729

    
730
/**
731
 * Compile settings of profile
732
 *
733
 * @param object $global_profile
734
 * @param object $profile
735
 * @return array
736
 */
737
function ckeditor_profile_settings_compile($global_profile, $profile) {
738
  global $user, $language, $theme;
739

    
740
  $current_theme = variable_get('theme_default', $theme);
741

    
742
  $settings = array();
743
  $conf = array();
744
  $conf = $profile->settings;
745
  $profile_name = $profile->name;
746

    
747
  if (user_access('customize ckeditor')) {
748
    foreach (array('default', 'show_toggle', 'width', 'lang', 'auto_lang') as $setting) {
749
      $conf[$setting] = ckeditor_user_get_setting($user, $profile, $setting);
750
    }
751
  }
752

    
753
  if (!isset($conf['ss'])) {
754
    $conf['ss'] = 2;
755
  }
756

    
757
  $themepath = drupal_get_path('theme', $current_theme) . '/';
758
  $host = base_path();
759

    
760
  // setting some variables
761
  $base_path = ckeditor_base_path('relative');
762
  $module_drupal_path = ckeditor_module_path('relative');
763
  $module_drupal_local_path = ckeditor_module_path('local');
764
  $editor_path = ckeditor_path('relative');
765
  $editor_local_path = ckeditor_path('local');
766

    
767
  // get the default drupal files path
768
  $files_path = $host . variable_get('file_private_path', conf_path() . '/files');
769

    
770
  $toolbar = $conf['toolbar'];
771

    
772
  if (!empty($conf['theme_config_js']) && $conf['theme_config_js'] == 't' && file_exists($themepath . 'ckeditor.config.js')) {
773
    $ckeditor_config_path = $host . $themepath . 'ckeditor.config.js?' . @filemtime($themepath . 'ckeditor.config.js');
774
  }
775
  else {
776
    $ckeditor_config_path = $module_drupal_path . "/ckeditor.config.js?" . @filemtime($module_drupal_path . "/ckeditor.config.js");
777
  }
778

    
779
  $settings['customConfig'] = $ckeditor_config_path;
780
  $settings['defaultLanguage'] = $conf['lang'];
781
  $settings['toolbar'] = $toolbar;
782
  $settings['enterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['enter_mode']));
783
  $settings['shiftEnterMode'] = constant("CKEDITOR_ENTERMODE_" . strtoupper($conf['shift_enter_mode']));
784
  $settings['toolbarStartupExpanded'] = ( $conf['expand'] == 't' );
785
  $settings['width'] = $conf['width'];
786
  //check if skin exists, if not select default one
787
  if (isset($global_profile->settings['skin']) && file_exists($editor_local_path . '/skins/' . $global_profile->settings['skin'])) {
788
    $settings['skin'] = $global_profile->settings['skin'];
789
  }
790
  else {
791
    $settings['skin'] = ckeditor_default_skin();
792
  }
793
  $settings['format_tags'] = $conf['font_format'];
794
  $settings['show_toggle'] = $conf['show_toggle'];
795
  $settings['ss'] = $conf['ss'];
796

    
797
  if (isset($conf['language_direction'])) {
798
    switch ($conf['language_direction']) {
799
      case 'default':
800
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
801
          $settings['contentsLangDirection'] = 'rtl';
802
        }
803
        break;
804
      case 'ltr':
805
        $settings['contentsLangDirection'] = 'ltr';
806
        break;
807
      case 'rtl':
808
        $settings['contentsLangDirection'] = 'rtl';
809
        break;
810
    }
811
  }
812

    
813
  if (isset($conf['loadPlugins'])) {
814
    $settings['loadPlugins'] = ckeditor_plugins_render($conf['loadPlugins']);
815

    
816
    if (array_key_exists('media', $settings['loadPlugins']) && module_exists('media')) {
817
      module_load_include('inc', 'media', 'includes/media.browser');
818
      $javascript = media_browser_js();
819
      foreach ($javascript as $key => $definitions) {
820
        foreach ($definitions as $definition) {
821
          $function = 'drupal_add_' . $key;
822
          call_user_func_array($function, $definition);
823
        }
824
      }
825
      drupal_add_js(drupal_get_path('module', 'ckeditor') . '/plugins/media/library.js', array('scope' => 'footer', 'weight' => -20));
826
    }
827
  }
828
  else {
829
    $settings['loadPlugins'] = array();
830
  }
831

    
832
  //add support for divarea plugin from CKE4
833
  if (isset($conf['use_divarea']) && $conf['use_divarea'] == 't' && file_exists($editor_local_path . '/plugins/divarea/plugin.js')) {
834
    $settings['loadPlugins']['divarea'] = array('name' => 'divarea', 'path' => $editor_path . '/plugins/divarea/', 'buttons' => FALSE, 'default' => 'f');
835
  }
836

    
837
  if (isset($conf['html_entities']) && $conf['html_entities'] == 'f') {
838
    $settings['entities'] = FALSE;
839
    $settings['entities_greek'] = FALSE;
840
    $settings['entities_latin'] = FALSE;
841
  }
842
  if (isset($conf['scayt_autoStartup']) && $conf['scayt_autoStartup'] == 't') {
843
    $settings['scayt_autoStartup'] = TRUE;
844
  }
845
  else {
846
    $settings['scayt_autoStartup'] = FALSE;
847
  }
848

    
849
  if ($conf['auto_lang'] == "f") {
850
    $settings['language'] = $conf['lang'];
851
    //[#1473010]
852
    $settings['scayt_sLang'] = ckeditor_scayt_langcode($conf['lang']);
853
  }
854

    
855
  if (isset($conf['forcePasteAsPlainText']) && $conf['forcePasteAsPlainText'] == 't') {
856
    $settings['forcePasteAsPlainText'] = TRUE;
857
  }
858

    
859
  if (isset($conf['custom_formatting']) && $conf['custom_formatting'] == 't') {
860
    foreach ($conf['formatting']['custom_formatting_options'] as $k => $v) {
861
      if ($v === 0) {
862
        $conf['formatting']['custom_formatting_options'][$k] = FALSE;
863
      }
864
      else {
865
        $conf['formatting']['custom_formatting_options'][$k] = TRUE;
866
      }
867
    }
868
    $settings['output_pre_indent'] = $conf['formatting']['custom_formatting_options']['pre_indent'];
869
    unset($conf['formatting']['custom_formatting_options']['pre_indent']);
870
    $settings['custom_formatting'] = $conf['formatting']['custom_formatting_options'];
871
  }
872

    
873
  // add code for filebrowser for users that have access
874
  $filebrowser = !empty($conf['filebrowser']) ? $conf['filebrowser'] : 'none';
875
  $filebrowser_image = !empty($conf['filebrowser_image']) ? $conf['filebrowser_image'] : $filebrowser;
876
  $filebrowser_flash = !empty($conf['filebrowser_flash']) ? $conf['filebrowser_flash'] : $filebrowser;
877

    
878
  if ($filebrowser == 'imce' && !module_exists('imce')) {
879
    $filebrowser = 'none';
880
  }
881

    
882
  if ($filebrowser == 'elfinder' && !module_exists('elfinder')) {
883
    $filebrowser = 'none';
884
  }
885

    
886
  /* MODULES NOT PORTED TO D7
887
    if ($filebrowser == 'tinybrowser' && !module_exists('tinybrowser')) {
888
    $filebrowser = 'none';
889
    }
890

    
891
    if ($filebrowser == 'ib' && !module_exists('imagebrowser')) {
892
    $filebrowser = 'none';
893
    }
894
    if ($filebrowser == 'webfm' && !module_exists('webfm_popup')) {
895
    $filebrowser = 'none';
896
    }
897
   */
898
  if ($filebrowser_image != $filebrowser) {
899
    if ($filebrowser_image == 'imce' && !module_exists('imce')) {
900
      $filebrowser_image = $filebrowser;
901
    }
902

    
903
    if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
904
      $filebrowser_image = $filebrowser;
905
    }
906
    /* MODULES NOT PORTED TO D7
907
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
908
      $filebrowser_image = $filebrowser;
909
      }
910
      if ($filebrowser_image == 'ib' && !module_exists('imagebrowser')) {
911
      $filebrowser_image = $filebrowser;
912
      }
913
      if ($filebrowser_image == 'webfm' && !module_exists('webfm_popup')) {
914
      $filebrowser_image = $filebrowser;
915
      }
916
     */
917
  }
918

    
919
  if ($filebrowser_flash != $filebrowser) {
920
    if ($filebrowser_flash == 'imce' && !module_exists('imce')) {
921
      $filebrowser_flash = $filebrowser;
922
    }
923

    
924
    if ($filebrowser_image == 'elfinder' && !module_exists('elfinder')) {
925
      $filebrowser_flash = $filebrowser;
926
    }
927
    /* MODULES NOT PORTED TO D7
928
      if ($filebrowser_image == 'tinybrowser' && !module_exists('tinybrowser')) {
929
      $filebrowser_flash = $filebrowser;
930
      }
931
      if ($filebrowser_flash == 'ib' && !module_exists('imagebrowser')) {
932
      $filebrowser_flash = $filebrowser;
933
      }
934
      if ($filebrowser_flash == 'webfm' && !module_exists('webfm_popup')) {
935
      $filebrowser_flash = $filebrowser;
936
      }
937
     */
938
  }
939

    
940
  if ($filebrowser == 'ckfinder' || $filebrowser_image == 'ckfinder' || $filebrowser_flash == 'ckfinder') {
941
    if (user_access('allow CKFinder file uploads')) {
942
      if (!empty($profile->settings['UserFilesPath'])) {
943
        $_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));
944
      }
945
      else {
946
        $_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));
947
      }
948
      if (!empty($profile->settings['UserFilesAbsolutePath'])) {
949
        $_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));
950
      }
951
      else {
952
        $_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));
953
      }
954
      if (variable_get('file_default_scheme', '') == 'private') {
955
        $private_dir = isset($global_profile->settings['private_dir']) ? trim($global_profile->settings['private_dir'], '/') : '';
956
        if (!empty($private_dir)) {
957
          $private_dir = strtr($private_dir, array('%u' => $user->uid, '%n' => $user->name));
958
          $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = url('system/files') . '/' . $private_dir . '/';
959
          $private_upload_path = file_uri_target('private://' . variable_get('file_private_path', '')) . '/' . $private_dir;
960
        }
961
        else {
962
          $_SESSION['ckeditor'][$profile_name]['UserFilesPath'] = url('system/files') . '/';
963
          $private_upload_path =  file_uri_target('private://' . variable_get('file_private_path', ''));
964
        }
965
        //add '/' to beginning of path if necessary
966
        if (strpos(variable_get('file_private_path', ''), '/') === 0 && $private_upload_path[0] != '/') {
967
          $private_upload_path = '/' . $private_upload_path;
968
        }
969
        //check if CKEditor private dir exists and create it if not
970
        if ($private_dir && !is_dir($private_upload_path)) {
971
          mkdir($private_upload_path, 0755, TRUE);
972
        }
973
        $_SESSION['ckeditor'][$profile_name]['UserFilesAbsolutePath'] = drupal_realpath($private_upload_path) . '/';
974
      }
975
    }
976
  }
977
  /* MODULES NOT PORTED TO D7
978
    if (in_array('tinybrowser', array($filebrowser, $filebrowser_image, $filebrowser_flash))) {
979
    $popup_win_size = variable_get('tinybrowser_popup_window_size', '770x480');
980
    if (!preg_match('#\d+x\d+#is', $popup_win_size)) {
981
    $popup_win_size = '770x480';
982
    }
983
    $popup_win_size = trim($popup_win_size);
984
    $popup_win_size = strtolower($popup_win_size);
985
    $win_size = split('x', $popup_win_size);
986
    }
987
   */
988
  switch ($filebrowser) {
989
    case 'ckfinder':
990
      if (user_access('allow CKFinder file uploads')) {
991
        $ckfinder_full_path = ckfinder_path('relative');
992
        $settings['filebrowserBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?id=' . $profile_name;
993
        $settings['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
994
        $settings['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Flash&id=' . $profile_name;
995
        $settings['filebrowserUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Files&id=' . $profile_name;
996
        $settings['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
997
        $settings['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Flash&id=' . $profile_name;
998
      }
999
      break;
1000
    case 'imce':
1001
      $settings['filebrowserBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
1002
      break;
1003
    case 'elfinder':
1004
      $settings['filebrowserBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
1005
      break;
1006
    /* MODULES NOT PORTED TO D7
1007
      case 'webfm':
1008
      if (user_access('access webfm')) {
1009
      $settings['filebrowserBrowseUrl'] = $host . "index.php?q=webfm_popup";
1010
      }
1011
      break;
1012
      case 'ib':
1013
      if (user_access('browse own images')) {
1014
      $settings['filebrowserBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
1015
      $settings['filebrowserWindowWidth'] = 700;
1016
      $settings['filebrowserWindowHeight'] = 520;
1017
      }
1018
      break;
1019
      case 'tinybrowser':
1020
      $settings['filebrowserBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=file";
1021
      $settings['filebrowserWindowWidth'] = (int) $win_size[0] + 15;
1022
      $settings['filebrowserWindowHeight'] = (int) $win_size[1] + 15;
1023
      break;
1024
     */
1025
  }
1026

    
1027
  if ($filebrowser_image != $filebrowser) {
1028
    switch ($filebrowser_image) {
1029
      case 'ckfinder':
1030
        if (user_access('allow CKFinder file uploads')) {
1031
          $ckfinder_full_path = ckfinder_path('relative');
1032
          $settings['filebrowserImageBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
1033
          $settings['filebrowserImageUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
1034
        }
1035
        break;
1036
      case 'imce':
1037
        $settings['filebrowserImageBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
1038
        break;
1039
      case 'elfinder':
1040
        $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
1041
        break;
1042
      /* MODULES NOT PORTED TO D7
1043
        case 'webfm':
1044
        if (user_access('access webfm')) {
1045
        $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=webfm_popup";
1046
        }
1047
        break;
1048
        case 'ib':
1049
        if (user_access('browse own images')) {
1050
        $settings['filebrowserImageBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
1051
        $settings['filebrowserImageWindowWidth'] = 680;
1052
        $settings['filebrowserImageWindowHeight'] = 439;
1053
        }
1054
        break;
1055
        case 'tinybrowser':
1056
        $settings['filebrowserImageBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=image";
1057
        $settings['filebrowserImageWindowWidth'] = (int) $win_size[0] + 15;
1058
        $settings['filebrowserImageWindowHeight'] = (int) $win_size[1] + 15;
1059
        break;
1060
       */
1061
    }
1062
  }
1063

    
1064
  if ($filebrowser_flash != $filebrowser) {
1065
    switch ($filebrowser_flash) {
1066
      case 'ckfinder':
1067
        if (user_access('allow CKFinder file uploads')) {
1068
          $ckfinder_full_path = ckfinder_path('relative');
1069
          $settings['filebrowserFlashBrowseUrl'] = $ckfinder_full_path . '/ckfinder.html?Type=Images&id=' . $profile_name;
1070
          $settings['filebrowserFlashUploadUrl'] = $ckfinder_full_path . '/core/connector/php/connector.php?command=QuickUpload&type=Images&id=' . $profile_name;
1071
        }
1072
        break;
1073
      case 'imce':
1074
        $settings['filebrowserFlashBrowseUrl'] = url('imce', array('query' => array('app' => 'ckeditor|sendto@ckeditor_imceSendTo|')));
1075
        break;
1076
      case 'elfinder':
1077
        $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=elfinder&app=ckeditor";
1078
        break;
1079
      /* MODULES NOT PORTED TO D7
1080
        case 'webfm':
1081
        if (user_access('access webfm')) {
1082
        $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=webfm_popup";
1083
        }
1084
        break;
1085
        case 'ib':
1086
        if (user_access('browse own images')) {
1087
        $settings['filebrowserFlashBrowseUrl'] = $host . "index.php?q=imagebrowser/view/browser&app=ckeditor";
1088
        $settings['filebrowserFlashWindowWidth'] = 680;
1089
        $settings['filebrowserFlashWindowHeight'] = 439;
1090
        }
1091
        break;
1092
        case 'tinybrowser':
1093
        $settings['filebrowserFlashBrowseUrl'] = $host . drupal_get_path('module', 'tinybrowser') . "/tinybrowser/tinybrowser.php?type=media";
1094
        $settings['filebrowserFlashWindowWidth'] = (int) $win_size[0] + 15;
1095
        $settings['filebrowserFlashWindowHeight'] = (int) $win_size[1] + 15;
1096
        break;
1097
       */
1098
    }
1099
  }
1100

    
1101
  if (!empty($conf['js_conf'])) {
1102
    preg_match_all('#config\.(\w+)[\s]*=[\s]*(.+?);[\s]*(?=config\.|$)#is', preg_replace("/[\n\r]+/", "", $conf['js_conf']), $matches);
1103
    foreach ($matches[2] as $i => $match) {
1104
      if (!empty($match)) {
1105
        $value = trim($match, " ;\n\r\t\0\x0B");
1106
        if (strcasecmp($value, 'true') == 0) {
1107
          $value = TRUE;
1108
        }
1109
        if (strcasecmp($value, 'false') == 0) {
1110
          $value = FALSE;
1111
        }
1112
        $settings["js_conf"][$matches[1][$i]] = $value;
1113
      }
1114
    }
1115
  }
1116

    
1117
  $settings['stylesCombo_stylesSet'] = "drupal:" . $module_drupal_path . '/ckeditor.styles.js';
1118
  if (!empty($conf['css_style'])) {
1119
    if ($conf['css_style'] == 'theme' && file_exists($themepath . 'ckeditor.styles.js')) {
1120
      $settings['stylesCombo_stylesSet'] = "drupal:" . $host . $themepath . 'ckeditor.styles.js';
1121
    }
1122
    elseif (!empty($conf['css_style']) && $conf['css_style'] == 'self') {
1123
      $conf['styles_path'] = str_replace("%h%t", "%t", $conf['styles_path']);
1124
      $settings['stylesCombo_stylesSet'] = "drupal:" . str_replace(array('%h', '%t', '%m'), array($host, $host . $themepath, $module_drupal_path), $conf['styles_path']);
1125
    }
1126
  }
1127

    
1128
  // add custom stylesheet if configured
1129
  // lets hope it exists but we'll leave that to the site admin
1130
  $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
1131
  $css_files = array();
1132
  switch ($conf['css_mode']) {
1133
    case 'theme':
1134
      global $language, $base_theme_info;
1135
      $themes = list_themes();
1136
      $theme_info = $themes[$current_theme];
1137
      if (!empty($theme_info->stylesheets)) {
1138
        $editorcss = "\"";
1139
        foreach ($base_theme_info as $base) { // Grab stylesheets from base theme
1140
          if (!empty($base->stylesheets)) { // may be empty when the base theme reference in the info file is invalid
1141
            foreach ($base->stylesheets as $type => $stylesheets) {
1142
              if ($type != "print") {
1143
                foreach ($stylesheets as $name => $path) {
1144
                  if (file_exists($path)) {
1145
                    $css_files[$name] = $host . $path . $query_string;
1146
                    // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
1147
                    if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
1148
                      $rtl_path = substr($path, 0, -4) . "-rtl.css";
1149
                      if (file_exists($rtl_path)) {
1150
                        $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
1151
                      }
1152
                    }
1153
                  }
1154
                }
1155
              }
1156
            }
1157
          }
1158
        }
1159
        if (!empty($theme_info->stylesheets)) { // Grab stylesheets from current theme
1160
          foreach ($theme_info->stylesheets as $type => $stylesheets) {
1161
            if ($type != "print") {
1162
              foreach ($stylesheets as $name => $path) {
1163
                // Checks if less module exists...
1164
                if (strstr($path, '.less') && module_exists('less')) {
1165
                  $path = 'sites/default/files/less/' . $path; // append the less file path
1166
                  $path = str_replace('.less', '', $path); // remove the .less
1167
                }
1168
                if (file_exists($path)) {
1169
                  $css_files[$name] = $host . $path . $query_string;
1170
                  // Grab rtl stylesheets ( will get rtl css files when thay are named with suffix "-rtl.css" (ex: fusion baased themes) )
1171
                  if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && substr($path, 0, -8) != "-rtl.css") {
1172
                    $rtl_path = substr($path, 0, -4) . "-rtl.css";
1173
                    if (file_exists($rtl_path)) {
1174
                      $css_files[$name . "-rtl"] = $host . $rtl_path . $query_string;
1175
                    }
1176
                  }
1177
                }
1178
                elseif (!empty($css_files[$name])) {
1179
                  unset($css_files[$name]);
1180
                }
1181
              }
1182
            }
1183
          }
1184
        }
1185
        // Grab stylesheets local.css and local-rtl.css if they exist (fusion based themes)
1186
        if (file_exists($themepath . 'css/local.css')) {
1187
          $css_files[] = $host . $themepath . 'css/local.css' . $query_string;
1188
        }
1189
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL && file_exists($themepath . 'css/local-rtl.css')) {
1190
          $css_files[] = $host . $themepath . 'css/local-rtl.css' . $query_string;
1191
        }
1192

    
1193
        // Grab stylesheets from color module
1194
        $color_paths = variable_get('color_' . $current_theme . '_stylesheets', array());
1195
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
1196
          if (!empty($color_paths[1])) {
1197
            $css_files[] = $host . $color_paths[1] . $query_string;
1198
          }
1199
        }
1200
        elseif (!empty($color_paths[0])) {
1201
          $css_files[] = $host . $color_paths[0] . $query_string;
1202
        }
1203
      }
1204
      else {
1205
        if (!file_exists($themepath . 'ckeditor.css') && file_exists($themepath . 'style.css')) {
1206
          $css_files[] = $host . $themepath . 'style.css' . $query_string;
1207
        }
1208
      }
1209
      if (file_exists($module_drupal_local_path . '/ckeditor.css')) {
1210
        $css_files[] = $module_drupal_path . '/ckeditor.css' . $query_string;
1211
      }
1212
      if (file_exists($themepath . 'ckeditor.css')) {
1213
        $css_files[] = $host . $themepath . 'ckeditor.css' . $query_string;
1214
      }
1215
      break;
1216

    
1217
    case 'self':
1218
      if (file_exists($module_drupal_local_path . '/ckeditor.css')) {
1219
        $css_files[] = $module_drupal_path . '/ckeditor.css' . $query_string;
1220
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
1221
          if (file_exists($module_drupal_local_path . '/ckeditor-rtl.css')) {
1222
            $css_files[] = $module_drupal_path . '/ckeditor-rtl.css' . $query_string;
1223
          }
1224
        }
1225
      }
1226
      foreach (explode(',', $conf['css_path']) as $css_path) {
1227
        $css_path = trim(str_replace("%h%t", "%t", $css_path));
1228
        $css_files[] = str_replace(array('%h', '%t'), array($host, $host . $themepath), $css_path) . $query_string;
1229
      }
1230
      break;
1231

    
1232
    case 'none':
1233
      if (file_exists($module_drupal_local_path . '/ckeditor.css')) {
1234
        $css_files[] = $module_drupal_path . '/ckeditor.css' . $query_string;
1235
        if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
1236
          if (file_exists($module_drupal_local_path . '/ckeditor-rtl.css')) {
1237
            $css_files[] = $module_drupal_path . '/ckeditor-rtl.css' . $query_string;
1238
          }
1239
        }
1240
      }
1241
      if (file_exists($editor_local_path . '/contents.css')) {
1242
        $css_files[] = $editor_path . '/contents.css' . $query_string;
1243
      }
1244
      break;
1245
  }
1246

    
1247
  if (isset($conf['ckeditor_load_method']) && $conf['ckeditor_load_method'] == 'ckeditor_source.js') {
1248
    foreach ($css_files as $k => $v) {
1249
      $css_files[$k] = $v . '&t=' . time();
1250
    }
1251
  }
1252

    
1253
  $settings['contentsCss'] = array_values($css_files);
1254

    
1255
  if (!empty($conf['uicolor']) && $conf['uicolor'] == "custom" && !empty($conf['uicolor_user'])) {
1256
    $settings['uiColor'] = $conf['uicolor_user'];
1257
  }
1258

    
1259
  if (!empty($conf['uicolor']) && strpos($conf['uicolor'], "color_") === 0) {
1260
    if (function_exists('color_get_palette')) {
1261
      $palette = @color_get_palette($current_theme, FALSE); //[#652274]
1262
      $color = str_replace("color_", "", $conf['uicolor']);
1263
      if (!empty($palette[$color])) {
1264
        $settings['uiColor'] = $palette[$color];
1265
      }
1266
    }
1267
  }
1268

    
1269
  return $settings;
1270
}
1271

    
1272
/**
1273
 * Load CKEditor for field ID
1274
 *
1275
 * @param object $field
1276
 * @param string $format
1277
 *
1278
 * @return object
1279
 *
1280
 */
1281
function ckeditor_load_by_field($field, $format, $show_toggle = TRUE, $add_fields_to_toggle = FALSE) {
1282
  global $theme;
1283
  static $processed_ids = array();
1284
  static $is_running = FALSE;
1285
  $use_ckeditor = FALSE;
1286
  $format_arr = FALSE;
1287
  $suffix = '';
1288

    
1289
  if (is_array($format)) {
1290
    $format_arr = $format;
1291
    $format = isset($format_arr['#value']) ? $format_arr['#value'] : $format_arr['#default_value'];
1292
  }
1293

    
1294
  if (!isset($field['#id'])) {
1295
    return $field;
1296
  }
1297

    
1298
  if (isset($processed_ids[$field['#id']])) {
1299
    return $field;
1300
  }
1301

    
1302
  if (key_exists('#wysiwyg', $field) && !$field['#wysiwyg']) {
1303
    return $field;
1304
  }
1305

    
1306
  if (isset($field['#access']) && !$field['#access']) {
1307
    return $field;
1308
  }
1309

    
1310
  if ($field['#id'] == "edit-log") {
1311
    return $field;
1312
  }
1313

    
1314
  if (isset($field['#attributes']['disabled']) && $field['#attributes']['disabled'] == 'disabled') {
1315
    return $field;
1316
  }
1317

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

    
1320
  if (!isset($processed_ids[$field['#id']])) {
1321
    $processed_ids[$field['#id']] = array();
1322
  }
1323

    
1324
  $global_profile = ckeditor_profile_load('CKEditor Global Profile');
1325
  $profile = ckeditor_get_profile($format);
1326
  $host = base_path();
1327

    
1328
  if ($profile === FALSE) {
1329
    $ckeditor_in_default_format = FALSE;
1330
    foreach ((array) $format_arr['#options'] as $key => $val) {
1331
      if ($key == $format)
1332
        continue;
1333
      if ($profile = ckeditor_get_profile($key)) {
1334
        $use_ckeditor = $key;
1335
        break;
1336
      }
1337
    }
1338
    if ($use_ckeditor === FALSE) {
1339
      return $field;
1340
    }
1341
  }
1342
  else {
1343
    $ckeditor_in_default_format = TRUE;
1344
  }
1345

    
1346
  $settings = FALSE;
1347
  if ($settings = ckeditor_profiles_compile($format)) {
1348
    $ckeditor_on = ($profile->settings['default'] == 't') ? TRUE : FALSE;
1349
  }
1350
  elseif ($settings = ckeditor_profiles_compile($use_ckeditor)) {
1351
    $ckeditor_on = FALSE;
1352
  }
1353
  else {
1354
    return $field;
1355
  }
1356

    
1357
  if ($settings) {
1358
    $textarea_id = $field['#id'];
1359
    $class[] = 'ckeditor-mod';
1360
    $_ckeditor_ids[] = $textarea_id;
1361

    
1362
    //settings are saved as strings, not booleans
1363
    if ($settings['show_toggle'] == 't' && $show_toggle) {
1364

    
1365
      if ($add_fields_to_toggle !== FALSE) {
1366
        if (is_array($add_fields_to_toggle)) {
1367
          $toggle_fields = "['" . $textarea_id . "','" . implode("','", $add_fields_to_toggle) . "']";
1368
        }
1369
        else {
1370
          $toggle_fields = "['" . $textarea_id . "','" . $add_fields_to_toggle . "']";
1371
        }
1372
      }
1373
      else {
1374
        $toggle_fields = "['{$textarea_id}']";
1375
      }
1376

    
1377
      $wysiwyg_link = '';
1378
      $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}\">";
1379
      $wysiwyg_link .= $ckeditor_on ? t('Switch to plain text editor') : t('Switch to rich text editor');
1380
      $wysiwyg_link .= '</a>';
1381

    
1382
      // Make sure to append to #suffix so it isn't completely overwritten
1383
      $suffix .= $wysiwyg_link;
1384
    }
1385

    
1386
    $editor_local_path = ckeditor_path('local');
1387
    $editor_url_path = ckeditor_path('url');
1388
    // get the default drupal files path
1389
    $files_path = $host . variable_get('file_private_path', conf_path() . '/files');
1390

    
1391
    if (!$is_running) {
1392
      if (!$ckeditor_in_default_format) {
1393
        $load_method = 'ckeditor_basic.js';
1394
        $load_time_out = 0;
1395
      }
1396
      elseif (isset($profile->settings['ckeditor_load_method'])) {
1397
        $load_method = $profile->settings['ckeditor_load_method'];
1398
        $load_time_out = $profile->settings['ckeditor_load_time_out'];
1399
      }
1400
      drupal_add_js('window.CKEDITOR_BASEPATH = "' . ckeditor_path('relative') . '/"', array('type' => 'inline', 'weight' => -100));
1401
      drupal_add_js(ckeditor_module_path('url') . '/includes/ckeditor.utils.js', array('type' => 'file', 'scope' => 'footer'));
1402

    
1403
      $preprocess = FALSE;
1404
      if (isset($global_profile->settings['ckeditor_aggregate']) && $global_profile->settings['ckeditor_aggregate'] == 't') {
1405
        $preprocess = TRUE;
1406
      }
1407

    
1408
      if (isset($load_method) && file_exists($editor_local_path . '/' . $load_method)) {
1409
        drupal_add_js($editor_url_path . '/' . $load_method, array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
1410
        if ($load_method == 'ckeditor_basic.js') {
1411
          drupal_add_js('CKEDITOR.loadFullCoreTimeout = ' . $load_time_out . ';', array('type' => 'inline', 'scope' => 'footer'));
1412
          drupal_add_js(array('ckeditor' => array('load_timeout' => TRUE)), 'setting');
1413
        }
1414
      }
1415
      else {
1416
        drupal_add_js($editor_url_path . '/ckeditor.js', array('type' => 'file', 'scope' => 'footer', 'preprocess' => $preprocess));
1417
      }
1418
      drupal_add_js(array('ckeditor' => array('module_path' => ckeditor_module_path('relative'), 'editor_path' => ckeditor_path('relative') . '/')), 'setting');
1419
      if (module_exists('paging')) {
1420
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1421
      }
1422
      if (module_exists('linktocontent_node')) {
1423
        drupal_add_js(array('ckeditor' => array('linktocontent_node' => TRUE)), 'setting');
1424
      }
1425
      if (module_exists('linktocontent_menu')) {
1426
        drupal_add_js(array('ckeditor' => array('linktocontent_menu' => TRUE)), 'setting');
1427
      }
1428
      if (module_exists('pagebreak')) {
1429
        drupal_add_js(array('ckeditor' => array('pagebreak' => TRUE)), 'setting');
1430
      }
1431
      drupal_add_js(array('ckeditor' => array('ajaxToken' => drupal_get_token('ckeditorAjaxCall'), 'xss_url' => url('ckeditor/xss'))), 'setting');
1432
      $is_running = TRUE;
1433
    }
1434

    
1435
    drupal_add_js(array('ckeditor' => array('theme' => $theme)), 'setting');
1436
    if (!empty($settings)) {
1437
      drupal_add_js(array('ckeditor' => array('elements' => array($textarea_id => $format))), 'setting');
1438
    }
1439
    if (!empty($ckeditor_on)) {
1440
      drupal_add_js(array('ckeditor' => array('autostart' => array($textarea_id => $ckeditor_on))), 'setting');
1441
    }
1442
    //[#1473010]
1443
    if (isset($settings['scayt_sLang'])) {
1444
      drupal_add_js(array('ckeditor' => array('scayt_language' => $settings['scayt_sLang'])), 'setting');
1445
    }
1446
    elseif (!empty($field["#language"]) && $field["#language"] != LANGUAGE_NONE) {
1447
      drupal_add_js(array('ckeditor' => array('scayt_language' => ckeditor_scayt_langcode($field["#language"]))), 'setting');
1448
    }
1449

    
1450
    // Remember extra information and reuse it during "Preview"
1451
    $processed_ids[$field['#id']]['suffix'] = $suffix;
1452
    $processed_ids[$field['#id']]['class'] = $class;
1453

    
1454
    if (empty($field['#suffix'])) {
1455
      $field['#suffix'] = $suffix;
1456
    }
1457
    else {
1458
      $field['#suffix'] .= $suffix;
1459
    }
1460

    
1461
    if (empty($field['#attributes']['class'])) {
1462
      $field['#attributes']['class'] = $class;
1463
    }
1464
    else {
1465
      $field['#attributes']['class'] = array_merge($field['#attributes']['class'], $class);
1466
    }
1467
  }
1468

    
1469
  return $field;
1470
}
1471

    
1472
/**
1473
 * Return all modules that provide security filters.
1474
 */
1475
function ckeditor_security_filters() {
1476
  $security_filters = array();
1477

    
1478
  $security_filters['modules'] = array(
1479
    'htmLawed' => array(
1480
      'title' => 'htmLawed',
1481
      'project_page' => 'http://drupal.org/project/htmLawed',
1482
      'weight' => 0,
1483
      'installed' => FALSE,
1484
      'filters' => array()
1485
    ),
1486
    'htmltidy' => array(
1487
      'title' => 'Htmltidy',
1488
      'project_page' => 'http://drupal.org/project/htmltidy',
1489
      'weight' => 0,
1490
      'installed' => FALSE,
1491
      'filters' => array()
1492
    ),
1493
    'htmlpurifier' => array(
1494
      'title' => 'HTML Purifier',
1495
      'project_page' => 'http://drupal.org/project/htmlpurifier',
1496
      'weight' => 0,
1497
      'installed' => FALSE,
1498
      'filters' => array()
1499
    ),
1500
    'wysiwyg_filter' => array(
1501
      'title' => 'WYSIWYG Filter',
1502
      'project_page' => 'http://drupal.org/project/wysiwyg_filter',
1503
      'weight' => 0,
1504
      'installed' => FALSE,
1505
      'filters' => array()
1506
    )
1507
  );
1508

    
1509
  $security_filters['filters'] = array();
1510

    
1511
  foreach ($security_filters['modules'] as $module_name => $module_conf) {
1512
    if (module_exists($module_name)) {
1513
      $security_filters['modules'][$module_name]['installed'] = TRUE;
1514
      $module_filters = module_invoke($module_name, 'filter_info');
1515
      foreach ($module_filters as $module_filter_name => $module_filter_conf) {
1516
        $security_filters['modules'][$module_name]['filters'][$module_filter_name] = $module_filter_conf;
1517
        $security_filters['filters'][$module_filter_name] = TRUE;
1518
      }
1519
    }
1520
  }
1521

    
1522
  //add filters from Drupal core
1523
  $security_filters['modules']['__drupal'] = array(
1524
    'title' => 'Drupal core',
1525
    'project_page' => FALSE,
1526
    'weight' => -1,
1527
    'installed' => TRUE,
1528
    'filters' => array(
1529
      'filter_html' => array(
1530
        'title' => 'Limit allowed HTML tags',
1531
        'description' => 'Removes the attributes that the built-in "Limit allowed HTML tags"-filter does not allow inside HTML elements/tags'
1532
      )
1533
    )
1534
  );
1535
  $security_filters['filters']['filter_html'] = TRUE;
1536

    
1537
  //load security filters added by API
1538
  $external_module_filters = module_invoke_all('ckeditor_security_filter');
1539
  if (count($external_module_filters) > 0) {
1540
    $security_filters['modules']['__external'] = array(
1541
      'title' => 'External filters',
1542
      'project_page' => FALSE,
1543
      'weight' => 1,
1544
      'installed' => TRUE,
1545
      'filters' => array()
1546
    );
1547
    foreach ($external_module_filters as $module_filter_name => $module_filter_conf) {
1548
      $security_filters['modules']['__external']['filters'][$module_filter_name] = $module_filter_conf;
1549
      $security_filters['filters'][$module_filter_name] = TRUE;
1550
    }
1551
  }
1552

    
1553
  return $security_filters;
1554
}