Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Intahwebz\Router;
- class HTTPRequest extends AbstractRequest {
- private $referedParams = array();
- private $files = array();
- private $server;
- function __construct(
- $server,// $_SERVER,
- $get,//$_GET,
- $post,//$_POST,
- $files,//$_FILES,
- $cookie//$_COOKIE
- ) {
- $this->determineHostName($server);
- $this->determineScheme($server);
- $this->determineMethod($server);
- $this->determineFormat();
- $this->determineRequestParams($get, $post);
- $this->determineRefererParams($server);
- $this->determinePath($server);
- $this->determinePort($server);
- $this->determineClientIP($server);
- $this->getNormalizedFILES($files);
- $this->server = $server;
- }
- function determineScheme(){
- $this->scheme = 'http';
- }
- function determineRefererParams($server){
- $this->referedParams = array();
- if(array_key_exists('HTTP_REFERER', $server) == true){
- $queryString = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
- $urlInfo = parse_url($queryString, PHP_URL_QUERY);
- parse_str($urlInfo, $this->referedParams);
- }
- }
- function determineMethod($server){
- $this->method = 'GET';
- if(array_key_exists('REQUEST_METHOD', $server) == true){
- $this->method = mb_strtoupper($server['REQUEST_METHOD']);
- }
- // if ('POST' === $this->method) {
- // 'X-HTTP-METHOD-OVERRIDE',
- // '_method',
- // $this->query->get('_method', 'POST'))));
- // }
- }
- function determineFormat(){
- $this->requestedFormat = 'default';
- }
- function determineHostName($server){
- $this->hostName = 'localhost';
- if(array_key_exists('HTTP_HOST', $server)){
- $this->hostName = $server['HTTP_HOST'];
- }
- }
- function determineRequestParams($get, $post){
- $this->requestParams = array_merge($get, $post);
- }
- function determinePort($server){
- $this->port = 80;
- if(array_key_exists('SERVER_PORT', $server)){
- $this->port = intval($server['SERVER_PORT']);
- }
- // if ($this->trustProxy && $this->headers->has('X-Forwarded-Port')) {
- // return $this->headers->get('X-Forwarded-Port');
- // }
- }
- function determinePath($server){
- $this->path = '/';
- if(array_key_exists('REQUEST_URI', $server)){
- $uriInfo = parse_url($server['REQUEST_URI']);
- $this->path = $uriInfo['path'];
- // $this->query = $uriInfo['query'];
- }
- }
- function determineClientIP($server){
- $headerArray = array(
- 'HTTP_CLIENT_IP',
- 'HTTP_X_FORWARDED_FOR',
- 'HTTP_X_FORWARDED',
- 'HTTP_X_CLUSTER_CLIENT_IP',
- 'HTTP_FORWARDED_FOR',
- 'HTTP_FORWARDED',
- 'REMOTE_ADDR'
- );
- foreach ($headerArray as $key){
- if (array_key_exists($key, $server) === true){
- foreach (explode(',', $server[$key]) as $ip){
- $ip = trim($ip); // just to be safe
- if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false){
- $this->clientIP = $ip;
- return;
- }
- }
- }
- }
- }
- function getNormalizedFILES($files){
- foreach($files as $fieldName => $fieldValue){
- foreach($fieldValue as $paramName => $paramValue){
- foreach((array)$paramValue as $value){
- $this->files[$fieldName][$paramName] = $value;
- }
- }
- }
- }
- function getFileUploadErrorMeaning($errorCode){
- switch($errorCode){
- case (UPLOAD_ERR_OK):{ //no error; possible file attack!
- return "There was a problem with your upload.";
- }
- case (UPLOAD_ERR_INI_SIZE): {//uploaded file exceeds the upload_max_filesize directive in php.ini
- return "The file you are trying to upload is too big.";
- }
- case (UPLOAD_ERR_FORM_SIZE):{ //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
- return "The file you are trying to upload is too big.";
- }
- case UPLOAD_ERR_PARTIAL: {//uploaded file was only partially uploaded
- //Todo - allow partial uploads
- return "The file you are trying upload was only partially uploaded.";
- }
- case (UPLOAD_ERR_NO_FILE): {//no file was uploaded
- return "You must select a file for upload.";
- }
- //TODO - handle these
- // UPLOAD_ERR_NO_TMP_DIR
- // UPLOAD_ERR_CANT_WRITE
- // UPLOAD_ERR_EXTENSION
- default: { //a default error, just in case! :)
- return "There was a problem with your upload, error code is ".$errorCode;
- }
- }
- }
- /**
- * @param $formFileName
- * @return \Intahwebz\Utils\UploadedFile
- * @throws \Intahwebz\Utils\FileUploadException
- * @throws \Exception
- */
- function getUploadedFile($formFileName){
- if(isset($this->files[$formFileName]) == FALSE){
- return false;
- }
- else{
- if($this->files[$formFileName]['error'] == UPLOAD_ERR_OK) {
- if(is_uploaded_file($this->files[$formFileName]['tmp_name']) ){
- return new \Intahwebz\Utils\UploadedFile(
- $this->files[$formFileName]['name'],
- $this->files[$formFileName]['tmp_name'],
- $this->files[$formFileName]['size']
- );
- }
- else{
- throw new \Intahwebz\Utils\FileUploadException("File not uploaded. Status [".$this->files[$formFileName]['error']."] indicated error.");
- }
- }
- else{
- throw new \Intahwebz\Utils\FileUploadException("Error detected in upload: ".$this->getFileUploadErrorMeaning($this->files[$formFileName]['error']));
- }
- }
- }
- function getReferrer() {
- if(array_key_exists("HTTP_REFERER", $this->server)) {
- return $this->server["HTTP_REFERER"];
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement