Subversion Repositories oidplus

Rev

Rev 1398 | 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 OIDplusPageAdminSysteminfo extends OIDplusPagePluginAdmin {
  27.  
  28.         /**
  29.          * @param string $actionID
  30.          * @param array $params
  31.          * @return array
  32.          * @throws OIDplusException
  33.          */
  34.         public function action(string $actionID, array $params): array {
  35.                 return parent::action($actionID, $params);
  36.         }
  37.  
  38.         /**
  39.          * @param bool $html
  40.          * @return void
  41.          */
  42.         public function init(bool $html=true) {
  43.         }
  44.  
  45.         /**
  46.          * @return array|mixed|string|string[]
  47.          */
  48.         private function getLoadedInis() {
  49.                 $s_inis = '';
  50.  
  51.                 $inis = array();
  52.                 $main_ini = php_ini_loaded_file();
  53.                 if ($main_ini !== false) $s_inis = '<b>'.htmlentities($main_ini).'</b>';
  54.  
  55.                 $more_ini = php_ini_scanned_files();
  56.                 if ($more_ini !== false) {
  57.                         $inis = explode(',', $more_ini);
  58.                         foreach ($inis as $ini) {
  59.                                 $s_inis .= '<br>'.htmlentities($ini);
  60.                         }
  61.                 }
  62.  
  63.                 if ($s_inis == '') $s_inis = _L('n/a');
  64.  
  65.                 return $s_inis;
  66.         }
  67.  
  68.         /**
  69.          * @param string $id
  70.          * @param array $out
  71.          * @param bool $handled
  72.          * @return void
  73.          * @throws OIDplusConfigInitializationException
  74.          * @throws OIDplusException
  75.          */
  76.         public function gui(string $id, array &$out, bool &$handled) {
  77.                 if ($id === 'oidplus:phpinfo') {
  78.                         $handled = true;
  79.                         $out['title'] = _L('PHP information');
  80.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/php_icon.png';
  81.  
  82.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  83.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
  84.                         }
  85.  
  86.                         $out['text'] = '<p><a '.OIDplus::gui()->link('oidplus:systeminfo').'><img src="img/arrow_back.png" width="16" alt="'._L('Go back').'"> '._L('Go back to the system information page').'</a></p>';
  87.  
  88.                         ob_start();
  89.                         $res = phpinfo();
  90.                         $cont = ob_get_contents();
  91.                         ob_end_clean();
  92.  
  93.                         if (!$res) {
  94.                                 $out['text'] .= '<p><font color="red">'._L('phpinfo() could not be called').'</font></p>';
  95.                         } else {
  96.                                 // phpinfo() sets "img {float: right; border: 0;}". We don't want that.
  97.                                 $cont = str_replace('img {', 'img.phpinfo {', $cont);
  98.                                 $cont = str_replace('<img', '<img class="phpinfo"', $cont);
  99.  
  100.                                 // phpinfo() sets the link colors. We don't want that
  101.                                 $cont = preg_replace('@a:.+ {.+}@ismU', '', $cont);
  102.  
  103.                                 // phpinfo() sets "h1 {font-size: 150%;}" and "h2 {font-size: 125%;}"
  104.                                 $cont = preg_replace('@(h1|h2|h3|h4|h5) {.+}@ismU', '', $cont);
  105.  
  106.                                 // Make compatible for dark themes by removing all foreground and background colors
  107.                                 $cont = preg_replace('@(body) {.+}@ismU', '', $cont, 1);
  108.                                 $cont = preg_replace('@background-color:(.+)[\\};]@ismU', '', $cont);
  109.                                 $cont = '<span style="font-family: sans-serif;">'.$cont.'</span>';
  110.  
  111.                                 // Prevent that dark-color scheme makes the font white in a non-dark-color OIDplus
  112.                                 $cont = str_replace('prefers-color-scheme', 'xxx', $cont);
  113.  
  114.                                 // Font sizes
  115.                                 $cont = preg_replace('@font-size:\\s*75%;@', '', $cont);
  116.                                 for ($i=5; $i>=1; $i--) {
  117.                                         $cont = str_replace('<h'.$i, '<h'.($i+1), $cont);
  118.                                         $cont = str_replace('</h'.$i, '</h'.($i+1), $cont);
  119.                                 }
  120.  
  121.                                 $out['text'] .= $cont;
  122.                         }
  123.                 }
  124.                 else if ($id === 'oidplus:systeminfo') {
  125.                         $handled = true;
  126.                         $out['title'] = _L('System information');
  127.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  128.  
  129.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  130.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
  131.                         }
  132.  
  133.                         $out['text']  = '';
  134.  
  135.                         # ---
  136.  
  137.                         $out['text'] .= '<h2>'._L('OIDplus 2.0').'</h2>';
  138.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  139.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  140.                         $out['text'] .= '<thead>';
  141.                         $out['text'] .= '       <tr>';
  142.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  143.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  144.                         $out['text'] .= '       </tr>';
  145.                         $out['text'] .= '</thead>';
  146.                         $out['text'] .= '<tbody>';
  147.  
  148.                         $sys_title = OIDplus::config()->getValue('system_title');
  149.                         $out['text'] .= '       <tr>';
  150.                         $out['text'] .= '               <td>'._L('System title').'</td>';
  151.                         $out['text'] .= '               <td>'.htmlentities($sys_title).'</td>';
  152.                         $out['text'] .= '       </tr>';
  153.  
  154.                         $out['text'] .= '       <tr>';
  155.                         $out['text'] .= '               <td>'._L('System directory').'</td>';
  156.                         $out['text'] .= '               <td>'.(isset($_SERVER['SCRIPT_FILENAME']) ? htmlentities(dirname($_SERVER['SCRIPT_FILENAME'])) : '<i>'._L('unknown').'</i>').'</td>';
  157.                         $out['text'] .= '       </tr>';
  158.  
  159.                         $sys_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL);
  160.                         $out['text'] .= '       <tr>';
  161.                         $out['text'] .= '               <td>'._L('System URL').'</td>';
  162.                         $out['text'] .= '               <td>'.htmlentities($sys_url).'</td>';
  163.                         $out['text'] .= '       </tr>';
  164.  
  165.                         $sysid_oid = OIDplus::getSystemId(true);
  166.                         $out['text'] .= '       <tr>';
  167.                         $out['text'] .= '               <td>'._L('System OID').' <abbr title="'._L('OID based on the public key of your OIDplus system. The last arc is also called OIDplus System ID.').'">(?)</abbr></td>';
  168.                         $out['text'] .= '               <td>'.(!$sysid_oid ? '<i>'._L('unknown').'</i>' : htmlentities($sysid_oid)).'</td>';
  169.                         $out['text'] .= '       </tr>';
  170.  
  171.                         $sysid_guid = OIDplus::getSystemGuid();
  172.                         $out['text'] .= '       <tr>';
  173.                         $out['text'] .= '               <td>'._L('System GUID').' <abbr title="'._L('UUIDv8 based on the System ID of your OIDplus system.').'">(?)</abbr></td>';
  174.                         $out['text'] .= '               <td>'.(!$sysid_guid ? '<i>'._L('unknown').'</i>' : htmlentities($sysid_guid)).'</td>';
  175.                         $out['text'] .= '       </tr>';
  176.  
  177.                         $sysid = OIDplus::getSystemId(false);
  178.                         $sysid_aid = $sysid ? 'D276000186B20005'.strtoupper(str_pad(dechex((int)$sysid),8,'0',STR_PAD_LEFT)) : '';
  179.                         $out['text'] .= '       <tr>';
  180.                         $out['text'] .= '               <td>'._L('System AID').' <abbr title="'._L('Application Identifier (ISO/IEC 7816) based on the system ID (which is based on the hash of the public key of your OIDplus system).').'">(?)</abbr></td>';
  181.                         $out['text'] .= '               <td>'.(!$sysid_aid ? '<i>'._L('unknown').'</i>' : htmlentities($sysid_aid)).' ('._L('No PIX allowed').')</td>';
  182.                         $out['text'] .= '       </tr>';
  183.  
  184.                         $out['text'] .= '       <tr>';
  185.                         $out['text'] .= '               <td>'._L('System X.500 Distinguished Name').'</td>';
  186.                         $out['text'] .= '               <td>'.(!$sysid ? '<i>'._L('unknown').'</i>' : '1.3.6.1.4.1.37476.2.5.2.9.4.1='.$sysid.'<span class="gray_font">, CN=OidPlus, DC=example, DC=com</span>').'</td>';
  187.                         $out['text'] .= '       </tr>';
  188.  
  189.                         $sys_ver = OIDplus::getVersion();
  190.                         $out['text'] .= '       <tr>';
  191.                         $out['text'] .= '               <td>'._L('System version').'</td>';
  192.                         $out['text'] .= '               <td>'.(!$sys_ver ? '<i>'._L('unknown').'</i>' : htmlentities($sys_ver)).'</td>';
  193.                         $out['text'] .= '       </tr>';
  194.  
  195.                         $sys_install_type = OIDplus::getInstallType();
  196.                         $out['text'] .= '       <tr>';
  197.                         $out['text'] .= '               <td>'._L('Installation type').'</td>';
  198.                         $out['text'] .= '               <td>'.htmlentities($sys_install_type).'</td>';
  199.                         $out['text'] .= '       </tr>';
  200.  
  201.                         $out['text'] .= '</tbody>';
  202.                         $out['text'] .= '</table>';
  203.                         $out['text'] .= '</div></div>';
  204.  
  205.                         # ---
  206.  
  207.                         $out['text'] .= '<h2>'._L('Webserver system').'</h2>';
  208.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  209.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  210.                         $out['text'] .= '<thead>';
  211.                         $out['text'] .= '       <tr>';
  212.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  213.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  214.                         $out['text'] .= '       </tr>';
  215.                         $out['text'] .= '</thead>';
  216.                         $out['text'] .= '<tbody>';
  217.  
  218.                         // Operating system (of the webserver)
  219.  
  220.                         if (php_uname("m") == php_uname("n")) {
  221.                                 // At some hosts like Strato, php_uname() always returns the same string
  222.                                 // "Linux localhost 3.10.0-1127.10.1.el7.x86_64 #1 SMP"
  223.                                 $out['text'] .= '       <tr>';
  224.                                 $out['text'] .= '               <td>'._L('Operating System').'</td>';
  225.                                 $out['text'] .= '               <td>'.htmlentities(PHP_OS).'</td>';
  226.                                 $out['text'] .= '       </tr>';
  227.                                 $out['text'] .= '       <tr>';
  228.                                 $out['text'] .= '               <td>'._L('Hostname').'</td>';
  229.                                 $out['text'] .= '               <td>'.htmlentities(gethostname()).'</td>';
  230.                                 $out['text'] .= '       </tr>';
  231.                         } else {
  232.                                 $out['text'] .= '       <tr>';
  233.                                 $out['text'] .= '               <td>'._L('Operating System').'</td>';
  234.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("s").' '.php_uname("r").' '.php_uname("v")).'</td>';
  235.                                 $out['text'] .= '       </tr>';
  236.                                 $out['text'] .= '       <tr>';
  237.                                 $out['text'] .= '               <td>'._L('Machine type').'</td>';
  238.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("m")).'</td>';
  239.                                 $out['text'] .= '       </tr>';
  240.                                 $out['text'] .= '       <tr>';
  241.                                 $out['text'] .= '               <td>'._L('Hostname').'</td>';
  242.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("n")).'</td>';
  243.                                 $out['text'] .= '       </tr>';
  244.                         }
  245.                         $out['text'] .= '       <tr>';
  246.                         $out['text'] .= '               <td>'._L('Server time').'</td>';
  247.                         $out['text'] .= '               <td>'.htmlentities(date('Y-m-d H:i:s P')).'</td>';
  248.                         $out['text'] .= '       </tr>';
  249.  
  250.                         // The actual web server stuff
  251.  
  252.                         $out['text'] .= '       <tr>';
  253.                         $out['text'] .= '               <td>'._L('Web server software').'</td>';
  254.                         $out['text'] .= '               <td>'.($_SERVER['SERVER_SOFTWARE'] ?? '<i>' . _L('unknown') . '</i>').'</td>';
  255.                         $out['text'] .= '       </tr>';
  256.                         $out['text'] .= '       <tr>';
  257.                         $out['text'] .= '               <td>'._L('Web server user account').'</td>';
  258.                         $current_user = get_own_username();  // TODO: should we also show the group?
  259.                         $out['text'] .= '               <td>'.($current_user === false ? '<i>'._L('unknown').'</i>' : htmlentities($current_user)).'</td>';
  260.                         $out['text'] .= '       </tr>';
  261.  
  262.                         // PHP (at webserver)
  263.  
  264.                         $out['text'] .= '       <tr>';
  265.                         $out['text'] .= '               <td>'._L('PHP version').'</td>';
  266.                         $out['text'] .= '               <td>'.PHP_VERSION.'</td>';
  267.                         $out['text'] .= '       </tr>';
  268.                         $out['text'] .= '       <tr>';
  269.                         $out['text'] .= '               <td>'._L('PHP configuration file(s)').'</td>';
  270.                         $out['text'] .= '               <td>'.$this->getLoadedInis().'</td>';
  271.                         $out['text'] .= '       </tr>';
  272.                         $out['text'] .= '       <tr>';
  273.                         $out['text'] .= '               <td>'._L('PHP installed extensions').'</td>';
  274.                         $out['text'] .= '               <td>'.htmlentities(implode(', ',get_loaded_extensions())).'</td>';
  275.                         $out['text'] .= '       </tr>';
  276.                         $out['text'] .= '</tbody>';
  277.                         $out['text'] .= '</table>';
  278.  
  279.                         $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:phpinfo').'>'._L('Show PHP server configuration (phpinfo)').'</a></p>';
  280.  
  281.                         $out['text'] .= '</div></div>';
  282.  
  283.                         # ---
  284.  
  285.                         $out['text'] .= '<h2>'._L('Database system').'</h2>';
  286.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  287.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  288.                         $out['text'] .= '<thead>';
  289.                         $out['text'] .= '       <tr>';
  290.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  291.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  292.                         $out['text'] .= '       </tr>';
  293.                         $out['text'] .= '</thead>';
  294.                         $out['text'] .= '<tbody>';
  295.  
  296.                         $out['text'] .= '       <tr>';
  297.                         $out['text'] .= '               <td>'._L('Database provider').'</td>';
  298.                         $out['text'] .= '               <td>'.OIDplus::db()->getPlugin()->getManifest()->getName().'</td>';
  299.                         $out['text'] .= '       </tr>';
  300.  
  301.                         $out['text'] .= '       <tr>';
  302.                         $out['text'] .= '               <td>'._L('SQL slang').'</td>';
  303.                         $out['text'] .= '               <td>'.OIDplus::db()->getSlang()->getManifest()->getName().'</td>';
  304.                         $out['text'] .= '       </tr>';
  305.  
  306.                         $table_prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX');
  307.                         $out['text'] .= '       <tr>';
  308.                         $out['text'] .= '               <td>'._L('Table name prefix').'</td>';
  309.                         $out['text'] .= '               <td>'.(!empty($table_prefix) ? htmlentities($table_prefix) : '<i>'._L('none').'</i>').'</td>';
  310.                         $out['text'] .= '       </tr>';
  311.                         $out['text'] .= '       <tr>';
  312.                         $out['text'] .= '               <td>'._L('Server time').'</td>';
  313.                         // We use "from ###config" because Oracle DB requires a "from" statement.
  314.                         // Instead of creating two queries (one with "select ..." and one with "select ... from dual"),
  315.                         // we make this query. It is OK, because the table ###config is never empty and we are only fetching the first row.
  316.                         $tmp = OIDplus::db()->query('select '.OIDplus::db()->sqlDate().' as tmp from ###config');
  317.                         if ($tmp) $tmp = $tmp->fetch_array();
  318.                         $tmp = $tmp['tmp'] ?? _L('n/a');
  319.                         $tmp = preg_replace('@\\.\\d{3}$@', '', $tmp); // remove milliseconds of Microsoft SQL Server
  320.                         $out['text'] .= '               <td>'.$tmp.'</td>';
  321.                         $out['text'] .= '       </tr>';
  322.  
  323.                         $infos = OIDplus::db()->getExtendedInfo();
  324.                         foreach ($infos as $name => $val) {
  325.                                 $out['text'] .= '       <tr>';
  326.                                 $out['text'] .= '               <td>' . htmlentities($name) . '</td>';
  327.                                 $out['text'] .= '               <td>' . htmlentities($val ?? '') . '</td>';
  328.                                 $out['text'] .= '       </tr>';
  329.                         }
  330.  
  331.                         $out['text'] .= '</tbody>';
  332.                         $out['text'] .= '</table>';
  333.                         $out['text'] .= '</div></div>';
  334.  
  335.                         # ---
  336.  
  337.                 }
  338.         }
  339.  
  340.         /**
  341.          * @param array $json
  342.          * @param string|null $ra_email
  343.          * @param bool $nonjs
  344.          * @param string $req_goto
  345.          * @return bool
  346.          * @throws OIDplusException
  347.          */
  348.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  349.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  350.  
  351.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  352.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  353.                 } else {
  354.                         $tree_icon = null; // default icon (folder)
  355.                 }
  356.  
  357.                 if (file_exists(__DIR__.'/img/php_icon16.png')) {
  358.                         $tree_icon_php = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/php_icon16.png';
  359.                 } else {
  360.                         $tree_icon_php = null; // default icon (folder)
  361.                 }
  362.  
  363.                 $json[] = array(
  364.                         'id' => 'oidplus:systeminfo',
  365.                         'icon' => $tree_icon,
  366.                         'text' => _L('System information'),
  367.                         'children' => array(array(
  368.                                 'id' => 'oidplus:phpinfo',
  369.                                 'icon' => $tree_icon_php,
  370.                                 'text' => _L('PHP information')
  371.                         ))
  372.                 );
  373.  
  374.                 return true;
  375.         }
  376.  
  377.         /**
  378.          * @param string $request
  379.          * @return array|false
  380.          */
  381.         public function tree_search(string $request) {
  382.                 return false;
  383.         }
  384.  
  385. }
  386.