forked from hngi/StartNGContacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlt.php
270 lines (234 loc) · 10.3 KB
/
lt.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
ob_start();
$er = error_reporting(0);
require_once dirname(__FILE__).'/admin/inc/unregister_globals.php';
require_once dirname(__FILE__).'/admin/inc/magic_quotes.php';
//# none of our parameters can contain html for now
$_GET = removeXss($_GET);
$_POST = removeXss($_POST);
$_REQUEST = removeXss($_REQUEST);
if (isset($_SERVER['ConfigFile']) && is_file($_SERVER['ConfigFile'])) {
include $_SERVER['ConfigFile'];
} elseif (is_file('config/config.php')) {
include 'config/config.php';
} else {
echo "Error, cannot find config file\n";
exit;
}
require_once dirname(__FILE__).'/admin/init.php';
$GLOBALS['database_module'] = basename($GLOBALS['database_module']);
$GLOBALS['language_module'] = basename($GLOBALS['language_module']);
require_once dirname(__FILE__).'/admin/'.$GLOBALS['database_module'];
// load default english and language
include_once dirname(__FILE__).'/admin/defaultFrontendTexts.php';
// Allow customisation per installation
if (is_file($_SERVER['DOCUMENT_ROOT'].'/'.$GLOBALS['language_module'])) {
include_once $_SERVER['DOCUMENT_ROOT'].'/'.$GLOBALS['language_module'];
}
require_once dirname(__FILE__).'/admin/inc/random_compat/random.php';
include_once dirname(__FILE__).'/admin/languages.php';
require_once dirname(__FILE__).'/admin/defaultconfig.php';
require_once dirname(__FILE__).'/admin/connect.php';
include_once dirname(__FILE__).'/admin/lib.php';
if (isset($_GET['tid'])) {
if (!is_string($_GET['tid'])) {
echo 'Invalid Request';
exit;
}
$tid = $_GET['tid'];
if (SIGN_WITH_HMAC) {
$hmac = $_GET['hm'];
if (empty($hmac)) {
echo 'Invalid Request';
exit;
}
$myUrl = sprintf('%s://%s%s', $_SERVER['REQUEST_SCHEME'], $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI']);
$myUrl = str_replace('&hm='.$hmac, '', $myUrl);
if (!hash_equals(hash_hmac(HASH_ALGO, $myUrl, HMACKEY), $hmac)) {
echo 'Invalid Request';
exit;
}
}
if (strlen($tid) == 64) {
$tid = str_replace(' ', '+', $tid);
$dec = bin2hex(base64_decode($tid));
$track = 'T|'.substr($dec, 0, 8).'-'.substr($dec, 8, 4).'-4'.substr($dec, 13, 3).'-'.substr($dec, 16, 4).'-'.substr($dec, 20, 12).'|'.
substr($dec, 32, 8).'-'.substr($dec, 40, 4).'-4'.substr($dec, 45, 3).'-'.substr($dec, 48, 4).'-'.substr($dec, 52, 12).'|'.
substr($dec, 64, 8).'-'.substr($dec, 72, 4).'-4'.substr($dec, 77, 3).'-'.substr($dec, 80, 4).'-'.substr($dec, 84, 12);
} else {
$track = base64_decode($tid);
$track = $track ^ XORmask;
}
if (!preg_match(
'/^(H|T)
\|([a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12})
\|([a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12})
\|([a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12})$/x',
$track,
$matches
)) {
FileNotFound();
}
$msgtype = $matches[1];
$fwduuid = $matches[2];
$messageuuid = $matches[3];
$useruuid = $matches[4];
// print $msgtype . '<br/>';
// print $fwduuid . '<br/>';
// print $messageuuid . '<br/>';
// print $useruuid . '<br/>';
$linkdata = Sql_Fetch_Assoc_query(sprintf('select * from %s where uuid = "%s"', $GLOBALS['tables']['linktrack_forward'],
$fwduuid));
if (empty($linkdata)) {
FileNotFound();
}
$fwdid = $linkdata['id'];
$userdata = Sql_Fetch_array_query(sprintf('select id from %s where uuid = "%s"', $GLOBALS['tables']['user'],
$useruuid));
if (empty($userdata)) {
FileNotFound();
}
$userid = $userdata['id'];
$messagedata = Sql_Fetch_array_query(sprintf('select id from %s where uuid = "%s"', $GLOBALS['tables']['message'],
$messageuuid));
if (empty($messagedata)) {
FileNotFound();
}
$messageid = $messagedata['id'];
$allowPersonalised = true;
} elseif (isset($_GET['id'])) {
if (!is_string($_GET['id'])) {
echo 'Invalid Request';
exit;
}
$id = $_GET['id'];
$track = base64_decode($id);
$track = $track ^ XORmask;
if (!preg_match('/^(H|T)\|([1-9]\d*)\|([1-9]\d*)\|([1-9]\d*)$/', $track, $matches)) {
FileNotFound();
}
$msgtype = $matches[1];
$fwdid = $matches[2];
$messageid = $matches[3];
$userid = $matches[4];
$linkdata = Sql_Fetch_array_query(sprintf('select * from %s where id = %d', $GLOBALS['tables']['linktrack_forward'],
$fwdid));
if (!$linkdata) {
//# try the old table to avoid breaking links
$linkdata = Sql_Fetch_array_query(sprintf('select * from %s where linkid = %d and userid = %d and messageid = %d',
$GLOBALS['tables']['linktrack'], $fwdid, $userid, $messageid));
if (!empty($linkdata['forward'])) {
//# we're not recording clicks, but at least links from older phpList versions won't break.
header('Location: '.$linkdata['forward'], true, 303);
exit;
}
// echo 'Invalid Request';
// maybe some logging?
FileNotFound();
}
//# verify that this subscriber actually received this message, otherwise they're allowed
//# normal URLS on test messages, but not personalised ones
$allowed = Sql_Fetch_Row_Query(sprintf('select userid from %s where userid = %d and messageid = %d',
$GLOBALS['tables']['usermessage'], $userid, $messageid));
$allowPersonalised = empty($allowed[0])
? !empty($_SESSION['adminloggedin'])
: true;
} else {
echo 'Invalid Request';
exit;
}
//# hmm a bit heavy to use here @@@optimise
$messagedata = loadMessageData($messageid);
$trackingcode = '';
//print "$track<br/>";
//print "User $userid, Mess $messageid, Link $linkid";
$ml = Sql_Fetch_Array_Query(sprintf('select * from %s where messageid = %d and forwardid = %d',
$GLOBALS['tables']['linktrack_ml'], $messageid, $fwdid));
if (empty($ml['firstclick'])) {
Sql_query(sprintf('update %s set firstclick = now(),latestclick = now(),clicked = clicked + 1 where forwardid = %d and messageid = %d',
$GLOBALS['tables']['linktrack_ml'], $fwdid, $messageid));
} else {
Sql_query(sprintf('update %s set clicked = clicked + 1, latestclick = now() where forwardid = %d and messageid = %d',
$GLOBALS['tables']['linktrack_ml'], $fwdid, $messageid));
}
if ($msgtype == 'H') {
Sql_query(sprintf('update %s set htmlclicked = htmlclicked + 1 where forwardid = %d and messageid = %d',
$GLOBALS['tables']['linktrack_ml'], $fwdid, $messageid));
$trackingcode = 'utm_source=phplist'.$messageid.'&utm_medium=email&utm_content=HTML&utm_campaign='.urlencode($messagedata['subject']);
} else {
Sql_query(sprintf('update %s set textclicked = textclicked + 1 where forwardid = %d and messageid = %d',
$GLOBALS['tables']['linktrack_ml'], $fwdid, $messageid));
$trackingcode = 'utm_source=phplist'.$messageid.'&utm_medium=email&utm_content=text&utm_campaign='.urlencode($messagedata['subject']);
}
$viewed = Sql_Fetch_Row_query(sprintf('select viewed from %s where messageid = %d and userid = %d',
$GLOBALS['tables']['usermessage'], $messageid, $userid));
if (!$viewed[0]) {
Sql_Query(sprintf('update %s set viewed = now() where messageid = %d and userid = %d',
$GLOBALS['tables']['usermessage'], $messageid, $userid));
Sql_Query(sprintf('update %s set viewed = viewed + 1 where id = %d',
$GLOBALS['tables']['message'], $messageid));
$metaData = array();
foreach (array('HTTP_USER_AGENT', 'HTTP_REFERER') as $key) {
if (isset($_SERVER[$key])) {
$metaData[$key] = htmlspecialchars(strip_tags($_SERVER[$key]));
}
}
Sql_Query(sprintf('insert into %s (messageid,userid,viewed,ip,data) values(%d,%d,now(),"%s","%s")',
$GLOBALS['tables']['user_message_view'], $messageid, $userid, $_SERVER['REMOTE_ADDR'], sql_escape(serialize($metaData))));
}
$uml = Sql_Fetch_Array_Query(sprintf('select * from %s where messageid = %d and forwardid = %d and userid = %d',
$GLOBALS['tables']['linktrack_uml_click'], $messageid, $fwdid, $userid));
if (empty($uml['firstclick'])) {
Sql_query(sprintf('insert into %s set firstclick = now(), forwardid = %d, messageid = %d, userid = %d',
$GLOBALS['tables']['linktrack_uml_click'], $fwdid, $messageid, $userid));
}
Sql_query(sprintf('update %s set clicked = clicked + 1, latestclick = now() where forwardid = %d and messageid = %d and userid = %d',
$GLOBALS['tables']['linktrack_uml_click'], $fwdid, $messageid, $userid));
if ($msgtype == 'H') {
Sql_query(sprintf('update %s set htmlclicked = htmlclicked + 1 where forwardid = %d and messageid = %d and userid = %d',
$GLOBALS['tables']['linktrack_uml_click'], $fwdid, $messageid, $userid));
} elseif ($msgtype == 'T') {
Sql_query(sprintf('update %s set textclicked = textclicked + 1 where forwardid = %d and messageid = %d and userid = %d',
$GLOBALS['tables']['linktrack_uml_click'], $fwdid, $messageid, $userid));
}
$url = $linkdata['url'];
if ($linkdata['personalise']) {
if (!$allowPersonalised) {
FileNotFound('<br/><i>'.s('Profile links in test campaigns only work when you are logged in as an administrator.').'</i><br/>');
}
$uid = Sql_Fetch_Row_Query(sprintf('select uniqid from %s where id = %d', $GLOBALS['tables']['user'], $userid));
if ($uid[0]) {
if (strpos($url, '?')) {
$url .= '&uid='.$uid[0];
} else {
$url .= '?uid='.$uid[0];
}
}
}
//print "$url<br/>";
if (!isset($_SESSION['entrypoint'])) {
$_SESSION['entrypoint'] = $url;
}
if (!empty($messagedata['google_track'])) {
//# take off existing tracking code, if found
if (strpos($url, 'utm_medium') !== false) {
$url = preg_replace('/utm_(\w+)\=[^&]+/', '', $url);
}
//# 16894 make sure to keep the fragment value at the end of the URL
if (strpos($url, '#')) {
list($tmplink, $fragment) = explode('#', $url);
$url = $tmplink;
unset($tmplink);
$fragment = '#'.$fragment;
} else {
$fragment = '';
}
if (strpos($url, '?')) {
$url = $url.'&'.$trackingcode.$fragment;
} else {
$url = $url.'?'.$trackingcode.$fragment;
}
}
//print "Location $url"; exit;
header('Location: '.$url, true, 303); //# use 303, because Location only uses 302, which gets indexed
exit;