2 // some code below is from:
4 * $Id: tiny_mce_gzip.php 315 2007-10-25 14:03:43Z spocke $
7 * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved.
9 * This file compresses the TinyMCE JavaScript using GZip.
12 // Discard any buffers
13 while ( @ob_end_clean() );
15 @ require('../../../wp-load.php');
17 function getFileContents($path) {
19 if ( function_exists('realpath') )
20 $path = realpath($path);
22 if ( ! $path || ! @is_file($path) )
25 if ( function_exists('file_get_contents') )
26 return @file_get_contents($path);
29 $fp = @fopen($path, 'r');
34 $content .= fgets($fp);
40 function putFileContents( $path, $content ) {
41 if ( function_exists('file_put_contents') )
42 return @file_put_contents( $path, $content );
45 $fp = @fopen( $path, 'wb' );
47 $newfile = fwrite( $fp, $content );
53 // Set up init variables
54 $baseurl = includes_url('js/tinymce');
56 $mce_css = $baseurl . '/wordpress.css';
57 $mce_css = apply_filters('mce_css', $mce_css);
59 $mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1
62 The following filter allows localization scripts to change the languages displayed in the spellchecker's drop-down menu.
63 By default it uses Google's spellchecker API, but can be configured to use PSpell/ASpell if installed on the server.
64 The + sign marks the default language. More information:
65 http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker
67 $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');
69 $plugins = array( 'safari', 'inlinepopups', 'autosave', 'spellchecker', 'paste', 'wordpress', 'media', 'fullscreen', 'wpeditimage' );
72 The following filter takes an associative array of external plugins for TinyMCE in the form 'plugin_name' => 'url'.
73 It adds the plugin's name to TinyMCE's plugins init and the call to PluginManager to load the plugin.
74 The url should be absolute and should include the js file name to be loaded. Example:
75 array( 'myplugin' => 'http://my-site.com/wp-content/plugins/myfolder/mce_plugin.js' )
76 If the plugin uses a button, it should be added with one of the "$mce_buttons" filters.
78 $mce_external_plugins = apply_filters('mce_external_plugins', array());
81 if ( ! empty($mce_external_plugins) ) {
84 The following filter loads external language files for TinyMCE plugins.
85 It takes an associative array 'plugin_name' => 'path', where path is the
86 include path to the file. The language file should follow the same format as
87 /tinymce/langs/wp-langs.php and should define a variable $strings that
88 holds all translated strings. Example:
89 $strings = 'tinyMCE.addI18n("' . $mce_locale . '.mypluginname_dlg",{tab_general:"General", ... })';
91 $mce_external_languages = apply_filters('mce_external_languages', array());
93 $loaded_langs = array();
96 if ( ! empty($mce_external_languages) ) {
97 foreach ( $mce_external_languages as $name => $path ) {
98 if ( is_file($path) && is_readable($path) ) {
100 $ext_plugins .= $strings;
101 $loaded_langs[] = $name;
106 foreach ( $mce_external_plugins as $name => $url ) {
108 if ( is_ssl() ) $url = str_replace('http://', 'https://', $url);
110 $plugins[] = '-' . $name;
112 if ( in_array($name, $loaded_langs) ) {
113 $plugurl = dirname($url);
114 $ext_plugins .= 'tinyMCEPreInit.load_ext("' . $plugurl . '", "' . $mce_locale . '");' . "\n";
116 $ext_plugins .= 'tinymce.PluginManager.load("' . $name . '", "' . $url . '");' . "\n";
119 $plugins = implode($plugins, ',');
121 $mce_buttons = apply_filters('mce_buttons', array('bold', 'italic', 'strikethrough', '|', 'bullist', 'numlist', 'blockquote', '|', 'justifyleft', 'justifycenter', 'justifyright', '|', 'link', 'unlink', 'wp_more', '|', 'spellchecker', 'fullscreen', 'wp_adv' ));
122 $mce_buttons = implode($mce_buttons, ',');
124 $mce_buttons_2 = apply_filters('mce_buttons_2', array('formatselect', 'underline', 'justifyfull', 'forecolor', '|', 'pastetext', 'pasteword', 'removeformat', '|', 'media', 'charmap', '|', 'outdent', 'indent', '|', 'undo', 'redo', 'wp_help' ));
125 $mce_buttons_2 = implode($mce_buttons_2, ',');
127 $mce_buttons_3 = apply_filters('mce_buttons_3', array());
128 $mce_buttons_3 = implode($mce_buttons_3, ',');
130 $mce_buttons_4 = apply_filters('mce_buttons_4', array());
131 $mce_buttons_4 = implode($mce_buttons_4, ',');
133 $no_captions = ( apply_filters( 'disable_captions', '' ) ) ? true : false;
135 // TinyMCE init settings
138 'onpageload' => 'wpEditorInit',
140 'theme' => 'advanced',
141 'skin' => 'wp_theme',
142 'theme_advanced_buttons1' => "$mce_buttons",
143 'theme_advanced_buttons2' => "$mce_buttons_2",
144 'theme_advanced_buttons3' => "$mce_buttons_3",
145 'theme_advanced_buttons4' => "$mce_buttons_4",
146 'language' => "$mce_locale",
147 'spellchecker_languages' => "$mce_spellchecker_languages",
148 'theme_advanced_toolbar_location' => 'top',
149 'theme_advanced_toolbar_align' => 'left',
150 'theme_advanced_statusbar_location' => 'bottom',
151 'theme_advanced_resizing' => true,
152 'theme_advanced_resize_horizontal' => false,
153 'dialog_type' => 'modal',
154 'relative_urls' => false,
155 'remove_script_host' => false,
156 'convert_urls' => false,
157 'apply_source_formatting' => false,
158 'remove_linebreaks' => true,
159 'paste_convert_middot_lists' => true,
160 'paste_remove_spans' => true,
161 'paste_remove_styles' => true,
162 'gecko_spellcheck' => true,
163 'entities' => '38,amp,60,lt,62,gt',
164 'accessibility_focus' => false,
165 'tab_focus' => ':next',
166 'content_css' => "$mce_css",
167 'save_callback' => 'switchEditors.saveCallback',
168 'wpeditimage_disable_captions' => $no_captions,
169 'plugins' => "$plugins",
170 // pass-through the settings for compression and caching, so they can be changed with "tiny_mce_before_init"
171 'disk_cache' => true,
173 'old_cache_max' => '1' // number of cache files to keep
176 // For people who really REALLY know what they're doing with TinyMCE
177 // You can modify initArray to add, remove, change elements of the config before tinyMCE.init (changed from action to filter)
178 $initArray = apply_filters('tiny_mce_before_init', $initArray);
180 // Setting "valid_elements", "invalid_elements" and "extended_valid_elements" can be done through "tiny_mce_before_init".
181 // Best is to use the default cleanup by not specifying valid_elements, as TinyMCE contains full set of XHTML 1.0.
183 // support for deprecated actions
185 do_action('mce_options');
186 $mce_deprecated = ob_get_contents();
189 $mce_deprecated = (string) $mce_deprecated;
190 if ( strlen( $mce_deprecated ) < 10 || ! strpos( $mce_deprecated, ':' ) || ! strpos( $mce_deprecated, ',' ) )
191 $mce_deprecated = '';
193 // Settings for the gzip compression and cache
194 $disk_cache = ( ! isset($initArray['disk_cache']) || false == $initArray['disk_cache'] ) ? false : true;
195 $compress = ( ! isset($initArray['compress']) || false == $initArray['compress'] ) ? false : true;
196 $old_cache_max = ( isset($initArray['old_cache_max']) ) ? (int) $initArray['old_cache_max'] : 0;
198 $initArray['disk_cache'] = $initArray['compress'] = $initArray['old_cache_max'] = null;
199 unset( $initArray['disk_cache'], $initArray['compress'], $initArray['old_cache_max'] );
201 // Anybody still using IE5/5.5? It can't handle gzip compressed js well.
202 if ( $msie = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ) {
203 $ie_ver = (int) substr( $_SERVER['HTTP_USER_AGENT'] , $msie + 5, 3 );
204 if ( $ie_ver && $ie_ver < 6 ) $compress = false;
207 // Cache path, this is where the .gz files will be stored
208 $cache_path = WP_CONTENT_DIR . '/uploads/js_cache';
209 if ( $disk_cache && ! is_dir($cache_path) )
210 $disk_cache = wp_mkdir_p($cache_path);
213 $plugins = explode( ',', $initArray['plugins'] );
214 $theme = ( 'simple' == $initArray['theme'] ) ? 'simple' : 'advanced';
215 $language = ( isset($initArray['language']) && ! empty($initArray['language']) ) ? substr( $initArray['language'], 0, 2 ) : 'en';
216 $cacheKey = $mce_options = '';
218 // Check if browser supports gzip
219 if ( $compress && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
220 if ( ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') || isset($_SERVER['---------------']) ) && function_exists('gzencode') && ! ini_get('zlib.output_compression') ) {
228 $cacheKey = apply_filters('tiny_mce_version', '20080810');
230 foreach ( $initArray as $v )
233 if ( ! empty($mce_external_plugins) ) {
234 foreach ( $mce_external_plugins as $n => $v )
238 $cacheKey = md5( $cacheKey );
239 $cache_file = $cache_path . '/tinymce_' . $cacheKey . $cache_ext;
242 $expiresOffset = 864000; // 10 days
243 header( 'Content-Type: application/x-javascript; charset=UTF-8' );
244 header( 'Vary: Accept-Encoding' ); // Handle proxies
245 header( 'Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expiresOffset ) . ' GMT' );
247 // Use cached file if exists
248 if ( $disk_cache && is_file($cache_file) && is_readable($cache_file) ) {
250 $mtime = gmdate("D, d M Y H:i:s", filemtime($cache_file)) . " GMT";
252 if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $mtime ) {
253 header('HTTP/1.1 304 Not Modified');
256 header("Last-Modified: " . $mtime);
257 header("Cache-Control: must-revalidate", false);
259 $content = getFileContents( $cache_file );
261 if ( '.gz' == $cache_ext )
262 header( 'Content-Encoding: gzip' );
268 foreach ( $initArray as $k => $v )
269 $mce_options .= $k . ':"' . $v . '",';
271 if ( $mce_deprecated ) $mce_options .= $mce_deprecated;
273 $mce_options = rtrim( trim($mce_options), '\n\r,' );
275 $content = 'var tinyMCEPreInit = { settings : { themes : "' . $theme . '", plugins : "' . $initArray['plugins'] . '", languages : "' . $language . '", debug : false }, base : "' . $baseurl . '", suffix : "", query : "ver=311" };';
278 $content .= getFileContents( 'tiny_mce_ext.js' );
281 $content .= getFileContents( 'tiny_mce.js' );
283 // Patch loading functions
284 $content .= 'tinyMCEPreInit.start();';
286 // Add all languages (WP)
287 include_once( dirname(__FILE__).'/langs/wp-langs.php' );
288 $content .= $strings;
291 $content .= getFileContents( 'themes/' . $theme . '/editor_template.js' );
294 foreach ( $plugins as $plugin )
295 $content .= getFileContents( 'plugins/' . $plugin . '/editor_plugin.js' );
297 // Add external plugins and init
298 $content .= $ext_plugins . 'tinyMCE.init({' . $mce_options . '});';
300 // Generate GZIP'd content
301 if ( '.gz' == $cache_ext ) {
302 header('Content-Encoding: gzip');
303 $content = gzencode( $content, 9, FORCE_GZIP );
310 if ( '' != $cacheKey && is_dir($cache_path) && is_readable($cache_path) ) {
312 $old_cache = array();
313 $handle = opendir($cache_path);
314 while ( false !== ( $file = readdir($handle) ) ) {
315 if ( $file == '.' || $file == '..' ) continue;
316 $saved = filectime("$cache_path/$file");
317 if ( strpos($file, 'tinymce_') !== false && substr($file, -3) == $cache_ext ) $old_cache["$saved"] = $file;
322 if ( 1 >= $old_cache_max ) $del_cache = $old_cache;
323 else $del_cache = array_slice( $old_cache, ($old_cache_max - 1) );
325 foreach ( $del_cache as $key )
326 @unlink("$cache_path/$key");
328 putFileContents( $cache_file, $content );