-
Notifications
You must be signed in to change notification settings - Fork 1
/
handle-admin.php
117 lines (94 loc) · 3.52 KB
/
handle-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
<?php
try {
require_once('Sprinkles.php');
require_once('admin-fields.php');
$hexcolor_regex = "/^\s*#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$\s*/";
$nonspecial_regex = '[A-Za-z0-9_~`!%^\'#*&$\/|=+-]';
$nonspecial_or_dot_regex = '[A-Za-z0-9_~`,!%^\'#*&$\/|.=+-]';
$word_regex = "[A-Za-z]($nonspecial_regex+[A-Za-z0-9])?";
# $email_regex = "/[A-Za-z0-9.-]+@[A-Za-z0-9.-]+/";
$email_regex = "/$nonspecial_or_dot_regex*@$word_regex(\.$word_regex)*/";
# c.f. RFC 821 p. 30. Doesn't handle quoted local-parts, #number domain
# segments or dotted-quad domain segments.
## Validation | FIXME: not finished
$bad_fields = array();
$contact_email = request_param('contact_email');
if (preg_match($email_regex, $contact_email)) {
$contact_email = trim($contact_email);
} else {
array_push($bad_fields, 'contact_email');
}
$contact_phone = request_param('contact_phone');
$contact_address = request_param('contact_address');
# TBD: additional links
$sprink = new Sprinkles(); # TBD: this is expensive; cheapen!
$active_username = $sprink->current_username();
if (!$active_username) {
redirect($sprink->authorize_url('admin.php', false)); exit(0);
}
$existing_admin_users = $sprink->get_users();
if (!$sprink->user_is_admin()) {
redirect('error.php'); exit(0);
}
sort($existing_admin_users);
$admin_users_str = request_param('admin_users_str');
$admin_users = preg_split('/,\s*|\s+/', $admin_users_str);
if (!member($active_username, $admin_users))
array_push($bad_fields, 'admin_users_str');
else {
$admin_users = array_filter($admin_users);
$existing_admin_usernames = array();
foreach ($existing_admin_users as $u)
array_push($existing_admin_usernames, $u['username']);
$new_admins = array_diff($admin_users, $existing_admin_usernames);
}
# TBD: Validate site links?
if (!$bad_fields) {
## Save the settings
# Save the site links.
// $urls = request_param('link_url');
// $texts = request_param('link_text');
// $links = array();
// foreach ($urls as $url) {
// $text = array_shift($texts);
// if ($url || $text) {
// array_push($links, array('url' => $url, 'text' => $text));
// }
// }
// $sprink->set_site_links($links);
$sql = 'update site_settings set ' .
'background_color = \'' . mysql_real_escape_string($background_color). '\', ' .
'contact_email = \'' . mysql_real_escape_string($contact_email). '\', ' .
'contact_address = \'' . mysql_real_escape_string($contact_address). '\', ' .
'map_url = \'' . mysql_real_escape_string($map_url). '\', ' .
'contact_phone = \'' . mysql_real_escape_string($contact_phone).'\', ' .
'configured = \'Y\', ' .
'logo_link = \'' . mysql_real_escape_string($logo_link) . '\' ' .
($logo_data ?
', logo_data = \'' . mysql_real_escape_string($logo_data) . '\'' : '')
;
$result = mysql_query($sql);
if (!$result) { print mysql_error(); return; }
$sprink->set_admin_users($admin_users);
$params = '';
if ($new_admins) {
$params .= '&new_admins=' . join(',', $new_admins);
}
redirect('admin.php?settings_saved=true' . $params);
exit(0);
} else {
foreach ($bad_fields as $field) {
$params .= '&invalid[' . $field . ']=true';
}
foreach ($fields as $field) {
$params .= '&' . $field . '=' . urlencode(request_param($field));
}
$params .= '&admin_users=' . urlencode($admin_users_str);
redirect('admin.php?errors=true' . $params);
exit(0);
}
} catch (Exception $e) {
error_log("Exception thrown while preparing page: " . $e->getMessage());
$smarty->display('error.t');
}
?>