2268 lines
58 KiB
PHP
Executable File
2268 lines
58 KiB
PHP
Executable File
<?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'] . ':' . $info['port'];
|
||
}
|
||
|
||
if (isset($info['user'])) {
|
||
$server['PHP_AUTH_USER'] = $info['user'];
|
||
}
|
||
|
||
if (isset($info['pass'])) {
|
||
$server['PHP_AUTH_PW'] = $info['pass'];
|
||
}
|
||
|
||
if (!isset($info['path'])) {
|
||
$info['path'] = '/';
|
||
}
|
||
|
||
$options = [];
|
||
$queryString = '';
|
||
|
||
$options[strtolower($method)] = $params;
|
||
|
||
if (isset($info['query'])) {
|
||
parse_str(html_entity_decode($info['query']), $query);
|
||
if (!empty($params)) {
|
||
$params = array_replace($query, $params);
|
||
$queryString = http_build_query($params, '', '&');
|
||
} else {
|
||
$params = $query;
|
||
$queryString = $info['query'];
|
||
}
|
||
} elseif (!empty($params)) {
|
||
$queryString = http_build_query($params, '', '&');
|
||
}
|
||
|
||
if ($queryString) {
|
||
parse_str($queryString, $get);
|
||
$options['get'] = isset($options['get']) ? array_merge($get, $options['get']) : $get;
|
||
}
|
||
|
||
$server['REQUEST_URI'] = $info['path'] . ('' !== $queryString ? '?' . $queryString : '');
|
||
$server['QUERY_STRING'] = $queryString;
|
||
$options['cookie'] = $cookie;
|
||
$options['param'] = $params;
|
||
$options['file'] = $files;
|
||
$options['server'] = $server;
|
||
$options['url'] = $server['REQUEST_URI'];
|
||
$options['baseUrl'] = $info['path'];
|
||
$options['pathinfo'] = '/' == $info['path'] ? '/' : ltrim($info['path'], '/');
|
||
$options['method'] = $server['REQUEST_METHOD'];
|
||
$options['domain'] = isset($info['scheme']) ? $info['scheme'] . '://' . $server['HTTP_HOST'] : '';
|
||
$options['content'] = $content;
|
||
|
||
$request = new static();
|
||
foreach ($options as $name => $item) {
|
||
if (property_exists($request, $name)) {
|
||
$request->$name = $item;
|
||
}
|
||
}
|
||
|
||
return $request;
|
||
}
|
||
|
||
/**
|
||
* 获取当前包含协议、端口的域名
|
||
* @access public
|
||
* @param bool $port 是否需要去除端口号
|
||
* @return string
|
||
*/
|
||
public function domain($port = false)
|
||
{
|
||
return $this->scheme() . '://' . $this->host($port);
|
||
}
|
||
|
||
/**
|
||
* 获取当前根域名
|
||
* @access public
|
||
* @return string
|
||
*/
|
||
public function rootDomain()
|
||
{
|
||
$root = $this->config['url_domain_root'];
|
||
|
||
if (!$root) {
|
||
$item = explode('.', $this->host(true));
|
||
$count = count($item);
|
||
$root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0];
|
||
}
|
||
|
||
return $root;
|
||
}
|
||
|
||
/**
|
||
* 获取当前子域名
|
||
* @access public
|
||
* @return string
|
||
*/
|
||
public function subDomain()
|
||
{
|
||
if (is_null($this->subDomain)) {
|
||
// 获取当前主域名
|
||
$rootDomain = $this->config['url_domain_root'];
|
||
|
||
if ($rootDomain) {
|
||
// 配置域名根 例如 thinkphp.cn 163.com.cn 如果是国家级域名 com.cn net.cn 之类的域名需要配置
|
||
$domain = explode('.', rtrim(stristr($this->host(true), $rootDomain, true), '.'));
|
||
} else {
|
||
$domain = explode('.', $this->host(true), -2);
|
||
}
|
||
|
||
$this->subDomain = implode('.', $domain);
|
||
}
|
||
|
||
return $this->subDomain;
|
||
}
|
||
|
||
/**
|
||
* 设置当前泛域名的值
|
||
* @access public
|
||
* @param string $domain 域名
|
||
* @return $this
|
||
*/
|
||
public function setPanDomain($domain)
|
||
{
|
||
$this->panDomain = $domain;
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 获取当前泛域名的值
|
||
* @access public
|
||
* @return string
|
||
*/
|
||
public function panDomain()
|
||
{
|
||
return $this->panDomain;
|
||
}
|
||
|
||
/**
|
||
* 设置当前完整URL 包括QUERY_STRING
|
||
* @access public
|
||
* @param string $url URL
|
||
* @return $this
|
||
*/
|
||
public function setUrl($url)
|
||
{
|
||
$this->url = $url;
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 获取当前完整URL 包括QUERY_STRING
|
||
* @access public
|
||
* @param bool $complete 是否包含域名
|
||
* @return string
|
||
*/
|
||
public function url($complete = false)
|
||
{
|
||
if (!$this->url) {
|
||
if ($this->isCli()) {
|
||
$this->url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
|
||
} elseif ($this->server('HTTP_X_REWRITE_URL')) {
|
||
$this->url = $this->server('HTTP_X_REWRITE_URL');
|
||
} elseif ($this->server('REQUEST_URI')) {
|
||
$this->url = $this->server('REQUEST_URI');
|
||
} elseif ($this->server('ORIG_PATH_INFO')) {
|
||
$this->url = $this->server('ORIG_PATH_INFO') . (!empty($this->server('QUERY_STRING')) ? '?' . $this->server('QUERY_STRING') : '');
|
||
} else {
|
||
$this->url = '';
|
||
}
|
||
}
|
||
|
||
return $complete ? $this->domain() . $this->url : $this->url;
|
||
}
|
||
|
||
/**
|
||
* 设置当前完整URL 不包括QUERY_STRING
|
||
* @access public
|
||
* @param string $url URL
|
||
* @return $this
|
||
*/
|
||
public function setBaseUrl($url)
|
||
{
|
||
$this->baseUrl = $url;
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 获取当前URL 不含QUERY_STRING
|
||
* @access public
|
||
* @param bool $domain 是否包含域名
|
||
* @return string|$this
|
||
*/
|
||
public function baseUrl($domain = false)
|
||
{
|
||
if (!$this->baseUrl) {
|
||
$str = $this->url();
|
||
$this->baseUrl = strpos($str, '?') ? strstr($str, '?', true) : $str;
|
||
}
|
||
|
||
return $domain ? $this->domain() . $this->baseUrl : $this->baseUrl;
|
||
}
|
||
|
||
/**
|
||
* 设置或获取当前执行的文件 SCRIPT_NAME
|
||
* @access public
|
||
* @param bool $domain 是否包含域名
|
||
* @return string|$this
|
||
*/
|
||
public function baseFile($domain = false)
|
||
{
|
||
if (!$this->baseFile) {
|
||
$url = '';
|
||
if (!$this->isCli()) {
|
||
$script_name = basename($this->server('SCRIPT_FILENAME'));
|
||
if (basename($this->server('SCRIPT_NAME')) === $script_name) {
|
||
$url = $this->server('SCRIPT_NAME');
|
||
} elseif (basename($this->server('PHP_SELF')) === $script_name) {
|
||
$url = $this->server('PHP_SELF');
|
||
} elseif (basename($this->server('ORIG_SCRIPT_NAME')) === $script_name) {
|
||
$url = $this->server('ORIG_SCRIPT_NAME');
|
||
} elseif (($pos = strpos($this->server('PHP_SELF'), '/' . $script_name)) !== false) {
|
||
$url = substr($this->server('SCRIPT_NAME'), 0, $pos) . '/' . $script_name;
|
||
} elseif ($this->server('DOCUMENT_ROOT') && strpos($this->server('SCRIPT_FILENAME'), $this->server('DOCUMENT_ROOT')) === 0) {
|
||
$url = str_replace('\\', '/', str_replace($this->server('DOCUMENT_ROOT'), '', $this->server('SCRIPT_FILENAME')));
|
||
}
|
||
}
|
||
$this->baseFile = $url;
|
||
}
|
||
|
||
return $domain ? $this->domain() . $this->baseFile : $this->baseFile;
|
||
}
|
||
|
||
/**
|
||
* 设置URL访问根地址
|
||
* @access public
|
||
* @param string $url URL地址
|
||
* @return string|$this
|
||
*/
|
||
public function setRoot($url = null)
|
||
{
|
||
$this->root = $url;
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 获取URL访问根地址
|
||
* @access public
|
||
* @param bool $domain 是否包含域名
|
||
* @return string|$this
|
||
*/
|
||
public function root($domain = false)
|
||
{
|
||
if (!$this->root) {
|
||
$file = $this->baseFile();
|
||
if ($file && 0 !== strpos($this->url(), $file)) {
|
||
$file = str_replace('\\', '/', dirname($file));
|
||
}
|
||
$this->root = rtrim($file, '/');
|
||
}
|
||
|
||
return $domain ? $this->domain() . $this->root : $this->root;
|
||
}
|
||
|
||
/**
|
||
* 获取URL访问根目录
|
||
* @access public
|
||
* @return string
|
||
*/
|
||
public function rootUrl()
|
||
{
|
||
$base = $this->root();
|
||
$root = strpos($base, '.') ? ltrim(dirname($base), DIRECTORY_SEPARATOR) : $base;
|
||
|
||
if ('' != $root) {
|
||
$root = '/' . ltrim($root, '/');
|
||
}
|
||
|
||
return $root;
|
||
}
|
||
|
||
public function setPathinfo($pathinfo)
|
||
{
|
||
$this->pathinfo = $pathinfo;
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 获取当前请求URL的pathinfo信息(含URL后缀)
|
||
* @access public
|
||
* @return string
|
||
*/
|
||
public function pathinfo()
|
||
{
|
||
if (is_null($this->pathinfo)) {
|
||
if (isset($_GET[$this->config['var_pathinfo']])) {
|
||
// 判断URL里面是否有兼容模式参数
|
||
$pathinfo = $_GET[$this->config['var_pathinfo']];
|
||
unset($_GET[$this->config['var_pathinfo']]);
|
||
unset($this->get[$this->config['var_pathinfo']]);
|
||
} elseif ($this->isCli()) {
|
||
// CLI模式下 index.php module/controller/action/params/...
|
||
$pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
|
||
} elseif ('cli-server' == PHP_SAPI) {
|
||
$pathinfo = strpos($this->server('REQUEST_URI'), '?') ? strstr($this->server('REQUEST_URI'), '?', true) : $this->server('REQUEST_URI');
|
||
} elseif ($this->server('PATH_INFO')) {
|
||
$pathinfo = $this->server('PATH_INFO');
|
||
}
|
||
|
||
// 分析PATHINFO信息
|
||
if (!isset($pathinfo)) {
|
||
foreach ($this->config['pathinfo_fetch'] as $type) {
|
||
if ($this->server($type)) {
|
||
$pathinfo = (0 === strpos($this->server($type), $this->server('SCRIPT_NAME'))) ?
|
||
substr($this->server($type), strlen($this->server('SCRIPT_NAME'))) : $this->server($type);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!empty($pathinfo)) {
|
||
unset($this->get[$pathinfo], $this->request[$pathinfo]);
|
||
}
|
||
|
||
$this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/');
|
||
}
|
||
|
||
return $this->pathinfo;
|
||
}
|
||
|
||
/**
|
||
* 获取当前请求URL的pathinfo信息(不含URL后缀)
|
||
* @access public
|
||
* @return string
|
||
*/
|
||
public function path()
|
||
{
|
||
if (is_null($this->path)) {
|
||
$suffix = $this->config['url_html_suffix'];
|
||
$pathinfo = $this->pathinfo();
|
||
|
||
if (false === $suffix) {
|
||
// 禁止伪静态访问
|
||
$this->path = $pathinfo;
|
||
} elseif ($suffix) {
|
||
// 去除正常的URL后缀
|
||
$this->path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
|
||
} else {
|
||
// 允许任何后缀访问
|
||
$this->path = preg_replace('/\.' . $this->ext() . '$/i', '', $pathinfo);
|
||
}
|
||
}
|
||
|
||
return $this->path;
|
||
}
|
||
|
||
/**
|
||
* 当前URL的访问后缀
|
||
* @access public
|
||
* @return string
|
||
*/
|
||
public function ext()
|
||
{
|
||
return pathinfo($this->pathinfo(), PATHINFO_EXTENSION);
|
||
}
|
||
|
||
/**
|
||
* 获取当前请求的时间
|
||
* @access public
|
||
* @param bool $float 是否使用浮点类型
|
||
* @return integer|float
|
||
*/
|
||
public function time($float = false)
|
||
{
|
||
return $float ? $this->server('REQUEST_TIME_FLOAT') : $this->server('REQUEST_TIME');
|
||
}
|
||
|
||
/**
|
||
* 当前请求的资源类型
|
||
* @access public
|
||
* @return false|string
|
||
*/
|
||
public function type()
|
||
{
|
||
$accept = $this->server('HTTP_ACCEPT');
|
||
|
||
if (empty($accept)) {
|
||
return false;
|
||
}
|
||
|
||
foreach ($this->mimeType as $key => $val) {
|
||
$array = explode(',', $val);
|
||
foreach ($array as $k => $v) {
|
||
if (stristr($accept, $v)) {
|
||
return $key;
|
||
}
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 设置资源类型
|
||
* @access public
|
||
* @param string|array $type 资源类型名
|
||
* @param string $val 资源类型
|
||
* @return void
|
||
*/
|
||
public function mimeType($type, $val = '')
|
||
{
|
||
if (is_array($type)) {
|
||
$this->mimeType = array_merge($this->mimeType, $type);
|
||
} else {
|
||
$this->mimeType[$type] = $val;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 当前的请求类型
|
||
* @access public
|
||
* @param bool $origin 是否获取原始请求类型
|
||
* @return string
|
||
*/
|
||
public function method($origin = false)
|
||
{
|
||
if ($origin) {
|
||
// 获取原始请求类型
|
||
return $this->server('REQUEST_METHOD') ?: 'GET';
|
||
} elseif (!$this->method) {
|
||
if (isset($_POST[$this->config['var_method']])) {
|
||
$method = strtolower($_POST[$this->config['var_method']]);
|
||
if (in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) {
|
||
$this->method = strtoupper($method);
|
||
$this->{$method} = $_POST;
|
||
} else {
|
||
$this->method = 'POST';
|
||
}
|
||
unset($_POST[$this->config['var_method']]);
|
||
} elseif ($this->server('HTTP_X_HTTP_METHOD_OVERRIDE')) {
|
||
$this->method = strtoupper($this->server('HTTP_X_HTTP_METHOD_OVERRIDE'));
|
||
} else {
|
||
$this->method = $this->server('REQUEST_METHOD') ?: 'GET';
|
||
}
|
||
}
|
||
|
||
return $this->method;
|
||
}
|
||
|
||
/**
|
||
* 是否为GET请求
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isGet()
|
||
{
|
||
return $this->method() == 'GET';
|
||
}
|
||
|
||
/**
|
||
* 是否为POST请求
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isPost()
|
||
{
|
||
return $this->method() == 'POST';
|
||
}
|
||
|
||
/**
|
||
* 是否为PUT请求
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isPut()
|
||
{
|
||
return $this->method() == 'PUT';
|
||
}
|
||
|
||
/**
|
||
* 是否为DELTE请求
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isDelete()
|
||
{
|
||
return $this->method() == 'DELETE';
|
||
}
|
||
|
||
/**
|
||
* 是否为HEAD请求
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isHead()
|
||
{
|
||
return $this->method() == 'HEAD';
|
||
}
|
||
|
||
/**
|
||
* 是否为PATCH请求
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isPatch()
|
||
{
|
||
return $this->method() == 'PATCH';
|
||
}
|
||
|
||
/**
|
||
* 是否为OPTIONS请求
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isOptions()
|
||
{
|
||
return $this->method() == 'OPTIONS';
|
||
}
|
||
|
||
/**
|
||
* 是否为cli
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isCli()
|
||
{
|
||
return PHP_SAPI == 'cli';
|
||
}
|
||
|
||
/**
|
||
* 是否为cgi
|
||
* @access public
|
||
* @return bool
|
||
*/
|
||
public function isCgi()
|
||
{
|
||
return strpos(PHP_SAPI, 'cgi') === 0;
|
||
}
|
||
|
||
/**
|
||
* 获取当前请求的参数
|
||
* @access public
|
||
* @param mixed $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function param($name = '', $default = null, $filter = '')
|
||
{
|
||
if (!$this->mergeParam) {
|
||
$method = $this->method(true);
|
||
|
||
// 自动获取请求变量
|
||
switch ($method) {
|
||
case 'POST':
|
||
$vars = $this->post(false);
|
||
break;
|
||
case 'PUT':
|
||
case 'DELETE':
|
||
case 'PATCH':
|
||
$vars = $this->put(false);
|
||
break;
|
||
default:
|
||
$vars = [];
|
||
}
|
||
|
||
// 当前请求参数和URL地址中的参数合并
|
||
$this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false));
|
||
|
||
$this->mergeParam = true;
|
||
}
|
||
|
||
if (true === $name) {
|
||
// 获取包含文件上传信息的数组
|
||
$file = $this->file();
|
||
$data = is_array($file) ? array_merge($this->param, $file) : $this->param;
|
||
|
||
return $this->input($data, '', $default, $filter);
|
||
}
|
||
|
||
return $this->input($this->param, $name, $default, $filter);
|
||
}
|
||
|
||
/**
|
||
* 设置路由变量
|
||
* @access public
|
||
* @param array $route 路由变量
|
||
* @return $this
|
||
*/
|
||
public function setRouteVars(array $route)
|
||
{
|
||
$this->route = array_merge($this->route, $route);
|
||
return $this;
|
||
}
|
||
|
||
/**
|
||
* 获取路由参数
|
||
* @access public
|
||
* @param string|false $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function route($name = '', $default = null, $filter = '')
|
||
{
|
||
return $this->input($this->route, $name, $default, $filter);
|
||
}
|
||
|
||
/**
|
||
* 获取GET参数
|
||
* @access public
|
||
* @param string|false $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function get($name = '', $default = null, $filter = '')
|
||
{
|
||
if (empty($this->get)) {
|
||
$this->get = $_GET;
|
||
}
|
||
|
||
return $this->input($this->get, $name, $default, $filter);
|
||
}
|
||
|
||
/**
|
||
* 获取POST参数
|
||
* @access public
|
||
* @param string|false $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function post($name = '', $default = null, $filter = '')
|
||
{
|
||
if (empty($this->post)) {
|
||
$this->post = !empty($_POST) ? $_POST : $this->getInputData($this->input);
|
||
}
|
||
|
||
return $this->input($this->post, $name, $default, $filter);
|
||
}
|
||
|
||
/**
|
||
* 获取PUT参数
|
||
* @access public
|
||
* @param string|false $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function put($name = '', $default = null, $filter = '')
|
||
{
|
||
if (is_null($this->put)) {
|
||
$this->put = $this->getInputData($this->input);
|
||
}
|
||
|
||
return $this->input($this->put, $name, $default, $filter);
|
||
}
|
||
|
||
protected function getInputData($content)
|
||
{
|
||
if (false !== strpos($this->contentType(), 'json')) {
|
||
return (array) json_decode($content, true);
|
||
} elseif (strpos($content, '=')) {
|
||
parse_str($content, $data);
|
||
return $data;
|
||
}
|
||
|
||
return [];
|
||
}
|
||
|
||
/**
|
||
* 获取DELETE参数
|
||
* @access public
|
||
* @param string|false $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function delete($name = '', $default = null, $filter = '')
|
||
{
|
||
return $this->put($name, $default, $filter);
|
||
}
|
||
|
||
/**
|
||
* 获取PATCH参数
|
||
* @access public
|
||
* @param string|false $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function patch($name = '', $default = null, $filter = '')
|
||
{
|
||
return $this->put($name, $default, $filter);
|
||
}
|
||
|
||
/**
|
||
* 获取request变量
|
||
* @access public
|
||
* @param string|false $name 变量名
|
||
* @param mixed $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||
* @return mixed
|
||
*/
|
||
public function request($name = '', $default = null, $filter = '')
|
||
{
|
||
if (empty($this->request)) {
|
||
$this->request = $_REQUEST;
|
||
}
|
||
|
||
return $this->input($this->request, $name, $default, $filter);
|
||
}
|
||
|
||
/**
|
||
* 获取session数据
|
||
* @access public
|
||
* @param string $name 数据名称
|
||
* @param string $default 默认值
|
||
* @return mixed
|
||
*/
|
||
public function session($name = '', $default = null)
|
||
{
|
||
if (empty($this->session)) {
|
||
$this->session = Session::get();
|
||
}
|
||
|
||
if ('' === $name) {
|
||
return $this->session;
|
||
}
|
||
|
||
$data = $this->getData($this->session, $name);
|
||
|
||
return is_null($data) ? $default : $data;
|
||
}
|
||
|
||
/**
|
||
* 获取cookie参数
|
||
* @access public
|
||
* @param string $name 变量名
|
||
* @param string $default 默认值
|
||
* @param string|array $filter 过滤方法
|
||