Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
596 daniel-mar 1
<?php
2 daniel-mar 2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
2 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
511 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
730 daniel-mar 26
class OIDplusGui extends OIDplusBaseClass {
2 daniel-mar 27
 
1116 daniel-mar 28
        /**
29
         * @param string $id
30
         * @return array
31
         */
32
        public static function generateContentPage(string $id): array {
2 daniel-mar 33
                $out = array();
34
 
35
                $handled = false;
36
                $out['title'] = '';
32 daniel-mar 37
                $out['icon'] = '';
2 daniel-mar 38
                $out['text'] = '';
39
 
281 daniel-mar 40
                foreach (OIDplus::getPagePlugins() as $plugin) {
348 daniel-mar 41
                        try {
42
                                $plugin->gui($id, $out, $handled);
1050 daniel-mar 43
                        } catch (\Exception $e) {
360 daniel-mar 44
                                $out['title'] = _L('Error');
800 daniel-mar 45
                                $out['icon'] = 'img/error.png';
348 daniel-mar 46
                                $out['text'] = $e->getMessage();
47
                        }
281 daniel-mar 48
                        if ($handled) break;
61 daniel-mar 49
                }
2 daniel-mar 50
 
51
                if (!$handled) {
936 daniel-mar 52
                        if (isset($_SERVER['SCRIPT_FILENAME']) && (strtolower(basename($_SERVER['SCRIPT_FILENAME'])) !== 'ajax.php')) { // don't send HTTP error codes in ajax.php, because we want a page and not a JavaScript alert box, when someone enters an invalid OID in the GoTo-Box
1068 daniel-mar 53
                                if (PHP_SAPI != 'cli') @http_response_code(404);
936 daniel-mar 54
                        }
360 daniel-mar 55
                        $out['title'] = _L('Error');
800 daniel-mar 56
                        $out['icon'] = 'img/error.png';
360 daniel-mar 57
                        $out['text'] = _L('The resource cannot be found.');
2 daniel-mar 58
                }
59
 
60
                return $out;
61
        }
250 daniel-mar 62
 
1116 daniel-mar 63
        /**
64
         * @param string $goto
65
         * @param bool $new_window
66
         * @return string
67
         */
68
        public static function link(string $goto, bool $new_window=false): string {
327 daniel-mar 69
                if ($new_window) {
70
                        return 'href="?goto='.urlencode($goto).'" target="_blank"';
250 daniel-mar 71
                } else {
327 daniel-mar 72
                        if (strpos($goto, '#') !== false) {
73
                                list($goto, $anchor) = explode('#', $goto, 2);
74
                                return 'href="?goto='.urlencode($goto).'#'.htmlentities($anchor).'" onclick="openOidInPanel('.js_escape($goto).', true, '.js_escape($anchor).'); return false;"';
75
                        } else {
76
                                return 'href="?goto='.urlencode($goto).'" onclick="openOidInPanel('.js_escape($goto).', true); return false;"';
77
                        }
250 daniel-mar 78
                }
79
        }
362 daniel-mar 80
 
1116 daniel-mar 81
        /**
82
         * @param string $goto
83
         * @param bool $useJs
84
         * @return string
85
         * @throws OIDplusConfigInitializationException
86
         * @throws OIDplusException
87
         */
88
        public static function getLanguageBox(string $goto, bool $useJs) {
89
                $out = '<div id="languageBox">';
362 daniel-mar 90
                $langbox_entries = array();
91
                $non_default_languages = 0;
92
                foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
389 daniel-mar 93
                        $flag = $pluginManifest->getLanguageFlag();
94
                        $code = $pluginManifest->getLanguageCode();
1041 daniel-mar 95
                        if ($code != OIDplus::getDefaultLang()) $non_default_languages++;
362 daniel-mar 96
                        if ($code == OIDplus::getCurrentLang()) {
97
                                $class = 'lng_flag';
98
                        } else {
99
                                $class = 'lng_flag picture_ghost';
100
                        }
1116 daniel-mar 101
                        $add = ($goto != '') ? '&amp;goto='.urlencode($goto) : '';
632 daniel-mar 102
 
635 daniel-mar 103
                        $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/language/'.$code.'/');
632 daniel-mar 104
 
105
                        if (count($dirs) > 0) {
106
                                $dir = substr($dirs[0], strlen(OIDplus::localpath()));
1060 daniel-mar 107
                                $langbox_entries[$code] = '<span class="lang_flag_bg"><a '.($useJs ? 'onclick="return !setLanguage(\''.$code.'\')" ' : '').'href="?lang='.$code.$add.'"><img src="'.OIDplus::webpath(null,OIDplus::PATH_RELATIVE).$dir.$flag.'" alt="'.$pluginManifest->getName().'" title="'.$pluginManifest->getName().'" class="'.$class.'" id="lng_flag_'.$code.'" height="20"></a></span> ';
632 daniel-mar 108
                        }
362 daniel-mar 109
                }
110
                if ($non_default_languages > 0) {
426 daniel-mar 111
                        foreach ($langbox_entries as $ent) {
1056 daniel-mar 112
                                $out .= "$ent\n\t\t";
426 daniel-mar 113
                        }
362 daniel-mar 114
                }
1056 daniel-mar 115
                $out .= '</div>';
116
                return $out;
362 daniel-mar 117
        }
