Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1116 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
  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.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusGui extends OIDplusBaseClass {
  27.  
  28.         public static function generateContentPage($id) {
  29.                 $out = array();
  30.  
  31.                 $handled = false;
  32.                 $out['title'] = '';
  33.                 $out['icon'] = '';
  34.                 $out['text'] = '';
  35.  
  36.                 foreach (OIDplus::getPagePlugins() as $plugin) {
  37.                         try {
  38.                                 $plugin->gui($id, $out, $handled);
  39.                         } catch (\Exception $e) {
  40.                                 $out['title'] = _L('Error');
  41.                                 $out['icon'] = 'img/error.png';
  42.                                 $out['text'] = $e->getMessage();
  43.                         }
  44.                         if ($handled) break;
  45.                 }
  46.  
  47.                 if (!$handled) {
  48.                         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
  49.                                 if (PHP_SAPI != 'cli') @http_response_code(404);
  50.                         }
  51.                         $out['title'] = _L('Error');
  52.                         $out['icon'] = 'img/error.png';
  53.                         $out['text'] = _L('The resource cannot be found.');
  54.                 }
  55.  
  56.                 return $out;
  57.         }
  58.  
  59.         public static function link($goto, $new_window=false): string {
  60.                 if ($new_window) {
  61.                         return 'href="?goto='.urlencode($goto).'" target="_blank"';
  62.                 } else {
  63.                         if (strpos($goto, '#') !== false) {
  64.                                 list($goto, $anchor) = explode('#', $goto, 2);
  65.                                 return 'href="?goto='.urlencode($goto).'#'.htmlentities($anchor).'" onclick="openOidInPanel('.js_escape($goto).', true, '.js_escape($anchor).'); return false;"';
  66.                         } else {
  67.                                 return 'href="?goto='.urlencode($goto).'" onclick="openOidInPanel('.js_escape($goto).', true); return false;"';
  68.                         }
  69.                 }
  70.         }
  71.  
  72.         public static function getLanguageBox($goto, $useJs) {
  73.                 $out = '';
  74.                 $out .= '<div id="languageBox">';
  75.                 $langbox_entries = array();
  76.                 $non_default_languages = 0;
  77.                 foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
  78.                         $flag = $pluginManifest->getLanguageFlag();
  79.                         $code = $pluginManifest->getLanguageCode();
  80.                         if ($code != OIDplus::getDefaultLang()) $non_default_languages++;
  81.                         if ($code == OIDplus::getCurrentLang()) {
  82.                                 $class = 'lng_flag';
  83.                         } else {
  84.                                 $class = 'lng_flag picture_ghost';
  85.                         }
  86.                         $add = (!is_null($goto)) ? '&amp;goto='.urlencode($goto) : '';
  87.  
  88.                         $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/language/'.$code.'/');
  89.  
  90.                         if (count($dirs) > 0) {
  91.                                 $dir = substr($dirs[0], strlen(OIDplus::localpath()));
  92.                                 $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> ';
  93.                         }
  94.                 }
  95.                 if ($non_default_languages > 0) {
  96.                         foreach ($langbox_entries as $ent) {
  97.                                 $out .= "$ent\n\t\t";
  98.                         }
  99.                 }
  100.                 $out .= '</div>';
  101.                 return $out;
  102.         }
  103.  
  104.         public static function html_exception_handler($exception) {
  105.                 if ($exception instanceof OIDplusConfigInitializationException) {
  106.                         echo '<!DOCTYPE HTML>';
  107.                         echo '<html><head><title>'.htmlentities(_L('OIDplus initialization error')).'</title></head><body>';
  108.                         echo '<h1>'.htmlentities(_L('OIDplus initialization error')).'</h1>';
  109.                         echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
  110.                         $msg = _L('Please check the file %1','<b>userdata/baseconfig/config.inc.php</b>');
  111.                         if (is_dir(__DIR__ . '/../../setup')) {
  112.                                 $msg .= ' '._L('or run <a href="%1">setup</a> again',OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/');
  113.                         }
  114.                         echo '<p>'.$msg.'</p>'; // No htmlentities, because we already did it above
  115.                         echo self::getExceptionTechInfo($exception);
  116.                         echo '</body></html>';
  117.                 } else {
  118.                         echo '<!DOCTYPE HTML>';
  119.                         echo '<html><head><title>'.htmlentities(_L('OIDplus error')).'</title></head><body>';
  120.                         echo '<h1>'.htmlentities(_L('OIDplus error')).'</h1>';
  121.                         // ENT_SUBSTITUTE because ODBC drivers might return ANSI instead of UTF-8 stuff
  122.                         echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
  123.                         echo self::getExceptionTechInfo($exception);
  124.                         echo '</body></html>';
  125.                 }
  126.         }
  127.  
  128.         private static function getExceptionTechInfo($exception) {
  129.                 $out = '';
  130.                 $out .= '<p><b>'.htmlentities(_L('Technical information about the problem')).':</b></p>';
  131.                 $out .= '<pre>';
  132.                 $out .= get_class($exception)."\n";
  133.                 $out .= _L('at file %1 (line %2)',$exception->getFile(),"".$exception->getLine())."\n\n";
  134.                 $out .= _L('Stacktrace').":\n";
  135.                 $out .= htmlentities($exception->getTraceAsString());
  136.                 $out .= '</pre>';
  137.                 return $out;
  138.         }
  139.  
  140.         public function tabBarStart() {
  141.                 return '<ul class="nav nav-tabs" id="myTab" role="tablist">';
  142.         }
  143.  
  144.         public function tabBarEnd() {
  145.                 return '</ul>';
  146.         }
  147.  
  148.         public function tabBarElement($id, $title, $active) {
  149.                 // data-bs-toggle is for Bootstrap 5
  150.                 // data-toggle is for Bootstrap 4 (InternetExplorer compatibility)
  151.                 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>';
  152.         }
  153.  
  154.         public function tabContentStart() {
  155.                 return '<div class="tab-content" id="myTabContent">';
  156.         }
  157.  
  158.         public function tabContentEnd() {
  159.                 return '</div>';
  160.         }
  161.  
  162.         public function tabContentPage($id, $content, $active) {
  163.                 return '<div class="tab-pane fade'.($active ? ' show active' : '').'" id="'.$id.'" role="tabpanel" aria-labelledby="'.$id.'-tab">'.$content.'</div>';
  164.         }
  165.  
  166.         public function combine_systemtitle_and_pagetitle($systemtitle, $pagetitle) {
  167.                 // Please also change the function in oidplus_base.js
  168.                 if ($systemtitle == $pagetitle) {
  169.                         return $systemtitle;
  170.                 } else {
  171.                         return $pagetitle . ' - ' . $systemtitle;
  172.                 }
  173.         }
  174.  
  175.         private function getCommonHeadElems($title) {
  176.                 // Get theme color (color of title bar)
  177.                 $design_plugin = OIDplus::getActiveDesignPlugin();
  178.                 $theme_color = is_null($design_plugin) ? '' : $design_plugin->getThemeColor();
  179.  
  180.                 $head_elems = array();
  181.                 $head_elems[] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
  182.                 if (OIDplus::baseConfig()->getValue('DATABASE_PLUGIN','') !== '') {
  183.                         $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
  184.                 }
  185.                 if ($theme_color != '') {
  186.                         $head_elems[] = '<meta name="theme-color" content="'.htmlentities($theme_color).'">';
  187.                 }
  188.                 $head_elems[] = '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
  189.                 $head_elems[] = '<title>'.htmlentities($title).'</title>';
  190.                 $head_elems[] = '<script src="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'polyfill.min.js.php"></script>';
  191.                 $head_elems[] = '<script src="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'oidplus.min.js.php?noBaseConfig=1" type="text/javascript"></script>';
  192.                 $head_elems[] = '<link rel="stylesheet" href="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'oidplus.min.css.php?noBaseConfig=1">';
  193.                 $head_elems[] = '<link rel="shortcut icon" type="image/x-icon" href="'.OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'favicon.ico.php">';
  194.                 if (OIDplus::baseConfig()->exists('CANONICAL_SYSTEM_URL')) {
  195.                         $head_elems[] = '<link rel="canonical" href="'.htmlentities(OIDplus::canonicalURL()).OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'">';
  196.                 }
  197.  
  198.                 return $head_elems;
  199.         }
  200.  
  201.         public function showMainPage($page_title_1, $page_title_2, $static_icon, $static_content, $extra_head_tags=array(), $static_node_id='') {
  202.                 $head_elems = $this->getCommonHeadElems($page_title_1);
  203.                 $head_elems = array_merge($head_elems, $extra_head_tags);
  204.  
  205.                 $plugins = OIDplus::getPagePlugins();
  206.                 foreach ($plugins as $plugin) {
  207.                         $plugin->htmlHeaderUpdate($head_elems);
  208.                 }
  209.  
  210.                 # ---
  211.  
  212.                 $out  = "<!DOCTYPE html>\n";
  213.  
  214.                 $out .= "<html lang=\"".substr(OIDplus::getCurrentLang(),0,2)."\">\n";
  215.                 $out .= "<head>\n";
  216.                 $out .= "\t".implode("\n\t",$head_elems)."\n";
  217.                 $out .= "</head>\n";
  218.  
  219.                 $out .= "<body>\n";
  220.  
  221.                 $out .= '<div id="loading" style="display:none">Loading&#8230;</div>';
  222.  
  223.                 $out .= '<div id="frames">';
  224.                 $out .= '<div id="content_window" class="borderbox">';
  225.  
  226.                 $out .= '<h1 id="real_title">';
  227.                 if ($static_icon != '') $out .= '<img src="'.htmlentities($static_icon).'" width="48" height="48" alt=""> ';
  228.                 $out .= htmlentities($page_title_2).'</h1>';
  229.                 $out .= '<div id="real_content">'.$static_content.'</div>';
  230.                 if ((!isset($_SERVER['REQUEST_METHOD'])) || ($_SERVER['REQUEST_METHOD'] == 'GET')) {
  231.                         $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>';
  232.                         $out .= '</p>';
  233.                 }
  234.                 $out .= '<br>';
  235.  
  236.                 $out .= '</div>';
  237.  
  238.                 $out .= '<div id="system_title_bar">';
  239.  
  240.                 $out .= '<div id="system_title_menu" onclick="mobileNavButtonClick(this)" onmouseenter="mobileNavButtonHover(this)" onmouseleave="mobileNavButtonHover(this)">';
  241.                 $out .= '       <div id="bar1"></div>';
  242.                 $out .= '       <div id="bar2"></div>';
  243.                 $out .= '       <div id="bar3"></div>';
  244.                 $out .= '</div>';
  245.  
  246.                 $out .= '<div id="system_title_text">';
  247.                 $out .= '       <a '.OIDplus::gui()->link('oidplus:system').' id="system_title_a">';
  248.                 $out .= '               <span id="system_title_logo"></span>';
  249.                 $out .= '               <span id="system_title_1">'.htmlentities(OIDplus::getEditionInfo()['vendor'].' OIDplus 2.0').'</span><br>';
  250.                 $out .= '               <span id="system_title_2">'.htmlentities(OIDplus::config()->getValue('system_title')).'</span>';
  251.                 $out .= '       </a>';
  252.                 $out .= '</div>';
  253.  
  254.                 $out .= '</div>';
  255.  
  256.                 $out .= OIDplus::gui()->getLanguageBox($static_node_id, true);
  257.  
  258.                 $out .= '<div id="gotobox">';
  259.                 $out .= '<input type="text" name="goto" id="gotoedit" value="'.htmlentities($static_node_id).'">';
  260.                 $out .= '<input type="button" value="'._L('Go').'" onclick="gotoButtonClicked()" id="gotobutton">';
  261.                 $out .= '</div>';
  262.  
  263.                 $out .= '<div id="oidtree" class="borderbox">';
  264.                 //$out .= '<noscript>';
  265.                 //$out .= '<p><b>'._L('Please enable JavaScript to use all features').'</b></p>';
  266.                 //$out .= '</noscript>';
  267.                 $out .= OIDplus::menuUtils()->nonjs_menu();
  268.                 $out .= '</div>';
  269.  
  270.                 $out .= '</div>';
  271.  
  272.                 $out .= "\n</body>\n";
  273.                 $out .= "</html>\n";
  274.  
  275.                 # ---
  276.  
  277.                 $plugins = OIDplus::getPagePlugins();
  278.                 foreach ($plugins as $plugin) {
  279.                         $plugin->htmlPostprocess($out);
  280.                 }
  281.  
  282.                 return $out;
  283.         }
  284.  
  285.         public function showSimplePage($page_title_1, $page_title_2, $static_icon, $static_content, $extra_head_tags=array()) {
  286.                 $head_elems = $this->getCommonHeadElems($page_title_1);
  287.                 $head_elems = array_merge($head_elems, $extra_head_tags);
  288.  
  289.                 # ---
  290.  
  291.                 $out  = "<!DOCTYPE html>\n";
  292.  
  293.                 $out .= "<html lang=\"".substr(OIDplus::getCurrentLang(),0,2)."\">\n";
  294.                 $out .= "<head>\n";
  295.                 $out .= "\t".implode("\n\t",$head_elems)."\n";
  296.                 $out .= "</head>\n";
  297.  
  298.                 $out .= "<body>\n";
  299.  
  300.                 $out .= '<div id="loading" style="display:none">Loading&#8230;</div>';
  301.  
  302.                 $out .= '<div id="frames">';
  303.                 $out .= '<div id="content_window" class="borderbox">';
  304.  
  305.                 $out .= '<h1 id="real_title">';
  306.                 if ($static_icon != '') $out .= '<img src="'.htmlentities($static_icon).'" width="48" height="48" alt=""> ';
  307.                 $out .= htmlentities($page_title_2).'</h1>';
  308.                 $out .= '<div id="real_content">'.$static_content.'</div>';
  309.                 $out .= '<br>';
  310.  
  311.                 $out .= '</div>';
  312.  
  313.                 $out .= '<div id="system_title_bar">';
  314.  
  315.                 $out .= '<div id="system_title_text">';
  316.                 $out .= '       <span id="system_title_logo"></span>';
  317.                 $out .= '       <span id="system_title_1">'.htmlentities(OIDplus::getEditionInfo()['vendor'].' OIDplus 2.0').'</span><br>';
  318.                 $out .= '       <span id="system_title_2">'.htmlentities($page_title_1).'</span>';
  319.                 $out .= '</div>';
  320.  
  321.                 $out .= '</div>';
  322.  
  323.                 $out .= OIDplus::gui()->getLanguageBox(null, true);
  324.  
  325.                 $out .= '</div>';
  326.  
  327.                 $out .= "\n</body>\n";
  328.                 $out .= "</html>\n";
  329.  
  330.                 # ---
  331.  
  332.                 return $out;
  333.         }
  334.  
  335. }
  336.