1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 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:
33: 34: 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();
41: self::$LastOfElementsTinymce = $this->config['elements'];
42: self::$ListOfElementsTinymce[] = self::$LastOfElementsTinymce;
43: }
44:
45: 46: 47: 48: 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: 64:
65: public function setConfig($config)
66: {
67: foreach ($config as $key => $val) {
68: $this->config[$key] = $val;
69: }
70: }
71:
72: 73: 74:
75: public function init()
76: {
77:
78: $configured = array();
79:
80:
81: if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) {
82: $this->setting = include __DIR__ . '/settings.php';
83: }
84:
85:
86: 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: 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:
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: if (isset($button['before'])) {
124: $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before'];
125: }
126: if (isset($button['add'])) {
127: $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add'];
128: }
129: if (isset($button[''])) {
130: $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button[''];
131: }
132: }
133: $configured[] = 'buttons';
134:
135: 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: 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: 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: 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: 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: 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) {
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:
210: 211: 212:
213: public function loadPlugins()
214: {
215: $plugins = array();
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:
233: 234: 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")) {
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: 253: 254: 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:
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: 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";
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:
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:
307: } else {
308: $file_browser_callback = '//suis absent';
309: }
310:
311:
312: $ret = "\n";
313:
314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347:
348:
349: $ret .= "<!-- Start TinyMce Rendering -->\n";
350: if ($isTinyMceJsLoaded) {
351: $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n";
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:
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:
371: $ret .= "tinymceload: true\n";
372: $ret .= "});\n";
373: $ret .= $callback . "\n";
374: $ret .= $file_browser_callback . "\n";
375:
376: $ret .= "</script>\n";
377: $ret .= "<!-- End TinyMce Rendering -->\n";
378:
379: return $ret;
380: }
381: }
382: