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