r2080 + more informative release notes for 0.16.1 (closing ticket:170)
[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 == '')
ebb5441d 108 return buildRedirectURL ('OK');
e673ee24 109 else
ebb5441d 110 return buildRedirectURL ('ERR', array ($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 == '')
ebb5441d 132 return buildRedirectURL ('OK');
e673ee24 133 else
ebb5441d 134 return buildRedirectURL ('ERR', array ($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 == '')
ebb5441d 158 return buildRedirectURL ('OK');
e673ee24 159 else
ebb5441d 160 return buildRedirectURL ('ERR', array ($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']))
ebb5441d 168 return buildRedirectURL ('ERR1');
103b1e1e
DO
169 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
170 if ($error != '')
ebb5441d 171 return buildRedirectURL ('ERR2', array ($error));
e673ee24 172 else
ebb5441d 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']))
ebb5441d 182 return buildRedirectURL ('ERR1');
103b1e1e 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 != '')
ebb5441d 190 return buildRedirectURL ('ERR2', array ($error));
e673ee24 191 else
ebb5441d 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 != '')
ebb5441d 201 return buildRedirectURL ('ERR', array ($error));
e673ee24 202 else
ebb5441d 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 != '')
ebb5441d 216 return buildRedirectURL ('ERR', array ($error));
e673ee24 217 else
ebb5441d 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 != '')
ebb5441d 230 return buildRedirectURL ('ERR', array ($error));
e673ee24 231 else
ebb5441d 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:
ebb5441d 317 return buildRedirectURL ('ERR');
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 }
ebb5441d 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 != '')
ebb5441d 355 return buildRedirectURL ('ERR', array ($error));
e673ee24 356 else
ebb5441d 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 != '')
ebb5441d 367 return buildRedirectURL ('ERR', array ($error));
e673ee24 368 else
ebb5441d 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 != '')
ebb5441d 393 return buildRedirectURL ('ERR', array ($error));
e673ee24 394 else
ebb5441d 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 != '')
ebb5441d 407 return buildRedirectURL ('ERR', $error);
e673ee24 408 else
ebb5441d 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 != '')
ebb5441d 417 return buildRedirectURL ('ERR', $error);
e673ee24 418 else
ebb5441d 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 != '')
ebb5441d 429 return buildRedirectURL ('ERR', array ($error));
e673ee24 430 else
ebb5441d 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 != '')
ebb5441d 445 return buildRedirectURL ('ERR', array ($error));
e673ee24 446 else
ebb5441d 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)
ebb5441d 459 return buildRedirectURL ('OK', array ($username));
e673ee24 460 else
ebb5441d 461 return buildRedirectURL ('ERR', array ($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)
ebb5441d 474 return buildRedirectURL ('ERR1');
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)
ebb5441d 480 return buildRedirectURL ('OK', array ($username));
e673ee24 481 else
ebb5441d 482 return buildRedirectURL ('ERR2', array ($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)
ebb5441d 489 return buildRedirectURL ('OK');
e673ee24 490 else
ebb5441d 491 return buildRedirectURL ('ERR');
e673ee24
DO
492}
493
cced6b7d 494function disableUser ()
e673ee24 495{
cced6b7d 496 assertUIntArg ('user_id', __FUNCTION__);
103b1e1e 497 if ($_REQUEST['user_id'] == 1)
ebb5441d
DO
498 return buildRedirectURL ('ERR1');
499 if (commitEnableUserAccount ($_REQUEST['user_id'], 'no'))
500 return buildRedirectURL ('OK');
e673ee24 501 else
ebb5441d 502 return buildRedirectURL ('ERR2');
e673ee24
DO
503}
504
e673ee24
DO
505// This function find differences in users's submit and PortCompat table
506// and modifies database accordingly.
507function savePortMap ()
508{
e673ee24
DO
509 $ptlist = getPortTypes();
510 $oldCompat = getPortCompat();
511 $newCompat = array();
512 foreach (array_keys ($ptlist) as $leftKey)
513 foreach (array_keys ($ptlist) as $rightKey)
514 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
515 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
516 // Build the new matrix from user's submit and compare it to
517 // the old matrix. Those pairs which appear on
518 // new matrix only, have to be stored in PortCompat table. Those who appear
519 // on the old matrix only, should be removed from PortCompat table.
520 // Those present in both matrices should be skipped.
521 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
522 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
523 $error_count = $success_count = 0;
524 foreach (array_keys ($ptlist) as $type1)
525 foreach (array_keys ($ptlist) as $type2)
526 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
527 switch ($oldCompatTable[$type1][$type2])
528 {
529 case TRUE: // new value is FALSE
530 if (removePortCompat ($type1, $type2) === TRUE)
531 $success_count++;
532 else
533 $error_count++;
534 break;
535 case FALSE: // new value is TRUE
536 if (addPortCompat ($type1, $type2) === TRUE)
537 $success_count++;
538 else
539 $error_count++;
540 break;
541 default:
ebb5441d 542 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
543 break;
544 }
103b1e1e 545 if ($error_count == 0)
ebb5441d 546 return buildRedirectURL ('OK', array ($error_count, $success_count));
103b1e1e 547 else
ebb5441d 548 return buildRedirectURL ('ERR', array ($error_count, $success_count));
e673ee24
DO
549}
550
551function updateDictionary ()
552{
2c6c7645
DO
553 assertUIntArg ('chapter_no', __FUNCTION__);
554 assertUIntArg ('dict_key', __FUNCTION__);
555 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 556 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
ebb5441d 557 return buildRedirectURL ('OK');
e673ee24 558 else
ebb5441d 559 return buildRedirectURL ('ERR');
e673ee24
DO
560}
561
562function supplementDictionary ()
563{
2c6c7645
DO
564 assertUIntArg ('chapter_no', __FUNCTION__);
565 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 566 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
ebb5441d 567 return buildRedirectURL ('OK');
e673ee24 568 else
ebb5441d 569 return buildRedirectURL ('ERR');
e673ee24
DO
570}
571
572function reduceDictionary ()
573{
2c6c7645
DO
574 assertUIntArg ('chapter_no', __FUNCTION__);
575 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24 576 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
ebb5441d 577 return buildRedirectURL ('OK');
e673ee24 578 else
ebb5441d 579 return buildRedirectURL ('ERR');
e673ee24
DO
580}
581
582function addChapter ()
583{
2c6c7645 584 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 585 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
ebb5441d 586 return buildRedirectURL ('OK');
e673ee24 587 else
ebb5441d 588 return buildRedirectURL ('ERR');
e673ee24
DO
589}
590
591function updateChapter ()
592{
2c6c7645
DO
593 assertUIntArg ('chapter_no', __FUNCTION__);
594 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 595 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
ebb5441d 596 return buildRedirectURL ('OK');
e673ee24 597 else
ebb5441d 598 return buildRedirectURL ('ERR');
e673ee24
DO
599}
600
601function delChapter ()
602{
2c6c7645 603 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 604 if (commitDeleteChapter ($_REQUEST['chapter_no']))
ebb5441d 605 return buildRedirectURL ('OK');
e673ee24 606 else
ebb5441d 607 return buildRedirectURL ('ERR');
e673ee24
DO
608}
609
610function changeAttribute ()
611{
2c6c7645
DO
612 assertUIntArg ('attr_id', __FUNCTION__);
613 assertStringArg ('attr_name', __FUNCTION__);
e673ee24 614 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
ebb5441d 615 return buildRedirectURL ('OK');
e673ee24 616 else
ebb5441d 617 return buildRedirectURL ('ERR');
e673ee24
DO
618}
619
620function createAttribute ()
621{
2c6c7645
DO
622 assertStringArg ('attr_name', __FUNCTION__);
623 assertStringArg ('attr_type', __FUNCTION__);
e673ee24 624 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
ebb5441d 625 return buildRedirectURL ('OK', array ($_REQUEST['attr_name']));
e673ee24 626 else
ebb5441d 627 return buildRedirectURL ('ERR');
e673ee24
DO
628}
629
630function deleteAttribute ()
631{
2c6c7645 632 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24 633 if (commitDeleteAttribute ($_REQUEST['attr_id']))
ebb5441d 634 return buildRedirectURL ('OK');
e673ee24 635 else
ebb5441d 636 return buildRedirectURL ('ERR');
e673ee24
DO
637}
638
639function supplementAttrMap ()
640{
2c6c7645
DO
641 assertUIntArg ('attr_id', __FUNCTION__);
642 assertUIntArg ('objtype_id', __FUNCTION__);
643 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 644 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
ebb5441d 645 return buildRedirectURL ('OK');
e673ee24 646 else
ebb5441d 647 return buildRedirectURL ('ERR');
e673ee24
DO
648}
649
650function reduceAttrMap ()
651{
2c6c7645
DO
652 assertUIntArg ('attr_id', __FUNCTION__);
653 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24 654 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
ebb5441d 655 return buildRedirectURL ('OK');
e673ee24 656 else
ebb5441d 657 return buildRedirectURL ('ERR');
e673ee24
DO
658}
659
22bb04da 660function clearSticker ()
e673ee24 661{
2c6c7645
DO
662 assertUIntArg ('attr_id', __FUNCTION__);
663 assertUIntArg ('object_id', __FUNCTION__);
22bb04da 664 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
ebb5441d 665 return buildRedirectURL ('OK');
e673ee24 666 else
ebb5441d 667 return buildRedirectURL ('ERR');
e673ee24
DO
668}
669
22bb04da
DO
670function updateObject ()
671{
672 assertUIntArg ('object_id', __FUNCTION__);
673 assertUIntArg ('object_type_id', __FUNCTION__);
674 assertStringArg ('object_name', __FUNCTION__, TRUE);
675 assertStringArg ('object_label', __FUNCTION__, TRUE);
676 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
677 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
678 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
679 $has_problems = 'yes';
680 else
681 $has_problems = 'no';
682
683 if (commitUpdateObject (
684 $_REQUEST['object_id'],
685 $_REQUEST['object_name'],
686 $_REQUEST['object_label'],
687 $_REQUEST['object_barcode'],
688 $_REQUEST['object_type_id'],
689 $has_problems,
690 $_REQUEST['object_asset_no'],
691 $_REQUEST['object_comment']
692 ) !== TRUE)
ebb5441d 693 return buildRedirectURL ('ERR');
22bb04da
DO
694 // Invalidate thumb cache of all racks objects could occupy.
695 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
696 resetThumbCache ($rack_id);
ebb5441d 697 return buildRedirectURL ('OK');
22bb04da
DO
698}
699
700function updateStickers ()
e673ee24 701{
2c6c7645 702 assertUIntArg ('object_id', __FUNCTION__);
f59e8cf5
DO
703 assertUIntArg ('num_attrs', __FUNCTION__);
704 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a
DO
705 $result = array();
706
103b1e1e 707 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
0dfb8a2a 708 {
2c6c7645 709 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
710 $attr_id = $_REQUEST["${i}_attr_id"];
711
712 // Field is empty, delete attribute and move on.
713 if (empty($_REQUEST["${i}_value"]))
714 {
103b1e1e 715 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
716 continue;
717 }
718
719 // The value could be uint/float, but we don't know ATM. Let SQL
720 // server check this and complain.
2c6c7645 721 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
722 $value = $_REQUEST["${i}_value"];
723 switch ($oldvalues[$attr_id]['type'])
724 {
725 case 'uint':
726 case 'float':
727 case 'string':
728 $oldvalue = $oldvalues[$attr_id]['value'];
729 break;
730 case 'dict':
731 $oldvalue = $oldvalues[$attr_id]['key'];
732 break;
733 default:
61e269b5 734 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
735 die;
736 }
737 if ($value == $oldvalue)
738 continue;
739
740 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 741 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a
DO
742 }
743
103b1e1e 744 if (in_array (FALSE, $result))
ebb5441d 745 return buildRedirectURL ('ERR');
0dfb8a2a 746
ebb5441d 747 return buildRedirectURL ('OK');
e673ee24
DO
748}
749
750function useupPort ()
751{
2c6c7645 752 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 753 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
ebb5441d 754 return buildRedirectURL ('OK');
e673ee24 755 else
ebb5441d 756 return buildRedirectURL ('ERR');
e673ee24
DO
757}
758
4fe32e78
DY
759function updateUI ()
760{
2c6c7645 761 assertUIntArg ('num_vars', __FUNCTION__);
c461c579 762 $error = '';
4fe32e78 763
103b1e1e 764 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 765 {
2c6c7645
DO
766 assertStringArg ("${i}_varname", __FUNCTION__);
767 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 768 $varname = $_REQUEST["${i}_varname"];
4fe32e78 769 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 770
c461c579
DO
771 // If form value = value in DB, don't bother updating DB
772 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
773 continue;
774
775 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
776 $error = setConfigVar ($varname, $varvalue, TRUE);
777 if (!empty ($error))
778 break;
4fe32e78
DY
779 }
780
c461c579 781 if ($error != '')
ebb5441d 782 return buildRedirectURL ('ERR', array ($error));
4fe32e78 783
ebb5441d 784 return buildRedirectURL ('OK');
4fe32e78
DY
785}
786
b07f617c
DO
787function resetUIConfig()
788{
1f86e996 789 setConfigVar ('default_port_type','24');
b07f617c
DO
790 setConfigVar ('MASSCOUNT','15');
791 setConfigVar ('MAXSELSIZE','30');
792 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
793 setConfigVar ('ROW_SCALE','2');
794 setConfigVar ('PORTS_PER_ROW','12');
795 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
796 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 797 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
798 setConfigVar ('USER_AUTH_SRC','database');
799 setConfigVar ('DEFAULT_SLB_VS_PORT','');
800 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 801 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 802 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 803 setConfigVar ('DETECT_URLS','no');
4b8d413e 804 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 805 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 806 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
807 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
808 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
809 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 810 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 811 setConfigVar ('IPV4_AUTO_RELEASE','1');
ebb5441d 812 return buildRedirectURL ('OK');
b07f617c
DO
813}
814
732e4578 815// Add single record.
ca461127
DO
816function addRealServer ()
817{
2c6c7645
DO
818 assertUIntArg ('pool_id', __FUNCTION__);
819 assertIPv4Arg ('remoteip', __FUNCTION__);
820 assertUIntArg ('rsport', __FUNCTION__);
821 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
822 if (!addRStoRSPool (
823 $_REQUEST['pool_id'],
824 $_REQUEST['remoteip'],
825 $_REQUEST['rsport'],
826 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
827 $_REQUEST['rsconfig']
828 ))
ebb5441d 829 return buildRedirectURL ('ERR');
d6517a21 830 else
ebb5441d 831 return buildRedirectURL ('OK');
d6517a21
DO
832}
833
732e4578
DO
834// Parse textarea submitted and try adding a real server for each line.
835function addRealServers ()
836{
cbadbb38 837 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
838 assertStringArg ('format', __FUNCTION__);
839 assertStringArg ('rawtext', __FUNCTION__);
732e4578
DO
840 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
841 $ngood = $nbad = 0;
842 $rsconfig = '';
843 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
844 foreach (explode ('\n', $rawtext) as $line)
845 {
846 if (empty ($line))
847 continue;
848 $match = array ();
849 switch ($_REQUEST['format'])
850 {
851 case 'ipvs_2': // address and port only
852 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
853 continue;
103b1e1e 854 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
855 $ngood++;
856 else
857 $nbad++;
858 break;
859 case 'ipvs_3': // address, port and weight
860 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
861 continue;
103b1e1e 862 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
863 $ngood++;
864 else
865 $nbad++;
866 break;
e69c2aa3
DO
867 case 'ssv_2': // IP address and port
868 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
869 continue;
103b1e1e 870 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
871 $ngood++;
872 else
873 $nbad++;
874 break;
732e4578 875 default:
ebb5441d 876 return buildRedirectURL ('ERR1');
732e4578
DO
877 break;
878 }
879 }
880 if ($nbad == 0 and $ngood > 0)
ebb5441d 881 return buildRedirectURL ('OK', array ($ngood));
732e4578 882 else
ebb5441d 883 return buildRedirectURL ('ERR2', array ($ngood, $nbad));
732e4578
DO
884}
885
d6517a21
DO
886function addVService ()
887{
2c6c7645
DO
888 assertIPv4Arg ('vip', __FUNCTION__);
889 assertUIntArg ('vport', __FUNCTION__);
890 assertStringArg ('proto', __FUNCTION__);
103b1e1e 891 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
ebb5441d 892 return buildRedirectURL ('ERR1');
2c6c7645
DO
893 assertStringArg ('name', __FUNCTION__, TRUE);
894 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
895 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
896 $error = commitCreateVS
897 (
103b1e1e
DO
898 $_REQUEST['vip'],
899 $_REQUEST['vport'],
7320b661 900 $_REQUEST['proto'],
103b1e1e
DO
901 $_REQUEST['name'],
902 $_REQUEST['vsconfig'],
c63a8d6e
DO
903 $_REQUEST['rsconfig'],
904 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
905 );
906 if ($error != '')
ebb5441d 907 return buildRedirectURL ('ERR2', array ($error));
ca461127 908 else
ebb5441d 909 return buildRedirectURL ('OK');
fb1c4a54
DO
910}
911
912function deleteRealServer ()
913{
2c6c7645 914 assertUIntArg ('id', __FUNCTION__);
d6517a21 915 if (!commitDeleteRS ($_REQUEST['id']))
ebb5441d 916 return buildRedirectURL ('ERR');
fb1c4a54 917 else
ebb5441d 918 return buildRedirectURL ('OK');
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 ))
ebb5441d 931 return buildRedirectURL ('ERR');
d6517a21 932 else
ebb5441d 933 return buildRedirectURL ('OK');
d6517a21
DO
934}
935
936function deleteVService ()
937{
e02e1941
DO
938 assertUIntArg ('vs_id', __FUNCTION__);
939 if (!commitDeleteVS ($_REQUEST['vs_id']))
ebb5441d 940 return buildRedirectURL ('ERR');
3241551e 941 else
ebb5441d 942 return buildRedirectURL ('OK');
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 ))
ebb5441d 957 return buildRedirectURL ('ERR');
fb1c4a54 958 else
ebb5441d 959 return buildRedirectURL ('OK');
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 ))
ebb5441d 976 return buildRedirectURL ('ERR');
d6517a21 977 else
ebb5441d 978 return buildRedirectURL ('OK');
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 ))
ebb5441d 999 return buildRedirectURL ('ERR');
3241551e 1000 else
ebb5441d 1001 return buildRedirectURL ('OK');
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 ))
ebb5441d 1018 return buildRedirectURL ('ERR');
3241551e 1019 else
ebb5441d 1020 return buildRedirectURL ('OK');
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);
c63a8d6e
DO
1028 $error = commitCreateRSPool
1029 (
103b1e1e
DO
1030 $_REQUEST['name'],
1031 $_REQUEST['vsconfig'],
c63a8d6e
DO
1032 $_REQUEST['rsconfig'],
1033 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1034 );
1035 if ($error != '')
ebb5441d 1036 return buildRedirectURL ('ERR', array ($error));
5ad76f01 1037 else
ebb5441d 1038 return buildRedirectURL ('OK');
5ad76f01
DO
1039}
1040
1041function deleteRSPool ()
1042{
2c6c7645 1043 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1044 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
ebb5441d 1045 return buildRedirectURL ('ERR');
5ad76f01 1046 else
ebb5441d 1047 return buildRedirectURL ('OK');
5ad76f01
DO
1048}
1049
1050function updateRSPool ()
1051{
2c6c7645
DO
1052 assertUIntArg ('pool_id', __FUNCTION__);
1053 assertStringArg ('name', __FUNCTION__, TRUE);
1054 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1055 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1056 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
ebb5441d 1057 return buildRedirectURL ('ERR');
5ad76f01 1058 else
ebb5441d 1059 return buildRedirectURL ('OK');
5ad76f01
DO
1060}
1061
1f7d18fa
DO
1062function updateRSInService ()
1063{
2c6c7645 1064 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1065 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1066 $orig = getRSPoolInfo ($pool_id);
1067 $nbad = $ngood = 0;
1068 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1069 {
1070 $rs_id = $_REQUEST["rsid_${i}"];
1071 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1072 $newval = 'yes';
1073 else
1074 $newval = 'no';
1075 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1076 {
1077 if (commitSetInService ($rs_id, $newval))
1078 $ngood++;
1079 else
1080 $nbad++;
1081 }
1082 }
1083 if (!$nbad)
ebb5441d 1084 return buildRedirectURL ('OK', array ($ngood));
1f7d18fa 1085 else
ebb5441d 1086 return buildRedirectURL ('ERR', array ($nbad, $ngood));
1f7d18fa
DO
1087}
1088
03eb5209
DO
1089function importPTRData ()
1090{
2c6c7645 1091 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209 1092 $nbad = $ngood = 0;
03eb5209
DO
1093 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1094 {
3f3bd41e 1095 $inputname = "import_${i}";
03eb5209
DO
1096 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1097 continue;
2c6c7645
DO
1098 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1099 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1100 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1101 // Non-existent addresses will not have this argument set in request.
1102 $rsvd = 'no';
03eb5209
DO
1103 if ($_REQUEST["rsvd_${i}"] == 'yes')
1104 $rsvd = 'yes';
3f3bd41e 1105 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1106 $ngood++;
1107 else
1108 $nbad++;
1109 }
1110 if (!$nbad)
ebb5441d 1111 return buildRedirectURL ('OK', array ($ngood));
03eb5209 1112 else
ebb5441d 1113 return buildRedirectURL ('ERR', array ($nbad, $ngood));
03eb5209
DO
1114}
1115
f3f0161f
DO
1116function generateAutoPorts ()
1117{
103b1e1e 1118 global $pageno;
2c6c7645 1119 assertUIntArg ('object_id', __FUNCTION__);
103b1e1e 1120 $info = getObjectInfo ($_REQUEST['object_id']);
f3f0161f 1121 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1122 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
ebb5441d 1123 return buildRedirectURL ('OK', array(), $pageno, 'ports');
f3f0161f 1124 else
ebb5441d 1125 return buildRedirectURL ('ERR');
f3f0161f
DO
1126}
1127
24cbe8af 1128// Filter out implicit tags before storing the new tag set.
2034d968 1129function saveEntityTags ($realm, $bypass)
24cbe8af 1130{
103b1e1e 1131 global $explicit_tags, $implicit_tags;
2c6c7645 1132 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1133 $entity_id = $_REQUEST[$bypass];
e04931c8 1134 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1135 // Build a chain from the submitted data, minimize it,
ab379543 1136 // then wipe existing records and store the new set instead.
49fb1027 1137 deleteTagsForEntity ($realm, $entity_id);
6e49bd1f 1138 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1139 $n_succeeds = $n_errors = 0;
6e49bd1f 1140 foreach ($newchain as $taginfo)
eb6ea26f 1141 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1142 $n_succeeds++;
1143 else
1144 $n_errors++;
ab379543 1145 if ($n_errors)
ebb5441d 1146 return buildRedirectURL ('ERR', array ($n_succeeds, $n_errors));
ab379543 1147 else
ebb5441d 1148 return buildRedirectURL ('OK', array ($n_succeeds));
2034d968
DO
1149}
1150
1151function saveObjectTags ()
1152{
1153 return saveEntityTags ('object', 'object_id');
1154}
1155
1156function saveIPv4PrefixTags ()
1157{
1158 return saveEntityTags ('ipv4net', 'id');
24cbe8af
DO
1159}
1160
41bd8c87
DO
1161function saveRackTags ()
1162{
1163 return saveEntityTags ('rack', 'rack_id');
1164}
1165
d817ba03
DO
1166function saveIPv4VSTags ()
1167{
1a4beb9e 1168 return saveEntityTags ('ipv4vs', 'vs_id');
d817ba03
DO
1169}
1170
1171function saveIPv4RSPoolTags ()
1172{
2e4170aa 1173 return saveEntityTags ('ipv4rspool', 'pool_id');
d817ba03
DO
1174}
1175
cced6b7d
DO
1176function saveUserTags ()
1177{
1178 return saveEntityTags ('user', 'user_id');
1179}
1180
fe7044ad
DO
1181function destroyTag ()
1182{
49fb1027
DO
1183 assertUIntArg ('tag_id', __FUNCTION__);
1184 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
ebb5441d 1185 return buildRedirectURL ('OK');
fe7044ad 1186 else
ebb5441d 1187 return buildRedirectURL ('ERR');
fe7044ad
DO
1188}
1189
1190function createTag ()
1191{
49fb1027 1192 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1193 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1194 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1195 if (!validTagName ($tagname))
ebb5441d 1196 return buildRedirectURL ('ERR1', array ($tagname));
0f3c6639 1197 if (tagExistsInDatabase ($tagname))
ebb5441d 1198 return buildRedirectURL ('ERR2', array ($tagname));
fe7044ad
DO
1199 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1200 $parent_id = 'NULL';
1201 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
ebb5441d 1202 return buildRedirectURL ('OK', array ($tagname));
fe7044ad 1203 else
ebb5441d 1204 return buildRedirectURL ('ERR', array ($tagname, $ret));
fe7044ad
DO
1205}
1206
49fb1027
DO
1207function updateTag ()
1208{
49fb1027
DO
1209 assertUIntArg ('tag_id', __FUNCTION__);
1210 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1211 assertStringArg ('tag_name', __FUNCTION__);
1212 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1213 if (!validTagName ($tagname))
ebb5441d 1214 return buildRedirectURL ('ERR');
49fb1027
DO
1215 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1216 $parent_id = 'NULL';
1217 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
ebb5441d 1218 return buildRedirectURL ('OK', array ($tagname));
49fb1027 1219 else
ebb5441d 1220 return buildRedirectURL ('ERR', array ($tagname, $ret));
49fb1027
DO
1221}
1222
eb6ea26f
DO
1223function rollTags ()
1224{
eb6ea26f
DO
1225 assertUIntArg ('row_id', __FUNCTION__);
1226 assertStringArg ('sum', __FUNCTION__, TRUE);
1227 assertUIntArg ('realsum', __FUNCTION__);
1228 $row_id = $_REQUEST['row_id'];
1229 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
ebb5441d 1230 return buildRedirectURL ('ERR');
eb6ea26f
DO
1231 $racks = getRacksForRow ($row_id);
1232 // Each time addTagForEntity() fails we assume it was just because of already existing record in its way.
1233 $newtags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1234 $tagstack = getExplicitTagsOnly (buildTagChainFromIds ($newtags));
eb6ea26f
DO
1235 $ndupes = $nnew = 0;
1236 foreach ($tagstack as $taginfo)
1237 foreach ($racks as $rackInfo)
1238 {
1239 if (addTagForEntity ('rack', $rackInfo['id'], $taginfo['id']))
1240 $nnew++;
1241 else
1242 $ndupes++;
1243 // FIXME: do something likewise for all object inside current rack
1244 }
e586fd68 1245 return buildRedirectURL ('OK', array ($nnew, $ndupes));
eb6ea26f
DO
1246}
1247
9457ca59 1248function changeMyPassword ()
cced6b7d 1249{
103b1e1e 1250 global $accounts, $remote_username;
9457ca59 1251 if (getConfigVar ('USER_AUTH_SRC') != 'database')
ebb5441d 1252 return buildRedirectURL ('ERR1');
0ac7e4b3
DO
1253 assertStringArg ('oldpassword', __FUNCTION__);
1254 assertStringArg ('newpassword1', __FUNCTION__);
1255 assertStringArg ('newpassword2', __FUNCTION__);
1256 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
ebb5441d 1257 return buildRedirectURL ('ERR2');
cced6b7d 1258 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
ebb5441d 1259 return buildRedirectURL ('ERR3');
0ac7e4b3 1260 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
ebb5441d 1261 return buildRedirectURL ('OK');
cced6b7d 1262 else
ebb5441d 1263 return buildRedirectURL ('ERR4');
cced6b7d
DO
1264}
1265
1266function saveRackCode ()
1267{
cced6b7d 1268 assertStringArg ('rackcode');
e6a4adb9
DO
1269 // For the test to succeed, unescape LFs, strip CRs.
1270 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1271 $parseTree = getRackCode ($newcode);
1272 if ($parseTree['result'] != 'ACK')
ebb5441d 1273 return buildRedirectURL ('ERR1', array ($parseTree['load']));
4a6a28f1 1274 saveScript ('RackCodeCache', '');
cf25e649 1275 if (saveScript ('RackCode', $newcode))
ebb5441d 1276 return buildRedirectURL ('OK');
cced6b7d 1277 else
ebb5441d 1278 return buildRedirectURL ('ERR2');
cced6b7d
DO
1279}
1280
46f92ff7
DO
1281// This handler's context is pre-built, but not authorized. It is assumed, that the
1282// handler will take existing context and before each commit check authorization
1283// on the base chain plus necessary context added.
1284function setPortVLAN ()
1285{
1286 assertUIntArg ('portcount', __FUNCTION__);
1287 $data = getSwitchVLANs ($_REQUEST['object_id']);
1288 if ($data === NULL)
ebb5441d 1289 return buildRedirectURL ('ERR1');
46f92ff7
DO
1290 list ($vlanlist, $portlist) = $data;
1291 // Here we just build up 1 set command for the gateway with all of the ports
1292 // included. The gateway is expected to filter unnecessary changes silently
1293 // and to provide a list of responses with either error or success message
1294 // for each of the rest.
1295 $nports = $_REQUEST['portcount'];
1296 $prefix = 'set ';
24dcb9d8 1297 $log = array ('v' => 2);
46f92ff7
DO
1298 $setcmd = '';
1299 for ($i = 0; $i < $nports; $i++)
1300 if
1301 (
1302 !isset ($_REQUEST['portname_' . $i]) ||
1303 !isset ($_REQUEST['vlanid_' . $i]) ||
1304 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1305 )
24dcb9d8 1306 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1307 elseif
1308 (
1309 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1310 $portlist[$i]['vlaind'] == 'TRUNK'
1311 )
1312 continue;
1313 else
1314 {
1315 $portname = $_REQUEST['portname_' . $i];
1316 $oldvlanid = $portlist[$i]['vlanid'];
1317 $newvlanid = $_REQUEST['vlanid_' . $i];
1318 // Finish the security context and evaluate it.
1319 $annex = array();
1320 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1321 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1322 if (!permitted (NULL, NULL, NULL, $annex))
1323 {
24dcb9d8 1324 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1325 continue;
1326 }
1327 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1328 $prefix = ';';
1329 }
1330 // Feed the gateway and interpret its (non)response.
1331 if ($setcmd != '')
24dcb9d8 1332 $log['m'] = array_merge ($log, setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1333 else
24dcb9d8 1334 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1335 return buildWideRedirectURL ($log);
1336}
1337
e673ee24 1338?>