forked from diegolamonica/EUCookieLaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheucookielaw-header.php
83 lines (59 loc) · 2.89 KB
/
eucookielaw-header.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* EUCookieLaw: simple object to accomplish european law requirements about cookie transmission to client
* @version 1.0
* @link https://github.com/diegolamonica/EUCookieLaw/
* @author Diego La Monica (diegolamonica) <[email protected]>
* @copyright 2015 Diego La Monica
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html GNU Lesser General Public License
* @note This program is distributed in the hope that it will be useful - WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
function euCookieLaw_callback($buffer){
if(!isset($_COOKIE['__eucookielaw'])) {
$headers = headers_list();
foreach ( $headers as $header ) {
if ( preg_match( "#^Set-Cookie:#", $header ) ) {
header( 'Set-Cookie:' );
break;
}
}
if ( preg_match( '#<script\W[^>]*(data-eucookielaw="block")[^>]*>.*?</script>#ms', $buffer, $items ) ) {
$buffer = str_replace( $items[0], '', $buffer );
}
!defined('EUCOOKIELAW_DISALLOWED_DOMAINS') && define('EUCOOKIELAW_DISALLOWED_DOMAINS', '');
!defined('EUCOOKIELAW_LOOK_IN_SCRIPTS') && define('EUCOOKIELAW_LOOK_IN_SCRIPTS', false);
if(EUCOOKIELAW_DISALLOWED_DOMAINS!='') {
! defined( 'EUCOOKIELAW_LOOK_IN_TAGS' ) && define( 'EUCOOKIELAW_LOOK_IN_TAGS', 'iframe|srcript|link' );
$disallowedDomains = preg_split( "#[;\n]#", EUCOOKIELAW_DISALLOWED_DOMAINS );
foreach ( $disallowedDomains as $disallowedDomain ) {
$disallowedDomain = trim($disallowedDomain);
if ( !empty($disallowedDomain) ) {
// Non empty tags (eg. <iframe>...</iframe>)
$multiLineTagRegExp = '#<(' . EUCOOKIELAW_LOOK_IN_TAGS . ')\W[^>]*(href|src)=("|\')(http(s)?:)?//' . preg_quote( $disallowedDomain, "#" ) . '.*?(\\3)[^>]*>.*?</\\1>#ms';
if ( preg_match( $multiLineTagRegExp, $buffer, $items ) ) {
$replaced = str_replace($items[4] . '//' . $disallowedDomain, 'about:blank', $items[0]);
$buffer = str_replace( $items[0], $replaced, $buffer );
}
// Empty tags ( eg. <link href="..." />)
$singleLineTagRegExp = '#<(' . EUCOOKIELAW_LOOK_IN_TAGS . ')\W[^>]*(href|src)=("|\')(http(s)?:)?//' . preg_quote( $disallowedDomain, "#" ) . '.*?("|\').*?/>#ms';
if ( preg_match( $singleLineTagRegExp, $buffer, $items ) ) {
$buffer = str_replace( $items[0], '', $buffer );
}
if(EUCOOKIELAW_LOOK_IN_SCRIPTS){
$pattern = "#<script[^>]*>(.*?)</script>#ims";
if(preg_match_all($pattern, $buffer, $matches)){
foreach($matches[1] as $index => $match){
if(strpos($match, $disallowedDomain)!==false){
$buffer = str_replace($match, "<![CDATA[\n//Removed by EUCookieLaw\n]]>", $buffer );
}
}
}
}
}
}
}
}
return $buffer;
}
ob_start("euCookieLaw_callback");