summaryrefslogtreecommitdiff
path: root/sites/all/modules/wysiwyg/wysiwyg.module
blob: 22130eab9485ff876f029519a7e2f929aecf4b73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
<?php

/**
 * @file
 * Integrates client-side editors with Drupal.
 */

/**
 * Implements hook_entity_info().
 */
function wysiwyg_entity_info() {
  $types['wysiwyg_profile'] = array(
    'label' => t('Wysiwyg profile'),
    'base table' => 'wysiwyg',
    'controller class' => 'WysiwygProfileController',
    'fieldable' => FALSE,
    // When loading all entities, DrupalDefaultEntityController::load() ignores
    // its static cache. Therefore, wysiwyg_profile_load_all() implements a
    // custom static cache.
    'static cache' => FALSE,
    'entity keys' => array(
      'id' => 'format',
    ),
  );
  return $types;
}

/**
 * Controller class for Wysiwyg profiles.
 */
class WysiwygProfileController extends DrupalDefaultEntityController {
  /**
   * Overrides DrupalDefaultEntityController::attachLoad().
   */
  function attachLoad(&$queried_entities, $revision_id = FALSE) {
    // Unserialize the profile settings.
    foreach ($queried_entities as $key => $record) {
      $queried_entities[$key]->settings = unserialize($record->settings);
    }
    // Call the default attachLoad() method.
    parent::attachLoad($queried_entities, $revision_id);
  }
}

/**
 * Implementation of hook_menu().
 */
function wysiwyg_menu() {
  $items['admin/config/content/wysiwyg'] = array(
    'title' => 'Wysiwyg profiles',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('wysiwyg_profile_overview'),
    'description' => 'Configure client-side editors.',
    'access arguments' => array('administer filters'),
    'file' => 'wysiwyg.admin.inc',
  );
  $items['admin/config/content/wysiwyg/profile'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/config/content/wysiwyg/profile/%wysiwyg_profile/edit'] = array(
    'title' => 'Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('wysiwyg_profile_form', 5),
    'access arguments' => array('administer filters'),
    'file' => 'wysiwyg.admin.inc',
    'tab_root' => 'admin/config/content/wysiwyg/profile',
    'tab_parent' => 'admin/config/content/wysiwyg/profile/%wysiwyg_profile',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/content/wysiwyg/profile/%wysiwyg_profile/delete'] = array(
    'title' => 'Remove',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('wysiwyg_profile_delete_confirm', 5),
    'access arguments' => array('administer filters'),
    'file' => 'wysiwyg.admin.inc',
    'tab_root' => 'admin/config/content/wysiwyg/profile',
    'tab_parent' => 'admin/config/content/wysiwyg/profile/%wysiwyg_profile',
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
  );
  // @see wysiwyg_dialog()
  $items['wysiwyg/%'] = array(
    'page callback' => 'wysiwyg_dialog',
    'page arguments' => array(1),
    'delivery callback' => 'wysiwyg_deliver_dialog_page',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'wysiwyg.dialog.inc',
  );
  return $items;
}

/**
 * Implements hook_element_info().
 */
function wysiwyg_element_info() {
  // @see wysiwyg_dialog()
  $types['wysiwyg_dialog_page'] = array(
    '#theme' => 'wysiwyg_dialog_page',
    '#theme_wrappers' => array('html'),
    '#show_messages' => TRUE,
  );
  return $types;
}

/**
 * Implementation of hook_theme().
 *
 * @see drupal_common_theme(), common.inc
 * @see template_preprocess_page(), theme.inc
 */
function wysiwyg_theme() {
  return array(
    'wysiwyg_profile_overview' => array(
      'render element' => 'form',
    ),
    'wysiwyg_admin_button_table' => array(
      'render element' => 'form',
    ),
    // @see wysiwyg_dialog()
    'wysiwyg_dialog_page' => array(
      'render element' => 'page',
      'file' => 'wysiwyg.dialog.inc',
      'template' => 'wysiwyg-dialog-page',
    ),
  );
}

/**
 * Implementation of hook_help().
 */
