├── phpstan.neon ├── CHANGELOG.md ├── ecs.php ├── src ├── translations │ └── en │ │ └── cookies.php ├── services │ ├── ServicesTrait.php │ └── CookiesService.php ├── variables │ └── CookiesVariable.php ├── Cookies.php ├── twigextensions │ └── CookiesTwigExtension.php └── icon.svg ├── LICENSE.txt ├── README.md └── composer.json /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon 3 | 4 | parameters: 5 | level: 5 6 | paths: 7 | - src 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Cookies Changelog 2 | 3 | ## 5.0.0 - 2024.04.15 4 | ### Added 5 | * Stable release for Craft CMS 5 6 | 7 | ## 5.0.0-beta.1 - 2024.02.12 8 | ### Added 9 | * Initial beta release for Craft CMS 5 10 | -------------------------------------------------------------------------------- /ecs.php: -------------------------------------------------------------------------------- 1 | paths([ 8 | __DIR__ . '/src', 9 | __FILE__, 10 | ]); 11 | $ecsConfig->parallel(); 12 | $ecsConfig->sets([SetList::CRAFT_CMS_4]); 13 | }; 14 | -------------------------------------------------------------------------------- /src/translations/en/cookies.php: -------------------------------------------------------------------------------- 1 | 'Cookies', 17 | '{name} plugin loaded' => '{name} plugin loaded', 18 | ]; 19 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 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. 9 | -------------------------------------------------------------------------------- /src/services/ServicesTrait.php: -------------------------------------------------------------------------------- 1 | [ 33 | 'cookies' => CookiesService::class, 34 | ], 35 | ]; 36 | } 37 | 38 | // Public Methods 39 | // ========================================================================= 40 | 41 | /** 42 | * Returns the cookies service 43 | * 44 | * @return CookiesService The cookies service 45 | * @throws InvalidConfigException 46 | */ 47 | public function getCookies(): CookiesService 48 | { 49 | return $this->get('cookies'); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-cookies/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence) 2 | 3 | # Cookies plugin for Craft CMS 5.x 4 | 5 | A simple plugin for setting and getting cookies from within [Craft CMS](http://craftcms.com) templates. 6 | 7 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 8 | 9 | ## Requirements 10 | 11 | This plugin requires Craft CMS 5.0.0 or later. 12 | 13 | **Installation** 14 | 15 | 1. Install with Composer via `composer require nystudio107/craft-cookies` from your project directory 16 | 2. Install the plugin via `./craft install/plugin cookies` via the CLI -or- in the Craft Control Panel under Settings > Plugins 17 | 18 | You can also install Cookies via the **Plugin Store** in the Craft Control Panel. 19 | 20 | ## Documentation 21 | 22 | Click here -> [Cookies Documentation](https://nystudio107.com/plugins/cookies/documentation) 23 | 24 | Brought to you by [nystudio107](http://nystudio107.com) 25 | -------------------------------------------------------------------------------- /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": "5.0.0", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "cookies" 12 | ], 13 | "support": { 14 | "docs": "https://nystudio107.com/docs/cookies/", 15 | "issues": "https://nystudio107.com/plugins/cookies/support", 16 | "source": "https://github.com/nystudio107/craft-cookies" 17 | }, 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "nystudio107", 22 | "homepage": "https://nystudio107.com/" 23 | } 24 | ], 25 | "require": { 26 | "php": "^8.2", 27 | "craftcms/cms": "^5.0.0" 28 | }, 29 | "require-dev": { 30 | "craftcms/ecs": "dev-main", 31 | "craftcms/phpstan": "dev-main", 32 | "craftcms/rector": "dev-main" 33 | }, 34 | "scripts": { 35 | "phpstan": "phpstan --ansi --memory-limit=1G", 36 | "check-cs": "ecs check --ansi", 37 | "fix-cs": "ecs check --fix --ansi" 38 | }, 39 | "config": { 40 | "allow-plugins": { 41 | "craftcms/plugin-installer": true, 42 | "yiisoft/yii2-composer": true 43 | }, 44 | "optimize-autoloader": true, 45 | "sort-packages": true 46 | }, 47 | "autoload": { 48 | "psr-4": { 49 | "nystudio107\\cookies\\": "src/" 50 | } 51 | }, 52 | "extra": { 53 | "class": "nystudio107\\cookies\\Cookies", 54 | "handle": "cookies", 55 | "name": "Cookies" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/variables/CookiesVariable.php: -------------------------------------------------------------------------------- 1 | cookies->set( 38 | $name, 39 | $value, 40 | $expire, 41 | $path, 42 | $domain, 43 | $secure, 44 | $httpOnly, 45 | $sameSite 46 | ); 47 | } 48 | 49 | /** 50 | * Get a cookie 51 | */ 52 | public function get(string $name): string 53 | { 54 | return Cookies::$plugin->cookies->get($name); 55 | } 56 | 57 | /** 58 | * Set a secure cookie 59 | */ 60 | public function setSecure( 61 | string $name = "", 62 | string $value = "", 63 | int $expire = 0, 64 | string $path = "/", 65 | string $domain = "", 66 | bool $secure = false, 67 | bool $httpOnly = false, 68 | string $sameSite = 'Lax', 69 | ): void { 70 | Cookies::$plugin->cookies->setSecure( 71 | $name, 72 | $value, 73 | $expire, 74 | $path, 75 | $domain, 76 | $secure, 77 | $httpOnly, 78 | $sameSite 79 | ); 80 | } 81 | 82 | /** 83 | * Get a secure cookie 84 | */ 85 | public function getSecure(string $name): string 86 | { 87 | return Cookies::$plugin->cookies->getSecure($name); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/Cookies.php: -------------------------------------------------------------------------------- 1 | CookiesService::class, 72 | ]; 73 | 74 | parent::__construct($id, $parent, $config); 75 | } 76 | 77 | /** 78 | * @inheritdoc 79 | */ 80 | public function init(): void 81 | { 82 | parent::init(); 83 | self::$plugin = $this; 84 | $this->name = $this->getName(); 85 | 86 | Event::on( 87 | CraftVariable::class, 88 | CraftVariable::EVENT_INIT, 89 | static function(Event $event): void { 90 | /** @var CraftVariable $variable */ 91 | $variable = $event->sender; 92 | $variable->set('cookies', CookiesVariable::class); 93 | } 94 | ); 95 | 96 | // Add in our Twig extensions 97 | Craft::$app->view->registerTwigExtension(new CookiesTwigExtension()); 98 | 99 | Craft::info( 100 | Craft::t( 101 | 'cookies', 102 | '{name} plugin loaded', 103 | ['name' => $this->name] 104 | ), 105 | __METHOD__ 106 | ); 107 | } 108 | 109 | /** 110 | * Returns the user-facing name of the plugin, which can override the name 111 | * in composer.json 112 | */ 113 | public function getName(): string 114 | { 115 | return Craft::t('cookies', 'Cookies'); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/twigextensions/CookiesTwigExtension.php: -------------------------------------------------------------------------------- 1 | $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), 42 | new TwigFilter('getCookie', fn($name) => $this->getCookie($name)), 43 | new TwigFilter('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), 44 | new TwigFilter('getSecureCookie', fn($name) => $this->getSecureCookie($name)), 45 | ]; 46 | } 47 | 48 | /** 49 | * @inheritdoc 50 | */ 51 | public function getFunctions(): array 52 | { 53 | return [ 54 | new TwigFunction('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), 55 | new TwigFunction('getCookie', fn($name) => $this->getCookie($name)), 56 | new TwigFunction('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), 57 | new TwigFunction('getSecureCookie', fn($name) => $this->getSecureCookie($name)), 58 | ]; 59 | } 60 | 61 | /** 62 | * Set a cookie 63 | */ 64 | public function setCookie( 65 | string $name = "", 66 | string $value = "", 67 | int $expire = 0, 68 | string $path = "/", 69 | string $domain = "", 70 | bool $secure = false, 71 | bool $httpOnly = false, 72 | string $sameSite = 'Lax', 73 | ): void { 74 | Cookies::$plugin->cookies->set( 75 | $name, 76 | $value, 77 | $expire, 78 | $path, 79 | $domain, 80 | $secure, 81 | $httpOnly, 82 | $sameSite 83 | ); 84 | } 85 | 86 | /** 87 | * Get a cookie 88 | */ 89 | public function getCookie(string $name): string 90 | { 91 | return Cookies::$plugin->cookies->get($name); 92 | } 93 | 94 | /** 95 | * Set a secure cookie 96 | */ 97 | public function setSecureCookie( 98 | string $name = "", 99 | string $value = "", 100 | int $expire = 0, 101 | string $path = "/", 102 | string $domain = "", 103 | bool $secure = false, 104 | bool $httpOnly = false, 105 | string $sameSite = 'Lax', 106 | ): void { 107 | Cookies::$plugin->cookies->setSecure( 108 | $name, 109 | $value, 110 | $expire, 111 | $path, 112 | $domain, 113 | $secure, 114 | $httpOnly, 115 | $sameSite 116 | ); 117 | } 118 | 119 | /** 120 | * Get a secure cookie 121 | */ 122 | public function getSecureCookie(string $name): string 123 | { 124 | return Cookies::$plugin->cookies->getSecure($name); 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/services/CookiesService.php: -------------------------------------------------------------------------------- 1 | response->cookies->remove($name); 43 | } else { 44 | $domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain; 45 | if (PHP_VERSION_ID >= 70300) { 46 | setcookie($name, $value, [ 47 | 'expires' => $expire, 48 | 'path' => $path, 49 | 'domain' => $domain, 50 | 'secure' => $secure, 51 | 'httponly' => $httpOnly, 52 | 'samesite' => $sameSite, 53 | ]); 54 | } else { 55 | setcookie($name, $value, ['expires' => $expire, 'path' => $path, 'domain' => $domain, 'secure' => $secure, 'httponly' => $httpOnly]); 56 | } 57 | 58 | $_COOKIE[$name] = $value; 59 | } 60 | } 61 | 62 | /** 63 | * Get a cookie 64 | */ 65 | public function get(string $name = ''): string 66 | { 67 | return $_COOKIE[$name] ?? ''; 68 | } 69 | 70 | /** 71 | * Set a secure cookie 72 | */ 73 | public function setSecure( 74 | string $name = '', 75 | string $value = '', 76 | int $expire = 0, 77 | string $path = '/', 78 | string $domain = '', 79 | bool $secure = false, 80 | bool $httpOnly = false, 81 | string $sameSite = 'Lax', 82 | ): void { 83 | if (empty($value)) { 84 | Craft::$app->response->cookies->remove($name); 85 | } else { 86 | $domain = empty($domain) ? Craft::$app->getConfig()->getGeneral()->defaultCookieDomain : $domain; 87 | $cookie = new Cookie(['name' => $name, 'value' => '']); 88 | 89 | try { 90 | $cookie->value = Craft::$app->security->hashData(base64_encode(serialize($value))); 91 | } catch (InvalidConfigException|Exception $e) { 92 | Craft::error( 93 | 'Error setting secure cookie: ' . $e->getMessage(), 94 | __METHOD__ 95 | ); 96 | 97 | return; 98 | } 99 | 100 | $cookie->expire = $expire; 101 | $cookie->path = $path; 102 | $cookie->domain = $domain; 103 | $cookie->secure = $secure; 104 | $cookie->httpOnly = $httpOnly; 105 | if (PHP_VERSION_ID >= 70300) { 106 | $cookie->sameSite = $sameSite; 107 | } 108 | 109 | Craft::$app->response->cookies->add($cookie); 110 | } 111 | } 112 | 113 | /** 114 | * Get a secure cookie 115 | */ 116 | public function getSecure(string $name = ''): string 117 | { 118 | $result = ''; 119 | $cookie = Craft::$app->request->cookies->get($name); 120 | if ($cookie !== null) { 121 | try { 122 | $data = Craft::$app->security->validateData($cookie->value); 123 | } catch (InvalidConfigException|Exception $e) { 124 | Craft::error( 125 | 'Error getting secure cookie: ' . $e->getMessage(), 126 | __METHOD__ 127 | ); 128 | $data = false; 129 | } 130 | 131 | if ( 132 | !empty($cookie->value) 133 | && $data !== false 134 | ) { 135 | $result = unserialize(base64_decode($data), ['allowed_classes' => false]); 136 | } 137 | } 138 | 139 | return $result; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 10 | 59 | 60 | --------------------------------------------------------------------------------