Subversion Repositories oidplus

Rev

Rev 1228 | Rev 1336 | 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 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('SHA1-Namebased UUID based on the public key 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.                         $sys_ver = OIDplus::getVersion();
  185.                         $out['text'] .= '       <tr>';
  186.                         $out['text'] .= '               <td>'._L('System version').'</td>';
  187.                         $out['text'] .= '               <td>'.(!$sys_ver ? '<i>'._L('unknown').'</i>' : htmlentities($sys_ver)).'</td>';
  188.                         $out['text'] .= '       </tr>';
  189.  
  190.                         $sys_install_type = OIDplus::getInstallType();
  191.                         $out['text'] .= '       <tr>';
  192.                         $out['text'] .= '               <td>'._L('Installation type').'</td>';
  193.                         $out['text'] .= '               <td>'.htmlentities($sys_install_type).'</td>';
  194.                         $out['text'] .= '       </tr>';
  195.  
  196.                         $out['text'] .= '</tbody>';
  197.                         $out['text'] .= '</table>';
  198.                         $out['text'] .= '</div></div>';
  199.  
  200.                         # ---
  201.  
  202.                         $out['text'] .= '<h2>'._L('Webserver system').'</h2>';
  203.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  204.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  205.                         $out['text'] .= '<thead>';
  206.                         $out['text'] .= '       <tr>';
  207.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  208.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  209.                         $out['text'] .= '       </tr>';
  210.                         $out['text'] .= '</thead>';
  211.                         $out['text'] .= '<tbody>';
  212.  
  213.                         // Operating system (of the webserver)
  214.  
  215.                         if (php_uname("m") == php_uname("n")) {
  216.                                 // At some hosts like Strato, php_uname() always returns the same string
  217.                                 // "Linux localhost 3.10.0-1127.10.1.el7.x86_64 #1 SMP"
  218.                                 $out['text'] .= '       <tr>';
  219.                                 $out['text'] .= '               <td>'._L('Operating System').'</td>';
  220.                                 $out['text'] .= '               <td>'.htmlentities(PHP_OS).'</td>';
  221.                                 $out['text'] .= '       </tr>';
  222.                                 $out['text'] .= '       <tr>';
  223.                                 $out['text'] .= '               <td>'._L('Hostname').'</td>';
  224.                                 $out['text'] .= '               <td>'.htmlentities(gethostname()).'</td>';
  225.                                 $out['text'] .= '       </tr>';
  226.                         } else {
  227.                                 $out['text'] .= '       <tr>';
  228.                                 $out['text'] .= '               <td>'._L('Operating System').'</td>';
  229.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("s").' '.php_uname("r").' '.php_uname("v")).'</td>';
  230.                                 $out['text'] .= '       </tr>';
  231.                                 $out['text'] .= '       <tr>';
  232.                                 $out['text'] .= '               <td>'._L('Machine type').'</td>';
  233.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("m")).'</td>';
  234.                                 $out['text'] .= '       </tr>';
  235.                                 $out['text'] .= '       <tr>';
  236.                                 $out['text'] .= '               <td>'._L('Hostname').'</td>';
  237.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("n")).'</td>';
  238.                                 $out['text'] .= '       </tr>';
  239.                         }
  240.                         $out['text'] .= '       <tr>';
  241.                         $out['text'] .= '               <td>'._L('Server time').'</td>';
  242.                         $out['text'] .= '               <td>'.htmlentities(date('Y-m-d H:i:s P')).'</td>';
  243.                         $out['text'] .= '       </tr>';
  244.  
  245.                         // The actual web server stuff
  246.  
  247.                         $out['text'] .= '       <tr>';
  248.                         $out['text'] .= '               <td>'._L('Web server software').'</td>';
  249.                         $out['text'] .= '               <td>'.($_SERVER['SERVER_SOFTWARE'] ?? '<i>' . _L('unknown') . '</i>').'</td>';
  250.                         $out['text'] .= '       </tr>';
  251.                         $out['text'] .= '       <tr>';
  252.                         $out['text'] .= '               <td>'._L('Web server user account').'</td>';
  253.                         $current_user = get_own_username();  // TODO: should we also show the group?
  254.                         $out['text'] .= '               <td>'.($current_user === false ? '<i>'._L('unknown').'</i>' : htmlentities($current_user)).'</td>';
  255.                         $out['text'] .= '       </tr>';
  256.  
  257.                         // PHP (at webserver)
  258.  
  259.                         $out['text'] .= '       <tr>';
  260.                         $out['text'] .= '               <td>'._L('PHP version').'</td>';
  261.                         $out['text'] .= '               <td>'.PHP_VERSION.'</td>';
  262.                         $out['text'] .= '       </tr>';
  263.                         $out['text'] .= '       <tr>';
  264.                         $out['text'] .= '               <td>'._L('PHP configuration file(s)').'</td>';
  265.                         $out['text'] .= '               <td>'.$this->getLoadedInis().'</td>';
  266.                         $out['text'] .= '       </tr>';
  267.                         $out['text'] .= '       <tr>';
  268.                         $out['text'] .= '               <td>'._L('PHP installed extensions').'</td>';
  269.                         $out['text'] .= '               <td>'.htmlentities(implode(', ',get_loaded_extensions())).'</td>';
  270.                         $out['text'] .= '       </tr>';
  271.                         $out['text'] .= '</tbody>';
  272.                         $out['text'] .= '</table>';
  273.  
  274.                         $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:phpinfo').'>'._L('Show PHP server configuration (phpinfo)').'</a></p>';
  275.  
  276.                         $out['text'] .= '</div></div>';
  277.  
  278.                         # ---
  279.  
  280.                         $out['text'] .= '<h2>'._L('Database system').'</h2>';
  281.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  282.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  283.                         $out['text'] .= '<thead>';
  284.                         $out['text'] .= '       <tr>';
  285.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  286.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  287.                         $out['text'] .= '       </tr>';
  288.                         $out['text'] .= '</thead>';
  289.                         $out['text'] .= '<tbody>';
  290.  
  291.                         $out['text'] .= '       <tr>';
  292.                         $out['text'] .= '               <td>'._L('Database provider').'</td>';
  293.                         $out['text'] .= '               <td>'.OIDplus::db()->getPlugin()->getManifest()->getName().'</td>';
  294.                         $out['text'] .= '       </tr>';
  295.  
  296.                         $out['text'] .= '       <tr>';
  297.                         $out['text'] .= '               <td>'._L('SQL slang').'</td>';
  298.                         $out['text'] .= '               <td>'.OIDplus::db()->getSlang()->getManifest()->getName().'</td>';
  299.                         $out['text'] .= '       </tr>';
  300.  
  301.                         $table_prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX');
  302.                         $out['text'] .= '       <tr>';
  303.                         $out['text'] .= '               <td>'._L('Table name prefix').'</td>';
  304.                         $out['text'] .= '               <td>'.(!empty($table_prefix) ? htmlentities($table_prefix) : '<i>'._L('none').'</i>').'</td>';
  305.                         $out['text'] .= '       </tr>';
  306.                         $out['text'] .= '       <tr>';
  307.                         $out['text'] .= '               <td>'._L('Server time').'</td>';
  308.                         // We use "from ###config" because Oracle DB requires a "from" statement.
  309.                         // Instead of creating two queries (one with "select ..." and one with "select ... from dual"),
  310.                         // we make this query. It is OK, because the table ###config is never empty and we are only fetching the first row.
  311.                         $tmp = OIDplus::db()->query('select '.OIDplus::db()->sqlDate().' as tmp from ###config');
  312.                         if ($tmp) $tmp = $tmp->fetch_array();
  313.                         $tmp = $tmp['tmp'] ?? _L('n/a');
  314.                         $tmp = preg_replace('@\\.\\d{3}$@', '', $tmp); // remove milliseconds of Microsoft SQL Server
  315.                         $out['text'] .= '               <td>'.$tmp.'</td>';
  316.                         $out['text'] .= '       </tr>';
  317.  
  318.                         $infos = OIDplus::db()->getExtendedInfo();
  319.                         foreach ($infos as $name => $val) {
  320.                                 $out['text'] .= '       <tr>';
  321.                                 $out['text'] .= '               <td>' . htmlentities($name) . '</td>';
  322.                                 $out['text'] .= '               <td>' . htmlentities($val ?? '') . '</td>';
  323.                                 $out['text'] .= '       </tr>';
  324.                         }
  325.  
  326.                         $out['text'] .= '</tbody>';
  327.                         $out['text'] .= '</table>';
  328.                         $out['text'] .= '</div></div>';
  329.  
  330.                         # ---
  331.  
  332.                 }
  333.         }
  334.  
  335.         /**
  336.          * @param array $json
  337.          * @param string|null $ra_email
  338.          * @param bool $nonjs
  339.          * @param string $req_goto
  340.          * @return bool
  341.          * @throws OIDplusException
  342.          */
  343.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  344.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  345.  
  346.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  347.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  348.                 } else {
  349.                         $tree_icon = null; // default icon (folder)
  350.                 }
  351.  
  352.                 if (file_exists(__DIR__.'/img/php_icon16.png')) {
  353.                         $tree_icon_php = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/php_icon16.png';
  354.                 } else {
  355.                         $tree_icon_php = null; // default icon (folder)
  356.                 }
  357.  
  358.                 $json[] = array(
  359.                         'id' => 'oidplus:systeminfo',
  360.                         'icon' => $tree_icon,
  361.                         'text' => _L('System information'),
  362.                         'children' => array(array(
  363.                                 'id' => 'oidplus:phpinfo',
  364.                                 'icon' => $tree_icon_php,
  365.                                 'text' => _L('PHP information')
  366.                         ))
  367.                 );
  368.  
  369.                 return true;
  370.         }
  371.  
  372.         /**
  373.          * @param string $request
  374.          * @return array|false
  375.          */
  376.         public function tree_search(string $request) {
  377.                 return false;
  378.         }
  379.  
  380. }
  381.