Repository navigation
Expand file tree
/
Copy pathfunctions.php
More file actions
1333 lines (1270 loc) · 52.3 KB
/
Copy pathfunctions.php
File metadata and controls
1333 lines (1270 loc) · 52.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
function hex2ascii($hex)
{
$ascii = '';
$hex = str_replace(" ", "", $hex);
for ($i = 0; $i < strlen($hex); $i = $i + 2) {
$ascii .= chr(hexdec(substr($hex, $i, 2)));
}
return($ascii);
}
function ascii2hex($ascii)
{
$hex = '';
for ($i = 0; $i < strlen($ascii); $i++) {
$byte = strtoupper(dechex(ord($ascii[$i])));
$byte = str_repeat('0', 2 - strlen($byte)) . $byte;
$hex .= $byte . " ";
}
$hex = str_replace(" ", "", $hex);
return $hex;
}
function csv_download_link($i = -1)
{
global $make_csv;
if ($make_csv) {
return;
}
$link = $_SERVER['REQUEST_URI'] . (str_contains($_SERVER['REQUEST_URI'], '?') ? "&" : "?") . "format=csv";
echo "<br style=\"clear: both;\" /><center>";
if ($i >= 0) {
echo "$i Umsätze gefunden<br /><br />";
}
echo "<a href=\"" . html_escape($link) . "\">Als CSV Datei herunterladen</a></center>";
}
function getfilter()
{
$filter = "";
if (is_string($_GET['filter_id'] ?? null) && preg_match('/^\d+$/', $_GET['filter_id']) == 1) {
$filter .= " AND voucher_id = " . $_GET['filter_id'];
}
if (is_string($_GET['filter_bid'] ?? null) && preg_match('/^\d+$/', $_GET['filter_bid']) == 1) {
$filter .= " AND id = " . $_GET['filter_bid'];
}
if (is_string($_GET['filter_date'] ?? null)) {
if (preg_match('/^\d\d\d\d$/', $_GET['filter_date']) == 1) {
$filter .= " AND date >= '" . $_GET['filter_date'] . "-01-01' AND date <= '" . $_GET['filter_date'] . "-12-31' ";
} elseif (preg_match('/^\d\d\d\d-\d\d$/', $_GET['filter_date']) == 1) {
$filter .= " AND date >= '" . $_GET['filter_date'] . "-01' AND date < TIMESTAMP '" . $_GET['filter_date'] . "-01' + INTERVAL '1 month' ";
} elseif (preg_match('/^\d\d\d\d-\d\d-\d\d$/', $_GET['filter_date']) == 1) {
$filter .= " AND date = '" . $_GET['filter_date'] . "' ";
}
}
if (is_string($_GET['filter_type'] ?? null) && preg_match('/^\d+$/', $_GET['filter_type']) == 1) {
$filter .= " AND type = " . $_GET['filter_type'];
} elseif (is_string($_GET['filter_type'] ?? null) && preg_match('/^[äöüÄÖÜa-z0-9,. ]+$/i', $_GET['filter_type']) == 1) {
$filter .= " AND lower(type.name) LIKE lower('%" . $_GET['filter_type'] . "%')";
}
if (is_string($_GET['filter_lo'] ?? null) && preg_match('/^\d+$/', $_GET['filter_lo']) == 1) {
$filter .= " AND orga = " . $_GET['filter_lo'];
}
if (is_string($_GET['filter_lo'] ?? null) && preg_match('/^[äöüÄÖÜa-z0-9,. ]+$/i', $_GET['filter_lo']) == 1) {
$filter .= " AND lower(lo.name) LIKE lower('%" . $_GET['filter_lo'] . "%')";
}
if (isset($_GET['filter_member'])) {
$filter .= " AND member ";
}
if (is_string($_GET['filter_member_id'] ?? null) && preg_match('/^\d+$/', $_GET['filter_member_id']) == 1) {
$filter .= " AND member_id = " . $_GET['filter_member_id'];
}
if (is_string($_GET['filter_gk'] ?? null) && preg_match('/^[@äöüÄÖÜa-z0-9,. ]+$/i', $_GET['filter_gk']) == 1) {
$filter .= " AND contra_account LIKE '" . $_GET['filter_gk'] . "%'";
}
if (is_string($_GET['filter_amount'] ?? null) && preg_match('/^(>|>=|=|<|<=) *-?\d+?$/', $_GET['filter_amount']) == 1) {
$filter .= " AND amount " . $_GET['filter_amount'] . "00";
} elseif (is_string($_GET['filter_amount'] ?? null) && preg_match('/^(>|>=|=|<|<=) *-?\d+((\.|,)(\d\d)?)?$/', $_GET['filter_amount']) == 1) {
$filter .= " AND amount " . str_replace([".",","], ["",""], $_GET['filter_amount']);
} elseif (is_string($_GET['filter_amount'] ?? null) && preg_match('/^*-?\d+((\.|,)(\d\d)?)?$/', $_GET['filter_amount']) == 1) {
$filter .= " AND amount = " . str_replace([".",","], ["",""], $_GET['filter_amount']);
}
if (is_string($_GET['filter_k'] ?? null) && preg_match('/^[@äöüÄÖÜa-z0-9,. ]+$/i', $_GET['filter_k']) == 1) {
$filter .= " AND account LIKE '" . $_GET['filter_k'] . "%'";
}
if (is_string($_GET['filter_vk'] ?? null) && preg_match('/^[@äöüÄÖÜa-z0-9,. ]+$/i', $_GET['filter_vk']) == 1) {
$filter .= " AND vaccount LIKE '" . $_GET['filter_vk'] . "%'";
}
if (is_string($_GET['filter_text'] ?? null) && preg_match('/^[@äöüÄÖÜa-z0-9,. ]+$/i', $_GET['filter_text']) == 1) {
$filter .= " AND lower(comment) LIKE lower('%" . $_GET['filter_text'] . "%')";
}
if (is_string($_GET['filter_comment'] ?? null) && preg_match('/^[äöüÄÖÜa-z0-9,. ]+$/i', $_GET['filter_comment']) == 1) {
$filter .= " AND lower(commentgf) LIKE lower('%" . $_GET['filter_comment'] . "%')";
}
if (isset($_GET['filter_comm'])) {
$filter .= " AND committed ";
}
if (is_string($_GET['filter_ack'] ?? null) && preg_match('/^[äöüÄÖÜa-z0-9 ]+$/i', $_GET['filter_ack']) == 1) {
$filter .= " AND (ack1 LIKE '%" . $_GET['filter_ack'] . "%' OR ack2 LIKE '%" . $_GET['filter_ack'] . "%') ";
}
if (is_string($_GET['filter_ack_old'] ?? null) && preg_match('/^[äöüÄÖÜa-z0-9 ]+$/i', $_GET['filter_ack_old']) == 1) {
$filter .= " AND (ack1_old LIKE '%" . $_GET['filter_ack_old'] . "%' OR ack2_old LIKE '%" . $_GET['filter_ack_old'] . "%') ";
}
if (isset($_GET['filter_refund'])) {
$filter .= " AND refund ";
}
if (isset($_GET['filter_bel'])) {
$filter .= " AND file != 0 ";
}
if (is_string($_GET['filter_name'] ?? null) && preg_match('/^[äöüÄÖÜa-z0-9 ]+$/i', $_GET['filter_name']) == 1) {
$filter .= " AND lower(vouchers.name) LIKE lower('%" . $_GET['filter_name'] . "%')";
}
return $filter;
}
// A listing cell or element: $text is plain text, encoded for HTML (the CSV
// export writes it unchanged apart from the cell rules in tag_raw()).
function tag($tag, $text)
{
return tag_raw($tag, listing_text($text));
}
// Like tag(), but $text is markup (links, forms, nested tags) and is written
// as is; the caller encodes every value in it.
function tag_raw($tag, $text)
{
global $make_csv;
if ($make_csv) {
$text = str_replace(["\r\n", "\n", "\r"], [' / ', ' / ', ' / '], $text ?? '');
// A tab would split the tab-separated cell.
if ($tag == 'td') {
$text = str_replace(["\t"], [' '], $text);
}
if (preg_match('/^-?\d+(\.\d+)?€$/', $text) == 1) {
$text = str_replace(["."], [","], $text);
}
// A cell that a spreadsheet would read as a formula or a quoted field (not
// a number), also after leading whitespace, starts with a single quote.
elseif ($tag == 'td' && preg_match('/^[ \t\n\r\f\x0B]*[=+\-@"]/', $text) == 1 && preg_match('/^-?\d+([.,]\d+)?$/', $text) != 1) {
$text = "'" . $text;
}
if ($tag == 'tr') {
return "$text\n";
} elseif ($tag == 'td') {
return "$text\t";
} else {
return "$text";
}
}
return "<$tag>$text</$tag>\n";
}
// Text and attribute values in HTML: & < > " ' as entities. ISO-8859-1 makes
// htmlspecialchars() byte-transparent for the UTF-8 pages on every PHP
// version (UTF-8 would drop invalid input on PHP 5.3), so umlauts and all
// other text stay unchanged.
function html_escape($text)
{
return htmlspecialchars(strval($text), ENT_QUOTES, 'ISO-8859-1');
}
// Listing cells, shared by the HTML page and the CSV export (no entities).
function listing_text($text)
{
global $make_csv;
if ($make_csv) {
return $text;
}
return html_escape($text);
}
// Plain text in an inline element (<del>, <i>) inside a listing cell: the
// element with the encoded text in HTML, only the text in the CSV export.
function wrap($tag, $text)
{
global $make_csv;
if ($make_csv) {
return strval($text);
}
return "<$tag>" . html_escape($text) . "</$tag>";
}
function acc_header($dbconn, $page = "index", $year = 2015, $navYear = null)
{
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
';
switch ($page) {
case "donations":
echo '<title>Spenden - Piratenpartei Österreichs</title>
<meta name="description" content="Aktuelle Spendenlisten der Piratenpartei Österreichs" />
<meta name="keywords" content="spenden, spendenliste" />';
break;
case "kdonations":
echo '<title>Sachspenden - Piratenpartei Österreichs</title>
<meta name="description" content="Liste aller Sachspenden welche die Piratenpartei Österreichs erhalten hat" />
<meta name="keywords" content="sachspenden, spenden, spendenliste, sachspendenliste" />';
break;
case "spendings":
echo '<title>Ausgaben - Piratenpartei Österreichs</title>
<meta name="description" content="Liste aller Ausgaben welche die Piratenpartei Österreichs getätigt hat" />
<meta name="keywords" content="ausgaben, ausgabenliste" />';
break;
case "wk":
echo '<title>Wahlkampfkosten - Piratenpartei Österreichs</title>
<meta name="description" content="Wahlkampfabrechnungen der Piratenpartei Österreichs" />
<meta name="keywords" content="wahlkampf, wahlkampfkosten, wahlkampfabrechnung" />';
break;
case "transactions":
echo '<title>Kontobewegungen - Piratenpartei Österreichs</title>
<meta name="description" content="Liste aller Kontobewegungen der Piratenpartei Österreichs" />
<meta name="keywords" content="konto, einsicht, kontenbewegungen, transaktionen" />';
break;
default:
echo '<title>Rechenschaftsbericht - Piratenpartei Österreichs</title>
<meta name="description" content="Der vorläufige Rechenschaftsbericht der Piratenpartei Österreichs" />
<meta name="keywords" content="einnahmen, ausgaben, rechenschaftsbericht, spenden, spendenliste, sachspenden, wahlkampf, wahlkampfabrechnung, piratenpartei, österreichs, österreich" />';
break;
}
echo '
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="screen" href="style.css?v=' . PGACC_VERSION . '" />
</head>
<body>
<div id="content">
<div class="wiki motd">Die Daten werden sukzessive eingearbeitet und in einem 4-Augen-Prinzip bestätigt. Die Angaben im Rechenschaftsbericht können also derzeit noch geringfügig variieren.' . percent_of_bookings() . '</div>
';
$rights = checklogin('rights', false);
// The links to the report and the donation list keep the year of these
// pages ($navYear, set by index.php once a year was chosen).
$yearQuery = $navYear === null ? '' : '?year=' . intval($navYear);
echo '
<div class="page ' . $page . '">
<div class="main" id="default">
<div class="slot_default" id="slot_default"><div class="ui_tabs"><div class="ui_tabs_links">
<a href="/' . $yearQuery . '"' . ($page == "index" ? ' class="selected"' : '') . '>Rechenschaftsbericht</a>';
echo '<a href="donations' . $yearQuery . '"' . ($page == "donations" ? ' class="selected"' : '') . '>Spenden</a>
<a href="kdonations"' . ($page == "kdonations" ? ' class="selected"' : '') . '>Sachspenden</a>
<a href="spendings"' . ($page == "spendings" ? ' class="selected"' : '') . '>Ausgaben</a>
<a href="wk"' . ($page == "wk" ? ' class="selected"' : '') . '>Wahlkampf</a>
<a href="transactions"' . ($page == "transactions" ? ' class="selected"' : '') . '>Kontobeweg.</a>
<a href="vreport"' . ($page == "vreport" ? ' class="selected"' : '') . '>Virt. Konten</a>
';/*<a href="index.php?action=statistics"'.($page == "statistics"?' class="selected"':'').'>Statistiken</a>
';*/
if (strlen($rights ?? '') > 0) {
echo '<br />';
if (write_rights($rights) !== false) {
echo '<a href="index.php?action=new"' . ($page == "new" ? ' class="selected"' : '') . '>Erfassen</a>';
echo '<a href="index.php?action=import"' . ($page == "import" ? ' class="selected"' : '') . '>Import</a>';
echo '<a href="index.php?action=import2"' . ($page == "import2" ? ' class="selected"' : '') . '>Belege Import</a>';
}
echo '<a href="index.php?action=open&filter_date=' . $year . '"' . ($page == "open" ? ' class="selected"' : '') . '>Offen</a>';
echo '<a href="index.php?action=closed&filter_date=' . $year . '"' . ($page == "closed" ? ' class="selected"' : '') . '>Abgeschlossen</a>';
echo '<a href="index.php?action=all&filter_date=' . $year . '"' . ($page == "all" ? ' class="selected"' : '') . '>Alles</a>';
echo '<a href="index.php?action=mb"' . ($page == "mb" ? ' class="selected"' : '') . '>Mitgliedsbeiträge</a>';
echo '<a href="index.php?action=tinyreport"' . ($page == "tinyreport" ? ' class="selected"' : '') . '>Konten</a>';
//echo '<a href="index.php?action=vreport"'.($page == "vreport"?' class="selected"':'').'>Virt. Konten</a>';
if (has_right($rights, 'bgf') || has_right($rights, 'Rbgf') || has_right($rights, 'root')) {
echo '<a href="index.php?action=deleted&filter_date=' . $year . '"' . ($page == "deleted" ? ' class="selected"' : '') . '>Alte Rev.</a>';
}
// if (strpos($rights,'root') !== false)
echo '<a href="index.php?action=accounts"' . ($page == "accounts" ? ' class="selected"' : '') . '>Benutzer</a>';
}
echo '</div><br />
';
}
function block_start($p = "")
{
global $make_csv;
if ($make_csv) {
return;
}
echo '
<br style="clear: both;" />
<div class="wiki use_terms' . $p . '">
';
}
function block_end()
{
global $make_csv;
if ($make_csv) {
return;
}
echo '
</div>
';
}
function acc_footer()
{
$rights = checklogin('rights', false);
echo '
</div></div>
<div class="slot_default" id="slot_default"><div class="ui_tabs">
<div class="ui_tabs"><div class="ui_tabs_links">
<a href="index.php?action=impressum">Impressum</a>
';
if (strlen($rights ?? '') == 0) {
echo '<a href="index.php?action=login">Login</a>';
}
if (strlen($rights ?? '') != 0) {
echo '<form class="inline" action="index.php?action=logout" method="post">' . csrf_field() . '<button class="link" type="submit">Logout</button></form>';
}
echo ' </div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
';
}
// The year selector of the report and the donations page: a link to the
// previous year, a drop-down (<details>, no JavaScript) with every year from
// the current one down to 2012 and a link to the next year. $urlForYear($year)
// returns the (unescaped) link of a year; with $allowAll the list starts with
// "Alle Jahre", linked as $urlForYear(null) and selected for $selected null.
// The previous and next links stay within 2012 and the current year;
// $selected is read as an integer.
function year_nav($selected, $urlForYear, $allowAll = false)
{
$first = 2012;
$last = intval(date('Y'));
$all = $allowAll && $selected === null;
$selected = intval($selected);
$label = $all ? 'Alle Jahre' : $selected;
$step = function ($class, $text, $label, $year) use ($urlForYear) {
if ($year === null) {
return "<span class=\"year_nav_step $class\" role=\"link\" aria-disabled=\"true\" aria-label=\"$label\">$text</span>";
}
$title = "$label: $year";
return "<a class=\"year_nav_step $class\" href=\"" . html_escape($urlForYear($year)) . "\" aria-label=\"$title\" title=\"$title\">$text</a>";
};
// With "Alle Jahre" selected, $selected is 0: no year is marked and there
// is no previous year.
$prev = $selected > $first ? min($selected - 1, $last) : null;
$next = !$all && $selected < $last ? max($selected + 1, $first) : null;
$items = [];
if ($allowAll) {
$items[] = [null, 'Alle Jahre', $all];
}
for ($i = $last; $i >= $first; $i--) {
$items[] = [$i, $i, $selected === $i];
}
$html = '<nav class="year_nav" aria-label="Jahr">' . $step('year_nav_prev', '‹', 'Vorheriges Jahr', $prev)
. '<details class="year_nav_menu"><summary aria-label="Jahr wählen: ' . $label . '">' . $label . '</summary><ul>';
foreach ($items as $item) {
$current = $item[2] ? ' aria-current="page"' : '';
$html .= '<li><a href="' . html_escape($urlForYear($item[0])) . "\"$current>" . $item[1] . '</a></li>';
}
return $html . '</ul></details>' . $step('year_nav_next', '›', 'Nächstes Jahr', $next) . '</nav>';
}
// Whether the comma-separated rights contain exactly this right ("bgf" is
// not contained in "Rbgf").
function has_right($rights, $right)
{
return in_array($right, rights_tokens($rights), true);
}
// The comma-separated rights, without surrounding whitespace.
function rights_tokens($rights)
{
return array_map(trim(...), explode(",", $rights ?? ''));
}
// Whether the rights may use this organization for a voucher: any for the
// exact bgf right, otherwise only the numeric organizations of the rights,
// compared as numbers like the database does ("03" is 3). This is the scope
// that rights2orgasql() lets the user read (root is no organization right
// there either), so users write only what they can see.
function organization_allowed($lo, $rights)
{
if (has_right($rights, 'bgf')) {
return true;
}
$lo = strval($lo);
if (preg_match('/^\d+$/D', $lo) != 1) {
return false;
}
foreach (rights_tokens($rights) as $right) {
if (preg_match('/^\d+$/D', $right) == 1 && ltrim($right, '0') === ltrim($lo, '0')) {
return true;
}
}
return false;
}
// Rights for changing data. The read-only right "Rbgf" (see accounts.php)
// does not count: false for a user who has no other right, otherwise the
// other rights. Rights without "Rbgf" are returned unchanged.
function write_rights($rights)
{
$tokens = rights_tokens($rights);
if (!in_array('Rbgf', $tokens, true)) {
return $rights;
}
$rights_w = [];
foreach ($tokens as $right) {
if ($right !== 'Rbgf' && $right !== '') {
$rights_w[] = $right;
}
}
if (count($rights_w) == 0) {
return false;
}
return implode(",", $rights_w);
}
function rights2orgasql($rights)
{
$rights_r = rights_tokens($rights);
if (!in_array('bgf', $rights_r) && !in_array('Rbgf', $rights_r)) {
$rights2 = [];
foreach ($rights_r as $right) {
if (preg_match('/^\d+$/', $right) == 1) {
$rights2[] = "'" . $right . "'";
}
}
if (count($rights2) > 0) {
return " AND orga IN (" . implode(",", $rights2) . ") ";
}
return " AND false ";
}
return "";
}
function konto2lo($konto)
{
switch ($konto) {
case '50110117270':
return 1;
case '50110117300':
return 2;
case '50110117318':
return 3;
case '50110117326':
return 4;
case '50110117350':
return 5;
case '50110117369':
return 6;
case '50110117393':
return 10;
case '10110123642':
return 9;
case '50110110437':
return 10;
default:
return $konto;
}
}
function getsort()
{
$sort = "date ASC, voucher_id DESC, vouchers.id ASC";
if (isset($_GET["sort"])) {
switch ($_GET["sort"]) {
case 'ida':
$sort = "voucher_id ASC, vouchers.id ASC";
break;
case 'idd':
$sort = "voucher_id DESC, vouchers.id ASC";
break;
case 'bida':
$sort = "vouchers.id ASC, voucher_id ASC";
break;
case 'bidd':
$sort = "vouchers.id DESC, voucher_id ASC";
break;
case 'dated':
$sort = "date DESC, voucher_id DESC, vouchers.id ASC";
break;
case 'datea':
$sort = "date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'typea':
$sort = "type ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'typed':
$sort = "type DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'loa':
$sort = "orga ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'lod':
$sort = "orga DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'membera':
$sort = "member_id ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'memberd':
$sort = "member_id DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'gka':
$sort = "contra_account DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'gkd':
$sort = "contra_account ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'ka':
$sort = "account ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'kd':
$sort = "account DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'ama':
$sort = "amount ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'amd':
$sort = "amount DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'commenta':
$sort = "commentgf ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'commentd':
$sort = "commentgf DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'texta':
$sort = "comment ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'textd':
$sort = "comment DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'comma':
$sort = "committed DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'commd':
$sort = "committed ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'acka':
$sort = "ack1 ASC, ack2 ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'ackd':
$sort = "ack1 DESC, ack2 DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'refunda':
$sort = "refund ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'refundd':
$sort = "refund DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'bela':
$sort = "file ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'beld':
$sort = "file DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'namea':
$sort = "name ASC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
case 'named':
$sort = "name DESC, date ASC, voucher_id DESC, vouchers.id ASC";
break;
default:
}
}
return $sort;
}
function getoppsort($sortn)
{
if (isset($_GET["sort"])) {
if ($_GET["sort"] == $sortn) {
$sortdir = substr($sortn ?? '', -1, 1);
switch ($sortdir) {
case 'a':
return substr($sortn, 0, -1) . 'd';
case 'd':
return substr($sortn, 0, -1) . 'a';
}
}
}
return $sortn;
}
function percent_of_bookings()
{
global $dbconn;
$rows = [];
$startYear = 2012;
$endYear = date('Y') + 1;
for ($currentYear = $startYear; $currentYear < $endYear + 1; $currentYear++) {
$query = "SELECT COUNT(*) AS c,COUNT(ack1) AS b,COUNT(ack2) AS a FROM vouchers WHERE date >= '" . $currentYear . "-01-01' AND date < '" . ($currentYear + 1) . "-01-01' AND NOT deleted;";
$result = db_query($dbconn, $query);
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
$c = intval($line['c']);
if ($c <= 0) {
continue;
}
$b = intval($line['b']) - intval($line['a']);
$bp = round($b * 100 / (1.0 * $c), 2);
$a = intval($line['a']);
$ap = round($a * 100 / (1.0 * $c), 2);
$d = $c - $b - $a;
$dp = round($d * 100 / (1.0 * $c), 2);
$rows[$currentYear] = '<tr><th scope="row">' . html_escape($currentYear) . '</th><td>' . html_escape($c) . '</td>'
. '<td>' . html_escape($d) . ' <span class="percent">(' . html_escape("$dp %") . ')</span></td>'
. '<td>' . html_escape($b) . ' <span class="percent">(' . html_escape("$bp %") . ')</span></td>'
. '<td>' . html_escape($a) . ' <span class="percent">(' . html_escape("$ap %") . ')</span></td>'
. '<td class="bar"><span aria-hidden="true" title="'
. html_escape("$currentYear: $dp % unbearbeitet, $bp % bearbeitet, $ap % abgeschlossen") . '">'
. '<span class="open" style="width: ' . html_escape("$dp%") . '"></span>'
. '<span class="edited" style="width: ' . html_escape("$bp%") . '"></span>'
. '<span class="closed" style="width: ' . html_escape("$ap%") . '"></span></span></td></tr>';
}
pg_free_result($result);
}
if ($rows === []) {
return "";
}
// Newest first. Shown: the current and the previous year, at least the
// two most recent years with bookings; older years behind <details>.
$rows = array_reverse($rows, true);
$shown = 0;
foreach (array_keys($rows) as $year) {
if ($year >= $endYear - 2) {
$shown++;
}
}
$shown = max($shown, min(2, count($rows)));
$older = array_slice($rows, $shown, null, true);
$text = progress_table('Bearbeitungsstand der Buchungszeilen je Jahr', '', array_slice($rows, 0, $shown, true));
if ($older !== []) {
$years = array_keys($older);
$range = html_escape(end($years));
if (count($years) > 1) {
$range .= '–' . html_escape($years[0]);
}
$text .= '<details class="more"><summary>Weitere Jahre anzeigen (' . $range . ')</summary>'
. progress_table('Bearbeitungsstand der Buchungszeilen, weitere Jahre', ' class="older"', $older)
. '</details>';
}
return '<div class="progress">' . $text . '</div>';
}
// One table of the booking progress. Both tables have the same columns and
// widths; the older years' table hides its caption and head visually.
function progress_table($caption, $attributes, $rows)
{
return '<table' . $attributes . '><caption>' . $caption . '</caption>'
. '<colgroup><col class="year" /><col span="4" /><col class="bar" /></colgroup>'
. '<thead><tr><th scope="col">Jahr</th><th scope="col">Buchungs­zeilen</th>'
. '<th scope="col"><span class="open" aria-hidden="true"></span>unbe­arbeitet</th>'
. '<th scope="col"><span class="edited" aria-hidden="true"></span>bear­beitet</th>'
. '<th scope="col"><span class="closed" aria-hidden="true"></span>abge­schlossen</th>'
. '<th class="bar" aria-hidden="true"></th></tr></thead>'
. '<tbody>' . implode('', $rows) . '</tbody></table>';
}
function eyes()
{
if (isset($_GET["noeyes"])) {
return " NOT deleted ";
} else {
return " NOT deleted AND (ack1 IS NOT NULL OR ack2 IS NOT NULL) ";
}
}
/**
* Return a subset of the parameter array with only keys of the whitelist.
* Only checks keys, not values.
*
* @author meisterluk
* @author Peter Grassberger <petertheone@gmail.com>
*
* @param $array array to select from
* @param $whitelist array of keys to select
* @return array a subset of array
*/
function whitelist($array, $whitelist)
{
$new_array = [];
foreach ($array as $key => $value) {
if (isset($whitelist[$key])) {
$new_array[$key] = $value;
}
}
return $new_array;
}
function vouchers_reset_ack($id, $rightssql)
{
global $dbconn;
$query = "
UPDATE vouchers
SET
ack1_old = ack1,
ack2_old = ack2,
ack1 = NULL,
ack2 = NULL
WHERE
NOT deleted AND
(ack1 IS NOT NULL OR ack2 IS NOT NULL) AND
voucher_id = $id $rightssql;
";
db_exec($dbconn, $query);
}
/*
* Replacement for the removed mcrypt extension, byte-compatible with
* mcrypt_encrypt()/mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data,
* MCRYPT_MODE_CBC, $iv): Rijndael with a 256-bit block (not AES), CBC mode,
* data zero-padded to whole 32-byte blocks (at least one), padding kept on
* decryption. Keys and IVs are 32-byte md5 hex strings. phpseclib uses the
* mcrypt extension where it is loaded and its own implementation elsewhere;
* both give the same bytes.
* Contract vectors: tests/fixtures/mcrypt/vectors.json.
*/
function rijndael256_cbc($key, $iv)
{
$cipher = new \phpseclib\Crypt\Rijndael(\phpseclib\Crypt\Base::MODE_CBC);
$cipher->setBlockLength(256);
$cipher->setKey($key);
$cipher->setIV($iv);
$cipher->disablePadding();
return $cipher;
}
function rijndael256_zero_pad($data)
{
$blocks = max(1, intval(ceil(strlen($data) / 32)));
return str_pad($data, $blocks * 32, "\0");
}
function rijndael256_encrypt($key, $data, $iv)
{
return rijndael256_cbc($key, $iv)->encrypt(rijndael256_zero_pad($data));
}
function rijndael256_decrypt($key, $data, $iv)
{
return rijndael256_cbc($key, $iv)->decrypt(rijndael256_zero_pad($data));
}
/*
* Receipt files, keyed by the voucher ID: key and IV, shared by encryption
* and decryption. See the mcrypt and receipt key items in
* tests/COMPATIBILITY_PLAN.md before changing the key derivation.
*/
function receipt_cipher_params($id, $key)
{
return [md5($id . $key . $id), md5($key . $id)];
}
function receipt_encrypt($id, $key, $data)
{
[$cipherKey, $iv] = receipt_cipher_params($id, $key);
return rijndael256_encrypt($cipherKey, $data, $iv);
}
function receipt_decrypt($id, $key, $data)
{
[$cipherKey, $iv] = receipt_cipher_params($id, $key);
return rijndael256_decrypt($cipherKey, $data, $iv);
}
/*
* Receipt storage in files/. New receipts are <n>.v2.aes: keyed on the file
* number <n> and the configured $key of constants.php, AES-256-CBC with a
* random IV, authenticated with HMAC-SHA256 (see receipt_v2_encrypt()).
* Legacy receipts are <n>.aes: keyed on the voucher ID with an empty key part
* (receipt_encrypt(); the callers never read the configured key).
* migrate-receipt-key.php re-encrypts legacy files. See the receipt key item
* in tests/COMPATIBILITY_PLAN.md.
*/
// At least 16 characters and not the CHANGE-ME placeholder of the README.
function receipt_key_is_valid($key)
{
return is_string($key) && strlen($key) >= 16 && !str_contains($key, 'CHANGE-ME');
}
// The configured key, or '' without a valid one (receipts then fail closed).
function receipt_configured_key()
{
// constants.php is required in global scope (index.php).
if (isset($GLOBALS['key']) && receipt_key_is_valid($GLOBALS['key'])) {
return $GLOBALS['key'];
}
return '';
}
// Identifies a key (the migration's --key-fingerprint, shown to root on the
// user administration page) without revealing it: Argon2id with a fixed salt
// (the first 16 bytes of SHA-256("pgacc-key-fingerprint")), opslimit 2 and
// memlimit 64 MiB (libsodium's interactive limits), first 8 hex digits. The
// slow derivation keeps a fingerprint that was seen from being a fast test
// for guessed keys.
function receipt_key_fingerprint($key)
{
$salt = substr(hash('sha256', 'pgacc-key-fingerprint', true), 0, 16);
return substr(bin2hex(sodium_crypto_pwhash(16, $key, $salt, 2, 67108864, SODIUM_CRYPTO_PWHASH_ALG_ARGON2ID13)), 0, 8);
}
// The receipt key paragraph of the user administration page (root only):
// the fingerprint of the configured key, and a warning for a short one.
function receipt_key_notice($key)
{
if (!receipt_key_is_valid($key)) {
return '<p>Fingerabdruck des Beleg-Schlüssels (für migrate-receipt-key.php --key-fingerprint): kein gültiger Schlüssel ($key) konfiguriert</p>';
}
return '<p>Fingerabdruck des Beleg-Schlüssels (für migrate-receipt-key.php --key-fingerprint): ' . html_escape(receipt_key_fingerprint($key)) . '</p>'
. (strlen($key) < 32 ? '<p>Warnung: Der Beleg-Schlüssel ($key) hat weniger als 32 Zeichen. Er darf nicht mehr geändert werden, sobald Belege damit verschlüsselt sind (UPGRADING.md).</p>' : '');
}
function receipt_v2_path($directory, $number)
{
return $directory . '/' . $number . '.v2.aes';
}
function receipt_legacy_path($directory, $number)
{
return $directory . '/' . $number . '.aes';
}
// PDF, JPEG, PNG and GIF by their magic bytes; anything else is sent as a
// download. A PDF header may also follow white-space (PDF's, NUL included)
// within the first 1024 bytes, as PDF readers accept it. ISO 32000 requires
// %PDF- as the first line, and Acrobat and Poppler accept the header after
// any bytes; only white-space is accepted here, to keep false positives
// (a wrong voucher ID in the migration, other file types) rare.
function receipt_content_type($plain)
{
if (preg_match('/^[\0\t\n\f\r ]+%PDF-/', substr($plain, 0, 1024))) {
return 'application/pdf';
}
$types = [
'%PDF' => 'application/pdf',
"\xFF\xD8\xFF" => 'image/jpeg',
"\x89PNG\r\n\x1A\n" => 'image/png',
'GIF87a' => 'image/gif',
'GIF89a' => 'image/gif',
];
foreach ($types as $magic => $type) {
if (str_starts_with($plain, $magic)) {
return $type;
}
}
return 'application/octet-stream';
}
function receipt_has_known_type($plain)
{
return receipt_content_type($plain) !== 'application/octet-stream';
}
// RFC 5869 HKDF-SHA256 (hash_hkdf() needs PHP 7.1.2). An empty salt is the
// RFC's default (HMAC treats it like HashLen zero bytes).
function receipt_hkdf_extract($salt, $ikm)
{
return hash_hmac('sha256', $ikm, $salt, true);
}
function receipt_hkdf_expand($prk, $info, $length)
{
$okm = '';
$block = '';
for ($counter = 1; strlen($okm) < $length; $counter++) {
$block = hash_hmac('sha256', $block . $info . chr($counter), $prk, true);
$okm .= $block;
}
return substr($okm, 0, $length);
}
// Encryption and MAC keys of one <n>.v2.aes file: HKDF-SHA256 of the
// configured key, with the purpose and the file number in the info.
function receipt_v2_keys($key, $number)
{
$prk = receipt_hkdf_extract('', $key);
return [
receipt_hkdf_expand($prk, 'pgacc-receipt-v2|enc|' . $number, 32),
receipt_hkdf_expand($prk, 'pgacc-receipt-v2|mac|' . $number, 32),
];
}
function receipt_v2_cipher($encryptionKey, $iv)
{
$cipher = new \phpseclib\Crypt\AES(\phpseclib\Crypt\Base::MODE_CBC);
$cipher->setKey($encryptionKey);
$cipher->setIV($iv);
return $cipher;
}
// A CBC cipher without padding that continues across calls (chunks).
function receipt_v2_stream_cipher($encryptionKey, $iv)
{
$cipher = receipt_v2_cipher($encryptionKey, $iv);
$cipher->disablePadding();
$cipher->enableContinuousBuffer();
return $cipher;
}
// Byte strings compared in constant time.
function receipt_equals($known, $given)
{
return hash_equals($known, $given);
}
/*
* The bytes of files/<number>.v2.aes: "PGR2", a 16-byte IV (random unless
* given, for test vectors), the AES-256-CBC ciphertext with PKCS#7 padding,
* and the HMAC-SHA256 of everything before it (encrypt-then-MAC).
* receipt_v2_encrypt_stream() writes the same bytes in chunks.
*/
function receipt_v2_encrypt($key, $number, $data, $iv = null)
{
$iv ??= \phpseclib\Crypt\Random::string(16);
[$encryptionKey, $macKey] = receipt_v2_keys($key, $number);
$ciphertext = receipt_v2_cipher($encryptionKey, $iv)->encrypt($data);
$mac = hash_init('sha256', HASH_HMAC, $macKey);
hash_update($mac, 'PGR2' . $iv);
hash_update($mac, $ciphertext);
return 'PGR2' . $iv . $ciphertext . hash_final($mac, true);
}
// The plain text of <number>.v2.aes bytes, or false: the MAC is checked first,
// so a wrong key, another file number or a modified file is rejected.
function receipt_v2_decrypt($key, $number, $file)
{
if (!is_string($file) || strlen($file) < 68 || !str_starts_with($file, 'PGR2')) {
return false;
}
[$encryptionKey, $macKey] = receipt_v2_keys($key, $number);
$ciphertext = substr($file, 20, -32);
$mac = hash_init('sha256', HASH_HMAC, $macKey);
hash_update($mac, substr($file, 0, 20));
hash_update($mac, $ciphertext);
if (!receipt_equals(hash_final($mac, true), substr($file, -32))) {
return false;
}
return receipt_v2_cipher($encryptionKey, substr($file, 4, 16))->decrypt($ciphertext);
}
// Encrypts stream $in to stream $out as <number>.v2.aes bytes, 64 KiB at a
// time, so memory does not grow with the receipt. Short reads are fine; a
// read that returns nothing before the end, or a failed write, returns false.
function receipt_v2_encrypt_stream($key, $number, $in, $out, $iv = null)
{
$iv ??= \phpseclib\Crypt\Random::string(16);
[$encryptionKey, $macKey] = receipt_v2_keys($key, $number);
$cipher = receipt_v2_stream_cipher($encryptionKey, $iv);
$mac = hash_init('sha256', HASH_HMAC, $macKey);
hash_update($mac, 'PGR2' . $iv);
$written = fwrite($out, 'PGR2' . $iv) === 20;
$pending = '';
while (true) {
$chunk = fread($in, 65536);
if (feof($in)) {
$pending .= (string) $chunk;
break;
}
if ((string) $chunk === '') {
return false;
}
$pending .= $chunk;
// Whole blocks now; the rest (and the padding) with the last chunk. An
// empty encrypt() would reset phpseclib's continuous IV.
$whole = strlen($pending) - strlen($pending) % 16;
if ($whole === 0) {
continue;
}
$ciphertext = $cipher->encrypt(substr($pending, 0, $whole));
$pending = (string) substr($pending, $whole);
hash_update($mac, $ciphertext);
$written = fwrite($out, $ciphertext) === strlen($ciphertext) && $written;
}
$padding = 16 - strlen($pending) % 16;
$ciphertext = $cipher->encrypt($pending . str_repeat(chr($padding), $padding));
hash_update($mac, $ciphertext);
$written = fwrite($out, $ciphertext) === strlen($ciphertext) && $written;
return fwrite($out, hash_final($mac, true)) === 32 && $written;
}
// A temporary file name next to $path, unique per process and call.
function receipt_temp_path($path)
{
return $path . '.tmp-' . getmypid() . '-' . str_replace('.', '', uniqid('', true));
}
// Writes a temporary file next to $path and renames it over $path, so readers
// never see a partly written file. $prepare (optional) is called with the
// temporary file's path before the rename and must return true. Only the
// temporary file is ever removed.
function receipt_write_file($path, $bytes, $prepare = null)
{
$temp = receipt_temp_path($path);
if (@file_put_contents($temp, $bytes) === strlen($bytes) && ($prepare === null || $prepare($temp)) && @rename($temp, $path)) {
return true;
}
@unlink($temp);
return false;
}
// Stores the uploaded file $source as <number>.v2.aes, encrypted in chunks
// and renamed into place. Fails closed: without a valid configured key
// nothing is written.
function receipt_store($directory, $number, $source)
{
$receiptKey = receipt_configured_key();
if ($receiptKey === '') {
error_log('pgacc: receipt not stored: no valid receipt key ($key) configured');
return false;
}
$path = receipt_v2_path($directory, $number);
$temp = receipt_temp_path($path);
$in = @fopen($source, 'rb');
$out = $in === false ? false : @fopen($temp, 'xb');
$stored = $out !== false && receipt_v2_encrypt_stream($receiptKey, $number, $in, $out);
if ($in !== false) {