-
Notifications
You must be signed in to change notification settings - Fork 0
/
detector.php
410 lines (372 loc) · 10.6 KB
/
detector.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
use Shared\Registry as Registry;
use ClusterPoint\DB as DB;
use Shared\RequestMethods as RequestMethods;
Class Detector Extends Tracker {
/**
* @constructor
*/
public function __construct() {
$mongo = Registry::get("MongoDB");
if (!$mongo) {
$m = new MongoClient();
$mongo = $m->stats;
Registry::set("MongoDB", $mongo);
}
$parser = Registry::get("UAParser");
if (!$parser) {
$parser = UAParser\Parser::create();
Registry::set("UAParser", $parser);
}
}
/**
* Stores various actions
*/
protected static $_actions = array();
/**
* Stores various triggers
*/
protected static $_triggers = array();
/**
* Set actions if empty and returns the actions
* @return array
*/
protected function _actions() {
if (empty(self::$_actions)) {
$actions = array(
"1" => array(
"title" => "Do Nothing"
),
"2" => array(
"title" => "Wait"
),
"3" => array(
"title" => "Redirect"
),
"4" => array(
"title" => "POST Values"
),
"5" => array(
"title" => "Overlay Iframe"
),
"6" => array(
"title" => "Popup"
),
"7" => array(
"title" => "Hide Content"
),
"8" => array(
"title" => "Replace Content"
),
"9" => array(
"title" => "Send Email"
),
"10" => array(
"title" => "Run Javascript"
),
"11" => array(
"title" => "Run PHP"
)
);
self::$_actions = $actions;
}
return self::$_actions;
}
/**
* Set triggers if empty and returns the triggers
* @return array
*/
protected function _triggers() {
if (empty(self::$_triggers)) {
$triggers = array(
"1" => array(
"title" => "PageView",
"detect" => function ($opts) {
return true;
}
),
"2" => array(
"title" => "Location",
"detect" => function ($opts) {
$saved = strtolower($opts['saved']);
$current = strtolower($opts['user']['location']);
return $saved == $current;
}
),
"3" => array(
"title" => "Landing Page",
"detect" => function ($opts) {
$stored = strtolower($opts['saved']);
$current = strtolower($opts['server']['landingPage']);
return $current == $stored;
}
),
"4" => array(
"title" => "Time of Visit",
"detect" => function ($opts) {
$range = explode("-", $opts['saved']);
$start = $range[0];
$current = date('G:i');
$end = $range[1];
$start_time = strtotime($start);
$current_time = strtotime($current);
$end_time = strtotime($end);
return ($current_time > $start_time && $current_time < $end_time);
}
),
"5" => array(
"title" => "Bots",
"detect" => function ($opts) {
$bots = explode(",", $opts['saved']);
$response = false;
foreach ($bots as $b) {
if ($opts['user']['ua'] == trim($b)) {
$response = true;
break;
}
}
if (strtolower($opts['saved']) == 'crawler' && $opts['user']['ua_info']->device->family == "Spider") {
$response = true;
}
return $response;
}
),
"6" => array(
"title" => "IP Range",
"detect" => function ($opts) {
if (strpos($opts['saved'], '/') == false) {
$range.= '/32';
}
// $range is in IP/CIDR format eg 127.0.0.1/24
list($opts['saved'], $netmask) = explode('/', $opts['saved'], 2);
$range_decimal = ip2long($opts['saved']);
$ip_decimal = ip2long($opts['user']['ip']);
$wildcard_decimal = pow(2, (32 - $netmask)) - 1;
$netmask_decimal = ~ $wildcard_decimal;
return (($ip_decimal & $netmask_decimal) == ($range_decimal & $netmask_decimal));
}
),
"7" => array(
"title" => "User-Agent",
"verify" => function ($inputs) {},
"detect" => function ($opts) {
return ($opts['user']['ua'] == $opts['saved']);
}
),
"8" => array(
"title" => "Browser",
"detect" => function ($opts) {
$current = $opts['user']['ua_info']->ua->family;
$saved = $opts['saved'];
return stristr($current, $saved);
}
),
"9" => array(
"title" => "Operating System",
"detect" => function ($opts) {
$current = $opts['user']['ua_info']->os->family;
$saved = $opts['saved'];
return stristr($current, $saved);
}
),
"10" => array(
"title" => "Device Type",
"detect" => function ($opts) {
$saved = strtolower($opts['saved']);
$check = strtolower($opts['user']['ua_info']->device->family);
switch ($check) {
case 'other':
$result = 'desktop';
break;
case 'android':
$result = 'mobile';
break;
default:
if (stristr($check, "Smartphone")) {
$result = 'mobile';
} elseif (stristr($opts['user']['ua_info']->os->family, "Android")) {
$result = "mobile";
} else {
$result = false;
}
break;
}
if (!$result) {
if (stristr($opts['user']['ua_info']->ua->family, "Mobile")) {
$result = 'mobile';
} else {
$result = 'desktop';
}
}
return ($saved == $result);
}
),
"11" => array(
"title" => "Referrer",
"detect" => function ($opts) {
if (empty($opts['server']['referer']) && empty($opts['saved'])) {
return true;
} else if (empty($opts['server']['referer']) || empty($opts['saved'])) {
return false;
}
$response = stristr($opts['server']['referer'], $opts['saved']);
return ($response !== FALSE) ? true : false;
}
),
"12" => array(
"title" => "Active Login",
"detect" => function ($opts) {
return false;
}
),
"13" => array(
"title" => "Repeat Visitor",
"detect" => function ($opts) {
$cookie = $opts["cookies"];
return isset($cookie["__trafficMonitor"]) ? true : false;
}
)
);
self::$_triggers = $triggers;
}
return self::$_triggers;
}
public function execute() {
if (RequestMethods::post('plugin_detector') == 'getTrigger') {
$data = $this->_setOpts();
$mongo_db = Registry::get("MongoDB");
$website = $mongo_db->website->findOne(array("url" => $data['server']['name']));
if (!isset($website)) {
echo 'return 0;';
return;
}
$triggers = $mongo_db->triggers->find(array('website_id' => (int) $website["website_id"], 'live' => true));
$triggers = $this->_sortTriggers($triggers);
$code = ''; $trigs = array(); $acts = array();
$arr_triggers = $this->_triggers(); $arr_actions = $this->_actions();
foreach ($triggers as $t) {
$key = $t["title"];
if (isset($arr_triggers[$key]["detect"])) {
$data['saved'] = $t["meta"];
if (!call_user_func_array($arr_triggers[$key]["detect"], array($data))) {
continue;
}
$action = $mongo_db->actions->findOne(array("trigger_id" => (int) $t["trigger_id"]));
//$this->googleAnalytics($website, $t, $data['user']['location']);
$trigs[] = array('title' => $key, 'id' => $t['trigger_id']);
$acts[] = array('title' => $action['title'], 'id' => $action['action_id']);
$code .= $action["code"];
}
}
echo $code;
$this->_detectorLogs($trigs, $acts, $data, $website);
} else {
header("Location: http://trafficmonitor.ca");
exit();
}
}
protected function _sortTriggers($triggers) {
$arr = array();
foreach ($triggers as $t) {
$arr[$t['priority']] = $t;
}
ksort($arr);
return $arr;
}
protected function _log($message) {
$logfile = APP_PATH . "/logs/" . date("Y-m-d") . ".txt";
$new = file_exists($logfile) ? false : true;
if ($handle = fopen($logfile, 'a')) {
$timestamp = strftime("%Y-%m-%d %H:%M:%S", time());
$content = "[{$timestamp}]{$message}\n";
fwrite($handle, $content);
fclose($handle);
if ($new) {
chmod($logfile, 0755);
}
} else {
echo "Could not open log file for writing";
}
}
protected function _setOpts() {
$data = array();
$data['user']['ip'] = $this->get_client_ip($_POST);
$data['user']['ua'] = RequestMethods::post("HTTP_USER_AGENT", "Bot");
$parser = Registry::get("UAParser");
$user_agent = $parser->parse($data['user']['ua']);
try {
$c = $this->country($data['user']['ip']);
} catch (\Exception $e) {
$c = "IN";
}
$data['user']['location'] = $c;
$data['user']['ua_info'] = $user_agent;
$data['server']['name'] = RequestMethods::post("HTTP_HOST");
$data['server']['landingPage'] = 'http://'. $data['server']['name']. RequestMethods::post("REQUEST_URI");
$data['server']['referer'] = RequestMethods::post("HTTP_REFERER", "");
$data["posted"] = RequestMethods::post("p");
$data["cookies"] = RequestMethods::post("c");
$data["session"] = RequestMethods::post("s");
return $data;
}
public function googleAnalytics($website, $trigger, $country) {
$data = array(
"v" => 1,
"tid" => "",
"cid" => $trigger["user_id"],
"t" => "pageview",
"dp" => $trigger["trigger_id"],
"uid" => $trigger["user_id"],
"ua" => "TrafficMonitor",
"cn" => $trigger["title"],
"cs" => $trigger["user_id"],
"cm" => "TrafficMonitor",
"ck" => $website["title"],
"ci" => $trigger["user_id"],
"dl" => $website["title"],
"dh" => $website["url"],
"dp" => $trigger["title"],
"dt" => $country
);
$url = "https://www.google-analytics.com/collect?".http_build_query($data);
// Get cURL resource
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'CloudStuff'
));
$resp = curl_exec($curl);
curl_close($curl);
}
/**
* Stores the each request logs in Mongo
*/
protected function _detectorLogs($t, $action, $data, $website) {
$hits = Registry::get("MongoDB")->logs;
$where = array(
'website_id' => (int) $website['website_id'],
'user_ip' => $data['user']['ip'],
'referer' => ($data['server']['referer'] ? $data['server']['referer'] : "Blank Referer"),
'landing_page' => $data['server']['landingPage']
);
$record = $hits->findOne($where);
if (isset($record)) {
$hits->update($where, array(
'$set' => array('created' => new \MongoDate())
));
} else {
$doc = array(
'user_id' => (int) $website["user_id"],
'triggers' => $t,
'actions' => $action,
'created' => new \MongoDate(),
'user_location' => $data['user']['location'],
'user_agent' => $data['user']['ua_info']->originalUserAgent,
'user_type' => ($data['user']['ua_info']->device->family == "Spider") ? "Bot" : "Person"
);
$doc = array_merge($doc, $where);
$hits->insert($doc);
}
}
}