Subversion Repositories oidplus

Rev

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