-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·7033 lines (5842 loc) · 414 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
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
//~ URL: https://www.tiny-server.com/
//~ Email: Ron de Jong <[email protected]>
//~ Author: Tiny Server, Ron de Jong, The Netherlands
//~ Description: Password Manager with SQLite DB and 100% Custom Fields
//~ Dependencies: A good Web Server such as Apache2 with PHP7.4 or higher
//~ PHP Extensions: standard tokenizer Core date openssl hash pcre mbstring
//~ License: Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License (CC BY-NC-ND 4.0) 2023
require("fortune/fortune.php");
define('DEMO', 0);
$date = date("Y-m-d_H-i-s");
$isodate = date("Y-m-d");
$isotime = date("H-i-s");
//~ Only relevant when used in combination with Tiny Server
$tiny_homedir = "/home/tiny/";
$tiny_scripts = "/home/tiny/Scripts/";
$tiny_services = "/home/tiny/Services/";
$tiny_setup_wrapper = "${tiny_services}by-name/apache2/apps/setup/www/tiny-setup";
$tiny_pass = "tinypass";
$tiny_pass_db = "${tiny_pass}.db";
$tiny_pass_export_file = "${date}_${tiny_pass}_export";
$non_tiny_pass_export_file = "${date}_non_ ${tiny_pass}_export";
$tiny_pass_path = dirname($_SERVER['SCRIPT_FILENAME']);
$tiny_pass_db_file = $tiny_pass_path.DIRECTORY_SEPARATOR.$tiny_pass_db;
$tiny_pass_dsn = "sqlite:" . $tiny_pass_db_file;
$logfile = "${tiny_pass}_index.php_${date}.log";
$tiny_apache2 = "/etc/apache2/";
$tiny_debug = 0;
$supplier_domain = "tiny-server.com";
$customer_domain = exec("awk -F\".\" 'END { print $(NF-1)\".\"\$NF}' /etc/mailname");
$web_desktpdomlnk = "https://${supplier_domain}/desktop/";
$logdata = "";
//~ Only relevant when used in combination with Tiny Server
$tiny_license_dir = "/var/tmp/";
$tiny_license_file = "tiny-license.txt";
$tiny_license_path = "${tiny_license_dir}${tiny_license_file}";
$tiny_license_content = exec("cat ${tiny_license_path} 2>/dev/null");
$user_auth_hash_type = "sha256"; // Stored hash to authenticate user logins - dataflow: [original users password] -> [sha256] -> [users.pass_hash]
$user_encr_hash_type = "sha512"; // Memory hash to en/de-crypt fields.field_value dataflow: [original login password] -> [hidden param] -> [sha512] -> [encrypt_data] -> [fields.field_value]
$user_orig_hash_type = "sha256"; // Stored hash type ioriginal value in table.field => fields.field_value to verify decryption
$user_encr_ciph_type = "aes-256-ctr"; // Cipher type to en/de-crypt fields.field_value "openssl_get_cipher_methods();" dataflow: [original login password] -> [sha512] -> [encrypt_data] -> [fields.field_value]
$ivlen = openssl_cipher_iv_length($user_encr_ciph_type);
$user_encr_init_vect = "1234567890123456"; // En/de-crypt initialization vector NOT BEING USED
$user_encr_hash_key = "1234567890123456"; // En/de-crypt initialization vector NOT BEING USED
$user_encr_ciph_opts = 0;
$user_stnd_pass_word = "123tinyfree";
$logo_bg = "img/logo_bg.jpg";
$menu_bg = "img/menu_bg.jpg";
$login_bg = "img/login_bg.jpg";
$default_bg = "img/default_bg.jpg";
//~ REQUEST_SCHEME = "https"
//~ SCRIPT_URI = "https://www.tiny-server.com/tiny/pass/index.php")
//~ HTTP_HOST = "www.tiny-server.com"
//~ SERVER_NAME = "www.tiny-server.com"
//~ define('TINY_PASS_URI', " . $_SERVER['SCRIPT_URI']. ");
//~ define('TINY_PASS_URI', $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST']. "/");
//~ define('LOGOUT_ACTION', "window.location.replace('" . TINY_PASS_URI . "');");
define('LOGOUT_ACTION', "");
//~ These kinds of history manipulations no longer work
//~ define('LOGOUT_ACTION', "deleteAllHistory();");
//~ define('LOGOUT_ACTION', "browser.history.deleteAll();");
//~ define('LOGOUT_ACTION', "window.location.replace(\"https://developer.mozilla.org/en-US/docs/Web/API/Location.reload\",);");
//~ define('LOGOUT_ACTION', "window.location.replace('https://developer.mozilla.org/');");
//~ define('TINY_PASS_URI', " . $_SERVER['SCRIPT_URI']. ");
//~ define('TINY_PASS_URI', $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST']. "/");
//~ define('LOGOUT_ACTION', "browser.history.deleteUrl({ url: " . TINY_PASS_URI . " })");
//~ foreach ($_SERVER as $key => $value) { echo $key . " = " . $value. "<br>\n"; } exit();
//~ ----------------------------------------------------------------------------
//~ CSV Export Constants
//~ ----------------------------------------------------------------------------
const EXPORT_FILE_TYPE = "text/csv";
const EXPORT_CSV_FORMATS =
[
"Edge" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "name", "url", "username", "password" ], // Read name field
"field_names2" => [ "Name", "URL", "Username", "Password" ], // Write name field
"field_types" => [ "text", "url", "text", "password" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, 0, 1, 2 ], // -1, nr (-1 = ignore field)
],
"Edge_2" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "name", "url", "username", "password" ], // Read name field
"field_names2" => [ "name", "url", "username", "password" ], // Write name field
"field_types" => [ "text", "url", "text", "password" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3 ], // -1, nr (-1 = ignore field)
],
"1Password" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "title", "url", "username", "password" ], // Read name field
"field_names2" => [ "Title", "URL", "Username", "Password" ], // Write name field
"field_types" => [ "text", "url", "text", "password" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, 0, 1, 2 ], // -1, nr (-1 = ignore field)
],
"1Password_2" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "title", "url", "username", "password" ], // Read name field
"field_names2" => [ "title", "url", "username", "password" ], // Write name field
"field_types" => [ "text", "url", "text", "password" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3 ], // -1, nr (-1 = ignore field)
],
"Chrome" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "name", "url", "username", "password", "note" ], // Read name field
"field_names2" => [ "Name", "URL", "Username", "Password", "Note" ], // Write name field
"field_types" => [ "text", "url", "text", "password", "textarea" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, 0, 1, 2, 3 ], // -1, nr (-1 = ignore field)
],
"Chrome_2" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "name", "url", "username", "password", "note" ], // Read name field
"field_names2" => [ "name", "url", "username", "password", "note" ], // Write name field
"field_types" => [ "text", "url", "text", "password", "textarea" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4 ], // -1, nr (-1 = ignore field)
],
"KeePass" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "Account", "Login Name", "Password", "Web Site", "Comments" ], // Read name field
"field_names2" => [ "Account", "Login", "Password", "URL", "Comments" ], // Write name field
"field_types" => [ "text", "text", "password", "url", "textarea" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, 1, 2, 0, 3 ], // -1, nr (-1 = ignore field)
],
"KeePass_2" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "Account", "Login Name", "Password", "Web Site", "Comments" ], // Read name field
"field_names2" => [ "Account", "Login Name", "Password", "Web Site", "Comments" ], // Write name field
"field_types" => [ "text", "text", "password", "url", "textarea" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4 ], // -1, nr (-1 = ignore field)
],
"TinyPass" =>
[
"secret_name" => 0,
"secret_group" => 1,
"field_names1" => [ "SecretName", "GroupName", "FieldOrder", "FieldName", "FieldType", "FieldValue" ], // Read name field
"field_names2" => [ "SecretName", "GroupName", "FieldOrder", "FieldName", "FieldType", "FieldValue" ], // Write name field
"field_types" => [ "text", "text", "integer", "text", "text", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4, 5 ], // -1, nr (-1 = ignore field)
],
"Dashlane" =>
[
"secret_name" => 1,
"secret_group" => -1,
"field_names1" => [ "Type", "name", "url", "username", "password", "note", "totp" ], // Read name field
"field_names2" => [ "Type", "Name", "URL", "Username", "Password", "Note", "totp" ], // Write name field
"field_types" => [ "text", "text", "url", "username", "password", "note", "totp" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, -1, 1, 2, 3, 4, -1 ], // -1, nr (-1 = ignore field)
],
"Dashlane_2" =>
[
"secret_name" => 1,
"secret_group" => -1,
"field_names1" => [ "Type", "name", "url", "username", "password", "note", "totp" ], // Read name field
"field_names2" => [ "Type", "Name", "URL", "Username", "Password", "Note", "totp" ], // Write name field
"field_types" => [ "text", "text", "url", "username", "password", "note", "totp" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4, 5, 6 ], // -1, nr (-1 = ignore field)
],
"1Password2" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "Title", "Url", "Username", "Password", "OTPAuth", "Favorite", "Archived", "Tags", "Notes" ], // Read name field
"field_names2" => [ "Title", "URL", "Username", "Password", "OTPAuth", "Favorite", "Archived", "Tags", "Notes" ], // Write name field
"field_types" => [ "text", "url", "text", "password", "text", "text", "text", "text", "textarea" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, 0, 1, 2, -1, -1, -1, 1, 3 ], // -1, nr (-1 = ignore field)
],
"1Password2_2" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "Title", "Url", "Username", "Password", "OTPAuth", "Favorite", "Archived", "Tags", "Notes" ], // Read name field
"field_names2" => [ "Title", "URL", "Username", "Password", "OTPAuth", "Favorite", "Archived", "Tags", "Notes" ], // Write name field
"field_types" => [ "text", "url", "text", "password", "text", "text", "text", "text", "textarea" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], // -1, nr (-1 = ignore field)
],
"Firefox" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "url", "username", "password", "httpRealm", "formActionOrigin", "guid", "timeCreated", "timeLastUsed", "timePasswordChanged" ], // Read name field
"field_names2" => [ "URL", "Username", "Password", "httpRealm", "formActionOrigin", "guid", "timeCreated", "timeLastUsed", "timePasswordChanged" ], // Write name field
"field_types" => [ "url", "text", "password", "url", "url", "text", "text", "text", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, -1, -1, -1, -1, -1, -1 ], // -1, nr (-1 = ignore field)
],
"Firefox_2" =>
[
"secret_name" => 0,
"secret_group" => -1,
"field_names1" => [ "url", "username", "password", "httpRealm", "formActionOrigin", "guid", "timeCreated", "timeLastUsed", "timePasswordChanged" ], // Read name field
"field_names2" => [ "URL", "Username", "Password", "httpRealm", "formActionOrigin", "guid", "timeCreated", "timeLastUsed", "timePasswordChanged" ], // Write name field
"field_types" => [ "url", "text", "password", "url", "url", "text", "text", "text", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4, 5, 6, 7, 8 ], // -1, nr (-1 = ignore field)
],
"Bitwarden" =>
[
"secret_name" => 3,
"secret_group" => 0,
"field_names1" => [ "folder", "favorite", "type", "name", "notes", "fields", "reprompt", "login_uri", "login_username", "login_password", "login_totp" ], // Read name field
"field_names2" => [ "folder", "favorite", "type", "Name", "Notes", "fields", "reprompt", "URI", "Username", "Password", "login_totp" ], // Write name field
"field_types" => [ "text", "text", "text", "text", "textarea", "text", "text", "url", "text", "password", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, -1, -1, -1, 4, -1, -1, 1, 2, 3, -1, ], // -1, nr (-1 = ignore field)
],
"Bitwarden_2" =>
[
"secret_name" => 3,
"secret_group" => 0,
"field_names1" => [ "folder", "favorite", "type", "name", "notes", "fields", "reprompt", "login_uri", "login_username", "login_password", "login_totp" ], // Read name field
"field_names2" => [ "folder", "favorite", "type", "name", "notes", "fields", "reprompt", "login_uri", "login_username", "login_password", "login_totp" ], // Write name field
"field_types" => [ "text", "text", "text", "text", "textarea", "text", "text", "url", "text", "login_password", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ], // -1, nr (-1 = ignore field)
],
"TeamPass" =>
[
"secret_name" => 1,
"secret_group" => 11,
"field_names1" => [ "id", "label", "description", "pw", "login", "restricted_to", "perso", "url", "email", "kb", "tag", "folder" ], // Read name field
"field_names2" => [ "Id", "Label", "Note", "Pass", "Login", "restricted_to", "perso", "URL", "Email", "kb", "Tag", "Folder" ], // Write name field
"field_types" => [ "text", "text", "textarea", "password", "text", "text", "text", "url", "email", "text", "text", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, -1, 4, 2, 1, -1, -1, 0, 3, -1, -1, -1 ], // -1, nr (-1 = ignore field)
],
"TeamPass_2" =>
[
"secret_name" => 1,
"secret_group" => 11,
"field_names1" => [ "id", "label", "description", "pw", "login", "restricted_to", "perso", "url", "email", "kb", "tag", "folder" ], // Read name field
"field_names2" => [ "id", "label", "description", "pw", "login", "restricted_to", "perso", "url", "email", "kb", "tag", "folder" ], // Write name field
"field_types" => [ "text", "text", "textarea", "password", "text", "text", "text", "url", "email", "text", "text", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 ], // -1, nr (-1 = ignore field)
],
"NordPass" =>
[
"secret_name" => 0,
"secret_group" => 10,
"field_names1" => [ "name", "url", "username", "password", "note", "cardholdername", "cardnumber", "cvc", "expirydate", "zipcode", "folder", "full_name", "phone_number", "email", "address1", "address2", "city", "country", "state" ], // Read name field
"field_names2" => [ "Name", "URL", "Username", "Password", "Note", "CardHoldeName", "CardNumber", "CVC", "ExpiryDate", "Zipcode", "Folder", "Full_name", "Phone_number", "Email", "Address1", "Address2", "City", "Country", "State" ], // Write name field
"field_types" => [ "text", "url", "text", "password", "textarea", "text", "text", "text", "text", "text", "text", "text", "text", "email", "text", "text", "text", "text", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ -1, 0, 1, 2, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ], // -1, nr (-1 = ignore field)
],
"NordPass_2" =>
[
"secret_name" => 0,
"secret_group" => 10,
"field_names1" => [ "name", "url", "username", "password", "note", "cardholdername", "cardnumber", "cvc", "expirydate", "zipcode", "folder", "full_name", "phone_number", "email", "address1", "address2", "city", "country", "state" ], // Read name field
"field_names2" => [ "name", "url", "username", "password", "note", "cardholdername", "cardnumber", "cvc", "expirydate", "zipcode", "folder", "full_name", "phone_number", "email", "address1", "address2", "city", "country", "state" ], // Write name field
"field_types" => [ "text", "url", "text", "password", "textarea", "text", "text", "text", "text", "text", "text", "text", "text", "email", "text", "text", "text", "text", "text" ], // URL, Email, Pass, Text, Textarea
"field_orders" => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ], // -1, nr (-1 = ignore field)
],
];
$cvs_keys = array_keys(EXPORT_CSV_FORMATS);
$cvs_field_lengths = array(); foreach (array_keys(EXPORT_CSV_FORMATS) as $key => $value) { $cvs_field_lengths[] = count(array_values(EXPORT_CSV_FORMATS[$value]["field_names1"])); }
$cvs_key_field_lengths = array_combine($cvs_keys, $cvs_field_lengths);
$csv_fields_desc = ""; foreach ($cvs_key_field_lengths as $key => $value) { $csv_fields_desc .= $key . "[" . $value . "] "; }
$csv_formats_desc = ""; foreach ($cvs_key_field_lengths as $key => $value) { $csv_formats_desc .= "[". $key . "] "; } $csv_formats_desc .= "CSV file\n\n[Format]: field1,field2,field3,field4...";
$csv_format_input_fields = ""; foreach ($cvs_keys as $key) { $csv_format_input_fields .= "[" . $key . "]: "; foreach (array_values(EXPORT_CSV_FORMATS[$key]["field_names1"]) as $field) { $csv_format_input_fields .= $field . ","; } $csv_format_input_fields .= "\n"; }
//~ $csv_format_input_fields = ""; foreach ($cvs_keys as $key) { $csv_format_input_fields .= "[" . $key . "]: "; for ($row = 0; $row < count(array_values(EXPORT_CSV_FORMATS[$key]["field_names1"])); $row++) { if ( EXPORT_CSV_FORMATS[$key]["field_orders"][$row] > -1 ) { $csv_format_input_fields .= "<b>". EXPORT_CSV_FORMATS[$key]["field_names2"][$row] . "</b>, "; } else { $csv_format_input_fields .= "". EXPORT_CSV_FORMATS[$key]["field_names2"][$row] . ", "; } } $csv_format_input_fields .= "\n"; }
//~ $csv_format_input_fields = ""; foreach ($cvs_keys as $key) { $csv_format_input_fields .= "[" . $key . "]: "; for ($row = 0; $row < count(array_values(EXPORT_CSV_FORMATS[$key]["field_names1"])); $row++) { if ( EXPORT_CSV_FORMATS[$key]["field_orders"][$row] > -1 ) { $csv_format_input_fields .= "<" . EXPORT_CSV_FORMATS[$key]["field_names2"][$row] . ">, "; } else { $csv_format_input_fields .= "[". EXPORT_CSV_FORMATS[$key]["field_names2"][$row] . "], "; } } $csv_format_input_fields .= "\n"; }
define('EXPORT_CSV_KEYS', $cvs_keys);
define('EXPORT_CSV_FORMATS_FIELD_LENGTHS', $cvs_key_field_lengths);
define('EXPORT_CSV_FORMATS_FIELD_DESC', $csv_fields_desc);
// \nImporting from a Tiny Pass formatted CSV file all fields are imported\n\n
// \nImporting from a Tiny Pass formatted CSV file all fields are imported\n\nImport from\n\n" . $csv_formats_desc . "\n\n" .
define('IMPORT_CSV_FORMATS_LIMITED_DESC', "Import *ALL* fields from Tiny Pass CSV file\nImport SOME fields from NON Tiny Pass CSV file\n\nChoose this option when you don't want all these useless NON Tiny Pass Format specific fields and you're not interested in exporting back to the original NON Tiny Pass format\n\nSupporting the following field formats:\n\n" . $csv_format_input_fields );
define('IMPORT_CSV_FORMATS_ORIGINAL_DESC', "Import *ALL* fields from NON Tiny Pass CSV file\nImport *ALL* fields from Tiny Pass CSV file\n\nChoose this option when you want to keep all the NON Tiny Pass specific fields because later you want to export back to the original NON Tiny Pass Format\n\nSupporting the following field formats:\n\n" . $csv_format_input_fields );
define('EXPORT_CSV_FORMATS_TINYPASS_DESC', "Export selected secrets to Tiny Pass formatted CSV file\nUse this export if it needs to be imported by Tiny Pass" );
define('EXPORT_CSV_FORMATS_NON_TINYPASS_DESC', "Export selected secrets to NON Tiny Pass formatted CSV file\nThis export works when [ Import *ALL* fields from NON Tiny Pass CSV file ] was used\n\nUse this export if it needs to be imported by the original Password Manager again" );
$cvs_keys = null; $cvs_field_lengths = null; $cvs_key_field_lengths = null; $csv_desc = null; $csv_format_input_fields = null;
//~ echo "Keys:\n\n"; print_r(array_keys(EXPORT_CSV_FORMATS)); echo "\n<br>\n<br>";
//~ foreach (array_keys(EXPORT_CSV_FORMATS) as $format)
//~ {
//~ echo "Name, Group:\n\n"; echo EXPORT_CSV_FORMATS[$format]["secret_name"] . "," . EXPORT_CSV_FORMATS[$format]["secret_group"]; echo "\n<br>";
//~ echo "Values: "; print_r(array_values(EXPORT_CSV_FORMATS[$format]["field_names1"])); echo "\n<br>";
//~ echo "Values: "; print_r(array_values(EXPORT_CSV_FORMATS[$format]["field_types"])); echo "\n<br>";
//~ echo "Values: "; print_r(array_values(EXPORT_CSV_FORMATS[$format]["field_orders"])); echo "\n<br>\n<br>";
//~ }
//~ echo "Name, Group:\n\n"; echo EXPORT_CSV_FORMATS["NordPass"]["secret_name"] . "," . EXPORT_CSV_FORMATS["NordPass"]["secret_group"]; echo "\n<br>";
//~ echo "FieldType[1]: "; echo EXPORT_CSV_FORMATS["NordPass"]["field_types"][1]; echo "\n<br>";
//~ echo "Values TinyPass: "; print_r(array_values(EXPORT_CSV_FORMATS["TinyPass"]["field_names1"])); echo "\n<br>";
//~ echo "Lengths TinyPass: "; print_r(EXPORT_CSV_FORMATS_FIELD_LENGTHS["TinyPass"]); echo "\n<br>";
//~ echo "Lengths: "; print_r(array_values(EXPORT_CSV_FORMATS_FIELD_LENGTHS)); echo "\n<br>";
//~ echo "Desc: " . EXPORT_CSV_FORMATS_FIELD_DESC . "\n\n";
//~ exit;
if ( isset($_SERVER['HTTP_HOST']) ) { define('TINY_PASS_HOST', $_SERVER['HTTP_HOST']); } else { define('TINY_PASS_HOST', ""); }
//~ $local_setup_version = exec("cat ${tiny_services}by-name/apache2/apps/$tiny_pass/version.txt;");
define('TINY_PASS_VERSION', 1);
define('TINY_PASS_UPGRADE', 0);
define('TINY_PASS_UPDATE', 0);
define('TINY_PASS_VERSION_DESC', TINY_PASS_VERSION . "." . TINY_PASS_UPGRADE . "." . TINY_PASS_UPDATE);
//~ ============================================================================
function encrypt_password($pass, $data) // data(orig) -> cipher -> base64
{
$key = hash($GLOBALS["user_encr_hash_type"], $pass);
$iv = substr($key,0,$GLOBALS["ivlen"]);
$cipher = openssl_encrypt($data, $GLOBALS["user_encr_ciph_type"], $key, $GLOBALS["user_encr_ciph_opts"], $iv);
$cipher_b64 = base64_encode($cipher);
return $cipher_b64;
}
function decrypt_password($pass, $data) // data(base64) -> cipher -> data(orig)
{
$key = hash($GLOBALS["user_encr_hash_type"], $pass);
$iv = substr($key,0,$GLOBALS["ivlen"]);
$cipher = base64_decode($data);
$orig = openssl_decrypt($cipher, $GLOBALS["user_encr_ciph_type"], $key, $GLOBALS["user_encr_ciph_opts"], $iv);
return $orig;
}
//~ ====================================================================
//~ Internal Database Functions
//~ ====================================================================
function createTables($pdo, $form, $button, $target)
{
$pdo->exec('
CREATE TABLE IF NOT EXISTS roles
(
role_id INTEGER PRIMARY KEY NOT NULL,
role_name VARCHAR (255) NOT NULL
)
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "exec(..)", $button, $target);
//~ Add initial records ----
$stmt = $pdo->prepare('SELECT * FROM roles WHERE role_name like :role1 OR role_name like :role2;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare1(..)", $button, $target);
$stmt->execute([':role1' => "Admin", ':role2' => "User"]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute1(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
if ( count($object_array) == 0)
{
//~ New Role
$pdo->exec('INSERT INTO roles(role_name) VALUES("Admin"),("User");');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "exec(..)", $button, $target);
}
//~ ----------------------------------------------------------------------------
$pdo->exec('
CREATE TABLE IF NOT EXISTS groups
(
group_id INTEGER PRIMARY KEY NOT NULL,
user_id INTEGER NOT NULL,
group_name VARCHAR (255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE
);
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "exec(..)", $button, $target);
//~ ----------------------------------------------------------------------------
$pdo->exec('
CREATE TABLE IF NOT EXISTS users
(
user_id INTEGER PRIMARY KEY NOT NULL,
role_id INTEGER NOT NULL,
user_name VARCHAR (255) NOT NULL,
hash_type VARCHAR (255) NOT NULL,
pass_hash VARCHAR (255) NOT NULL,
FOREIGN KEY (role_id) REFERENCES roles (role_id) ON UPDATE CASCADE ON DELETE RESTRICT
);
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "exec(..)", $button, $target);
//~ ----------------------------------------------------------------------------
$pdo->exec('
CREATE TABLE IF NOT EXISTS secrets
(
secret_id INTEGER PRIMARY KEY NOT NULL,
user_id INTEGER NOT NULL,
group_id INTEGER,
secret_date INTEGER NOT NULL,
secret_name VARCHAR (255) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (group_id) REFERENCES groups (group_id) ON UPDATE CASCADE ON DELETE SET NULL
);
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "exec(..)", $button, $target);
//~ -----------------------------------------------------------
$pdo->exec('
CREATE TABLE IF NOT EXISTS fields
(
field_id INTEGER PRIMARY KEY NOT NULL,
user_id INTEGER NOT NULL,
secret_id INTEGER NOT NULL,
field_ordr INTEGER NOT NULL,
field_name VARCHAR (255) NOT NULL,
field_type INTEGER (255) NOT NULL,
field_value VARCHAR (255),
field_hash VARCHAR (255),
FOREIGN KEY (user_id) REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE
FOREIGN KEY (secret_id) REFERENCES secrets (secret_id) ON UPDATE CASCADE ON DELETE CASCADE
);
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "exec(..)", $button, $target);
}
function createUsers($pdo, $form, $button, $target)
{
$user_name = "admin"; $qty = 10; $pass_word = $GLOBALS["user_stnd_pass_word"]; if ( ! userExists($pdo, $user_name, $form, $button, $target) ) { insertUser($pdo, 1, $user_name, $pass_word, $form, $button, $target); create_test_secrets($user_name, $pass_word, $qty); }
$user_name = "tiny"; $qty = 11; $pass_word = $GLOBALS["user_stnd_pass_word"]; if ( ! userExists($pdo, $user_name, $form, $button, $target) ) { insertUser($pdo, 2, $user_name, $pass_word, $form, $button, $target); create_test_secrets($user_name, $pass_word, $qty); }
}
//~ ====================================================================
//~ External Database Functions
//~ ====================================================================
function connectDB($form, $button, $target)
{
$pdo = new PDO($GLOBALS["tiny_pass_dsn"]);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
//~ $tables = getTables($pdo,$form,$button,$target);
//~ if ( count($tables) == 0) { createTables($pdo, $form, $button, $target); }
createTables($pdo, $form, $button, $target);
createUsers($pdo, $form, $button, $target);
return $pdo;
}
function getTables($pdo, $form, $button, $target)
{
$stmt = $pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name;");
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "exec(..)", $button, $target);
$tables = []; while ($record = $stmt->fetch(\PDO::FETCH_ASSOC)) { $tables[] = $record['name']; }
return $tables;
}
$type2num_arr = array("url" => 0, "email" => 1, "password" => 2, "text" => 3, "textarea" => 4);
$num2type_arr = array(0 => "url", 1 => "email", 2 => "password", 3 => "text", 4 => "textarea");
function getType2Num($type) { return $type2num_arr[type]; }
function getNum2Type($num) { return $tnum2ype_arr[num]; }
//~ ----------------------------------------------------------------------------
function web_log()
{
global $logdata;
$loglines = preg_split("/(\r\n|\n|\r)/",$logdata);
foreach ($loglines as &$logline) { error_log($logline, 0); }
}
function buf_log($data)
{
echo $data;
global $logdata;
if ( strlen($logdata) < 300 )
{
$logdata .= $data;
}
else
{
$logdata .= $data;
web_log();
$logdata = "";
}
}
function message($message, $form, $instruction, $button, $target)
{
echo "<div class=\"eyesloaded\"></div>";
echo "<p style=\"position: fixed; top: 10%; left: 85%; transform: translateX(-50%); text-shadow: 0px 0px 5px rgba(30,30,5,0.8); font-size: x-large; white-space: nowrap;\">T E R M I N A L</p>";
echo "<div style=\"position: fixed; top: 85%; left: 85%; transform: translateX(-50%); z-index: 100;\"><table style=\"width:100%; text-align: center; border: 0px solid;\"><tr><td><form class=\"info\" action=\"$target\" method=\"post\" autocomplete=\"off\"><button class=\"bigbutton\" style=\"font-size: large; white-space: nowrap;\" type=\"submit\">$button</button></form></td></tr></table></div>";
echo "<div style=\"width: 100vw; position: stycky; top: 25%; margin-left: 50%; transform: translateX(-50%); z-index: 0; overflow-y: auto;\" >";
echo "$message";
//~ echo "<p style=\"text-align:center;\">Form [ $form ]</p>";
echo "<script type='text/javascript'> document.getElementById(\"loader\").style.visibility = \"visible\"; document.getElementById(\"loader\").style.opacity = \"1.0\"; var setup_img_el = document.getElementById(\"loader\"); setup_img_el.remove(); </script>";
}
function messageRaw($message)
{
print "$message";
}
function dboHeader($form, $instruction, $button, $target)
{
if( $errorArray[0] != "00000" || strlen($errorArray[1]) > 0 || strlen($errorArray[2]) > 0)
{
$tp_login_uname = htmlspecialchars($_POST['tp_login_uname'], ENT_QUOTES, 'UTF-8');
$tp_login_pword = htmlspecialchars($_POST['tp_login_pword'], ENT_QUOTES, 'UTF-8');
echo "<div style=\"position: fixed; left: 84%; top: 50%;\" class=\"diceloaded\"></div>";
echo "<p style=\"position: fixed; top: 10%; left: 85%; transform: translateX(-50%); text-shadow: 0px 0px 5px rgba(30,30,5,0.8); font-size: x-large; white-space: nowrap;\">DB Error</p>";
echo "<div style=\"position: fixed; top: 85%; left: 85%; transform: translateX(-50%); z-index: 100;\"><table style=\"width:100%; text-align: center; border: 0px solid;\"><tr><td>";
echo "<form class=\"info\" action=\"$target\" method=\"post\" autocomplete=\"off\">";
echo "<input type=\"hidden\" id=\"tinypass_formname\" name=\"tinypass_formname\" value=\"$form\">";
echo "<input type=\"hidden\" id=\"tp_login_uname\" name=\"tp_login_uname\" value=\"$tp_login_uname\">";
echo "<input type=\"hidden\" id=\"tp_login_pword\" name=\"tp_login_pword\" value=\"$tp_login_pword\">";
//~ echo "<button class=\"bigbutton\" style=\"font-size: large; white-space: nowrap;\" type=\"submit\">$button</button>";
echo "</form>";
echo "</td></tr></table></div>";
echo "<div style=\"width: 100vw; position: stycky; top: 25%; margin-left: 50%; transform: translateX(-50%); z-index: 0; overflow-y: auto;\" ><p style=\"text-align:center;\">Form [ $form ]</p><p><br /></p><p style=\"text-align:center;\"><span style=\"color: red;\">Error Instruction:</span> $instruction</p>";
}
}
function dboError($errorArray, $header, $form, $instruction, $button, $target)
{
if( $errorArray[0] != "00000" || strlen($errorArray[1]) > 0 || strlen($errorArray[2]) > 0)
{
if ( $header == "header" ) { send_html_header(""); dboHeader($form, $instruction, $button, $target); }
echo "<table style=\"text-align: center; font-size: large; width:100%\" class=\"t_able _table-bordered\">";
echo " <tbody>";
$errorCounter = 0;
$errorField = array("SQLState","Error code","Error mess");
foreach ($errorArray as $error)
{
echo "<tr><td style=\"text-align:center; color: red;\">$errorField[$errorCounter]</td><td style=\"text-align:center; color: white;\"><a style=\"_color: white\"class=\"_border\" href=\"https://en.wikipedia.org/wiki/SQLSTATE\" target=\"_blank\">$error</a></td></tr>";
$errorCounter++;
}
echo " </tbody>";
echo "</table>";
exit;
}
}
//~ ====================================================================
//~ Database Functions
//~ ====================================================================
function printHeader($message, $form, $button, $target)
{
send_html_header("");
echo "<div class=\"eyesloaded\"></div>";
echo "<p style=\"position: fixed; top: 10%; left: 85%; transform: translateX(-50%); text-shadow: 0px 0px 5px rgba(30,30,5,0.8); font-size: x-large; white-space: nowrap;\">T E R M I N A L</p>";
echo "<div style=\"position: fixed; top: 85%; left: 85%; transform: translateX(-50%); z-index: 100;\"><table style=\"width:100%; text-align: center; border: 0px solid;\"><tr><td><form class=\"info\" action=\"$target\" method=\"post\" autocomplete=\"off\"><button class=\"bigbutton\" style=\"font-size: large; white-space: nowrap;\" type=\"submit\">$button</button></form></td></tr></table></div>";
echo "<div style=\"width: 100vw; position: stycky; top: 25%; margin-left: 50%; transform: translateX(-50%); z-index: 0;\" >";
echo "<h1 style=\"text-align:center;\">$message</h1>";
echo "<p style=\"text-align:center;\">Form [ $form ]</p>";
//~ echo "<p style=\"text-align:center;\">TinyPass</p>";
echo "<script type='text/javascript'> document.getElementById(\"loader\").style.visibility = \"visible\"; document.getElementById(\"loader\").style.opacity = \"1.0\"; var setup_img_el = document.getElementById(\"loader\"); setup_img_el.remove(); </script>";
}
function printFooter()
{
echo "<script type='text/javascript'> document.getElementById(\"loader\").style.visibility = \"visible\"; document.getElementById(\"loader\").style.opacity = \"1.0\"; var setup_img_el = document.getElementById(\"loader\"); setup_img_el.remove(); </script>";
}
//~ ============================================================================
function authenticateUser($user_name, $pass_word, $form, $button, $target)
{
$pdo = connectDB($form, $button, $target);
//~ echo "\$pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS): [" . $pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS) . "]\n"; // not supported attribute
$pass_hash = hash($GLOBALS["user_auth_hash_type"], "$pass_word");
$stmt = $pdo->prepare('SELECT * FROM users WHERE user_name = :user_name AND pass_hash = :pass_hash;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare1(..)", $button, $target);
$stmt->execute([':user_name' => $user_name, ':pass_hash' => $pass_hash]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute1(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
//~ echo "\$GLOBALS[\"user_auth_hash_type\"]: ". $GLOBALS["user_auth_hash_type"] . "\n";
//~ echo "Password: " . $pass_word . "\n";
//~ echo "Login-PassHash: " . $pass_hash . "\n";
//~ echo "DB----PassHash: ". $object_array[0]->pass_hash . "\n";
if ( count($object_array) > 0) { return true; }
else { return false; }
}
function userExists($pdo, $user_name, $form, $button, $target)
{
$stmt = $pdo->prepare('SELECT * FROM users WHERE user_name = :user_name;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare1(..)", $button, $target);
$stmt->execute([':user_name' => $user_name]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute1(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
if ( count($object_array) > 0) { return true; } else { return false; }
}
function insertUser($pdo, $role_id, $user_name, $pass_word, $form, $button, $target)
{
if ( userExists($pdo, $user_name, $form, $button, $target) ) { dboError(array("-","-","User: \"$user_name\" exists!"), "header", $form, "insertUser(\"$user_name\")", $button, $target); }
$hash_type = $GLOBALS["user_auth_hash_type"];
$pass_hash = hash($GLOBALS["user_auth_hash_type"], $pass_word);
//~ New User
$stmt = $pdo->prepare('INSERT INTO users(role_id, user_name, hash_type, pass_hash) VALUES(:role_id,:user_name,:hash_type,:pass_hash);');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare2(..)", $button, $target);
$stmt->execute([':role_id' => $role_id, ':user_name' => $user_name, ':hash_type' => $hash_type, ':pass_hash' => $pass_hash]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute2(..)", $button, $target);
return $pdo->lastInsertId();
}
function updateUser($pdo, $role_id, $user_id, $user_name, $form, $button, $target)
{
//~ Update User
//~ $stmt = $pdo->prepare('UPDATE secrets SET group_id = :group_id, secret_date = :secret_date, secret_name = :secret_name WHERE secret_id = :secret_id;');
$stmt = $pdo->prepare('
UPDATE users SET role_id = :role_id, user_name = :user_name
WHERE user_id = :user_id;
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare2(..)", $button, $target);
$result = $stmt->execute([':role_id' => $role_id, ':user_id' => $user_id, ':user_name' => $user_name]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute2(..)", $button, $target);
return $result;
}
function updateUserCredentials($pdo, $role_id, $user_id, $user_name, $pass_word, $form, $button, $target)
{
$hash_type = $GLOBALS["user_auth_hash_type"];
$pass_hash = hash($GLOBALS["user_auth_hash_type"], $pass_word);
//~ Update User
//~ $stmt = $pdo->prepare('UPDATE secrets SET group_id = :group_id, secret_date = :secret_date, secret_name = :secret_name WHERE secret_id = :secret_id;');
$stmt = $pdo->prepare('
UPDATE users SET role_id = :role_id, user_name = :user_name, hash_type = :hash_type, pass_hash = :pass_hash
WHERE user_id = :user_id;
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare2(..)", $button, $target);
$result = $stmt->execute([':role_id' => $role_id, ':user_id' => $user_id, ':user_name' => $user_name, ':hash_type' => $hash_type, ':pass_hash' => $pass_hash]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute2(..)", $button, $target);
return $result;
}
function userIsAdmin($user_name, $form, $button, $target)
{
$pdo = connectDB($form, $button, $target);
$role_id = getRoleIdByUserName($pdo, $user_name, $form, $button, $target);
$stmt = $pdo->prepare('
SELECT role_name
FROM roles
WHERE role_id = :role_id;
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':role_id' => $role_id]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
if ( $object_array[0]->role_name == "Admin" ) { return true; } else { return false; }
}
function selectUsers($pdo, $form, $button, $target)
{
//~ user_id INTEGER PRIMARY KEY NOT NULL,
//~ role_id INTEGER NOT NULL,
//~ user_name VARCHAR (255) NOT NULL,
//~ hash_type VARCHAR (255) NOT NULL,
//~ pass_hash VARCHAR (255) NOT NULL,
$stmt = $pdo->prepare('
SELECT *
FROM users
ORDER BY user_name;
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute();
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array;
}
function selectUserById($pdo, $user_id, $form, $button, $target)
{
$stmt = $pdo->prepare('
SELECT *
FROM users
WHERE user_id = :user_id;
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':user_id' => $user_id]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array;
}
//~ user_id INTEGER PRIMARY KEY NOT NULL,
//~ role_id INTEGER NOT NULL,
//~ user_name VARCHAR (255) NOT NULL,
//~ hash_type VARCHAR (255) NOT NULL,
//~ pass_hash VARCHAR (255) NOT NULL,
function searchUsersByName($pdo, $search_name_filter, $search_group_filter, $primar_column_order_fld, $primar_column_order_dir, $second_column_order_fld, $second_column_order_dir, $form, $button, $target) // $primar_column_order_fld = "left_column" or "right_column", $primar_column_order_dir = "ASC"or "DESC"
{
$stmt = $pdo->prepare("
SELECT users.user_id, users.role_id, users.user_name, users.hash_type, users.pass_hash, COUNT(secrets.user_id) AS user_secrets
FROM users
LEFT JOIN secrets ON users.user_id = secrets.user_id
WHERE users.user_name like :search_name_filter
AND COALESCE(secrets.secret_name,'') like :search_group_filter
GROUP BY users.user_id
ORDER BY ${primar_column_order_fld} ${primar_column_order_dir}, ${second_column_order_fld} ${second_column_order_dir};
");
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "query(..)", $button, $target);
$stmt->execute([
':search_name_filter' => "%" . $search_name_filter . "%"
,':search_group_filter' => "%" . $search_group_filter . "%"
]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array;
}
function getUserIdByUserName($user_name, $form, $button, $target)
{
$pdo = connectDB($form, $button, $target);
$stmt = $pdo->prepare('SELECT user_id FROM users WHERE user_name = :user_name;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':user_name' => $user_name]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array[0]->user_id;
}
function getUserNameByUserId($pdo, $user_id, $form, $button, $target)
{
$stmt = $pdo->prepare('SELECT user_name FROM users WHERE user_id = :user_id;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':user_id' => $user_id]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array[0]->user_name;
}
function getRoleIdByUserName($pdo, $user_name, $form, $button, $target)
{
$stmt = $pdo->prepare('SELECT role_id FROM users WHERE user_name = :user_name;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':user_name' => $user_name]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array[0]->role_id;
}
function printUsers($users)
{
echo "<table style=\"text-align: center; font-size: large; width:100%\" class=\"t_able _table-bordered\">";
echo " <thead>";
echo " <tr><th colspan=5>Users</th></tr>";
echo " <tr><th>User Id</th><th>Role Id</th><th>User Name</th><th>Pass Hash</th></tr>";
echo " </thead>";
echo " <tbody>";
foreach ($users as $user)
{
echo " <tr><td>$user->user_id</td><td>$user->role_id</td><td>$user->user_name</td><td>$user->pass_hash</td></tr>";
}
echo " </tbody>";
echo "</table>";
}
function deleteUserByUserId($pdo, $user_id, $form, $button, $target)
{
$user_name = getUserNameByUserId($pdo, $user_id, $form, $button, $target);
if ( $user_name === "admin" || $user_name === "tiny" )
{
dboError(array("-","-","Forbidden to delete user: " . $user_name), "header", $form, "deleteUserByUserId(\"$user_name\")", "[ ◀ OK ▶ ]","index.php");
}
else
{
deleteSecretByUserId($pdo, $user_id, $form, $button, $target);
$stmt = $pdo->prepare('DELETE FROM users WHERE user_id = :user_id;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare('DELETE FROM users..)", $button, $target);
$result = $stmt->execute([':user_id' => $user_id]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute([':user_id' => \$user_id])", $button, $target);
return $result;
}
}
//~ ----------------------------------------------------------------------------
//~ ----------------------------------------------------------------------------
function insertRole($pdo, $role_name, $form, $button, $target)
{
//~ Stop if role_name exists
$stmt = $pdo->prepare('SELECT * FROM roles WHERE role_name = :role_name;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare1(..)", $button, $target);
$stmt->execute([':role_name' => $role_name]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute1(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
if ( count($object_array) > 0) { dboError($err, "header", $form, "insertRole(\"$role_name\")", $button, $target); dboError(array("-","-","Roles: \"$role_name\" exists"), "header", $form, "execute1(..)", $button, $target); }
//~ New Role
$stmt = $pdo->prepare('INSERT INTO roles(role_name) VALUES(:role_name);');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare2(..)", $button, $target);
$stmt->execute([':role_name' => $role_name]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute2(..)", $button, $target);
return $pdo->lastInsertId();
}
function selectRoles($pdo, $role_name, $form, $button, $target)
{
$stmt = $pdo->prepare('SELECT * FROM roles WHERE role_name like :role_name;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':role_name' => "%" . $role_name . "%"]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array;
}
//~ Table roles
//~ role_id INTEGER PRIMARY KEY NOT NULL,
//~ role_name VARCHAR (255) NOT NULL
//~ Table users
//~ user_id INTEGER PRIMARY KEY NOT NULL,
//~ role_id INTEGER NOT NULL,
//~ user_name VARCHAR (255) NOT NULL,
//~ hash_type VARCHAR (255) NOT NULL,
//~ pass_hash VARCHAR (255) NOT NULL,
function getRoleNameByUserName($pdo, $user_name, $form, $button, $target)
{
$role_id = getRoleIdByUserName($pdo, $user_name, $form, $button, $target);
$stmt = $pdo->prepare('
SELECT role_name
FROM roles
WHERE role_id = :role_id;
');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':role_id' => $role_id]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array[0]->role_name;
}
function getRoleIdByRoleName($pdo, $role_name, $form, $button, $target)
{
$stmt = $pdo->prepare('SELECT role_id FROM roles WHERE role_name = :role_name;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$stmt->execute([':role_name' => $role_name]);
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
$object_array = []; while ($record = $stmt->fetchObject()) { $object_array[] = $record; }
return $object_array[0]->role_id;
}
function printRoles($roles)
{
echo "<table style=\"text-align: center; font-size: large; width:100%\" class=\"t_able _table-bordered\">";
echo " <thead>";
echo " <tr><th colspan=5>Roles</th></tr>";
echo " <tr><th>Role Id</th><th>Role Name</th></tr>";
echo " </thead>";
echo " <tbody>";
foreach ($roles as $role)
{
echo " <tr><td>$role->role_id</td><td>$role->role_name</td></tr>";
}
echo " </tbody>";
echo "</table>";
}
function updateRole($pdo, $fromRoleName, $toRoleName, $form, $button, $target)
{
$stmt = $pdo->prepare('UPDATE roles SET role_name = :toRoleName WHERE role_name = :fromRoleName;');
$errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare(..)", $button, $target);
$result = $stmt->execute([':fromRoleName' => $fromRoleName, ':toRoleName' => $toRoleName]); $errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute(..)", $button, $target);
return $result;
}
function deleteRole($pdo, $role_name, $form, $button, $target)
{
$roles = selectRoles($pdo, $role_name, $form, "[ ◀ OK ▶ ]", "index.php");
//~ printHeader("Delete role: \"$role_name\"", $form, "[ ◀ OK ▶ ]", "index.php");
//~ printRoles($roles);
if ( count($roles) == 0) { dboError(array("-","-","Role: \"$role_name\" not found"), "header", $form, "deleteRole(\"$role_name\")", $button, $target); }
else
{
$stmt = $pdo->prepare('DELETE FROM roles WHERE role_name = :role_name;'); $errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "prepare('DELETE FROM roles..)", $button, $target);
$stmt->execute([':role_name' => $role_name]); $errorArray = $pdo->errorInfo(); dboError($errorArray, "header", $form, "execute([':role_name' => \$role_name])", $button, $target);
$roles = selectRoles($pdo, $role_name, $form, "[ ◀ OK ▶ ]", "index.php");
printHeader("Delete role: \"$role_name\"", $form, "[ ◀ OK ▶ ]", "index.php");
printRoles($roles);
}
}
//~ ----------------------------------------------------------------------------
//~ $pdo->exec('CREATE TABLE IF NOT EXISTS secrets