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

TinyMCEJQ::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 jQuery adapter for XOOPS
14
 *
15
 * @copyright       XOOPS Project (https://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 TinyMCEJQ
27
{
28
    var $rootpath;
29
    var $config = array();
30
    var $setting = array();
31
32
    // PHP 5 Constructor
33
    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...
34
     {
35
        $this->setConfig($config);
36
        $this->rootpath = $this->config["rootpath"] . "/tiny_mce";
37
        $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...
38
    }
39
40
    // PHP 4 Contructor
41
    function TinyMCEJQ($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...
42
    {
43
        $this->__construct($config) ;
44
    }
45
46
    function &instance( $config )
47
    {
48
        static $instance;
49
        if (!isset($instance)) {
50
            $instance = new TinyMCEJQ($config);
51
        } else {
52
            $instance->setConfig($config);
53
        }
54
        return $instance;
55
    }
56
57
    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...
58
    {
59
        foreach ($config as $key => $val) {
60
            $this->config[$key] = $val;
61
        }
62
    }
63
64
    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...
65
    {
66
        // list of configured options
67
        $configured = array();
68
69
        // Load default settings
70
        if ( ! ($this->setting = @include( $GLOBALS['xoops']->path( "var/configs/tinymce.php" ) ) ) ) {
71
            $this->setting = include dirname(__FILE__) . "/settings.php";
72
        }
73
74
        // get editor language (from ...)
75
        if (is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config["language"] . '.js')) {
76
            $this->setting["language"] = $this->config["language"];
77
            $configured[] = "language";
78
        }
79
80
        $this->setting["content_css"] = implode( ",", $this->loadCss() );
81
        $configured[] = "content_css";
82
83
        if ( !empty($this->config["theme"]) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . "/themes/" . $this->config["theme"]) ) {
84
            $this->setting["theme"] = $this->config["theme"];
85
            $configured[] = "theme";
86
        }
87
88
        if (!empty($this->config["mode"])) {
89
            $this->setting["mode"] = $this->config["mode"];
90
            $configured[] = "mode";
91
        }
92
93
        // load all plugins except the plugins in setting["exclude_plugins"]
94
        $this->setting["plugins"] = implode(",", $this->loadPlugins());
95
        $configured[] = "plugins";
96
97
        if ( $this->setting["theme"] != "simple" ) {
98
            if (empty($this->config["buttons"])) {
99
                $this->config["buttons"][] = array(
100
                    "before"    => "",
101
                    "add"       => "",
102
                    );
103
                $this->config["buttons"][] = array(
104
                    "before"    => "",
105
                    "add"       => "",
106
                    );
107
                $this->config["buttons"][] = array(
108
                    "before"    => "",
109
                    "add"       => "",
110
                    );
111
            }
112
            $i = 0;
113
            foreach ($this->config["buttons"] as $button) {
114
                $i++;
115
                if (isset($button["before"])) {
116
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add_before"] = $button["before"];
117
                }
118
                if (isset($button["add"])) {
119
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add"] = $button["add"];
120
                }
121
                if (isset($button[""])) {
122
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = $button[""];
123
                }
124
            }
125
            $configured[] = "buttons";
126
127
            if (isset($this->config["toolbar_location"])) {
128
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = $this->config["toolbar_location"];
129
                $configured[] = "toolbar_location";
130
            } else {
131
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = "top";
132
            }
133
134
            if (isset($this->config["toolbar_align"])) {
135
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = $this->config["toolbar_align"];
136
                $configured[] = "toolbar_align";
137
            } else {
138
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = "left";
139
            }
140
141
            if (isset($this->config["statusbar_location"])) {
142
                $this->setting["theme_" . $this->setting["theme"] . "_statusbar_location"] = $this->config["statusbar_location"];
143
                $configured[] = "statusbar_location";
144
            }
145
146
            if (isset($this->config["path_location"])) {
147
                $this->setting["theme_" . $this->setting["theme"] . "_path_location"] = $this->config["path_location"];
148
                $configured[] = "path_location";
149
            }
150
151
            if (isset($this->config["resize_horizontal"])) {
152
                $this->setting["theme_" . $this->setting["theme"] . "_resize_horizontal"] = $this->config["resize_horizontal"];
153
                $configured[] = "resize_horizontal";
154
            }
155
156
            if (isset($this->config["resizing"])) {
157
                $this->setting["theme_" . $this->setting["theme"] . "_resizing"] = $this->config["resizing"];
158
                $configured[] = "resizing";
159
            }
160
161
            if (!empty($this->config["fonts"])) {
162
                $this->setting["theme_" . $this->setting["theme"] . "_fonts"] = $this->config["fonts"];
163
                $configured[] = "fonts";
164
            }
165
166
            for ($i=1 ; $i <= 4 ; $i++ ) {
167
                $buttons = array();
168
                if ( isset($this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"]) ) {
169
                    $checklist = explode(",", $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] );
170
                    foreach ( $checklist as $plugin ) {
171
                        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...
172
                            if ( in_array( $plugin, $this->xoopsPlugins ) ) {
173
                                $buttons[] = $plugin;
174
                            }
175
                        } else {
176
                            $buttons[] = $plugin;
177
                        }
178
                    }
179
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = implode(",", $buttons);
180
                }
181
            }
182
        }
183
184
        $configured = array_unique($configured);
185
        foreach ($this->config as $key => $val) {
186
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
187
                continue;
188
            }
189
            $this->setting[$key] = $val;
190
        }
