PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/2.5/wp-includes/js/tinymce/tiny_mce_config.php

#
PHP | 329 lines | 204 code | 71 blank | 54 comment | 64 complexity | 6e28b016bef3d6868817b01a3eb0426b MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. // some code below is from:
  3. /**
  4. * $Id: tiny_mce_gzip.php 315 2007-10-25 14:03:43Z spocke $
  5. *
  6. * @author Moxiecode
  7. * @copyright Copyright Š 2005-2006, Moxiecode Systems AB, All rights reserved.
  8. *
  9. * This file compresses the TinyMCE JavaScript using GZip.
  10. **/
  11. @ require('../../../wp-config.php');
  12. function getFileContents($path) {
  13. $path = realpath($path);
  14. if ( ! $path || ! @is_file($path) )
  15. return '';
  16. if ( function_exists('file_get_contents') )
  17. return @file_get_contents($path);
  18. $content = '';
  19. $fp = @fopen($path, 'r');
  20. if ( ! $fp )
  21. return '';
  22. while ( ! feof($fp) )
  23. $content .= fgets($fp);
  24. fclose($fp);
  25. return $content;
  26. }
  27. function putFileContents( $path, $content ) {
  28. if ( function_exists('file_put_contents') )
  29. return @file_put_contents( $path, $content );
  30. $newfile = false;
  31. $fp = @fopen( $path, 'wb' );
  32. if ($fp) {
  33. $newfile = fwrite( $fp, $content );
  34. fclose($fp);
  35. }
  36. return $newfile;
  37. }
  38. // Set up init variables
  39. $https = ( isset($_SERVER['HTTPS']) && 'on' == strtolower($_SERVER['HTTPS']) ) ? true : false;
  40. $baseurl = get_option('siteurl') . '/wp-includes/js/tinymce';
  41. if ( $https ) str_replace('http://', 'https://', $baseurl);
  42. $mce_css = $baseurl . '/wordpress.css';
  43. $mce_css = apply_filters('mce_css', $mce_css);
  44. if ( $https ) str_replace('http://', 'https://', $mce_css);
  45. $mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1
  46. /*
  47. The following filter allows localization scripts to change the languages displayed in the spellchecker's drop-down menu.
  48. By default it uses Google's spellchecker API, but can be configured to use PSpell/ASpell if installed on the server.
  49. The + sign marks the default language. More information:
  50. http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
  51. */
  52. $mce_spellchecker_languages = apply_filters('mce_spellchecker_languages', '+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv');
  53. $plugins = array( 'safari', 'inlinepopups', 'autosave', 'spellchecker', 'paste', 'wordpress', 'media', 'fullscreen' );
  54. /*
  55. The following filter takes an associative array of external plugins for TinyMCE in the form 'plugin_name' => 'url'.
  56. It adds the plugin's name to TinyMCE's plugins init and the call to PluginManager to load the plugin.
  57. The url should be absolute and should include the js file name to be loaded. Example:
  58. array( 'myplugin' => 'http://my-site.com/wp-content/plugins/myfolder/mce_plugin.js' )
  59. If the plugin uses a button, it should be added with one of the "$mce_buttons" filters.
  60. */
  61. $mce_external_plugins = apply_filters('mce_external_plugins', array());
  62. $ext_plugins = "\n";
  63. if ( ! empty($mce_external_plugins) ) {
  64. /*
  65. The following filter loads external language files for TinyMCE plugins.
  66. It takes an associative array 'plugin_name' => 'path', where path is the
  67. include path to the file. The language file should follow the same format as
  68. /tinymce/langs/wp-langs.php and should define a variable $strings that
  69. holds all translated strings. Example:
  70. $strings = 'tinyMCE.addI18n("' . $mce_locale . '.mypluginname_dlg",{tab_general:"General", ... })';
  71. */
  72. $mce_external_languages = apply_filters('mce_external_languages', array());
  73. $loaded_langs = array();
  74. $strings = '';
  75. if ( ! empty($mce_external_languages) ) {
  76. foreach ( $mce_external_languages as $name => $path ) {
  77. if ( is_file($path) && is_readable($path) ) {
  78. include_once($path);
  79. $ext_plugins .= $strings;
  80. $loaded_langs[] = $name;
  81. }
  82. }
  83. }
  84. foreach ( $mce_external_plugins as $name => $url ) {
  85. if ( $https ) str_replace('http://', 'https://', $url);
  86. $plugins[] = '-' . $name;
  87. if ( in_array($name, $loaded_langs) ) {
  88. $plugurl = dirname($url);
  89. $ext_plugins .= 'tinyMCEPreInit.load_ext("' . $plugurl . '", "' . $mce_locale . '");' . "\n";
  90. }
  91. $ext_plugins .= 'tinymce.PluginManager.load("' . $name . '", "' . $url . '");' . "\n";
  92. }
  93. }
  94. $plugins = implode($plugins, ',');
  95. $mce_buttons = apply_filters('mce_buttons', array('bold', 'italic', 'strikethrough', '|', 'bullist', 'numlist', 'blockquote', '|', 'justifyleft', 'justifycenter', 'justifyright', '|', 'link', 'unlink', 'image', 'wp_more', '|', 'spellchecker', 'fullscreen', 'wp_adv' ));
  96. $mce_buttons = implode($mce_buttons, ',');
  97. $mce_buttons_2 = apply_filters('mce_buttons_2', array('formatselect', 'underline', 'justifyfull', 'forecolor', '|', 'pastetext', 'pasteword', 'removeformat', '|', 'media', 'charmap', '|', 'outdent', 'indent', '|', 'undo', 'redo', 'wp_help' ));
  98. $mce_buttons_2 = implode($mce_buttons_2, ',');
  99. $mce_buttons_3 = apply_filters('mce_buttons_3', array());
  100. $mce_buttons_3 = implode($mce_buttons_3, ',');
  101. $mce_buttons_4 = apply_filters('mce_buttons_4', array());
  102. $mce_buttons_4 = implode($mce_buttons_4, ',');
  103. // TinyMCE init settings
  104. $initArray = array (
  105. 'mode' => 'none',
  106. 'onpageload' => 'wpEditorInit',
  107. 'width' => '100%',
  108. 'theme' => 'advanced',
  109. 'skin' => 'wp_theme',
  110. 'theme_advanced_buttons1' => "$mce_buttons",
  111. 'theme_advanced_buttons2' => "$mce_buttons_2",
  112. 'theme_advanced_buttons3' => "$mce_buttons_3",
  113. 'theme_advanced_buttons4' => "$mce_buttons_4",
  114. 'language' => "$mce_locale",
  115. 'spellchecker_languages' => "$mce_spellchecker_languages",
  116. 'theme_advanced_toolbar_location' => 'top',
  117. 'theme_advanced_toolbar_align' => 'left',
  118. 'theme_advanced_statusbar_location' => 'bottom',
  119. 'theme_advanced_resizing' => true,
  120. 'theme_advanced_resize_horizontal' => false,
  121. 'dialog_type' => 'modal',
  122. 'relative_urls' => false,
  123. 'remove_script_host' => false,
  124. 'apply_source_formatting' => false,
  125. 'remove_linebreaks' => true,
  126. 'paste_convert_middot_lists' => true,
  127. 'paste_remove_spans' => true,
  128. 'paste_remove_styles' => true,
  129. 'gecko_spellcheck' => true,
  130. 'entities' => '38,amp,60,lt,62,gt',
  131. 'accessibility_focus' => false,
  132. 'tab_focus' => ':next',
  133. 'content_css' => "$mce_css",
  134. 'save_callback' => 'switchEditors.saveCallback',
  135. 'plugins' => "$plugins",
  136. // pass-through the settings for compression and caching, so they can be changed with "tiny_mce_before_init"
  137. 'disk_cache' => true,
  138. 'compress' => true,
  139. 'old_cache_max' => '1' // number of cache files to keep
  140. );
  141. // For people who really REALLY know what they're doing with TinyMCE
  142. // You can modify initArray to add, remove, change elements of the config before tinyMCE.init (changed from action to filter)
  143. $initArray = apply_filters('tiny_mce_before_init', $initArray);
  144. // Setting "valid_elements", "invalid_elements" and "extended_valid_elements" can be done through "tiny_mce_before_init".
  145. // Best is to use the default cleanup by not specifying valid_elements, as TinyMCE contains full set of XHTML 1.0.
  146. // support for deprecated actions
  147. ob_start();
  148. do_action('mce_options');
  149. $mce_deprecated = ob_get_contents();
  150. ob_end_clean();
  151. $mce_deprecated = (string) $mce_deprecated;
  152. if ( strlen( $mce_deprecated ) < 10 || ! strpos( $mce_deprecated, ':' ) || ! strpos( $mce_deprecated, ',' ) )
  153. $mce_deprecated = '';
  154. // Settings for the gzip compression and cache
  155. $disk_cache = ( ! isset($initArray['disk_cache']) || false == $initArray['disk_cache'] ) ? false : true;
  156. $compress = ( ! isset($initArray['compress']) || false == $initArray['compress'] ) ? false : true;
  157. $old_cache_max = ( isset($initArray['old_cache_max']) ) ? (int) $initArray['old_cache_max'] : 0;
  158. $initArray['disk_cache'] = $initArray['compress'] = $initArray['old_cache_max'] = null;
  159. unset( $initArray['disk_cache'], $initArray['compress'], $initArray['old_cache_max'] );
  160. // Anybody still using IE5/5.5? It can't handle gzip compressed js well.
  161. if ( $msie = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) {
  162. $ie_ver = (int) substr( $_SERVER['HTTP_USER_AGENT'] , $msie + 5, 3 );
  163. if ( $ie_ver && $ie_ver < 6 ) $compress = false;
  164. }
  165. // Cache path, this is where the .gz files will be stored
  166. $cache_path = ABSPATH . 'wp-content/uploads/js_cache';
  167. if ( $disk_cache && ! is_dir($cache_path) )
  168. $disk_cache = wp_mkdir_p($cache_path);
  169. $cache_ext = '.js';
  170. $plugins = explode( ',', $initArray['plugins'] );
  171. $theme = ( 'simple' == $initArray['theme'] ) ? 'simple' : 'advanced';
  172. $language = isset($initArray['language']) ? substr( $initArray['language'], 0, 2 ) : 'en';
  173. $cacheKey = $mce_options = '';
  174. // Check if browser supports gzip
  175. if ( $compress && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
  176. if ( ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') || isset($_SERVER['---------------']) ) && function_exists('gzencode') && ! ini_get('zlib.output_compression') ) {
  177. $cache_ext = '.gz';
  178. }
  179. }
  180. // Setup cache info
  181. if ( $disk_cache ) {
  182. $cacheKey = apply_filters('tiny_mce_version', '20080327');
  183. foreach ( $initArray as $v )
  184. $cacheKey .= $v;
  185. if ( ! empty($mce_external_plugins) ) {
  186. foreach ( $mce_external_plugins as $n => $v )
  187. $cacheKey .= $n;
  188. }
  189. $cacheKey = md5( $cacheKey );
  190. $cache_file = $cache_path . '/tinymce_' . $cacheKey . $cache_ext;
  191. }
  192. $expiresOffset = 864000; // 10 days
  193. header( 'Content-Type: application/x-javascript; charset=UTF-8' );
  194. header( 'Vary: Accept-Encoding' ); // Handle proxies
  195. header( 'Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . ' GMT' );
  196. // Use cached file if exists
  197. if ( $disk_cache && is_file($cache_file) && is_readable($cache_file) ) {
  198. $mtime = gmdate("D, d M Y H:i:s", filemtime($cache_file)) . " GMT";
  199. if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $mtime ) {
  200. header('HTTP/1.1 304 Not Modified');
  201. exit;
  202. }
  203. header("Last-Modified: " . $mtime);
  204. header("Cache-Control: must-revalidate", false);
  205. $content = getFileContents( $cache_file );
  206. if ( '.gz' == $cache_ext )
  207. header( 'Content-Encoding: gzip' );
  208. header( 'Content-Length: ' . strlen($content) );
  209. echo $content;
  210. exit;
  211. }
  212. foreach ( $initArray as $k => $v )
  213. $mce_options .= $k . ':"' . $v . '",';
  214. if ( $mce_deprecated ) $mce_options .= $mce_deprecated;
  215. $mce_options = rtrim( trim($mce_options), '\n\r,' );
  216. $content = 'var tinyMCEPreInit = { settings : { themes : "' . $theme . '", plugins : "' . $initArray['plugins'] . '", languages : "' . $language . '", debug : false }, base : "' . $baseurl . '", suffix : "" };';
  217. // Load patch
  218. $content .= getFileContents( 'tiny_mce_ext.js' );
  219. // Add core
  220. $content .= getFileContents( 'tiny_mce.js' );
  221. // Patch loading functions
  222. $content .= 'tinyMCEPreInit.start();';
  223. // Add all languages (WP)
  224. include_once( dirname(__FILE__).'/langs/wp-langs.php' );
  225. $content .= $strings;
  226. // Add themes
  227. $content .= getFileContents( 'themes/' . $theme . '/editor_template.js' );
  228. // Add plugins
  229. foreach ( $plugins as $plugin )
  230. $content .= getFileContents( 'plugins/' . $plugin . '/editor_plugin.js' );
  231. // Add external plugins and init
  232. $content .= $ext_plugins . 'tinyMCE.init({' . $mce_options . '});';
  233. // Generate GZIP'd content
  234. if ( '.gz' == $cache_ext ) {
  235. header('Content-Encoding: gzip');
  236. $content = gzencode( $content, 9, FORCE_GZIP );
  237. }
  238. // Stream to client
  239. header( 'Content-Length: ' . strlen($content) );
  240. echo $content;
  241. // Write file
  242. if ( '' != $cacheKey && is_dir($cache_path) && is_readable($cache_path) ) {
  243. $old_cache = array();
  244. $handle = opendir($cache_path);
  245. while ( false !== ( $file = readdir($handle) ) ) {
  246. if ( $file == '.' || $file == '..' ) continue;
  247. $saved = filectime("$cache_path/$file");
  248. if ( strpos($file, 'tinymce_') !== false && substr($file, -3) == $cache_ext ) $old_cache["$saved"] = $file;
  249. }
  250. closedir($handle);
  251. krsort($old_cache);
  252. if ( 1 >= $old_cache_max ) $del_cache = $old_cache;
  253. else $del_cache = array_slice( $old_cache, ($old_cache_max - 1) );
  254. foreach ( $del_cache as $key )
  255. @unlink("$cache_path/$key");
  256. putFileContents( $cache_file, $content );
  257. }
  258. ?>