[ Index ]

WordPress Source Cross Reference

title

Body

[close]

/wp-includes/js/tinymce/ -> tiny_mce_gzip.php (source)

   1  <?php
   2  /**
   3   * $RCSfile: tiny_mce_gzip.php,v $
   4   * $Revision: $
   5   * $Date: $
   6   *
   7   * @version 1.08
   8   * @author Moxiecode
   9   * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved.
  10   *
  11   * This file compresses the TinyMCE JavaScript using GZip and
  12   * enables the browser to do two requests instead of one for each .js file.
  13   * Notice: This script defaults the button_tile_map option to true for extra performance.
  14   */
  15  
  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      if ( empty($text) )
  39          $text = file_get_contents(realpath(sprintf($path, 'en')));
  40  
  41      // Send lang file through gettext
  42      if ( function_exists('__') && strtolower(substr($language, 0, 2)) != 'en' ) {
  43          $search1 = "/^tinyMCELang\\[(['\"])(.*)\\1\]( ?= ?)(['\"])(.*)\\4/Uem";
  44          $replace1 = "'tinyMCELang[\\1\\2\\1]\\3'.stripslashes('\\4').__('\\5').stripslashes('\\4')";
  45  
  46          $search2 = "/\\s:\\s(['\"])(.*)\\1(,|\\s*})/Uem";
  47          $replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1').'\\3'";
  48  
  49          $search = array($search1, $search2);
  50          $replace = array($replace1, $replace2);
  51  
  52          $text = preg_replace($search, $replace, $text);
  53  
  54          return $text;
  55      }
  56  
  57      return $text;
  58  }
  59  
  60  function wp_compact_tinymce_js($text) {
  61      // This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
  62  
  63      // Strip comments
  64      $text = preg_replace("!(^|\s+)//.*$!m", '', $text);
  65      $text = preg_replace("!/\*.*?\*/!s", '', $text);
  66  
  67      // Strip leading tabs, carriage returns and unnecessary line breaks.
  68      $text = preg_replace("!^\t+!m", '', $text);
  69      $text = str_replace("\r", '', $text);
  70      $text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
  71  
  72      return "$text\n";
  73  }
  74  
  75  
  76  // General options
  77  $suffix = "";                            // Set to "_src" to use source version
  78  $expiresOffset = 3600 * 24 * 10;        // 10 days util client cache expires
  79  $diskCache = false;                        // If you enable this option gzip files will be cached on disk.
  80  $cacheDir = realpath(".");                // Absolute directory path to where cached gz files will be stored
  81  $debug = false;                            // Enable this option if you need debuging info
  82  
  83  // Headers
  84  header("Content-type: text/javascript; charset: UTF-8");
  85  // header("Cache-Control: must-revalidate");
  86  header("Vary: Accept-Encoding"); // Handle proxies
  87  header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
  88  
  89  // Get data to load
  90  $theme = isset($_GET['theme']) ? TinyMCE_cleanInput($_GET['theme']) : "";
  91  $language = isset($_GET['language']) ? TinyMCE_cleanInput($_GET['language']) : "";
  92  $plugins = isset($_GET['plugins']) ? TinyMCE_cleanInput($_GET['plugins']) : "";
  93  $lang = isset($_GET['lang']) ? TinyMCE_cleanInput($_GET['lang']) : "en";
  94  $index = isset($_GET['index']) ? TinyMCE_cleanInput($_GET['index']) : -1;
  95  $cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug);
  96  $cacheFile = $cacheDir == "" ? "" : $cacheDir . "/" . "tinymce_" .  $cacheKey . ".gz";
  97  $cacheData = "";
  98  
  99  // Patch older versions of PHP < 4.3.0
 100  if (!function_exists('file_get_contents')) {
 101  	function file_get_contents($filename) {
 102          $fd = fopen($filename, 'rb');
 103          $content = fread($fd, filesize($filename));
 104          fclose($fd);
 105          return $content;
 106      }
 107  }
 108  
 109  // Security check function, can only contain a-z 0-9 , _ - and whitespace.
 110  function TinyMCE_cleanInput($str) {
 111      return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_
 112  }
 113  
 114  function TinyMCE_echo($str) {
 115      global $cacheData, $diskCache;
 116  
 117      if ($diskCache)
 118          $cacheData .= $str;
 119      else
 120          echo $str;
 121  }
 122  
 123  // Only gzip the contents if clients and server support it
 124  $encodings = array();
 125  
 126  if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
 127      $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
 128  
 129  // Check for gzip header or northon internet securities
 130  if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
 131      $enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
 132  
 133      // Use cached file if it exists but not in debug mode
 134      if (file_exists($cacheFile) && !$debug) {
 135          header("Content-Encoding: " . $enc);
 136          echo file_get_contents($cacheFile);
 137          die;
 138      }
 139  
 140      if (!$diskCache)
 141          ob_start("ob_gzhandler");
 142  } else
 143      $diskCache = false;
 144  
 145  if ($index > -1) {
 146      // Write main script and patch some things
 147      if ($index == 0) {
 148          TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("tiny_mce" . $suffix . ".js")))); // WP
 149          TinyMCE_echo('TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;');
 150          TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;');
 151      } else
 152          TinyMCE_echo('tinyMCE = realTinyMCE;');
 153  
 154      // Do init based on index
 155      TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);");
 156  
 157      // Load external plugins
 158      if ($index == 0)
 159          TinyMCE_echo("tinyMCECompressed.loadPlugins();");
 160  
 161      // Load theme, language pack and theme language packs
 162      if ($theme) {
 163          TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("themes/" . $theme . "/editor_template" . $suffix . ".js")))); // WP
 164          TinyMCE_echo(wp_tinymce_lang("themes/" . $theme . "/langs/%s.js")); // WP
 165      }
 166  
 167      /* WP if ($language) WP */
 168          TinyMCE_echo(wp_tinymce_lang("langs/%s.js")); // WP
 169  
 170      // Load all plugins and their language packs
 171      $plugins = explode(",", $plugins);
 172      foreach ($plugins as $plugin) {
 173          $pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
 174          /* WP $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js"); WP */
 175  
 176          if ($pluginFile)
 177              TinyMCE_echo(file_get_contents($pluginFile));
 178  
 179          /* WP if ($languageFile) WP */
 180              TinyMCE_echo(wp_tinymce_lang("plugins/" . $plugin . "/langs/%s.js")); // WP
 181      }
 182  
 183      // Reset tinyMCE compressor engine
 184      TinyMCE_echo("tinyMCE = tinyMCECompressed;");
 185  
 186      // Write to cache
 187      if ($diskCache) {
 188          // Calculate compression ratio and debug target output path
 189          if ($debug) {
 190              $ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP)) / strlen($cacheData) * 100.0);
 191              TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');");
 192          }
 193  
 194          $cacheData = gzencode($cacheData, 9, FORCE_GZIP);
 195  
 196          // Write to file if possible
 197          $fp = @fopen($cacheFile, "wb");
 198          if ($fp) {
 199              fwrite($fp, $cacheData);
 200              fclose($fp);
 201          }
 202  
 203          // Output
 204          header("Content-Encoding: " . $enc);
 205          echo $cacheData;
 206      }
 207  
 208      die;
 209  }
 210  ?>
 211  
 212  function TinyMCECompressed() {
 213      this.configs = new Array();
 214      this.loadedFiles = new Array();
 215      this.externalPlugins = new Array();
 216      this.loadAdded = false;
 217      this.isLoaded = false;
 218  }
 219  
 220  TinyMCECompressed.prototype.init = function(settings) {
 221      var elements = document.getElementsByTagName('script');
 222      var scriptURL = "";
 223  
 224      for (var i=0; i<elements.length; i++) {
 225          if (elements[i].src && elements[i].src.indexOf("tiny_mce_gzip.php") != -1) {
 226              scriptURL = elements[i].src;
 227              break;
 228          }
 229      }
 230  
 231      settings["theme"] = typeof(settings["theme"]) != "undefined" ? settings["theme"] : "default";
 232      settings["plugins"] = typeof(settings["plugins"]) != "undefined" ? settings["plugins"] : "";
 233      settings["language"] = typeof(settings["language"]) != "undefined" ? settings["language"] : "en";
 234      settings["button_tile_map"] = typeof(settings["button_tile_map"]) != "undefined" ? settings["button_tile_map"] : true;
 235      this.configs[this.configs.length] = settings;
 236      this.settings = settings;
 237  
 238      scriptURL += (scriptURL.indexOf('?') == -1) ? '?' : '&';
 239      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);
 240      document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + scriptURL + '"></script>');
 241  
 242      if (!this.loadAdded) {
 243          tinyMCE.addEvent(window, "DOMContentLoaded", TinyMCECompressed.prototype.onLoad);
 244          tinyMCE.addEvent(window, "load", TinyMCECompressed.prototype.onLoad);
 245          this.loadAdded = true;
 246      }
 247  }
 248  
 249  TinyMCECompressed.prototype.onLoad = function() {
 250      if (tinyMCE.isLoaded)
 251          return true;
 252  
 253      tinyMCE = realTinyMCE;
 254      TinyMCE_Engine.prototype.onLoad();
 255      tinyMCE._addUnloadEvents();
 256  
 257      tinyMCE.isLoaded = true;
 258  }
 259  
 260  TinyMCECompressed.prototype.addEvent = function(o, n, h) {
 261      if (o.attachEvent)
 262          o.attachEvent("on" + n, h);
 263      else
 264          o.addEventListener(n, h, false);
 265  }
 266  
 267  TinyMCECompressed.prototype.getOnce = function(str) {
 268      var ar = str.replace(/\s+/g, '').split(',');
 269  
 270      for (var i=0; i<ar.length; i++) {
 271          if (ar[i] == '' || ar[i].charAt(0) == '-') {
 272              ar[i] = null;
 273              continue;
 274          }
 275  
 276          // Skip load
 277          for (var x=0; x<this.loadedFiles.length; x++) {
 278              if (this.loadedFiles[x] == ar[i])
 279                  ar[i] = null;
 280          }
 281  
 282          this.loadedFiles[this.loadedFiles.length] = ar[i];
 283      }
 284  
 285      // Glue
 286      str = "";
 287      for (var i=0; i<ar.length; i++) {
 288          if (ar[i] == null)
 289              continue;
 290  
 291          str += ar[i];
 292  
 293          if (i != ar.length-1)
 294              str += ",";
 295      }
 296  
 297      return str;
 298  };
 299  
 300  TinyMCECompressed.prototype.loadPlugins = function() {
 301      var i, ar;
 302  
 303      TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
 304      tinyMCE = realTinyMCE;
 305  
 306      ar = tinyMCECompressed.externalPlugins;
 307      for (i=0; i<ar.length; i++)
 308          tinyMCE.loadPlugin(ar[i].name, ar[i].url);
 309  
 310      TinyMCE.prototype.loadScript = function() {};
 311  };
 312  
 313  TinyMCECompressed.prototype.loadPlugin = function(n, u) {
 314      this.externalPlugins[this.externalPlugins.length] = {name : n, url : u};
 315  };
 316  
 317  TinyMCECompressed.prototype.importPluginLanguagePack = function(n, v) {
 318      tinyMCE = realTinyMCE;
 319      TinyMCE.prototype.loadScript = TinyMCE.prototype.orgLoadScript;
 320      tinyMCE.importPluginLanguagePack(n, v);
 321  };
 322  
 323  TinyMCECompressed.prototype.addPlugin = function(n, p) {
 324      tinyMCE = realTinyMCE;
 325      tinyMCE.addPlugin(n, p);
 326  };
 327  
 328  var tinyMCE = new TinyMCECompressed();
 329  var tinyMCECompressed = tinyMCE;

Your comment here...

Name: Location:
Comments:


List: Classes | Functions | Variables | Constants | Tables

Generated: Sat Jul 15 11:57:04 2006 Courtesy of Taragana