ArcaniSGK507

Untitled

Mar 17th, 2025
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.04 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. /**
  6.  * Last Hammer Framework 2.0
  7.  * PHP Version 8.3 (Required).
  8.  *
  9.  * @see https://github.com/arcanisgk/LH-Framework
  10.  *
  11.  * @author    Walter Nuñez (arcanisgk/original founder) <[email protected]>
  12.  * @copyright 2017 - 2024
  13.  * @license   http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  14.  * @note      This program is distributed in the hope that it will be useful
  15.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16.  * or FITNESS FOR A PARTICULAR PURPOSE.
  17.  */
  18.  
  19. namespace Asset\Framework\Http;
  20.  
  21. use Asset\Framework\Core\Variable;
  22. use Asset\Framework\Trait\SingletonTrait;
  23. use JetBrains\PhpStorm\NoReturn;
  24.  
  25. /**
  26.  * Class that handles: Controller Request
  27.  *
  28.  * @package Asset\Framework\Http;
  29.  */
  30. class Request
  31. {
  32.     use SingletonTrait;
  33.  
  34.     private const array BOOLEAN_STRINGS = ['true', 'false'];
  35.  
  36.     private const array VALID_GLOBALS = ['POST', 'GET', 'SERVER'];
  37.  
  38.     /**
  39.      * @var array
  40.      */
  41.     private array $superGlobals = [];
  42.  
  43.     /**
  44.      *
  45.      */
  46.     public function __construct()
  47.     {
  48.         $this->initializeSuperGlobals();
  49.     }
  50.  
  51.     /**
  52.      * @return void
  53.      */
  54.     private function initializeSuperGlobals(): void
  55.     {
  56.         foreach (self::VALID_GLOBALS as $global) {
  57.             $this->superGlobals[$global] = $GLOBALS["_$global"] ?? [];
  58.         }
  59.     }
  60.  
  61.     /**
  62.      * @return $this
  63.      */
  64.     public function CleanSuperGlobal(): self
  65.     {
  66.         foreach (['POST', 'GET'] as $global) {
  67.             if (empty($this->superGlobals[$global])) {
  68.                 unset($GLOBALS["_$global"]);
  69.                 continue;
  70.             }
  71.             $GLOBALS["_$global"] = $this->evaluateSuperGlobal($this->superGlobals[$global]);
  72.         }
  73.  
  74.         return $this;
  75.     }
  76.  
  77.     /**
  78.      * @param $data
  79.      * @return mixed
  80.      */
  81.     private function evaluateSuperGlobal($data): mixed
  82.     {
  83.         if (!is_array($data)) {
  84.             return $this->processSingleValue($data);
  85.         }
  86.  
  87.         $result = [];
  88.         foreach ($data as $key => $value) {
  89.             if (is_array($value)) {
  90.                 $result[$key] = $this->evaluateSuperGlobal($value);
  91.                 continue;
  92.             }
  93.  
  94.             if (Variable::getInstance()->isJson($value)) {
  95.                 $result[$key] = $this->evaluateSuperGlobal(json_decode($value, true));
  96.                 continue;
  97.             }
  98.  
  99.             $result[$key] = $this->processSingleValue($value);
  100.         }
  101.  
  102.         return $result;
  103.     }
  104.  
  105.     /**
  106.      * @param mixed $value
  107.      * @return mixed
  108.      */
  109.     private function processSingleValue(mixed $value): mixed
  110.     {
  111.         if (is_string($value)) {
  112.             if ($this->isStringBoolean($value)) {
  113.                 return filter_var($value, FILTER_VALIDATE_BOOLEAN);
  114.             }
  115.  
  116.             if (is_numeric($value)) {
  117.                 return $this->convertNumericString($value);
  118.             }
  119.  
  120.             return self::sanitizeInput($value);
  121.         }
  122.  
  123.         return $value;
  124.     }
  125.  
  126.     /**
  127.      * @param string $value
  128.      * @return bool
  129.      */
  130.     private function isStringBoolean(string $value): bool
  131.     {
  132.         return in_array(strtolower($value), self::BOOLEAN_STRINGS, true);
  133.     }
  134.  
  135.     /**
  136.      * @param string $value
  137.      * @return int|float|string
  138.      */
  139.     private function convertNumericString(string $value): int|float|string
  140.     {
  141.         if (filter_var($value, FILTER_VALIDATE_INT) !== false) {
  142.             return (int)$value;
  143.         }
  144.  
  145.         if (filter_var($value, FILTER_VALIDATE_FLOAT) !== false) {
  146.             return (float)$value;
  147.         }
  148.  
  149.         return $value;
  150.     }
  151.  
  152.     /**
  153.      * @param mixed $input
  154.      * @return string
  155.      */
  156.     public static function sanitizeInput(mixed $input): string
  157.     {
  158.         return trim(
  159.             htmlspecialchars(
  160.                 htmlentities(
  161.                     addslashes(
  162.                         strip_tags((string)$input)
  163.                     ),
  164.                     ENT_QUOTES,
  165.                     'UTF-8'
  166.                 ),
  167.                 ENT_QUOTES,
  168.                 'UTF-8'
  169.             )
  170.         );
  171.     }
  172.  
  173.     /**
  174.      * @param string $location
  175.      * @return void
  176.      */
  177.     #[NoReturn]
  178.    public function redirect(string $location): void
  179.     {
  180.         header("Location: $location");
  181.         exit;
  182.     }
  183.  
  184.     /**
  185.      * @param string $location
  186.      * @return void
  187.      */
  188.     #[NoReturn]
  189.    public function redirectToUri(string $location): void
  190.     {
  191.         header("Content-Type: application/json");
  192.         echo json_encode(['nav' => $location]);
  193.         exit;
  194.     }
  195.  
  196.     /**
  197.      * @return void
  198.      */
  199.     public function ContentType(): void
  200.     {
  201.         $_SERVER["CONTENT_TYPE"] = $this->getServer("CONTENT_TYPE") ? trim($this->getServer("CONTENT_TYPE")) : null;
  202.     }
  203.  
  204.     /**
  205.      * @param string|null $key
  206.      * @return mixed
  207.      */
  208.     public function getServer(?string $key = null): mixed
  209.     {
  210.         return $this->getGlobalValue('SERVER', $key);
  211.     }
  212.  
  213.     /**
  214.      * @param string $global
  215.      * @param string|null $key
  216.      * @return mixed
  217.      */
  218.     private function getGlobalValue(string $global, ?string $key): mixed
  219.     {
  220.         if ($key === null) {
  221.             return $GLOBALS["_$global"] ?? [];
  222.         }
  223.  
  224.         return $GLOBALS["_$global"][$key] ?? null;
  225.     }
  226.  
  227.     /**
  228.      * @return bool
  229.      */
  230.     public function isAjax(): bool
  231.     {
  232.         return strtolower($this->getServer('HTTP_X_REQUESTED_WITH') ?? '') === 'xmlhttprequest';
  233.     }
  234.  
  235.     /**
  236.      * @param string|null $key
  237.      * @return mixed
  238.      */
  239.     public function getPost(?string $key = null): mixed
  240.     {
  241.         return $this->getGlobalValue('POST', $key);
  242.     }
  243.  
  244.     /**
  245.      * @param string $key
  246.      * @return bool
  247.      */
  248.     public function hasPost(string $key): bool
  249.     {
  250.         return isset($this->superGlobals['POST'][$key]);
  251.     }
  252.  
  253.     /**
  254.      * @param string|null $key
  255.      * @return mixed
  256.      */
  257.     public function getGet(?string $key = null): mixed
  258.     {
  259.         //ex_c($_GET);
  260.  
  261.         return $this->getGlobalValue('GET', $key);
  262.     }
  263.  
  264.     /**
  265.      * @param string $key
  266.      * @return bool
  267.      */
  268.     public function hasGet(string $key): bool
  269.     {
  270.         return isset($this->superGlobals['GET'][$key]);
  271.     }
  272.  
  273.     /**
  274.      * @param string $key
  275.      * @return bool
  276.      */
  277.     public function hasServer(string $key): bool
  278.     {
  279.         return isset($this->superGlobals['SERVER'][$key]);
  280.     }
  281.  
  282.     /**
  283.      * @param string $node
  284.      * @param array $params
  285.      * @return string
  286.      */
  287.     public function buildUrl(string $node, array $params = []): string
  288.     {
  289.         $protocol = CONFIG->app->host->getProtocol();
  290.         $domain   = CONFIG->app->host->getDomain();
  291.         $baseUrl  = sprintf('%s://%s/%s', $protocol, $domain, $node);
  292.  
  293.         if (empty($params)) {
  294.             return $baseUrl;
  295.         }
  296.  
  297.         return $baseUrl.'/'.str_replace('&', '&', http_build_query($params, '', '&'));
  298.     }
  299. }
Add Comment
Please, Sign In to add comment