Subversion Repositories oidplus

Rev

Rev 355 | Rev 362 | 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 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. require_once __DIR__ . '/3p/minify/src/Minify.php';
  24. require_once __DIR__ . '/3p/minify/src/JS.php';
  25. require_once __DIR__ . '/3p/minify/src/Exception.php';
  26.  
  27. error_reporting(E_ALL);
  28.  
  29. $files = array();
  30.  
  31. $files[] = __DIR__ . '/3p/jquery/jquery-2.2.1.min.js'; // We are using jQuery 2.2.1, because 3.3.1 seems to be incompatible with jsTree (HTML content will not be loaded into jsTree!) TODO: File bug report
  32. $files[] = __DIR__ . '/3p/bootstrap/js/bootstrap.min.js';
  33. $files[] = __DIR__ . '/3p/jstree/jstree.min.js';
  34. $files[] = __DIR__ . '/3p/tinymce/tinymce.min.js';
  35. $files[] = __DIR__ . '/3p/jquery-ui/jquery-ui.min.js';
  36. $files[] = __DIR__ . '/3p/layout/jquery.layout.min.js';
  37. $files[] = __DIR__ . '/3p/spamspan/spamspan.js';
  38. $files[] = __DIR__ . '/3p/bignumber.js/bignumber.min.js';
  39. $files[] = __DIR__ . '/3p/sha3_js/sha3.js'; // https://github.com/emn178/js-sha3
  40.  
  41. $files[] = __DIR__ . '/oidplus_base.js';
  42.  
  43. $manifests = OIDplus::getAllPluginManifests('*Pages', true);
  44. foreach ($manifests as $manifest) {
  45.         foreach ($manifest->getJSFiles() as $js_file) {
  46.                 $files[] = $js_file;
  47.         }
  48. }
  49.  
  50. $translation_array = array();
  51. foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
  52.         $xmldata = $pluginManifest->getRawXml();
  53.         $lang = $xmldata->language->code->__toString();
  54.         $translation_array[$lang] = array();
  55.         if (strpos($lang,'/') !== false) return $str; // prevent attack (but actually, the sanitization above should work)
  56.         if (strpos($lang,'\\') !== false) return $str; // prevent attack (but actually, the sanitization above should work)
  57.         if (strpos($lang,'..') !== false) return $str; // prevent attack (but actually, the sanitization above should work)
  58.         $translation_file = __DIR__.'/plugins/language/'.$lang.'/messages.xml';
  59.         if (!file_exists($translation_file)) continue;
  60.         $xml = @simplexml_load_string(file_get_contents($translation_file));
  61.         if (!$xml) continue; // if there is an UTF-8 or parsing error, don't output any errors, otherwise the JavaScript is corrupt and the page won't render correctly
  62.         foreach ($xml->message as $msg) {
  63.                 $src = trim($msg->source->__toString());
  64.                 $dst = trim($msg->target->__toString());
  65.                 $translation_array[$lang][$src] = $dst;
  66.         }
  67. }
  68. $files[] = 'var language_messages = '.json_encode($translation_array).';';
  69.  
  70. //$tbl_prefix = OIDplus::baseConfig()->getValue('OIDPLUS_TABLENAME_PREFIX','');
  71. //$files[] = 'var language_tblprefix = '.json_encode($tbl_prefix).';';
  72. $files[] = 'var language_tblprefix = "<tableprefix>";'; // hide OIDPLUS_TABLENAME_PREFIX from the client
  73.  
  74. # ---
  75.  
  76. $minifier = null;
  77. $out = '';
  78.  
  79. foreach ($files as $file) {
  80.         if (OIDplus::baseConfig()->getValue('MINIFY_JS', true)) {
  81.                 if (is_null($minifier)) {
  82.                         $minifier = new Minify\JS($file);
  83.                 } else {
  84.                         $minifier->add($file);
  85.                 }
  86.         } else {
  87.                 $out .= file_get_contents($file)."\n";
  88.         }
  89. }
  90.  
  91. if (OIDplus::baseConfig()->getValue('RECAPTCHA_ENABLED', false) == true) {
  92.         if (OIDplus::baseConfig()->getValue('MINIFY_JS', true)) {
  93.                 $minifier->add('oidplus_external_recaptcha();');
  94.         } else {
  95.                 $out .= "oidplus_external_recaptcha();\n";
  96.         }
  97. }
  98.  
  99. if (OIDplus::baseConfig()->getValue('DISABLE_MSIE_COMPAT', false) == false) {
  100.         if (OIDplus::baseConfig()->getValue('MINIFY_JS', true)) {
  101.                 $minifier->add('oidplus_external_polyfill();');
  102.         } else {
  103.                 $out .= "oidplus_external_polyfill();\n";
  104.         }
  105. }
  106.  
  107. if (OIDplus::baseConfig()->getValue('MINIFY_JS', true)) {
  108.         $out = $minifier->minify();
  109. }
  110.  
  111. $etag = md5($out);
  112. header("Etag: $etag");
  113. header('Content-MD5: '.$etag); // RFC 2616 clause 14.15
  114. if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && (trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag)) {
  115.         header("HTTP/1.1 304 Not Modified");
  116. } else {
  117.         header('Content-Type:application/javascript');
  118.         echo $out;
  119. }