includes/clientside/jsres.php
changeset 420 301f546688d1
parent 411 d1a95497b68f
child 421 dbae4d327846
equal deleted inserted replaced
419:b8b4e38825db 420:301f546688d1
    11  *
    11  *
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    13  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    14  */
    14  */
    15 
    15 
    16 if(!isset($_GET['title'])) $_GET['title'] = 'null';
    16 // Setup Enano
    17 require('../common.php');
       
    18 
    17 
    19 define('ENABLE_COMPRESSION', '');
    18 //
       
    19 // Determine the location of Enano as an absolute path.
       
    20 //
    20 
    21 
    21 ob_start();
    22 // We need to see if this is a specially marked Enano development server. You can create an Enano
       
    23 // development server by cloning the Mercurial repository into a directory named repo, and then
       
    24 // using symlinks to reference the original files so as to segregate unique files from non-unique
       
    25 // and distribution-standard ones. Enano will pivot its root directory accordingly if the file
       
    26 // .enanodev is found in the Enano root (not /repo/).
       
    27 if ( strpos(__FILE__, '/repo/') && ( file_exists('../../.enanodev') || file_exists('../../../.enanodev') ) )
       
    28 {
       
    29   // We have a development directory. Remove /repo/ from the picture.
       
    30   $filename = str_replace('/repo/', '/', __FILE__);
       
    31 }
       
    32 else
       
    33 {
       
    34   // Standard Enano installation
       
    35   $filename = __FILE__;
       
    36 }
       
    37 
       
    38 // ENANO_ROOT is sometimes defined by plugins like AjIM that need the constant before the Enano API is initialized
       
    39 if ( !defined('ENANO_ROOT') )
       
    40   define('ENANO_ROOT', dirname(dirname(dirname($filename))));
       
    41 
       
    42 chdir(ENANO_ROOT);
       
    43 
       
    44 // CONFIG
       
    45 
       
    46 // Files safe to run full (aggressive) compression on
       
    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');
       
    48 
       
    49 // Files that should NOT be compressed due to already being compressed, licensing, or invalid produced code
       
    50 $compress_unsafe = array('SpryEffects.js', 'json.js', 'fat.js');
       
    51 
       
    52 require('includes/functions.php');
       
    53 require('includes/json2.php');
       
    54 require('includes/js-compressor.php');
       
    55 
       
    56 // Output format will always be JS
    22 header('Content-type: text/javascript');
    57 header('Content-type: text/javascript');
       
    58 $everything = '';
    23 
    59 
    24 $file = ( isset($_GET['file']) ) ? $_GET['file'] : 'enano-lib-basic.js';
    60 // Load and parse enano_lib_basic
       
    61 $file = @file_get_contents('includes/clientside/static/enano-lib-basic.js');
    25 
    62 
    26 if(!preg_match('/^([a-z0-9_-]+)\.js$/i', $file))
    63 $pos_start_includes = strpos($file, '/*!START_INCLUDER*/');
    27   die('// ERROR: Hacking attempt');
    64 $pos_end_includes = strpos($file, '/*!END_INCLUDER*/');
    28 
    65 
    29 $fname = './static/' . $file;
    66 if ( !$pos_start_includes || !$pos_end_includes )
    30 if ( !file_exists($fname) )
    67 {
    31   die('// ERROR: File not found: ' . $file);
    68   die('// Error: enano-lib-basic does not have required metacomments');
       
    69 }
    32 
    70 
    33 $everything = file_get_contents($fname);
    71 $pos_end_includes += strlen('/*!END_INCLUDER*/');
    34 
    72 
    35 $mtime = filemtime($fname);
    73 preg_match('/var thefiles = (\[([^\]]+?)\]);/', $file, $match);
    36 header('Last-Modified: '.enano_date('D, d M Y H:i:s T', $mtime));
       
    37 header('Content-disposition: attachment; filename=' . $file);
       
    38 
    74 
    39 if(defined('ENABLE_COMPRESSION'))
    75 if ( empty($match) )
       
    76   die('// Error: could not retrieve file list from enano-lib-basic');
       
    77 
       
    78 // Decode file list
       
    79 try
    40 {
    80 {
    41   echo "/*
    81   $file_list = enano_json_decode($match[1]);
    42  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
    82 }
    43  * Version 1.1.2 (Caoineag alpha 2)
    83 catch ( Exception $e )
    44  * [Aggressively compressed] Javascript client code
    84 {
    45  * Copyright (C) 2006-2007 Dan Fuhry
    85   die("// Exception caught during file list parsing");
    46  * Enano is Free Software, licensed under the GNU General Public License; see http://enanocms.org/ for details.
    86 }
    47  */
       
    48 
    87 
    49 ";
    88 $apex = filemtime('includes/clientside/static/enano-lib-basic.js');
       
    89 
       
    90 $before_includes = substr($file, 0, $pos_start_includes);
       
    91 $after_includes = substr($file, $pos_end_includes);
       
    92 
       
    93 $everything .= $before_includes;
       
    94 $everything .= $after_includes;
       
    95 
       
    96 foreach ( $file_list as $js_file )
       
    97 {
       
    98   $file_contents = file_get_contents("includes/clientside/static/$js_file");
       
    99   $file_md5 = md5($file_contents);
       
   100   $time = filemtime("includes/clientside/static/$js_file");
       
   101   if ( $time > $apex )
       
   102     $apex = $time;
       
   103   // Is this file cached?
       
   104   $cache_path = ENANO_ROOT . "/cache/jsres_$js_file.json";
       
   105   $loaded_cache = false;
    50   
   106   
    51   $cache_file = ENANO_ROOT . '/cache/jsres-' . $file . '.php';
   107   if ( file_exists($cache_path) )
    52   
       
    53   if ( file_exists($cache_file) )
       
    54   {
   108   {
    55     $cached = file_get_contents ( $cache_file );
   109     // Load the cache file and parse it.
    56     $data = unserialize ( $cached );
   110     $cache_file = file_get_contents($cache_path);
    57     if ( $data['md5'] == md5 ( $everything ) )
   111     try
    58     {
   112     {
    59       echo "// The code in this file was fetched from cache\n\n";
   113       $cache_file = enano_json_decode($cache_file);
    60       echo $data['code'];
   114     }
    61       exit;
   115     catch ( Exception $e )
       
   116     {
       
   117       // Don't do anything - let our fallbacks come into place
       
   118     }
       
   119     if ( is_array($cache_file) && isset($cache_file['md5']) && isset($cache_file['src']) )
       
   120     {
       
   121       if ( $cache_file['md5'] === $file_md5 )
       
   122       {
       
   123         $loaded_cache = true;
       
   124         $file_contents = $cache_file['src'];
       
   125       }
       
   126     }
       
   127   }
       
   128   if ( !$loaded_cache )
       
   129   {
       
   130     // Try to open the cache file and write to it. If we can't do that, just don't compress the code.
       
   131     $handle = @fopen($cache_path, 'w');
       
   132     if ( $handle )
       
   133     {
       
   134       $aggressive = in_array($js_file, $full_compress_safe);
       
   135       if ( !in_array($js_file, $compress_unsafe) )
       
   136         $file_contents = perform_js_compress($file_contents, $aggressive);
       
   137       
       
   138       $payload = enano_json_encode(array(
       
   139           'md5' => $file_md5,
       
   140           'src' => $file_contents
       
   141         ));
       
   142       fwrite($handle, $payload);
       
   143       fclose($handle);
    62     }
   144     }
    63   }
   145   }
    64   
   146   
    65   if ( getConfig('cache_thumbs') == '1' )
   147   $everything .= "\n // $js_file\n";
    66   {
   148   $everything .= "\n" . $file_contents;
    67     $js_compressor = new JavascriptCompressor();
       
    68     $packed = $js_compressor->getPacked($everything);
       
    69     $data = Array(
       
    70       'md5' => md5 ( $everything ),
       
    71       'code' => $packed
       
    72       );
       
    73     echo "// The code in this file was fetched from the static scripts and compressed (packed code cached)\n\n";
       
    74     echo $packed;
       
    75     
       
    76     $fh = @fopen($cache_file, 'w');
       
    77     if (!$fh)
       
    78       die('// ERROR: Can\'t open cache file for writing');
       
    79     fwrite($fh, serialize ( $data ) );
       
    80     fclose($fh);
       
    81     
       
    82     exit;
       
    83   }
       
    84   
       
    85   echo "// The code in this file was not compressed because packed-script caching is disabled\n\n";
       
    86   echo $everything;
       
    87   
       
    88 }
   149 }
    89 else
   150 
    90 {
   151 $date = date('r', $apex);
    91   echo "// The code in this file was not compressed because all script compression is disabled\n\n";
   152 header("Date: $date");
    92   echo $everything;
   153 header("Last-Modified: $date");
    93 }
   154 
    94 ?>
   155 echo $everything;
       
   156