Make WordPress Core


Ignore:
Timestamp:
03/26/2007 03:44:35 AM (20 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/tinymce/tiny_mce_gzip.php

    r4845 r5108  
    11<?php
    22/**
    3  * $RCSfile: tiny_mce_gzip.php,v $
    4  * $Revision: $
    5  * $Date: $
     3 * $Id: tiny_mce_gzip.php 158 2006-12-21 14:32:19Z spocke $
    64 *
    7  * @version 1.08
    85 * @author Moxiecode
    96 * @copyright Copyright  2005-2006, Moxiecode Systems AB, All rights reserved.
    … …  
    1411 */
    1512
    16 @require_once('../../../wp-config.php');
    17 
    18 // gzip_compression();
    19 
    20 function wp_tinymce_lang($path) {
    21         global $language;
    22 
    23         $text = '';
    24 
    25         // Look for xx_YY.js, xx_yy.js, xx.js
    26         $file = realpath(sprintf($path, $language));
    27         if ( file_exists($file) )
    28                 $text = file_get_contents($file);
    29         $file = realpath(sprintf($path, strtolower($language)));
    30         if ( file_exists($file) )
    31                 $text = file_get_contents($file);
    32         $file = realpath(sprintf($path, substr($language, 0, 2)));
    33         if ( file_exists($file) )
    34                 $text = file_get_contents($file);
    35 
    36 
    37         // Fall back on en.js
    38         $file = realpath(sprintf($path, 'en'));
    39         if ( empty($text) && file_exists($file) )
    40                 $text = file_get_contents($file);
    41 
    42         // Send lang file through gettext
    43         if ( function_exists('__') && strtolower(substr($language, 0, 2)) != 'en' ) {
    44                 $search1 = "/^tinyMCELang\\[(['\"])(.*)\\1\]( ?= ?)(['\"])(.*)\\4/Uem";
    45                 $replace1 = "'tinyMCELang[\\1\\2\\1]\\3'.stripslashes('\\4').__('\\5').stripslashes('\\4')";
    46 
    47                 $search2 = "/\\s:\\s(['\"])(.*)\\1(,|\\s*})/Uem";
    48                 $replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1').'\\3'";
    49 
    50                 $search = array($search1, $search2);
    51                 $replace = array($replace1, $replace2);
    52 
    53                 $text = preg_replace($search, $replace, $text);
    54 
    55                 return $text;
    56         }
    57 
    58         return $text;
    59 }
    60 
    61 function wp_compact_tinymce_js($text) {
    62         // This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
    63 
    64         // Strip comments
    65         $text = preg_replace("!(^|\s+)//.*$!m", '', $text);
    66         $text = preg_replace("!/\*.*?\*/!s", '', $text);
    67 
    68         // Strip leading tabs, carriage returns and unnecessary line breaks.
    69         $text = preg_replace("!^\t+!m", '', $text);
    70         $text = str_replace("\r", '', $text);
    71         $text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
    72 
    73         return "$text\n";
    74 }
    75 
    76 
    77 // General options
    78 $suffix = "";                                                   // Set to "_src" to use source version
    79 $expiresOffset = 3600 * 24 * 10;                // 10 days util client cache expires
    80 $diskCache = false;                                             // If you enable this option gzip files will be cached on disk.
    81 $cacheDir = realpath(".");                              // Absolute directory path to where cached gz files will be stored
    82 $debug = false;                                                 // Enable this option if you need debuging info
    83 
    84 // Headers
    85 header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
    86 // header("Cache-Control: must-revalidate");
    87 header("Vary: Accept-Encoding"); // Handle proxies
    88 header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
    89 
    90 // Get data to load
    91 $theme = isset($_GET['theme']) ? TinyMCE_cleanInput($_GET['theme']) : "";
    92 $language = isset($_GET['language']) ? TinyMCE_cleanInput($_GET['language']) : "";
    93 $plugins = isset($_GET['plugins']) ? TinyMCE_cleanInput($_GET['plugins']) : "";
    94 $lang = isset($_GET['lang']) ? TinyMCE_cleanInput($_GET['lang']) : "en";
    95 $index = isset($_GET['index']) ? TinyMCE_cleanInput($_GET['index']) : -1;
    96 $cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug);
    97 $cacheFile = $cacheDir == "" ? "" : $cacheDir . "/" . "tinymce_" .  $cacheKey . ".gz";
    98 $cacheData = "";
    99 
    100 // Patch older versions of PHP < 4.3.0
    101 if (!function_exists('file_get_contents')) {
    102         function file_get_contents($filename) {
    103                 $fd = fopen($filename, 'rb');
    104                 $content = fread($fd, filesize($filename));
    105                 fclose($fd);
    106                 return $content;
    107         }
    108 }
    109 
    110 // Security check function, can only contain a-z 0-9 , _ - and whitespace.
    111 function TinyMCE_cleanInput($str) {
    112         return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_
    113 }
    114 
    115 function TinyMCE_echo($str) {
    116         global $cacheData, $diskCache;
    117 
    118         if ($diskCache)
    119                 $cacheData .= $str;
    120         else
    121                 echo $str;
    122 }
    123 
    124 // Only gzip the contents if clients and server support it
    125 $encodings = array();
    126 
    127 if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
    128         $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
    129 
    130 // Check for gzip header or northon internet securities
    131 if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
    132         $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
    133 
    134         // Use cached file if it exists but not in debug mode
    135         if (file_exists($cacheFile) && !$debug) {
    136                 header("Content-Encoding: " . $enc);
    137                 echo file_get_contents($cacheFile);
    138                 die;
    139         }
    140 
    141         if (!$diskCache)
    142                 ob_start("ob_gzhandler");
    143 } else
     13        @require_once('../../../wp-config.php');  // For get_bloginfo().
     14
     15        // Get input
     16        $plugins = explode(',', getParam("plugins", ""));
     17        $languages = explode(',', getParam("languages", ""));
     18        $themes = explode(',', getParam("themes", ""));
     19        $diskCache = getParam("diskcache", "") == "true";
     20        $isJS = getParam("js", "") == "true";
     21        $compress = getParam("compress", "true") == "true";
     22        $suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
     23        $cachePath = realpath("."); // Cache path, this is where the .gz files will be stored
     24        $expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
     25        $content = "";
     26        $encodings = array();
     27        $supportsGzip = false;
     28        $enc = "";
     29        $cacheKey = "";
     30
     31        // Custom extra javascripts to pack
     32        $custom = array(/*
     33                "some custom .js file",
     34                "some custom .js file"
     35        */);
     36
     37        // WP
     38        $index = getParam("index", -1);
     39        $theme = getParam("theme", "");
     40        $themes = array($theme);
     41        $language = getParam("language", "en");
     42        if ( empty($language) )
     43                $language = 'en';
     44        $languages = array($language);
     45        if ( $language != strtolower($language) )
     46                $languages[] = strtolower($language);
     47        if ( $language != substr($language, 0, 2) )
     48                $languages[] = substr($language, 0, 2);
    14449        $diskCache = false;
     50        $isJS = true;
     51        $suffix = '';
     52
     53        // Headers
     54        header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
     55        header("Vary: Accept-Encoding");  // Handle proxies
     56        header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
     57
     58        // Is called directly then auto init with default settings
     59        if (!$isJS) {
     60                echo getFileContents("tiny_mce_gzip.js");
     61                echo "tinyMCE_GZ.init({});";
     62                die();
     63        }
     64
     65        // Setup cache info
     66        if ($diskCache) {
     67                if (!$cachePath)
     68                        die("alert('Real path failed.');");
     69
     70                $cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "");
     71
     72                foreach ($custom as $file)
     73                        $cacheKey .= $file;
     74
     75                $cacheKey = md5($cacheKey);
     76
     77                if ($compress)
     78                        $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
     79                else
     80                        $cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
     81        }
     82
     83        // Check if it supports gzip
     84        if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
     85                $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
     86
     87        if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
     88                $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
     89                $supportsGzip = true;
     90        }
     91
     92        // Use cached file disk cache
     93        if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
     94                if ($compress)
     95                        header("Content-Encoding: " . $enc);
     96
     97                echo getFileContents($cacheFile);
     98                die();
     99        }
    145100
    146101if ($index > -1) {
    147102        // Write main script and patch some things
    148         if ($index == 0) {
    149                 TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("tiny_mce" . $suffix . ".js")))); // WP
    150                 TinyMCE_echo('TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;');
    151                 TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;');
     103        if ( $index == 0 ) {
     104                // Add core
     105                $content .= wp_compact_tinymce_js(getFileContents("tiny_mce" . $suffix . ".js"));
     106                $content .= 'TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;';
     107                $content .= 'TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;';
    152108        } else
    153                 TinyMCE_echo('tinyMCE = realTinyMCE;');
    154 
     109                $content .= 'tinyMCE = realTinyMCE;';
     110
     111        // Patch loading functions
     112        //$content .= "tinyMCE_GZ.start();";
     113       
    155114        // Do init based on index
    156         TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);");
     115        $content .= "tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);";
    157116
    158117        // Load external plugins
    159         if ($index == 0)
    160                 TinyMCE_echo("tinyMCECompressed.loadPlugins();");
    161 
    162         // Load theme, language pack and theme language packs
    163         if ($theme) {
    164                 TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("themes/" . $theme . "/editor_template" . $suffix . ".js")))); // WP
    165                 TinyMCE_echo(wp_tinymce_lang("themes/" . $theme . "/langs/%s.js")); // WP
    166         }
    167 
    168         /* WP if ($language) WP */
    169                 TinyMCE_echo(wp_tinymce_lang("langs/%s.js")); // WP
    170 
    171         // Load all plugins and their language packs
    172         $plugins = explode(",", $plugins);
     118        if ( $index == 0 )
     119                $content .= "tinyMCECompressed.loadPlugins();";
     120
     121        // Add core languages
     122        foreach ($languages as $lang)
     123                $content .= getFileContents("langs/" . $lang . ".js");
     124
     125        // Add themes
     126        foreach ($themes as $theme) {
     127                $content .= wp_compact_tinymce_js(getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js"));
     128
     129                foreach ($languages as $lang)
     130                        $content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
     131        }
     132
     133        // Add plugins
    173134        foreach ($plugins as $plugin) {
    174                 $pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
    175                 /* WP $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js"); WP */
    176 
    177                 if ($pluginFile)
    178                         TinyMCE_echo(file_get_contents($pluginFile));
    179 
    180                 /* WP if ($languageFile) WP */
    181                         TinyMCE_echo(wp_tinymce_lang("plugins/" . $plugin . "/langs/%s.js")); // WP
    182         }
     135                $content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
     136
     137                foreach ($languages as $lang)
     138                        $content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
     139        }
     140
     141        // Add custom files
     142        foreach ($custom as $file)
     143                $content .= getFileContents($file);
    183144
    184145        // Reset tinyMCE compressor engine
    185         TinyMCE_echo("tinyMCE = tinyMCECompressed;");
    186 
    187         // Write to cache
    188         if ($diskCache) {
    189                 // Calculate compression ratio and debug target output path
    190                 if ($debug) {
    191                         $ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP)) / strlen($cacheData) * 100.0);
    192                         TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');");
    193                 }
    194 
    195                 $cacheData = gzencode($cacheData, 9, FORCE_GZIP);
    196 
    197                 // Write to file if possible
    198                 $fp = @fopen($cacheFile, "wb");
     146        $content .= "tinyMCE = tinyMCECompressed;";
     147
     148        // Restore loading functions
     149        //$content .= "tinyMCE_GZ.end();";
     150
     151        // Generate GZIP'd content
     152        if ($supportsGzip) {
     153                if ($compress) {
     154                        header("Content-Encoding: " . $enc);
     155                        $cacheData = gzencode($content, 9, FORCE_GZIP);
     156                } else
     157                        $cacheData = $content;
     158
     159                // Write gz file
     160                if ($diskCache && $cacheKey != "")
     161                        putFileContents($cacheFile, $cacheData);
     162
     163                // Stream to client
     164                echo $cacheData;
     165        } else {
     166                // Stream uncompressed content
     167                echo $content;
     168        }
     169
     170        die;
     171}
     172
     173        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
     174
     175        function getParam($name, $def = false) {
     176                if (!isset($_GET[$name]))
     177                        return $def;
     178
     179                return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
     180        }
     181
     182        function getFileContents($path) {
     183                $path = realpath($path);
     184
     185                if (!$path || !@is_file($path))
     186                        return "";
     187
     188                if (function_exists("file_get_contents"))
     189                        return @file_get_contents($path);
     190
     191                $content = "";
     192                $fp = @fopen($path, "r");
     193                if (!$fp)
     194                        return "";
     195
     196                while (!feof($fp))
     197                        $content .= fgets($fp);
     198
     199                fclose($fp);
     200
     201                return $content;
     202        }
     203
     204        function putFileContents($path, $content) {
     205                if (function_exists("file_put_contents"))
     206                        return @file_put_contents($path, $content);
     207
     208                $fp = @fopen($path, "wb");
    199209                if ($fp) {
    200                         fwrite($fp, $cacheData);
     210                        fwrite($fp, $content);
    201211                        fclose($fp);
    202212                }
    203 
    204                 // Output
    205                 header("Content-Encoding: " . $enc);
    206                 echo $cacheData;
    207         }
    208 
    209         die;
    210 }
     213        }
     214
     215        // WP specific
     216        function wp_compact_tinymce_js($text) {
     217                // This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
     218
     219                // Strip comments
     220                $text = preg_replace("!(^|\s+)//.*$!m", '', $text);
     221                $text = preg_replace("!/\*.*?\*/!s", '', $text);
     222
     223                // Strip leading tabs, carriage returns and unnecessary line breaks.
     224                $text = preg_replace("!^\t+!m", '', $text);
     225                $text = str_replace("\r", '', $text);
     226                $text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
     227
     228                return "$text\n";
     229        }
    211230?>
    212231
Note: See TracChangeset for help on using the changeset viewer.