232 lines
5.7 KiB
PHP
Executable File
232 lines
5.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace RR;
|
|
|
|
use RR\ParamType;
|
|
|
|
/**
|
|
* Class to abstract the input (GET PARAMS, POST, etc...) value grabbing
|
|
* POST always have precedence
|
|
*/
|
|
class Request
|
|
{
|
|
|
|
/**
|
|
* Search a param on the GET/POST array
|
|
* If the key is not found we throw a `required_param_not_found` Exception
|
|
*
|
|
* @param strig $paramname
|
|
* @param ParamType $type
|
|
* @param Array $options
|
|
*
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public static function requiredParam(
|
|
string $parname,
|
|
ParamType $type = ParamType::INT,
|
|
$options = Array()
|
|
): mixed {
|
|
if (isset($_POST[$parname])) {
|
|
$param = $_POST[$parname];
|
|
} else if (isset($_GET[$parname])) {
|
|
$param = $_GET[$parname];
|
|
} else {
|
|
throw new \Exception('required_param_not_found');
|
|
}
|
|
/*$param = self::filter($type, $param);
|
|
|
|
foreach ($options as $key => $option){
|
|
self::$key($param, $option);
|
|
}*/
|
|
return $param;
|
|
}
|
|
|
|
/**
|
|
* Search a array param on the GET/POST array
|
|
* If the key is not found, we throw a `required_param_not_found` Exception
|
|
*
|
|
* @param strig $paramname
|
|
* @param ParamType $type
|
|
* @param Array $options
|
|
*
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public static function requiredParamArray(
|
|
string $parname,
|
|
$options = Array()
|
|
): mixed {
|
|
if (isset($_POST[$parname])) {
|
|
$param = $_POST[$parname];
|
|
} else if (isset($_GET[$parname])) {
|
|
$param = $_GET[$parname];
|
|
} else {
|
|
throw new \Exception('required_param_not_found');
|
|
}
|
|
return $param;
|
|
}
|
|
|
|
/**
|
|
* Search a array param on the GET/POST array
|
|
* If the key is not found we return the default value
|
|
*
|
|
* If the default $value type is not the same of the $type
|
|
* Wwe throw a `default_value_type_missmatch` Exception
|
|
*
|
|
* @param strig $paramname
|
|
* @param ParamType $type
|
|
* @param mixed $default
|
|
* @param Array $options
|
|
*
|
|
* @return mixed
|
|
* @throws \Exception
|
|
*/
|
|
public static function optionalParam(
|
|
string $parname,
|
|
ParamType $type = ParamType::INT,
|
|
mixed $default = "",
|
|
$options = Array(),
|
|
): mixed {
|
|
if (isset($_POST[$parname])) {
|
|
$param = $_POST[$parname];
|
|
} else if (isset($_GET[$parname])) {
|
|
$param = $_GET[$parname];
|
|
} else {
|
|
return $default;
|
|
}
|
|
return $param;
|
|
}
|
|
|
|
/**
|
|
* NOT IMPLEMENTED YET
|
|
* Search a array param on the GET/POST array
|
|
*
|
|
* @param strig $paramname
|
|
* @param Array $default
|
|
* @param Array $options
|
|
*
|
|
* @todo Implement
|
|
*
|
|
* @return Array
|
|
*/
|
|
public static function optionalParamArray(
|
|
string $parname,
|
|
Array $default = Array(),
|
|
$options = Array()
|
|
): Array{
|
|
// Not implemented yet
|
|
return $default;
|
|
}
|
|
|
|
/**
|
|
* Try to detect if the request came from AJAX/FETCH/POSTMAN
|
|
* in this cases, we might want to return something else
|
|
*
|
|
* @return bool
|
|
*/
|
|
public static function detectAjaxRequest(): bool
|
|
{
|
|
if (
|
|
isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
|
|
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
if (
|
|
isset($_SERVER['CONTENT_TYPE']) &&
|
|
str_contains(
|
|
strtolower($_SERVER['CONTENT_TYPE']),
|
|
'application/json'
|
|
)
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
// Detecta ferramentas conhecidas
|
|
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
|
|
|
|
if (
|
|
str_contains($userAgent, 'postman') ||
|
|
str_contains($userAgent, 'curl') ||
|
|
str_contains($userAgent, 'insomnia') ||
|
|
str_contains($userAgent, 'python') ||
|
|
str_contains($userAgent, 'java')
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
// Headers comuns de navegadores modernos
|
|
if (
|
|
isset($_SERVER['HTTP_SEC_FETCH_SITE']) ||
|
|
isset($_SERVER['HTTP_SEC_FETCH_MODE']) ||
|
|
isset($_SERVER['HTTP_REFERER'])
|
|
) {
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $paramname
|
|
* @param ParamType $type
|
|
* @param bool $cast
|
|
*
|
|
* @todo Implement casting
|
|
* @todo Review this logic, I don'r remember what it does
|
|
*
|
|
* @return mixed
|
|
private static function filter(
|
|
$paramname = "",
|
|
ParamType $type = ParamType::INT,
|
|
$cast = true
|
|
): mixed {
|
|
switch ($type){
|
|
case ParamType::INT:
|
|
if(is_integer($paramname)){
|
|
return $paramname;
|
|
}
|
|
break;
|
|
case ParamType::DOUBLE:
|
|
if(is_double($paramname)){
|
|
return $paramname;
|
|
}
|
|
break;
|
|
case ParamType::ALPHA:
|
|
return $paramname;
|
|
break;
|
|
case ParamType::ALPHANUM:
|
|
return $paramname;
|
|
break;
|
|
}
|
|
}
|
|
*/
|
|
|
|
/**
|
|
* NOT IMPLEMENTED YET
|
|
* Check if the numeric value is in a number range
|
|
*
|
|
* Examples:
|
|
* From 10 to 35 = inRange($value, "10-35")
|
|
* From 45 to infinity = inRange($value, "45-")
|
|
* All numbers below 0 = inRange($value, "-0")
|
|
*
|
|
* @todo Not Implemented yet
|
|
*
|
|
* @param int|float $value
|
|
* @param string $range
|
|
*
|
|
* @return bool
|
|
*/
|
|
private static function inRange(
|
|
int|float $value,
|
|
string $range
|
|
): bool {
|
|
return true;
|
|
}
|
|
|
|
}
|