Subversion Repositories oidplus

Rev

Rev 1205 | Rev 1263 | 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 = '';
1227 daniel-mar 140
                        //$htmlMessage = htmlentities($exception->getMessage());
141
                        $htmlMessage = nl2br(htmlentities(html_to_text($exception->getMessage())));
142
 
366 daniel-mar 143
                }
1203 daniel-mar 144
                if (!$htmlTitle) {
145
                        $htmlTitle = _L('OIDplus Error');
146
                }
147
 
148
                echo '<!DOCTYPE HTML>';
149
                echo '<html><head><title>'.$htmlTitle.'</title></head><body>';
150
                echo '<h1>'.$htmlTitle.'</h1>';
151
                echo $htmlMessage;
152
                echo self::getExceptionTechInfo($exception);
153
                echo '</body></html>';
366 daniel-mar 154
        }
420 daniel-mar 155
 
1116 daniel-mar 156
        /**
157
         * @param \Throwable $exception
158
         * @return string
159
         */
1188 daniel-mar 160
        private static function getExceptionTechInfo(\Throwable $exception): string {
1116 daniel-mar 161
                $out  = '<p><b>'.htmlentities(_L('Technical information about the problem')).':</b></p>';
780 daniel-mar 162
                $out .= '<pre>';
163
                $out .= get_class($exception)."\n";
1227 daniel-mar 164
 
165
                $sourceFile = $exception->getFile();
1201 daniel-mar 166
                $stacktrace = $exception->getTraceAsString();
1227 daniel-mar 167
 
168
                // Censor paths
1201 daniel-mar 169
                try {
170
                        $syspath = OIDplus::localpath(NULL);
171
                        $stacktrace = str_replace($syspath, '...'.DIRECTORY_SEPARATOR, $stacktrace); // for security
1227 daniel-mar 172
                        $sourceFile = str_replace($syspath, '...'.DIRECTORY_SEPARATOR, $sourceFile); // for security
1201 daniel-mar 173
                } catch (\Throwable $e) {
174
                        // Catch Exception and Error, because this step (censoring) is purely optional and shoult not prevent the stacktrace of being shown
175
                }
1227 daniel-mar 176
 
177
                $out .= _L('at file %1 (line %2)',$sourceFile,"".$exception->getLine())."\n\n";
178
                $out .= _L('Stacktrace').":\n";
1201 daniel-mar 179
                $out .= htmlentities($stacktrace);
1227 daniel-mar 180
 
780 daniel-mar 181
                $out .= '</pre>';
182
                return $out;
183
        }
184
 
1116 daniel-mar 185
        /**
186
         * @return string
187
         */
1130 daniel-mar 188
        public function tabBarStart(): string {
420 daniel-mar 189
                return '<ul class="nav nav-tabs" id="myTab" role="tablist">';
190
        }
191
 
1116 daniel-mar 192
        /**
193
         * @return string
194
         */
1130 daniel-mar 195
        public function tabBarEnd(): string {
420 daniel-mar 196
                return '</ul>';
197
        }
198
 
1116 daniel-mar 199
        /**
200
         * @param string $id
201
         * @param string $title
202
         * @param bool $active
203
         * @return string
204
         */
1130 daniel-mar 205
        public function tabBarElement(string $id, string $title, bool $active): string {
641 daniel-mar 206
                // data-bs-toggle is for Bootstrap 5
207
                // data-toggle is for Bootstrap 4 (InternetExplorer compatibility)
208
                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 209
        }
210
 
1116 daniel-mar 211
        /**
212
         * @return string
213
         */
1130 daniel-mar 214
        public function tabContentStart(): string {
420 daniel-mar 215
                return '<div class="tab-content" id="myTabContent">';
216
        }
217
 
1116 daniel-mar 218
        /**
219
         * @return string
220
         */
1130 daniel-mar 221
        public function tabContentEnd(): string {
420 daniel-mar 222
                return '</div>';
223
        }
224
 
1116 daniel-mar 225
        /**
226
         * @param string $id
227
         * @param string $content
228
         * @param bool $active
229
         * @return string
230
         */
1130 daniel-mar 231
        public function tabContentPage(string $id, string $content, bool $active): string {
420 daniel-mar 232
                return '<div class="tab-pane fade'.($active ? ' show active' : '').'" id="'.$id.'" role="tabpanel" aria-labelledby="'.$id.'-tab">'.$content.'</div>';
233
        }
234
 
1116 daniel-mar 235
        /**
236
         * @param string $systemtitle
237
         * @param string $pagetitle
238
         * @return string
239
         */
1130 daniel-mar 240
        public function combine_systemtitle_and_pagetitle(string $systemtitle, string $pagetitle): string {
1066 daniel-mar 241
                // Please also change the function in oidplus_base.js
242
                if ($systemtitle == $pagetitle) {
243
                        return $systemtitle;
244
                } else {
245
                        return $pagetitle . ' - ' . $systemtitle;
246
                }
247
        }
