TinyMCE::init()   F
last analyzed

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 137
Code Lines 84

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 28
eloc 84
c 6
b 0
f 0
nc 24672
nop 0
dl 0
loc 137
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
13
/**
14
 *  TinyMCE 4.x adapter for XOOPS
15
 *
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @package         class
19
 * @subpackage      editor
20
 * @since           2.3.0
21
 * @author          Taiwen Jiang <[email protected]>
22
 * @author          Lucio Rota <[email protected]>
23
 * @author          Laurent JEN <[email protected]>
24
 * @version         $Id $
25
 */
26
class TinyMCE
27
{
28
    public $rootpath;
29
    public $config  = [];
30
    public $setting = [];
31
32
    /**
33
     * PHP 5 Constructor
34
     *
35
     * @param string $config The configuration
36
     **/
37
38
    public function __construct($config)
39
    {
40
        $this->setConfig($config);
41
        $this->rootpath = $this->config['rootpath'] . '/tinymce/js/tinymce';
42
    }
43
44
    /**
45
     * Creates one instance of the tinyMCE object
46
     *
47
     * @param array $config The configuration
48
     * @return   object    $instance   The instance of tinyMCE object
49
     **/
50
51
    public function &instance($config)
52
    {
53
        static $instance;
54
        if (!isset($instance)) {
55
            $instance = new TinyMCE($config);
56
        } else {
57
            $instance->setConfig($config);
58
        }
59
60
        return $instance;
61
    }
62
63
    public function setConfig($config)
64
    {
65
        foreach ($config as $key => $val) {
66
            $this->config[$key] = $val;
67
        }
68
    }
69
70
    /**
71
     * Initializes the tinyMCE
72
     * @return   true
73
     **/
74
75
    public function init()
76
    {
77
        // list of configured options
78
        $configured                = [];
79
        $this->setting['selector'] = 'textarea';
80
        $this->setting['theme']    = 'modern';
81
82
        // Load default settings
83
        if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) {
84
            $this->setting = include __DIR__ . '/settings.php';
85
        }
86
87
        // get editor language (from ...)
88
        if (is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) {
89
            $this->setting['language'] = $this->config['language'];
90
            $configured[]              = 'language';
91
        }
92
93
        $this->setting['content_css'] = implode(',', $this->loadCss());
94
        $configured[]                 = 'content_css';
95
96
        if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) {
97
            $this->setting['theme'] = $this->config['theme'];
98
            $configured[]           = 'theme';
99
        }
100
101
        if (!empty($this->config['mode'])) {
102
            $this->setting['mode'] = $this->config['mode'];
103
            $configured[]          = 'mode';
104
        }
105
106
        // load all plugins except the plugins in setting["exclude_plugins"]
107
        $this->setting['plugins'] = implode(',', $this->loadPlugins());
108
        $configured[]             = 'plugins';
109
110
        if ('simple' !== $this->setting['theme']) {
111
            if (empty($this->config['buttons'])) {
112
                $this->config['buttons'][] = [
113
                    'before' => '',
114
                    'add'    => '',
115
                ];
116
                $this->config['buttons'][] = [
117
                    'before' => '',
118
                    'add'    => '',
119
                ];
120
                $this->config['buttons'][] = [
121
                    'before' => '',
122
                    'add'    => '',
123
                ];
124
            }
125
            $i = 0;
126
            foreach ($this->config['buttons'] as $button) {
127
                ++$i;
128
                if (isset($button['before'])) {
129
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before'];
130
                }
131
                if (isset($button['add'])) {
132
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add'];
133
                }
134
                if (isset($button[''])) {
135
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button[''];
136
                }
137
            }
138
            $configured[] = 'buttons';
139
140
            if (isset($this->config['toolbar_location'])) {
141
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location'];
142
                $configured[]                                                            = 'toolbar_location';
143
            } else {
144
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top';
145
            }
146
147
            if (isset($this->config['toolbar_align'])) {
148
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align'];
149
                $configured[]                                                         = 'toolbar_align';
150
            } else {
151
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left';
152
            }
153
154
            if (isset($this->config['statusbar_location'])) {
155
                $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location'];
156
                $configured[]                                                              = 'statusbar_location';
157
            }
158
159
            if (isset($this->config['path_location'])) {
160
                $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location'];
161
                $configured[]                                                         = 'path_location';
162
            }
163
164
            if (isset($this->config['resize_horizontal'])) {
165
                $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal'];
166
                $configured[]                                                             = 'resize_horizontal';
167
            }
168
169
            if (isset($this->config['resizing'])) {
170
                $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing'];
171
                $configured[]                                                    = 'resizing';
172
            }
173
174
            if (!empty($this->config['fonts'])) {
175
                $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts'];
176
                $configured[]                                                 = 'fonts';
177
            }
178
179
            for ($i = 1; $i <= 4; ++$i) {
180
                $buttons = [];
181
                if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) {
182
                    $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]);
