|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Libs;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @title MyLib
|
|
|
|
* @description MyLib
|
|
|
|
* @package App\Service
|
|
|
|
* @author zcstatham
|
|
|
|
* @time 2021/1/25
|
|
|
|
*/
|
|
|
|
class MyLib
|
|
|
|
{
|
|
|
|
public static function scanFile($path) {
|
|
|
|
$result = array();
|
|
|
|
$files = scandir($path);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if ($file != '.' && $file != '..') {
|
|
|
|
if (is_dir($path . '/' . $file)) {
|
|
|
|
self::scanFile($path . '/' . $file);
|
|
|
|
} else {
|
|
|
|
$result[] = substr(basename($file), 0, -4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function Parser($text) {
|
|
|
|
$doc = new Doc();
|
|
|
|
return $doc->parse($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function GetClientIP()
|
|
|
|
{
|
|
|
|
global $HTTP_SERVER_VARS;
|
|
|
|
if (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])) {
|
|
|
|
$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
|
|
|
|
} elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]) {
|
|
|
|
$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
|
|
|
|
} elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"]) {
|
|
|
|
$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
|
|
|
|
} elseif (getenv("HTTP_X_FORWARDED_FOR")) {
|
|
|
|
$ip = getenv("HTTP_X_FORWARDED_FOR");
|
|
|
|
} elseif (getenv("HTTP_CLIENT_IP")) {
|
|
|
|
$ip = getenv("HTTP_CLIENT_IP");
|
|
|
|
} elseif (getenv("REMOTE_ADDR")) {
|
|
|
|
$ip = getenv("REMOTE_ADDR");
|
|
|
|
} else {
|
|
|
|
$ip = "0.0.0.0";
|
|
|
|
}
|
|
|
|
return $ip ;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function GetServerIP()
|
|
|
|
{
|
|
|
|
$ip = '0.0.0.0';
|
|
|
|
if (isset($_SERVER)) {
|
|
|
|
if (isset($_SERVER['SERVER_ADDR'])) {
|
|
|
|
$ip = $_SERVER['SERVER_ADDR'];
|
|
|
|
} elseif (isset($_SERVER['LOCAL_ADDR'])) {
|
|
|
|
$ip = $_SERVER['LOCAL_ADDR'];
|
|
|
|
} elseif (isset($_SERVER['SERVER_NAME'])) {
|
|
|
|
return gethostbyname($_SERVER['SERVER_NAME']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ip = getenv('SERVER_ADDR');
|
|
|
|
}
|
|
|
|
return $ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function IpIsLan(string $ip)
|
|
|
|
{
|
|
|
|
if ($ip == '127.0.0.1') return true;
|
|
|
|
return self::IpIsValid($ip,"192.168.0.0-192.168.255.255")
|
|
|
|
|| self::IpIsValid($ip,"172.16.0.0-172.31.255.255")
|
|
|
|
|| self::IpIsValid($ip,"10.0.0.0-10.255.255.255");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function IpIsValid(string $ip, string $ip_section = ''){
|
|
|
|
$ip = trim($ip);
|
|
|
|
$ip_section = trim($ip_section);
|
|
|
|
if (empty($ip)) return false;
|
|
|
|
$pattern_ip = '((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)';
|
|
|
|
$pattern_ipb = '/'. $pattern_ip ."\-". $pattern_ip .'/';
|
|
|
|
$pattern_ip = '/' . $pattern_ip . '/';
|
|
|
|
$match_count = preg_match($pattern_ip, $ip);
|
|
|
|
if ($pattern_ipb == '' && $match_count > 0) return true;
|
|
|
|
if (!$match_count || ($ip_section != '' && preg_match($pattern_ipb, $ip_section))) return false;
|
|
|
|
list($ip_min, $ip_max) = explode('-', $ip_section);
|
|
|
|
$ip_min = explode('.', $ip_min);
|
|
|
|
$ip_max = explode('.', $ip_max);
|
|
|
|
$ips = explode('.', $ip);
|
|
|
|
for($i = 0; $i < 4; $i ++) {
|
|
|
|
if($ips[$i] > $ip_max[$i] || $ips[$i] < $ip_min[$i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|