Subversion Repositories Sites.tela-botanica.org

Rev

Rev 609 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 florian 1
<?php
2
/**
3
 * $Id: tiny_mce_gzip.php 158 2006-12-21 14:32:19Z spocke $
4
 *
5
 * @author Moxiecode
6
 * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved.
7
 *
8
 * This file compresses the TinyMCE JavaScript using GZip and
9
 * enables the browser to do two requests instead of one for each .js file.
10
 * Notice: This script defaults the button_tile_map option to true for extra performance.
11
 */
12
 
13
	// Set the error reporting to minimal.
14
	@error_reporting(E_ERROR | E_WARNING | E_PARSE);
15
 
16
	// Get input
17
	$plugins = explode(',', getParam("plugins", ""));
18
	$languages = explode(',', getParam("languages", ""));
19
	$themes = explode(',', getParam("themes", ""));
20
	$diskCache = getParam("diskcache", "") == "true";
21
	$isJS = getParam("js", "") == "true";
22
	$compress = getParam("compress", "true") == "true";
23
	$suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
24
	$cachePath = realpath("."); // Cache path, this is where the .gz files will be stored
25
	$expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
26
	$content = "";
27
	$encodings = array();
28
	$supportsGzip = false;
29
	$enc = "";
30
	$cacheKey = "";
31
 
32
	// Custom extra javascripts to pack
33
	$custom = array(/*
34
		"some custom .js file",
35
		"some custom .js file"
36
	*/);
37
 
38
	// Headers
39
	header("Content-type: text/javascript");
40
	header("Vary: Accept-Encoding");  // Handle proxies
41
	header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
42
 
43
	// Is called directly then auto init with default settings
44
	if (!$isJS) {
45
		echo getFileContents("tiny_mce_gzip.js");
46
		echo "tinyMCE_GZ.init({});";
47
		die();
48
	}
49
 
50
	// Setup cache info
51
	if ($diskCache) {
52
		if (!$cachePath)
53
			die("alert('Real path failed.');");
54
 
55
		$cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "");
56
 
57
		foreach ($custom as $file)
58
			$cacheKey .= $file;
59
 
60
		$cacheKey = md5($cacheKey);
61
 
62
		if ($compress)
63
			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
64
		else
65
			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
66
	}
67
 
68
	// Check if it supports gzip
69
	if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
70
		$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
71
 
72
	if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
73
		$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
74
		$supportsGzip = true;
75
	}
76
 
77
	// Use cached file disk cache
78
	if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
79
		if ($compress)
80
			header("Content-Encoding: " . $enc);
81
 
82
		echo getFileContents($cacheFile);
83
		die();
84
	}
85
 
86
	// Add core
87
	$content .= getFileContents("tiny_mce" . $suffix . ".js");
88
 
89
	// Patch loading functions
90
	$content .= "tinyMCE_GZ.start();";
91
 
92
	// Add core languages
93
	foreach ($languages as $lang)
94
		$content .= getFileContents("langs/" . $lang . ".js");
95
 
96
	// Add themes
97
	foreach ($themes as $theme) {
98
		$content .= getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js");
99
 
100
		foreach ($languages as $lang)
101
			$content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
102
	}
103
 
104
	// Add plugins
105
	foreach ($plugins as $plugin) {
106
		$content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
107
 
108
		foreach ($languages as $lang)
109
			$content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
110
	}
111
 
112
	// Add custom files
113
	foreach ($custom as $file)
114
		$content .= getFileContents($file);
115
 
116
	// Restore loading functions
117
	$content .= "tinyMCE_GZ.end();";
118
 
119
	// Generate GZIP'd content
120
	if ($supportsGzip) {
121
		if ($compress) {
122
			header("Content-Encoding: " . $enc);
123
			$cacheData = gzencode($content, 9, FORCE_GZIP);
124
		} else
125
			$cacheData = $content;
126
 
127
		// Write gz file
128
		if ($diskCache && $cacheKey != "")
129
			putFileContents($cacheFile, $cacheData);
130
 
131
		// Stream to client
132
		echo $cacheData;
133
	} else {
134
		// Stream uncompressed content
135
		echo $content;
136
	}
137
 
138
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
139
 
140
	function getParam($name, $def = false) {
141
		if (!isset($_GET[$name]))
142
			return $def;
143
 
144
		return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
145
	}
146
 
147
	function getFileContents($path) {
148
		$path = realpath($path);
149
 
150
		if (!$path || !@is_file($path))
151
			return "";
152
 
153
		if (function_exists("file_get_contents"))
154
			return @file_get_contents($path);
155
 
156
		$content = "";
157
		$fp = @fopen($path, "r");
158
		if (!$fp)
159
			return "";
160
 
161
		while (!feof($fp))
162
			$content .= fgets($fp);
163
 
164
		fclose($fp);
165
 
166
		return $content;
167
	}
168
 
169
	function putFileContents($path, $content) {
170
		if (function_exists("file_put_contents"))
171
			return @file_put_contents($path, $content);
172
 
173
		$fp = @fopen($path, "wb");
174
		if ($fp) {
175
			fwrite($fp, $content);
176
			fclose($fp);
177
		}
178
	}
179
?>