Skip to content

Instantly share code, notes, and snippets.

@francescor93
Created January 12, 2022 22:55
Show Gist options
  • Select an option

  • Save francescor93/ca1581fbdf5fb604de47abd56cab1655 to your computer and use it in GitHub Desktop.

Select an option

Save francescor93/ca1581fbdf5fb604de47abd56cab1655 to your computer and use it in GitHub Desktop.
Help server-side check of Iubenda policies consents
<?php
/**
* Iubenda Helper
*
* A PHP class to help check the consents status of Iubenda policies
* when checking needs to be done on the server side.
*
* // Just instantiate the class:
* $helper = new IubendaHelper();
* // Check if a cookie was found
* if ($helper->hasCookie()) {
* // And check generic or per purpose, based on your policy
* if ($helper->hasConsent()) {
* echo "Generic consent found";
* }
* if ($helper->hasConsent(1)) {
* echo "Per purpose consent found";
* }
* }
*
* Disclaimer:
* Slightly based on the original Iubenda class (https://github.com/iubenda/iubenda-cookie-class/blob/master/iubenda.class.php),
* this simplified class is intended to facilitate consents verification.
* However, like any human work, it is not guaranteed to be completely error free
* Furthermore, neither this development nor I are directly affiliated with Iubenda
*
* @author Francesco Rega <[email protected]>
* @version 1.0
* @access public
*/
class IubendaHelper {
/**
* Stores the cookie value
*
* @var string
*/
private string $cookie;
/**
* Constructor. Saves the value of the cookie in the "cookie" object property.
*
* If an additional $id parameter, corresponding to the site or policy identifier, is provided, it will be used to read that cookie directly.
* If not present, all existing cookies will be looped until the correct one is found.
*
* @param integer $id
*/
public function __construct(int $id = 0) {
if ($id) {
if (isset($_COOKIE['_iub_cs-' . $id])) {
$this->cookie = $_COOKIE['_iub_cs-' . $id];
}
} else {
foreach ($_COOKIE as $name => $value) {
$cookie = str_starts_with($name, '_iub_cs-');
if ($cookie) {
$this->cookie = $value;
}
}
}
}
/**
* Basic method to check if, after calling the constructor, a Iubenda cookie has been found or not.
*
* @return boolean
*/
public function hasCookie(): bool {
return !empty($this->cookie);
}
/**
* Method to check whether the user has given consent or not.
* If an additional $purpose parameter is provided, a search will be performed on per purpose consent, otherwise on generic consent.
*
* Be careful: you are not free to choose the one you prefer.
* If you have enabled per purpose consent in your policy, you will need to pass the $purpose parameter to this method.
*
* @param integer $purpose
*
* @return boolean
*/
public function hasConsent(int $purpose = 0): bool {
return ($purpose) ? $this->searchByPurpose($purpose) : $this->searchByGeneralConsent();
}
/**
* Method used to check if generic consent has been provided, by looking for "consent" to be true.
*
* @return boolean
*/
private function searchByGeneralConsent(): bool {
$value = json_decode(stripslashes($this->cookie));
if ((isset($value->consent)) && ($value->consent == true)) {
return true;
}
return false;
}
/**
* Method to check if a consent has been expressed for the purpose provided, verifying that its value is present and corresponding to true.
*
* @param integer $purpose
*
* @return boolean
*/
private function searchByPurpose(int $purpose): bool {
$value = json_decode(stripslashes($this->cookie));
if (!empty($value->purposes)) {
if ((isset($value->purposes->{$purpose})) && ($value->purposes->{$purpose} == true)) {
return true;
}
return false;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment