From 6c7ac2b250cf3b0c08c50afb84d1dc0ab9909778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 22 Oct 2015 21:01:26 +0200 Subject: [PATCH 1/4] [TASK] Added ip method to Response class. --- src/Pecee/Http/Request.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index ad5f575..89994a4 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -60,6 +60,14 @@ class Request { return $this->headers; } + /** + * Get id address + * @return string + */ + public function getIp() { + return isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; + } + /** * Get header value by name * @param string $name From b8061f2aa7994b302d84003f3632b49b96b4381b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 22 Oct 2015 21:04:52 +0200 Subject: [PATCH 2/4] [TASK] Added getUserAgent and getReferer methods to Request class. --- src/Pecee/Http/Request.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 89994a4..7829336 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -68,6 +68,22 @@ class Request { return isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; } + /** + * Get referer + * @return string + */ + public function getReferer() { + return isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; + } + + /** + * Get user agent + * @return string + */ + public function getUserAgent() { + return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; + } + /** * Get header value by name * @param string $name From f5597c24cecbbebf2338123e18ab55206897f53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 22 Oct 2015 21:13:54 +0200 Subject: [PATCH 3/4] [FEATURE] Added getInput method to return request items. --- src/Pecee/Http/Request.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 7829336..dd65683 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -93,4 +93,14 @@ class Request { return (isset($this->headers[$name])) ? $this->headers[$name] : null; } + /** + * Get request input or default value + * @param string $name + * @param string $defaultValue + * @return mixed + */ + public function getInput($name, $defaultValue) { + return (isset($_REQUEST[$name]) ? $_REQUEST[$name] : $defaultValue); + } + } \ No newline at end of file From b37c73d5dd8dbc7ca3e2778e9f25b40702d7eb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Thu, 22 Oct 2015 21:42:33 +0200 Subject: [PATCH 4/4] [FEATURE] Added more features to Response class. --- src/Pecee/Http/Response.php | 56 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/Pecee/Http/Response.php b/src/Pecee/Http/Response.php index 5512b0c..6749044 100644 --- a/src/Pecee/Http/Response.php +++ b/src/Pecee/Http/Response.php @@ -21,7 +21,7 @@ class Response { * @param string $url */ public function redirect($url) { - header('location: ' . $url); + $this->header('Location: ' . $url); die(); } @@ -29,9 +29,59 @@ class Response { $this->redirect(url()); } + /** + * Add http authorisation + * @param string $name + * @return self $this + */ public function auth($name = '') { - header('WWW-Authenticate: Basic realm="' . $name . '"'); - header('HTTP/1.0 401 Unauthorized'); + $this->headers([ + 'WWW-Authenticate: Basic realm="' . $name . '"', + 'HTTP/1.0 401 Unauthorized' + ]); + return $this; + } + + public function cache($duration = 2592000) { + $this->headers([ + 'Cache-Control: public,max-age='.$duration.',must-revalidate', + 'Expires: '.gmdate('D, d M Y H:i:s',(time()+$duration)).' GMT', + 'Last-modified: '.gmdate('D, d M Y H:i:s',time()).' GMT' + ]); + return $this; + } + + /** + * Json encode array + * @param array $value + * @return self $this + */ + public function json(array $value) { + $this->header('Content-type: application/json'); + echo json_encode($value); + return $this; + } + + /** + * Add header to response + * @param string $value + * @return self $this + */ + public function header($value) { + header($value); + return $this; + } + + /** + * Add multiple headers to response + * @param array $headers + * @return self $this + */ + public function headers(array $headers) { + foreach($headers as $header) { + header($header); + } + return $this; } } \ No newline at end of file