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