function wysiwyg_help($path, $arg) {
  switch ($path) {
    case 'admin/config/content/wysiwyg':
      $output = '<p>' . t('A Wysiwyg profile is associated with a text format. A Wysiwyg profile defines which client-side editor is loaded with a particular text format, what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') . '</p>';
      return $output;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function wysiwyg_form_alter(&$form, &$form_state) {
  // Teaser splitter is unconditionally removed and NOT supported.
  if (isset($form['body_field'])) {
    unset($form['body_field']['teaser_js']);
  }
}

/**
 * Implements hook_element_info_alter().
 */
function wysiwyg_element_info_alter(&$types) {
  $types['text_format']['#pre_render'][] = 'wysiwyg_pre_render_text_format';
}

/**
 * Process a text format widget to load and attach editors.
 *
 * The element's #id is used as reference to attach client-side editors.
 */
function wysiwyg_pre_render_text_format($element) {
  // filter_process_format() copies properties to the expanded 'value' child
  // element. Skip this text format widget, if it contains no 'format' or when
  // the current user does not have access to edit the value.
  if (!isset($element['format']) || !empty($element['value']['#disabled'])) {
    return $element;
  }
  // Allow modules to programmatically enforce no client-side editor by setting
  // the #wysiwyg property to FALSE.
  if (isset($element['#wysiwyg']) && !$element['#wysiwyg']) {
    return $element;
  }

  $format_field = &$element['format'];
  $field = &$element['value'];
  $settings = array(
    'field' => $field['#id'],
  );

  // If this textarea is #resizable and we will load at least one
  // editor, then only load the behavior and let the 'none' editor
  // attach/detach it to avoid hi-jacking the UI. Due to our CSS class
  // parsing, we can add arbitrary parameters for each input format.
  // The #resizable property will be removed below, if at least one
  // profile has been loaded.
  $resizable = 0;
  if (!empty($field['#resizable'])) {
    $resizable = 1;
    drupal_add_js('misc/textarea.js');
  }
  // Determine the available text formats.
  foreach ($format_field['format']['#options'] as $format_id => $format_name) {
    $format = 'format' . $format_id;
    // Initialize default settings, defaulting to 'none' editor.
    $settings[$format] = array(
      'editor' => 'none',
      'status' => 1,
      'toggle' => 1,
      'resizable' => $resizable,
    );

    // Fetch the profile associated to this text format.
    $profile = wysiwyg_get_profile($format_id);
    if ($profile) {
      $loaded = TRUE;
      $settings[$format]['editor'] = $profile->editor;
      $settings[$format]['status'] = (int) wysiwyg_user_get_status($profile);
      if (isset($profile->settings['show_toggle'])) {
        $settings[$format]['toggle'] = (int) $profile->settings['show_toggle'];
      }
      // Check editor theme (and reset it if not/no longer available).
      $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings['theme']) ? $profile->settings['theme'] : ''));

      // Add plugin settings (first) for this text format.
      wysiwyg_add_plugin_settings($profile);
      // Add profile settings for this text format.
      wysiwyg_add_editor_settings($profile, $theme);
    }
  }
  // Use a hidden element for a single text format.
  if (!$format_field['format']['#access']) {
    $format_field['wysiwyg'] = array(
      '#type' => 'hidden',
      '#name' => $format_field['format']['#name'],
      '#value' => $format_id,
      '#attributes' => array(
        'id' => $format_field['format']['#id'],
        'class' => array('wysiwyg'),
      ),
    );
    $format_field['wysiwyg']['#attached']['js'][] = array(
      'data' => array(
        'wysiwyg' => array(
          'triggers' => array(
            $format_field['format']['#id'] => $settings,
          ),
        ),
      ),
      'type' => 'setting',
    );
  }
  // Otherwise, attach to text format selector.
  else {
    $format_field['format']['#attributes']['class'][] = 'wysiwyg';
    $format_field['format']['#attached']['js'][] = array(
      'data' => array(
        'wysiwyg' => array(
          'triggers' => array(
            $format_field['format']['#id'] => $settings,
          ),
        ),
      ),
      'type' => 'setting',
    );
  }

  // If we loaded at least one editor, then the 'none' editor will
  // handle resizable textareas instead of core.
  if (isset($loaded) && $resizable) {
    $field['#resizable'] = FALSE;
  }

  return $element;
}

/**
 * Determine the profile to use for a given input format id.
 *
 * This function also performs sanity checks for the configured editor in a
 * profile to ensure that we do not load a malformed editor.
 *
 * @param $format
 *   The internal id of an input format.
 *
 * @return
 *   A wysiwyg profile.
 *
 * @see wysiwyg_load_editor(), wysiwyg_get_editor()
 */