248
 
1116 daniel-mar 249
        /**
250
         * @param string $title
251
         * @return string[]
252
         * @throws OIDplusException
253
         */
254
        private function getCommonHeadElems(string $title): array {
1065 daniel-mar 255
                // Get theme color (color of title bar)
256
                $design_plugin = OIDplus::getActiveDesignPlugin();
257
                $theme_color = is_null($design_plugin) ? '' : $design_plugin->getThemeColor();
1055 daniel-mar 258
 
1065 daniel-mar 259
                $head_elems = array();
260
                $head_elems[] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
261
                if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN','') !== '') {
262
                        $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
263
                }
264
                if ($theme_color != '') {
265
                        $head_elems[] = '<meta name="theme-color" content="'.htmlentities($theme_color).'">';
266
                }
267
                $head_elems[] = '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
1066 daniel-mar 268
                $head_elems[] = '<title>'.htmlentities($title).'</title>';
1065 daniel-mar 269
                $head_elems[] = '<script src="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'polyfill.min.js.php"></script>';
270
                $head_elems[] = '<script src="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'oidplus.min.js.php?noBaseConfig=1" type="text/javascript"></script>';
271
                $head_elems[] = '<link rel="stylesheet" href="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'oidplus.min.css.php?noBaseConfig=1">';
272
                $head_elems[] = '<link rel="shortcut icon" type="image/x-icon" href="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'favicon.ico.php">';
273
                if (OIDplus::baseConfig()->exists('CANONICAL_SYSTEM_URL')) {
1066 daniel-mar 274
                        $head_elems[] = '<link rel="canonical" href="'.htmlentities(OIDplus::canonicalURL()).OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'">';
1065 daniel-mar 275
                }
1066 daniel-mar 276
 
277
                return $head_elems;
278
        }
279
 
1116 daniel-mar 280
        /**
281
         * @param string $page_title_1
282
         * @param string $page_title_2
283
         * @param string $static_icon
284
         * @param string $static_content
285
         * @param array $extra_head_tags
286
         * @param string $static_node_id
287
         * @return string
288
         * @throws OIDplusConfigInitializationException
289
         * @throws OIDplusException
290
         */
1130 daniel-mar 291
        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 292
                $head_elems = $this->getCommonHeadElems($page_title_1);
1065 daniel-mar 293
                $head_elems = array_merge($head_elems, $extra_head_tags);
1055 daniel-mar 294
 
1116 daniel-mar 295
                $plugins = OIDplus::getAllPlugins();
1066 daniel-mar 296
                foreach ($plugins as $plugin) {
297
                        $plugin->htmlHeaderUpdate($head_elems);
298
                }
299
 
1065 daniel-mar 300
                # ---
1055 daniel-mar 301
 
1065 daniel-mar 302
                $out  = "<!DOCTYPE html>\n";
303
 
304
                $out .= "<html lang=\"".substr(OIDplus::getCurrentLang(),0,2)."\">\n";
305
                $out .= "<head>\n";
306
                $out .= "\t".implode("\n\t",$head_elems)."\n";
307
                $out .= "</head>\n";
308
 
309
                $out .= "<body>\n";
310
 
1056 daniel-mar 311
                $out .= '<div id="loading" style="display:none">Loading&#8230;</div>';
1055 daniel-mar 312
 
1056 daniel-mar 313
                $out .= '<div id="frames">';
314
                $out .= '<div id="content_window" class="borderbox">';
1055 daniel-mar 315
 
1056 daniel-mar 316
                $out .= '<h1 id="real_title">';
317
                if ($static_icon != '') $out .= '<img src="'.htmlentities($static_icon).'" width="48" height="48" alt=""> ';
318
                $out .= htmlentities($page_title_2).'</h1>';
319
                $out .= '<div id="real_content">'.$static_content.'</div>';
1066 daniel-mar 320
                if ((!isset($_SERVER['REQUEST_METHOD'])) || ($_SERVER['REQUEST_METHOD'] == 'GET')) {
321
                        $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>';
322
                        $out .= '</p>';
323
                }
1056 daniel-mar 324
                $out .= '<br>';
1055 daniel-mar 325
 
1056 daniel-mar 326
                $out .= '</div>';
1055 daniel-mar 327
 
1056 daniel-mar 328
                $out .= '<div id="system_title_bar">';
1055 daniel-mar 329
 
1066 daniel-mar 330
                $out .= '<div id="system_title_menu" onclick="mobileNavButtonClick(this)" onmouseenter="mobileNavButtonHover(this)" onmouseleave="mobileNavButtonHover(this)">';
331
                $out .= '       <div id="bar1"></div>';
