Completed
Pull Request — master (#133)
by Goffy
16:45
created

TinyMCE::loadCss()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
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
     * @param $config
47
     *
48
     * @return TinyMCE
49
     */
50
    public function &instance($config)
51
    {
52
        static $instance;
53
        if (!isset($instance)) {
54
            $instance = new TinyMCE($config);
55
        } else {
56
            $instance->setConfig($config);
57
        }
58
59
        return $instance;
60
    }
61
62
    /**
63
     * @param $config
64
     */
65
    public function setConfig($config)
66
    {
67
        foreach ($config as $key => $val) {
68
            $this->config[$key] = $val;
69
        }
70
    }
71
72
    /**
73
     * @return bool
74
     */
75
    public function init()
76
    {
77
        // list of configured options
78
        $configured = array();
79
80
        // Load default settings
81
        if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) {
82
            $this->setting = include __DIR__ . '/settings.php';
83
        }
84
85
        // get editor language (from ...)
86 View Code Duplication
        if (isset($this->config['language']) && is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) {
87
            $this->setting['language'] = $this->config['language'];
88
            $configured[]              = 'language';
89
        }
90
91
        $this->setting['content_css'] = implode(',', $this->loadCss());
92
        $configured[]                 = 'content_css';
93
94 View Code Duplication
        if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) {
95
            $this->setting['theme'] = $this->config['theme'];
96
            $configured[]           = 'theme';
97
        }
98
99
        if (!empty($this->config['mode'])) {
100
            $this->setting['mode'] = $this->config['mode'];
101
            $configured[]          = 'mode';
102
        }
103
104
        // load all plugins except the plugins in setting["exclude_plugins"]
105
        $this->setting['plugins'] = implode(',', $this->loadPlugins());
106
        $configured[]             = 'plugins';
107
108
        if ($this->setting['theme'] !== 'simple') {
109
            if (empty($this->config['buttons'])) {
110
                $this->config['buttons'][] = array(
111
                    'before' => '',
112
                    'add' => '');
113
                $this->config['buttons'][] = array(
114
                    'before' => '',
115
                    'add' => '');
116
                $this->config['buttons'][] = array(
117
                    'before' => '',
118
                    'add' => '');
119
            }
120
            $i = 0;
121
            foreach ($this->config['buttons'] as $button) {
122
                ++$i;
123 View Code Duplication
                if (isset($button['before'])) {
124
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before'];
125
                }
126 View Code Duplication
                if (isset($button['add'])) {
127
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add'];
128
                }
129 View Code Duplication
                if (isset($button[''])) {
130
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button[''];
131
                }
132
            }
133
            $configured[] = 'buttons';
134
135 View Code Duplication
            if (isset($this->config['toolbar_location'])) {
136
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location'];
137
                $configured[]                                                            = 'toolbar_location';
138
            } else {
139
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top';
140
            }
141
142 View Code Duplication
            if (isset($this->config['toolbar_align'])) {
143
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align'];
144
                $configured[]                                                         = 'toolbar_align';
145
            } else {
146
                $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left';
147
            }
148
149 View Code Duplication
            if (isset($this->config['statusbar_location'])) {
150
                $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location'];
151
                $configured[]                                                              = 'statusbar_location';
152
            }
153
154 View Code Duplication
            if (isset($this->config['path_location'])) {
155
                $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location'];
156
                $configured[]                                                         = 'path_location';
157
            }
158
159 View Code Duplication
            if (isset($this->config['resize_horizontal'])) {
160
                $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal'];
161
                $configured[]                                                             = 'resize_horizontal';
162
            }
163
164 View Code Duplication
            if (isset($this->config['resizing'])) {
165
                $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing'];
166
                $configured[]                                                    = 'resizing';
167
            }
168
169
            if (!empty($this->config['fonts'])) {
170
                $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts'];
171
                $configured[]                                                 = 'fonts';
172
            }
173
174
            for ($i = 1; $i <= 4; ++$i) {
175
                $buttons = array();
176
                if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) {
177
                    $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]);
178
                    foreach ($checklist as $plugin) {
179
                        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...
180
                            if (in_array($plugin, $this->xoopsPlugins)) {
181
                                $buttons[] = $plugin;
182
                            }
183
                        } else {
184
                            $buttons[] = $plugin;
185
                        }
186
                    }
187
                    $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons);
188
                }
189
            }
190
        }
191
192
        $configured = array_unique($configured);
193
        foreach ($this->config as $key => $val) {
194
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
195
                continue;
196
            }
197
            $this->setting[$key] = $val;
198
        }
199
200
        if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) {
201
            $this->setting['docs_language'] = 'en';
202
        }
203
204
        unset($this->config, $configured);
205
206
        return true;
207
    }
208
209
    // load all plugins execpt the plugins in setting["exclude_plugins"]
210
    /**
211
     * @return array
212
     */
213
    public function loadPlugins()
214
    {
215
        $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...
216
        $plugins_list = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
217
        if (empty($this->setting['plugins'])) {
218
            $plugins = $plugins_list;
219
        } else {
220
            $plugins = array_intersect(explode(',', $this->setting['plugins']), $plugins_list);
221
        }
222
        if (!empty($this->setting['exclude_plugins'])) {
223
            $plugins = array_diff($plugins, explode(',', $this->setting['exclude_plugins']));
224
        }
225
        if (!empty($this->config['plugins'])) {
226
            $plugins = array_merge($plugins, $this->config['plugins']);
227
        }
228
229
        return $plugins;
230
    }