function wysiwyg_get_profile($format) {
  if ($profile = wysiwyg_profile_load($format)) {
    if (wysiwyg_load_editor($profile)) {
      return $profile;
    }
  }
  return FALSE;
}

/**
 * Load an editor library and initialize basic Wysiwyg settings.
 *
 * @param $profile
 *   A wysiwyg editor profile.
 *
 * @return
 *   TRUE if the editor has been loaded, FALSE if not.
 *
 * @see wysiwyg_get_profile()
 */
function wysiwyg_load_editor($profile) {
  static $settings_added;
  static $loaded = array();
  $path = drupal_get_path('module', 'wysiwyg');

  $name = $profile->editor;
  // Library files must be loaded only once.
  if (!isset($loaded[$name])) {
    // Load editor.
    $editor = wysiwyg_get_editor($name);
    if ($editor) {
      $default_library_options = array(
        'type' => 'file',
        'scope' => 'header',
        'defer' => FALSE,
        'cache' => TRUE,
        'preprocess' => TRUE,
      );
      // Determine library files to load.
      // @todo Allow to configure the library/execMode to use.
      if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) {
        $library = $profile->settings['library'];
        $files = $editor['libraries'][$library]['files'];
      }
      else {
        // Fallback to the first defined library by default (external libraries can change).
        $library = key($editor['libraries']);
        $files = array_shift($editor['libraries']);
        $files = $files['files'];
      }

      // Check whether the editor requires an initialization script.
      if (!empty($editor['init callback'])) {
        $init = $editor['init callback']($editor, $library, $profile);
        if (!empty($init)) {
          // Build a file for each of the editors to hold the init scripts.
          // @todo Aggregate all initialization scripts into one file.
          $uri = 'public://js/wysiwyg/wysiwyg_' . $name . '_' . drupal_hash_base64($init) . '.js';
          $init_exists = file_exists($uri);
          if (!$init_exists) {
            $js_path = dirname($uri);
            file_prepare_directory($js_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
          }
          // Attempt to create the file, or fall back to an inline script (which
          // will not work in Ajax calls).
          if (!$init_exists && !file_unmanaged_save_data($init, $uri, FILE_EXISTS_REPLACE)) {
            drupal_add_js($init, array('type' => 'inline') + $default_library_options);
          }
          else {
            drupal_add_js(file_create_url($uri), $default_library_options);
          }
        }
      }

      foreach ($files as $file => $options) {
        if (is_array($options)) {
          $options += $default_library_options;
          drupal_add_js($editor['library path'] . '/' . $file, $options);
        }
        else {
          drupal_add_js($editor['library path'] . '/' . $options);
        }
      }
      // If editor defines an additional load callback, invoke it.
      // @todo Isn't the settings callback sufficient?
      if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
        $editor['load callback']($editor, $library);
      }
      // Load JavaScript integration files for this editor.
      $files = array();
      if (isset($editor['js files'])) {
        $files = $editor['js files'];
      }
      foreach ($files as $file) {
        drupal_add_js($editor['js path'] . '/' . $file);
      }
      // Load CSS stylesheets for this editor.
      $files = array();
      if (isset($editor['css files'])) {
        $files = $editor['css files'];
      }
      foreach ($files as $file) {
        drupal_add_css($editor['css path'] . '/' . $file);
      }
      $loaded[$name] = TRUE;
    }
    else {
      $loaded[$name] = FALSE;
    }
  }

  // Add basic Wysiwyg settings if any editor has been added.
  if (!isset($settings_added) && $loaded[$name]) {
    drupal_add_js(array('wysiwyg' => array(
      'configs' => array(),
      'plugins' => array(),
      'disable' => t('Disable rich-text'),
      'enable' => t('Enable rich-text'),
    )), 'setting');

    // Initialize our namespaces in the *header* to do not force editor
    // integration scripts to check and define Drupal.wysiwyg on its own.
    drupal_add_js($path . '/wysiwyg.init.js', array('group' => JS_LIBRARY));

    // The 'none' editor is a special editor implementation, allowing us to
    // attach and detach regular Drupal behaviors just like any other editor.
    drupal_add_js($path . '/editors/js/none.js');

    // Add wysiwyg.js to the footer to ensure it's executed after the
    // Drupal.settings array has been rendered and populated. Also, since editor
    // library initialization functions must be loaded first by the browser,
    // and Drupal.wysiwygInit() must be executed AFTER editors registered
    // their callbacks and BEFORE Drupal.behaviors are applied, this must come
    // last.
    drupal_add_js($path . '/wysiwyg.js', array('scope' => 'footer'));

    $settings_added = TRUE;
  }

  return $loaded[$name];
}

