Added TinyMCE compression support and made some supporting modifications to common.php
authorDan
Mon, 05 May 2008 20:28:13 -0400
changeset 544 81b4499a963e
parent 543 dffcbfbc4e59
child 545 e4c8e706f167
Added TinyMCE compression support and made some supporting modifications to common.php
includes/clientside/static/editor.js
includes/clientside/static/enano-lib-basic.js
includes/clientside/tinymce/tiny_mce_gzip.js
includes/clientside/tinymce/tiny_mce_gzip.php
includes/common.php
--- a/includes/clientside/static/editor.js	Mon May 05 20:08:44 2008 -0400
+++ b/includes/clientside/static/editor.js	Mon May 05 20:28:13 2008 -0400
@@ -35,6 +35,15 @@
   content_css : css_url
 };
 
+var enano_tinymce_gz_options = {
+	plugins : 'table,save,safari,pagebreak,style,layer,advhr,insertdatetime,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras' + do_popups,
+	themes : 'advanced',
+	languages : 'en',
+	disk_cache : true,
+	debug : false
+};
+
+
 // Check tinyMCE to make sure its init is finished
 function tinymce_preinit_check()
 {
--- a/includes/clientside/static/enano-lib-basic.js	Mon May 05 20:08:44 2008 -0400
+++ b/includes/clientside/static/enano-lib-basic.js	Mon May 05 20:28:13 2008 -0400
@@ -269,7 +269,11 @@
   {
     var script = document.createElement('script');
     script.type="text/javascript";
-    script.src=scriptPath+"/includes/clientside/tinymce/tiny_mce.js";
+    script.src=scriptPath+"/includes/clientside/tinymce/tiny_mce_gzip.js";
+    script.onload = function(e)
+    {
+      tinyMCE_GZ.init(enano_tinymce_gz_options);
+    }
     head.appendChild(script);
   }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/includes/clientside/tinymce/tiny_mce_gzip.js	Mon May 05 20:28:13 2008 -0400
@@ -0,0 +1,185 @@
+var tinyMCE_GZ = {
+	settings : {
+		themes : '',
+		plugins : '',
+		languages : '',
+		disk_cache : true,
+		page_name : 'tiny_mce_gzip.php',
+		debug : false,
+		suffix : ''
+	},
+
+	init : function(s, cb, sc) {
+		var t = this, n, i, nl = document.getElementsByTagName('script');
+
+		for (n in s)
+			t.settings[n] = s[n];
+
+		s = t.settings;
+
+		for (i=0; i<nl.length; i++) {
+			n = nl[i];
+
+			if (n.src && n.src.indexOf('tiny_mce') != -1)
+				t.baseURL = n.src.substring(0, n.src.lastIndexOf('/'));
+		}
+
+		if (!t.coreLoaded)
+			t.loadScripts(1, s.themes, s.plugins, s.languages, cb, sc);
+	},
+
+	loadScripts : function(co, th, pl, la, cb, sc) {
+		var t = this, x, w = window, q, c = 0, ti, s = t.settings;
+
+		function get(s) {
+			x = 0;
+
+			try {
+				x = new ActiveXObject(s);
+			} catch (s) {
+			}
+
+			return x;
+		};
+
+		// Build query string
+		q = 'js=true&diskcache=' + (s.disk_cache ? 'true' : 'false') + '&core=' + (co ? 'true' : 'false') + '&suffix=' + escape(s.suffix) + '&themes=' + escape(th) + '&plugins=' + escape(pl) + '&languages=' + escape(la);
+
+		if (co)
+			t.coreLoaded = 1;
+
+		// Send request
+		x = w.XMLHttpRequest ? new XMLHttpRequest() : get('Msxml2.XMLHTTP') || get('Microsoft.XMLHTTP');
+		x.overrideMimeType && x.overrideMimeType('text/javascript');
+		x.open('GET', t.baseURL + '/' + s.page_name + '?' + q, !!cb);
+//		x.setRequestHeader('Content-Type', 'text/javascript');
+		x.send('');
+
+		// Handle asyncronous loading
+		if (cb) {
+			// Wait for response
+			ti = w.setInterval(function() {
+				if (x.readyState == 4 || c++ > 10000) {
+					w.clearInterval(ti);
+
+					if (c < 10000 && x.status == 200) {
+						t.loaded = 1;
+						t.eval(x.responseText);
+						tinymce.dom.Event.domLoaded = true;
+						cb.call(sc || t, x);
+					}
+
+					ti = x = null;
+				}
+			}, 10);
+		} else
+			t.eval(x.responseText);
+	},
+
+	start : function() {
+		var t = this, each = tinymce.each, s = t.settings, sl, ln = s.languages.split(',');
+
+		tinymce.suffix = s.suffix;
+
+		// Extend script loader
+		tinymce.create('tinymce.compressor.ScriptLoader:tinymce.dom.ScriptLoader', {
+			loadScripts : function(sc, cb, s) {
+				var ti = this, th = [], pl = [], la = [];
+
+				each(sc, function(o) {
+					var u = o.url;
+
+					if ((!ti.lookup[u] || ti.lookup[u].state != 2) && u.indexOf(t.baseURL) === 0) {
+						// Collect theme
+						if (u.indexOf('editor_template') != -1) {
+							th.push(/\/themes\/([^\/]+)/.exec(u)[1]);
+							load(u, 1);
+						}
+
+						// Collect plugin
+						if (u.indexOf('editor_plugin') != -1) {
+							pl.push(/\/plugins\/([^\/]+)/.exec(u)[1]);
+							load(u, 1);
+						}
+
+						// Collect language
+						if (u.indexOf('/langs/') != -1) {
+							la.push(/\/langs\/([^.]+)/.exec(u)[1]);
+							load(u, 1);
+						}
+					}
+				});
+
+				if (th.length + pl.length + la.length > 0) {
+					if (sl.settings.strict_mode) {
+						// Async
+						t.loadScripts(0, th.join(','), pl.join(','), la.join(','), cb, s);
+						return;
+					} else
+						t.loadScripts(0, th.join(','), pl.join(','), la.join(','), cb, s);
+				}
+
+				return ti.parent(sc, cb, s);
+			}
+		});
+
+		sl = tinymce.ScriptLoader = new tinymce.compressor.ScriptLoader();
+
+		function load(u, sp) {
+			var o;
+
+			if (!sp)
+				u = t.baseURL + u;
+
+			o = {url : u, state : 2};
+			sl.queue.push(o);
+			sl.lookup[o.url] = o;
+		};
+
+		// Add core languages
+		each (ln, function(c) {
+			if (c)
+				load('/langs/' + c + '.js');
+		});
+
+		// Add themes with languages
+		each(s.themes.split(','), function(n) {
+			if (n) {
+				load('/themes/' + n + '/editor_template' + s.suffix + '.js');
+
+				each (ln, function(c) {
+					if (c)
+						load('/themes/' + n + '/langs/' + c + '.js');
+				});
+			}
+		});
+
+		// Add plugins with languages
+		each(s.plugins.split(','), function(n) {
+			if (n) {
+				load('/plugins/' + n + '/editor_plugin' + s.suffix + '.js');
+
+				each (ln, function(c) {
+					if (c)
+						load('/plugins/' + n + '/langs/' + c + '.js');
+				});
+			}
+		});
+	},
+
+	end : function() {
+	},
+
+	eval : function(co) {
+		var w = window;
+
+		// Evaluate script
+		if (!w.execScript) {
+			if (/Gecko/.test(navigator.userAgent))
+				eval(co, w); // Firefox 3.0
+			else
+				eval.call(w, co);
+		} else
+			w.execScript(co); // IE
+	}
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/includes/clientside/tinymce/tiny_mce_gzip.php	Mon May 05 20:28:13 2008 -0400
@@ -0,0 +1,187 @@
+<?php
+/**
+ * $Id: tiny_mce_gzip.php 315 2007-10-25 14:03:43Z spocke $
+ *
+ * @author Moxiecode
+ * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved.
+ *
+ * This file compresses the TinyMCE JavaScript using GZip and
+ * enables the browser to do two requests instead of one for each .js file.
+ * Notice: This script defaults the button_tile_map option to true for extra performance.
+ */
+
+	// Set the error reporting to minimal.
+	@error_reporting(E_ERROR | E_WARNING | E_PARSE);
+  
+  // load Enano
+  define('ENANO_COMMON_ROOTONLY', 1);
+  require('../../common.php');
+
+	// Get input
+	$plugins = explode(',', getParam("plugins", ""));
+	$languages = explode(',', getParam("languages", ""));
+	$themes = explode(',', getParam("themes", ""));
+	$diskCache = getParam("diskcache", "") == "true";
+	$isJS = getParam("js", "") == "true";
+	$compress = getParam("compress", "true") == "true";
+	$core = getParam("core", "true") == "true";
+	$suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
+	$cachePath = realpath(ENANO_ROOT . "/cache"); // Cache path, this is where the .gz files will be stored
+	$expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
+	$content = "";
+	$encodings = array();
+	$supportsGzip = false;
+	$enc = "";
+	$cacheKey = "";
+
+	// Custom extra javascripts to pack
+	$custom = array(/*
+		"some custom .js file",
+		"some custom .js file"
+	*/);
+
+	// Headers
+	header("Content-type: text/javascript");
+	header("Vary: Accept-Encoding");  // Handle proxies
+	header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
+
+	// Is called directly then auto init with default settings
+	if (!$isJS) {
+		echo getFileContents("tiny_mce_gzip.js");
+		echo "tinyMCE_GZ.init({});";
+		die();
+	}
+
+	// Setup cache info
+	if ($diskCache) {
+		if (!$cachePath)
+			die("alert('Real path failed.');");
+
+		$cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "") . $suffix;
+
+		foreach ($custom as $file)
+			$cacheKey .= $file;
+
+		$cacheKey = md5($cacheKey);
+
+		if ($compress)
+			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
+		else
+			$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
+	}
+
+	// Check if it supports gzip
+	if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
+		$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
+
+	if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
+		$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
+		$supportsGzip = true;
+	}
+
+	// Use cached file disk cache
+	if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
+		if ($compress)
+			header("Content-Encoding: " . $enc);
+
+		echo getFileContents($cacheFile);
+		die();
+	}
+
+	// Add core
+	if ($core == "true") {
+		$content .= getFileContents("tiny_mce" . $suffix . ".js");
+
+		// Patch loading functions
+		$content .= "tinyMCE_GZ.start();";
+	}
+
+	// Add core languages
+	foreach ($languages as $lang)
+		$content .= getFileContents("langs/" . $lang . ".js");
+
+	// Add themes
+	foreach ($themes as $theme) {
+		$content .= getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js");
+
+		foreach ($languages as $lang)
+			$content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
+	}
+
+	// Add plugins
+	foreach ($plugins as $plugin) {
+		$content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
+
+		foreach ($languages as $lang)
+			$content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
+	}
+
+	// Add custom files
+	foreach ($custom as $file)
+		$content .= getFileContents($file);
+
+	// Restore loading functions
+	if ($core == "true")
+		$content .= "tinyMCE_GZ.end();";
+
+	// Generate GZIP'd content
+	if ($supportsGzip) {
+		if ($compress) {
+			header("Content-Encoding: " . $enc);
+			$cacheData = gzencode($content, 9, FORCE_GZIP);
+		} else
+			$cacheData = $content;
+
+		// Write gz file
+		if ($diskCache && $cacheKey != "")
+			putFileContents($cacheFile, $cacheData);
+
+		// Stream to client
+		echo $cacheData;
+	} else {
+		// Stream uncompressed content
+		echo $content;
+	}
+
+	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
+
+	function getParam($name, $def = false) {
+		if (!isset($_GET[$name]))
+			return $def;
+
+		return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
+	}
+
+	function getFileContents($path) {
+		$path = realpath($path);
+
+		if (!$path || !@is_file($path))
+			return "";
+
+		if (function_exists("file_get_contents"))
+			return @file_get_contents($path);
+
+		$content = "";
+		$fp = @fopen($path, "r");
+		if (!$fp)
+			return "";
+
+		while (!feof($fp))
+			$content .= fgets($fp);
+
+		fclose($fp);
+
+		return $content;
+	}
+
+	function putFileContents($path, $content) {
+		if (function_exists("file_put_contents"))
+			return @file_put_contents($path, $content);
+
+		$fp = @fopen($path, "wb");
+		if ($fp) {
+			fwrite($fp, $content);
+			fclose($fp);
+		}
+	}
+?>
\ No newline at end of file
--- a/includes/common.php	Mon May 05 20:08:44 2008 -0400
+++ b/includes/common.php	Mon May 05 20:28:13 2008 -0400
@@ -32,6 +32,13 @@
   exit;
 }
 