231
232
    // return all xoops plugins
233
    /**
234
     * @return array
235
     */
236
    public function get_xoopsPlugins()
237
    {
238
        $xoopsPlugins = array();
239
        $allplugins   = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins');
240
        foreach ($allplugins as $plugin) {
241
            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...
242
                if ($right = @include XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php") {
243
                    $xoopsPlugins[$plugin] = $plugin;
244
                }
245
            }
246
        }
247
248
        return $xoopsPlugins;
249
    }
250
251
    /**
252
     * @param string $css_file
253
     *
254
     * @return array
255
     */
256
    public function loadCss($css_file = 'style.css')
257
    {
258
        static $css_url, $css_path;
259
260
        if (!isset($css_url)) {
261
            $css_url  = dirname(xoops_getcss($GLOBALS['xoopsConfig']['theme_set']));
262
            $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
263
        }
264
265
        $css         = array();
266
        $css[]       = $css_url . '/' . $css_file;
267
        $css_content = file_get_contents($css_path . '/' . $css_file);
268
269
        // get all import css files
270
        if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) {
271
            foreach ($matches[1] as $key => $css_import) {
272
                $css = array_merge($css, $this->loadCss($css_import));
273
            }
274
        }
275
276
        return $css;
277
    }
278
279
    /**
280
     * @return string
281
     */
282
    public function render()
283
    {
284
        static $isTinyMceJsLoaded = false;
285
286
        $this->init();
287
        if (isset($this->setting['elements']) && self::$LastOfElementsTinymce != $this->setting['elements']) {
288
            $ret = "\n<!-- 'tiny_mce.js' SCRIPT NOT YET " . $this->setting['elements'] . " -->\n"; //debug
289
290
            return $ret;
291
        } else {
292
            $this->setting['elements'] = implode(',', self::$ListOfElementsTinymce);
293
        }
294
        if (!empty($this->setting['callback'])) {
295
            $callback = $this->setting['callback'];
296
            unset($this->setting['callback']);
297
        } else {
298
            $callback = '';
299
        }
300
        if (!empty($this->setting['file_browser_callback'])) {
301
            $fbc_name = XOOPS_ROOT_PATH . '/class/xoopseditor/tinymce/include/' . $this->setting['file_browser_callback'] . '.js';
302
            //suis passé la /lesrevespossibles/x244/class/xoopseditor/tinymce/tinymce/jscripts/include/openFinder.js
303
            $file_browser_callback = "MyXoopsUrl ='" . XOOPS_URL . "';\n";
304
            $file_browser_callback .= file_get_contents($fbc_name);
305
            $file_browser_callback .= "\n//suis passé la " . $fbc_name;
306
            //unset($this->setting["file_browser_callback"]);
307
        } else {
308
            $file_browser_callback = '//suis absent';
309
        }
310
311
        // create returned string - start
312
        $ret = "\n";
313
314
        /*
315
        // IE BUG - start
316
        // more info here:
317
        // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
318
        // possible solution here:
319
        // http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
320
                $ret =<<<EOF
321
        <script language='javascript' type='text/javascript'>
322
            if (/msie/i.test (navigator.userAgent)) { //only override IE
323
                document.nativeGetElementById = document.getElementById;
324
                document.getElementById = function(id) {
325
                    var elem = document.nativeGetElementById(id);
326
                    if (elem) {
327
                        //make sure that it is a valid match on id
328
                        if (elem.attributes['id'].value == id) {
329
                            return elem;
330
                        } else {
331
                            //otherwise find the correct element
332
                            for (var i=1;i<document.all[id].length;i++) {
333
                                if (document.all[id][i].attributes['id'].value == id) {
334
                                    return document.all[id][i];
335
                                }
336
                            }
337
                        }
338
                    }
339
340
                    return null;
341
                };
342
            }
343
        </script>
344
        \n
345
        EOF;
346
        // IE BUG - end
347
        */
348
349
        $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug
350
        if ($isTinyMceJsLoaded) {
351
            $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug
352
        } else {
353
            $ret .= "<script type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/tiny_mce.js'></script>\n";
354
            $isTinyMceJsLoaded = true;
355
        }
356
        $ret .= "<script type='text/javascript'>\n";
357
        $ret .= "tinyMCE.init({\n";
358
        // set options - start
359
        foreach ($this->setting as $key => $val) {
360
            $ret .= $key . ':';
361
            if ($val === true) {
362
                $ret .= 'true,';
363
            } elseif ($val === false) {
364
                $ret .= 'false,';
365
            } else {
366
                $ret .= "'{$val}',";
367
            }
368
            $ret .= "\n";
369
        }
370
        // set options - end
371
        $ret .= "tinymceload: true\n";
372
        $ret .= "});\n";
373
        $ret .= $callback . "\n";
374
        $ret .= $file_browser_callback . "\n";
375
        //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n";
376
        $ret .= "</script>\n";
377
        $ret .= "<!-- End TinyMce Rendering -->\n";//debug
378
        // create returned string - end
379
        return $ret;
380
    }
381
}
382