-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathackee.class.php
208 lines (194 loc) · 6.65 KB
/
ackee.class.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*
Package: Ackee-PHP
Version: 1.0
File: ackee.class.php
License: GPL-2.0+
(c) 2020 Brooke. Some Rights Reserved
*/
namespace Ackee\PHP;
if (!class_exists('Core')) {
/**
* Core Class for implementing Ackee via PHP
*/
class Core
{
private $dataOpts = array();
/**
* Validates and returns the URL of the Ackee instance.
*
* @since 1.0.0
* @access private
* @return string
*/
private static function getInstallURL()
{
if (!defined('ACKEE_INSTALL_URL')) {
trigger_error('Define ACKEE_INSTALL_URL or no tracker will be echoed', E_USER_WARNING);
} else {
$url = filter_var(ACKEE_INSTALL_URL, FILTER_VALIDATE_URL);
if (false === $url) {
trigger_error('ACKEE_INSTALL_URL is not a valid URL', E_USER_WARNING);
} else {
return rtrim($url, '/');
}
}
}
/**
* Validates and returns the tracker name from the Ackee instance.
*
* @since 1.0.0
* @access private
* @return string
*/
private static function getTrackerName()
{
if (!defined('ACKEE_TRACKER')) {
trigger_error('Define ACKEE_TRACKER or no tracker will be echoed', E_USER_WARNING);
} else {
$trackerName = filter_var(ACKEE_TRACKER, FILTER_SANITIZE_STRING);
if (!preg_match('/^([a-zA-Z0-9-_]+).js$/', $trackerName)) {
trigger_error('ACKEE_TRACKER does not end in js or contains invalid characters', E_USER_WARNING);
} else {
return $trackerName;
}
}
}
/**
* Validates and returns the domain ID from the Ackee instance.
*
* @since 1.0.0
* @access private
* @return string
*/
private static function getDomainID()
{
if (!defined('ACKEE_DOMAIN_ID')) {
trigger_error('Define ACKEE_DOMAIN_ID or no tracker will be echoed', E_USER_WARNING);
} else {
$trackerID = filter_var(ACKEE_DOMAIN_ID, FILTER_SANITIZE_STRING);
if (!preg_match('/^([a-zA-Z0-9-]+$)/', $trackerID)) {
trigger_error('ACKEE_DOMAIN_ID is not a valid ID', E_USER_WARNING);
} else {
return $trackerID;
}
}
}
/**
* Boolen to determine if the detailed data should be collected
*
* Either define ACKEE_DETAILED as true or to a string that will be the name of a cookie
* which if set as true will make this function return true.
*
* @since 1.0.0
* @link https://github.com/electerious/Ackee/blob/master/docs/Anonymization.md#personal-data
* @access private
* @return bool
*/
private static function useDetailed()
{
if (!defined('ACKEE_DETAILED')) {
return false;
} elseif (
ACKEE_DETAILED === true ||
isset($_COOKIE[ACKEE_DETAILED]) &&
filter_var($_COOKIE[ACKEE_DETAILED], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === true
) {
return true;
} else {
return false;
}
}
/**
* Boolen to determine if the visitor has opted-out via a cookie
*
* If ACKEE_OPT_OUT_COOKIE is defined and set to true the visit will not be tracked
*
* @since 1.0.0
* @access private
* @return bool
*/
private static function respectOptOutCookie()
{
if (
defined('ACKEE_OPT_OUT_COOKIE') &&
isset($_COOKIE[ACKEE_OPT_OUT_COOKIE]) &&
filter_var($_COOKIE[ACKEE_OPT_OUT_COOKIE], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === true
) {
return true;
} else {
return false;
}
}
/**
* Boolen to determine if the visitor has enabled Do Not Track (DNT) in their browser
*
* If ACKEE_DISABLE_DNT is defined and not false this setting will have no effect
*
* @since 1.0.0
* @access private
* @return bool
*/
private static function respectDNT()
{
if (
defined('ACKEE_DISABLE_DNT') &&
filter_var(ACKEE_DISABLE_DNT, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== false &&
filter_var(ACKEE_DISABLE_DNT, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null
) {
return false;
} elseif (array_key_exists('HTTP_DNT', $_SERVER) && (1 === (int) $_SERVER['HTTP_DNT'])) {
return true;
} else {
return false;
}
}
/**
* Returns the data-ackee-opts
*
* You can add options by passing settings into the $dataOpts global
*
* @since 1.0.0
* @link https://github.com/electerious/Ackee/blob/master/docs/Anonymization.md#personal-data
* @access private
* @return string
*/
private static function buildDataOpts($dataOpts)
{
if (self::useDetailed()) {
$dataOpts['detailed'] = true;
}
if (is_array($dataOpts) && !empty($dataOpts)) {
return "data-ackee-opts='" . json_encode(($dataOpts)) . "'";
}
}
/**
* Echos the Ackee Script onto the page.
*
* @since 1.0.0
* @access public
* @return string
*/
public function ackeeTracker()
{
global $dataOpts;
$installDomain = self::getInstallURL();
$trackerName = self::getTrackerName();
$domainID = self::getDomainID();
$dataOpsArray = self::buildDataOpts($dataOpts);
if ((bool)self::respectDNT() || (bool)self::respectOptOutCookie()) {
return false;
} else {
echo(
'<script async src="' . $installDomain . '/' . $trackerName . '" ' .
'data-ackee-server="' . $installDomain . '" ' .
'data-ackee-domain-id="' . $domainID . '" ' .
$dataOpsArray . '> ' .
'</script>' . "\r\n"
);
}
}
// end class
}
//end check if class class_exists
}