Subversion Repositories oidplus

Rev

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