forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
259 lines
8.0 KiB
259 lines
8.0 KiB
2 years ago
|
<?php
|
||
|
|
||
|
namespace app\Providers;
|
||
|
|
||
|
/**
|
||
|
* Description of WebHeader
|
||
|
*
|
||
|
* @author Lucas Awade
|
||
|
*/
|
||
|
class WebHeader
|
||
|
{
|
||
|
|
||
|
/** @class support headers * */
|
||
|
private $headers = [
|
||
|
"Access-Control-Allow-Headers",
|
||
|
"Access-Control-Allow-Methods",
|
||
|
"Access-Control-Allow-Origin",
|
||
|
"Cache-Control",
|
||
|
"Connection",
|
||
|
"Content-Description",
|
||
|
"Content-Disposition",
|
||
|
"Content-Length",
|
||
|
"Access-Control-Max-Age",
|
||
|
"Content-Transfer-Encoding",
|
||
|
"Content-Type",
|
||
|
"Expires",
|
||
|
"Pragma",
|
||
|
"Location"
|
||
|
];
|
||
|
|
||
|
/** @codes https://http.cat/ * */
|
||
|
const HTTP_CODE_RESPONSE = [
|
||
|
100 => 'Continue',
|
||
|
101 => 'Switching Protocols',
|
||
|
102 => 'Processing',
|
||
|
200 => 'OK',
|
||
|
201 => 'Created',
|
||
|
202 => 'Accepted',
|
||
|
204 => 'No Content',
|
||
|
206 => 'Partial Content',
|
||
|
207 => 'Multi-Status',
|
||
|
300 => 'Multiple Choices',
|
||
|
301 => 'Moves Permanently',
|
||
|
302 => 'Found',
|
||
|
303 => 'See Other',
|
||
|
304 => 'Not Modified',
|
||
|
305 => 'Use Proxy',
|
||
|
307 => 'Temporary Redirect',
|
||
|
308 => 'Permanent Redirect',
|
||
|
400 => 'Bad Request',
|
||
|
401 => 'Unauthorized',
|
||
|
402 => 'Payment Required',
|
||
|
403 => 'Forbidden',
|
||
|
404 => 'Not Found',
|
||
|
405 => 'Method Not Allowed',
|
||
|
406 => 'Not Acceptable',
|
||
|
408 => 'Request Timeout',
|
||
|
409 => 'Conflict',
|
||
|
410 => 'Gone',
|
||
|
411 => 'Length Required',
|
||
|
412 => 'Precondition Failed',
|
||
|
413 => 'Payload Too Large',
|
||
|
414 => 'Request-URI Too Long',
|
||
|
415 => 'Unsupported Media Type',
|
||
|
416 => 'Request Range Not Satisfiable',
|
||
|
417 => 'Expectation Failed',
|
||
|
418 => 'Im a teapot',
|
||
|
420 => 'Enhance Your Calm',
|
||
|
421 => 'Misdirected Request',
|
||
|
422 => 'Unprocessable Entity',
|
||
|
423 => 'Locked',
|
||
|
424 => 'Failed Dependency',
|
||
|
425 => 'Too Early',
|
||
|
426 => 'Upgrade Required',
|
||
|
429 => 'Too Many Requests',
|
||
|
431 => 'Request Header Fields Too Large',
|
||
|
444 => 'No Response',
|
||
|
450 => 'Blocked by Windows Parental Controls',
|
||
|
451 => 'Unavailable For Legal Reasons',
|
||
|
499 => 'Client Closed Request',
|
||
|
500 => 'Internal Server Error',
|
||
|
501 => 'Not Implemented',
|
||
|
502 => 'Bad Gateway',
|
||
|
503 => 'Service Unavailable',
|
||
|
504 => 'Gateway Timeout',
|
||
|
506 => 'Variant Also Negotiates',
|
||
|
507 => 'Insufficient Storage',
|
||
|
508 => 'Loop Detected',
|
||
|
509 => 'Bandwidth Limit Exceeded',
|
||
|
510 => 'Not Extended',
|
||
|
511 => 'Network Authentication Required',
|
||
|
599 => 'Network Connect Timeout Error'
|
||
|
];
|
||
|
|
||
|
/**
|
||
|
* This variable set methods to use headers
|
||
|
* @var $methods
|
||
|
*/
|
||
|
private $methods = [];
|
||
|
|
||
|
/**
|
||
|
* This variable for logs
|
||
|
* @var $log
|
||
|
*/
|
||
|
private $log;
|
||
|
|
||
|
/**
|
||
|
* This variable for set new configs.
|
||
|
* @var $config
|
||
|
*/
|
||
|
private $config;
|
||
|
|
||
|
########################################################################
|
||
|
##### CLASS METHODS #####
|
||
|
########################################################################
|
||
|
|
||
|
function __construct($config = null)
|
||
|
{
|
||
|
$this->config($config);
|
||
|
$this->blockRequest();
|
||
|
}
|
||
|
|
||
|
public function config($config)
|
||
|
{
|
||
|
if ($config) {
|
||
|
foreach ($config as $key => $val) {
|
||
|
$this->config[strtoupper($key)] = $val;
|
||
|
}
|
||
|
}
|
||
|
return $this->config;
|
||
|
}
|
||
|
|
||
|
public function API($contentType)
|
||
|
{
|
||
|
$this->methods([
|
||
|
"Access-Control-Allow-Origin" => ['*'],
|
||
|
"Content-Type" => $contentType,
|
||
|
"Access-Control-Allow-Methods" => ['GET', 'POST', 'PUT', 'DELETE'],
|
||
|
"Access-Control-Max-Age" => 0,
|
||
|
"Access-Control-Allow-Headers" => ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'Authorization']
|
||
|
]);
|
||
|
$this->bootstrap();
|
||
|
}
|
||
|
|
||
|
public function response($code)
|
||
|
{
|
||
|
if (array_key_exists($code, self::HTTP_CODE_RESPONSE)) {
|
||
|
header("HTTP/1.0 {$code} " . self::HTTP_CODE_RESPONSE[$code]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function redirect($url = null)
|
||
|
{
|
||
|
// $redirect = ($url ? $url : $this->config['REDIRECT']);
|
||
|
// if (strpos('http://', $redirect) === false && strpos('https://', $redirect) === false) {
|
||
|
// $redirect = "http://$redirect";
|
||
|
// }
|
||
|
$redirect = $url ? $url : CONF_MIDDLEWARE_REDIRECT;
|
||
|
$this->methods([
|
||
|
"Location" => ($redirect)
|
||
|
]);
|
||
|
$this->bootstrap();
|
||
|
}
|
||
|
|
||
|
public function fileTransfer($name, $file, $mimetype)
|
||
|
{
|
||
|
//logger('logggeeeee')->info(basename($name . "." . explode('/', $mimetype)[1]));
|
||
|
$this->methods([
|
||
|
"Content-Description" => 'File Transfer',
|
||
|
"Content-Transfer-Encoding" => "binary",
|
||
|
"Content-Type" => $mimetype,
|
||
|
"Cache-Control" => "must-revalidate",
|
||
|
"Content-Length" => filesize($file),
|
||
|
"Content-Disposition" => "attachment; filename=" . basename($name . "." . explode('/', $mimetype)[1]),
|
||
|
"Expires" => 0,
|
||
|
"Connection" => 'close',
|
||
|
"Pragma" => 'public'
|
||
|
]);
|
||
|
$this->bootstrap();
|
||
|
ob_end_clean();
|
||
|
ob_start();
|
||
|
readfile($file);
|
||
|
ob_flush();
|
||
|
}
|
||
|
|
||
|
########################################################################
|
||
|
##### PRIVATE METHODS #####
|
||
|
########################################################################
|
||
|
|
||
|
private function bootstrap($header = null)
|
||
|
{
|
||
|
if (!$header && !$this->getMethods()) {
|
||
|
return null;
|
||
|
}
|
||
|
foreach ($header as $key => $val) {
|
||
|
if (!in_array($key, $this->headers)) {
|
||
|
array_push($this->headers, $key);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
$this->methods($header);
|
||
|
$this->headers();
|
||
|
$this->clean();
|
||
|
}
|
||
|
|
||
|
private function blockRequest()
|
||
|
{
|
||
|
if ($this->config['BLOCK_REQUEST']) {
|
||
|
if (is_array($this->config['BLOCK_REQUEST'])) {
|
||
|
if (in_array($_SERVER['REMOTE_ADDR'], $this->config['BLOCK_REQUEST'])) {
|
||
|
$this->log->info("BLOCK REQUEST: " . $_SERVER['REMOTE_ADDR']);
|
||
|
$this->response(301);
|
||
|
$this->redirect(($this->config['REDIRECT'] ? $this->config['REDIRECT'] : 'index.php'));
|
||
|
exit(0);
|
||
|
}
|
||
|
} else if ($_SERVER['REMOTE_ADDR'] == $this->config['BLOCK_REQUEST']) {
|
||
|
$this->log->info("BLOCK REQUEST: " . $_SERVER['REMOTE_ADDR']);
|
||
|
$this->response(301);
|
||
|
$this->redirect($this->config['REDIRECT']);
|
||
|
exit(0);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function headers()
|
||
|
{
|
||
|
foreach ($this->methods as $key => $header) {
|
||
|
if (in_array($key, $this->headers)) {
|
||
|
header("{$key}: {$header}");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function methods($header)
|
||
|
{
|
||
|
foreach ($header as $key => $val) {
|
||
|
$method = str_replace(' ', '-', ucwords(str_replace('-', ' ', $key)));
|
||
|
if (is_array($val)) {
|
||
|
$this->methods[$method] = implode(',', $val);
|
||
|
} else {
|
||
|
$this->methods[$method] = $val;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function clean()
|
||
|
{
|
||
|
unset($this->methods);
|
||
|
}
|
||
|
|
||
|
########################################################################
|
||
|
##### GETS AND SETTERS #####
|
||
|
########################################################################
|
||
|
|
||
|
function getMethods()
|
||
|
{
|
||
|
return $this->methods;
|
||
|
}
|
||
|
}
|