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