2014-06-18 04:11:11 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Misc
|
|
|
|
{
|
2015-01-16 12:14:07 -05:00
|
|
|
/**
|
|
|
|
* Returns human size
|
2015-07-07 09:57:41 -04:00
|
|
|
*
|
|
|
|
* @param float $filesize File size
|
|
|
|
* @param int $precision Number of decimals
|
|
|
|
* @return string Human size
|
2015-01-16 12:14:07 -05:00
|
|
|
*/
|
2014-06-18 04:11:11 -04:00
|
|
|
public static function getSize($filesize, $precision = 2)
|
|
|
|
{
|
|
|
|
$units = array('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
|
2015-01-16 12:14:07 -05:00
|
|
|
|
2014-06-18 04:11:11 -04:00
|
|
|
foreach ($units as $idUnit => $unit)
|
|
|
|
{
|
|
|
|
if ($filesize > 1024)
|
2015-01-16 12:14:07 -05:00
|
|
|
$filesize /= 1024;
|
2014-06-18 04:11:11 -04:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-11 09:28:05 -05:00
|
|
|
return round($filesize, $precision).' '.$units[$idUnit].'B';
|
2014-06-18 04:11:11 -04:00
|
|
|
}
|
2015-01-16 12:14:07 -05:00
|
|
|
|
2014-06-18 04:11:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns hostname
|
2015-07-07 09:57:41 -04:00
|
|
|
*
|
|
|
|
* @return string Hostname
|
2014-06-18 04:11:11 -04:00
|
|
|
*/
|
|
|
|
public static function getHostname()
|
|
|
|
{
|
|
|
|
return php_uname('n');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-16 12:14:07 -05:00
|
|
|
/**
|
|
|
|
* Returns CPU cores number
|
2015-07-07 09:57:41 -04:00
|
|
|
*
|
|
|
|
* @return int Number of cores
|
2015-01-16 12:14:07 -05:00
|
|
|
*/
|
|
|
|
public static function getCpuCoresNumber()
|
|
|
|
{
|
|
|
|
if (!($num_cores = shell_exec('/bin/grep -c ^processor /proc/cpuinfo')))
|
|
|
|
{
|
|
|
|
if (!($num_cores = trim(shell_exec('/usr/bin/nproc'))))
|
|
|
|
{
|
|
|
|
$num_cores = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)$num_cores <= 0)
|
|
|
|
$num_cores = 1;
|
|
|
|
|
|
|
|
return (int)$num_cores;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-18 04:11:11 -04:00
|
|
|
/**
|
|
|
|
* Returns server IP
|
2015-07-07 09:57:41 -04:00
|
|
|
*
|
|
|
|
* @return string Server local IP
|
2014-06-18 04:11:11 -04:00
|
|
|
*/
|
|
|
|
public static function getLanIp()
|
|
|
|
{
|
|
|
|
return $_SERVER['SERVER_ADDR'];
|
|
|
|
}
|
|
|
|
|
2014-12-19 12:06:37 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a command that exists in the system among $cmds
|
2015-07-07 09:57:41 -04:00
|
|
|
*
|
|
|
|
* @param array $cmds List of commands
|
|
|
|
* @param string $args List of arguments (optional)
|
|
|
|
* @param bool $returnWithArgs If true, returns command with the arguments
|
|
|
|
* @return string Command
|
2014-12-19 12:06:37 -05:00
|
|
|
*/
|
|
|
|
public static function whichCommand($cmds, $args = '', $returnWithArgs = true)
|
|
|
|
{
|
|
|
|
$return = '';
|
|
|
|
|
|
|
|
foreach ($cmds as $cmd)
|
|
|
|
{
|
|
|
|
if (trim(shell_exec($cmd.$args)) != '')
|
|
|
|
{
|
|
|
|
$return = $cmd;
|
|
|
|
|
|
|
|
if ($returnWithArgs)
|
|
|
|
$return .= $args;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-18 04:11:11 -04:00
|
|
|
/**
|
|
|
|
* Allows to pluralize a word based on a number
|
|
|
|
* Ex : echo 'mot'.Misc::pluralize(5); ==> prints mots
|
|
|
|
* Ex : echo 'cheva'.Misc::pluralize(5, 'ux', 'l'); ==> prints chevaux
|
|
|
|
* Ex : echo 'cheva'.Misc::pluralize(1, 'ux', 'l'); ==> prints cheval
|
|
|
|
*
|
2015-07-07 09:57:41 -04:00
|
|
|
* @param int $nb Number
|
|
|
|
* @param string $plural String for plural word
|
|
|
|
* @param string $singular String for singular word
|
|
|
|
* @return string String pluralized
|
2014-06-18 04:11:11 -04:00
|
|
|
*/
|
|
|
|
public static function pluralize($nb, $plural = 's', $singular = '')
|
|
|
|
{
|
|
|
|
return $nb > 1 ? $plural : $singular;
|
|
|
|
}
|
2015-07-07 09:57:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a port is open (TCP or UPD)
|
|
|
|
*
|
|
|
|
* @param string $host Host to check
|
|
|
|
* @param int $port Port number
|
|
|
|
* @param string $protocol tcp or udp
|
|
|
|
* @param integer $timeout Timeout
|
|
|
|
* @return bool True if the port is open else false
|
|
|
|
*/
|
|
|
|
public static function scanPort($host, $port, $protocol = 'tcp', $timeout = 3)
|
|
|
|
{
|
|
|
|
if ($protocol == 'tcp')
|
|
|
|
{
|
|
|
|
$handle = @fsockopen($host, $port, $errno, $errstr, $timeout);
|
|
|
|
|
|
|
|
if ($handle)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
elseif ($protocol == 'udp')
|
|
|
|
{
|
|
|
|
$handle = @fsockopen('udp://'.$host, $port, $errno, $errstr, $timeout);
|
|
|
|
|
|
|
|
socket_set_timeout($handle, $timeout);
|
|
|
|
|
|
|
|
$write = fwrite($handle, 'x00');
|
|
|
|
|
|
|
|
$startTime = time();
|
|
|
|
|
|
|
|
$header = fread($handle, 1);
|
|
|
|
|
|
|
|
$endTime = time();
|
|
|
|
|
|
|
|
$timeDiff = $endTime - $startTime;
|
|
|
|
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
if ($timeDiff >= $timeout)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-18 04:11:11 -04:00
|
|
|
}
|