366 daniel-mar 118
 
1116 daniel-mar 119
        /**
120
         * @param \Throwable $exception
121
         * @return void
122
         * @throws OIDplusException
123
         */
124
        public static function html_exception_handler(\Throwable $exception) {
366 daniel-mar 125
                if ($exception instanceof OIDplusConfigInitializationException) {
463 daniel-mar 126
                        echo '<!DOCTYPE HTML>';
1056 daniel-mar 127
                        echo '<html><head><title>'.htmlentities(_L('OIDplus initialization error')).'</title></head><body>';
128
                        echo '<h1>'.htmlentities(_L('OIDplus initialization error')).'</h1>';
366 daniel-mar 129
                        echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
1056 daniel-mar 130
                        $msg = _L('Please check the file %1','<b>userdata/baseconfig/config.inc.php</b>');
366 daniel-mar 131
                        if (is_dir(__DIR__ . '/../../setup')) {
1056 daniel-mar 132
                                $msg .= ' '._L('or run <a href="%1">setup</a> again',OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/');
366 daniel-mar 133
                        }
1115 daniel-mar 134
                        echo '<p>'.$msg.'</p>'; // No htmlentities, because we already did it above
780 daniel-mar 135
                        echo self::getExceptionTechInfo($exception);
463 daniel-mar 136
                        echo '</body></html>';
366 daniel-mar 137
                } else {
463 daniel-mar 138
                        echo '<!DOCTYPE HTML>';
1056 daniel-mar 139
                        echo '<html><head><title>'.htmlentities(_L('OIDplus error')).'</title></head><body>';
140
                        echo '<h1>'.htmlentities(_L('OIDplus error')).'</h1>';
366 daniel-mar 141
                        // ENT_SUBSTITUTE because ODBC drivers might return ANSI instead of UTF-8 stuff
142
                        echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
780 daniel-mar 143
                        echo self::getExceptionTechInfo($exception);
463 daniel-mar 144
                        echo '</body></html>';
366 daniel-mar 145
                }
146
        }
420 daniel-mar 147
 
1116 daniel-mar 148
        /**
149
         * @param \Throwable $exception
150
         * @return string
151
         */
152
        private static function getExceptionTechInfo(\Throwable $exception) {
153
                $out  = '<p><b>'.htmlentities(_L('Technical information about the problem')).':</b></p>';
780 daniel-mar 154
                $out .= '<pre>';
155
                $out .= get_class($exception)."\n";
156
                $out .= _L('at file %1 (line %2)',$exception->getFile(),"".$exception->getLine())."\n\n";
157
                $out .= _L('Stacktrace').":\n";
922 daniel-mar 158
                $out .= htmlentities($exception->getTraceAsString());
780 daniel-mar 159
                $out .= '</pre>';
160
                return $out;
161
        }
162
 
1116 daniel-mar 163
        /**
164
         * @return string
165
         */
420 daniel-mar 166
        public function tabBarStart() {
167
                return '<ul class="nav nav-tabs" id="myTab" role="tablist">';
168
        }
169
 
1116 daniel-mar 170
        /**
171
         * @return string
172
         */
420 daniel-mar 173
        public function tabBarEnd() {
174
                return '</ul>';
175
        }
176
 
1116 daniel-mar 177
        /**
178
         * @param string $id
179
         * @param string $title
180
         * @param bool $active
181
         * @return string
182
         */
183
        public function tabBarElement(string $id, string $title, bool $active) {
641 daniel-mar 184
                // data-bs-toggle is for Bootstrap 5
185
                // data-toggle is for Bootstrap 4 (InternetExplorer compatibility)
186
                return '<li class="nav-item"><a class="nav-link'.($active ? ' active' : '').'" id="'.$id.'-tab" data-bs-toggle="tab" data-toggle="tab" href="#'.$id.'" role="tab" aria-controls="'.$id.'" aria-selected="'.($active ? 'true' : 'false').'">'.$title.'</a></li>';
420 daniel-mar 187
        }
188
 
1116 daniel-mar 189
        /**
190
         * @return string
191
         */
420 daniel-mar 192
        public function tabContentStart() {
193
                return '<div class="tab-content" id="myTabContent">';
194
        }
195
 
1116 daniel-mar 196
        /**
197
         * @return string
198
         */
420 daniel-mar 199
        public function tabContentEnd() {
200
                return '</div>';
201
        }
202
 
1116 daniel-mar 203
        /**
204
         * @param string $id
205
         * @param string $content
206
         * @param bool $active
207
         * @return string
208
         */
209
        public function tabContentPage(string $id, string $content, bool $active) {
420 daniel-mar 210
                return '<div class="tab-pane fade'.($active ? ' show active' : '').'" id="'.$id.'" role="tabpanel" aria-labelledby="'.$id.'-tab">'.$content.'</div>';
211
        }
212
 
1116 daniel-mar 213
        /**
214
         * @param string $systemtitle
215
         * @param string $pagetitle
216
         * @return string
217
         */
218
        public function combine_systemtitle_and_pagetitle(string $systemtitle, string $pagetitle) {
1066 daniel-mar 219
                // Please also change the function in oidplus_base.js
220
                if ($systemtitle == $pagetitle) {
221
                        return $systemtitle;
222
                } else {
223
                        return $pagetitle . ' - ' . $systemtitle;
224
                }
225
        }
226
 
1116 daniel-mar 227
        /**
228
         * @param string $title
229
         * @return string[]
230
         * @throws OIDplusException
231
         */
232
        private function getCommonHeadElems(string $title): array {
1065 daniel-mar 233
                // Get theme color (color of title bar)
234
                $design_plugin = OIDplus::getActiveDesignPlugin();
235
                $theme_color = is_null($design_plugin) ? '' : $design_plugin->getThemeColor();
1055 daniel-mar 236
 
1065 daniel-mar 237
                $head_elems = array();
238
                $head_elems[] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
239
                if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN','') !== '') {
240
                        $head_elems[] = '<meta name="OIDplus-SystemTitle" content="'.htmlentities(OIDplus::config()->getValue('system_title')).'">'; // Do not remove. This meta tag is acessed by oidplus_base.js
241
                }
242
                if ($theme_color != '') {
243
                        $head_elems[] = '<meta name="theme-color" content="'.htmlentities($theme_color).'">';
244
                }
245
                $head_elems[] = '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
1066 daniel-mar 246
                $head_elems[] = '<title>'.htmlentities($title).'</title>';
1065 daniel-mar 247
                $head_elems[] = '<script src="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'polyfill.min.js.php"></script>';
248
                $head_elems[] = '<script src="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'oidplus.min.js.php?noBaseConfig=1" type="text/javascript"></script>';
249
                $head_elems[] = '<link rel="stylesheet" href="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'oidplus.min.css.php?noBaseConfig=1">';
250
                $head_elems[] = '<link rel="shortcut icon" type="image/x-icon" href="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'favicon.ico.php">';
251
                if (OIDplus::baseConfig()->exists('CANONICAL_SYSTEM_URL')) {
1066 daniel-mar 252
                        $head_elems[] = '<link rel="canonical" href="'.htmlentities(OIDplus::canonicalURL()).OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'">';
1065 daniel-mar 253
                }
1066 daniel-mar 254
 
255
                return $head_elems;
256
        }