183
                    foreach ($checklist as $plugin) {
184
                        if (false != strpos(strtolower($plugin), 'xoops')) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos(strtolower($plugin), 'xoops') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
185
                            if (in_array($plugin, $this->xoopsPlugins)) {
186
                                $buttons[] = $plugin;
187
                            }
188
                        } else {
189
                            $buttons[] = $plugin;
190
                        }
191
                    }
192
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons);
193
                }
194
            }
195
        }
196
197
        $configured = array_unique($configured);
198
        foreach ($this->config as $key => $val) {
199
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
200
                continue;
201
            }
202
            $this->setting[$key] = $val;
203
        }
204
205
        if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) {
206
            $this->setting['docs_language'] = 'en';
207
        }
208
209
        unset($this->config, $configured);
210
211
        return true;
212
    }
213
214
    // load all plugins execpt the plugins in setting["exclude_plugins"]
215
    public function loadPlugins()
216
    {
217
        $plugins      = [];
218
        $plugins_list = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
219
        if (empty($this->setting['plugins'])) {
220
            $plugins = $plugins_list;
221
        } else {
222
            $plugins = array_intersect(explode(',', $this->setting['plugins']), $plugins_list);
223
        }
224
        if (!empty($this->setting['exclude_plugins'])) {
225
            $plugins = array_diff($plugins, explode(',', $this->setting['exclude_plugins']));
226
        }
227
        if (!empty($this->config['plugins'])) {
228
            $plugins = array_merge($plugins, $this->config['plugins']);
229
        }
230
231
        return $plugins;
232
    }
233
234
    // return all xoops plugins
235
    public function get_xoopsPlugins()
236
    {
237
        $xoopsPlugins = [];
238
        $allplugins   = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
239
        foreach ($allplugins as $plugin) {
240
            if (false != strpos(strtolower($plugin), 'xoops') && file_exists(XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php")) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos(strtolower($plugin), 'xoops') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
241
                if ($right = @include XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php") {
242
                    $xoopsPlugins[$plugin] = $plugin;
243
                }
244
            }
245
        }
246
247
        return $xoopsPlugins;
248
    }
249
250
    public function loadCss($css_file = 'style.css')
251
    {
252
        static $css_url, $css_path;
253
254
        if (!isset($css_url)) {
255
            $css_url  = dirname(xoops_getcss($GLOBALS['xoopsConfig']['theme_set']));
256
            $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
257
        }
258
259
        $css         = [];
260
        $css[]       = $css_url . '/' . $css_file;
261
        $css_content = file_get_contents($css_path . '/' . $css_file);
262
263
        // get all import css files
264
        if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) {
265
            foreach ($matches[1] as $key => $css_import) {
266
                $css = array_merge($css, $this->loadCss($css_import));
267
            }
268
        }
269
270
        return $css;
271
    }
272
273
    /**
274
     * Renders the tinyMCE
275
     * @return   string  $ret      The rendered HTML string
276
     **/
277
    public function render()
