Skip to content

Commit

Permalink
Created the initial package
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrankruijter committed Apr 12, 2018
1 parent 6cf17af commit a4002b1
Show file tree
Hide file tree
Showing 10 changed files with 680 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
23 changes: 23 additions & 0 deletions Config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"scopes": [
{
"scope": "functional",
"required": 1,
"title": "Functional cookies",
"description": "These are the cookies we set purely for functionalities in our websites. We can not track you in any way by settings these. For example if we didn't set a cookie that stored these settings, we wouldn't be able to not constantly prompt you with this screen."
},
{
"scope": "analytical",
"required": 0,
"title": "Analytical cookies",
"description": "With these cookies, behaviour on our website can be monitored through third party services. These cookies can be used by said third parties to track you."
}],
"messages":
{
"required_help": "These cookies can not be unset during your visit to this website, because this would result in degraded performance.",
"settings_button": "Cookie Settings",
"cookiebar_title": "Change your cookie settings.",
"cookiebar_button": "Accept Settings",
"cookiebar_description": "To comply with new regulations regarding GDPR we are now obligated by law to provide you with settings surrounding cookies. We have not set any cookies that would be able to track you. If you wish to change these settings later on, we will provide you with a button in order to do so."
}
}
112 changes: 112 additions & 0 deletions Cookie/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Copyright (C) Jyxon, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Jyxon\GdprCookieCompliance\Cookie;

use Jyxon\GdprCookieCompliance\Cookie\Settings;

class Manager
{
/**
* Contains the Settings object.
*
* @var Settings
*/
private $settings;

/**
* Constructor
*
* @param Settings $settings
*/
public function __construct(Settings $settings)
{
$this->settings = $settings;
}

/**
* Checks wether there is consent for setting a cookie.
*
* @param string $scope
*
* @return bool
*/
public function canSet(string $scope): bool
{
return in_array($scope, $this->settings->getAllowedCookies());
}

/**
* Send a cookie if there is consent. Also deletes cookies for which is no longer consent.
*
* @param string $scope
* @param string $name
* @param string $value
* @param integer $expire
* @param string $path
* @param string $domain
* @param boolean $secure
* @param boolean $httponly
*
* @return bool
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function setCookie(
string $scope,
string $name,
string $value = "",
int $expire = 0,
string $path = "",
string $domain = "",
bool $secure = false,
bool $httponly = false
): bool {
if ($this->canSet($scope)) {
return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
}

if (isset($_COOKIE[$name])) {
return setcookie($name, "", time() - 360, $path, $domain, $secure, $httponly);
}

return false;
}

/**
* Fetch a cookie if there is consent. Also deletes cookies for which is no longer consent.
*
* @param string $scope
* @param string $name
* @param string $path
* @param string $domain
* @param boolean $secure
* @param boolean $httponly
*
* @return mixed
*
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function getCookie(
string $scope,
string $name,
string $path = "",
string $domain = "",
bool $secure = false,
bool $httponly = false
) {
if (isset($_COOKIE[$name])) {
if ($this->canSet($scope)) {
return $_COOKIE[$name];
}

setcookie($name, "", time() - 360, $path, $domain, $secure, $httponly);
}

return null;
}
}
76 changes: 76 additions & 0 deletions Cookie/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright (C) Jyxon, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Jyxon\GdprCookieCompliance\Cookie;

class Settings
{
/**
* Contains the allowed cookie scopes.
*
* @var array
*/
private $allowedCookies;

/**
* Contains the scope settings from the configuration file.
*
* @var array
*/
private $scopeSettings;

/**
* Constructor
*
* @param string $settingsPath
*/
public function __construct(string $settingsPath = '')
{
$settingsPath = ($settingsPath == '' ? dirname(__FILE__) . '/../Config/config.json' : $settingsPath);
$this->setScopeSettings($settingsPath);
$this->setAllowedCookies();
}

/**
* Returns the allowed cookies.
*
* @return array
*/
public function getAllowedCookies(): array
{
return $this->allowedCookies;
}

/**
* Sets the local variable of $allowedCookies with the contents of the "gdpr_cookie" contents.
*
* @return void
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function setAllowedCookies()
{
$allowed = (isset($_COOKIE["gdpr_cookie"]) ? explode(',', $_COOKIE["gdpr_cookie"]) : []);
foreach ($this->scopeSettings["scopes"] as $scope) {
if ($scope['required'] == 1) {
$allowed[] = $scope['scope'];
}
}

$this->allowedCookies = $allowed;
}

/**
* Fetches the settings from the config.json file.
*
* @param string $settingsPath
*
* @return void
*/
private function setScopeSettings(string $settingsPath)
{
$this->scopeSettings = json_decode(file_get_contents($settingsPath), true);
}
}
85 changes: 85 additions & 0 deletions Frontend/css/gdpr_cookie_bar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
.gdpr_cookie_bar {
position: fixed;
bottom: 0;
width: 100%;
background: #fff;
border-top: 1px solid #d9d9d9;
z-index: 9;
padding-bottom: 10px;
}

.gdpr_cookie_bar_title, .gdpr_cookie_bar_description {
margin-top: 5px;
text-align: center;
}

.gdpr_cookie_bar_title {
font-size: 18px;
font-weight: 600;
margin-top: 10px;
}

.gdpr_cookie_bar_option {
margin-bottom: 10px;
}

.gdpr_cookie_bar .gdpr_cookie_bar_option_checkbox, .gdpr_cookie_bar .gdpr_cookie_bar_option_title {
display: inline-block;
margin-right: 5px;
}

.gdpr_cookie_bar .gdpr_cookie_bar_option_description {
display: block;
}

.gdpr_cookie_bar .gdpr_cookie_bar_accept {
border: none;
background-color: #5ddb63;
color: #fff;
font-size: 16px;
cursor: pointer;
padding: 5px 10px;
}

.gdpr_cookie_bar.hidden {
bottom: -100%;
}

.gdpr_cookie_bar .gdpr_cookie_bar_close {
position: absolute;
top: 10px;
right: 10px;
width: 10px;
height: 10px;
cursor: pointer;
padding: 5px;
}

.gdpr_cookie_bar .gdpr_cookie_bar_close:after, .gdpr_cookie_bar .gdpr_cookie_bar_close:before {
content: "";
display: block;
width: 2px;
height: 20px;
background-color: #000;
position: absolute;
}

.gdpr_cookie_bar .gdpr_cookie_bar_close:after {
transform: rotate(-45deg);
}

.gdpr_cookie_bar .gdpr_cookie_bar_close:before {
transform: rotate(45deg);
}

.gdpr_cookie_settings_button {
border: none;
position: fixed;
bottom: 0;
right: 0;
z-index: 10;
background-color: #d9d9d9;
font-size: 16px;
cursor: pointer;
padding: 5px 10px;
}
31 changes: 31 additions & 0 deletions Frontend/js/cookie_tool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function cookie_tool()
{
return {
setCookie: (function (name, value, expiration, path = "/")
{
var currentDate = new Date();
currentDate.setDate(currentDate.getDate() + expiration);
var expires = "expires=" + currentDate.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=" + path + ";";
}),
getCookie: (function (name)
{
name += "=";
var decodedCookie = decodeURIComponent(document.cookie);
var variables = decodedCookie.split(';');
for (var i = 0; i < variables.length; i++)
{
var cookie = variables[i].trim();
if (cookie.indexOf(name) == 0)
{
return cookie.substring(name.length, cookie.length);
}
}
return "";
}),
deleteCookie: (function (name, path = "/")
{
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=" + path + ";";
})
};
}
Loading

0 comments on commit a4002b1

Please sign in to comment.