Subversion Repositories oidplus

Rev

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