Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
davide-alghi committed Jul 14, 2021
0 parents commit 9606487
Show file tree
Hide file tree
Showing 35 changed files with 2,211 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# EditorConfig is awesome: http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

# TS/JS-Files
[*.{ts,js}]
indent_size = 2

# JSON-Files
[*.json]
indent_style = tab

# ReST-Files
[*.rst]
indent_size = 3
max_line_length = 80

# YAML-Files
[*.{yaml,yml}]
indent_size = 2

# package.json
[package.json]
indent_size = 2

# TypoScript
[*.{typoscript,tsconfig}]
indent_size = 2

# XLF-Files
[*.xlf]
indent_style = tab

# SQL-Files
[*.sql]
indent_style = tab
indent_size = 2

# .htaccess
[{_.htaccess,.htaccess}]
indent_style = tab
158 changes: 158 additions & 0 deletions Classes/Cookie/CookieManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

declare(strict_types = 1);

/**
* This file is part of the TYPO3 CMS extension.
* The extension name is: Cookie Consent Plus.
* The extension key is: cookieconsent_plus.
* Cookie Consent Plus extends dp_cookieconsent TYPO3 extension
* The developer is Davide Alghi (Abbiategrasso - Italy).
* Cookie Consent Plus Copyright (C) 2021 Davide Alghi.
* All Rights Reserved.
* Cookie Consent Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Cookie Consent Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Cookie Consent Plus. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html.
* See the file LICENSE.md for copying conditions.
* Website: https://www.penguinable.it
*
* @category TYPO3
* @copyright 2021 Davide Alghi
* @author Davide Alghi <davide@penguinable.it>
* @license GPLv3
*/

namespace PAD\CookieconsentPlus\Cookie;

class CookieManager
{
const COOKIECONSENTSTATUS_NAME = 'cookieconsent_status';
const COOKIECONSENTSTATUS_OPTOUT_NAME = 'dp_cookieconsent_status';
const COOKIEDISMISSVALUE = 'dismiss';
const COOKIEALLOWVALUE = 'allow';
const COOKIEDENYVALUE = 'deny';
protected $cookieValue = '';
protected $optoutCookieValue = '';
protected $cookieStatus = false;
protected $mandatoryCookieStatus = false;
protected $statisticsCookieStatus = false;
protected $marketingCookieStatus = false;

/**
* Sets statuses from cookies value
*
* @param void
*/
public function __construct()
{
if (isset($_COOKIE[self::COOKIECONSENTSTATUS_NAME])) {
$this->cookieValue = $_COOKIE[self::COOKIECONSENTSTATUS_NAME];
if ($this->cookieValue) {
$this->cookieStatus = $_COOKIE[self::COOKIECONSENTSTATUS_NAME] == self::COOKIEDENYVALUE ? false : true;
if ($this->cookieStatus) {
$this->mandatoryCookieStatus = true;
if ($this->cookieValue != self::COOKIEDISMISSVALUE && isset($_COOKIE[self::COOKIECONSENTSTATUS_OPTOUT_NAME])) {
$this->optoutCookieValue = $_COOKIE[self::COOKIECONSENTSTATUS_OPTOUT_NAME];
$optoutCookieValueArray = json_decode($_COOKIE[self::COOKIECONSENTSTATUS_OPTOUT_NAME], true);
$this->statisticsCookieStatus = (boolean) $optoutCookieValueArray['dp--cookie-statistics'];
$this->marketingCookieStatus = (boolean) $optoutCookieValueArray['dp--cookie-marketing'];
} else {
$this->statisticsCookieStatus = true;
$this->marketingCookieStatus = true;
}
}
}
}
}

/**
* Returns cookieconsent_status cookie value
*
* @param void
* @return string
*/
public function getCookieValue(): string
{
return $this->cookieValue;
}

/**
* Returns dp_cookieconsent_status cookie value
* in array format
*
* @param void
* @return array
*/
public function getOptoutCookieValue(): array
{
return json_decode($this->optoutCookieValue, true);
}

/**
* Returns cookies status
*
* @param void
* @return bool
*/
public function getCookieStatus(): bool
{
return $this->cookieStatus;
}

/**
* Returns mandatory cookies status
* accepted: true, denied: false
*
* @param void
* @return bool
*/
public function isMandatoryOn(): bool
{
return $this->mandatoryCookieStatus;
}

/**
* Returns statistics cookies status
* accepted: true, denied: false
*
* @param void
* @return bool
*/
public function isStatisticsOn(): bool
{
return $this->statisticsCookieStatus;
}

/**
* Returns marketing cookies status
* accepted: true, denied: false
*
* @param void
* @return bool
*/
public function isMarketingOn(): bool
{
return $this->marketingCookieStatus;
}

/**
* Removes cookie with defined
* name, path and domain
*
* @param string $name - the cookie name
* @param string $path - the path for which the cookie is valid
* @param string $domain - the domain from which the cookie comes
* @return void
*/
public function removeCookie(string $name, string $path, string $domain): void
{
setcookie($name, '', time() - 3600, $path, $domain);
}
}
165 changes: 165 additions & 0 deletions Classes/Xclass/PageRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

declare(strict_types = 1);

/**
* This file is part of the TYPO3 CMS extension.
* The extension name is: Cookie Consent Plus.
* The extension key is: cookieconsent_plus.
* Cookie Consent Plus extends dp_cookieconsent TYPO3 extension
* The developer is Davide Alghi (Abbiategrasso - Italy).
* Cookie Consent Plus Copyright (C) 2021 Davide Alghi.
* All Rights Reserved.
* Cookie Consent Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Cookie Consent Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Cookie Consent Plus. If not, see https://www.gnu.org/licenses/gpl-3.0.en.html.
* See the file LICENSE.md for copying conditions.
* Website: https://www.penguinable.it
*
* @category TYPO3
* @copyright 2021 Davide Alghi
* @author Davide Alghi <davide@penguinable.it>
* @license GPLv3
*/

