wp-serverinfo icon indicating copy to clipboard operation
wp-serverinfo copied to clipboard

Bug: Fatal error when system() function is disabled

Open withvision opened this issue 3 months ago • 2 comments

Hello,

First of all, thank you for the great plugin, I really like it. However, I found a small “bug” or rather an improvement to a function that I think would be useful.

Description:

The plugin throws a fatal error when the system() function is disabled on the server (which is common on shared hosting for security reasons).

The Error Message:

PHP Fatal error: Uncaught Error: Call to undefined function system() in /wp-content/plugins/wp-serverinfo/wp-serverinfo.php:535

Root Cause:

The get_serverload() function here at wp-serverinfo.php tries to use system('uptime') without checking if the function is available or disabled via disable_functions in php.ini.

My Proposed Solution:

Check if system functions are available before calling them. Here's a fixed version that tries multiple alternatives:

### Function: Get The Server Load (PATCHED VERSION)

![Image](https://github.com/user-attachments/assets/9604387b-fd92-4a58-afdf-072d1b051708)

function get_serverload() {
    $server_load = '';
    if(PHP_OS != 'WINNT' && PHP_OS != 'WIN32') {
        if(@file_exists('/proc/loadavg') ) {
            if ($fh = @fopen( '/proc/loadavg', 'r' )) {
                $data = @fread( $fh, 6 );
                @fclose( $fh );
                $load_avg = explode( " ", $data );
                $server_load = trim($load_avg[0]);
            }
        } else if(function_exists('system') && !in_array('system', explode(',', ini_get('disable_functions')))) {
            // Only use if system() is available and not disabled
            $data = @system('uptime');
            if($data !== false) {
                preg_match('/(.*):{1}(.*)/', $data, $matches);
                if(isset($matches[2])) {
                    $load_arr = explode(',', $matches[2]);
                    $server_load = trim($load_arr[0]);
                }
            }
        } else if(function_exists('exec') && !in_array('exec', explode(',', ini_get('disable_functions')))) {
            // Try exec() as alternative
            $data = @exec('uptime 2>&1', $output, $return_var);
            if($return_var === 0 && !empty($data)) {
                preg_match('/load average: ([0-9\.]+)/', $data, $matches);
                if(isset($matches[1])) {
                    $server_load = $matches[1];
                }
            }
        } else if(function_exists('shell_exec') && !in_array('shell_exec', explode(',', ini_get('disable_functions')))) {
            // Try shell_exec() as alternative
            $data = @shell_exec('uptime 2>&1');
            if(!empty($data)) {
                preg_match('/load average: ([0-9\.]+)/', $data, $matches);
                if(isset($matches[1])) {
                    $server_load = $matches[1];
                }
            }
        }
    }
    if(empty($server_load)) {
        $server_load = __('N/A', 'wp-serverinfo');
    }
    return $server_load;
}

The Benefits

  • Prevents fatal errors on servers with disabled system functions
  • Tries multiple alternatives (system, exec, shell_exec)
  • Gracefully falls back to "N/A" when no method is available
  • Maintains backward compatibility

Testet Environment:

  • WordPress 6.x
  • PHP 8.x with disable_functions = system,exec,shell_exec,passthru
  • Shared hosting environment

Image

withvision avatar Oct 16 '25 23:10 withvision

You want to submit a PR?

lesterchan avatar Nov 04 '25 13:11 lesterchan

Hey Lester, i just wanted to report this issue so you’re aware of it. But if you prefer, I can also create a pull request with the fix,totally fine either way, just let me know what works best for you.

withvision avatar Nov 04 '25 15:11 withvision

Yea, if you could create the PR, they would help! Will review it

lesterchan avatar Nov 05 '25 02:11 lesterchan