-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.php
951 lines (936 loc) · 37.9 KB
/
index.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
<?php
if(!file_exists('config.php')) {
include('resources/install.php');
include('templates/header.php');
include('templates/emptynav.html');
if(isset($_POST['submit'])) {
$filled = array('db_type_mysql' => 'true', 'db_host' => $_POST['db_host'], 'db_username' => $_POST['db_username'], 'db_password' => $_POST['db_password'], 'db_name' => $_POST['db_name'], 'app_email' => $_POST['app_email'], 'admin_email' => $_POST['admin_email'], 'user_email' => $_POST['user_email'], 'user_name' => $_POST['user_name'], 'user_iban' => $_POST['user_iban'], 'user_password' => $_POST['user_password'], 'base_url' => $_POST['base_url'], 'currency' => $_POST['currency'], 'days' => $_POST['days'], 'cron' => true);
$db_type = 'mysql';
if($_POST['db_type'] === 'pgsql') {
$db_type = 'pgsql';
$filled['db_type_mysql'] = false;
}
$cron_type = 'cron';
if($_POST['cron_type'] === 'webcron') {
$cron_type = 'webcron';
$filled['cron'] = false;
}
if(!filter_var($_POST['app_email'], FILTER_VALIDATE_EMAIL)) {
$error = 'You need to enter a valid application email address';
$filled['app_email'] = '';
}
elseif(!filter_var($_POST['admin_email'], FILTER_VALIDATE_EMAIL)) {
$error = 'You need to enter a valid admin email address';
$filled['admin_email'] = '';
}
elseif(!filter_var($_POST['user_email'], FILTER_VALIDATE_EMAIL)) {
$error = 'You need to enter a valid account email address';
$filled['user_email'] = '';
}
elseif(strlen($_POST['user_name']) < 5) {
$error = 'The full name for your account seems oddly short, are you quite sure you didn\'t misspell it?';
$filled['user_name'] = '';
}
elseif(strlen($_POST['user_iban']) < 12) {
$error = 'You need to enter a valid IBAN for your account';
$filled['user_iban'] = '';
}
elseif(strlen($_POST['user_password']) < 8) {
$error = 'Let\'s be realistic, a proper password isn\'t that short';
$filled['user_password'] = '';
}
elseif(!filter_var($_POST['base_url'], FILTER_VALIDATE_URL)) {
$error = 'Your base URL is not a valid URL';
$filled['base_url'] = '';
}
elseif(!in_array($_POST['currency'], $currencies)) {
$error = 'Specify a currency from the dropdown or file a GitHub issue if yours is missing';
$filled['currency'] = '';
}
elseif(intval($_POST['days']) < 1 OR intval($_POST['days']) > 30) {
$error = 'Please enter a valid and realistic number of days between reminders';
$filled['days'] = '';
}
else {
if($db_type === 'pgsql') {
try {
$db = new PDO('pgsql:host=' . $_POST['db_host'] . ';dbname=' . $_POST['db_name'], $_POST['db_username'], $_POST['db_password']);
$dsn = 'pgsql:host=' . $_POST['db_host'] . ';dbname=' . $_POST['db_name'];
} catch (PDOException $e) {
$error = 'There was a problem connecting to the database: ' . $e;
}
}
else {
try {
$db = new PDO('mysql:host=' . $_POST['db_host'] . ';dbname=' . $_POST['db_name'], $_POST['db_username'], $_POST['db_password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$dsn = 'mysql:host=' . $_POST['db_host'] . ';dbname=' . $_POST['db_name'];
} catch (PDOException $e) {
$error = 'There was a problem connecting to the database: ' . $e;
}
}
}
if(!isset($error)) {
if(!create_db($db_type)) {
$error = 'Could not create database structures, please make sure you have the correct rights and permissions.';
}
elseif(!create_first_user($_POST['user_email'], $_POST['user_name'], $_POST['user_password'], $_POST['user_iban'])) {
$error = 'Could not create first user account. This is quite abnormal considering database structure creation did not error.';
}
}
if(isset($error)) {
include('templates/error.php');
include('templates/form_install.php');
}
else {
$config = create_config($dsn, $_POST['db_username'], $_POST['db_password'], $_POST['app_email'], $_POST['admin_email'], $_POST['base_url'], $_POST['currency'], $_POST['days'], $cron_type);
if($config != 'created') {
$success = 'Installation completed successfully, but the installer could not write your configuration to config.php. Please follow the instructions below to finish installation.';
include('templates/success.php');
$title = 'Create a config.php file in the Tabby top directory with the following contents';
include('templates/box_config.php');
}
else {
$success = 'Installation completed successfully. You can now start using your installation of Tabby.';
include('templates/success.php');
}
if($cron_type === 'cron') {
$title = 'Don\'t forget to install a cronjob. Below is an example for crontab. You can also add a script to /etc/cron.daily or create a systemd timer.';
$config = '0 5 * * * php ' . escapeshellarg(realpath('cron.php'));
include('templates/box_config.php');
}
$check_install = check_install($_POST['base_url']);
if($check_install !== 'OK') {
$title = 'By default, Tabby uses a .htaccess file with mod_rewrite to support semantic URLs and hide the changelog.txt file';
$config = '';
if($check_install === 'doublefail' OR $check_install === 'regfail'){
$config .= 'It seems visits to semantic URLs aren\'t correctly mapped to index.php. If you are running Apache, please verify that mod_rewrite is enabled and either allow .htaccess files ("AllowOverride All" on the Directory context, "AccessFileName .htaccess" globally) or appropriately move the contents of .htaccess to a VirtualHost or Directory context within your configuration. If you are using nginx, add a fallback to index.php in the appropriate try_files statement. Instructions on how to fall back to index.php are easily available online for other webserver software.';
}
if($check_install === 'doublefail') {
$config .= "\n\n" . 'It also seems changelog.txt is publicly available. While this isn\'t a problem in and by itself, the changelog clearly indicates which version of Tabby you are running. If any security issue would turn up in the future, a possible attacker could deduce whether you are vulnerable or not based on the changelog when it\'s available. To prevent this, changelog.txt is redirected to index.php. This also means that you can followed identical instructions for Apache. If .htaccess works correctly or its contents are moved to configuration in the right context, both issues should be resolved. If you are using nginx, a rewrite or return can be used prior to try_files to move visits for changelog.txt to the Tabby base URL. Instructions on how to redirect a specific file are easily available online for other webserver software.';
}
elseif($check_install === 'changelogfail') {
$config .= 'It seems changelog.txt is publicly available. While this isn\'t a problem in and by itself, the changelog clearly indicates which version of Tabby you are running. If any security issue would turn up in the future, a possible attacker could deduce whether you are vulnerable or not based on the changelog when it\'s available. To prevent this, changelog.txt is redirected to index.php. If you are running Apache, it is quite unusual that the mod_rewrite rules are working correctly for semantic URLs but not to protect changelog.txt. If you have made changes to .htaccess or to your Apache configuration, then please check these in further detail. If you are using nginx, a rewrite or return can be used prior to try_files to move visits for changelog.txt to the Tabby base URL. Instructions on how to redirect a specific file are easily available online for other webserver software.';
}
include('templates/box_config.php');
}
}
}
else {
$base_url = 'http://';
if(isset($_SERVER['HTTPS']) AND $_SERVER['HTTPS'] === 'on') {
$base_url = 'https://';
}
$base_url .= $_SERVER['HTTP_HOST'];
// This is the simplest way to strip index.php?something#something from the current request URI
// see https://stackoverflow.com/questions/6283071/in-php-is-there-a-simple-way-to-get-the-directory-part-of-a-uri
$base_url .= preg_replace('{/[^/]+$}','/', $_SERVER['REQUEST_URI']);
$filled = array('db_type_mysql' => true, 'db_host' => 'localhost', 'db_username' => '', 'db_password' => '', 'db_name' => '', 'app_email' => 'no-reply@' . $_SERVER['HTTP_HOST'], 'admin_email' => '', 'user_email' => '', 'user_name' => '', 'user_iban' => '', 'user_password' => '', 'base_url' => $base_url, 'currency' => '', 'days' => 5, 'cron' => true);
include('templates/form_install.php');
}
include('templates/footer.php');
exit;
}
include('config.php');
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, parse_url($base_url, PHP_URL_PATH)) === 0) {
$local_uri = substr($uri, strlen(parse_url($base_url, PHP_URL_PATH)));
}
$local_uri = strtok($local_uri, '?');
$location = rtrim($local_uri, '/');
include('resources/init.php');
include('templates/header.php');
include('resources/users.php');
if($location == 'logout') {
include('templates/emptynav.html');
unset($_SESSION['tabby_loggedin']);
$success = 'You\'ve been signed out.';
include('templates/success.php');
}
elseif(isset($_SESSION['tabby_loggedin'])) {
include('templates/nav.html');
include('resources/uiblocks.php');
include('resources/transactions.php');
include('resources/people.php');
include('resources/activities.php');
include('resources/recurring.php');
if(substr($location, 0, 13) == 'adminconfirm/') {
$confirmation = explode('adminconfirm/', $location);
$confirmation = $confirmation[1];
if(isset($_POST['deny'])) {
delete_pending_user($confirmation);
$success = 'This pending user has been deleted.';
include('templates/success.php');
}
elseif(isset($_POST['approve'])) {
if(user_admin_confirm($confirmation)) {
$success = 'This user has been contacted his account account has been approved';
include('templates/success.php');
}
else {
$error = 'Could not approve that user for some reason.';
include('templates/error.php');
}
}
else {
$pending = get_pending_user_from_confirmation($confirmation);
include('templates/adminconfirm.php');
}
}
elseif(substr($location, 0, 13) == 'aliasconfirm/') {
$split = explode('/', $location);
confirm_user_alias($split[1]);
$success = 'If this confirmation URL was valid, the alias has been confirmed.';
include('templates/success.php');
}
elseif(substr($location, 0, 6) == 'token/') {
$success = 'It seems you\'re logged in, redirecting you to your debt overview';
include('templates/success.php');
}
elseif($location == 'recurring/add') {
$debtors = get_debtors();
if(isset($_POST['add'])) {
if(strlen($_POST['name']) < 2) {
$error = 'Your recurring expense needs a name.';
include('templates/error.php');
$filled = array('name' => '', 'amount' => $_POST['amount'], 'frequency' => (isset($_POST['frequency'])) ? $_POST['frequency'] : '', 'frequency_days' => $_POST['frequency_days'], 'date' => $_POST['date']);
include('templates/form_recurring.php');
}
elseif(strlen($_POST['amount']) < 1) {
$error = 'Your recurring expense needs an associated cost.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'amount' => '', 'frequency' => (isset($_POST['frequency'])) ? $_POST['frequency'] : '', 'frequency_days' => $_POST['frequency_days'], 'date' => $_POST['date']);
include('templates/form_recurring.php');
}
elseif(!isset($_POST['frequency'])) {
$error = 'Your should indicate how frequent this expense should be applied.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'amount' => $_POST['amount'], 'frequency' => '', 'frequency_days' => $_POST['frequency_days'], 'date' => $_POST['date']);
include('templates/form_recurring.php');
}
elseif($_POST['frequency'] == 'days' AND intval($_POST['frequency_days']) < 1) {
$error = 'You should enter a number of days larger than 0 if the frequency of your recurring expense is a custom amount of days.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'amount' => $_POST['amount'], 'frequency' => 'days', 'frequency_days' => '', 'date' => $_POST['date']);
include('templates/form_recurring.php');
}
elseif($_POST['frequency'] != 'days' AND intval($_POST['frequency_days']) > 0) {
$error = 'You cannot select weekly, monthly or yearly frequency and then still specify a custom amount of days. Either empty the days field or select the custom amount of days frequency.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'amount' => $_POST['amount'], 'frequency' => (isset($_POST['frequency'])) ? $_POST['frequency'] : '', 'frequency_days' => $_POST['frequency_days'], 'date' => $_POST['date']);
include('templates/form_recurring.php');
}
elseif(!strtotime($_POST['date'])) {
$error = 'Your recurring expense needs a date.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'amount' => $_POST['amount'], 'frequency' => (isset($_POST['frequency'])) ? $_POST['frequency'] : '', 'frequency_days' => $_POST['frequency_days'], 'date' => '');
include('templates/form_recurring.php');
}
elseif(strtotime($_POST['date']) < strtotime(date('d-m-Y'))) {
$error = 'Your recurring expense needs a date.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'amount' => $_POST['amount'], 'frequency' => (isset($_POST['frequency'])) ? $_POST['frequency'] : '', 'frequency_days' => $_POST['frequency_days'], 'date' => '');
include('templates/form_recurring.php');
}
elseif(check_debtor($_POST['debtor'][0])) {
$error = 'Your recurring expense needs at least one contact.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'amount' => $_POST['amount'], 'frequency' => (isset($_POST['frequency'])) ? $_POST['frequency'] : '', 'frequency_days' => $_POST['frequency_days'], 'date' => $_POST['date']);
include('templates/form_recurring.php');
}
else {
$amount = str_replace(',', '.', $_POST['amount']) * 100;
$frequency = frequency_to_dateintervalstring($_POST['frequency'], $_POST['frequency_days']);
$debtors = array();
foreach($_POST['debtor'] as $debtor) {
if(!check_debtor($debtor)) {
$debtor_details = get_debtor_details($debtor);
$debtors[] = $debtor_details['id'];
}
}
$recurid = add_recurring($_POST['name'], $amount, $_POST['date'], $frequency, $debtors);
if($_POST['date'] == date('Y-m-d')) {
execute_recurring($recurid);
}
$success = 'Your new recurring expense has been added.';
$redirect = 'recurring';
include('templates/success.php');
}
}
elseif($debtors == array()) {
$error = 'You don\'t have any contacts. You should fix that first before trying to register debt.';
include('templates/error.php');
}
else {
$filled = array('name' => '', 'amount' => '', 'frequency' => '', 'frequency_days' => '', 'date' => '');
include('templates/form_recurring.php');
}
}
elseif($location == 'activities/add') {
$debtors = get_debtors();
if(isset($_POST['add'])) {
if(strlen($_POST['name']) < 2) {
$error = 'Your activity needs a name.';
include('templates/error.php');
$filled = array('name' => '', 'date' => $_POST['date']);
include('templates/form_activity.php');
}
elseif(!strtotime($_POST['date'])) {
$error = 'Your activity needs a date.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'date' => '');
include('templates/form_activity.php');
}
elseif(check_debtor($_POST['debtor'][0]) OR strlen($_POST['comment'][0]) < 2 OR strlen($_POST['amount'][0]) < 1) {
$error = 'Your activity should contain at least one contact with a comment and an amount of debt specified.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'date' => $_POST['date']);
include('templates/form_activity.php');
}
else {
$actid = add_activity($_POST['name'], $_POST['date']);
for($i=0;;$i++) {
if(!isset($_POST['comment'][$i]) OR check_debtor($_POST['debtor'][$i]) OR strlen($_POST['comment'][$i]) < 2 OR strlen($_POST['amount'][$i]) < 1) {
break;
}
$debtor = get_debtor_details($_POST['debtor'][$i]);
$user = get_user_details();
$amount = str_replace(',', '.', $_POST['amount'][$i]) * 100;
add_debt($actid, $debtor['id'], $_POST['comment'][$i], $amount);
$finstate = get_debtor_financial_state($debtor['email']);
if(isset($_POST['sendmail']) AND $_POST['sendmail'] == 1) {
$token = get_debtor_token($debtor['email']);
if($token === FALSE) {
$token = create_debtor_token($debtor['email']);
}
email_new_debt($debtor, $user, $_POST['name'], date('d M Y', strtotime($_POST['date'])), $_POST['comment'][$i], $amount, $finstate['total'], $token);
}
}
$success = 'Your new activity has been added.';
$redirect = 'people';
include('templates/success.php');
}
}
elseif($debtors == array()) {
$error = 'You don\'t have any contacts. You should fix that first before trying to register debt.';
include('templates/error.php');
}
else {
$filled = array('name' => '', 'date' => '');
include('templates/form_activity.php');
}
}
elseif($location == 'people/add') {
if(isset($_POST['add'])) {
if(strlen($_POST['name']) < 2) {
$error = 'Your contact needs a name.';
include('templates/error.php');
$filled = array('name' => '', 'email' => $_POST['email']);
include('templates/form_people.php');
}
elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error = 'That\'s not a real email address!';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'email' => '');
include('templates/form_people.php');
}
elseif(!check_debtor($_POST['email'])) {
$error = 'Another contact of yours already uses that email address.';
include('templates/error.php');
$filled = array('name' => $_POST['name'], 'email' => '');
include('templates/form_people.php');
}
else {
add_debtor($_POST['name'], $_POST['email']);
$success = 'Your new contact has been added.';
$redirect = 'people';
include('templates/success.php');
}
}
else {
$filled = array('name' => '', 'email' => '');
include('templates/form_people.php');
}
}
elseif(substr($location, 0, 18) == 'activities/detail/') {
if(isset($_GET['del'])) {
if(del_debt($_GET['del'])) {
$success = 'Debt deleted.';
include('templates/success.php');
}
else {
$error = 'Sure you haven\'t deleted that already?';
include('templates/error.php');
}
}
$actid = explode('activities/detail/', $location);
$actid = $actid[1];
title('Detailed view');
detailcard(get_activity_transactions($actid), 'activities');
}
elseif(substr($location, 0, 17) == 'recurring/delete/') {
if(isset($_POST['delete'])) {
$recurringid = explode('recurring/delete/', $location);
$recurringid = $recurringid[1];
if(del_recurring($recurringid)) {
$success = 'Your recurring expense has been deleted.';
$redirect = 'recurring';
include('templates/success.php');
}
else {
$error = 'Sure you haven\'t deleted that already?';
include('templates/error.php');
}
}
else {
$what = 'recurring expense';
$warning = 'This will not delete past occurences of this recurring expense, but remove all future runs.';
$backlink = 'recurring';
include('templates/confirm_delete.php');
}
}
elseif(substr($location, 0, 18) == 'activities/delete/') {
//confirm before delete, only delete activity because cascade
if(isset($_POST['delete'])) {
$actid = explode('activities/delete/', $location);
$actid = $actid[1];
if(del_activity($actid)) {
$success = 'Your activity has been deleted.';
$redirect = 'activities';
include('templates/success.php');
}
else {
$error = 'Sure you haven\'t deleted that already?';
include('templates/error.php');
}
}
else {
$what = 'activity';
$warning = 'This will also delete any debt connected to this activity.';
$backlink = 'activities';
include('templates/confirm_delete.php');
}
}
elseif(substr($location, 0, 14) == 'people/detail/') {
if(isset($_GET['del'])) {
if(substr($_GET['del'], 0, 1) == 'c') {
if(del_credit(ltrim($_GET['del'], 'c'))) {
$success = 'Credit deleted.';
include('templates/success.php');
}
else {
$error = 'Sure you haven\'t deleted that already?';
include('templates/error.php');
}
}
elseif(substr($_GET['del'], 0, 1) == 'd') {
if(del_debt(ltrim($_GET['del'], 'd'))) {
$success = 'Debt deleted.';
include('templates/success.php');
}
else {
$error = 'Sure you haven\'t deleted that already?';
include('templates/error.php');
}
}
}
$debtormail = explode('people/detail/', $location);
$debtormail = $debtormail[1];
title('Detailed view');
detailcard(get_debtor_transactions($debtormail), 'people');
}
elseif(substr($location, 0, 13) == 'mydebt/alias/') {
$aliaspart = explode('mydebt/alias/', $location);
$alias_with_usermail = explode('/detail/', $aliaspart[1]);
$alias = $alias_with_usermail[0];
$usermail = $alias_with_usermail[1];
title('Detailed view');
if(!in_array($alias, get_confirmed_aliases())) {
$error = 'That alias in your URL doesn\'t look right.';
include('templates/error.php');
}
else {
detailcard(get_user_transactions_for_debtor($usermail, $alias), 'user');
}
}
elseif(substr($location, 0, 14) == 'mydebt/detail/') {
$usermail = explode('mydebt/detail/', $location);
$usermail = $usermail[1];
title('Detailed view');
include('templates/button_merge.html');
detailcard(get_user_transactions_for_debtor($usermail, $_SESSION['tabby_loggedin']), 'user');
}
elseif($location == 'people/list') {
$debtors = get_debtors();
include('templates/table_people.php');
}
elseif(substr($location, 0, 18) == 'people/list/delete') {
if(isset($_POST['delete'])) {
$debtormail = explode('people/list/delete/', $location);
$debtormail = $debtormail[1];
if(delete_debtor($debtormail)) {
$success = 'Your contact has been deleted.';
$redirect = 'people/list';
include('templates/success.php');
}
else {
$error = 'This contact can\'t be deleted. Are you sure they have no debt or credit?';
include('templates/error.php');
}
}
else {
$what = 'contact';
$warning = 'This is only possible if your contact has no debt or credit. Make sure to delete that first!';
$backlink = 'people/list';
include('templates/confirm_delete.php');
}
}
elseif(substr($location, 0, 16) == 'people/list/edit') {
$debtormail = explode('people/list/edit/', $location);
$debtormail = $debtormail[1];
$debtor = get_debtor_details($debtormail);
if(isset($_POST['edit'])) {
if(strlen($_POST['name']) < 2) {
$error = 'Your contact needs a name.';
include('templates/error.php');
}
elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error = 'That\'s not a real email address!';
include('templates/error.php');
}
elseif($_POST['edit'] !== $_POST['email'] AND !check_debtor($_POST['email'])) {
$error = 'Another contact of yours already uses that email address.';
include('templates/error.php');
}
else {
update_debtor($_POST['edit'], $_POST['name'], $_POST['email']);
$success = 'Your contact has been updated.';
$redirect = 'people/list';
include('templates/success.php');
$debtor = get_debtor_details($_POST['email']);
}
}
include('templates/form_people_edit.php');
}
elseif(substr($location, 0, 15) == 'recurring/edit/') {
$recurringid = explode('recurring/edit/', $location);
$recurringid = $recurringid[1];
if(isset($_POST['edit'])) {
if(strlen($_POST['name']) < 2) {
$error = 'Your recurring expense needs a name.';
include('templates/error.php');
}
elseif(strlen($_POST['amount']) < 1) {
$error = 'Your recurring expense needs an associated cost.';
include('templates/error.php');
}
elseif(check_any_debtors($_POST['debtor'])) {
$error = 'Your recurring expense needs at least one contact.';
include('templates/error.php');
}
else {
$amount = str_replace(',', '.', $_POST['amount']) * 100;
$debtors = array();
foreach($_POST['debtor'] as $debtor) {
if(!check_debtor($debtor)) {
$debtor_details = get_debtor_details($debtor);
$debtors[] = $debtor_details['id'];
}
}
update_recurring($recurringid, $_POST['name'], $amount, $debtors);
$success = 'Your recurring expense has been updated.';
$redirect = 'recurring';
include('templates/success.php');
}
}
$filled = get_recurring($recurringid);
$debtors = get_debtors();
include('templates/form_recurring_edit.php');
}
elseif($location == 'remind') {
$user = get_user_details();
if(isset($_POST['submit'])) {
$finstate = get_all_debtor_financial_state();
if($_POST['debtor'] == 'TABBY_REMIND_EVERYONE') {
$error = 'No one has any debt with you.';
if(strlen($_POST['comment']) > 3) {
$comment = $_POST['comment'];
}
else {
$comment = NULL;
}
foreach($finstate as $debtor) {
if($debtor['total'] < 0) {
unset($error);
$token = get_debtor_token($debtor['email']);
if($token === FALSE) {
$token = create_debtor_token($debtor['email']);
}
email_reminder($debtor['email'], $debtor['total'], $comment, $token, $user);
}
}
if(isset($error)) {
include('templates/error.php');
}
else {
$success = 'We\'ve sent your reminder(s).';
include('templates/success.php');
$user = get_user_details();
}
}
else {
$error = 'Weird, I don\'t know about that person.';
foreach($finstate as $debtor) {
if($debtor['email'] == $_POST['debtor']) {
if($debtor['total'] < 0) {
unset($error);
$token = get_debtor_token($debtor['email']);
if($token === FALSE) {
$token = create_debtor_token($debtor['email']);
}
email_reminder($debtor['email'], $debtor['total'], $comment, $token, $user);
$success = 'We\'ve sent your reminder.';
include('templates/success.php');
$user = get_user_details();
break;
}
else {
$error = 'That person doens\'t have any debt with you right now. No reminder was sent.';
}
}
}
if(isset($error)) {
include('templates/error.php');
}
}
}
$debtors = get_debtors();
include('templates/form_remind.php');
}
elseif($location == 'profile') {
$user = get_user_details();
if(isset($_POST['submit'])) {
if(strlen($_POST['name']) < 5) {
$error = 'Your name seems oddly short, are you quite sure you didn\'t misspell it?';
}
elseif(strlen($_POST['iban']) < 12) {
$error = 'An IBAN is for sure longer than that. Or do you prefer that people just won\'t pay?';
}
elseif(strlen($_POST['password']) < 8) {
$error = 'Let\'s be realistic, a proper password isn\'t that short.';
}
elseif($_POST['name'] === $user['name'] AND $_POST['iban'] === $user['iban'] AND $_POST['password'] === 'TABBY_DEFAULT_VALUE') {
$error = 'You haven\'t changed anything.';
}
else {
update_user($_SESSION['tabby_loggedin'], $_POST['name'], $_POST['iban']);
if($_POST['password'] !== 'TABBY_DEFAULT_VALUE') {
change_password($_SESSION['tabby_loggedin'], $_POST['password']);
}
$success = 'Your profile has been updated.';
include('templates/success.php');
$user = get_user_details();
}
if(isset($error)) {
include('templates/error.php');
}
}
elseif(isset($_POST['add_alias'])) {
if(!filter_var($_POST['alias'], FILTER_VALIDATE_EMAIL)) {
$error = 'That\'s not a real email address!';
include('templates/error.php');
}
elseif(!validate_user_alias($_POST['alias'])) {
$error = 'That email address is already in use within Tabby somewhere.';
include('templates/error.php');
}
else {
add_user_alias($_POST['alias']);
$success = 'You new alias has been added. Don\'t forget to confirm it by clicking the link in the confirmation email.';
include('templates/success.php');
}
}
$filled = array('email' => $user['email'], 'name' => $user['name'], 'iban' => $user['iban'], 'aliases' => get_user_aliases());
include('templates/form_profile.php');
}
elseif($location == 'mydebt') {
if(empty(get_transactions_per_user($_SESSION['tabby_loggedin']))) {
$success = 'No one has any debt for you.';
include('templates/success.php');
}
else {
title('Your debt with other users');
include('templates/button_merge.html');
carddeck(get_transactions_per_user($_SESSION['tabby_loggedin']), 'user');
}
$aliases = get_confirmed_aliases();
if(!empty($aliases)) {
subtitle('Debt with other users based on your aliases');
$all_empty = true;
foreach($aliases as $alias) {
if(!empty(get_transactions_per_user($alias))) {
$all_empty = false;
subsubtitle('Debt for alias ' . $alias);
carddeck(get_transactions_per_user($alias), 'user', 'mydebt/alias/' . $alias);
}
}
if($all_empty) {
$success = 'No debt available for any of your aliases.';
include('templates/success.php');
}
}
}
elseif($location == 'merge') {
if(isset($_POST['merge'])) {
if(check_debtor($_POST['merge'])) {
$error = 'Nice try, but that\'s not going to work.';
include('templates/error.php');
}
else {
$debt_with_loggedin = get_user_transactions_for_debtor($_SESSION['tabby_loggedin'], $_POST['merge']);
$debt_with_other = get_user_transactions_for_debtor($_POST['merge'], $_SESSION['tabby_loggedin']);
if($debt_with_loggedin['total'] < 0 AND $debt_with_other['total'] < 0) {
$amount = -max($debt_with_loggedin['total'], $debt_with_other['total']);
$debtor = get_debtor_details($_SESSION['tabby_loggedin'], $_POST['merge']);
add_credit($debtor['id'], $_POST['mergemessage'], $amount, date('Y-m-d'));
$debtor = get_debtor_details($_POST['merge']);
add_credit($debtor['id'], $_POST['mergemessage'], $amount, date('Y-m-d'));
$success = 'The debt with ' . $debtor['name'] . '(' . $debtor['email'] . ') has been merged.';
include('templates/success.php');
}
else {
$error = 'You have no mutual debt with this contact or the mutual debt has already been processed.';
include('templates/error.php');
}
}
}
$mergeable = array();
foreach(get_transactions_per_user($_SESSION['tabby_loggedin']) as $otheruser => $debt_with_other) {
if(!isset($debt_with_other['total']) OR $debt_with_other['total'] >= 0) {
// other user has no debt for currently logged in user
continue;
}
$debt_with_loggedin = get_user_transactions_for_debtor($_SESSION['tabby_loggedin'], $otheruser);
if($debt_with_loggedin['total'] < 0) {
$debt_after_merge = 'You';
if($debt_with_loggedin['total'] < $debt_with_other['total']) {
$debt_after_merge = $debt_with_other['name'];
}
elseif($debt_with_loggedin['total'] == $debt_with_other['total']) {
$debt_after_merge = 'No one';
}
$mergeable[$otheruser] = array('name' => $debt_with_other['name'], 'other_debt' => $debt_with_other['total'], 'loggedin_debt' => $debt_with_loggedin['total'], 'debt_after_merge' => $debt_after_merge);
}
}
if(!empty($mergeable)) {
include('templates/table_merge.php');
}
else {
$aliases = get_confirmed_aliases();
if(!empty($aliases)) {
$success = 'You have no mergeable debt. This means that anyone you owe doesn\'t simultaneously owe you on this instance of Tabby (excluding any aliases, since those aren\'t visible to others).';
}
else {
$success = 'You have no mergeable debt. This means that anyone you owe doesn\'t simultaneously owe you on this instance of Tabby.';
}
include('templates/success.php');
}
}
elseif($location == 'recurring') {
$recurring = get_user_recurring();
include('templates/table_recurring.php');
}
elseif($location == 'activities') {
if(isset($_POST['debt'])) {
if(check_debtor($_POST['debtor']) OR strlen($_POST['comment']) < 2 OR strlen($_POST['amount']) < 1) {
$error = 'Please select a contact, fill in a comment and enter an amount to add debt.';
include('templates/error.php');
}
else {
$debtor = get_debtor_details($_POST['debtor']);
$amount = str_replace(',', '.', $_POST['amount']) * 100;
$act = get_activity($_POST['debt']);
if(!$act) {
$error = 'Stop playing';
include('templates/error.php');
}
else {
add_debt($act['id'], $debtor['id'], $_POST['comment'], $amount);
$finstate = get_debtor_financial_state($debtor['email']);
if(isset($_POST['sendmail'])) {
$user = get_user_details();
$token = get_debtor_token($debtor['email']);
if($token === FALSE) {
$token = create_debtor_token($debtor['email']);
}
email_new_debt($debtor, $user, $act['name'], date('d M Y', strtotime($act['date'])), $_POST['comment'], $amount, $finstate['total'], $token);
}
$success = 'Debt added.';
include('templates/success.php');
}
}
}
title('Overview of your activities');
include('templates/buttons.html');
$debtors = get_debtors();
carddeck(get_transactions_per_activity(), 'activities', $debtors);
}
else { // $location == 'people' fallback
title('Overview of your contacts');
include('templates/buttons.html');
if(isset($_POST['credit'])) {
if(!strtotime($_POST['date'])) {
$error = 'Your credit needs a date.';
include('templates/error.php');
}
elseif(strlen($_POST['comment']) < 2) {
$error = 'Your credit needs a comment.';
include('templates/error.php');
}
elseif(strlen($_POST['amount']) < 1) {
$error = 'We need to know how much money you\'ve received.';
include('templates/error.php');
}
elseif(check_debtor($_POST['credit'])) {
$error = 'Nice try, but that\'s not going to work.';
include('templates/error.php');
}
else {
$debtor = get_debtor_details($_POST['credit']);
add_credit($debtor['id'], $_POST['comment'], str_replace(',', '.', $_POST['amount']) * 100, $_POST['date']);
$success = 'Credit has been added for '. $debtor['email'] . '.';
include('templates/success.php');
}
}
carddeck(get_transactions_per_debtor(), 'people');
}
}
elseif($location == 'login') {
include('templates/emptynav.html');
if(isset($_POST['password'])) {
if(check_credentials($_POST['email'], $_POST['password'])) {
$_SESSION['tabby_loggedin'] = $_POST['email'];
$success = 'You\'ve been logged in.';
$redirect = 'people';
include('templates/success.php');
}
else {
$error = 'Incorrect login credentials.';
include('templates/error.php');
include('templates/login.html');
}
}
else {
include('templates/login.html');
}
}
elseif($location == 'register') {
include('templates/emptynav.html');
if(isset($_POST['register'])) {
if(strlen($_POST['name']) < 5) {
$error = 'Your name seems oddly short, are you quite sure you didn\'t misspell it?';
}
elseif(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error = 'That\'s not a real email address!';
}
elseif(strlen($_POST['iban']) < 12) {
$error = 'An IBAN is for sure longer than that. Or do you prefer that people just won\'t pay?';
}
elseif(strlen($_POST['password']) < 8) {
$error = 'Let\'s be realistic, a proper password isn\'t that short.';
}
elseif(user_exists($_POST['email'])) {
$error = 'It seems we already know about you, that\'s weird!';
}
else {
register_user($_POST['email'], $_POST['name'], $_POST['password'], $_POST['iban']);
$success = 'Your registration has been recorded. You should receive an email confirmation soon.';
include('templates/success.php');
}
if(isset($error)) {
include('templates/error.php');
include('templates/register.html');
}
}
else {
include('templates/register.html');
}
}
elseif(substr($location, 0, 8) == 'confirm/') {
include('templates/emptynav.html');
$split = explode('/', $location);
if(user_email_confirm($split[1])) {
$success = 'Your confirmation has been processed and the owner of this Tabby instance has been notified. It is now up to the owner to fully approve your account.';
include('templates/success.php');
}
else {
$error = 'Never heard of that confirmation code.';
include('templates/error.php');
}
}
elseif(substr($location, 0, 13) == 'aliasconfirm/') {
include('templates/emptynav.html');
$split = explode('/', $location);
confirm_user_alias($split[1]);
$success = 'If this confirmation URL was valid, the alias has been confirmed.';
include('templates/success.php');
}
elseif(substr($location, 0, 13) == 'adminconfirm/') {
include('templates/emptynav.html');
$error = 'Revisit this link when logged in.';
include('templates/error.php');
}
elseif($location == 'requesttoken') {
include('templates/emptynav.html');
include('resources/people.php');
if(isset($_POST['email'])) {
$token = get_debtor_token($_POST['email']);
if($token !== FALSE) {
mail_token($_POST['email'], $token);
}
$success = 'If you are a known contact for any of our users, you will receive an email containing the link for your debt and credit overview.';
include('templates/success.php');
}
else {
$error = 'You seem to be at the wrong place somehow.';
include('templates/error.php');
}
}
elseif(substr($location, 0, 6) == 'token/') {
include('templates/tokennav.php');
include('resources/uiblocks.php');
include('resources/transactions.php');
include('resources/people.php');
$split = explode('/', $location);
$token = $split[1];
$email = get_token_email($token);
if($email === FALSE) {
$error = 'Go away.';
include('templates/error.php');
}
else {
if(isset($_GET['reset'])) {
$newtoken = reset_token($token);
mail_token($email, $newtoken);
$success = 'Your token has been reset, a new link has been email to you. This current link will also stop working.';
include('templates/success.php');
}
elseif(isset($split[2]) AND $split[2] == 'detail') {
title('Detailed view');
detailcard(get_user_transactions_for_debtor($split[3], $email), 'user');
}
else {
if(empty(get_transactions_per_user($email))) {
$success = 'No one has any debt for you.';
include('templates/success.php');
}
carddeck(get_transactions_per_user($email), 'user');
}
}
}
else {
include('templates/index.html');
}
include('templates/footer.php');
if($webcron) {
include('cron.php');
}