microphp/Core/Request.php

147 lines
4.3 KiB
PHP
Raw Normal View History

2023-04-02 10:00:20 +03:00
<?php
include_once "Reflection.php";
include_once "RequestHeader.php";
include_once "RequestFile.php";
include_once "Session.php";
class Request
{
use ReflectionHook;
public static $header;
public static $cookie;
public static $method;
public static $session;
public static $contentType;
public static $file;
2023-04-07 01:02:39 +03:00
public static $url;
public static $accepts;
public static $ip;
public static $isajax;
2023-04-02 10:00:20 +03:00
public static $data = [];
public static $query = [];
public static $ready = false;
2023-04-02 14:10:58 +03:00
public function __construct()
2023-04-02 10:00:20 +03:00
{
if(!Request::$ready)
{
Request::$header = new RequestHeader();
Request::$cookie = new RequestCookie();
Request::$file = new RequestFile();
Request::$session = new Session();
Request::$method = strtolower($_SERVER["REQUEST_METHOD"]);
Request::$contentType = strtolower($_SERVER["CONTENT_TYPE"] ?? "");
Request::$ready = true;
2023-04-07 01:02:39 +03:00
Request::$url = $_SERVER['REQUEST_URI'];
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
Request::$ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
}else{
Request::$ip = $_SERVER['REMOTE_ADDR'];
};
Request::$accepts = explode(',', $_SERVER['HTTP_ACCEPT']);
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
Request::$isajax = true;
}else{
if(
in_array("application/json", Request::$accepts) || in_array("text/json", Request::$accepts)
)
{
Request::$isajax = true;
}
};
2023-04-02 10:00:20 +03:00
$this->mutateRequest();
}
}
2023-04-02 14:10:58 +03:00
public function getJSONRequest()
2023-04-02 10:00:20 +03:00
{
return json_decode(file_get_contents('php://input'), true);
}
2023-04-02 14:10:58 +03:00
public function mutateRequest()
2023-04-02 10:00:20 +03:00
{
if(Request::$contentType == "application/json")
{
Request::$data = $this->getJSONRequest();
}
else
{
if(Request::$method == "post")
{
Request::$data = $_POST;
}
Request::$query = $_GET;
};
}
2023-04-02 14:10:58 +03:00
public function contentType()
2023-04-02 10:00:20 +03:00
{
return Request::$contentType;
}
2023-04-02 14:10:58 +03:00
public function method()
2023-04-02 10:00:20 +03:00
{
return Request::$method;
}
2023-04-02 14:10:58 +03:00
public function get($name)
2023-04-02 10:00:20 +03:00
{
return Request::$query[$name] ?? null;
}
2023-04-02 14:10:58 +03:00
public function has($name)
2023-04-02 10:00:20 +03:00
{
return $this->input($name) !== null;
}
2023-04-02 14:10:58 +03:00
public function post($name)
2023-04-02 10:00:20 +03:00
{
return Request::$data[$name] ?? null;
}
2023-04-02 14:10:58 +03:00
public function input($name)
2023-04-02 10:00:20 +03:00
{
return Request::staticGet($name) ?? Request::staticPost($name) ?? null;
}
2023-04-02 14:10:58 +03:00
public function staticContentType()
2023-04-02 10:00:20 +03:00
{
return $this->contentType();
}
2023-04-02 14:10:58 +03:00
public function staticMethod()
2023-04-02 10:00:20 +03:00
{
return $this->method();
}
2023-04-02 14:10:58 +03:00
public function staticGet($name)
2023-04-02 10:00:20 +03:00
{
return $this->get($name);
}
2023-04-02 14:10:58 +03:00
public function staticHeaders()
2023-04-02 10:00:20 +03:00
{
return Request::$headers;
}
2023-04-02 14:10:58 +03:00
public function staticFile()
2023-04-02 10:00:20 +03:00
{
return Request::$file;
}
2023-04-02 14:10:58 +03:00
public function staticSession()
2023-04-02 10:00:20 +03:00
{
return Request::$session;
}
2023-04-02 14:10:58 +03:00
public function staticCookie()
2023-04-02 10:00:20 +03:00
{
return Request::$cookie;
}
2023-04-02 14:10:58 +03:00
public function staticPost($name)
2023-04-02 10:00:20 +03:00
{
return $this->post($name);
}
2023-04-02 14:10:58 +03:00
public function staticInput($name)
2023-04-02 10:00:20 +03:00
{
return $this->input($name);
}
2023-04-02 14:10:58 +03:00
public function staticHas($name)
2023-04-02 10:00:20 +03:00
{
return $this->has($name);
}
2023-04-02 14:10:58 +03:00
public function getAttribute($name)
2023-04-02 10:00:20 +03:00
{
return Request::staticInput($name);
}
2023-04-07 01:02:39 +03:00
};
define("request", new Request(), false);