Subversion Repositories oidplus

Rev

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