257
 
1116 daniel-mar 258
        /**
259
         * @param string $page_title_1
260
         * @param string $page_title_2
261
         * @param string $static_icon
262
         * @param string $static_content
263
         * @param array $extra_head_tags
264
         * @param string $static_node_id
265
         * @return string
266
         * @throws OIDplusConfigInitializationException
267
         * @throws OIDplusException
268
         */
269
        public function showMainPage(string $page_title_1, string $page_title_2, string $static_icon, string $static_content, array $extra_head_tags=array(), string $static_node_id='') {
1066 daniel-mar 270
                $head_elems = $this->getCommonHeadElems($page_title_1);
1065 daniel-mar 271
                $head_elems = array_merge($head_elems, $extra_head_tags);
1055 daniel-mar 272
 
1116 daniel-mar 273
                $plugins = OIDplus::getAllPlugins();
1066 daniel-mar 274
                foreach ($plugins as $plugin) {
275
                        $plugin->htmlHeaderUpdate($head_elems);
276
                }
277
 
1065 daniel-mar 278
                # ---
1055 daniel-mar 279
 
1065 daniel-mar 280
                $out  = "<!DOCTYPE html>\n";
281
 
282
                $out .= "<html lang=\"".substr(OIDplus::getCurrentLang(),0,2)."\">\n";
283
                $out .= "<head>\n";
284
                $out .= "\t".implode("\n\t",$head_elems)."\n";
285
                $out .= "</head>\n";
286
 
287
                $out .= "<body>\n";
288
 
1056 daniel-mar 289
                $out .= '<div id="loading" style="display:none">Loading&#8230;</div>';
1055 daniel-mar 290
 
1056 daniel-mar 291
                $out .= '<div id="frames">';
292
                $out .= '<div id="content_window" class="borderbox">';
1055 daniel-mar 293
 
1056 daniel-mar 294
                $out .= '<h1 id="real_title">';
295
                if ($static_icon != '') $out .= '<img src="'.htmlentities($static_icon).'" width="48" height="48" alt=""> ';
296
                $out .= htmlentities($page_title_2).'</h1>';
297
                $out .= '<div id="real_content">'.$static_content.'</div>';
1066 daniel-mar 298
                if ((!isset($_SERVER['REQUEST_METHOD'])) || ($_SERVER['REQUEST_METHOD'] == 'GET')) {
299
                        $out .= '<br><p><img src="img/share.png" width="15" height="15" alt="'._L('Share').'"> <a href="?goto='.htmlentities($static_node_id).'" id="static_link" class="gray_footer_font">'._L('Static link to this page').'</a>';
300
                        $out .= '</p>';
301
                }
1056 daniel-mar 302
                $out .= '<br>';
1055 daniel-mar 303
 
1056 daniel-mar 304
                $out .= '</div>';
1055 daniel-mar 305
 
1056 daniel-mar 306
                $out .= '<div id="system_title_bar">';
1055 daniel-mar 307
 
1066 daniel-mar 308
                $out .= '<div id="system_title_menu" onclick="mobileNavButtonClick(this)" onmouseenter="mobileNavButtonHover(this)" onmouseleave="mobileNavButtonHover(this)">';
309
                $out .= '       <div id="bar1"></div>';
310
                $out .= '       <div id="bar2"></div>';
311
                $out .= '       <div id="bar3"></div>';
312
                $out .= '</div>';
313
 
1056 daniel-mar 314
                $out .= '<div id="system_title_text">';
1066 daniel-mar 315
                $out .= '       <a '.OIDplus::gui()->link('oidplus:system').' id="system_title_a">';
316
                $out .= '               <span id="system_title_logo"></span>';
317
                $out .= '               <span id="system_title_1">'.htmlentities(OIDplus::getEditionInfo()['vendor'].' OIDplus 2.0').'</span><br>';
318
                $out .= '               <span id="system_title_2">'.htmlentities(OIDplus::config()->getValue('system_title')).'</span>';
319
                $out .= '       </a>';
320
                $out .= '</div>';
321
 
322
                $out .= '</div>';
323
 
324
                $out .= OIDplus::gui()->getLanguageBox($static_node_id, true);
325
 
326
                $out .= '<div id="gotobox">';
327
                $out .= '<input type="text" name="goto" id="gotoedit" value="'.htmlentities($static_node_id).'">';
328
                $out .= '<input type="button" value="'._L('Go').'" onclick="gotoButtonClicked()" id="gotobutton">';
329
                $out .= '</div>';
330
 
331
                $out .= '<div id="oidtree" class="borderbox">';
332
                //$out .= '<noscript>';
333
                //$out .= '<p><b>'._L('Please enable JavaScript to use all features').'</b></p>';
334
                //$out .= '</noscript>';
335
                $out .= OIDplus::menuUtils()->nonjs_menu();
336
                $out .= '</div>';
337
 
338
                $out .= '</div>';
339
 
340
                $out .= "\n</body>\n";
341
                $out .= "</html>\n";
342
 
343
                # ---
344
 
1116 daniel-mar 345
                $plugins = OIDplus::getAllPlugins();
1066 daniel-mar 346
                foreach ($plugins as $plugin) {
347
                        $plugin->htmlPostprocess($out);
348
                }
349
 
350
                return $out;
351
        }
