-
Notifications
You must be signed in to change notification settings - Fork 44
/
admin.php
1949 lines (1738 loc) · 74.3 KB
/
admin.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
// SSO Server admin. Based on Admin Pack.
// (C) 2020 CubicleSoft. All Rights Reserved.
define("SSO_FILE", 1);
define("SSO_MODE", "admin");
require_once "config.php";
define("BB_ROOT_URL", SSO_ROOT_URL);
define("BB_SUPPORT_PATH", SSO_SUPPORT_PATH);
require_once SSO_ROOT_PATH . "/" . SSO_SUPPORT_PATH . "/debug.php";
require_once SSO_ROOT_PATH . "/" . SSO_SUPPORT_PATH . "/str_basics.php";
require_once SSO_ROOT_PATH . "/" . SSO_SUPPORT_PATH . "/page_basics.php";
require_once SSO_ROOT_PATH . "/" . SSO_SUPPORT_PATH . "/sso_functions.php";
require_once SSO_ROOT_PATH . "/" . SSO_SUPPORT_PATH . "/aes.php";
require_once SSO_ROOT_PATH . "/" . SSO_SUPPORT_PATH . "/blowfish.php";
require_once SSO_ROOT_PATH . "/" . SSO_SUPPORT_PATH . "/random.php";
SetDebugLevel();
Str::ProcessAllInput();
// Don't proceed any further if this is an acciental re-upload of this file to the root path.
if (SSO_STO_ADMIN && SSO_ROOT_PATH == str_replace("\\", "/", dirname(__FILE__))) exit();
if (SSO_USE_HTTPS && !BB_IsSSLRequest())
{
header("Location: " . BB_GetFullRequestURLBase("https"));
exit();
}
// Initialize language settings.
BB_InitLangmap(SSO_ROOT_PATH . "/" . SSO_LANG_PATH . "/", SSO_DEFAULT_LANG);
BB_SetLanguage(SSO_ROOT_PATH . "/" . SSO_LANG_PATH . "/", SSO_ADMIN_LANG);
// Initialize the global CSPRNG instance.
$sso_rng = new CSPRNG();
// Calculate the remote IP address.
$sso_ipaddr = SSO_GetRemoteIP();
$bb_randpage = SSO_BASE_RAND_SEED;
$bb_rootname = "SSO Server Admin";
$bb_usertoken = "";
$sso_site_admin = false;
$sso_user_id = "0";
// Require developers to inject code here. For example, integration with a specific login system or IP address restrictions.
if (file_exists("admin_hook.php")) require_once "admin_hook.php";
if (!is_string($bb_usertoken) || $bb_usertoken === "")
{
echo "Invalid user token.\n";
exit();
}
BB_ProcessPageToken("action");
// Connect to the database and generate database globals.
SSO_DBConnect(true);
// Load in fields with admin select.
SSO_LoadFields(true);
// Load in $sso_settings and initialize it.
SSO_LoadSettings();
// Menu/Navigation options.
if ($sso_site_admin)
{
$sso_menuopts = array(
"SSO Server Options" => array(
"Find User" => BB_GetRequestURLBase() . "?action=finduser&sec_t=" . BB_CreateSecurityToken("finduser"),
"Manage Fields" => BB_GetRequestURLBase() . "?action=managefields&sec_t=" . BB_CreateSecurityToken("managefields"),
"Manage Tags" => BB_GetRequestURLBase() . "?action=managetags&sec_t=" . BB_CreateSecurityToken("managetags"),
"Manage API Keys" => BB_GetRequestURLBase() . "?action=manageapikeys&sec_t=" . BB_CreateSecurityToken("manageapikeys"),
"Manage IP Cache" => BB_GetRequestURLBase() . "?action=manageipcache&sec_t=" . BB_CreateSecurityToken("manageipcache"),
"Configure" => BB_GetRequestURLBase() . "?action=configure&sec_t=" . BB_CreateSecurityToken("configure"),
"Reset All Sessions" => array("href" => BB_GetRequestURLBase() . "?action=resetsessions&sec_t=" . BB_CreateSecurityToken("resetsessions"), "onclick" => "return confirm('" . htmlspecialchars(BB_JSSafe(BB_Translate("Are you sure you want to reset all sessions?"))) . "');"),
)
);
}
else
{
$sso_menuopts = array(
"SSO Server Options" => array(
"Find User" => BB_GetRequestURLBase() . "?action=finduser&sec_t=" . BB_CreateSecurityToken("finduser"),
)
);
}
// Load providers.
$providers = SSO_GetProviderList();
$sso_providers = array();
$menuopts = array();
$newprovider = false;
foreach ($providers as $sso_provider)
{
if (!isset($sso_settings[$sso_provider]))
{
$sso_settings[$sso_provider] = array();
$newprovider = true;
}
require_once SSO_ROOT_PATH . "/" . SSO_PROVIDER_PATH . "/" . $sso_provider . "/index.php";
if (class_exists($sso_provider))
{
$sso_providers[$sso_provider] = new $sso_provider;
$sso_providers[$sso_provider]->Init();
$result = $sso_providers[$sso_provider]->MenuOpts();
if (is_array($result) && isset($result["name"]) && isset($result["items"]))
{
$order = (isset($sso_settings[""]["order"][$sso_provider]) ? $sso_settings[""]["order"][$sso_provider] : $sso_providers[$sso_provider]->DefaultOrder());
SSO_AddSortedOutput($menuopts, $order, $result["name"], $result["items"]);
}
}
}
// Some providers take a while to initialize the first time (e.g. Generic Login).
if ($newprovider) SSO_SaveSettings();
// Merge provider menus into the main menu.
ksort($menuopts);
foreach ($menuopts as $menus)
{
ksort($menus);
$sso_menuopts = array_merge($sso_menuopts, $menus);
}
// Append product information.
$sso_menuopts["About SSO Server"] = array(
"Homepage" => array("href" => "http://barebonescms.com/documentation/sso/", "target" => "_blank"),
"Donate" => array("style" => "color: #008800;", "href" => "http://barebonescms.com/donate/", "target" => "_blank", "title" => BB_Translate("Don't be a cheapskate. Support the author to keep SSO Server development going.")),
"Forums" => array("href" => "http://barebonescms.com/forums/", "target" => "_blank"),
);
if (function_exists("AdminHook_MenuOpts")) AdminHook_MenuOpts();
function SSO_CreateConfigURL($action2, $extra = array())
{
global $sso_provider;
$extra["provider"] = $sso_provider;
if ($action2 != "") $extra["action2"] = $action2;
$extra2 = "";
foreach ($extra as $key => $val) $extra2 .= "&" . urlencode($key) . "=" . urlencode($val);
return BB_GetRequestURLBase() . "?action=config" . $extra2 . "&sec_t=" . BB_CreateSecurityToken("config", array_values($extra)) . "&sec_extra=" . implode(",", array_keys($extra));
}
function SSO_CreateConfigLink($title, $action2, $extra = array(), $confirm = "")
{
return "<a href=\"" . htmlspecialchars(SSO_CreateConfigURL($action2, $extra)) . "\"" . ($confirm != "" ? " onclick=\"return confirm('" . htmlspecialchars(BB_JSSafe(BB_Translate($confirm))) . "');\"" : "") . ">" . htmlspecialchars(BB_Translate($title)) . "</a>";
}
function SSO_ConfigRedirect($action2, $extra = array(), $msgtype = "", $msg = "")
{
header("Location: " . SSO_CreateConfigURL($action2, $extra) . ($msg != "" ? "&bb_msgtype=" . urlencode($msgtype) . "&bb_msg=" . urlencode($msg) : ""));
exit();
}
if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "config" && isset($_REQUEST["provider"]) && isset($sso_providers[$_REQUEST["provider"]]) && isset($_REQUEST["action2"]))
{
// Pass the request to the specified provider.
$sso_provider = $_REQUEST["provider"];
$sso_providers[$sso_provider]->Config();
}
else if ($sso_site_admin && isset($_REQUEST["action"]) && $_REQUEST["action"] == "deleteusertag")
{
$row = $sso_db->GetRow("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "id = ?",
), $sso_db_users, $_REQUEST["id"]);
if ($row === false) BB_RedirectPage("error", "User does not exist.");
else if (isset($_REQUEST["id2"]))
{
$row2 = $sso_db->GetRow("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "id = ?",
), $sso_db_tags, $_REQUEST["id2"]);
if ($row2 !== false && ($row2->tag_name != SSO_SITE_ADMIN_TAG || $sso_site_admin))
{
$sso_db->Query("DELETE", array($sso_db_user_tags, "WHERE" => "user_id = ? AND tag_id = ?"), $row->id, $row2->id);
}
BB_RedirectPage("success", "Successfully deleted the user tag.", array("action=edituser&id=" . $row->id . "&sec_t=" . BB_CreateSecurityToken("edituser")));
}
}
else if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "edituser")
{
$row = $sso_db->GetRow("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "id = ?",
), $sso_db_users, $_REQUEST["id"]);
if ($row === false) BB_RedirectPage("error", "User does not exist.");
else
{
$tags = array("" => "");
$result = $sso_db->Query("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "enabled = 1",
"ORDER BY" => "tag_name"
), $sso_db_tags);
while ($row2 = $result->NextRow())
{
if ($row2->tag_name != SSO_SITE_ADMIN_TAG || $sso_site_admin) $tags[$row2->id] = $row2->tag_name;
}
$sso_provider = $row->provider_name;
$userinfo = SSO_LoadDecryptedUserInfo($row);
if ($userinfo === false) BB_RedirectPage("error", "Unable to load user information.");
if (isset($sso_providers[$sso_provider])) $protectedfields = $sso_providers[$sso_provider]->GetProtectedFields();
else
{
foreach ($sso_fields as $key => $encrypted) $protectedfields[$key] = true;
}
$geoip_opts = SSO_GetGeoIPOpts();
foreach ($geoip_opts as $opt => $val)
{
if ($sso_settings[""]["iprestrict"]["geoip_map_" . $opt] != "") $protectedfields[$sso_settings[""]["iprestrict"]["geoip_map_" . $opt]] = true;
}
$fields = $sso_fields;
foreach ($userinfo as $key => $val) $fields[$key] = $key;
if (function_exists("AdminHook_EditUser_PreFields")) AdminHook_EditUser_PreFields();
if (isset($_REQUEST["version"]))
{
if ((int)$_REQUEST["version"] < 0) BB_SetPageMessage("error", "Account Version must 0 or higher.");
if (!isset($tags[$_REQUEST["tag_id"]])) BB_SetPageMessage("error", "Please select a valid tag.");
else if ($_REQUEST["tag_id"] == "" && $_REQUEST["tag_reason"] != "") BB_SetPageMessage("error", "Please select a tag.");
else if ($_REQUEST["tag_id"] != "" && $_REQUEST["tag_reason"] == "") BB_SetPageMessage("error", "Please enter a reason for the new tag.");
else if ($_REQUEST["tag_id"] != "" && strlen($_REQUEST["tag_reason"]) > 100) BB_SetPageMessage("error", "The reason for the new tag is too long.");
if (function_exists("AdminHook_EditUser_Check")) AdminHook_EditUser_Check();
if (BB_GetPageMessageType() != "error")
{
foreach ($fields as $key => $fieldinfo)
{
if (substr($key, 0, 5) == "sso__") continue;
if (!isset($protectedfields[$key]) || !$protectedfields[$key])
{
$userinfo[$key] = (isset($_REQUEST["field_edit_" . md5($key)]) ? $_REQUEST["field_edit_" . md5($key)] : "");
}
}
if (function_exists("AdminHook_EditUser_PostFieldsCheck")) AdminHook_EditUser_PostFieldsCheck();
if ($sso_site_admin && isset($_REQUEST["impersonation"]))
{
if (!(int)$_REQUEST["impersonation"])
{
unset($userinfo["sso__impersonation"]);
unset($userinfo["sso__impersonation_key"]);
unset($userinfo["sso__impersonation_auto"]);
}
else
{
if (!isset($userinfo["sso__impersonation"]))
{
$userinfo["sso__impersonation"] = "1";
$userinfo["sso__impersonation_key"] = $sso_rng->GenerateString(64);
$userinfo["sso__impersonation_auto"] = "0";
}
if (isset($_REQUEST["reset_impersonation_key"]) && $_REQUEST["reset_impersonation_key"] == "yes") $userinfo["sso__impersonation_key"] = $sso_rng->GenerateString(64);
if (isset($_REQUEST["impersonation_auto"])) $userinfo["sso__impersonation_auto"] = (string)(int)$_REQUEST["impersonation_auto"];
}
}
$info2 = SSO_CreateEncryptedUserInfo($userinfo);
$sso_db->Query("UPDATE", array($sso_db_users, array(
"version" => (int)$_REQUEST["version"],
"info" => serialize($userinfo),
"info2" => $info2,
), "WHERE" => "id = ?"), $row->id);
if ($sso_site_admin && $_REQUEST["tag_id"] != "" && $_REQUEST["tag_reason"] != "")
{
try
{
$sso_db->Query("INSERT", array($sso_db_user_tags, array(
"user_id" => $row->id,
"tag_id" => (int)$_REQUEST["tag_id"],
"issuer_id" => $sso_user_id,
"reason" => $_REQUEST["tag_reason"],
"created" => CSDB::ConvertToDBTime(time()),
)));
}
catch (Exception $e)
{
// Don't do anything here. Just catch the database exception and let the code fall through.
// It should be nearly impossible to get here in the first place.
}
}
BB_RedirectPage("success", "Successfully updated the user.", array("action=edituser&id=" . $row->id . "&sec_t=" . BB_CreateSecurityToken("edituser")));
}
}
$lastipaddr = IPAddr::NormalizeIP($row->lastipaddr);
$lastipaddrid = $sso_db->GetOne("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "ipaddr = ?",
), $sso_db_ipcache, $lastipaddr["ipv6"]);
$desc = "<br />" . implode(" | ", (isset($sso_providers[$sso_provider]) ? $sso_providers[$sso_provider]->GetEditUserLinks($row->provider_id) : array()));
$contentopts = array(
"desc" => "Edit the user.",
"htmldesc" => $desc,
"nonce" => "action",
"hidden" => array(
"action" => "edituser",
"id" => $row->id
),
"fields" => array(
"startrow",
array(
"title" => "User ID",
"type" => "static",
"value" => $row->id
),
array(
"title" => "Provider Name",
"type" => "static",
"value" => (isset($sso_providers[$sso_provider]) ? $sso_providers[$sso_provider]->DisplayName() : $sso_provider)
),
array(
"title" => "Provider ID",
"type" => "static",
"value" => $row->provider_id
),
"startrow",
array(
"title" => "Account Version",
"type" => "text",
"width" => "15em",
"name" => "version",
"value" => BB_GetValue("version", $row->version)
),
array(
"title" => "Last IP Address",
"type" => "custom",
"value" => ($lastipaddrid ? "<a href=\"" . BB_GetRequestURLBase() . "?action=viewipaddr&id=" . htmlspecialchars($lastipaddrid) . "&sec_t=" . BB_CreateSecurityToken("viewipaddr") . "\">" : "") . htmlspecialchars($lastipaddr["ipv4"] != "" ? $lastipaddr["ipv4"] : $lastipaddr["shortipv6"]) . ($lastipaddrid ? "</a>" : "")
),
array(
"title" => "Last Activated",
"type" => "static",
"width" => "15em",
"value" => BB_FormatTimestamp("M j, Y @ g:i A", CSDB::ConvertFromDBTime($row->lastactivated))
),
"endrow",
),
"submit" => "Save",
"focus" => true
);
foreach ($fields as $key => $fieldinfo)
{
if (substr($key, 0, 5) == "sso__") continue;
if (isset($protectedfields[$key]) && $protectedfields[$key])
{
$contentopts["fields"][] = array(
"title" => "Field - '" . $key . "'",
"type" => "custom",
"value" => "<div class=\"static\">" . (isset($userinfo[$key]) ? htmlspecialchars($userinfo[$key]) : "<i>" . htmlspecialchars(BB_Translate("Undefined")) . "</i>") . "</div>"
);
}
else
{
$contentopts["fields"][] = array(
"title" => "Field - '" . $key . "'",
"type" => (isset($userinfo[$key]) && strpos($userinfo[$key], "\n") !== false ? "textarea" : "text"),
"name" => "field_edit_" . md5($key),
"value" => BB_GetValue("field_edit_" . md5($key), (isset($userinfo[$key]) ? $userinfo[$key] : "")),
"desc" => (isset($sso_select_fields[$key]) ? substr($sso_select_fields[$key], strlen($key) + 3) : "")
);
}
}
if (function_exists("AdminHook_EditUser_PostFields")) AdminHook_EditUser_PostFields();
if ($sso_site_admin)
{
if (!isset($userinfo["sso__impersonation"])) $userinfo["sso__impersonation"] = "0";
$contentopts["fields"][] = array(
"title" => "Allow User Impersonation?",
"type" => "select",
"name" => "impersonation",
"options" => array("No", "Yes"),
"select" => BB_GetValue("impersonation", (string)(int)$userinfo["sso__impersonation"])
);
if ((int)$userinfo["sso__impersonation"])
{
$contentopts["fields"][] = array(
"title" => "Impersonation Key",
"type" => "custom",
"value" => "<div class=\"textareawrap\"><textarea class=\"text\" style=\"background-color: #EEEEEE;\" rows=\"3\" readonly>" . htmlspecialchars($userinfo["sso__impersonation_key"] . "-" . $row->id) . "</textarea></div>",
"htmldesc" => "<input type=\"checkbox\" id=\"reset_impersonation_key\" name=\"reset_impersonation_key\" value=\"yes\" /> <label for=\"reset_impersonation_key\">" . BB_Translate("Generate new impersonation key") . "</label>"
);
$contentopts["fields"][] = array(
"title" => "Automate User Impersonation Sign In?",
"type" => "select",
"name" => "impersonation_auto",
"options" => array("No", "Yes"),
"select" => BB_GetValue("impersonation_auto", (string)(int)$userinfo["sso__impersonation_auto"])
);
}
$contentopts["fields"][] = "split";
$rows = array();
$result = $sso_db->Query("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "user_id = ?",
"ORDER BY" => "created"
), $sso_db_user_tags, $row->id);
while ($row2 = $result->NextRow())
{
if (isset($tags[$row2->tag_id]))
{
$rows[] = array(htmlspecialchars($tags[$row2->tag_id]), BB_FormatTimestamp("M j, Y", CSDB::ConvertFromDBTime($row2->created)), ($row2->issuer_id !== "0" ? "<a href=\"" . BB_GetRequestURLBase() . "?action=edituser&id=" . $row2->issuer_id . "&sec_t=" . BB_CreateSecurityToken("edituser") . "\">" . $row2->issuer_id . "</a>" : 0), htmlspecialchars($row2->reason), "<a href=\"" . BB_GetRequestURLBase() . "?action=deleteusertag&id=" . $row2->user_id . "&id2=" . $row2->tag_id . "&sec_t=" . BB_CreateSecurityToken("deleteusertag") . "\" onclick=\"return confirm('" . htmlspecialchars(BB_JSSafe(BB_Translate("Are you sure you want to remove the tag '%s' from this user?", $tags[$row2->tag_id]))) . "');\">" . BB_Translate("Delete") . "</a>");
unset($tags[$row2->tag_id]);
}
}
if (count($rows))
{
$contentopts["fields"][] = array(
"title" => "User Tags",
"type" => "table",
"cols" => array("Tag Name", "Issued On", "Issued By", "Reason", "Options"),
"rows" => $rows
);
}
$contentopts["fields"][] = "startrow";
$contentopts["fields"][] = array(
"title" => "Add Tag",
"type" => "select",
"width" => "15em",
"name" => "tag_id",
"options" => $tags,
"select" => BB_GetValue("tag_id", array()),
);
$contentopts["fields"][] = array(
"title" => "Reason",
"type" => "text",
"width" => "33em",
"name" => "tag_reason",
"value" => BB_GetValue("tag_reason", ""),
);
$contentopts["fields"][] = "endrow";
}
BB_GeneratePage("Edit User", $sso_menuopts, $contentopts);
}
}
else if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "finduser")
{
if (isset($_REQUEST["opts"]))
{
if (BB_GetPageMessageType() != "error")
{
$sqlfrom = array("? AS u");
$sqlwhere = array();
$sqlvars = array($sso_db_users);
$optdesc = array();
// Extract special prefixes.
$specialopts = array();
$opts = $_REQUEST["opts"];
do
{
$found = false;
$pos = strpos($opts, ":");
if ($pos !== false)
{
$key = substr($opts, 0, $pos);
if ($key === "id" || $key === "provider_name" || $key === "provider_id" || $key === "version" || $key === "lastipaddr" || $key === "tag_name")
{
$opts = trim(substr($opts, $pos + 1));
if (strlen($opts))
{
if ($opts[0] === "\"")
{
$opts = substr($opts, 1);
$pos = strpos($opts, $chr);
if ($pos === false)
{
$pos = strlen($opts);
$opts .= "\"";
}
$val = substr($opts, 0, $pos);
$opts = trim(substr($opts, $pos + 1));
}
else
{
$pos = strpos($opts, " ");
if ($pos === false) $pos = strlen($opts);
$val = substr($opts, 0, $pos);
$opts = trim(substr($opts, $pos));
}
if ($val !== "") $specialopts[$key] = $val;
$found = true;
}
}
}
} while ($found);
$tagname = false;
if (isset($specialopts["tag_name"]))
{
$tags = array();
$row = $sso_db->GetRow("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "enabled = 1 AND tag_name = ?"
), $sso_db_tags, $specialopts["tag_name"]);
if ($row && ($row->tag_name != SSO_SITE_ADMIN_TAG || $sso_site_admin))
{
$sqlfrom[] = "? AS ut";
$sqlwhere[] = "u.id = ut.user_id";
$sqlwhere[] = "ut.tag_id = ?";
$sqlvars[] = $sso_db_user_tags;
$sqlvars[] = $row->id;
$tagname = $row->tag_name;
}
}
if (isset($specialopts["id"]))
{
$sqlwhere[] = "u.id = ?";
$sqlvars[] = $specialopts["id"];
$optdesc[] = htmlspecialchars(BB_Translate("User ID: %s", $specialopts["id"]));
}
if (isset($specialopts["provider_name"]) && isset($sso_providers[$specialopts["provider_name"]]))
{
$sqlwhere[] = "u.provider_name = ?";
$sqlvars[] = $specialopts["provider_name"];
$optdesc[] = htmlspecialchars(BB_Translate("Provider Name: %s", $sso_providers[$specialopts["provider_name"]]->DisplayName()));
}
if (isset($specialopts["provider_id"]))
{
$sqlwhere[] = "u.provider_id = ?";
$sqlvars[] = $specialopts["provider_id"];
$optdesc[] = htmlspecialchars(BB_Translate("Provider ID: %s", $specialopts["provider_id"]));
}
if (isset($specialopts["version"]) && (int)$specialopts["version"] >= 0)
{
$sqlwhere[] = "u.version = ?";
$sqlvars[] = (int)$specialopts["version"];
$optdesc[] = htmlspecialchars(BB_Translate("Version: %s", (int)$specialopts["version"]));
}
if (isset($specialopts["lastipaddr"]))
{
$sqlwhere[] = "u.lastipaddr = ?";
$ipaddr = IPAddr::NormalizeIP($specialopts["lastipaddr"]);
$sqlvars[] = $ipaddr["ipv6"];
$optdesc[] = htmlspecialchars(BB_Translate("IP Address: %s", $ipaddr["ipv6"] . ($ipaddr["ipv4"] != "" ? " (" . $ipaddr["ipv4"] . ")" : "")));
}
if ($tagname !== false) $optdesc[] = htmlspecialchars(BB_Translate("Tag: %s", $tagname));
if ($opts === "") $opts = array();
else $opts = explode(" ", preg_replace('/\s+/', " ", $opts));
if (count($opts))
{
foreach ($opts as $opt)
{
$sqlwhere[] = "u.info LIKE ?";
$sqlvars[] = "%" . $opt . "%";
}
$optdesc[] = htmlspecialchars(BB_Translate("Unencrypted fields contain: %s", implode(", ", $opts)));
}
if (!count($optdesc)) $optdesc[] = BB_Translate("Latest Accounts");
$desc = "<ul><li>" . implode("</li><li>", $optdesc) . "</li></ul>";
SSO_LoadFieldSearchOrder();
$rows = array();
$sqlopts = array(
"u.*",
"FROM" => implode(", ", $sqlfrom),
"LIMIT" => "300"
);
if (count($sqlwhere)) $sqlopts["WHERE"] = implode(" AND ", $sqlwhere);
else $sqlopts["ORDER BY"] = "u.id DESC";
$result = $sso_db->Query("SELECT", $sqlopts, $sqlvars);
while ($row = $result->NextRow())
{
$userinfo = SSO_LoadDecryptedUserInfo($row);
$user = "";
foreach ($sso_settings[""]["search_order"] as $key => $display)
{
$desc2 = false;
$val = false;
if ($key === "id")
{
$desc2 = "Account ID";
$val = $row->id;
}
else if ($key === "provider_name")
{
$desc2 = "Provider Name";
$val = $row->provider_name . (isset($sso_providers[$row->provider_name]) ? " - " . $sso_providers[$row->provider_name]->DisplayName() : "");
}
else if ($key === "provider_id")
{
$desc2 = "Provider ID";
$val = $row->provider_id;
}
else if ($key === "version")
{
$desc2 = "Account Version";
$val = $row->version;
}
else if ($key === "lastipaddr")
{
$desc2 = "Last IP Address";
$ipaddr = IPAddr::NormalizeIP($row->lastipaddr);
$val = $ipaddr["ipv6"] . ($ipaddr["ipv4"] != "" ? " (" . $ipaddr["ipv4"] . ")" : "");
}
else if ($key === "lastactivated")
{
$desc2 = "Last Activated";
$val = BB_FormatTimestamp("M j, Y @ g:i A", CSDB::ConvertFromDBTime($row->lastactivated));
}
else if ($key === "tag_id")
{
$desc2 = "Tag";
$val = $tagname;
}
else if (substr($key, 0, 6) === "field_")
{
if (isset($sso_fields[substr($key, 6)]))
{
$desc2 = substr($key, 6);
$val = (isset($userinfo[$desc2]) ? $userinfo[$desc2] : "");
}
}
if ($desc2 !== false && $val !== false)
{
$val = htmlspecialchars($val);
$found = false;
foreach ($opts as $opt)
{
if (stripos($val, $opt) !== false)
{
$val = str_ireplace($opt, "<b>" . $opt . "</b>", $val);
$found = true;
}
}
if (($display && $val != "") || $found)
{
$user .= "<div class=\"search_field\"><div class=\"search_field_key\">" . htmlspecialchars($desc2) . "</div><div class=\"search_field_val\">" . $val . "</div></div>";
}
}
}
$rows[] = array($user, "<a href=\"" . BB_GetRequestURLBase() . "?action=edituser&id=" . htmlspecialchars($row->id) . "&sec_t=" . BB_CreateSecurityToken("edituser") . "\">Edit</a>");
}
ob_start();
?>
<style type="text/css">
div.formfields div.formitem table div.search_field {
float: left;
border: 1px solid #CCCCCC;
margin: 0.5em 1.0em 0.5em 0;
}
div.formfields div.formitem table div.search_field_key {
float: left;
padding: 0.2em 0.5em;
background-color: #EEEEEE;
border-right: 1px solid #CCCCCC;
}
div.formfields div.formitem table div.search_field_val {
float: left;
padding: 0.2em 0.5em;
}
div.formfields div.formitem table div.search_field_val b {
background-color: #FFFBCC;
}
</style>
<?php
$desc .= ob_get_contents();
ob_end_clean();
$contentopts = array(
"desc" => "Search results for users with the following search options:",
"htmldesc" => $desc,
"fields" => array(
array(
"type" => "table",
"cols" => array("User", "Options"),
"rows" => $rows
)
)
);
// Let providers add their own search results.
foreach ($sso_providers as $sso_provider => &$instance)
{
$instance->FindUsers();
}
if (count($contentopts["fields"]) > 1) $contentopts["fields"][0]["title"] = "SSO Server";
BB_GeneratePage("Search Results", $sso_menuopts, $contentopts);
exit();
}
}
$encryptedfields = array();
foreach ($sso_fields as $key => $encrypted)
{
if ($encrypted) $encryptedfields[] = $key;
}
$contentopts = array(
"desc" => "Find a user.",
"nonce" => "action",
"hidden" => array(
"action" => "finduser",
),
"fields" => array(
array(
"title" => "Search Terms",
"type" => "text",
"name" => "opts",
"value" => BB_GetValue("opts", ""),
"desc" => "Runs an AND query for the specified search terms across all non-encrypted fields. Leave blank for the 300 most recent users. Some providers may also run searches."
),
(count($encryptedfields) ? array(
"title" => "Encrypted Fields",
"type" => "static",
"value" => htmlspecialchars(implode(", ", $encryptedfields)),
) : ""),
array(
"title" => "Special Prefixes",
"type" => "static",
"value" => "id, provider_name, provider_id, version, lastipaddr, tag_name",
"desc" => "If Search Terms starts with one of these prefixes, a colon, and a value, an exact match will be made. Prefixes must appear before other search terms. (e.g. id:5 would retrieve the user account with ID 5.)"
),
array(
"title" => "Internal Provider Names",
"type" => "static",
"value" => htmlspecialchars(implode(", ", array_keys($sso_providers))),
"desc" => "When using the 'provider_name' prefix above, it expects the internal provider name to be used."
),
),
"submit" => "Search",
"focus" => true
);
BB_GeneratePage("Find User", $sso_menuopts, $contentopts);
}
else if ($sso_site_admin && isset($_REQUEST["action"]) && $_REQUEST["action"] == "addfield")
{
if (isset($_REQUEST["name"]))
{
$_REQUEST["name"] = UTF8::MakeValid($_REQUEST["name"]);
if ($_REQUEST["name"] == "" || is_numeric($_REQUEST["name"])) BB_SetPageMessage("error", "Please fill in 'Field Name'.");
else if (substr($_REQUEST["name"], 0, 5) == "sso__") BB_SetPageMessage("error", "The 'Field Name' field contains a reserved prefix.");
else if ($sso_db->GetOne("SELECT", array("COUNT(*)", "FROM" => "?", "WHERE" => "field_name = ?"), $sso_db_fields, $_REQUEST["name"])) BB_SetPageMessage("error", "The Field Name '" . $_REQUEST["name"] . "' already exists.");
else if ($sso_db->GetOne("SELECT", array("COUNT(*)", "FROM" => "?", "WHERE" => "field_hash = ?"), $sso_db_fields, md5($_REQUEST["name"]))) BB_SetPageMessage("error", "The Field Name has a MD5 hash collision with another field.");
else if ($_REQUEST["desc"] == "") BB_SetPageMessage("error", "Please fill in 'Field Description'.");
if (BB_GetPageMessageType() != "error")
{
$sso_db->Query("INSERT", array($sso_db_fields, array(
"field_name" => $_REQUEST["name"],
"field_desc" => $_REQUEST["desc"],
"field_hash" => md5($_REQUEST["name"]),
"encrypted" => ((int)$_REQUEST["encrypt"] ? 1 : 0),
"enabled" => 1,
"created" => CSDB::ConvertToDBTime(time()),
)));
BB_RedirectPage("success", "Successfully created the field.", array("action=managefields&sec_t=" . BB_CreateSecurityToken("managefields")));
}
}
$contentopts = array(
"desc" => "Add a new field.",
"nonce" => "action",
"hidden" => array(
"action" => "addfield"
),
"fields" => array(
array(
"title" => "Field Name",
"type" => "text",
"name" => "name",
"value" => BB_GetValue("name", ""),
"desc" => "The name of the field to create. (e.g. 'first_name', 'email')"
),
array(
"title" => "Field Description",
"type" => "text",
"name" => "desc",
"value" => BB_GetValue("desc", ""),
"desc" => "A short description of this field and what it is for."
),
array(
"title" => "Encrypt Field?",
"type" => "select",
"name" => "encrypt",
"options" => array("No", "Yes"),
"select" => BB_GetValue("encrypt", "0"),
"desc" => "When enabled, data in this field will be encrypted and information can be viewed and edited but the field will not be searchable."
)
),
"submit" => "Create",
"focus" => true
);
BB_GeneratePage("Add Field", $sso_menuopts, $contentopts);
}
else if ($sso_site_admin && isset($_REQUEST["action"]) && $_REQUEST["action"] == "togglefield")
{
$row = $sso_db->GetRow("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "id = ?",
), $sso_db_fields, $_REQUEST["id"]);
if ($row)
{
if ($_REQUEST["type"] == "enabled")
{
$sso_db->Query("UPDATE", array($sso_db_fields, array(
"enabled" => ($row->enabled ? 0 : 1),
), "WHERE" => "id = ?"), $row->id);
BB_RedirectPage("success", "Successfully " . ($row->enabled ? "disabled" : "enabled") . " the field.", array("action=managefields&sec_t=" . BB_CreateSecurityToken("managefields")));
}
else if ($_REQUEST["type"] == "encrypted")
{
$sso_db->Query("UPDATE", array($sso_db_fields, array(
"encrypted" => ($row->encrypted ? 0 : 1),
), "WHERE" => "id = ?"), $row->id);
BB_RedirectPage("success", "Successfully " . ($row->encrypted ? "disabled" : "enabled") . " encryption for the field.", array("action=managefields&sec_t=" . BB_CreateSecurityToken("managefields")));
}
}
BB_RedirectPage("error", "Unable to find field.", array("action=managefields&sec_t=" . BB_CreateSecurityToken("managefields")));
}
else if ($sso_site_admin && isset($_REQUEST["action"]) && $_REQUEST["action"] == "deletefield")
{
if (isset($_REQUEST["id"])) $sso_db->Query("DELETE", array($sso_db_fields, "WHERE" => "id = ?"), $_REQUEST["id"]);
BB_RedirectPage("success", "Successfully deleted the field.", array("action=managefields&sec_t=" . BB_CreateSecurityToken("managefields")));
}
else if ($sso_site_admin && isset($_REQUEST["action"]) && $_REQUEST["action"] == "managefields")
{
$desc = "<br />";
$desc .= "<a href=\"" . BB_GetRequestURLBase() . "?action=addfield&sec_t=" . BB_CreateSecurityToken("addfield") . "\">Add Field</a>";
$rows = array();
$result = $sso_db->Query("SELECT", array(
"*",
"FROM" => "?",
"ORDER BY" => "field_name",
), $sso_db_fields);
while ($row = $result->NextRow())
{
$rows[] = array(htmlspecialchars($row->field_name), htmlspecialchars($row->field_desc), BB_Translate($row->enabled ? "Yes" : "No"), BB_Translate($row->encrypted ? "Yes" : "No"), "<a href=\"" . BB_GetRequestURLBase() . "?action=togglefield&id=" . $row->id . "&type=enabled&sec_t=" . BB_CreateSecurityToken("togglefield") . "\">" . htmlspecialchars(BB_Translate($row->enabled ? "Disable" : "Enable")) . "</a> | <a href=\"" . BB_GetRequestURLBase() . "?action=togglefield&id=" . $row->id . "&type=encrypted&sec_t=" . BB_CreateSecurityToken("togglefield") . "\" onclick=\"return confirm('" . htmlspecialchars(BB_JSSafe(BB_Translate("Toggling the encryption status of fields doesn't immediately affect existing data. Are you sure you want to toggle the encryption status of '%s'?", $row->field_name))) . "');\">" . htmlspecialchars(BB_Translate($row->encrypted ? "Decrypt" : "Encrypt")) . "</a> | <a href=\"" . BB_GetRequestURLBase() . "?action=deletefield&id=" . $row->id . "&sec_t=" . BB_CreateSecurityToken("deletefield") . "\" onclick=\"return confirm('" . htmlspecialchars(BB_JSSafe(BB_Translate("Deleting fields doesn't affect existing data but disabling is usually better. Are you sure you want to delete '%s'?", $row->field_name))) . "');\">" . htmlspecialchars(BB_Translate("Delete")) . "</a>");
}
$contentopts = array(
"desc" => "Manage user fields.",
"htmldesc" => $desc,
"fields" => array(
array(
"type" => "table",
"cols" => array("Field", "Description", "Enabled", "Encrypted", "Options"),
"rows" => $rows
)
)
);
BB_GeneratePage("Manage Fields", $sso_menuopts, $contentopts);
}
else if ($sso_site_admin && isset($_REQUEST["action"]) && $_REQUEST["action"] == "addtag")
{
if (isset($_REQUEST["name"]))
{
$_REQUEST["name"] = UTF8::MakeValid($_REQUEST["name"]);
if ($_REQUEST["name"] == "" || is_numeric($_REQUEST["name"])) BB_SetPageMessage("error", "Please fill in 'Tag Name'.");
else if ($sso_db->GetOne("SELECT", array("COUNT(*)", "FROM" => "?", "WHERE" => "tag_name = ?"), $sso_db_tags, $_REQUEST["name"])) BB_SetPageMessage("error", "The Tag Name '" . $_REQUEST["name"] . "' already exists.");
else if ($_REQUEST["desc"] == "") BB_SetPageMessage("error", "Please fill in 'Tag Description'.");
if (BB_GetPageMessageType() != "error")
{
$sso_db->Query("INSERT", array($sso_db_tags, array(
"tag_name" => $_REQUEST["name"],
"tag_desc" => $_REQUEST["desc"],
"enabled" => 1,
"created" => CSDB::ConvertToDBTime(time()),
)));
BB_RedirectPage("success", "Successfully created the tag.", array("action=managetags&sec_t=" . BB_CreateSecurityToken("managetags")));
}
}
$contentopts = array(
"desc" => "Add a new tag.",
"nonce" => "action",
"hidden" => array(
"action" => "addtag"
),
"fields" => array(
array(
"title" => "Tag Name",
"type" => "text",
"name" => "name",
"value" => BB_GetValue("name", ""),
"desc" => "The name of the tag to create. (e.g. 'forum_moderator', 'bb_developer')"
),
array(
"title" => "Tag Description",
"type" => "text",
"name" => "desc",
"value" => BB_GetValue("desc", ""),
"desc" => "A short description of this tag and what it is for."
)
),
"submit" => "Create",
"focus" => true
);
BB_GeneratePage("Add Tag", $sso_menuopts, $contentopts);
}
else if ($sso_site_admin && isset($_REQUEST["action"]) && $_REQUEST["action"] == "toggletag")
{
$row = $sso_db->GetRow("SELECT", array(
"*",
"FROM" => "?",
"WHERE" => "id = ?",
), $sso_db_tags, $_REQUEST["id"]);
if ($row)
{
if ($row->tag_name != SSO_SITE_ADMIN_TAG && $row->tag_name != SSO_ADMIN_TAG && $row->tag_name != SSO_LOCKED_TAG)
{
$sso_db->Query("UPDATE", array($sso_db_tags, array(
"enabled" => ($row->enabled ? 0 : 1),
), "WHERE" => "id = ?"), $row->id);
}
BB_RedirectPage("success", "Successfully " . ($row->enabled ? "disabled" : "enabled") . " the tag.", array("action=managetags&sec_t=" . BB_CreateSecurityToken("managetags")));
}
BB_RedirectPage("error", "Unable to find tag.", array("action=managetags&sec_t=" . BB_CreateSecurityToken("managetags")));