-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.php
291 lines (236 loc) · 12.4 KB
/
notification.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
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
// use PHPMailer\PHPMailer\PHPMailer;
// use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
// require 'vendor/autoload.php';
define("servername", "localhost");
define("dbusername", "root");
define("dbpassword", "");
define("dbname", "mydb");
$json = json_decode(file_get_contents('php://input'), true);
$lat = $json['lat'];
$lng = $json['lng'];
$username= $json['username'];
$age= $json['age'];
$gender= $json['gender'];
$category= $json['category'];
$response=array();
function findPointsInsideCircle($lat, $lng, $username, $age, $gender, $category){
$conn = new mysqli(servername, dbusername, dbpassword, dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// $sql="SELECT id, username, email, type, ( 6371 * acos ( cos ( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin ( radians($lat) ) * sin( radians( lat ) ) ) ) < radius AS result FROM circle WHERE NOW() BETWEEN startDateTime AND endDateTime AND status='Active' AND ageFrom <='$age' AND ageTo >='$age' AND gender='$gender' Having result=1";
$sql="SELECT id, username, email, type, ( 6371 * acos ( cos ( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin ( radians($lat) ) * sin( radians( lat ) ) ) ) < radius AS result FROM circle WHERE NOW() BETWEEN startDateTime AND endDateTime AND status='Active' AND ageFrom <='$age' AND ageTo >='$age' AND gender='$gender' AND FIND_IN_SET($category, category) Having result=1";
$result = $conn->query($sql);
$count = 0;
$arrayofData=array();
if ($result->num_rows > 0) {
//output data of each row
while ($row = $result->fetch_assoc()) {
array_push($arrayofData, $row);
}
foreach ($arrayofData as $key => $value) {
//query to update status to inside in the tractusers table
$query="INSERT INTO trackusers (id, creator, type, latLng, geofence_id, status, dateTime) VALUES ('$username', '{$value['username']}', '{$value['type']}', ST_GEOMFROMTEXT('POINT($lat $lng)'), {$value['id']}, 'INSIDE', NOW() )";
$result=$conn->query($query);
if ($result) {
//save tracked user's data into activegeo table
$query2="INSERT INTO activegeofence (id, creator, type ,latLng, geofence_id, status, dateTime) VALUES ('$username', '{$value['username']}', '{$value['type']}' , ST_GEOMFROMTEXT('POINT($lat $lng)'), {$value['id']}, 'INSIDE', NOW()) ON DUPLICATE KEY UPDATE dateTime=NOW()";
$result2=$conn->query($query2);
if ($result2) {
//echo "Could not successfully run query ($result2) from DB: " . mysqli_error($conn);
return "Found Circle";
}
}
// $mail = new PHPMailer();
// try {
// //Server settings
// // $mail->SMTPDebug = 2; // Enable verbose debug output
// $mail->isSMTP(); // Set mailer to use SMTP
// $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
// $mail->SMTPAuth = true; // Enable SMTP authentication
// $mail->Username = '[email protected]'; // SMTP username
// $mail->Password = 'bienvenumb1'; // SMTP password
// $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
// $mail->Port = 587; // TCP port to connect to
// //Recipients
// $mail->setFrom('[email protected]', 'Plot');
// $mail->addAddress("{$value['email']}", 'Barry'); // Add a recipient
// // $mail->addAddress('[email protected]'); // Name is optional
// // $mail->addReplyTo('[email protected]', 'Information');
// // $mail->addCC('[email protected]');
// // $mail->addBCC('[email protected]');
// //Attachments
// // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// //date
// $datetime=date('D, d M Y H:i:s');
// //Content
// $mail->isHTML(true); // Set email format to HTML
// $mail->Subject = 'User just entered your geofence';
// $mail->Body = "<H1>Bellow are the details</H1><br>".
// "Geofence Name: {$value['username']}"."<br>".
// "User Entered: $username"."<br>".
// "Time: $datetime <br>";
// $mail->AltBody ='This is the body in plain text for non-HTML mail clients';
// $mail->send();
// //echo 'Message has been sent';
// } catch (Exception $e) {
// echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
// }
unset($arrayofData);
}
} else {
//check the users inside the active goefen
$query="SELECT * FROM activegeofence where id='$username' and type='Circle'";
$resultquery= $conn->query($query);
$arrayofgeofences = array();
if($resultquery->num_rows > 0){
while ($row= $resultquery->fetch_assoc()) {
array_push($arrayofgeofences, $row);
}
//update status exit
foreach ($arrayofgeofences as $key=> $value) {
$trackquery2="INSERT INTO trackusers (id, creator, type, latLng, geofence_id, status, dateTime) VALUES ('$username', '{$value['creator']}', '{$value['type']}', ST_GEOMFROMTEXT('POINT($lat $lng)'), {$value['geofence_id']}, 'EXIT', NOW())";
$trackresult2 = $conn->query($trackquery2);
if ($trackresult2) {
//delete row in the table
$trackquery2="DELETE FROM activegeofence WHERE id='$username' and geofence_id= {$value['geofence_id']}";
$trackresult2 = $conn->query($trackquery2);
if ($trackresult2) {
// echo "Could not successfully run query ($trackresult2) from DB: " . mysqli_error($conn);
return "Exit Circle";
}
return $arrayofgeofences;
}
unset($arrayofgeofences);
}
} else{
//update status to no geofence
$trackquery2="INSERT INTO trackusers (id, latLng, status, dateTime) VALUES ('$username', ST_GEOMFROMTEXT('POINT($lat $lng)'), 'NO GEOFENCE', NOW())";
$trackresult2 = $conn->query($trackquery2);
if ($trackresult2) {
// echo "Could not successfully run query ($trackresult2) from DB: " . mysqli_error($conn);
return "No Geofence";
}
}
}
$conn->close();
}
//end of circle
function findPointsInsidePolygon($lat, $lng, $username, $age, $gender, $category){
$conn = new mysqli(servername, dbusername, dbpassword, dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//start of polygon notification
// $sql="SELECT id, username, email, type, ST_CONTAINS(points, ST_GEOMFROMTEXT('POINT($lat $lng)')) as result FROM polygon WHERE NOW() BETWEEN startDateTime AND endDateTime AND status='Active' AND ageFrom <='$age' AND ageTo >='$age' AND gender='$gender' Having result=1";
$sql="SELECT id, username, email, type, ST_CONTAINS(points, ST_GEOMFROMTEXT('POINT($lat $lng)')) as result FROM polygon WHERE NOW() BETWEEN startDateTime AND endDateTime AND status='Active' AND ageFrom <='$age' AND ageTo >='$age' AND gender='$gender' AND FIND_IN_SET($category, category) Having result=1";
$result = $conn->query($sql);
$count = 0;
$arrayofData=array();
if ($result->num_rows > 0) {
//output data of each row
while ($row = $result->fetch_assoc()) {
array_push($arrayofData, $row);
}
foreach ($arrayofData as $key => $value) {
//query to update status to inside in the tractusers table
$query="INSERT INTO trackusers (id, creator, type, latLng, geofence_id, status, dateTime) VALUES ('$username', '{$value['username']}', '{$value['type']}', ST_GEOMFROMTEXT('POINT($lat $lng)'), {$value['id']}, 'INSIDE', NOW() )";
$result=$conn->query($query);
if ($result) {
//save tracked user's data into activegeo table
$query2="INSERT INTO activegeofence (id, creator, type ,latLng, geofence_id, status, dateTime) VALUES ('$username', '{$value['username']}', '{$value['type']}' , ST_GEOMFROMTEXT('POINT($lat $lng)'), {$value['id']}, 'INSIDE', NOW()) ON DUPLICATE KEY UPDATE dateTime=NOW()";
$result2=$conn->query($query2);
if ($result2) {
// echo "Could not successfully run query ($result2) from DB: " . mysqli_error($conn);
return "Found Polygon";
}
}
// $mail = new PHPMailer();
// try {
// //Server settings
// // $mail->SMTPDebug = 2; // Enable verbose debug output
// $mail->isSMTP(); // Set mailer to use SMTP
// $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
// $mail->SMTPAuth = true; // Enable SMTP authentication
// $mail->Username = '[email protected]'; // SMTP username
// $mail->Password = 'bienvenumb1'; // SMTP password
// $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
// $mail->Port = 587; // TCP port to connect to
// //Recipients
// $mail->setFrom('[email protected]', 'Plot');
// $mail->addAddress("{$value['email']}", 'Barry'); // Add a recipient
// // $mail->addAddress('[email protected]'); // Name is optional
// // $mail->addReplyTo('[email protected]', 'Information');
// // $mail->addCC('[email protected]');
// // $mail->addBCC('[email protected]');
// //Attachments
// // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// //date
// $datetime=date('D, d M Y H:i:s');
// //Content
// $mail->isHTML(true); // Set email format to HTML
// $mail->Subject = 'User just entered your geofence';
// $mail->Body = "<H1>Bellow are the details</H1><br>".
// "Geofence Name: {$value['username']}"."<br>".
// "User Entered: $username"."<br>".
// "Time: $datetime <br>";
// $mail->AltBody ='This is the body in plain text for non-HTML mail clients';
// $mail->send();
// //echo 'Message has been sent';
// } catch (Exception $e) {
// echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
// }
unset($arrayofData);
}
} else {
//check the users inside the active goefen
$query="SELECT * FROM activegeofence where id='$username' and type='Polygon'";
$resultquery= $conn->query($query);
$arrayofgeofences = array();
if($resultquery->num_rows > 0){
while ($row= $resultquery->fetch_assoc()) {
array_push($arrayofgeofences, $row);
}
//update status exit
foreach ($arrayofgeofences as $key=> $value) {
$trackquery2="INSERT INTO trackusers (id, creator, type, latLng, geofence_id, status, dateTime) VALUES ('$username', '{$value['creator']}', '{$value['type']}', ST_GEOMFROMTEXT('POINT($lat $lng)'), {$value['geofence_id']}, 'EXIT', NOW())";
$trackresult2 = $conn->query($trackquery2);
if ($trackresult2) {
//delete row in the table
$trackquery2="DELETE FROM activegeofence WHERE id='$username' and geofence_id= {$value['geofence_id']}";
$trackresult2 = $conn->query($trackquery2);
if ($trackresult2) {
// echo "Could not successfully run query ($trackresult2) from DB: " . mysqli_error($conn);
return "Exit Polygon";
}
}
return $arrayofgeofences;
unset($arrayofgeofences);
}
} else{
//update status to no geofence
$trackquery2="INSERT INTO trackusers (id, latLng, status, dateTime) VALUES ('$username', ST_GEOMFROMTEXT('POINT($lat $lng)'), 'NO GEOFENCE', NOW())";
$trackresult2 = $conn->query($trackquery2);
if ($trackresult2) {
// echo "Could not successfully run query ($trackresult2) from DB: " . mysqli_error($conn);
return "No Geofence";
}
}
}
$conn->close();
}
//function caller conditions
if(findPointsInsideCircle($lat, $lng, $username, $age, $gender, $category) && findPointsInsidePolygon($lat, $lng, $username, $age, $gender, $category)){
$var1 = findPointsInsideCircle($lat, $lng, $username, $age, $gender, $category);
$var2 = findPointsInsidePolygon($lat, $lng, $username, $age, $gender, $category);
array_push($response, $var1);
array_push($response, $var2);
}
print_r(json_encode($response));
?>