332
                $out .= '       <div id="bar2"></div>';
333
                $out .= '       <div id="bar3"></div>';
334
                $out .= '</div>';
335
 
1056 daniel-mar 336
                $out .= '<div id="system_title_text">';
1066 daniel-mar 337
                $out .= '       <a '.OIDplus::gui()->link('oidplus:system').' id="system_title_a">';
338
                $out .= '               <span id="system_title_logo"></span>';
339
                $out .= '               <span id="system_title_1">'.htmlentities(OIDplus::getEditionInfo()['vendor'].' OIDplus 2.0').'</span><br>';
340
                $out .= '               <span id="system_title_2">'.htmlentities(OIDplus::config()->getValue('system_title')).'</span>';
341
                $out .= '       </a>';
342
                $out .= '</div>';
343
 
344
                $out .= '</div>';
345
 
346
                $out .= OIDplus::gui()->getLanguageBox($static_node_id, true);
347
 
348
                $out .= '<div id="gotobox">';
349
                $out .= '<input type="text" name="goto" id="gotoedit" value="'.htmlentities($static_node_id).'">';
350
                $out .= '<input type="button" value="'._L('Go').'" onclick="gotoButtonClicked()" id="gotobutton">';
351
                $out .= '</div>';
352
 
353
                $out .= '<div id="oidtree" class="borderbox">';
354
                //$out .= '<noscript>';
355
                //$out .= '<p><b>'._L('Please enable JavaScript to use all features').'</b></p>';
356
                //$out .= '</noscript>';
357
                $out .= OIDplus::menuUtils()->nonjs_menu();
358
                $out .= '</div>';
359
 
360
                $out .= '</div>';
361
 
362
                $out .= "\n</body>\n";
363
                $out .= "</html>\n";
364
 
365
                # ---
366
 
1116 daniel-mar 367
                $plugins = OIDplus::getAllPlugins();
1066 daniel-mar 368
                foreach ($plugins as $plugin) {
369
                        $plugin->htmlPostprocess($out);
370
                }
371
 
372
                return $out;
373
        }
374
 
1116 daniel-mar 375
        /**
376
         * @param string $page_title_1
377
         * @param string $page_title_2
378
         * @param string $static_icon
379
         * @param string $static_content
380
         * @param string[] $extra_head_tags
381
         * @return string
382
         * @throws OIDplusConfigInitializationException
383
         * @throws OIDplusException
384
         */
1130 daniel-mar 385
        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 386
                $head_elems = $this->getCommonHeadElems($page_title_1);
387
                $head_elems = array_merge($head_elems, $extra_head_tags);
388
 
389
                # ---
390
 
391
                $out  = "<!DOCTYPE html>\n";
392
 
393
                $out .= "<html lang=\"".substr(OIDplus::getCurrentLang(),0,2)."\">\n";
394
                $out .= "<head>\n";
395
                $out .= "\t".implode("\n\t",$head_elems)."\n";
396
                $out .= "</head>\n";
397
 
398
                $out .= "<body>\n";
399
 
400
                $out .= '<div id="loading" style="display:none">Loading&#8230;</div>';
401
 
402
                $out .= '<div id="frames">';
403
                $out .= '<div id="content_window" class="borderbox">';
404
 
405
                $out .= '<h1 id="real_title">';
406
                if ($static_icon != '') $out .= '<img src="'.htmlentities($static_icon).'" width="48" height="48" alt=""> ';
407
                $out .= htmlentities($page_title_2).'</h1>';
408
                $out .= '<div id="real_content">'.$static_content.'</div>';
409
                $out .= '<br>';
410
 
411
                $out .= '</div>';
412
 
413
                $out .= '<div id="system_title_bar">';
414
 
415
                $out .= '<div id="system_title_text">';
1056 daniel-mar 416
                $out .= '       <span id="system_title_logo"></span>';
417
                $out .= '       <span id="system_title_1">'.htmlentities(OIDplus::getEditionInfo()['vendor'].' OIDplus 2.0').'</span><br>';
418
                $out .= '       <span id="system_title_2">'.htmlentities($page_title_1).'</span>';
419
                $out .= '</div>';
1055 daniel-mar 420
 
1056 daniel-mar 421
                $out .= '</div>';
1055 daniel-mar 422
 
1116 daniel-mar 423
                $out .= OIDplus::gui()->getLanguageBox('', true);
1055 daniel-mar 424
 
1056 daniel-mar 425
                $out .= '</div>';
426
 
1065 daniel-mar 427
                $out .= "\n</body>\n";
428
                $out .= "</html>\n";
1056 daniel-mar 429
 
1065 daniel-mar 430
                # ---
431
 
1056 daniel-mar 432
                return $out;
1055 daniel-mar 433
        }
434
 
366 daniel-mar 435
}