-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
executable file
·1563 lines (1318 loc) · 60.4 KB
/
lib.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
session_start();
include('config.php');
require 'lib/PHPMailer/PHPMailerAutoload.php';
if(isset($_POST['operation'])){
$md5_psw_salt="kjsdhfjsh";
$md5_psw_salt_admin="olkkohfjshpoiu";
/*
if($_POST['operation']=="getTabs"){
$query = "SELECT * FROM tabs where visibility =1 order by position, tOrder";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'records'=>$records));
//echo json_encode($records);
//break;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
if($_POST['operation']=="get_category"){
$query = "SELECT * FROM categories order by category_type";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('records'=>$records));
//echo json_encode($records);
//break;
}
}
*/
if($_POST['operation']=="updateCall" && isset($_POST['id'])){
$id=$_POST['id'];
$query = "UPDATE requests set escapee=1 where id=$id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
else if($_POST['operation']=="updateTabEdit" && isset($_POST['tab_id'])){
$tab_id=$_POST['tab_id'];
$tab_name=$_POST['tab_name'];
$tab_visibility=$_POST['tab_visibility'];
$tab_position=$_POST['tab_position'];
$tab_tOrder=$_POST['tab_tOrder'];
$tab_type=$_POST['tab_type'];
$tab_data=$_POST['tab_data'];
$query = "UPDATE tabs SET name='$tab_name' , visibility=$tab_visibility, position=$tab_position, tOrder=$tab_tOrder, type=$tab_type, data='$tab_data' WHERE id=$tab_id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
else if($_POST['operation']=="addTabEdit"){
$tab_cid=$_POST['tab_cid'];
$tab_name=$_POST['tab_name'];
$tab_visibility=$_POST['tab_visibility'];
$tab_position=$_POST['tab_position'];
$tab_tOrder=$_POST['tab_tOrder'];
$tab_type=$_POST['tab_type'];
$tab_data=$_POST['tab_data'];
$query = "INSERT INTO tabs (client_id, name, visibility, position, tOrder, type, data) VALUES ($tab_cid, '$tab_name' , $tab_visibility, $tab_position, $tab_tOrder, $tab_type, '$tab_data')";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
else if($_POST['operation']=="deleteTabEdit"){
$tab_id=$_POST['tab_id'];
$query = "DELETE FROM tabs WHERE id=$tab_id;";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
else if($_POST['operation']=="insertCall" && isset($_POST['table']) && isset($_POST['cid'])){
$table = $_POST['table'];
if($table==-1){
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'1'));
}else{
$type="0"; //0 for simple call
$session="NULL";
$cid=$_POST['cid'];
$d = "NULL";
$dTime="NULL";
if(isset($_POST['type'])) $type=$_POST['type']; // type is passed from parameter 1=request bill, 2=request water, 3=request coffe, 4 is menù type r_session != null
if(isset($_POST['session'])) $session="'".$_POST['session']."'";
if(isset($_POST['d']) && $_POST['d']!='') $d=$_POST['d'];
if(isset($_POST['dTime']) && $_POST['dTime']!='') $dTime="'".$_POST['dTime']."'";
/*
if($type==4 && isset($_POST['session']) && isset($_POST['order_id'])){
$r_session=$_POST['session'];
$order_id=$_POST['order_id'];
$query = "INSERT INTO requests (r_type, r_table, escapee, r_session, order_id) VALUES ('$type', $table, 0, '$r_session', $order_id)";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$query = "UPDATE orderrows set client_visibility = 0 where order_id = $order_id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
//DA SOSTITUIRE
$_SESSION['order_id']=-1;
}
*/
//else{
$orderId='NULL';
if(isset($_POST['session'])){
$session2 = $_POST['session'];
$query = "SELECT id from orders where session = '$session2' and client_id=$cid ORDER BY id DESC LIMIT 1";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
if(isset($_SESSION['old_orders'])){
$old_orders = $_SESSION['old_orders'];
}
else{
$old_orders = array();
$_SESSION['old_orders']=$old_orders;
}
while ($record = mysqli_fetch_array($result)){
array_push($old_orders, $record[0]);
$orderId=$record[0];
}
$_SESSION['old_orders']=$old_orders;
}
$queryC = "SELECT user_login_required, enable_print from clients where id=$cid LIMIT 1";
$result = mysqli_query($db_con,$queryC) or die('Errant query: '.$queryC);
while ($record = mysqli_fetch_array($result)){
$enable_print=$record['enable_print'];
$user_login_required=$record['user_login_required'];
}
$userId='NULL';
if($user_login_required==1){
if(isset($_SESSION['userLoginId'])){
$userId=$_SESSION['userLoginId'];
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'E_SESSION','returnCode'=>'100'));
exit;
}
}
$query = "INSERT INTO requests (client_id, r_type, r_table, escapee, r_session, order_id, user_id, delivery, delivery_time) VALUES ($cid, '$type', $table, 0, $session, $orderId, $userId, $d, $dTime)";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$lastinserdetId = mysqli_insert_id($db_con);
//}
if ($enable_print==1) {
// type is passed from parameter 1=request bill, 2=request water, 3=request coffe, 4 is menù type r_session != null
if($type == "4" ){
//$query = "select r.r_table, COALESCE(m.printdescription,m.name) as name, ors.number, ors.note from requests r inner join orders o on r.r_session = o.session inner join orderrows ors on o.id = ors.order_id inner join menurows m on ors.row_id = m.id where r.id = $lastinserdetId";
$query = "SELECT
t.description as r_table,
COALESCE(m.printdescription,m.name) as name,
ors.number,
ors.note
from
requests r inner join tables t on r.r_table=t.id inner join orders o on r.r_session = o.session inner join orderrows ors on o.id = ors.order_id inner join menurows m on ors.row_id = m.id
where r.id = $lastinserdetId";
$tabl ="-1";
$data="";
$result =mysqli_query($db_con,$query) or die('Errant query: '.$query);
while ($record = mysqli_fetch_array($result)){
$tabl = $record[0];
//$data .= $record[1].": ".$record[2]."\n";
$data .= " ".$record[2].": \t".$record[1];//."\n";
if($record[3] != ""){
$data .= " (".$record[3].")\n";
}
else{
$data .= "\n";
}
}
}
else{
$query = "select printdescription from callstype where type=$type";
$tabl =$table;
$data="";
$result =mysqli_query($db_con,$query) or die('Errant query: '.$query);
while ($record = mysqli_fetch_array($result)){
$data .= "\t".$record[0]."\n";
}
}
$fp = stream_socket_client($connessione, $errno, $errstr, 3);
if (!$fp) {
//echo "$errstr ($errno)<br />\n";
for ($i=0; $i < $tentativi; $i++) {
usleep(20);
$fp = stream_socket_client($connessione, $errno, $errstr, 3);
if ($fp){
$table= $filler1."possibile errore segnalare a Fabio".$filler1.$boldON.$tabl.$boldOFF.$filler1.$filler1;
$towrite = $bip.$table.$data.$filler2.$cut;
fwrite($fp, $towrite);
//echo ("Ho stampato:\n".$towrite);
fclose($fp);
break;
}
}
}
else {
$table= $filler1.$boldON.$tabl.$boldOFF.$filler1.$filler1;
$towrite = $bip.$table.$data.$filler2.$cut;
fwrite($fp, $towrite);
//echo ("Ho stampato:\n".$towrite);
fclose($fp);
}
//break;
}
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'requestId'=>$lastinserdetId));
}
}
/*
else if($_POST['operation']=="getRows" && isset($_POST['tabId'])){
//$section=$_POST['section'];
$tabId=$_POST['tabId'];
//$query = "SELECT * FROM menurows where section=$section and status = 1 order by name";
$query = "SELECT c.name as cName, IF(c.saleable=0,0,mc.saleable) as saleable, m.id, m.name, m.description, m.price, m.image_path_low from categories c LEFT JOIN menurowcategories mc on mc.idc = c.id LEFT JOIN menurows m on mc.idm = m.id where COALESCE(m.status,1) = 1 and c.visibility =1 and c.tabId = $tabId order by mc.mcOrder";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'records'=>$records));
//echo json_encode($records);
//break;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
*/
else if($_POST['operation']=="getDetail" && isset($_POST['id']) && isset($_POST['cid'])){
$id=$_POST['id'];
$cid= $_POST['cid'];
//$query = "SELECT * FROM menurows where id=$id";
$query = "SELECT name, price, COALESCE(long_description,'') as long_description, COALESCE(long_description_en,'') as long_description_en, image_path_high, note FROM menurows where id=$id AND client_id=$cid";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'records'=>$records));
//echo json_encode($records);
//break;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
else if($_POST['operation']=="getOrderDetail" && isset($_POST['oid']) && isset($_POST['session'])){
$oid=$_POST['oid'];
$session=$_POST['session'];
$query = "SELECT m.name, m.price, COALESCE(m.long_description,'') as long_description, COALESCE(m.long_description_en,'') as long_description_en, m.image_path_high, m.note, orr.note as notetext, orr.number FROM menurows as m join orderrows as orr on m.id = orr.row_id join orders as o on orr.order_id = o.id where orr.id= $oid and o.session = '$session'";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'records'=>$records));
//echo json_encode($records);
//break;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
else if($_POST['operation']=="insertOrderRow" && isset($_POST['rowId']) && isset($_POST['number']) && isset($_POST['session']) && isset($_POST['table']) && isset($_POST['note']) && isset($_POST['cid'])){
$rowId=$_POST['rowId'];
$number=$_POST['number'];
$session=$_POST['session'];
$table=$_POST['table'];
$note=$_POST['note'];
$cid=$_POST['cid'];
$queryS = "SELECT id FROM orders where session='$session' and client_id=$cid";
//error_log(' /n 1 '.$queryS, 3, "log/log.txt");
$result = mysqli_query($db_con,$queryS);
if (!$result) {
echo 'Could not run query: ' . mysqli_error() .$queryS;
exit;
}
$row = mysqli_fetch_row($result);
//echo (intval($order_id));
$orderId=$row[0];
//error_log(' /n 2 '.$orderId, 3, "log/log.txt");
if(empty($orderId)) {
$query = "INSERT INTO orders (client_id, session, tableId) VALUES ($cid, '$session', '$table')";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$orderId=mysqli_insert_id($db_con);
//error_log(' /n 3 '.$orderId, 3, "log/log.txt");
}
//controllo se è stato già inserito e nel caso faccio l'update del number
$queryS2 = "SELECT id FROM orderrows where row_id='$rowId' and order_id=$orderId and note='$note'";
$result = mysqli_query($db_con,$queryS2);
if (!$result) {
echo 'Could not run query: ' . mysqli_error() .$queryS2;
exit;
}
$row2 = mysqli_fetch_row($result);
$rowselected=$row2[0];
//error_log(' /n 4 '.$rowselected, 3, "log/log.txt");
if(empty($rowselected)) {
$query = "INSERT INTO orderrows (client_id, order_id, row_id, number, note) VALUES ($cid, $orderId, $rowId, $number, '$note')";
//error_log(' /n 5 '.$rowselected, 3, "log/log.txt");
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else{
$query="UPDATE orderrows set number = number+$number where id = $rowselected";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
}
else if($_POST['operation']=="getCart" && isset($_POST['session']) && isset($_POST['cid'])){
$session=$_POST['session'];
$cid=$_POST['cid'];
$query = "SELECT o.id as oid, m.id, m.name, COALESCE(m.description,'') as description, m.price, CONCAT(left(m.price, length(m.price)-2),',',right(m.price, 2)) as pricedes ,m.image_path_low, o.id as cart_row_id, o.order_id, o.number from menurows as m JOIN orderrows as o on m.id = o.row_id where m.client_id=$cid AND o.order_id = (SELECT max(id) FROM orders where session='$session') and client_visibility = 1 order by m.name";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'records'=>$records));
//echo json_encode($records);
//break;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
else if($_POST['operation']=="pushNumber" && isset($_POST['cartRowId'])){
$id=$_POST['cartRowId'];
$queryS = "SELECT m.qty_min, m.qty_max, m.qty_step FROM menurows m join orderrows o on o.row_id=m.id where o.id=$id and m.client_id=o.client_id;";
$result = mysqli_query($db_con,$queryS) or die('Errant query: '.$queryS);
$min;
$max;
$step;
while ($record = mysqli_fetch_array($result)){
$min=$record['qty_min'];
$max=$record['qty_max'];
$step=$record['qty_step'];
}
$query="UPDATE orderrows set number = IF(number < $max , number+$step, number) where id = $id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else if($_POST['operation']=="pullNumber" && isset($_POST['cartRowId'])){
$id=$_POST['cartRowId'];
$queryS = "SELECT m.qty_min, m.qty_max, m.qty_step FROM menurows m join orderrows o on o.row_id=m.id where o.id=$id and m.client_id=o.client_id;";
$result = mysqli_query($db_con,$queryS) or die('Errant query: '.$queryS);
$min;
$max;
$step;
while ($record = mysqli_fetch_array($result)){
$min=$record['qty_min'];
$max=$record['qty_max'];
$step=$record['qty_step'];
}
$query="UPDATE orderrows set number = IF(number > $min , number-$step, number) where id = $id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else if($_POST['operation']=="deleteOrderRow" && isset($_POST['cartRowId'])){
$id=$_POST['cartRowId'];
$query="DELETE FROM orderrows where id = $id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else if($_POST['operation']=="updateOrderRow" && isset($_POST['rowOrderId']) && isset($_POST['number']) && isset($_POST['note'])){
$id=$_POST['rowOrderId'];
$number=$_POST['number'];
$note=mysqli_real_escape_string($db_con,$_POST['note']);
$query="UPDATE orderrows set number = $number, note= '$note' where id = $id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else if($_POST['operation']=="login" && isset($_POST['username']) && isset($_POST['password'])){
$usr=mysqli_real_escape_string($db_con,$_POST['username']);
$password=md5(($_POST['password']).$md5_psw_salt);
$query="SELECT id, client_id, username, activated, image_path_low from users where (username='$usr' OR phone_number = '$usr') AND password = '$password' LIMIT 1";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
$_SESSION['userLoginId'] = $post['id'];
$_SESSION['userLoginUsername'] = $post['username'];
$_SESSION['userLoginActvated'] = $post['activated'];
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'records'=>$records));
//echo json_encode($records);
//break;
}else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
else if($_POST['operation']=="activateAccount" && isset($_POST['code'])){
if(isset($_SESSION['userLoginId'])){
$id=$_SESSION['userLoginId'];
$code=mysqli_real_escape_string($db_con,$_POST['code']);
$query="UPDATE users set activated = 1 where id=$id and confirmation_code=$code";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
if(mysqli_affected_rows()>0){
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_NOUPDATE','returnCode'=>'1'));
}
exit;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'E_SESSION','returnCode'=>'100'));
exit;
}
}
else if($_POST['operation']=="logout"){
unset($_SESSION['userLoginId']);
unset($_SESSION['userLoginUsername']);
unset($_SESSION['userLoginActvated']);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
else if($_POST['operation']=="isLogin"){
if(isset($_SESSION['userLoginId'])){
$id = $_SESSION['userLoginId'];
$query="SELECT id, username, activated, image_path_low from users where id= $id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$records= array();
if(mysqli_num_rows($result)) {
while($post = mysqli_fetch_assoc($result)) {
$records[] = array('record'=>$post);
}
}
// if we have new data return it
if(!empty($records)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0', 'records'=>$records));
//echo json_encode($records);
//break;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
}
}
else if($_POST['operation']=="forgetPassword" && isset($_POST['username'])){
$username=mysqli_real_escape_string($db_con,$_POST['username']);
$queryS = "SELECT id FROM users where username = '$username'";
$result = mysqli_query($db_con,$queryS);
if (!$result) {
echo 'Could not run query: ' . mysqli_error() .$queryS;
exit;
}
$row = mysqli_fetch_row($result);
$userId=$row[0];
if(empty($userId)) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_EUSER','returnCode'=>'1'));
exit;
}
else{
//creazione password random
$passwordClear=rand(10000000, 99999999);
$password=md5(($passwordClear).$md5_psw_salt);
$query="UPDATE users set password='$password' where id =$userId";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = $smtp_address;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $smtp_usr; // SMTP username
$mail->Password = $smtp_psw; // SMTP password
$mail->From = $email_from;
$mail->FromName = $email_from_name;
$mail->addAddress($username, $username);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->Subject = 'Password dimenticata Servizi Smart';
$mail->Body = 'riepilgo dati di accesso </br> <p>Utente: <b>'.$username.'</b> </br> <p>Password: <b>'.$passwordClear.'</b>';
$mail->AltBody = 'riepilgo dati di accesso Utente: '.$username.' Password: '.$passwordClear;
if(!$mail->send()) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_EMAIL','returnCode'=>'3'));
exit;
}
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
}
else if($_POST['operation']=="changePassword" && isset($_POST['passwordNew']) && isset($_POST['passwordOld'])){
$pswold=mysqli_real_escape_string($db_con,$_POST['passwordOld']);
$pswnew=mysqli_real_escape_string($db_con,$_POST['passwordNew']);
$pswoldc=md5(($pswold).$md5_psw_salt);
$pswnewc=md5(($pswnew).$md5_psw_salt);
$id=-1;
if(isset($_SESSION['userLoginId'])){
$id = $_SESSION['userLoginId'];
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_ESESSION','returnCode'=>'100'));
exit;
}
$queryS = "SELECT password, username FROM users where id = $id";
$result = mysqli_query($db_con,$queryS);
if (!$result) {
echo 'Could not run query: ' . mysqli_error() .$queryS;
exit;
}
$row = mysqli_fetch_row($result);
$pass=$row[0];
$username = $row[1];
if(empty($pass) || $pass != $pswoldc) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_EPASS','returnCode'=>'2'));
exit;
}
else{
$query="UPDATE users set password='$pswnewc' where id =$id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = $smtp_address;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $smtp_usr; // SMTP username
$mail->Password = $smtp_psw; // SMTP password
$mail->From = $email_from;
$mail->FromName = $email_from_name;
$mail->addAddress($username, $username);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->Subject = 'Cambio password Servizi Smart';
$mail->Body = 'riepilgo dati di accesso </br> <p>Utente: <b>'.$username.'</b> </br> <p>Password: <b>'.$pswnew.'</b>';
$mail->AltBody = 'riepilgo dati di accesso Utente: '.$username.' Password: '.$pswnew;
if(!$mail->send()) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_EMAIL','returnCode'=>'3'));
exit;
}
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
/*
$query="UPDATE users set password='$pswnewc' where id =$id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = $smtp_address;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $smtp_usr; // SMTP username
$mail->Password = $smtp_psw; // SMTP password
$mail->From = $email_from;
$mail->FromName = $email_from_name;
$mail->addAddress($username, $username);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->Subject = 'AAAA';
$mail->Body = 'riepilgo dati di accesso </br>';
$mail->AltBody = 'riepilgo dati di accesso';
if(!$mail->send()) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_EMAIL','returnCode'=>'3'));
exit;
}
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
/*
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = $smtp_address;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $smtp_usr; // SMTP username
$mail->Password = $smtp_psw; // SMTP password
$mail->From = $email_from;
$mail->FromName = $email_from_name;
$mail->addAddress($username, $username);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->Subject = 'Cambio password Servizi Smart';
$mail->Body = 'Password cambiata con successo</br> <p>Nuova password: <b>'.$pswnew.'</b>';
$mail->AltBody = 'Password cambiata con successo Nuova password: '.$pswnew;
if(!$mail->send()) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_EMAIL','returnCode'=>'3'));
exit;
}
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
*/
}
//$.post('lib.php', {operation: "insertUserAddress", aName: aName, aLastName: aLastName, aAddress: aAddress, aNumber: aNumber, aApartmentNumber: aApartmentNumber, aFloor: aFloor, aZipCode: aZipCode, aRegioni: aRegioni, aProvince: aProvince, aComuni: aComuni}, onInsertUserAddress);
else if($_POST['operation']=="insertUserAddress" && isset($_POST['aName']) && isset($_POST['aLastName']) && isset($_POST['aAddress']) && isset($_POST['aNumber']) && isset($_POST['aDoorBell']) && isset($_POST['aZipCode']) && isset($_POST['aRegioni']) && isset($_POST['aProvince']) && isset($_POST['aComuni'])){
$id=-1;
if(isset($_SESSION['userLoginId'])){
$id = $_SESSION['userLoginId'];
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_ESESSION','returnCode'=>'100'));
exit;
}
$aApartmentNumber="NULL";
$aFloor="NULL";
$aName = $_POST['aName'];
$aLastName = $_POST['aLastName'];
$aAddress = $_POST['aAddress'];
$aNumber = $_POST['aNumber'];
$aDoorBell = $_POST['aDoorBell'];
$aZipCode = $_POST['aZipCode'];
$aRegioni = $_POST['aRegioni'];
$aProvince = $_POST['aProvince'];
$aComuni = $_POST['aComuni'];
if(isset($_POST['aApartmentNumber'])){
$aApartmentNumber="'".$_POST['aApartmentNumber']."'";
}
if(isset($_POST['aFloor'])){
$aFloor="'".$_POST['aFloor']."'";
}
$query ="INSERT INTO useraddresses (user_id, name, last_name, address, address_number, doorbell, apartment_number, floor, zip_code, district_id) VALUES ($id, '$aName', '$aLastName', '$aAddress', '$aNumber', '$aDoorBell', $aApartmentNumber, $aFloor, $aZipCode, $aComuni)";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else if($_POST['operation']=="changeNumber" && isset($_POST['cellpre']) && isset($_POST['cell'])){
$prefix=mysqli_real_escape_string($db_con,$_POST['cellpre']);
$number=mysqli_real_escape_string($db_con,$_POST['cell']);
$id=-1;
if(isset($_SESSION['userLoginId'])){
$id = $_SESSION['userLoginId'];
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_ESESSION','returnCode'=>'100'));
exit;
}
$queryS = "SELECT id FROM users where pre_phone_number='$prefix' AND phone_number = '$number'";
$result = mysqli_query($db_con,$queryS);
if (!$result) {
echo 'Could not run query: ' . mysqli_error() .$queryS;
exit;
}
$row = mysqli_fetch_row($result);
$userId=$row[0];
if(empty($userId)) {
$confirm=rand(100000, 999999);
$query="UPDATE users set pre_phone_number=$prefix, phone_number=$number, confirmation_code=$confirm, activated=0 where id=$id";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
$fields = array(
'method' => 'send_sms_classic',
//'method' => 'send_sms_basic',
'username' => $gateway_usr,
'password' => $gateway_psw,
'recipients[]' => $prefix.$number,
'text' => 'codice attivazione: '.$confirm,
'sender_string' => $sender_name
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($gateway_url, false, $context);
parse_str($result, $output);
if ($output['status'] == 'success'){
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_NOSMS','returnCode'=>'2'));
exit;
}
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_ENUMBER','returnCode'=>'1'));
exit;
}
}
else if($_POST['operation']=="register" && isset($_POST['username']) && isset($_POST['password']) && isset($_POST['prefix']) && isset($_POST['number']) && isset($_POST['email']) && isset($_POST['cid'])){
$username=mysqli_real_escape_string($db_con,$_POST['username']);
$password=md5(($_POST['password']).$md5_psw_salt);
$passwordClear = $_POST['password'];
$prefix=mysqli_real_escape_string($db_con,$_POST['prefix']);
$number=mysqli_real_escape_string($db_con,$_POST['number']);
$email=mysqli_real_escape_string($db_con,$_POST['email']);
$confirm=rand(100000, 999999);
$cid = $_POST['cid'];
$queryS = "SELECT id FROM users where pre_phone_number='$prefix' AND phone_number = '$number'";
$result = mysqli_query($db_con,$queryS);
if (!$result) {
echo 'Could not run query: ' . mysqli_error() .$queryS;
exit;
}
$row = mysqli_fetch_row($result);
$userId=$row[0];
if(empty($userId)) {
$query="INSERT INTO users (client_id, username, password , pre_phone_number , phone_number , email , confirmation_code , activated ) VALUES($cid, '$username','$password','$prefix','$number','$email', '$confirm' ,0)";
$result = mysqli_query($db_con,$query) or die('Errant query: '.$query);
//SEND MAIL
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = $smtp_address;
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $smtp_usr; // SMTP username
$mail->Password = $smtp_psw; // SMTP password
$mail->From = $email_from;
$mail->FromName = $email_from_name;
$mail->addAddress($username, $username);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->Subject = 'Creazione nuovo account Servizi Smart';
$mail->Body = 'riepilgo dati di accesso </br> <p>Utente: <b>'.$username.'</b> </br> <p>Password: <b>'.$passwordClear.'</b>';
$mail->AltBody = 'riepilgo dati di accesso Utente: '.$username.' Password: '.$passwordClear;
if(!$mail->send()) {
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_EMAIL','returnCode'=>'3'));
exit;
}
// Submit those variables to the server
$fields = array(
'method' => 'send_sms_classic',
//'method' => 'send_sms_basic',
'username' => $gateway_usr,
'password' => $gateway_psw,
'recipients[]' => $prefix.$number,
'text' => 'codice attivazione: '.$confirm,
'sender_string' => $sender_name
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($fields),
),
);
$context = stream_context_create($options);
$result = file_get_contents($gateway_url, false, $context);
parse_str($result, $output);
//echo $output['status'];
//echo $output['remaining_sms']; // foo bar
/*
$fields_string="";
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$gateway_url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//non fanno ciò che mi aspetto
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_HEADER, false); //clear header
//close connection
curl_close($ch);
*/
if ($output['status'] == 'success'){
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_OK','returnCode'=>'0'));
exit;
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_NOSMS','returnCode'=>'2'));
exit;
}
}
else{
header('Content-type: application/json');
echo json_encode(array('response'=> 'S_ENUMBER','returnCode'=>'1'));
exit;
}
}
//ADMIN FUNCTION
//CHECK ALWAYS IF SESSION IS OK
else if($_POST['operation']=="adminLogin" && isset($_POST['username']) && isset($_POST['password'])){
//$usr=mysqli_real_escape_string($db_con,$_POST['username']);