Completed
Push — master ( 10fc3d...cbc6e7 )
by Michael
18s queued 10s
created

TinyMCE::instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 9.4285
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 4.x adapter for XOOPS
14
 *
15
 * @copyright       XOOPS Project (http://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
 * @version         $Id $
24
 */
25
26
class TinyMCE
27
{
28
    public $rootpath;
29
    public $config = array();
30
    public $setting = array();
31
32
    /**
33
     * constructor
34
     *
35
     * @param string $config The configuration
36
     */
37
    public function __construct($config)
38
    {
39
        $this->setConfig($config);
40
        $this->rootpath = $this->config["rootpath"] . "/tinymce/js/tinymce";
41
    }
42
43
    /**
44
     * Creates one instance of the tinyMCE object
45
     *
46
     * @param array $config The configuration
47
     *
48
     * @return TinyMCE $instance The instance of tinyMCE object
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
    public function setConfig($config)
63
    {
64
        foreach ($config as $key => $val) {
65
            $this->config[$key] = $val;
66
        }
67
    }
68
69
    /**
70
     * Initializes the tinyMCE
71
     *
72
     * @return   boolean
73
     */
74
    public function init()
75
    {
76
        // list of configured options
77
        $configured = array();
78
        $this->setting["selector"] = "textarea";
79
        $this->setting["theme"] = "modern";
80
81
        // Load default settings
82
        if (!($this->setting = @include($GLOBALS['xoops']->path("var/configs/tinymce.php")))) {
83
            $this->setting = include __DIR__ . "/settings.php";
84
        }
85
86
        // get editor language (from ...)
87
        if (is_readable(\XoopsBaseConfig::get('root-path') . $this->rootpath . '/langs/' . $this->config["language"] . '.js')) {
88
            $this->setting["language"] = $this->config["language"];
89
            $configured[] = "language";
90
        }
91
92
        $this->setting["content_css"] = implode(",", $this->loadCss());
93
        $configured[] = "content_css";
94
95
        if (!empty($this->config["theme"])
96
            && is_dir(\XoopsBaseConfig::get('root-path') . $this->rootpath . "/themes/" . $this->config["theme"])
97
        ) {
98
            $this->setting["theme"] = $this->config["theme"];
99
            $configured[] = "theme";
100
        }
101
102
        if (!empty($this->config["mode"])) {
103
            $this->setting["mode"] = $this->config["mode"];
104
            $configured[] = "mode";
105
        }
106
107
        // load all plugins except the plugins in setting["exclude_plugins"]
108
        $this->setting["plugins"] = implode(",", $this->loadPlugins());
109
        $configured[] = "plugins";
110
111
        if ($this->setting["theme"] !== "simple") {
112
            if (empty($this->config["buttons"])) {
113
                $this->config["buttons"][] = array(
114
                    "before"    => "",
115
                    "add"       => "",
116
                    );
117
                $this->config["buttons"][] = array(
118
                    "before"    => "",
119
                    "add"       => "",
120
                    );
121
                $this->config["buttons"][] = array(
122
                    "before"    => "",
123
                    "add"       => "",
124
                    );
125
            }
126
            $i = 0;
127
            foreach ($this->config["buttons"] as $button) {
128
                $i++;
129
                if (isset($button["before"])) {
130
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add_before"] = $button["before"];
131
                }
132
                if (isset($button["add"])) {
133
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add"] = $button["add"];
134
                }
135
                if (isset($button[""])) {
136
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = $button[""];
137
                }
138
            }
139
            $configured[] = "buttons";
140
141
            if (isset($this->config["toolbar_location"])) {
142
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"]
143
                    = $this->config["toolbar_location"];
144
                $configured[] = "toolbar_location";
145
            } else {
146
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = "top";
147
            }
148
149
            if (isset($this->config["toolbar_align"])) {
150
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = $this->config["toolbar_align"];
151
                $configured[] = "toolbar_align";
152
            } else {
153
                $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = "left";
154
            }
155
156
            if (isset($this->config["statusbar_location"])) {
157
                $this->setting["theme_" . $this->setting["theme"] . "_statusbar_location"]
158
                    = $this->config["statusbar_location"];
159
                $configured[] = "statusbar_location";
160
            }
161
162
            if (isset($this->config["path_location"])) {
163
                $this->setting["theme_" . $this->setting["theme"] . "_path_location"] = $this->config["path_location"];
164
                $configured[] = "path_location";
165
            }
166
167
            if (isset($this->config["resize_horizontal"])) {
168
                $this->setting["theme_" . $this->setting["theme"] . "_resize_horizontal"]
169
                    = $this->config["resize_horizontal"];
170
                $configured[] = "resize_horizontal";
171
            }
172
173
            if (isset($this->config["resizing"])) {
174
                $this->setting["theme_" . $this->setting["theme"] . "_resizing"] = $this->config["resizing"];
175
                $configured[] = "resizing";
176
            }
177
178
            if (!empty($this->config["fonts"])) {
179
                $this->setting["theme_" . $this->setting["theme"] . "_fonts"] = $this->config["fonts"];
180
                $configured[] = "fonts";
181
            }
182
183
            for ($i=1; $i <= 4; $i++) {
184
                $buttons = array();
185
                if (isset($this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"])) {
186
                    $checklist = explode(",", $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"]);
187
                    foreach ($checklist as $plugin) {
188
                        if (strpos(strtolower($plugin), "xoops") !== false) {
189
                            if (in_array($plugin, $this->xoopsPlugins)) {
190
                                $buttons[] = $plugin;
191
                            }
192
                        } else {
193
                            $buttons[] = $plugin;
194
                        }
195
                    }
196
                    $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = implode(",", $buttons);
197
                }
198
            }
199
        }
200
201
        $configured = array_unique($configured);
202
        foreach ($this->config as $key => $val) {
203
            if (isset($this->setting[$key]) || in_array($key, $configured)) {
204
                continue;
205
            }
206
            $this->setting[$key] = $val;
207
        }
208
209
        if (!is_dir(\XoopsBaseConfig::get('root-path') . $this->rootpath . "/themes/" . $this->setting["theme"] . '/docs/' . $this->setting["language"] . '/')) {
210
            $this->setting["docs_language"] = "en";
211
        }
212
213
        unset($this->config, $configured);
214
215
        return true;
216
    }
217
218
    /**
219
      * loadPlugins - load all plugins execpt the plugins in setting["exclude_plugins"]
220
      *
221
      * @return array plugins
222
      */
223
    public function loadPlugins()
224
    {
225
        $plugins = array();
226
        $plugins_list = XoopsLists::getDirListAsArray(\XoopsBaseConfig::get('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
    /**
243
     * get_xoopsPlugins - return all xoops plugins
244
     *
245
     * @return array plugins
246
     */
247
    public function get_xoopsPlugins()
248
    {
249
        $xoopsPlugins = array();
250
        $xoops_root_path = \XoopsBaseConfig::get('root-path');
251
        $allplugins = XoopsLists::getDirListAsArray ($xoops_root_path . $this->rootpath . "/plugins");
252
        foreach ($allplugins as $plugin) {
253
            if (strpos(strtolower($plugin), "xoops") !== false
254
                && file_exists($xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php")) {
255
                if ($right = @include $xoops_root_path . $this->config["rootpath"] . "/include/$plugin.php") {
256
                    $xoopsPlugins[$plugin] = $plugin;
257
                }
258
            }
259
        }
260
261
        return $xoopsPlugins;
262
    }
263
264
    public function loadCss($css_file = 'style.css')
265
    {
266
        static $css_url, $css_path;
267
268
        if (!isset($css_url)) {
269
            $css_url = dirname(\Xoops::getInstance()->getCss($GLOBALS['xoopsConfig']['theme_set']));
270
            $css_path = str_replace(\XoopsBaseConfig::get('themes-url'), \XoopsBaseConfig::get('themes-path'), $css_url);
271
        }
272
273
        $css = array();
274
        $css[] = $css_url . '/' . $css_file;
275
        $css_content = file_get_contents($css_path . '/' . $css_file);
276
277
        // get all import css files
278
        if (preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER)) {
279
            foreach ($matches[1] as $key => $css_import) {
280
                $css = array_merge($css, $this->loadCss($css_import));
281
            }
282
        }
283
284
        return $css;
285
    }
286
287
    /**
288
     * Renders the tinyMCE
289
     *
290
     * @return   string  $ret      The rendered HTML string
291
     */
292
    public function render()
293
    {
294
        static $rendered;
295
        if ($rendered) {
296
            return null;
297
        }
298
299
        $rendered = true;
300
301
        $this->init();
302
303
        if (!empty($this->setting["callback"])) {
304
            $callback = $this->setting["callback"];
305
            unset($this->setting["callback"]);
306
        } else {
307
            $callback = "";
308
        }
309
310
        $ret = '<script language="javascript" type="text/javascript" src="' . \XoopsBaseConfig::get('url') . $this->rootpath . '/tinymce.min.js"></script>';
311
        $ret .= '<script language="javascript" type="text/javascript">
312
                tinyMCE.init({
313
            ';
314
315
        foreach ($this->setting as $key => $val) {
316
            $ret .= $key . ":";
317
            if ($val === true) {
318
                $ret.= "true,";
319
            } elseif ($val === false) {
320
                $ret .= "false,";
321
            } else {
322
                $ret .= "'{$val}',";
323
            }
324
            $ret .= "\n";
325
        }
326
327
        //	 Ajout alain01 tinymce v4
328
329
        $chemin_array=parse_url(\XoopsBaseConfig::get('url'));
330
        $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...
331
        $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...
332
        //$chemin_path =  $chemin_array["path"]; // /myweb1
333
        if (!isset($chemin_array['path'])) {
334
            $chemin_path = '';
335
        } else {
336
            $chemin_path =  $chemin_array["path"];
337
        }
338
339
        //$ret .='language_url : "'.$chemin_path.'/class/xoopseditor/tinymce4/tinymce/js/tinymce/langs/fr_FR.js",';
340
341
        $ret .= 'external_plugins: {';
342
        $ret .= '"qrcode": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/qrcode/plugin.min.js",';
343
        $ret .= '"youtube": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/youtube/plugin.min.js",';
344
        $ret .= '"alignbtn": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/alignbtn/plugin.min.js",';
345
        $ret .= '"chartextbtn": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/chartextbtn/plugin.min.js",';
346
        $ret .= '"xoops_code": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/xoops_code/plugin.min.js",';
347
        $ret .= '"xoops_quote": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/xoops_quote/plugin.min.js",';
348
        $ret .= '"xoops_tagextgal": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/xoops_tagextgal/plugin.min.js",';
349
        $ret .= '"codemirror": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/codemirror/plugin.min.js",';
350
        $ret .= '"filemanager": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/filemanager/plugin.min.js",';
351
        $ret .= '"responsivefilemanager": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/responsivefilemanager/plugin.min.js",';
352
        $ret .= '},';
353
        $ret .= "\n";
354
355
        $ret .= 'codemirror: {
356
            indentOnInit: true,
357
            path: "CodeMirror",
358
            config: {
359
                mode: "application/x-httpd-php",
360
                lineNumbers: false
361
            },
362
            jsFiles: [
363
                "mode/clike/clike.js",
364
                "mode/php/php.js"
365
            ]
366
            },';
367
        $ret .= "\n";
368
369
        $ret .= '"external_filemanager_path": "'.$chemin_path.'/class/xoopseditor/tinymce4/external_plugins/filemanager/",';
370
        $ret .= "\n";
371
372
        $ret .='templates: "'.$chemin_path.'/uploads/filemanager/templates/liste-templates.js",';
373
        $ret .= "\n";
374
        // fin ajout alain01
375
376
        $ret .= 'relative_urls : false,
377
                remove_script_host : false, tinymceload : "1"});
378
                '.$callback.'
379
                function showMCE(id)
380
                {
381
                    if (tinyMCE.getInstanceById(id) == null) {
382
                        tinyMCE.execCommand("mceAddControl", false, id);
383
                    } else {
384
                        tinyMCE.execCommand("mceRemoveControl", false, id);
385
                    }
386
                }
387
                </script>
388
            ';
389
390
        return $ret ;
391
    }
392
}
393