Subversion Repositories oidplus

Rev

Rev 557 | Rev 564 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
112 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
511 daniel-mar 5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
112 daniel-mar 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
 
261 daniel-mar 22
require_once __DIR__ . '/includes/oidplus.inc.php';
462 daniel-mar 23
 
112 daniel-mar 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
 
462 daniel-mar 28
error_reporting(OIDplus::baseConfig()->getValue('DEBUG') ? E_ALL : 0);
486 daniel-mar 29
@ini_set('display_errors', OIDplus::baseConfig()->getValue('DEBUG') ? 1 : 0);
112 daniel-mar 30
 
215 daniel-mar 31
$files = array();
112 daniel-mar 32
 
417 daniel-mar 33
$do_minify = OIDplus::baseConfig()->getValue('MINIFY_JS', true);
215 daniel-mar 34
 
530 daniel-mar 35
$files[] = process_file(__DIR__ . '/3p/jquery/jquery-3.6.0.js');
462 daniel-mar 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');
417 daniel-mar 44
 
368 daniel-mar 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
 
424 daniel-mar 57
if (!isset($_COOKIE['csrf_token'])) {
58
        $token = OIDplus::authUtils()->genCSRFToken();
557 daniel-mar 59
        OIDplus::cookieUtils()->setcookie('csrf_token', $token, 0, false);
426 daniel-mar 60
        $files[] = 'var csrf_token = '.js_escape($token).';';
424 daniel-mar 61
} else {
426 daniel-mar 62
        $files[] = 'var csrf_token = '.js_escape($_COOKIE['csrf_token']).';';
424 daniel-mar 63
}
64
 
561 daniel-mar 65
$files[] = 'var samesite_policy = '.js_escape(OIDplus::baseConfig()->getValue('COOKIE_SAMESITE_POLICY','Strict')).';';
66
 
448 daniel-mar 67
$files[] = process_file(__DIR__ . '/includes/oidplus_base.js');
215 daniel-mar 68
 
368 daniel-mar 69
# ---
70
 
420 daniel-mar 71
function process_file($filename) {
462 daniel-mar 72
        global $do_minify;
73
 
74
        $filename_min = preg_replace('/\.[^.]+$/', '.min.js', $filename);
75
        $filename_full = $filename;
76
 
77
        if ($do_minify) {
78
                if (file_exists($filename_min)) {
79
                        $filename = $filename_min;
80
                } else if (file_exists($filename_full)) {
81
                        $filename = $filename_full;
82
                } else {
83
                        return "console.error('Script file not found: $filename');";
84
                }
429 daniel-mar 85
        } else {
462 daniel-mar 86
                if (file_exists($filename_full)) {
87
                        $filename = $filename_full;
88
                } else if (file_exists($filename_min)) {
89
                        $filename = $filename_min;
90
                } else {
91
                        return "console.error('Script file not found: $filename');";
92
                }
93
        }
420 daniel-mar 94
 
490 daniel-mar 95
        $thisdir = __DIR__;
96
        $thisdir = str_replace('\\', '/', $thisdir); // change Windows Backslashes into Web-Slashes
97
        $filename = str_replace('\\', '/', $filename); // change Windows Backslashes into Web-Slashes
98
        $dir = dirname((strpos($filename, $thisdir.'/') === 0) ? substr($filename, strlen($thisdir.'/')) : $filename);
462 daniel-mar 99
        $cont = file_get_contents($filename);
420 daniel-mar 100
 
462 daniel-mar 101
        // 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
102
        $cont = str_replace("//# sourceMappingURL=", "//# sourceMappingURL=".$dir.'/', $cont);
103
 
104
        return $cont."\n\n";
420 daniel-mar 105
}
106
 
107
# ---
108
 
307 daniel-mar 109
$manifests = OIDplus::getAllPluginManifests('*Pages', true);
110
foreach ($manifests as $manifest) {
111
        foreach ($manifest->getJSFiles() as $js_file) {
420 daniel-mar 112
                $files[] = process_file($js_file);
277 daniel-mar 113
        }
114
}
112 daniel-mar 115
 
368 daniel-mar 116
# ---
362 daniel-mar 117
 
417 daniel-mar 118
if ($do_minify) {
368 daniel-mar 119
        $minifier = null;
120
        foreach ($files as $file) {
261 daniel-mar 121
                if (is_null($minifier)) {
122
                        $minifier = new Minify\JS($file);
123
                } else {
124
                        $minifier->add($file);
125
                }
368 daniel-mar 126
        }
127
        $out = is_null($minifier) ? '' : $minifier->minify();
128
} else {
129
        $out = '';
130
        foreach ($files as $file) {
364 daniel-mar 131
                if (file_exists($file)) {
132
                        $out .= file_get_contents($file)."\n";
133
                } else {
134
                        $out .= "$file\n";
135
                }
215 daniel-mar 136
        }
137
}
138
 
368 daniel-mar 139
# ---
218 daniel-mar 140
 
481 daniel-mar 141
if (OIDplus::baseConfig()->getValue('DEBUG')) {
142
        // In debug mode, we might get PHP error messages (see "error_reporting" above),
143
        // so it would be severe if we would allow ETAG! (since $out does not contain the PHP error messages!)
144
        header('Content-Type:application/javascript');
145
        echo $out;
146
} else {
147
        httpOutWithETag($out, 'application/javascript', 'oidplus.js');
148
}