Rev 375 | Rev 379 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
375 | daniel-mar | 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 | |||
376 | daniel-mar | 24 | define('REQUIRED_POLYFILLS', array( |
25 | 'fetch', // Internet Explorer |
||
26 | 'URL' // Internet Explorer |
||
27 | )); |
||
375 | daniel-mar | 28 | |
376 | daniel-mar | 29 | define('MINIFY_POLYFILL', true); |
30 | |||
31 | define('POLYFILL_CACHE_MAX_AGE', 24*60*60); |
||
32 | |||
33 | # --- |
||
34 | |||
35 | header('Content-Type:application/javascript'); |
||
36 | |||
37 | if (!isset($_SERVER['HTTP_USER_AGENT'])) die('/* ERROR: No User Agent available */'); |
||
38 | |||
375 | daniel-mar | 39 | $ua = $_SERVER['HTTP_USER_AGENT']; |
40 | |||
376 | daniel-mar | 41 | if (MINIFY_POLYFILL) { |
42 | $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.min.js'; |
||
43 | } else { |
||
44 | $cache_file = __DIR__ . '/userdata/cache/.polyfill_'.md5(implode(',',REQUIRED_POLYFILLS)).'_'.md5($ua).'.js'; |
||
45 | } |
||
375 | daniel-mar | 46 | |
376 | daniel-mar | 47 | if (!file_exists($cache_file) || (time()-filemtime($cache_file)) > POLYFILL_CACHE_MAX_AGE) { |
375 | daniel-mar | 48 | |
376 | daniel-mar | 49 | if (MINIFY_POLYFILL) { |
50 | $url = 'https://polyfill.io/v3/polyfill.min.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS)); |
||
51 | } else { |
||
52 | $url = 'https://polyfill.io/v3/polyfill.js?features='.urlencode(implode(',',REQUIRED_POLYFILLS)); |
||
53 | } |
||
54 | |||
55 | $opts = [ |
||
56 | "http" => [ |
||
57 | "method" => "GET", |
||
58 | "header" => "User-Agent: ".$ua."\r\n" |
||
59 | ] |
||
60 | ]; |
||
61 | $context = stream_context_create($opts); |
||
62 | $cont = @file_get_contents($url, false, $context); |
||
63 | |||
64 | if ($cont === false) { |
||
65 | |||
66 | if (file_exists($cache_file)) { |
||
67 | $out = file_get_contents($cache_file); |
||
68 | } else { |
||
69 | die('/* ERROR: polyfill.io not available and no cache file exists! */'); |
||
70 | } |
||
71 | |||
72 | } else { |
||
73 | |||
74 | $ua = str_replace('*/', '', $ua); |
||
75 | $cont = '/* Polyfill '.$url.' for '.$ua.' */'.$cont; // TODO: escape |
||
76 | |||
77 | @file_put_contents($cache_file, $cont); |
||
78 | |||
79 | $out = $cont; |
||
80 | |||
81 | } |
||
82 | |||
83 | } else { |
||
84 | |||
85 | $out = file_get_contents($cache_file); |
||
86 | |||
87 | } |
||
88 | |||
375 | daniel-mar | 89 | # --- |
90 | |||
91 | $etag = md5($out); |
||
92 | header("Etag: $etag"); |
||
93 | header('Content-MD5: '.$etag); // RFC 2616 clause 14.15 |
||
94 | if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) { |
||
95 | header("HTTP/1.1 304 Not Modified"); |
||
96 | } else { |
||
97 | echo $out; |
||
98 | } |