r2650 - don't display preselect above tag tree, cause it's already highlighted
[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 == '')
135080d8 108 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 109 else
135080d8 110 return buildRedirectURL (__FUNCTION__, '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 == '')
135080d8 132 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 133 else
135080d8 134 return buildRedirectURL (__FUNCTION__, '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 == '')
135080d8 158 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 159 else
135080d8 160 return buildRedirectURL (__FUNCTION__, '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']))
135080d8 168 return buildRedirectURL (__FUNCTION__, '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 != '')
135080d8 171 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 172 else
135080d8 173 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
174}
175
176function editPortForObject ()
177{
2c6c7645 178 assertUIntArg ('port_id', __FUNCTION__);
611170af 179 assertUIntArg ('port_type_id', __FUNCTION__);
d0a69ce8 180 // tolerate empty value now to produce custom informative message later
103b1e1e
DO
181 assertStringArg ('name', __FUNCTION__, TRUE);
182 if (empty ($_REQUEST['name']))
135080d8 183 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e 184
e673ee24
DO
185 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
186 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
187 else
188 $port_rc = 'NULL';
611170af 189 $error = commitUpdatePort ($_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['port_type_id'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
103b1e1e 190 if ($error != '')
135080d8 191 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 192 else
135080d8 193 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e673ee24
DO
194}
195
196function delPortFromObject ()
197{
103b1e1e 198 assertUIntArg ('port_id', __FUNCTION__);
103b1e1e 199 $error = delObjectPort ($_REQUEST['port_id']);
e673ee24
DO
200
201 if ($error != '')
135080d8 202 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 203 else
135080d8 204 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
205}
206
207function linkPortForObject ()
208{
103b1e1e
DO
209 assertUIntArg ('port_id', __FUNCTION__);
210 assertUIntArg ('remote_port_id', __FUNCTION__);
baf37d01
DO
211 assertStringArg ('port_name', __FUNCTION__, TRUE);
212 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
213 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 214
103b1e1e 215 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
e673ee24 216 if ($error != '')
135080d8 217 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 218 else
135080d8 219 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
220}
221
222function unlinkPortForObject ()
223{
103b1e1e 224 assertUIntArg ('port_id', __FUNCTION__);
baf37d01
DO
225 assertStringArg ('port_name', __FUNCTION__, TRUE);
226 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
227 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 228
103b1e1e 229 $error = unlinkPort ($_REQUEST['port_id']);
e673ee24 230 if ($error != '')
135080d8 231 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 232 else
135080d8 233 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
234}
235
236function addMultiPorts ()
237{
2c6c7645
DO
238 assertStringArg ('format', __FUNCTION__);
239 assertStringArg ('input', __FUNCTION__);
240 assertUIntArg ('port_type', __FUNCTION__);
241 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
242 $format = $_REQUEST['format'];
243 $port_type = $_REQUEST['port_type'];
244 $object_id = $_REQUEST['object_id'];
245 // Input lines are escaped, so we have to explode and to chop by 2-char
246 // \n and \r respectively.
247 $lines1 = explode ('\n', $_REQUEST['input']);
248 foreach ($lines1 as $line)
249 {
250 $parts = explode ('\r', $line);
251 reset ($parts);
252 if (empty ($parts[0]))
253 continue;
254 else
255 $lines2[] = rtrim ($parts[0]);
256 }
257 $ports = array();
258 foreach ($lines2 as $line)
259 {
260 switch ($format)
261 {
262 case 'fisxii':
263 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
264 list ($slot, $port) = explode ('/', $words[0]);
265 $ports[] = array
266 (
267 'name' => "e ${slot}/${port}",
268 'l2address' => $words[8],
269 'label' => "slot ${slot} port ${port}"
270 );
271 break;
272 case 'c3600asy':
273 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
274/*
275How Async Lines are Numbered in Cisco 3600 Series Routers
276http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
277
278Understanding 16- and 32-Port Async Network Modules
279http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
280*/
281 $async = $words[0];
282 $slot = floor (($async - 1) / 32);
283 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
284 $cable = $async - $slot * 32 - $octalgroup * 8;
285 $og_label[0] = 'async 0-7';
286 $og_label[1] = 'async 8-15';
287 $og_label[2] = 'async 16-23';
288 $og_label[3] = 'async 24-31';
289 $ports[] = array
290 (
291 'name' => "async ${async}",
292 'l2address' => '',
293 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
294 );
295 break;
296 case 'fiwg':
297 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
298 $ifnumber = $words[0] * 1;
299 $ports[] = array
300 (
301 'name' => "e ${ifnumber}",
302 'l2address' => "${words[8]}",
303 'label' => "${ifnumber}"
304 );
305 break;
351d4dbf 306 case 'ssv1':
5b585342 307 $words = explode (' ', $line);
351d4dbf
DO
308 if (empty ($words[0]) or empty ($words[1]))
309 continue;
310 $ports[] = array
311 (
312 'name' => $words[0],
313 'l2address' => $words[1],
314 'label' => ''
315 );
316 break;
e673ee24 317 default:
135080d8 318 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
319 break;
320 }
321 }
322 // Create ports, if they don't exist.
323 $added_count = $updated_count = $error_count = 0;
324 foreach ($ports as $port)
325 {
326 $port_id = getPortID ($object_id, $port['name']);
327 if ($port_id === NULL)
328 {
329 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
330 if ($result == '')
331 $added_count++;
332 else
333 $error_count++;
334 }
335 else
336 {
611170af 337 $result = commitUpdatePort ($port_id, $port['name'], $port_type, $port['label'], $port['l2address']);
e673ee24
DO
338 if ($result == '')
339 $updated_count++;
340 else
341 $error_count++;
342 }
343 }
135080d8 344 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $updated_count, $error_count));
e673ee24
DO
345}
346
b4c1ef87 347function updIPv4Allocation ()
e673ee24 348{
b4c1ef87
DO
349 assertIPv4Arg ('ip', __FUNCTION__);
350 assertUIntArg ('object_id', __FUNCTION__);
351 assertStringArg ('bond_name', __FUNCTION__, TRUE);
352 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 353
b4c1ef87 354 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 355 if ($error != '')
135080d8 356 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 357 else
135080d8 358 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
359}
360
b4c1ef87 361function delIPv4Allocation ()
e673ee24 362{
b4c1ef87
DO
363 assertIPv4Arg ('ip', __FUNCTION__);
364 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 365
b4c1ef87 366 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 367 if ($error != '')
135080d8 368 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 369 else
135080d8 370 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
371}
372
b4c1ef87 373function addIPv4Allocation ()
e673ee24 374{
b4c1ef87
DO
375 assertIPv4Arg ('ip', __FUNCTION__);
376 assertUIntArg ('object_id', __FUNCTION__);
377 assertStringArg ('bond_name', __FUNCTION__, TRUE);
378 assertStringArg ('bond_type', __FUNCTION__);
da958e52 379
b4c1ef87
DO
380 // Strip masklen.
381 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
2be3fd23 382 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
135080d8 383 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
5222f192 384
b4c1ef87 385 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
5222f192 386 if ($error != '')
135080d8 387 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
53ef3908
DO
388 $address = getIPv4Address ($ip);
389 if ($address['reserved'] == 'yes' or !empty ($address['name']))
b4c1ef87
DO
390 {
391 $release = getConfigVar ('IPV4_AUTO_RELEASE');
392 if ($release >= 1)
393 $address['reserved'] = 'no';
394 if ($release >= 2)
395 $address['name'] = '';
396 updateAddress ($ip, $address['name'], $address['reserved']);
397 }
135080d8 398 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
399}
400
42023f03 401function addIPv4Prefix ()
e673ee24 402{
2c6c7645 403 assertStringArg ('range', __FUNCTION__);
e4b9a099 404 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 405
fa05e3de
DO
406 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
407 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 408 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 409 if ($error != '')
135080d8 410 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 411 else
135080d8 412 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
413}
414
42023f03 415function delIPv4Prefix ()
e673ee24 416{
da958e52 417 assertUIntArg ('id', __FUNCTION__);
42023f03 418 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 419 if ($error != '')
135080d8 420 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 421 else
135080d8 422 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
423}
424
04d619d0 425function updIPv4Prefix ()
e673ee24 426{
da958e52 427 assertUIntArg ('id', __FUNCTION__);
e4b9a099 428 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 429
da958e52 430 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
e673ee24 431 if ($error != '')
135080d8 432 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 433 else
135080d8 434 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
435}
436
437function editAddress ()
438{
da958e52
DO
439 assertIPv4Arg ('ip', __FUNCTION__);
440 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 441
e673ee24
DO
442 if (isset ($_REQUEST['reserved']))
443 $reserved = $_REQUEST['reserved'];
444 else
445 $reserved = 'off';
da958e52 446 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 447 if ($error != '')
135080d8 448 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 449 else
135080d8 450 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
451}
452
cced6b7d 453function createUser ()
e673ee24 454{
2c6c7645
DO
455 assertStringArg ('username', __FUNCTION__);
456 assertStringArg ('realname', __FUNCTION__, TRUE);
457 assertStringArg ('password', __FUNCTION__);
e673ee24 458 $username = $_REQUEST['username'];
93bdb7ba 459 $password = sha1 ($_REQUEST['password']);
e673ee24
DO
460 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
461 if ($result == TRUE)
135080d8 462 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 463 else
135080d8 464 return buildRedirectURL (__FUNCTION__, 'ERR', array ($username));
e673ee24
DO
465}
466
cced6b7d 467function updateUser ()
e673ee24 468{
cced6b7d 469 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
470 assertStringArg ('username', __FUNCTION__);
471 assertStringArg ('realname', __FUNCTION__, TRUE);
472 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
473 $username = $_REQUEST['username'];
474 $new_password = $_REQUEST['password'];
103b1e1e 475 $old_hash = getHashByID ($_REQUEST['user_id']);
e673ee24 476 if ($old_hash == NULL)
135080d8 477 return buildRedirectURL (__FUNCTION__, 'ERR1');
e673ee24
DO
478 // Update user password only if provided password is not the same as current password hash.
479 if ($new_password != $old_hash)
93bdb7ba 480 $new_password = sha1 ($new_password);
103b1e1e 481 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
e673ee24 482 if ($result == TRUE)
135080d8 483 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 484 else
135080d8 485 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($username));
e673ee24
DO
486}
487
e673ee24
DO
488// This function find differences in users's submit and PortCompat table
489// and modifies database accordingly.
490function savePortMap ()
491{
e673ee24
DO
492 $ptlist = getPortTypes();
493 $oldCompat = getPortCompat();
494 $newCompat = array();
495 foreach (array_keys ($ptlist) as $leftKey)
496 foreach (array_keys ($ptlist) as $rightKey)
497 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
498 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
499 // Build the new matrix from user's submit and compare it to
500 // the old matrix. Those pairs which appear on
501 // new matrix only, have to be stored in PortCompat table. Those who appear
502 // on the old matrix only, should be removed from PortCompat table.
503 // Those present in both matrices should be skipped.
504 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
505 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
506 $error_count = $success_count = 0;
507 foreach (array_keys ($ptlist) as $type1)
508 foreach (array_keys ($ptlist) as $type2)
509 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
510 switch ($oldCompatTable[$type1][$type2])
511 {
512 case TRUE: // new value is FALSE
513 if (removePortCompat ($type1, $type2) === TRUE)
514 $success_count++;
515 else
516 $error_count++;
517 break;
518 case FALSE: // new value is TRUE
519 if (addPortCompat ($type1, $type2) === TRUE)
520 $success_count++;
521 else
522 $error_count++;
523 break;
524 default:
ebb5441d 525 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
526 break;
527 }
103b1e1e 528 if ($error_count == 0)
135080d8 529 return buildRedirectURL (__FUNCTION__, 'OK', array ($error_count, $success_count));
103b1e1e 530 else
135080d8 531 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error_count, $success_count));
e673ee24
DO
532}
533
534function updateDictionary ()
535{
2c6c7645
DO
536 assertUIntArg ('chapter_no', __FUNCTION__);
537 assertUIntArg ('dict_key', __FUNCTION__);
538 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 539 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
135080d8 540 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 541 else
135080d8 542 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
543}
544
545function supplementDictionary ()
546{
2c6c7645
DO
547 assertUIntArg ('chapter_no', __FUNCTION__);
548 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 549 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
135080d8 550 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 551 else
135080d8 552 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
553}
554
555function reduceDictionary ()
556{
2c6c7645
DO
557 assertUIntArg ('chapter_no', __FUNCTION__);
558 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24 559 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
135080d8 560 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 561 else
135080d8 562 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
563}
564
565function addChapter ()
566{
2c6c7645 567 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 568 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
135080d8 569 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 570 else
135080d8 571 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
572}
573
574function updateChapter ()
575{
2c6c7645
DO
576 assertUIntArg ('chapter_no', __FUNCTION__);
577 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 578 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
135080d8 579 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 580 else
135080d8 581 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
582}
583
584function delChapter ()
585{
2c6c7645 586 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 587 if (commitDeleteChapter ($_REQUEST['chapter_no']))
135080d8 588 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 589 else
135080d8 590 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
591}
592
593function changeAttribute ()
594{
2c6c7645
DO
595 assertUIntArg ('attr_id', __FUNCTION__);
596 assertStringArg ('attr_name', __FUNCTION__);
e673ee24 597 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
135080d8 598 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 599 else
135080d8 600 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
601}
602
603function createAttribute ()
604{
2c6c7645
DO
605 assertStringArg ('attr_name', __FUNCTION__);
606 assertStringArg ('attr_type', __FUNCTION__);
e673ee24 607 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
135080d8 608 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
e673ee24 609 else
135080d8 610 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
611}
612
613function deleteAttribute ()
614{
2c6c7645 615 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24 616 if (commitDeleteAttribute ($_REQUEST['attr_id']))
135080d8 617 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 618 else
135080d8 619 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
620}
621
622function supplementAttrMap ()
623{
2c6c7645
DO
624 assertUIntArg ('attr_id', __FUNCTION__);
625 assertUIntArg ('objtype_id', __FUNCTION__);
626 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 627 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
135080d8 628 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 629 else
135080d8 630 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
631}
632
633function reduceAttrMap ()
634{
2c6c7645
DO
635 assertUIntArg ('attr_id', __FUNCTION__);
636 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24 637 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
135080d8 638 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 639 else
135080d8 640 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
641}
642
22bb04da 643function clearSticker ()
e673ee24 644{
2c6c7645
DO
645 assertUIntArg ('attr_id', __FUNCTION__);
646 assertUIntArg ('object_id', __FUNCTION__);
22bb04da 647 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
135080d8 648 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 649 else
135080d8 650 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
651}
652
4fbb5a00
DY
653function updateObjectAllocation ()
654{
655 assertUIntArg ('object_id', __FUNCTION__);
656
657 $is_submit = isset ($_REQUEST['got_atoms']);
658 $is_update = isset ($_REQUEST['rackmulti'][0]);
659
4fbb5a00
DY
660 if ($is_submit)
661 {
662 $object_id = $_REQUEST['object_id'];
663 $workingRacksData = getResidentRacksData ($object_id);
664 if ($workingRacksData === NULL)
665 {
666 print_r ($workingRacksData);
667 showError ('getResidentRacksData() failed', __FUNCTION__);
668 return;
669 }
2135fd83 670 $workingRacksData = array();
4fbb5a00
DY
671 foreach ($_REQUEST['rackmulti'] as $cand_id)
672 {
673 if (!isset ($workingRacksData[$cand_id]))
674 {
675 $rackData = getRackData ($cand_id);
676 if ($rackData == NULL)
677 {
678 showError ('getRackData() failed', __FUNCTION__);
679 return;
680 }
681 $workingRacksData[$cand_id] = $rackData;
682 }
683 }
2135fd83
DY
684
685 foreach ($workingRacksData as &$rd)
686 applyObjectMountMask ($rd, $object_id);
4fbb5a00
DY
687
688 $oldMolecule = getMoleculeForObject ($object_id);
689 $worldchanged = FALSE;
690 $log = array();
691 foreach ($workingRacksData as $rack_id => $rackData)
692 {
693 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
694 $log[] = $logrecord;
695 }
696 return buildWideRedirectURL($log);
697
698 }
699 else
700 {
2135fd83
DY
701 unset($_GET['page']);
702 unset($_GET['tab']);
703 unset($_GET['op']);
704 unset($_POST['page']);
705 unset($_POST['tab']);
706 unset($_POST['op']);
707 return buildWideRedirectURL(array(), NULL, NULL, array_merge($_GET, $_POST));
4fbb5a00
DY
708 }
709
710}
711
712
22bb04da
DO
713function updateObject ()
714{
cdd3efe9 715 assertUIntArg ('num_attrs', __FUNCTION__);
22bb04da
DO
716 assertUIntArg ('object_id', __FUNCTION__);
717 assertUIntArg ('object_type_id', __FUNCTION__);
718 assertStringArg ('object_name', __FUNCTION__, TRUE);
719 assertStringArg ('object_label', __FUNCTION__, TRUE);
720 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
721 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
722 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
723 $has_problems = 'yes';
724 else
725 $has_problems = 'no';
726
727 if (commitUpdateObject (
728 $_REQUEST['object_id'],
729 $_REQUEST['object_name'],
730 $_REQUEST['object_label'],
731 $_REQUEST['object_barcode'],
732 $_REQUEST['object_type_id'],
733 $has_problems,
734 $_REQUEST['object_asset_no'],
735 $_REQUEST['object_comment']
736 ) !== TRUE)
135080d8 737 return buildRedirectURL (__FUNCTION__, 'ERR');
22bb04da 738
cdd3efe9 739 // Update optional attributes
f59e8cf5 740 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a 741 $result = array();
103b1e1e 742 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
0dfb8a2a 743 {
2c6c7645 744 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
745 $attr_id = $_REQUEST["${i}_attr_id"];
746
747 // Field is empty, delete attribute and move on.
748 if (empty($_REQUEST["${i}_value"]))
749 {
103b1e1e 750 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
751 continue;
752 }
753
754 // The value could be uint/float, but we don't know ATM. Let SQL
755 // server check this and complain.
2c6c7645 756 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
757 $value = $_REQUEST["${i}_value"];
758 switch ($oldvalues[$attr_id]['type'])
759 {
760 case 'uint':
761 case 'float':
762 case 'string':
763 $oldvalue = $oldvalues[$attr_id]['value'];
764 break;
765 case 'dict':
766 $oldvalue = $oldvalues[$attr_id]['key'];
767 break;
768 default:
61e269b5 769 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
770 die;
771 }
772 if ($value == $oldvalue)
773 continue;
774
775 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 776 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a 777 }
103b1e1e 778 if (in_array (FALSE, $result))
135080d8 779 return buildRedirectURL (__FUNCTION__, 'ERR');
0dfb8a2a 780
cdd3efe9
AD
781 // Invalidate thumb cache of all racks objects could occupy.
782 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
783 resetThumbCache ($rack_id);
784
135080d8 785 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
786}
787
42d73cbf
DY
788
789function addMultipleObjects()
790{
7abce0b7 791 $log = emptyLog();
42d73cbf 792 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
793 $max = getConfigVar ('MASSCOUNT');
794 for ($i = 0; $i < $max; $i++)
795 {
796 if (!isset ($_REQUEST["${i}_object_type_id"]))
797 {
57777e4a 798 $log = mergeLogs ($log, oneLiner (184, array ($i + 1)));
42d73cbf
DY
799 break;
800 }
801 assertUIntArg ("${i}_object_type_id", __FUNCTION__, TRUE);
802 assertStringArg ("${i}_object_name", __FUNCTION__, TRUE);
803 assertStringArg ("${i}_object_label", __FUNCTION__, TRUE);
804 assertStringArg ("${i}_object_asset_no", __FUNCTION__, TRUE);
805 assertStringArg ("${i}_object_barcode", __FUNCTION__, TRUE);
7abce0b7 806 $name = $_REQUEST["${i}_object_name"];
42d73cbf
DY
807
808 // It's better to skip silently, than to print a notice.
7abce0b7 809 if ($_REQUEST["${i}_object_type_id"] == 0)
42d73cbf 810 continue;
7abce0b7
DO
811 if (commitAddObject
812 (
813 $name,
814 $_REQUEST["${i}_object_label"],
815 $_REQUEST["${i}_object_barcode"],
816 $_REQUEST["${i}_object_type_id"],
817 $_REQUEST["${i}_object_asset_no"],
818 $taglist
819 ) === TRUE)
57777e4a 820 $log = mergeLogs ($log, oneLiner (80, array ($name)));
42d73cbf 821 else
57777e4a 822 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 823 }
7abce0b7 824 return buildWideRedirectURL ($log);
42d73cbf
DY
825}
826
827function addLotOfObjects()
828{
7abce0b7 829 $log = emptyLog();
42d73cbf 830 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
831 assertUIntArg ('global_type_id', __FUNCTION__, TRUE);
832 assertStringArg ('namelist', __FUNCTION__, TRUE);
833 $global_type_id = $_REQUEST['global_type_id'];
7abce0b7 834 if ($global_type_id == 0 or empty ($_REQUEST['namelist']))
57777e4a 835 $log = mergeLogs ($log, oneLiner (186));
42d73cbf
DY
836 else
837 {
838 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
839 $names1 = explode ('\n', $_REQUEST['namelist']);
840 $names2 = array();
841 foreach ($names1 as $line)
842 {
843 $parts = explode ('\r', $line);
844 reset ($parts);
845 if (empty ($parts[0]))
846 continue;
847 else
848 $names2[] = rtrim ($parts[0]);
849 }
57777e4a
DO
850 foreach ($names2 as $name)
851 if (commitAddObject ($name, '', '', $global_type_id, '', $taglist) === TRUE)
852 $log = mergeLogs ($log, oneLiner (80, array ($name)));
42d73cbf 853 else
57777e4a 854 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 855 }
7abce0b7 856 return buildWideRedirectURL ($log);
42d73cbf
DY
857}
858
52b34485
AD
859function deleteObject ()
860{
861 assertUIntArg ('object_id', __FUNCTION__);
862 $error = commitDeleteObject ($_REQUEST['object_id']);
863
864 if ($error != '')
135080d8 865 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
52b34485 866
135080d8 867 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
52b34485
AD
868}
869
e673ee24
DO
870function useupPort ()
871{
2c6c7645 872 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 873 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
135080d8 874 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 875 else
135080d8 876 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
877}
878
4fe32e78
DY
879function updateUI ()
880{
2c6c7645 881 assertUIntArg ('num_vars', __FUNCTION__);
c461c579 882 $error = '';
4fe32e78 883
103b1e1e 884 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 885 {
2c6c7645
DO
886 assertStringArg ("${i}_varname", __FUNCTION__);
887 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 888 $varname = $_REQUEST["${i}_varname"];
4fe32e78 889 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 890
c461c579
DO
891 // If form value = value in DB, don't bother updating DB
892 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
893 continue;
894
895 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
896 $error = setConfigVar ($varname, $varvalue, TRUE);
897 if (!empty ($error))
898 break;
4fe32e78
DY
899 }
900
c461c579 901 if ($error != '')
135080d8 902 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
4fe32e78 903
135080d8 904 return buildRedirectURL (__FUNCTION__, 'OK');
4fe32e78
DY
905}
906
b07f617c
DO
907function resetUIConfig()
908{
1f86e996 909 setConfigVar ('default_port_type','24');
b07f617c
DO
910 setConfigVar ('MASSCOUNT','15');
911 setConfigVar ('MAXSELSIZE','30');
b07f617c
DO
912 setConfigVar ('ROW_SCALE','2');
913 setConfigVar ('PORTS_PER_ROW','12');
914 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
915 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
b5ce46aa
DO
916 setConfigVar ('DEFAULT_SLB_VS_PORT','');
917 setConfigVar ('DEFAULT_SLB_RS_PORT','');
72281138 918 setConfigVar ('DETECT_URLS','no');
4b8d413e 919 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 920 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 921 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
922 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
923 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
924 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 925 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 926 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df 927 setConfigVar ('SHOW_LAST_TAB', 'no');
f0ed1181 928 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 929 setConfigVar ('TREE_THRESHOLD', '25');
2be3fd23 930 setConfigVar ('IPV4_JAYWALK', 'no');
9318d2ef 931 setConfigVar ('ADDNEW_AT_TOP', 'yes');
64347dcf 932 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
37e59768
DO
933 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
934 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
935 setConfigVar ('PREVIEW_TEXT_COLS', '80');
936 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
f3d274bf 937 setConfigVar ('VENDOR_SIEVE', '');
35bf153d
DO
938 setConfigVar ('IPV4LB_LISTSRC', '{$typeid_4}');
939 setConfigVar ('IPV4OBJ_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_12} or {$typeid_445} or {$typeid_447}');
940 setConfigVar ('IPV4NAT_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
941 setConfigVar ('ASSETWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
942 setConfigVar ('NAMEWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
135080d8 943 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
944}
945
732e4578 946// Add single record.
ca461127
DO
947function addRealServer ()
948{
2c6c7645
DO
949 assertUIntArg ('pool_id', __FUNCTION__);
950 assertIPv4Arg ('remoteip', __FUNCTION__);
79a9edb4 951 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 952 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
953 if (!addRStoRSPool (
954 $_REQUEST['pool_id'],
955 $_REQUEST['remoteip'],
956 $_REQUEST['rsport'],
957 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
958 $_REQUEST['rsconfig']
959 ))
135080d8 960 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 961 else
135080d8 962 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
963}
964
732e4578
DO
965// Parse textarea submitted and try adding a real server for each line.
966function addRealServers ()
967{
cbadbb38 968 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
969 assertStringArg ('format', __FUNCTION__);
970 assertStringArg ('rawtext', __FUNCTION__);
732e4578
DO
971 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
972 $ngood = $nbad = 0;
973 $rsconfig = '';
974 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
975 foreach (explode ('\n', $rawtext) as $line)
976 {
977 if (empty ($line))
978 continue;
979 $match = array ();
980 switch ($_REQUEST['format'])
981 {
982 case 'ipvs_2': // address and port only
983 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
984 continue;
103b1e1e 985 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
986 $ngood++;
987 else
988 $nbad++;
989 break;
990 case 'ipvs_3': // address, port and weight
991 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
992 continue;
103b1e1e 993 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
994 $ngood++;
995 else
996 $nbad++;
997 break;
e69c2aa3
DO
998 case 'ssv_2': // IP address and port
999 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1000 continue;
103b1e1e 1001 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1002 $ngood++;
1003 else
1004 $nbad++;
1005 break;
948c37e3
DO
1006 case 'ssv_1': // IP address
1007 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1008 continue;
1009 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1010 $ngood++;
1011 else
1012 $nbad++;
1013 break;
732e4578 1014 default:
135080d8 1015 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1016 break;
1017 }
1018 }
1019 if ($nbad == 0 and $ngood > 0)
135080d8 1020 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1021 else
135080d8 1022 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1023}
1024
d6517a21
DO
1025function addVService ()
1026{
2c6c7645
DO
1027 assertIPv4Arg ('vip', __FUNCTION__);
1028 assertUIntArg ('vport', __FUNCTION__);
1029 assertStringArg ('proto', __FUNCTION__);
103b1e1e 1030 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1031 return buildRedirectURL (__FUNCTION__, 'ERR1');
2c6c7645
DO
1032 assertStringArg ('name', __FUNCTION__, TRUE);
1033 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1034 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1035 $error = commitCreateVS
1036 (
103b1e1e
DO
1037 $_REQUEST['vip'],
1038 $_REQUEST['vport'],
7320b661 1039 $_REQUEST['proto'],
103b1e1e
DO
1040 $_REQUEST['name'],
1041 $_REQUEST['vsconfig'],
c63a8d6e
DO
1042 $_REQUEST['rsconfig'],
1043 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1044 );
1045 if ($error != '')
135080d8 1046 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1047 else
135080d8 1048 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1049}
1050
1051function deleteRealServer ()
1052{
2c6c7645 1053 assertUIntArg ('id', __FUNCTION__);
d6517a21 1054 if (!commitDeleteRS ($_REQUEST['id']))
135080d8 1055 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1056 else
135080d8 1057 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1058}
1059
3241551e
DO
1060function deleteLoadBalancer ()
1061{
2c6c7645
DO
1062 assertUIntArg ('object_id', __FUNCTION__);
1063 assertUIntArg ('pool_id', __FUNCTION__);
1064 assertUIntArg ('vs_id', __FUNCTION__);
103b1e1e
DO
1065 if (!commitDeleteLB (
1066 $_REQUEST['object_id'],
1067 $_REQUEST['pool_id'],
1068 $_REQUEST['vs_id']
1069 ))
135080d8 1070 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1071 else
135080d8 1072 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1073}
1074
1075function deleteVService ()
1076{
e02e1941
DO
1077 assertUIntArg ('vs_id', __FUNCTION__);
1078 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1079 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1080 else
135080d8 1081 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1082}
1083
fb1c4a54
DO
1084function updateRealServer ()
1085{
2c6c7645
DO
1086 assertUIntArg ('rs_id', __FUNCTION__);
1087 assertIPv4Arg ('rsip', __FUNCTION__);
79a9edb4 1088 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 1089 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1090 if (!commitUpdateRS (
1091 $_REQUEST['rs_id'],
1092 $_REQUEST['rsip'],
1093 $_REQUEST['rsport'],
1094 $_REQUEST['rsconfig']
1095 ))
135080d8 1096 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1097 else
135080d8 1098 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1099}
1100
c1ca768c 1101function updateLoadBalancer ()
3241551e 1102{
2c6c7645
DO
1103 assertUIntArg ('object_id', __FUNCTION__);
1104 assertUIntArg ('pool_id', __FUNCTION__);
1105 assertUIntArg ('vs_id', __FUNCTION__);
1106 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1107 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1108 if (!commitUpdateLB (
1109 $_REQUEST['object_id'],
1110 $_REQUEST['pool_id'],
1111 $_REQUEST['vs_id'],
1112 $_REQUEST['vsconfig'],
1113 $_REQUEST['rsconfig']
1114 ))
135080d8 1115 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1116 else
135080d8 1117 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1118}
1119
1120function updateVService ()
1121{
e02e1941 1122 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
1123 assertIPv4Arg ('vip', __FUNCTION__);
1124 assertUIntArg ('vport', __FUNCTION__);
1125 assertStringArg ('proto', __FUNCTION__);
1126 assertStringArg ('name', __FUNCTION__, TRUE);
1127 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1128 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1129 if (!commitUpdateVS (
1130 $_REQUEST['vs_id'],
1131 $_REQUEST['vip'],
1132 $_REQUEST['vport'],
1133 $_REQUEST['proto'],
1134 $_REQUEST['name'],
1135 $_REQUEST['vsconfig'],
1136 $_REQUEST['rsconfig']
1137 ))
135080d8 1138 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1139 else
135080d8 1140 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1141}
1142
1143function addLoadBalancer ()
1144{
2c6c7645
DO
1145 assertUIntArg ('pool_id', __FUNCTION__);
1146 assertUIntArg ('object_id', __FUNCTION__);
1147 assertUIntArg ('vs_id', __FUNCTION__);
1148 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1149 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1150 if (!addLBtoRSPool (
1151 $_REQUEST['pool_id'],
1152 $_REQUEST['object_id'],
1153 $_REQUEST['vs_id'],
1154 $_REQUEST['vsconfig'],
1155 $_REQUEST['rsconfig']
1156 ))
135080d8 1157 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1158 else
135080d8 1159 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1160}
1161
5ad76f01
DO
1162function addRSPool ()
1163{
2c6c7645
DO
1164 assertStringArg ('name', __FUNCTION__, TRUE);
1165 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1166 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1167 $error = commitCreateRSPool
1168 (
103b1e1e
DO
1169 $_REQUEST['name'],
1170 $_REQUEST['vsconfig'],
c63a8d6e
DO
1171 $_REQUEST['rsconfig'],
1172 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1173 );
1174 if ($error != '')
135080d8 1175 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1176 else
135080d8 1177 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1178}
1179
1180function deleteRSPool ()
1181{
2c6c7645 1182 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1183 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
135080d8 1184 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1185 else
135080d8 1186 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1187}
1188
1189function updateRSPool ()
1190{
2c6c7645
DO
1191 assertUIntArg ('pool_id', __FUNCTION__);
1192 assertStringArg ('name', __FUNCTION__, TRUE);
1193 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1194 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1195 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
135080d8 1196 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1197 else
135080d8 1198 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1199}
1200
1f7d18fa
DO
1201function updateRSInService ()
1202{
2c6c7645 1203 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1204 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1205 $orig = getRSPoolInfo ($pool_id);
1206 $nbad = $ngood = 0;
1207 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1208 {
1209 $rs_id = $_REQUEST["rsid_${i}"];
1210 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1211 $newval = 'yes';
1212 else
1213 $newval = 'no';
1214 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1215 {
1216 if (commitSetInService ($rs_id, $newval))
1217 $ngood++;
1218 else
1219 $nbad++;
1220 }
1221 }
1222 if (!$nbad)
135080d8 1223 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1224 else
135080d8 1225 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1226}
1227
8d9c16e7
DO
1228// FIXME: check, that each submitted address belongs to the prefix we
1229// are operating on.
03eb5209
DO
1230function importPTRData ()
1231{
2c6c7645 1232 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209 1233 $nbad = $ngood = 0;
03eb5209
DO
1234 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1235 {
3f3bd41e 1236 $inputname = "import_${i}";
03eb5209
DO
1237 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1238 continue;
2c6c7645
DO
1239 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1240 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1241 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1242 // Non-existent addresses will not have this argument set in request.
1243 $rsvd = 'no';
03eb5209
DO
1244 if ($_REQUEST["rsvd_${i}"] == 'yes')
1245 $rsvd = 'yes';
3f3bd41e 1246 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1247 $ngood++;
1248 else
1249 $nbad++;
1250 }
1251 if (!$nbad)
135080d8 1252 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1253 else
135080d8 1254 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1255}
1256
f3f0161f
DO
1257function generateAutoPorts ()
1258{
103b1e1e 1259 global $pageno;
2c6c7645 1260 assertUIntArg ('object_id', __FUNCTION__);
7ddbcf59 1261 $info = getObjectInfo ($_REQUEST['object_id'], FALSE);
f3f0161f 1262 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1263 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1264 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1265 else
135080d8 1266 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1267}
1268
24cbe8af 1269// Filter out implicit tags before storing the new tag set.
3355ca56 1270function saveEntityTags ()
24cbe8af 1271{
be2ae2a2
DO
1272 global $explicit_tags, $implicit_tags, $page, $pageno, $etype_by_pageno;
1273 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
3355ca56
DO
1274 {
1275 showError ('Internal error', __FUNCTION__);
1276 die;
1277 }
be2ae2a2 1278 $realm = $etype_by_pageno[$pageno];
3355ca56 1279 $bypass = $page[$pageno]['bypass'];
2c6c7645 1280 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1281 $entity_id = $_REQUEST[$bypass];
e04931c8 1282 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1283 // Build a chain from the submitted data, minimize it,
ab379543 1284 // then wipe existing records and store the new set instead.
01b6b4d6 1285 destroyTagsForEntity ($realm, $entity_id);
6e49bd1f 1286 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1287 $n_succeeds = $n_errors = 0;
6e49bd1f 1288 foreach ($newchain as $taginfo)
eb6ea26f 1289 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1290 $n_succeeds++;
1291 else
1292 $n_errors++;
ab379543 1293 if ($n_errors)
135080d8 1294 return buildRedirectURL (__FUNCTION__, 'ERR', array ($n_succeeds, $n_errors));
ab379543 1295 else
135080d8 1296 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1297}
1298
fe7044ad
DO
1299function destroyTag ()
1300{
49fb1027 1301 assertUIntArg ('tag_id', __FUNCTION__);
c615a655
DO
1302 global $taglist;
1303 if (!isset ($taglist[$_REQUEST['tag_id']]))
1304 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['tag_id']));
49fb1027 1305 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
c615a655 1306 return buildRedirectURL (__FUNCTION__, 'OK', array ($taglist[$_REQUEST['tag_id']]['tag']));
fe7044ad 1307 else
c615a655 1308 return buildRedirectURL (__FUNCTION__, 'ERR2');
fe7044ad
DO
1309}
1310
1311function createTag ()
1312{
49fb1027 1313 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1314 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1315 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1316 if (!validTagName ($tagname))
135080d8 1317 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
0f3c6639 1318 if (tagExistsInDatabase ($tagname))
135080d8 1319 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname));
fe7044ad
DO
1320 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1321 $parent_id = 'NULL';
1322 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
135080d8 1323 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
fe7044ad 1324 else
135080d8 1325 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
fe7044ad
DO
1326}
1327
49fb1027
DO
1328function updateTag ()
1329{
49fb1027
DO
1330 assertUIntArg ('tag_id', __FUNCTION__);
1331 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1332 assertStringArg ('tag_name', __FUNCTION__);
1333 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1334 if (!validTagName ($tagname))
135080d8 1335 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1336 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1337 $parent_id = 'NULL';
1338 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
135080d8 1339 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
49fb1027 1340 else
135080d8 1341 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname, $ret));
49fb1027
DO
1342}
1343
eb6ea26f
DO
1344function rollTags ()
1345{
eb6ea26f
DO
1346 assertUIntArg ('row_id', __FUNCTION__);
1347 assertStringArg ('sum', __FUNCTION__, TRUE);
1348 assertUIntArg ('realsum', __FUNCTION__);
eb6ea26f 1349 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1350 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1351 // Even if the user requested an empty tag list, don't bail out, but process existing
1352 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1353 // have its chains refined to "normal" form.
1354 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1355 $n_ok = 0;
1356 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1357 // filter out the same tag again and again. It will have own noise to cancel.
1358 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
1359 foreach (getRacksForRow ($_REQUEST['row_id']) as $rackInfo)
1360 {
1361 if (rebuildTagChainForEntity ('rack', $rackInfo['id'], $extrachain))
1362 $n_ok++;
1363 foreach (stuffInRackspace (getRackData ($rackInfo['id'])) as $object_id)
1364 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1365 $n_ok++;
1366 }
135080d8 1367 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1368}
1369
9457ca59 1370function changeMyPassword ()
cced6b7d 1371{
204284ba
DO
1372 global $accounts, $remote_username, $user_auth_src;
1373 if ($user_auth_src != 'database')
135080d8 1374 return buildRedirectURL (__FUNCTION__, 'ERR1');
0ac7e4b3
DO
1375 assertStringArg ('oldpassword', __FUNCTION__);
1376 assertStringArg ('newpassword1', __FUNCTION__);
1377 assertStringArg ('newpassword2', __FUNCTION__);
93bdb7ba 1378 if ($accounts[$remote_username]['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1379 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1380 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1381 return buildRedirectURL (__FUNCTION__, 'ERR3');
93bdb7ba 1382 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1383 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1384 else
135080d8 1385 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1386}
1387
1388function saveRackCode ()
1389{
cced6b7d 1390 assertStringArg ('rackcode');
e6a4adb9
DO
1391 // For the test to succeed, unescape LFs, strip CRs.
1392 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1393 $parseTree = getRackCode ($newcode);
1394 if ($parseTree['result'] != 'ACK')
135080d8 1395 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
4a6a28f1 1396 saveScript ('RackCodeCache', '');
cf25e649 1397 if (saveScript ('RackCode', $newcode))
135080d8 1398 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1399 else
135080d8 1400 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1401}
1402
46f92ff7
DO
1403// This handler's context is pre-built, but not authorized. It is assumed, that the
1404// handler will take existing context and before each commit check authorization
1405// on the base chain plus necessary context added.
1406function setPortVLAN ()
1407{
1408 assertUIntArg ('portcount', __FUNCTION__);
1409 $data = getSwitchVLANs ($_REQUEST['object_id']);
1410 if ($data === NULL)
135080d8 1411 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1412 list ($vlanlist, $portlist) = $data;
1413 // Here we just build up 1 set command for the gateway with all of the ports
1414 // included. The gateway is expected to filter unnecessary changes silently
1415 // and to provide a list of responses with either error or success message
1416 // for each of the rest.
1417 $nports = $_REQUEST['portcount'];
1418 $prefix = 'set ';
db55cf54 1419 $log = emptyLog();
46f92ff7
DO
1420 $setcmd = '';
1421 for ($i = 0; $i < $nports; $i++)
1422 if
1423 (
1424 !isset ($_REQUEST['portname_' . $i]) ||
1425 !isset ($_REQUEST['vlanid_' . $i]) ||
1426 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1427 )
24dcb9d8 1428 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1429 elseif
1430 (
1431 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1432 $portlist[$i]['vlaind'] == 'TRUNK'
1433 )
1434 continue;
1435 else
1436 {
1437 $portname = $_REQUEST['portname_' . $i];
1438 $oldvlanid = $portlist[$i]['vlanid'];
1439 $newvlanid = $_REQUEST['vlanid_' . $i];
1440 // Finish the security context and evaluate it.
1441 $annex = array();
1442 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1443 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1444 if (!permitted (NULL, NULL, NULL, $annex))
1445 {
24dcb9d8 1446 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1447 continue;
1448 }
1449 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1450 $prefix = ';';
1451 }
1452 // Feed the gateway and interpret its (non)response.
1453 if ($setcmd != '')
f0ff4930 1454 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1455 else
24dcb9d8 1456 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1457 return buildWideRedirectURL ($log);
1458}
1459
2987fc1f
DO
1460function submitSLBConfig ()
1461{
1462 assertUIntArg ('object_id', __FUNCTION__);
1463 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
ff0df5c3 1464 $msglog = gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1465 return buildWideRedirectURL ($msglog);
1466}
1467
345fd640
AD
1468function addRow ()
1469{
1470 assertStringArg ('name', __FUNCTION__);
1471
10bac82a 1472 if (commitAddRow ($_REQUEST['name']) === TRUE)
135080d8 1473 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1474 else
135080d8 1475 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1476}
1477
1478function updateRow ()
1479{
1480 assertUIntArg ('row_id', __FUNCTION__);
1481 assertStringArg ('name', __FUNCTION__);
1482
10bac82a 1483 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1484 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1485 else
135080d8 1486 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1487}
1488
9f14a7ef
DY
1489function deleteRow ()
1490{
1491 assertUIntArg ('row_id', __FUNCTION__);
7be51fea 1492 $rowinfo = getRackRowInfo ($_REQUEST['row_id']);
9f14a7ef
DY
1493
1494 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
7be51fea 1495 return buildRedirectURL (__FUNCTION__, 'OK', array ($rowinfo['name']));
9f14a7ef 1496 else
7be51fea 1497 return buildRedirectURL (__FUNCTION__, 'ERR', array ($rowinfo['name']));
9f14a7ef
DY
1498}
1499
f19c75d6
DO
1500function addRack ()
1501{
1502 assertUIntArg ('row_id', __FUNCTION__);
1503 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1504 if (isset ($_REQUEST['got_data']))
1505 {
1506 assertStringArg ('rack_name', __FUNCTION__);
1507 assertUIntArg ('rack_height1', __FUNCTION__);
1508 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1509
1510 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1511 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1512 else
135080d8 1513 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1514 }
1515 elseif (isset ($_REQUEST['got_mdata']))
1516 {
1517 assertUIntArg ('rack_height2', __FUNCTION__);
1518 assertStringArg ('rack_names', __FUNCTION__, TRUE);
db55cf54 1519 $log = emptyLog();
f19c75d6
DO
1520 // copy-and-paste from renderAddMultipleObjectsForm()
1521 $names1 = explode ('\n', $_REQUEST['rack_names']);
1522 $names2 = array();
1523 foreach ($names1 as $line)
1524 {
1525 $parts = explode ('\r', $line);
1526 reset ($parts);
1527 if (empty ($parts[0]))
1528 continue;
1529 else
1530 $names2[] = rtrim ($parts[0]);
1531 }
b3029d5c 1532 global $msgcode;
f19c75d6
DO
1533 foreach ($names2 as $cname)
1534 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1535 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1536 else
b3029d5c 1537 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1538 return buildWideRedirectURL ($log);
1539 }
1540 else
135080d8 1541 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1542}
1543
c8187437
DY
1544function deleteRack ()
1545{
1546 assertUIntArg ('rack_id', __FUNCTION__);
1547 $rack_id = $_REQUEST['rack_id'];
1548 $rackData = getRackData ($rack_id);
1549 if ($rackData == NULL)
1550 {
1551 showError ('getRackData() failed', __FUNCTION__);
1552 return;
1553 }
1554 if (count ($rackData['mountedObjects']) == 0)
1555 {
1556 resetThumbCache ($rack_id);
1557 if (TRUE === commitDeleteRack ($rack_id))
1558 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
1559 else
1560 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1561 }
1562 else
1563 {
1564 return buildRedirectURL (__FUNCTION__, 'ERR1');
1565 }
1566}
1567
7056988c
DO
1568function updateRack ()
1569{
7056988c
DO
1570 assertUIntArg ('rack_id', __FUNCTION__);
1571 assertUIntArg ('rack_row_id', __FUNCTION__);
1572 assertUIntArg ('rack_height', __FUNCTION__);
1573 assertStringArg ('rack_name', __FUNCTION__);
1574 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
7056988c 1575
7056988c 1576 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1577 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1578 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1579 else
135080d8 1580 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1581}
1582
4fbb5a00
DY
1583function updateRackDesign ()
1584{
1585 assertUIntArg ('rack_id', __FUNCTION__);
1586 if (($rackData = getRackData ($_REQUEST['rack_id'])) == NULL)
1587 {
1588 showError ('getRackData() failed', __FUNCTION__);
1589 return;
1590 }
1591 applyRackDesignMask($rackData);
1592 markupObjectProblems ($rackData);
1593 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1594 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1595}
1596
1597function updateRackProblems ()
1598{
1599 assertUIntArg ('rack_id', __FUNCTION__);
1600 if (($rackData = getRackData ($_REQUEST['rack_id'])) == NULL)
1601 {
1602 showError ('getRackData() failed', __FUNCTION__);
1603 return;
1604 }
1605 applyRackProblemMask($rackData);
1606 markupObjectProblems ($rackData);
1607 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1608 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1609}
1610
1611
1612
7056988c
DO
1613function querySNMPData ()
1614{
1615 assertUIntArg ('object_id', __FUNCTION__);
1616 assertStringArg ('community', __FUNCTION__);
83fdf85f 1617 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
7056988c
DO
1618}
1619
e1ae3fb4
AD
1620// File-related functions
1621function addFileWithoutLink ()
1622{
1623 assertStringArg ('comment', __FUNCTION__, TRUE);
1624
1625 // Make sure the file can be uploaded
1626 if (get_cfg_var('file_uploads') != 1)
135080d8 1627 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1628
1629 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1630 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1631
1632 if ($error != '')
135080d8 1633 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1634
135080d8 1635 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1636}
1637
1638function addFileToEntity ()
1639{
1640 assertStringArg ('entity_type', __FUNCTION__);
1641 assertUIntArg ('entity_id', __FUNCTION__);
1642 assertStringArg ('comment', __FUNCTION__, TRUE);
1643 if (empty ($_REQUEST['entity_type']) || empty ($_REQUEST['entity_id']))
135080d8 1644 return buildRedirectURL (__FUNCTION__, 'ERR');
e1ae3fb4
AD
1645
1646 // Make sure the file can be uploaded
1647 if (get_cfg_var('file_uploads') != 1)
135080d8 1648 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1649
1650 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1651 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1652 if ($error != '')
135080d8 1653 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4
AD
1654
1655 $file_id = lastInsertID();
1656 $error = commitLinkFile ($file_id, $_REQUEST['entity_type'], $_REQUEST['entity_id']);
1657 if ($error != '')
135080d8 1658 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1659
135080d8 1660 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1661}
1662
1663function linkFileToEntity ()
1664{
e1ae3fb4 1665 assertUIntArg ('file_id', __FUNCTION__);
482c7f35 1666 global $page, $pageno, $etype_by_pageno;
121496b6
DO
1667 $entity_type = $etype_by_pageno[$pageno];
1668 $bypass_name = $page[$pageno]['bypass'];
1669 assertUIntArg ($bypass_name, __FUNCTION__);
1670
1671 $fi = getFileInfo ($_REQUEST['file_id']);
1672 if ($fi === NULL)
135080d8 1673 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
121496b6 1674 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
e1ae3fb4 1675 if ($error != '')
135080d8 1676 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
e1ae3fb4 1677
135080d8 1678 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
e1ae3fb4
AD
1679}
1680
fd1fb420
AD
1681function replaceFile ()
1682{
db55cf54 1683 global $sic;
fd1fb420
AD
1684 assertUIntArg ('file_id', __FUNCTION__);
1685
1686 // Make sure the file can be uploaded
1687 if (get_cfg_var('file_uploads') != 1)
db55cf54
DO
1688 return buildRedirectURL (__FUNCTION__, 'ERR1');
1689 $shortInfo = getFileInfo ($sic['file_id']);
fd1fb420
AD
1690
1691 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1692 if ($fp === FALSE)
1693 return buildRedirectURL (__FUNCTION__, 'ERR2');
f8874cdb 1694 $error = commitReplaceFile ($sic['file_id'], $fp);
fd1fb420 1695 if ($error != '')
db55cf54 1696 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
fd1fb420 1697
db55cf54 1698 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1699}
1700
e1ae3fb4
AD
1701function updateFile ()
1702{
1703 assertUIntArg ('file_id', __FUNCTION__);
9db67d95
DO
1704 assertStringArg ('file_name', __FUNCTION__);
1705 assertStringArg ('file_type', __FUNCTION__);
1706 assertStringArg ('file_comment', __FUNCTION__, TRUE);
f3d274bf
DO
1707 // prepared statement params below
1708 global $sic;
1709 $error = commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']);
e1ae3fb4 1710 if ($error != '')
135080d8 1711 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1712
135080d8 1713 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1714}
1715
1716function unlinkFile ()
1717{
1718 assertUIntArg ('link_id', __FUNCTION__);
1719 $error = commitUnlinkFile ($_REQUEST['link_id']);
1720
1721 if ($error != '')
135080d8 1722 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1723
8bc5d1e4 1724 return buildRedirectURL (__FUNCTION__, 'OK');
e1ae3fb4
AD
1725}
1726
1727function deleteFile ()
1728{
1729 assertUIntArg ('file_id', __FUNCTION__);
f8874cdb 1730 $shortInfo = getFileInfo ($_REQUEST['file_id']);
e1ae3fb4
AD
1731 $error = commitDeleteFile ($_REQUEST['file_id']);
1732
1733 if ($error != '')
135080d8 1734 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1735
f8874cdb 1736 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1737}
1738
8628ae44
DO
1739function updateFileText ()
1740{
1741 assertUIntArg ('file_id', __FUNCTION__);
70cb9b56 1742 assertStringArg ('mtime_copy', __FUNCTION__);
8628ae44
DO
1743 assertStringArg ('file_text', __FUNCTION__, TRUE); // it's Ok to save empty
1744 $shortInfo = getFileInfo ($_REQUEST['file_id']);
70cb9b56
DO
1745 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1746 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb
DO
1747 global $sic;
1748 $error = commitReplaceFile ($sic['file_id'], $sic['file_text']);
8628ae44 1749 if ($error == '')
f8874cdb 1750 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
70cb9b56 1751 return buildRedirectURL (__FUNCTION__, 'ERR2');
8628ae44
DO
1752}
1753
e673ee24 1754?>