namespace PAD\CookieconsentPlus\Xclass;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Domain\Repository\PageRepository as CmsPageRepository;
use PAD\CookieconsentPlus\Cookie\CookieManager;

class PageRepository extends CmsPageRepository
{
const ISCOOKIESDEPENDENT_FIELD = 'tx_cookieconsentplus_iscookiesdependent';
const ISCOOKIESDEPENDENT_OFF = 0;
const ISCOOKIESDEPENDENT_ON = 1;
const CONDITIONTYPE_FIELD = 'tx_cookieconsentplus_conditiontype';
const CONDITIONTYPE_VALUE_SHOWAND = 'showand';
const CONDITIONTYPE_VALUE_SHOWOR = 'showor';
const CONDITIONTYPE_VALUE_HIDEAND = 'hideand';
const CONDITIONTYPE_VALUE_HIDEOR = 'hideor';
const MANDATORYCONDITION_FIELD = 'tx_cookieconsentplus_mandatorycondition';
const STATISTICSCONDITION_FIELD = 'tx_cookieconsentplus_statisticscondition';
const MARKETINGCONDITION_FIELD = 'tx_cookieconsentplus_marketingcondition';
const CONDITION_VALUE_ANYVALUE = 'anyvalue';
const CONDITION_VALUE_DENIED = 'denied';
const CONDITION_VALUE_ACCEPTED = 'accepted';

protected $allowedTables = [
'pages',
'tt_content',
];

/**
* Returns enable fields (constraints) for cookies dependency
*
* @param string $table - table on which to create the query
* @return string
*/
protected function getCookiesEnableFields($table): string
{
$constraints = [];
if (in_array($table, $this->allowedTables)) {
$cookieManager = GeneralUtility::makeInstance(CookieManager::class);
$isMandatoryOn = $cookieManager->isMandatoryOn();
$isStatisticsOn = $cookieManager->isStatisticsOn();
$isMarketingOn = $cookieManager->isMarketingOn();
$mandatoryValues = [
self::CONDITION_VALUE_ANYVALUE,
$isMandatoryOn ? self::CONDITION_VALUE_ACCEPTED : self::CONDITION_VALUE_DENIED,
];
$statisticsValues = [
self::CONDITION_VALUE_ANYVALUE,
$isStatisticsOn ? self::CONDITION_VALUE_ACCEPTED : self::CONDITION_VALUE_DENIED,
];
$marketingValues = [
self::CONDITION_VALUE_ANYVALUE,
$isMarketingOn ? self::CONDITION_VALUE_ACCEPTED : self::CONDITION_VALUE_DENIED,
];
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
$expressionBuilder = $queryBuilder->expr();
$mandatoryValues = array_map([$expressionBuilder, 'literal'], $mandatoryValues);
$statisticsValues = array_map([$expressionBuilder, 'literal'], $statisticsValues);
$marketingValues = array_map([$expressionBuilder, 'literal'], $marketingValues);
$constraints[] = $expressionBuilder->eq(
self::ISCOOKIESDEPENDENT_FIELD,
self::ISCOOKIESDEPENDENT_OFF
);
$constraints[] = $expressionBuilder->andX(
$expressionBuilder->eq(
self::ISCOOKIESDEPENDENT_FIELD,
self::ISCOOKIESDEPENDENT_ON
),
$expressionBuilder->eq(
self::CONDITIONTYPE_FIELD,
$expressionBuilder->literal(self::CONDITIONTYPE_VALUE_SHOWAND)
),
$expressionBuilder->andX(
$expressionBuilder->in(
self::MANDATORYCONDITION_FIELD,
$mandatoryValues
),
$expressionBuilder->in(
self::STATISTICSCONDITION_FIELD,
$statisticsValues
),
$expressionBuilder->in(
self::MARKETINGCONDITION_FIELD,
$marketingValues
),
)
);
$constraints[] = $expressionBuilder->andX(
$expressionBuilder->eq(
self::ISCOOKIESDEPENDENT_FIELD,
self::ISCOOKIESDEPENDENT_ON
),
$expressionBuilder->eq(
self::CONDITIONTYPE_FIELD,
$expressionBuilder->literal(self::CONDITIONTYPE_VALUE_SHOWOR)
),
$expressionBuilder->orX(
$expressionBuilder->in(
self::MANDATORYCONDITION_FIELD,
$mandatoryValues
),
$expressionBuilder->in(
self::STATISTICSCONDITION_FIELD,
$statisticsValues
),
$expressionBuilder->in(
self::MARKETINGCONDITION_FIELD,
$marketingValues
),
)
);
}
return strval($expressionBuilder->orX(...$constraints));
}

/**
* {@inheritdoc}
*
* In addition cookies sql constraints
* are added to enableFields query
*/
public function enableFields($table, $show_hidden = -1, $ignore_array = [], $noVersionPreview = false): string
{
$enableFields = parent::enableFields($table, $show_hidden, $ignore_array, $noVersionPreview);
if (in_array($table, $this->allowedTables)) {
$cookiesEnableFields = $this->getCookiesEnableFields($table);
if (!empty($cookiesEnableFields)) {
$enableFields .= ' AND (' . $cookiesEnableFields . ')';
}
}
return $enableFields;
}
}
Loading

0 comments on commit 9606487

Please sign in to comment.