Update to latest tinyMCE compressor. Simplify MCE language loading. see #3882

git-svn-id: http://svn.automattic.com/wordpress/trunk@5108 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-03-26 03:44:35 +00:00
parent b154fd93b1
commit ee5f8a4944
2 changed files with 190 additions and 171 deletions

View File

@ -1,10 +1,7 @@
<?php
/**
* $RCSfile: tiny_mce_gzip.php,v $
* $Revision: $
* $Date: $
* $Id: tiny_mce_gzip.php 158 2006-12-21 14:32:19Z spocke $
*
* @version 1.08
* @author Moxiecode
* @copyright Copyright 2005-2006, Moxiecode Systems AB, All rights reserved.
*
@ -13,201 +10,223 @@
* Notice: This script defaults the button_tile_map option to true for extra performance.
*/
@require_once('../../../wp-config.php');
@require_once('../../../wp-config.php'); // For get_bloginfo().
// gzip_compression();
// Get input
$plugins = explode(',', getParam("plugins", ""));
$languages = explode(',', getParam("languages", ""));
$themes = explode(',', getParam("themes", ""));
$diskCache = getParam("diskcache", "") == "true";
$isJS = getParam("js", "") == "true";
$compress = getParam("compress", "true") == "true";
$suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
$cachePath = realpath("."); // Cache path, this is where the .gz files will be stored
$expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
$content = "";
$encodings = array();
$supportsGzip = false;
$enc = "";
$cacheKey = "";
function wp_tinymce_lang($path) {
global $language;
// Custom extra javascripts to pack
$custom = array(/*
"some custom .js file",
"some custom .js file"
*/);
$text = '';
// Look for xx_YY.js, xx_yy.js, xx.js
$file = realpath(sprintf($path, $language));
if ( file_exists($file) )
$text = file_get_contents($file);
$file = realpath(sprintf($path, strtolower($language)));
if ( file_exists($file) )
$text = file_get_contents($file);
$file = realpath(sprintf($path, substr($language, 0, 2)));
if ( file_exists($file) )
$text = file_get_contents($file);
// Fall back on en.js
$file = realpath(sprintf($path, 'en'));
if ( empty($text) && file_exists($file) )
$text = file_get_contents($file);
// Send lang file through gettext
if ( function_exists('__') && strtolower(substr($language, 0, 2)) != 'en' ) {
$search1 = "/^tinyMCELang\\[(['\"])(.*)\\1\]( ?= ?)(['\"])(.*)\\4/Uem";
$replace1 = "'tinyMCELang[\\1\\2\\1]\\3'.stripslashes('\\4').__('\\5').stripslashes('\\4')";
$search2 = "/\\s:\\s(['\"])(.*)\\1(,|\\s*})/Uem";
$replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1').'\\3'";
$search = array($search1, $search2);
$replace = array($replace1, $replace2);
$text = preg_replace($search, $replace, $text);
return $text;
}
return $text;
}
function wp_compact_tinymce_js($text) {
// This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
// Strip comments
$text = preg_replace("!(^|\s+)//.*$!m", '', $text);
$text = preg_replace("!/\*.*?\*/!s", '', $text);
// Strip leading tabs, carriage returns and unnecessary line breaks.
$text = preg_replace("!^\t+!m", '', $text);
$text = str_replace("\r", '', $text);
$text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
return "$text\n";
}
// General options
$suffix = ""; // Set to "_src" to use source version
$expiresOffset = 3600 * 24 * 10; // 10 days util client cache expires
$diskCache = false; // If you enable this option gzip files will be cached on disk.
$cacheDir = realpath("."); // Absolute directory path to where cached gz files will be stored
$debug = false; // Enable this option if you need debuging info
// Headers
header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
// header("Cache-Control: must-revalidate");
header("Vary: Accept-Encoding"); // Handle proxies
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
// Get data to load
$theme = isset($_GET['theme']) ? TinyMCE_cleanInput($_GET['theme']) : "";
$language = isset($_GET['language']) ? TinyMCE_cleanInput($_GET['language']) : "";
$plugins = isset($_GET['plugins']) ? TinyMCE_cleanInput($_GET['plugins']) : "";
$lang = isset($_GET['lang']) ? TinyMCE_cleanInput($_GET['lang']) : "en";
$index = isset($_GET['index']) ? TinyMCE_cleanInput($_GET['index']) : -1;
$cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug);
$cacheFile = $cacheDir == "" ? "" : $cacheDir . "/" . "tinymce_" . $cacheKey . ".gz";
$cacheData = "";
// Patch older versions of PHP < 4.3.0
if (!function_exists('file_get_contents')) {
function file_get_contents($filename) {
$fd = fopen($filename, 'rb');
$content = fread($fd, filesize($filename));
fclose($fd);
return $content;
}
}
// Security check function, can only contain a-z 0-9 , _ - and whitespace.
function TinyMCE_cleanInput($str) {
return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_
}
function TinyMCE_echo($str) {
global $cacheData, $diskCache;
if ($diskCache)
$cacheData .= $str;
else
echo $str;
}
// Only gzip the contents if clients and server support it
$encodings = array();
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
// Check for gzip header or northon internet securities
if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
// Use cached file if it exists but not in debug mode
if (file_exists($cacheFile) && !$debug) {
header("Content-Encoding: " . $enc);
echo file_get_contents($cacheFile);
die;
}
if (!$diskCache)
ob_start("ob_gzhandler");
} else
// WP
$index = getParam("index", -1);
$theme = getParam("theme", "");
$themes = array($theme);
$language = getParam("language", "en");
if ( empty($language) )
$language = 'en';
$languages = array($language);
if ( $language != strtolower($language) )
$languages[] = strtolower($language);
if ( $language != substr($language, 0, 2) )
$languages[] = substr($language, 0, 2);
$diskCache = false;
$isJS = true;
$suffix = '';
// Headers
header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
header("Vary: Accept-Encoding"); // Handle proxies
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
// Is called directly then auto init with default settings
if (!$isJS) {
echo getFileContents("tiny_mce_gzip.js");
echo "tinyMCE_GZ.init({});";
die();
}
// Setup cache info
if ($diskCache) {
if (!$cachePath)
die("alert('Real path failed.');");
$cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "");
foreach ($custom as $file)
$cacheKey .= $file;
$cacheKey = md5($cacheKey);
if ($compress)
$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
else
$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
}
// Check if it supports gzip
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
$supportsGzip = true;
}
// Use cached file disk cache
if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
if ($compress)
header("Content-Encoding: " . $enc);
echo getFileContents($cacheFile);
die();
}
if ($index > -1) {
// Write main script and patch some things
if ($index == 0) {
TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("tiny_mce" . $suffix . ".js")))); // WP
TinyMCE_echo('TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;');
TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;');
if ( $index == 0 ) {
// Add core
$content .= wp_compact_tinymce_js(getFileContents("tiny_mce" . $suffix . ".js"));
$content .= 'TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;';
$content .= 'TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;';
} else
TinyMCE_echo('tinyMCE = realTinyMCE;');
$content .= 'tinyMCE = realTinyMCE;';
// Patch loading functions
//$content .= "tinyMCE_GZ.start();";
// Do init based on index
TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);");
$content .= "tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);";
// Load external plugins
if ($index == 0)
TinyMCE_echo("tinyMCECompressed.loadPlugins();");
if ( $index == 0 )
$content .= "tinyMCECompressed.loadPlugins();";
// Load theme, language pack and theme language packs
if ($theme) {
TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("themes/" . $theme . "/editor_template" . $suffix . ".js")))); // WP
TinyMCE_echo(wp_tinymce_lang("themes/" . $theme . "/langs/%s.js")); // WP
// Add core languages
foreach ($languages as $lang)
$content .= getFileContents("langs/" . $lang . ".js");
// Add themes
foreach ($themes as $theme) {
$content .= wp_compact_tinymce_js(getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js"));
foreach ($languages as $lang)
$content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
}
/* WP if ($language) WP */
TinyMCE_echo(wp_tinymce_lang("langs/%s.js")); // WP
// Load all plugins and their language packs
$plugins = explode(",", $plugins);
// Add plugins
foreach ($plugins as $plugin) {
$pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
/* WP $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js"); WP */
$content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
if ($pluginFile)
TinyMCE_echo(file_get_contents($pluginFile));
/* WP if ($languageFile) WP */
TinyMCE_echo(wp_tinymce_lang("plugins/" . $plugin . "/langs/%s.js")); // WP
foreach ($languages as $lang)
$content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
}
// Add custom files
foreach ($custom as $file)
$content .= getFileContents($file);
// Reset tinyMCE compressor engine
TinyMCE_echo("tinyMCE = tinyMCECompressed;");
$content .= "tinyMCE = tinyMCECompressed;";
// Write to cache
if ($diskCache) {
// Calculate compression ratio and debug target output path
if ($debug) {
$ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP)) / strlen($cacheData) * 100.0);
TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');");
}
// Restore loading functions
//$content .= "tinyMCE_GZ.end();";
$cacheData = gzencode($cacheData, 9, FORCE_GZIP);
// Generate GZIP'd content
if ($supportsGzip) {
if ($compress) {
header("Content-Encoding: " . $enc);
$cacheData = gzencode($content, 9, FORCE_GZIP);
} else
$cacheData = $content;
// Write to file if possible
$fp = @fopen($cacheFile, "wb");
if ($fp) {
fwrite($fp, $cacheData);
fclose($fp);
}
// Write gz file
if ($diskCache && $cacheKey != "")
putFileContents($cacheFile, $cacheData);
// Output
header("Content-Encoding: " . $enc);
// Stream to client
echo $cacheData;
} else {
// Stream uncompressed content
echo $content;
}
die;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function getParam($name, $def = false) {
if (!isset($_GET[$name]))
return $def;
return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
}
function getFileContents($path) {
$path = realpath($path);
if (!$path || !@is_file($path))
return "";
if (function_exists("file_get_contents"))
return @file_get_contents($path);
$content = "";
$fp = @fopen($path, "r");
if (!$fp)
return "";
while (!feof($fp))
$content .= fgets($fp);
fclose($fp);
return $content;
}
function putFileContents($path, $content) {
if (function_exists("file_put_contents"))
return @file_put_contents($path, $content);
$fp = @fopen($path, "wb");
if ($fp) {
fwrite($fp, $content);
fclose($fp);
}
}
// WP specific
function wp_compact_tinymce_js($text) {
// This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
// Strip comments
$text = preg_replace("!(^|\s+)//.*$!m", '', $text);
$text = preg_replace("!/\*.*?\*/!s", '', $text);
// Strip leading tabs, carriage returns and unnecessary line breaks.
$text = preg_replace("!^\t+!m", '', $text);
$text = str_replace("\r", '', $text);
$text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
return "$text\n";
}
?>
function TinyMCECompressed() {

View File

@ -15,7 +15,7 @@ class WP_Scripts {
$this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
$this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3517' );
$this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
$this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '20070124' );
$this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '20070325' );
$mce_config = apply_filters('tiny_mce_config_url', '/wp-includes/js/tinymce/tiny_mce_config.php');
$this->add( 'wp_tiny_mce', $mce_config, array('tiny_mce'), '20070225' );
$this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0-0');