352
 
1116 daniel-mar 353
        /**
354
         * @param string $page_title_1
355
         * @param string $page_title_2
356
         * @param string $static_icon
357
         * @param string $static_content
358
         * @param string[] $extra_head_tags
359
         * @return string
360
         * @throws OIDplusConfigInitializationException
361
         * @throws OIDplusException
362
         */
363
        public function showSimplePage(string $page_title_1, string $page_title_2, string $static_icon, string $static_content, array $extra_head_tags=array()) {
1066 daniel-mar 364
                $head_elems = $this->getCommonHeadElems($page_title_1);
365
                $head_elems = array_merge($head_elems, $extra_head_tags);
366
 
367
                # ---
368
 
369
                $out  = "<!DOCTYPE html>\n";
370
 
371
                $out .= "<html lang=\"".substr(OIDplus::getCurrentLang(),0,2)."\">\n";
372
                $out .= "<head>\n";
373
                $out .= "\t".implode("\n\t",$head_elems)."\n";
374
                $out .= "</head>\n";
375
 
376
                $out .= "<body>\n";
377
 
378
                $out .= '<div id="loading" style="display:none">Loading&#8230;</div>';
379
 
380
                $out .= '<div id="frames">';
381
                $out .= '<div id="content_window" class="borderbox">';
382
 
383
                $out .= '<h1 id="real_title">';
384
                if ($static_icon != '') $out .= '<img src="'.htmlentities($static_icon).'" width="48" height="48" alt=""> ';
385
                $out .= htmlentities($page_title_2).'</h1>';
386
                $out .= '<div id="real_content">'.$static_content.'</div>';
387
                $out .= '<br>';
388
 
389
                $out .= '</div>';
390
 
391
                $out .= '<div id="system_title_bar">';
392
 
393
                $out .= '<div id="system_title_text">';
1056 daniel-mar 394
                $out .= '       <span id="system_title_logo"></span>';
395
                $out .= '       <span id="system_title_1">'.htmlentities(OIDplus::getEditionInfo()['vendor'].' OIDplus 2.0').'</span><br>';
396
                $out .= '       <span id="system_title_2">'.htmlentities($page_title_1).'</span>';
397
                $out .= '</div>';
1055 daniel-mar 398
 
1056 daniel-mar 399
                $out .= '</div>';
1055 daniel-mar 400
 
1116 daniel-mar 401
                $out .= OIDplus::gui()->getLanguageBox('', true);
1055 daniel-mar 402
 
1056 daniel-mar 403
                $out .= '</div>';
404
 
1065 daniel-mar 405
                $out .= "\n</body>\n";
406
                $out .= "</html>\n";
1056 daniel-mar 407
 
1065 daniel-mar 408
                # ---
409
 
1056 daniel-mar 410
                return $out;
1055 daniel-mar 411
        }
412
 
366 daniel-mar 413
}