Passed
Branch master (f2d2e3)
by Michael
18:45
created

TinyMCE::init()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 136

Duplication

Lines 136
Ratio 100 %

Importance

Changes 0
Metric Value
cc 28
nc 24672
nop 0
dl 136
loc 136
rs 0
c 0
b 0
f 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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 *  TinyMCE adapter for XOOPS
14
 *
15
 * @copyright       (c) 2000-2014 XOOPS Project (www.xoops.org)
16
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
17
 * @package         class
18
 * @subpackage      editor
19
 * @since           2.3.0
20
 * @author          Taiwen Jiang <[email protected]>
21
 * @author          Lucio Rota <[email protected]>
22
 * @author          Laurent JEN <[email protected]>
23
 * @version         $Id $
24
 */
25
26
class TinyMCE
27
{
28
    var $rootpath;
29
    var $config = array();
30
    var $setting = array();
31
    static $LastOfElementsTinymce="";
32
    static $ListOfElementsTinymce=array();
33
34
    // PHP 5 Constructor
35
    /**
36
     * @param $config
37
     */
38
    function __construct($config)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
     {
40
        $this->setConfig($config);
41
        $this->rootpath = $this->config["rootpath"] . "/tinymce/jscripts/tiny_mce";
42
        $this->xoopsPlugins = $this->get_xoopsPlugins();
0 ignored issues
show
Bug Best Practice introduced by
The property xoopsPlugins does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        self::$LastOfElementsTinymce=$this->config["elements"];
44
           self::$ListOfElementsTinymce[]= self::$LastOfElementsTinymce;
45
    }
46
47
    // PHP 4 Contructor
48
    /**
49
     * @param $config
50
     */
51
    function TinyMCE($config)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
    {
53
        $this->__construct($config) ;
54
    }
55
56
    /**
57
     * @param $config
58
     *
59
     * @return TinyMCE
60
     */
61
    function &instance( $config )
62
    {
63
        static $instance;
64
        if (!isset($instance)) {
65
            $instance = new TinyMCE($config);
66
        } else {
67
            $instance->setConfig($config);
68
        }
69
70
        return $instance;
71
    }
72
73
    /**
74
     * @param $config
75
     */
76
    function setConfig( $config )
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
77
    {
78
        foreach ($config as $key => $val) {
79
            $this->config[$key] = $val;
80
        }
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    function init()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
    {
88
        // list of configured options
89
        $configured = array();
90
91
        // Load default settings
92
        if ( ! ($this->setting = @include( $GLOBALS['xoops']->path( "var/configs/tinymce.php" ) ) ) ) {
93
            $this->setting = include __DIR__ . "/settings.php";
94
        }
95
96
        // get editor language (from ...)
97
        if (isset($this->config["language"]) && is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config["language"] . '.js')) {
98
            $this->setting["language"] = $this->config["language"];
99
            $configured[] = "language";
100
        }
101
102
        $this->setting["content_css"] = implode( ",", $this->loadCss() );
103
        $configured[] = "content_css";
104
105
        if ( !empty($this->config["theme"]) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . "/themes/" . $this->config["theme"]) ) {
106
            $this->setting["theme"] = $this->config["theme"];
107
            $configured[] = "theme";
108
        }
109
110
        if (!empty($this->config["mode"])) {
111
            $this->setting["mode"] = $this->config["mode"];
112
            $configured[] = "mode";
113
        }
114
115
        // load all plugins except the plugins in setting["exclude_plugins"]
116
        $this->setting["plugins"] = implode(",", $this->loadPlugins());
117
        $configured[] = "plugins";
118
119
        if ($this->setting["theme"] != "simple") {
120
            if (empty($this->config["buttons"])) {
121
                $this->config["buttons"][] = array(
122
                    "before"    => "",
123
                    "add"       => "",
124
                    );
125
                $this->config["buttons"][] = array(
126
                    "before"    => "",
127
                    "add"       => "",
128
                    );
129
                $this->config["buttons"][] = array(
130
                    "before"    => "",
131
                    "add"       => "",
132
                    );
133
            }
134
            $i = 0;
135
            foreach ($this->config["buttons"] as $button) {
136
                $i++;
137
                if (isset($button["before"])) {
138
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add_before"] = $button["before"];
139
                }
140
                if (isset($button["add"])) {
141
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add"] = $button["add"];
142
                }
143
                if (isset($button[""])) {
144
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = $button[""];
145
                }
146
            }
147
            $configured[] = "buttons";
148
149
            if (isset($this->config["toolbar_location"])) {
150
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = $this->config["toolbar_location"];
151
                $configured[] = "toolbar_location";
152
            } else {
153
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = "top";
154
            }
155
156
            if (isset($this->config["toolbar_align"])) {
157
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = $this->config["toolbar_align"];
158
                $configured[] = "toolbar_align";
159
            } else {
160
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = "left";
161
            }
162
163
            if (isset($this->config["statusbar_location"])) {
164
                $this->setting["theme_" . $this->setting["theme"] . "_statusbar_location"] = $this->config["statusbar_location"];
165
                $configured[] = "statusbar_location";
166
            }
167
168
            if (isset($this->config["path_location"])) {
169
                $this->setting["theme_" . $this->setting["theme"] . "_path_location"] = $this->config["path_location"];
170
                $configured[] = "path_location";
171
            }
172
173
            if (isset($this->config["resize_horizontal"])) {
174
                $this->setting["theme_" . $this->setting["theme"] . "_resize_horizontal"] = $this->config["resize_horizontal"];
175
                $configured[] = "resize_horizontal";
176
            }
177
178
            if (isset($this->config["resizing"])) {
179
                $this->setting["theme_" . $this->setting["theme"] . "_resizing"] = $this->config["resizing"];
180
                $configured[] = "resizing";
181
            }
182
183
            if (!empty($this->config["fonts"])) {
184
                $this->setting["theme_" . $this->setting["theme"] . "_fonts"] = $this->config["fonts"];
185
                $configured[] = "fonts";
186
            }
187
188
            for ($i=1 ; $i <= 4 ; $i++) {
189
                $buttons = array();
190
                if ( isset($this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"]) ) {
191
                    $checklist = explode(",", $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] );
192
                    foreach ($checklist as $plugin) {
193
                        if ( strpos( strtolower($plugin), "xoops") != false ) {
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...
194
                            if ( in_array( $plugin, $this->xoopsPlugins ) ) {
195
                                $buttons[] = $plugin;
196
                            }
197
                        } else {
198
                            $buttons[] = $plugin;
199
                        }
200
                    }
201
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = implode(",", $buttons);
202
                }
203
            }
204
        }
205
206
        $configured = array_unique($configured);
207
        foreach ($this->config as $key => $val) {
208
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
209
                continue;
210
            }
211
            $this->setting[$key] = $val;
212
        }
