File: tinymce.class.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (381 lines) | stat: -rw-r--r-- 13,269 bytes parent folder | download | duplicates (2)
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
<?php // $Id: tinymce.class.php,v 1.3 2006/03/06 12:38:14 julmis Exp $

/**
 * This file contains the tinymce subclass for moodle editorObject.
 *
 * @author Janne Mikkonen
 * @version  $Id: tinymce.class.php,v 1.3 2006/03/06 12:38:14 julmis Exp $
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package editorObject
 */
class tinymce extends editorObject {

    /**
    * The tinyconf variable holds custom variable keys and values pairs in array.
    * @var array $tinyconf
    */
    var $tinyconf = array();

    /**
    * Internal tinyconfkeys is an array of valid configuration keys.
    * @var array $tinyconfkeys
    */
    var $tinyconfkeys = array(
        "mode","theme","plugins","language","ask","textarea_trigger",
        "editor_selector","editor_deselector","elements","docs_language",
        "debug","focus_alert","directionality","auto_reset_designmode",
        "auto_focus","nowrap","button_tile_map","auto_resize","browsers",
        "dialog_type","accessibility_warnings","accessibility_focus",
        "event_elements","table_inline_editing","object_resizing","custom_shortcuts",
        "cleanup","valid_elements","extended_valid_elements","invalid_elements",
        "verify_css_classes","verify_html","preformatted","encoding","cleanup_on_startup",
        "fix_content_duplication","inline_styles","convert_newlines_to_brs","force_br_newlines",
        "force_p_newlines","entities","entity_encoding","remove_linebreaks","convert_fonts_to_spans",
        "font_size_classes","font_size_style_values","merge_styles_invalid_parents",
        "force_hex_style_colors","apply_source_formatting","trim_span_elements","doctype",
        "convert_urls","relative_urls","remove_script_host","document_base_url",
        "urlconverter_callback","insertlink_callback","insertimage_callback","setupcontent_callback",
        "save_callback","onchange_callback","init_instance_callback","file_browser_callback",
        "cleanup_callback","handle_event_callback","execcommand_callback","oninit","onpageload",
        "content_css","popups_css","editor_css","width","height","visual","visual_table_class",
        "custom_undo_redo","custom_undo_redo_levels","custom_undo_redo_keyboard_shortcuts",
        "custom_undo_redo_restore_selection","external_link_list_url","external_image_list_url",
        "add_form_submit_trigger","add_unload_trigger","submit_patch");

    /**
    * Array of valid advanced theme configuration keys.
    * @var array $tinythemekeys
    */
    var $tinythemekeys = array(
        "theme_advanced_layout_manager","theme_advanced_blockformats","theme_advanced_styles",
        "theme_advanced_source_editor_width","theme_advanced_source_editor_height",
        "theme_advanced_toolbar_location","theme_advanced_toolbar_align",
        "theme_advanced_statusbar_location","theme_advanced_buttons<1-n>","theme_advanced_buttons<1-n>_add",
        "theme_advanced_buttons<1-n>_add_before","theme_advanced_disable","theme_advanced_containers",
        "theme_advanced_containers_default_class","theme_advanced_containers_default_align",
        "theme_advanced_container_<container>","theme_advanced_container_<container>_class",
        "theme_advanced_container_<container>_align","theme_advanced_custom_layout",
        "theme_advanced_link_targets","theme_advanced_resizing","theme_advanced_resizing_use_cookie",
        "theme_advanced_resize_horizontal","theme_advanced_path","theme_advanced_fonts");

    /**
    * The defaults configuration array for internal use.
    * @var array $defaults
    */
    var $defaults = array();

    /**
    * For internal usage variable which holds the information
    * should dialogs script be printed after configuration.
    * @var bool $printdialogs
    */
    var $printdialogs = false;

