--- /dev/null
+<?php\r
+ /******\r
+ *\r
+ * EditArea PHP compressor\r
+ * Developped by Christophe Dolivet\r
+ * Released under LGPL, Apache and BSD licenses\r
+ * v1.1.3 (2007/01/18) \r
+ *\r
+ ******/\r
+ \r
+ // CONFIG\r
+ $param['cache_duration']= 3600 * 24 * 10; // 10 days util client cache expires\r
+ $param['compress'] = true; // enable the code compression, should be activated but it can be usefull to desactivate it for easier error retrieving (true or false)\r
+ $param['debug'] = false; // Enable this option if you need debuging info\r
+ $param['use_disk_cache']= true; // If you enable this option gzip files will be cached on disk.\r
+ $param['use_gzip']= true; // Enable gzip compression\r
+ // END CONFIG\r
+ \r
+ $compressor= new Compressor($param);\r
+ \r
+ class Compressor{\r
+ \r
+ \r
+ function compressor($param)\r
+ {\r
+ $this->__construct($param);\r
+ }\r
+ \r
+ function __construct($param)\r
+ {\r
+ $this->start_time= $this->get_microtime();\r
+ $this->file_loaded_size=0;\r
+ $this->param= $param;\r
+ $this->script_list="";\r
+ $this->path= dirname(__FILE__)."/";\r
+ if(isset($_GET['plugins'])){\r
+ $this->load_all_plugins= true;\r
+ $this->full_cache_file= $this->path."edit_area_full_with_plugins.js";\r
+ $this->gzip_cache_file= $this->path."edit_area_full_with_plugins.gz";\r
+ }else{\r
+ $this->load_all_plugins= false;\r
+ $this->full_cache_file= $this->path."edit_area_full.js";\r
+ $this->gzip_cache_file= $this->path."edit_area_full.gz";\r
+ }\r
+ \r
+ $this->check_gzip_use();\r
+ $this->send_headers();\r
+ $this->check_cache();\r
+ $this->load_files();\r
+ $this->send_datas();\r
+ }\r
+ \r
+ function send_headers()\r
+ {\r
+ header("Content-type: text/javascript; charset: UTF-8");\r
+ header("Vary: Accept-Encoding"); // Handle proxies\r
+ header(sprintf("Expires: %s GMT", gmdate("D, d M Y H:i:s", time() + $this->param['cache_duration'])) );\r
+ if($this->use_gzip)\r
+ header("Content-Encoding: ".$this->gzip_enc_header);\r
+ }\r
+ \r
+ function check_gzip_use()\r
+ {\r
+ $encodings = array();\r
+ $desactivate_gzip=false;\r
+ \r
+ if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))\r
+ $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));\r
+ \r
+ // desactivate gzip for IE version < 7\r
+ if(preg_match("/(?:msie )([0-9.]+)/i", $_SERVER['HTTP_USER_AGENT'], $ie))\r
+ {\r
+ if($ie[1]<7)\r
+ $desactivate_gzip=true; \r
+ }\r
+ \r
+ // Check for gzip header or northon internet securities\r
+ if (!$desactivate_gzip && $this->param['use_gzip'] && (in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {\r
+ $this->gzip_enc_header= in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";\r
+ $this->use_gzip=true;\r
+ $this->cache_file=$this->gzip_cache_file;\r
+ }else{\r
+ $this->use_gzip=false;\r
+ $this->cache_file=$this->full_cache_file;\r
+ }\r
+ }\r
+ \r
+ function check_cache()\r
+ {\r
+ // Only gzip the contents if clients and server support it\r
+ if (file_exists($this->cache_file)) {\r
+ // check if cache file must be updated\r
+ $cache_date=0; \r
+ if ($dir = opendir($this->path)) {\r
+ while (($file = readdir($dir)) !== false) {\r
+ if(is_file($this->path.$file) && $file!="." && $file!="..")\r
+ $cache_date= max($cache_date, filemtime($this->path.$file));\r
+ }\r
+ closedir($dir);\r
+ }\r
+ if($this->load_all_plugins){\r
+ $plug_path= $this->path."plugins/";\r
+ if (($dir = @opendir($plug_path)) !== false)\r
+ {\r
+ while (($file = readdir($dir)) !== false)\r
+ {\r
+ if ($file !== "." && $file !== "..")\r
+ {\r
+ if(is_dir($plug_path.$file) && file_exists($plug_path.$file."/".$file.".js"))\r
+ $cache_date= max($cache_date, filemtime("plugins/".$file."/".$file.".js"));\r
+ }\r
+ }\r
+ closedir($dir);\r
+ }\r
+ }\r
+\r
+ if(filemtime($this->cache_file) >= $cache_date){\r
+ // if cache file is up to date\r
+ $last_modified = gmdate("D, d M Y H:i:s",filemtime($this->cache_file))." GMT";\r
+ if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) && strcasecmp($_SERVER["HTTP_IF_MODIFIED_SINCE"], $last_modified) === 0)\r
+ {\r
+ header("HTTP/1.1 304 Not Modified");\r
+ header("Last-modified: ".$last_modified);\r
+ header("Cache-Control: Public"); // Tells HTTP 1.1 clients to cache\r
+ header("Pragma:"); // Tells HTTP 1.0 clients to cache\r
+ }\r
+ else\r
+ {\r
+ header("Last-modified: ".$last_modified);\r
+ header("Cache-Control: Public"); // Tells HTTP 1.1 clients to cache\r
+ header("Pragma:"); // Tells HTTP 1.0 clients to cache\r
+ header('Content-Length: '.filesize($this->cache_file));\r
+ echo file_get_contents($this->cache_file);\r
+ } \r
+ die;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ function load_files()\r
+ {\r
+ $loader= $this->get_content("edit_area_loader.js")."\n";\r
+ \r
+ // get the list of other files to load\r
+ $loader= preg_replace("/(t\.scripts_to_load=\s*)\[([^\]]*)\];/e"\r
+ , "\$this->replace_scripts('script_list', '\\1', '\\2')"\r
+ , $loader);\r
+ \r
+ $loader= preg_replace("/(t\.sub_scripts_to_load=\s*)\[([^\]]*)\];/e"\r
+ , "\$this->replace_scripts('sub_script_list', '\\1', '\\2')"\r
+ , $loader);\r
+\r
+ $this->datas= $loader;\r
+ $this->compress_javascript($this->datas);\r
+ \r
+ // load other scripts needed for the loader\r
+ preg_match_all('/"([^"]*)"/', $this->script_list, $match);\r
+ foreach($match[1] as $key => $value)\r
+ {\r
+ $content= $this->get_content(preg_replace("/\\|\//i", "", $value).".js");\r
+ $this->compress_javascript($content);\r
+ $this->datas.= $content."\n";\r
+ }\r
+ //$this->datas);\r
+ //$this->datas= preg_replace('/(( |\t|\r)*\n( |\t)*)+/s', "", $this->datas);\r
+ \r
+ // improved compression step 1/2 \r
+ $this->datas= preg_replace(array("/(\b)EditAreaLoader(\b)/", "/(\b)editAreaLoader(\b)/", "/(\b)editAreas(\b)/"), array("EAL", "eAL", "eAs"), $this->datas);\r
+ //$this->datas= str_replace(array("EditAreaLoader", "editAreaLoader", "editAreas"), array("EAL", "eAL", "eAs"), $this->datas);\r
+ $this->datas.= "var editAreaLoader= eAL;var editAreas=eAs;EditAreaLoader=EAL;";\r
+ \r
+ // load sub scripts\r
+ $sub_scripts="";\r
+ $sub_scripts_list= array();\r
+ preg_match_all('/"([^"]*)"/', $this->sub_script_list, $match);\r
+ foreach($match[1] as $value){\r
+ $sub_scripts_list[]= preg_replace("/\\|\//i", "", $value).".js";\r
+ }\r
+ \r
+ if($this->load_all_plugins){\r
+ // load plugins scripts\r
+ $plug_path= $this->path."plugins/";\r
+ if (($dir = @opendir($plug_path)) !== false)\r
+ {\r
+ while (($file = readdir($dir)) !== false)\r
+ {\r
+ if ($file !== "." && $file !== "..")\r
+ {\r
+ if(is_dir($plug_path.$file) && file_exists($plug_path.$file."/".$file.".js"))\r
+ $sub_scripts_list[]= "plugins/".$file."/".$file.".js";\r
+ }\r
+ }\r
+ closedir($dir);\r
+ }\r
+ }\r
+ \r
+ foreach($sub_scripts_list as $value){\r
+ $sub_scripts.= $this->get_javascript_content($value);\r
+ }\r
+ // improved compression step 2/2 \r
+ $sub_scripts= preg_replace(array("/(\b)editAreaLoader(\b)/", "/(\b)editAreas(\b)/", "/(\b)editArea(\b)/", "/(\b)EditArea(\b)/"), array("eAL", "eAs", "eA", "EA"), $sub_scripts);\r
+ // $sub_scripts= str_replace(array("editAreaLoader", "editAreas", "editArea", "EditArea"), array("eAL", "eAs", "eA", "EA"), $sub_scripts);\r
+ $sub_scripts.= "var editArea= eA;EditArea=EA;";\r
+ \r
+ \r
+ // add the scripts\r
+ // $this->datas.= sprintf("editAreaLoader.iframe_script= \"<script type='text/javascript'>%s</script>\";\n", $sub_scripts);\r
+ \r
+ \r
+ // add the script and use a last compression \r
+ if( $this->param['compress'] )\r
+ {\r
+ $last_comp = array( 'Á' => 'this',\r
+ 'Â' => 'textarea',\r
+ 'Ã' => 'function',\r
+ 'Ä' => 'prototype',\r
+ 'Å' => 'settings',\r
+ 'Æ' => 'length',\r
+ 'Ç' => 'style',\r
+ 'È' => 'parent',\r
+ 'É' => 'last_selection',\r
+ 'Ê' => 'value',\r
+ 'Ë' => 'true',\r
+ 'Ì' => 'false'\r
+ /*,\r
+ 'Î' => '"',\r
+ 'Ï' => "\n",\r
+ 'À' => "\r"*/);\r
+ }\r
+ else\r
+ {\r
+ $last_comp = array();\r
+ }\r
+ \r
+ $js_replace= '';\r
+ foreach( $last_comp as $key => $val )\r
+ $js_replace .= ".replace(/". $key ."/g,'". str_replace( array("\n", "\r"), array('\n','\r'), $val ) ."')";\r
+ \r
+ $this->datas.= sprintf("editAreaLoader.iframe_script= \"<script type='text/javascript'>%s</script>\"%s;\n",\r
+ str_replace( array_values($last_comp), array_keys($last_comp), $sub_scripts ), \r
+ $js_replace);\r
+ \r
+ if($this->load_all_plugins)\r
+ $this->datas.="editAreaLoader.all_plugins_loaded=true;\n";\r
+ \r
+ \r
+ // load the template\r
+ $this->datas.= sprintf("editAreaLoader.template= \"%s\";\n", $this->get_html_content("template.html"));\r
+ // load the css\r
+ $this->datas.= sprintf("editAreaLoader.iframe_css= \"<style>%s</style>\";\n", $this->get_css_content("edit_area.css"));\r
+ \r
+ // $this->datas= "function editArea(){};editArea.prototype.loader= function(){alert('bouhbouh');} var a= new editArea();a.loader();";\r
+ \r
+ }\r
+ \r
+ function send_datas()\r
+ {\r
+ if($this->param['debug']){\r
+ $header=sprintf("/* USE PHP COMPRESSION\n");\r
+ $header.=sprintf("javascript size: based files: %s => PHP COMPRESSION => %s ", $this->file_loaded_size, strlen($this->datas));\r
+ if($this->use_gzip){\r
+ $gzip_datas= gzencode($this->datas, 9, FORCE_GZIP); \r
+ $header.=sprintf("=> GZIP COMPRESSION => %s", strlen($gzip_datas));\r
+ $ratio = round(100 - strlen($gzip_datas) / $this->file_loaded_size * 100.0); \r
+ }else{\r
+ $ratio = round(100 - strlen($this->datas) / $this->file_loaded_size * 100.0);\r
+ }\r
+ $header.=sprintf(", reduced by %s%%\n", $ratio);\r
+ $header.=sprintf("compression time: %s\n", $this->get_microtime()-$this->start_time); \r
+ $header.=sprintf("%s\n", implode("\n", $this->infos));\r
+ $header.=sprintf("*/\n");\r
+ $this->datas= $header.$this->datas; \r
+ }\r
+ $mtime= time(); // ensure that the 2 disk files will have the same update time\r
+ // generate gzip file and cahce it if using disk cache\r
+ if($this->use_gzip){\r
+ $this->gzip_datas= gzencode($this->datas, 9, FORCE_GZIP);\r
+ if($this->param['use_disk_cache'])\r
+ $this->file_put_contents($this->gzip_cache_file, $this->gzip_datas, $mtime);\r
+ }\r
+ \r
+ // generate full js file and cache it if using disk cache \r
+ if($this->param['use_disk_cache'])\r
+ $this->file_put_contents($this->full_cache_file, $this->datas, $mtime);\r
+ \r
+ // generate output\r
+ if($this->use_gzip)\r
+ echo $this->gzip_datas;\r
+ else\r
+ echo $this->datas;\r
+ \r
+// die;\r
+ }\r
+ \r
+ \r
+ function get_content($end_uri)\r
+ {\r
+ $end_uri=preg_replace("/\.\./", "", $end_uri); // Remove any .. (security)\r
+ $file= $this->path.$end_uri;\r
+ if(file_exists($file)){\r
+ $this->infos[]=sprintf("'%s' loaded", $end_uri);\r
+ /*$fd = fopen($file, 'rb');\r
+ $content = fread($fd, filesize($file));\r
+ fclose($fd);\r
+ return $content;*/\r
+ return $this->file_get_contents($file);\r
+ }else{\r
+ $this->infos[]=sprintf("'%s' not loaded", $end_uri);\r
+ return "";\r
+ }\r
+ }\r
+ \r
+ function get_javascript_content($end_uri)\r
+ {\r
+ $val=$this->get_content($end_uri);\r
+ \r
+ $this->compress_javascript($val);\r
+ $this->prepare_string_for_quotes($val);\r
+ return $val;\r
+ }\r
+ \r
+ function compress_javascript(&$code)\r
+ {\r
+ if($this->param['compress'])\r
+ {\r
+ // remove all comments\r
+ // (\"(?:[^\"\\]*(?:\\\\)*(?:\\\"?)?)*(?:\"|$))|(\'(?:[^\'\\]*(?:\\\\)*(?:\\'?)?)*(?:\'|$))|(?:\/\/(?:.|\r|\t)*?(\n|$))|(?:\/\*(?:.|\n|\r|\t)*?(?:\*\/|$))\r
+ $code= preg_replace("/(\"(?:[^\"\\\\]*(?:\\\\\\\\)*(?:\\\\\"?)?)*(?:\"|$))|(\'(?:[^\'\\\\]*(?:\\\\\\\\)*(?:\\\\\'?)?)*(?:\'|$))|(?:\/\/(?:.|\r|\t)*?(\n|$))|(?:\/\*(?:.|\n|\r|\t)*?(?:\*\/|$))/s", "$1$2$3", $code);\r
+ // remove line return, empty line and tabulation\r
+ $code= preg_replace('/(( |\t|\r)*\n( |\t)*)+/s', " ", $code);\r
+ // add line break before "else" otherwise navigators can't manage to parse the file\r
+ $code= preg_replace('/(\b(else)\b)/', "\n$1", $code);\r
+ // remove unnecessary spaces\r
+ $code= preg_replace('/( |\t|\r)*(;|\{|\}|=|==|\-|\+|,|\(|\)|\|\||&\&|\:)( |\t|\r)*/', "$2", $code);\r
+ }\r
+ }\r
+ \r
+ function get_css_content($end_uri){\r
+ $code=$this->get_content($end_uri);\r
+ // remove comments\r
+ $code= preg_replace("/(?:\/\*(?:.|\n|\r|\t)*?(?:\*\/|$))/s", "", $code);\r
+ // remove spaces\r
+ $code= preg_replace('/(( |\t|\r)*\n( |\t)*)+/s', "", $code);\r
+ // remove spaces\r
+ $code= preg_replace('/( |\t|\r)?(\:|,|\{|\})( |\t|\r)+/', "$2", $code);\r
+ \r
+ $this->prepare_string_for_quotes($code);\r
+ return $code;\r
+ }\r
+ \r
+ function get_html_content($end_uri){\r
+ $code=$this->get_content($end_uri);\r
+ //$code= preg_replace('/(\"(?:\\\"|[^\"])*(?:\"|$))|' . "(\'(?:\\\'|[^\'])*(?:\'|$))|(?:\/\/(?:.|\r|\t)*?(\n|$))|(?:\/\*(?:.|\n|\r|\t)*?(?:\*\/|$))/s", "$1$2$3", $code);\r
+ $code= preg_replace('/(( |\t|\r)*\n( |\t)*)+/s', " ", $code);\r
+ $this->prepare_string_for_quotes($code);\r
+ return $code;\r
+ }\r
+ \r
+ function prepare_string_for_quotes(&$str){\r
+ // prepare the code to be putted into quotes \r
+ /*$pattern= array("/(\\\\)?\"/", '/\\\n/' , '/\\\r/' , "/(\r?\n)/");\r
+ $replace= array('$1$1\\"', '\\\\\\n', '\\\\\\r' , '\\\n"$1+"');*/\r
+ $pattern= array("/(\\\\)?\"/", '/\\\n/' , '/\\\r/' , "/(\r?\n)/");\r
+ if($this->param['compress'])\r
+ $replace= array('$1$1\\"', '\\\\\\n', '\\\\\\r' , '\n');\r
+ else\r
+ $replace= array('$1$1\\"', '\\\\\\n', '\\\\\\r' , "\\n\"\n+\"");\r
+ $str= preg_replace($pattern, $replace, $str);\r
+ }\r
+ \r
+ function replace_scripts($var, $param1, $param2)\r
+ {\r
+ $this->$var=stripslashes($param2);\r
+ return $param1."[];";\r
+ }\r
+\r
+ /* for php version that have not thoses functions */\r
+ function file_get_contents($file)\r
+ {\r
+ $fd = fopen($file, 'rb');\r
+ $content = fread($fd, filesize($file));\r
+ fclose($fd);\r
+ $this->file_loaded_size+= strlen($content);\r
+ return $content; \r
+ }\r
+ \r
+ function file_put_contents($file, &$content, $mtime=-1)\r
+ {\r
+ if($mtime==-1)\r
+ $mtime=time();\r
+ $fp = @fopen($file, "wb");\r
+ if ($fp) {\r
+ fwrite($fp, $content);\r
+ fclose($fp);\r
+ touch($file, $mtime);\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ function get_microtime()\r
+ {\r
+ list($usec, $sec) = explode(" ", microtime());\r
+ return ((float)$usec + (float)$sec);\r
+ }\r
+ } \r
+?>\r