/**
 * Add editor settings for a given input format.
 */
function wysiwyg_add_editor_settings($profile, $theme) {
  static $formats = array();

  if (!isset($formats[$profile->format])) {
    $config = wysiwyg_get_editor_config($profile, $theme);
    // drupal_to_js() does not properly convert numeric array keys, so we need
    // to use a string instead of the format id.
    if ($config) {
      drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array('format' . $profile->format => $config)))), 'setting');
    }
    $formats[$profile->format] = TRUE;
  }
}

/**
 * Add settings for external plugins.
 *
 * Plugins can be used in multiple profiles, but not necessarily in all. Because
 * of that, we need to process plugins for each profile, even if most of their
 * settings are not stored per profile.
 *
 * Implementations of hook_wysiwyg_plugin() may execute different code for each
 * editor. Therefore, we have to invoke those implementations for each editor,
 * but process the resulting plugins separately for each profile.
 *
 * Drupal plugins differ to native plugins in that they have plugin-specific
 * definitions and settings, which need to be processed only once. But they are
 * also passed to the editor to prepare settings specific to the editor.
 * Therefore, we load and process the Drupal plugins only once, and hand off the
 * effective definitions for each profile to the editor.
 *
 * @param $profile
 *   A wysiwyg editor profile.
 *
 * @todo Rewrite wysiwyg_process_form() to build a registry of effective
 *   profiles in use, so we can process plugins in multiple profiles in one shot
 *   and simplify this entire function.
 */
function wysiwyg_add_plugin_settings($profile) {
  static $plugins = array();
  static $processed_plugins = array();
  static $processed_formats = array();

  // Each input format must only processed once.
  // @todo ...as long as we do not have multiple profiles per format.
  if (isset($processed_formats[$profile->format])) {
    return;
  }
  $processed_formats[$profile->format] = TRUE;

  $editor = wysiwyg_get_editor($profile->editor);

  // Collect native plugins for this editor provided via hook_wysiwyg_plugin()
  // and Drupal plugins provided via hook_wysiwyg_include_directory().
  if (!array_key_exists($editor['name'], $plugins)) {
    $plugins[$editor['name']] = wysiwyg_get_plugins($editor['name']);
  }

  // Nothing to do, if there are no plugins.
  if (empty($plugins[$editor['name']])) {
    return;
  }

  // Determine name of proxy plugin for Drupal plugins.
  $proxy = (isset($editor['proxy plugin']) ? key($editor['proxy plugin']) : '');

  // Process native editor plugins.
  if (isset($editor['plugin settings callback'])) {
    // @todo Require PHP 5.1 in 3.x and use array_intersect_key().
    $profile_plugins_native = array();
    foreach ($plugins[$editor['name']] as $plugin => $meta) {
      // Skip Drupal plugins (handled below).
      if ($plugin === $proxy) {
        continue;
      }
      // Only keep native plugins that are enabled in this profile.
      if (isset($profile->settings['buttons'][$plugin])) {
        $profile_plugins_native[$plugin] = $meta;
      }
    }
    // Invoke the editor's plugin settings callback, so it can populate the
    // settings for native external plugins with required values.
    $settings_native = call_user_func($editor['plugin settings callback'], $editor, $profile, $profile_plugins_native);

    if ($settings_native) {
      drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('native' => $settings_native)))), 'setting');
    }
  }

  // Process Drupal plugins.
  if ($proxy && isset($editor['proxy plugin settings callback'])) {
    $profile_plugins_drupal = array();
    foreach (wysiwyg_get_all_plugins() as $plugin => $meta) {
      if (isset($profile->settings['buttons'][$proxy][$plugin])) {
        // JavaScript and plugin-specific settings for Drupal plugins must be
        // loaded and processed only once. Plugin information is cached
        // statically to pass it to the editor's proxy plugin settings callback.
        if (!isset($processed_plugins[$proxy][$plugin])) {
          $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin] = $meta;
          // Load the Drupal plugin's JavaScript.
          drupal_add_js($meta['js path'] . '/' . $meta['js file']);
          // Add plugin-specific settings.
          if (isset($meta['settings'])) {
            drupal_add_js(array('wysiwyg' => array('plugins' => array('drupal' => array($plugin => $meta['settings'])))), 'setting');
          }
        }
        else {
          $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin];
        }
      }
    }
    // Invoke the editor's proxy plugin settings callback, so it can populate
    // the settings for Drupal plugins with custom, required values.
    $settings_drupal = call_user_func($editor['proxy plugin settings callback'], $editor, $profile, $profile_plugins_drupal);

    if ($settings_drupal) {
      drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('drupal' => $settings_drupal)))), 'setting');
    }
  }
}

