wpmudev-sls
11/17/2017 - 4:52 PM

[WordPress - General] Show additional server information. Useful for debugging website.

[WordPress - General] Show additional server information. Useful for debugging website.

<?php
/*
Plugin Name: WPMU DEV Tools
Plugin URI: https://premium.wpmudev.org/
Version: 0.5.1
Description: Shows diagnostic information about the current server environment as well as tests cURL connectivity to <a href="https://premium.wpmudev.org" target="_blank">premium.wpmudev.org</a> and displays the debug.log. <br /><strong>Notice</strong>: This plugin utilizes <a href="http://php.net/manual/en/function.shell-exec.php" target="_blank">shell_exec()</a>. This must be enabled in PHP for full functionality.
Author: James Morris & Lindeni @ WPMU DEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/

if ( ! defined( 'ABSPATH' ) ) {
   exit;
}

error_reporting(E_ALL);
ini_set('display_errors', 1);

class WPMUDEV_Supplementary_Tools {

   private static $_instance = null;

   public static function get_instance() {
      if( is_null( self::$_instance ) ){
         self::$_instance = new WPMUDEV_Supplementary_Tools();
      }
      return self::$_instance;
   }

   public function __construct() {

      add_action( 'init', array($this, 'debug_on' ) );
      add_action( 'admin_menu', array($this, 'wpmudev_tools_admin_menu' ) );
      add_action( 'network_admin_menu', array($this, 'wpmudev_tools_admin_menu' ) );
      add_action( 'admin_head', array( $this, 'css' ) );

   }

   public function wpmudev_tools_admin_menu(){

      add_management_page(
         __( 'WPMU DEV Tools', 'wpmudevtools' ),
         __( 'WPMU DEV Tools', 'wpmudevtools' ),
         'manage_options',
         'wpmudevtools',
         array( $this, 'wpmudev_show_server_info' )
      );
      
   }

   public function debug_on(){
      if (! defined('WP_DEBUG') ) define('WP_DEBUG', true);
      if (! defined('WP_DEBUG_LOG') ) define('WP_DEBUG_LOG', true);
      if (! defined('WP_DEBUG_DISPLAY') ) define('WP_DEBUG_DISPLAY', false);
      @ini_set('display_errors', 0);
   }

   public function wpmudev_show_server_info() {
      ?>
      <?php
      $du = shell_exec('du -hs ' . ABSPATH);
      $ls = shell_exec('ls -al ' . ABSPATH);
      $cat = shell_exec('cat ' . ABSPATH . '/wp-content/debug.log');
      $realIP = file_get_contents("http://ipecho.net/plain");
      ?>
      <div id="tools_output">
         <h1>WPMU DEV Tools</h1>
         <hr />
         <h2>Description:</h2>
         <p>Shows diagnostic information about the current server environment as well as tests cURL connectivity to <a href="https://premium.wpmudev.org" target="_blank">premium.wpmudev.org</a> and displays the debug.log. <br /><strong>Notice</strong>: This plugin utilizes <a href="http://php.net/manual/en/function.shell-exec.php" target="_blank">shell_exec()</a>. This must be enabled in PHP for full functionality.</p>
         <h2>Current Disk Usage</h2>
         <p><?php echo $du; ?></p>
         <h2>Server IP: <?php echo $_SERVER['SERVER_ADDR']; ?></h2>
         <h2>Outbound IP (<em>may differ from Server IP</em>): <?php echo $realIP; ?></h2>
         <h2>Memory Informmation (RAM)</h2>
         <pre><?php $meminfo = $this->get_memory_info();
         foreach ( $meminfo as $key => $value ) {
            echo $key . ' : ' . $value . '<br />'; 
         }?></pre>
         <h2>Current Directory Listing</h2>
         <pre><?php echo $ls; ?></pre>
         <h2>cURL Test</h2>
         <pre> <?php
         if ($this->isDEVAvailible('https://premium.wpmudev.org/api/dashboard/v1/')) {
            echo "<h2>You're able to access the WPMU DEV API via cURL!</h2>";
         }
         else {
            echo "<h2>You're <strong>NOT</strong> able to access the WPMU DEV API via cURL!</h2>";
         }

         if ( $this->isGoogleAvailible('https://google.com')) {
            echo "<h2>You're able to access Google via cURL!</h2>";
         }
         else {
            echo "<h2>You're <strong>NOT</strong> able to access Google via cURL!</h2>";
         }

         ?> </pre>
         <h2>cURL Test 2</h2>
         <pre><?php 
            echo "<h3>Response</h3>";
            echo "<hr />";
            $ch = curl_init('https://premium.wpmudev.org/api/dashboard/v1/');
            curl_exec($ch);
            $info = curl_getinfo($ch);
            echo "<hr />";
            echo "<h3>Timing</h3>";
            echo "<hr />";
            print_r ( $info );
            curl_close($ch);
            echo "<hr />";
         ?></pre>
         <h2>debug.log output</h2>
         <pre><?php echo $cat; ?></pre>
         <h2>PHPInfo</h2>
         <div id="phpinfoscroll"><?php $this->phpinfo(); ?></div>
      </div>
      <?php 
   }


//returns true, if domain is availible, false if not    
   function isDEVAvailible($domain) {
//check, if a valid url is provided
      if(!filter_var($domain, FILTER_VALIDATE_URL)) {
         return false;
      }

//initialize curl
      $curlInit = curl_init($domain);
      curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
      curl_setopt($curlInit,CURLOPT_HEADER,true);
      curl_setopt($curlInit,CURLOPT_NOBODY,true);
      curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

//get answer
      $response = curl_exec($curlInit);

      curl_close($curlInit);

      if ($response) return true;

      return false;
   }

//returns true, if domain is availible, false if not
   function isGoogleAvailible($domain)
   {
//check, if a valid url is provided
      if(!filter_var($domain, FILTER_VALIDATE_URL))
      {
         return false;
      }

//initialize curl
      $curlInit = curl_init($domain);
      curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
      curl_setopt($curlInit,CURLOPT_HEADER,true);
      curl_setopt($curlInit,CURLOPT_NOBODY,true);
      curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

//get answer
      $response = curl_exec($curlInit);

      curl_close($curlInit);

      if ($response) return true;

      return false;
   }

   function get_memory_details(){
      $fh = fopen('/proc/meminfo','r');
      $mem = 0;
      while ( $line = fgets( $fh ) ) {
         $pieces = array();
         if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) {
            $mem = $pieces[1];
            break;
         }
      }
      fclose( $fh );

      echo "$mem kB RAM found";	
   }

   function get_memory_info() 
   {       
      $data = explode("\n", file_get_contents("/proc/meminfo"));
      $meminfo = array();
      foreach ($data as $line) {
         list($key, $val) = explode(":", $line);
         $meminfo[$key] = trim($val);
      }		
      return $meminfo;
   }

   public function phpinfo(){
      ob_start();
      phpinfo();
      $php_info = ob_get_clean();

//remove the styling because it overrides wordpress styles
      $css = $this->get_string_between($php_info, '<head>', '</head>'); 
      $php_info = str_replace( 
         array( $css, '<html xmlns="http://www.w3.org/1999/xhtml"><head>', '</head>','<body>','</body>','</html>'), 
         "", $php_info 
      );

      $php_info = str_replace( '<table>', '<table class="form-table">', $php_info );

      echo $php_info;	
   }

   public function get_string_between($string, $start, $end){
      $string = ' ' . $string;
      $ini = strpos($string, $start);
      if ($ini == 0) return '';
      $ini += strlen($start);
      $len = strpos($string, $end, $ini) - $ini;
      return substr($string, $ini, $len);
   }

   public function css(){ 
      if ( $_GET['page'] == 'wpmudevtools' ) : ?>
      <style type="text/css">
/**body {background-color: #fff; color: #222; font-family: sans-serif;}
pre {margin: 0; font-family: monospace;} 
a:link {color: #009; text-decoration: none; background-color: #fff;}
a:hover {text-decoration: underline;}**/
#tools_output {
   margin: 1em 1em 0 0;
   padding: 1em;
   background-color: white;
   border: 1px solid silver;
   border-radius: 5px;
   overflow: hidden;
}
pre{
   height:250px;
   overflow:scroll;
   background:#efefef;
   border:1px solid silver;
   padding:0 1em;
}
iframe, #phpinfoscroll{
   width: 100%;
   height:500px;
   overflow:scroll;
   border:1px solid silver;
   padding:0 1em;
}
#content table {border-collapse: collapse; border: 0; width: 934px; box-shadow: 1px 2px 3px #ccc;}
.center {text-align: center;}
.center table {margin: 1em auto; text-align: left;}
.center th {text-align: center !important;}
td, th {border: 1px solid #666; font-size: 75%; vertical-align: baseline; padding: 4px 5px;}
#content h1 {font-size: 150%;}
#content h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccf; width: 300px; font-weight: bold;}
.h {background-color: #99c; font-weight: bold;}
.v {background-color: #ddd; max-width: 300px; overflow-x: auto; word-wrap: break-word;}
.v i {color: #999;}
img {float: right; border: 0;}
hr {width: 934px; background-color: #ccc; border: 0; height: 1px;}
pre hr {width:100% !important;}
</style>
<?php
endif;			
}
public function log(){
}		
}
add_action( 'plugins_loaded', function(){
   $GLOBALS['WPMUDEV_Supplementary_Tools'] = WPMUDEV_Supplementary_Tools::get_instance();
}, 10 );