forked from phpLicenseWatcher/phpLicenseWatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers_admin.php
248 lines (222 loc) · 9.04 KB
/
servers_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
<?php
require_once __DIR__ . "/common.php";
require_once __DIR__ . "/tools.php";
require_once __DIR__ . "/html_table.php";
require_once __DIR__ . "/lmtools.php";
require_once __DIR__ . "/servers_admin_db.php";
switch(true) {
case isset($_POST['submit_id']):
$msg = db_process();
main_form($msg);
break;
case isset($_POST['edit_id']):
edit_form();
break;
case isset($_POST['delete_id']):
$msg = db_delete_server();
main_form($msg);
break;
case isset($_POST['export_servers']) && $_POST['export_servers'] === "1":
$json = db_get_servers_json();
ajax_send_data($json, "application/json"); // q.v. common.php
break;
case isset($_FILES['server_import']):
$json = validate_uploaded_json();
if ($json === false) {
$msg = array('msg' => "Invalid server JSON.", 'lvl' => "failure");
} else {
$msg = db_import_servers_json($json);
}
main_form($msg);
break;
case isset($_POST['cancel']):
default:
main_form();
}
exit;
/**
* Display server list and controls to add or edit a server.
*
* @param string $response Print any error/success messages from a add or edit.
*/
function main_form($alert=null) {
db_connect($db);
$server_list = db_get_servers($db, array(), array(), "label", false);
$db->close();
$table = new html_table(array('class' => "table alt-rows-bgcolor"));
$headers = array("Name", "Label", "Licensing", "Is Active", "Status", "Server Version", "Last Updated");
$table->add_row($headers, array(), "th");
// Don't display "no servers polled" notice when there are no servers in DB.
// Otherwise, assume all servers aren't polled until shown otherwise.
$display_notice = count($server_list) > 0 ? true : false;
foreach($server_list as $i => $server) {
$row = array(
$server['name'],
$server['label'],
ucwords($server['license_manager']),
$server['is_active'] ? "True" : "False",
$server['status'],
$server['version'],
date_format(date_create($server['last_updated']), "m/d/Y h:ia"),
"<button type='submit' form='server_list' name='edit_id' class='btn btn-link' value='{$server['id']}' aria-label='edit {$server['name']}'>EDIT</button>"
);
$table->add_row($row);
$last_row = $table->get_rows_count() - 1;
$table->update_cell($last_row, 0, null, null, "th");
switch($server['status']) {
case null:
$table->update_cell($last_row, 4, array('class'=>"info"), "Not Polled");
break;
case SERVER_UP:
// No table cell update.
$display_notice = false;
break;
case SERVER_VENDOR_DOWN:
$table->update_cell($last_row, 4, array('class'=>"warning"));
break;
case SERVER_DOWN:
default:
$table->update_cell($last_row, 4, array('class'=>"danger"));
break;
}
}
// Get alert HTML based on message/properties.
switch(true) {
case is_string($alert):
$alert_html = get_alert_html($alert);
break;
case isset($alert['msg']) && isset($alert['lvl']):
$alert_html = get_alert_html($alert['msg'], $alert['lvl']);
break;
case is_null($alert):
default:
$alert_html = "";
break;
}
if ($display_notice) {
$alert_html .= get_not_polled_notice();
}
// Control Panel
$control_panel_html = <<<HTML
<div id='control_panel'>
<button type='submit' form='server_list' name='edit_id' class='btn servers-control-panel' value='new'>New Server</button>
<button type='button' id='export' class='btn servers-control-panel'>Export Servers</button>
<button type='button' id='import' class='btn servers-control-panel'>Import Servers</button>
<form method="post" action="" enctype="multipart/form-data" id='upload-form' class='inline-block'>
<input type='file' accept='application/json' id='upload' name='server_import' class='servers-control-panel'>
</form>
</div>
HTML;
// Print view.
print_header();
print <<<HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="servers_admin_jquery.js"></script>
<h1>Server Administration</h1>
<p>You may edit an existing server's name, label, active status, or add a new server to the database.<br>
Server names must be unique and in the form of <code>port@domain.tld</code>, <code>port@hostname</code>, or <code>port@ipv4</code>. Port is optional, but must unprivileged.
{$alert_html}
{$control_panel_html}
<form id='server_list' action='servers_admin.php' method='POST'>
{$table->get_html()}
</form>
HTML;
print_footer();
} // END function main_form()
/** Add/Edit server form. No DB operations. */
function edit_form() {
$id = $_POST['edit_id'];
// Validate adding a new server or editing an existing server.
// Skip back to the main_form() if something is wrong.
$err_msg = array('msg' => "Validation failed when requesting form to edit an existing server.", 'lvl' => "failure");
switch(true) {
case ctype_digit($id):
$server_details = db_server_details_by_getid($id);
$delete_button = "<button type='button' class='btn edit-form' id='delete-button'>Remove</button>";
if ($server_details === false) {
main_form($err_msg);
return null;
}
break;
case $id === "new":
$server_details = array('name'=>"", 'label'=>"", 'is_active'=>"1");
$delete_button = "";
break;
default:
main_form($err_msg);
return null;
}
// get supported license managers
$lmtools = new lmtools();
$lm_supported = $lmtools->list_all_available();
// print view
$is_checked = $server_details['is_active'] === '1' ? " CHECKED" : "";
$server_select_box = build_select_box($lm_supported, array('name' => "license_manager", 'id' => "license_manager"), $server_details['license_manager']);
print_header();
print <<<HTML
<h1>Server Details</h1>
<form action='servers_admin.php' method='post' class='edit-form'>
<div class='edit-form block'>
<label for='name'>Name (format: <code>port@domain</code>, <code>port@host</code>, <code>port@ipv4</code>, port optional.)</label><br>
<input type='text' name='name' id='name' class='edit-form' value='{$server_details['name']}'>
</div><div class='edit-form block'>
<label for='label'>Label</label><br>
<input type='text' name='label' id='label' class='edit-form' value='{$server_details['label']}'>
</div><div class='edit-form block'>
<label for='license_manager'>Server Type:</label>
{$server_select_box}
</div><div class='edit-form inline-block'>
<label for='is_active'>Is Active?</label>
<input type='checkbox' name='is_active' id='is_active' class='edit-form'{$is_checked}>
</div><div class='edit-form inline-block float-right'>
<input type='hidden' id='delete-server'>
<button type='submit' class='btn btn-cancel edit-form' name='cancel' value='1'>Cancel</button>
<button type='submit' class='btn btn-primary edit-form' name='submit_id' value='{$id}'>Submit</button>
{$delete_button}
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$('#delete-button').click(function() {
if (confirm("Confirm removal for \\"{$server_details['name']}\\" ({$server_details['label']})\\n*** THIS WILL REMOVE USAGE HISTORY FOR EVERY FEATURE\\n*** THIS CANNOT BE UNDONE")) {
$('#delete-server').attr('name', "delete_id");
$('#delete-server').attr('value', {$id});
$('form').submit();
}
});
</script>
</form>
HTML;
print_footer();
} // END function edit_form()
function validate_uploaded_json() {
$tmp_name = $_FILES['server_import']['tmp_name'];
$type = $_FILES['server_import']['type'];
$error = $_FILES['server_import']['error'];
switch(false) {
case isset($tmp_name) && is_uploaded_file($tmp_name):
case isset($type) && $type === "application/json":
case isset($error) && $error === 0:
return false;
}
$file = file_get_contents($tmp_name);
if ($file === false) {
return false;
}
$json = json_decode($file, true);
if (is_null($json)) {
return false;
}
foreach ($json as $row) {
// validate_server_name() is defined in servers_admin_db.php
switch (false) {
case is_array($row):
case array_key_exists('license_manager', $row):
case array_key_exists('name', $row) && validate_server_name($row['name']):
case array_key_exists('label', $row):
case array_key_exists('is_active', $row) && preg_match("/^[01]$/", $row['is_active']) === 1:
return false;
}
}
return $json;
} // END Function validate_uploaded_file()
?>