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