├── .gitignore ├── resources └── img │ └── plugin-logo.png ├── src ├── translations │ └── en │ │ └── cookies.php ├── Cookies.php ├── variables │ └── CookiesVariable.php ├── twigextensions │ └── CookiesTwigExtension.php ├── services │ └── CookiesService.php └── icon.svg ├── LICENSE.txt ├── composer.json ├── CHANGELOG.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | -------------------------------------------------------------------------------- /resources/img/plugin-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hashchange/craft-cookies/v1/resources/img/plugin-logo.png -------------------------------------------------------------------------------- /src/translations/en/cookies.php: -------------------------------------------------------------------------------- 1 | 'Cookies plugin loaded', 17 | ]; 18 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2017 nystudio107 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nystudio107/craft-cookies", 3 | "description": "A simple plugin for setting and getting cookies from within Craft CMS templates.", 4 | "type": "craft-plugin", 5 | "version": "1.1.10", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "cookies" 12 | ], 13 | "support": { 14 | "docs": "https://github.com/nystudio107/craft-cookies/blob/v1/README.md", 15 | "issues": "https://github.com/nystudio107/craft-cookies/issues" 16 | }, 17 | "license": "MIT", 18 | "authors": [ 19 | { 20 | "name": "nystudio107", 21 | "homepage": "https://nystudio107.com/" 22 | } 23 | ], 24 | "require": { 25 | "craftcms/cms": "^3.0.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "nystudio107\\cookies\\": "src/" 30 | } 31 | }, 32 | "extra": { 33 | "name": "Cookies", 34 | "handle": "cookies", 35 | "schemaVersion": "1.0.0", 36 | "hasCpSettings": false, 37 | "hasCpSection": false, 38 | "changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft-cookies/v1/CHANGELOG.md", 39 | "class": "nystudio107\\cookies\\Cookies", 40 | "components": { 41 | "cookies": "nystudio107\\cookies\\services\\CookiesService" 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Cookies Changelog 2 | 3 | ## 1.1.10 - 2017.07.22 4 | ### Changed 5 | * If the passed in domain is empty, use the `defaultCookieDomain` config setting 6 | * Don't unserialize any classes in secure cookie data 7 | * Code cleanup 8 | 9 | ## 1.1.9 - 2017.02.01 10 | ### Changed 11 | * Renamed the composer package name to `craft-cookies` 12 | * Check to ensure a cookie exists before accessing it in `getSecure()` 13 | 14 | ## 1.1.8 - 2017.01.23 15 | ### Changed 16 | * Fixed an issue with removing cookies 17 | * Added try/catch so errors are logged instead of exceptions thrown 18 | 19 | ## 1.1.7 - 2017.12.06 20 | ### Changed 21 | * Updated to require craftcms/cms `^3.0.0-RC1` 22 | * Switched to `Craft::$app->view->registerTwigExtension` to register the Twig extension 23 | 24 | ## 1.1.6 - 2017.08.05 25 | ### Changed 26 | * Craft 3 beta 23 compatibility 27 | 28 | ## 1.1.5 - 2017.07.09 29 | ### Changed 30 | * Craft 3 beta 20 compatibility 31 | 32 | ## 1.1.4 - 2017.03.24 33 | ### Changed 34 | * `hasSettings` -> `hasCpSettings` for Craft 3 beta 8 compatibility 35 | 36 | ## 1.1.3 - 2017.03.12 37 | ### Added 38 | * Added `craft/cms` as a composer dependency 39 | * Added code inspection typehinting for the plugin & services 40 | 41 | ### Changed 42 | * Code refactor/cleanup 43 | 44 | ## 1.1.2 - 2017.02.17 45 | ### Changed 46 | * Code cleanup 47 | * Added a new colored icon 48 | 49 | ## 1.1.1 - 2017.02.10 50 | ### Changed 51 | * Cleaned up `composer.json` 52 | 53 | ## 1.1.0 - 2017.02.01 54 | ### Added 55 | - Ported the plugin to Craft 3 56 | -------------------------------------------------------------------------------- /src/Cookies.php: -------------------------------------------------------------------------------- 1 | name = $this->getName(); 46 | 47 | Event::on( 48 | CraftVariable::class, 49 | CraftVariable::EVENT_INIT, 50 | function (Event $event) { 51 | /** @var CraftVariable $variable */ 52 | $variable = $event->sender; 53 | $variable->set('cookies', CookiesVariable::class); 54 | } 55 | ); 56 | 57 | // Add in our Twig extensions 58 | Craft::$app->view->registerTwigExtension(new CookiesTwigExtension()); 59 | 60 | Craft::info( 61 | Craft::t( 62 | 'cookies', 63 | '{name} plugin loaded', 64 | ['name' => $this->name] 65 | ), 66 | __METHOD__ 67 | ); 68 | } 69 | 70 | /** 71 | * Returns the user-facing name of the plugin, which can override the name 72 | * in composer.json 73 | * 74 | * @return string 75 | */ 76 | public function getName() 77 | { 78 | return Craft::t('cookies', 'Cookies'); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/variables/CookiesVariable.php: -------------------------------------------------------------------------------- 1 | cookies->set( 47 | $name, 48 | $value, 49 | $expire, 50 | $path, 51 | $domain, 52 | $secure, 53 | $httpOnly 54 | ); 55 | } 56 | 57 | /** 58 | * Get a cookie 59 | * 60 | * @param $name 61 | * 62 | * @return mixed 63 | */ 64 | public function get($name) 65 | { 66 | return Cookies::$plugin->cookies->get($name); 67 | } 68 | 69 | /** 70 | * Set a secure cookie 71 | * 72 | * @param string $name 73 | * @param string $value 74 | * @param int $expire 75 | * @param string $path 76 | * @param string $domain 77 | * @param bool $secure 78 | * @param bool $httpOnly 79 | */ 80 | public function setSecure( 81 | $name = "", 82 | $value = "", 83 | $expire = 0, 84 | $path = "/", 85 | $domain = "", 86 | $secure = false, 87 | $httpOnly = false 88 | ) { 89 | Cookies::$plugin->cookies->setSecure( 90 | $name, 91 | $value, 92 | $expire, 93 | $path, 94 | $domain, 95 | $secure, 96 | $httpOnly 97 | ); 98 | } 99 | 100 | /** 101 | * Get a secure cookie 102 | * 103 | * @param $name 104 | * 105 | * @return mixed 106 | */ 107 | public function getSecure($name) 108 | { 109 | return Cookies::$plugin->cookies->getSecure($name); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/twigextensions/CookiesTwigExtension.php: -------------------------------------------------------------------------------- 1 | cookies->set( 83 | $name, 84 | $value, 85 | $expire, 86 | $path, 87 | $domain, 88 | $secure, 89 | $httpOnly 90 | ); 91 | } 92 | 93 | /** 94 | * Get a cookie 95 | * 96 | * @param $name 97 | * 98 | * @return mixed 99 | */ 100 | public function getCookie($name) 101 | { 102 | return Cookies::$plugin->cookies->get($name); 103 | } 104 | 105 | /** 106 | * Set a secure cookie 107 | * 108 | * @param string $name 109 | * @param string $value 110 | * @param int $expire 111 | * @param string $path 112 | * @param string $domain 113 | * @param bool $secure 114 | * @param bool $httpOnly 115 | */ 116 | public function setSecureCookie( 117 | $name = "", 118 | $value = "", 119 | $expire = 0, 120 | $path = "/", 121 | $domain = "", 122 | $secure = false, 123 | $httpOnly = false 124 | ) { 125 | Cookies::$plugin->cookies->setSecure( 126 | $name, 127 | $value, 128 | $expire, 129 | $path, 130 | $domain, 131 | $secure, 132 | $httpOnly 133 | ); 134 | } 135 | 136 | /** 137 | * Get a secure cookie 138 | * 139 | * @param $name 140 | * 141 | * @return mixed 142 | */ 143 | public function getSecureCookie($name) 144 | { 145 | return Cookies::$plugin->cookies->getSecure($name); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/services/CookiesService.php: -------------------------------------------------------------------------------- 1 | response->cookies->remove($name); 51 | } else { 52 | $domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain; 53 | $expire = (int)$expire; 54 | setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly); 55 | $_COOKIE[$name] = $value; 56 | } 57 | } 58 | 59 | /** 60 | * Get a cookie 61 | * 62 | * @param string $name 63 | * 64 | * @return mixed 65 | */ 66 | public function get($name = '') 67 | { 68 | $result = ''; 69 | if (isset($_COOKIE[$name])) { 70 | $result = $_COOKIE[$name]; 71 | } 72 | 73 | return $result; 74 | } 75 | 76 | /** 77 | * Set a secure cookie 78 | * 79 | * @param string $name 80 | * @param string $value 81 | * @param int $expire 82 | * @param string $path 83 | * @param string $domain 84 | * @param bool $secure 85 | * @param bool $httpOnly 86 | */ 87 | public function setSecure( 88 | $name = '', 89 | $value = '', 90 | $expire = 0, 91 | $path = '/', 92 | $domain = '', 93 | $secure = false, 94 | $httpOnly = false 95 | ) { 96 | if (empty($value)) { 97 | Craft::$app->response->cookies->remove($name); 98 | } else { 99 | $domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain; 100 | $expire = (int)$expire; 101 | $cookie = new Cookie(['name' => $name, 'value' => '']); 102 | 103 | try { 104 | $cookie->value = Craft::$app->security->hashData(base64_encode(serialize($value))); 105 | } catch (InvalidConfigException $e) { 106 | Craft::error( 107 | 'Error setting secure cookie: '.$e->getMessage(), 108 | __METHOD__ 109 | ); 110 | 111 | return; 112 | } catch (Exception $e) { 113 | Craft::error( 114 | 'Error setting secure cookie: '.$e->getMessage(), 115 | __METHOD__ 116 | ); 117 | 118 | return; 119 | } 120 | $cookie->expire = $expire; 121 | $cookie->path = $path; 122 | $cookie->domain = $domain; 123 | $cookie->secure = $secure; 124 | $cookie->httpOnly = $httpOnly; 125 | 126 | Craft::$app->response->cookies->add($cookie); 127 | } 128 | } 129 | 130 | /** 131 | * Get a secure cookie 132 | * 133 | * @param string $name 134 | * 135 | * @return mixed 136 | */ 137 | public function getSecure($name = '') 138 | { 139 | $result = ''; 140 | $cookie = Craft::$app->request->cookies->get($name); 141 | if ($cookie !== null) { 142 | try { 143 | $data = Craft::$app->security->validateData($cookie->value); 144 | } catch (InvalidConfigException $e) { 145 | Craft::error( 146 | 'Error getting secure cookie: '.$e->getMessage(), 147 | __METHOD__ 148 | ); 149 | $data = false; 150 | } catch (Exception $e) { 151 | Craft::error( 152 | 'Error getting secure cookie: '.$e->getMessage(), 153 | __METHOD__ 154 | ); 155 | $data = false; 156 | } 157 | if ($cookie 158 | && !empty($cookie->value) 159 | && $data !== false 160 | ) { 161 | $result = @unserialize(base64_decode($data), false); 162 | } 163 | } 164 | 165 | return $result; 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 10 | 59 | 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/quality-score.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/?branch=v1) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/coverage.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/?branch=v1) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/build.png?b=v1)](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/build-status/v1) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/code-intelligence.svg?b=v1)](https://scrutinizer-ci.com/code-intelligence) 2 | 3 | # Cookies plugin for Craft CMS 3.x 4 | 5 | A simple plugin for setting and getting cookies from within [Craft CMS](http://craftcms.com) templates. 6 | 7 | ![Screenshot](resources/img/plugin-logo.png) 8 | 9 | Related: [Cookies for Craft 2.x](https://github.com/nystudio107/cookies) 10 | 11 | ## Requirements 12 | 13 | This plugin requires Craft CMS 3.0.0 or later. 14 | 15 | **Installation** 16 | 17 | 1. Install with Composer via `composer require nystudio107/craft-cookies` from your project directory 18 | 2. Install plugin in the Craft Control Panel under Settings > Plugins 19 | 20 | You can also install Cookies via the **Plugin Store** in the Craft AdminCP. 21 | 22 | ## Setting cookies 23 | 24 | All three of these methods accomplish the same thing: 25 | 26 | {# Set the cookie using 'setCookie' function #} 27 | {% do setCookie( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %} 28 | 29 | {# Set the cookie using 'setCookie' filter #} 30 | {% do NAME | setCookie( VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %} 31 | 32 | {# Set the cookie using 'set' variable #} 33 | {% do craft.cookies.set( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %} 34 | 35 | They all act as a wrapper for the PHP `setcookie` function: 36 | 37 | More info: (http://php.net/manual/en/function.setcookie.php) 38 | 39 | All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified 40 | 41 | **Examples** 42 | 43 | {% do setCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp) %} 44 | {# Sets a cookie to expire in an hour. #} 45 | 46 | {% do 'marvin' | setCookie('martian', now | date_modify("+30 days").timestamp) %} 47 | {# Sets a cookie to expire in 30 days. #} 48 | 49 | {% do craft.cookies.set('marvin', 'martian', '', '/foo/' ) %} 50 | {# Cookie available within /foo/ directory and sub-directories. #} 51 | 52 | ## Setting Secure cookies 53 | 54 | All three of these methods accomplish the same thing: 55 | 56 | {# Set the cookie using 'setSecureCookie' function #} 57 | {% do setSecureCookie( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %} 58 | 59 | {# Set the cookie using 'setSecureCookie' filter #} 60 | {% do NAME | setSecureCookie( VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %} 61 | 62 | {# Set the cookie using 'setSecure' variable #} 63 | {% do craft.cookies.setSecure( NAME, VALUE, DURATION, PATH, DOMAIN, SECURE, HTTPONLY) %} 64 | 65 | This function works the same as `setCookie` but instead of using the PHP `setcookie` function, it uses the `craft()->request->getCookies()->add` to add the cookies via Craft. It also utilizes `craft->security` framework to encrypt and validate the cookie contents between requests. 66 | 67 | All of the parameters except for `NAME` are optional. The `PATH` defaults to `/` if not specified 68 | 69 | **Examples** 70 | 71 | {% do setSecureCookie('marvin', 'martian', now | date_modify("+1 hour").timestamp) %} 72 | {# Sets a cookie to expire in an hour. #} 73 | 74 | {% do 'marvin' | setSecureCookie('martian', now | date_modify("+30 days").timestamp) %} 75 | {# Sets a cookie to expire in 30 days. #} 76 | 77 | {% do craft.cookies.setSecure('marvin', 'martian', '', '/foo/' ) %} 78 | {# Cookie available within /foo/ directory and sub-directories. #} 79 | 80 | ## Retrieving cookies 81 | 82 | Both of these methods accomplish the same thing: 83 | 84 | {# Get the cookie using 'getCookie' function #} 85 | {% do getCookie( NAME ) %} 86 | 87 | {# Get the cookie using 'get' variable #} 88 | {% do craft.cookies.get( NAME ) %} 89 | 90 | **Example** 91 | 92 | {% do getCookie('marvin') %} 93 | {# Get the cookie using 'getCookie' function #} 94 | 95 | {% do craft.cookies.get('marvin') %} 96 | {# Get the cookie using 'get' variable #} 97 | 98 | {% if getCookie('marvin') %} 99 | {% set myCookie = getCookie('marvin') %} 100 | {{ myCookie }} 101 | {% endif %} 102 | 103 | ## Retrieving Secure cookies 104 | 105 | Both of these methods accomplish the same thing: 106 | 107 | {# Get the cookie using 'getSecureCookie' function #} 108 | {% do getSecureCookie( NAME ) %} 109 | 110 | {# Get the cookie using 'getSecure' variable #} 111 | {% do craft.cookies.getSecure( NAME ) %} 112 | 113 | **Example** 114 | 115 | {% do getSecureCookie('marvin') %} 116 | {# Get the cookie using 'getSecureCookie' function #} 117 | 118 | {% do craft.cookies.getSecure('marvin') %} 119 | {# Get the cookie using 'getSecure' variable #} 120 | 121 | {% if getSecureCookie('marvin') %} 122 | {% set myCookie = getSecureCookie('marvin') %} 123 | {{ myCookie }} 124 | {% endif %} 125 | 126 | This function works the same as `getCookie` but it uses `craft()->request->getCookie()` to retrieve the cookies via Craft. It also utilizes `craft->security` framework to decrypt and validate the cookie contents between requests. 127 | 128 | **Example** 129 | 130 | {% do getSecureCookie('marvin') %} 131 | {# Get the cookie using 'getSecureCookie' function #} 132 | 133 | {% do craft.cookies.getSecure('marvin') %} 134 | {# Get the cookie using 'getSecure' variable #} 135 | 136 | {% if getSecureCookie('marvin') %} 137 | {% set myCookie = getSecureCookie('marvin') %} 138 | {{ myCookie }} 139 | {% endif %} 140 | 141 | ## Deleting cookies 142 | 143 | All three of these methods accomplish the same thing: 144 | 145 | {# Delete a cookie by passing no VALUE to 'setCookie' function #} 146 | {% do setCookie( NAME ) %} 147 | 148 | {# Delete a cookie by passing no VALUE to 'setCookie' filter #} 149 | {% do NAME | setCookie() %} 150 | 151 | {# Delete a cookie by passing no VALUE to 'set' variable #} 152 | {% do craft.cookies.set( NAME ) %} 153 | 154 | ## Deleting Secure cookies 155 | 156 | All three of these methods accomplish the same thing: 157 | 158 | {# Delete a cookie by passing no VALUE to 'setSecureCookie' function #} 159 | {% do setSecureCookie( NAME ) %} 160 | 161 | {# Delete a cookie by passing no VALUE to 'setSecureCookie' filter #} 162 | {% do NAME | setSecureCookie() %} 163 | 164 | {# Delete a cookie by passing no VALUE to 'setSecure' variable #} 165 | {% do craft.cookies.setSecure( NAME ) %} 166 | 167 | Brought to you by [nystudio107](http://nystudio107.com) --------------------------------------------------------------------------------