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