includes/clientside/jsres.php
changeset 586 234ddd896555
parent 582 a38876c0793c
child 587 705ed7162315
equal deleted inserted replaced
585:35e91d16ecf5 586:234ddd896555
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    10  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
    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 
       
    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();
    15 
    33 
    16 // Disable for IE, it causes problems.
    34 // Disable for IE, it causes problems.
    17 if ( ( strstr(@$_SERVER['HTTP_USER_AGENT'], 'MSIE') /*|| true*/ ) && !isset($_GET['early']) )
    35 if ( ( strstr(@$_SERVER['HTTP_USER_AGENT'], 'MSIE') /*|| true*/ ) && !isset($_GET['early']) )
    18 {
    36 {
    19   header('HTTP/1.1 302 Redirect');
    37   header('HTTP/1.1 302 Redirect');
    99   }
   117   }
   100 }
   118 }
   101 
   119 
   102 // Output format will always be JS
   120 // Output format will always be JS
   103 header('Content-type: text/javascript');
   121 header('Content-type: text/javascript');
   104 $everything = '';
   122 $everything = "/* The code represented in this file is compressed for optimization purposes. The full source code is available in " . scriptPath . "/includes/clientside/static. */\n\nvar ENANO_JSRES_COMPRESSED = true;\n\n";
   105 
   123 
   106 // if we only want the tiny version of the API (just enough to get by until the full one is loaded), send that
   124 // if we only want the tiny version of the API (just enough to get by until the full one is loaded), send that
   107 // with a simple ETag and far future expires header
   125 // with a simple ETag and far future expires header
   108 if ( isset($_GET['early']) )
   126 if ( isset($_GET['early']) )
   109 {
   127 {
   163 $apex = filemtime('includes/clientside/static/enano-lib-basic.js');
   181 $apex = filemtime('includes/clientside/static/enano-lib-basic.js');
   164 
   182 
   165 $before_includes = substr($file, 0, $pos_start_includes);
   183 $before_includes = substr($file, 0, $pos_start_includes);
   166 $after_includes = substr($file, $pos_end_includes);
   184 $after_includes = substr($file, $pos_end_includes);
   167 
   185 
   168 // compress enano-lib-basic
   186 if ( isset($_GET['f']) )
   169 $libbasic = "$before_includes\n$after_includes";
   187 {
   170 $libbasic = jsres_cache_check('enano-lib-basic.js', $libbasic);
   188   // requested a single file
   171 $everything .= $libbasic;
   189   $js_file =& $_GET['f'];
   172 
   190   if ( !preg_match('/^[a-z0-9_-]+\.js$/i', $js_file) )
   173 // $everything .= $before_includes;
   191   {
   174 // $everything .= $after_includes;
   192     header('HTTP/1.1 404 Not Found');
   175 
   193     exit('Not found');
   176 foreach ( $file_list as $js_file )
   194   }
   177 {
   195   
       
   196   $apex = filemtime("includes/clientside/static/$js_file");
       
   197   
   178   $file_contents = file_get_contents("includes/clientside/static/$js_file");
   198   $file_contents = file_get_contents("includes/clientside/static/$js_file");
   179   $time = filemtime("includes/clientside/static/$js_file");
   199   $everything = jsres_cache_check($js_file, $file_contents);
   180   if ( $time > $apex )
   200 }
   181     $apex = $time;
   201 else
   182   
   202 {
   183   $file_contents = jsres_cache_check($js_file, $file_contents);
   203   // compress enano-lib-basic
   184   
   204   $libbasic = "$before_includes\n$after_includes";
   185   $everything .= "\n\n// $js_file\n";
   205   $libbasic = jsres_cache_check('enano-lib-basic.js', $libbasic);
   186   $everything .= "\n" . $file_contents;
   206   $everything .= $libbasic;
       
   207   
       
   208   // $everything .= $before_includes;
       
   209   // $everything .= $after_includes;
       
   210   
       
   211   foreach ( $file_list as $js_file )
       
   212   {
       
   213     $file_contents = file_get_contents("includes/clientside/static/$js_file");
       
   214     $time = filemtime("includes/clientside/static/$js_file");
       
   215     if ( $time > $apex )
       
   216       $apex = $time;
       
   217     
       
   218     $file_contents = jsres_cache_check($js_file, $file_contents);
       
   219     
       
   220     $everything .= "\n\n// $js_file\n";
       
   221     $everything .= "\n" . $file_contents;
       
   222   }
   187 }
   223 }
   188 
   224 
   189 // generate ETag
   225 // generate ETag
   190 $etag = base64_encode(hexdecode(sha1($everything)));
   226 $etag = base64_encode(hexdecode(sha1($everything)));
   191 
   227 
   206 $date = date('r', $apex);
   242 $date = date('r', $apex);
   207 header("Date: $date");
   243 header("Date: $date");
   208 header("Last-Modified: $date");
   244 header("Last-Modified: $date");
   209 header("ETag: \"$etag\"");
   245 header("ETag: \"$etag\"");
   210 header("Expires: $expires");
   246 header("Expires: $expires");
       
   247 
       
   248 $local_end = microtime_float();
       
   249 $local_gentime = $local_end - $local_start;
       
   250 $local_gentime = round($local_gentime, 5);
       
   251 header("X-Performance: generated in $local_gentime seconds");
   211 
   252 
   212 echo $everything;
   253 echo $everything;
   213 
   254 
   214 if ( $do_gzip )
   255 if ( $do_gzip )
   215 {
   256 {