/**
 * Retrieve available themes for an editor.
 *
 * Editor themes control the visual presentation of an editor.
 *
 * @param $profile
 *   A wysiwyg editor profile; passed/altered by reference.
 * @param $selected_theme
 *   An optional theme name that ought to be used.
 *
 * @return
 *   An array of theme names, or a single, checked theme name if $selected_theme
 *   was given.
 */
function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) {
  static $themes = array();

  if (!isset($themes[$profile->editor])) {
    $editor = wysiwyg_get_editor($profile->editor);
    if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
      $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
    }
    // Fallback to 'default' otherwise.
    else {
      $themes[$editor['name']] = array('default');
    }
  }

  // Check optional $selected_theme argument, if given.
  if (isset($selected_theme)) {
    // If the passed theme name does not exist, use the first available.
    if (!in_array($selected_theme, $themes[$profile->editor])) {
      $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0];
    }
  }

  return isset($selected_theme) ? $selected_theme : $themes[$profile->editor];
}

/**
 * Return plugin metadata from the plugin registry.
 *
 * @param $editor_name
 *   The internal name of an editor to return plugins for.
 *
 * @return
 *   An array for each plugin.
 */
function wysiwyg_get_plugins($editor_name) {
  $plugins = array();
  if (!empty($editor_name)) {
    $editor = wysiwyg_get_editor($editor_name);
    // Add internal editor plugins.
    if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
      $plugins = $editor['plugin callback']($editor);
    }
    // Add editor plugins provided via hook_wysiwyg_plugin().
    $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
    // Add API plugins provided by Drupal modules.
    // @todo We need to pass the filepath to the plugin icon for Drupal plugins.
    if (isset($editor['proxy plugin'])) {
      $plugins += $editor['proxy plugin'];
      $proxy = key($editor['proxy plugin']);
      foreach (wysiwyg_get_all_plugins() as $plugin_name => $info) {
        $plugins[$proxy]['buttons'][$plugin_name] = $info['title'];
      }
    }
  }
  return $plugins;
}

/**
 * Return an array of initial editor settings for a Wysiwyg profile.
 */
function wysiwyg_get_editor_config($profile, $theme) {
  $editor = wysiwyg_get_editor($profile->editor);
  $settings = array();
  if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
    $settings = $editor['settings callback']($editor, $profile->settings, $theme);

    // Allow other modules to alter the editor settings for this format.
    $context = array('editor' => $editor, 'profile' => $profile, 'theme' => $theme);
    drupal_alter('wysiwyg_editor_settings', $settings, $context);
  }
  return $settings;
}

/**
 * Retrieve stylesheets for HTML/IFRAME-based editors.
 *
 * This assumes that the content editing area only needs stylesheets defined
 * for the scope 'theme'.
 *
 * @return
 *   An array containing CSS files, including proper base path.
 */
function wysiwyg_get_css() {
  static $files;

  if (isset($files)) {
    return $files;
  }
  // In node form previews, the theme has not been initialized yet.
  if (!empty($_POST)) {
    drupal_theme_initialize();
  }

  $files = array();
  foreach (drupal_add_css() as $filepath => $info) {
    if ($info['group'] >= CSS_THEME && $info['media'] != 'print') {
      if ($info['type'] == 'external') {
        $files[] = $filepath;
      }
      elseif (file_exists($filepath)) {
        $files[] = base_path() . $filepath;
      }
    }
  }
  return $files;
}

/**
 * Loads a profile for a given text format.
 *
 * Since there are commonly not many text formats, and each text format-enabled
 * form element will possibly have to load every single profile, all existing
 * profiles are loaded and cached once to reduce the amount of database queries.
 */
function wysiwyg_profile_load($format) {
  $profiles = wysiwyg_profile_load_all();
  return (isset($profiles[$format]) ? $profiles[$format] : FALSE);
}

/**
 * Loads all profiles.
 */