    /**
    * PHP4 style class constructor.
    *
    * @param int $courseid
    */
    function tinymce ($courseid) {
        parent::editorObject();
        $this->courseid = clean_param($courseid, PARAM_INT);

        $isteacher = isteacher($courseid);

        $this->defaults = array(
            "mode"     => "textareas",
            "theme"    => $this->cfg->tinymcetheme,
            "language" => $this->__get_language(),
            "width"    => "100%",
            "plugins"      => !empty($this->cfg->tinymceplugins) ?
                              $this->cfg->tinymceplugins : '',
            "content_css"  => !empty($this->cfg->tinymcecontentcss) ?
                              $this->cfg->tinymcecontentcss : '',
            "popup_css"    => !empty($this->cfg->tinymcepopupcss) ?
                              $this->cfg->tinymcepopupcss : '',
            "editor_css"   => !empty($this->cfg->tinymceeditorcss) ?
                              $this->cfg->tinymceeditorcss : '',
            "file_browser_callback" => $isteacher ? 'moodleFileBrowser' : '',
            "convert_urls"  => false,
            "relative_urls" => false);

            if ( $this->cfg->tinymcetheme == 'advanced' ) {
                $this->defaults['theme_advanced_buttons1_add'] = "fontselect,fontsizeselect";
                $this->defaults['theme_advanced_buttons2_add'] = "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor,liststyle";
                $this->defaults['theme_advanced_buttons2_add_before'] = "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator";
                $this->defaults['theme_advanced_buttons3_add_before'] = "tablecontrols,separator";
                $this->defaults['theme_advanced_buttons3_add'] = "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen";
                $this->defaults['theme_advanced_toolbar_location'] = "top";
                $this->defaults['theme_advanced_toolbar_align'] = "left";
                $this->defaults['theme_advanced_statusbar_location'] = "bottom";
                $this->defaults['theme_advanced_resizing'] = true;
                $this->defaults['theme_advanced_resize_horizontal'] = true;
            }

            $this->printdialogs = $isteacher ? true : false;
    }

    /**
    * PHP5 style class constructor.
    *
    * @param int $courseid
    */
    function __construct($courseid) {
        $this->tinymce($courseid);
    }

    /**
    * Checks configuration key validity.
    * @param string $key Configuration key to check.
    * @return bool Returns true if key is valid. Otherwise false is returned.
    */
    function __is_valid_key($key) {

        if ( is_array($key) ) {
            return false;
        }

        if ( strstr($key, "theme_advanced_") ) { // Search in theme keys.

            foreach ( $this->tinythemekeys as $value ) {
                if ( strstr($key, "<1-n>") ) {
                    $value = preg_replace("/<(.*)>/", "([0-9]+)", $value);
                } else {
                    $value = preg_replace("/<(.*)>/", "([a-z0-9]+)", $value);
                }

                if ( preg_match("/^(". $value .")$/i", $key) ) {
                    return true;
                }
            }

        } else {
            if ( in_array($key, $this->tinyconfkeys) ) {
                return true;
            }
        }
        return false;

    }

    /**
    * Sets configuration key and value pairs.
    * Passed parameters can be key and value pair or
    * an associative array of keys and values.
    * @todo code example
    */
    function setconfig () {

        $numargs = func_num_args();

        if ( $numargs > 2 ) {
            $this->error("Too many arguments!");
            exit;
        }

        if ( $numargs < 1 ) {
            $this->error("No arguments passed!");
            exit;
        }

        switch ( $numargs ) {
            case 1: // Must be an array.

                $arg = func_get_arg(0);
                if ( is_array($arg) ) {
                    foreach ( $arg as $key => $value ) {
                        if ( !is_string($key) ) {
                            $this->error("Array is not associative array!");
                            exit;
                        }
                        if ( !$this->__is_valid_key($key) ) {
                            $this->error("Invalid configuration key: '$key'");
                        }

                        $this->tinyconf[$key] = $value;
                    }
                } else {
                    $this->error("Given argument is not an array!!!");
                }

            break;
            case 2: // Key, Value pair.

                $key   = func_get_arg(0);
                $value = func_get_arg(1);

                if ( !$this->__is_valid_key($key) ) {
                    $this->error("Invalid configuration key: $key");
                }
                $this->tinyconf[$key] = $value;

            break;
        }

    }

