first commit

This commit is contained in:
Mr.Qin
2022-08-19 19:48:37 +08:00
commit afdd648b65
3275 changed files with 631084 additions and 0 deletions

View File

@@ -0,0 +1,291 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Webman\Http;
use Webman\App;
use Webman\Route\Route;
/**
* Class Request
* @package Webman\Http
*/
class Request extends \Workerman\Protocols\Http\Request
{
/**
* @var string
*/
public $plugin = null;
/**
* @var string
*/
public $app = null;
/**
* @var string
*/
public $controller = null;
/**
* @var string
*/
public $action = null;
/**
* @var Route
*/
public $route = null;
/**
* @return mixed|null
*/
public function all()
{
return $this->post() + $this->get();
}
/**
* @param string $name
* @param string|null $default
* @return mixed|null
*/
public function input($name, $default = null)
{
$post = $this->post();
if (isset($post[$name])) {
return $post[$name];
}
$get = $this->get();
return isset($get[$name]) ? $get[$name] : $default;
}
/**
* @param array $keys
* @return array
*/
public function only(array $keys)
{
$all = $this->all();
$result = [];
foreach ($keys as $key) {
if (isset($all[$key])) {
$result[$key] = $all[$key];
}
}
return $result;
}
/**
* @param array $keys
* @return mixed|null
*/
public function except(array $keys)
{
$all = $this->all();
foreach ($keys as $key) {
unset($all[$key]);
}
return $all;
}
/**
* @param string|null $name
* @return null|UploadFile[]|UploadFile
*/
public function file($name = null)
{
$files = parent::file($name);
if (null === $files) {
return $name === null ? [] : null;
}
if ($name !== null) {
// Multi files
if (\is_array(\current($files))) {
return $this->parseFiles($files);
}
return $this->parseFile($files);
}
$upload_files = [];
foreach ($files as $name => $file) {
// Multi files
if (\is_array(\current($file))) {
$upload_files[$name] = $this->parseFiles($file);
} else {
$upload_files[$name] = $this->parseFile($file);
}
}
return $upload_files;
}
/**
* @param array $file
* @return UploadFile
*/
protected function parseFile(array $file)
{
return new UploadFile($file['tmp_name'], $file['name'], $file['type'], $file['error']);
}
/**
* @param array $files
* @return array
*/
protected function parseFiles(array $files)
{
$upload_files = [];
foreach ($files as $key => $file) {
if (\is_array(\current($file))) {
$upload_files[$key] = $this->parseFiles($file);
} else {
$upload_files[$key] = $this->parseFile($file);
}
}
return $upload_files;
}
/**
* @return string
*/
public function getRemoteIp()
{
return App::connection()->getRemoteIp();
}
/**
* @return int
*/
public function getRemotePort()
{
return App::connection()->getRemotePort();
}
/**
* @return string
*/
public function getLocalIp()
{
return App::connection()->getLocalIp();
}
/**
* @return int
*/
public function getLocalPort()
{
return App::connection()->getLocalPort();
}
/**
* @param bool $safe_mode
* @return string
*/
public function getRealIp(bool $safe_mode = true)
{
$remote_ip = $this->getRemoteIp();
if ($safe_mode && !static::isIntranetIp($remote_ip)) {
return $remote_ip;
}
return $this->header('client-ip', $this->header('x-forwarded-for',
$this->header('x-real-ip', $this->header('x-client-ip',
$this->header('via', $remote_ip)))));
}
/**
* @return string
*/
public function url()
{
return '//' . $this->host() . $this->path();
}
/**
* @return string
*/
public function fullUrl()
{
return '//' . $this->host() . $this->uri();
}
/**
* @return bool
*/
public function isAjax()
{
return $this->header('X-Requested-With') === 'XMLHttpRequest';
}
/**
* @return bool
*/
public function isPjax()
{
return (bool)$this->header('X-PJAX');
}
/**
* @return bool
*/
public function expectsJson()
{
return ($this->isAjax() && !$this->isPjax()) || $this->acceptJson();
}
/**
* @return bool
*/
public function acceptJson()
{
return false !== \strpos($this->header('accept', ''), 'json');
}
/**
* @param string $ip
* @return bool
*/
public static function isIntranetIp(string $ip)
{
// Not validate ip .
if (!\filter_var($ip, \FILTER_VALIDATE_IP)) {
return false;
}
// Is intranet ip ? For IPv4, the result of false may not be accurate, so we need to check it manually later .
if (!\filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE)) {
return true;
}
// Manual check only for IPv4 .
if (!\filter_var($ip, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
return false;
}
// Manual check .
$reserved_ips = [
1681915904 => 1686110207, // 100.64.0.0 - 100.127.255.255
3221225472 => 3221225727, // 192.0.0.0 - 192.0.0.255
3221225984 => 3221226239, // 192.0.2.0 - 192.0.2.255
3227017984 => 3227018239, // 192.88.99.0 - 192.88.99.255
3323068416 => 3323199487, // 198.18.0.0 - 198.19.255.255
3325256704 => 3325256959, // 198.51.100.0 - 198.51.100.255
3405803776 => 3405804031, // 203.0.113.0 - 203.0.113.255
3758096384 => 4026531839, // 224.0.0.0 - 239.255.255.255
];
$ip_long = \ip2long($ip);
foreach ($reserved_ips as $ip_start => $ip_end) {
if (($ip_long >= $ip_start) && ($ip_long <= $ip_end)) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,81 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Webman\Http;
use Webman\App;
use Throwable;
/**
* Class Response
* @package Webman\Http
*/
class Response extends \Workerman\Protocols\Http\Response
{
/**
* @var Throwable
*/
protected $_exception = null;
/**
* @param string $file
* @return $this
*/
public function file(string $file)
{
if ($this->notModifiedSince($file)) {
return $this->withStatus(304);
}
return $this->withFile($file);
}
/**
* @param string $file
* @param string $download_name
* @return $this
*/
public function download(string $file, string $download_name = '')
{
$this->withFile($file);
if ($download_name) {
$this->header('Content-Disposition', "attachment; filename=\"$download_name\"");
}
return $this;
}
/**
* @param string $file
* @return bool
*/
protected function notModifiedSince(string $file)
{
$if_modified_since = App::request()->header('if-modified-since');
if ($if_modified_since === null || !($mtime = \filemtime($file))) {
return false;
}
return $if_modified_since === \gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
}
/**
* @param Throwable $exception
* @return Throwable
*/
public function exception($exception = null)
{
if ($exception) {
$this->_exception = $exception;
}
return $this->_exception;
}
}

View File

@@ -0,0 +1,96 @@
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Webman\Http;
use Webman\File;
/**
* Class UploadFile
* @package Webman\Http
*/
class UploadFile extends File
{
/**
* @var string
*/
protected $_uploadName = null;
/**
* @var string
*/
protected $_uploadMimeType = null;
/**
* @var int
*/
protected $_uploadErrorCode = null;
/**
* UploadFile constructor.
*
* @param string $file_name
* @param string $upload_name
* @param string $upload_mime_type
* @param int $upload_error_code
*/
public function __construct(string $file_name, string $upload_name, string $upload_mime_type, int $upload_error_code)
{
$this->_uploadName = $upload_name;
$this->_uploadMimeType = $upload_mime_type;
$this->_uploadErrorCode = $upload_error_code;
parent::__construct($file_name);
}
/**
* @return string
*/
public function getUploadName()
{
return $this->_uploadName;
}
/**
* @return string
*/
public function getUploadMineType()
{
return $this->_uploadMimeType;
}
/**
* @return mixed
*/
public function getUploadExtension()
{
return \pathinfo($this->_uploadName, PATHINFO_EXTENSION);
}
/**
* @return int
*/
public function getUploadErrorCode()
{
return $this->_uploadErrorCode;
}
/**
* @return bool
*/
public function isValid()
{
return $this->_uploadErrorCode === UPLOAD_ERR_OK;
}
}