includes/clientside/jsres.php
author Dan
Wed, 13 Feb 2008 21:59:07 -0500
changeset 420 301f546688d1
parent 411 d1a95497b68f
child 421 dbae4d327846
permissions -rw-r--r--
Re-enabled, debugged, and optimized Javascript compression code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     1
<?php
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     2
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     3
/*
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     4
 * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
411
d1a95497b68f Rebrand as 1.1.2; made upgrade framework functional
Dan
parents: 387
diff changeset
     5
 * Version 1.1.2 (Caoineag alpha 2)
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     6
 * Copyright (C) 2006-2007 Dan Fuhry
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     7
 * jsres.php - the Enano client-side runtime, a.k.a. AJAX on steroids
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     8
 *
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
     9
 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    10
 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    11
 *
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    13
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    14
 */
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    15
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    16
// Setup Enano
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    17
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    18
//
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    19
// Determine the location of Enano as an absolute path.
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    20
//
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    21
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    22
// We need to see if this is a specially marked Enano development server. You can create an Enano
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    23
// development server by cloning the Mercurial repository into a directory named repo, and then
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    24
// using symlinks to reference the original files so as to segregate unique files from non-unique
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    25
// and distribution-standard ones. Enano will pivot its root directory accordingly if the file
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    26
// .enanodev is found in the Enano root (not /repo/).
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    27
if ( strpos(__FILE__, '/repo/') && ( file_exists('../../.enanodev') || file_exists('../../../.enanodev') ) )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    28
{
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    29
  // We have a development directory. Remove /repo/ from the picture.
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    30
  $filename = str_replace('/repo/', '/', __FILE__);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    31
}
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    32
else
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    33
{
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    34
  // Standard Enano installation
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    35
  $filename = __FILE__;
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    36
}
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    37
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    38
// ENANO_ROOT is sometimes defined by plugins like AjIM that need the constant before the Enano API is initialized
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    39
if ( !defined('ENANO_ROOT') )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    40
  define('ENANO_ROOT', dirname(dirname(dirname($filename))));
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    41
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    42
chdir(ENANO_ROOT);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    43
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    44
// CONFIG
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    45
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    46
// Files safe to run full (aggressive) compression on
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    47
$full_compress_safe = array('ajax.js', 'editor.js', 'acl.js', 'rijndael.js', 'admin-menu.js', 'autofill.js', 'comments.js', 'misc.js', 'faders.js', 'dropdown.js');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    48
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    49
// Files that should NOT be compressed due to already being compressed, licensing, or invalid produced code
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    50
$compress_unsafe = array('SpryEffects.js', 'json.js', 'fat.js');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    51
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    52
require('includes/functions.php');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    53
require('includes/json2.php');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    54
require('includes/js-compressor.php');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    55
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    56
// Output format will always be JS
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    57
header('Content-type: text/javascript');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    58
$everything = '';
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    59
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    60
// Load and parse enano_lib_basic
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    61
$file = @file_get_contents('includes/clientside/static/enano-lib-basic.js');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    62
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    63
$pos_start_includes = strpos($file, '/*!START_INCLUDER*/');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    64
$pos_end_includes = strpos($file, '/*!END_INCLUDER*/');
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    65
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    66
if ( !$pos_start_includes || !$pos_end_includes )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    67
{
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    68
  die('// Error: enano-lib-basic does not have required metacomments');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    69
}
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    70
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    71
$pos_end_includes += strlen('/*!END_INCLUDER*/');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    72
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    73
preg_match('/var thefiles = (\[([^\]]+?)\]);/', $file, $match);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    74
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    75
if ( empty($match) )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    76
  die('// Error: could not retrieve file list from enano-lib-basic');
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    77
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    78
// Decode file list
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    79
try
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    80
{
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    81
  $file_list = enano_json_decode($match[1]);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    82
}
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    83
catch ( Exception $e )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    84
{
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    85
  die("// Exception caught during file list parsing");
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    86
}
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    87
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    88
$apex = filemtime('includes/clientside/static/enano-lib-basic.js');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    89
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    90
$before_includes = substr($file, 0, $pos_start_includes);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    91
$after_includes = substr($file, $pos_end_includes);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    92
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    93
$everything .= $before_includes;
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    94
$everything .= $after_includes;
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
    95
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    96
foreach ( $file_list as $js_file )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    97
{
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    98
  $file_contents = file_get_contents("includes/clientside/static/$js_file");
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
    99
  $file_md5 = md5($file_contents);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   100
  $time = filemtime("includes/clientside/static/$js_file");
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   101
  if ( $time > $apex )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   102
    $apex = $time;
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   103
  // Is this file cached?
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   104
  $cache_path = ENANO_ROOT . "/cache/jsres_$js_file.json";
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   105
  $loaded_cache = false;
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   106
  
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   107
  if ( file_exists($cache_path) )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   108
  {
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   109
    // Load the cache file and parse it.
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   110
    $cache_file = file_get_contents($cache_path);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   111
    try
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   112
    {
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   113
      $cache_file = enano_json_decode($cache_file);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   114
    }
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   115
    catch ( Exception $e )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   116
    {
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   117
      // Don't do anything - let our fallbacks come into place
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   118
    }
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   119
    if ( is_array($cache_file) && isset($cache_file['md5']) && isset($cache_file['src']) )
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   120
    {
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   121
      if ( $cache_file['md5'] === $file_md5 )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   122
      {
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   123
        $loaded_cache = true;
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   124
        $file_contents = $cache_file['src'];
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   125
      }
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   126
    }
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   127
  }
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   128
  if ( !$loaded_cache )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   129
  {
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   130
    // Try to open the cache file and write to it. If we can't do that, just don't compress the code.
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   131
    $handle = @fopen($cache_path, 'w');
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   132
    if ( $handle )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   133
    {
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   134
      $aggressive = in_array($js_file, $full_compress_safe);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   135
      if ( !in_array($js_file, $compress_unsafe) )
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   136
        $file_contents = perform_js_compress($file_contents, $aggressive);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   137
      
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   138
      $payload = enano_json_encode(array(
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   139
          'md5' => $file_md5,
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   140
          'src' => $file_contents
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   141
        ));
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   142
      fwrite($handle, $payload);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   143
      fclose($handle);
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   144
    }
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   145
  }
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   146
  
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   147
  $everything .= "\n // $js_file\n";
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   148
  $everything .= "\n" . $file_contents;
1
fe660c52c48f Adding /includes
dan@scribus.fuhry.local.fuhry.local
parents:
diff changeset
   149
}
420
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   150
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   151
$date = date('r', $apex);
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   152
header("Date: $date");
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   153
header("Last-Modified: $date");
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   154
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   155
echo $everything;
301f546688d1 Re-enabled, debugged, and optimized Javascript compression code
Dan
parents: 411
diff changeset
   156