-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
1804 lines (1530 loc) · 59.7 KB
/
functions.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_once("db.php");
include_once("states.php");
include_once("dxccs.php");
include_once("wae.php");
$call_exceptions = unserialize(file_get_contents("db/calls.phpserial"));
# this is the year for which we generate the score tables. we keep this
# variable in the old year a few days into january of the next year so everyone
# can submit their final logs etc.
$site_year = 2025;
$arr_states = array("AK"=>1, "HI"=>1, "CT"=>1, "ME"=>1, "MA"=>1, "NH"=>1, "RI"=>1, "VT"=>1, "NJ"=>1, "NY"=>1, "DE"=>1, "MD"=>1, "PA"=>1, "AL"=>1, "FL"=>1, "GA"=>1, "KY"=>1, "NC"=>1, "SC"=>1, "TN"=>1, "VA"=>1, "AR"=>1, "LA"=>1, "MS"=>1, "NM"=>1, "OK"=>1, "TX"=>1, "CA"=>1, "AZ"=>1, "ID"=>1, "MT"=>1, "NV"=>1, "OR"=>1, "UT"=>1, "WA"=>1, "WY"=>1, "MI"=>1, "OH"=>1, "WV"=>1, "IL"=>1, "IN"=>1, "WI"=>1, "CO"=>1, "IA"=>1, "KS"=>1, "MN"=>1, "MO"=>1, "NE"=>1, "ND"=>1, "SD"=>1);
function stats($c) {
if ($_SESSION['manual']) {
stats_manual($c);
}
else {
stats_default($c);
}
}
function stats_manual($c) {
global $db;
$q = mysqli_query($db, "select aca, acma, cma, was, dxcc, wae, waz from cwops_scores where uid=".$_SESSION['id']);
$r = mysqli_fetch_row($q);
?>
<h2>Statistics for <?=$_SESSION['callsign'];?></h2>
<table>
<tr><th>Award</th><th>Score</th><th>PDF</th></tr>
<tr><td>ACA</td> <td><input size="3" value="<?=$r[0];?>" id="acamanual" onchange="update_manual(this.id);"></td> <td><a href="/api.php?action=award_pdf&type=aca">Download PDF award</a></td></tr>
<tr><td>ACMA</td> <td><input size="3" value="<?=$r[1];?>" id="acmamanual" onchange="update_manual(this.id);"></td> <td><a href="/api.php?action=award_pdf&type=acma">Download PDF award</a></td></tr>
<tr><td>CMA</td> <td><input size="3" value="<?=$r[2];?>"id="cmamanual" onchange="update_manual(this.id);"></td> <td><a href="/api.php?action=award_pdf&type=cma">Download PDF award</a></td></tr>
<tr><td>WAS</td> <td><input size="3" value="<?=$r[3];?>" id="wasmanual" onchange="update_manual(this.id);"></td> <td><a href="/api.php?action=award_pdf&type=was">Download PDF award</a></td></tr>
<tr><td>DXCC</td><td><input size="3" value="<?=$r[4];?>" id="dxccmanual" onchange="update_manual(this.id);"></td> <td><a href="/api.php?action=award_pdf&type=dxcc">Download PDF award</a></td></tr>
<tr><td>WAE</td> <td><input size="3" value="<?=$r[5];?>" id="waemanual" onchange="update_manual(this.id);"></td> <td><a href="/api.php?action=award_pdf&type=wae">Download PDF award</a></td></tr>
<tr><td>WAZ</td> <td><input size="3" value="<?=$r[6];?>" id="wazmanual" onchange="update_manual(this.id);"></td> <td><a href="/api.php?action=award_pdf&type=waz">Download PDF award</a></td></tr>
</table>
<?
}
function stats_default($c) {
global $db;
global $wae_adif;
global $site_year;
# ACA
$q = mysqli_query($db, "SELECT count(distinct(`nr`)) from cwops_log where `mycall`='$c' and year=$site_year and nr > 0");
$r = mysqli_fetch_row($q);
$aca = $r[0];
$q = mysqli_query($db, "SELECT count(distinct `nr`, `band`) from cwops_log where `mycall`='$c' and year=$site_year and nr > 0");
$r = mysqli_fetch_row($q);
$acma = $r[0];
if ($site_year == 2023)
$acma = 0;
$q = mysqli_query($db, "SELECT count(distinct `nr`, `band`) from cwops_log where `mycall`='$c' and nr > 0");
$r = mysqli_fetch_row($q);
$cma = $r[0];
$q = mysqli_query($db, "SELECT count(distinct(`was`)) from cwops_log where length(`was`) = 2 and `mycall`='$c' and nr > 0");
$r = mysqli_fetch_row($q);
$was = $r[0];
$q = mysqli_query($db, "SELECT count(distinct(`dxcc`)) from cwops_log where `dxcc` > 0 and `mycall`='$c' and nr > 0");
$r = mysqli_fetch_row($q);
$dxcc = $r[0];
$q = mysqli_query($db, "SELECT count(distinct(`dxcc`)) from cwops_log where `dxcc` > 0 and `mycall`='$c' and dxcc in (".implode(',', $wae_adif).") and nr > 0");
$r = mysqli_fetch_row($q);
$wae = $r[0];
# add special WAE areas (include Turkey / 390 because TA1 = ET is a WAE country)
$q = mysqli_query($db, "SELECT count(distinct(`wae`)) from cwops_log where LENGTH(wae) = 2 and `mycall`='$c' and dxcc in (390, ".implode(',', $wae_adif).") and nr > 0");
$r = mysqli_fetch_row($q);
$wae += $r[0];
# remove duplicate Kosovo if needed (it may be logged as WAE but with DXCC
# = Serbia (before 2018-01-21), and as a proper DXCC (after 2018-01-21) but
# should not count twice.
$q = mysqli_query($db, "SELECT count(distinct(dxcc)) from cwops_log where ((wae='KO' and dxcc=296) or dxcc=522) and `mycall`='$c' and nr > 0;");
$r = mysqli_fetch_row($q);
if ($r[0] == 2) {
$wae--;
}
$q = mysqli_query($db, "SELECT count(distinct(`waz`)) from cwops_log where waz > 0 and waz < 41 and `mycall`='$c' and nr > 0");
$r = mysqli_fetch_row($q);
$waz = $r[0];
# QTX current year
$q = mysqli_query($db, "SELECT count(*) from cwops_log where qsolength >= 20 and `mycall`='$c' and year=$site_year");
$r = mysqli_fetch_row($q);
$qtx = $r[0];
$q = mysqli_query($db, "SELECT count(*) from cwops_log where qsolength >= 10 and qsolength < 20 and `mycall`='$c' and year=$site_year");
$r = mysqli_fetch_row($q);
$mqtx = $r[0];
# Lifetime QTX
$q = mysqli_query($db, "SELECT count(*) from cwops_log where qsolength >= 20 and `mycall`='$c'");
$r = mysqli_fetch_row($q);
$ltqtx = $r[0];
$q = mysqli_query($db, "SELECT count(*) from cwops_log where qsolength >= 10 and qsolength < 20 and `mycall`='$c' and date >= '2018-07-01'");
$r = mysqli_fetch_row($q);
$ltmqtx = $r[0];
# "Current month" QTX (where current month shows last month's value up to
# the 5th of the next month)
if (date("d") >= 6) { $cm = date("Y-m"); }
else { $cm = date("Y-m", time() - 24*7*60*60); }
$q = mysqli_query($db, "SELECT count(*) from cwops_log where qsolength >= 20 and `mycall`='$c' and date like '$cm%'");
$r = mysqli_fetch_row($q);
$cmqtx = $r[0];
$q = mysqli_query($db, "SELECT count(*) from cwops_log where qsolength >= 10 and qsolength < 20 and `mycall`='$c' and date like '$cm%'");
$r = mysqli_fetch_row($q);
$cmmqtx = $r[0];
$cma_d = $cma;
$acma_d = $acma;
$aca_d = $aca;
$was_d = $was;
$dxcc_d = $dxcc;
$wae_d = $wae;
$waz_d = $waz;
$qtx_d = $qtx;
$mqtx_d = $mqtx;
# retrieve last saved result (if any) and generate diff display)
$q = mysqli_query($db, "select * from cwops_scores where uid=".$_SESSION['id']);
if ($q && $r = mysqli_fetch_array($q, MYSQLI_ASSOC)) {
if ($r['cma']) {
if ($cma - $r['cma'])
$cma_d = $cma." <span style='color:green'>+".($cma-$r['cma'])."</span>";
if ($acma - $r['acma'])
$acma_d = $acma." <span style='color:green'>+".($acma-$r['acma'])."</span>";
if ($aca - $r['aca'])
$aca_d = $aca." <span style='color:green'>+".($aca-$r['aca'])."</span>";
if ($was - $r['was'])
$was_d = $was." <span style='color:green'>+".($was-$r['was'])."</span>";
if ($dxcc - $r['dxcc'])
$dxcc_d = $dxcc." <span style='color:green'>+".($dxcc-$r['dxcc'])."</span>";
if ($wae - $r['wae'])
$wae_d = $wae." <span style='color:green'>+".($wae-$r['wae'])."</span>";
if ($waz - $r['waz'])
$waz_d = $waz." <span style='color:green'>+".($waz-$r['waz'])."</span>";
if ($qtx - $r['qtx'])
$qtx_d = $qtx." <span style='color:green'>+".($qtx-$r['qtx'])."</span>";
if ($mqtx - $r['mqtx'])
$mqtx_d = $mqtx." <span style='color:green'>+".($mqtx-$r['mqtx'])."</span>";
}
}
$q = mysqli_query($db, "delete from cwops_scores where `uid` = ".$_SESSION['id']);
$q = mysqli_query($db, "insert into cwops_scores (`uid`, `aca`, `acma`, `cma`, `was`, `dxcc`, `wae`, `waz`, `qtx`, `mqtx`, `ltqtx`, `ltmqtx`, `cmqtx`, `cmmqtx`,`updated`) VALUES (".$_SESSION['id'].", $aca, $acma, $cma, $was, $dxcc, $wae, $waz, $qtx, $mqtx, $ltqtx, $ltmqtx, $cmqtx, $cmmqtx, NOW());");
if (!$q) {
error_log("score update failed".mysqli_error($db));
}
?>
<h2>Statistics for <?=$_SESSION['callsign'];?></h2>
<!-- p>Note: The year for which the scores are calculated will remain 2024 until January 3rd, 2025, to give you sufficient time to upload your remaining 2024 logs. After that, it will switch to 2025 and the 2024 score table will be archived.</p -->
<table>
<tr><th>Award</th><th>Score</th><th>Details</th><th>PDF</th></tr>
<tr><td>ACA</td> <td><?=$aca_d?></td> <td><?=award_details('aca', 'y');?></td><td><a id="pdfaca" href="/api.php?action=award_pdf&type=aca&year=<?=$site_year;?>">Download PDF award</a></td></tr>
<tr><td>CMA</td> <td><?=$cma_d?></td> <td><?=award_details('cma', 'x');?></td><td><a href="/api.php?action=award_pdf&type=cma">Download PDF award</a></td></tr>
<tr><td>ACMA</td> <td><?=$acma_d?></td> <td><?=award_details('acma', 'y');?></td><td><a id="pdfacma" href="/api.php?action=award_pdf&type=acma&year=<?=$site_year;?>">Download PDF award</a></td></tr>
<tr><td>WAS</td> <td><?=$was_d?></td> <td><?=award_details('was', 'b');?></td><td><a href="/api.php?action=award_pdf&type=was">Download PDF award</a></td></tr>
<tr><td>DXCC</td><td><?=$dxcc_d?></td><td><?=award_details('dxcc', 'b');?></td><td><a href="/api.php?action=award_pdf&type=dxcc">Download PDF award</a></td></tr>
<tr><td>WAE</td><td><?=$wae_d?></td><td><?=award_details('wae', 'b');?></td><td><a href="/api.php?action=award_pdf&type=wae">Download PDF award</a></td></tr>
<tr><td>WAZ</td> <td><?=$waz_d?></td> <td><?=award_details('waz', 'b');?></td><td><a href="/api.php?action=award_pdf&type=waz">Download PDF award</a></td></tr>
<tr><td>QTX</td> <td><?=$qtx_d?> / <?=$mqtx_d?></td> <td><?=award_details('qtx', 'x');?></td><td></td></tr>
</table>
<br>
<div id="details"></div>
<script>
function load_stats(t, d) {
if (document.getElementById('band'+t)) {
var band = document.getElementById('band'+t).value;
}
else {
var band = "";
}
if (document.getElementById('year'+t)) {
var year = document.getElementById('year'+t).value;
}
else {
var year = "";
}
console.log('Details for ' + t + ' on ' + band + " and year = " + year);
var request = new XMLHttpRequest();
request.open("GET", '/api?action=' + t + '&band=' + band + '&year=' + year, true);
request.onreadystatechange = function() {
var done = 4, ok = 200;
if (request.readyState == done && request.status == ok) {
if (request.responseText) {
var s = document.getElementById(d);
s.innerHTML = request.responseText;
}
};
}
request.send();
}
</script>
<?
} # stats
function award_details($t, $b) {
$ret = "<button id='$t' onClick='javascript:load_stats(this.id, \"details\");'>Show details</button>";
if ($b == 'b') {
$ret .= "<select name=\"band\" id=\"band$t\" size=1>
<option>all</option>
<option>160</option>
<option>80</option>
<option>60</option>
<option>40</option>
<option>30</option>
<option>20</option>
<option>17</option>
<option>15</option>
<option>12</option>
<option>10</option>
<option>6</option>
<option>2</option></select>";
}
else if ($b == 'y') {
$ret .= "<select onChange=\"set_award_year('$t', this.value);\" name=\"year\" id=\"year$t\" size=1>\n";
for ($i = date("Y"); $i >= 2010; $i--) {
if ($i == date("Y")) {
$selected = "selected";
}
else {
$selected = "";
}
$ret .= "<option $selected>$i</option>\n";
}
$ret .= "</select>";
}
return $ret;
}
function aca($c, $y) {
global $db;
$ret = "";
$q = mysqli_query($db, "SELECT `nr`, hiscall, date, band from cwops_log where `mycall`='$c' and year=$y and nr > 0 group by `nr`");
if(!$q) {
echo mysqli_error($db);
}
$cnt = 1;
$ret .= "<table><tr><th>Count</th><th>CWops</th><th>Call</th><th>Date</th><th>Band (m)</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
$ret .= "<tr><td>".$cnt++."</td><td>".$r[0]."</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td></tr>\n";
}
$ret .= "</table>";
$cnt--;
$ret = "<h2>ACA details for $c ($y): $cnt</h2>".$ret;
return $ret;
}
function acma($c, $y) {
global $db;
$ret = "<h2>ACMA details for $c</h2>";
$q = mysqli_query($db, "SELECT nr, hiscall, date, band from cwops_log where `mycall`='$c' and year=$y and nr > 0 group by nr, band");
if(!$q) {
echo mysqli_error($db);
}
$cnt = 1;
$ret .= "<table><tr><th>Count</th><th>CWops</th><th>Call</th><th>Date</th><th>Band (m)</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
$ret .= "<tr><td>".$cnt++."</td><td>".$r[0]."</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td></tr>\n";
}
$ret .= "</table>";
return $ret;
}
function cma($c) {
global $db;
$ret = "<h2>CMA details for $c</h2>";
$q = mysqli_query($db, "SELECT nr, hiscall, date, band from cwops_log where `mycall`='$c' and nr > 0 group by nr, band");
if(!$q) {
echo mysqli_error($db);
}
$cnt = 1;
$ret .= "<table><tr><th>Count</th><th>CWops</th><th>Call</th><th>Date</th><th>Band (m)</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
$ret .= "<tr><td>".$cnt++."</td><td>".$r[0]."</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td></tr>\n";
}
$ret .= "</table>";
return $ret;
}
function was($c, $b) {
global $db;
global $arr_states;
$ret = "<h2>WAS details for $c";
if ($b != "all") {
$band = " and band=$b ";
$ret .= " (".$b."m)";
}
$ret .= "</h2>";
$q = mysqli_query($db, "SELECT `was`, `nr`, hiscall, date, band from cwops_log where `mycall`='$c' and nr > 0 and LENGTH(`was`) = 2 $band group by `was`");
if(!$q) {
echo mysqli_error($db);
}
$states_needed = $arr_states;
$cnt = 1;
$ret .= "<table><tr><th>Count</th><th>State</th><th>CWops</th><th>Call</th><th>Date</th><th>Band</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
$ret .= "<tr><td>".$cnt++."</td><td>".$r[0]."</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td><td>".$r[4]."</td></tr>\n";
unset($states_needed[$r[0]]);
}
$ret .= "</table>";
$ret .= "<p>Needed: ".implode(", ", array_keys($states_needed))."</p>";
return $ret;
}
function waz($c, $b) {
global $db;
$ret = "<h2>WAZ details for $c";
if ($b != "all") {
$band = " and band=$b ";
$ret .= " (".$b."m)";
}
$ret .="</h2>";
$q = mysqli_query($db, "SELECT `waz`, `nr`, hiscall, date, band from cwops_log where `mycall`='$c' and nr > 0 and waz > 0 $band group by `waz`");
if(!$q) {
echo mysqli_error($db);
}
$cnt = 1;
$ret .= "<table><tr><th>Count</th><th>Zone</th><th>CWops</th><th>Call</th><th>Date</th><th>Band</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
$ret .= "<tr><td>".$cnt++."</td><td>".$r[0]."</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td><td>".$r[4]."</td></tr>\n";
}
$ret .= "</table>";
return $ret;
}
function dxcc($c, $b) {
global $db;
global $dxcc;
$ret = "<h2>DXCC details for $c";
if ($b != "all") {
$band = " and band=$b ";
$ret .= " (".$b."m)";
}
$ret .="</h2>";
$q = mysqli_query($db, "SELECT `dxcc`, `nr`, hiscall, date, band from cwops_log where `mycall`='$c' and nr > 0 and dxcc > 0 $band group by `dxcc` order by hiscall asc");
if(!$q) {
echo mysqli_error($db);
}
$cnt = 1;
$ret .= "<table><tr><th>Count</th><th>DXCC</th><th>CWops</th><th>Call</th><th>Date</th><th>Band</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
$ret .= "<tr><td>".$cnt++."</td><td>".$dxcc[$r[0]]." (".$r[0].")</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td><td>".$r[4]."</td></tr>\n";
}
$ret .= "</table>";
return $ret;
}
function wae($c, $b) {
global $db;
global $waes;
global $wae_adif;
global $dxcc;
$needed = array();
foreach (array_merge($wae_adif, array_keys($waes)) as $k) {
$needed[$k] = 1;
}
$ret = "<h2>WAE details for $c";
if ($b != "all") {
$band = " and band=$b ";
$ret .= " (".$b."m)";
}
$ret .="</h2>";
$q = mysqli_query($db, "SELECT `dxcc`, `nr`, hiscall, date, band from cwops_log where `mycall`='$c' and nr > 0 and dxcc in (".implode(',', $wae_adif).") and wae='' $band group by `dxcc` order by hiscall asc");
if(!$q) {
echo mysqli_error($db);
}
# Special status for Kosovo: QSOs before 2018-01-21 count as WAE KO but
# DXCC Serbia. But we need to make sure only to count Kosovo once. So if
# we have Kosovo worked as a proper DXCC, don't count it again in the 2nd
# query where we specifically query for WAEs.
$ko = false;
$cnt = 1;
$ret .= "<table><tr><th>Count</th><th>DXCC</th><th>CWops</th><th>Call</th><th>Date</th><th>Band</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
$ret .= "<tr><td>".$cnt++."</td><td>".$dxcc[$r[0]]." (".$r[0].")</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td><td>".$r[4]."</td></tr>\n";
unset($needed[$r[0]]);
if ($r[0] == 522) {
$ko = true;
}
}
# fetch extra WAE entities
$q = mysqli_query($db, "SELECT `wae`, `nr`, hiscall, date, band from cwops_log where `mycall`='$c' and nr > 0 and LENGTH(wae) = 2 $band group by `wae`");
if(!$q) {
echo mysqli_error($db);
}
$kowae = false;
while ($r = mysqli_fetch_row($q)) {
unset($needed[$r[0]]);
if ($ko && $r[0] == 'KO') { # do now show Kosovo twice (DXCC + WAE)
continue;
}
if ($r[0] == 'KO') { # if we have Kosovo as WAE but not DXCC, remove it from needed DXCC list
$kowae = true;
unset($needed[522]);
}
$ret .= "<tr><td>".$cnt++."</td><td>".$waes[$r[0]]." (".$r[0].")</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td><td>".$r[4]."</td></tr>\n";
}
if ($ko) {
unset($needed['KO']);
}
# do not show kosovo twice on needed list if it was neither worked as a
# DXCC nor as a WAE
if ($kowae == false && $ko == false) {
unset($needed['KO']);
}
// replace numeric ADIF numbers with DXCC names and WAE abbreviations with
// full name
foreach (array_keys($needed) as $k) {
if (is_numeric($k)) {
$needed[$dxcc[$k]] = 1;
}
else {
$needed[$waes[$k]] = 1;
}
unset($needed[$k]);
}
$ret .= "</table><br>Still needed:<br>".implode('<br>', array_keys($needed));
return $ret;
}
function qtx($c) {
global $site_year;
global $db;
$s = array();
$ret = "<div class='qtxcontainer'>";
foreach (array("QTX", "mQTX") as $t) {
if ($t == "mQTX")
$q = mysqli_query($db, "SELECT nr, hiscall, date, band, qsolength from cwops_log where `mycall`='$c' and year=$site_year and qsolength >= 10 and qsolength < 20");
else
$q = mysqli_query($db, "SELECT nr, hiscall, date, band, qsolength from cwops_log where `mycall`='$c' and year=$site_year and qsolength >= 20");
if(!$q) {
echo mysqli_error($db);
}
$cnt = 1;
$ret .= "<table><tr><th colspan=6>$t</th></tr><tr><th>Count</th><th>CWops</th><th>Call</th><th>Date</th><th>Band (m)</th><th>Length</th></tr>\n";
while ($r = mysqli_fetch_row($q)) {
if ($r[0]+0 == 0) {
$r[0] = "";
}
$ret .= "<tr><td>".$cnt++."</td><td>".$r[0]."</td><td>".$r[1]."</td><td>".$r[2]."</td><td>".$r[3]."</td><td>".$r[4]."</td></tr>\n";
}
$s[$t] = $cnt-1;
$ret .= "</table> ";
# Lifetime scores
if ($t == "mQTX")
$q = mysqli_query($db, "SELECT count(*) from cwops_log where `mycall`='$c' and qsolength >= 10 and qsolength < 20 and date >= '2018-07-01'");
else
$q = mysqli_query($db, "SELECT count(*) from cwops_log where `mycall`='$c' and qsolength >= 20");
$r = mysqli_fetch_row($q);
$s["lt$t"] = $r[0];
}
$ret .= "</div>";
$ret = "<h2>QTX details for $c</h2>\n<table><tr><td>QTX/mQTX this year:</td><td>".$s["QTX"]."</td><td>".$s["mQTX"]."</td></tr><tr><td>QTX/mQTX lifetime:</td><td>".$s["ltQTX"]."</td><td>".$s["ltmQTX"]."</td></tr></table><br><br>".$ret;
return $ret;
}
# import an ADIF file to the log of $callsign
#
# Only import QSOs when it's a new
# - Member (identified by his number) on this band OR in a new year
# - State on this band
# - DXCC on this band
# - WAZ on this band
# - WAE (on any band)
# Steps:
# 1. Load the current member list into an array
# 2. Parse ADIF/CSV into an array, omitting all calls that are not members (considering the date of the QSO) and non-CW-QSOs
# 3. Iterate through imported log and based on (1) decide which QSOs are saved in the database.
# If ign is set, ignore DXCC, WAZ and State info from the uploaded log
function import($filename, $adif, $callsign, $ign) {
global $db;
$ret = "<br>Starting import ($filename) for $callsign...<br>";
$members = get_memberlist();
$ret .= "Loaded member list with ".count($members)." entries.<br>";
# detect start date for importing QSOs, based on user's callsign.
$startdate = get_joindate($callsign);
if ($startdate) {
$ret .= "CWops join date for $callsign is ".$startdate." <br>";
$startdate = preg_replace('/\-/', '', $startdate);
}
else {
$ret .= "<b>Could not identify $callsign as a CWops member (possibly because you recently joined and the member list on this site is not yet updated). Starting import from 2010-01-01. If you joined CWops later, please only upload logs after your start date!</b><br>";
}
# detect data format. it may be ADIF, CSV export from CAM
# or Cabrillo. If it is Cabrillo or CSV, convert it to ADIF
# first and then import it
if (strstr($adif, "START-OF-LOG:")) {
$ret .= "Data format looks like Cabrillo. Trying to convert...<br>";
$adif = parse_cam_cbr($adif, $members, "CBR");
}
else if (!(strstr($adif, "<eoh>") or strstr($adif, "<EOH>"))) { # no end of ADIF header
$ret .= "Data format looks like CAM exported CSV (not ADIF). Trying to convert...<br>";
$adif = parse_cam_cbr($adif, $members, "CAM");
}
$qsos = parse_adif($adif, $members, $ign, $startdate);
$ret .= "Parsed log file with ".count($qsos)." QSOs with CWops members (or QTX QSOs).<br>";
$qsos = filter_qsos($qsos, $callsign);
$ret .= "Imported ".count($qsos)." QSOs which were new for award purposes.<br>";
$nr = rand(0,10000);
$ret .= "Full log of the import below. <a href='#' onClick='javascript:document.getElementById(\"import_log$nr\").style.display = \"none\";'>Click here to hide</a>";
$ret .= "<pre id='import_log$nr'>";
$import_log = "";
foreach ($qsos as $q) {
$import_log .= "QSO: ".$q['call']." ".$q['date']." ".$q['band']." needed for: ".$q['reasons']."<br>";
}
$ret .= $import_log;
$ret .= "</pre>";
# save this to the upload history
$q = mysqli_query($db, "insert into cwops_uploads (`uid`, `ts`, `count`, `result`)
VALUES (".$_SESSION['id'].", NOW(), ".count($qsos)." ,'".mysqli_real_escape_string($db, $import_log)."')");
if (!$q) {
error_log("import: insert into cwops_upload failed: ".mysqli_error($db));
}
return $ret;
}
function get_memberlist() {
global $db;
$q = mysqli_query($db, "SELECT * from cwops_members;");
$members = array();
while ($r = mysqli_fetch_array($q, MYSQLI_ASSOC)) {
array_push($members, $r);
}
return $members;
}
# parse CAM CSV file format or Cabrillo log and make ADIF
# CAM: 20100101,1111,N3JT,40M,CW,K,VA,JIM,1
# CBR: QSO: 3500 CW 2019-10-03 0721 DA0HSC 599 M DL5AXX 599 GI
function parse_cam_cbr ($data, $members, $type) {
# make hash table for quicker member lookup
$mh = array(); # call -> info
$mhnr = array(); # nr -> call
foreach ($members as $m) {
$mh[$m["callsign"]] = $m;
$mhnr[$m["nr"]] = $m["callsign"];
}
$data = strtoupper($data);
$data = preg_replace('/\r/', '', $data);
$qsos = explode("\n", $data);
$adif = "header\n<EOH>\n";
foreach ($qsos as $q) {
if ($type == "CAM") {
$a = explode(",", $q);
if (count($a) != 9) {
continue;
}
$qso_date = $a[0];
$call = $a[2];
$band = $a[3];
$state = $a[6];
}
if ($type == "CBR") {
$a = preg_split('/\s+/', $q);
if ($a[0] != "QSO:") {
continue;
}
$qso_date = preg_replace('/\-/', '', $a[3]);
# find the call. most of the time it's the 9th field...
$call = $a[8];
# if not, search after the 7th field
$i = 6;
while (!is_call($call)) {
$call = $a[$i++];
if ($i > count($a)) {
break;
}
}
switch ($a[1]) {
case 50:
$band = "6m";
break;
case 70:
$band = "4m";
break;
case 144:
$band = "2m";
break;
default:
$band = f2b($a[1]/1000)."m";
break;
}
$state = "--";
}
$adif .= makeadi('qso_date', $qso_date);
$adif .= makeadi('call', $call);
$adif .= makeadi('band', $band);
$adif .= makeadi('state', $state);
$adif .= makeadi('mode', "CW");
# CAM: Is this a member call? If not, but the CWops nr is valid,
# add the member's call as a remark
if ($type == "CAM" and !array_key_exists($a[2], $mh) and array_key_exists($a[8], $mhnr)) {
$adif .= "CWO:".$mhnr[$a[8]]." ";
}
$adif .= " <EOR>\n";
}
file_put_contents("/tmp/adif", $adif);
return $adif;
}
function makeadi ($field, $value) {
return "<".$field.":".strlen($value).">".$value." ";
}
# parse ADIF and return member QSOs (matched by date)
function parse_adif($adif, $members, $ign, $startdate) {
global $arr_states;
$out = array();
# make hash table for quicker member lookup
$mh = array();
foreach ($members as $m) {
# fix date
$m['joined'] = preg_replace('/-/','', $m['joined']);
$m['left'] = preg_replace('/-/','', $m['left']);
$mh[$m["callsign"]] = $m;
}
$adif = strtoupper($adif);
$qsos = explode("<EOR>", $adif);
foreach ($qsos as $q) {
unset($call); unset($date); unset($band); unset($qso_length);
if (preg_match('/<CALL:\d+(:\w)?()>([A-Z0-9\/]+)/', $q, $match)) {
$qsocall = $match[3];
# check if QSO is CW mode
if (!preg_match('/<MODE:2(:\w)?()>CW/', $q)) {
continue;
}
# strip call (portable stuff etc.)
if (preg_match('/\//', $qsocall, $match)) {
$tmp = explode('/', $qsocall);
$call = "";
foreach ($tmp as $c) {
if (strlen($c) > strlen($call)) {
$call = $c;
}
}
}
else {
$call = $qsocall;
}
# Check if there's a "CWO:" specified somewhere, e.g. in the
# comment field
if (preg_match('/CWO:([A-Z0-9\/]+)/', $q, $match)) {
$call = $match[1];
}
# check if this is a QTX QSO. Then it will be counted even if
# it's a non-member!
$qso_length = qso_length($q);
# Check if there's a "QLEN:" specified somewhere, e.g. in the
# comment field - this overrides anything found from TIME_ON
# and TIME_OFF
if (preg_match('/QLEN:([0-9\/]+)/', $q, $match)) {
$qso_length = $match[1];
}
# check if it's a member and then date vs. membership date
if (array_key_exists($call, $mh) or $qso_length >= 10) {
preg_match('/<QSO_DATE:\d+(:\w)?()>([0-9]+)/', $q, $match);
if (($match[3] && $match[3] >= $mh[$call]['joined'] && $match[3] <= $mh[$call]['left']) or ($match[3] && $qso_length >= 10)) {
$date = $match[3];
# we have a start date. if the QSO is before this date,
# ignore it.
if ($startdate && $date < $startdate) {
continue;
}
# Valid QSO. Now find out band and optionally state, wae,
# waz and dxcc.
# band?
if (preg_match('/<BAND:\d+(:\w)?()>([0-9]+)(C)?M/', $q, $match)) {
$band = $match[3];
}
# freq?
else if (preg_match('/<FREQ:\d+(:\w)?()>([0-9]+)/', $q, $match)) {
$band = f2b($match[3]);
}
$qso = array();
$qso['call'] = $qsocall;
$qso['date'] = $date;
$qso['band'] = $band;
$qso['nr'] = 0; # QTX
# assign CWops number to this QSO only they were a member
# at this time... (could be a QTX QSO with an ex member)
if ($date >= $mh[$call]['joined'] && $date <= $mh[$call]['left']) {
$qso['nr'] = $mh[$call]['nr'];
}
$qso['was'] = $mh[$call]['was'];
$qso['waz'] = 0;
$qso['wae'] = '';
$qso['dxcc'] = 0;
$qso['qsolength'] = $qso_length;
# make sure every QSO is at least one minute long, we use 0
# as an invalid value
if ($qso['qsolength'] == 0) {
$qso['qsolength'] = 1;
}
if (!$ign) {
# see if there's a state in ADIF which overrides the state
# from the database
if (preg_match('/<STATE:\d+(:\w)?()>([A-Z]+)/', $q, $match)) {
$qso['was'] = $match[3];
}
# CQ zone
if (preg_match('/<CQZ:\d+(:\w)?()>([0-9]+)/', $q, $match)) {
$qso['waz'] = $match[3];
}
# DXCC
if (preg_match('/<DXCC:\d+(:\w)?()>([0-9]+)/', $q, $match)) {
$qso['dxcc'] = $match[3];
}
}
# WAE "Region" http://adif.org/310/ADIF_310.htm#Region_Enumeration
if (preg_match('/<REGION:\d+(:\w)?()>([A-Z]{2})/', $q, $match)) {
$qso['wae'] = $match[3];
}
else {
# recognize some WAEs automatically
if (in_array($qso['call'], array("4U1VIC", "4U1A"))) {
$qso['wae'] = 'IV';
}
elseif (substr($qso['call'], 0, 3) == "IT9") {
$qso['wae'] = 'SY';
}
elseif (preg_match('/^T[ABC]1[A-Z]+$/', $qso['call']) or substr($qso['call'], 0, 4) == "TA1/" ) { # first regex won't match TA1AA/2
$qso['wae'] = 'ET';
}
}
# sanitize WAZ: Some logs may contain the ITU zone instead
# of the CQ zone in the CQZ field, or a completely invalid
# value.
$itu = lookup($qsocall, 'itu', $date);
if ($qso['waz'] == 0 or $qso['waz'] > 40 or $qso['waz'] == $itu) {
$qso['waz'] = lookup($qsocall, 'waz', $date);
}
# hamqth returns empty value for /mm
if ($qso['waz'] == "") { $qso['waz'] = 0; }
if ($qso['dxcc'] == 0) {
$qso['dxcc'] = lookup($qsocall, 'adif', $date);
}
# remove state for cases where it's not applicable, e.g.
# KP2/W1XYZ
# USA KL7 KH6
if ($qso['dxcc'] != 291 && $qso['dxcc'] != 6 && $qso['dxcc'] != 110) {
$qso['was'] = "";
}
if (array_key_exists('was', $qso) and $qso['was'] != "") {
# as per WAS rules, DC counts as Maryland
if ($qso['was'] == "DC") {
$qso['was'] = "MD";
}
# PR is not a state for WAS
if ($qso['was'] == "PR") {
$qso['was'] = "";
}
# check if it's a proper state for US stations
if (!array_key_exists($qso['was'], $arr_states)) {
error_log("Imported state ".$qso['was']." for ".$qso['call']." invalid. ");
$qso['was'] = "";
}
}
# finally, some hard-coded exceptions:
if ($qso['call'] == "K7SV") {
$qso['waz'] = 5;
}
if ($qso['call'] == "NL7VX") {
$qso['waz'] = 5;
$qso['dxcc'] = 291;
$qso['was'] = "VA";
}
array_push($out, $qso);
}
}
}
}
return $out;
}
function qso_length ($q) {
if (preg_match('/<TIME_ON:\d+(:\w)?()>([0-9]{2,2})([0-9]{2,2})/', $q, $t_on ) &&
preg_match('/<TIME_OFF:\d+(:\w)?()>([0-9]{2,2})([0-9]{2,2})/', $q, $t_off)
) {
$hr1 = $t_on[3];
$hr2 = $t_off[3];
$min1 = $t_on[4];
$min2 = $t_off[4];
if ($min2 < $min1) { $min2 += 60; $hr2--; }
if ($hr2 < $hr1) { $hr2 += 24; }
$qso_length = ($min2 - $min1) + ($hr2 - $hr1)*60;
return $qso_length;
}
return 0;
}
function f2b ($f) {
$f *= 1000;
if ($f < 2000) { return "160"; }
elseif ($f < 4000) { return "80"; }
elseif ($f < 5500) { return "60"; }
elseif ($f < 7300) { return "40"; }
elseif ($f < 10150) { return "30"; }
elseif ($f < 14350) { return "20"; }
elseif ($f < 18168) { return "17"; }
elseif ($f < 21450) { return "15"; }
elseif ($f < 24990) { return "12"; }
elseif ($f < 29700) { return "10"; }
elseif ($f < 54000) { return "6"; }
elseif ($f < 75000) { return "4"; }
elseif ($f < 148000) { return "2"; }
elseif ($f < 440000) { return "0.7"; }
return 0;
}
function member_lookup($call) {
global $associates;
$members = get_memberlist($call);
foreach ($members as $m) {
if ($m["callsign"] == $call) {
return json_encode($m);
}
}
}
# look up calls on HamQTH's API
# Save all data in a local Redis Database to avoid flooding the API
# Also load exceptions from OK1RR's country file.
function lookup ($call, $what, $date) {
global $call_exceptions;
if (!$call) {
return "";
}
if ($call == "W8S") {
$call = "KH8S";
}
if ($call == "TX5S") {
$call = "FO0/F8UFT";
}
if ($call == "N5J") {
$call = "KH5J";
}
# check if this callsign is in OK1RR's exception list
if (isset($call_exceptions->$call)) {
error_log("Exceptions for $call found (date: $date): ");
$exc = $call_exceptions->$call;
# check if the date range fits
foreach ($exc as $e) {
error_log($e->start." -> ".$e->stop);
if ($date >= $e->start and $date <= $e->stop) {
if ($what == 'json') {
return json_encode($e);
}
else {
return $e->$what;
}
error_log(json_encode($e));
}
}
}
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$data = $redis->get("HamQTH".$call);