-
Notifications
You must be signed in to change notification settings - Fork 1
/
fvls_process.php
288 lines (226 loc) · 8.17 KB
/
fvls_process.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
<?php
/*
//
// Fresh Vine Link Shortener
//
//
// Page Purpose:
// This includes all of the data processing for the service
//
// Version: 1.0
//
*/
if( defined('FVLS_PROCESS_INCLUDE') )
return;
define('FVLS_PROCESS_INCLUDE', true);
if( !isset( $FLVS_db_link ) || is_null( $FLVS_db_link ) ){
if( defined('FVLS_DEVELOPER_MODE') && FVLS_DEVELOPER_MODE )
echo 'FV Link Shortener Error: You must first connect to the database';
exit();
}
//
// Load up Browscap | used to parse out the user_agent string.
ini_set("memory_limit","512M"); // Needed to successfully cache the browser cache
require 'fvls_Browscap.php';
use phpbrowscap\Browscap; // The Browscap class is in the phpbrowscap namespace, so import it
/**
* Check if the short tag exists, and if it does - return the desired link
**/
function FVLS_CheckShortTag( $ShortTag ){
//
// Check if the short tag is set
global $fvls_TablePrefix;
$TableName = $fvls_TablePrefix . 'link';
$ShortTag = addslashes( $ShortTag ); // Ensure no shenanigans
$response = fvls_db_ExecuteQuery( "SELECT * FROM `$TableName`
WHERE `ShortTag` = '$ShortTag'
AND `Active` = '1'" ); // Look for this short tag
if( $response->num_rows !== 1 )
return false; // not a valid response
// Get short tag info
$row = $response->fetch_assoc();
//
// Lets log this click
$tmp = FVLS_LogClick( $ShortTag );
return $row['EndLink'];
}
/**
* Will log the basics about the particular click
**/
function FVLS_LogClick( $ShortTag ){
$ShortTag = addslashes( stripslashes( $ShortTag ) );
//
// Build the query
//
// Required Values
$Insert = array('ShortTag' => $ShortTag);
$Insert['IPAddress'] = addslashes( $_SERVER['REMOTE_ADDR'] );
// $Insert['IPAddress'] = '8.8.8.8'; // TEMP for testing
$Insert['UserAgentKey'] = sha1( $_SERVER['HTTP_USER_AGENT'] );
//
// Optional Values
if( isset( $_SERVER['HTTP_REFERER'] ) ){
$Insert['RefererURL'] = addslashes( $_SERVER['HTTP_REFERER'] );
$parsed = parse_url( $Insert['RefererURL'] );
if( isset( $parsed['host'] ) )
$Insert['RefererDomain'] = $parsed['host'];
}
if( isset( $_REQUEST['l'] ) )
$Insert['Location'] = addslashes( $_REQUEST['l'] ); // Grab the location aspect
if( isset( $_REQUEST['p'] ) )
$Insert['PersonID'] = addslashes( $_REQUEST['p'] ); // Grab the person ID aspect
if( isset( $_REQUEST['a'] ) )
$Insert['VariableA'] = addslashes( $_REQUEST['a'] ); // Grab the variable a
if( isset( $_REQUEST['b'] ) )
$Insert['VariableB'] = addslashes( $_REQUEST['b'] ); // Grab the variable b
if( isset( $_REQUEST['c'] ) )
$Insert['VariableC'] = addslashes( $_REQUEST['c'] ); // Grab the variable c
// Optional Values
//
global $fvls_TablePrefix;
$TableName = $fvls_TablePrefix . 'link_click';
$sql = "INSERT INTO `$TableName` (`". implode( '`,`', array_keys( $Insert ) ) ."`)
VALUES ('". implode( "','", $Insert ) ."');";
// Build the query
//
//
// Save the click
$response = fvls_db_ExecuteQuery( $sql );
//
// Check the Response - ensure we added 1 row
if( fvls_db_AffectedRows() != 1 )
return false;
//
// Check if the User Agent needs to be added
global $fvls_TablePrefix;
$TableName = $fvls_TablePrefix . 'link_ua';
$sqlUAcheck = "SELECT * FROM `$TableName`
WHERE `UAkey` = '$Insert[UserAgentKey]';";
$response = fvls_db_ExecuteQuery( $sqlUAcheck );
if( $response->num_rows == 0 ){
$uas = addslashes( $_SERVER['HTTP_USER_AGENT'] );
$sqlUAadd = "INSERT INTO `$TableName` (`UAkey`, `UAstring`)
VALUES ('$Insert[UserAgentKey]', '$uas');";
$response = fvls_db_ExecuteQuery( $sqlUAadd );
if( fvls_db_AffectedRows() != 1 )
return false;
}
// Check if the User Agent needs to be added
//
return true;
}
/**
* Process through the clicks that have not had their IP and User Agent processed yet
**/
function FVLS_ProcessClicks(){
//
// Process the IP addresses
global $fvls_TablePrefix;
$TableName = $fvls_TablePrefix . 'link_click';
$sqlCheck = "SELECT * FROM `$TableName`
WHERE `Processed` = '0'
GROUP BY `IPAddress`;";
$results = fvls_db_ExecuteQuery( $sqlCheck );
if( $results->num_rows > 0 ){
while( $rows = $results->fetch_assoc() ){
$endpoint = "http://freegeoip.net/json/" . $rows['IPAddress']; // Possible alternative would be http://ipinfo.io/
$IPCoded = $Geo = fvls_curl( $endpoint, array(), 'get', array(), true );
if( !is_array( $IPCoded ) )
$Geo = json_decode( $IPCoded, true );
if( $IPCoded === false || !is_array( $Geo ) )
continue; // Unable to process
if( array_key_exists('error', $Geo ) && $Geo['error'] == 400 )
continue;
//
// Prepare the Update
$Update = array('Processed' => 1 );
if( isset( $Geo['country_code'] ) && $Geo['country_code'] != '' )
$Update['Country'] = $Geo['country_code'];
if( isset( $Geo['region_code'] ) && $Geo['region_code'] != '' )
$Update['Region'] = $Geo['region_code'];
if( isset( $Geo['city'] ) && $Geo['city'] != '' )
$Update['City'] = $Geo['city'];
if( isset( $Geo['latitude'] ) && $Geo['latitude'] != '' )
$Update['Latitude'] = $Geo['latitude'];
if( isset( $Geo['longitude'] ) && $Geo['longitude'] != '' )
$Update['Longitude'] = $Geo['longitude'];
//
// Build out the query
$SetTemp = array();
foreach( $Update as $k => $v )
$SetTemp[] = "`$k` = '" . addslashes( $v ) . "'";
$SetTemp = implode( ', ', $SetTemp );
global $fvls_TablePrefix;
$TableName = $fvls_TablePrefix . 'link_click';
$sqlUpdate = "UPDATE `$TableName`
SET $SetTemp
WHERE `IPAddress` = '$rows[IPAddress]'
AND `Processed` = '0'";
fvls_db_ExecuteQuery( $sqlUpdate ); // process the query
if( fvls_db_AffectedRows() == 0 )
return false; // Something is not right here
}
}
// Process the IP addresses
//
//
// Process the User Agents
global $fvls_TablePrefix;
$TableName = $fvls_TablePrefix . 'link_ua';
$sqlCheck = "SELECT * FROM `$TableName`
WHERE `DeviceType` IS NULL;"; // Lets us update them in batches
$results = fvls_db_ExecuteQuery( $sqlCheck );
if( $results->num_rows > 0 ){
$CachePath = FVLS_APP_PATH . 'includes/';
if( defined( 'FVLS_CACHE_PATH' ) && !is_null( FVLS_CACHE_PATH ) )
$CachePath = FVLS_CACHE_PATH; // prep the cache path
while( $rows = $results->fetch_assoc() ){
$bc = new Browscap($CachePath); // Create a new Browscap object (loads or creates the cache)
// Get information about the current browser's user agent
$browser = $bc->getBrowser( $rows['UAstring'] );
$Update = array();
$Update['DeviceType'] = 'Other'; // 'Desktop', 'Mobile', 'Crawler', 'Other'
if( $browser->Crawler )
$Update['DeviceType'] = 'Crawler'; // Web Crawler
else if( $browser->isMobileDevice == 1 )
$Update['DeviceType'] = 'Mobile';
else if( $browser->isMobileDevice == 0 )
$Update['DeviceType'] = 'Desktop';
//
// Learn about the Device
$Update['DeviceManufactor'] = $browser->Device_Maker;
$Update['DeviceName'] = $browser->Device_Name;
//
// Learn about the operating system
$Update['OS_Vendor'] = $browser->Platform;
$version = explode( '.', $browser->Platform_Version, 2 );
var_dump( $version );
$Update['OS_VersionMajor'] = $version[0];
$Update['OS_VersionMinor'] = 0;
if( array_key_exists( 1, $version ) )
$Update['OS_VersionMinor'] = $version[1];
//
// Learna about the browser being used
$Update['Browser'] = $browser->Browser;
$Update['BrowserVersion'] = $browser->Version;
//
// Build out the query
$SetTemp = array();
foreach( $Update as $k => $v )
$SetTemp[] = "`$k` = '" . addslashes( $v ) . "'";
$SetTemp = implode( ', ', $SetTemp );
global $fvls_TablePrefix;
$TableName = $fvls_TablePrefix . 'link_ua';
$sqlUpdate = "UPDATE `$TableName`
SET $SetTemp
WHERE `UAkey` = '$rows[UAkey]';"; // Lets us update them in batches
fvls_db_ExecuteQuery( $sqlUpdate ); // process the query
// if( fvls_db_AffectedRows() == 0 )
// return false; // Something is not right here
}
}
// Process the User Agents
//
return true;
}
?>