Subversion Repositories oidplus

Rev

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