    /**
    * For internal usage. Print out configuration arrays.
    * @param string $conftype Type of configuration.
    * @return void
    */
    function __printconfig ($conftype='') {

        switch ( $conftype ) {
            case 'merge': // New config overrides defaults if found.
                $conf = array_merge($this->defaults,$this->tinyconf);
            break;
            case 'append': // Append mode leave default value if found.
                $conf = $this->defaults;
                $keys = array_keys($this->defaults);
                foreach ( $this->tinyconf as $key => $value ) {
                    if ( in_array($key, $keys) ) {
                        continue;
                    } else {
                        $conf[$key] = $value;
                    }
                }
            break;
            case 'default':
                $conf = $this->defaults;
            break;
            default:
                $conf = $this->tinyconf;
        }

        echo "\n";
        echo '<script language="javascript" type="text/javascript">'."\n";
        echo '<!--'."\n";
        echo '  tinyMCE.init({'."\n";

        if ( !empty($conf) ) {
            $max = count($conf);
            $cnt = 1;
            foreach ( $conf as $key => $value ) {

                if ( empty($value) ) {
                    continue;
                }

                if ( $cnt > 1 ) {
                    echo ',' ."\n";
                }

                echo "\t" . $key .' : ';

                if ( is_bool($value) ) {
                    echo ($value) ? 'true' : 'false';
                } else {
                    echo '"'. $value .'"';
                }

                $cnt++;
            }
        }

        echo '  });'."\n";

        if ( $this->printdialogs ) {
            $this->__dialogs();
        }
        echo '// done hiding -->'."\n";
        echo '</script>'."\n";

    }

    /**
    * Print out code that start up the editor.
    * @param string $conftype Configuration type to print.
    */
    function starteditor($conftype='default') {
        $this->__printconfig($conftype);
    }

    /**
    * For backward compatibility only.
    * @param string $name
    * @param string $editorhidesomebuttons
    */
    function use_html_editor($name='', $editorhidesomebuttons='') {
        if ( empty($this->tinyconf) ) {
            $this->__printconfig('default');
        }

        if ( !empty($this->cfg->editorsrc) ) {
            unset($this->cfg->editorsrc);
        }

    }

    /**
    * Print out needed script for custom dialog which is
    * needed to provide access to Moodle's files and folders.
    * For internal use only.
    */
    function __dialogs() {
        ?>

        function moodleFileBrowser (field_name, url, type, win) {
            Dialog("<?php p($this->cfg->wwwroot) ?>/lib/editor/htmlarea/popups/link.php?id=<?php p($this->courseid) ?>", 470, 400, function (param) {

                if ( !param ) {
                    return false;
                }

                win.document.forms[0].elements[field_name].value = param;

            },null);
        }

        <?php
    }

    /**
    * Try to generate TinyMCE compatible language string from
    * current users language. If not successful return default
    * language which is english.
    * For internal use only.
    */
    function __get_language() {

        $tinylangdir = $this->cfg->libdir .'/editor/tinymce/jscripts/tiny_mce/langs';
        $currentlanguage = current_language();
        $defaultlanguage = 'en';

        if ( !$fp = opendir($tinylangdir) ) {
            return $defaultlanguage;
            exit;
        }

        $languages = array();

        while ( ($file = readdir($fp)) !== false ) {
            if ( preg_match("/\.js$/i", $file) ) {
                array_push($languages, basename($file, '.js'));
            }
        }

        if ( $fp ) {
            closedir($fp);
        }

        // If language is found in array.
        if ( in_array($currentlanguage, $languages) ) {
            return $currentlanguage;
        }

        // Check if two character country code is found (eg. fi, de, sv etc.)
        // then return that.
        $currentlanguage = str_replace("_utf8", "", $currentlanguage);

        if ( in_array($currentlanguage, $languages) ) {
            return $currentlanguage;
        }

        return $defaultlanguage;

    }

}

?>