r2220 - scanIPv4Spans() isn't necessary any more, scanIPv4Space() is now used directly
[racktables] / inc / ophandlers.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of operation handlers for RackTables.
5*
6*/
7
17c32f4c
DO
8// This function assures that specified argument was passed
9// and is a number greater than zero.
2c6c7645 10function assertUIntArg ($argname, $caller = 'N/A', $allow_zero = FALSE)
17c32f4c
DO
11{
12 if (!isset ($_REQUEST[$argname]))
13 {
2c6c7645 14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
2c6c7645 19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
2c6c7645 24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
2c6c7645 29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
30 die();
31 }
32}
33
34// This function assures that specified argument was passed
35// and is a non-empty string.
2c6c7645 36function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
37{
38 if (!isset ($_REQUEST[$argname]))
39 {
2c6c7645 40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
2c6c7645 45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
2c6c7645 50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
51 die();
52 }
53}
54
2c6c7645 55function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
56{
57 if (!isset ($_REQUEST[$argname]))
58 {
2c6c7645 59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
2c6c7645 64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
2c6c7645 69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
70 die();
71 }
72}
73
2c6c7645 74function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c 75{
2c6c7645 76 assertStringArg ($argname, $caller, $ok_if_empty);
17c32f4c
DO
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
2c6c7645 79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "' (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
80 die();
81 }
82}
83
e673ee24
DO
84function addPortForwarding ()
85{
2c6c7645
DO
86 assertUIntArg ('object_id', __FUNCTION__);
87 assertIPv4Arg ('localip', __FUNCTION__);
88 assertIPv4Arg ('remoteip', __FUNCTION__);
89 assertUIntArg ('localport', __FUNCTION__);
9bea3a8b 90 assertStringArg ('proto', __FUNCTION__);
2c6c7645 91 assertStringArg ('description', __FUNCTION__, TRUE);
103b1e1e 92 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
31fc4157 93 if (empty ($remoteport))
f75eb878 94 $remoteport = $_REQUEST['localport'];
e673ee24 95
9bea3a8b
DO
96 $error = newPortForwarding
97 (
103b1e1e 98 $_REQUEST['object_id'],
9bea3a8b
DO
99 $_REQUEST['localip'],
100 $_REQUEST['localport'],
101 $_REQUEST['remoteip'],
102 $remoteport,
103 $_REQUEST['proto'],
104 $_REQUEST['description']
105 );
e673ee24
DO
106
107 if ($error == '')
ebb5441d 108 return buildRedirectURL ('OK');
e673ee24 109 else
ebb5441d 110 return buildRedirectURL ('ERR', array ($error));
e673ee24
DO
111}
112
113function delPortForwarding ()
114{
103b1e1e
DO
115 assertUIntArg ('object_id', __FUNCTION__);
116 assertIPv4Arg ('localip', __FUNCTION__);
117 assertIPv4Arg ('remoteip', __FUNCTION__);
118 assertUIntArg ('localport', __FUNCTION__);
119 assertUIntArg ('remoteport', __FUNCTION__);
120 assertStringArg ('proto', __FUNCTION__);
e673ee24 121
103b1e1e
DO
122 $error = deletePortForwarding
123 (
124 $_REQUEST['object_id'],
125 $_REQUEST['localip'],
126 $_REQUEST['localport'],
127 $_REQUEST['remoteip'],
128 $_REQUEST['remoteport'],
129 $_REQUEST['proto']
130 );
e673ee24 131 if ($error == '')
ebb5441d 132 return buildRedirectURL ('OK');
e673ee24 133 else
ebb5441d 134 return buildRedirectURL ('ERR', array ($error));
e673ee24
DO
135}
136
137function updPortForwarding ()
138{
103b1e1e
DO
139 assertUIntArg ('object_id', __FUNCTION__);
140 assertIPv4Arg ('localip', __FUNCTION__);
141 assertIPv4Arg ('remoteip', __FUNCTION__);
142 assertUIntArg ('localport', __FUNCTION__);
143 assertUIntArg ('remoteport', __FUNCTION__);
144 assertStringArg ('proto', __FUNCTION__);
145 assertStringArg ('description', __FUNCTION__);
e673ee24 146
103b1e1e
DO
147 $error = updatePortForwarding
148 (
149 $_REQUEST['object_id'],
150 $_REQUEST['localip'],
151 $_REQUEST['localport'],
152 $_REQUEST['remoteip'],
153 $_REQUEST['remoteport'],
154 $_REQUEST['proto'],
155 $_REQUEST['description']
156 );
e673ee24 157 if ($error == '')
ebb5441d 158 return buildRedirectURL ('OK');
e673ee24 159 else
ebb5441d 160 return buildRedirectURL ('ERR', array ($error));
e673ee24
DO
161}
162
e673ee24
DO
163function addPortForObject ()
164{
103b1e1e
DO
165 assertUIntArg ('object_id', __FUNCTION__);
166 assertStringArg ('port_name', __FUNCTION__, TRUE);
167 if (empty ($_REQUEST['port_name']))
ebb5441d 168 return buildRedirectURL ('ERR1');
103b1e1e
DO
169 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
170 if ($error != '')
ebb5441d 171 return buildRedirectURL ('ERR2', array ($error));
e673ee24 172 else
ebb5441d 173 return buildRedirectURL ('OK', array ($_REQUEST['port_name']));
e673ee24
DO
174}
175
176function editPortForObject ()
177{
2c6c7645 178 assertUIntArg ('port_id', __FUNCTION__);
d0a69ce8 179 // tolerate empty value now to produce custom informative message later
103b1e1e
DO
180 assertStringArg ('name', __FUNCTION__, TRUE);
181 if (empty ($_REQUEST['name']))
ebb5441d 182 return buildRedirectURL ('ERR1');
103b1e1e 183
e673ee24
DO
184 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
185 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
186 else
187 $port_rc = 'NULL';
103b1e1e
DO
188 $error = commitUpdatePort ($_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
189 if ($error != '')
ebb5441d 190 return buildRedirectURL ('ERR2', array ($error));
e673ee24 191 else
ebb5441d 192 return buildRedirectURL ('OK', array ($_REQUEST['name']));
e673ee24
DO
193}
194
195function delPortFromObject ()
196{
103b1e1e 197 assertUIntArg ('port_id', __FUNCTION__);
103b1e1e 198 $error = delObjectPort ($_REQUEST['port_id']);
e673ee24
DO
199
200 if ($error != '')
ebb5441d 201 return buildRedirectURL ('ERR', array ($error));
e673ee24 202 else
ebb5441d 203 return buildRedirectURL ('OK', array ($_REQUEST['port_name']));
e673ee24
DO
204}
205
206function linkPortForObject ()
207{
103b1e1e
DO
208 assertUIntArg ('port_id', __FUNCTION__);
209 assertUIntArg ('remote_port_id', __FUNCTION__);
baf37d01
DO
210 assertStringArg ('port_name', __FUNCTION__, TRUE);
211 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
212 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 213
103b1e1e 214 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
e673ee24 215 if ($error != '')
ebb5441d 216 return buildRedirectURL ('ERR', array ($error));
e673ee24 217 else
ebb5441d 218 return buildRedirectURL ('OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
219}
220
221function unlinkPortForObject ()
222{
103b1e1e 223 assertUIntArg ('port_id', __FUNCTION__);
baf37d01
DO
224 assertStringArg ('port_name', __FUNCTION__, TRUE);
225 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
226 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 227
103b1e1e 228 $error = unlinkPort ($_REQUEST['port_id']);
e673ee24 229 if ($error != '')
ebb5441d 230 return buildRedirectURL ('ERR', array ($error));
e673ee24 231 else
ebb5441d 232 return buildRedirectURL ('OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
233}
234
235function addMultiPorts ()
236{
2c6c7645
DO
237 assertStringArg ('format', __FUNCTION__);
238 assertStringArg ('input', __FUNCTION__);
239 assertUIntArg ('port_type', __FUNCTION__);
240 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
241 $format = $_REQUEST['format'];
242 $port_type = $_REQUEST['port_type'];
243 $object_id = $_REQUEST['object_id'];
244 // Input lines are escaped, so we have to explode and to chop by 2-char
245 // \n and \r respectively.
246 $lines1 = explode ('\n', $_REQUEST['input']);
247 foreach ($lines1 as $line)
248 {
249 $parts = explode ('\r', $line);
250 reset ($parts);
251 if (empty ($parts[0]))
252 continue;
253 else
254 $lines2[] = rtrim ($parts[0]);
255 }
256 $ports = array();
257 foreach ($lines2 as $line)
258 {
259 switch ($format)
260 {
261 case 'fisxii':
262 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
263 list ($slot, $port) = explode ('/', $words[0]);
264 $ports[] = array
265 (
266 'name' => "e ${slot}/${port}",
267 'l2address' => $words[8],
268 'label' => "slot ${slot} port ${port}"
269 );
270 break;
271 case 'c3600asy':
272 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
273/*
274How Async Lines are Numbered in Cisco 3600 Series Routers
275http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
276
277Understanding 16- and 32-Port Async Network Modules
278http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
279*/
280 $async = $words[0];
281 $slot = floor (($async - 1) / 32);
282 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
283 $cable = $async - $slot * 32 - $octalgroup * 8;
284 $og_label[0] = 'async 0-7';
285 $og_label[1] = 'async 8-15';
286 $og_label[2] = 'async 16-23';
287 $og_label[3] = 'async 24-31';
288 $ports[] = array
289 (
290 'name' => "async ${async}",
291 'l2address' => '',
292 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
293 );
294 break;
295 case 'fiwg':
296 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
297 $ifnumber = $words[0] * 1;
298 $ports[] = array
299 (
300 'name' => "e ${ifnumber}",
301 'l2address' => "${words[8]}",
302 'label' => "${ifnumber}"
303 );
304 break;
351d4dbf 305 case 'ssv1':
5b585342 306 $words = explode (' ', $line);
351d4dbf
DO
307 if (empty ($words[0]) or empty ($words[1]))
308 continue;
309 $ports[] = array
310 (
311 'name' => $words[0],
312 'l2address' => $words[1],
313 'label' => ''
314 );
315 break;
e673ee24 316 default:
ebb5441d 317 return buildRedirectURL ('ERR');
e673ee24
DO
318 break;
319 }
320 }
321 // Create ports, if they don't exist.
322 $added_count = $updated_count = $error_count = 0;
323 foreach ($ports as $port)
324 {
325 $port_id = getPortID ($object_id, $port['name']);
326 if ($port_id === NULL)
327 {
328 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
329 if ($result == '')
330 $added_count++;
331 else
332 $error_count++;
333 }
334 else
335 {
336 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
337 if ($result == '')
338 $updated_count++;
339 else
340 $error_count++;
341 }
342 }
ebb5441d 343 return buildRedirectURL ('OK', array ($added_count, $updated_count, $error_count));
e673ee24
DO
344}
345
b4c1ef87 346function updIPv4Allocation ()
e673ee24 347{
b4c1ef87
DO
348 assertIPv4Arg ('ip', __FUNCTION__);
349 assertUIntArg ('object_id', __FUNCTION__);
350 assertStringArg ('bond_name', __FUNCTION__, TRUE);
351 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 352
b4c1ef87 353 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 354 if ($error != '')
ebb5441d 355 return buildRedirectURL ('ERR', array ($error));
e673ee24 356 else
ebb5441d 357 return buildRedirectURL ('OK');
e673ee24
DO
358}
359
b4c1ef87 360function delIPv4Allocation ()
e673ee24 361{
b4c1ef87
DO
362 assertIPv4Arg ('ip', __FUNCTION__);
363 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 364
b4c1ef87 365 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 366 if ($error != '')
ebb5441d 367 return buildRedirectURL ('ERR', array ($error));
e673ee24 368 else
ebb5441d 369 return buildRedirectURL ('OK');
e673ee24
DO
370}
371
b4c1ef87 372function addIPv4Allocation ()
e673ee24 373{
b4c1ef87
DO
374 assertIPv4Arg ('ip', __FUNCTION__);
375 assertUIntArg ('object_id', __FUNCTION__);
376 assertStringArg ('bond_name', __FUNCTION__, TRUE);
377 assertStringArg ('bond_type', __FUNCTION__);
da958e52 378
b4c1ef87
DO
379 // Strip masklen.
380 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
bb0a44e9 381 if (NULL === getIPv4AddressNetworkId ($ip))
5222f192
DO
382 return buildRedirectURL ('ERR1', array ($ip));
383
b4c1ef87 384 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
5222f192
DO
385 if ($error != '')
386 return buildRedirectURL ('ERR2', array ($error));
53ef3908
DO
387 $address = getIPv4Address ($ip);
388 if ($address['reserved'] == 'yes' or !empty ($address['name']))
b4c1ef87
DO
389 {
390 $release = getConfigVar ('IPV4_AUTO_RELEASE');
391 if ($release >= 1)
392 $address['reserved'] = 'no';
393 if ($release >= 2)
394 $address['name'] = '';
395 updateAddress ($ip, $address['name'], $address['reserved']);
396 }
5222f192 397 return buildRedirectURL ('OK');
e673ee24
DO
398}
399
42023f03 400function addIPv4Prefix ()
e673ee24 401{
2c6c7645
DO
402 assertStringArg ('range', __FUNCTION__);
403 assertStringArg ('name', __FUNCTION__);
e673ee24 404
fa05e3de
DO
405 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
406 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 407 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 408 if ($error != '')
33bcd1ce 409 return buildRedirectURL ('ERR', array ($error));
e673ee24 410 else
ebb5441d 411 return buildRedirectURL ('OK');
e673ee24
DO
412}
413
42023f03 414function delIPv4Prefix ()
e673ee24 415{
da958e52 416 assertUIntArg ('id', __FUNCTION__);
42023f03 417 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 418 if ($error != '')
33bcd1ce 419 return buildRedirectURL ('ERR', array ($error));
e673ee24 420 else
ebb5441d 421 return buildRedirectURL ('OK');
e673ee24
DO
422}
423
04d619d0 424function updIPv4Prefix ()
e673ee24 425{
da958e52
DO
426 assertUIntArg ('id', __FUNCTION__);
427 assertStringArg ('name', __FUNCTION__);
e673ee24 428
da958e52 429 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
e673ee24 430 if ($error != '')
ebb5441d 431 return buildRedirectURL ('ERR', array ($error));
e673ee24 432 else
ebb5441d 433 return buildRedirectURL ('OK');
e673ee24
DO
434}
435
436function editAddress ()
437{
da958e52
DO
438 assertIPv4Arg ('ip', __FUNCTION__);
439 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 440
e673ee24
DO
441 if (isset ($_REQUEST['reserved']))
442 $reserved = $_REQUEST['reserved'];
443 else
444 $reserved = 'off';
da958e52 445 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 446 if ($error != '')
ebb5441d 447 return buildRedirectURL ('ERR', array ($error));
e673ee24 448 else
ebb5441d 449 return buildRedirectURL ('OK');
e673ee24
DO
450}
451
cced6b7d 452function createUser ()
e673ee24 453{
2c6c7645
DO
454 assertStringArg ('username', __FUNCTION__);
455 assertStringArg ('realname', __FUNCTION__, TRUE);
456 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
457 $username = $_REQUEST['username'];
458 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
459 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
460 if ($result == TRUE)
ebb5441d 461 return buildRedirectURL ('OK', array ($username));
e673ee24 462 else
ebb5441d 463 return buildRedirectURL ('ERR', array ($username));
e673ee24
DO
464}
465
cced6b7d 466function updateUser ()
e673ee24 467{
cced6b7d 468 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
469 assertStringArg ('username', __FUNCTION__);
470 assertStringArg ('realname', __FUNCTION__, TRUE);
471 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
472 $username = $_REQUEST['username'];
473 $new_password = $_REQUEST['password'];
103b1e1e 474 $old_hash = getHashByID ($_REQUEST['user_id']);
e673ee24 475 if ($old_hash == NULL)
ebb5441d 476 return buildRedirectURL ('ERR1');
e673ee24
DO
477 // Update user password only if provided password is not the same as current password hash.
478 if ($new_password != $old_hash)
479 $new_password = hash (PASSWORD_HASH, $new_password);
103b1e1e 480 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
e673ee24 481 if ($result == TRUE)
ebb5441d 482 return buildRedirectURL ('OK', array ($username));
e673ee24 483 else
ebb5441d 484 return buildRedirectURL ('ERR2', array ($username));
e673ee24
DO
485}
486
cced6b7d 487function enableUser ()
e673ee24 488{
cced6b7d
DO
489 assertUIntArg ('user_id', __FUNCTION__);
490 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
ebb5441d 491 return buildRedirectURL ('OK');
e673ee24 492 else
ebb5441d 493 return buildRedirectURL ('ERR');
e673ee24
DO
494}
495
cced6b7d 496function disableUser ()
e673ee24 497{
cced6b7d 498 assertUIntArg ('user_id', __FUNCTION__);
103b1e1e 499 if ($_REQUEST['user_id'] == 1)
ebb5441d
DO
500 return buildRedirectURL ('ERR1');
501 if (commitEnableUserAccount ($_REQUEST['user_id'], 'no'))
502 return buildRedirectURL ('OK');
e673ee24 503 else
ebb5441d 504 return buildRedirectURL ('ERR2');
e673ee24
DO
505}
506
e673ee24
DO
507// This function find differences in users's submit and PortCompat table
508// and modifies database accordingly.
509function savePortMap ()
510{
e673ee24
DO
511 $ptlist = getPortTypes();
512 $oldCompat = getPortCompat();
513 $newCompat = array();
514 foreach (array_keys ($ptlist) as $leftKey)
515 foreach (array_keys ($ptlist) as $rightKey)
516 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
517 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
518 // Build the new matrix from user's submit and compare it to
519 // the old matrix. Those pairs which appear on
520 // new matrix only, have to be stored in PortCompat table. Those who appear
521 // on the old matrix only, should be removed from PortCompat table.
522 // Those present in both matrices should be skipped.
523 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
524 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
525 $error_count = $success_count = 0;
526 foreach (array_keys ($ptlist) as $type1)
527 foreach (array_keys ($ptlist) as $type2)
528 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
529 switch ($oldCompatTable[$type1][$type2])
530 {
531 case TRUE: // new value is FALSE
532 if (removePortCompat ($type1, $type2) === TRUE)
533 $success_count++;
534 else
535 $error_count++;
536 break;
537 case FALSE: // new value is TRUE
538 if (addPortCompat ($type1, $type2) === TRUE)
539 $success_count++;
540 else
541 $error_count++;
542 break;
543 default:
ebb5441d 544 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
545 break;
546 }
103b1e1e 547 if ($error_count == 0)
ebb5441d 548 return buildRedirectURL ('OK', array ($error_count, $success_count));
103b1e1e 549 else
ebb5441d 550 return buildRedirectURL ('ERR', array ($error_count, $success_count));
e673ee24
DO
551}
552
553function updateDictionary ()
554{
2c6c7645
DO
555 assertUIntArg ('chapter_no', __FUNCTION__);
556 assertUIntArg ('dict_key', __FUNCTION__);
557 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 558 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
ebb5441d 559 return buildRedirectURL ('OK');
e673ee24 560 else
ebb5441d 561 return buildRedirectURL ('ERR');
e673ee24
DO
562}
563
564function supplementDictionary ()
565{
2c6c7645
DO
566 assertUIntArg ('chapter_no', __FUNCTION__);
567 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 568 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
ebb5441d 569 return buildRedirectURL ('OK');
e673ee24 570 else
ebb5441d 571 return buildRedirectURL ('ERR');
e673ee24
DO
572}
573
574function reduceDictionary ()
575{
2c6c7645
DO
576 assertUIntArg ('chapter_no', __FUNCTION__);
577 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24 578 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
ebb5441d 579 return buildRedirectURL ('OK');
e673ee24 580 else
ebb5441d 581 return buildRedirectURL ('ERR');
e673ee24
DO
582}
583
584function addChapter ()
585{
2c6c7645 586 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 587 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
ebb5441d 588 return buildRedirectURL ('OK');
e673ee24 589 else
ebb5441d 590 return buildRedirectURL ('ERR');
e673ee24
DO
591}
592
593function updateChapter ()
594{
2c6c7645
DO
595 assertUIntArg ('chapter_no', __FUNCTION__);
596 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 597 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
ebb5441d 598 return buildRedirectURL ('OK');
e673ee24 599 else
ebb5441d 600 return buildRedirectURL ('ERR');
e673ee24
DO
601}
602
603function delChapter ()
604{
2c6c7645 605 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 606 if (commitDeleteChapter ($_REQUEST['chapter_no']))
ebb5441d 607 return buildRedirectURL ('OK');
e673ee24 608 else
ebb5441d 609 return buildRedirectURL ('ERR');
e673ee24
DO
610}
611
612function changeAttribute ()
613{
2c6c7645
DO
614 assertUIntArg ('attr_id', __FUNCTION__);
615 assertStringArg ('attr_name', __FUNCTION__);
e673ee24 616 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
ebb5441d 617 return buildRedirectURL ('OK');
e673ee24 618 else
ebb5441d 619 return buildRedirectURL ('ERR');
e673ee24
DO
620}
621
622function createAttribute ()
623{
2c6c7645
DO
624 assertStringArg ('attr_name', __FUNCTION__);
625 assertStringArg ('attr_type', __FUNCTION__);
e673ee24 626 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
ebb5441d 627 return buildRedirectURL ('OK', array ($_REQUEST['attr_name']));
e673ee24 628 else
ebb5441d 629 return buildRedirectURL ('ERR');
e673ee24
DO
630}
631
632function deleteAttribute ()
633{
2c6c7645 634 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24 635 if (commitDeleteAttribute ($_REQUEST['attr_id']))
ebb5441d 636 return buildRedirectURL ('OK');
e673ee24 637 else
ebb5441d 638 return buildRedirectURL ('ERR');
e673ee24
DO
639}
640
641function supplementAttrMap ()
642{
2c6c7645
DO
643 assertUIntArg ('attr_id', __FUNCTION__);
644 assertUIntArg ('objtype_id', __FUNCTION__);
645 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 646 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
ebb5441d 647 return buildRedirectURL ('OK');
e673ee24 648 else
ebb5441d 649 return buildRedirectURL ('ERR');
e673ee24
DO
650}
651
652function reduceAttrMap ()
653{
2c6c7645
DO
654 assertUIntArg ('attr_id', __FUNCTION__);
655 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24 656 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
ebb5441d 657 return buildRedirectURL ('OK');
e673ee24 658 else
ebb5441d 659 return buildRedirectURL ('ERR');
e673ee24
DO
660}
661
22bb04da 662function clearSticker ()
e673ee24 663{
2c6c7645
DO
664 assertUIntArg ('attr_id', __FUNCTION__);
665 assertUIntArg ('object_id', __FUNCTION__);
22bb04da 666 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
ebb5441d 667 return buildRedirectURL ('OK');
e673ee24 668 else
ebb5441d 669 return buildRedirectURL ('ERR');
e673ee24
DO
670}
671
22bb04da
DO
672function updateObject ()
673{
674 assertUIntArg ('object_id', __FUNCTION__);
675 assertUIntArg ('object_type_id', __FUNCTION__);
676 assertStringArg ('object_name', __FUNCTION__, TRUE);
677 assertStringArg ('object_label', __FUNCTION__, TRUE);
678 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
679 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
680 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
681 $has_problems = 'yes';
682 else
683 $has_problems = 'no';
684
685 if (commitUpdateObject (
686 $_REQUEST['object_id'],
687 $_REQUEST['object_name'],
688 $_REQUEST['object_label'],
689 $_REQUEST['object_barcode'],
690 $_REQUEST['object_type_id'],
691 $has_problems,
692 $_REQUEST['object_asset_no'],
693 $_REQUEST['object_comment']
694 ) !== TRUE)
ebb5441d 695 return buildRedirectURL ('ERR');
22bb04da
DO
696 // Invalidate thumb cache of all racks objects could occupy.
697 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
698 resetThumbCache ($rack_id);
ebb5441d 699 return buildRedirectURL ('OK');
22bb04da
DO
700}
701
702function updateStickers ()
e673ee24 703{
2c6c7645 704 assertUIntArg ('object_id', __FUNCTION__);
f59e8cf5
DO
705 assertUIntArg ('num_attrs', __FUNCTION__);
706 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a
DO
707 $result = array();
708
103b1e1e 709 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
0dfb8a2a 710 {
2c6c7645 711 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
712 $attr_id = $_REQUEST["${i}_attr_id"];
713
714 // Field is empty, delete attribute and move on.
715 if (empty($_REQUEST["${i}_value"]))
716 {
103b1e1e 717 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
718 continue;
719 }
720
721 // The value could be uint/float, but we don't know ATM. Let SQL
722 // server check this and complain.
2c6c7645 723 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
724 $value = $_REQUEST["${i}_value"];
725 switch ($oldvalues[$attr_id]['type'])
726 {
727 case 'uint':
728 case 'float':
729 case 'string':
730 $oldvalue = $oldvalues[$attr_id]['value'];
731 break;
732 case 'dict':
733 $oldvalue = $oldvalues[$attr_id]['key'];
734 break;
735 default:
61e269b5 736 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
737 die;
738 }
739 if ($value == $oldvalue)
740 continue;
741
742 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 743 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a
DO
744 }
745
103b1e1e 746 if (in_array (FALSE, $result))
ebb5441d 747 return buildRedirectURL ('ERR');
0dfb8a2a 748
ebb5441d 749 return buildRedirectURL ('OK');
e673ee24
DO
750}
751
752function useupPort ()
753{
2c6c7645 754 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 755 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
ebb5441d 756 return buildRedirectURL ('OK');
e673ee24 757 else
ebb5441d 758 return buildRedirectURL ('ERR');
e673ee24
DO
759}
760
4fe32e78
DY
761function updateUI ()
762{
2c6c7645 763 assertUIntArg ('num_vars', __FUNCTION__);
c461c579 764 $error = '';
4fe32e78 765
103b1e1e 766 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 767 {
2c6c7645
DO
768 assertStringArg ("${i}_varname", __FUNCTION__);
769 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 770 $varname = $_REQUEST["${i}_varname"];
4fe32e78 771 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 772
c461c579
DO
773 // If form value = value in DB, don't bother updating DB
774 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
775 continue;
776
777 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
778 $error = setConfigVar ($varname, $varvalue, TRUE);
779 if (!empty ($error))
780 break;
4fe32e78
DY
781 }
782
c461c579 783 if ($error != '')
ebb5441d 784 return buildRedirectURL ('ERR', array ($error));
4fe32e78 785
ebb5441d 786 return buildRedirectURL ('OK');
4fe32e78
DY
787}
788
b07f617c
DO
789function resetUIConfig()
790{
1f86e996 791 setConfigVar ('default_port_type','24');
b07f617c
DO
792 setConfigVar ('MASSCOUNT','15');
793 setConfigVar ('MAXSELSIZE','30');
794 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
795 setConfigVar ('ROW_SCALE','2');
796 setConfigVar ('PORTS_PER_ROW','12');
797 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
798 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 799 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
800 setConfigVar ('USER_AUTH_SRC','database');
801 setConfigVar ('DEFAULT_SLB_VS_PORT','');
802 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 803 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 804 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 805 setConfigVar ('DETECT_URLS','no');
4b8d413e 806 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 807 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 808 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
809 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
810 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
811 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 812 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 813 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df
DO
814 setConfigVar ('SHOW_LAST_TAB', 'no');
815 setConfigVar ('COOKIE_TTL', '1209600');
f0ed1181 816 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 817 setConfigVar ('TREE_THRESHOLD', '25');
ebb5441d 818 return buildRedirectURL ('OK');
b07f617c
DO
819}
820
732e4578 821// Add single record.
ca461127
DO
822function addRealServer ()
823{
2c6c7645
DO
824 assertUIntArg ('pool_id', __FUNCTION__);
825 assertIPv4Arg ('remoteip', __FUNCTION__);
79a9edb4 826 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 827 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
828 if (!addRStoRSPool (
829 $_REQUEST['pool_id'],
830 $_REQUEST['remoteip'],
831 $_REQUEST['rsport'],
832 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
833 $_REQUEST['rsconfig']
834 ))
ebb5441d 835 return buildRedirectURL ('ERR');
d6517a21 836 else
ebb5441d 837 return buildRedirectURL ('OK');
d6517a21
DO
838}
839
732e4578
DO
840// Parse textarea submitted and try adding a real server for each line.
841function addRealServers ()
842{
cbadbb38 843 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
844 assertStringArg ('format', __FUNCTION__);
845 assertStringArg ('rawtext', __FUNCTION__);
732e4578
DO
846 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
847 $ngood = $nbad = 0;
848 $rsconfig = '';
849 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
850 foreach (explode ('\n', $rawtext) as $line)
851 {
852 if (empty ($line))
853 continue;
854 $match = array ();
855 switch ($_REQUEST['format'])
856 {
857 case 'ipvs_2': // address and port only
858 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
859 continue;
103b1e1e 860 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
861 $ngood++;
862 else
863 $nbad++;
864 break;
865 case 'ipvs_3': // address, port and weight
866 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
867 continue;
103b1e1e 868 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
869 $ngood++;
870 else
871 $nbad++;
872 break;
e69c2aa3
DO
873 case 'ssv_2': // IP address and port
874 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
875 continue;
103b1e1e 876 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
877 $ngood++;
878 else
879 $nbad++;
880 break;
948c37e3
DO
881 case 'ssv_1': // IP address
882 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
883 continue;
884 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
885 $ngood++;
886 else
887 $nbad++;
888 break;
732e4578 889 default:
ebb5441d 890 return buildRedirectURL ('ERR1');
732e4578
DO
891 break;
892 }
893 }
894 if ($nbad == 0 and $ngood > 0)
ebb5441d 895 return buildRedirectURL ('OK', array ($ngood));
732e4578 896 else
ebb5441d 897 return buildRedirectURL ('ERR2', array ($ngood, $nbad));
732e4578
DO
898}
899
d6517a21
DO
900function addVService ()
901{
2c6c7645
DO
902 assertIPv4Arg ('vip', __FUNCTION__);
903 assertUIntArg ('vport', __FUNCTION__);
904 assertStringArg ('proto', __FUNCTION__);
103b1e1e 905 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
ebb5441d 906 return buildRedirectURL ('ERR1');
2c6c7645
DO
907 assertStringArg ('name', __FUNCTION__, TRUE);
908 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
909 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
910 $error = commitCreateVS
911 (
103b1e1e
DO
912 $_REQUEST['vip'],
913 $_REQUEST['vport'],
7320b661 914 $_REQUEST['proto'],
103b1e1e
DO
915 $_REQUEST['name'],
916 $_REQUEST['vsconfig'],
c63a8d6e
DO
917 $_REQUEST['rsconfig'],
918 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
919 );
920 if ($error != '')
ebb5441d 921 return buildRedirectURL ('ERR2', array ($error));
ca461127 922 else
ebb5441d 923 return buildRedirectURL ('OK');
fb1c4a54
DO
924}
925
926function deleteRealServer ()
927{
2c6c7645 928 assertUIntArg ('id', __FUNCTION__);
d6517a21 929 if (!commitDeleteRS ($_REQUEST['id']))
ebb5441d 930 return buildRedirectURL ('ERR');
fb1c4a54 931 else
ebb5441d 932 return buildRedirectURL ('OK');
fb1c4a54
DO
933}
934
3241551e
DO
935function deleteLoadBalancer ()
936{
2c6c7645
DO
937 assertUIntArg ('object_id', __FUNCTION__);
938 assertUIntArg ('pool_id', __FUNCTION__);
939 assertUIntArg ('vs_id', __FUNCTION__);
103b1e1e
DO
940 if (!commitDeleteLB (
941 $_REQUEST['object_id'],
942 $_REQUEST['pool_id'],
943 $_REQUEST['vs_id']
944 ))
ebb5441d 945 return buildRedirectURL ('ERR');
d6517a21 946 else
ebb5441d 947 return buildRedirectURL ('OK');
d6517a21
DO
948}
949
950function deleteVService ()
951{
e02e1941
DO
952 assertUIntArg ('vs_id', __FUNCTION__);
953 if (!commitDeleteVS ($_REQUEST['vs_id']))
ebb5441d 954 return buildRedirectURL ('ERR');
3241551e 955 else
ebb5441d 956 return buildRedirectURL ('OK');
3241551e
DO
957}
958
fb1c4a54
DO
959function updateRealServer ()
960{
2c6c7645
DO
961 assertUIntArg ('rs_id', __FUNCTION__);
962 assertIPv4Arg ('rsip', __FUNCTION__);
79a9edb4 963 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 964 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
965 if (!commitUpdateRS (
966 $_REQUEST['rs_id'],
967 $_REQUEST['rsip'],
968 $_REQUEST['rsport'],
969 $_REQUEST['rsconfig']
970 ))
ebb5441d 971 return buildRedirectURL ('ERR');
fb1c4a54 972 else
ebb5441d 973 return buildRedirectURL ('OK');
ca461127
DO
974}
975
c1ca768c 976function updateLoadBalancer ()
3241551e 977{
2c6c7645
DO
978 assertUIntArg ('object_id', __FUNCTION__);
979 assertUIntArg ('pool_id', __FUNCTION__);
980 assertUIntArg ('vs_id', __FUNCTION__);
981 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
982 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
983 if (!commitUpdateLB (
984 $_REQUEST['object_id'],
985 $_REQUEST['pool_id'],
986 $_REQUEST['vs_id'],
987 $_REQUEST['vsconfig'],
988 $_REQUEST['rsconfig']
989 ))
ebb5441d 990 return buildRedirectURL ('ERR');
d6517a21 991 else
ebb5441d 992 return buildRedirectURL ('OK');
d6517a21
DO
993}
994
995function updateVService ()
996{
e02e1941 997 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
998 assertIPv4Arg ('vip', __FUNCTION__);
999 assertUIntArg ('vport', __FUNCTION__);
1000 assertStringArg ('proto', __FUNCTION__);
1001 assertStringArg ('name', __FUNCTION__, TRUE);
1002 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1003 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1004 if (!commitUpdateVS (
1005 $_REQUEST['vs_id'],
1006 $_REQUEST['vip'],
1007 $_REQUEST['vport'],
1008 $_REQUEST['proto'],
1009 $_REQUEST['name'],
1010 $_REQUEST['vsconfig'],
1011 $_REQUEST['rsconfig']
1012 ))
ebb5441d 1013 return buildRedirectURL ('ERR');
3241551e 1014 else
ebb5441d 1015 return buildRedirectURL ('OK');
3241551e
DO
1016}
1017
1018function addLoadBalancer ()
1019{
2c6c7645
DO
1020 assertUIntArg ('pool_id', __FUNCTION__);
1021 assertUIntArg ('object_id', __FUNCTION__);
1022 assertUIntArg ('vs_id', __FUNCTION__);
1023 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1024 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1025 if (!addLBtoRSPool (
1026 $_REQUEST['pool_id'],
1027 $_REQUEST['object_id'],
1028 $_REQUEST['vs_id'],
1029 $_REQUEST['vsconfig'],
1030 $_REQUEST['rsconfig']
1031 ))
ebb5441d 1032 return buildRedirectURL ('ERR');
3241551e 1033 else
ebb5441d 1034 return buildRedirectURL ('OK');
3241551e
DO
1035}
1036
5ad76f01
DO
1037function addRSPool ()
1038{
2c6c7645
DO
1039 assertStringArg ('name', __FUNCTION__, TRUE);
1040 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1041 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1042 $error = commitCreateRSPool
1043 (
103b1e1e
DO
1044 $_REQUEST['name'],
1045 $_REQUEST['vsconfig'],
c63a8d6e
DO
1046 $_REQUEST['rsconfig'],
1047 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1048 );
1049 if ($error != '')
ebb5441d 1050 return buildRedirectURL ('ERR', array ($error));
5ad76f01 1051 else
ebb5441d 1052 return buildRedirectURL ('OK');
5ad76f01
DO
1053}
1054
1055function deleteRSPool ()
1056{
2c6c7645 1057 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1058 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
ebb5441d 1059 return buildRedirectURL ('ERR');
5ad76f01 1060 else
ebb5441d 1061 return buildRedirectURL ('OK');
5ad76f01
DO
1062}
1063
1064function updateRSPool ()
1065{
2c6c7645
DO
1066 assertUIntArg ('pool_id', __FUNCTION__);
1067 assertStringArg ('name', __FUNCTION__, TRUE);
1068 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1069 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1070 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
ebb5441d 1071 return buildRedirectURL ('ERR');
5ad76f01 1072 else
ebb5441d 1073 return buildRedirectURL ('OK');
5ad76f01
DO
1074}
1075
1f7d18fa
DO
1076function updateRSInService ()
1077{
2c6c7645 1078 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1079 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1080 $orig = getRSPoolInfo ($pool_id);
1081 $nbad = $ngood = 0;
1082 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1083 {
1084 $rs_id = $_REQUEST["rsid_${i}"];
1085 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1086 $newval = 'yes';
1087 else
1088 $newval = 'no';
1089 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1090 {
1091 if (commitSetInService ($rs_id, $newval))
1092 $ngood++;
1093 else
1094 $nbad++;
1095 }
1096 }
1097 if (!$nbad)
ebb5441d 1098 return buildRedirectURL ('OK', array ($ngood));
1f7d18fa 1099 else
ebb5441d 1100 return buildRedirectURL ('ERR', array ($nbad, $ngood));
1f7d18fa
DO
1101}
1102
8d9c16e7
DO
1103// FIXME: check, that each submitted address belongs to the prefix we
1104// are operating on.
03eb5209
DO
1105function importPTRData ()
1106{
2c6c7645 1107 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209 1108 $nbad = $ngood = 0;
03eb5209
DO
1109 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1110 {
3f3bd41e 1111 $inputname = "import_${i}";
03eb5209
DO
1112 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1113 continue;
2c6c7645
DO
1114 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1115 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1116 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1117 // Non-existent addresses will not have this argument set in request.
1118 $rsvd = 'no';
03eb5209
DO
1119 if ($_REQUEST["rsvd_${i}"] == 'yes')
1120 $rsvd = 'yes';
3f3bd41e 1121 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1122 $ngood++;
1123 else
1124 $nbad++;
1125 }
1126 if (!$nbad)
ebb5441d 1127 return buildRedirectURL ('OK', array ($ngood));
03eb5209 1128 else
ebb5441d 1129 return buildRedirectURL ('ERR', array ($nbad, $ngood));
03eb5209
DO
1130}
1131
f3f0161f
DO
1132function generateAutoPorts ()
1133{
103b1e1e 1134 global $pageno;
2c6c7645 1135 assertUIntArg ('object_id', __FUNCTION__);
103b1e1e 1136 $info = getObjectInfo ($_REQUEST['object_id']);
f3f0161f 1137 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1138 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
ebb5441d 1139 return buildRedirectURL ('OK', array(), $pageno, 'ports');
f3f0161f 1140 else
ebb5441d 1141 return buildRedirectURL ('ERR');
f3f0161f
DO
1142}
1143
24cbe8af 1144// Filter out implicit tags before storing the new tag set.
3355ca56 1145function saveEntityTags ()
24cbe8af 1146{
3355ca56
DO
1147 $page2realm = array
1148 (
1149 'object' => 'object',
1150 'iprange' => 'ipv4net',
1151 'rack' => 'rack',
1152 'ipv4vs' => 'ipv4vs',
1153 'ipv4rsp' => 'ipv4rspool',
1154 'user' => 'user'
1155 );
1156 global $explicit_tags, $implicit_tags, $page, $pageno;
1157 if (!isset ($page2realm[$pageno]) or !isset ($page[$pageno]['bypass']))
1158 {
1159 showError ('Internal error', __FUNCTION__);
1160 die;
1161 }
1162 $realm = $page2realm[$pageno];
1163 $bypass = $page[$pageno]['bypass'];
2c6c7645 1164 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1165 $entity_id = $_REQUEST[$bypass];
e04931c8 1166 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1167 // Build a chain from the submitted data, minimize it,
ab379543 1168 // then wipe existing records and store the new set instead.
01b6b4d6 1169 destroyTagsForEntity ($realm, $entity_id);
6e49bd1f 1170 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1171 $n_succeeds = $n_errors = 0;
6e49bd1f 1172 foreach ($newchain as $taginfo)
eb6ea26f 1173 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1174 $n_succeeds++;
1175 else
1176 $n_errors++;
ab379543 1177 if ($n_errors)
ebb5441d 1178 return buildRedirectURL ('ERR', array ($n_succeeds, $n_errors));
ab379543 1179 else
ebb5441d 1180 return buildRedirectURL ('OK', array ($n_succeeds));
2034d968
DO
1181}
1182
fe7044ad
DO
1183function destroyTag ()
1184{
49fb1027
DO
1185 assertUIntArg ('tag_id', __FUNCTION__);
1186 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
ebb5441d 1187 return buildRedirectURL ('OK');
fe7044ad 1188 else
ebb5441d 1189 return buildRedirectURL ('ERR');
fe7044ad
DO
1190}
1191
1192function createTag ()
1193{
49fb1027 1194 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1195 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1196 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1197 if (!validTagName ($tagname))
ebb5441d 1198 return buildRedirectURL ('ERR1', array ($tagname));
0f3c6639 1199 if (tagExistsInDatabase ($tagname))
ebb5441d 1200 return buildRedirectURL ('ERR2', array ($tagname));
fe7044ad
DO
1201 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1202 $parent_id = 'NULL';
1203 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
ebb5441d 1204 return buildRedirectURL ('OK', array ($tagname));
fe7044ad 1205 else
33b76fd5 1206 return buildRedirectURL ('ERR3', array ($tagname, $ret));
fe7044ad
DO
1207}
1208
49fb1027
DO
1209function updateTag ()
1210{
49fb1027
DO
1211 assertUIntArg ('tag_id', __FUNCTION__);
1212 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1213 assertStringArg ('tag_name', __FUNCTION__);
1214 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1215 if (!validTagName ($tagname))
33b76fd5 1216 return buildRedirectURL ('ERR1', array ($tagname));
49fb1027
DO
1217 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1218 $parent_id = 'NULL';
1219 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
ebb5441d 1220 return buildRedirectURL ('OK', array ($tagname));
49fb1027 1221 else
33b76fd5 1222 return buildRedirectURL ('ERR2', array ($tagname, $ret));
49fb1027
DO
1223}
1224
eb6ea26f
DO
1225function rollTags ()
1226{
eb6ea26f
DO
1227 assertUIntArg ('row_id', __FUNCTION__);
1228 assertStringArg ('sum', __FUNCTION__, TRUE);
1229 assertUIntArg ('realsum', __FUNCTION__);
eb6ea26f 1230 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
ebb5441d 1231 return buildRedirectURL ('ERR');
abef7149
DO
1232 // Even if the user requested an empty tag list, don't bail out, but process existing
1233 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1234 // have its chains refined to "normal" form.
1235 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1236 $n_ok = 0;
1237 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1238 // filter out the same tag again and again. It will have own noise to cancel.
1239 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
1240 foreach (getRacksForRow ($_REQUEST['row_id']) as $rackInfo)
1241 {
1242 if (rebuildTagChainForEntity ('rack', $rackInfo['id'], $extrachain))
1243 $n_ok++;
1244 foreach (stuffInRackspace (getRackData ($rackInfo['id'])) as $object_id)
1245 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1246 $n_ok++;
1247 }
1248 return buildRedirectURL ('OK', array ($n_ok));
eb6ea26f
DO
1249}
1250
9457ca59 1251function changeMyPassword ()
cced6b7d 1252{
103b1e1e 1253 global $accounts, $remote_username;
9457ca59 1254 if (getConfigVar ('USER_AUTH_SRC') != 'database')
ebb5441d 1255 return buildRedirectURL ('ERR1');
0ac7e4b3
DO
1256 assertStringArg ('oldpassword', __FUNCTION__);
1257 assertStringArg ('newpassword1', __FUNCTION__);
1258 assertStringArg ('newpassword2', __FUNCTION__);
1259 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
ebb5441d 1260 return buildRedirectURL ('ERR2');
cced6b7d 1261 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
ebb5441d 1262 return buildRedirectURL ('ERR3');
0ac7e4b3 1263 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
ebb5441d 1264 return buildRedirectURL ('OK');
cced6b7d 1265 else
ebb5441d 1266 return buildRedirectURL ('ERR4');
cced6b7d
DO
1267}
1268
1269function saveRackCode ()
1270{
cced6b7d 1271 assertStringArg ('rackcode');
e6a4adb9
DO
1272 // For the test to succeed, unescape LFs, strip CRs.
1273 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1274 $parseTree = getRackCode ($newcode);
1275 if ($parseTree['result'] != 'ACK')
ebb5441d 1276 return buildRedirectURL ('ERR1', array ($parseTree['load']));
4a6a28f1 1277 saveScript ('RackCodeCache', '');
cf25e649 1278 if (saveScript ('RackCode', $newcode))
ebb5441d 1279 return buildRedirectURL ('OK');
cced6b7d 1280 else
ebb5441d 1281 return buildRedirectURL ('ERR2');
cced6b7d
DO
1282}
1283
46f92ff7
DO
1284// This handler's context is pre-built, but not authorized. It is assumed, that the
1285// handler will take existing context and before each commit check authorization
1286// on the base chain plus necessary context added.
1287function setPortVLAN ()
1288{
1289 assertUIntArg ('portcount', __FUNCTION__);
1290 $data = getSwitchVLANs ($_REQUEST['object_id']);
1291 if ($data === NULL)
ebb5441d 1292 return buildRedirectURL ('ERR1');
46f92ff7
DO
1293 list ($vlanlist, $portlist) = $data;
1294 // Here we just build up 1 set command for the gateway with all of the ports
1295 // included. The gateway is expected to filter unnecessary changes silently
1296 // and to provide a list of responses with either error or success message
1297 // for each of the rest.
1298 $nports = $_REQUEST['portcount'];
1299 $prefix = 'set ';
73067b6a 1300 $log = array ('v' => 2, 'm' => array());
46f92ff7
DO
1301 $setcmd = '';
1302 for ($i = 0; $i < $nports; $i++)
1303 if
1304 (
1305 !isset ($_REQUEST['portname_' . $i]) ||
1306 !isset ($_REQUEST['vlanid_' . $i]) ||
1307 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1308 )
24dcb9d8 1309 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1310 elseif
1311 (
1312 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1313 $portlist[$i]['vlaind'] == 'TRUNK'
1314 )
1315 continue;
1316 else
1317 {
1318 $portname = $_REQUEST['portname_' . $i];
1319 $oldvlanid = $portlist[$i]['vlanid'];
1320 $newvlanid = $_REQUEST['vlanid_' . $i];
1321 // Finish the security context and evaluate it.
1322 $annex = array();
1323 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1324 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1325 if (!permitted (NULL, NULL, NULL, $annex))
1326 {
24dcb9d8 1327 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1328 continue;
1329 }
1330 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1331 $prefix = ';';
1332 }
1333 // Feed the gateway and interpret its (non)response.
1334 if ($setcmd != '')
f0ff4930 1335 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1336 else
24dcb9d8 1337 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1338 return buildWideRedirectURL ($log);
1339}
1340
2987fc1f
DO
1341function submitSLBConfig ()
1342{
1343 assertUIntArg ('object_id', __FUNCTION__);
1344 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
3dd72e34 1345 $msglog = gwSendFile ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1346 return buildWideRedirectURL ($msglog);
1347}
1348
f19c75d6
DO
1349function addRack ()
1350{
1351 assertUIntArg ('row_id', __FUNCTION__);
1352 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1353 if (isset ($_REQUEST['got_data']))
1354 {
1355 assertStringArg ('rack_name', __FUNCTION__);
1356 assertUIntArg ('rack_height1', __FUNCTION__);
1357 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1358
1359 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
1360 return buildRedirectURL ('OK', array ($_REQUEST['rack_name']));
1361 else
1362 return buildRedirectURL ('ERR1', array ($_REQUEST['rack_name']));
1363 }
1364 elseif (isset ($_REQUEST['got_mdata']))
1365 {
1366 assertUIntArg ('rack_height2', __FUNCTION__);
1367 assertStringArg ('rack_names', __FUNCTION__, TRUE);
1368 $log = array ('v' => 2);
1369 // copy-and-paste from renderAddMultipleObjectsForm()
1370 $names1 = explode ('\n', $_REQUEST['rack_names']);
1371 $names2 = array();
1372 foreach ($names1 as $line)
1373 {
1374 $parts = explode ('\r', $line);
1375 reset ($parts);
1376 if (empty ($parts[0]))
1377 continue;
1378 else
1379 $names2[] = rtrim ($parts[0]);
1380 }
1381 foreach ($names2 as $cname)
1382 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
1383 $log['m'][] = array ('c' => getMessageCode ('OK'), 'a' => array ($cname));
1384 else
1385 $log['m'][] = array ('c' => getMessageCode ('ERR1'), 'a' => array ($cname));
1386 return buildWideRedirectURL ($log);
1387 }
1388 else
1389 return buildRedirectURL ('ERR2');
f19c75d6
DO
1390}
1391
7056988c
DO
1392function updateRack ()
1393{
7056988c
DO
1394 assertUIntArg ('rack_id', __FUNCTION__);
1395 assertUIntArg ('rack_row_id', __FUNCTION__);
1396 assertUIntArg ('rack_height', __FUNCTION__);
1397 assertStringArg ('rack_name', __FUNCTION__);
1398 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
7056988c 1399
7056988c 1400 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a
DO
1401 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
1402 return buildRedirectURL ('OK', array ($_REQUEST['rack_name']));
1403 else
1404 return buildRedirectURL ('ERR');
7056988c
DO
1405}
1406
1407function querySNMPData ()
1408{
1409 assertUIntArg ('object_id', __FUNCTION__);
1410 assertStringArg ('community', __FUNCTION__);
83fdf85f 1411 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
7056988c
DO
1412}
1413
e673ee24 1414?>