r4095 genericAssertion(): add 'enum/wdmstd' and 'iif' cases
[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
08408472
AA
8// This array is deprecated. Please do not add new message constants to it.
9// use the new showError, showWarning, showSuccess functions instead
b6a7d936 10$msgcode = array();
17c32f4c 11
08408472
AA
12// This function is DEPRECATED. Show messages through showError and showSuccess,
13// you dont need to return anything from an ophandler to redirect user back to the page containing submit form
14function buildWideRedirectURL ($log = NULL, $nextpage = NULL, $nexttab = NULL, $moreArgs = array())
17c32f4c 15{
790a60e8 16 global $page, $pageno, $tabno;
b6a7d936
DO
17 if ($nextpage === NULL)
18 $nextpage = $pageno;
19 if ($nexttab === NULL)
20 $nexttab = $tabno;
790a60e8 21 $url = "index.php?page=${nextpage}&tab=${nexttab}";
b6a7d936
DO
22 if (isset ($page[$nextpage]['bypass']))
23 $url .= '&' . $page[$nextpage]['bypass'] . '=' . $_REQUEST[$page[$nextpage]['bypass']];
17c32f4c 24
b6a7d936 25 if (count($moreArgs)>0)
17c32f4c 26 {
b6a7d936
DO
27 foreach($moreArgs as $arg=>$value)
28 {
29 if (gettype($value) == 'array')
30 {
31 foreach ($value as $v)
32 {
33 $url .= '&'.urlencode($arg.'[]').'='.urlencode($v);
34 }
35 }
36 else
37 $url .= '&'.urlencode($arg).'='.urlencode($value);
38 }
17c32f4c 39 }
b6a7d936 40
08408472
AA
41 if (! empty ($log))
42 {
43 if (empty ($_SESSION['log']))
44 $_SESSION['log'] = $log;
45 elseif ($_SESSION['log']['v'] == $log['v'])
46 $_SESSION['log'] = array_merge_recursive($log, $_SESSION['log']);
47 elseif ($log['v'] == 1 and $_SESSION['log']['v'] == 2)
48 foreach ($log['m'] as $msg)
49 setMessage($msg['message'], $msg['code'], FALSE);
50 elseif ($log['v'] == 2 and $_SESSION['log']['v'] == 1)
51 {
52 foreach ($_SESSION['log'] as $msg)
53 {
54 if (! is_array ($msg))
55 continue;
56 var_dump ($msg);
57 $new_v2_item = array('c' => '', 'a' => array());
58 switch ($msg['code'])
59 {
60 case 'error':
61 $new_v2_item['c'] = 100;
62 break;
63 case 'success':
64 $new_v2_item['c'] = 0;
65 break;
66 case 'warning':
67 $new_v2_item['c'] = 200;
68 break;
69 default:
70 $new_v2_item['c'] = 300;
71 }
72 $new_v2_item['a'][] = $msg['message'];
73 $log['m'][] = $new_v2_item;
74 }
75 $_SESSION['log'] = $log; // substitute v1 log structure with merged v2
76 }
77 }
b6a7d936 78 return $url;
17c32f4c
DO
79}
80
08408472
AA
81// This function is DEPRECATED. Show messages through showError and showSuccess,
82// you dont need to return anything from an ophandler to redirect user back to the page containing submit form
05d3c190 83function buildRedirectURL ($callfunc, $status, $log_args = array(), $nextpage = NULL, $nexttab = NULL, $url_args = array())
17c32f4c 84{
b6a7d936
DO
85 global $pageno, $tabno, $msgcode;
86 if ($nextpage === NULL)
87 $nextpage = $pageno;
88 if ($nexttab === NULL)
89 $nexttab = $tabno;
05d3c190 90 return buildWideRedirectURL (oneLiner ($msgcode[$callfunc][$status], $log_args), $nextpage, $nexttab, $url_args);
17c32f4c
DO
91}
92
b6a7d936
DO
93$msgcode['addPortForwarding']['OK'] = 2;
94$msgcode['addPortForwarding']['ERR'] = 100;
e673ee24
DO
95function addPortForwarding ()
96{
0cc24e9a
DY
97 assertUIntArg ('object_id');
98 assertIPv4Arg ('localip');
99 assertIPv4Arg ('remoteip');
100 assertUIntArg ('localport');
101 assertStringArg ('proto');
102 assertStringArg ('description', TRUE);
103b1e1e 103 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
59a83bd8 104 if (!strlen ($remoteport))
f75eb878 105 $remoteport = $_REQUEST['localport'];
e673ee24 106
9bea3a8b
DO
107 $error = newPortForwarding
108 (
103b1e1e 109 $_REQUEST['object_id'],
9bea3a8b
DO
110 $_REQUEST['localip'],
111 $_REQUEST['localport'],
112 $_REQUEST['remoteip'],
113 $remoteport,
114 $_REQUEST['proto'],
115 $_REQUEST['description']
116 );
e673ee24
DO
117
118 if ($error == '')
135080d8 119 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 120 else
135080d8 121 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
122}
123
b6a7d936 124$msgcode['delPortForwarding']['OK'] = 3;
a5c589d2 125$msgcode['delPortForwarding']['ERR'] = 111;
e673ee24
DO
126function delPortForwarding ()
127{
0cc24e9a
DY
128 assertUIntArg ('object_id');
129 assertIPv4Arg ('localip');
130 assertIPv4Arg ('remoteip');
131 assertUIntArg ('localport');
132 assertUIntArg ('remoteport');
133 assertStringArg ('proto');
e673ee24 134
a5c589d2 135 $result = deletePortForwarding
103b1e1e
DO
136 (
137 $_REQUEST['object_id'],
138 $_REQUEST['localip'],
139 $_REQUEST['localport'],
140 $_REQUEST['remoteip'],
141 $_REQUEST['remoteport'],
142 $_REQUEST['proto']
143 );
a5c589d2 144 buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
e673ee24
DO
145}
146
b6a7d936 147$msgcode['updPortForwarding']['OK'] = 4;
a5c589d2 148$msgcode['updPortForwarding']['ERR'] = 109;
e673ee24
DO
149function updPortForwarding ()
150{
0cc24e9a
DY
151 assertUIntArg ('object_id');
152 assertIPv4Arg ('localip');
153 assertIPv4Arg ('remoteip');
154 assertUIntArg ('localport');
155 assertUIntArg ('remoteport');
156 assertStringArg ('proto');
157 assertStringArg ('description');
e673ee24 158
a5c589d2 159 $result = updatePortForwarding
103b1e1e
DO
160 (
161 $_REQUEST['object_id'],
162 $_REQUEST['localip'],
163 $_REQUEST['localport'],
164 $_REQUEST['remoteip'],
165 $_REQUEST['remoteport'],
166 $_REQUEST['proto'],
167 $_REQUEST['description']
168 );
a5c589d2 169 buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
e673ee24
DO
170}
171
b6a7d936
DO
172$msgcode['addPortForObject']['OK'] = 5;
173$msgcode['addPortForObject']['ERR1'] = 101;
174$msgcode['addPortForObject']['ERR2'] = 100;
e673ee24
DO
175function addPortForObject ()
176{
0cc24e9a 177 assertStringArg ('port_name', TRUE);
9b6e7bd1 178 genericAssertion ('port_l2address', 'l2address0');
59a83bd8 179 if (!strlen ($_REQUEST['port_name']))
135080d8 180 return buildRedirectURL (__FUNCTION__, 'ERR1');
6405ecd3
DO
181 $error = commitAddPort
182 (
183 $_REQUEST['object_id'],
184 trim ($_REQUEST['port_name']),
185 $_REQUEST['port_type_id'],
186 trim ($_REQUEST['port_label']),
187 trim ($_REQUEST['port_l2address'])
188 );
103b1e1e 189 if ($error != '')
135080d8 190 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 191 else
135080d8 192 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
193}
194
b6a7d936
DO
195$msgcode['editPortForObject']['OK'] = 6;
196$msgcode['editPortForObject']['ERR1'] = 101;
197$msgcode['editPortForObject']['ERR2'] = 100;
e673ee24
DO
198function editPortForObject ()
199{
0cc24e9a
DY
200 assertUIntArg ('port_id');
201 assertUIntArg ('port_type_id');
e8acfc2d 202 assertStringArg ('reservation_comment', TRUE);
9b6e7bd1 203 genericAssertion ('l2address', 'l2address0');
d0a69ce8 204 // tolerate empty value now to produce custom informative message later
0cc24e9a 205 assertStringArg ('name', TRUE);
59a83bd8 206 if (!strlen ($_REQUEST['name']))
135080d8 207 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e 208
e8acfc2d 209 $error = commitUpdatePort ($_REQUEST['object_id'], $_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['port_type_id'], $_REQUEST['label'], $_REQUEST['l2address'], $_REQUEST['reservation_comment']);
103b1e1e 210 if ($error != '')
135080d8 211 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 212 else
135080d8 213 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e673ee24
DO
214}
215
b6a7d936
DO
216$msgcode['linkPortForObject']['OK'] = 8;
217$msgcode['linkPortForObject']['ERR'] = 100;
e673ee24
DO
218function linkPortForObject ()
219{
0cc24e9a
DY
220 assertUIntArg ('port_id');
221 assertUIntArg ('remote_port_id');
0c7c9f8b 222 assertStringArg ('cable', TRUE);
e673ee24 223
cd3775e9 224 // FIXME: ensure, that at least one of these ports belongs to the current object
0c7c9f8b 225 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id'], $_REQUEST['cable']);
e673ee24 226 if ($error != '')
135080d8 227 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
d0dadd80
DO
228 global $sic;
229 $local_port_info = getPortInfo ($sic['port_id']);
230 $remote_port_info = getPortInfo ($sic['remote_port_id']);
cd3775e9
DO
231 $remote_object = spotEntity ('object', $remote_port_info['object_id']);
232 return buildRedirectURL
233 (
234 __FUNCTION__,
235 'OK',
236 array
237 (
238 $local_port_info['name'],
239 $remote_port_info['name'],
240 $remote_object['dname'],
241 )
242 );
e673ee24
DO
243}
244
b6a7d936
DO
245$msgcode['addMultiPorts']['OK'] = 10;
246$msgcode['addMultiPorts']['ERR'] = 123;
e673ee24
DO
247function addMultiPorts ()
248{
0cc24e9a
DY
249 assertStringArg ('format');
250 assertStringArg ('input');
ff0eaf57 251 assertStringArg ('port_type');
e673ee24
DO
252 $format = $_REQUEST['format'];
253 $port_type = $_REQUEST['port_type'];
254 $object_id = $_REQUEST['object_id'];
255 // Input lines are escaped, so we have to explode and to chop by 2-char
256 // \n and \r respectively.
7f42d792 257 $lines1 = explode ("\n", $_REQUEST['input']);
e673ee24
DO
258 foreach ($lines1 as $line)
259 {
260 $parts = explode ('\r', $line);
261 reset ($parts);
59a83bd8 262 if (!strlen ($parts[0]))
e673ee24
DO
263 continue;
264 else
265 $lines2[] = rtrim ($parts[0]);
266 }
267 $ports = array();
268 foreach ($lines2 as $line)
269 {
270 switch ($format)
271 {
272 case 'fisxii':
84986395 273 $words = explode (' ', preg_replace ('/[[:space:]]+/', ' ', $line));
e673ee24
DO
274 list ($slot, $port) = explode ('/', $words[0]);
275 $ports[] = array
276 (
277 'name' => "e ${slot}/${port}",
278 'l2address' => $words[8],
279 'label' => "slot ${slot} port ${port}"
280 );
281 break;
282 case 'c3600asy':
84986395 283 $words = explode (' ', preg_replace ('/[[:space:]]+/', ' ', trim (substr ($line, 3))));
e673ee24
DO
284/*
285How Async Lines are Numbered in Cisco 3600 Series Routers
286http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
287
288Understanding 16- and 32-Port Async Network Modules
289http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
290*/
291 $async = $words[0];
292 $slot = floor (($async - 1) / 32);
293 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
294 $cable = $async - $slot * 32 - $octalgroup * 8;
295 $og_label[0] = 'async 0-7';
296 $og_label[1] = 'async 8-15';
297 $og_label[2] = 'async 16-23';
298 $og_label[3] = 'async 24-31';
299 $ports[] = array
300 (
301 'name' => "async ${async}",
302 'l2address' => '',
303 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
304 );
305 break;
306 case 'fiwg':
84986395 307 $words = explode (' ', preg_replace ('/[[:space:]]+/', ' ', $line));
e673ee24
DO
308 $ifnumber = $words[0] * 1;
309 $ports[] = array
310 (
311 'name' => "e ${ifnumber}",
312 'l2address' => "${words[8]}",
313 'label' => "${ifnumber}"
314 );
315 break;
351d4dbf 316 case 'ssv1':
5b585342 317 $words = explode (' ', $line);
59a83bd8 318 if (!strlen ($words[0]) or !strlen ($words[1]))
351d4dbf
DO
319 continue;
320 $ports[] = array
321 (
322 'name' => $words[0],
323 'l2address' => $words[1],
324 'label' => ''
325 );
326 break;
e673ee24 327 default:
135080d8 328 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
329 break;
330 }
331 }
332 // Create ports, if they don't exist.
333 $added_count = $updated_count = $error_count = 0;
334 foreach ($ports as $port)
335 {
e9d357e1
DO
336 $port_ids = getPortIDs ($object_id, $port['name']);
337 if (!count ($port_ids))
e673ee24
DO
338 {
339 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
340 if ($result == '')
341 $added_count++;
342 else
343 $error_count++;
344 }
e9d357e1 345 elseif (count ($port_ids) == 1) // update only single-socket ports
e673ee24 346 {
e9d357e1 347 $result = commitUpdatePort ($object_id, $port_ids[0], $port['name'], $port_type, $port['label'], $port['l2address']);
e673ee24
DO
348 if ($result == '')
349 $updated_count++;
350 else
351 $error_count++;
352 }
353 }
135080d8 354 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $updated_count, $error_count));
e673ee24
DO
355}
356
a1fc539a 357$msgcode['addBulkPorts']['OK'] = 82;
a1fc539a
RF
358function addBulkPorts ()
359{
647635ad
DO
360 assertStringArg ('port_type_id');
361 assertStringArg ('port_name');
362 assertStringArg ('port_label', TRUE);
329aa3e5 363 assertUIntArg ('port_numbering_start', TRUE);
647635ad 364 assertUIntArg ('port_numbering_count');
a1fc539a
RF
365
366 $object_id = $_REQUEST['object_id'];
367 $port_name = $_REQUEST['port_name'];
368 $port_type_id = $_REQUEST['port_type_id'];
369 $port_label = $_REQUEST['port_label'];
370 $port_numbering_start = $_REQUEST['port_numbering_start'];
371 $port_numbering_count = $_REQUEST['port_numbering_count'];
372
a1fc539a
RF
373 $added_count = $error_count = 0;
374 if(strrpos($port_name, "%u") === false )
375 $port_name .= '%u';
2b5a8c1b 376 for ($i=0,$c=$port_numbering_start; $i<$port_numbering_count; $i++,$c++)
a1fc539a 377 {
2b5a8c1b 378 $result = commitAddPort ($object_id, @sprintf($port_name,$c), $port_type_id, @sprintf($port_label,$c), '');
a1fc539a
RF
379 if ($result == '')
380 $added_count++;
381 else
382 $error_count++;
383 }
384 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $error_count));
385}
386
b6a7d936 387$msgcode['updIPv4Allocation']['OK'] = 12;
32832c0e 388$msgcode['updIPv4Allocation']['ERR'] = 109;
b4c1ef87 389function updIPv4Allocation ()
e673ee24 390{
0cc24e9a
DY
391 assertIPv4Arg ('ip');
392 assertUIntArg ('object_id');
393 assertStringArg ('bond_name', TRUE);
394 assertStringArg ('bond_type');
e673ee24 395
32832c0e
DO
396 $result = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
397 return buildRedirectURL (__FUNCTION__, $result === FALSE ? 'ERR' : 'OK');
e673ee24
DO
398}
399
21ee3351
AA
400$msgcode['updIPv6Allocation']['OK'] = 12;
401$msgcode['updIv6PAllocation']['ERR'] = 109;
402function updIPv6Allocation ()
403{
404 $ipv6 = assertIPv6Arg ('ip');
405 assertUIntArg ('object_id');
406 assertStringArg ('bond_name', TRUE);
407 assertStringArg ('bond_type');
408
409 $result = updateIPv6Bond ($ipv6, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
410 return buildRedirectURL (__FUNCTION__, $result === FALSE ? 'ERR' : 'OK');
411}
412
b6a7d936 413$msgcode['delIPv4Allocation']['OK'] = 14;
32832c0e 414$msgcode['delIPv4Allocation']['ERR'] = 111;
b4c1ef87 415function delIPv4Allocation ()
e673ee24 416{
0cc24e9a
DY
417 assertIPv4Arg ('ip');
418 assertUIntArg ('object_id');
e673ee24 419
32832c0e
DO
420 $result = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
421 return buildRedirectURL (__FUNCTION__, $result === FALSE ? 'ERR' : 'OK');
e673ee24
DO
422}
423
21ee3351
AA
424$msgcode['delIPv6Allocation']['OK'] = 14;
425$msgcode['delIPv6Allocation']['ERR'] = 111;
426function delIPv6Allocation ()
427{
428 assertUIntArg ('object_id');
429 $ipv6 = assertIPv6Arg ('ip');
430 $result = unbindIPv6FromObject ($ipv6, $_REQUEST['object_id']);
431 return buildRedirectURL (__FUNCTION__, $result === FALSE ? 'ERR' : 'OK');
432}
433
b6a7d936
DO
434$msgcode['addIPv4Allocation']['OK'] = 13;
435$msgcode['addIPv4Allocation']['ERR1'] = 170;
436$msgcode['addIPv4Allocation']['ERR2'] = 100;
b4c1ef87 437function addIPv4Allocation ()
e673ee24 438{
0cc24e9a
DY
439 assertIPv4Arg ('ip');
440 assertUIntArg ('object_id');
441 assertStringArg ('bond_name', TRUE);
442 assertStringArg ('bond_type');
da958e52 443
b4c1ef87 444 // Strip masklen.
84986395 445 $ip = preg_replace ('@/[[:digit:]]+$@', '', $_REQUEST['ip']);
2be3fd23 446 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
135080d8 447 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
5222f192 448
fc2e1602 449 if (FALSE === bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']))
135080d8 450 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
53ef3908 451 $address = getIPv4Address ($ip);
59a83bd8 452 if ($address['reserved'] == 'yes' or strlen ($address['name']))
b4c1ef87
DO
453 {
454 $release = getConfigVar ('IPV4_AUTO_RELEASE');
455 if ($release >= 1)
456 $address['reserved'] = 'no';
457 if ($release >= 2)
458 $address['name'] = '';
459 updateAddress ($ip, $address['name'], $address['reserved']);
460 }
135080d8 461 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
462}
463
21ee3351
AA
464$msgcode['addIPv6Allocation']['OK'] = 13;
465$msgcode['addIPv6Allocation']['ERR1'] = 170;
466$msgcode['addIPv6Allocation']['ERR2'] = 100;
467function addIPv6Allocation ()
468{
469 assertUIntArg ('object_id');
470 assertStringArg ('bond_name', TRUE);
471 assertStringArg ('bond_type');
472
473 // Strip masklen.
474 $ipv6 = new IPv6Address;
475 if (! $ipv6->parse (preg_replace ('@/\d+$@', '', $_REQUEST['ip'])))
476 throw new InvalidRequestArgException('ip', $_REQUEST['ip'], 'parameter is not a valid ipv6 address');
477
478 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv6AddressNetworkId ($ipv6))
479 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
480
481 if (FALSE === bindIPv6ToObject ($ipv6, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']))
482 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
483 $address = getIPv6Address ($ipv6);
484 if ($address['reserved'] == 'yes' or strlen ($address['name']))
485 {
486 $release = getConfigVar ('IPV4_AUTO_RELEASE');
487 if ($release >= 1)
488 $address['reserved'] = 'no';
489 if ($release >= 2)
490 $address['name'] = '';
491 updateAddress ($ipv6, $address['name'], $address['reserved']);
492 }
493 return buildRedirectURL (__FUNCTION__, 'OK');
494}
495
b6a7d936
DO
496$msgcode['addIPv4Prefix']['OK'] = 23;
497$msgcode['addIPv4Prefix']['ERR'] = 100;
498$msgcode['addIPv4Prefix']['ERR1'] = 173;
499$msgcode['addIPv4Prefix']['ERR2'] = 174;
500$msgcode['addIPv4Prefix']['ERR3'] = 175;
501$msgcode['addIPv4Prefix']['ERR4'] = 176;
42023f03 502function addIPv4Prefix ()
e673ee24 503{
0cc24e9a
DY
504 assertStringArg ('range');
505 assertStringArg ('name', TRUE);
e673ee24 506
fa05e3de
DO
507 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
508 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
357eb2ea
DO
509 global $sic;
510 $error = createIPv4Prefix ($_REQUEST['range'], $sic['name'], $is_bcast == 'on', $taglist);
e673ee24 511 if ($error != '')
135080d8 512 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 513 else
135080d8 514 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
515}
516
21ee3351
AA
517$msgcode['addIPv6Prefix']['OK'] = 86;
518$msgcode['addIPv6Prefix']['ERR'] = 100;
519$msgcode['addIPv6Prefix']['ERR1'] = 173;
520$msgcode['addIPv6Prefix']['ERR2'] = 174;
521$msgcode['addIPv6Prefix']['ERR3'] = 175;
522$msgcode['addIPv6Prefix']['ERR4'] = 176;
523function addIPv6Prefix ()
524{
525 assertStringArg ('range');
526 assertStringArg ('name', TRUE);
527
528 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
529 global $sic;
530 $error = createIPv6Prefix ($_REQUEST['range'], $sic['name'], $taglist);
531 if ($error != '')
532 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
533 else
534 return buildRedirectURL (__FUNCTION__, 'OK');
535}
536
b6a7d936
DO
537$msgcode['delIPv4Prefix']['OK'] = 24;
538$msgcode['delIPv4Prefix']['ERR'] = 100;
42023f03 539function delIPv4Prefix ()
e673ee24 540{
0cc24e9a 541 assertUIntArg ('id');
42023f03 542 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 543 if ($error != '')
135080d8 544 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 545 else
135080d8 546 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
547}
548
21ee3351
AA
549$msgcode['delIPv6Prefix']['OK'] = 85;
550$msgcode['delIPv6Prefix']['ERR'] = 100;
551function delIPv6Prefix ()
552{
553 assertUIntArg ('id');
554 $error = destroyIPv6Prefix ($_REQUEST['id']);
555 if ($error != '')
556 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
557 else
558 return buildRedirectURL (__FUNCTION__, 'OK');
559}
560
b6a7d936 561$msgcode['updIPv4Prefix']['OK'] = 25;
6cfedb71 562$msgcode['updIPv4Prefix']['ERR'] = 109;
04d619d0 563function updIPv4Prefix ()
e673ee24 564{
0cc24e9a
DY
565 assertUIntArg ('id');
566 assertStringArg ('name', TRUE);
567 assertStringArg ('comment', TRUE);
99ab184f 568 global $sic;
6cfedb71
DO
569 $result = updateIPv4Network_real ($sic['id'], $sic['name'], $sic['comment']);
570 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
e673ee24
DO
571}
572
21ee3351
AA
573$msgcode['updIPv6Prefix']['OK'] = 86;
574$msgcode['updIPv6Prefix']['ERR'] = 109;
575function updIPv6Prefix ()
576{
577 assertUIntArg ('id');
578 assertStringArg ('name', TRUE);
579 assertStringArg ('comment', TRUE);
580 global $sic;
581 $result = updateIPv6Network_real ($sic['id'], $sic['name'], $sic['comment']);
582 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
583}
584
b6a7d936
DO
585$msgcode['editAddress']['OK'] = 27;
586$msgcode['editAddress']['ERR'] = 100;
e673ee24
DO
587function editAddress ()
588{
0cc24e9a 589 assertStringArg ('name', TRUE);
e673ee24 590
e673ee24
DO
591 if (isset ($_REQUEST['reserved']))
592 $reserved = $_REQUEST['reserved'];
593 else
594 $reserved = 'off';
da958e52 595 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 596 if ($error != '')
135080d8 597 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 598 else
135080d8 599 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
600}
601
21ee3351
AA
602$msgcode['editv6Address']['OK'] = 27;
603$msgcode['editv6Address']['ERR'] = 100;
604function editv6Address ()
605{
606 $ipv6 = assertIPArg ('ip');
607 assertStringArg ('name', TRUE);
608
609 if (isset ($_REQUEST['reserved']))
610 $reserved = $_REQUEST['reserved'];
611 else
612 $reserved = 'off';
613 $error = updateAddress ($ipv6, $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
614 if ($error != '')
615 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
616 else
617 return buildRedirectURL (__FUNCTION__, 'OK');
618}
619
b6a7d936
DO
620$msgcode['createUser']['OK'] = 40;
621$msgcode['createUser']['ERR'] = 102;
cced6b7d 622function createUser ()
e673ee24 623{
0cc24e9a
DY
624 assertStringArg ('username');
625 assertStringArg ('realname', TRUE);
626 assertStringArg ('password');
e673ee24 627 $username = $_REQUEST['username'];
93bdb7ba 628 $password = sha1 ($_REQUEST['password']);
e673ee24 629 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
f857f71f 630 if ($result != TRUE)
135080d8 631 return buildRedirectURL (__FUNCTION__, 'ERR', array ($username));
f857f71f
DO
632 if (isset ($_REQUEST['taglist']))
633 produceTagsForLastRecord ('user', $_REQUEST['taglist']);
634 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24
DO
635}
636
b6a7d936
DO
637$msgcode['updateUser']['OK'] = 39;
638$msgcode['updateUser']['ERR1'] = 103;
639$msgcode['updateUser']['ERR1'] = 104;
cced6b7d 640function updateUser ()
e673ee24 641{
0cc24e9a
DY
642 assertStringArg ('username');
643 assertStringArg ('realname', TRUE);
644 assertStringArg ('password');
e673ee24
DO
645 $username = $_REQUEST['username'];
646 $new_password = $_REQUEST['password'];
0b2c74cb 647 if (NULL == ($userinfo = spotEntity ('user', $_REQUEST['user_id'])))
135080d8 648 return buildRedirectURL (__FUNCTION__, 'ERR1');
e673ee24 649 // Update user password only if provided password is not the same as current password hash.
b82cce3f 650 if ($new_password != $userinfo['user_password_hash'])
93bdb7ba 651 $new_password = sha1 ($new_password);
103b1e1e 652 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
32832c0e 653 if ($result !== FALSE)
135080d8 654 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 655 else
135080d8 656 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($username));
e673ee24
DO
657}
658
b6a7d936
DO
659$msgcode['updateDictionary']['OK'] = 51;
660$msgcode['updateDictionary']['ERR'] = 109;
e673ee24
DO
661function updateDictionary ()
662{
0cc24e9a
DY
663 assertUIntArg ('dict_key');
664 assertStringArg ('dict_value');
32832c0e 665 if (FALSE !== commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']))
135080d8 666 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 667 else
135080d8 668 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
669}
670
b6a7d936
DO
671$msgcode['updateChapter']['OK'] = 54;
672$msgcode['updateChapter']['ERR'] = 113;
e673ee24
DO
673function updateChapter ()
674{
0cc24e9a
DY
675 assertUIntArg ('chapter_no');
676 assertStringArg ('chapter_name');
32832c0e 677 if (FALSE !== commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']))
135080d8 678 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 679 else
135080d8 680 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
681}
682
b6a7d936
DO
683$msgcode['delChapter']['OK'] = 53;
684$msgcode['delChapter']['ERR'] = 114;
e673ee24
DO
685function delChapter ()
686{
0cc24e9a 687 assertUIntArg ('chapter_no');
e673ee24 688 if (commitDeleteChapter ($_REQUEST['chapter_no']))
135080d8 689 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 690 else
135080d8 691 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
692}
693
b6a7d936
DO
694$msgcode['changeAttribute']['OK'] = 46;
695$msgcode['changeAttribute']['ERR'] = 115;
e673ee24
DO
696function changeAttribute ()
697{
0cc24e9a
DY
698 assertUIntArg ('attr_id');
699 assertStringArg ('attr_name');
32832c0e 700 if (FALSE !== commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
135080d8 701 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 702 else
135080d8 703 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
704}
705
b6a7d936 706$msgcode['supplementAttrMap']['OK'] = 48;
7028a42c
DO
707$msgcode['supplementAttrMap']['ERR1'] = 154;
708$msgcode['supplementAttrMap']['ERR2'] = 118;
e673ee24
DO
709function supplementAttrMap ()
710{
0cc24e9a
DY
711 assertUIntArg ('attr_id');
712 assertUIntArg ('objtype_id');
7028a42c
DO
713 $attrMap = getAttrMap();
714 if ($attrMap[$_REQUEST['attr_id']]['type'] != 'dict')
d5add09a 715 $chapter_id = NULL;
7028a42c
DO
716 else
717 {
d5add09a
DO
718 try
719 {
720 assertUIntArg ('chapter_no');
721 }
722 catch (InvalidRequestArgException $e)
723 {
7028a42c 724 return buildRedirectURL (__FUNCTION__, 'ERR1', array ('chapter not selected'));
d5add09a
DO
725 }
726 $chapter_id = $_REQUEST['chapter_no'];
7028a42c 727 }
654e4636 728 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $chapter_id) !== FALSE)
135080d8 729 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 730 else
7028a42c 731 return buildRedirectURL (__FUNCTION__, 'ERR2');
e673ee24
DO
732}
733
b6a7d936
DO
734$msgcode['clearSticker']['OK'] = 15;
735$msgcode['clearSticker']['ERR'] = 120;
22bb04da 736function clearSticker ()
e673ee24 737{
0cc24e9a 738 assertUIntArg ('attr_id');
654e4636 739 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) !== FALSE)
135080d8 740 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 741 else
135080d8 742 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
743}
744
e97e8866 745$msgcode['updateObjectAllocation']['OK'] = 63;
4fbb5a00
DY
746function updateObjectAllocation ()
747{
e97e8866
DO
748 if (!isset ($_REQUEST['got_atoms']))
749 {
750 unset($_GET['page']);
751 unset($_GET['tab']);
752 unset($_GET['op']);
753 unset($_POST['page']);
754 unset($_POST['tab']);
755 unset($_POST['op']);
756 return buildWideRedirectURL (array(), NULL, NULL, array_merge ($_GET, $_POST));
757 }
758 $object_id = $_REQUEST['object_id'];
759 $workingRacksData = array();
760 foreach ($_REQUEST['rackmulti'] as $cand_id)
4fbb5a00 761 {
e97e8866 762 if (!isset ($workingRacksData[$cand_id]))
4fbb5a00 763 {
e97e8866 764 $rackData = spotEntity ('rack', $cand_id);
e97e8866
DO
765 amplifyCell ($rackData);
766 $workingRacksData[$cand_id] = $rackData;
4fbb5a00 767 }
e97e8866 768 }
2135fd83 769
e97e8866
DO
770 foreach ($workingRacksData as &$rd)
771 applyObjectMountMask ($rd, $object_id);
4fbb5a00 772
e97e8866
DO
773 $oldMolecule = getMoleculeForObject ($object_id);
774 $changecnt = 0;
775 $log = array();
776 foreach ($workingRacksData as $rack_id => $rackData)
4fbb5a00 777 {
e97e8866
DO
778 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
779 $log[] = $logrecord;
780 if ($logrecord['code'] == 300)
781 continue;
782 $changecnt++;
783 // Reload our working copy after form processing.
784 $rackData = spotEntity ('rack', $cand_id);
785 amplifyCell ($rackData);
786 applyObjectMountMask ($rackData, $object_id);
787 $workingRacksData[$rack_id] = $rackData;
4fbb5a00 788 }
e97e8866
DO
789 if (!$changecnt)
790 return buildRedirectURL (__FUNCTION__, 'OK', $changecnt);
791 // Log a record.
792 $newMolecule = getMoleculeForObject ($object_id);
793 $oc = count ($oldMolecule);
794 $nc = count ($newMolecule);
795 $omid = $oc ? createMolecule ($oldMolecule) : 'NULL';
796 $nmid = $nc ? createMolecule ($newMolecule) : 'NULL';
797 global $remote_username;
798 $comment = empty ($_REQUEST['comment']) ? 'NULL' : "'${_REQUEST['comment']}'";
799 $query =
800 "insert into MountOperation(object_id, old_molecule_id, new_molecule_id, user_name, comment) " .
801 "values (${object_id}, ${omid}, ${nmid}, '${remote_username}', ${comment})";
802 global $dbxlink;
803 $result = $dbxlink->query ($query);
804 if ($result == NULL)
805 $log[] = array ('code' => 500, 'message' => 'SQL query failed during history logging.');
806 else
807 $log[] = array ('code' => 200, 'message' => 'History logged.');
808 return buildWideRedirectURL ($log);
4fbb5a00
DY
809}
810
b6a7d936
DO
811$msgcode['updateObject']['OK'] = 16;
812$msgcode['updateObject']['ERR'] = 121;
22bb04da
DO
813function updateObject ()
814{
0cc24e9a 815 assertUIntArg ('num_attrs', TRUE);
0cc24e9a
DY
816 assertStringArg ('object_name', TRUE);
817 assertStringArg ('object_label', TRUE);
818 assertStringArg ('object_barcode', TRUE);
819 assertStringArg ('object_asset_no', TRUE);
22bb04da
DO
820 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
821 $has_problems = 'yes';
822 else
823 $has_problems = 'no';
824
825 if (commitUpdateObject (
826 $_REQUEST['object_id'],
827 $_REQUEST['object_name'],
828 $_REQUEST['object_label'],
829 $_REQUEST['object_barcode'],
22bb04da
DO
830 $has_problems,
831 $_REQUEST['object_asset_no'],
832 $_REQUEST['object_comment']
833 ) !== TRUE)
135080d8 834 return buildRedirectURL (__FUNCTION__, 'ERR');
22bb04da 835
cdd3efe9 836 // Update optional attributes
f59e8cf5 837 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a 838 $result = array();
0f63538a
DO
839 $num_attrs = isset ($_REQUEST['num_attrs']) ? $_REQUEST['num_attrs'] : 0;
840 for ($i = 0; $i < $num_attrs; $i++)
0dfb8a2a 841 {
0cc24e9a 842 assertUIntArg ("${i}_attr_id");
0dfb8a2a
DO
843 $attr_id = $_REQUEST["${i}_attr_id"];
844
01d82bd2
RF
845 // Field is empty, delete attribute and move on. OR if the field type is a dictionary and it is the --NOT SET-- value of 0
846 if (!strlen ($_REQUEST["${i}_value"]) || ($oldvalues[$attr_id]['type']=='dict' && $_REQUEST["${i}_value"] == 0))
0dfb8a2a 847 {
103b1e1e 848 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
849 continue;
850 }
851
852 // The value could be uint/float, but we don't know ATM. Let SQL
853 // server check this and complain.
0cc24e9a 854 assertStringArg ("${i}_value");
0dfb8a2a
DO
855 $value = $_REQUEST["${i}_value"];
856 switch ($oldvalues[$attr_id]['type'])
857 {
858 case 'uint':
859 case 'float':
860 case 'string':
861 $oldvalue = $oldvalues[$attr_id]['value'];
862 break;
863 case 'dict':
864 $oldvalue = $oldvalues[$attr_id]['key'];
865 break;
866 default:
0dfb8a2a 867 }
59a83bd8 868 if ($value === $oldvalue) // ('' == 0), but ('' !== 0)
0dfb8a2a
DO
869 continue;
870
871 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 872 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a 873 }
103b1e1e 874 if (in_array (FALSE, $result))
135080d8 875 return buildRedirectURL (__FUNCTION__, 'ERR');
0dfb8a2a 876
cdd3efe9
AD
877 // Invalidate thumb cache of all racks objects could occupy.
878 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
879 resetThumbCache ($rack_id);
880
135080d8 881 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
882}
883
42d73cbf
DY
884
885function addMultipleObjects()
886{
7abce0b7 887 $log = emptyLog();
42d73cbf 888 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
889 $max = getConfigVar ('MASSCOUNT');
890 for ($i = 0; $i < $max; $i++)
891 {
892 if (!isset ($_REQUEST["${i}_object_type_id"]))
893 {
57777e4a 894 $log = mergeLogs ($log, oneLiner (184, array ($i + 1)));
42d73cbf
DY
895 break;
896 }
0cc24e9a
DY
897 assertUIntArg ("${i}_object_type_id", TRUE);
898 assertStringArg ("${i}_object_name", TRUE);
899 assertStringArg ("${i}_object_label", TRUE);
900 assertStringArg ("${i}_object_asset_no", TRUE);
901 assertStringArg ("${i}_object_barcode", TRUE);
7abce0b7 902 $name = $_REQUEST["${i}_object_name"];
42d73cbf
DY
903
904 // It's better to skip silently, than to print a notice.
7abce0b7 905 if ($_REQUEST["${i}_object_type_id"] == 0)
42d73cbf 906 continue;
fee7977b 907 if (($object_id = commitAddObject
7abce0b7
DO
908 (
909 $name,
910 $_REQUEST["${i}_object_label"],
911 $_REQUEST["${i}_object_barcode"],
912 $_REQUEST["${i}_object_type_id"],
913 $_REQUEST["${i}_object_asset_no"],
914 $taglist
fee7977b
DO
915 )) !== FALSE){
916 $info = spotEntity ('object', $object_id);
917 // FIXME: employ amplifyCell() instead of calling loader functions directly
918 amplifyCell ($info);
919 $log = mergeLogs ($log, oneLiner (80, array ('<a href="' . makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) . '">' . $info['dname'] . '</a>')));
920 }else{
57777e4a 921 $log = mergeLogs ($log, oneLiner (185, array ($name)));
fee7977b 922 }
42d73cbf 923 }
7abce0b7 924 return buildWideRedirectURL ($log);
42d73cbf
DY
925}
926
927function addLotOfObjects()
928{
7abce0b7 929 $log = emptyLog();
42d73cbf 930 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
0cc24e9a
DY
931 assertUIntArg ('global_type_id', TRUE);
932 assertStringArg ('namelist', TRUE);
42d73cbf 933 $global_type_id = $_REQUEST['global_type_id'];
59a83bd8 934 if ($global_type_id == 0 or !strlen ($_REQUEST['namelist']))
57777e4a 935 $log = mergeLogs ($log, oneLiner (186));
42d73cbf
DY
936 else
937 {
938 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
7f42d792 939 $names1 = explode ("\n", $_REQUEST['namelist']);
42d73cbf
DY
940 $names2 = array();
941 foreach ($names1 as $line)
942 {
943 $parts = explode ('\r', $line);
944 reset ($parts);
59a83bd8 945 if (!strlen ($parts[0]))
42d73cbf
DY
946 continue;
947 else
948 $names2[] = rtrim ($parts[0]);
949 }
57777e4a 950 foreach ($names2 as $name)
9d535a0c
DO
951 if (($object_id = commitAddObject ($name, '', '', $global_type_id, '', $taglist)) !== FALSE)
952 {
953 $info = spotEntity ('object', $object_id);
954 amplifyCell ($info);
955 $log = mergeLogs ($log, oneLiner (80, array ('<a href="' . makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) . '">' . $info['dname'] . '</a>')));
956 }
42d73cbf 957 else
57777e4a 958 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 959 }
7abce0b7 960 return buildWideRedirectURL ($log);
42d73cbf
DY
961}
962
b6a7d936 963$msgcode['deleteObject']['OK'] = 76;
52b34485
AD
964function deleteObject ()
965{
0cc24e9a 966 assertUIntArg ('object_id');
4bb5b87f 967 $oinfo = spotEntity ('object', $_REQUEST['object_id']);
c78a40ec 968
f0e2c99f 969 $racklist = getResidentRacksData ($_REQUEST['object_id'], FALSE);
4bb5b87f 970 commitDeleteObject ($_REQUEST['object_id']);
f0e2c99f
DO
971 foreach ($racklist as $rack_id)
972 resetThumbCache ($rack_id);
abd1e9ac
DO
973 return buildRedirectURL (__FUNCTION__, 'OK', array ($oinfo['dname']));
974}
52b34485 975
abd1e9ac
DO
976$msgcode['resetObject']['OK'] = 83;
977$msgcode['resetObject']['ERR'] = 100;
978function resetObject ()
979{
abd1e9ac
DO
980 $oinfo = spotEntity ('object', $_REQUEST['object_id']);
981
982 $racklist = getResidentRacksData ($_REQUEST['object_id'], FALSE);
983 commitResetObject ($_REQUEST['object_id']);
984 foreach ($racklist as $rack_id)
985 resetThumbCache ($rack_id);
c78a40ec 986 return buildRedirectURL (__FUNCTION__, 'OK', array ($oinfo['dname']));
52b34485
AD
987}
988
b6a7d936
DO
989$msgcode['useupPort']['OK'] = 11;
990$msgcode['useupPort']['ERR'] = 124;
e673ee24
DO
991function useupPort ()
992{
0cc24e9a 993 assertUIntArg ('port_id');
32832c0e 994 if (FALSE !== commitUseupPort ($_REQUEST['port_id']))
135080d8 995 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 996 else
135080d8 997 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
998}
999
b6a7d936 1000$msgcode['updateUI']['OK'] = 56;
4fe32e78
DY
1001function updateUI ()
1002{
0cc24e9a 1003 assertUIntArg ('num_vars');
4fe32e78 1004
103b1e1e 1005 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 1006 {
0cc24e9a
DY
1007 assertStringArg ("${i}_varname");
1008 assertStringArg ("${i}_varvalue", TRUE);
4fe32e78 1009 $varname = $_REQUEST["${i}_varname"];
4fe32e78 1010 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 1011
c461c579 1012 // If form value = value in DB, don't bother updating DB
ed941e67 1013 if (!isConfigVarChanged($varname, $varvalue))
4fe32e78 1014 continue;
3a089a44
DO
1015 // any exceptions will be handled by process.php
1016 setConfigVar ($varname, $varvalue, TRUE);
3540d15c
DY
1017 }
1018 return buildRedirectURL (__FUNCTION__, 'OK');
1019}
1020
1021$msgcode['saveMyPreferences']['OK'] = 56;
3540d15c
DY
1022function saveMyPreferences ()
1023{
1024 assertUIntArg ('num_vars');
1025
1026 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
1027 {
1028 assertStringArg ("${i}_varname");
1029 assertStringArg ("${i}_varvalue", TRUE);
1030 $varname = $_REQUEST["${i}_varname"];
1031 $varvalue = $_REQUEST["${i}_varvalue"];
1032
1033 // If form value = value in DB, don't bother updating DB
ed941e67 1034 if (!isConfigVarChanged($varname, $varvalue))
3540d15c 1035 continue;
3a089a44 1036 setUserConfigVar ($varname, $varvalue);
3540d15c
DY
1037 }
1038 return buildRedirectURL (__FUNCTION__, 'OK');
1039}
1040
1041$msgcode['resetMyPreference']['OK'] = 56;
3540d15c
DY
1042function resetMyPreference ()
1043{
1044 assertStringArg ("varname");
3a089a44 1045 resetUserConfigVar ($_REQUEST["varname"]);
135080d8 1046 return buildRedirectURL (__FUNCTION__, 'OK');
4fe32e78
DY
1047}
1048
b6a7d936 1049$msgcode['resetUIConfig']['OK'] = 57;
b07f617c
DO
1050function resetUIConfig()
1051{
b07f617c
DO
1052 setConfigVar ('MASSCOUNT','15');
1053 setConfigVar ('MAXSELSIZE','30');
b07f617c
DO
1054 setConfigVar ('ROW_SCALE','2');
1055 setConfigVar ('PORTS_PER_ROW','12');
1056 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
1057 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
b5ce46aa
DO
1058 setConfigVar ('DEFAULT_SLB_VS_PORT','');
1059 setConfigVar ('DEFAULT_SLB_RS_PORT','');
72281138 1060 setConfigVar ('DETECT_URLS','no');
4b8d413e 1061 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 1062 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 1063 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
1064 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
1065 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
1066 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 1067 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 1068 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df 1069 setConfigVar ('SHOW_LAST_TAB', 'no');
f0ed1181 1070 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 1071 setConfigVar ('TREE_THRESHOLD', '25');
2be3fd23 1072 setConfigVar ('IPV4_JAYWALK', 'no');
9318d2ef 1073 setConfigVar ('ADDNEW_AT_TOP', 'yes');
64347dcf 1074 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
37e59768
DO
1075 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
1076 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
1077 setConfigVar ('PREVIEW_TEXT_COLS', '80');
1078 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
f3d274bf 1079 setConfigVar ('VENDOR_SIEVE', '');
35bf153d 1080 setConfigVar ('IPV4LB_LISTSRC', '{$typeid_4}');
8fee82b4
DO
1081 setConfigVar ('IPV4OBJ_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_12} or {$typeid_445} or {$typeid_447} or {$typeid_798}');
1082 setConfigVar ('IPV4NAT_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_798}');
35bf153d
DO
1083 setConfigVar ('ASSETWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
1084 setConfigVar ('NAMEWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
f5883ec1 1085 setConfigVar ('RACKS_PER_ROW','12');
590e1281 1086 setConfigVar ('FILTER_PREDICATE_SIEVE','');
23cdc7e9
DO
1087 setConfigVar ('FILTER_DEFAULT_ANDOR','or');
1088 setConfigVar ('FILTER_SUGGEST_ANDOR','yes');
1089 setConfigVar ('FILTER_SUGGEST_TAGS','yes');
1090 setConfigVar ('FILTER_SUGGEST_PREDICATES','yes');
1091 setConfigVar ('FILTER_SUGGEST_EXTRA','no');
8fee82b4 1092 setConfigVar ('DEFAULT_SNMP_COMMUNITY','public');
f06fe423 1093 setConfigVar ('IPV4_ENABLE_KNIGHT','yes');
9e51318b
DO
1094 setConfigVar ('TAGS_TOPLIST_SIZE','50');
1095 setConfigVar ('TAGS_QUICKLIST_SIZE','20');
1096 setConfigVar ('TAGS_QUICKLIST_THRESHOLD','50');
f44fdef9 1097 setConfigVar ('ENABLE_MULTIPORT_FORM', 'no');
2400d7ec
DO
1098 setConfigVar ('DEFAULT_PORT_IIF_ID', '1');
1099 setConfigVar ('DEFAULT_PORT_OIF_IDS', '1=24; 3=1078; 4=1077; 5=1079; 6=1080; 8=1082; 9=1084');
4a47d34b 1100 setConfigVar ('IPV4_TREE_RTR_AS_CELL', 'yes');
cd3775e9 1101 setConfigVar ('PROXIMITY_RANGE', 0);
ca4eb604 1102 setConfigVar ('IPV4_TREE_SHOW_VLAN', 'yes');
407ed7bb
DO
1103 setConfigVar ('VLANSWITCH_LISTSRC', '');
1104 setConfigVar ('VLANIPV4NET_LISTSRC', '');
2fb1f79b
DO
1105 setConfigVar ('DEFAULT_VDOM_ID', '');
1106 setConfigVar ('DEFAULT_VST_ID', '');
01b9a31a 1107 setConfigVar ('STATIC_FILTER', 'yes');
37cb9e18
DO
1108 setConfigVar ('8021Q_DEPLOY_MINAGE', '300');
1109 setConfigVar ('8021Q_DEPLOY_MAXAGE', '3600');
1110 setConfigVar ('8021Q_DEPLOY_RETRY', '10800');
2582446d 1111 setConfigVar ('8021Q_WRI_AFTER_CONFT_LISTSRC', 'false');
4492050b 1112 setConfigVar ('8021Q_INSTANT_DEPLOY', 'no');
b49a479e
DO
1113 setConfigVar ('CDP_RUNNERS_LISTSRC', '');
1114 setConfigVar ('LLDP_RUNNERS_LISTSRC', '');
0328f6d6 1115 setConfigVar ('HNDP_RUNNERS_LISTSRC', '');
95857b5c 1116 setConfigVar ('SHRINK_TAG_TREE_ON_CLICK', 'yes');
1ebbf889 1117 setConfigVar ('MAX_UNFILTERED_ENTITIES', '0');
61e79d63 1118 setConfigVar ('SYNCDOMAIN_MAX_PROCESSES', '0');
135080d8 1119 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
1120}
1121
b6a7d936
DO
1122$msgcode['addRealServer']['OK'] = 34;
1123$msgcode['addRealServer']['ERR'] = 126;
732e4578 1124// Add single record.
ca461127
DO
1125function addRealServer ()
1126{
0cc24e9a
DY
1127 assertUIntArg ('pool_id');
1128 assertIPv4Arg ('remoteip');
1129 assertStringArg ('rsport', TRUE);
1130 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1131 if (!addRStoRSPool (
1132 $_REQUEST['pool_id'],
1133 $_REQUEST['remoteip'],
1134 $_REQUEST['rsport'],
1135 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1136 $_REQUEST['rsconfig']
1137 ))
135080d8 1138 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1139 else
135080d8 1140 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1141}
1142
b6a7d936
DO
1143$msgcode['addRealServers']['OK'] = 37;
1144$msgcode['addRealServers']['ERR1'] = 131;
1145$msgcode['addRealServers']['ERR2'] = 127;
732e4578
DO
1146// Parse textarea submitted and try adding a real server for each line.
1147function addRealServers ()
1148{
0cc24e9a
DY
1149 assertStringArg ('format');
1150 assertStringArg ('rawtext');
732e4578
DO
1151 $ngood = $nbad = 0;
1152 $rsconfig = '';
1153 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
7f42d792 1154 foreach (explode ("\n", dos2unix ($_REQUEST['rawtext'])) as $line)
732e4578 1155 {
59a83bd8 1156 if (!strlen ($line))
732e4578
DO
1157 continue;
1158 $match = array ();
1159 switch ($_REQUEST['format'])
1160 {
1161 case 'ipvs_2': // address and port only
1162 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1163 continue;
103b1e1e 1164 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1165 $ngood++;
1166 else
1167 $nbad++;
1168 break;
1169 case 'ipvs_3': // address, port and weight
1170 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1171 continue;
103b1e1e 1172 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1173 $ngood++;
1174 else
1175 $nbad++;
1176 break;
e69c2aa3
DO
1177 case 'ssv_2': // IP address and port
1178 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1179 continue;
103b1e1e 1180 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1181 $ngood++;
1182 else
1183 $nbad++;
1184 break;
948c37e3
DO
1185 case 'ssv_1': // IP address
1186 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1187 continue;
1188 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1189 $ngood++;
1190 else
1191 $nbad++;
1192 break;
732e4578 1193 default:
135080d8 1194 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1195 break;
1196 }
1197 }
1198 if ($nbad == 0 and $ngood > 0)
135080d8 1199 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1200 else
135080d8 1201 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1202}
1203
b6a7d936
DO
1204$msgcode['addVService']['OK'] = 28;
1205$msgcode['addVService']['ERR1'] = 132;
1206$msgcode['addVService']['ERR2'] = 100;
d6517a21
DO
1207function addVService ()
1208{
0cc24e9a
DY
1209 assertIPv4Arg ('vip');
1210 assertUIntArg ('vport');
1211 assertStringArg ('proto');
103b1e1e 1212 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1213 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1214 assertStringArg ('name', TRUE);
1215 assertStringArg ('vsconfig', TRUE);
1216 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1217 $error = commitCreateVS
1218 (
103b1e1e
DO
1219 $_REQUEST['vip'],
1220 $_REQUEST['vport'],
7320b661 1221 $_REQUEST['proto'],
103b1e1e
DO
1222 $_REQUEST['name'],
1223 $_REQUEST['vsconfig'],
c63a8d6e
DO
1224 $_REQUEST['rsconfig'],
1225 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1226 );
1227 if ($error != '')
135080d8 1228 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1229 else
135080d8 1230 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1231}
1232
b6a7d936
DO
1233$msgcode['deleteVService']['OK'] = 29;
1234$msgcode['deleteVService']['ERR'] = 130;
d6517a21
DO
1235function deleteVService ()
1236{
0cc24e9a 1237 assertUIntArg ('vs_id');
e02e1941 1238 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1239 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1240 else
135080d8 1241 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1242}
1243
1f54e1ba
DO
1244$msgcode['updateSLBDefConfig']['OK'] = 43;
1245$msgcode['updateSLBDefConfig']['ERR'] = 109;
1246function updateSLBDefConfig ()
1247{
1248 $data = array(
1249 'vs' => $_REQUEST['vsconfig'],
1250 'rs' => $_REQUEST['rsconfig']
1251 );
1252 if (!commitUpdateSLBDefConf ($data))
1253 return buildRedirectURL (__FUNCTION__, 'ERR');
1254 else
1255 return buildRedirectURL (__FUNCTION__, 'OK');
1256}
1257
b6a7d936
DO
1258$msgcode['updateRealServer']['OK'] = 36;
1259$msgcode['updateRealServer']['ERR'] = 133;
fb1c4a54
DO
1260function updateRealServer ()
1261{
0cc24e9a
DY
1262 assertUIntArg ('rs_id');
1263 assertIPv4Arg ('rsip');
1264 assertStringArg ('rsport', TRUE);
1265 assertStringArg ('rsconfig', TRUE);
a5c589d2 1266 if (FALSE === commitUpdateRS (
103b1e1e
DO
1267 $_REQUEST['rs_id'],
1268 $_REQUEST['rsip'],
1269 $_REQUEST['rsport'],
1270 $_REQUEST['rsconfig']
1271 ))
135080d8 1272 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1273 else
135080d8 1274 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1275}
1276
b6a7d936
DO
1277$msgcode['updateLoadBalancer']['OK'] = 20;
1278$msgcode['updateLoadBalancer']['ERR'] = 134;
c1ca768c 1279function updateLoadBalancer ()
3241551e 1280{
0cc24e9a
DY
1281 assertUIntArg ('object_id');
1282 assertUIntArg ('pool_id');
1283 assertUIntArg ('vs_id');
1284 assertStringArg ('vsconfig', TRUE);
1285 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1286 if (! empty($_REQUEST['prio']))
1287 assertUIntArg('prio', TRUE);
1288
a5c589d2 1289 if (FALSE === commitUpdateLB (
103b1e1e
DO
1290 $_REQUEST['object_id'],
1291 $_REQUEST['pool_id'],
1292 $_REQUEST['vs_id'],
1293 $_REQUEST['vsconfig'],
1f54e1ba
DO
1294 $_REQUEST['rsconfig'],
1295 $_REQUEST['prio']
103b1e1e 1296 ))
135080d8 1297 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1298 else
135080d8 1299 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1300}
1301
b6a7d936
DO
1302$msgcode['updateVService']['OK'] = 30;
1303$msgcode['updateVService']['ERR'] = 135;
d6517a21
DO
1304function updateVService ()
1305{
0cc24e9a
DY
1306 assertUIntArg ('vs_id');
1307 assertIPv4Arg ('vip');
1308 assertUIntArg ('vport');
1309 assertStringArg ('proto');
1310 assertStringArg ('name', TRUE);
1311 assertStringArg ('vsconfig', TRUE);
1312 assertStringArg ('rsconfig', TRUE);
a5c589d2 1313 if (FALSE === commitUpdateVS (
103b1e1e
DO
1314 $_REQUEST['vs_id'],
1315 $_REQUEST['vip'],
1316 $_REQUEST['vport'],
1317 $_REQUEST['proto'],
1318 $_REQUEST['name'],
1319 $_REQUEST['vsconfig'],
1320 $_REQUEST['rsconfig']
1321 ))
135080d8 1322 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1323 else
135080d8 1324 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1325}
1326
b6a7d936
DO
1327$msgcode['addLoadBalancer']['OK'] = 18;
1328$msgcode['addLoadBalancer']['ERR'] = 137;
3241551e
DO
1329function addLoadBalancer ()
1330{
0cc24e9a
DY
1331 assertUIntArg ('pool_id');
1332 assertUIntArg ('object_id');
1333 assertUIntArg ('vs_id');
1334 assertStringArg ('vsconfig', TRUE);
1335 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1336 if (! empty($_REQUEST['prio']))
1337 assertUIntArg('prio', TRUE);
1338
103b1e1e
DO
1339 if (!addLBtoRSPool (
1340 $_REQUEST['pool_id'],
1341 $_REQUEST['object_id'],
1342 $_REQUEST['vs_id'],
1343 $_REQUEST['vsconfig'],
1f54e1ba
DO
1344 $_REQUEST['rsconfig'],
1345 $_REQUEST['prio']
103b1e1e 1346 ))
135080d8 1347 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1348 else
135080d8 1349 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1350}
1351
b6a7d936
DO
1352$msgcode['addRSPool']['OK'] = 31;
1353$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1354function addRSPool ()
1355{
0cc24e9a
DY
1356 assertStringArg ('name', TRUE);
1357 assertStringArg ('vsconfig', TRUE);
1358 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1359 $error = commitCreateRSPool
1360 (
103b1e1e
DO
1361 $_REQUEST['name'],
1362 $_REQUEST['vsconfig'],
c63a8d6e
DO
1363 $_REQUEST['rsconfig'],
1364 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1365 );
1366 if ($error != '')
135080d8 1367 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1368 else
135080d8 1369 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1370}
1371
b6a7d936
DO
1372$msgcode['deleteRSPool']['OK'] = 32;
1373$msgcode['deleteRSPool']['ERR'] = 138;
5ad76f01
DO
1374function deleteRSPool ()
1375{
0cc24e9a 1376 assertUIntArg ('pool_id');
654e4636 1377 if (commitDeleteRSPool ($_REQUEST['pool_id']) === FALSE)
135080d8 1378 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1379 else
135080d8 1380 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1381}
1382
b6a7d936
DO
1383$msgcode['updateRSPool']['OK'] = 33;
1384$msgcode['updateRSPool']['ERR'] = 139;
5ad76f01
DO
1385function updateRSPool ()
1386{
0cc24e9a
DY
1387 assertStringArg ('name', TRUE);
1388 assertStringArg ('vsconfig', TRUE);
1389 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1390 if (FALSE === commitUpdateRSPool
1391 (
1392 $_REQUEST['pool_id'],
1393 $_REQUEST['name'],
1394 $_REQUEST['vsconfig'],
1395 $_REQUEST['rsconfig']
1396 )
1397 )
135080d8 1398 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1399 else
135080d8 1400 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1401}
1402
b6a7d936
DO
1403$msgcode['updateRSInService']['OK'] = 38;
1404$msgcode['updateRSInService']['ERR'] = 140;
1f7d18fa
DO
1405function updateRSInService ()
1406{
0cc24e9a 1407 assertUIntArg ('rscount');
841a7a7a 1408 $pool_id = $_REQUEST['pool_id'];
a6e91ac2
DO
1409 $orig = spotEntity ('ipv4rspool', $pool_id);
1410 amplifyCell ($orig);
1f7d18fa
DO
1411 $nbad = $ngood = 0;
1412 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1413 {
1414 $rs_id = $_REQUEST["rsid_${i}"];
1415 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1416 $newval = 'yes';
1417 else
1418 $newval = 'no';
1419 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1420 {
a5c589d2 1421 if (FALSE !== commitSetInService ($rs_id, $newval))
1f7d18fa
DO
1422 $ngood++;
1423 else
1424 $nbad++;
1425 }
1426 }
1427 if (!$nbad)
135080d8 1428 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1429 else
135080d8 1430 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1431}
1432
b6a7d936
DO
1433$msgcode['importPTRData']['OK'] = 26;
1434$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1435// FIXME: check, that each submitted address belongs to the prefix we
1436// are operating on.
03eb5209
DO
1437function importPTRData ()
1438{
0cc24e9a 1439 assertUIntArg ('addrcount');
03eb5209 1440 $nbad = $ngood = 0;
03eb5209
DO
1441 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1442 {
3f3bd41e 1443 $inputname = "import_${i}";
03eb5209
DO
1444 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1445 continue;
0cc24e9a
DY
1446 assertIPv4Arg ("addr_${i}");
1447 assertStringArg ("descr_${i}", TRUE);
1448 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1449 // Non-existent addresses will not have this argument set in request.
1450 $rsvd = 'no';
03eb5209
DO
1451 if ($_REQUEST["rsvd_${i}"] == 'yes')
1452 $rsvd = 'yes';
3f3bd41e 1453 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1454 $ngood++;
1455 else
1456 $nbad++;
1457 }
1458 if (!$nbad)
135080d8 1459 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1460 else
135080d8 1461 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1462}
1463
b6a7d936
DO
1464$msgcode['generateAutoPorts']['OK'] = 21;
1465$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1466function generateAutoPorts ()
1467{
103b1e1e 1468 global $pageno;
6297d584 1469 $info = spotEntity ('object', $_REQUEST['object_id']);
f3f0161f 1470 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1471 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1472 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1473 else
135080d8 1474 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1475}
1476
b6a7d936
DO
1477$msgcode['saveEntityTags']['OK'] = 22;
1478$msgcode['saveEntityTags']['ERR1'] = 143;
24cbe8af 1479// Filter out implicit tags before storing the new tag set.
3355ca56 1480function saveEntityTags ()
24cbe8af 1481{
1d356026
DO
1482 global $pageno, $etype_by_pageno;
1483 if (!isset ($etype_by_pageno[$pageno]))
1484 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
be2ae2a2 1485 $realm = $etype_by_pageno[$pageno];
1d356026 1486 $entity_id = getBypassValue();
e04931c8 1487 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1488 // Build a chain from the submitted data, minimize it,
ab379543 1489 // then wipe existing records and store the new set instead.
01b6b4d6 1490 destroyTagsForEntity ($realm, $entity_id);
eb27afad
DO
1491 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1492 // so why not use it?
6e49bd1f 1493 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1494 $n_succeeds = $n_errors = 0;
6e49bd1f 1495 foreach ($newchain as $taginfo)
eb6ea26f 1496 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1497 $n_succeeds++;
1498 else
1499 $n_errors++;
ab379543 1500 if ($n_errors)
9a61c175 1501 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1502 else
135080d8 1503 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1504}
1505
b6a7d936
DO
1506$msgcode['updateTag']['OK'] = 60;
1507$msgcode['updateTag']['ERR1'] = 145;
a5c589d2 1508$msgcode['updateTag']['ERR2'] = 109;
49fb1027
DO
1509function updateTag ()
1510{
0cc24e9a
DY
1511 assertUIntArg ('tag_id');
1512 assertUIntArg ('parent_id', TRUE);
1513 assertStringArg ('tag_name');
49fb1027 1514 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1515 if (!validTagName ($tagname))
135080d8 1516 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1517 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1518 $parent_id = 'NULL';
a5c589d2 1519 if (FALSE !== commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id))
135080d8 1520 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
590e1281
DO
1521 // Use old name in the message, cause update failed.
1522 global $taglist;
a5c589d2 1523 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag']));
49fb1027
DO
1524}
1525
b6a7d936
DO
1526$msgcode['rollTags']['OK'] = 67;
1527$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1528function rollTags ()
1529{
0cc24e9a
DY
1530 assertStringArg ('sum', TRUE);
1531 assertUIntArg ('realsum');
eb6ea26f 1532 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1533 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1534 // Even if the user requested an empty tag list, don't bail out, but process existing
1535 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1536 // have its chains refined to "normal" form.
1537 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1538 $n_ok = 0;
1539 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1540 // filter out the same tag again and again. It will have own noise to cancel.
1541 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
61a1d996 1542 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
abef7149 1543 {
61a1d996 1544 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
abef7149 1545 $n_ok++;
61a1d996
DO
1546 amplifyCell ($rack);
1547 foreach ($rack['mountedObjects'] as $object_id)
abef7149
DO
1548 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1549 $n_ok++;
1550 }
135080d8 1551 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1552}
1553
b6a7d936
DO
1554$msgcode['changeMyPassword']['OK'] = 61;
1555$msgcode['changeMyPassword']['ERR1'] = 150;
1556$msgcode['changeMyPassword']['ERR2'] = 151;
1557$msgcode['changeMyPassword']['ERR3'] = 152;
1558$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1559function changeMyPassword ()
cced6b7d 1560{
b82cce3f 1561 global $remote_username, $user_auth_src;
204284ba 1562 if ($user_auth_src != 'database')
135080d8 1563 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1564 assertStringArg ('oldpassword');
1565 assertStringArg ('newpassword1');
1566 assertStringArg ('newpassword2');
b82cce3f 1567 $remote_userid = getUserIDByUsername ($remote_username);
0b2c74cb 1568 $userinfo = spotEntity ('user', $remote_userid);
b82cce3f 1569 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1570 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1571 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1572 return buildRedirectURL (__FUNCTION__, 'ERR3');
32832c0e 1573 if (FALSE !== commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1574 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1575 else
135080d8 1576 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1577}
1578
b6a7d936
DO
1579$msgcode['saveRackCode']['OK'] = 43;
1580$msgcode['saveRackCode']['ERR1'] = 154;
1581$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1582function saveRackCode ()
1583{
cced6b7d 1584 assertStringArg ('rackcode');
e6a4adb9 1585 // For the test to succeed, unescape LFs, strip CRs.
7f42d792 1586 $newcode = dos2unix ($_REQUEST['rackcode']);
cf25e649
DO
1587 $parseTree = getRackCode ($newcode);
1588 if ($parseTree['result'] != 'ACK')
135080d8 1589 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
bb09f49c
DO
1590 if (FALSE !== saveScript ('RackCode', $newcode))
1591 {
1592 saveScript ('RackCodeCache', base64_encode (serialize ($parseTree)));
135080d8 1593 return buildRedirectURL (__FUNCTION__, 'OK');
bb09f49c
DO
1594 }
1595 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1596}
1597
b6a7d936 1598$msgcode['setPortVLAN']['ERR1'] = 156;
46f92ff7
DO
1599// This handler's context is pre-built, but not authorized. It is assumed, that the
1600// handler will take existing context and before each commit check authorization
1601// on the base chain plus necessary context added.
1602function setPortVLAN ()
1603{
0cc24e9a 1604 assertUIntArg ('portcount');
46f92ff7
DO
1605 $data = getSwitchVLANs ($_REQUEST['object_id']);
1606 if ($data === NULL)
135080d8 1607 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1608 list ($vlanlist, $portlist) = $data;
1609 // Here we just build up 1 set command for the gateway with all of the ports
1610 // included. The gateway is expected to filter unnecessary changes silently
1611 // and to provide a list of responses with either error or success message
1612 // for each of the rest.
1613 $nports = $_REQUEST['portcount'];
1614 $prefix = 'set ';
db55cf54 1615 $log = emptyLog();
46f92ff7
DO
1616 $setcmd = '';
1617 for ($i = 0; $i < $nports; $i++)
1618 if
1619 (
1620 !isset ($_REQUEST['portname_' . $i]) ||
1621 !isset ($_REQUEST['vlanid_' . $i]) ||
1622 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1623 )
24dcb9d8 1624 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1625 elseif
1626 (
1627 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1628 $portlist[$i]['vlaind'] == 'TRUNK'
1629 )
1630 continue;
1631 else
1632 {
1633 $portname = $_REQUEST['portname_' . $i];
1634 $oldvlanid = $portlist[$i]['vlanid'];
1635 $newvlanid = $_REQUEST['vlanid_' . $i];
647635ad
DO
1636 if
1637 (
1638 !permitted (NULL, NULL, NULL, array (array ('tag' => '$fromvlan_' . $oldvlanid))) or
1639 !permitted (NULL, NULL, NULL, array (array ('tag' => '$tovlan_' . $newvlanid)))
1640 )
46f92ff7 1641 {
24dcb9d8 1642 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1643 continue;
1644 }
1645 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1646 $prefix = ';';
1647 }
1648 // Feed the gateway and interpret its (non)response.
1649 if ($setcmd != '')
f0ff4930 1650 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1651 else
24dcb9d8 1652 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1653 return buildWideRedirectURL ($log);
1654}
1655
12b0c847
DO
1656$msgcode['submitSLBConfig']['OK'] = 66;
1657$msgcode['submitSLBConfig']['ERR'] = 164;
2987fc1f
DO
1658function submitSLBConfig ()
1659{
2987fc1f 1660 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
12b0c847
DO
1661 try
1662 {
1663 gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
1664 }
3a089a44 1665 catch (RTGatewayError $e)
12b0c847 1666 {
3a089a44 1667 return buildRedirectURL (__FUNCTION__, 'ERR', array ($e->getMessage()));
12b0c847
DO
1668 }
1669 return buildRedirectURL (__FUNCTION__, 'OK', array ('slbconfig'));
2987fc1f
DO
1670}
1671
b6a7d936
DO
1672$msgcode['updateRow']['OK'] = 75;
1673$msgcode['updateRow']['ERR'] = 100;
345fd640
AD
1674function updateRow ()
1675{
0cc24e9a
DY
1676 assertUIntArg ('row_id');
1677 assertStringArg ('name');
345fd640 1678
39eadd27 1679 if (FALSE !== commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1680 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1681 else
135080d8 1682 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1683}
1684
b6a7d936
DO
1685$msgcode['addRack']['OK'] = 65;
1686$msgcode['addRack']['ERR1'] = 171;
1687$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1688function addRack ()
1689{
f19c75d6
DO
1690 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1691 if (isset ($_REQUEST['got_data']))
1692 {
0cc24e9a
DY
1693 assertStringArg ('rack_name');
1694 assertUIntArg ('rack_height1');
1695 assertStringArg ('rack_comment', TRUE);
f19c75d6
DO
1696
1697 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1698 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1699 else
135080d8 1700 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1701 }
1702 elseif (isset ($_REQUEST['got_mdata']))
1703 {
0cc24e9a
DY
1704 assertUIntArg ('rack_height2');
1705 assertStringArg ('rack_names', TRUE);
db55cf54 1706 $log = emptyLog();
f19c75d6 1707 // copy-and-paste from renderAddMultipleObjectsForm()
7f42d792 1708 $names1 = explode ("\n", $_REQUEST['rack_names']);
f19c75d6
DO
1709 $names2 = array();
1710 foreach ($names1 as $line)
1711 {
1712 $parts = explode ('\r', $line);
1713 reset ($parts);
59a83bd8 1714 if (!strlen ($parts[0]))
f19c75d6
DO
1715 continue;
1716 else
1717 $names2[] = rtrim ($parts[0]);
1718 }
b3029d5c 1719 global $msgcode;
f19c75d6
DO
1720 foreach ($names2 as $cname)
1721 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1722 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1723 else
b3029d5c 1724 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1725 return buildWideRedirectURL ($log);
1726 }
1727 else
135080d8 1728 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1729}
1730
b6a7d936
DO
1731$msgcode['deleteRack']['OK'] = 79;
1732$msgcode['deleteRack']['ERR'] = 100;
1733$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1734function deleteRack ()
1735{
0cc24e9a 1736 assertUIntArg ('rack_id');
61a1d996
DO
1737 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1738 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1739 amplifyCell ($rackData);
1740 if (count ($rackData['mountedObjects']))
c8187437 1741 return buildRedirectURL (__FUNCTION__, 'ERR1');
61a1d996
DO
1742 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1743 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1744 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
c8187437
DY
1745}
1746
b6a7d936
DO
1747$msgcode['updateRack']['OK'] = 68;
1748$msgcode['updateRack']['ERR'] = 177;
7056988c
DO
1749function updateRack ()
1750{
0cc24e9a
DY
1751 assertUIntArg ('rack_row_id');
1752 assertUIntArg ('rack_height');
1753 assertStringArg ('rack_name');
1754 assertStringArg ('rack_comment', TRUE);
7056988c 1755
7056988c 1756 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1757 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1758 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1759 else
135080d8 1760 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1761}
1762
4fbb5a00
DY
1763function updateRackDesign ()
1764{
6cfedb71 1765 $rackData = spotEntity ('rack', $_REQUEST['rack_id']);
61a1d996 1766 amplifyCell ($rackData);
4fbb5a00
DY
1767 applyRackDesignMask($rackData);
1768 markupObjectProblems ($rackData);
1769 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1770 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1771}
1772
1773function updateRackProblems ()
1774{
6cfedb71 1775 $rackData = spotEntity ('rack', $_REQUEST['rack_id']);
61a1d996 1776 amplifyCell ($rackData);
4fbb5a00
DY
1777 applyRackProblemMask($rackData);
1778 markupObjectProblems ($rackData);
1779 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1780 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1781}
1782
7056988c
DO
1783function querySNMPData ()
1784{
8228bdb4
DO
1785 assertStringArg ('community', TRUE);
1786
1787 $snmpsetup = array ();
1788 if ( strlen($_REQUEST['community']) == 0 ) {
1789 assertStringArg ('sec_name');
1790 assertStringArg ('sec_level');
1791 assertStringArg ('auth_protocol');
1792 assertStringArg ('auth_passphrase', TRUE);
1793 assertStringArg ('priv_protocol');
1794 assertStringArg ('priv_passphrase', TRUE);
1795
1796 $snmpsetup['sec_name'] = $_REQUEST['sec_name'];
1797 $snmpsetup['sec_level'] = $_REQUEST['sec_level'];
1798 $snmpsetup['auth_protocol'] = $_REQUEST['auth_protocol'];
1799 $snmpsetup['auth_passphrase'] = $_REQUEST['auth_passphrase'];
1800 $snmpsetup['priv_protocol'] = $_REQUEST['priv_protocol'];
1801 $snmpsetup['priv_passphrase'] = $_REQUEST['priv_passphrase'];
1802 }
1803 else {
1804 $snmpsetup['community'] = $_REQUEST['community'];
1805 }
1806
1807
1808 return doSNMPmining ($_REQUEST['object_id'], $snmpsetup);
7056988c
DO
1809}
1810
b6a7d936 1811$msgcode['addFileWithoutLink']['OK'] = 69;
6cfedb71
DO
1812$msgcode['addFileWithoutLink']['ERR1'] = 181;
1813$msgcode['addFileWithoutLink']['ERR2'] = 110;
e1ae3fb4
AD
1814// File-related functions
1815function addFileWithoutLink ()
1816{
0cc24e9a 1817 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1818
1819 // Make sure the file can be uploaded
1820 if (get_cfg_var('file_uploads') != 1)
6cfedb71 1821 return buildRedirectURL (__FUNCTION__, 'ERR1');
e1ae3fb4
AD
1822
1823 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650 1824 global $sic;
6cfedb71
DO
1825 if (FALSE === commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']))
1826 return buildRedirectURL (__FUNCTION__, 'ERR2');
f857f71f
DO
1827 if (isset ($_REQUEST['taglist']))
1828 produceTagsForLastRecord ('file', $_REQUEST['taglist']);
6cfedb71 1829 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($_FILES['file']['name'])));
e1ae3fb4
AD
1830}
1831
b6a7d936 1832$msgcode['addFileToEntity']['OK'] = 69;
b6a7d936 1833$msgcode['addFileToEntity']['ERR2'] = 181;
6cfedb71 1834$msgcode['addFileToEntity']['ERR3'] = 110;
e1ae3fb4
AD
1835function addFileToEntity ()
1836{
1d356026
DO
1837 global $pageno, $etype_by_pageno;
1838 if (!isset ($etype_by_pageno[$pageno]))
1839 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
9a61c175 1840 $realm = $etype_by_pageno[$pageno];
1d356026 1841 $entity_id = getBypassValue();
0cc24e9a 1842 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1843
1844 // Make sure the file can be uploaded
1845 if (get_cfg_var('file_uploads') != 1)
9a61c175 1846 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
1847
1848 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650 1849 global $sic;
6cfedb71
DO
1850 if (FALSE === commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']))
1851 return buildRedirectURL (__FUNCTION__, 'ERR3');
1852 if (FALSE === commitLinkFile (lastInsertID(), $realm, $entity_id))
1853 return buildRedirectURL (__FUNCTION__, 'ERR3');
e1ae3fb4 1854
6cfedb71 1855 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($_FILES['file']['name'])));
e1ae3fb4
AD
1856}
1857
b6a7d936
DO
1858$msgcode['linkFileToEntity']['OK'] = 71;
1859$msgcode['linkFileToEntity']['ERR1'] = 178;
6cfedb71 1860$msgcode['linkFileToEntity']['ERR2'] = 110;
e1ae3fb4
AD
1861function linkFileToEntity ()
1862{
0cc24e9a 1863 assertUIntArg ('file_id');
1d356026
DO
1864 global $pageno, $etype_by_pageno;
1865 if (!isset ($etype_by_pageno[$pageno]))
1866 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
121496b6 1867
d3b5008b 1868 $fi = spotEntity ('file', $_REQUEST['file_id']);
1d356026 1869 if (FALSE === commitLinkFile ($_REQUEST['file_id'], $etype_by_pageno[$pageno], getBypassValue()))
6cfedb71 1870 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4 1871
6cfedb71 1872 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($fi['name'])));
e1ae3fb4
AD
1873}
1874
b6a7d936
DO
1875$msgcode['replaceFile']['OK'] = 70;
1876$msgcode['replaceFile']['ERR1'] = 181;
1877$msgcode['replaceFile']['ERR2'] = 207;
6cfedb71 1878$msgcode['replaceFile']['ERR3'] = 109;
fd1fb420
AD
1879function replaceFile ()
1880{
db55cf54 1881 global $sic;
fd1fb420
AD
1882
1883 // Make sure the file can be uploaded
1884 if (get_cfg_var('file_uploads') != 1)
db55cf54 1885 return buildRedirectURL (__FUNCTION__, 'ERR1');
d3b5008b 1886 $shortInfo = spotEntity ('file', $sic['file_id']);
fd1fb420
AD
1887
1888 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1889 if ($fp === FALSE)
1890 return buildRedirectURL (__FUNCTION__, 'ERR2');
6cfedb71
DO
1891 if (FALSE === commitReplaceFile ($sic['file_id'], $fp))
1892 return buildRedirectURL (__FUNCTION__, 'ERR3');
fd1fb420 1893
d3346ce2
DO
1894 usePreparedExecuteBlade
1895 (
1896 'UPDATE File SET thumbnail = NULL WHERE id = ?',
1897 $sic['file_id']
1898 );
1899
db55cf54 1900 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1901}
1902
b6a7d936 1903$msgcode['updateFile']['OK'] = 70;
6cfedb71 1904$msgcode['updateFile']['ERR'] = 109;
e1ae3fb4
AD
1905function updateFile ()
1906{
0cc24e9a
DY
1907 assertStringArg ('file_name');
1908 assertStringArg ('file_type');
1909 assertStringArg ('file_comment', TRUE);
f3d274bf 1910 global $sic;
6cfedb71
DO
1911 if (FALSE === commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']))
1912 return buildRedirectURL (__FUNCTION__, 'ERR');
7221c918 1913 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['file_name']));
e1ae3fb4
AD
1914}
1915
b6a7d936
DO
1916$msgcode['unlinkFile']['OK'] = 72;
1917$msgcode['unlinkFile']['ERR'] = 182;
e1ae3fb4
AD
1918function unlinkFile ()
1919{
0cc24e9a 1920 assertUIntArg ('link_id');
e1ae3fb4
AD
1921 $error = commitUnlinkFile ($_REQUEST['link_id']);
1922
1923 if ($error != '')
135080d8 1924 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1925
8bc5d1e4 1926 return buildRedirectURL (__FUNCTION__, 'OK');
e1ae3fb4
AD
1927}
1928
b6a7d936
DO
1929$msgcode['deleteFile']['OK'] = 73;
1930$msgcode['deleteFile']['ERR'] = 100;
e1ae3fb4
AD
1931function deleteFile ()
1932{
0cc24e9a 1933 assertUIntArg ('file_id');
d3b5008b 1934 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
e1ae3fb4
AD
1935 $error = commitDeleteFile ($_REQUEST['file_id']);
1936
1937 if ($error != '')
135080d8 1938 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1939
f8874cdb 1940 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1941}
1942
b6a7d936
DO
1943$msgcode['updateFileText']['OK'] = 78;
1944$msgcode['updateFileText']['ERR1'] = 179;
1945$msgcode['updateFileText']['ERR2'] = 180;
8628ae44
DO
1946function updateFileText ()
1947{
0cc24e9a
DY
1948 assertStringArg ('mtime_copy');
1949 assertStringArg ('file_text', TRUE); // it's Ok to save empty
d3b5008b 1950 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
70cb9b56
DO
1951 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1952 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb 1953 global $sic;
6cfedb71
DO
1954 if (FALSE === commitReplaceFile ($sic['file_id'], $sic['file_text']))
1955 return buildRedirectURL (__FUNCTION__, 'ERR2');
1956 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
8628ae44
DO
1957}
1958
2dfa1b73
DO
1959$msgcode['addPortInterfaceCompat']['OK'] = 48;
1960$msgcode['addPortInterfaceCompat']['ERR'] = 110;
1961function addPortInterfaceCompat ()
1962{
0cc24e9a
DY
1963 assertUIntArg ('iif_id');
1964 assertUIntArg ('oif_id');
2dfa1b73
DO
1965 if (commitSupplementPIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
1966 return buildRedirectURL (__FUNCTION__, 'OK');
1967 return buildRedirectURL (__FUNCTION__, 'ERR');
1968}
1969
2400d7ec
DO
1970$ifcompatpack = array
1971(
1972 '1000cwdm80' => array (1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216),
1973 '1000dwdm80' => array // ITU channels 20~61
1974 (
1975 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226,
1976 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236,
1977 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
1978 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
1979 1257, 1258
1980 ),
1981 '10000dwdm80' => array // same channels for 10GE
1982 (
1983 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268,
1984 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278,
1985 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
1986 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298,
1987 1299, 1300
1988 ),
1989);
1990
3d9ac3d4
DO
1991$msgcode['addPortInterfaceCompatPack']['OK'] = 44;
1992$msgcode['addPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
1993function addPortInterfaceCompatPack ()
1994{
0cc24e9a
DY
1995 assertStringArg ('standard');
1996 assertUIntArg ('iif_id');
3d9ac3d4
DO
1997 global $ifcompatpack;
1998 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
1999 return buildRedirectURL (__FUNCTION__, 'ERR');
2000 $ngood = $nbad = 0;
2001 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2002 if (commitSupplementPIC ($_REQUEST['iif_id'], $oif_id))
2003 $ngood++;
2004 else
2005 $nbad++;
2006 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2007}
2008
3d9ac3d4
DO
2009$msgcode['delPortInterfaceCompatPack']['OK'] = 44;
2010$msgcode['delPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2011function delPortInterfaceCompatPack ()
2012{
0cc24e9a
DY
2013 assertStringArg ('standard');
2014 assertUIntArg ('iif_id');
10005279
DO
2015 global $ifcompatpack, $sic;
2016 if (!array_key_exists ($sic['standard'], $ifcompatpack) or !array_key_exists ($sic['iif_id'], getPortIIFOptions()))
3d9ac3d4
DO
2017 return buildRedirectURL (__FUNCTION__, 'ERR');
2018 $ngood = $nbad = 0;
10005279
DO
2019 foreach ($ifcompatpack[$sic['standard']] as $oif_id)
2020 if (usePreparedDeleteBlade ('PortInterfaceCompat', array ('iif_id' => $sic['iif_id'], 'oif_id' => $oif_id)))
3d9ac3d4
DO
2021 $ngood++;
2022 else
2023 $nbad++;
2024 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2025}
2026
38cd7704
DO
2027$msgcode['add8021QOrder']['OK'] = 48;
2028$msgcode['add8021QOrder']['ERR'] = 118;
2029function add8021QOrder ()
8198f2c6 2030{
80ec1f4f
DO
2031 assertUIntArg ('vdom_id');
2032 assertUIntArg ('object_id');
38cd7704 2033 assertUIntArg ('vst_id');
c1aa3ada
DO
2034 global $sic;
2035 $result = usePreparedExecuteBlade
8198f2c6 2036 (
c1aa3ada
DO
2037 'INSERT INTO VLANSwitch (domain_id, object_id, template_id, last_change, out_of_sync) ' .
2038 'VALUES (?, ?, ?, NOW(), "yes")',
2039 array ($sic['vdom_id'], $sic['object_id'], $sic['vst_id'])
8198f2c6 2040 );
c1aa3ada 2041 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
8198f2c6
DO
2042}
2043
38cd7704
DO
2044$msgcode['del8021QOrder']['OK'] = 49;
2045$msgcode['del8021QOrder']['ERR'] = 119;
2046function del8021QOrder ()
8198f2c6 2047{
80ec1f4f 2048 assertUIntArg ('object_id');
4191d7ab
AA
2049 assertUIntArg ('vdom_id');
2050 assertUIntArg ('vst_id');
8198f2c6 2051 global $sic;
38cd7704 2052 $result = usePreparedDeleteBlade ('VLANSwitch', array ('object_id' => $sic['object_id']));
4191d7ab
AA
2053 $focus_hints = array
2054 (
2055 'prev_objid' => $_REQUEST['object_id'],
2056 'prev_vstid' => $_REQUEST['vst_id'],
2057 'prev_vdid' => $_REQUEST['vdom_id'],
2058 );
2059 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR', array(), NULL, NULL, $focus_hints);
8198f2c6
DO
2060}
2061
8198f2c6 2062$msgcode['delVLANDescription']['OK'] = 49;
c48f2e18
DO
2063$msgcode['delVLANDescription']['ERR1'] = 105;
2064$msgcode['delVLANDescription']['ERR2'] = 111;
8198f2c6
DO
2065function delVLANDescription ()
2066{
80ec1f4f 2067 assertUIntArg ('vlan_id');
8198f2c6 2068 global $sic;
c48f2e18 2069 if ($sic['vlan_id'] == VLAN_DFL_ID)
048b0420 2070 return buildRedirectURL (__FUNCTION__, 'ERR1');
8198f2c6 2071 $result = commitReduceVLANDescription ($sic['vdom_id'], $sic['vlan_id']);
c48f2e18 2072 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR2');
8198f2c6
DO
2073}
2074
2075$msgcode['updVLANDescription']['OK'] = 51;
c48f2e18
DO
2076$msgcode['updVLANDescription']['ERR1'] = 105;
2077$msgcode['updVLANDescription']['ERR2'] = 109;
8198f2c6
DO
2078function updVLANDescription ()
2079{
80ec1f4f
DO
2080 assertUIntArg ('vlan_id');
2081 assertStringArg ('vlan_type');
2082 assertStringArg ('vlan_descr', TRUE);
8198f2c6 2083 global $sic;
c48f2e18 2084 if ($sic['vlan_id'] == VLAN_DFL_ID)
048b0420 2085 return buildRedirectURL (__FUNCTION__, 'ERR1');
8198f2c6
DO
2086 $result = commitUpdateVLANDescription
2087 (
2088 $sic['vdom_id'],
2089 $sic['vlan_id'],
0dabdc53 2090 $sic['vlan_type'],
8198f2c6
DO
2091 $sic['vlan_descr']
2092 );
6cfedb71 2093 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR2');
8198f2c6
DO
2094}
2095
2096$msgcode['createVLANDomain']['OK'] = 48;
2097$msgcode['createVLANDomain']['ERR'] = 110;
2098function createVLANDomain ()
2099{
80ec1f4f 2100 assertStringArg ('vdom_descr');
8198f2c6
DO
2101 global $sic;
2102 $result = usePreparedInsertBlade
2103 (
2104 'VLANDomain',
2105 array
2106 (
2107 'description' => $sic['vdom_descr'],
2108 )
2109 );
10c7f49e
DO
2110 $result = $result and usePreparedInsertBlade
2111 (
2112 'VLANDescription',
2113 array
2114 (
2115 'domain_id' => lastInsertID(),
2116 'vlan_id' => VLAN_DFL_ID,
2117 'vlan_type' => 'compulsory',
c48f2e18 2118 'vlan_descr' => 'default',
10c7f49e
DO
2119 )
2120 );
8198f2c6
DO
2121 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2122}
2123
2124$msgcode['destroyVLANDomain']['OK'] = 49;
2125$msgcode['destroyVLANDomain']['ERR'] = 111;
2126function destroyVLANDomain ()
2127{
80ec1f4f 2128 assertUIntArg ('vdom_id');
8198f2c6 2129 global $sic;
bbae3611 2130 $result = FALSE !== usePreparedDeleteBlade ('VLANDomain', array ('id' => $sic['vdom_id']));
8198f2c6
DO
2131 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2132}
2133
2134$msgcode['updateVLANDomain']['OK'] = 51;
2135$msgcode['updateVLANDomain']['ERR'] = 109;
2136function updateVLANDomain ()
2137{
80ec1f4f
DO
2138 assertUIntArg ('vdom_id');
2139 assertStringArg ('vdom_descr');
8198f2c6 2140 global $sic;
d3192636 2141 $result = FALSE !== commitUpdateVLANDomain ($sic['vdom_id'], $sic['vdom_descr']);
8198f2c6
DO
2142 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2143}
2144
4492050b
DO
2145$msgcode['save8021QPorts']['OK1'] = 63;
2146$msgcode['save8021QPorts']['OK2'] = 41;
706d9175
DO
2147$msgcode['save8021QPorts']['ERR1'] = 160;
2148$msgcode['save8021QPorts']['ERR2'] = 109;
2149function save8021QPorts ()
8198f2c6 2150{
a7492e95 2151 global $sic, $dbxlink;
70cff23f 2152 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de8aa722
DO
2153 assertStringArg ('form_mode');
2154 if ($sic['form_mode'] != 'save' and $sic['form_mode'] != 'duplicate')
2155 throw new InvalidRequestArgException ('form_mode', $sic['form_mode']);
2156 $extra = array();
a7492e95 2157 $dbxlink->beginTransaction();
2996cbbb
DO
2158 try
2159 {
a7492e95
DO
2160 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
2161 throw new InvalidArgException ('object_id', $object_id, 'VLAN domain is not set for this object');
9c45ea37 2162 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
1a43ce88 2163 throw new InvalidRequestArgException ('mutex_rev', $sic['mutex_rev'], 'expired form data');
de8aa722 2164 $after = $before = apply8021QOrder ($vswitch['template_id'], getStored8021QConfig ($sic['object_id'], 'desired'));
af204724 2165 $changes = array();
de8aa722 2166 switch ($sic['form_mode'])
706d9175 2167 {
de8aa722
DO
2168 case 'save':
2169 assertUIntArg ('nports');
2170 if ($sic['nports'] == 1)
706d9175 2171 {
de8aa722
DO
2172 assertStringArg ('pn_0');
2173 $extra = array ('port_name' => $sic['pn_0']);
2174 }
2175 for ($i = 0; $i < $sic['nports']; $i++)
2176 {
2177 assertStringArg ('pn_' . $i);
2178 assertStringArg ('pm_' . $i);
2179 // An access port only generates form input for its native VLAN,
2180 // which we derive allowed VLAN list from.
2181 $native = isset ($sic['pnv_' . $i]) ? $sic['pnv_' . $i] : 0;
2182 switch ($sic["pm_${i}"])
2183 {
2184 case 'trunk':
b36fc896 2185# assertArrayArg ('pav_' . $i);
de8aa722
DO
2186 $allowed = isset ($sic['pav_' . $i]) ? $sic['pav_' . $i] : array();
2187 break;
2188 case 'access':
2189 if ($native == 'same')
2190 continue 2;
2191 assertUIntArg ('pnv_' . $i);
2192 $allowed = array ($native);
2193 break;
2194 default:
2195 throw new InvalidRequestArgException ("pm_${i}", $_REQUEST["pm_${i}"], 'unknown port mode');
2196 }
2197 $changes[$sic['pn_' . $i]] = array
2198 (
2199 'mode' => $sic['pm_' . $i],
2200 'allowed' => $allowed,
2201 'native' => $native,
2202 );
706d9175 2203 }
de8aa722
DO
2204 break;
2205 case 'duplicate':
2206 assertStringArg ('from_port');
2207# assertArrayArg ('to_ports');
2208 if (!array_key_exists ($sic['from_port'], $before))
2209 throw new InvalidArgException ('from_port', $sic['from_port'], 'this port does not exist');
2210 foreach ($sic['to_ports'] as $tpn)
2211 if (!array_key_exists ($tpn, $before))
2212 throw new InvalidArgException ('to_ports[]', $tpn, 'this port does not exist');
2213 elseif ($tpn != $sic['from_port'])
2214 $changes[$tpn] = $before[$sic['from_port']];
2215 break;
706d9175 2216 }
af204724
DO
2217 $domain_vlanlist = getDomainVLANs ($vswitch['domain_id']);
2218 $changes = filter8021QChangeRequests
a7492e95 2219 (
af204724 2220 $domain_vlanlist,
9165e8f7 2221 $before,
af204724 2222 apply8021QOrder ($vswitch['template_id'], $changes)
a7492e95 2223 );
bb2024b9 2224 $changes = authorize8021QChangeRequests ($before, $changes);
af204724
DO
2225 foreach ($changes as $port_name => $port)
2226 $after[$port_name] = $port;
b3a27170 2227 $new_uplinks = filter8021QChangeRequests ($domain_vlanlist, $after, produceUplinkPorts ($domain_vlanlist, $after));
7455e4c0 2228 $npulled = replace8021QPorts ('desired', $vswitch['object_id'], $before, $changes);
b3a27170 2229 $nsaved_uplinks = replace8021QPorts ('desired', $vswitch['object_id'], $before, $new_uplinks);
2996cbbb
DO
2230 }
2231 catch (Exception $e)
861292ee 2232 {
a7492e95 2233 $dbxlink->rollBack();
706d9175 2234 return buildRedirectURL (__FUNCTION__, 'ERR2', array(), NULL, NULL, $extra);
861292ee 2235 }
b3a27170 2236 if ($npulled + $nsaved_uplinks)
c1aa3ada
DO
2237 $result = usePreparedExecuteBlade
2238 (
2239 'UPDATE VLANSwitch SET mutex_rev=mutex_rev+1, last_change=NOW(), out_of_sync="yes" WHERE object_id=?',
2240 array ($sic['object_id'])
2241 );
a7492e95 2242 $dbxlink->commit();
4492050b 2243 $log = oneLiner (63, array ($npulled + $nsaved_uplinks));
b3a27170 2244 if ($nsaved_uplinks)
4492050b 2245 {
b3a27170 2246 initiateUplinksReverb ($vswitch['object_id'], $new_uplinks);
4492050b
DO
2247 $log = mergeLogs ($log, oneLiner (41));
2248 }
2249 if ($npulled + $nsaved_uplinks > 0 and getConfigVar ('8021Q_INSTANT_DEPLOY') == 'yes')
2250 {
2251 try
2252 {
2253 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], TRUE))
2254 $log = mergeLogs ($log, oneLiner (191));
2255 else
2256 $log = mergeLogs ($log, oneLiner (63, array ($done)));
2257 }
2258 catch (Exception $e)
2259 {
2260 $log = mergeLogs ($log, oneLiner (109));
2261 }
2262 }
2263 return buildWideRedirectURL ($log, NULL, NULL, $extra);
8198f2c6
DO
2264}
2265
8846b060
DO
2266$msgcode['bindVLANtoIPv4']['OK'] = 48;
2267$msgcode['bindVLANtoIPv4']['ERR'] = 110;
2268function bindVLANtoIPv4 ()
2269{
2270 assertUIntArg ('id'); // network id
2271 global $sic;
2272 $result = commitSupplementVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2273 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2274}
2275
21ee3351
AA
2276$msgcode['bindVLANtoIPv6']['OK'] = 48;
2277$msgcode['bindVLANtoIPv6']['ERR'] = 110;
2278function bindVLANtoIPv6 ()
2279{
2280 assertUIntArg ('id'); // network id
2281 global $sic;
2282 $result = commitSupplementVLANIPv6 ($sic['vlan_ck'], $_REQUEST['id']);
2283 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2284}
2285
8846b060
DO
2286$msgcode['unbindVLANfromIPv4']['OK'] = 49;
2287$msgcode['unbindVLANfromIPv4']['ERR'] = 111;
2288function unbindVLANfromIPv4 ()
2289{
2290 assertUIntArg ('id'); // network id
2291 global $sic;
2292 $result = commitReduceVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2293 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2294}
2295
21ee3351
AA
2296$msgcode['unbindVLANfromIPv6']['OK'] = 49;
2297$msgcode['unbindVLANfromIPv6']['ERR'] = 111;
2298function unbindVLANfromIPv6 ()
2299{
2300 assertUIntArg ('id'); // network id
2301 global $sic;
2302 $result = commitReduceVLANIPv6 ($sic['vlan_ck'], $sic['id']);
2303 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2304}
2305
228c430c 2306$msgcode['process8021QSyncRequest']['OK'] = 63;
ec523868 2307$msgcode['process8021QSyncRequest']['ERR'] = 191;
228c430c 2308function process8021QSyncRequest ()
be28b696 2309{
2e84a05b
DO
2310 // behave depending on current operation: exec8021QPull or exec8021QPush
2311 global $sic, $op;
ec523868
DO
2312 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], $op == 'exec8021QPush'))
2313 return buildRedirectURL (__FUNCTION__, 'ERR');
2314 return buildRedirectURL (__FUNCTION__, 'OK', array ($done));
be28b696
DO
2315}
2316
f9428bc6
AA
2317$msgcode['process8021QRecalcRequest']['CHANGED'] = 87;
2318$msgcode['process8021QRecalcRequest']['NO_CHANGES'] = 300;
fb3e3731 2319$msgcode['process8021QRecalcRequest']['ERR'] = 192;
f9428bc6
AA
2320function process8021QRecalcRequest ()
2321{
f9428bc6 2322 global $sic;
fb3e3731
AA
2323 if (! permitted (NULL, NULL, NULL, array (array ('tag' => '$op_recalc8021Q'))))
2324 return buildRedirectURL (__FUNCTION__, 'ERR');
f9428bc6
AA
2325 $counters = recalc8021QPorts ($sic['object_id']);
2326 if ($counters['ports'])
2327 return buildRedirectURL (__FUNCTION__, 'CHANGED', array ($counters['ports'], $counters['switches']));
2328 else
2329 return buildRedirectURL (__FUNCTION__, 'NO_CHANGES', array ('No changes were made'));
2330}
2331
be28b696 2332$msgcode['resolve8021QConflicts']['OK'] = 63;
bcd14540
DO
2333$msgcode['resolve8021QConflicts']['ERR1'] = 179;
2334$msgcode['resolve8021QConflicts']['ERR2'] = 109;
be28b696 2335function resolve8021QConflicts ()
07de6bb3 2336{
a7492e95 2337 global $sic, $dbxlink;
70cff23f 2338 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de47b574
DO
2339 assertUIntArg ('nrows');
2340 // Divide submitted radio buttons into 3 groups:
19d1e731 2341 // left (saved version wins)
07de6bb3 2342 // asis (ignore)
19d1e731
DO
2343 // right (running version wins)
2344 $F = array();
339534a0 2345 for ($i = 0; $i < $sic['nrows']; $i++)
07de6bb3 2346 {
de47b574 2347 if (!array_key_exists ("i_${i}", $sic))
07de6bb3 2348 continue;
de47b574
DO
2349 // let's hope other inputs are in place
2350 switch ($sic["i_${i}"])
07de6bb3
DO
2351 {
2352 case 'left':
07de6bb3 2353 case 'right':
bcd14540 2354 $F[$sic["pn_${i}"]] = array
07de6bb3 2355 (
ca9e1c9d 2356 'mode' => $sic["rm_${i}"],
de47b574
DO
2357 'allowed' => $sic["ra_${i}"],
2358 'native' => $sic["rn_${i}"],
bcd14540 2359 'decision' => $sic["i_${i}"],
07de6bb3
DO
2360 );
2361 break;
2362 default:
2363 // don't care
2364 }
2365 }
a7492e95 2366 $dbxlink->beginTransaction();
07de6bb3
DO
2367 try
2368 {
a7492e95 2369 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
ec4d604c 2370 throw new InvalidArgException ('object_id', $sic['object_id'], 'VLAN domain is not set for this object');
9c45ea37 2371 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
1a43ce88 2372 throw new InvalidRequestArgException ('mutex_rev', $sic['mutex_rev'], 'expired form (table data has changed)');
9c2201ba
DO
2373 $D = getStored8021QConfig ($vswitch['object_id'], 'desired');
2374 $C = getStored8021QConfig ($vswitch['object_id'], 'cached');
2375 $R = getRunning8021QConfig ($vswitch['object_id']);
d5e306b2 2376 $plan = get8021QSyncOptions ($vswitch, $D, $C, $R['portdata']);
bcd14540
DO
2377 $ndone = 0;
2378 foreach ($F as $port_name => $port)
2379 {
9c2201ba
DO
2380 if (!array_key_exists ($port_name, $plan))
2381 continue;
2382 elseif ($plan[$port_name]['status'] == 'merge_conflict')
bcd14540 2383 {
9c2201ba
DO
2384 // for R neither mutex nor revisions can be emulated, but revision change can be
2385 if (!same8021QConfigs ($port, $R['portdata'][$port_name]))
1a43ce88 2386 throw new InvalidRequestArgException ("port ${port_name}", '(hidden)', 'expired form (switch data has changed)');
1f8f67c2 2387 if ($port['decision'] == 'right') // D wins, frame R by writing value of R to C
9c2201ba 2388 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $port);
1f8f67c2 2389 elseif ($port['decision'] == 'left') // R wins, cross D up
9c2201ba
DO
2390 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $D[$port_name]);
2391 // otherwise there was no decision made
2392 }
1f8f67c2
DO
2393 elseif
2394 (
2395 $plan[$port_name]['status'] == 'delete_conflict' or
2396 $plan[$port_name]['status'] == 'martian_conflict'
2397 )
9c2201ba 2398 {
1f8f67c2 2399 if ($port['decision'] == 'left') // confirm deletion of local copy
9c2201ba 2400 $ndone += del8021QPort ($vswitch['object_id'], $port_name);
bcd14540 2401 }
9c2201ba 2402 // otherwise ignore a decision, which doesn't address a conflict
bcd14540 2403 }
07de6bb3 2404 }
1a43ce88 2405 catch (InvalidRequestArgException $e)
07de6bb3 2406 {
a7492e95 2407 $dbxlink->rollBack();
bcd14540 2408 return buildRedirectURL (__FUNCTION__, 'ERR1');
ec4d604c 2409 }
bcd14540 2410 catch (Exception $e)
ec4d604c 2411 {
bcd14540
DO
2412 $dbxlink->rollBack();
2413 return buildRedirectURL (__FUNCTION__, 'ERR2');
ec4d604c 2414 }
a7492e95 2415 $dbxlink->commit();
bcd14540 2416 return buildRedirectURL (__FUNCTION__, 'OK', array ($ndone));
07de6bb3
DO
2417}
2418
e0d188ef
DO
2419$msgcode['addVLANSwitchTemplate']['OK'] = 48;
2420$msgcode['addVLANSwitchTemplate']['ERR'] = 110;
2421function addVLANSwitchTemplate()
2422{
2423 assertStringArg ('vst_descr');
2424 global $sic;
2425 $max_local_vlans = NULL;
2426 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2427 {
2428 assertUIntArg ('vst_maxvlans');
2429 $max_local_vlans = $sic['vst_maxvlans'];
2430 }
2431 $result = usePreparedInsertBlade
2432 (
2433 'VLANSwitchTemplate',
2434 array
2435 (
2436 'max_local_vlans' => $max_local_vlans,
2437 'description' => $sic['vst_descr'],
2438 )
2439 );
2440 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2441}
2442
2443$msgcode['delVLANSwitchTemplate']['OK'] = 49;
2444$msgcode['delVLANSwitchTemplate']['ERR'] = 111;
2445function delVLANSwitchTemplate()
2446{
2447 assertUIntArg ('vst_id');
2448 global $sic;
2449 $result = FALSE !== usePreparedDeleteBlade ('VLANSwitchTemplate', array ('id' => $sic['vst_id']));
2450 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2451}
2452
2453$msgcode['updVLANSwitchTemplate']['OK'] = 51;
2454$msgcode['updVLANSwitchTemplate']['ERR'] = 109;
2455function updVLANSwitchTemplate()
2456{
2457 assertUIntArg ('vst_id');
2458 assertStringArg ('vst_descr');
2459 global $sic;
2460 $max_local_vlans = NULL;
2461 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2462 {
2463 assertUIntArg ('vst_maxvlans');
2464 $max_local_vlans = $sic['vst_maxvlans'];
2465 }
2466 $result = commitUpdateVST ($sic['vst_id'], $max_local_vlans, $sic['vst_descr']);
6cfedb71 2467 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
e0d188ef
DO
2468}
2469
09ec2e59
AA
2470$msgcode['cloneVSTRule']['OK'] = 48;
2471$msgcode['cloneVSTRule']['ERR'] = 179;
2472function cloneVSTRule()
e0d188ef 2473{
09ec2e59
AA
2474 global $dbxlink;
2475 $message = '';
09ec2e59
AA
2476 assertUIntArg ('mutex_rev', TRUE);
2477 $dst_vst = getVLANSwitchTemplate ($_REQUEST['vst_id']);
2478 if ($dst_vst['mutex_rev'] != $_REQUEST['mutex_rev'])
2479 $message = "User ${dst_vst['saved_by']} saved this template after you started to edit it. Please concern differencies";
2480 else
eb51ceff 2481 {
eb51ceff
DO
2482 assertUIntArg ('from_id');
2483 $src_vst = getVLANSwitchTemplate ($_REQUEST['from_id']);
09ec2e59
AA
2484 if (! commitUpdateVSTRules ($_REQUEST['vst_id'], $src_vst['rules']))
2485 $message = 'DB error';
eb51ceff 2486 }
09ec2e59
AA
2487 $result = !(BOOL) $message;
2488 if ($result)
2489 $message = 'Supplement succeeded';
2490 return buildWideRedirectURL (array (array ('code' => $result ? 'success' : 'error', 'message' => $message)));
e0d188ef
DO
2491}
2492
e0d188ef
DO
2493function updVSTRule()
2494{
09ec2e59 2495 global $port_role_options;
09ec2e59
AA
2496 assertUIntArg ('mutex_rev', TRUE);
2497 assertStringArg ('template_json');
2498 $message = '';
2499 $data = json_decode ($_POST['template_json'], TRUE);
2500 if (! isset ($data) or ! is_array ($data))
2501 $message = 'Invalid JSON code received from client';
2502 else
2503 {
2504 $rule_no = 0;
2505 foreach ($data as $rule)
2506 {
2507 $rule_no++;
2508 if (! isInteger ($rule['rule_no']))
2509 $message = 'Invalid rule number';
2510 elseif (! isPCRE ($rule['port_pcre']))
2511 $message = 'Invalid port regexp';
2512 elseif (! isset ($rule['port_role']) or ! array_key_exists ($rule['port_role'], $port_role_options))
2513 $message = 'Invalid port role';
2514 elseif (! isset ($rule['wrt_vlans']) or ! preg_match ('/^[ 0-9\-,]*$/', $rule['wrt_vlans']))
2515 $message = 'Invalid port vlan mask';
2516 elseif (! isset ($rule['description']))
2517 $message = 'Invalid description';
2518
2519 if ($message)
2520 {
2521 $message .= " in rule $rule_no";
2522 break;
2523 }
2524 }
2525 if (! $message)
2526 {
2527 $vst = getVLANSwitchTemplate ($_REQUEST['vst_id']);
2528 if ($vst['mutex_rev'] != $_REQUEST['mutex_rev'])
2529 $message = "User ${vst['saved_by']} saved this template after you started to edit it. Please concern differencies";
2530 }
2531 if (! $message)
2532 if (! commitUpdateVSTRules ($_REQUEST['vst_id'], $data))
2533 $message = 'DB update error';
2534 if ($message)
2535 $_SESSION['vst_edited'] = $data;
2536 }
2537 if ($message)
2538 {
2539 $result = FALSE;
2540 $message = "Template update failed: $message";
2541 }
2542 else
2543 {
2544 $result = TRUE;
2545 $message = "Update success";
2546 }
2547 return buildWideRedirectURL (array (array ('code' => $result ? 'success' : 'error', 'message' => $message)));
e0d188ef
DO
2548}
2549
5b5b1cab
DO
2550$msgcode['importDPData']['OK'] = 44;
2551function importDPData()
b3247480
DO
2552{
2553 global $sic;
2554 assertUIntArg ('nports');
2555 $nignored = $ndone = 0;
1dc057f8 2556 $POIFC = getPortOIFCompat();
b3247480
DO
2557 for ($i = 0; $i < $sic['nports']; $i++)
2558 if (array_key_exists ("do_${i}", $sic))
2559 {
2560 assertUIntArg ("pid1_${i}");
2561 assertUIntArg ("pid2_${i}");
2562 $porta = getPortInfo ($_REQUEST["pid1_${i}"]);
2563 $portb = getPortInfo ($_REQUEST["pid2_${i}"]);
2564 if
2565 (
2566 $porta['linked'] or
2567 $portb['linked'] or
2568 ($porta['object_id'] != $sic['object_id'] and $portb['object_id'] != $sic['object_id'])
2569 )
2570 {
2571 $nignored++;
2572 continue;
2573 }
1dc057f8
DO
2574 foreach ($POIFC as $item)
2575 if ($item['type1'] == $porta['oif_id'] and $item['type2'] == $portb['oif_id'])
2576 {
2577 linkPorts ($_REQUEST["pid1_${i}"], $_REQUEST["pid2_${i}"]);
2578 $ndone++;
524170a8 2579 continue 2; // next port
1dc057f8
DO
2580 }
2581 $nignored++;
b3247480
DO
2582 }
2583 return buildRedirectURL (__FUNCTION__, 'OK', array ($nignored, $ndone));
2584}
2585
9a90adc4
DO
2586$msgcode['addObjectlog']['OK'] = 0;
2587function addObjectlog ()
2588{
9a90adc4
DO
2589 assertStringArg ('logentry');
2590 global $remote_username, $sic;
2591 $oi = spotEntity ('object', $sic['object_id']);
2592 usePreparedExecuteBlade ('INSERT INTO ObjectLog SET object_id=?, user=?, date=NOW(), content=?', array ($sic['object_id'], $remote_username, $sic['logentry']));
2593 $ob_url = makeHref (array ('page' => 'object', 'tab' => 'objectlog', 'object_id' => $sic['object_id']));
2594 return buildRedirectURL (__FUNCTION__, 'OK', array ("Log entry for <a href=" . ${ob_url} . ">${oi['dname']}</a> added by ${remote_username}"));
2595}
2596
cde28cf0
DO
2597function tableHandler ($opspec)
2598{
2599 global $sic;
2600 if (!array_key_exists ('table', $opspec))
2601 throw new InvalidArgException ('opspec', '(malformed array structure)', '"table" not set');
2602 $columns = array();
2603 foreach ($opspec['arglist'] as $argspec)
2604 {
2605 genericAssertion ($argspec['url_argname'], $argspec['assertion']);
4eba4f82
DO
2606 // "table_colname" is normally used for an override, if it is not
2607 // set, use the URL argument name
2608 $table_colname = array_key_exists ('table_colname', $argspec) ?
2609 $argspec['table_colname'] :
2610 $argspec['url_argname'];
2611 $arg_value = $sic[$argspec['url_argname']];
2612 if
2613 (
2614 ($argspec['assertion'] == 'uint0' and $arg_value == 0)
2615 or ($argspec['assertion'] == 'string0' and $arg_value == '')
2616 )
2617 switch (TRUE)
2618 {
2619 case !array_key_exists ('if_empty', $argspec): // no action requested
2620 break;
2621 case $argspec['if_empty'] == 'NULL':
2622 $arg_value = NULL;
2623 break;
2624// A trick below is likely to break non-INSERT queries.
2625// case $argspec['if_empty'] == 'omit':
2626// continue 2;
2627 default:
2628 throw new InvalidArgException ('opspec', '(malformed array structure)', '"if_empty" not recognized');
2629 }
2630 $columns[$table_colname] = $arg_value;
cde28cf0
DO
2631 }
2632 switch ($opspec['action'])
2633 {
2634 case 'INSERT':
2635 $retcode = TRUE === usePreparedInsertBlade ($opspec['table'], $columns) ? 48 : 110;
2636 break;
10005279
DO
2637 case 'DELETE':
2638 $conjunction = array_key_exists ('conjunction', $opspec) ? $opspec['conjunction'] : 'AND';
2639 $retcode = FALSE !== usePreparedDeleteBlade ($opspec['table'], $columns, $conjunction) ? 49 : 111;
2640 break;
cde28cf0
DO
2641 default:
2642 throw new InvalidArgException ('opspec/action', '(malformed array structure)');
2643 }
2644 return buildWideRedirectURL (oneLiner ($retcode));
2645}
2646
e673ee24 2647?>