-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_page.php
More file actions
275 lines (236 loc) · 10.9 KB
/
fetch_page.php
File metadata and controls
275 lines (236 loc) · 10.9 KB
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
<!--Fetch Page-->
<!--Fetch results for both Twitter and Facebook from database depending on the user input-->
<!--By Shiyi Zhang and Wei-Ling Chin-->
<?php
include("config.php");
// Initialize the variables
$input = "";
$geotagged = False;
$profile = False;
$geoword = False;
$networking = False;
$facebook = False;
$twitter = False;
// Check if we have user input from prototypePage.php
if (isset($_POST['arguments'])) {
// Check if each checkbox is ticked or not
foreach($_POST['arguments'] as $key => $value){
if($value=="geotagged") {
$geotagged = True;
}
elseif ($value=="profile") {
$profile = True;
}
elseif($value=="geoword") {
$geoword = True;
}
elseif($value=="networking") {
$networking = True;
}
elseif($value=="facebook") {
$facebook = True;
}
elseif($value=="twitter") {
$twitter = True;
} else {
$input = $value;
}
}
} else {
// Print out an error message if not user input is given after clicking the search button
echo "Error: Cannot get input";
}
// Initialize the query for each checkbox
$query_geotagged_fb = "";
$query_profile_fb = "";
$query_geoword_fb = "";
$query_networking_fb = "";
$query_geotagged_twitter = "";
$query_profile_twitter = "";
$query_geoword_twitter = "";
$query_networking_twitter = "";
$final_query = "";
// Check for valid input
if(empty($input)){
exit("Error: Please input a search word");
}
// Get twitter results
if($twitter){
$twitter_query = "SELECT user_id FROM `post_twitter` where tweet_text LIKE '%".$input."%'";
$results = mysqli_query($connecDB, $twitter_query);
// Output results from database
echo '<table class="result">';
while($row = mysqli_fetch_array($results))
{
$user_id = $row['user_id'];
$city = "None";
// Get the user location information using the geo-tagged method, the result will be assigned into the $city variable
if($geotagged){
// Query data from the api_twitter table which uses the geo-tagged method
$query = "SELECT * FROM api_twitter WHERE user_id=".$user_id;
$outputs = mysqli_query($connecDB, $query);
if(mysqli_num_rows($outputs) != 0){
while($row_inner = mysqli_fetch_array($outputs)){
$lat = $row_inner['geo_lat'];
$long = $row_inner['geo_long'];
$city = $row_inner['city'];
$country = $row_inner['country'];
$cnt = "1";
}
}
}
// If no location information can be found, use the user profile method to locate this user
if($city == "None" && $profile){
// Query data from the profile_twitter table which uses the user profile method
$query = "SELECT profile_twitter.location_id, locations.city, locations.country, locations.geo_lat, locations.geo_long FROM profile_twitter JOIN locations ON profile_twitter.location_id = locations.location_id WHERE profile_twitter.`user_id` =".$user_id;
$outputs = mysqli_query($connecDB, $query);
if(mysqli_num_rows($outputs) != 0){
while($row_inner = mysqli_fetch_array($outputs)){
$lat = $row_inner['geo_lat'];
$long = $row_inner['geo_long'];
$city = $row_inner['city'];
$country = $row_inner['country'];
$cnt = "1";
}
}
}
// If no location information can be found, use the social network method to locate this user
if($city == "None" && $networking){
// Query data from the networking table which uses the social network method
$query = "SELECT networking.location_id, locations.city, locations.country, locations.geo_lat, locations.geo_long FROM networking JOIN locations ON networking.location_id = locations.location_id WHERE networking.`user_id` =".$user_id;
$outputs = mysqli_query($connecDB, $query);
if(mysqli_num_rows($outputs) != 0){
while($row_inner = mysqli_fetch_array($outputs)){
$lat = $row_inner['geo_lat'];
$long = $row_inner['geo_long'];
$city = $row_inner['city'];
$country = $row_inner['country'];
$cnt = "1";
}
}
}
// If no location information can be found, use the geo-word table to locate the user
if($city == "None" && $geoword){
// Query data from the geoword table which uses the geo-word method
$query = "SELECT geoword.location_id, locations.city, locations.country, locations.geo_lat, locations.geo_long FROM geoword JOIN locations ON geoword.location_id = locations.location_id WHERE geoword.`user_id` =".$user_id;
$outputs = mysqli_query($connecDB, $query);
if(mysqli_num_rows($outputs) != 0){
while($row_inner = mysqli_fetch_array($outputs)){
$lat = $row_inner['geo_lat'];
$long = $row_inner['geo_long'];
$city = $row_inner['city'];
$country = $row_inner['country'];
$cnt = "1";
}
}
}
// If no location information can be found using the four geolocation methods, the user is considered as unlocatable
if($city=="None" || empty($city)){
continue;
}
// Display the locations of the users onto a table
echo '<tr>';
echo '<td>' . $country . '</td><td>' . $city . '</td><td>' . $lat.', '.$long . '</td><td>' . $cnt . '</td>';
echo '</tr>';
}
echo '</table>';
}
// Get Facebook results if the Facebook checkbox is ticked
if($facebook){
// Get the user profile location from the profile_fb table according to the user input
// E.g., to get the locations of users that have mentioned Katy Perry on Facebook: SELECT user_id FROM profile_fb WHERE artist LIKE "%katyperry%"
$fb_query = "SELECT user_id "
. "FROM profile_fb "
. "WHERE artist LIKE '%".$input."%'";
$result_fb = mysqli_query($connecDB, $fb_query);
// Display the result into the result table
echo '<table class="result">';
// For each row, store the location information into the $city variable
while($row_fb = mysqli_fetch_array($result_fb))
{
$user_id = $row_fb['user_id'];
$city = "None";
// If the profile checkbox is selected, use the user profile method to locate a user
if($profile){
// Query data from the profile_fb table which uses the user profile method
$query = "SELECT profile_fb.location_id, locations.city, locations.country, locations.geo_lat, locations.geo_long "
. "FROM profile_fb "
. "JOIN locations ON profile_fb.location_id = locations.location_id "
. "WHERE profile_fb.`user_id` =".$user_id;
$outputs = mysqli_query($connecDB, $query);
if(mysqli_num_rows($outputs) != 0){
while($row_inner = mysqli_fetch_array($outputs)){
$lat = $row_inner['geo_lat'];
$long = $row_inner['geo_long'];
$city = $row_inner['city'];
$country = $row_inner['country'];
$cnt = "1";
}
}
}
// If no location information can be found, use the social network method to locate this user
if($city == "None" && $networking){
// Query data from the networking_fb table which uses the social network method
$query = "SELECT networking_fb.location_id, locations.city, locations.country, locations.geo_lat, locations.geo_long "
. "FROM networking_fb "
. "JOIN locations ON networking_fb.location_id = locations.location_id "
. "WHERE networking_fb.`user_id` =".$user_id;
$outputs = mysqli_query($connecDB, $query);
if(mysqli_num_rows($outputs) != 0){
while($row_inner = mysqli_fetch_array($outputs)){
$lat = $row_inner['geo_lat'];
$long = $row_inner['geo_long'];
$city = $row_inner['city'];
$country = $row_inner['country'];
$cnt = "1";
}
}
}
// If not location information can be found using the two geolocation methods for Facebook, the user is then considered as unlocatable
if($city=="None" || empty($city)){
continue;
}
// Display the results into the table
echo '<tr>';
echo '<td>' . $country . '</td><td>' . $city . '</td><td>' . $lat.', '.$long . '</td><td>' . $cnt . '</td>';
echo '</tr>';
}
echo '</table>';
}
//Function to get city and country name using long and lat. Limit of 2500 per day.
function geolonglat($lat, $long){
$attemps = 0;
$success = False;
while (!$success && $attemps <3){
$attemps = $attemps + 1;
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$long."&sensor=true";
$data = file_get_contents($url);
$jsondata = json_decode($data,true);
if( is_array($jsondata) && $jsondata['status'] == "OK"){
// city
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("locality", $address["types"])) {
$city = $address["long_name"];
}
}
}
// country
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("country", $address["types"])) {
$country = $address["long_name"];
}
}
}
$success = True;
return array($city, $country);
}
if( is_array($jsondata) && $jsondata['status'] == "OVER_QUERY_LIMIT"){
sleep(5);
}
}
if($attemps == 3){
print "excessed reques to google map service limit";
}
}