Subversion Repositories oidplus

Rev

Rev 448 | Rev 481 | 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
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
 
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);
112 daniel-mar 29
 
215 daniel-mar 30
$files = array();
112 daniel-mar 31
 
417 daniel-mar 32
$do_minify = OIDplus::baseConfig()->getValue('MINIFY_JS', true);
215 daniel-mar 33
 
462 daniel-mar 34
$files[] = process_file(__DIR__ . '/3p/jquery/jquery-3.5.1.js');
35
$files[] = process_file(__DIR__ . '/3p/bootstrap4/js/bootstrap.js');
36
$files[] = process_file(__DIR__ . '/3p/jstree/jstree.js');
37
$files[] = process_file(__DIR__ . '/3p/tinymce/tinymce.js');
38
$files[] = process_file(__DIR__ . '/3p/jquery-ui/jquery-ui.js');
39
$files[] = process_file(__DIR__ . '/3p/layout/jquery.layout_and_plugins.js');
40
$files[] = process_file(__DIR__ . '/3p/spamspan/spamspan.js');
41
$files[] = process_file(__DIR__ . '/3p/bignumber.js/bignumber.js');
42
$files[] = process_file(__DIR__ . '/3p/sha3_js/sha3.js');
417 daniel-mar 43
 
368 daniel-mar 44
# ---
45
 
46
$files[] = 'var DEFAULT_LANGUAGE = '.json_encode(OIDplus::DEFAULT_LANGUAGE).';';
47
 
48
OIDplus::registerAllPlugins('language', 'OIDplusLanguagePlugin', null);
49
$translation_array = OIDplus::getTranslationArray();
50
$files[] = 'var language_messages = '.json_encode($translation_array).';';
51
 
52
//$tbl_prefix = OIDplus::baseConfig()->getValue('OIDPLUS_TABLENAME_PREFIX','');
53
//$files[] = 'var language_tblprefix = '.json_encode($tbl_prefix).';';
54
$files[] = 'var language_tblprefix = "<tableprefix>";'; // hide OIDPLUS_TABLENAME_PREFIX from the client
55
 
424 daniel-mar 56
if (!isset($_COOKIE['csrf_token'])) {
57
        $token = OIDplus::authUtils()->genCSRFToken();
58
        setcookie('csrf_token', $token, 0, ini_get('session.cookie_path'), '', false, true);
426 daniel-mar 59
        $files[] = 'var csrf_token = '.js_escape($token).';';
424 daniel-mar 60
} else {
426 daniel-mar 61
        $files[] = 'var csrf_token = '.js_escape($_COOKIE['csrf_token']).';';
424 daniel-mar 62
}
63
 
448 daniel-mar 64
$files[] = process_file(__DIR__ . '/includes/oidplus_base.js');
215 daniel-mar 65
 
368 daniel-mar 66
# ---
67
 
420 daniel-mar 68
function process_file($filename) {
462 daniel-mar 69
        global $do_minify;
70
 
71
        $filename_min = preg_replace('/\.[^.]+$/', '.min.js', $filename);
72
        $filename_full = $filename;
73
 
74
        if ($do_minify) {
75
                if (file_exists($filename_min)) {
76
                        $filename = $filename_min;
77
                } else if (file_exists($filename_full)) {
78
                        $filename = $filename_full;
79
                } else {
80
                        return "console.error('Script file not found: $filename');";
81
                }
429 daniel-mar 82
        } else {
462 daniel-mar 83
                if (file_exists($filename_full)) {
84
                        $filename = $filename_full;
85
                } else if (file_exists($filename_min)) {
86
                        $filename = $filename_min;
87
                } else {
88
                        return "console.error('Script file not found: $filename');";
89
                }
90
        }
420 daniel-mar 91
 
462 daniel-mar 92
        $dir = dirname((strpos($filename, __DIR__.'/') === 0) ? substr($filename, strlen(__DIR__.'/')) : $filename);
93
        $cont = file_get_contents($filename);
420 daniel-mar 94
 
462 daniel-mar 95
        // 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
96
        $cont = str_replace("//# sourceMappingURL=", "//# sourceMappingURL=".$dir.'/', $cont);
97
 
98
        return $cont."\n\n";
420 daniel-mar 99
}
100
 
101
# ---
102
 
307 daniel-mar 103
$manifests = OIDplus::getAllPluginManifests('*Pages', true);
104
foreach ($manifests as $manifest) {
105
        foreach ($manifest->getJSFiles() as $js_file) {
420 daniel-mar 106
                $files[] = process_file($js_file);
277 daniel-mar 107
        }
108
}
112 daniel-mar 109
 
368 daniel-mar 110
# ---
362 daniel-mar 111
 
417 daniel-mar 112
if ($do_minify) {
368 daniel-mar 113
        $minifier = null;
114
        foreach ($files as $file) {
261 daniel-mar 115
                if (is_null($minifier)) {
116
                        $minifier = new Minify\JS($file);
117
                } else {
118
                        $minifier->add($file);
119
                }
368 daniel-mar 120
        }
121
        $out = is_null($minifier) ? '' : $minifier->minify();
122
} else {
123
        $out = '';
124
        foreach ($files as $file) {
364 daniel-mar 125
                if (file_exists($file)) {
126
                        $out .= file_get_contents($file)."\n";
127
                } else {
128
                        $out .= "$file\n";
129
                }
215 daniel-mar 130
        }
131
}
132
 
368 daniel-mar 133
# ---
218 daniel-mar 134
 
379 daniel-mar 135
httpOutWithETag($out, 'application/javascript', 'oidplus.js');