Completed
Pull Request — master (#77)
by Michael
11:21
created

TinyMCE::setConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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-2016 XOOPS Project (www.xoops.org)
16
 * @license             GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
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
 */
24
class TinyMCE
25
{
26
    public        $rootpath;
27
    public        $config                = array();
28
    public        $setting               = array();
29
    public static $LastOfElementsTinymce = '';
30
    public static $ListOfElementsTinymce = array();
31
32
    // PHP 5 Constructor
33
    /**
34
     * @param $config
35
     */
36
    public function __construct($config)
37
    {
38
        $this->setConfig($config);
39
        $this->rootpath                = $this->config['rootpath'] . '/tinymce/jscripts/tiny_mce';
40
        $this->xoopsPlugins            = $this->get_xoopsPlugins();
0 ignored issues
show
Bug introduced by
The property xoopsPlugins does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
        self::$LastOfElementsTinymce   = $this->config['elements'];
42
        self::$ListOfElementsTinymce[] = self::$LastOfElementsTinymce;
43
    }
44
45
    /**
46
     * Deprecated, use getInstance() instead
47
     */
48
    public function instance($config)
49
    {
50
        return TinyMCE::getInstance($config);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
51
    }    
52
    
53
    /**
54
     * @param $config
55
     *
56
     * @return TinyMCE
57
     */
58
    public static function getInstance($config)
59
    {
60
        static $instance;
61
        if (!isset($instance)) {
62
            $instance = new TinyMCE($config);
63
        } else {
64
            $instance->setConfig($config);
65
        }
66
        return $instance;
67
    }
68
69
    /**
70
     * @param $config
71
     */
72
    public function setConfig($config)
73
    {
74
        foreach ($config as $key => $val) {
75
            $this->config[$key] = $val;
76
        }
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function init()
83
    {
84
        // list of configured options
85
        $configured = array();
86
87
        // Load default settings
88
        if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) {
89
            $this->setting = include __DIR__ . '/settings.php';
90
        }
91
92
        // get editor language (from ...)
93 View Code Duplication
        if (isset($this->config['language']) && is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) {
94
            $this->setting['language'] = $this->config['language'];
95
            $configured[]              = 'language';
96
        }
97
98
        $this->setting['content_css'] = implode(',', $this->loadCss());
99
        $configured[]                 = 'content_css';
100
101 View Code Duplication
        if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) {
102
            $this->setting['theme'] = $this->config['theme'];
103
            $configured[]           = 'theme';
104
        }
105
106
        if (!empty($this->config['mode'])) {
107
            $this->setting['mode'] = $this->config['mode'];
108
            $configured[]          = 'mode';
109
        }
110
111
        // load all plugins except the plugins in setting["exclude_plugins"]
112
        $this->setting['plugins'] = implode(',', $this->loadPlugins());
113
        $configured[]             = 'plugins';
114
115
        if ($this->setting['theme'] !== 'simple') {
116
            if (empty($this->config['buttons'])) {
117
                $this->config['buttons'][] = array(
118
                    'before' => '',
119
                    'add'    => ''
120
                );
121
                $this->config['buttons'][] = array(
122
                    'before' => '',
123
                    'add'    => ''
124
                );
125
                $this->config['buttons'][] = array(
126
                    'before' => '',
127
                    'add'    => ''
128
                );
129
            }
130
            $i = 0;
131
            foreach ($this->config['buttons'] as $button) {
132
                ++$i;
133 View Code Duplication
                if (isset($button['before'])) {
134
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before'];
135
                }
136 View Code Duplication
                if (isset($button['add'])) {
137
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add'];
138
                }
139 View Code Duplication
                if (isset($button[''])) {
140
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button[''];
141
                }
142
            }
143
            $configured[] = 'buttons';
144
145 View Code Duplication
            if (isset($this->config['toolbar_location'])) {
146
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location'];
147
                $configured[]                                                            = 'toolbar_location';
148
            } else {
149
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top';
150
            }
151
152 View Code Duplication
            if (isset($this->config['toolbar_align'])) {
153
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align'];
154
                $configured[]                                                         = 'toolbar_align';
155
            } else {
156
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left';
157
            }
158
159 View Code Duplication
            if (isset($this->config['statusbar_location'])) {
160
                $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location'];
161
                $configured[]                                                              = 'statusbar_location';
162
            }
163
164 View Code Duplication
            if (isset($this->config['path_location'])) {
165
                $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location'];
166
                $configured[]                                                         = 'path_location';
167
            }
168
169 View Code Duplication
            if (isset($this->config['resize_horizontal'])) {
170
                $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal'];
171
                $configured[]                                                             = 'resize_horizontal';
172
            }
173
174 View Code Duplication
            if (isset($this->config['resizing'])) {
175
                $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing'];
176
                $configured[]                                                    = 'resizing';
177
            }
178
179
            if (!empty($this->config['fonts'])) {
180
                $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts'];
181
                $configured[]                                                 = 'fonts';
182
            }
183
184
            for ($i = 1; $i <= 4; ++$i) {
185
                $buttons = array();
186
                if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) {
187
                    $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]);
188
                    foreach ($checklist as $plugin) {
189
                        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...
190
                            if (in_array($plugin, $this->xoopsPlugins)) {
191
                                $buttons[] = $plugin;
192
                            }
193
                        } else {
194
                            $buttons[] = $plugin;
195
                        }
196
                    }
197
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons);
198
                }
