Subversion Repositories oidplus

Rev

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