Projet

Général

Profil

Paste
Télécharger (25,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / ckeditor.module @ 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
 * The name of the simplified toolbar that should be forced.
38
 * Make sure that this toolbar is defined in ckeditor.config.js or fckconfig.js.
39
 */
40
define('CKEDITOR_FORCE_SIMPLE_TOOLBAR_NAME', 'DrupalBasic');
41
define('CKEDITOR_ENTERMODE_P', 1);
42
define('CKEDITOR_ENTERMODE_BR', 2);
43
define('CKEDITOR_ENTERMODE_DIV', 3);
44

    
45
global $_ckeditor_configuration;
46
global $_ckeditor_ids;
47

    
48
require_once('includes/ckeditor.user.inc');
49

    
50
$_ckeditor_configuration = array();
51
$_ckeditor_ids = array();
52

    
53
/**
54
 * Implementation of hook_menu().
55
 */
56
function ckeditor_menu() {
57
  $items = array();
58

    
59
  $items['ckeditor/xss'] = array(
60
    'title' => 'XSS Filter',
61
    'description' => 'XSS Filter.',
62
    'page callback' => 'ckeditor_filter_xss',
63
    'file' => 'includes/ckeditor.page.inc',
64
    'access callback' => TRUE,
65
    'type' => MENU_CALLBACK,
66
  );
67

    
68
  $items['ckeditor/disable/wysiwyg/%'] = array(
69
    'title' => 'Disable the WYSIWYG module',
70
    'description' => 'Disable WYSIWYG module.',
71
    'page callback' => 'ckeditor_disable_wysiwyg',
72
    'page arguments' => array(3),
73
    'file' => 'includes/ckeditor.admin.inc',
74
    'access arguments' => array('administer ckeditor'),
75
    'access callback' => TRUE,
76
    'type' => MENU_CALLBACK,
77
  );
78

    
79
  $items['admin/config/content/ckeditor'] = array(
80
    'title' => 'CKEditor',
81
    'description' => 'Configure the rich text editor.',
82
    'page callback' => 'ckeditor_admin_main',
83
    'file' => 'includes/ckeditor.admin.inc',
84
    'access arguments' => array('administer ckeditor'),
85
    'type' => MENU_NORMAL_ITEM,
86
  );
87

    
88
  $items['admin/config/content/ckeditor/skinframe'] = array(
89
    'title' => 'Change skin of CKEditor',
90
    'description' => 'Configure skin for CKEditor.',
91
    'page callback' => 'ckeditor_skinframe',
92
    'file' => 'includes/ckeditor.admin.inc',
93
    'access arguments' => array('administer ckeditor'),
94
    'type' => MENU_CALLBACK,
95
  );
96

    
97
  $items['admin/config/content/ckeditor/add'] = array(
98
    'title' => 'Add a new CKEditor profile',
99
    'description' => 'Configure the rich text editor.',
100
    'page callback' => 'drupal_get_form',
101
    'page arguments' => array('ckeditor_admin_profile_form'),
102
    'file' => 'includes/ckeditor.admin.inc',
103
    'access arguments' => array('administer ckeditor'),
104
    'type' => MENU_CALLBACK,
105
  );
106

    
107
  $items['admin/config/content/ckeditor/clone/%ckeditor_profile'] = array(
108
    'title' => 'Clone the CKEditor profile',
109
    'description' => 'Configure the rich text editor.',
110
    'page callback' => 'drupal_get_form',
111
    'page arguments' => array('ckeditor_admin_profile_clone_form', 5),
112
    'file' => 'includes/ckeditor.admin.inc',
113
    'access arguments' => array('administer ckeditor'),
114
    'type' => MENU_CALLBACK,
115
  );
116

    
117
  $items['admin/config/content/ckeditor/edit/%ckeditor_profile'] = array(
118
    'title' => 'Edit the CKEditor profile',
119
    'description' => 'Configure the rich text editor.',
120
    'page callback' => 'drupal_get_form',
121
    'page arguments' => array('ckeditor_admin_profile_form', 5),
122
    'file' => 'includes/ckeditor.admin.inc',
123
    'access arguments' => array('administer ckeditor'),
124
    'type' => MENU_CALLBACK,
125
  );
126

    
127
  $items['admin/config/content/ckeditor/delete/%ckeditor_profile'] = array(
128
    'title' => 'Delete the CKEditor profile',
129
    'description' => 'Configure the rich text editor.',
130
    'page callback' => 'drupal_get_form',
131
    'page arguments' => array('ckeditor_admin_profile_delete_form', 5),
132
    'file' => 'includes/ckeditor.admin.inc',
133
    'access arguments' => array('administer ckeditor'),
134
    'type' => MENU_CALLBACK,
135
  );
136

    
137
  $items['admin/config/content/ckeditor/addg'] = array(
138
    'title' => 'Add the CKEditor Global profile',
139
    'description' => 'Configure the rich text editor.',
140
    'page callback' => 'drupal_get_form',
141
    'page arguments' => array('ckeditor_admin_global_profile_form', 'add'),
142
    'file' => 'includes/ckeditor.admin.inc',
143
    'access arguments' => array('administer ckeditor'),
144
    'type' => MENU_CALLBACK,
145
  );
146

    
147
  $items['admin/config/content/ckeditor/editg'] = array(
148
    'title' => 'Edit the CKEditor Global profile',
149
    'description' => 'Configure the rich text editor.',
150
    'page callback' => 'drupal_get_form',
151
    'page arguments' => array('ckeditor_admin_global_profile_form', 'edit'),
152
    'file' => 'includes/ckeditor.admin.inc',
153
    'access arguments' => array('administer ckeditor'),
154
    'type' => MENU_CALLBACK,
155
  );
156

    
157
  return $items;
158
}
159

    
160
/**
161
 * Implementation of hook_permission().
162
 *
163
 * People -> Permissions
164
 */
165
function ckeditor_permission() {
166
  $arr = array();
167
  $arr['administer ckeditor'] = array(
168
    'title' => t('Administer CKEditor access'),
169
    'description' => t('Allow users to change CKEditor settings.')
170
  );
171

    
172
  $arr['customize ckeditor'] = array(
173
    'title' => t('Customize CKEditor appearance'),
174
    'description' => t('Allow users to customize CKEditor appearance.')
175
  );
176

    
177
  if (file_exists(ckfinder_path('local'))) {
178
    $arr['allow CKFinder file uploads'] = array(
179
      'title' => t('CKFinder access'),
180
      'description' => t('Allow users to use CKFinder.')
181
    );
182
  }
183
  return $arr;
184
}
185

    
186
/**
187
 * Implementation of hook_help().
188
 *
189
 * This function delegates the execution to ckeditor_help_delegate() in includes/ckeditor.page.inc to
190
 * lower the amount of code in ckeditor.module.
191
 */
192
function ckeditor_help($path, $arg) {
193
  module_load_include('inc', 'ckeditor', 'includes/ckeditor.page');
194
  return module_invoke('ckeditor', 'help_delegate', $path, $arg);
195
}
196

    
197
/**
198
 * Check CKEditor version
199
 */
200
function ckeditor_get_version($main_version = FALSE) {
201
  static $ckeditor_version = FALSE;
202

    
203
  if ($ckeditor_version !== FALSE) {
204
    if (!$main_version) {
205
      return $ckeditor_version;
206
    }
207
    $version = explode('.', $ckeditor_version);
208
    return trim($version[0]);
209
  }
210

    
211
  $editor_path = ckeditor_path('local', TRUE);
212
  $jspath = $editor_path . '/ckeditor.js';
213

    
214
  $configcontents = @file_get_contents($jspath);
215
  if (!$configcontents) {
216
    return $ckeditor_version = NULL;
217
  }
218
  $matches = array();
219
  if (preg_match('#,version:[\'\"]{1}(.*?)[\'\"]{1},#', $configcontents, $matches)) {
220
    $ckeditor_version = $matches[1];
221
    if ($ckeditor_version == '%VERSION%') {
222
      $ckeditor_version = '4.0.0';
223
    }
224
    if (!$main_version) {
225
      return $ckeditor_version;
226
    }
227
    $version = explode('.', $ckeditor_version);
228
    return trim($version[0]);
229
  }
230
  return $ckeditor_version = NULL;
231
}
232

    
233
/**
234
 * Implementation of hook_init().
235
 */
236
function ckeditor_init() {
237
  drupal_add_css(drupal_get_path('module', 'ckeditor') . '/ckeditor.css');
238
}
239

    
240
/**
241
 * Implements hook_form_FORM_ID_alter() for user_profile_form().
242
 */
243
function ckeditor_form_user_profile_form_alter(&$form, &$form_state) {
244
  if ($form['#user_category'] == 'account') {
245
    module_load_include('inc', 'ckeditor', 'includes/ckeditor.user');
246
    ckeditor_user_customize($form, $form_state, 'user_profile_form');
247
  }
248
}
249

    
250
/**
251
 * Implementation of hook_element_info_alter().
252
 *
253
 * Replace the textarea with CKEditor using a callback function (ckeditor_pre_render_text_format).
254
 */
255
function ckeditor_element_info_alter(&$types) {
256
  $types['text_format']['#pre_render'][] = 'ckeditor_pre_render_text_format';
257
}
258

    
259
/**
260
 * This function creates the HTML objects required for CKEditor.
261
 *
262
 * @param $element
263
 *   A fully populated form element to add the editor to.
264
 * @return
265
 *   The same $element with extra CKEditor markup and initialization.
266
 */
267
function ckeditor_pre_render_text_format($element) {
268
  static $init = FALSE;
269
  if (!isset($element['#format'])) {
270
    return $element;
271
  }
272

    
273
  module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
274
  if ($init === FALSE) {
275
    $input_formats = ckeditor_profiles_compile();
276
    drupal_add_js(array('ckeditor' => array('input_formats' => $input_formats, 'plugins' => array())), 'setting');
277
    $init = TRUE;
278
  }
279

    
280
  if (isset($element['value'])) {
281
    if (!isset($element['format'])) {
282
      return $element;
283
    }
284
    if (isset($element['summary'])) {
285
      $element['value'] = ckeditor_load_by_field($element['value'], $element['format']['format'], TRUE, $element['summary']['#id']);
286
      $element['summary'] = ckeditor_load_by_field($element['summary'], $element['format']['format'], FALSE);
287
    }
288
    else {
289
      $element['value'] = ckeditor_load_by_field($element['value'], $element['format']['format']);
290
    }
291
  }
292
  else {
293
    $element = ckeditor_load_by_field($element, $element['#format']);
294
  }
295

    
296
  return $element;
297
}
298

    
299
/**
300
 * Load all profiles. Just load one profile if $name is passed in.
301
 */
302
function ckeditor_profile_load($name = '', $clear = FALSE, $check_access = TRUE) {
303
  static $profiles = array();
304
  global $user;
305

    
306
  if (empty($profiles) || $clear === TRUE) {
307
    $result = db_select('ckeditor_settings', 's')->fields('s')->execute();
308
    foreach ($result as $data) {
309
      $data->settings = unserialize($data->settings);
310
      $data->input_formats = array();
311

    
312
      $profiles[$data->name] = $data;
313
    }
314
    if ($check_access === FALSE) {
315
      // don't check if user has access to filter formats, needed for exporting as feature with drush
316
      $input_formats = filter_formats();
317
    } else {
318
      $input_formats = filter_formats($user);
319
    }
320
    $result = db_select('ckeditor_input_format', 'f')->fields('f')->execute();
321
    foreach ($result as $data) {
322
      if (isset($input_formats[$data->format])) {
323
        $profiles[$data->name]->input_formats[$data->format] = $input_formats[$data->format]->name;
324
      }
325
    }
326
  }
327

    
328
  return ($name ? (isset($profiles[urldecode($name)]) ? $profiles[urldecode($name)] : FALSE) : $profiles);
329
}
330

    
331
/**
332
 * Generate base path of the Drupal installation.
333
 *
334
 * @return
335
 *   Path of the Drupal installation.
336
 */
337
function ckeditor_base_path($mode = 'relative') {
338
  if ($mode == 'local') {
339
    return $cke_base_local_path = '.';
340
  }
341
  return rtrim(base_path(), '/');
342
}
343

    
344
/**
345
 * Generate module path of the CKEditor module.
346
 *
347
 * @return
348
 *   Path of CKEditor module.
349
 */
350
function ckeditor_module_path($mode = 'relative') {
351
  switch ($mode) {
352
    default:
353
    case 'relative':
354
      return ckeditor_base_path('relative') . '/' . drupal_get_path('module', 'ckeditor');
355
    case 'local':
356
      return ckeditor_base_path('local') . '/' . drupal_get_path('module', 'ckeditor');
357
    case 'url':
358
      return drupal_get_path('module', 'ckeditor');
359
  }
360
}
361

    
362
/**
363
 * Generate library path of the Drupal installation.
364
 *
365
 * @return
366
 *   Path of library in the Drupal installation.
367
 */
368
function ckeditor_library_path($mode = 'relative') {
369
  $lib_path = 'sites/all/libraries';
370

    
371
  if (function_exists('libraries_get_path')) {
372
    $path = libraries_get_path('ckeditor');
373
    if ($path !== FALSE) {
374
      $lib_path = drupal_substr($path, 0, strlen($path) - 9);
375
    }
376
  }
377
  switch ($mode) {
378
    default:
379
    case 'relative':
380
      return ckeditor_base_path('relative') . '/' . $lib_path;
381
    case 'local':
382
      return ckeditor_base_path('local') . '/' . $lib_path;
383
    case 'url':
384
      return $lib_path;
385
  }
386
}
387

    
388
/**
389
 * Read the CKEditor path from the Global profile.
390
 *
391
 * @return
392
 *   Path to CKEditor folder.
393
 */
394
function ckeditor_path($mode = 'relative', $refresh = FALSE) {
395
  static $cke_static;
396

    
397
  if (!isset($cke_static)) {
398
    $cke_static = array();
399
  }
400

    
401
  if ($refresh || !isset($cke_static[$mode])) {
402
    $global_profile = ckeditor_profile_load('CKEditor Global Profile', $refresh);
403
    switch ($mode) {
404
      default:
405
      case 'relative':
406
        if ($global_profile && isset($global_profile->settings['ckeditor_path'])) {
407
          $cke_path = $global_profile->settings['ckeditor_path'];
408
          $cke_path = strtr($cke_path, array("%b" => ckeditor_base_path('relative'), "%m" => ckeditor_module_path('relative'), "%l" => ckeditor_library_path('relative')));
409
          $cke_path = str_replace('\\', '/', $cke_path);
410
          $cke_path = str_replace('//', '/', $cke_path);
411
          return $cke_static[$mode] = $cke_path;
412
        }
413
        return $cke_static[$mode] = ckeditor_module_path('relative') . '/ckeditor';
414
      case 'local':
415
        if ($global_profile) {
416
          if (!empty($global_profile->settings['ckeditor_local_path'])) {
417
            return $cke_static[$mode] = $global_profile->settings['ckeditor_local_path'];
418
          }
419
          if (isset($global_profile->settings['ckeditor_path'])) {
420
            $cke_local_path = $global_profile->settings['ckeditor_path'];
421
            $cke_local_path = strtr($cke_local_path, array("%b" => ckeditor_base_path('local'), "%m" => ckeditor_module_path('local'), "%l" => ckeditor_library_path('local')));
422
            return $cke_static[$mode] = $cke_local_path;
423
          }
424
        }
425
        return $cke_static[$mode] = ckeditor_module_path('local') . '/ckeditor';
426
      case 'url':
427
        if ($global_profile && isset($global_profile->settings['ckeditor_path'])) {
428
          $cke_path = $global_profile->settings['ckeditor_path'];
429
          $cke_path = strtr($cke_path, array("%m" => ckeditor_module_path('url'), "%l" => ckeditor_library_path('url')));
430
          $cke_path = str_replace('\\', '/', $cke_path);
431
          $cke_path = str_replace('//', '/', $cke_path);
432
          //In D7 base path in URL mode is not needed, so we need to remove it with trailing slash (if exists)
433
          $cke_path = str_replace(array("%b/", "%b"), '', $cke_path);
434
          return $cke_static[$mode] = $cke_path;
435
        }
436
        return $cke_static[$mode] = ckeditor_module_path('url') . '/ckeditor';
437
    }
438
  }
439
  return $cke_static[$mode];
440
}
441

    
442
/**
443
 * Read the CKEditor plugins path from the Global profile.
444
 *
445
 * @return
446
 *   Path to CKEditor plugins folder.
447
 */
448
function ckeditor_plugins_path($mode = 'relative', $refresh = FALSE) {
449
  static $cke_static;
450

    
451
  if (!isset($cke_static)) {
452
    $cke_static = array();
453
  }
454

    
455
  if ($refresh || !isset($cke_static[$mode])) {
456
    $global_profile = ckeditor_profile_load('CKEditor Global Profile', $refresh);
457
    switch ($mode) {
458
      default:
459
      case 'relative':
460
        if ($global_profile && isset($global_profile->settings['ckeditor_plugins_path'])) {
461
          $cke_plugins_path = $global_profile->settings['ckeditor_plugins_path'];
462
          $cke_plugins_path = strtr($cke_plugins_path, array("%b" => ckeditor_base_path('relative'), "%m" => ckeditor_module_path('relative'), "%l" => ckeditor_library_path('relative')));
463
          $cke_plugins_path = str_replace('\\', '/', $cke_plugins_path);
464
          $cke_plugins_path = str_replace('//', '/', $cke_plugins_path);
465
          $cke_plugins_path = rtrim($cke_plugins_path, ' \/');
466
          return $cke_static[$mode] = $cke_plugins_path;
467
        }
468
        return $cke_static[$mode] = ckeditor_module_path('relative') . '/plugins';
469
      case 'local':
470
        if ($global_profile) {
471
          if (!empty($global_profile->settings['ckeditor_plugins_local_path'])) {
472
            return $cke_static[$mode] = $global_profile->settings['ckeditor_plugins_local_path'];
473
          }
474
          if (isset($global_profile->settings['ckeditor_plugins_path'])) {
475
            $cke_plugins_local_path = $global_profile->settings['ckeditor_plugins_path'];
476
            $cke_plugins_local_path = strtr($cke_plugins_local_path, array("%b" => ckeditor_base_path('local'), "%m" => ckeditor_module_path('local'), "%l" => ckeditor_library_path('local')));
477
            return $cke_static[$mode] = $cke_plugins_local_path;
478
          }
479
        }
480
        return $cke_static[$mode] = ckeditor_module_path('local') . '/plugins';
481
      case 'url':
482
        if ($global_profile && isset($global_profile->settings['ckeditor_plugins_path'])) {
483
          $cke_plugins_path = $global_profile->settings['ckeditor_plugins_path'];
484
          $cke_plugins_path = strtr($cke_plugins_path, array("%m" => ckeditor_module_path('url'), "%l" => ckeditor_library_path('url')));
485
          $cke_plugins_path = str_replace('\\', '/', $cke_plugins_path);
486
          $cke_plugins_path = str_replace('//', '/', $cke_plugins_path);
487
          $cke_plugins_path = rtrim($cke_plugins_path, ' \/');
488
          //In D7 base path in URL mode is not needed, so we need to remove it with trailing slash (if exists)
489
          $cke_plugins_path = str_replace(array("%b/", "%b"), '', $cke_plugins_path);
490
          return $cke_static[$mode] = $cke_plugins_path;
491
        }
492
        return $cke_static[$mode] = ckeditor_module_path('url') . '/plugins';
493
    }
494
  }
495
  return $cke_static[$mode];
496
}
497

    
498
/**
499
 * Read the CKFinder path from the Global profile.
500
 *
501
 * @return
502
 *   Path to CKFinder folder.
503
 */
504
function ckfinder_path($mode = 'relative', $refresh = FALSE) {
505
 static $cke_static;
506

    
507
  if (!isset($cke_static)) {
508
    $cke_static = array();
509
  }
510

    
511
  if ($refresh || !isset($cke_static[$mode])) {
512
    $global_profile = ckeditor_profile_load('CKEditor Global Profile', $refresh);
513
    switch ($mode) {
514
      default:
515
      case 'relative':
516
        if ($global_profile && isset($global_profile->settings['ckfinder_path'])) {
517
          $ckfinder_path = $global_profile->settings['ckfinder_path'];
518
          $ckfinder_path = strtr($ckfinder_path, array("%b" => ckeditor_base_path('relative'), "%m" => ckeditor_module_path('relative'), "%l" => ckeditor_library_path('relative')));
519
          $ckfinder_path = str_replace('\\', '/', $ckfinder_path);
520
          $ckfinder_path = str_replace('//', '/', $ckfinder_path);
521
          return $cke_static[$mode] = $ckfinder_path;
522
        }
523
        return $cke_static[$mode] = ckeditor_module_path('relative') . '/ckfinder';
524
      case 'local':
525
        if ($global_profile) {
526
          if (!empty($global_profile->settings['ckfinder_local_path'])) {
527
            return $cke_static[$mode] = $global_profile->settings['ckfinder_local_path'];
528
          }
529
          if (isset($global_profile->settings['ckfinder_path'])) {
530
            $ckfinder_path = $global_profile->settings['ckfinder_path'];
531
            $ckfinder_path = strtr($ckfinder_path, array("%b" => ckeditor_base_path('local'), "%m" => ckeditor_module_path('local'), "%l" => ckeditor_library_path('local')));
532
            return $cke_static[$mode] = $ckfinder_path;
533
          }
534
        }
535
        return $cke_static[$mode] = ckeditor_module_path('local') . '/ckfinder';
536
      case 'url':
537
        if ($global_profile && isset($global_profile->settings['ckfinder_path'])) {
538
          $ckfinder_path = $global_profile->settings['ckfinder_path'];
539
          $ckfinder_path = strtr($ckfinder_path, array("%m" => ckeditor_module_path('url'), "%l" => ckeditor_library_path('url')));
540
          $ckfinder_path = str_replace('\\', '/', $cke_plugins_path);
541
          $ckfinder_path = str_replace('//', '/', $cke_plugins_path);
542
          //In D7 base path in URL mode is not needed, so we need to remove it with trailing slash (if exists)
543
          $ckfinder_path = str_replace(array("%b/", "%b"), '', $ckfinder_path);
544
          return $cke_static[$mode] = $ckfinder_path;
545
        }
546
        return $cke_static[$mode] = ckeditor_module_path('url') . '/ckfinder';
547
    }
548
  }
549
  return $cke_static[$mode];
550
}
551

    
552
/**
553
 * Implementation of hook_features_api().
554
 *
555
 * Allow exporting of CKEditor profiles by the Features module.
556
 */
557
function ckeditor_features_api() {
558
  return array(
559
    'ckeditor_profile' => array(
560
      'name' => t('CKEditor profiles'),
561
      'feature_source' => TRUE,
562
      'default_hook' => 'ckeditor_profile_defaults',
563
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
564
      'file' => drupal_get_path('module', 'ckeditor') . '/includes/ckeditor.features.inc',
565
    )
566
  );
567
}
568

    
569
/**
570
 * Implementation of hook_file_download().
571
 * Support for private downloads.
572
 * CKEditor does not implement any kind of potection on private files.
573
 */
574
function ckeditor_file_download($uri) {
575
  if ($path = file_create_url($uri)) {
576
    $result = db_query("SELECT f.* FROM {file_managed} f WHERE uri = :uri", array(':uri' => $uri));
577
    foreach ($result as $record) {
578
      return NULL;
579
    }
580
    //No info in DB? Probably a file uploaded with FCKeditor / CKFinder
581
    $global_profile = ckeditor_profile_load("CKEditor Global Profile");
582
    //Assume that files inside of ckeditor directory belong to the CKEditor. If private directory is set, let the decision about protection to the user.
583
    $private_dir_db = $private_dir = isset($global_profile->settings['private_dir']) ? trim($global_profile->settings['private_dir'], '\/') : '';
584
    $private_dir_db = str_replace(array('\\%u', '\\%n'), array('', ''), $private_dir_db);
585
    $private_dir = preg_quote($private_dir, '#');
586
    $private_dir = strtr($private_dir, array('%u' => '(\d+)', '%n' => '([\x80-\xF7 \w@.-]+)')); // regex for %n taken from user_validate_name() in user.module
587
    $private_dir = trim($private_dir, '\/');
588

    
589
    $regex = '#^' . preg_quote('private://', '#') . $private_dir . '#';
590

    
591
    if (!strstr($uri, 'private://') && !strstr($uri, 'public://')) {
592
      $path = 'private://' . $uri;
593
    }
594
    else {
595
      $path = $uri;
596
    }
597
    //check if CKEditor's "Enable access to files located in the private folder" option is disabled or enabled
598
    $allow_download_private_files = FALSE;
599
    if (isset($global_profile->settings['ckeditor_allow_download_private_files']) && $global_profile->settings['ckeditor_allow_download_private_files'] === 't') {
600
      $allow_download_private_files = TRUE;
601
    }
602
    //denied access to file if private upload is set and CKEditor's "Enable access to files located in the private folder" option is disabled
603
    if ($allow_download_private_files == FALSE)
604
      return NULL;
605
    //check if file can be served by comparing regex and path to file
606
    if (preg_match($regex, $path)) {
607
      $info = image_get_info($uri);
608
      return array('Content-Type' => $info['mime_type']);
609
    }
610
  }
611
}
612

    
613
/**
614
 * Implementation of hook_modules_enabled().
615
 *
616
 * Enable disabled plugins by hook_modules_disabled().
617
 */
618
function ckeditor_modules_enabled($modules) {
619
  module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
620
  if (in_array('libraries', $modules)) {
621
    // Check and update path to CKEditor in the global profile
622
    _ckeditor_requirements_isinstalled();
623
  }
624
  $profiles_list = ckeditor_profile_input_formats();
625
  $plugins_list = ckeditor_load_plugins();
626
  foreach ($profiles_list AS $_profile => $_inputs) {
627
    $changed = FALSE;
628
    $profile = ckeditor_profile_load($_profile);
629
    if (!isset($profile->settings['loadPlugins'])) continue;
630
    foreach (array_keys((array) $profile->settings['loadPlugins']) as $plugin_name) {
631
      if (isset($profile->settings['loadPlugins'][$plugin_name]['active']) && $profile->settings['loadPlugins'][$plugin_name]['active'] == 0 && array_key_exists($plugin_name, $plugins_list)) {
632
        $profile->settings['loadPlugins'][$plugin_name]['active'] = 1;
633
        $changed = TRUE;
634
      }
635
    }
636
    if ($changed === TRUE) {
637
      db_update('ckeditor_settings')
638
          ->fields(array(
639
            'settings' => serialize($profile->settings)
640
          ))
641
          ->condition('name', $profile->name, '=')
642
          ->execute();
643
    }
644
  }
645
}
646

    
647
/**
648
 * Implementation of hook_modules_disabled().
649
 *
650
 * Disable enabled plugins in CKEditor profiles added by disabled modules.
651
 */
652
function ckeditor_modules_disabled($modules) {
653
  module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
654
  $profiles_list = ckeditor_profile_input_formats();
655
  $plugins_list = ckeditor_load_plugins();
656
  foreach ($profiles_list AS $_profile => $_inputs) {
657
    $changed = FALSE;
658
    $profile = ckeditor_profile_load($_profile);
659
    if (!isset($profile->settings['loadPlugins'])) continue;
660
    foreach (array_keys((array) $profile->settings['loadPlugins']) as $plugin_name) {
661
      if (!array_key_exists($plugin_name, $plugins_list)) {
662
        $profile->settings['loadPlugins'][$plugin_name]['active'] = 0;
663
        $changed = TRUE;
664
      }
665
    }
666
    if ($changed === TRUE) {
667
      db_update('ckeditor_settings')
668
          ->fields(array(
669
            'settings' => serialize($profile->settings)
670
          ))
671
          ->condition('name', $profile->name, '=')
672
          ->execute();
673
    }
674
  }
675
}
676

    
677
/**
678
 * Implementation of hook_modules_uninstalled().
679
 *
680
 * Remove enabled plugins in CKEditor profiles added by uninstalled modules.
681
 */
682
function ckeditor_modules_uninstalled($modules) {
683
  module_load_include('inc', 'ckeditor', 'includes/ckeditor.lib');
684
  $profiles_list = ckeditor_profile_input_formats();
685
  $plugins_list = ckeditor_load_plugins();
686
  foreach ($profiles_list AS $_profile => $_inputs) {
687
    $changed = FALSE;
688
    $profile = ckeditor_profile_load($_profile);
689
    if (!isset($profile->settings['loadPlugins'])) continue;
690
    foreach (array_keys((array) $profile->settings['loadPlugins']) as $plugin_name) {
691
      if (!array_key_exists($plugin_name, $plugins_list)) {
692
        unset($profile->settings['loadPlugins'][$plugin_name]);
693
        $changed = TRUE;
694
      }
695
    }
696
    if ($changed === TRUE) {
697
      db_update('ckeditor_settings')
698
          ->fields(array(
699
            'settings' => serialize($profile->settings)
700
          ))
701
          ->condition('name', $profile->name, '=')
702
          ->execute();
703
    }
704
  }
705
}
706

    
707

    
708
/**
709
 * Implements hook_field_extra_fields().
710
 */
711
function ckeditor_field_extra_fields() {
712
  $fields['user']['user']['form']['ckeditor'] = array(
713
    'label' => t('Rich text editor settings'),
714
    'description' => t('Rich text editor settings'),
715
    'weight' => 10,
716
  );
717
  return $fields;
718
}