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