From efb66252909a38e9dd229e697d43be98e80ee43a Mon Sep 17 00:00:00 2001 From: Cretu Eusebiu Date: Thu, 6 Aug 2015 22:47:26 +0300 Subject: [PATCH] Init --- README.md | 44 ++++++++++++ composer.json | 22 ++++++ src/Cookie.php | 39 ++++++++++ src/CookieJar.php | 142 +++++++++++++++++++++++++++++++++++++ src/CookieJarInterface.php | 52 ++++++++++++++ 5 files changed, 299 insertions(+) create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Cookie.php create mode 100644 src/CookieJar.php create mode 100644 src/CookieJarInterface.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..2358f32 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +### Installation + +`composer require hazzard/cookie` + +### Usage + +``` +use Hazzard\Cookie\Cookie; + +// Set a cookie for 60 minutes. +Cookie::set('name', 'value', 60); + +// Set a cookie "forever". +Cookie::forever('name', 'value'); + +// Retrieve a cookie. +$value = Cookie::get('name'); + +// Forget a cookie. +Cookie::forget('name'); +``` + + +##### Default path and domain + +``` +Cookie::setDefaultPathAndDomain('/path/to', 'domain.com'); +``` + + +##### Encrypted cookies + +First `composer require hazzard/encryption`, then: + +``` +use Hazzard\Cookie\Cookie; +use Hazzard\Encryption\Encrypter; + +$encrypter = new Encrypter('a random 32 character string', 'AES-256-CBC'); + +Cookie::setEncrypter($encrypter); +``` + +You can use any other encrypter as long as it has the `encrypt` and `decrypt` methods. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..262781c --- /dev/null +++ b/composer.json @@ -0,0 +1,22 @@ +{ + "name": "hazzard/cookie", + "description": "A simple cookie package.", + "license": "MIT", + "authors": [ + { + "name": "Cretu Eusebiu", + "email": "cretu.eusebiu@gmail.com" + } + ], + "require": { + "php": ">=5.3" + }, + "autoload": { + "psr-4": { + "Hazzard\\Cookie\\": "src/" + } + }, + "suggest": { + "hazzard/encryption": "Required to for encrypted cookies." + } +} diff --git a/src/Cookie.php b/src/Cookie.php new file mode 100644 index 0000000..041fbd4 --- /dev/null +++ b/src/Cookie.php @@ -0,0 +1,39 @@ +getPathAndDomain($path, $domain); + + $time = ($minutes == 0) ? 0 : time() + ($minutes * 60); + + if (isset($this->encrypter)) { + $value = $this->encrypter->encrypt($value); + } + + return setcookie($name, $value, $time, $path, $domain, $secure, $httpOnly); + } + + /** + * Retrieve a cookie. + * + * @param string $name + * @param mixed $default + * @return mixed + */ + public function get($key, $default = null) + { + if (array_key_exists($key, $_COOKIE)) { + $value = $_COOKIE[$key]; + + if (isset($this->encrypter)) { + $value = $this->encrypter->decrypt($value); + } + + return $value; + } + + return $default; + } + + /** + * Set a cookie that lasts "forever" (five years). + * + * @param string $name + * @param string $value + * @param string $path + * @param string $domain + * @param bool $secure + * @param bool $httpOnly + * @return bool + */ + public function forever($name, $value, $path = null, $domain = null, $secure = false, $httpOnly = true) + { + return $this->set($name, $value, 2628000, $path, $domain, $secure, $httpOnly); + } + + /** + * Expire a cookie. + * + * @param string $name + * @param string $path + * @param string $domain + * @return bool + */ + public function forget($name, $path = null, $domain = null) + { + return $this->set($name, null, -2628000, $path, $domain); + } + + /** + * Get the path and domain, or the default values. + * + * @param string $path + * @param string $domain + * @return array + */ + protected function getPathAndDomain($path, $domain) + { + return array($path ?: $this->path, $domain ?: $this->domain); + } + + /** + * Set the default path and domain for the jar. + * + * @param string $path + * @param string $domain + * @return $this + */ + public function setDefaultPathAndDomain($path, $domain) + { + list($this->path, $this->domain) = array($path, $domain); + + return $this; + } + + /** + * Set encrypter. + * + * @param \Hazzard\Encryption\Encrypter $encrypter + * @return $this + */ + public function setEncrypter($encrypter) + { + $this->encrypter = $encrypter; + + return $this; + } +} diff --git a/src/CookieJarInterface.php b/src/CookieJarInterface.php new file mode 100644 index 0000000..8b4e19a --- /dev/null +++ b/src/CookieJarInterface.php @@ -0,0 +1,52 @@ +