function wysiwyg_profile_load_all() {
  // entity_load(..., FALSE) does not re-use its own static cache upon
  // repetitive calls, so a custom static cache is required.
  // @see wysiwyg_entity_info()
  $profiles = &drupal_static(__FUNCTION__);

  if (!isset($profiles)) {
    // Additional database cache to support alternative caches like memcache.
    if ($cached = cache_get('wysiwyg_profiles')) {
      $profiles = $cached->data;
    }
    else {
      $profiles = entity_load('wysiwyg_profile', FALSE);
      cache_set('wysiwyg_profiles', $profiles);
    }
  }

  return $profiles;
}

/**
 * Deletes a profile from the database.
 */
function wysiwyg_profile_delete($format) {
  db_delete('wysiwyg')
    ->condition('format', $format)
    ->execute();
}

/**
 * Clear all Wysiwyg profile caches.
 */
function wysiwyg_profile_cache_clear() {
  entity_get_controller('wysiwyg_profile')->resetCache();
  drupal_static_reset('wysiwyg_profile_load_all');
  cache_clear_all('wysiwyg_profiles', 'cache');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function wysiwyg_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  $account = $form['#user'];
  $user_formats = filter_formats($account);
  $options = array();
  $options_default = array();
  foreach (wysiwyg_profile_load_all() as $format => $profile) {
    // Only show profiles that have user_choose enabled.
    if (!empty($profile->settings['user_choose']) && isset($user_formats[$format])) {
      $options[$format] = check_plain($user_formats[$format]->name);
      if (wysiwyg_user_get_status($profile, $account)) {
        $options_default[] = $format;
      }
    }
  }
  if (!empty($options)) {
    $form['wysiwyg']['wysiwyg_status'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Text formats enabled for rich-text editing'),
      '#options' => $options,
      '#default_value' => $options_default,
    );
  }
}

/**
 * Implements hook_user_insert().
 *
 * Wysiwyg's user preferences are normally not exposed on the user registration
 * form, but in case they are manually altered in, we invoke
 * wysiwyg_user_update() accordingly.
 */
function wysiwyg_user_insert(&$edit, $account, $category) {
  wysiwyg_user_update($edit, $account, $category);
}

/**
 * Implements hook_user_update().
 */
function wysiwyg_user_update(&$edit, $account, $category) {
  if (isset($edit['wysiwyg_status'])) {
    db_delete('wysiwyg_user')
      ->condition('uid', $account->uid)
      ->execute();
    $query = db_insert('wysiwyg_user')
      ->fields(array('uid', 'format', 'status'));
    foreach ($edit['wysiwyg_status'] as $format => $status) {
      $query->values(array(
        'uid' => $account->uid,
        'format' => $format,
        'status' => (int) (bool) $status,
      ));
    }
    $query->execute();
  }
}

function wysiwyg_user_get_status($profile, $account = NULL) {
  global $user;

  if (!isset($account)) {
    $account = $user;
  }

  // Default wysiwyg editor status information is only required on forms, so we
  // do not pre-emptively load and attach this information on every user_load().
  if (!isset($account->wysiwyg_status)) {
    $account->wysiwyg_status = db_query("SELECT format, status FROM {wysiwyg_user} WHERE uid = :uid", array(
      ':uid' => $account->uid,
    ))->fetchAllKeyed();
  }

  if (!empty($profile->settings['user_choose']) && isset($account->wysiwyg_status[$profile->format])) {
    $status = $account->wysiwyg_status[$profile->format];
  }
  else {
    $status = isset($profile->settings['default']) ? $profile->settings['default'] : TRUE;
  }

  return (bool) $status;
}

/**
 * @defgroup wysiwyg_api Wysiwyg API
 * @{
 *
 * @todo Forked from Panels; abstract into a separate API module that allows
 *   contrib modules to define supported include/plugin types.
 */

/**
 * Return library information for a given editor.
 *
 * @param $name
 *   The internal name of an editor.
 *
 * @return
 *   The library information for the editor, or FALSE if $name is unknown or not
 *   installed properly.
 */
function wysiwyg_get_editor($name) {
  $editors = wysiwyg_get_all_editors();
  return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE;
}

/**
 * Compile a list holding all supported editors including installed editor version information.
 */
