r2040 + cleaned object editor up, made opject updater a separate ophandler
[racktables] / inc / ophandlers.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of operation handlers for RackTables.
5*
6*/
7
17c32f4c
DO
8// This function assures that specified argument was passed
9// and is a number greater than zero.
2c6c7645 10function assertUIntArg ($argname, $caller = 'N/A', $allow_zero = FALSE)
17c32f4c
DO
11{
12 if (!isset ($_REQUEST[$argname]))
13 {
2c6c7645 14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
2c6c7645 19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
2c6c7645 24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
2c6c7645 29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
30 die();
31 }
32}
33
34// This function assures that specified argument was passed
35// and is a non-empty string.
2c6c7645 36function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
37{
38 if (!isset ($_REQUEST[$argname]))
39 {
2c6c7645 40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
2c6c7645 45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
2c6c7645 50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
51 die();
52 }
53}
54
2c6c7645 55function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
56{
57 if (!isset ($_REQUEST[$argname]))
58 {
2c6c7645 59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
2c6c7645 64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
2c6c7645 69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
70 die();
71 }
72}
73
2c6c7645 74function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c 75{
2c6c7645 76 assertStringArg ($argname, $caller, $ok_if_empty);
17c32f4c
DO
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
2c6c7645 79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "' (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
80 die();
81 }
82}
83
e673ee24
DO
84function addPortForwarding ()
85{
86 global $root, $pageno, $tabno;
87
2c6c7645
DO
88 assertUIntArg ('object_id', __FUNCTION__);
89 assertIPv4Arg ('localip', __FUNCTION__);
90 assertIPv4Arg ('remoteip', __FUNCTION__);
91 assertUIntArg ('localport', __FUNCTION__);
9bea3a8b 92 assertStringArg ('proto', __FUNCTION__);
2c6c7645 93 assertStringArg ('description', __FUNCTION__, TRUE);
e673ee24 94 $object_id = $_REQUEST['object_id'];
e673ee24 95 $remoteport = $_REQUEST['remoteport'];
31fc4157 96 if (empty ($remoteport))
f75eb878 97 $remoteport = $_REQUEST['localport'];
e673ee24
DO
98
99 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
100
9bea3a8b
DO
101 $error = newPortForwarding
102 (
103 $object_id,
104 $_REQUEST['localip'],
105 $_REQUEST['localport'],
106 $_REQUEST['remoteip'],
107 $remoteport,
108 $_REQUEST['proto'],
109 $_REQUEST['description']
110 );
e673ee24
DO
111
112 if ($error == '')
113 return "${retpage}&message=".urlencode('Port forwarding successfully added.');
114 else
115 {
116 return "${retpage}&error=".urlencode($error);
117 }
118
119}
120
121function delPortForwarding ()
122{
123 global $root, $pageno, $tabno;
124
125 $object_id = $_REQUEST['object_id'];
126 $localip = $_REQUEST['localip'];
127 $remoteip = $_REQUEST['remoteip'];
128 $localport = $_REQUEST['localport'];
129 $remoteport = $_REQUEST['remoteport'];
130 $proto = $_REQUEST['proto'];
e673ee24
DO
131
132 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
133
134 $error=deletePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto);
135 if ($error == '')
136 return "${retpage}&message=".urlencode('Port forwarding successfully deleted.');
137 else
138 {
139 return "${retpage}&error=".urlencode($error);
140 }
141
142}
143
144function updPortForwarding ()
145{
146 global $root, $pageno, $tabno;
147
148 $object_id = $_REQUEST['object_id'];
149 $localip = $_REQUEST['localip'];
150 $remoteip = $_REQUEST['remoteip'];
151 $localport = $_REQUEST['localport'];
152 $remoteport = $_REQUEST['remoteport'];
153 $proto = $_REQUEST['proto'];
154 $description = $_REQUEST['description'];
155
156 $retpage="${root}?page=${pageno}&tab=${tabno}&object_id=$object_id";
157
158 $error=updatePortForwarding($object_id, $localip, $localport, $remoteip, $remoteport, $proto, $description);
159 if ($error == '')
160 return "${retpage}&message=".urlencode('Port forwarding successfully updated.');
161 else
162 {
163 return "${retpage}&error=".urlencode($error);
164 }
165
166}
167
e673ee24
DO
168function addPortForObject ()
169{
170 global $root, $pageno, $tabno;
171
172 $object_id = $_REQUEST['object_id'];
173 $port_name = $_REQUEST['port_name'];
174 $port_l2address = $_REQUEST['port_l2address'];
175 $port_label = $_REQUEST['port_label'];
176 $port_type_id = $_REQUEST['port_type_id'];
177
178
179 if ($port_name == '')
180 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
181 else
182 {
183 $error = commitAddPort ($object_id, $port_name, $port_type_id, $port_label, $port_l2address);
184 if ($error != '')
185 {
186 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
187 }
188 else
189 {
190 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name added successfully");
191 }
192 }
193
194}
195
196function editPortForObject ()
197{
198 global $root, $pageno, $tabno;
199
2c6c7645
DO
200 assertUIntArg ('port_id', __FUNCTION__);
201 assertUIntArg ('object_id', __FUNCTION__);
202 assertStringArg ('name', __FUNCTION__);
e673ee24
DO
203 $port_id = $_REQUEST['port_id'];
204 $object_id = $_REQUEST['object_id'];
205 $port_name = $_REQUEST['name'];
206 $port_l2address = $_REQUEST['l2address'];
207 $port_label = $_REQUEST['label'];
208 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
209 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
210 else
211 $port_rc = 'NULL';
212
213 if ($port_name == '')
214 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode('Port name cannot be empty');
215 else
216 {
217 $error = commitUpdatePort ($port_id, $port_name, $port_label, $port_l2address, $port_rc);
218 if ($error != '')
219 {
220 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
221 }
222 else
223 {
224 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name updated successfully");
225 }
226 }
227
228}
229
230function delPortFromObject ()
231{
232 global $root, $pageno, $tabno;
233
234 $port_id = $_REQUEST['port_id'];
235 $port_name = $_REQUEST['port_name'];
236 $object_id = $_REQUEST['object_id'];
237 $error = delObjectPort($port_id);
238
239 if ($error != '')
240 {
241 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
242 }
243 else
244 {
245 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name deleted successfully");
246 }
247}
248
249function linkPortForObject ()
250{
251 global $root, $pageno, $tabno;
252
253 $port_id = $_REQUEST['port_id'];
254 $remote_port_id = $_REQUEST['remote_port_id'];
255 $object_id = $_REQUEST['object_id'];
256 $port_name = $_REQUEST['port_name'];
257 $remote_port_name = $_REQUEST['remote_port_name'];
258 $remote_object_name = $_REQUEST['remote_object_name'];
259
260 $error = linkPorts($port_id, $remote_port_id);
261 if ($error != '')
262 {
263 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
264 }
265 else
266 {
267 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name successfully linked with port $remote_port_name at object $remote_object_name");
268 }
269}
270
271function unlinkPortForObject ()
272{
273 global $root, $pageno, $tabno;
274
275 $port_id = $_REQUEST['port_id'];
276 $object_id = $_REQUEST['object_id'];
277 $port_name = $_REQUEST['port_name'];
278 $remote_port_name = $_REQUEST['remote_port_name'];
279 $remote_object_name = $_REQUEST['remote_object_name'];
280
281 $error = unlinkPort($port_id);
282 if ($error != '')
283 {
284 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=".urlencode($error);
285 }
286 else
287 {
288 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=".urlencode("Port $port_name successfully unlinked from port $remote_port_name at object $remote_object_name");
289 }
290}
291
292function addMultiPorts ()
293{
294 global $root, $pageno, $tabno;
295 // Parse data.
2c6c7645
DO
296 assertStringArg ('format', __FUNCTION__);
297 assertStringArg ('input', __FUNCTION__);
298 assertUIntArg ('port_type', __FUNCTION__);
299 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
300 $format = $_REQUEST['format'];
301 $port_type = $_REQUEST['port_type'];
302 $object_id = $_REQUEST['object_id'];
303 // Input lines are escaped, so we have to explode and to chop by 2-char
304 // \n and \r respectively.
305 $lines1 = explode ('\n', $_REQUEST['input']);
306 foreach ($lines1 as $line)
307 {
308 $parts = explode ('\r', $line);
309 reset ($parts);
310 if (empty ($parts[0]))
311 continue;
312 else
313 $lines2[] = rtrim ($parts[0]);
314 }
315 $ports = array();
316 foreach ($lines2 as $line)
317 {
318 switch ($format)
319 {
320 case 'fisxii':
321 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
322 list ($slot, $port) = explode ('/', $words[0]);
323 $ports[] = array
324 (
325 'name' => "e ${slot}/${port}",
326 'l2address' => $words[8],
327 'label' => "slot ${slot} port ${port}"
328 );
329 break;
330 case 'c3600asy':
331 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
332/*
333How Async Lines are Numbered in Cisco 3600 Series Routers
334http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
335
336Understanding 16- and 32-Port Async Network Modules
337http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
338*/
339 $async = $words[0];
340 $slot = floor (($async - 1) / 32);
341 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
342 $cable = $async - $slot * 32 - $octalgroup * 8;
343 $og_label[0] = 'async 0-7';
344 $og_label[1] = 'async 8-15';
345 $og_label[2] = 'async 16-23';
346 $og_label[3] = 'async 24-31';
347 $ports[] = array
348 (
349 'name' => "async ${async}",
350 'l2address' => '',
351 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
352 );
353 break;
354 case 'fiwg':
355 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
356 $ifnumber = $words[0] * 1;
357 $ports[] = array
358 (
359 'name' => "e ${ifnumber}",
360 'l2address' => "${words[8]}",
361 'label' => "${ifnumber}"
362 );
363 break;
351d4dbf 364 case 'ssv1':
5b585342 365 $words = explode (' ', $line);
351d4dbf
DO
366 if (empty ($words[0]) or empty ($words[1]))
367 continue;
368 $ports[] = array
369 (
370 'name' => $words[0],
371 'l2address' => $words[1],
372 'label' => ''
373 );
374 break;
e673ee24
DO
375 default:
376 return
377 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" .
378 urlencode('Cannot process submitted data: unknown format code.');
379 break;
380 }
381 }
382 // Create ports, if they don't exist.
383 $added_count = $updated_count = $error_count = 0;
384 foreach ($ports as $port)
385 {
386 $port_id = getPortID ($object_id, $port['name']);
387 if ($port_id === NULL)
388 {
389 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
390 if ($result == '')
391 $added_count++;
392 else
393 $error_count++;
394 }
395 else
396 {
397 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
398 if ($result == '')
399 $updated_count++;
400 else
401 $error_count++;
402 }
403 }
404 return
405 "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" .
406 urlencode("Added ${added_count} ports, updated ${updated_count} ports, encountered ${error_count} errors.");
407}
408
b4c1ef87 409function updIPv4Allocation ()
e673ee24 410{
da958e52 411 global $pageno, $tabno;
b4c1ef87
DO
412 assertIPv4Arg ('ip', __FUNCTION__);
413 assertUIntArg ('object_id', __FUNCTION__);
414 assertStringArg ('bond_name', __FUNCTION__, TRUE);
415 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 416
b4c1ef87 417 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 418 if ($error != '')
46f92ff7 419 return buildRedirectURL_ERR ($error);
e673ee24 420 else
46f92ff7 421 return buildRedirectURL_OK ('allocation updated');
e673ee24
DO
422}
423
b4c1ef87 424function delIPv4Allocation ()
e673ee24 425{
da958e52 426 global $pageno, $tabno;
b4c1ef87
DO
427 assertIPv4Arg ('ip', __FUNCTION__);
428 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 429
b4c1ef87 430 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 431 if ($error != '')
46f92ff7 432 return buildRedirectURL_ERR ($error);
e673ee24 433 else
46f92ff7 434 return buildRedirectURL_OK ('deallocated');
e673ee24
DO
435}
436
b4c1ef87 437function addIPv4Allocation ()
e673ee24 438{
da958e52 439 global $pageno, $tabno;
b4c1ef87
DO
440 assertIPv4Arg ('ip', __FUNCTION__);
441 assertUIntArg ('object_id', __FUNCTION__);
442 assertStringArg ('bond_name', __FUNCTION__, TRUE);
443 assertStringArg ('bond_type', __FUNCTION__);
da958e52 444
b4c1ef87
DO
445 // Strip masklen.
446 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
447 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
448 $address = getIPAddress ($ip);
449 if ($address['exists'] and ($address['reserved'] == 'yes' or strlen ($address['name']) > 0))
450 {
451 $release = getConfigVar ('IPV4_AUTO_RELEASE');
452 if ($release >= 1)
453 $address['reserved'] = 'no';
454 if ($release >= 2)
455 $address['name'] = '';
456 updateAddress ($ip, $address['name'], $address['reserved']);
457 }
e673ee24 458 if ($error != '')
46f92ff7 459 return buildRedirectURL_ERR ($error);
e673ee24 460 else
46f92ff7 461 return buildRedirectURL_OK ('allocated');
e673ee24
DO
462}
463
42023f03 464function addIPv4Prefix ()
e673ee24
DO
465{
466 global $root, $pageno, $tabno;
2c6c7645
DO
467 assertStringArg ('range', __FUNCTION__);
468 assertStringArg ('name', __FUNCTION__);
e673ee24 469
fa05e3de
DO
470 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
471 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 472 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 473 if ($error != '')
42023f03 474 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
e673ee24 475 else
42023f03 476 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix successfully added");
e673ee24
DO
477}
478
42023f03 479function delIPv4Prefix ()
e673ee24
DO
480{
481 global $root, $pageno, $tabno;
482
da958e52 483 assertUIntArg ('id', __FUNCTION__);
42023f03 484 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 485 if ($error != '')
42023f03 486 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
e673ee24 487 else
42023f03 488 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix deleted");
e673ee24
DO
489}
490
42023f03 491function editRange ()
e673ee24 492{
da958e52
DO
493 global $pageno, $tabno;
494 assertUIntArg ('id', __FUNCTION__);
495 assertStringArg ('name', __FUNCTION__);
e673ee24 496
da958e52 497 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
e673ee24 498 if ($error != '')
46f92ff7 499 return buildRedirectURL_ERR ($error);
e673ee24 500 else
46f92ff7 501 return buildRedirectURL_OK ('IPv4 prefix updated');
e673ee24
DO
502}
503
504function editAddress ()
505{
da958e52
DO
506 global $pageno, $tabno;
507 assertIPv4Arg ('ip', __FUNCTION__);
508 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 509
e673ee24
DO
510 if (isset ($_REQUEST['reserved']))
511 $reserved = $_REQUEST['reserved'];
512 else
513 $reserved = 'off';
da958e52 514 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 515 if ($error != '')
46f92ff7 516 return buildRedirectURL_ERR ($error);
e673ee24 517 else
46f92ff7 518 return buildRedirectURL_OK ('IPv4 address updated');
e673ee24
DO
519}
520
cced6b7d 521function createUser ()
e673ee24
DO
522{
523 global $root, $pageno, $tabno;
2c6c7645
DO
524 assertStringArg ('username', __FUNCTION__);
525 assertStringArg ('realname', __FUNCTION__, TRUE);
526 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
527 $username = $_REQUEST['username'];
528 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
529 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
530 if ($result == TRUE)
531 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
532 else
533 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
534}
535
cced6b7d 536function updateUser ()
e673ee24
DO
537{
538 global $root, $pageno, $tabno;
cced6b7d 539 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
540 assertStringArg ('username', __FUNCTION__);
541 assertStringArg ('realname', __FUNCTION__, TRUE);
542 assertStringArg ('password', __FUNCTION__);
e673ee24 543 // We might be asked to change username, so use user ID only.
cced6b7d 544 $id = $_REQUEST['user_id'];
e673ee24
DO
545 $username = $_REQUEST['username'];
546 $new_password = $_REQUEST['password'];
547 $old_hash = getHashByID ($id);
548 if ($old_hash == NULL)
549 {
61e269b5 550 showError ('getHashByID() failed', __FUNCTION__);
e673ee24
DO
551 return;
552 }
553 // Update user password only if provided password is not the same as current password hash.
554 if ($new_password != $old_hash)
555 $new_password = hash (PASSWORD_HASH, $new_password);
cced6b7d 556 $result = commitUpdateUserAccount ($id, $username, $_REQUEST['realname'], $new_password);
e673ee24
DO
557 if ($result == TRUE)
558 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
559 else
560 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
561}
562
cced6b7d 563function enableUser ()
e673ee24
DO
564{
565 global $root, $pageno, $tabno;
cced6b7d
DO
566 assertUIntArg ('user_id', __FUNCTION__);
567 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
e673ee24
DO
568 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
569 else
570 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
571}
572
cced6b7d 573function disableUser ()
e673ee24
DO
574{
575 global $root, $pageno, $tabno;
cced6b7d
DO
576 assertUIntArg ('user_id', __FUNCTION__);
577 $id = $_REQUEST['user_id'];
e673ee24
DO
578 if ($id == 1)
579 $result = FALSE;
580 else
581 $result = commitEnableUserAccount ($id, 'no');
582 if ($result == TRUE)
583 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
584 else
585 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
586}
587
e673ee24
DO
588// This function find differences in users's submit and PortCompat table
589// and modifies database accordingly.
590function savePortMap ()
591{
592 global $root, $pageno, $tabno;
593 $ptlist = getPortTypes();
594 $oldCompat = getPortCompat();
595 $newCompat = array();
596 foreach (array_keys ($ptlist) as $leftKey)
597 foreach (array_keys ($ptlist) as $rightKey)
598 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
599 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
600 // Build the new matrix from user's submit and compare it to
601 // the old matrix. Those pairs which appear on
602 // new matrix only, have to be stored in PortCompat table. Those who appear
603 // on the old matrix only, should be removed from PortCompat table.
604 // Those present in both matrices should be skipped.
605 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
606 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
607 $error_count = $success_count = 0;
608 foreach (array_keys ($ptlist) as $type1)
609 foreach (array_keys ($ptlist) as $type2)
610 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
611 switch ($oldCompatTable[$type1][$type2])
612 {
613 case TRUE: // new value is FALSE
614 if (removePortCompat ($type1, $type2) === TRUE)
615 $success_count++;
616 else
617 $error_count++;
618 break;
619 case FALSE: // new value is TRUE
620 if (addPortCompat ($type1, $type2) === TRUE)
621 $success_count++;
622 else
623 $error_count++;
624 break;
625 default:
61e269b5 626 showError ('oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
627 break;
628 }
629 return
630 "${root}?page=${pageno}&tab=${tabno}&" .
631 ($error_count == 0 ? 'message' : 'error') .
632 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
633}
634
635function deleteDictWord ()
636{
637 global $root, $pageno, $tabno;
638 return
639 "${root}?page=${pageno}&tab=${tabno}&" .
640 "error=" . urlencode ('Dragon ate this word!');
641}
642
643function updateDictionary ()
644{
645 global $root, $pageno, $tabno;
2c6c7645
DO
646 assertUIntArg ('chapter_no', __FUNCTION__);
647 assertUIntArg ('dict_key', __FUNCTION__);
648 assertStringArg ('dict_value', __FUNCTION__);
e673ee24
DO
649 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
650 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
651 else
652 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
653}
654
655function supplementDictionary ()
656{
657 global $root, $pageno, $tabno;
2c6c7645
DO
658 assertUIntArg ('chapter_no', __FUNCTION__);
659 assertStringArg ('dict_value', __FUNCTION__);
e673ee24
DO
660 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
661 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
662 else
663 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
664}
665
666function reduceDictionary ()
667{
668 global $root, $pageno, $tabno;
2c6c7645
DO
669 assertUIntArg ('chapter_no', __FUNCTION__);
670 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24
DO
671 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
672 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
673 else
674 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
675}
676
677function addChapter ()
678{
679 global $root, $pageno, $tabno;
2c6c7645 680 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24
DO
681 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
682 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
683 else
684 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
685}
686
687function updateChapter ()
688{
689 global $root, $pageno, $tabno;
2c6c7645
DO
690 assertUIntArg ('chapter_no', __FUNCTION__);
691 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24
DO
692 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
693 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
694 else
695 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
696}
697
698function delChapter ()
699{
700 global $root, $pageno, $tabno;
2c6c7645 701 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24
DO
702 if (commitDeleteChapter ($_REQUEST['chapter_no']))
703 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
704 else
705 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
706}
707
708function changeAttribute ()
709{
710 global $root, $pageno, $tabno;
2c6c7645
DO
711 assertUIntArg ('attr_id', __FUNCTION__);
712 assertStringArg ('attr_name', __FUNCTION__);
e673ee24
DO
713 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
714 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
715 else
716 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
717}
718
719function createAttribute ()
720{
721 global $root, $pageno, $tabno;
2c6c7645
DO
722 assertStringArg ('attr_name', __FUNCTION__);
723 assertStringArg ('attr_type', __FUNCTION__);
e673ee24
DO
724 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
725 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
726 else
727 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
728}
729
730function deleteAttribute ()
731{
732 global $root, $pageno, $tabno;
2c6c7645 733 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24
DO
734 if (commitDeleteAttribute ($_REQUEST['attr_id']))
735 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
736 else
737 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
738}
739
740function supplementAttrMap ()
741{
742 global $root, $pageno, $tabno;
2c6c7645
DO
743 assertUIntArg ('attr_id', __FUNCTION__);
744 assertUIntArg ('objtype_id', __FUNCTION__);
745 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24
DO
746 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
747 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
748 else
749 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
750}
751
752function reduceAttrMap ()
753{
754 global $root, $pageno, $tabno;
2c6c7645
DO
755 assertUIntArg ('attr_id', __FUNCTION__);
756 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24
DO
757 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
758 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
759 else
760 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
761}
762
22bb04da 763function clearSticker ()
e673ee24 764{
2c6c7645
DO
765 assertUIntArg ('attr_id', __FUNCTION__);
766 assertUIntArg ('object_id', __FUNCTION__);
22bb04da
DO
767 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
768 return buildRedirectURL_OK ('Reset succeeded.');
e673ee24 769 else
22bb04da 770 return buildRedirectURL_ERR ('Reset failed!');
e673ee24
DO
771}
772
22bb04da
DO
773function updateObject ()
774{
775 assertUIntArg ('object_id', __FUNCTION__);
776 assertUIntArg ('object_type_id', __FUNCTION__);
777 assertStringArg ('object_name', __FUNCTION__, TRUE);
778 assertStringArg ('object_label', __FUNCTION__, TRUE);
779 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
780 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
781 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
782 $has_problems = 'yes';
783 else
784 $has_problems = 'no';
785
786 if (commitUpdateObject (
787 $_REQUEST['object_id'],
788 $_REQUEST['object_name'],
789 $_REQUEST['object_label'],
790 $_REQUEST['object_barcode'],
791 $_REQUEST['object_type_id'],
792 $has_problems,
793 $_REQUEST['object_asset_no'],
794 $_REQUEST['object_comment']
795 ) !== TRUE)
796 return buildRedirectURL_ERR ('commitUpdateObject() failed');
797 // Invalidate thumb cache of all racks objects could occupy.
798 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
799 resetThumbCache ($rack_id);
800 return buildRedirectURL_OK ('Update done');
801}
802
803function updateStickers ()
e673ee24
DO
804{
805 global $root, $pageno, $tabno;
2c6c7645 806 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 807 $object_id = $_REQUEST['object_id'];
0dfb8a2a
DO
808 $oldvalues = getAttrValues ($object_id);
809
2c6c7645 810 assertUIntArg ('num_attrs', __FUNCTION__);
0dfb8a2a
DO
811 $num_attrs = $_REQUEST['num_attrs'];
812 $result = array();
813
814 for ($i = 0; $i < $num_attrs; $i++)
815 {
2c6c7645 816 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
817 $attr_id = $_REQUEST["${i}_attr_id"];
818
819 // Field is empty, delete attribute and move on.
820 if (empty($_REQUEST["${i}_value"]))
821 {
822 commitResetAttrValue ($object_id, $attr_id);
823 continue;
824 }
825
826 // The value could be uint/float, but we don't know ATM. Let SQL
827 // server check this and complain.
2c6c7645 828 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
829 $value = $_REQUEST["${i}_value"];
830 switch ($oldvalues[$attr_id]['type'])
831 {
832 case 'uint':
833 case 'float':
834 case 'string':
835 $oldvalue = $oldvalues[$attr_id]['value'];
836 break;
837 case 'dict':
838 $oldvalue = $oldvalues[$attr_id]['key'];
839 break;
840 default:
61e269b5 841 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
842 die;
843 }
844 if ($value == $oldvalue)
845 continue;
846
847 // Note if the queries succeed or not, it determines which page they see.
848 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
849 }
850
851 if (in_array(false, $result))
e673ee24 852 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
0dfb8a2a
DO
853
854 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
e673ee24
DO
855}
856
857function useupPort ()
858{
859 global $root, $pageno, $tabno;
2c6c7645
DO
860 assertUIntArg ('port_id', __FUNCTION__);
861 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
862 $object_id = $_REQUEST['object_id'];
863 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
864 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
865 else
866 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
867}
868
4fe32e78
DY
869function updateUI ()
870{
c461c579 871 global $root, $pageno, $tabno;
4fe32e78
DY
872 $oldvalues = loadConfigCache();
873
2c6c7645 874 assertUIntArg ('num_vars', __FUNCTION__);
4fe32e78 875 $num_vars = $_REQUEST['num_vars'];
c461c579 876 $error = '';
4fe32e78
DY
877
878 for ($i = 0; $i < $num_vars; $i++)
879 {
2c6c7645
DO
880 assertStringArg ("${i}_varname", __FUNCTION__);
881 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 882 $varname = $_REQUEST["${i}_varname"];
4fe32e78 883 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 884
c461c579
DO
885 // If form value = value in DB, don't bother updating DB
886 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
887 continue;
888
889 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
890 $error = setConfigVar ($varname, $varvalue, TRUE);
891 if (!empty ($error))
892 break;
4fe32e78
DY
893 }
894
c461c579
DO
895 if ($error != '')
896 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
4fe32e78 897
c461c579 898 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
4fe32e78
DY
899}
900
b07f617c
DO
901function resetUIConfig()
902{
6c12ef9b 903 global $root, $pageno, $tabno;
1f86e996 904 setConfigVar ('default_port_type','24');
b07f617c
DO
905 setConfigVar ('MASSCOUNT','15');
906 setConfigVar ('MAXSELSIZE','30');
907 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
908 setConfigVar ('ROW_SCALE','2');
909 setConfigVar ('PORTS_PER_ROW','12');
910 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
911 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 912 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
913 setConfigVar ('USER_AUTH_SRC','database');
914 setConfigVar ('DEFAULT_SLB_VS_PORT','');
915 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 916 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 917 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 918 setConfigVar ('DETECT_URLS','no');
4b8d413e 919 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 920 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 921 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
922 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
923 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
924 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 925 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 926 setConfigVar ('IPV4_AUTO_RELEASE','1');
6c12ef9b 927 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
b07f617c
DO
928}
929
732e4578 930// Add single record.
ca461127
DO
931function addRealServer ()
932{
933 global $root, $pageno, $tabno;
934
2c6c7645
DO
935 assertUIntArg ('pool_id', __FUNCTION__);
936 assertIPv4Arg ('remoteip', __FUNCTION__);
937 assertUIntArg ('rsport', __FUNCTION__);
938 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
22a5b5ae 939 $pool_id = $_REQUEST['pool_id'];
2910c1d4 940 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
841a7a7a 941 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
d6517a21 942 else
841a7a7a 943 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully added");
d6517a21
DO
944}
945
732e4578
DO
946// Parse textarea submitted and try adding a real server for each line.
947function addRealServers ()
948{
949 global $root, $pageno, $tabno;
950
cbadbb38 951 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
952 assertStringArg ('format', __FUNCTION__);
953 assertStringArg ('rawtext', __FUNCTION__);
cbadbb38 954 $pool_id = $_REQUEST['pool_id'];
732e4578
DO
955 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
956 $ngood = $nbad = 0;
957 $rsconfig = '';
958 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
959 foreach (explode ('\n', $rawtext) as $line)
960 {
961 if (empty ($line))
962 continue;
963 $match = array ();
964 switch ($_REQUEST['format'])
965 {
966 case 'ipvs_2': // address and port only
967 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
968 continue;
7e7a8387 969 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
970 $ngood++;
971 else
972 $nbad++;
973 break;
974 case 'ipvs_3': // address, port and weight
975 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
976 continue;
7e7a8387 977 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
978 $ngood++;
979 else
980 $nbad++;
981 break;
e69c2aa3
DO
982 case 'ssv_2': // IP address and port
983 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
984 continue;
985 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
986 $ngood++;
987 else
988 $nbad++;
989 break;
732e4578
DO
990 default:
991 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__ . ': invalid format requested');
992 break;
993 }
994 }
995 if ($nbad == 0 and $ngood > 0)
841a7a7a 996 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
732e4578 997 else
841a7a7a 998 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
732e4578
DO
999}
1000
d6517a21
DO
1001function addVService ()
1002{
1003 global $root, $pageno, $tabno;
1004
2c6c7645
DO
1005 assertIPv4Arg ('vip', __FUNCTION__);
1006 assertUIntArg ('vport', __FUNCTION__);
1007 assertStringArg ('proto', __FUNCTION__);
d6517a21
DO
1008 $proto = $_REQUEST['proto'];
1009 if ($proto != 'TCP' and $proto != 'UDP')
1010 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
2c6c7645
DO
1011 assertStringArg ('name', __FUNCTION__, TRUE);
1012 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1013 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
d6517a21
DO
1014 $pool_id = $_REQUEST['id'];
1015 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1016 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
ca461127 1017 else
d6517a21 1018 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
fb1c4a54
DO
1019}
1020
1021function deleteRealServer ()
1022{
1023 global $root, $pageno, $tabno;
1024
2c6c7645
DO
1025 assertUIntArg ('pool_id', __FUNCTION__);
1026 assertUIntArg ('id', __FUNCTION__);
fb1c4a54 1027 $pool_id = $_REQUEST['pool_id'];
d6517a21 1028 if (!commitDeleteRS ($_REQUEST['id']))
841a7a7a 1029 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
fb1c4a54 1030 else
841a7a7a 1031 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
fb1c4a54
DO
1032}
1033
3241551e
DO
1034function deleteLoadBalancer ()
1035{
1036 global $root, $pageno, $tabno;
1037
2c6c7645
DO
1038 assertUIntArg ('object_id', __FUNCTION__);
1039 assertUIntArg ('pool_id', __FUNCTION__);
1040 assertUIntArg ('vs_id', __FUNCTION__);
49fb2686
DO
1041 if (!commitDeleteLB ($_REQUEST['object_id'], $_REQUEST['pool_id'], $_REQUEST['vs_id']))
1042 return buildRedirectURL_ERR ('commitDeleteLB() failed');
d6517a21 1043 else
49fb2686 1044 return buildRedirectURL_OK ('Load balancer was successfully deleted');
d6517a21
DO
1045}
1046
1047function deleteVService ()
1048{
e02e1941
DO
1049 assertUIntArg ('vs_id', __FUNCTION__);
1050 if (!commitDeleteVS ($_REQUEST['vs_id']))
1051 return buildRedirectURL_ERR ('commitDeleteVS() failed');
3241551e 1052 else
e02e1941 1053 return buildRedirectURL_OK ('Virtual service was successfully deleted');
3241551e
DO
1054}
1055
fb1c4a54
DO
1056function updateRealServer ()
1057{
1058 global $root, $pageno, $tabno;
1059
3c52ff91 1060 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
1061 assertUIntArg ('rs_id', __FUNCTION__);
1062 assertIPv4Arg ('rsip', __FUNCTION__);
1063 assertUIntArg ('rsport', __FUNCTION__);
1064 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
3241551e 1065 // only necessary for generating next URL
3c52ff91 1066 $pool_id = $_REQUEST['pool_id'];
732e4578 1067 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
841a7a7a 1068 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
fb1c4a54 1069 else
841a7a7a 1070 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
ca461127
DO
1071}
1072
c1ca768c 1073function updateLoadBalancer ()
3241551e
DO
1074{
1075 global $root, $pageno, $tabno;
1076
2c6c7645
DO
1077 assertUIntArg ('object_id', __FUNCTION__);
1078 assertUIntArg ('pool_id', __FUNCTION__);
1079 assertUIntArg ('vs_id', __FUNCTION__);
1080 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1081 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
49fb2686
DO
1082 if (!commitUpdateLB ($_REQUEST['object_id'], $_REQUEST['pool_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1083 return buildRedirectURL_ERR ('commitUpdateLB() failed');
d6517a21 1084 else
49fb2686 1085 return buildRedirectURL_OK ('Load balancer info was successfully updated');
d6517a21
DO
1086}
1087
1088function updateVService ()
1089{
e02e1941 1090 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
1091 assertIPv4Arg ('vip', __FUNCTION__);
1092 assertUIntArg ('vport', __FUNCTION__);
1093 assertStringArg ('proto', __FUNCTION__);
1094 assertStringArg ('name', __FUNCTION__, TRUE);
1095 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1096 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
e02e1941 1097 if (!commitUpdateVS ($_REQUEST['vs_id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
c9890988 1098 return buildRedirectURL_ERR ('commitUpdateVS() failed');
3241551e 1099 else
c9890988 1100 return buildRedirectURL_OK ('Virtual service was successfully updated');
3241551e
DO
1101}
1102
1103function addLoadBalancer ()
1104{
1105 global $root, $pageno, $tabno;
1106
2c6c7645
DO
1107 assertUIntArg ('pool_id', __FUNCTION__);
1108 assertUIntArg ('object_id', __FUNCTION__);
1109 assertUIntArg ('vs_id', __FUNCTION__);
1110 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1111 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
49fb2686
DO
1112 if (!addLBtoRSPool ($_REQUEST['pool_id'], $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1113 return buildRedirectURL_ERR ('addLBtoRSPool() failed');
3241551e 1114 else
49fb2686 1115 return buildRedirectURL_OK ('Load balancer was successfully added');
3241551e
DO
1116}
1117
5ad76f01
DO
1118function addRSPool ()
1119{
1120 global $root, $pageno, $tabno;
1121
2c6c7645
DO
1122 assertStringArg ('name', __FUNCTION__, TRUE);
1123 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1124 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
70c24883 1125 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
1126 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1127 else
1128 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1129}
1130
1131function deleteRSPool ()
1132{
1133 global $root, $pageno, $tabno;
1134
2c6c7645 1135 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1136 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
5ad76f01
DO
1137 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1138 else
1139 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1140}
1141
1142function updateRSPool ()
1143{
1144 global $root, $pageno, $tabno;
1145
2c6c7645
DO
1146 assertUIntArg ('pool_id', __FUNCTION__);
1147 assertStringArg ('name', __FUNCTION__, TRUE);
1148 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1149 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1150 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
ea5a3250 1151 return buildRedirectURL_ERR ('commitUpdateRSPool() failed');
5ad76f01 1152 else
ea5a3250 1153 return buildRedirectURL_OK ('Real server pool was successfully updated');
5ad76f01
DO
1154}
1155
1f7d18fa
DO
1156function updateRSInService ()
1157{
1158 global $root, $pageno, $tabno;
2c6c7645
DO
1159 assertUIntArg ('pool_id', __FUNCTION__);
1160 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1161 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1162 $orig = getRSPoolInfo ($pool_id);
1163 $nbad = $ngood = 0;
1164 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1165 {
1166 $rs_id = $_REQUEST["rsid_${i}"];
1167 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1168 $newval = 'yes';
1169 else
1170 $newval = 'no';
1171 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1172 {
1173 if (commitSetInService ($rs_id, $newval))
1174 $ngood++;
1175 else
1176 $nbad++;
1177 }
1178 }
1179 if (!$nbad)
841a7a7a 1180 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1f7d18fa 1181 else
841a7a7a 1182 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1f7d18fa
DO
1183}
1184
03eb5209
DO
1185function importPTRData ()
1186{
1187 global $root, $pageno, $tabno;
2c6c7645
DO
1188 assertUIntArg ('id', __FUNCTION__);
1189 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209
DO
1190 $nbad = $ngood = 0;
1191 $id = $_REQUEST['id'];
1192 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1193 {
3f3bd41e 1194 $inputname = "import_${i}";
03eb5209
DO
1195 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1196 continue;
2c6c7645
DO
1197 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1198 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1199 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1200 // Non-existent addresses will not have this argument set in request.
1201 $rsvd = 'no';
03eb5209
DO
1202 if ($_REQUEST["rsvd_${i}"] == 'yes')
1203 $rsvd = 'yes';
3f3bd41e 1204 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1205 $ngood++;
1206 else
1207 $nbad++;
1208 }
1209 if (!$nbad)
0aa8633e 1210 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
03eb5209 1211 else
0aa8633e 1212 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
03eb5209
DO
1213}
1214
f3f0161f
DO
1215function generateAutoPorts ()
1216{
1217 global $root, $pageno, $tabno;
2c6c7645 1218 assertUIntArg ('object_id', __FUNCTION__);
f3f0161f
DO
1219 $object_id = $_REQUEST['object_id'];
1220 $info = getObjectInfo ($object_id);
1221 // Navigate away in case of success, stay at the place otherwise.
1222 if (executeAutoPorts ($object_id, $info['objtype_id']))
1223 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1224 else
1225 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1226}
1227
24cbe8af 1228// Filter out implicit tags before storing the new tag set.
2034d968 1229function saveEntityTags ($realm, $bypass)
24cbe8af
DO
1230{
1231 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
2c6c7645 1232 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1233 $entity_id = $_REQUEST[$bypass];
e04931c8 1234 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1235 // Build a chain from the submitted data, minimize it,
ab379543 1236 // then wipe existing records and store the new set instead.
49fb1027 1237 deleteTagsForEntity ($realm, $entity_id);
6e49bd1f 1238 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1239 $n_succeeds = $n_errors = 0;
6e49bd1f 1240 foreach ($newchain as $taginfo)
eb6ea26f 1241 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1242 $n_succeeds++;
1243 else
1244 $n_errors++;
ab379543 1245 if ($n_errors)
6e49bd1f 1246 return buildRedirectURL_ERR ("Tried chaining ${n_succeeds} tags, but experienced ${n_errors} errors.");
ab379543 1247 else
6e49bd1f 1248 return buildRedirectURL_OK ("Chained ${n_succeeds} tags");
2034d968
DO
1249}
1250
1251function saveObjectTags ()
1252{
1253 return saveEntityTags ('object', 'object_id');
1254}
1255
1256function saveIPv4PrefixTags ()
1257{
1258 return saveEntityTags ('ipv4net', 'id');
24cbe8af
DO
1259}
1260
41bd8c87
DO
1261function saveRackTags ()
1262{
1263 return saveEntityTags ('rack', 'rack_id');
1264}
1265
d817ba03
DO
1266function saveIPv4VSTags ()
1267{
1a4beb9e 1268 return saveEntityTags ('ipv4vs', 'vs_id');
d817ba03
DO
1269}
1270
1271function saveIPv4RSPoolTags ()
1272{
2e4170aa 1273 return saveEntityTags ('ipv4rspool', 'pool_id');
d817ba03
DO
1274}
1275
cced6b7d
DO
1276function saveUserTags ()
1277{
1278 return saveEntityTags ('user', 'user_id');
1279}
1280
fe7044ad
DO
1281function destroyTag ()
1282{
1283 global $root, $pageno, $tabno;
49fb1027
DO
1284 assertUIntArg ('tag_id', __FUNCTION__);
1285 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
fe7044ad
DO
1286 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1287 else
1288 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1289}
1290
1291function createTag ()
1292{
1293 global $root, $pageno, $tabno;
49fb1027 1294 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1295 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1296 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80
DO
1297 if (!validTagName ($tagname))
1298 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
0f3c6639
DO
1299 if (tagExistsInDatabase ($tagname))
1300 return buildRedirectURL_ERR ("Tag '${tagname}' (or similar name) already exists");
fe7044ad
DO
1301 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1302 $parent_id = 'NULL';
1303 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1304 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1305 else
1306 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
1307}
1308
49fb1027
DO
1309function updateTag ()
1310{
1311 global $root, $pageno, $tabno;
1312 assertUIntArg ('tag_id', __FUNCTION__);
1313 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1314 assertStringArg ('tag_name', __FUNCTION__);
1315 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80
DO
1316 if (!validTagName ($tagname))
1317 return buildRedirectURL_ERR ("Invalid tag name '${tagname}'");
49fb1027
DO
1318 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1319 $parent_id = 'NULL';
1320 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1321 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Updated tag '${tagname}'.");
1322 else
1323 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not update tag '${tagname}' because of error '${ret}'");
1324}
1325
eb6ea26f
DO
1326function rollTags ()
1327{
1328 global $root, $pageno, $tabno;
1329 assertUIntArg ('row_id', __FUNCTION__);
1330 assertStringArg ('sum', __FUNCTION__, TRUE);
1331 assertUIntArg ('realsum', __FUNCTION__);
1332 $row_id = $_REQUEST['row_id'];
1333 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1334 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&error=" . urlencode ("test failed");
1335 $racks = getRacksForRow ($row_id);
1336 // Each time addTagForEntity() fails we assume it was just because of already existing record in its way.
1337 $newtags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1338 $tagstack = getExplicitTagsOnly (buildTagChainFromIds ($newtags));
eb6ea26f
DO
1339 $ndupes = $nnew = 0;
1340 foreach ($tagstack as $taginfo)
1341 foreach ($racks as $rackInfo)
1342 {
1343 if (addTagForEntity ('rack', $rackInfo['id'], $taginfo['id']))
1344 $nnew++;
1345 else
1346 $ndupes++;
1347 // FIXME: do something likewise for all object inside current rack
1348 }
1349 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&message=" . urlencode ("${nnew} new records done, ${ndupes} already existed");
1350}
1351
9457ca59 1352function changeMyPassword ()
cced6b7d 1353{
cced6b7d 1354 global $accounts, $root, $pageno, $tabno, $remote_username;
9457ca59
DO
1355 if (getConfigVar ('USER_AUTH_SRC') != 'database')
1356 return buildRedirectURL_ERR ('Can only change password under DB authentication.');
0ac7e4b3
DO
1357 assertStringArg ('oldpassword', __FUNCTION__);
1358 assertStringArg ('newpassword1', __FUNCTION__);
1359 assertStringArg ('newpassword2', __FUNCTION__);
1360 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
9457ca59 1361 return buildRedirectURL_ERR ('Old password doesn\'t match.');
cced6b7d 1362 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
9457ca59 1363 return buildRedirectURL_ERR ('New passwords don\'t match.');
0ac7e4b3 1364 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
9457ca59 1365 return buildRedirectURL_OK ('Password changed successfully.');
cced6b7d 1366 else
9457ca59 1367 return buildRedirectURL_ERR ('Password change failed.');
cced6b7d
DO
1368}
1369
1370function saveRackCode ()
1371{
1372 global $root, $pageno, $tabno;
1373 assertStringArg ('rackcode');
e6a4adb9
DO
1374 // For the test to succeed, unescape LFs, strip CRs.
1375 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1376 $parseTree = getRackCode ($newcode);
1377 if ($parseTree['result'] != 'ACK')
1378 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Verification failed: ' . $parseTree['load']);
1379 if (saveScript ('RackCode', $newcode))
cced6b7d
DO
1380 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Saved successfully.');
1381 else
1382 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Save failed.');
1383}
1384
46f92ff7
DO
1385// This handler's context is pre-built, but not authorized. It is assumed, that the
1386// handler will take existing context and before each commit check authorization
1387// on the base chain plus necessary context added.
1388function setPortVLAN ()
1389{
1390 assertUIntArg ('portcount', __FUNCTION__);
1391 $data = getSwitchVLANs ($_REQUEST['object_id']);
1392 if ($data === NULL)
1393 return buildRedirectURL_ERR ('getSwitchVLANs() failed');
1394 list ($vlanlist, $portlist) = $data;
1395 // Here we just build up 1 set command for the gateway with all of the ports
1396 // included. The gateway is expected to filter unnecessary changes silently
1397 // and to provide a list of responses with either error or success message
1398 // for each of the rest.
1399 $nports = $_REQUEST['portcount'];
1400 $prefix = 'set ';
1401 $log = array();
1402 $setcmd = '';
1403 for ($i = 0; $i < $nports; $i++)
1404 if
1405 (
1406 !isset ($_REQUEST['portname_' . $i]) ||
1407 !isset ($_REQUEST['vlanid_' . $i]) ||
1408 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1409 )
1410 $log[] = array ('code' => 'error', 'message' => "Ignoring malformed record #${i} in form submit");
1411 elseif
1412 (
1413 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1414 $portlist[$i]['vlaind'] == 'TRUNK'
1415 )
1416 continue;
1417 else
1418 {
1419 $portname = $_REQUEST['portname_' . $i];
1420 $oldvlanid = $portlist[$i]['vlanid'];
1421 $newvlanid = $_REQUEST['vlanid_' . $i];
1422 // Finish the security context and evaluate it.
1423 $annex = array();
1424 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1425 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1426 if (!permitted (NULL, NULL, NULL, $annex))
1427 {
1428 $log[] = array ('code' => 'error', 'message' => "Permission denied moving port ${portname} from VLAN${oldvlanid} to VLAN${newvlanid}");
1429 continue;
1430 }
1431 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1432 $prefix = ';';
1433 }
1434 // Feed the gateway and interpret its (non)response.
1435 if ($setcmd != '')
1436 $log = array_merge ($log, setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1437 else
1438 $log[] = array ('code' => 'warning', 'message' => 'nothing happened...');
1439 return buildWideRedirectURL ($log);
1440}
1441
e673ee24 1442?>