Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1131 | 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.                                 $out['icon'] = 'img/error.png';
  84.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  85.                                 return;
  86.                         }
  87.  
  88.                         $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>';
  89.  
  90.                         ob_start();
  91.                         $res = phpinfo();
  92.                         $cont = ob_get_contents();
  93.                         ob_end_clean();
  94.  
  95.                         if (!$res) {
  96.                                 $out['text'] .= '<p><font color="red">'._L('phpinfo() could not be called').'</font></p>';
  97.                         } else {
  98.                                 // phpinfo() sets "img {float: right; border: 0;}". We don't want that.
  99.                                 $cont = str_replace('img {', 'img.phpinfo {', $cont);
  100.                                 $cont = str_replace('<img', '<img class="phpinfo"', $cont);
  101.  
  102.                                 // phpinfo() sets the link colors. We don't want that
  103.                                 $cont = preg_replace('@a:.+ {.+}@ismU', '', $cont);
  104.  
  105.                                 // phpinfo() sets "h1 {font-size: 150%;}" and "h2 {font-size: 125%;}"
  106.                                 $cont = preg_replace('@(h1|h2|h3|h4|h5) {.+}@ismU', '', $cont);
  107.  
  108.                                 // Make compatible for dark themes by removing all foreground and background colors
  109.                                 $cont = preg_replace('@(body) {.+}@ismU', '', $cont, 1);
  110.                                 $cont = preg_replace('@background-color:(.+)[\\};]@ismU', '', $cont);
  111.                                 $cont = '<span style="font-family: sans-serif;">'.$cont.'</span>';
  112.  
  113.                                 // Font sizes
  114.                                 $cont = preg_replace('@font-size:\\s*75%;@', '', $cont);
  115.                                 for ($i=5; $i>=1; $i--) {
  116.                                         $cont = str_replace('<h'.$i, '<h'.($i+1), $cont);
  117.                                         $cont = str_replace('</h'.$i, '</h'.($i+1), $cont);
  118.                                 }
  119.  
  120.                                 $out['text'] .= $cont;
  121.                         }
  122.                 }
  123.                 else if ($id === 'oidplus:systeminfo') {
  124.                         $handled = true;
  125.                         $out['title'] = _L('System information');
  126.                         $out['icon']  = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png';
  127.  
  128.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  129.                                 $out['icon'] = 'img/error.png';
  130.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  131.                                 return;
  132.                         }
  133.  
  134.                         $out['text']  = '';
  135.  
  136.                         # ---
  137.  
  138.                         $out['text'] .= '<h2>'._L('OIDplus').'</h2>';
  139.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  140.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  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.  
  146.                         $sys_title = OIDplus::config()->getValue('system_title');
  147.                         $out['text'] .= '       <tr>';
  148.                         $out['text'] .= '               <td>'._L('System title').'</td>';
  149.                         $out['text'] .= '               <td>'.htmlentities($sys_title).'</td>';
  150.                         $out['text'] .= '       </tr>';
  151.  
  152.                         $out['text'] .= '       <tr>';
  153.                         $out['text'] .= '               <td>'._L('System directory').'</td>';
  154.                         $out['text'] .= '               <td>'.(isset($_SERVER['SCRIPT_FILENAME']) ? htmlentities(dirname($_SERVER['SCRIPT_FILENAME'])) : '<i>'._L('unknown').'</i>').'</td>';
  155.                         $out['text'] .= '       </tr>';
  156.  
  157.                         $sysid_oid = OIDplus::getSystemId(true);
  158.                         $out['text'] .= '       <tr>';
  159.                         $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>';
  160.                         $out['text'] .= '               <td>'.(!$sysid_oid ? '<i>'._L('unknown').'</i>' : htmlentities($sysid_oid)).'</td>';
  161.                         $out['text'] .= '       </tr>';
  162.  
  163.                         $sysid_guid = OIDplus::getSystemGuid();
  164.                         $out['text'] .= '       <tr>';
  165.                         $out['text'] .= '               <td>'._L('System GUID').' <abbr title="'._L('SHA1-Namebased UUID based on the public key of your OIDplus system.').'">(?)</abbr></td>';
  166.                         $out['text'] .= '               <td>'.(!$sysid_guid ? '<i>'._L('unknown').'</i>' : htmlentities($sysid_guid)).'</td>';
  167.                         $out['text'] .= '       </tr>';
  168.  
  169.                         $sysid = OIDplus::getSystemId(false);
  170.                         $sysid_aid = $sysid ? 'D276000186B20005'.strtoupper(str_pad(dechex((int)$sysid),8,'0',STR_PAD_LEFT)) : '';
  171.                         $out['text'] .= '       <tr>';
  172.                         $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>';
  173.                         $out['text'] .= '               <td>'.(!$sysid_aid ? '<i>'._L('unknown').'</i>' : htmlentities($sysid_aid)).' ('._L('No PIX allowed').')</td>';
  174.                         $out['text'] .= '       </tr>';
  175.  
  176.                         $sys_url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL);
  177.                         $out['text'] .= '       <tr>';
  178.                         $out['text'] .= '               <td>'._L('System URL').'</td>';
  179.                         $out['text'] .= '               <td>'.htmlentities($sys_url).'</td>';
  180.                         $out['text'] .= '       </tr>';
  181.  
  182.                         $sys_ver = OIDplus::getVersion();
  183.                         $out['text'] .= '       <tr>';
  184.                         $out['text'] .= '               <td>'._L('System version').'</td>';
  185.                         $out['text'] .= '               <td>'.(!$sys_ver ? '<i>'._L('unknown').'</i>' : htmlentities($sys_ver)).'</td>';
  186.                         $out['text'] .= '       </tr>';
  187.  
  188.                         $sys_install_type = OIDplus::getInstallType();
  189.                         $out['text'] .= '       <tr>';
  190.                         $out['text'] .= '               <td>'._L('Installation type').'</td>';
  191.                         $out['text'] .= '               <td>'.htmlentities($sys_install_type).'</td>';
  192.                         $out['text'] .= '       </tr>';
  193.  
  194.                         $out['text'] .= '</table>';
  195.                         $out['text'] .= '</div></div>';
  196.  
  197.                         # ---
  198.  
  199.                         $out['text'] .= '<h2>'._L('PHP').'</h2>';
  200.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  201.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  202.                         $out['text'] .= '       <tr>';
  203.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  204.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  205.                         $out['text'] .= '       </tr>';
  206.                         $out['text'] .= '       <tr>';
  207.                         $out['text'] .= '               <td>'._L('PHP version').'</td>';
  208.                         $out['text'] .= '               <td>'.PHP_VERSION.'</td>';
  209.                         $out['text'] .= '       </tr>';
  210.                         $out['text'] .= '       <tr>';
  211.                         $out['text'] .= '               <td>'._L('PHP configuration file(s)').'</td>';
  212.                         $out['text'] .= '               <td>'.$this->getLoadedInis().'</td>';
  213.                         $out['text'] .= '       </tr>';
  214.                         $out['text'] .= '       <tr>';
  215.                         $out['text'] .= '               <td>'._L('Installed extensions').'</td>';
  216.                         $out['text'] .= '               <td>'.htmlentities(implode(', ',get_loaded_extensions())).'</td>';
  217.                         $out['text'] .= '       </tr>';
  218.                         $out['text'] .= '</table>';
  219.                         $out['text'] .= '<p><a '.OIDplus::gui()->link('oidplus:phpinfo').'>'._L('Show PHP server configuration (phpinfo)').'</a></p>';
  220.                         $out['text'] .= '</div></div>';
  221.  
  222.                         # ---
  223.  
  224.                         $out['text'] .= '<h2>'._L('Webserver').'</h2>';
  225.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  226.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  227.                         $out['text'] .= '       <tr>';
  228.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  229.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  230.                         $out['text'] .= '       </tr>';
  231.                         $out['text'] .= '       <tr>';
  232.                         $out['text'] .= '               <td>'._L('Server software').'</td>';
  233.                         $out['text'] .= '               <td>'.($_SERVER['SERVER_SOFTWARE'] ?? '<i>' . _L('unknown') . '</i>').'</td>';
  234.                         $out['text'] .= '       </tr>';
  235.                         $out['text'] .= '       <tr>';
  236.                         $out['text'] .= '               <td>'._L('User account').'</td>';
  237.                         $current_user = exec('whoami');
  238.                         if ($current_user == '') {
  239.                                 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  240.                                         // Windows on an IIS server:
  241.                                         //     getenv('USERNAME')     MARSCHALL$                (That is the "machine account", see https://docs.microsoft.com/en-us/iis/manage/configuring-security/application-pool-identities#accessing-the-network )
  242.                                         //     get_current_user()     DefaultAppPool
  243.                                         //     exec('whoami')         iis apppool\defaultapppool
  244.                                         // Windows with XAMPP:
  245.                                         //     getenv('USERNAME')     dmarschall
  246.                                         //     get_current_user()     dmarschall               (even if script has a different NTFS owner!)
  247.                                         //     exec('whoami')         hickelsoft\dmarschall
  248.                                         $current_user = get_current_user();
  249.                                         if ($current_user == '') $current_user = getenv('USERNAME');
  250.                                 } else {
  251.                                         // On Linux:
  252.                                         // get_current_user() will get the owner of the PHP script, not the process owner!
  253.                                         // We want the process owner, so we use posix_geteuid().
  254.                                         if (function_exists('posix_geteuid') && function_exists('posix_getpwuid')) {
  255.                                                 $uid = posix_geteuid();
  256.                                                 $current_user = posix_getpwuid($uid); // receive username (required read access to /etc/passwd )
  257.                                         } else {
  258.                                                 $uid = -1;
  259.                                         }
  260.                                         if ($current_user == '') $current_user = get_current_user();
  261.                                         if (($current_user == '') && ($uid >= 0)) $current_user = '#'.$uid;
  262.                                 }
  263.                         }
  264.                         $out['text'] .= '               <td>'.($current_user == '' ? '<i>'._L('unknown').'</i>' : htmlentities($current_user)).'</td>';
  265.                         $out['text'] .= '       </tr>';
  266.                         $out['text'] .= '</table>';
  267.                         $out['text'] .= '</div></div>';
  268.  
  269.                         # ---
  270.  
  271.                         $out['text'] .= '<h2>'._L('Database').'</h2>';
  272.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  273.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  274.                         $out['text'] .= '       <tr>';
  275.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  276.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  277.                         $out['text'] .= '       </tr>';
  278.  
  279.                         $out['text'] .= '       <tr>';
  280.                         $out['text'] .= '               <td>'._L('Database provider').'</td>';
  281.                         $out['text'] .= '               <td>'.OIDplus::db()->getPlugin()->getManifest()->getName().'</td>';
  282.                         $out['text'] .= '       </tr>';
  283.  
  284.                         $out['text'] .= '       <tr>';
  285.                         $out['text'] .= '               <td>'._L('SQL slang').'</td>';
  286.                         $out['text'] .= '               <td>'.OIDplus::db()->getSlang()->getManifest()->getName().'</td>';
  287.                         $out['text'] .= '       </tr>';
  288.  
  289.                         $table_prefix = OIDplus::baseConfig()->getValue('TABLENAME_PREFIX');
  290.                         $out['text'] .= '       <tr>';
  291.                         $out['text'] .= '               <td>'._L('Table name prefix').'</td>';
  292.                         $out['text'] .= '               <td>'.(!empty($table_prefix) ? htmlentities($table_prefix) : '<i>'._L('none').'</i>').'</td>';
  293.                         $out['text'] .= '       </tr>';
  294.                         $out['text'] .= '       <tr>';
  295.                         $out['text'] .= '               <td>'._L('Server time').'</td>';
  296.                         $tmp = OIDplus::db()->query('select '.OIDplus::db()->sqlDate().' as tmp');
  297.                         if ($tmp) $tmp = $tmp->fetch_array();
  298.                         $tmp = $tmp['tmp'] ?? _L('n/a');
  299.                         $out['text'] .= '               <td>'.$tmp.'</td>';
  300.                         $out['text'] .= '       </tr>';
  301.                         $out['text'] .= '</table>';
  302.                         $out['text'] .= '</div></div>';
  303.  
  304.                         // TODO: can we somehow get the DBMS version, connection string etc?
  305.  
  306.                         # ---
  307.  
  308.                         $out['text'] .= '<h2>'._L('Operating System').'</h2>';
  309.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  310.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  311.                         $out['text'] .= '       <tr>';
  312.                         $out['text'] .= '               <th width="50%">'._L('Attribute').'</th>';
  313.                         $out['text'] .= '               <th width="50%">'._L('Value').'</th>';
  314.                         $out['text'] .= '       </tr>';
  315.                         if (php_uname("m") == php_uname("n")) {
  316.                                 // At some hosts like Strato, php_uname() always returns the same string
  317.                                 // "Linux localhost 3.10.0-1127.10.1.el7.x86_64 #1 SMP"
  318.                                 $out['text'] .= '       <tr>';
  319.                                 $out['text'] .= '               <td>'._L('Operating System').'</td>';
  320.                                 $out['text'] .= '               <td>'.htmlentities(PHP_OS).'</td>';
  321.                                 $out['text'] .= '       </tr>';
  322.                                 $out['text'] .= '       <tr>';
  323.                                 $out['text'] .= '               <td>'._L('Hostname').'</td>';
  324.                                 $out['text'] .= '               <td>'.htmlentities(gethostname()).'</td>';
  325.                                 $out['text'] .= '       </tr>';
  326.                         } else {
  327.                                 $out['text'] .= '       <tr>';
  328.                                 $out['text'] .= '               <td>'._L('Operating System').'</td>';
  329.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("s").' '.php_uname("r").' '.php_uname("v")).'</td>';
  330.                                 $out['text'] .= '       </tr>';
  331.                                 $out['text'] .= '       <tr>';
  332.                                 $out['text'] .= '               <td>'._L('Machine type').'</td>';
  333.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("m")).'</td>';
  334.                                 $out['text'] .= '       </tr>';
  335.                                 $out['text'] .= '       <tr>';
  336.                                 $out['text'] .= '               <td>'._L('Hostname').'</td>';
  337.                                 $out['text'] .= '               <td>'.htmlentities(php_uname("n")).'</td>';
  338.                                 $out['text'] .= '       </tr>';
  339.                         }
  340.                         $out['text'] .= '       <tr>';
  341.                         $out['text'] .= '               <td>'._L('Server time').'</td>';
  342.                         $out['text'] .= '               <td>'.htmlentities(date('Y-m-d H:i:s P')).'</td>';
  343.                         $out['text'] .= '       </tr>';
  344.                         $out['text'] .= '</table>';
  345.                         $out['text'] .= '</div></div>';
  346.  
  347.                         # ---
  348.  
  349.                 }
  350.         }
  351.  
  352.         /**
  353.          * @param array $json
  354.          * @param string|null $ra_email
  355.          * @param bool $nonjs
  356.          * @param string $req_goto
  357.          * @return bool
  358.          * @throws OIDplusException
  359.          */
  360.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  361.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  362.  
  363.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  364.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  365.                 } else {
  366.                         $tree_icon = null; // default icon (folder)
  367.                 }
  368.  
  369.                 if (file_exists(__DIR__.'/img/php_icon16.png')) {
  370.                         $tree_icon_php = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/php_icon16.png';
  371.                 } else {
  372.                         $tree_icon_php = null; // default icon (folder)
  373.                 }
  374.  
  375.                 $json[] = array(
  376.                         'id' => 'oidplus:systeminfo',
  377.                         'icon' => $tree_icon,
  378.                         'text' => _L('System information'),
  379.                         'children' => array(array(
  380.                                 'id' => 'oidplus:phpinfo',
  381.                                 'icon' => $tree_icon_php,
  382.                                 'text' => _L('PHP information')
  383.                         ))
  384.                 );
  385.  
  386.                 return true;
  387.         }
  388.  
  389.         /**
  390.          * @param string $request
  391.          * @return array|false
  392.          */
  393.         public function tree_search(string $request) {
  394.                 return false;
  395.         }
  396.  
  397.         /**
  398.          * @param string $id
  399.          * @return bool
  400.          */
  401.         public function implementsFeature(string $id): bool {
  402.                 return false;
  403.         }
  404. }
  405.