2005-08-23 03:33:52 +02:00
|
|
|
<?php
|
2006-03-30 09:50:33 +02:00
|
|
|
/**
|
2007-03-26 05:44:35 +02:00
|
|
|
* $Id: tiny_mce_gzip.php 158 2006-12-21 14:32:19Z spocke $
|
2006-03-30 09:50:33 +02:00
|
|
|
*
|
|
|
|
* @author Moxiecode
|
2007-01-31 03:07:21 +01:00
|
|
|
* @copyright Copyright 2005-2006, Moxiecode Systems AB, All rights reserved.
|
2006-03-30 09:50:33 +02:00
|
|
|
*
|
|
|
|
* This file compresses the TinyMCE JavaScript using GZip and
|
|
|
|
* enables the browser to do two requests instead of one for each .js file.
|
|
|
|
* Notice: This script defaults the button_tile_map option to true for extra performance.
|
|
|
|
*/
|
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
@require_once('../../../wp-config.php'); // For get_bloginfo().
|
|
|
|
|
|
|
|
// 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 = "";
|
|
|
|
|
|
|
|
// Custom extra javascripts to pack
|
|
|
|
$custom = array(/*
|
|
|
|
"some custom .js file",
|
|
|
|
"some custom .js file"
|
|
|
|
*/);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Setup cache info
|
|
|
|
if ($diskCache) {
|
|
|
|
if (!$cachePath)
|
|
|
|
die("alert('Real path failed.');");
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
$cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "");
|
2005-11-06 04:30:11 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
foreach ($custom as $file)
|
|
|
|
$cacheKey .= $file;
|
2005-12-05 04:04:25 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
$cacheKey = md5($cacheKey);
|
2005-12-05 04:04:25 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
if ($compress)
|
|
|
|
$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
|
|
|
|
else
|
|
|
|
$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
|
|
|
|
}
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Check if it supports gzip
|
|
|
|
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
|
|
|
|
$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-04-27 22:55:05 +02:00
|
|
|
if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression') && ini_get('output_handler') != 'ob_gzhandler') {
|
2007-03-26 05:44:35 +02:00
|
|
|
$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
|
|
|
|
$supportsGzip = true;
|
|
|
|
}
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Use cached file disk cache
|
|
|
|
if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
|
|
|
|
if ($compress)
|
|
|
|
header("Content-Encoding: " . $enc);
|
2005-12-05 04:04:25 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
echo getFileContents($cacheFile);
|
|
|
|
die();
|
2005-12-05 04:04:25 +01:00
|
|
|
}
|
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
if ($index > -1) {
|
|
|
|
// Write main script and patch some things
|
|
|
|
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
|
|
|
|
$content .= 'tinyMCE = realTinyMCE;';
|
2005-11-06 04:30:11 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Patch loading functions
|
|
|
|
//$content .= "tinyMCE_GZ.start();";
|
2007-06-14 04:25:30 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Do init based on index
|
|
|
|
$content .= "tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);";
|
2005-08-23 03:33:52 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Load external plugins
|
|
|
|
if ( $index == 0 )
|
|
|
|
$content .= "tinyMCECompressed.loadPlugins();";
|
2005-12-12 02:27:56 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Add core languages
|
2007-03-27 00:05:36 +02:00
|
|
|
$lang_content = '';
|
2007-03-26 05:44:35 +02:00
|
|
|
foreach ($languages as $lang)
|
2007-03-27 00:05:36 +02:00
|
|
|
$lang_content .= getFileContents("langs/" . $lang . ".js");
|
|
|
|
if ( empty($lang_content) )
|
|
|
|
$lang_content .= getFileContents("langs/en.js");
|
|
|
|
$content .= $lang_content;
|
2005-12-12 02:27:56 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Add themes
|
|
|
|
foreach ($themes as $theme) {
|
|
|
|
$content .= wp_compact_tinymce_js(getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js"));
|
2006-03-06 23:51:28 +01:00
|
|
|
|
2007-03-27 00:05:36 +02:00
|
|
|
$lang_content = '';
|
2007-03-26 05:44:35 +02:00
|
|
|
foreach ($languages as $lang)
|
2007-03-27 00:05:36 +02:00
|
|
|
$lang_content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
|
|
|
|
if ( empty($lang_content) )
|
|
|
|
$lang_content .= getFileContents("themes/" . $theme . "/langs/en.js");
|
|
|
|
$content .= $lang_content;
|
2006-03-30 09:50:33 +02:00
|
|
|
}
|
2006-03-06 23:51:28 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Add plugins
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
|
2006-03-06 23:51:28 +01:00
|
|
|
|
2007-03-27 00:05:36 +02:00
|
|
|
$lang_content = '';
|
2007-03-26 05:44:35 +02:00
|
|
|
foreach ($languages as $lang)
|
2007-03-27 00:05:36 +02:00
|
|
|
$lang_content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
|
|
|
|
if ( empty($lang_content) )
|
|
|
|
$lang_content .= getFileContents("plugins/" . $plugin . "/langs/en.js");
|
|
|
|
$content .= $lang_content;
|
2007-03-26 05:44:35 +02:00
|
|
|
}
|
2005-12-12 02:27:56 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Add custom files
|
|
|
|
foreach ($custom as $file)
|
|
|
|
$content .= getFileContents($file);
|
2006-04-13 04:57:25 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Reset tinyMCE compressor engine
|
|
|
|
$content .= "tinyMCE = tinyMCECompressed;";
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Restore loading functions
|
|
|
|
//$content .= "tinyMCE_GZ.end();";
|
2006-03-06 23:51:28 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Generate GZIP'd content
|
|
|
|
if ($supportsGzip) {
|
|
|
|
if ($compress) {
|
|
|
|
header("Content-Encoding: " . $enc);
|
|
|
|
$cacheData = gzencode($content, 9, FORCE_GZIP);
|
|
|
|
} else
|
|
|
|
$cacheData = $content;
|
2006-04-13 04:57:25 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Write gz file
|
|
|
|
if ($diskCache && $cacheKey != "")
|
|
|
|
putFileContents($cacheFile, $cacheData);
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// Stream to client
|
|
|
|
echo $cacheData;
|
|
|
|
} else {
|
|
|
|
// Stream uncompressed content
|
|
|
|
echo $content;
|
|
|
|
}
|
2006-04-13 04:57:25 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
die;
|
|
|
|
}
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
function getParam($name, $def = false) {
|
|
|
|
if (!isset($_GET[$name]))
|
|
|
|
return $def;
|
2006-04-13 04:57:25 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
|
2006-03-30 09:50:33 +02:00
|
|
|
}
|
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
function getFileContents($path) {
|
|
|
|
$path = realpath($path);
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
if (!$path || !@is_file($path))
|
|
|
|
return "";
|
2005-12-12 02:27:56 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
if (function_exists("file_get_contents"))
|
|
|
|
return @file_get_contents($path);
|
2005-12-12 02:27:56 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
$content = "";
|
|
|
|
$fp = @fopen($path, "r");
|
|
|
|
if (!$fp)
|
|
|
|
return "";
|
2005-08-23 03:33:52 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
while (!feof($fp))
|
|
|
|
$content .= fgets($fp);
|
2005-12-15 22:42:38 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
2005-12-15 22:42:38 +01:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
function putFileContents($path, $content) {
|
|
|
|
if (function_exists("file_put_contents"))
|
|
|
|
return @file_put_contents($path, $content);
|
2006-03-30 09:50:33 +02:00
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
$fp = @fopen($path, "wb");
|
2006-03-30 09:50:33 +02:00
|
|
|
if ($fp) {
|
2007-03-26 05:44:35 +02:00
|
|
|
fwrite($fp, $content);
|
2006-03-30 09:50:33 +02:00
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-26 05:44:35 +02:00
|
|
|
// 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";
|
|
|
|
}
|
2005-12-15 22:42:38 +01:00
|
|
|
?>
|
|
|
|
|
2006-03-30 09:50:33 +02:00
|
|
|
function TinyMCECompressed() {
|
|
|
|
this.configs = new Array();
|
|
|
|
this.loadedFiles = new Array();
|
2006-04-13 04:57:25 +02:00
|
|
|
this.externalPlugins = new Array();
|
2006-03-30 09:50:33 +02:00
|
|
|
this.loadAdded = false;
|
|
|
|
this.isLoaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.init = function(settings) {
|
|
|
|
var elements = document.getElementsByTagName('script');
|
|
|
|
var scriptURL = "";
|
|
|
|
|
|
|
|
for (var i=0; i<elements.length; i++) {
|
|
|
|
if (elements[i].src && elements[i].src.indexOf("tiny_mce_gzip.php") != -1) {
|
|
|
|
scriptURL = elements[i].src;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
settings["theme"] = typeof(settings["theme"]) != "undefined" ? settings["theme"] : "default";
|
|
|
|
settings["plugins"] = typeof(settings["plugins"]) != "undefined" ? settings["plugins"] : "";
|
|
|
|
settings["language"] = typeof(settings["language"]) != "undefined" ? settings["language"] : "en";
|
|
|
|
settings["button_tile_map"] = typeof(settings["button_tile_map"]) != "undefined" ? settings["button_tile_map"] : true;
|
|
|
|
this.configs[this.configs.length] = settings;
|
|
|
|
this.settings = settings;
|
|
|
|
|
2006-04-13 04:57:25 +02:00
|
|
|
scriptURL += (scriptURL.indexOf('?') == -1) ? '?' : '&';
|
|
|
|
scriptURL += "theme=" + escape(this.getOnce(settings["theme"])) + "&language=" + escape(this.getOnce(settings["language"])) + "&plugins=" + escape(this.getOnce(settings["plugins"])) + "&lang=" + settings["language"] + "&index=" + escape(this.configs.length-1);
|
2006-03-30 09:50:33 +02:00
|
|
|
document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + scriptURL + '"></script>');
|
|
|
|
|
|
|
|
if (!this.loadAdded) {
|
|
|
|
tinyMCE.addEvent(window, "DOMContentLoaded", TinyMCECompressed.prototype.onLoad);
|
|
|
|
tinyMCE.addEvent(window, "load", TinyMCECompressed.prototype.onLoad);
|
|
|
|
this.loadAdded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.onLoad = function() {
|
|
|
|
if (tinyMCE.isLoaded)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
tinyMCE = realTinyMCE;
|
|
|
|
TinyMCE_Engine.prototype.onLoad();
|
|
|
|
tinyMCE._addUnloadEvents();
|
2006-04-13 04:57:25 +02:00
|
|
|
|
2006-03-30 09:50:33 +02:00
|
|
|
tinyMCE.isLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.addEvent = function(o, n, h) {
|
|
|
|
if (o.attachEvent)
|
|
|
|
o.attachEvent("on" + n, h);
|
|
|
|
else
|
|
|
|
o.addEventListener(n, h, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.getOnce = function(str) {
|
2006-04-13 04:57:25 +02:00
|
|
|
var ar = str.replace(/\s+/g, '').split(',');
|
2006-03-30 09:50:33 +02:00
|
|
|
|
|
|
|
for (var i=0; i<ar.length; i++) {
|
2006-04-13 04:57:25 +02:00
|
|
|
if (ar[i] == '' || ar[i].charAt(0) == '-') {
|
|
|
|
ar[i] = null;
|
2006-03-30 09:50:33 +02:00
|
|
|
continue;
|
2006-04-13 04:57:25 +02:00
|
|
|
}
|
2006-03-30 09:50:33 +02:00
|
|
|
|
|
|
|
// Skip load
|
|
|
|
for (var x=0; x<this.loadedFiles.length; x++) {
|
|
|
|
if (this.loadedFiles[x] == ar[i])
|
|
|
|
ar[i] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadedFiles[this.loadedFiles.length] = ar[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Glue
|
|
|
|
str = "";
|
|
|
|
for (var i=0; i<ar.length; i++) {
|
|
|
|
if (ar[i] == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
str += ar[i];
|
|
|
|
|
|
|
|
if (i != ar.length-1)
|
|
|
|
str += ",";
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
2006-04-13 04:57:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.loadPlugins = function() {
|
|
|
|
var i, ar;
|
|
|
|
|
|
|
|
TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
|
|
|
|
tinyMCE = realTinyMCE;
|
|
|
|
|
|
|
|
ar = tinyMCECompressed.externalPlugins;
|
|
|
|
for (i=0; i<ar.length; i++)
|
|
|
|
tinyMCE.loadPlugin(ar[i].name, ar[i].url);
|
|
|
|
|
|
|
|
TinyMCE.prototype.loadScript = function() {};
|
|
|
|
};
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.loadPlugin = function(n, u) {
|
|
|
|
this.externalPlugins[this.externalPlugins.length] = {name : n, url : u};
|
|
|
|
};
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.importPluginLanguagePack = function(n, v) {
|
|
|
|
tinyMCE = realTinyMCE;
|
|
|
|
TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
|
|
|
|
tinyMCE.importPluginLanguagePack(n, v);
|
|
|
|
};
|
|
|
|
|
|
|
|
TinyMCECompressed.prototype.addPlugin = function(n, p) {
|
|
|
|
tinyMCE = realTinyMCE;
|
|
|
|
tinyMCE.addPlugin(n, p);
|
|
|
|
};
|
2005-08-23 03:33:52 +02:00
|
|
|
|
2006-03-30 09:50:33 +02:00
|
|
|
var tinyMCE = new TinyMCECompressed();
|
|
|
|
var tinyMCECompressed = tinyMCE;
|