*/ // Déclaration du namespace namespace LaswitchTech\Core; // Import de classes supplémentaires dans l’espace de noms global use Exception; class Request { // Propriétés private $Get; private $Post; private $Files; private $Server; private $Cookie; private $Request; private $Arguments; /** * Constructeur */ public function __construct(){ // Importer les variables globales global $_GET, $_POST, $_FILES, $_SERVER, $_COOKIE, $_REQUEST, $argv; // Définir les propriétés $this->Get = $_GET; $this->Post = $_POST; $this->Files = $_FILES; $this->Server = $_SERVER; $this->Cookie = $_COOKIE; $this->Request = $_REQUEST; $this->Arguments = $argv ?? []; // Nettoyer les variables globales unset($_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_REQUEST, $argv); } /** * Récupérer l’hôte */ public function getHost() { return $this->Server['HTTP_HOST'] ?? ''; } /** * Récupérer le préfixe SSL */ public function getHostSSL() { return $this->Server['HTTPS'] == 'on' ? 'https://' : 'http://'; } /** * Récupérer l’adresse complète de l’hôte */ public function getHostAddress() { return defined('STDIN') ? 'localhost' : $this->getHostSSL() . $this->getHost(); } /** * Récupérer l’URI * @return string */ public function getUri() { return parse_url($this->Server['REQUEST_URI'] ?? '', PHP_URL_PATH); } /** * Récupérer les segments de l’URI * @return array */ public function getUriSegments() { return array_filter(explode( '/', $this->getUri())); } /** * Récupérer et retourner la propriété Namespace */ public function getNamespace(){ $namespace = ""; foreach($this->getUriSegments() as $Segment){ $namespace .= "/{$Segment}"; }; return $namespace; } /** * Récupérer la méthode de requête * @return string */ public function getMethod() { return $this->Server["REQUEST_METHOD"] ?? 'GET'; } /** * Récupérer la chaîne de requête * @return string */ public function getQueryString() { return $this->Server['QUERY_STRING'] ?? ''; } /** * Récupérer les paramètres * @param string $type * @param string $key * @return mixed */ public function getParams($type, $key = null){ switch(strtoupper($type)){ case 'GET': $array = $this->Get; break; case 'POST': $array = $this->Post; break; case 'FILES': $array = $this->Files; break; case 'COOKIE': $array = $this->Cookie; break; case 'SERVER': $array = $this->Server; break; case 'QUERY': parse_str($this->Server['QUERY_STRING'], $array); break; case 'REQUEST': $array = $this->Request; break; default: return null; } return !is_null($key) ? ($array[$key] ?? null) : $array; } /** * Décoder les données de REQUEST * @param string $string * @return string */ public function decode($string){ return urldecode(base64_decode($string)); } /** * Récupérer les arguments * @param int $index * @return mixed */ public function getArguments($index = null){ return !is_null($index) ? ($this->Arguments[$index] ?? null) : $this->Arguments; } /** * Demander une entrée utilisateur * @param string $string * @param array|int|string $options * @param string $default * @return string */ public function request($string, $options = null, $default = null){ if(defined('STDIN')){ $modes = ['select','text','string']; $mode = 'string'; if($options != null || $options == 0){ if(is_array($options)){ $mode = 'select'; } else if(is_int($options)){ $mode = 'text'; } else { if(is_string($options)){ $default = $options; } } } $stdin = function(){ $handle = fopen ("php://stdin","r"); return str_replace("\n",'',fgets($handle)); }; switch($mode){ case"select": $answer = null; foreach($options as $key => $value){ $options[$key] = strtoupper($value); } while($answer == null || !in_array(strtoupper($answer),$options)){ print_r($string . ' ('); foreach($options as $key => $option){ if($key > 0){ print_r('/'); } print_r($option); } print_r(')'); if($default != null){ print_r('['.$default.']'); } print_r(': '); $answer = $stdin(); if($default != null && $answer == ""){ $answer = $default; } } break; case"text": $answer = ''; $exits = ['END','EXIT','QUIT','EOF',':Q','']; $count = 0; $max = 5; $print = false; if(is_bool($default)){ $print = $default; } if(is_int($options)){ $max = $options; } if($print){ print_r($string . ' tapez ('); foreach($exits as $key => $exit){ if($key > 0){ print_r('/'); } print_r($exit); } print_r(') pour sortir' . PHP_EOL); } else { print_r($string . PHP_EOL); } do { $line = fgets(STDIN); if(in_array(strtoupper(str_replace("\n",'',$line)),$exits)){ if($max <= 0){ $max = 1; } $count = $max; } else { $answer .= $line; $count++; } } while ($count < $max || $max <= 0); break; default: $answer = null; while($answer == null){ print_r($string . ' '); if($default != null){ print_r('['.$default.']'); } print_r(': '); $answer = $stdin(); if($default != null && $answer == ""){ $answer = $default; } if($answer == ''){ $answer = null; } } break; } $answer = trim($answer,"\n"); if($answer == ''){ $answer = null; } return $answer; } } }