Subversion Repositories oidplus

Rev

Rev 1321 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 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. $files[] = process_file(__DIR__ . '/vendor/twbs/bootstrap/dist/js/bootstrap.js');
  35. $files[] = process_file(__DIR__ . '/vendor/vakata/jstree/dist/jstree.js');
  36. $files[] = process_file(__DIR__ . '/vendor/tinymce/tinymce/tinymce.js');
  37. $files[] = process_file(__DIR__ . '/vendor/components/jqueryui/jquery-ui.js');
  38. $files[] = process_file(__DIR__ . '/vendor/gedmarc/layout/dist/jquery.layout_and_plugins.js');
  39. $files[] = process_file(__DIR__ . '/vendor/spamspan/spamspan/spamspan.js');
  40. $files[] = process_file(__DIR__ . '/vendor/emn178/js-sha3/src/sha3.js');
  41. $files[] = process_file(__DIR__ . '/vendor/script47/bs5-utils/dist/js/Bs5Utils.js');
  42.  
  43. # ---
  44.  
  45. $files[] = 'var DEFAULT_LANGUAGE = '.json_encode(OIDplus::getDefaultLang()).';';
  46.  
  47. OIDplus::registerAllPlugins('language', OIDplusLanguagePlugin::class, null);
  48. $translation_array = OIDplus::getTranslationArray();
  49. $files[] = 'var language_messages = '.json_encode($translation_array).';';
  50.  
  51. //$tbl_prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX','');
  52. //$files[] = 'var language_tblprefix = '.json_encode($tbl_prefix).';';
  53. $files[] = 'var language_tblprefix = "<tableprefix>";'; // hide TABLENAME_PREFIX from the client
  54.  
  55. $files[] = 'var oidplus_webpath_relative = '.js_escape(OIDplus::webpath(null, OIDplus::PATH_RELATIVE_TO_ROOT)).';';
  56. $files[] = 'var oidplus_webpath_absolute_canonical = '.js_escape(OIDplus::webpath(null, OIDplus::PATH_ABSOLUTE_CANONICAL)).';';
  57.  
  58. // The CSRF token is set by index.php
  59. // TODO: can there race-conditions if we set csrf_token here? Or should we set it as inline-script in index.php ?
  60. $files[] = 'var csrf_token = '.js_escape($_COOKIE['csrf_token'] ?? '').';';
  61.  
  62. $files[] = 'var samesite_policy = '.js_escape(OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY','Strict')).';';
  63.  
  64. $files[] = process_file(__DIR__ . '/includes/oidplus_functions.js');
  65. $files[] = process_file(__DIR__ . '/includes/oidplus_language.js');
  66. $files[] = process_file(__DIR__ . '/includes/oidplus_base.js');
  67.  
  68. # ---
  69.  
  70. /**
  71.  * @param string $filename
  72.  * @return string
  73.  */
  74. function process_file(string $filename): string {
  75.         global $do_minify;
  76.  
  77.         $filename_min = preg_replace('/\.[^.]+$/', '.min.js', $filename);
  78.         $filename_full = $filename;
  79.  
  80.         if ($do_minify) {
  81.                 if (file_exists($filename_min)) {
  82.                         $filename = $filename_min;
  83.                 } else if (file_exists($filename_full)) {
  84.                         $filename = $filename_full;
  85.                 } else {
  86.                         return "console.error('Script file not found: $filename');";
  87.                 }
  88.         } else {
  89.                 if (file_exists($filename_full)) {
  90.                         $filename = $filename_full;
  91.                 } else if (file_exists($filename_min)) {
  92.                         $filename = $filename_min;
  93.                 } else {
  94.                         return "console.error('Script file not found: $filename');";
  95.                 }
  96.         }
  97.  
  98.         $thisdir = __DIR__;
  99.         $thisdir = str_replace('\\', '/', $thisdir); // change Windows Backslashes into Web-Slashes
  100.         $filename = str_replace('\\', '/', $filename); // change Windows Backslashes into Web-Slashes
  101.         $dir = dirname((strpos($filename, $thisdir.'/') === 0) ? substr($filename, strlen($thisdir.'/')) : $filename);
  102.         $cont = file_get_contents($filename);
  103.  
  104.         // 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"
  105.         $cont = str_replace("//# sourceMappingURL=", "//# sourceMappingURL=".$dir.'/', $cont);
  106.  
  107.         return $cont."\n\n";
  108. }
  109.  
  110. # ---
  111.  
  112. $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
  113. foreach ($manifests as $manifest) {
  114.         foreach ($manifest->getJSFiles() as $js_file) {
  115.                 $files[] = process_file($js_file);
  116.         }
  117. }
  118.  
  119. # ---
  120.  
  121. if ($do_minify) {
  122.         $minifier = null;
  123.         foreach ($files as $file) {
  124.                 if (is_null($minifier)) {
  125.                         $minifier = new Minify\JS($file);
  126.                 } else {
  127.                         $minifier->add($file);
  128.                 }
  129.         }
  130.         $out = is_null($minifier) ? '' : $minifier->minify(); /** @phpstan-ignore-line */
  131. } else {
  132.         $out = '';
  133.         foreach ($files as $file) {
  134.                 if (file_exists($file)) {
  135.                         $out .= file_get_contents($file)."\n";
  136.                 } else {
  137.                         $out .= "$file\n";
  138.                 }
  139.         }
  140. }
  141.  
  142. # ---
  143.  
  144. if (OIDplus::baseConfig()->getValue('DEBUG')) {
  145.         // In debug mode, we might get PHP error messages (see "error_reporting" above),
  146.         // so it would be severe if we would allow ETAG! (since $out does not contain the PHP error messages!)
  147.         header('Content-Type:application/javascript');
  148.         echo $out;
  149. } else {
  150.         httpOutWithETag($out, 'application/javascript', 'oidplus.js');
  151. }
  152.