Subversion Repositories oidplus

Rev

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