初始上传

This commit is contained in:
2026-04-04 17:27:12 +08:00
parent 4d80d28eb4
commit b7e11774ee
11191 changed files with 1588469 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\middleware;
use Closure;
use think\Config;
use think\Request;
use think\Response;
/**
* 跨域请求支持
*/
class AllowCrossDomain
{
protected $cookieDomain;
protected $header = [
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => 1800,
'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With',
];
public function __construct(Config $config)
{
$this->cookieDomain = $config->get('cookie.domain', '');
}
/**
* 允许跨域请求
* @access public
* @param Request $request
* @param Closure $next
* @param array $header
* @return Response
*/
public function handle($request, Closure $next, ? array $header = [])
{
$header = !empty($header) ? array_merge($this->header, $header) : $this->header;
if (!isset($header['Access-Control-Allow-Origin'])) {
$origin = $request->header('origin');
if ($origin && ('' == $this->cookieDomain || strpos($origin, $this->cookieDomain))) {
$header['Access-Control-Allow-Origin'] = $origin;
} else {
$header['Access-Control-Allow-Origin'] = '*';
}
}
return $next($request)->header($header);
}
}

View File

@@ -0,0 +1,183 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\middleware;
use Closure;
use think\Cache;
use think\Config;
use think\Request;
use think\Response;
/**
* 请求缓存处理
*/
class CheckRequestCache
{
/**
* 缓存对象
* @var Cache
*/
protected $cache;
/**
* 配置参数
* @var array
*/
protected $config = [
// 请求缓存规则 true为自动规则
'request_cache_key' => true,
// 请求缓存有效期
'request_cache_expire' => null,
// 全局请求缓存排除规则
'request_cache_except' => [],
// 请求缓存的Tag
'request_cache_tag' => '',
];
public function __construct(Cache $cache, Config $config)
{
$this->cache = $cache;
$this->config = array_merge($this->config, $config->get('route'));
}
/**
* 设置当前地址的请求缓存
* @access public
* @param Request $request
* @param Closure $next
* @param mixed $cache
* @return Response
*/
public function handle($request, Closure $next, $cache = null)
{
if ($request->isGet() && false !== $cache) {
if (false === $this->config['request_cache_key']) {
// 关闭当前缓存
$cache = false;
}
$cache = $cache ?? $this->getRequestCache($request);
if ($cache) {
if (is_array($cache)) {
[$key, $expire, $tag] = array_pad($cache, 3, null);
} else {
$key = md5($request->url(true));
$expire = $cache;
$tag = null;
}
$key = $this->parseCacheKey($request, $key);
if (strtotime($request->server('HTTP_IF_MODIFIED_SINCE', '')) + $expire > $request->server('REQUEST_TIME')) {
// 读取缓存
return Response::create()->code(304);
} elseif (($hit = $this->cache->get($key)) !== null) {
[$content, $header, $when] = $hit;
if (null === $expire || $when + $expire > $request->server('REQUEST_TIME')) {
return Response::create($content)->header($header);
}
}
}
}
$response = $next($request);
if (isset($key) && 200 == $response->getCode() && $response->isAllowCache()) {
$header = $response->getHeader();
$header['Cache-Control'] = 'max-age=' . $expire . ',must-revalidate';
$header['Last-Modified'] = gmdate('D, d M Y H:i:s') . ' GMT';
$header['Expires'] = gmdate('D, d M Y H:i:s', time() + $expire) . ' GMT';
$this->cache->tag($tag)->set($key, [$response->getContent(), $header, time()], $expire);
}
return $response;
}
/**
* 读取当前地址的请求缓存信息
* @access protected
* @param Request $request
* @return mixed
*/
protected function getRequestCache($request)
{
$key = $this->config['request_cache_key'];
$expire = $this->config['request_cache_expire'];
$except = $this->config['request_cache_except'];
$tag = $this->config['request_cache_tag'];
foreach ($except as $rule) {
if (0 === stripos($request->url(), $rule)) {
return;
}
}
return [$key, $expire, $tag];
}
/**
* 读取当前地址的请求缓存信息
* @access protected
* @param Request $request
* @param mixed $key
* @return null|string
*/
protected function parseCacheKey($request, $key)
{
if ($key instanceof \Closure) {
$key = call_user_func($key, $request);
}
if (false === $key) {
// 关闭当前缓存
return;
}
if (true === $key) {
// 自动缓存功能
$key = '__URL__';
} elseif (strpos($key, '|')) {
[$key, $fun] = explode('|', $key);
}
// 特殊规则替换
if (false !== strpos($key, '__')) {
$key = str_replace(['__CONTROLLER__', '__ACTION__', '__URL__'], [$request->controller(), $request->action(), md5($request->url(true))], $key);
}
if (false !== strpos($key, ':')) {
$param = $request->param();
foreach ($param as $item => $val) {
if (is_string($val) && false !== strpos($key, ':' . $item)) {
$key = str_replace(':' . $item, (string) $val, $key);
}
}
} elseif (strpos($key, ']')) {
if ('[' . $request->ext() . ']' == $key) {
// 缓存某个后缀的请求
$key = md5($request->url());
} else {
return;
}
}
if (isset($fun)) {
$key = $fun($key);
}
return $key;
}
}

