r1978 + remove debug output from renderRackObject()
[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{
b4c1ef87
DO
411 global $root, $pageno, $tabno, $page;
412 assertIPv4Arg ('ip', __FUNCTION__);
413 assertUIntArg ('object_id', __FUNCTION__);
414 assertStringArg ('bond_name', __FUNCTION__, TRUE);
415 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 416
b4c1ef87
DO
417 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
418 $bpname = $page[$pageno]['bypass'];
419 $baseurl = "${root}?page=${pageno}&tab=${tabno}&${bpname}=" . $_REQUEST[$bpname];
e673ee24 420 if ($error != '')
b4c1ef87 421 return "${baseurl}&error=" . urlencode ($error);
e673ee24 422 else
b4c1ef87 423 return "${baseurl}&message=" . urlencode ("allocation updated");
e673ee24
DO
424}
425
b4c1ef87 426function delIPv4Allocation ()
e673ee24 427{
b4c1ef87
DO
428 global $root, $pageno, $tabno, $page;
429 assertIPv4Arg ('ip', __FUNCTION__);
430 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 431
b4c1ef87
DO
432 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
433 $bpname = $page[$pageno]['bypass'];
434 $baseurl = "${root}?page=${pageno}&tab=${tabno}&${bpname}=" . $_REQUEST[$bpname];
e673ee24 435 if ($error != '')
b4c1ef87 436 return "${baseurl}&error=" . urlencode ($error);
e673ee24 437 else
b4c1ef87 438 return "${baseurl}&message=" . urlencode ("deallocated");
e673ee24
DO
439}
440
b4c1ef87 441function addIPv4Allocation ()
e673ee24 442{
b4c1ef87 443 global $root, $pageno, $tabno, $page;
e673ee24 444
b4c1ef87
DO
445 assertIPv4Arg ('ip', __FUNCTION__);
446 assertUIntArg ('object_id', __FUNCTION__);
447 assertStringArg ('bond_name', __FUNCTION__, TRUE);
448 assertStringArg ('bond_type', __FUNCTION__);
449 // Strip masklen.
450 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
451 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
452 $address = getIPAddress ($ip);
453 if ($address['exists'] and ($address['reserved'] == 'yes' or strlen ($address['name']) > 0))
454 {
455 $release = getConfigVar ('IPV4_AUTO_RELEASE');
456 if ($release >= 1)
457 $address['reserved'] = 'no';
458 if ($release >= 2)
459 $address['name'] = '';
460 updateAddress ($ip, $address['name'], $address['reserved']);
461 }
462 $bpname = $page[$pageno]['bypass'];
463 $baseurl = "${root}?page=${pageno}&tab=${tabno}&${bpname}=" . $_REQUEST[$bpname];
e673ee24 464 if ($error != '')
b4c1ef87 465 return "${baseurl}&error=" . urlencode ($error);
e673ee24 466 else
b4c1ef87 467 return "${baseurl}&message=" . urlencode ("allocated");
e673ee24
DO
468}
469
42023f03 470function addIPv4Prefix ()
e673ee24
DO
471{
472 global $root, $pageno, $tabno;
2c6c7645
DO
473 assertStringArg ('range', __FUNCTION__);
474 assertStringArg ('name', __FUNCTION__);
e673ee24 475
fa05e3de
DO
476 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
477 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 478 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 479 if ($error != '')
42023f03 480 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
e673ee24 481 else
42023f03 482 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix successfully added");
e673ee24
DO
483}
484
42023f03 485function delIPv4Prefix ()
e673ee24
DO
486{
487 global $root, $pageno, $tabno;
488
42023f03
DO
489 assertUIntArg ('id');
490 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 491 if ($error != '')
42023f03 492 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ($error);
e673ee24 493 else
42023f03 494 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("IPv4 prefix deleted");
e673ee24
DO
495}
496
42023f03 497function editRange ()
e673ee24
DO
498{
499 global $root, $pageno, $tabno;
500
501 $id = $_REQUEST['id'];
42023f03
DO
502 $name = $_REQUEST['name'];
503 $error = updateRange($id, $name);
e673ee24
DO
504 if ($error != '')
505 {
42023f03 506 return "${root}?page=${pageno}&tab=${tabno}&id=$id&error=".urlencode($error);
e673ee24
DO
507 }
508 else
509 {
42023f03 510 return "${root}?page=${pageno}&tab=${tabno}&id=$id&message=".urlencode("Range updated");
e673ee24
DO
511 }
512
513}
514
515function editAddress ()
516{
517 global $root, $pageno, $tabno;
518
519 $ip = $_REQUEST['ip'];
520 $name = $_REQUEST['name'];
521 if (isset ($_REQUEST['reserved']))
522 $reserved = $_REQUEST['reserved'];
523 else
524 $reserved = 'off';
525 $error = updateAddress($ip, $name, $reserved=='on'?'yes':'no');
526 if ($error != '')
527 {
528 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&error=".urlencode($error);
529 }
530 else
531 {
532 return "${root}?page=${pageno}&tab=${tabno}&ip=$ip&message=".urlencode("Address updated");
533 }
534
535}
536
cced6b7d 537function createUser ()
e673ee24
DO
538{
539 global $root, $pageno, $tabno;
2c6c7645
DO
540 assertStringArg ('username', __FUNCTION__);
541 assertStringArg ('realname', __FUNCTION__, TRUE);
542 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
543 $username = $_REQUEST['username'];
544 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
545 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
546 if ($result == TRUE)
547 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} created.");
548 else
549 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error creating user account ${username}.");
550}
551
cced6b7d 552function updateUser ()
e673ee24
DO
553{
554 global $root, $pageno, $tabno;
cced6b7d 555 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
556 assertStringArg ('username', __FUNCTION__);
557 assertStringArg ('realname', __FUNCTION__, TRUE);
558 assertStringArg ('password', __FUNCTION__);
e673ee24 559 // We might be asked to change username, so use user ID only.
cced6b7d 560 $id = $_REQUEST['user_id'];
e673ee24
DO
561 $username = $_REQUEST['username'];
562 $new_password = $_REQUEST['password'];
563 $old_hash = getHashByID ($id);
564 if ($old_hash == NULL)
565 {
61e269b5 566 showError ('getHashByID() failed', __FUNCTION__);
e673ee24
DO
567 return;
568 }
569 // Update user password only if provided password is not the same as current password hash.
570 if ($new_password != $old_hash)
571 $new_password = hash (PASSWORD_HASH, $new_password);
cced6b7d 572 $result = commitUpdateUserAccount ($id, $username, $_REQUEST['realname'], $new_password);
e673ee24
DO
573 if ($result == TRUE)
574 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account ${username} updated.");
575 else
576 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error updating user account ${username}.");
577}
578
cced6b7d 579function enableUser ()
e673ee24
DO
580{
581 global $root, $pageno, $tabno;
cced6b7d
DO
582 assertUIntArg ('user_id', __FUNCTION__);
583 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
e673ee24
DO
584 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account enabled.");
585 else
586 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error enabling user account.");
587}
588
cced6b7d 589function disableUser ()
e673ee24
DO
590{
591 global $root, $pageno, $tabno;
cced6b7d
DO
592 assertUIntArg ('user_id', __FUNCTION__);
593 $id = $_REQUEST['user_id'];
e673ee24
DO
594 if ($id == 1)
595 $result = FALSE;
596 else
597 $result = commitEnableUserAccount ($id, 'no');
598 if ($result == TRUE)
599 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("User account disabled.");
600 else
601 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error disabling user account.");
602}
603
e673ee24
DO
604// This function find differences in users's submit and PortCompat table
605// and modifies database accordingly.
606function savePortMap ()
607{
608 global $root, $pageno, $tabno;
609 $ptlist = getPortTypes();
610 $oldCompat = getPortCompat();
611 $newCompat = array();
612 foreach (array_keys ($ptlist) as $leftKey)
613 foreach (array_keys ($ptlist) as $rightKey)
614 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
615 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
616 // Build the new matrix from user's submit and compare it to
617 // the old matrix. Those pairs which appear on
618 // new matrix only, have to be stored in PortCompat table. Those who appear
619 // on the old matrix only, should be removed from PortCompat table.
620 // Those present in both matrices should be skipped.
621 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
622 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
623 $error_count = $success_count = 0;
624 foreach (array_keys ($ptlist) as $type1)
625 foreach (array_keys ($ptlist) as $type2)
626 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
627 switch ($oldCompatTable[$type1][$type2])
628 {
629 case TRUE: // new value is FALSE
630 if (removePortCompat ($type1, $type2) === TRUE)
631 $success_count++;
632 else
633 $error_count++;
634 break;
635 case FALSE: // new value is TRUE
636 if (addPortCompat ($type1, $type2) === TRUE)
637 $success_count++;
638 else
639 $error_count++;
640 break;
641 default:
61e269b5 642 showError ('oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
643 break;
644 }
645 return
646 "${root}?page=${pageno}&tab=${tabno}&" .
647 ($error_count == 0 ? 'message' : 'error') .
648 "=" . urlencode ("${error_count} failures and ${success_count} successfull changes.");
649}
650
651function deleteDictWord ()
652{
653 global $root, $pageno, $tabno;
654 return
655 "${root}?page=${pageno}&tab=${tabno}&" .
656 "error=" . urlencode ('Dragon ate this word!');
657}
658
659function updateDictionary ()
660{
661 global $root, $pageno, $tabno;
2c6c7645
DO
662 assertUIntArg ('chapter_no', __FUNCTION__);
663 assertUIntArg ('dict_key', __FUNCTION__);
664 assertStringArg ('dict_value', __FUNCTION__);
e673ee24
DO
665 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
666 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Update succeeded.');
667 else
668 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed!");
669}
670
671function supplementDictionary ()
672{
673 global $root, $pageno, $tabno;
2c6c7645
DO
674 assertUIntArg ('chapter_no', __FUNCTION__);
675 assertStringArg ('dict_value', __FUNCTION__);
e673ee24
DO
676 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
677 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
678 else
679 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
680}
681
682function reduceDictionary ()
683{
684 global $root, $pageno, $tabno;
2c6c7645
DO
685 assertUIntArg ('chapter_no', __FUNCTION__);
686 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24
DO
687 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
688 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
689 else
690 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
691}
692
693function addChapter ()
694{
695 global $root, $pageno, $tabno;
2c6c7645 696 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24
DO
697 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
698 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was added.');
699 else
700 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error adding chapter.');
701}
702
703function updateChapter ()
704{
705 global $root, $pageno, $tabno;
2c6c7645
DO
706 assertUIntArg ('chapter_no', __FUNCTION__);
707 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24
DO
708 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
709 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was updated.');
710 else
711 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error updating chapter.');
712}
713
714function delChapter ()
715{
716 global $root, $pageno, $tabno;
2c6c7645 717 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24
DO
718 if (commitDeleteChapter ($_REQUEST['chapter_no']))
719 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Chapter was deleted.');
720 else
721 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting chapter.');
722}
723
724function changeAttribute ()
725{
726 global $root, $pageno, $tabno;
2c6c7645
DO
727 assertUIntArg ('attr_id', __FUNCTION__);
728 assertStringArg ('attr_name', __FUNCTION__);
e673ee24
DO
729 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
730 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Rename successful.');
731 else
732 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error renaming attribute.');
733}
734
735function createAttribute ()
736{
737 global $root, $pageno, $tabno;
2c6c7645
DO
738 assertStringArg ('attr_name', __FUNCTION__);
739 assertStringArg ('attr_type', __FUNCTION__);
e673ee24
DO
740 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
741 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Attribute '${_REQUEST['attr_name']}' created.");
742 else
743 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error creating attribute.');
744}
745
746function deleteAttribute ()
747{
748 global $root, $pageno, $tabno;
2c6c7645 749 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24
DO
750 if (commitDeleteAttribute ($_REQUEST['attr_id']))
751 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Attribute was deleted.');
752 else
753 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Error deleting attribute.');
754}
755
756function supplementAttrMap ()
757{
758 global $root, $pageno, $tabno;
2c6c7645
DO
759 assertUIntArg ('attr_id', __FUNCTION__);
760 assertUIntArg ('objtype_id', __FUNCTION__);
761 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24
DO
762 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
763 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Supplement succeeded.');
764 else
765 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Supplement failed!");
766}
767
768function reduceAttrMap ()
769{
770 global $root, $pageno, $tabno;
2c6c7645
DO
771 assertUIntArg ('attr_id', __FUNCTION__);
772 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24
DO
773 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
774 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Reduction succeeded.');
775 else
776 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Reduction failed!");
777}
778
779function resetAttrValue ()
780{
781 global $root, $pageno, $tabno;
2c6c7645
DO
782 assertUIntArg ('attr_id', __FUNCTION__);
783 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
784 $object_id = $_REQUEST['object_id'];
785 if (commitResetAttrValue ($object_id, $_REQUEST['attr_id']) === TRUE)
786 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reset succeeded.');
787 else
788 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Reset failed!");
789}
790
0dfb8a2a 791function updateAttrValues ()
e673ee24
DO
792{
793 global $root, $pageno, $tabno;
2c6c7645 794 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 795 $object_id = $_REQUEST['object_id'];
0dfb8a2a
DO
796 $oldvalues = getAttrValues ($object_id);
797
2c6c7645 798 assertUIntArg ('num_attrs', __FUNCTION__);
0dfb8a2a
DO
799 $num_attrs = $_REQUEST['num_attrs'];
800 $result = array();
801
802 for ($i = 0; $i < $num_attrs; $i++)
803 {
2c6c7645 804 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
805 $attr_id = $_REQUEST["${i}_attr_id"];
806
807 // Field is empty, delete attribute and move on.
808 if (empty($_REQUEST["${i}_value"]))
809 {
810 commitResetAttrValue ($object_id, $attr_id);
811 continue;
812 }
813
814 // The value could be uint/float, but we don't know ATM. Let SQL
815 // server check this and complain.
2c6c7645 816 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
817 $value = $_REQUEST["${i}_value"];
818 switch ($oldvalues[$attr_id]['type'])
819 {
820 case 'uint':
821 case 'float':
822 case 'string':
823 $oldvalue = $oldvalues[$attr_id]['value'];
824 break;
825 case 'dict':
826 $oldvalue = $oldvalues[$attr_id]['key'];
827 break;
828 default:
61e269b5 829 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
830 die;
831 }
832 if ($value == $oldvalue)
833 continue;
834
835 // Note if the queries succeed or not, it determines which page they see.
836 $result[] = commitUpdateAttrValue ($object_id, $attr_id, $value);
837 }
838
839 if (in_array(false, $result))
e673ee24 840 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Update failed!");
0dfb8a2a
DO
841
842 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Update succeeded.');
e673ee24
DO
843}
844
845function useupPort ()
846{
847 global $root, $pageno, $tabno;
2c6c7645
DO
848 assertUIntArg ('port_id', __FUNCTION__);
849 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
850 $object_id = $_REQUEST['object_id'];
851 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
852 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ('Reservation removed.');
853 else
854 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ("Error removing reservation!");
855}
856
4fe32e78
DY
857function updateUI ()
858{
c461c579 859 global $root, $pageno, $tabno;
4fe32e78
DY
860 $oldvalues = loadConfigCache();
861
2c6c7645 862 assertUIntArg ('num_vars', __FUNCTION__);
4fe32e78 863 $num_vars = $_REQUEST['num_vars'];
c461c579 864 $error = '';
4fe32e78
DY
865
866 for ($i = 0; $i < $num_vars; $i++)
867 {
2c6c7645
DO
868 assertStringArg ("${i}_varname", __FUNCTION__);
869 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 870 $varname = $_REQUEST["${i}_varname"];
4fe32e78 871 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 872
c461c579
DO
873 // If form value = value in DB, don't bother updating DB
874 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
875 continue;
876
877 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
878 $error = setConfigVar ($varname, $varvalue, TRUE);
879 if (!empty ($error))
880 break;
4fe32e78
DY
881 }
882
c461c579
DO
883 if ($error != '')
884 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
4fe32e78 885
c461c579 886 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
4fe32e78
DY
887}
888
b07f617c
DO
889function resetUIConfig()
890{
6c12ef9b 891 global $root, $pageno, $tabno;
1f86e996 892 setConfigVar ('default_port_type','24');
b07f617c
DO
893 setConfigVar ('MASSCOUNT','15');
894 setConfigVar ('MAXSELSIZE','30');
895 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
896 setConfigVar ('ROW_SCALE','2');
897 setConfigVar ('PORTS_PER_ROW','12');
898 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
899 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 900 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
901 setConfigVar ('USER_AUTH_SRC','database');
902 setConfigVar ('DEFAULT_SLB_VS_PORT','');
903 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 904 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 905 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 906 setConfigVar ('DETECT_URLS','no');
4b8d413e 907 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 908 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 909 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
910 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
911 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
912 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 913 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 914 setConfigVar ('IPV4_AUTO_RELEASE','1');
6c12ef9b 915 return "${root}?page=${pageno}&tab=default&message=" . urlencode ("Reset complete");
b07f617c
DO
916}
917
732e4578 918// Add single record.
ca461127
DO
919function addRealServer ()
920{
921 global $root, $pageno, $tabno;
922
2c6c7645
DO
923 assertUIntArg ('pool_id', __FUNCTION__);
924 assertIPv4Arg ('remoteip', __FUNCTION__);
925 assertUIntArg ('rsport', __FUNCTION__);
926 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
22a5b5ae 927 $pool_id = $_REQUEST['pool_id'];
2910c1d4 928 if (!addRStoRSPool ($pool_id, $_REQUEST['remoteip'], $_REQUEST['rsport'], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), $_REQUEST['rsconfig']))
841a7a7a 929 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addRStoRSPool() failed');
d6517a21 930 else
841a7a7a 931 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully added");
d6517a21
DO
932}
933
732e4578
DO
934// Parse textarea submitted and try adding a real server for each line.
935function addRealServers ()
936{
937 global $root, $pageno, $tabno;
938
cbadbb38 939 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
940 assertStringArg ('format', __FUNCTION__);
941 assertStringArg ('rawtext', __FUNCTION__);
cbadbb38 942 $pool_id = $_REQUEST['pool_id'];
732e4578
DO
943 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
944 $ngood = $nbad = 0;
945 $rsconfig = '';
946 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
947 foreach (explode ('\n', $rawtext) as $line)
948 {
949 if (empty ($line))
950 continue;
951 $match = array ();
952 switch ($_REQUEST['format'])
953 {
954 case 'ipvs_2': // address and port only
955 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
956 continue;
7e7a8387 957 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
958 $ngood++;
959 else
960 $nbad++;
961 break;
962 case 'ipvs_3': // address, port and weight
963 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
964 continue;
7e7a8387 965 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
966 $ngood++;
967 else
968 $nbad++;
969 break;
e69c2aa3
DO
970 case 'ssv_2': // IP address and port
971 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
972 continue;
973 if (addRStoRSPool ($pool_id, $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
974 $ngood++;
975 else
976 $nbad++;
977 break;
732e4578
DO
978 default:
979 return "${root}?page=${pageno}&tab=${tabno}&id=${pool_id}&error=" . urlencode (__FUNCTION__ . ': invalid format requested');
980 break;
981 }
982 }
983 if ($nbad == 0 and $ngood > 0)
841a7a7a 984 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Successfully added ${ngood} real servers");
732e4578 985 else
841a7a7a 986 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Added ${ngood} real servers and encountered ${nbad} errors");
732e4578
DO
987}
988
d6517a21
DO
989function addVService ()
990{
991 global $root, $pageno, $tabno;
992
2c6c7645
DO
993 assertIPv4Arg ('vip', __FUNCTION__);
994 assertUIntArg ('vport', __FUNCTION__);
995 assertStringArg ('proto', __FUNCTION__);
d6517a21
DO
996 $proto = $_REQUEST['proto'];
997 if ($proto != 'TCP' and $proto != 'UDP')
998 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode (__FUNCTION__ . ': invalid protocol');
2c6c7645
DO
999 assertStringArg ('name', __FUNCTION__, TRUE);
1000 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1001 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
d6517a21
DO
1002 $pool_id = $_REQUEST['id'];
1003 if (!commitCreateVS ($_REQUEST['vip'], $_REQUEST['vport'], $proto, $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1004 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateVS() failed');
ca461127 1005 else
d6517a21 1006 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully created");
fb1c4a54
DO
1007}
1008
1009function deleteRealServer ()
1010{
1011 global $root, $pageno, $tabno;
1012
2c6c7645
DO
1013 assertUIntArg ('pool_id', __FUNCTION__);
1014 assertUIntArg ('id', __FUNCTION__);
fb1c4a54 1015 $pool_id = $_REQUEST['pool_id'];
d6517a21 1016 if (!commitDeleteRS ($_REQUEST['id']))
841a7a7a 1017 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteRS() failed');
fb1c4a54 1018 else
841a7a7a 1019 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully deleted");
fb1c4a54
DO
1020}
1021
3241551e
DO
1022function deleteLoadBalancer ()
1023{
1024 global $root, $pageno, $tabno;
1025
2c6c7645
DO
1026 assertUIntArg ('object_id', __FUNCTION__);
1027 assertUIntArg ('pool_id', __FUNCTION__);
1028 assertUIntArg ('vs_id', __FUNCTION__);
3241551e 1029 $pool_id = $_REQUEST['pool_id'];
c4d85fd1 1030 if (!commitDeleteLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id']))
841a7a7a 1031 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitDeleteLB() failed');
d6517a21 1032 else
841a7a7a 1033 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully deleted");
d6517a21
DO
1034}
1035
1036function deleteVService ()
1037{
1038 global $root, $pageno, $tabno;
1039
2c6c7645 1040 assertUIntArg ('id', __FUNCTION__);
d6517a21
DO
1041 if (!commitDeleteVS ($_REQUEST['id']))
1042 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteVS() failed');
3241551e 1043 else
d6517a21 1044 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully deleted");
3241551e
DO
1045}
1046
fb1c4a54
DO
1047function updateRealServer ()
1048{
1049 global $root, $pageno, $tabno;
1050
3c52ff91 1051 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
1052 assertUIntArg ('rs_id', __FUNCTION__);
1053 assertIPv4Arg ('rsip', __FUNCTION__);
1054 assertUIntArg ('rsport', __FUNCTION__);
1055 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
3241551e 1056 // only necessary for generating next URL
3c52ff91 1057 $pool_id = $_REQUEST['pool_id'];
732e4578 1058 if (!commitUpdateRS ($_REQUEST['rs_id'], $_REQUEST['rsip'], $_REQUEST['rsport'], $_REQUEST['rsconfig']))
841a7a7a 1059 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateRS() failed');
fb1c4a54 1060 else
841a7a7a 1061 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Real server was successfully updated");
ca461127
DO
1062}
1063
c1ca768c 1064function updateLoadBalancer ()
3241551e
DO
1065{
1066 global $root, $pageno, $tabno;
1067
2c6c7645
DO
1068 assertUIntArg ('object_id', __FUNCTION__);
1069 assertUIntArg ('pool_id', __FUNCTION__);
1070 assertUIntArg ('vs_id', __FUNCTION__);
1071 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1072 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
3241551e 1073 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1074 if (!commitUpdateLB ($_REQUEST['object_id'], $pool_id, $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
841a7a7a 1075 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('commitUpdateLB() failed');
d6517a21 1076 else
841a7a7a 1077 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer info was successfully updated");
d6517a21
DO
1078}
1079
1080function updateVService ()
1081{
1082 global $root, $pageno, $tabno;
1083
2c6c7645
DO
1084 assertUIntArg ('id', __FUNCTION__);
1085 assertIPv4Arg ('vip', __FUNCTION__);
1086 assertUIntArg ('vport', __FUNCTION__);
1087 assertStringArg ('proto', __FUNCTION__);
1088 assertStringArg ('name', __FUNCTION__, TRUE);
1089 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1090 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
d6517a21
DO
1091 if (!commitUpdateVS ($_REQUEST['id'], $_REQUEST['vip'], $_REQUEST['vport'], $_REQUEST['proto'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1092 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateVS() failed');
3241551e 1093 else
d6517a21 1094 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Virtual service was successfully updated");
3241551e
DO
1095}
1096
1097function addLoadBalancer ()
1098{
1099 global $root, $pageno, $tabno;
1100
2c6c7645
DO
1101 assertUIntArg ('pool_id', __FUNCTION__);
1102 assertUIntArg ('object_id', __FUNCTION__);
1103 assertUIntArg ('vs_id', __FUNCTION__);
1104 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1105 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
3241551e 1106 $pool_id = $_REQUEST['pool_id'];
c1ca768c 1107 if (!addLBtoRSPool ($pool_id, $_REQUEST['object_id'], $_REQUEST['vs_id'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
841a7a7a 1108 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ('addLBtoRSPool() failed');
3241551e 1109 else
841a7a7a 1110 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ("Load balancer was successfully added");
3241551e
DO
1111}
1112
5ad76f01
DO
1113function addRSPool ()
1114{
1115 global $root, $pageno, $tabno;
1116
2c6c7645
DO
1117 assertStringArg ('name', __FUNCTION__, TRUE);
1118 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1119 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
70c24883 1120 if (!commitCreateRSPool ($_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
1121 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitCreateRSPool() failed');
1122 else
1123 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully created");
1124}
1125
1126function deleteRSPool ()
1127{
1128 global $root, $pageno, $tabno;
1129
2c6c7645 1130 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1131 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
5ad76f01
DO
1132 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitDeleteRSPool() failed');
1133 else
1134 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully deleted");
1135}
1136
1137function updateRSPool ()
1138{
1139 global $root, $pageno, $tabno;
1140
2c6c7645
DO
1141 assertUIntArg ('pool_id', __FUNCTION__);
1142 assertStringArg ('name', __FUNCTION__, TRUE);
1143 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1144 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1145 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
5ad76f01
DO
1146 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('commitUpdateRSPool() failed');
1147 else
1148 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Real server pool was successfully updated");
1149}
1150
1f7d18fa
DO
1151function updateRSInService ()
1152{
1153 global $root, $pageno, $tabno;
2c6c7645
DO
1154 assertUIntArg ('pool_id', __FUNCTION__);
1155 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1156 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1157 $orig = getRSPoolInfo ($pool_id);
1158 $nbad = $ngood = 0;
1159 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1160 {
1161 $rs_id = $_REQUEST["rsid_${i}"];
1162 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1163 $newval = 'yes';
1164 else
1165 $newval = 'no';
1166 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1167 {
1168 if (commitSetInService ($rs_id, $newval))
1169 $ngood++;
1170 else
1171 $nbad++;
1172 }
1173 }
1174 if (!$nbad)
841a7a7a 1175 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&message=" . urlencode ($ngood . " real server(s) were successfully (de)activated");
1f7d18fa 1176 else
841a7a7a 1177 return "${root}?page=${pageno}&tab=${tabno}&pool_id=${pool_id}&error=" . urlencode ("Encountered ${nbad} errors, (de)activated ${ngood} real servers");
1f7d18fa
DO
1178}
1179
03eb5209
DO
1180function importPTRData ()
1181{
1182 global $root, $pageno, $tabno;
2c6c7645
DO
1183 assertUIntArg ('id', __FUNCTION__);
1184 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209
DO
1185 $nbad = $ngood = 0;
1186 $id = $_REQUEST['id'];
1187 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1188 {
3f3bd41e 1189 $inputname = "import_${i}";
03eb5209
DO
1190 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1191 continue;
2c6c7645
DO
1192 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1193 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1194 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1195 // Non-existent addresses will not have this argument set in request.
1196 $rsvd = 'no';
03eb5209
DO
1197 if ($_REQUEST["rsvd_${i}"] == 'yes')
1198 $rsvd = 'yes';
3f3bd41e 1199 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1200 $ngood++;
1201 else
1202 $nbad++;
1203 }
1204 if (!$nbad)
0aa8633e 1205 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&message=" . urlencode ($ngood . " IP address(es) were successfully updated");
03eb5209 1206 else
0aa8633e 1207 return "${root}?page=${pageno}&tab=${tabno}&id=${id}&error=" . urlencode ("Encountered ${nbad} errors, updated ${ngood} IP address(es)");
03eb5209
DO
1208}
1209
f3f0161f
DO
1210function generateAutoPorts ()
1211{
1212 global $root, $pageno, $tabno;
2c6c7645 1213 assertUIntArg ('object_id', __FUNCTION__);
f3f0161f
DO
1214 $object_id = $_REQUEST['object_id'];
1215 $info = getObjectInfo ($object_id);
1216 // Navigate away in case of success, stay at the place otherwise.
1217 if (executeAutoPorts ($object_id, $info['objtype_id']))
1218 return "${root}?page=${pageno}&tab=ports&object_id=${object_id}&message=" . urlencode ('Generation complete');
1219 else
1220 return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&error=" . urlencode ('executeAutoPorts() failed');
1221}
1222
24cbe8af 1223// Filter out implicit tags before storing the new tag set.
2034d968 1224function saveEntityTags ($realm, $bypass)
24cbe8af
DO
1225{
1226 global $root, $pageno, $tabno, $explicit_tags, $implicit_tags;
2c6c7645 1227 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1228 $entity_id = $_REQUEST[$bypass];
e04931c8 1229 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
ab379543
DO
1230 // Build a trail from the submitted data, minimize it,
1231 // then wipe existing records and store the new set instead.
49fb1027 1232 deleteTagsForEntity ($realm, $entity_id);
e04931c8 1233 $newtrail = getExplicitTagsOnly (buildTrailFromIds ($taglist));
ab379543
DO
1234 $n_succeeds = $n_errors = 0;
1235 foreach ($newtrail as $taginfo)
eb6ea26f 1236 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1237 $n_succeeds++;
1238 else
1239 $n_errors++;
ab379543 1240 if ($n_errors)
2034d968 1241 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&error=" . urlencode ("Replaced trail with ${n_succeeds} tags, but experienced ${n_errors} errors.");
ab379543 1242 else
cced6b7d 1243 return "${root}?page=${pageno}&tab=${tabno}&${bypass}=${entity_id}&message=" . urlencode ("New chain is ${n_succeeds} tags long");
2034d968
DO
1244}
1245
1246function saveObjectTags ()
1247{
1248 return saveEntityTags ('object', 'object_id');
1249}
1250
1251function saveIPv4PrefixTags ()
1252{
1253 return saveEntityTags ('ipv4net', 'id');
24cbe8af
DO
1254}
1255
41bd8c87
DO
1256function saveRackTags ()
1257{
1258 return saveEntityTags ('rack', 'rack_id');
1259}
1260
d817ba03
DO
1261function saveIPv4VSTags ()
1262{
1263 return saveEntityTags ('ipv4vs', 'id');
1264}
1265
1266function saveIPv4RSPoolTags ()
1267{
2e4170aa 1268 return saveEntityTags ('ipv4rspool', 'pool_id');
d817ba03
DO
1269}
1270
cced6b7d
DO
1271function saveUserTags ()
1272{
1273 return saveEntityTags ('user', 'user_id');
1274}
1275
fe7044ad
DO
1276function destroyTag ()
1277{
1278 global $root, $pageno, $tabno;
49fb1027
DO
1279 assertUIntArg ('tag_id', __FUNCTION__);
1280 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
fe7044ad
DO
1281 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
1282 else
1283 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
1284}
1285
1286function createTag ()
1287{
1288 global $root, $pageno, $tabno;
49fb1027 1289 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1290 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1291 $tagname = trim ($_REQUEST['tag_name']);
fe7044ad
DO
1292 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1293 $parent_id = 'NULL';
1294 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1295 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
1296 else
1297 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
1298}
1299
49fb1027
DO
1300function updateTag ()
1301{
1302 global $root, $pageno, $tabno;
1303 assertUIntArg ('tag_id', __FUNCTION__);
1304 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1305 assertStringArg ('tag_name', __FUNCTION__);
1306 $tagname = trim ($_REQUEST['tag_name']);
1307 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1308 $parent_id = 'NULL';
1309 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1310 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Updated tag '${tagname}'.");
1311 else
1312 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not update tag '${tagname}' because of error '${ret}'");
1313}
1314
eb6ea26f
DO
1315function rollTags ()
1316{
1317 global $root, $pageno, $tabno;
1318 assertUIntArg ('row_id', __FUNCTION__);
1319 assertStringArg ('sum', __FUNCTION__, TRUE);
1320 assertUIntArg ('realsum', __FUNCTION__);
1321 $row_id = $_REQUEST['row_id'];
1322 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1323 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&error=" . urlencode ("test failed");
1324 $racks = getRacksForRow ($row_id);
1325 // Each time addTagForEntity() fails we assume it was just because of already existing record in its way.
1326 $newtags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1327 $tagstack = getExplicitTagsOnly (buildTrailFromIds ($newtags));
1328 $ndupes = $nnew = 0;
1329 foreach ($tagstack as $taginfo)
1330 foreach ($racks as $rackInfo)
1331 {
1332 if (addTagForEntity ('rack', $rackInfo['id'], $taginfo['id']))
1333 $nnew++;
1334 else
1335 $ndupes++;
1336 // FIXME: do something likewise for all object inside current rack
1337 }
1338 return "${root}?page=${pageno}&tab=${tabno}&row_id=${row_id}&message=" . urlencode ("${nnew} new records done, ${ndupes} already existed");
1339}
1340
cced6b7d
DO
1341function changePassword ()
1342{
1343 assertUIntArg ('user_id');
1344 $user_id = $_REQUEST['user_id'];
1345 $username = getUsernameByID ($user_id);
1346 global $accounts, $root, $pageno, $tabno, $remote_username;
1347 if ($accounts[$remote_username]['user_id'] != 1)
1348 {
1349 assertStringArg ('oldpassword');
1350 if ($accounts[$username]['user_password_hash'] != $_REQUEST['oldpassword'])
1351 return "${root}?page=${pageno}&tab=${tabno}&user_id=${user_id}&error=" . urlencode ('Old password doesn\'t match!');
1352 }
1353 assertStringArg ('newpassword1');
1354 assertStringArg ('newpassword2');
1355 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
1356 return "${root}?page=${pageno}&tab=${tabno}&user_id=${user_id}&error=" . urlencode ('New passwords don\'t match!');
1357 if (commitUpdateUserAccount ($user_id, $accounts[$username]['user_name'], $accounts[$username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
1358 return "${root}?page=${pageno}&tab=${tabno}&user_id=${user_id}&message=" . urlencode ('Password changed successfully.');
1359 else
1360 return "${root}?page=${pageno}&tab=${tabno}&user_id=${user_id}&error=" . urlencode ('DB update failed');
1361}
1362
1363function saveRackCode ()
1364{
1365 global $root, $pageno, $tabno;
1366 assertStringArg ('rackcode');
1367 if (saveScript ('RackCode', $_REQUEST['rackcode']))
1368 return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ('Saved successfully.');
1369 else
1370 return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ('Save failed.');
1371}
1372
e673ee24 1373?>