forked from binarymaster/3WiFi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3wifid.php
executable file
·308 lines (289 loc) · 8.11 KB
/
3wifid.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
<?php
include 'config.php';
require_once 'utils.php';
require_once 'db.php';
Header('Content-Type: text/plain');
echo "3WiFi Daemon Script\n\n";
if ($argv[0] != basename(__FILE__))
{
die('This is CLI script. Use it with php-cli.');
}
if (count($argv) < 2)
{
echo "USAGE:\n";
echo "$argv[0] <action>\n";
exit();
}
set_time_limit(0);
while (!db_connect())
{
logt('Error: MySQL connection failed. Retrying in 5 seconds...');
sleep(5);
}
logt('MySQL database connected.');
logt("Running `$argv[1]' task...");
switch ($argv[1])
{
// Обработчик загрузок
case 'uploads':
while (true)
{
logt('Cleaning up old tasks... (tstate = 0)');
$db->query('UPDATE tasks SET tstate=1,checkexist=1,nowait=1 WHERE tstate=0 AND modified < NOW() - INTERVAL 12 HOUR');
logt('Fetching a task... (tstate = 1)');
$res = $db->query('SELECT `tid` FROM tasks WHERE `tstate`=1 ORDER BY `created` LIMIT 1');
$tid = (($row = $res->fetch_row()) ? $row[0] : null);
$res->close();
$slp = 5;
if ($tid != null)
{
$task = getTask($tid);
logt("Processing task id = $tid");
$checkexist = $task['checkexist'];
$nowait = $task['nowait'];
$warn = array();
$uid = $task['uid'];
$ext = $task['ext'];
$filename = 'uploads/'.$tid.$ext;
$hangcheck = 5;
$cntp = 0;
$cnta = 0;
$aps = array();
if (($handle = fopen($filename, 'r')) !== false)
{
$cmtid = getCommentId($task['comment'], true);
$time = microtime(true);
switch ($ext)
{
case '.csv':
while (($data = fgetcsv($handle, 1000, ';')) !== false)
{
$cntp++;
if ($cntp == 1) continue; // Пропуск заголовка CSV
$res = db_add_ap($data, $cmtid, $uid);
($res == 0 ? $cnta++ : $warn[$cntp - 1] = $res);
if (microtime(true) - $time > $hangcheck)
{
logt("Status: $cntp processed, $cnta added (Working)");
$db->query("UPDATE tasks SET `lines`=$cntp,`accepted`=$cnta WHERE `tid`='$tid'");
$time = microtime(true);
}
}
$cntp--;
break;
case '.txt':
while (($str = fgets($handle)) !== false)
{
$data = explode("\t", $str);
$cntp++;
$res = db_add_ap($data, $cmtid, $uid);
($res == 0 ? $cnta++ : $warn[$cntp] = $res);
if (microtime(true) - $time > $hangcheck)
{
logt("Status: $cntp processed, $cnta added (Working)");
$db->query("UPDATE tasks SET `lines`=$cntp,`accepted`=$cnta WHERE `tid`='$tid'");
$time = microtime(true);
}
}
break;
}
fclose($handle);
}
logt("Status: $cntp processed, $cnta added (Done!)");
$warns = array();
foreach ($warn as $line => $wid)
$warns[] = implode('|', array($line, $wid));
$warns = implode(',', $warns);
logt('Removing temporary file...');
unlink($filename);
if (!$nowait)
{
// Сохраняем очередь ожидания для следующего этапа
logt('Writing .BSS list for 2nd stage...');
$filename = 'uploads/'.$tid.'.bss';
if (($handle = fopen($filename, 'a')) !== false)
{
for ($i = 0; $i < count($aps); $i++)
fwrite($handle, $aps[$i]."\n");
fclose($handle);
}
logt("Set tstate = 2 (geolocation)");
$db->query("UPDATE tasks SET `lines`=$cntp,`accepted`=$cnta,`warns`='$warns',`tstate`=2 WHERE `tid`='$tid'");
logt("Task processing complete.");
}
else
{
$db->query("DELETE FROM tasks WHERE `tid`='$tid'");
logt("Task processing in `nowait' mode complete.");
}
unset($aps);
$slp = 0;
}
else
logt('No task was fetched.');
if ($slp > 0) sleep($slp);
}
break;
// Финализация заданий
case 'finalize':
function checkAPs($aps)
{
$result[-1] = 0;
$result[0] = 0;
$result[1] = 0;
foreach ($aps as $bssid => $st)
$result[$st]++;
return $result;
}
while (true)
{
logt('Clean complete tasks... (tstate = 3)');
$db->query("DELETE FROM tasks WHERE `tstate`=3 AND `modified` < DATE_SUB(NOW(), INTERVAL 1 MINUTE)");
logt('Fetching a task... (tstate = 2)');
$res = $db->query('SELECT `tid` FROM tasks WHERE `tstate`=2 ORDER BY `created` LIMIT 1');
$tid = (($row = $res->fetch_row()) ? $row[0] : null);
$res->close();
$slp = 5;
if ($tid != null)
{
$task = getTask($tid);
logt("Finalizing task id = $tid");
$aps = array();
$filename = 'uploads/'.$tid.'.bss';
// Получаем очередь BSSID в ожидании
logt('Reading .BSS list...');
if (($handle = fopen($filename, 'r')) !== false)
{
while (($str = fgets($handle)) !== false)
{
$str = trim(str_replace("\n", '', $str));
if (!strlen($str)) continue;
$aps[$str] = 0;
}
fclose($handle);
}
$hangcheck = 2;
$time = microtime(true);
$start = $time;
$found = 0;
while (microtime(true) - $start < 600) // макс. 10 минут ожидания
{
$apschk = checkAPs($aps);
$found = $apschk[1];
if ($apschk[0] == 0) break; // задание обработано, выходим
foreach ($aps as $bssid => $st)
{
if ($st == 0)
{
$res = QuerySql("SELECT `latitude`,`longitude` FROM GEO_TABLE WHERE `BSSID`=$bssid AND `latitude` IS NOT NULL LIMIT 1");
if ($row = $res->fetch_row())
{
$row[0] = (float)$row[0];
$row[1] = (float)$row[1];
$aps[$bssid] = ($row[0] == 0 && $row[1] == 0 ? -1 : 1);
}
$res->close();
}
}
if (microtime(true) - $time > $hangcheck)
{
logt('Status: '.$apschk[1].' found, '.$apschk[-1].' no, '.$apschk[0].' left (Working)');
$db->query("UPDATE tasks SET `onmap`=$found WHERE `tid`='$tid'");
$time = microtime(true);
}
sleep(2);
}
logt('Removing temporary file...');
unlink($filename);
logt('Status: '.$apschk[1].' found, '.$apschk[-1].' not found (Done!)');
logt("Set tstate = 3 (complete)");
$db->query("UPDATE tasks SET `onmap`=$found,`tstate`=3 WHERE `tid`='$tid'");
logt("Task processing complete.");
$slp = 0;
}
else
logt('No task was fetched.');
if ($slp > 0) sleep($slp);
}
break;
// Обновление ранее не найденных точек
case 'recheck':
$geoquery = 'SELECT `BSSID` FROM GEO_TABLE WHERE `latitude` = 0 AND `longitude` = 0';
// Получение координат для новых добавленных BSSID
case 'geolocate':
if (!isset($geoquery))
$geoquery = 'SELECT `BSSID` FROM GEO_TABLE WHERE `latitude` IS NULL';
require_once 'geoext.php';
require_once 'quadkey.php';
while (true)
{
logt('Fetching incomplete BSSIDs...');
$res = QuerySql($geoquery);
$total = $res->num_rows;
if ($total == 0)
{
logt('No new BSSIDs was fetched.');
}
else
{
$done = 0;
$found = 0;
$hangcheck = 5;
$time = microtime(true);
while ($row = $res->fetch_row())
{
$done++;
$bssid = $row[0];
$latitude = 0;
$longitude = 0;
$quadkey = 'NULL';
$coords = GeoLocateAP(dec2mac($bssid));
if ($coords != '')
{
$found++;
$coords = explode(';', $coords);
$latitude = (float)$coords[0];
$longitude = (float)$coords[1];
$quadkey = base_convert(latlon_to_quadkey($latitude, $longitude, MAX_ZOOM_LEVEL), 2, 10);
}
QuerySql("UPDATE GEO_TABLE SET `latitude`=$latitude,`longitude`=$longitude, `quadkey`=$quadkey WHERE `BSSID`=$bssid");
if ((microtime(true) - $time > $hangcheck) && ($done < $total))
{
logt("Status: $done of $total, $found found on map (Working)");
$time = microtime(true);
}
}
logt("Status: $done of $total, $found found on map (Done!)");
}
$res->close();
sleep(10);
}
break;
// Обслуживание таблиц в памяти
case 'memory':
while (true)
{
if(!TRY_USE_MEMORY_TABLES)
{
logt('Memory tables are not used');
continue;
}
$DataBaseStatus = GetStatsValue(STATS_DATABASE_STATUS);
if($DataBaseStatus == -1) // Service table not initialized
{
SetStatsValue(STATS_DATABASE_STATUS, DATABASE_PREPARE, true);
}
MemoryDataBaseInit();
if($DataBaseStatus != DATABASE_ACTIVE)
{
SetStatsValue(STATS_DATABASE_STATUS, DATABASE_ACTIVE, true);
}
sleep(10);
}
break;
default:
logt("Error: Unsupported action `$argv[1]'");
break;
}
$db->close();
?>