vr-shopxo-source/thinkphp/library/think/Request.php

2268 lines
58 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
use think\facade\Cookie;
use think\facade\Session;
class Request
{
/**
* 配置参数
* @var array
*/
protected $config = [
// 表单请求类型伪装变量
'var_method' => '_method',
// 表单ajax伪装变量
'var_ajax' => '_ajax',
// 表单pjax伪装变量
'var_pjax' => '_pjax',
// PATHINFO变量名 用于兼容模式
'var_pathinfo' => 's',
// 兼容PATH_INFO获取
'pathinfo_fetch' => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],
// 默认全局过滤方法 用逗号分隔多个
'default_filter' => '',
// 域名根如thinkphp.cn
'url_domain_root' => '',
// HTTPS代理标识
'https_agent_name' => '',
// IP代理获取标识
'http_agent_ip' => 'HTTP_X_REAL_IP',
// URL伪静态后缀
'url_html_suffix' => 'html',
];
/**
* 请求类型
* @var string
*/
protected $method;
/**
* 主机名(含端口)
* @var string
*/
protected $host;
/**
* 域名(含协议及端口)
* @var string
*/
protected $domain;
/**
* 子域名
* @var string
*/
protected $subDomain;
/**
* 泛域名
* @var string
*/
protected $panDomain;
/**
* 当前URL地址
* @var string
*/
protected $url;
/**
* 基础URL
* @var string
*/
protected $baseUrl;
/**
* 当前执行的文件
* @var string
*/
protected $baseFile;
/**
* 访问的ROOT地址
* @var string
*/
protected $root;
/**
* pathinfo
* @var string
*/
protected $pathinfo;
/**
* pathinfo不含后缀
* @var string
*/
protected $path;
/**
* 当前路由信息
* @var array
*/
protected $routeInfo = [];
/**
* 当前调度信息
* @var \think\route\Dispatch
*/
protected $dispatch;
/**
* 当前模块名
* @var string
*/
protected $module;
/**
* 当前控制器名
* @var string
*/
protected $controller;
/**
* 当前操作名
* @var string
*/
protected $action;
/**
* 当前语言集
* @var string
*/
protected $langset;
/**
* 当前请求参数
* @var array
*/
protected $param = [];
/**
* 当前GET参数
* @var array
*/
protected $get = [];
/**
* 当前POST参数
* @var array
*/
protected $post = [];
/**
* 当前REQUEST参数
* @var array
*/
protected $request = [];
/**
* 当前ROUTE参数
* @var array
*/
protected $route = [];
/**
* 当前PUT参数
* @var array
*/
protected $put;
/**
* 当前SESSION参数
* @var array
*/
protected $session = [];
/**
* 当前FILE参数
* @var array
*/
protected $file = [];
/**
* 当前COOKIE参数
* @var array
*/
protected $cookie = [];
/**
* 当前SERVER参数
* @var array
*/
protected $server = [];
/**
* 当前ENV参数
* @var array
*/
protected $env = [];
/**
* 当前HEADER参数
* @var array
*/
protected $header = [];
/**
* 资源类型定义
* @var array
*/
protected $mimeType = [
'xml' => 'application/xml,text/xml,application/x-xml',
'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
'js' => 'text/javascript,application/javascript,application/x-javascript',
'css' => 'text/css',
'rss' => 'application/rss+xml',
'yaml' => 'application/x-yaml,text/yaml',
'atom' => 'application/atom+xml',
'pdf' => 'application/pdf',
'text' => 'text/plain',
'image' => 'image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*',
'csv' => 'text/csv',
'html' => 'text/html,application/xhtml+xml,*/*',
];
/**
* 当前请求内容
* @var string
*/
protected $content;
/**
* 全局过滤规则
* @var array
*/
protected $filter;
/**
* 扩展方法
* @var array
*/
protected $hook = [];
/**
* php://input内容
* @var string
*/
protected $input;
/**
* 请求缓存
* @var array
*/
protected $cache;
/**
* 缓存是否检查
* @var bool
*/
protected $isCheckCache;
/**
* 请求安全Key
* @var string
*/
protected $secureKey;
/**
* 是否合并Param
* @var bool
*/
protected $mergeParam = false;
/**
* 架构函数
* @access public
* @param array $options 参数
*/
public function __construct(array $options = [])
{
$this->init($options);
// 保存 php://input
$this->input = file_get_contents('php://input');
}
public function init(array $options = [])
{
$this->config = array_merge($this->config, $options);
if (is_null($this->filter) && !empty($this->config['default_filter'])) {
$this->filter = $this->config['default_filter'];
}
}
public function config($name = null)
{
if (is_null($name)) {
return $this->config;
}
return isset($this->config[$name]) ? $this->config[$name] : null;
}
public static function __make(App $app, Config $config)
{
$request = new static($config->pull('app'));
$request->server = $_SERVER;
$request->env = $app['env']->get();
return $request;
}
public function __call($method, $args)
{
if (array_key_exists($method, $this->hook)) {
array_unshift($args, $this);
return call_user_func_array($this->hook[$method], $args);
}
throw new Exception('method not exists:' . static::class . '->' . $method);
}
/**
* Hook 方法注入
* @access public
* @param string|array $method 方法名
* @param mixed $callback callable
* @return void
*/
public function hook($method, $callback = null)
{
if (is_array($method)) {
$this->hook = array_merge($this->hook, $method);
} else {
$this->hook[$method] = $callback;
}
}
/**
* 创建一个URL请求
* @access public
* @param string $uri URL地址
* @param string $method 请求类型
* @param array $params 请求参数
* @param array $cookie
* @param array $files
* @param array $server
* @param string $content
* @return \think\Request
*/
public function create($uri, $method = 'GET', $params = [], $cookie = [], $files = [], $server = [], $content = null)
{
$server['PATH_INFO'] = '';
$server['REQUEST_METHOD'] = strtoupper($method);
$info = parse_url($uri);
if (isset($info['host'])) {
$server['SERVER_NAME'] = $info['host'];
$server['HTTP_HOST'] = $info['host'];
}
if (isset($info['scheme'])) {
if ('https' === $info['scheme']) {
$server['HTTPS'] = 'on';
$server['SERVER_PORT'] = 443;
} else {
unset($server['HTTPS']);
$server['SERVER_PORT'] = 80;
}
}
if (isset($info['port'])) {
$server['SERVER_PORT'] = $info['port'];
$server['HTTP_HOST'] = $server['HTTP_HOST']