includes/clientside/tinymce/tiny_mce_gzip.php
changeset 1344 dc96d6c5cd1e
parent 1343 2a31905a567d
child 1345 1de01205143b
equal deleted inserted replaced
1343:2a31905a567d 1344:dc96d6c5cd1e
     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   require('../../common.php');
       
    32 
       
    33 	// Get input
       
    34 	$plugins = explode(',', getParam("plugins", ""));
       
    35 	$languages = explode(',', getParam("languages", ""));
       
    36 	$themes = explode(',', getParam("themes", ""));
       
    37 	$diskCache = getParam("diskcache", "") == "true";
       
    38 	$isJS = getParam("js", "") == "true";
       
    39 	$compress = getParam("compress", "true") == "true";
       
    40 	$core = getParam("core", "true") == "true";
       
    41 	$suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
       
    42   // 5/5/2008 - set cache path to Enano's usual directory
       
    43 	$cachePath = realpath(ENANO_ROOT . "/cache"); // Cache path, this is where the .gz files will be stored
       
    44 	$expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
       
    45 	$content = "";
       
    46 	$encodings = array();
       
    47 	$supportsGzip = false;
       
    48 	$enc = "";
       
    49 	$cacheKey = "";
       
    50 
       
    51 	// Custom extra javascripts to pack
       
    52 	$custom = array(/*
       
    53 		"some custom .js file",
       
    54 		"some custom .js file"
       
    55 	*/);
       
    56 
       
    57 	// Headers
       
    58 	header("Content-type: text/javascript");
       
    59 	header("Vary: Accept-Encoding");  // Handle proxies
       
    60 	header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
       
    61 
       
    62 	// Is called directly then auto init with default settings
       
    63 	if (!$isJS) {
       
    64 		echo getFileContents("tiny_mce_gzip.js");
       
    65 		echo "tinyMCE_GZ.init({});";
       
    66 		die();
       
    67 	}
       
    68 
       
    69 	// Setup cache info
       
    70 	if ($diskCache) {
       
    71 		if (!$cachePath)
       
    72 			die("alert('Real path failed.');");
       
    73 
       
    74 		$cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "") . $suffix;
       
    75 
       
    76 		foreach ($custom as $file)
       
    77 			$cacheKey .= $file;
       
    78 
       
    79 		$cacheKey = md5($cacheKey);
       
    80 
       
    81 		if ($compress)
       
    82 			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
       
    83 		else
       
    84 			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
       
    85 	}
       
    86 
       
    87 	// Check if it supports gzip
       
    88 	if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
       
    89 		$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
       
    90 
       
    91 	if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
       
    92 		$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
       
    93 		$supportsGzip = true;
       
    94 	}
       
    95   
       
    96   // Send an ETag (Enano modification 5/5/2008)
       
    97   if ( isset($cacheKey) )
       
    98   {
       
    99     $etag =& $cacheKey;
       
   100     header("ETag: \"$etag\"");
       
   101     if ( isset($_SERVER['HTTP_IF_NONE_MATCH']) )
       
   102     {
       
   103       if ( "\"$etag\"" == $_SERVER['HTTP_IF_NONE_MATCH'] )
       
   104       {
       
   105         header('HTTP/1.1 304 Not Modified');
       
   106         exit();
       
   107       }
       
   108     }
       
   109   }
       
   110 
       
   111 	// Use cached file disk cache
       
   112 	if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
       
   113 		if ($compress)
       
   114 			header("Content-Encoding: " . $enc);
       
   115 
       
   116 		echo getFileContents($cacheFile);
       
   117 		die();
       
   118 	}
       
   119 
       
   120 	// Add core
       
   121 	if ($core == "true") {
       
   122 		$content .= getFileContents("tiny_mce" . $suffix . ".js");
       
   123 
       
   124 		// Patch loading functions
       
   125 		$content .= "tinyMCE_GZ.start();";
       
   126 	}
       
   127 
       
   128 	// Add core languages
       
   129 	foreach ($languages as $lang)
       
   130 		$content .= getFileContents("langs/" . $lang . ".js");
       
   131 
       
   132 	// Add themes
       
   133 	foreach ($themes as $theme) {
       
   134 		$content .= getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js");
       
   135 
       
   136 		foreach ($languages as $lang)
       
   137 			$content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
       
   138 	}
       
   139 
       
   140 	// Add plugins
       
   141 	foreach ($plugins as $plugin) {
       
   142 		$content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
       
   143 
       
   144 		foreach ($languages as $lang)
       
   145 			$content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
       
   146 	}
       
   147 
       
   148 	// Add custom files
       
   149 	foreach ($custom as $file)
       
   150 		$content .= getFileContents($file);
       
   151 
       
   152 	// Restore loading functions
       
   153 	if ($core == "true")
       
   154 		$content .= "tinyMCE_GZ.end();";
       
   155   
       
   156 	// Generate GZIP'd content
       
   157 	if ($supportsGzip) {
       
   158 		if ($compress) {
       
   159 			header("Content-Encoding: " . $enc);
       
   160 			$cacheData = gzencode($content, 9, FORCE_GZIP);
       
   161 		} else
       
   162 			$cacheData = $content;
       
   163 
       
   164 		// Write gz file
       
   165 		if ($diskCache && $cacheKey != "")
       
   166 			putFileContents($cacheFile, $cacheData);
       
   167 
       
   168 		// Stream to client
       
   169 		echo $cacheData;
       
   170 	} else {
       
   171 		// Stream uncompressed content
       
   172 		echo $content;
       
   173 	}
       
   174 
       
   175 	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   176 
       
   177 	function getParam($name, $def = false) {
       
   178 		if (!isset($_GET[$name]))
       
   179 			return $def;
       
   180 
       
   181 		return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
       
   182 	}
       
   183 
       
   184 	function getFileContents($path) {
       
   185 		$path = realpath($path);
       
   186 
       
   187 		if (!$path || !@is_file($path))
       
   188 			return "";
       
   189 
       
   190 		if (function_exists("file_get_contents"))
       
   191 			return @file_get_contents($path);
       
   192 
       
   193 		$content = "";
       
   194 		$fp = @fopen($path, "r");
       
   195 		if (!$fp)
       
   196 			return "";
       
   197 
       
   198 		while (!feof($fp))
       
   199 			$content .= fgets($fp);
       
   200 
       
   201 		fclose($fp);
       
   202 
       
   203 		return $content;
       
   204 	}
       
   205 
       
   206 	function putFileContents($path, $content) {
       
   207 		if (function_exists("file_put_contents"))
       
   208 			return @file_put_contents($path, $content);
       
   209 
       
   210 		$fp = @fopen($path, "wb");
       
   211 		if ($fp) {
       
   212 			fwrite($fp, $content);
       
   213 			fclose($fp);
       
   214 		}
       
   215 	}
       
   216 ?>