Subversion Repositories oidplus

Rev

Rev 1188 | Rev 1203 | 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 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.                                 $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
  47.                                 $out['text'] = '<p>'.$htmlmsg.'</p>';
  48.                                 if (OIDplus::baseConfig()->getValue('DEBUG')) {
  49.                                         $out['text'] .= self::getExceptionTechInfo($e);
  50.                                 }
  51.                         }
  52.                         if ($handled) break;
  53.                 }
  54.  
  55.                 if (!$handled) {
  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
  57.                                 if (PHP_SAPI != 'cli') @http_response_code(404);
  58.                         }
  59.                         $out['title'] = _L('Error');
  60.                         $out['icon'] = 'img/error.png';
  61.                         $out['text'] = _L('The resource cannot be found.');
  62.                 }
  63.  
  64.                 return $out;
  65.         }
  66.  
  67.         /**
  68.          * @param string $goto
  69.          * @param bool $new_window
  70.          * @return string
  71.          */
  72.         public function link(string $goto, bool $new_window=false): string {
  73.                 if ($new_window) {
  74.                         return 'href="?goto='.urlencode($goto).'" target="_blank"';
  75.                 } else {
  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.                         }
  82.                 }
  83.         }
  84.  
  85.         /**
  86.          * @param string $goto
  87.          * @param bool $useJs
  88.          * @return string
  89.          * @throws OIDplusConfigInitializationException
  90.          * @throws OIDplusException
  91.          */
  92.         public function getLanguageBox(string $goto, bool $useJs): string {
  93.                 $out = '<div id="languageBox">';
  94.                 $langbox_entries = array();
  95.                 $non_default_languages = 0;
  96.                 foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
  97.                         $flag = $pluginManifest->getLanguageFlag();
  98.                         $code = $pluginManifest->getLanguageCode();
  99.                         if ($code != OIDplus::getDefaultLang()) $non_default_languages++;
  100.                         if ($code == OIDplus::getCurrentLang()) {
  101.                                 $class = 'lng_flag';
  102.                         } else {
  103.                                 $class = 'lng_flag picture_ghost';
  104.                         }
  105.                         $add = ($goto != '') ? '&amp;goto='.urlencode($goto) : '';
  106.  
  107.                         $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/language/'.$code.'/');
  108.  
  109.                         if (count($dirs) > 0) {
  110.                                 $dir = substr($dirs[0], strlen(OIDplus::localpath()));
  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> ';
  112.                         }
  113.                 }
  114.                 if ($non_default_languages > 0) {
  115.                         foreach ($langbox_entries as $ent) {
  116.                                 $out .= "$ent\n\t\t";
  117.                         }
  118.                 }
  119.                 $out .= '</div>';
  120.                 return $out;
  121.         }
  122.  
  123.         /**
  124.          * @param \Throwable $exception
  125.          * @return void
  126.          * @throws OIDplusException
  127.          */
  128.         public static function html_exception_handler(\Throwable $exception) {
  129.                 // Note: This method must be static
  130.  
  131.                 // OXOXO: Implement HTMLEXCEPTIONS
  132.  
  133.                 if ($exception instanceof OIDplusConfigInitializationException) {
  134.                         echo '<!DOCTYPE HTML>';
  135.                         echo '<html><head><title>'.htmlentities(_L('OIDplus initialization error')).'</title></head><body>';
  136.                         echo '<h1>'.htmlentities(_L('OIDplus initialization error')).'</h1>';
  137.                         echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
  138.                         $msg = _L('Please check the file %1','<b>userdata/baseconfig/config.inc.php</b>');
  139.                         if (is_dir(__DIR__ . '/../../setup')) {
  140.                                 $msg .= ' '._L('or run <a href="%1">setup</a> again',OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/');
  141.                         }
  142.                         echo '<p>'.$msg.'</p>'; // No htmlentities, because we already did it above
  143.                         echo self::getExceptionTechInfo($exception);
  144.                         echo '</body></html>';
  145.                 } else {
  146.                         echo '<!DOCTYPE HTML>';
  147.                         echo '<html><head><title>'.htmlentities(_L('OIDplus error')).'</title></head><body>';
  148.                         echo '<h1>'.htmlentities(_L('OIDplus error')).'</h1>';
  149.                         // ENT_SUBSTITUTE because ODBC drivers might return ANSI instead of UTF-8 stuff
  150.                         echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
  151.                         echo self::getExceptionTechInfo($exception);
  152.                         echo '</body></html>';
  153.                 }
  154.         }
  155.  
  156.         /**
  157.          * @param \Throwable $exception
  158.          * @return string
  159.          */
  160.         private static function getExceptionTechInfo(\Throwable $exception): string {
  161.                 $out  = '<p><b>'.htmlentities(_L('Technical information about the problem')).':</b></p>';
  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";
  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);
  174.                 $out .= '</pre>';
  175.                 return $out;
  176.         }
  177.  
  178.         /**
  179.          * @return string
  180.          */
  181.         public function tabBarStart(): string {
  182.                 return '<ul class="nav nav-tabs" id="myTab" role="tablist">';
  183.         }
  184.  
  185.         /**
  186.          * @return string
  187.          */
  188.         public function tabBarEnd(): string {
  189.                 return '</ul>';
  190.         }
  191.  
  192.         /**
  193.          * @param string $id
  194.          * @param string $title
  195.          * @param bool $active
  196.          * @return string
  197.          */
  198.         public function tabBarElement(string $id, string $title, bool $active): string {
  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>';
  202.         }
  203.  
  204.         /**
  205.          * @return string
  206.          */
  207.         public function tabContentStart(): string {
  208.                 return '<div class="tab-content" id="myTabContent">';
  209.         }
  210.  
  211.         /**
  212.          * @return string
  213.          */
  214.         public function tabContentEnd(): string {
  215.                 return '</div>';
  216.         }
  217.  
  218.         /**
  219.          * @param string $id
  220.          * @param string $content
  221.          * @param bool $active
  222.          * @return string
  223.          */
  224.         public function tabContentPage(string $id, string $content, bool $active): string {
  225.                 return '<div class="tab-pane fade'.($active ? ' show active' : '').'" id="'.$id.'" role="tabpanel" aria-labelledby="'.$id.'-tab">'.$content.'</div>';
  226.         }
  227.  
  228.         /**
  229.          * @param string $systemtitle
  230.          * @param string $pagetitle
  231.          * @return string
  232.          */
  233.         public function combine_systemtitle_and_pagetitle(string $systemtitle, string $pagetitle): string {
  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.  
  242.         /**
  243.          * @param string $title
  244.          * @return string[]
  245.          * @throws OIDplusException
  246.          */
  247.         private function getCommonHeadElems(string $title): array {
  248.                 // Get theme color (color of title bar)
  249.                 $design_plugin = OIDplus::getActiveDesignPlugin();
  250.                 $theme_color = is_null($design_plugin) ? '' : $design_plugin->getThemeColor();
  251.  
  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">';
  261.                 $head_elems[] = '<title>'.htmlentities($title).'</title>';
  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')) {
  267.                         $head_elems[] = '<link rel="canonical" href="'.htmlentities(OIDplus::canonicalURL()).OIDplus::webpath(null, OIDplus::PATH_RELATIVE).'">';
  268.                 }
  269.  
  270.                 return $head_elems;
  271.         }
  272.  
  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.          */
  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 {
  285.                 $head_elems = $this->getCommonHeadElems($page_title_1);
  286.                 $head_elems = array_merge($head_elems, $extra_head_tags);
  287.  
  288.                 $plugins = OIDplus::getAllPlugins();
  289.                 foreach ($plugins as $plugin) {
  290.                         $plugin->htmlHeaderUpdate($head_elems);
  291.                 }
  292.  
  293.                 # ---
  294.  
  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.  
  304.                 $out .= '<div id="loading" style="display:none">Loading&#8230;</div>';
  305.  
  306.                 $out .= '<div id="frames">';
  307.                 $out .= '<div id="content_window" class="borderbox">';
  308.  
  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>';
  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.                 }
  317.                 $out .= '<br>';
  318.  
  319.                 $out .= '</div>';
  320.  
  321.                 $out .= '<div id="system_title_bar">';
  322.  
  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.  
  329.                 $out .= '<div id="system_title_text">';
  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.  
  360.                 $plugins = OIDplus::getAllPlugins();
  361.                 foreach ($plugins as $plugin) {
  362.                         $plugin->htmlPostprocess($out);
  363.                 }
  364.  
  365.                 return $out;
  366.         }
  367.  
  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.          */
  378.         public function showSimplePage(string $page_title_1, string $page_title_2, string $static_icon, string $static_content, array $extra_head_tags=array()): string {
  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">';
  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>';
  413.  
  414.                 $out .= '</div>';
  415.  
  416.                 $out .= OIDplus::gui()->getLanguageBox('', true);
  417.  
  418.                 $out .= '</div>';
  419.  
  420.                 $out .= "\n</body>\n";
  421.                 $out .= "</html>\n";
  422.  
  423.                 # ---
  424.  
  425.                 return $out;
  426.         }
  427.  
  428. }
  429.