+// only do this if it hasn't been done yet
+if ( !defined('ENANO_COMMON_ROOT_LOADED') )
+{
+
+// log this
+define('ENANO_COMMON_ROOT_LOADED', 1);
+
 // Our version number
 // This needs to match the version number in the database. This number should
 // be the expected output of enano_version(), which will always be in the
@@ -104,6 +111,24 @@
 // List of scheduled tasks (don't change this manually, use register_cron_task())
 $cron_tasks = array();
 
+} // check for ENANO_COMMON_ROOT_LOADED
+else
+{
+  // loading a second time
+  if ( !defined('ENANO_COMMON_ROOT_LOADED_MULTI') )
+  {
+    define('ENANO_COMMON_ROOT_LOADED_MULTI', 1);
+  }
+}
+
+// If all we really need is the root directory, just leave now
+// checking for ENANO_COMMON_ROOT_LOADED_MULTI here means that if common
+// is included a second time, the rest of Enano will load.
+if ( defined('ENANO_COMMON_ROOTONLY') && !defined('ENANO_COMMON_ROOT_LOADED_MULTI') )
+{
+  return true;
+}
+
 // Start including files. LOTS of files. Yeah!
 require_once(ENANO_ROOT.'/includes/constants.php');
 require_once(ENANO_ROOT.'/includes/functions.php');