You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.4 KiB
PHP

<?php
namespace Base\Tool;
use Base\Tool\MobileDetect;
/**
* @author elf<360197197@qq.com>
*/
class Request {
private $serverInfo;
private $scheme;
private $mobileDetect;
public function __construct()
{
$this->serverInfo = $_SERVER;
}
public function isWechat()
{
$userAgent = $this->getUserAgent();
if (strpos($userAgent, 'MicroMessenger') == false && strpos($userAgent, 'Windows Phone') == false) {
return false;
} else {
return true;
}
}
public function getUserAgent()
{
return $this->serverInfo['HTTP_USER_AGENT'] ?? '';
}
public function getMobileDetect()
{
if (!$this->mobileDetect) {
$this->mobileDetect = new MobileDetect($this->serverInfo);
}
return $this->mobileDetect;
}
public function isMobile()
{
return $this->getMobileDetect()->isMobile();
}
public function isTablet()
{
return $this->getMobileDetect()->isTablet();
}
public function isIOS()
{
return $this->getMobileDetect()->isIOS();
}
public function isAndroid()
{
return $this->getMobileDetect()->isAndroidOS();
}
public function isIPadOS()
{
return $this->getMobileDetect()->isIPadOS();
}
public function isIOS13()
{
$version = $this->getMobileDetect()->version('iPhone');
if ($version) {
$versionItems = explode('_', $version);
return isset($versionItems[0]) && intval($versionItems[0]) == 13;
}
return false;
}
/**
* 获取客户端IP
*/
public function getClientIP()
{
$serverInfo = $this->serverInfo;
$ip = '0.0.0.0';
if (isset($serverInfo['HTTP_X_FORWARDED_FOR'])) {
$items = explode(',', $serverInfo['HTTP_X_FORWARDED_FOR']);
$position = array_search('unknown', $items);
if (false !== $position) {
unset($items[$position]);
}
$ip = trim($items[0]);
} elseif (isset($serverInfo['HTTP_CLIENT_IP'])) {
$ip = $serverInfo['HTTP_CLIENT_IP'];
} elseif (isset($this->serverInfo['REMOTE_ADDR'])) {
$ip = $serverInfo['REMOTE_ADDR'];
}
return $ip;
}
public function getHost()
{
$host = $this->getScheme() . '://' . $this->serverInfo['SERVER_NAME'];
if (in_array($this->serverInfo['SERVER_PORT'], [80, 443])) {
return $host;
}
return $host . ':' . $this->serverInfo['SERVER_PORT'];
}
public function getScheme()
{
if ($this->scheme) {
return $this->scheme;
}
$serverInfo = $this->serverInfo;
if (isset($serverInfo['REQUEST_SCHEME'])) {
$this->scheme = $serverInfo['REQUEST_SCHEME'];
return $this->scheme;
}
if ((isset($serverInfo['HTTPS']) && $serverInfo['HTTPS'] == 'on')
|| (isset($serverInfo['HTTP_X_FORWARDED_PROTO']) && $serverInfo['HTTP_X_FORWARDED_PROTO'] == 'https'
|| (isset($serverInfo['SERVER_PORT']) && $serverInfo['SERVER_PORT'] == 443))
) {
$this->scheme = 'https';
} else {
$this->scheme = 'http';
}
return $this->scheme;
}
public function getServerInfo()
{
return $this->serverInfo;
}
}