Subversion Repositories oidplus

Rev

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