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