Subversion Repositories oidplus

Rev

Rev 376 | Rev 427 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. // We do this stuff to avoid that the client includes an externa JavaScript from polyfill.io,
  21. // since some users might have a problem with that (in regards privacy of their IP address etc.)
  22. // So, the OIDplus webserver will act as proxy.
  23.  
  24. define('REQUIRED_POLYFILLS', array(
  25.         'fetch', // Internet Explorer
  26.         'URL'    // Internet Explorer
  27. ));
  28.  
  29. define('MINIFY_POLYFILL', true);
  30.  
  31. define('POLYFILL_CACHE_MAX_AGE', 24*60*60); // 1 day
  32.  
  33. # ---
  34.  
  35. require_once __DIR__ . '/includes/functions.inc.php';
  36.  
  37. if (!isset($_SERVER['HTTP_USER_AGENT'])) {
  38.         $out = '/* ERROR: No User Agent available */';
  39.         httpOutWithETag($out, 'application/javascript', 'polyfill.js');
  40. }
  41.  
  42. $ua = $_SERVER['HTTP_USER_AGENT'];
  43.  
  44. if (MINIFY_POLYFILL) {
  45.         $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.min.js';
  46. } else {
  47.         $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.js';
  48. }
  49.  
  50. if (!file_exists($cache_file) || (time()-filemtime($cache_file)) > POLYFILL_CACHE_MAX_AGE) {
  51.  
  52.         if (MINIFY_POLYFILL) {
  53.                 $url = 'https://polyfill.io/v3/polyfill.min.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
  54.         } else {
  55.                 $url = 'https://polyfill.io/v3/polyfill.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
  56.         }
  57.  
  58.         $opts = [
  59.             "http" => [
  60.                 "method" => "GET",
  61.                 "header" => "User-Agent: ".$ua."\r\n"
  62.             ]
  63.         ];
  64.         $context = stream_context_create($opts);
  65.         $cont = @file_get_contents($url, false, $context);
  66.  
  67.         if ($cont === false) {
  68.  
  69.                 if (file_exists($cache_file)) {
  70.                         $out = file_get_contents($cache_file);
  71.  
  72.                         // In case polyfill.io is down, don't delay further requests
  73.                         // Instead, try again after the next max cache age
  74.                         touch($cache_file);
  75.                 } else {
  76.                         $out = '/* ERROR: polyfill.io not available and no cache file exists! */';
  77.                         httpOutWithETag($out, 'application/javascript', 'polyfill.js');
  78.                 }
  79.  
  80.         } else {
  81.  
  82.                 $ua = str_replace('*/', '', $ua);
  83.                 $cont = '/* Polyfill '.$url.' for '.$ua.' */'.$cont;
  84.  
  85.                 @file_put_contents($cache_file, $cont);
  86.  
  87.                 $out = $cont;
  88.  
  89.         }
  90.  
  91. } else {
  92.  
  93.         $out = file_get_contents($cache_file);
  94.  
  95. }
  96.  
  97. # ---
  98.  
  99. httpOutWithETag($out, 'application/javascript', 'polyfill.js');
  100.