278
    {
279
        static $rendered;
280
        if ($rendered) {
281
            return null;
282
        }
283
284
        $rendered = true;
285
286
        $this->init();
287
288
        if (!empty($this->setting['callback'])) {
289
            $callback = $this->setting['callback'];
290
            unset($this->setting['callback']);
291
        } else {
292
            $callback = '';
293
        }
294
295
        $ret = '<script language="javascript" type="text/javascript" src="' . XOOPS_URL . $this->rootpath . '/tinymce.min.js"></script>';
296
        $ret .= '<script language="javascript" type="text/javascript">
297
                tinyMCE.init({
298
            ';
299
300
        foreach ($this->setting as $key => $val) {
301
            $ret .= $key . ':';
302
            if (true === $val) {
303
                $ret .= 'true,';
304
            } elseif (false === $val) {
305
                $ret .= 'false,';
306
            } else {
307
                $ret .= "'{$val}',";
308
            }
309
            $ret .= "\n";
310
        }
311
312
        //   Ajout alain01 tinymce v4
313
314
        $chemin_array  = parse_url(XOOPS_URL);
315
        $chemin_scheme = $chemin_array['scheme']; // http
0 ignored issues
show
Unused Code introduced by
The assignment to $chemin_scheme is dead and can be removed.
Loading history...
316
        $chemin_host   = $chemin_array['host']; // www.example.com  or // localhost
0 ignored issues
show
Unused Code introduced by
The assignment to $chemin_host is dead and can be removed.
Loading history...
317
        //  $chemin_path =  $chemin_array["path"]; // /myweb1
318
        if (!isset($chemin_array['path'])) {
319
            $chemin_path = '';
320
        } else {
321
            $chemin_path = $chemin_array['path'];
322
        }
323
324
        //   $ret .='language_url : "'.$chemin_path.'/class/xoopseditor/tinymce4/tinymce/js/tinymce/langs/fr_FR.js",';
325
326
        $ret .= 'external_plugins: {';
327
        $ret .= '"qrcode": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/qrcode/plugin.min.js",';
328
        $ret .= '"youtube": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/youtube/plugin.min.js",';
329
        $ret .= '"alignbtn": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/alignbtn/plugin.min.js",';
330
        $ret .= '"chartextbtn": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/chartextbtn/plugin.min.js",';
331
        $ret .= '"xoops_code": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/xoops_code/plugin.min.js",';
332
        $ret .= '"xoops_quote": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/xoops_quote/plugin.min.js",';
333
        $ret .= '"xoops_tagextgal": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/xoops_tagextgal/plugin.min.js",';
334
335
        $ret .= '"codemirror": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/codemirror/plugin.min.js",';
336
337
        $ret .= '"filemanager": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/filemanager/plugin.min.js",';
338
        $ret .= '"responsivefilemanager": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/responsivefilemanager/plugin.min.js",';
339
        $ret .= '},';
340
341
        $ret .= 'codemirror: {
342
    indentOnInit: true,
343
    path: "CodeMirror",
344
    config: {
345
       mode: "application/x-httpd-php",
346
       lineNumbers: false
347
    },
348
    jsFiles: [
349
       "mode/clike/clike.js",
350
       "mode/php/php.js"
351
    ]
352
  },';
353
354
        $ret .= "\n";
355
356
        $ret .= '"external_filemanager_path": "' . $chemin_path . '/class/xoopseditor/tinymce4/external_plugins/filemanager/",';
357
        $ret .= "\n";
358
359
        $ret .= 'templates: "' . $chemin_path . '/uploads/filemanager/templates/liste-templates.js",';
360
        $ret .= "\n";
361
        // fin ajout alain01
362
363
        $ret .= 'relative_urls : false,
364
                remove_script_host : false, tinymceload : "1"});
365
                ' . $callback . '
366
                function showMCE(id) {
367
                    if (tinyMCE.getInstanceById(id) == null) {
368
                        tinyMCE.execCommand("mceAddControl", false, id);
369
                    } else {
370
                        tinyMCE.execCommand("mceRemoveControl", false, id);
371
                    }
372
                }
373
                </script>
374
            ';
375
376
        return $ret;
377
    }
378
}
379