-
Notifications
You must be signed in to change notification settings - Fork 89
/
user.php
1547 lines (1489 loc) · 38 KB
/
user.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include 'config.php';
require_once 'db.php';
require_once 'utils.php';
require_once 'user.class.php';
global $db;
$UserManager = new User();
$UserManager->load();
function getFloatCoord($coord)
{
global $db;
$name = randomStr(12, false);
if (!QuerySql("CREATE TEMPORARY TABLE `$name` (coord FLOAT(12,8))")) return null;
QuerySql("INSERT INTO `$name` (coord) VALUES ($coord)");
$res = QuerySql("SELECT coord FROM `$name`");
$row = $res->fetch_row();
$res->close();
QuerySql("DROP TABLE `$name`");
return $row[0];
}
$json = array();
$json['result'] = false;
if (!db_connect())
{
$json['error'] = 'database';
header('Content-Type: application/json');
echo json_encode($json);
exit();
}
$action = isset($_GET['a']) ? $_GET['a'] : null;
if ($action == null)
{
Header('HTTP/1.0 303 See Other');
Header('Location: /');
exit;
}
switch($action)
{
// Вход пользователя
case 'login':
$json['result'] = false;
if ($UserManager->isLogged())
{
$json['error'] = 'loggedin';
break;
}
if (isset($_POST['login']) && isset($_POST['password']))
{
filterLogin($_POST['login']);
if (!$UserManager->Auth($_POST['password'], $_POST['login']))
{
$json['error'] = 'loginfail';
break;
}
if ($UserManager->Level == -2)
{
$json['error'] = 'lowlevel';
$info = $UserManager->getUserInfo($UserManager->uID);
if ($info['ban_reason'] != null)
{
$json['reason'] = $info['ban_reason'];
$json['inherited'] = false;
if ($json['reason'] == "inherit")
{
$info = $UserManager->getUserInfo($UserManager->puID);
$json['reason'] = $info['ban_reason'];
$json['inherited'] = true;
}
}
$UserManager->out();
break;
}
$json['result'] = true;
}
break;
// Выход из учётной записи
case 'logout':
$json['result'] = false;
if ($UserManager->isLogged())
{
if (!$UserManager->checkToken($_GET['token']))
{
$json['error'] = 'token';
break;
}
$UserManager->out();
$json['result'] = true;
}
else
{
$json['error'] = 'unauthorized';
}
break;
// Проверка логина и ника на существование
case 'testreg':
$json['result'] = false;
if ($UserManager->isLogged())
{
$json['error'] = 'loggedin';
break;
}
if (isset($_POST['invite']) && (isset($_POST['login']) || isset($_POST['nick'])))
{
if (strlen($_POST['invite']) != INVITE_LEN || !$UserManager->isValidInvite($_POST['invite']))
{
$json['error'] = 'invite';
break;
}
if (isset($_POST['login']))
{
filterLogin($_POST['login']);
if (strlen($_POST['login']) < LOGIN_MIN || strlen($_POST['login']) > LOGIN_MAX)
{
$json['error'] = 'login';
break;
}
if ($UserManager->isUserLogin($_POST['login']))
{
$json['error'] = 'login';
break;
}
}
if (isset($_POST['nick']))
{
filterNick($_POST['nick']);
if (strlen($_POST['nick']) < NICK_MIN || strlen($_POST['nick']) > NICK_MAX)
{
$json['error'] = 'nick';
break;
}
if ($UserManager->isUserNick($_POST['nick']))
{
$json['error'] = 'nick';
break;
}
}
$json['result'] = true;
}
else
$json['error'] = 'form';
break;
// Получение токена для защиты от CSRF
case 'token':
$json['result'] = false;
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
$json['token'] = $UserManager->genToken();
$json['result'] = $json['token'] != '';
break;
// Регистрация нового пользователя
case 'reg':
if ($UserManager->isLogged())
{
$json['error'] = 'loggedin';
break;
}
$newLogin = isset($_POST['login']) ? $_POST['login'] : NULL;
$newNick = isset($_POST['nick']) ? $_POST['nick'] : NULL;
$newPassword = isset($_POST['password']) ? $_POST['password'] : NULL;
$newInvite = isset($_POST['invite']) ? $_POST['invite'] : NULL;
if(is_null($newLogin)
|| is_null($newNick)
|| is_null($newPassword)
|| is_null($newInvite))
{
$json['error'] = 'form';
break;
}
if (strlen($newInvite) != INVITE_LEN || !$UserManager->isValidInvite($newInvite))
{
$json['error'] = 'invite';
break;
}
filterLogin($newLogin);
if (strlen($newLogin) < LOGIN_MIN || strlen($newLogin) > LOGIN_MAX)
{
$json['error'] = 'form';
break;
}
if ($UserManager->isUserLogin($newLogin))
{
$json['error'] = 'login';
break;
}
filterNick($newNick);
if (strlen($newNick) < NICK_MIN || strlen($newNick) > NICK_MAX)
{
$json['error'] = 'form';
break;
}
if ($UserManager->isUserNick($newNick))
{
$json['error'] = 'nick';
break;
}
if (strlen($newPassword) < PASS_MIN || strlen($newPassword) > PASS_MAX)
{
$json['error'] = 'password';
break;
}
$json['result'] = $UserManager->Registration($newLogin, $newNick, $newPassword, $newInvite);
break;
// Смена никнейма пользователя
case 'changenick':
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
if (!$UserManager->checkToken($_POST['token']))
{
$json['error'] = 'token';
break;
}
$newNick = isset($_POST['nick']) ? $_POST['nick'] : null;
if (is_null($newNick))
{
$json['error'] = 'form';
break;
}
filterNick($_POST['nick']);
if (strlen($_POST['nick']) < NICK_MIN || strlen($_POST['nick']) > NICK_MAX)
{
$json['error'] = 'form';
break;
}
if ($_POST['nick'] == $UserManager->Nick)
{
$json['result'] = true;
break;
}
if ($UserManager->isUserNick($_POST['nick']))
{
$json['error'] = 'nick';
break;
}
$json['result'] = $UserManager->changeNick($_POST['nick']);
if (!$json['result']) $json['error'] = 'database';
break;
// Смена пароля пользователя
case 'changepass':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
if (!$UserManager->checkToken($_POST['token']))
{
$json['error'] = 'token';
break;
}
$newPass = isset($_POST['password']) ? $_POST['password'] : null;
if(is_null($newPass))
{
$json['error'] = 'form';
break;
}
if (strlen($newPass) < PASS_MIN || strlen($newPass) > PASS_MAX)
{
$json['error'] = 'password';
break;
}
if ($UserManager->changePass($newPass))
{
$json['result'] = true;
}
else
{
$json['error'] = '';
}
break;
// Просмотр загруженных точек
case 'myuploads':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$uid = $UserManager->uID;
if (!is_null($UserManager->vuID))
$uid = $UserManager->vuID;
if (!$res = QuerySql(
"SELECT id, time, cmtval,
IP, Port, Authorization, name,
RadioOff, Hidden,
NoBSSID, BSSID, ESSID, Security,
WiFiKey, WPSPIN, latitude, longitude,
fuid IS NOT NULL fav
FROM uploads
INNER JOIN BASE_TABLE USING(id)
LEFT JOIN comments USING (cmtid)
LEFT JOIN GEO_TABLE USING (BSSID)
LEFT JOIN (SELECT uid AS fuid,id FROM favorites WHERE uid=$uid) ufav USING(id)
WHERE uid=$uid ORDER BY time DESC LIMIT 200"))
{
$json['error'] = 'database';
break;
}
$json['result'] = true;
$json['data'] = array();
while ($row = $res->fetch_assoc())
{
$ap = array();
$ap['id'] = (int)$row['id'];
$ap['time'] = $row['time'];
$ap['comment'] = $row['cmtval'];
$ap['ipport'] = _long2ip($row['IP']);
if ($row['Port'] != '') $ap['ipport'] .= ':'.$row['Port'];
$ap['auth'] = $row['Authorization'];
$ap['name'] = $row['name'];
$ap['nowifi'] = (bool)$row['RadioOff'];
$ap['hidden'] = (bool)$row['Hidden'];
$ap['bssid'] = bssid2str((int)$row['NoBSSID'], $row['BSSID']);
$ap['essid'] = $row['ESSID'];
$ap['sec'] = sec2str((int)$row['Security']);
$ap['key'] = $row['WiFiKey'];
$ap['wps'] = pin2str($row['WPSPIN']);
$ap['lat'] = null;
$ap['lon'] = null;
if ($row['NoBSSID'] == 0 && $row['latitude'] != 0 && $row['longitude'] != 0)
{
$ap['lat'] = (float)$row['latitude'];
$ap['lon'] = (float)$row['longitude'];
}
$ap['fav'] = (bool)$row['fav'];
$json['data'][] = $ap;
unset($ap);
}
$res->close();
break;
// Проверка доступности закачки
case 'dlcheck':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$uid = $UserManager->uID;
if (!is_null($UserManager->vuID))
$uid = $UserManager->vuID;
if (!$res = QuerySql("SELECT COUNT(id) FROM uploads WHERE uid=$uid"))
{
$json['error'] = 'database';
break;
}
$row = $res->fetch_row();
$res->close();
$json['result'] = true;
$json['count'] = (int)$row[0];
break;
// Скачивание загруженных точек
case 'download':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$uid = $UserManager->uID;
if (!is_null($UserManager->vuID))
$uid = $UserManager->vuID;
if (!$res = QuerySql("SELECT IP, Port, Authorization, name, RadioOff, Hidden, NoBSSID, BSSID, ESSID, Security, WiFiKey, WPSPIN, LANIP, LANMask, WANIP, WANMask, WANGateway, DNS1, DNS2, DNS3, latitude, longitude, data
FROM BASE_TABLE RIGHT JOIN uploads USING(id) LEFT JOIN extinfo USING (id) LEFT JOIN GEO_TABLE USING (BSSID) WHERE uid=$uid ORDER BY time"))
{
$json['error'] = 'database';
break;
}
unset($json);
Header('Content-Disposition: attachment; filename=myuploads.txt');
Header('Content-Type: text/plain; charset=utf-8');
while ($row = $res->fetch_row())
{
$row[0] = _long2ip($row[0]);
if ($row[0] == '') $row[0] = '0.0.0.0';
if ($row[1] == '') $row[1] = '0';
$row[4] = ($row[4] == 1 ? '[X]' : '');
$row[5] = ($row[5] == 1 ? '[X]' : '');
$row[7] = bssid2str((int)$row[6], $row[7]);
$row[9] = sec2str((int)$row[9]);
$row[11] = pin2str($row[11]);
$row[12] = ($row[12] != 0 ? _long2ip($row[12]) : ''); // LAN IP
$row[13] = ($row[13] != 0 ? _long2ip($row[13]) : ''); // LAN Mask
$row[14] = ($row[14] != 0 ? _long2ip($row[14]) : ''); // WAN IP
$row[15] = ($row[15] != 0 ? _long2ip($row[15]) : ''); // WAN Mask
$row[16] = ($row[16] != 0 ? _long2ip($row[16]) : ''); // WAN Gateway
$row[17] = ($row[17] != 0 ? _long2ip($row[17]) : ''); // DNS 1
$row[18] = ($row[18] != 0 ? _long2ip($row[18]) : ''); // DNS 2
$row[19] = ($row[19] != 0 ? _long2ip($row[19]) : ''); // DNS 3
$dns = trim($row[17].' '.$row[18].' '.$row[19]);
if ($row[6] > 0)
{ // not accessible
$row[20] = '';
$row[21] = '';
}
else if ($row[20] == 0 && $row[21] == 0)
{
$row[20] = 'not found';
$row[21] = 'not found';
}
// IP Addr Port Time/Stat Auth name RadioOff Hidden BSSID ESSID Security Wi-Fi Key WPS PIN LAN IP LAN Mask WAN IP WAN Mask WAN Gate DNS Latitude Longitude Comment
$line = $row[0]."\t".$row[1]."\t0\t\t".$row[2]."\t".$row[3]."\t".$row[4]."\t".$row[5]."\t".$row[7]."\t".$row[8]."\t".$row[9]."\t".$row[10]."\t".$row[11]."\t".$row[12]."\t".$row[13]."\t".$row[14]."\t".$row[15]."\t".$row[16]."\t".$dns."\t".$row[20]."\t".$row[21]."\t".$row[22]."\t\n";
echo $line;
}
$res->close();
break;
// Просмотр избранных точек
case 'myfavs':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$uid = $UserManager->uID;
if (!is_null($UserManager->vuID))
$uid = $UserManager->vuID;
if (!$res = QuerySql(
"SELECT id,time,cmtval,IP,
RadioOff,Hidden,
NoBSSID,BSSID,ESSID,
Security,WiFiKey,WPSPIN,
WANIP,latitude,longitude
FROM favorites
INNER JOIN BASE_TABLE USING(id)
LEFT JOIN comments USING(cmtid)
LEFT JOIN GEO_TABLE USING(BSSID)
WHERE uid=$uid"))
{
$json['error'] = 'database';
break;
}
$json['result'] = true;
$json['data'] = array();
while ($row = $res->fetch_assoc())
{
$ap = array();
$ap['id'] = (int)$row['id'];
$ap['time'] = $row['time'];
$ap['comment'] = $row['cmtval'];
$ip = _long2ip($row['IP']);
$wanip = _long2ip($row['WANIP']);
$ap['range'] = ($ip != '' ? $ip : ($wanip != '' ? $wanip : ''));
if (isLocalIP($ap['range'])
&& $ap['range'] != $wanip
&& isValidIP($wanip)
&& !isLocalIP($wanip))
{
$ap['range'] = $wanip;
}
if (isValidIP($ap['range']))
{
$oct = explode('.', $ap['range']);
array_pop($oct);
array_pop($oct);
$ap['range'] = implode('.', $oct).'.0.0/16';
} else
$ap['range'] = '';
$ap['nowifi'] = (bool)$row['RadioOff'];
$ap['hidden'] = (bool)$row['Hidden'];
$ap['bssid'] = bssid2str((int)$row['NoBSSID'], $row['BSSID']);
$ap['essid'] = $row['ESSID'];
$ap['sec'] = sec2str((int)$row['Security']);
$ap['key'] = $row['WiFiKey'];
$ap['wps'] = pin2str($row['WPSPIN']);
$ap['lat'] = null;
$ap['lon'] = null;
if ($row['NoBSSID'] == 0 && $row['latitude'] != 0 && $row['longitude'] != 0)
{
$ap['lat'] = (float)$row['latitude'];
$ap['lon'] = (float)$row['longitude'];
}
$json['data'][] = $ap;
unset($ap);
}
$res->close();
break;
// Добавление/удаление точки в избранное
case 'fav':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
if (!$UserManager->checkToken($_GET['token']))
{
$json['error'] = 'token';
break;
}
$uid = $UserManager->uID;
$id = isset($_GET['id']) ? (int)$_GET['id'] : null;
if (is_null($id))
{
$json['error'] = 'form';
break;
}
if (!$res = QuerySql("SELECT id FROM favorites WHERE uid=$uid AND id=$id"))
{
$json['error'] = 'database';
break;
}
$isfav = $res->num_rows > 0;
$res->close();
if ($isfav)
{
QuerySql("DELETE FROM favorites WHERE uid=$uid AND id=$id");
$json['fav'] = false;
$json['result'] = true;
break;
}
$res = QuerySql("SELECT COUNT(id) FROM favorites WHERE uid=$uid");
$row = $res->fetch_row();
$res->close();
if ($row[0] >= FAV_MAX)
{
$json['error'] = 'limit';
break;
}
if (!QuerySql("INSERT INTO favorites (uid,id) VALUES ($uid,$id)"))
{
$json['error'] = 'unknown';
break;
}
$json['fav'] = true;
$json['result'] = true;
break;
// Просмотр избранных локаций
case 'mylocs':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$uid = $UserManager->uID;
if (!is_null($UserManager->vuID))
$uid = $UserManager->vuID;
if (!$res = QuerySql("SELECT latitude,longitude,comment FROM locations WHERE uid=$uid"))
{
$json['error'] = 'database';
break;
}
$json['result'] = true;
$json['data'] = array();
while ($row = $res->fetch_row())
{
$loc = array();
$loc['lat'] = (float)$row[0];
$loc['lon'] = (float)$row[1];
$loc['cmt'] = $row[2];
$json['data'][] = $loc;
unset($loc);
}
$res->close();
break;
// Избранные локации на карте
case 'mylocmap':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$bbox = explode(',', $_GET['bbox']);
$lat1 = (float)$bbox[0];
$lon1 = (float)$bbox[1];
$lat2 = (float)$bbox[2];
$lon2 = (float)$bbox[3];
$callback = $_GET['callback'];
$mob = (isset($_GET['mobile']) ? (bool)$_GET['mobile'] : false);
$uid = $UserManager->uID;
if (!is_null($UserManager->vuID))
$uid = $UserManager->vuID;
if (!$res = QuerySql("SELECT latitude,longitude,comment FROM locations WHERE uid=$uid AND latitude BETWEEN $lat1 AND $lat2 AND longitude BETWEEN $lon1 AND $lon2"))
{
$json['error'] = 'database';
break;
}
unset($json); // здесь используется JSON-P
Header('Content-Type: application/javascript');
$json['error'] = null;
$json['data']['type'] = 'FeatureCollection';
$json['data']['features'] = array();
$loc['type'] = 'Feature';
$loc['options']['iconColor'] = '#00D000';
$loc['geometry']['type'] = 'Point';
$propContent = ($mob ? 'balloonContent' : 'hintContent');
while ($row = $res->fetch_row())
{
$loc['geometry']['coordinates'][0] = (float)$row[0];
$loc['geometry']['coordinates'][1] = (float)$row[1];
$loc['id'] = 'loc'.substr(md5($row[0].$row[1]), 0, 4);
$loc['properties'][$propContent] = '<b>Локация:</b><br>'.htmlspecialchars($row[2]);
$json['data']['features'][] = $loc;
}
$res->close();
$db->close();
echo 'typeof '.$callback.' === \'function\' && '.$callback.'('.json_encode($json).');';
exit;
break;
// Добавление/изменение локации
case 'addloc':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
if (!$UserManager->checkToken($_GET['token']))
{
$json['error'] = 'token';
break;
}
$uid = $UserManager->uID;
$lat = isset($_GET['lat']) ? (float)$_GET['lat'] : null;
$lon = isset($_GET['lon']) ? (float)$_GET['lon'] : null;
$cmt = isset($_GET['cmt']) ? trim(preg_replace('/\s+/', ' ', $_GET['cmt'])) : null;
if ($cmt == '') $cmt = null;
if (is_null($lat) || is_null($lon) || is_null($cmt))
{
$json['error'] = 'form';
break;
}
$lat = getFloatCoord($lat);
$lon = getFloatCoord($lon);
if (!$res = QuerySql("SELECT uid FROM locations WHERE uid=$uid AND latitude=$lat AND longitude=$lon"))
{
$json['error'] = 'database';
break;
}
$cmt = $db->real_escape_string($cmt);
$exist = $res->num_rows > 0;
$res->close();
$json['lat'] = (float)$lat;
$json['lon'] = (float)$lon;
$json['cmt'] = $cmt;
if ($exist)
{
QuerySql("UPDATE locations SET comment='$cmt' WHERE uid=$uid AND latitude=$lat AND longitude=$lon");
$json['new'] = false;
$json['result'] = true;
break;
}
$res = QuerySql("SELECT COUNT(uid) FROM locations WHERE uid=$uid");
$row = $res->fetch_row();
$res->close();
if ($row[0] >= LOC_MAX)
{
$json['error'] = 'limit';
break;
}
if (!QuerySql("INSERT INTO locations (uid,latitude,longitude,comment) VALUES ($uid,$lat,$lon,'$cmt')"))
{
$json['error'] = 'unknown';
break;
}
$json['new'] = true;
$json['result'] = true;
break;
// Удаление локации
case 'delloc':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
if (!$UserManager->checkToken($_GET['token']))
{
$json['error'] = 'token';
break;
}
$uid = $UserManager->uID;
$lat = isset($_GET['lat']) ? (float)$_GET['lat'] : null;
$lon = isset($_GET['lon']) ? (float)$_GET['lon'] : null;
if (is_null($lat) || is_null($lon))
{
$json['error'] = 'form';
break;
}
$lat = getFloatCoord($lat);
$lon = getFloatCoord($lon);
if (!QuerySql("DELETE FROM locations WHERE uid=$uid AND latitude=$lat AND longitude=$lon"))
{
$json['error'] = 'database';
break;
}
$json['result'] = true;
break;
// Управление приглашениями
case 'myinvites':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
$data = $UserManager->listInvites();
if (!is_array($data))
{
$json['error'] = 'unknown';
break;
}
$json['result'] = true;
$json['data'] = $data;
break;
// Создание приглашения
case 'createinv':
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
if (!$UserManager->checkToken($_GET['token']))
{
$json['error'] = 'token';
break;
}
$level = isset($_GET['level']) ? (int)$_GET['level'] : 1;
$json['result'] = $UserManager->createInvite($level);
if (!$json['result'])
$json['error'] = $UserManager->LastError;
break;
// Изменение приглашения
case 'updateinv':
if (!$UserManager->checkToken($_POST['token']))
{
$json['error'] = 'token';
break;
}
$invite = isset($_POST['invite']) ? $_POST['invite'] : null;
$level = isset($_POST['level']) ? (int)$_POST['level'] : null;
if (is_null($invite) || is_null($level))
{
$json['error'] = 'form';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$json['result'] = $UserManager->updateInvite($invite, $level);
if (!$json['result'])
$json['error'] = $UserManager->LastError;
break;
// Удаление приглашения
case 'deleteinv':
if (!$UserManager->checkToken($_POST['token']))
{
$json['error'] = 'token';
break;
}
$invite = isset($_POST['invite']) ? $_POST['invite'] : null;
if (is_null($invite))
{
$json['error'] = 'form';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
$json['result'] = $UserManager->deleteInvite($invite);
if (!$json['result'])
$json['error'] = $UserManager->LastError;
break;
// Создание API ключа
case 'createapikey':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level < 1)
{
$json['error'] = 'lowlevel';
break;
}
if (!$UserManager->checkToken($_POST['token']))
{
$json['error'] = 'token';
break;
}
$type = isset($_POST['type']) ? (int)$_POST['type'] : null;
$ApiKey = $UserManager->createApiKey($type);
if($ApiKey === false)
{
$json['result'] = false;
if (!$json['result'])
$json['error'] = $UserManager->LastError;
break;
}
$json['result'] = true;
$json['data'] = array();
switch($type)
{
case 1: $json['data']['r'] = $ApiKey;
break;
case 2: $json['data']['w'] = $ApiKey;
break;
}
break;
// Поиск пользователей
case 'finduser':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level != 3)
{
$json['error'] = 'lowlevel';
break;
}
if (!isset($_GET['query']))
{
$json['error'] = 'form';
break;
}
$query = $_GET['query'];
if (empty($query) || strlen($query) > max(LOGIN_MAX, NICK_MAX))
{
$json['error'] = 'form';
break;
}
$query = $db->real_escape_string($query);
if (!$res = QuerySql("SELECT uid, login, nick FROM users WHERE login LIKE '%{$query}%' OR nick LIKE '%{$query}%'"))
{
$json['error'] = 'database';
break;
}
$json['result'] = true;
$json['users'] = array();
while ($row = $res->fetch_row())
{
$usr = array();
$usr['uid'] = (int)$row[0];
$usr['login'] = $row[1];
$usr['html'] = highlightWords(htmlspecialchars($row[1] . ' | ' . $row[2]), array($query));
$json['users'][] = $usr;
unset($usr);
}
$res->close();
break;
// Получение информации о пользователе
case 'getuser':
if (!$UserManager->isLogged())
{
$json['error'] = 'unauthorized';
break;
}
if ($UserManager->Level != 3)
{
$json['error'] = 'lowlevel';
break;
}
if (isset($_GET['invite']))
{
if (strlen($_GET['invite']) != INVITE_LEN)
{
$json['error'] = 'form';
break;
}
$info = $UserManager->getInviteInfo($_GET['invite']);
if (!$info)
{
$json['error'] = 'invite';
break;
}
$uid = $info['uid'];
}
elseif (isset($_GET['uid']))
{
$uid = $_GET['uid'];
}
else
{
$json['error'] = 'form';
break;
}
$info = $UserManager->getUserInfo($uid);
$json['result'] = !is_null($info['login']);
if ($json['result'])
{
$json['uid'] = (int)$info['uid'];
$json['login'] = $info['login'];
$json['nick'] = $info['nick'];
$json['regdate'] = $info['regdate'];
$json['level'] = (int)$info['level'];
if (isset($info['ban_reason']))
$json['ban_reason'] = $info['ban_reason'];
$json['lastupdate'] = $info['lastupdate'];
$inv = $UserManager->listInvites($json['uid']);
if (is_array($inv))
{
$json['inv_invited'] = 0;
$json['inv_created'] = count($inv);
for ($i = 0; $i < count($inv); $i++)
{
if (!empty($inv[$i]['nick']))
$json['inv_invited']++;
}
}
$json['inv_left'] = (int)$info['invites'];
$json['puid'] = (int)$info['puid'];
$json['refuser'] = $UserManager->getUserNameById($info['puid']);
}
else
{
$json['error'] = 'notfound';
}
break;
// Изменение информации о пользователе
case 'setuser':
if (!$UserManager->isLogged())
{