Subversion Repositories oidplus

Rev

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