<?php
namespace ticky;
use ticky\helper\url;
class request {
protected $filter;
protected $input;
protected function __construct($options = array()) {
foreach ($options as $name => $item) {
if (property_exists($this, $name)) {
$this->$name = $item;
}
}
if (is_null($this->filter)) {
$this->filter = '';
}
$this->input = file_get_contents('php://input');
}
public static function isGet() {
return self::getMethod() === 'GET';
}
public static function isPost() {
return self::getMethod() === 'POST';
}
public static function isOptions() {
return self::getMethod() === 'OPTIONS';
}
public static function isHead() {
return self::getMethod() === 'HEAD';
}
public static function isDelete() {
return self::getMethod() === 'DELETE';
}
public static function isPut() {
return self::getMethod() === 'PUT';
}
public static function isPatch() {
return self::getMethod() === 'PATCH';
}
public static function isTrace() {
return self::getMethod() === 'TRACE';
}
public static function isAjax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
}
public static function isPjax() {
return self::isAjax() && !empty($_SERVER['HTTP_X_PJAX']);
}
public static function isFlash() {
return isset($_SERVER['HTTP_USER_AGENT']) &&
(stripos($_SERVER['HTTP_USER_AGENT'], 'Shockwave') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'Flash') !== false);
}
public static function isCLI() {
return PHP_SAPI === 'cli';
}
public static function isWin() {
return strstr(PHP_OS, 'WIN');
}
public static function isLinux() {
return strstr(PHP_OS, 'Linux');
}
public static function isWeChat() {
return strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
}
public static function getMethod() {
if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
return strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
}
if (isset($_SERVER['REQUEST_METHOD'])) {
return strtoupper($_SERVER['REQUEST_METHOD']);
}
return 'GET';
}
public static function getParam($name, $default = null) {
if (self::isCLI()) {
$params = array_filter($_SERVER['argv']);
array_shift($params);
foreach ($params as $k => $param) {
if ($param[0] == '-' && $k % 2 == 0) {
$input[$params[$k][1]] = $params[$k + 1];
}
}
} else {
$method = strtoupper(self::getMethod());
switch ($method) {
case 'GET':
$input = $_GET;
break;
case 'POST':
$input = $_POST;
break;
default:
return null;
}
}
if (!$name) {
return $input;
}
$value = isset($input[$name]) ? $input[$name] : "";
if (!isset($default)) {
return $value;
}
gettype($default) == 'unknown type' or settype($value, gettype($default));
return isset($value) ? $value : $default;
}
public static function getUA() {
return isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
}
public static function getUri() {
return isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
}
public static function getUrl($isAbsolute = false) {
$uri = self::getUri();
if ($isAbsolute) {
$protocol = self::getScheme() . '://';
return $protocol . $_SERVER['HTTP_HOST'] . self::getUri();
} else {
return $uri;
}
}
public static function getScheme() {
return url::getScheme($_SERVER['SERVER_NAME']);
}
public static function getHost() {
return url::getHost(self::getUrl());
}
public static function getDomain() {
return url::getDomain(self::getUrl());
}
public static function getPath() {
return url::getPath(self::getUrl());
}
public static function getQuery() {
return url::getQuery(self::getUrl());
}
public static function getExtension() {
return url::getExtension(self::getUrl(false));
}
public static function getTime($float = false) {
return $float ? $_SERVER['REQUEST_TIME_FLOAT'] : $_SERVER['REQUEST_TIME'];
}
public static function getServerIp() {
if (isset($_SERVER)) {
if ($_SERVER['SERVER_ADDR']) {
$serverIp = $_SERVER['SERVER_ADDR'];
} else {
$serverIp = $_SERVER['LOCAL_ADDR'];
}
} else {
$serverIp = getenv('SERVER_ADDR');
}
return filter_var($serverIp, FILTER_VALIDATE_IP) !== false ? $serverIp : '';
}
public static function getClientIp() {
if (isset($_SERVER['HTTP_CLIENT_IP'])) {
$clientIp = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$clientIp = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$clientIp = $_SERVER['REMOTE_ADDR'];
} else {
$clientIp = '0.0.0.0';
}
if ($clientIp == "::1") {
return '0.0.0.0';
} else {
return filter_var($clientIp, FILTER_VALIDATE_IP) !== false ? $clientIp : '0.0.0.0';
}
}
public static function getReffer() {
return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
}
public static function get($name, $default = null) {
if (self::isCLI()) {
$params = array_filter($_SERVER['argv']);
array_shift($params);
foreach ($params as $k => $param) {
if ($param[0] == '-' && $k % 2 == 0) {
$input[$params[$k][1]] = $params[$k + 1];
}
}
} else {
$input = $_GET;
}
if (!$name) {
return $input;
}
if (isset($input[$name]) && $input[$name]) {
$value = $input[$name];
}
if (!isset($default)) {
return isset($value) ? $value : $default;
}
if (!isset($value)) {
$value = $default;
}
gettype($default) == 'unknown type' or settype($value, gettype($default));
return isset($value) ? $value : $default;
}
public static function post($name, $default = null) {
$input = $_POST;
if (!$name) {
return $input;
}
if (isset($input[$name]) && $input[$name]) {
$value = $input[$name];
}
if (!isset($default)) {
return isset($value) ? $value : $default;
}
if (!isset($value)) {
$value = $default;
}
gettype($default) == 'unknown type' or settype($value, gettype($default));
return isset($value) ? $value : $default;
}
public static function put() {
$_PUT = array();
if (self::getMethod() === "PUT") {
parse_str(file_get_contents('php://input', false, null, -1, $_SERVER['CONTENT_LENGTH']), $_PUT);
}
return $_PUT;
}
public static function file($name = '') {
$file = isset($_FILES) ? $_FILES : array();
if (is_array($name)) {
return $file = array_merge($file, $name);
}
$files = $file;
if (!empty($files)) {
$array = array();
foreach ($files as $key => $file) {
if (is_array($file['name'])) {
$item =[];
$keys = array_keys($file);
$count = count($file['name']);
for ($i = 0; $i < $count; $i++) {
if (empty($file['tmp_name'][$i]) || !is_file($file['tmp_name'][$i])) {
continue;
}
$temp['key'] = $key;
foreach ($keys as $_key) {
$temp[$_key] = $file[$_key][$i];
}
$item[] = (new upload($temp['tmp_name']))->setUploadInfo($temp);
}
$array[$key] = $item;
} else {
if ($file instanceof File) {
$array[$key] = $file;
} else {
if (empty($file['tmp_name']) || !is_file($file['tmp_name'])) {
continue;
}
$array[$key] = (new upload($file['tmp_name']))->setUploadInfo($file);
}
}
}
if (strpos($name, '.')) {
list($name, $sub) = explode('.', $name);
}
if ('' === $name) {
return $array;
} elseif (isset($sub) && isset($array[$name][$sub])) {
return $array[$name][$sub];
} elseif (isset($array[$name])) {
return $array[$name];
}
}
}
}