/**
  * Generate server environment information such as:
  * - OS Version
  * - Apache version
  * - Apache Loaded Modules
  * - PHP Version
  * - PHP Loaded Modules
  * - PHP Major Configuration Values
  * - MySQL Server Version (retrieved from DB adapter)
  * - MySQL Supported Engines (took into account only enabled engines)
  * - MySQL Databases Present
  * - MySQL Plugins
  * - MySQL Major Global Variables
  *
  * Current method uses cURL request to sysreport.php tool (web mode only for phpinfo).
  * Sometimes it is not allowed or not applicable to request sysreport.php from outside. In this case information
  * will be not complete.
  *
  * @return array
  * @throws Exception
  */
 protected function _generateEnvironmentData()
 {
     $data = array();
     $count = 0;
     try {
         $phpInfo = $this->_collectPHPInfo();
     } catch (Exception $e) {
         $this->_log($e);
         $phpInfo = null;
     }
     if ($phpInfo === null) {
         $this->_log(null, 'So Environment Information will not be fully collected.');
     }
     if (is_array($phpInfo) && !empty($phpInfo)) {
         if (isset($phpInfo['General']['System'])) {
             $data[] = array('OS Information', $phpInfo['General']['System']);
             $count++;
         }
         if (isset($phpInfo['apache2handler']['Apache Version'])) {
             $data[] = array('Apache Version', $phpInfo['apache2handler']['Apache Version']);
             $count++;
         }
         if (isset($phpInfo['Apache Environment']['DOCUMENT_ROOT'])) {
             $data[] = array('Document Root', $phpInfo['Apache Environment']['DOCUMENT_ROOT']);
             $count++;
         } else {
             if (isset($phpInfo['PHP Variables']['_SERVER["DOCUMENT_ROOT"]'])) {
                 $data[] = array('Document Root', $phpInfo['PHP Variables']['_SERVER["DOCUMENT_ROOT"]']);
                 $count++;
             }
         }
         if (isset($phpInfo['Apache Environment']['SERVER_ADDR']) && isset($phpInfo['Apache Environment']['SERVER_PORT'])) {
             $data[] = array('Server Address', $phpInfo['Apache Environment']['SERVER_ADDR'] . ':' . $phpInfo['Apache Environment']['SERVER_PORT']);
             $count++;
         } else {
             if (isset($phpInfo['PHP Variables']['_SERVER["SERVER_ADDR"]']) && isset($phpInfo['PHP Variables']['_SERVER["SERVER_PORT"]'])) {
                 $data[] = array('Server Address', $phpInfo['PHP Variables']['_SERVER["SERVER_ADDR"]'] . ':' . $phpInfo['PHP Variables']['_SERVER["SERVER_PORT"]']);
                 $count++;
             }
         }
         if (isset($phpInfo['Apache Environment']['REMOTE_ADDR']) && isset($phpInfo['Apache Environment']['REMOTE_PORT'])) {
             $data[] = array('Remote Address', $phpInfo['Apache Environment']['REMOTE_ADDR'] . ':' . $phpInfo['Apache Environment']['REMOTE_PORT']);
             $count++;
         } else {
             if (isset($phpInfo['PHP Variables']['_SERVER["REMOTE_ADDR"]']) && isset($phpInfo['PHP Variables']['_SERVER["REMOTE_PORT"]'])) {
                 $data[] = array('Remote Address', $phpInfo['PHP Variables']['_SERVER["REMOTE_ADDR"]'] . ':' . $phpInfo['PHP Variables']['_SERVER["REMOTE_PORT"]']);
                 $count++;
             }
         }
     }
     // Apache Loaded Modules
     if (is_array($phpInfo) && !empty($phpInfo) && isset($phpInfo['apache2handler']['Loaded Modules'])) {
         $modulesInfo = '';
         $modules = explode(' ', $phpInfo['apache2handler']['Loaded Modules']);
         foreach ($modules as $module) {
             $modulesInfo .= $module . "\n";
         }
         $data[] = array('Apache Loaded Modules', trim($modulesInfo));
         $count++;
     }
     try {
         // DB (MySQL) Server Version
         if (!$this->_readConnection) {
             throw new Exception('Cant\'t connect to DB. MySQL version can\'t be retrieved.');
         }
         $data[] = array('MySQL Server Version', $this->_readConnection->getServerVersion());
     } catch (Exception $e) {
         $this->_log($e);
         $data[] = array('MySQL Server Version', 'n/a');
     }
     $count++;
     // MySQL Enabled and Supported Engines
     try {
         if (!$this->_readConnection) {
             throw new Exception('Cant\'t connect to DB. MySQL supported engines list can\'t be retrieved.');
         }
         $engines = $this->_readConnection->fetchAll('SHOW ENGINES');
         $supportedEngines = '';
         if ($engines) {
             foreach ($engines as $engine) {
                 if ($engine['Support'] != 'NO' && $engine['Engine'] != 'DISABLED') {
                     $supportedEngines .= $engine['Engine'] . '; ';
                 }
             }
         }
         $data[] = array('MySQL Supported Engines', $supportedEngines);
         unset($engines, $supportedEngines);
     } catch (Exception $e) {
         $this->_log($e);
         $data[] = array('MySQL Supported Engines', 'n/a');
     }
     $count++;
     // MySQL Databases amount
     try {
         if (!$this->_readConnection) {
             throw new Exception('Cant\'t connect to DB. Database number can\'t be collected.');
         }
         $databases = $this->_readConnection->fetchAll('SHOW DATABASES');
         $dbNumber = $databases ? sizeof($databases) : 0;
         $data[] = array('MySQL Databases Present', $dbNumber);
         unset($databases);
     } catch (Exception $e) {
         $this->_log($e);
         $data[] = array('MySQL Databases Present', 'n/a');
     }
     $count++;
     // MySQL Configuration
     $importantConfig = array('datadir', 'default_storage_engine', 'general_log', 'general_log_file', 'innodb_buffer_pool_size', 'innodb_io_capacity', 'innodb_log_file_size', 'innodb_thread_concurrency', 'innodb_flush_log_at_trx_commit', 'innodb_open_files', 'join_buffer_size', 'key_buffer_size', 'max_allowed_packet', 'max_connect_errors', 'max_connections', 'max_heap_table_size', 'query_cache_size', 'query_cache_limit', 'read_buffer_size', 'skip_name_resolve', 'slow_query_log', 'slow_query_log_file', 'sync_binlog', 'table_open_cache', 'tmp_table_size', 'wait_timeout', 'version');
     $maxSettingNameLength = 0;
     foreach ($importantConfig as $settingName) {
         $length = strlen($settingName);
         if ($length > $maxSettingNameLength) {
             $maxSettingNameLength = $length;
         }
     }
     try {
         if (!$this->_readConnection) {
             throw new Exception('Cant\'t connect to DB. MySQL config settings can\'t be collected.');
         }
         $variables = $this->_readConnection->fetchAssoc('SHOW GLOBAL VARIABLES');
         if ($variables) {
             $configuration = '';
             foreach ($variables as $variable) {
                 if (!in_array($variable['Variable_name'], $importantConfig)) {
                     continue;
                 }
                 if (substr($variable['Variable_name'], -4) == 'size') {
                     $variable['Value'] = $this->_formatBytes($variable['Value'], 3, 'IEC');
                 }
                 $indent = str_repeat(' ', $maxSettingNameLength - strlen($variable['Variable_name']) + 4);
                 $configuration .= $variable['Variable_name'] . $indent . ' => "' . $variable['Value'] . '"' . "\n";
             }
             $data[] = array('MySQL Configuration', trim($configuration));
         } else {
             $data[] = array('MySQL Configuration', 'n/a');
         }
         unset($variables);
     } catch (Exception $e) {
         $this->_log($e);
         $data[] = array('MySQL Configuration', 'n/a');
     }
     $count++;
     // MySQL Plugins
     try {
         if (!$this->_readConnection) {
             throw new Exception('Cant\'t connect to DB. MySQL plugins list can\'t be retrieved.');
         }
         $plugins = $this->_readConnection->fetchAssoc('SHOW PLUGINS');
         $installedPlugins = '';
         if ($plugins) {
             foreach ($plugins as $plugin) {
                 $installedPlugins .= ($plugin['Status'] == 'DISABLED' ? '-disabled- ' : '') . $plugin['Name'] . "\n";
             }
         }
         $data[] = array('MySQL Plugins', trim($installedPlugins));
         unset($plugins, $installedPlugins);
     } catch (Exception $e) {
         $this->_log($e);
         $data[] = array('MySQL Plugins', 'n/a');
     }
     $count++;
     // PHP Version
     $data[] = array('PHP Version', PHP_VERSION);
     $count++;
     if (is_array($phpInfo) && !empty($phpInfo)) {
         if (isset($phpInfo['General']['Loaded Configuration File'])) {
             $data[] = array('PHP Loaded Config File', $phpInfo['General']['Loaded Configuration File']);
             $count++;
         }
         if (isset($phpInfo['General']['Additional .ini files parsed'])) {
             $data[] = array('PHP Additional .ini files parsed', $phpInfo['General']['Additional .ini files parsed']);
             $count++;
         }
     }
     // PHP Important Config Settings
     $importantConfig = array('memory_limit', 'register_globals', 'safe_mode', 'upload_max_filesize', 'post_max_size', 'allow_url_fopen', 'default_charset', 'error_log', 'error_reporting', 'extension_dir', 'file_uploads', 'upload_tmp_dir', 'log_errors', 'magic_quotes_gpc', 'max_execution_time', 'max_file_uploads', 'max_input_time', 'max_input_vars');
     $maxSettingNameLength = 0;
     foreach ($importantConfig as $settingName) {
         $length = strlen($settingName);
         if ($length > $maxSettingNameLength) {
             $maxSettingNameLength = $length;
         }
     }
     if (is_array($phpInfo) && !empty($phpInfo)) {
         $coreEntry = isset($phpInfo['Core']) ? $phpInfo['Core'] : (isset($phpInfo['PHP Core']) ? $phpInfo['PHP Core'] : null);
         if ($coreEntry !== null) {
             $configuration = '';
             foreach ($coreEntry as $key => $info) {
                 if (in_array($key, $importantConfig)) {
                     $indent = str_repeat(' ', $maxSettingNameLength - strlen($key) + 4);
                     $configuration .= $key . $indent . ' => Local = "' . $info['local'] . '", Master = "' . $info['master'] . '"' . "\n";
                 }
             }
             $data[] = array('PHP Configuration', trim($configuration));
             $count++;
         }
     } else {
         $iniValues = ini_get_all();
         if (!empty($iniValues) && is_array($iniValues)) {
             $configuration = '';
             foreach ($iniValues as $key => $info) {
                 if (in_array($key, $importantConfig)) {
                     $configuration .= $key . ' => Local = "' . $info['local_value'] . '", Master = "' . $info['global_value'] . '"' . "\n";
                 }
             }
             $data[] = array('PHP Configuration', $configuration);
             $count++;
         }
     }
     try {
         /**
          * PHP Loaded Modules
          */
         $defaultPhpInfoCategories = array('General', 'apache2handler', 'Apache Environment', 'PHP Core', 'Core', 'HTTP Headers Information', 'Environment', 'PHP Variables');
         if (is_array($phpInfo) && !empty($phpInfo)) {
             $modulesInfo = '';
             foreach ($phpInfo as $module => $info) {
                 if (!in_array($module, $defaultPhpInfoCategories)) {
                     // Collect additional information for required modules by Magento
                     switch ($module) {
                         case 'curl':
                             if (isset($info['cURL Information'])) {
                                 $module .= ' [' . $info['cURL Information'] . ']';
                             }
                             break;
                         case 'dom':
                             if (isset($info['libxml Version'])) {
                                 $module .= ' [' . $info['libxml Version'] . ']';
                             }
                             break;
                         case 'gd':
                             if (isset($info['GD Version'])) {
                                 $module .= ' [' . $info['GD Version'] . ']';
                             }
                             break;
                         case 'iconv':
                             if (isset($info['iconv library version'])) {
                                 $module .= ' [' . $info['iconv library version'] . ']';
                             }
                             break;
                         case 'mcrypt':
                             if (isset($info['Version'])) {
                                 $module .= ' [' . $info['Version'] . ']';
                             }
                             break;
                         case 'pdo_mysql':
                             if (isset($info['Client API version'])) {
                                 $module .= ' [' . $info['Client API version'] . ']';
                             } else {
                                 if (isset($info['PDO Driver for MySQL, client library version'])) {
                                     $module .= ' [' . $info['PDO Driver for MySQL, client library version'] . ']';
                                 }
                             }
                             break;
                         case 'SimpleXML':
                             if (isset($info['Revision'])) {
                                 $module .= ' [' . $info['Revision'] . ']';
                             }
                             break;
                         case 'soap':
                         case 'hash':
                         default:
                             $module .= phpversion($module) ? ' [' . phpversion($module) . ']' : '';
                             break;
                     }
                     $modulesInfo .= $module . "\n";
                 }
             }
             $data[] = array('PHP Loaded Modules', trim($modulesInfo));
             $count++;
         } else {
             $modules = get_loaded_extensions();
             if (is_array($modules) && !empty($modules)) {
                 $modules = array_map('strtolower', $modules);
                 sort($modules);
                 $modulesInfo = '';
                 foreach ($modules as $module) {
                     $modulesInfo .= $module . (phpversion($module) ? ' [' . phpversion($module) . ']' : '') . "\n";
                 }
                 $data[] = array('PHP Loaded Modules', trim($modulesInfo));
                 $count++;
             }
         }
     } catch (Exception $e) {
         $this->_log($e);
         $data[] = array('PHP Loaded Modules', 'n/a');
     }
     $systemReport = array();
     $systemReport['Environment Information'] = array('header' => array('Parameter', 'Value'), 'data' => $data, 'count' => $count);
     return $systemReport;
 }