213
214
        if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . "/themes/" . $this->setting["theme"] . '/docs/' . $this->setting["language"] . '/')) {
215
            $this->setting["docs_language"] = "en";
216
        }
217
218
        unset($this->config, $configured);
219
220
        return true;
221
    }
222
223
    // load all plugins execpt the plugins in setting["exclude_plugins"]
224
    /**
225
     * @return array
226
     */
227
    function loadPlugins()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
228
    {
229
        $plugins = array();
230
        $plugins_list = XoopsLists::getDirListAsArray( XOOPS_ROOT_PATH . $this->rootpath . "/plugins" );
231
        if (empty($this->setting["plugins"])) {
232
            $plugins = $plugins_list;
233
        } else {
234
            $plugins = array_intersect(explode(",", $this->setting["plugins"]), $plugins_list);
235
        }
236
        if (!empty($this->setting["exclude_plugins"])) {
237
            $plugins = array_diff($plugins, explode(",", $this->setting["exclude_plugins"]));
238
        }
239
        if (!empty($this->config["plugins"])) {
240
            $plugins = array_merge($plugins, $this->config["plugins"]);
241
        }
242
243
        return $plugins;
244
    }
245
246
    // return all xoops plugins
247
    /**
248
     * @return array
249
     */
250
    function get_xoopsPlugins()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
251
    {
252
        $xoopsPlugins = array();
253
        $allplugins = XoopsLists::getDirListAsArray( XOOPS_ROOT_PATH . $this->rootpath . "/plugins" );
254
        foreach ($allplugins as $plugin) {
255
            if ( strpos( strtolower($plugin), "xoops") != false && 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...
256
                if ($right = @include XOOPS_ROOT_PATH . $this->config["rootpath"] . "/include/$plugin.php") {
257
                    $xoopsPlugins[$plugin] = $plugin;
258
                }
259
            }
260
        }
261
262
        return $xoopsPlugins;
263
    }
264
265
    /**
266
     * @param string $css_file
267
     *
268
     * @return array
269
     */
270
    function loadCss($css_file = 'style.css')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
271
    {
272
        static $css_url, $css_path;
273
274
        if (!isset($css_url)) {
275
            $css_url = dirname( xoops_getcss($GLOBALS['xoopsConfig']['theme_set']) );
276
            $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
277
        }
278
279
        $css = array();
280
        $css[] = $css_url . '/' . $css_file;
281
        $css_content = file_get_contents( $css_path . '/' . $css_file );
282
283
        // get all import css files
284
        if ( preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER) ) {
285
            foreach ($matches[1] as $key => $css_import) {
286
                $css = array_merge( $css, $this->loadCss( $css_import) );
287
            }
288
        }
289
290
        return $css;
291
    }
292
293
    /**
294
     * @return string
295
     */
296
    function render()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
297
    {
298
        static $isTinyMceJsLoaded = false;
299
300
        $this->init();
301
        if (isset($this->setting["elements"]) && self::$LastOfElementsTinymce!=$this->setting["elements"]) {
302
                $ret = "\n<!-- 'tiny_mce.js' SCRIPT NOT YET ".$this->setting["elements"]." -->\n"; //debug
303
304
                return $ret;
305
               } else {
306
            $this->setting["elements"]=implode(",",self::$ListOfElementsTinymce);
307
            }
308
        if ( !empty($this->setting["callback"]) ) {
309
            $callback = $this->setting["callback"];
310
            unset($this->setting["callback"]);
311
        } else {
312
            $callback = "";
313
        }
314
        if ( !empty($this->setting["file_browser_callback"]) ) {
315
            $fbc_name=XOOPS_ROOT_PATH . "/class/xoopseditor/tinymce/include/".$this->setting["file_browser_callback"].".js";
316
            //suis passé la /lesrevespossibles/x244/class/xoopseditor/tinymce/tinymce/jscripts/include/openFinder.js
317
            $file_browser_callback ="MyXoopsUrl ='".XOOPS_URL."';\n";
318
            $file_browser_callback .= file_get_contents($fbc_name);
319
             $file_browser_callback .="\n//suis passé la ".$fbc_name;
320
            //unset($this->setting["file_browser_callback"]);
321
        } else {
322
            $file_browser_callback = "//suis absent";
323
        }
324
325
        // create returned string - start
326
        $ret = "\n";
327
328
/*
329
// IE BUG - start
330
// more info here:
331
// http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
332
// possible solution here:
333
// http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
334
        $ret =<<<EOF
335
<script language='javascript' type='text/javascript'>
336
    if (/msie/i.test (navigator.userAgent)) { //only override IE
337
        document.nativeGetElementById = document.getElementById;
338
        document.getElementById = function(id) {
339
            var elem = document.nativeGetElementById(id);
340
            if (elem) {
341
                //make sure that it is a valid match on id
342
                if (elem.attributes['id'].value == id) {
343
                    return elem;
344
                } else {
345
                    //otherwise find the correct element
346
                    for (var i=1;i<document.all[id].length;i++) {
347
                        if (document.all[id][i].attributes['id'].value == id) {
348
                            return document.all[id][i];
349
                        }
350
                    }
351
                }
352
            }
353
354
            return null;
355
        };
356
    }
357
</script>
358
\n
359
EOF;
360
// IE BUG - end
361
*/
362
363
        $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug
364
        if ($isTinyMceJsLoaded) {
365
            $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug
366
        } else {
367
            $ret .= "<script type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/tiny_mce.js'></script>\n";
368
            $isTinyMceJsLoaded = true;
369
        }
370
        $ret .= "<script type='text/javascript'>\n";
371
        $ret .= "tinyMCE.init({\n";
372
        // set options - start
373
        foreach ($this->setting as $key => $val) {
374
            $ret .= $key . ":";
375
            if ($val === true) {
376
                $ret.= "true,";
377
            } elseif ($val === false) {
378
                $ret .= "false,";
379
            } else {
380
                $ret .= "'{$val}',";
381
            }
382
            $ret .= "\n";
383
        }
384
        // set options - end
385
        $ret .= "tinymceload: true\n";
386
        $ret .= "});\n";
387
        $ret .= $callback . "\n";
388
    $ret .= $file_browser_callback. "\n";
389
        //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n";
390
        $ret .= "</script>\n";
391
        $ret .= "<!-- End TinyMce Rendering -->\n";//debug
392
        // create returned string - end
393
        return $ret ;
394
    }
395
}
396