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