function wysiwyg_get_all_editors() {
  static $editors;

  if (isset($editors)) {
    return $editors;
  }

  $editors = wysiwyg_load_includes('editors', 'editor');
  foreach ($editors as $editor => $properties) {
    // Fill in required properties.
    $editors[$editor] += array(
      'title' => '',
      'vendor url' => '',
      'download url' => '',
      'editor path' => wysiwyg_get_path($editors[$editor]['name']),
      'library path' => wysiwyg_get_path($editors[$editor]['name']),
      'libraries' => array(),
      'version callback' => NULL,
      'themes callback' => NULL,
      'settings form callback' => NULL,
      'settings callback' => NULL,
      'plugin callback' => NULL,
      'plugin settings callback' => NULL,
      'versions' => array(),
      'js path' => $editors[$editor]['path'] . '/js',
      'css path' => $editors[$editor]['path'] . '/css',
    );
    // Check whether library is present.
    if (!($editors[$editor]['installed'] = file_exists($editors[$editor]['library path']))) {
      continue;
    }
    // Detect library version.
    if (function_exists($editors[$editor]['version callback'])) {
      $editors[$editor]['installed version'] = $editors[$editor]['version callback']($editors[$editor]);
    }
    if (empty($editors[$editor]['installed version'])) {
      $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
      $editors[$editor]['installed'] = FALSE;
      continue;
    }
    // Determine to which supported version the installed version maps.
    ksort($editors[$editor]['versions']);
    $version = 0;
    foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) {
      if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
        $version = $supported_version;
      }
    }
    if (!$version) {
      $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $editors[$editor]['title']));
      $editors[$editor]['installed'] = FALSE;
      continue;
    }
    // Apply library version specific definitions and overrides.
    $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
    unset($editors[$editor]['versions']);
  }
  return $editors;
}

/**
 * Invoke hook_wysiwyg_plugin() in all modules.
 */
function wysiwyg_get_all_plugins() {
  static $plugins;

  if (isset($plugins)) {
    return $plugins;
  }

  $plugins = wysiwyg_load_includes('plugins', 'plugin');
  foreach ($plugins as $name => $properties) {
    $plugin = &$plugins[$name];
    // Fill in required/default properties.
    $plugin += array(
      'title' => $plugin['name'],
      'vendor url' => '',
      'js path' => $plugin['path'] . '/' . $plugin['name'],
      'js file' => $plugin['name'] . '.js',
      'css path' => $plugin['path'] . '/' . $plugin['name'],
      'css file' => $plugin['name'] . '.css',
      'icon path' => $plugin['path'] . '/' . $plugin['name'] . '/images',
      'icon file' => $plugin['name'] . '.png',
      'dialog path' => $plugin['name'],
      'dialog settings' => array(),
      'settings callback' => NULL,
      'settings form callback' => NULL,
    );
    // Fill in default settings.
    $plugin['settings'] += array(
      'path' => base_path() . $plugin['path'] . '/' . $plugin['name'],
    );
    // Check whether library is present.
    if (!($plugin['installed'] = file_exists($plugin['js path'] . '/' . $plugin['js file']))) {
      continue;
    }
  }
  return $plugins;
}

/**
 * Load include files for wysiwyg implemented by all modules.
 *
 * @param $type
 *   The type of includes to search for, can be 'editors'.
 * @param $hook
 *   The hook name to invoke.
 * @param $file
 *   An optional include file name without .inc extension to limit the search to.
 *
 * @see wysiwyg_get_directories(), _wysiwyg_process_include()
 */
function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) {
  // Determine implementations.
  $directories = wysiwyg_get_directories($type);
  $directories['wysiwyg'] = drupal_get_path('module', 'wysiwyg') . '/' . $type;
  $file_list = array();
  foreach ($directories as $module => $path) {
    $file_list[$module] = drupal_system_listing("/{$file}.inc\$/", $path, 'name', 0);
  }

  // Load implementations.
  $info = array();
  foreach (array_filter($file_list) as $module => $files) {
    foreach ($files as $file) {
      include_once './' . $file->uri;
      $result = _wysiwyg_process_include($module, $module . '_' . $file->name, dirname($file->uri), $hook);
      if (is_array($result)) {
        $info = array_merge($info, $result);
      }
    }
  }
  return $info;
}

/**
 * Helper function to build paths to libraries.
 *
 * @param $library
 *   The external library name to return the path for.
 * @param $base_path
 *   Whether to prefix the resulting path with base_path().
 *
 * @return
 *   The path to the specified library.
 *
 * @ingroup libraries
 */
