/lib/includes/js/edit_area/edit_area/edit_area_compressor.php
PHP | 631 lines | 485 code | 37 blank | 109 comment | 40 complexity | 1fd037f9a3d7a6b110a2087b0fc2eee2 MD5 | raw file
- <?php #! /usr/bin/php
- //; /usr/bin/php $0 $@ ; exit 0;
- /* ^ can't use the shebang trick here as that would produce one more line
- of output when this file is run from a webserver, so we take the second-best
- approach, which is to favor the webserver and tolerate a few sh/bash error
- reports while it'll start the PHP CLI for us after all.
- Execute from the commandline (bash) like this, for example:
- $ ./edit_area_compressor.php plugins=1 compress=1
- Don't bother about these error reports then:
- ./edit_area_compressor.php: line 1: ?php: No such file or directory
- ./edit_area_compressor.php: line 2: //: is a directory
- PHP Deprecated: Comments starting with '#' are deprecated in /etc/php5/cli/conf.d/idn.ini on line 1 in Unknown on line 0
- The latter shows up on Ubuntu 10.04 installs; again not to bother. The first two
- errors are due to our hack to help bash/sh kickstart the PHP interpreter after all,
- which is a bit convoluted as the shebang trick cannot happen for it would output
- the shebang line with the generated JavaScript when the script is executed by
- a web server. We don't that to happen, so we tolerate the error reports when running
- in a UNIX shell environment.
- */
- /******
- *
- * EditArea PHP compressor
- * Developed by Christophe Dolivet
- * Released under LGPL, Apache and BSD licenses
- * v1.1.3 (2007/01/18)
- *
- ******/
- if (0) // if (1) when you need to diagnose your environment
- {
- echo "_SERVER:\n";
- var_dump($_SERVER);
- echo "_ENV:\n";
- var_dump($_ENV);
- echo "ours:\n";
- echo "\n argv[0] = " . (empty($argv[0]) ? "(NULL)" : $argv[0]);
- echo "\n SHELL = " . (!array_key_exists('SHELL', $_ENV) ? "(NULL)" : $_ENV['SHELL']);
- echo "\n SESSIONNAME = " . (!array_key_exists('SESSIONNAME', $_SERVER) ? "(NULL)" : $_SERVER['SESSIONNAME']);
- echo "\n HTTP_HOST = " . (!array_key_exists('HTTP_HOST', $_SERVER) ? "(NULL)" : $_SERVER['HTTP_HOST']);
- echo "\n QUERY_STRING = " . (!array_key_exists('QUERY_STRING', $_SERVER) ? "(NULL)" : $_SERVER['QUERY_STRING']);
- echo "\n REQUEST_METHOD = " . (!array_key_exists('REQUEST_METHOD', $_SERVER) ? "(NULL)" : $_SERVER['REQUEST_METHOD']);
- die();
- }
- /*
- only run the compressor in here when executed from the commandline; otherwise we'll only
- define the compressor class and wait for the other code out there to call us:
- Tests turn out that $_SERVER['SESSIONNAME'] == 'Console' only on Windows machines, while
- UNIX boxes don't need to present $_ENV['SHELL']. Hence this check to see whether we're
- running from a console or crontab:
- - argv[0] WILL be set when run from the command line (it should list our PHP file)
- - $_SERVER['HTTP_HOST'] does NOT EXIST when run from the console
- - $_SERVER['QUERY_STRING'] does NOT EXIST when run from the console (it may very well be EMPTY when run by the web server!)
- - $_SERVER['REQUEST_METHOD'] does NOT EXIST when run from the console
- Supported arguments when run from the commandline:
- plugins - generate the 'full_with_plugins' version instead of the 'full' one
- you can also override any of the $param[] items like so, for example:
- debug=0 - equals $params['debug'] = false;
- debug=1 - equals $params['debug'] = true;
- */
- if (!empty($argv[0]) && stristr($argv[0], '.php') !== false &&
- !array_key_exists('HTTP_HOST', $_SERVER) &&
- !array_key_exists('QUERY_STRING', $_SERVER) &&
- !array_key_exists('REQUEST_METHOD', $_SERVER))
- {
- // CONFIG
- $param['cache_duration'] = 3600 * 24 * 10; // 10 days util client cache expires
- $param['compress'] = false; // Enable the code compression, should be activated but it can be useful to deactivate it for easier error diagnostics (true or false)
- $param['debug'] = false; // Enable this option if you need debugging info
- $param['use_disk_cache'] = true; // If you enable this option gzip files will be cached on disk.
- $param['use_gzip']= false; // Enable gzip compression
- $param['plugins'] = true; // isset($_GET['plugins']); Include plugins in the compressed/flattened JS output.
- $param['echo2stdout'] = false; // Output generated JS to stdout; alternative is to store it in the object for later retrieval.
- $param['include_langs_and_syntaxes'] = true; // Set to FALSE for backwards compatibility: do not include the language files and syntax definitions in the flattened output.
- // END CONFIG
- for ($i = 1; $i < $argc; $i++)
- {
- $arg = explode('=', $argv[$i], 2);
- $param[$arg[0]] = (isset($arg[1]) ? intval($arg[1]) : true);
- }
- $param['running_from_commandline'] = true; // UNSET or FALSE when executed from a web server
- $param['verbose2stdout'] = !$param['echo2stdout']; // UNSET or FALSE when executed from a web server
- if (!empty($param['verbose2stdout']))
- {
- echo "\nEditArea Compressor:\n";
- echo "Settings:\n";
- foreach($param as $key => $value)
- {
- echo sprintf(" %30s: %d\n", $key, $value);
- }
- }
- $compressor = new Compressor($param);
- }
- class Compressor
- {
- //function compressor($param)
- //{
- // $this->__construct($param);
- //}
- //
- //--> Strict Standards: Redefining already defined constructor for class Compressor
- function __construct($param)
- {
- $this->datas = false;
- $this->gzip_datas = false;
- $this->generated_headers = array();
- $this->start_time= $this->get_microtime();
- $this->file_loaded_size=0;
- $this->param= $param;
- $this->script_list="";
- $this->path= str_replace('\\','/',dirname(__FILE__)).'/';
- if($this->param['plugins'])
- {
- if (!empty($this->param['verbose2stdout'])) echo "\n\n\nGenerating output with plugins included\n";
- $this->load_all_plugins= true;
- $this->full_cache_file= $this->path."edit_area_full_with_plugins.js";
- $this->gzip_cache_file= $this->path."edit_area_full_with_plugins.gz";
- }
- else
- {
- if (!empty($this->param['verbose2stdout'])) echo "\n\n\nGenrating output WITHOUT plugins\n";
- $this->load_all_plugins= false;
- $this->full_cache_file= $this->path."edit_area_full.js";
- $this->gzip_cache_file= $this->path."edit_area_full.gz";
- }
- $this->check_gzip_use();
- $this->send_headers();
- $this->check_cache();
- $this->load_files();
- $this->send_datas();
- }
- /**
- * Return the HTTP headers associated with the compressed/flattened output file as an array of header lines.
- */
- public function get_headers()
- {
- return $this->generated_headers;
- }
- /**
- * Return the generated flattened JavaScript as a string.
- *
- * Return FALSE on error.
- */
- public function get_flattened()
- {
- return $this->datas;
- }
- /**
- * Return the generated flattened and GZIPped JavaScript (as output by gzencode()).
- *
- * Return FALSE on error or when GZIPped JavaScript has not been generated.
- */
- public function get_flattened_gzipped()
- {
- return $this->gzip_datas;
- }
- private function send_header1($headerline)
- {
- $this->generated_headers[] = $headerline;
- if ($this->param['echo2stdout'] && !headers_sent())
- {
- header($headerline);
- }
- }
- private function send_headers()
- {
- $this->send_header1("Content-type: text/javascript; charset: UTF-8");
- $this->send_header1("Vary: Accept-Encoding"); // Handle proxies
- $this->send_header1(sprintf("Expires: %s GMT", gmdate("D, d M Y H:i:s", time() + intval($this->param['cache_duration']))) );
- if($this->use_gzip)
- {
- $this->send_header1("Content-Encoding: ".$this->gzip_enc_header);
- }
- }
- private function check_gzip_use()
- {
- $encodings = array();
- $desactivate_gzip=false;
- if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
- {
- $encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
- }
- // deactivate gzip for IE version < 7
- if (!isset($_SERVER['HTTP_USER_AGENT']))
- {
- // run from the commandline: do NOT use gzip
- $desactivate_gzip=true;
- }
- else if(preg_match("/(?:msie )([0-9.]+)/i", $_SERVER['HTTP_USER_AGENT'], $ie))
- {
- if($ie[1]<7)
- $desactivate_gzip=true;
- }
- // Check for gzip header or northon internet securities
- 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')) {
- $this->gzip_enc_header= in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
- $this->use_gzip=true;
- $this->cache_file=$this->gzip_cache_file;
- }else{
- $this->use_gzip=false;
- $this->cache_file=$this->full_cache_file;
- }
- }
- private function check_cache()
- {
- // Only gzip the contents if clients and server support it
- if ($this->param['use_disk_cache'] && file_exists($this->cache_file) && empty($this->param['running_from_commandline'])) {
- // check if cache file must be updated
- $cache_date=0;
- if ($dir = opendir($this->path)) {
- while (($file = readdir($dir)) !== false) {
- if(is_file($this->path.$file) && $file!="." && $file!="..")
- $cache_date= max($cache_date, filemtime($this->path.$file));
-