Subversion Repositories oidplus

Rev

Rev 1041 | Rev 1055 | 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 - 2021 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. class OIDplusGui extends OIDplusBaseClass {
  23.  
  24.         public static function generateContentPage($id) {
  25.                 $out = array();
  26.  
  27.                 $handled = false;
  28.                 $out['title'] = '';
  29.                 $out['icon'] = '';
  30.                 $out['text'] = '';
  31.  
  32.                 foreach (OIDplus::getPagePlugins() as $plugin) {
  33.                         try {
  34.                                 $plugin->gui($id, $out, $handled);
  35.                         } catch (\Exception $e) {
  36.                                 $out['title'] = _L('Error');
  37.                                 $out['icon'] = 'img/error.png';
  38.                                 $out['text'] = $e->getMessage();
  39.                         }
  40.                         if ($handled) break;
  41.                 }
  42.  
  43.                 if (!$handled) {
  44.                         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
  45.                                 http_response_code(404);
  46.                         }
  47.                         $out['title'] = _L('Error');
  48.                         $out['icon'] = 'img/error.png';
  49.                         $out['text'] = _L('The resource cannot be found.');
  50.                 }
  51.  
  52.                 return $out;
  53.         }
  54.  
  55.         public static function link($goto, $new_window=false): string {
  56.                 if ($new_window) {
  57.                         return 'href="?goto='.urlencode($goto).'" target="_blank"';
  58.                 } else {
  59.                         if (strpos($goto, '#') !== false) {
  60.                                 list($goto, $anchor) = explode('#', $goto, 2);
  61.                                 return 'href="?goto='.urlencode($goto).'#'.htmlentities($anchor).'" onclick="openOidInPanel('.js_escape($goto).', true, '.js_escape($anchor).'); return false;"';
  62.                         } else {
  63.                                 return 'href="?goto='.urlencode($goto).'" onclick="openOidInPanel('.js_escape($goto).', true); return false;"';
  64.                         }
  65.                 }
  66.         }
  67.  
  68.         public static function getLanguageBox($goto, $useJs) {
  69.                 echo '<div id="languageBox">';
  70.                 $langbox_entries = array();
  71.                 $non_default_languages = 0;
  72.                 foreach (OIDplus::getAllPluginManifests('language') as $pluginManifest) {
  73.                         $flag = $pluginManifest->getLanguageFlag();
  74.                         $code = $pluginManifest->getLanguageCode();
  75.                         if ($code != OIDplus::getDefaultLang()) $non_default_languages++;
  76.                         if ($code == OIDplus::getCurrentLang()) {
  77.                                 $class = 'lng_flag';
  78.                         } else {
  79.                                 $class = 'lng_flag picture_ghost';
  80.                         }
  81.                         $add = (!is_null($goto)) ? '&amp;goto='.urlencode($goto) : '';
  82.  
  83.                         $dirs = glob(OIDplus::localpath().'plugins/'.'*'.'/language/'.$code.'/');
  84.  
  85.                         if (count($dirs) > 0) {
  86.                                 $dir = substr($dirs[0], strlen(OIDplus::localpath()));
  87.                                 $langbox_entries[$code] = '<span class="lang_flag_bg"><a '.($useJs ? 'onclick="setLanguage(\''.$code.'\'); return false" ' : '').'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> ';
  88.                         }
  89.                 }
  90.                 if ($non_default_languages > 0) {
  91.                         foreach ($langbox_entries as $ent) {
  92.                                 echo "$ent\n\t\t";
  93.                         }
  94.                 }
  95.                 echo '</div>';
  96.         }
  97.  
  98.         public static function html_exception_handler($exception) {
  99.                 if ($exception instanceof OIDplusConfigInitializationException) {
  100.                         echo '<!DOCTYPE HTML>';
  101.                         echo '<html><head><title>'._L('OIDplus initialization error').'</title></head><body>';
  102.                         echo '<h1>'._L('OIDplus initialization error').'</h1>';
  103.                         echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
  104.                         echo '<p>'._L('Please check the file %1','<b>userdata/baseconfig/config.inc.php</b>');
  105.                         if (is_dir(__DIR__ . '/../../setup')) {
  106.                                 echo ' '._L('or run <a href="%1">setup</a> again',OIDplus::webpath(null,OIDplus::PATH_RELATIVE).'setup/');
  107.                         }
  108.                         echo '</p>';
  109.                         echo self::getExceptionTechInfo($exception);
  110.                         echo '</body></html>';
  111.                 } else {
  112.                         echo '<!DOCTYPE HTML>';
  113.                         echo '<html><head><title>'._L('OIDplus error').'</title></head><body>';
  114.                         echo '<h1>'._L('OIDplus error').'</h1>';
  115.                         // ENT_SUBSTITUTE because ODBC drivers might return ANSI instead of UTF-8 stuff
  116.                         echo '<p>'.htmlentities($exception->getMessage(), ENT_SUBSTITUTE).'</p>';
  117.                         echo self::getExceptionTechInfo($exception);
  118.                         echo '</body></html>';
  119.                 }
  120.         }
  121.  
  122.         private static function getExceptionTechInfo($exception) {
  123.                 $out = '';
  124.                 $out .= '<p><b>'._L('Technical information about the problem').':</b></p>';
  125.                 $out .= '<pre>';
  126.                 $out .= get_class($exception)."\n";
  127.                 $out .= _L('at file %1 (line %2)',$exception->getFile(),"".$exception->getLine())."\n\n";
  128.                 $out .= _L('Stacktrace').":\n";
  129.                 $out .= htmlentities($exception->getTraceAsString());
  130.                 $out .= '</pre>';
  131.                 return $out;
  132.         }
  133.  
  134.         public function tabBarStart() {
  135.                 return '<ul class="nav nav-tabs" id="myTab" role="tablist">';
  136.         }
  137.  
  138.         public function tabBarEnd() {
  139.                 return '</ul>';
  140.         }
  141.  
  142.         public function tabBarElement($id, $title, $active) {
  143.                 // data-bs-toggle is for Bootstrap 5
  144.                 // data-toggle is for Bootstrap 4 (InternetExplorer compatibility)
  145.                 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>';
  146.         }
  147.  
  148.         public function tabContentStart() {
  149.                 return '<div class="tab-content" id="myTabContent">';
  150.         }
  151.  
  152.         public function tabContentEnd() {
  153.                 return '</div>';
  154.         }
  155.  
  156.         public function tabContentPage($id, $content, $active) {
  157.                 return '<div class="tab-pane fade'.($active ? ' show active' : '').'" id="'.$id.'" role="tabpanel" aria-labelledby="'.$id.'-tab">'.$content.'</div>';
  158.         }
  159.  
  160. }
  161.