316 lines
12 KiB
PHP
316 lines
12 KiB
PHP
|
<?php
|
|||
|
/* file:常用函数、工具类函数封装的集合
|
|||
|
Created by wanghong<1204772286@qq.com>
|
|||
|
Date: 2021-02-24 */
|
|||
|
|
|||
|
namespace utils;
|
|||
|
|
|||
|
class Utils{
|
|||
|
|
|||
|
/*
|
|||
|
* 文件根据类型进行分类
|
|||
|
$ext -string 文件后缀
|
|||
|
return -int
|
|||
|
* */
|
|||
|
public static function getFileType($ext){
|
|||
|
$ext=strtolower($ext);
|
|||
|
$image=['jpg','jpeg','png','bmp','gif'];
|
|||
|
$radio=['mp3','wav','wmv','amr'];
|
|||
|
$video=['mp4','3gp','avi','m2v','mkv','mov'];
|
|||
|
$doc=['ppt','pptx','doc','docx','xls','xlsx','pdf','txt','md'];
|
|||
|
if(in_array($ext,$doc)){
|
|||
|
$fileType=1;
|
|||
|
}elseif(in_array($ext,$image)){
|
|||
|
$fileType=2;
|
|||
|
}elseif(in_array($ext,$radio)){
|
|||
|
$fileType=3;
|
|||
|
}elseif(in_array($ext,$video)){
|
|||
|
$fileType=4;
|
|||
|
}else{
|
|||
|
$fileType=9;
|
|||
|
}
|
|||
|
return $fileType;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/*
|
|||
|
获取文件的大小
|
|||
|
*/
|
|||
|
public static function get_file_size($size, $limit = 0)
|
|||
|
{
|
|||
|
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
|
|||
|
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
|
|||
|
return round($size, 2) . $units[$i + $limit];
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
h5ip网址缩短
|
|||
|
$link -string
|
|||
|
return string
|
|||
|
*/
|
|||
|
public static function h5ipUrl($link)
|
|||
|
{
|
|||
|
$url = "http://h5ip.cn/index/api?format=json&url=" . $link;
|
|||
|
$data = json_decode(curl_request($url), true);
|
|||
|
if ($data['code'] == 0) {
|
|||
|
return $data['short_url'];
|
|||
|
} else {
|
|||
|
return $link;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
linux系统探测
|
|||
|
*/
|
|||
|
public static function sys_linux()
|
|||
|
{
|
|||
|
// CPU
|
|||
|
if (false === ($str = @file("/proc/cpuinfo"))) return false;
|
|||
|
$str = implode("", $str);
|
|||
|
@preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\(\@.-]+)([\r\n]+)/s", $str, $model);
|
|||
|
@preg_match_all("/cpu\s+MHz\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $mhz);
|
|||
|
@preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/", $str, $cache);
|
|||
|
@preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $bogomips);
|
|||
|
if (false !== is_array($model[1])) {
|
|||
|
$res['cpu']['num'] = sizeof($model[1]);
|
|||
|
for ($i = 0; $i < $res['cpu']['num']; $i++) {
|
|||
|
$res['cpu']['model'][] = $model[1][$i] . ' (' . $mhz[1][$i] . ')';
|
|||
|
$res['cpu']['mhz'][] = $mhz[1][$i];
|
|||
|
$res['cpu']['cache'][] = $cache[1][$i];
|
|||
|
$res['cpu']['bogomips'][] = $bogomips[1][$i];
|
|||
|
}
|
|||
|
if ($res['cpu']['num'] == 1)
|
|||
|
$x1 = '';
|
|||
|
else
|
|||
|
$x1 = ' ×' . $res['cpu']['num'];
|
|||
|
$mhz[1][0] = ' | 频率:' . $mhz[1][0];
|
|||
|
$cache[1][0] = ' | 二级缓存:' . $cache[1][0];
|
|||
|
$bogomips[1][0] = ' | Bogomips:' . $bogomips[1][0];
|
|||
|
$res['cpu']['model'][] = $model[1][0] . $mhz[1][0] . $cache[1][0] . $bogomips[1][0] . $x1;
|
|||
|
if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("<br />", $res['cpu']['model']);
|
|||
|
if (false !== is_array($res['cpu']['mhz'])) $res['cpu']['mhz'] = implode("<br />", $res['cpu']['mhz']);
|
|||
|
if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("<br />", $res['cpu']['cache']);
|
|||
|
if (false !== is_array($res['cpu']['bogomips'])) $res['cpu']['bogomips'] = implode("<br />", $res['cpu']['bogomips']);
|
|||
|
}
|
|||
|
// NETWORK
|
|||
|
// UPTIME
|
|||
|
if (false === ($str = @file("/proc/uptime"))) return false;
|
|||
|
$str = explode(" ", implode("", $str));
|
|||
|
$str = trim($str[0]);
|
|||
|
$min = $str / 60;
|
|||
|
$hours = $min / 60;
|
|||
|
$days = floor($hours / 24);
|
|||
|
$hours = floor($hours - ($days * 24));
|
|||
|
$min = floor($min - ($days * 60 * 24) - ($hours * 60));
|
|||
|
if ($days !== 0) $res['uptime'] = $days . "天";
|
|||
|
if ($hours !== 0) $res['uptime'] .= $hours . "小时";
|
|||
|
$res['uptime'] .= $min . "分钟";
|
|||
|
|
|||
|
// MEMORY
|
|||
|
if (false === ($str = @file("/proc/meminfo"))) return false;
|
|||
|
$str = implode("", $str);
|
|||
|
preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
|
|||
|
preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);
|
|||
|
|
|||
|
$res['memTotal'] = round($buf[1][0] / 1024, 2);
|
|||
|
$res['memFree'] = round($buf[2][0] / 1024, 2);
|
|||
|
$res['memBuffers'] = round($buffers[1][0] / 1024, 2);
|
|||
|
$res['memCached'] = round($buf[3][0] / 1024, 2);
|
|||
|
$res['memUsed'] = $res['memTotal'] - $res['memFree'];
|
|||
|
$res['memPercent'] = (floatval($res['memTotal']) != 0) ? round($res['memUsed'] / $res['memTotal'] * 100, 2) : 0;
|
|||
|
$res['memRealUsed'] = $res['memTotal'] - $res['memFree'] - $res['memCached'] - $res['memBuffers']; //真实内存使用
|
|||
|
$res['memRealFree'] = $res['memTotal'] - $res['memRealUsed']; //真实空闲
|
|||
|
$res['memRealPercent'] = (floatval($res['memTotal']) != 0) ? round($res['memRealUsed'] / $res['memTotal'] * 100, 2) : 0; //真实内存使用率
|
|||
|
$res['memCachedPercent'] = (floatval($res['memCached']) != 0) ? round($res['memCached'] / $res['memTotal'] * 100, 2) : 0; //Cached内存使用率
|
|||
|
$res['swapTotal'] = round($buf[4][0] / 1024, 2);
|
|||
|
$res['swapFree'] = round($buf[5][0] / 1024, 2);
|
|||
|
$res['swapUsed'] = round($res['swapTotal'] - $res['swapFree'], 2);
|
|||
|
$res['swapPercent'] = (floatval($res['swapTotal']) != 0) ? round($res['swapUsed'] / $res['swapTotal'] * 100, 2) : 0;
|
|||
|
|
|||
|
// LOAD AVG
|
|||
|
if (false === ($str = @file("/proc/loadavg"))) return false;
|
|||
|
$str = explode(" ", implode("", $str));
|
|||
|
$str = array_chunk($str, 4);
|
|||
|
$res['loadAvg'] = implode(" ", $str[0]);
|
|||
|
return $res;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* 将数组按字母A-Z排序
|
|||
|
* @return [type] [description]
|
|||
|
*/
|
|||
|
public static function chartSort($array, $field,$isGroup=true,$chart='chart')
|
|||
|
{
|
|||
|
$newArray = [];
|
|||
|
foreach ($array as $k => &$v) {
|
|||
|
$v[$chart] = self::getFirstChart($v[$field]);
|
|||
|
$newArray[] = $v;
|
|||
|
}
|
|||
|
$data = [];
|
|||
|
if($isGroup){
|
|||
|
foreach ($newArray as $k => $v) {
|
|||
|
if (array_key_exists($v[$chart], $data)) {
|
|||
|
$data[$v[$chart]][] = $v;
|
|||
|
} else {
|
|||
|
$data[$v[$chart]] = [];
|
|||
|
$data[$v[$chart]][] = $v;
|
|||
|
}
|
|||
|
}
|
|||
|
ksort($data);
|
|||
|
}else{
|
|||
|
return $newArray;
|
|||
|
}
|
|||
|
return $data;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 返回取汉字的第一个字的首字母
|
|||
|
* @param [type] $str [string]
|
|||
|
* @return [type] [strind]
|
|||
|
*/
|
|||
|
public static function getFirstChart($str)
|
|||
|
{
|
|||
|
$str = str_replace(' ', '', $str);
|
|||
|
if (empty($str)) {
|
|||
|
return '#';
|
|||
|
}
|
|||
|
$char = ord($str[0]);
|
|||
|
if ($char >= ord('A') && $char <= ord('z')) {
|
|||
|
return strtoupper($str[0]);
|
|||
|
}
|
|||
|
$s1 = iconv('UTF-8', 'gb2312//IGNORE', $str);
|
|||
|
$s2 = iconv('gb2312', 'UTF-8//IGNORE', $s1);
|
|||
|
$s = $s2 == $str ? $s1 : $str;
|
|||
|
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
|
|||
|
if ($asc >= -20319 && $asc <= -20284) return 'A';
|
|||
|
if ($asc >= -20283 && $asc <= -19776) return 'B';
|
|||
|
if ($asc >= -19775 && $asc <= -19219) return 'C';
|
|||
|
if ($asc >= -19218 && $asc <= -18711) return 'D';
|
|||
|
if ($asc >= -18710 && $asc <= -18527) return 'E';
|
|||
|
if ($asc >= -18526 && $asc <= -18240) return 'F';
|
|||
|
if ($asc >= -18239 && $asc <= -17923) return 'G';
|
|||
|
if ($asc >= -17922 && $asc <= -17418) return 'H';
|
|||
|
if ($asc >= -17417 && $asc <= -16475) return 'J';
|
|||
|
if ($asc >= -16474 && $asc <= -16213) return 'K';
|
|||
|
if ($asc >= -16212 && $asc <= -15641) return 'L';
|
|||
|
if ($asc >= -15640 && $asc <= -15166) return 'M';
|
|||
|
if ($asc >= -15165 && $asc <= -14923) return 'N';
|
|||
|
if ($asc >= -14922 && $asc <= -14915) return 'O';
|
|||
|
if ($asc >= -14914 && $asc <= -14631) return 'P';
|
|||
|
if ($asc >= -14630 && $asc <= -14150) return 'Q';
|
|||
|
if ($asc >= -14149 && $asc <= -14091) return 'R';
|
|||
|
if ($asc >= -14090 && $asc <= -13319) return 'S';
|
|||
|
if ($asc >= -13318 && $asc <= -12839) return 'T';
|
|||
|
if ($asc >= -12838 && $asc <= -12557) return 'W';
|
|||
|
if ($asc >= -12556 && $asc <= -11848) return 'X';
|
|||
|
if ($asc >= -11847 && $asc <= -11056) return 'Y';
|
|||
|
if ($asc >= -11055 && $asc <= -10247) return 'Z';
|
|||
|
return "#";
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 人民币转大写
|
|||
|
* @param
|
|||
|
*/
|
|||
|
function cny($ns)
|
|||
|
{
|
|||
|
static $cnums = array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"),
|
|||
|
$cnyunits = array("圆", "角", "分"),
|
|||
|
$grees = array("拾", "佰", "仟", "万", "拾", "佰", "仟", "亿");
|
|||
|
list($ns1, $ns2) = explode(".", $ns, 2);
|
|||
|
$ns2 = array_filter(array($ns2[1], $ns2[0]));
|
|||
|
$ret = array_merge($ns2, array(implode("", _cny_map_unit(str_split($ns1), $grees)), ""));
|
|||
|
$ret = implode("", array_reverse(_cny_map_unit($ret, $cnyunits)));
|
|||
|
return str_replace(array_keys($cnums), $cnums, $ret);
|
|||
|
}
|
|||
|
|
|||
|
function _cny_map_unit($list, $units)
|
|||
|
{
|
|||
|
$ul = count($units);
|
|||
|
$xs = array();
|
|||
|
foreach (array_reverse($list) as $x) {
|
|||
|
$l = count($xs);
|
|||
|
if ($x != "0" || !($l % 4)) {
|
|||
|
$n = ($x == '0' ? '' : $x) . ($units[($l - 1) % $ul]);
|
|||
|
} else {
|
|||
|
$n = is_numeric($xs[0][0]) ? $x : '';
|
|||
|
}
|
|||
|
array_unshift($xs, $n);
|
|||
|
}
|
|||
|
return $xs;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 解析获取php.ini 的upload_max_filesize(单位:byte)
|
|||
|
* @param $dec int 小数位数
|
|||
|
* @return float (单位:byte)
|
|||
|
* */
|
|||
|
public static function get_upload_max_filesize_byte($dec = 2)
|
|||
|
{
|
|||
|
$max_size = ini_get('upload_max_filesize');
|
|||
|
preg_match('/(^[0-9\.]+)(\w+)/', $max_size, $info);
|
|||
|
$size = $info[1];
|
|||
|
$suffix = strtoupper($info[2]);
|
|||
|
$a = array_flip(array("B", "KB", "MB", "GB", "TB", "PB"));
|
|||
|
$b = array_flip(array("B", "K", "M", "G", "T", "P"));
|
|||
|
$pos = $a[$suffix] && $a[$suffix] !== 0 ? $a[$suffix] : $b[$suffix];
|
|||
|
return round($size * pow(1024, $pos), $dec);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 十六进制 转 RGB
|
|||
|
*/
|
|||
|
public static function hex2rgb($hexColor)
|
|||
|
{
|
|||
|
$color = str_replace('#', '', $hexColor);
|
|||
|
if (strlen($color) > 3) {
|
|||
|
$rgb = array(
|
|||
|
'r' => hexdec(substr($color, 0, 2)),
|
|||
|
'g' => hexdec(substr($color, 2, 2)),
|
|||
|
'b' => hexdec(substr($color, 4, 2))
|
|||
|
);
|
|||
|
} else {
|
|||
|
$color = $hexColor;
|
|||
|
$r = substr($color, 0, 1) . substr($color, 0, 1);
|
|||
|
$g = substr($color, 1, 1) . substr($color, 1, 1);
|
|||
|
$b = substr($color, 2, 1) . substr($color, 2, 1);
|
|||
|
$rgb = array(
|
|||
|
'r' => hexdec($r),
|
|||
|
'g' => hexdec($g),
|
|||
|
'b' => hexdec($b)
|
|||
|
);
|
|||
|
}
|
|||
|
return $rgb;
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* RGB转 十六进制
|
|||
|
* @param $rgb RGB颜色的字符串 如:rgb(255,255,255);
|
|||
|
* @return string 十六进制颜色值 如:#FFFFFF
|
|||
|
*/
|
|||
|
public static function RGBToHex($rgb){
|
|||
|
$regexp = "/^rgb\(([0-9]{0,3})\,\s*([0-9]{0,3})\,\s*([0-9]{0,3})\)/";
|
|||
|
$re = preg_match($regexp, $rgb, $match);
|
|||
|
$re = array_shift($match);
|
|||
|
$hexColor = "#";
|
|||
|
$hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
|
|||
|
for ($i = 0; $i < 3; $i++) {
|
|||
|
$r = null;
|
|||
|
$c = $match[$i];
|
|||
|
$hexAr = array();
|
|||
|
while ($c > 16) {
|
|||
|
$r = $c % 16;
|
|||
|
$c = ($c / 16) >> 0;
|
|||
|
array_push($hexAr, $hex[$r]);
|
|||
|
}
|
|||
|
array_push($hexAr, $hex[$c]);
|
|||
|
$ret = array_reverse($hexAr);
|
|||
|
$item = implode('', $ret);
|
|||
|
$item = str_pad($item, 2, '0', STR_PAD_LEFT);
|
|||
|
$hexColor .= $item;
|
|||
|
}
|
|||
|
return $hexColor;
|
|||
|
}
|
|||
|
|
|||
|
}
|