Subversion Repositories oidplus

Rev

Rev 481 | Rev 963 | 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 - 2021 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 external 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.         // Internet Explorer for various AJAX calls
  26.         'fetch',
  27.         'URL',
  28.  
  29.         // Internet Explorer for TinyMCE if it is included inside oidplus.min.js.php ( https://github.com/tinymce/tinymce/blob/5c1702a119e683f93e03ecc2231f11d17ce57395/modules/tinymce/src/core/main/ts/api/EditorManager.ts#L271 )
  30.         'document.currentScript'
  31. ));
  32.  
  33. define('MINIFY_POLYFILL', true); // TODO: put into baseconfig?
  34.  
  35. define('POLYFILL_CACHE_MAX_AGE', 24*60*60); // 1 day, TODO: put into baseconfig?
  36.  
  37. # ---
  38.  
  39. require_once __DIR__ . '/includes/functions.inc.php';
  40.  
  41. error_reporting(0);
  42.  
  43. if (!isset($_SERVER['HTTP_USER_AGENT'])) {
  44.         $out = '/* ERROR: No User Agent available */';
  45.         httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js');
  46. }
  47.  
  48. $ua = $_SERVER['HTTP_USER_AGENT'];
  49.  
  50. if (MINIFY_POLYFILL) {
  51.         $cache_file = __DIR__ . '/userdata/cache/js_polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.min.js';
  52. } else {
  53.         $cache_file = __DIR__ . '/userdata/cache/js_polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.js';
  54. }
  55.  
  56. if (!file_exists($cache_file) || (time()-filemtime($cache_file)) > POLYFILL_CACHE_MAX_AGE) {
  57.  
  58.         if (MINIFY_POLYFILL) {
  59.                 $url = 'https://polyfill.io/v3/polyfill.min.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
  60.         } else {
  61.                 $url = 'https://polyfill.io/v3/polyfill.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS));
  62.         }
  63.  
  64.         $opts = [
  65.             "http" => [
  66.                 "method" => "GET",
  67.                 "header" => "User-Agent: ".$ua."\r\n"
  68.             ]
  69.         ];
  70.         $context = stream_context_create($opts);
  71.         $cont = @file_get_contents($url, false, $context);
  72.  
  73.         if ($cont === false) {
  74.  
  75.                 if (file_exists($cache_file)) {
  76.                         $out = file_get_contents($cache_file);
  77.  
  78.                         // In case polyfill.io is down, don't delay further requests
  79.                         // Instead, try again after the next max cache age
  80.                         touch($cache_file);
  81.                 } else {
  82.                         $out = '/* ERROR: polyfill.io not available and no cache file exists! */';
  83.                         httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js');
  84.                 }
  85.  
  86.         } else {
  87.  
  88.                 $ua = str_replace('*/', '', $ua);
  89.                 $cont = '/* Polyfill '.$url.' for '.$ua.' */'.$cont;
  90.  
  91.                 if (POLYFILL_CACHE_MAX_AGE > 0) {
  92.                         @file_put_contents($cache_file, $cont);
  93.                 }
  94.  
  95.                 $out = $cont;
  96.         }
  97.  
  98. } else {
  99.  
  100.         $out = file_get_contents($cache_file);
  101.  
  102. }
  103.  
  104. # ---
  105.  
  106. httpOutWithETag($out, 'application/javascript', 'polyfill'.(MINIFY_POLYFILL ? '.min' : '').'.js');
  107.