diff options
author | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2007-03-26 11:04:22 +0000 |
---|---|---|
committer | donncha <donncha@7be80a69-a1ef-0310-a953-fb0f7c49ff36> | 2007-03-26 11:04:22 +0000 |
commit | 6b2942866a251bb84a81cb35811ee9a1d55a8d26 (patch) | |
tree | 09b245f77ee6eaf5856b00b0e2e7f97bdb3550d8 /wp-includes/js/tinymce/tiny_mce_gzip.php | |
parent | 2e96b99ceb195735a641e299f3209840cc101052 (diff) | |
download | wordpress-mu-6b2942866a251bb84a81cb35811ee9a1d55a8d26.tar.gz wordpress-mu-6b2942866a251bb84a81cb35811ee9a1d55a8d26.tar.xz wordpress-mu-6b2942866a251bb84a81cb35811ee9a1d55a8d26.zip |
WP Merge to rev 5109
git-svn-id: http://svn.automattic.com/wordpress-mu/trunk@934 7be80a69-a1ef-0310-a953-fb0f7c49ff36
Diffstat (limited to 'wp-includes/js/tinymce/tiny_mce_gzip.php')
-rw-r--r-- | wp-includes/js/tinymce/tiny_mce_gzip.php | 331 |
1 files changed, 175 insertions, 156 deletions
diff --git a/wp-includes/js/tinymce/tiny_mce_gzip.php b/wp-includes/js/tinymce/tiny_mce_gzip.php index 0de8f42..d3d7c79 100644 --- a/wp-includes/js/tinymce/tiny_mce_gzip.php +++ b/wp-includes/js/tinymce/tiny_mce_gzip.php @@ -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'); - -// gzip_compression(); - -function wp_tinymce_lang($path) { - global $language; + @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(); + } - $text = ''; + // Setup cache info + if ($diskCache) { + if (!$cachePath) + die("alert('Real path failed.');"); - // 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); + $cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", ""); + foreach ($custom as $file) + $cacheKey .= $file; - // Fall back on en.js - $file = realpath(sprintf($path, 'en')); - if ( empty($text) && file_exists($file) ) - $text = file_get_contents($file); + $cacheKey = md5($cacheKey); - // 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')"; + if ($compress) + $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz"; + else + $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js"; + } - $search2 = "/\\s:\\s(['\"])(.*)\\1(,|\\s*})/Uem"; - $replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1').'\\3'"; + // Check if it supports gzip + if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) + $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))); - $search = array($search1, $search2); - $replace = array($replace1, $replace2); + 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; + } - $text = preg_replace($search, $replace, $text); + // Use cached file disk cache + if ($diskCache && $supportsGzip && file_exists($cacheFile)) { + if ($compress) + header("Content-Encoding: " . $enc); - return $text; + echo getFileContents($cacheFile); + die(); } - 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. +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;'; - // Strip comments - $text = preg_replace("!(^|\s+)//.*$!m", '', $text); - $text = preg_replace("!/\*.*?\*/!s", '', $text); + // Patch loading functions + //$content .= "tinyMCE_GZ.start();"; + + // Do init based on index + $content .= "tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);"; - // 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); + // Load external plugins + if ( $index == 0 ) + $content .= "tinyMCECompressed.loadPlugins();"; - return "$text\n"; -} + // 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")); -// 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; + foreach ($languages as $lang) + $content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js"); } -} -// 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,-_ -} + // Add plugins + foreach ($plugins as $plugin) { + $content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js"); -function TinyMCE_echo($str) { - global $cacheData, $diskCache; + foreach ($languages as $lang) + $content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js"); + } - if ($diskCache) - $cacheData .= $str; - else - echo $str; -} + // Add custom files + foreach ($custom as $file) + $content .= getFileContents($file); -// Only gzip the contents if clients and server support it -$encodings = array(); + // Reset tinyMCE compressor engine + $content .= "tinyMCE = tinyMCECompressed;"; -if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) - $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING']))); + // Restore loading functions + //$content .= "tinyMCE_GZ.end();"; -// 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"; + // Generate GZIP'd content + if ($supportsGzip) { + if ($compress) { + header("Content-Encoding: " . $enc); + $cacheData = gzencode($content, 9, FORCE_GZIP); + } else + $cacheData = $content; - // 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; - } + // Write gz file + if ($diskCache && $cacheKey != "") + putFileContents($cacheFile, $cacheData); - if (!$diskCache) - ob_start("ob_gzhandler"); -} else - $diskCache = false; + // Stream to client + echo $cacheData; + } else { + // Stream uncompressed content + echo $content; + } -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;'); - } else - TinyMCE_echo('tinyMCE = realTinyMCE;'); + die; +} - // Do init based on index - TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);"); + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - // Load external plugins - if ($index == 0) - TinyMCE_echo("tinyMCECompressed.loadPlugins();"); + function getParam($name, $def = false) { + if (!isset($_GET[$name])) + return $def; - // 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 + return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_ } - /* WP if ($language) WP */ - TinyMCE_echo(wp_tinymce_lang("langs/%s.js")); // WP + function getFileContents($path) { + $path = realpath($path); - // Load all plugins and their language packs - $plugins = explode(",", $plugins); - foreach ($plugins as $plugin) { - $pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js"); - /* WP $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js"); WP */ + if (!$path || !@is_file($path)) + return ""; - if ($pluginFile) - TinyMCE_echo(file_get_contents($pluginFile)); + if (function_exists("file_get_contents")) + return @file_get_contents($path); - /* WP if ($languageFile) WP */ - TinyMCE_echo(wp_tinymce_lang("plugins/" . $plugin . "/langs/%s.js")); // WP - } + $content = ""; + $fp = @fopen($path, "r"); + if (!$fp) + return ""; - // Reset tinyMCE compressor engine - TinyMCE_echo("tinyMCE = tinyMCECompressed;"); + while (!feof($fp)) + $content .= fgets($fp); - // 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 . "');"); - } + fclose($fp); + + return $content; + } - $cacheData = gzencode($cacheData, 9, FORCE_GZIP); + function putFileContents($path, $content) { + if (function_exists("file_put_contents")) + return @file_put_contents($path, $content); - // Write to file if possible - $fp = @fopen($cacheFile, "wb"); + $fp = @fopen($path, "wb"); if ($fp) { - fwrite($fp, $cacheData); + fwrite($fp, $content); fclose($fp); } - - // Output - header("Content-Encoding: " . $enc); - echo $cacheData; } - die; -} + // 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() { |