tinymce/3rdparty/tiny_mce_gzip.php
changeset 0 c78d206bf01c
equal deleted inserted replaced
-1:000000000000 0:c78d206bf01c
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Copyright (C) 2006-2009 Dan Fuhry
       
     6  * tiny_mce_gzip.php - TinyMCE gzip and caching script, stock from MoxieCode with one modification
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14 
       
    15 /**
       
    16  * $Id: tiny_mce_gzip.php 315 2007-10-25 14:03:43Z spocke $
       
    17  *
       
    18  * @author Moxiecode
       
    19  * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved.
       
    20  *
       
    21  * This file compresses the TinyMCE JavaScript using GZip and
       
    22  * enables the browser to do two requests instead of one for each .js file.
       
    23  * Notice: This script defaults the button_tile_map option to true for extra performance.
       
    24  */
       
    25 
       
    26 	// Set the error reporting to minimal.
       
    27 	@error_reporting(E_ERROR | E_WARNING | E_PARSE);
       
    28   
       
    29   // 5/5/2008 - load Enano, but only get the root directory
       
    30   define('ENANO_COMMON_ROOTONLY', 1);
       
    31   if ( file_exists('../../../includes/common.php') )
       
    32   {
       
    33 	  require('../../../includes/common.php');
       
    34   }
       
    35   else if ( isset($_SERVER['SCRIPT_FILENAME']) )
       
    36   {
       
    37   	  $file = dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])))) . '/includes/common.php';
       
    38   	  if ( file_exists($file) )
       
    39   	  {
       
    40   	  	  require($file);
       
    41   	  }
       
    42   }
       
    43   if ( !defined('ENANO_ROOT') )
       
    44   {
       
    45   	  die("Could not determine the Enano root directory.");
       
    46   }
       
    47 
       
    48 	// Get input
       
    49 	$plugins = explode(',', getParam("plugins", ""));
       
    50 	$languages = explode(',', getParam("languages", ""));
       
    51 	$themes = explode(',', getParam("themes", ""));
       
    52 	$diskCache = getParam("diskcache", "") == "true";
       
    53 	$isJS = getParam("js", "") == "true";
       
    54 	$compress = getParam("compress", "true") == "true";
       
    55 	$core = getParam("core", "true") == "true";
       
    56 	$suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
       
    57   // 5/5/2008 - set cache path to Enano's usual directory
       
    58 	$cachePath = realpath(ENANO_ROOT . "/cache"); // Cache path, this is where the .gz files will be stored
       
    59 	$expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
       
    60 	$content = "";
       
    61 	$encodings = array();
       
    62 	$supportsGzip = false;
       
    63 	$enc = "";
       
    64 	$cacheKey = "";
       
    65 
       
    66 	// Custom extra javascripts to pack
       
    67 	$custom = array(/*
       
    68 		"some custom .js file",
       
    69 		"some custom .js file"
       
    70 	*/);
       
    71 
       
    72 	// Headers
       
    73 	header("Content-type: text/javascript");
       
    74 	header("Vary: Accept-Encoding");  // Handle proxies
       
    75 	header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
       
    76 
       
    77 	// Is called directly then auto init with default settings
       
    78 	if (!$isJS) {
       
    79 		echo getFileContents("tiny_mce_gzip.js");
       
    80 		echo "tinyMCE_GZ.init({});";
       
    81 		die();
       
    82 	}
       
    83 
       
    84 	// Setup cache info
       
    85 	if ($diskCache) {
       
    86 		if (!$cachePath)
       
    87 			die("alert('Real path failed.');");
       
    88 
       
    89 		$cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "") . $suffix;
       
    90 
       
    91 		foreach ($custom as $file)
       
    92 			$cacheKey .= $file;
       
    93 
       
    94 		$cacheKey = md5($cacheKey);
       
    95 
       
    96 		if ($compress)
       
    97 			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
       
    98 		else
       
    99 			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
       
   100 	}
       
   101 
       
   102 	// Check if it supports gzip
       
   103 	if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
       
   104 		$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
       
   105 
       
   106 	if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
       
   107 		$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
       
   108 		$supportsGzip = true;
       
   109 	}
       
   110   
       
   111   // Send an ETag (Enano modification 5/5/2008)
       
   112   if ( isset($cacheKey) )
       
   113   {
       
   114     $etag =& $cacheKey;
       
   115     header("ETag: \"$etag\"");
       
   116     if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) )
       
   117     {
       
   118       if ( "\"$etag\"" == $_SERVER['HTTP_IF_NONE_MATCH'] )
       
   119       {
       
   120         header('HTTP/1.1 304 Not Modified');
       
   121         exit();
       
   122       }
       
   123     }
       
   124   }
       
   125 
       
   126 	// Use cached file disk cache
       
   127 	if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
       
   128 		if ($compress)
       
   129 			header("Content-Encoding: " . $enc);
       
   130 
       
   131 		echo getFileContents($cacheFile);
       
   132 		die();
       
   133 	}
       
   134 
       
   135 	// Add core
       
   136 	if ($core == "true") {
       
   137 		$content .= getFileContents("tiny_mce" . $suffix . ".js");
       
   138 
       
   139 		// Patch loading functions
       
   140 		$content .= "tinyMCE_GZ.start();";
       
   141 	}
       
   142 
       
   143 	// Add core languages
       
   144 	foreach ($languages as $lang)
       
   145 		$content .= getFileContents("langs/" . $lang . ".js");
       
   146 
       
   147 	// Add themes
       
   148 	foreach ($themes as $theme) {
       
   149 		$content .= getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js");
       
   150 
       
   151 		foreach ($languages as $lang)
       
   152 			$content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
       
   153 	}
       
   154 
       
   155 	// Add plugins
       
   156 	foreach ($plugins as $plugin) {
       
   157 		$content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
       
   158 
       
   159 		foreach ($languages as $lang)
       
   160 			$content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
       
   161 	}
       
   162 
       
   163 	// Add custom files
       
   164 	foreach ($custom as $file)
       
   165 		$content .= getFileContents($file);
       
   166 
       
   167 	// Restore loading functions
       
   168 	if ($core == "true")
       
   169 		$content .= "tinyMCE_GZ.end();";
       
   170   
       
   171 	// Generate GZIP'd content
       
   172 	if ($supportsGzip) {
       
   173 		if ($compress) {
       
   174 			header("Content-Encoding: " . $enc);
       
   175 			$cacheData = gzencode($content, 9, FORCE_GZIP);
       
   176 		} else
       
   177 			$cacheData = $content;
       
   178 
       
   179 		// Write gz file
       
   180 		if ($diskCache && $cacheKey != "")
       
   181 			putFileContents($cacheFile, $cacheData);
       
   182 
       
   183 		// Stream to client
       
   184 		echo $cacheData;
       
   185 	} else {
       
   186 		// Stream uncompressed content
       
   187 		echo $content;
       
   188 	}
       
   189 
       
   190 	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   191 
       
   192 	function getParam($name, $def = false) {
       
   193 		if (!isset($_GET[$name]))
       
   194 			return $def;
       
   195 
       
   196 		return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
       
   197 	}
       
   198 
       
   199 	function getFileContents($path) {
       
   200 		$path = realpath($path);
       
   201 
       
   202 		if (!$path || !@is_file($path))
       
   203 			return "";
       
   204 
       
   205 		if (function_exists("file_get_contents"))
       
   206 			return @file_get_contents($path);
       
   207 
       
   208 		$content = "";
       
   209 		$fp = @fopen($path, "r");
       
   210 		if (!$fp)
       
   211 			return "";
       
   212 
       
   213 		while (!feof($fp))
       
   214 			$content .= fgets($fp);
       
   215 
       
   216 		fclose($fp);
       
   217 
       
   218 		return $content;
       
   219 	}
       
   220 
       
   221 	function putFileContents($path, $content) {
       
   222 		if (function_exists("file_put_contents"))
       
   223 			return @file_put_contents($path, $content);
       
   224 
       
   225 		$fp = @fopen($path, "wb");
       
   226 		if ($fp) {
       
   227 			fwrite($fp, $content);
       
   228 			fclose($fp);
       
   229 		}
       
   230 	}
       
   231 ?>