-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.php
164 lines (142 loc) · 4.8 KB
/
tracker.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
<?php
use GeoIp2\Database\Reader;
/**
* Tracker Class
* @author Faizan Ayubi
*/
class Tracker {
public $item;
function __construct($item_id) {
$this->initialize($item_id);
$this->googleAnalytics();
if (!$this->is_bot($_SERVER["HTTP_USER_AGENT"])) {
$c = $this->cookie();
if ($c < 3) {
$this->mongo();
}
}
}
/**
* checks if cookie exists then return true else returns false and creates cookie
* @return bool
*/
public function cookie() {
$cookie = "i".$this->item->id."u".$this->item->user_id;
$value = 1;
if(!isset($_COOKIE[$cookie])) {
setcookie($cookie, $value);
$_COOKIE[$cookie] = $value;
} else {
$value = $_COOKIE[$cookie];
setcookie($cookie, ++$value);
}
return $value;
}
public function is_bot($user_agent) {
$crawlersNames = 'Bloglines subscriber|Dumbot|Sosoimagespider|QihooBot|FAST-WebCrawler|Superdownloads Spiderman|LinkWalker|msnbot|ASPSeek|WebAlta Crawler|Lycos|FeedFetcher-Google|Yahoo|YoudaoBot|AdsBot-Google|Googlebot|Scooter|Gigabot|Charlotte|eStyle|AcioRobot|GeonaBot|msnbot-media|Baidu|CocoCrawler|Google|Charlotte t|Yahoo! Slurp China|Sogou web spider|YodaoBot|MSRBOT|AbachoBOT|Sogou head spider|AltaVista|IDBot|Sosospider|Yahoo! Slurp|Java VM|DotBot|LiteFinder|Yeti|Rambler|Scrubby|Baiduspider|accoona|Google|msnbot|Rambler|Yahoo|AbachoBOT|accoona|AcioRobot|ASPSeek|CocoCrawler|Dumbot|FAST-WebCrawler|GeonaBot|Gigabot|Lycos|MSRBOT|Scooter|AltaVista|IDBot|eStyle|Scrubby';
return !empty($user_agent) ? preg_match("/{$crawlersNames}/i", $user_agent) > 0 : false;
}
public function toObject($array) {
$result = new \stdClass();
foreach ($array as $key => $value) {
$result->{$key} = $value;
}
return $result;
}
public function initialize($item_id) {
$item = array();
$str = base64_decode($item_id);
$datas = explode("&", $str);
foreach ($datas as $data) {
$property = explode("=", $data);
$item[$property[0]] = $property[1];
}
$this->item = $this->toObject($item);
}
function image() {
$img = explode(".", $this->item->image);
$cdn = CDN . "resize/{$img[0]}-". DIMENSION .".{$img[1]}";
return $cdn;
}
public function googleAnalytics() {
$target = parse_url($this->item->url);
$data = array(
"v" => 1,
"tid" => GA,
"cid" => $this->item->id,
"t" => "pageview",
"dp" => $this->item->id,
"uid" => $this->item->user_id,
"ua" => "CloudStuff",
"cn" => $this->item->title,
"cs" => $this->item->user_id,
"cm" => ADNETWORK,
"ck" => $this->item->username,
"ci" => $this->item->id,
"dl" => $this->item->url,
"dh" => $target["host"],
"dp" => $target["path"],
"dt" => $this->item->title
);
$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);
}
public function mongo() {
$today = strftime("%Y-%m-%d", strtotime('now'));
$country = $this->country();
$m = new MongoClient();
$db = $m->stats;
$collection = $db->hits;
$doc = array(
'item_id' => $this->item->id,
'user_id' => $this->item->user_id,
'click' => 1,
'country' => $country,
'created' => $today
);
$record = $collection->findOne(array('item_id' => $this->item->id,'user_id' => $this->item->user_id, 'country' => $country, 'created' => $today));
if (isset($record)) {
$collection->update(array('item_id' => $this->item->id,'user_id' => $this->item->user_id,'country' => $country,'created' => $today), array('$set' => array("click" => $record["click"]+1)));
} else{
$collection->insert($doc);
}
}
public function country($ip) {
// This creates the Reader object, which should be reused across
// lookups.
$reader = new Reader(maxmind_db_path);
// Replace "city" with the appropriate method for your database, e.g.,
// "country".
$record = $reader->country($ip);
return $record->country->isoCode;
}
function get_client_ip($arr = null) {
$ipaddress = '';
if (!$arr) {
$arr = &$_SERVER;
}
if (isset($arr['HTTP_CLIENT_IP']))
$ipaddress = $arr['HTTP_CLIENT_IP'];
else if(isset($arr['HTTP_X_FORWARDED_FOR']))
$ipaddress = $arr['HTTP_X_FORWARDED_FOR'];
else if(isset($arr['HTTP_X_FORWARDED']))
$ipaddress = $arr['HTTP_X_FORWARDED'];
else if(isset($arr['HTTP_FORWARDED_FOR']))
$ipaddress = $arr['HTTP_FORWARDED_FOR'];
else if(isset($arr['HTTP_FORWARDED']))
$ipaddress = $arr['HTTP_FORWARDED'];
else if(isset($arr['REMOTE_ADDR']))
$ipaddress = $arr['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
}