View File

@@ -0,0 +1,45 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\middleware;
use Closure;
use think\exception\ValidateException;
use think\Request;
use think\Response;
/**
* 表单令牌支持
*/
class FormTokenCheck
{
/**
* 表单令牌检测
* @access public
* @param Request $request
* @param Closure $next
* @param string $token 表单令牌Token名称
* @return Response
*/
public function handle(Request $request, Closure $next, string $token = null)
{
$check = $request->checkToken($token ?: '__token__');
if (false === $check) {
throw new ValidateException('invalid token');
}
return $next($request);
}
}

View File

@@ -0,0 +1,118 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\middleware;
use Closure;
use think\App;
use think\Config;
use think\Cookie;
use think\Lang;
use think\Request;
use think\Response;
/**
* 多语言加载
*/
class LoadLangPack
{
protected $app;
protected $lang;
protected $config;
public function __construct(App $app, Lang $lang, Config $config)
{
$this->app = $app;
$this->lang = $lang;
$this->config = $lang->getConfig();
}
/**
* 路由初始化(路由规则注册)
* @access public
* @param Request $request
* @param Closure $next
* @return Response
*/
public function handle($request, Closure $next)
{
// 自动侦测当前语言
$langset = $this->detect($request);
if ($this->lang->defaultLangSet() != $langset) {
$this->lang->switchLangSet($langset);
}
$this->saveToCookie($this->app->cookie, $langset);
return $next($request);
}
/**
* 自动侦测设置获取语言选择
* @access protected
* @param Request $request
* @return string
*/
protected function detect(Request $request): string
{
// 自动侦测设置获取语言选择
$langSet = '';
if ($request->get($this->config['detect_var'])) {
// url中设置了语言变量
$langSet = $request->get($this->config['detect_var']);
} elseif ($request->header($this->config['header_var'])) {
// Header中设置了语言变量
$langSet = $request->header($this->config['header_var']);
} elseif ($request->cookie($this->config['cookie_var'])) {
// Cookie中设置了语言变量
$langSet = $request->cookie($this->config['cookie_var']);
} elseif ($request->server('HTTP_ACCEPT_LANGUAGE')) {
// 自动侦测浏览器语言
$langSet = $request->server('HTTP_ACCEPT_LANGUAGE');
}
if (preg_match('/^([a-z\d\-]+)/i', $langSet, $matches)) {
$langSet = strtolower($matches[1]);
if (isset($this->config['accept_language'][$langSet])) {
$langSet = $this->config['accept_language'][$langSet];
}
} else {
$langSet = $this->lang->getLangSet();
}
if (empty($this->config['allow_lang_list']) || in_array($langSet, $this->config['allow_lang_list'])) {
// 合法的语言
$this->lang->setLangSet($langSet);
} else {
$langSet = $this->lang->getLangSet();
}
return $langSet;
}
/**
* 保存当前语言到Cookie
* @access protected
* @param Cookie $cookie Cookie对象
* @param string $langSet 语言
* @return void
*/
protected function saveToCookie(Cookie $cookie, string $langSet)
{
if ($this->config['use_cookie']) {
$cookie->set($this->config['cookie_var'], $langSet);
}
}
}

View File

@@ -0,0 +1,80 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\middleware;
use Closure;
use think\App;
use think\Request;
use think\Response;
use think\Session;
/**
* Session初始化
*/
class SessionInit
{
/** @var App */
protected $app;
/** @var Session */
protected $session;
public function __construct(App $app, Session $session)
{
$this->app = $app;
$this->session = $session;
}
/**
* Session初始化
* @access public
* @param Request $request
* @param Closure $next
* @return Response
*/
public function handle($request, Closure $next)
{
// Session初始化
$varSessionId = $this->app->config->get('session.var_session_id');
$cookieName = $this->session->getName();
if ($varSessionId && $request->request($varSessionId)) {
$sessionId = $request->request($varSessionId);
} else {
$sessionId = $request->cookie($cookieName);
}
if ($sessionId) {
$this->session->setId($sessionId);
}
$this->session->init();
$request->withSession($this->session);
/** @var Response $response */
$response = $next($request);
$response->setSession($this->session);
$this->app->cookie->set($cookieName, $this->session->getId());
return $response;
}
public function end(Response $response)
{
$this->session->save();
}
}