Subversion Repositories oidplus

Rev

Rev 833 | Rev 982 | 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 - 2022 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. use MatthiasMullie\Minify;
  21.  
  22. require_once __DIR__ . '/includes/oidplus.inc.php';
  23.  
  24. error_reporting(OIDplus::baseConfig()->getValue('DEBUG') ? E_ALL : 0);
  25. @ini_set('display_errors', OIDplus::baseConfig()->getValue('DEBUG') ? '1' : '0');
  26.  
  27. $files = array();
  28.  
  29. $do_minify = OIDplus::baseConfig()->getValue('MINIFY_JS', true);
  30.  
  31. $files[] = process_file(__DIR__ . '/vendor/components/jquery/jquery.js');
  32. if (isInternetExplorer()) {
  33.         // There are several JS IE11 incompatibilities in Bootstrap 5:
  34.         // - "const getUID = prefix => {" which is another syntax of "getUID: function getUID(prefix) {"
  35.         // - "Template strings" `...`
  36.         // - let {.....} = ....
  37.         // (more?)
  38.         // We don't include bootstrap.js for Internet Explorer.
  39.         // Therefore, the page is still displayed, although without fancy JS stuff of bootstrap
  40.         // Use bootstrap 4.5.0 JS as fallback (Note that the CSS is still 5.x!)
  41.         $files[] = process_file(__DIR__ . '/vendor/bootstrap4.min.js');
  42.         $files[] = process_file(__DIR__ . '/vendor/ie11CustomProperties.min.js');
  43. } else {
  44.         $files[] = process_file(__DIR__ . '/vendor/twbs/bootstrap/dist/js/bootstrap.js');
  45. }
  46. $files[] = process_file(__DIR__ . '/vendor/vakata/jstree/dist/jstree.js');
  47. $files[] = process_file(__DIR__ . '/vendor/tinymce/tinymce/tinymce.js');
  48. $files[] = process_file(__DIR__ . '/vendor/components/jqueryui/jquery-ui.js');
  49. $files[] = process_file(__DIR__ . '/vendor/gedmarc/layout/dist/jquery.layout_and_plugins.js');
  50. $files[] = process_file(__DIR__ . '/vendor/spamspan/spamspan/spamspan.js');
  51. $files[] = process_file(__DIR__ . '/vendor/emn178/js-sha3/src/sha3.js');
  52. if (!isInternetExplorer()) {
  53.         // Non IE gets Toast instead of alert() for some message types
  54.         // (BS5 Utils is not compatible with Internet Explorer)
  55.         $files[] = process_file(__DIR__ . '/vendor/script47/bs5-utils/dist/js/Bs5Utils.js');
  56. }
  57.  
  58. # ---
  59.  
  60. $files[] = 'var DEFAULT_LANGUAGE = '.json_encode(OIDplus::DEFAULT_LANGUAGE).';';
  61.  
  62. OIDplus::registerAllPlugins('language', 'OIDplusLanguagePlugin', null);
  63. $translation_array = OIDplus::getTranslationArray();
  64. $files[] = 'var language_messages = '.json_encode($translation_array).';';
  65.  
  66. //$tbl_prefix = OIDplus::baseConfig()->getValue('OIDPLUS_TABLENAME_PREFIX','');
  67. //$files[] = 'var language_tblprefix = '.json_encode($tbl_prefix).';';
  68. $files[] = 'var language_tblprefix = "<tableprefix>";'; // hide OIDPLUS_TABLENAME_PREFIX from the client
  69.  
  70. // The CSRF token is set by index.php
  71. // TODO: can there race-conditions if we set csrf_token here? Or should we set it as inline-script in index.php ?
  72. $files[] = 'var csrf_token = '.js_escape(isset($_COOKIE['csrf_token']) ? $_COOKIE['csrf_token'] : '').';';
  73.  
  74. $files[] = 'var samesite_policy = '.js_escape(OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY','Strict')).';';
  75.  
  76. $files[] = process_file(__DIR__ . '/includes/oidplus_base.js');
  77.  
  78. # ---
  79.  
  80. function process_file($filename) {
  81.         global $do_minify;
  82.  
  83.         $filename_min = preg_replace('/\.[^.]+$/', '.min.js', $filename);
  84.         $filename_full = $filename;
  85.  
  86.         if ($do_minify) {
  87.                 if (file_exists($filename_min)) {
  88.                         $filename = $filename_min;
  89.                 } else if (file_exists($filename_full)) {
  90.                         $filename = $filename_full;
  91.                 } else {
  92.                         return "console.error('Script file not found: $filename');";
  93.                 }
  94.         } else {
  95.                 if (file_exists($filename_full)) {
  96.                         $filename = $filename_full;
  97.                 } else if (file_exists($filename_min)) {
  98.                         $filename = $filename_min;
  99.                 } else {
  100.                         return "console.error('Script file not found: $filename');";
  101.                 }
  102.         }
  103.  
  104.         $thisdir = __DIR__;
  105.         $thisdir = str_replace('\\', '/', $thisdir); // change Windows Backslashes into Web-Slashes
  106.         $filename = str_replace('\\', '/', $filename); // change Windows Backslashes into Web-Slashes
  107.         $dir = dirname((strpos($filename, $thisdir.'/') === 0) ? substr($filename, strlen($thisdir.'/')) : $filename);
  108.         $cont = file_get_contents($filename);
  109.  
  110.         // TODO: WHY???? "DevTools failed to load SourceMap: Could not load content for http://localhost/oidplus/bootstrap.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE"
  111.         $cont = str_replace("//# sourceMappingURL=", "//# sourceMappingURL=".$dir.'/', $cont);
  112.  
  113.         return $cont."\n\n";
  114. }
  115.  
  116. # ---
  117.  
  118. $manifests = OIDplus::getAllPluginManifests(implode(',',OIDplus::INTERACTIVE_PLUGIN_TYPES), true); // due to interface gridGeneratorLinks (1.3.6.1.4.1.37476.2.5.2.3.6) this plugin type can also have CSS
  119. foreach ($manifests as $manifest) {
  120.         foreach ($manifest->getJSFiles() as $js_file) {
  121.                 $files[] = process_file($js_file);
  122.         }
  123. }
  124.  
  125. # ---
  126.  
  127. if ($do_minify) {
  128.         $minifier = null;
  129.         foreach ($files as $file) {
  130.                 if (is_null($minifier)) {
  131.                         $minifier = new Minify\JS($file);
  132.                 } else {
  133.                         $minifier->add($file);
  134.                 }
  135.         }
  136.         $out = is_null($minifier) ? '' : $minifier->minify(); /** @phpstan-ignore-line */
  137. } else {
  138.         $out = '';
  139.         foreach ($files as $file) {
  140.                 if (file_exists($file)) {
  141.                         $out .= file_get_contents($file)."\n";
  142.                 } else {
  143.                         $out .= "$file\n";
  144.                 }
  145.         }
  146. }
  147.  
  148. # ---
  149.  
  150. if (OIDplus::baseConfig()->getValue('DEBUG')) {
  151.         // In debug mode, we might get PHP error messages (see "error_reporting" above),
  152.         // so it would be severe if we would allow ETAG! (since $out does not contain the PHP error messages!)
  153.         header('Content-Type:application/javascript');
  154.         echo $out;
  155. } else {
  156.         httpOutWithETag($out, 'application/javascript', 'oidplus.js');
  157. }
  158.