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