1 | <?php |
||
2 | |||
3 | namespace LeKoala\CookieConsent; |
||
4 | |||
5 | use SilverStripe\i18n\i18n; |
||
6 | use SilverStripe\View\Requirements; |
||
7 | use SilverStripe\SiteConfig\SiteConfig; |
||
8 | use SilverStripe\Core\Config\Configurable; |
||
9 | use SilverStripe\Control\Cookie; |
||
10 | use SilverStripe\ORM\DataObject; |
||
11 | use SilverStripe\Control\Director; |
||
12 | |||
13 | /** |
||
14 | * Add cookie consent to your website |
||
15 | * |
||
16 | * @link https://www.cookieconsent.com/ |
||
17 | * @link https://cookiesandyou.com/ |
||
18 | * @link https://www.cookiebot.com/en/gdpr-cookies/ |
||
19 | */ |
||
20 | class CookieConsent |
||
21 | { |
||
22 | use Configurable; |
||
23 | |||
24 | // an array like {"strictly-necessary":true,"functionality":true,"tracking":false,"targeting":false} |
||
25 | const COOKIE_CONSENT_LEVEL = 'cookie_consent_level'; |
||
26 | // true/false depending on sttate |
||
27 | const COOKIE_CONSENT_ACCEPTED = 'cookie_consent_user_accepted'; |
||
28 | |||
29 | /** |
||
30 | * @config |
||
31 | * @var string |
||
32 | */ |
||
33 | private static $use_cdn = false; |
||
34 | |||
35 | /** |
||
36 | * @config |
||
37 | * @var string |
||
38 | */ |
||
39 | private static $opts = [ |
||
40 | "notice_banner_type" => "interstitial", |
||
41 | "consent_type" => "express", |
||
42 | "palette" => "dark", |
||
43 | "change_preferences_selector" => "#cookieconsent-preferences", |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Add requirements |
||
48 | * |
||
49 | * Make sure to call this AFTER you have defined scripts that should be loaded conditionally |
||
50 | * @link https://stackoverflow.com/questions/45794634/loading-google-analytics-after-page-load-by-appending-script-in-head-doesnt-alw |
||
51 | * @return void |
||
52 | */ |
||
53 | public static function requirements() |
||
54 | { |
||
55 | $SiteConfig = null; |
||
56 | if (class_exists(SiteConfig::class)) { |
||
57 | $SiteConfig = SiteConfig::current_site_config(); |
||
58 | } |
||
59 | |||
60 | $conf = self::config(); |
||
61 | |||
62 | // options to pass to js constructor |
||
63 | $opts = $conf->opts; |
||
64 | |||
65 | // some options are autoconfigured |
||
66 | $opts['language'] = self::getLanguage(); |
||
67 | // otherwise you can set it manually in yml |
||
68 | if ($SiteConfig) { |
||
69 | $opts['website_name'] = $SiteConfig->Title; |
||
70 | } |
||
71 | |||
72 | $privacyLink = 'https://cookiesandyou.com/'; |
||
73 | // If we have a privacy notice, use it! |
||
74 | if ($conf->privacy_notice_class && class_exists($conf->privacy_notice_class)) { |
||
75 | $privacyNotice = DataObject::get_one($conf->privacy_notice_class); |
||
76 | if ($privacyNotice) { |
||
77 | $privacyLink = $privacyNotice->Link(); |
||
78 | } |
||
79 | } |
||
80 | $opts['website_privacy_policy_url'] = $privacyLink; |
||
81 | |||
82 | $jsonOpts = json_encode($opts); |
||
83 | |||
84 | // Include script |
||
85 | $use_cdn = self::config()->use_cdn; |
||
86 | if ($use_cdn) { |
||
87 | Requirements::javascript("//www.cookieconsent.com/releases/4.0.0/cookie-consent.js"); |
||
88 | } else { |
||
89 | Requirements::javascript("lekoala/silverstripe-cookieconsent:javascript/cookie-consent.js"); |
||
90 | } |
||
91 | |||
92 | // Include custom init |
||
93 | $js = <<<JS |
||
94 | cookieconsent.run($jsonOpts); |
||
95 | JS; |
||
96 | Requirements::customScript($js, 'CookiesConsentInit'); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get a valid language based on current locale |
||
101 | * @return string |
||
102 | */ |
||
103 | public static function getLanguage() |
||
104 | { |
||
105 | $lang = substr(i18n::get_locale(), 0, 2); |
||
106 | if (in_array($lang, self::getAvailableLanguages())) { |
||
107 | return $lang; |
||
108 | } |
||
109 | return 'en'; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return array |
||
114 | */ |
||
115 | public static function getAvailableLanguages() |
||
116 | { |
||
117 | return [ |
||
118 | 'en', |
||
119 | 'de', |
||
120 | 'fr', |
||
121 | 'es', |
||
122 | 'ca_es', |
||
123 | 'it', |
||
124 | 'nl', |
||
125 | 'pt', |
||
126 | 'fi', |
||
127 | 'hu', |
||
128 | 'cs', |
||
129 | 'hr', |
||
130 | 'da', |
||
131 | 'sl', |
||
132 | 'pl', |
||
133 | 'ro', |
||
134 | 'sr', |
||
135 | 'bg', |
||
136 | 'cy', |
||
137 | ]; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Clear requirements, useful if you don't want any popup on a specific page after init |
||
142 | * |
||
143 | * @return void |
||
144 | */ |
||
145 | public static function clearRequirements() |
||
146 | { |
||
147 | $use_cdn = self::config()->use_cdn; |
||
148 | if ($use_cdn) { |
||
149 | Requirements::clear("//www.cookieconsent.com/releases/3.1.0/cookie-consent.js"); |
||
150 | } else { |
||
151 | Requirements::clear("lekoala/silverstripe-cookieconsent:javascript/cookie-consent-3.1.0.min.js"); |
||
152 | } |
||
153 | Requirements::clear('CookiesConsentInit'); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return bool |
||
158 | */ |
||
159 | public static function isAllowed() |
||
160 | { |
||
161 | return (bool)Cookie::get(self::COOKIE_CONSENT_ACCEPTED); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Helper method to set cookies if accepted |
||
166 | * |
||
167 | * @param string $name |
||
168 | * @param string $value |
||
169 | * @param integer $expiry |
||
170 | * @param bool $httpOnly |
||
171 | * @return void |
||
172 | */ |
||
173 | public static function setCookie($name, $value, $expiry = 90, $httpOnly = true) |
||
174 | { |
||
175 | if (self::isAllowed()) { |
||
176 | $secure = Director::is_https(); |
||
177 | $path = $domain = null; |
||
178 | return Cookie::set($name, $value, $expiry, $path, $domain, $secure, $httpOnly); |
||
179 | } |
||
180 | return false; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return void |
||
185 | */ |
||
186 | public static function clearStatus() |
||
187 | { |
||
188 | return Cookie::force_expiry(self::COOKIE_CONSENT_ACCEPTED); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return void |
||
193 | */ |
||
194 | public static function forceAllow() |
||
195 | { |
||
196 | return Cookie::set(self::COOKIE_CONSENT_ACCEPTED, true, 90, null, null, false, false); |
||
197 | } |
||
198 | } |
||
199 |