191
192
        if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . "/themes/" . $this->setting["theme"] . '/docs/' . $this->setting["language"] . '/')) {
193
            $this->setting["docs_language"] = "en";
194
        }
195
196
        unset($this->config, $configured);
197
198
        return true;
199
    }
200
201
    // load all plugins execpt the plugins in setting["exclude_plugins"]
202
    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...
203
    {
204
        $plugins = array();
205
        $plugins_list = XoopsLists::getDirListAsArray( XOOPS_ROOT_PATH . $this->rootpath . "/plugins" );
206
        if (empty($this->setting["plugins"])) {
207
            $plugins = $plugins_list;
208
        } else {
209
            $plugins = array_intersect(explode(",", $this->setting["plugins"]), $plugins_list);
210
        }
211
        if (!empty($this->setting["exclude_plugins"])) {
212
            $plugins = array_diff($plugins, explode(",", $this->setting["exclude_plugins"]));
213
        }
214
        if (!empty($this->config["plugins"])) {
215
            $plugins = array_merge($plugins, $this->config["plugins"]);
216
        }
217
        return $plugins;
218
    }
219
220
    // return all xoops plugins
221
    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...
222
        $xoopsPlugins = array();
223
        $allplugins = XoopsLists::getDirListAsArray( XOOPS_ROOT_PATH . $this->rootpath . "/plugins" );
224
        foreach ( $allplugins as $plugin ) {
225
            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...
226
                if ( $right = @include XOOPS_ROOT_PATH . $this->config["rootpath"] . "/include/$plugin.php" ) {
227
                    $xoopsPlugins[$plugin] = $plugin;
228
                }
229
            }
230
        }
231
        return $xoopsPlugins;
232
    }
233
234
    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...
235
    {
236
        static $css_url, $css_path;
237
238
        if (!isset($css_url)) {
239
            $css_url = dirname( xoops_getcss($GLOBALS['xoopsConfig']['theme_set']) );
240
            $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
241
        }
242
243
        $css = array();
244
        $css[] = $css_url . '/' . $css_file;
245
        $css_content = file_get_contents( $css_path . '/' . $css_file );
246
247
        // get all import css files
248
        if ( preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER) ) {
249
            foreach( $matches[1] as $key => $css_import ) {
250
                $css = array_merge( $css, $this->loadCss( $css_import) );
251
            }
252
        }
253
        return $css;
254
    }
255
256
    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...
257
    {
258
        static $isTinyMceJsLoaded = false;
259
260
        $this->init();
261
262
        if ( !empty($this->setting["callback"]) ) {
263
            $callback = $this->setting["callback"];
264
            unset($this->setting["callback"]);
265
        } else {
266
            $callback = "";
267
        }
268
269
        // create returned string - start
270
        $ret = "\n";
271
272
/*
273
// IE BUG - start
274
// more info here:
275
// http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
276
// possible solution here:
277
// http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
278
        $ret =<<<EOF
279
<script language='javascript' type='text/javascript'>
280
    if (/msie/i.test (navigator.userAgent)) //only override IE
281
        {
282
        document.nativeGetElementById = document.getElementById;
283
        document.getElementById = function(id) {
284
            var elem = document.nativeGetElementById(id);
285
            if(elem) {
286
                //make sure that it is a valid match on id
287
                if(elem.attributes['id'].value == id)    {
288
                    return elem;
289
                } else {
290
                    //otherwise find the correct element
291
                    for(var i=1;i<document.all[id].length;i++) {
292
                        if(document.all[id][i].attributes['id'].value == id) {
293
                            return document.all[id][i];
294
                        }
295
                    }
296
                }
297
            }
298
            return null;
299
        };
300
    }
301
</script>
302
\n
303
EOF;
304
// IE BUG - end
305
*/
306
307
        $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug
308
		$ret .= "<script language='javascript' type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/jquery.tinymce.js'></script>\n";
309
        if ($isTinyMceJsLoaded) {
310
            $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug
311
        } else {
312
            $ret .= "<script language='javascript' type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/tiny_mce.js'></script>\n";
313
            $isTinyMceJsLoaded = true;
314
        }
315
        $ret .= "<script language='javascript' type='text/javascript'>\n";
316
        $ret .= "tinyMCE.init({\n";
317
        // set options - start
318
        foreach ($this->setting as $key => $val) {
319
            $ret .= $key . ":";
320
            if ($val === true) {
321
                $ret.= "true,";
322
            } elseif ($val === false) {
323
                $ret .= "false,";
324
            } else {
325
                $ret .= "'{$val}',";
326
            }
327
            $ret .= "\n";
328
        }
329
        // set options - end
330
        $ret .= "tinymceload: true\n";
331
        $ret .= "});\n";
332
        $ret .= $callback . "\n";
333
        //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n";
334
        $ret .= "</script>\n";
335
        $ret .= "<!-- End TinyMce Rendering -->\n";//debug
336
        // create returned string - end
337
        return $ret ;
338
    }
339
}
340
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
341