function wysiwyg_get_path($library, $base_path = FALSE) {
  static $libraries;

  if (!isset($libraries)) {
    $libraries = wysiwyg_get_libraries();
  }
  if (!isset($libraries[$library])) {
    // Most often, external libraries can be shared across multiple sites.
    return 'sites/all/libraries/' . $library;
  }

  $path = ($base_path ? base_path() : '');
  $path .= $libraries[$library];

  return $path;
}

/**
 * Return an array of library directories.
 *
 * Returns an array of library directories from the all-sites directory
 * (i.e. sites/all/libraries/), the profiles directory, and site-specific
 * directory (i.e. sites/somesite/libraries/). The returned array will be keyed
 * by the library name. Site-specific libraries are prioritized over libraries
 * in the default directories. That is, if a library with the same name appears
 * in both the site-wide directory and site-specific directory, only the
 * site-specific version will be listed.
 *
 * @return
 *   A list of library directories.
 *
 * @ingroup libraries
 */
function wysiwyg_get_libraries() {
  global $profile;

  // When this function is called during Drupal's initial installation process,
  // the name of the profile that is about to be installed is stored in the
  // global $profile variable. At all other times, the regular system variable
  // contains the name of the current profile, and we can call variable_get()
  // to determine the profile.
  if (!isset($profile)) {
    $profile = variable_get('install_profile', 'default');
  }

  $directory = 'libraries';
  $searchdir = array();
  $config = conf_path();

  // The 'profiles' directory contains pristine collections of modules and
  // themes as organized by a distribution.  It is pristine in the same way
  // that /modules is pristine for core; users should avoid changing anything
  // there in favor of sites/all or sites/<domain> directories.
  if (file_exists("profiles/$profile/$directory")) {
    $searchdir[] = "profiles/$profile/$directory";
  }

  // Always search sites/all/*.
  $searchdir[] = 'sites/all/' . $directory;

  // Also search sites/<domain>/*.
  if (file_exists("$config/$directory")) {
    $searchdir[] = "$config/$directory";
  }

  // Retrieve list of directories.
  // @todo Core: Allow to scan for directories.
  $directories = array();
  $nomask = array('CVS');
  foreach ($searchdir as $dir) {
    if (is_dir($dir) && $handle = opendir($dir)) {
      while (FALSE !== ($file = readdir($handle))) {
        if (!in_array($file, $nomask) && $file[0] != '.') {
          if (is_dir("$dir/$file")) {
            $directories[$file] = "$dir/$file";
          }
        }
      }
      closedir($handle);
    }
  }

  return $directories;
}

/**
 * Return a list of directories by modules implementing wysiwyg_include_directory().
 *
 * @param $plugintype
 *   The type of a plugin; can be 'editors'.
 *
 * @return
 *   An array containing module names suffixed with '_' and their defined
 *   directory.
 *
 * @see wysiwyg_load_includes(), _wysiwyg_process_include()
 */
function wysiwyg_get_directories($plugintype) {
  $directories = array();
  foreach (module_implements('wysiwyg_include_directory') as $module) {
    $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
    if (isset($result) && is_string($result)) {
      $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
    }
  }
  return $directories;
}

/**
 * Process a single hook implementation of a wysiwyg editor.
 *
 * @param $module
 *   The module that owns the hook.
 * @param $identifier
 *   Either the module or 'wysiwyg_' . $file->name
 * @param $hook
 *   The name of the hook being invoked.
 */
function _wysiwyg_process_include($module, $identifier, $path, $hook) {
  $function = $identifier . '_' . $hook;
  if (!function_exists($function)) {
    return NULL;
  }
  $result = $function();
  if (!isset($result) || !is_array($result)) {
    return NULL;
  }

  // Fill in defaults.
  foreach ($result as $editor => $properties) {
    $result[$editor]['module'] = $module;
    $result[$editor]['name'] = $editor;
    $result[$editor]['path'] = $path;
  }
  return $result;
}

/**
 * @} End of "defgroup wysiwyg_api".
 */

/**
 * Implements hook_features_api().
 */
function wysiwyg_features_api() {
  return array(
    'wysiwyg' => array(
      'name' => t('Wysiwyg profiles'),
      'default_hook' => 'wysiwyg_default_profiles',
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
      'feature_source' => TRUE,
      'file' => drupal_get_path('module', 'wysiwyg') . '/wysiwyg.features.inc',
    ),
  );
}