PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/xoops/xoopseditor/tinymce/releases/3.3.9.2/htdocs/class/xoopseditor/tinymce/tinymce.php

https://github.com/krisxoofoo/XooFoo
PHP | 339 lines | 231 code | 38 blank | 70 comment | 47 complexity | c5afd1840c042ace02e066e49c60248e MD5 | raw 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. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. */
  10. /**
  11. * TinyMCE adapter for XOOPS
  12. *
  13. * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
  14. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  15. * @package class
  16. * @subpackage editor
  17. * @since 2.3.0
  18. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  19. * @author Lucio Rota <lucio.rota@gmail.com>
  20. * @author Laurent JEN <dugris@frxoops.org>
  21. * @version $Id $
  22. */
  23. class TinyMCE
  24. {
  25. var $rootpath;
  26. var $config = array();
  27. var $setting = array();
  28. // PHP 5 Constructor
  29. function __construct($config)
  30. {
  31. $this->setConfig($config);
  32. $this->rootpath = $this->config["rootpath"] . "/tinymce/jscripts/tiny_mce";
  33. $this->xoopsPlugins = $this->get_xoopsPlugins();
  34. }
  35. // PHP 4 Contructor
  36. function TinyMCE($config)
  37. {
  38. $this->__construct($config) ;
  39. }
  40. function &instance( $config )
  41. {
  42. static $instance;
  43. if (!isset($instance)) {
  44. $instance = new TinyMCE($config);
  45. } else {
  46. $instance->setConfig($config);
  47. }
  48. return $instance;
  49. }
  50. function setConfig( $config )
  51. {
  52. foreach ($config as $key => $val) {
  53. $this->config[$key] = $val;
  54. }
  55. }
  56. function init()
  57. {
  58. // list of configured options
  59. $configured = array();
  60. // Load default settings
  61. if ( ! ($this->setting = @include( $GLOBALS['xoops']->path( "var/configs/tinymce.php" ) ) ) ) {
  62. $this->setting = include dirname(__FILE__) . "/settings.php";
  63. }
  64. // get editor language (from ...)
  65. if (is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config["language"] . '.js')) {
  66. $this->setting["language"] = $this->config["language"];
  67. $configured[] = "language";
  68. }
  69. $this->setting["content_css"] = implode( ",", $this->loadCss() );
  70. $configured[] = "content_css";
  71. if ( !empty($this->config["theme"]) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . "/themes/" . $this->config["theme"]) ) {
  72. $this->setting["theme"] = $this->config["theme"];
  73. $configured[] = "theme";
  74. }
  75. if (!empty($this->config["mode"])) {
  76. $this->setting["mode"] = $this->config["mode"];
  77. $configured[] = "mode";
  78. }
  79. // load all plugins except the plugins in setting["exclude_plugins"]
  80. $this->setting["plugins"] = implode(",", $this->loadPlugins());
  81. $configured[] = "plugins";
  82. if ( $this->setting["theme"] != "simple" ) {
  83. if (empty($this->config["buttons"])) {
  84. $this->config["buttons"][] = array(
  85. "before" => "",
  86. "add" => "",
  87. );
  88. $this->config["buttons"][] = array(
  89. "before" => "",
  90. "add" => "",
  91. );
  92. $this->config["buttons"][] = array(
  93. "before" => "",
  94. "add" => "",
  95. );
  96. }
  97. $i = 0;
  98. foreach ($this->config["buttons"] as $button) {
  99. $i++;
  100. if (isset($button["before"])) {
  101. $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add_before"] = $button["before"];
  102. }
  103. if (isset($button["add"])) {
  104. $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}_add"] = $button["add"];
  105. }
  106. if (isset($button[""])) {
  107. $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = $button[""];
  108. }
  109. }
  110. $configured[] = "buttons";
  111. if (isset($this->config["toolbar_location"])) {
  112. $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = $this->config["toolbar_location"];
  113. $configured[] = "toolbar_location";
  114. } else {
  115. $this->setting["theme_" . $this->setting["theme"] . "_toolbar_location"] = "top";
  116. }
  117. if (isset($this->config["toolbar_align"])) {
  118. $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = $this->config["toolbar_align"];
  119. $configured[] = "toolbar_align";
  120. } else {
  121. $this->setting["theme_" . $this->setting["theme"] . "_toolbar_align"] = "left";
  122. }
  123. if (isset($this->config["statusbar_location"])) {
  124. $this->setting["theme_" . $this->setting["theme"] . "_statusbar_location"] = $this->config["statusbar_location"];
  125. $configured[] = "statusbar_location";
  126. }
  127. if (isset($this->config["path_location"])) {
  128. $this->setting["theme_" . $this->setting["theme"] . "_path_location"] = $this->config["path_location"];
  129. $configured[] = "path_location";
  130. }
  131. if (isset($this->config["resize_horizontal"])) {
  132. $this->setting["theme_" . $this->setting["theme"] . "_resize_horizontal"] = $this->config["resize_horizontal"];
  133. $configured[] = "resize_horizontal";
  134. }
  135. if (isset($this->config["resizing"])) {
  136. $this->setting["theme_" . $this->setting["theme"] . "_resizing"] = $this->config["resizing"];
  137. $configured[] = "resizing";
  138. }
  139. if (!empty($this->config["fonts"])) {
  140. $this->setting["theme_" . $this->setting["theme"] . "_fonts"] = $this->config["fonts"];
  141. $configured[] = "fonts";
  142. }
  143. for ($i=1 ; $i <= 4 ; $i++ ) {
  144. $buttons = array();
  145. if ( isset($this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"]) ) {
  146. $checklist = explode(",", $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] );
  147. foreach ( $checklist as $plugin ) {
  148. if ( strpos( strtolower($plugin), "xoops") != false ) {
  149. if ( in_array( $plugin, $this->xoopsPlugins ) ) {
  150. $buttons[] = $plugin;
  151. }
  152. } else {
  153. $buttons[] = $plugin;
  154. }
  155. }
  156. $this->setting["theme_" . $this->setting["theme"] . "_buttons{$i}"] = implode(",", $buttons);
  157. }
  158. }
  159. }
  160. $configured = array_unique($configured);
  161. foreach ($this->config as $key => $val) {
  162. if (isset($this->setting[$key]) || in_array($key, $configured)) {
  163. continue;
  164. }
  165. $this->setting[$key] = $val;
  166. }
  167. if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . "/themes/" . $this->setting["theme"] . '/docs/' . $this->setting["language"] . '/')) {
  168. $this->setting["docs_language"] = "en";
  169. }
  170. unset($this->config, $configured);
  171. return true;
  172. }
  173. // load all plugins execpt the plugins in setting["exclude_plugins"]
  174. function loadPlugins()
  175. {
  176. $plugins = array();
  177. $plugins_list = XoopsLists::getDirListAsArray( XOOPS_ROOT_PATH . $this->rootpath . "/plugins" );
  178. if (empty($this->setting["plugins"])) {
  179. $plugins = $plugins_list;
  180. } else {
  181. $plugins = array_intersect(explode(",", $this->setting["plugins"]), $plugins_list);
  182. }
  183. if (!empty($this->setting["exclude_plugins"])) {
  184. $plugins = array_diff($plugins, explode(",", $this->setting["exclude_plugins"]));
  185. }
  186. if (!empty($this->config["plugins"])) {
  187. $plugins = array_merge($plugins, $this->config["plugins"]);
  188. }
  189. return $plugins;
  190. }
  191. // return all xoops plugins
  192. function get_xoopsPlugins() {
  193. $xoopsPlugins = array();
  194. $allplugins = XoopsLists::getDirListAsArray( XOOPS_ROOT_PATH . $this->rootpath . "/plugins" );
  195. foreach ( $allplugins as $plugin ) {
  196. if ( strpos( strtolower($plugin), "xoops") != false && file_exists(XOOPS_ROOT_PATH . $this->config["rootpath"] . "/include/$plugin.php") ) {
  197. if ( $right = @include XOOPS_ROOT_PATH . $this->config["rootpath"] . "/include/$plugin.php" ) {
  198. $xoopsPlugins[$plugin] = $plugin;
  199. }
  200. }
  201. }
  202. return $xoopsPlugins;
  203. }
  204. function loadCss($css_file = 'style.css')
  205. {
  206. static $css_url, $css_path;
  207. if (!isset($css_url)) {
  208. $css_url = dirname( xoops_getcss($GLOBALS['xoopsConfig']['theme_set']) );
  209. $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url);
  210. }
  211. $css = array();
  212. $css[] = $css_url . '/' . $css_file;
  213. $css_content = file_get_contents( $css_path . '/' . $css_file );
  214. // get all import css files
  215. if ( preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER) ) {
  216. foreach( $matches[1] as $key => $css_import ) {
  217. $css = array_merge( $css, $this->loadCss( $css_import) );
  218. }
  219. }
  220. return $css;
  221. }
  222. function render()
  223. {
  224. static $isTinyMceJsLoaded = false;
  225. $this->init();
  226. if ( !empty($this->setting["callback"]) ) {
  227. $callback = $this->setting["callback"];
  228. unset($this->setting["callback"]);
  229. } else {
  230. $callback = "";
  231. }
  232. // create returned string - start
  233. $ret = "\n";
  234. /*
  235. // IE BUG - start
  236. // more info here:
  237. // http://www.456bereastreet.com/archive/200802/beware_of_id_and_name_attribute_mixups_when_using_getelementbyid_in_internet_explorer/
  238. // possible solution here:
  239. // http://www.sixteensmallstones.org/ie-javascript-bugs-overriding-internet-explorers-documentgetelementbyid-to-be-w3c-compliant-exposes-an-additional-bug-in-getattributes
  240. $ret =<<<EOF
  241. <script language='javascript' type='text/javascript'>
  242. if (/msie/i.test (navigator.userAgent)) //only override IE
  243. {
  244. document.nativeGetElementById = document.getElementById;
  245. document.getElementById = function(id) {
  246. var elem = document.nativeGetElementById(id);
  247. if(elem) {
  248. //make sure that it is a valid match on id
  249. if(elem.attributes['id'].value == id) {
  250. return elem;
  251. } else {
  252. //otherwise find the correct element
  253. for(var i=1;i<document.all[id].length;i++) {
  254. if(document.all[id][i].attributes['id'].value == id) {
  255. return document.all[id][i];
  256. }
  257. }
  258. }
  259. }
  260. return null;
  261. };
  262. }
  263. </script>
  264. \n
  265. EOF;
  266. // IE BUG - end
  267. */
  268. $ret .= "<!-- Start TinyMce Rendering -->\n"; //debug
  269. if ($isTinyMceJsLoaded) {
  270. $ret .= "<!-- 'tiny_mce.js' SCRIPT IS ALREADY LOADED -->\n"; //debug
  271. } else {
  272. $ret .= "<script language='javascript' type='text/javascript' src='" . XOOPS_URL . $this->rootpath . "/tiny_mce.js'></script>\n";
  273. $isTinyMceJsLoaded = true;
  274. }
  275. $ret .= "<script language='javascript' type='text/javascript'>\n";
  276. $ret .= "tinyMCE.init({\n";
  277. // set options - start
  278. foreach ($this->setting as $key => $val) {
  279. $ret .= $key . ":";
  280. if ($val === true) {
  281. $ret.= "true,";
  282. } elseif ($val === false) {
  283. $ret .= "false,";
  284. } else {
  285. $ret .= "'{$val}',";
  286. }
  287. $ret .= "\n";
  288. }
  289. // set options - end
  290. $ret .= "tinymceload: true\n";
  291. $ret .= "});\n";
  292. $ret .= $callback . "\n";
  293. //$ret .= "function toggleEditor(id) {tinyMCE.execCommand('mceToggleEditor',false, id);}\n";
  294. $ret .= "</script>\n";
  295. $ret .= "<!-- End TinyMce Rendering -->\n";//debug
  296. // create returned string - end
  297. return $ret ;
  298. }
  299. }
  300. ?>