199
            }
200
        }
201
202
        $configured = array_unique($configured);
203
        foreach ($this->config as $key => $val) {
204
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
205
                continue;
206
            }
207
            $this->setting[$key] = $val;
208
        }
209
210
        if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) {
211
            $this->setting['docs_language'] = 'en';
212
        }
213
214
        unset($this->config, $configured);
215
216
        return true;
217
    }
218
219
    // load all plugins execpt the plugins in setting["exclude_plugins"]
220
    /**
221
     * @return array
222
     */
223
    public function loadPlugins()
224
    {
225
        $plugins      = array();
0 ignored issues
show
Unused Code introduced by
$plugins is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
226
        $plugins_list = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
227
        if (empty($this->setting['plugins'])) {
228
            $plugins = $plugins_list;
229
        } else {
230
            $plugins = array_intersect(explode(',', $this->setting['plugins']), $plugins_list);
231
        }
232
        if (!empty($this->setting['exclude_plugins'])) {
233
            $plugins = array_diff($plugins, explode(',', $this->setting['exclude_plugins']));
234
        }
235
        if (!empty($this->config['plugins'])) {
236
            $plugins = array_merge($plugins, $this->config['plugins']);
237
        }
238
239
        return $plugins;
240
    }
241
242
    // return all xoops plugins
243
    /**
244
     * @return array
245
     */
246
    public function get_xoopsPlugins()
247
    {
248
        $xoopsPlugins = array();
249
        $allplugins   = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
250
        foreach ($allplugins as $plugin) {
251
            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...
252
                if ($right = @include XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php") {
253
                    $xoopsPlugins[$plugin] = $plugin;
254
                }
255
            }
256
        }
257
258
        return $xoopsPlugins;
259
    }
260
261
    /**
262
     * @param string $css_file
263
     *
264
     * @return array
265
     */
266
    public function loadCss($css_file = 'style.css')
267
    {
268
        static $css_url, $css_path;
269
270
        if (!isset($css_url)) {
271
            $css_url  = dirname(xoops_getcss($GLOBALS['xoopsConfig']['theme_set']));
272
            $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
273
        }
274
275
        $css         = array();
276
        $css[]       = $css_url . '/' . $css_file;
277
        $css_content = file_get_contents($css_path . '/' . $css_file);
278
279
        // get all import css files
280
        if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) {
281
            foreach ($matches[1] as $key => $css_import) {
282
                $css = array_merge($css, $this->loadCss($css_import));
283
            }
284
        }
