Subversion Repositories oidplus

Rev

Rev 490 | Rev 530 | 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. use MatthiasMullie\Minify;
  21.  
  22. require_once __DIR__ . '/includes/oidplus.inc.php';
  23.  
  24. require_once __DIR__ . '/3p/minify/src/Minify.php';
  25. require_once __DIR__ . '/3p/minify/src/JS.php';
  26. require_once __DIR__ . '/3p/minify/src/Exception.php';
  27.  
  28. error_reporting(OIDplus::baseConfig()->getValue('DEBUG') ? E_ALL : 0);
  29. @ini_set('display_errors', OIDplus::baseConfig()->getValue('DEBUG') ? 1 : 0);
  30.  
  31. $files = array();
  32.  
  33. $do_minify = OIDplus::baseConfig()->getValue('MINIFY_JS', true);
  34.  
  35. $files[] = process_file(__DIR__ . '/3p/jquery/jquery-3.5.1.js');
  36. $files[] = process_file(__DIR__ . '/3p/bootstrap4/js/bootstrap.js');
  37. $files[] = process_file(__DIR__ . '/3p/jstree/jstree.js');
  38. $files[] = process_file(__DIR__ . '/3p/tinymce/tinymce.js');
  39. $files[] = process_file(__DIR__ . '/3p/jquery-ui/jquery-ui.js');
  40. $files[] = process_file(__DIR__ . '/3p/layout/jquery.layout_and_plugins.js');
  41. $files[] = process_file(__DIR__ . '/3p/spamspan/spamspan.js');
  42. $files[] = process_file(__DIR__ . '/3p/bignumber.js/bignumber.js');
  43. $files[] = process_file(__DIR__ . '/3p/sha3_js/sha3.js');
  44.  
  45. # ---
  46.  
  47. $files[] = 'var DEFAULT_LANGUAGE = '.json_encode(OIDplus::DEFAULT_LANGUAGE).';';
  48.  
  49. OIDplus::registerAllPlugins('language', 'OIDplusLanguagePlugin', null);
  50. $translation_array = OIDplus::getTranslationArray();
  51. $files[] = 'var language_messages = '.json_encode($translation_array).';';
  52.  
  53. //$tbl_prefix = OIDplus::baseConfig()->getValue('OIDPLUS_TABLENAME_PREFIX','');
  54. //$files[] = 'var language_tblprefix = '.json_encode($tbl_prefix).';';
  55. $files[] = 'var language_tblprefix = "<tableprefix>";'; // hide OIDPLUS_TABLENAME_PREFIX from the client
  56.  
  57. if (!isset($_COOKIE['csrf_token'])) {
  58.         $token = OIDplus::authUtils()->genCSRFToken();
  59.         setcookie('csrf_token', $token, 0, ini_get('session.cookie_path'), '', false, true);
  60.         $files[] = 'var csrf_token = '.js_escape($token).';';
  61. } else {
  62.         $files[] = 'var csrf_token = '.js_escape($_COOKIE['csrf_token']).';';
  63. }
  64.  
  65. $files[] = process_file(__DIR__ . '/includes/oidplus_base.js');
  66.  
  67. # ---
  68.  
  69. function process_file($filename) {
  70.         global $do_minify;
  71.  
  72.         $filename_min = preg_replace('/\.[^.]+$/', '.min.js', $filename);
  73.         $filename_full = $filename;
  74.  
  75.         if ($do_minify) {
  76.                 if (file_exists($filename_min)) {
  77.                         $filename = $filename_min;
  78.                 } else if (file_exists($filename_full)) {
  79.                         $filename = $filename_full;
  80.                 } else {
  81.                         return "console.error('Script file not found: $filename');";
  82.                 }
  83.         } else {
  84.                 if (file_exists($filename_full)) {
  85.                         $filename = $filename_full;
  86.                 } else if (file_exists($filename_min)) {
  87.                         $filename = $filename_min;
  88.                 } else {
  89.                         return "console.error('Script file not found: $filename');";
  90.                 }
  91.         }
  92.  
  93.         $thisdir = __DIR__;
  94.         $thisdir = str_replace('\\', '/', $thisdir); // change Windows Backslashes into Web-Slashes
  95.         $filename = str_replace('\\', '/', $filename); // change Windows Backslashes into Web-Slashes
  96.         $dir = dirname((strpos($filename, $thisdir.'/') === 0) ? substr($filename, strlen($thisdir.'/')) : $filename);
  97.         $cont = file_get_contents($filename);
  98.  
  99.         // 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
  100.         $cont = str_replace("//# sourceMappingURL=", "//# sourceMappingURL=".$dir.'/', $cont);
  101.  
  102.         return $cont."\n\n";
  103. }
  104.  
  105. # ---
  106.  
  107. $manifests = OIDplus::getAllPluginManifests('*Pages', true);
  108. foreach ($manifests as $manifest) {
  109.         foreach ($manifest->getJSFiles() as $js_file) {
  110.                 $files[] = process_file($js_file);
  111.         }
  112. }
  113.  
  114. # ---
  115.  
  116. if ($do_minify) {
  117.         $minifier = null;
  118.         foreach ($files as $file) {
  119.                 if (is_null($minifier)) {
  120.                         $minifier = new Minify\JS($file);
  121.                 } else {
  122.                         $minifier->add($file);
  123.                 }
  124.         }
  125.         $out = is_null($minifier) ? '' : $minifier->minify();
  126. } else {
  127.         $out = '';
  128.         foreach ($files as $file) {
  129.                 if (file_exists($file)) {
  130.                         $out .= file_get_contents($file)."\n";
  131.                 } else {
  132.                         $out .= "$file\n";
  133.                 }
  134.         }
  135. }
  136.  
  137. # ---
  138.  
  139. if (OIDplus::baseConfig()->getValue('DEBUG')) {
  140.         // In debug mode, we might get PHP error messages (see "error_reporting" above),
  141.         // so it would be severe if we would allow ETAG! (since $out does not contain the PHP error messages!)
  142.         header('Content-Type:application/javascript');
  143.         echo $out;
  144. } else {
  145.         httpOutWithETag($out, 'application/javascript', 'oidplus.js');
  146. }
  147.