includes/clientside/jscompress.php
changeset 650 e45183014778
child 662 fcab604da9a7
equal deleted inserted replaced
649:74e03196fd43 650:e45183014778
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.4 (Caoineag alpha 4)
       
     6  * Copyright (C) 2006-2008 Dan Fuhry
       
     7  * jsres.php - the Enano client-side runtime, a.k.a. AJAX on steroids
       
     8  *
       
     9  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    11  *
       
    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.
       
    14  */
       
    15 
       
    16 /**
       
    17  * Returns a floating-point number with the current UNIX timestamp in microseconds. Defined very early because we gotta call it
       
    18  * from very early on in the script to measure the starting time of Enano.
       
    19  * @return float
       
    20  */
       
    21 
       
    22 // First check to see if something already declared this function.... it happens often.
       
    23 if ( !function_exists('microtime_float') )
       
    24 {
       
    25   function microtime_float()
       
    26   {
       
    27     list($usec, $sec) = explode(" ", microtime());
       
    28     return ((float)$usec + (float)$sec);
       
    29   }
       
    30 }
       
    31 
       
    32 $local_start = microtime_float();
       
    33 
       
    34 // Setup Enano
       
    35 
       
    36 //
       
    37 // Determine the location of Enano as an absolute path.
       
    38 //
       
    39 
       
    40 // We need to see if this is a specially marked Enano development server. You can create an Enano
       
    41 // development server by cloning the Mercurial repository into a directory named repo, and then
       
    42 // using symlinks to reference the original files so as to segregate unique files from non-unique
       
    43 // and distribution-standard ones. Enano will pivot its root directory accordingly if the file
       
    44 // .enanodev is found in the Enano root (not /repo/).
       
    45 if ( strpos(__FILE__, '/repo/') && ( file_exists('../../.enanodev') || file_exists('../../../.enanodev') ) )
       
    46 {
       
    47   // We have a development directory. Remove /repo/ from the picture.
       
    48   $filename = str_replace('/repo/', '/', __FILE__);
       
    49 }
       
    50 else
       
    51 {
       
    52   // Standard Enano installation
       
    53   $filename = __FILE__;
       
    54 }
       
    55 
       
    56 // ENANO_ROOT is sometimes defined by plugins like AjIM that need the constant before the Enano API is initialized
       
    57 if ( !defined('ENANO_ROOT') )
       
    58   define('ENANO_ROOT', dirname(dirname(dirname($filename))));
       
    59 
       
    60 chdir(ENANO_ROOT);
       
    61 
       
    62 require('includes/common.php');
       
    63 if ( !defined('ENANO_CLI') )
       
    64 {
       
    65   die_friendly('Not for web use', '<p>This script is designed to be run from a command-line environment.</p>');
       
    66 }
       
    67 
       
    68 if ( !getConfig('cdn_path') )
       
    69 {
       
    70   die_friendly('CDN support not enabled', 'This script is for compressing the Enano Javascript runtimes for CDN use.');
       
    71 }
       
    72 
       
    73 echo "\x1B[1mCreating zip file with compressed Javascript runtimes.\x1B[0m\n";
       
    74 echo "\x1B[0;32mWhen finished, upload the contents of enano-lib.zip to:\n\x1B[1;34m  " . getConfig('cdn_path') . "/includes/clientside/static/\x1B[0m\n";
       
    75 
       
    76 echo "\x1B[0;33mChecking for zip support...";
       
    77 
       
    78 // do we have zip file support?
       
    79 $have_zip = false;
       
    80 $path = ( isset($_SERVER['PATH']) ) ? $_SERVER['PATH'] : false;
       
    81 if ( !$path )
       
    82 {
       
    83   die_semicritical('Can\'t get your PATH', 'Unable to get the PATH environment variable');
       
    84 }
       
    85 
       
    86 $path = ( strtolower(PHP_OS) === 'win32' ) ? explode(';', $path) : explode(':', $path);
       
    87 $pathext = ( strtolower(PHP_OS) === 'win32' ) ? '.exe' : '';
       
    88 
       
    89 foreach ( $path as $dir )
       
    90 {
       
    91   if ( file_exists("$dir/zip$pathext") )
       
    92   {
       
    93     $have_zip = true;
       
    94     break;
       
    95   }
       
    96 }
       
    97 
       
    98 if ( !$have_zip )
       
    99 {
       
   100   // no zupport zor zipping ziles
       
   101   echo "\x1B[31;1mnot found\x1B[0m\n\x1B[1mPlease install the zip utility using your distribution's package manager\nand then rerun this script.\x1B[0m";
       
   102   exit(1);
       
   103 }
       
   104 
       
   105 echo "\x1B[1mall good\x1B[0m\n";
       
   106 echo "\x1B[0;33mMinifying Javascript files...";
       
   107 
       
   108 if ( !@mkdir('includes/clientside/staticmin') )
       
   109 {
       
   110   echo "\x1B[31;1mcouldn't create temp directory\x1B[0m\n\x1B[1mCheck permissions please, we couldn't create includes/clientside/staticmin.\x1B[0m";
       
   111   exit(1);
       
   112 }
       
   113 
       
   114 require('includes/clientside/jsres.php');
       
   115 
       
   116 // $everything now contains the core runtimes
       
   117 // hack to lie about compression, this keeps load_component() from doing jsres.php?f=...
       
   118 $everything = str_replace('ENANO_JSRES_COMPRESSED = true', 'ENANO_JSRES_COMPRESSED = false', $everything);
       
   119 
       
   120 chdir('includes/clientside/staticmin');
       
   121 $handle = @fopen('./enano-lib-basic.js', 'w');
       
   122 if ( !$handle )
       
   123 {
       
   124   echo "\x1B[31;1mcouldn't open file\x1B[0m\n\x1B[1mCheck permissions please, we couldn't create a file inside includes/clientside/staticmin.\x1B[0m";
       
   125   exit(1);
       
   126 }
       
   127 
       
   128 fwrite($handle, $everything);
       
   129 fclose($handle);
       
   130 
       
   131 // for each JS file in includes/clientside/static, compress & write
       
   132 if ( $dr = @opendir('../static') )
       
   133 {
       
   134   while ( $dh = @readdir($dr) )
       
   135   {
       
   136     if ( !preg_match('/\.js$/', $dh) || $dh === 'enano-lib-basic.js' )
       
   137       continue;
       
   138     
       
   139     $contents = @file_get_contents("../static/$dh");
       
   140     $compressed = jsres_cache_check($dh, $contents);
       
   141     $compressed = str_replace('/* JavaScriptCompressor 0.8 [www.devpro.it], thanks to Dean Edwards for idea [dean.edwards.name] */' . "\r\n", '', $compressed);
       
   142     
       
   143     $handle = @fopen("./$dh", 'w');
       
   144     if ( !$handle )
       
   145     {
       
   146       echo "\x1B[31;1mcouldn't open file\x1B[0m\n\x1B[1mCheck permissions please, we couldn't create a file inside includes/clientside/staticmin.\x1B[0m";
       
   147       exit(1);
       
   148     }
       
   149     fwrite($handle, $compressed);
       
   150     fclose($handle);
       
   151   }
       
   152 }
       
   153 else
       
   154 {
       
   155   echo "\x1B[31;1mcouldn't open includes directory\x1B[0m\n\x1B[1mUnable to get our hands into includes/clientside/static/ to compress everything.\x1B[0m";
       
   156   exit(1);
       
   157 }
       
   158 
       
   159 echo "\x1B[1mdone\x1B[0m\n";
       
   160 echo "\x1B[0;33mCompressing into enano-lib.zip...";
       
   161 
       
   162 $result = system('zip -yrq9 ../enano-lib.zip *.js');
       
   163 if ( $result != 0 )
       
   164 {
       
   165   // failure
       
   166   echo "\x1B[31;1mzip creation failed\x1B[0m\n\x1B[1mzip returned result $result\x1B[0m";
       
   167   exit(1);
       
   168 }
       
   169 
       
   170 echo "\x1B[1mdone\x1B[0m\n";
       
   171 
       
   172 // done, clean up
       
   173 
       
   174 echo "\x1B[0;33mCleaning up...";
       
   175 chdir('..');
       
   176 
       
   177 if ( $dr = @opendir('./staticmin') )
       
   178 {
       
   179   while ( $dh = @readdir($dr) )
       
   180   {
       
   181     if ( preg_match('/\.js$/', $dh) )
       
   182       unlink("./staticmin/$dh");
       
   183   }
       
   184 }
       
   185 
       
   186 @rmdir('./staticmin');
       
   187 
       
   188 echo "\x1B[1mdone\x1B[0m\n";