285
286
        return $css;
287
    }
288
289
    /**
290
     * @return string
291
     */
292
    public function render()
293
    {
294
        static $isTinyMceJsLoaded = false;
295
296
        $this->init();
297
        if (isset($this->setting['elements']) && self::$LastOfElementsTinymce != $this->setting['elements']) {
298
            $ret = "\n<!-- 'tiny_mce.js' SCRIPT NOT YET " . $this->setting['elements'] . " -->\n"; //debug
299
300
            return $ret;
301
        } else {
302
            $this->setting['elements'] = implode(',', self::$ListOfElementsTinymce);
303
        }
304
        if (!empty($this->setting['callback'])) {
305
            $callback = $this->setting['callback'];
306
            unset($this->setting['callback']);
307
        } else {
308
            $callback = '';
309
        }
310
        if (!empty($this->setting['file_browser_callback'])) {
311
            $fbc_name = XOOPS_ROOT_PATH . '/class/xoopseditor/tinymce/include/' . $this->setting['file_browser_callback'] . '.js';
312
            //suis passé la /lesrevespossibles/x244/class/xoopseditor/tinymce/tinymce/jscripts/include/openFinder.js
313
            $file_browser_callback = "MyXoopsUrl ='" . XOOPS_URL . "';\n";
314
            $file_browser_callback .= file_get_contents($fbc_name);
315
            $file_browser_callback .= "\n//suis passé la " . $fbc_name;
316
            //unset($this->setting["file_browser_callback"]);
317
        } else {
318
            $file_browser_callback = '//suis absent';
319
        }
320
321
        // create returned string - start
322
        $ret = "\n";
323
324
        /*
325
        // IE BUG - start
326
        // more info here:
327
        // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
328
        // possible solution here:
329
        // http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
330
                $ret =<<<EOF
331
        <script language='javascript' type='text/javascript'>
332
            if (/msie/i.test (navigator.userAgent)) { //only override IE
333
                document.nativeGetElementById = document.getElementById;
334
                document.getElementById = function(id) {
335
                    var elem = document.nativeGetElementById(id);
336
                    if (elem) {
337
                        //make sure that it is a valid match on id
338
                        if (elem.attributes['id'].value == id) {
339
                            return elem;
340
                        } else {
341
                            //otherwise find the correct element
342
                            for (var i=1;i<document.all[id].length;i++) {
343
                                if (document.all[id][i].attributes['id'].value == id) {
344
                                    return document.all[id][i];
345
                                }
346
                            }
347
                        }
348
                    }
349
350
                    return null;
351
                };
352
            }
353
        </script>
354
        \n
355
        EOF;
356
        // IE BUG - end
357
        */
358
359
        $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug
360
        if ($isTinyMceJsLoaded) {
361
            $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug
362
        } else {
363
            $ret .= "<script type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/tiny_mce.js'></script>\n";
364
            $isTinyMceJsLoaded = true;
365
        }
366
        $ret .= "<script type='text/javascript'>\n";
367
        $ret .= "tinyMCE.init({\n";
368
        // set options - start
369
        foreach ($this->setting as $key => $val) {
370
            $ret .= $key . ':';
371
            if ($val === true) {
372
                $ret .= 'true,';
373
            } elseif ($val === false) {
374
                $ret .= 'false,';
375
            } else {
376
                $ret .= "'{$val}',";
377
            }
378
            $ret .= "\n";
379
        }
380
        // set options - end
381
        $ret .= "tinymceload: true\n";
382
        $ret .= "});\n";
383
        $ret .= $callback . "\n";
384
        $ret .= $file_browser_callback . "\n";
385
        //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n";
386
        $ret .= "</script>\n";
387
        $ret .= "<!-- End TinyMce Rendering -->\n";//debug
388
        // create returned string - end
389
        return $ret;
390
    }
391
}
392