| 1 |
<?php |
| 2 |
/** |
| 3 |
* Hosting environment handling. |
| 4 |
* |
| 5 |
* @package System |
| 6 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace POSessions\System; |
| 11 |
|
| 12 |
/** |
| 13 |
* The class responsible to manage and detect hosting environment. |
| 14 |
* |
| 15 |
* @package System |
| 16 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class Hosting { |
| 20 |
|
| 21 |
|
| 22 |
/** |
| 23 |
* Initializes the class and set its properties. |
| 24 |
* |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Check if the server config allows shell_exec(). |
| 32 |
* |
| 33 |
* @return bool True if shell_exec() can be used, false otherwise. |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
private static function is_shell_enabled() { |
| 37 |
if ( function_exists( 'shell_exec' ) && ! in_array( 'shell_exec', array_map( 'trim', explode( ', ', ini_get( 'disable_functions' ) ) ), true ) && (int) strtolower( ini_get( 'safe_mode' ) ) !== 1 ) { |
| 38 |
// phpcs:ignore |
| 39 |
$return = shell_exec( 'cat /proc/cpuinfo' ); |
| 40 |
if ( ! empty( $return ) ) { |
| 41 |
return true; |
| 42 |
} else { |
| 43 |
return false; |
| 44 |
} |
| 45 |
} else { |
| 46 |
return false; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get CPU count of the server. |
| 52 |
* |
| 53 |
* @return int|bool The count of CPUs, false if it's not countable. |
| 54 |
* @since 1.0.0 |
| 55 |
*/ |
| 56 |
public static function count_server_cpu() { |
| 57 |
$cpu_count = Cache::get_global( '/Hardware/CPU/Count' ); |
| 58 |
if ( false === $cpu_count ) { |
| 59 |
if ( self::is_shell_enabled() ) { |
| 60 |
// phpcs:ignore |
| 61 |
$cpu_count = shell_exec( 'cat /proc/cpuinfo |grep "physical id" | sort | uniq | wc -l' ); |
| 62 |
Cache::set_global( '/Hardware/CPU/Count', $cpu_count, 'diagnosis' ); |
| 63 |
} else { |
| 64 |
return false; |
| 65 |
} |
| 66 |
} |
| 67 |
return (int) $cpu_count; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get core count of the server. |
| 72 |
* |
| 73 |
* @return int|bool The count of cores, false if it's not countable. |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public static function count_server_core() { |
| 77 |
$core_count = Cache::get_global( '/Hardware/Core/Count' ); |
| 78 |
if ( false === $core_count ) { |
| 79 |
if ( self::is_shell_enabled() ) { |
| 80 |
// phpcs:ignore |
| 81 |
$core_count = shell_exec( "echo \"$((`cat /proc/cpuinfo | grep cores | grep -o '[0-9]' | uniq` * `cat /proc/cpuinfo |grep 'physical id' | sort | uniq | wc -l`))\"" ); |
| 82 |
Cache::set_global( '/Hardware/Core/Count', $core_count, 'diagnosis' ); |
| 83 |
} else { |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
return $core_count; |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|