r4097 fixed PHP tags
[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'];
00e93d63 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;
b6a7d936
DO
1194$msgcode['addVService']['ERR1'] = 132;
1195$msgcode['addVService']['ERR2'] = 100;
d6517a21
DO
1196function addVService ()
1197{
0cc24e9a
DY
1198 assertIPv4Arg ('vip');
1199 assertUIntArg ('vport');
1200 assertStringArg ('proto');
103b1e1e 1201 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1202 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1203 assertStringArg ('name', TRUE);
1204 assertStringArg ('vsconfig', TRUE);
1205 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1206 $error = commitCreateVS
1207 (
103b1e1e
DO
1208 $_REQUEST['vip'],
1209 $_REQUEST['vport'],
7320b661 1210 $_REQUEST['proto'],
103b1e1e
DO
1211 $_REQUEST['name'],
1212 $_REQUEST['vsconfig'],
c63a8d6e
DO
1213 $_REQUEST['rsconfig'],
1214 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1215 );
1216 if ($error != '')
135080d8 1217 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1218 else
135080d8 1219 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1220}
1221
00e93d63
DO
1222$msgcode['deleteVService']['OK'] = 49;
1223$msgcode['deleteVService']['ERR'] = 111;
d6517a21
DO
1224function deleteVService ()
1225{
0cc24e9a 1226 assertUIntArg ('vs_id');
e02e1941 1227 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1228 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1229 else
135080d8 1230 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1231}
1232
1f54e1ba
DO
1233$msgcode['updateSLBDefConfig']['OK'] = 43;
1234$msgcode['updateSLBDefConfig']['ERR'] = 109;
1235function updateSLBDefConfig ()
1236{
1237 $data = array(
1238 'vs' => $_REQUEST['vsconfig'],
1239 'rs' => $_REQUEST['rsconfig']
1240 );
1241 if (!commitUpdateSLBDefConf ($data))
1242 return buildRedirectURL (__FUNCTION__, 'ERR');
1243 else
1244 return buildRedirectURL (__FUNCTION__, 'OK');
1245}
1246
00e93d63
DO
1247$msgcode['updateRealServer']['OK'] = 51;
1248$msgcode['updateRealServer']['ERR'] = 109;
fb1c4a54
DO
1249function updateRealServer ()
1250{
0cc24e9a
DY
1251 assertUIntArg ('rs_id');
1252 assertIPv4Arg ('rsip');
1253 assertStringArg ('rsport', TRUE);
1254 assertStringArg ('rsconfig', TRUE);
a5c589d2 1255 if (FALSE === commitUpdateRS (
103b1e1e
DO
1256 $_REQUEST['rs_id'],
1257 $_REQUEST['rsip'],
1258 $_REQUEST['rsport'],
1259 $_REQUEST['rsconfig']
1260 ))
135080d8 1261 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1262 else
135080d8 1263 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1264}
1265
00e93d63
DO
1266$msgcode['updateLoadBalancer']['OK'] = 51;
1267$msgcode['updateLoadBalancer']['ERR'] = 109;
c1ca768c 1268function updateLoadBalancer ()
3241551e 1269{
0cc24e9a
DY
1270 assertUIntArg ('object_id');
1271 assertUIntArg ('pool_id');
1272 assertUIntArg ('vs_id');
1273 assertStringArg ('vsconfig', TRUE);
1274 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1275 if (! empty($_REQUEST['prio']))
1276 assertUIntArg('prio', TRUE);
1277
a5c589d2 1278 if (FALSE === commitUpdateLB (
103b1e1e
DO
1279 $_REQUEST['object_id'],
1280 $_REQUEST['pool_id'],
1281 $_REQUEST['vs_id'],
1282 $_REQUEST['vsconfig'],
1f54e1ba
DO
1283 $_REQUEST['rsconfig'],
1284 $_REQUEST['prio']
103b1e1e 1285 ))
135080d8 1286 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1287 else
135080d8 1288 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1289}
1290
00e93d63
DO
1291$msgcode['updateVService']['OK'] = 51;
1292$msgcode['updateVService']['ERR'] = 109;
d6517a21
DO
1293function updateVService ()
1294{
0cc24e9a
DY
1295 assertUIntArg ('vs_id');
1296 assertIPv4Arg ('vip');
1297 assertUIntArg ('vport');
1298 assertStringArg ('proto');
1299 assertStringArg ('name', TRUE);
1300 assertStringArg ('vsconfig', TRUE);
1301 assertStringArg ('rsconfig', TRUE);
a5c589d2 1302 if (FALSE === commitUpdateVS (
103b1e1e
DO
1303 $_REQUEST['vs_id'],
1304 $_REQUEST['vip'],
1305 $_REQUEST['vport'],
1306 $_REQUEST['proto'],
1307 $_REQUEST['name'],
1308 $_REQUEST['vsconfig'],
1309 $_REQUEST['rsconfig']
1310 ))
135080d8 1311 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1312 else
135080d8 1313 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1314}
1315
00e93d63
DO
1316$msgcode['addLoadBalancer']['OK'] = 48;
1317$msgcode['addLoadBalancer']['ERR'] = 110;
3241551e
DO
1318function addLoadBalancer ()
1319{
0cc24e9a
DY
1320 assertUIntArg ('pool_id');
1321 assertUIntArg ('object_id');
1322 assertUIntArg ('vs_id');
1323 assertStringArg ('vsconfig', TRUE);
1324 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1325 if (! empty($_REQUEST['prio']))
1326 assertUIntArg('prio', TRUE);
1327
103b1e1e
DO
1328 if (!addLBtoRSPool (
1329 $_REQUEST['pool_id'],
1330 $_REQUEST['object_id'],
1331 $_REQUEST['vs_id'],
1332 $_REQUEST['vsconfig'],
1f54e1ba
DO
1333 $_REQUEST['rsconfig'],
1334 $_REQUEST['prio']
103b1e1e 1335 ))
135080d8 1336 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1337 else
135080d8 1338 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1339}
1340
00e93d63 1341$msgcode['addRSPool']['OK'] = 48;
b6a7d936 1342$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1343function addRSPool ()
1344{
0cc24e9a
DY
1345 assertStringArg ('name', TRUE);
1346 assertStringArg ('vsconfig', TRUE);
1347 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1348 $error = commitCreateRSPool
1349 (
103b1e1e
DO
1350 $_REQUEST['name'],
1351 $_REQUEST['vsconfig'],
c63a8d6e
DO
1352 $_REQUEST['rsconfig'],
1353 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1354 );
1355 if ($error != '')
135080d8 1356 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1357 else
135080d8 1358 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1359}
1360
00e93d63
DO
1361$msgcode['deleteRSPool']['OK'] = 49;
1362$msgcode['deleteRSPool']['ERR'] = 111;
5ad76f01
DO
1363function deleteRSPool ()
1364{
0cc24e9a 1365 assertUIntArg ('pool_id');
654e4636 1366 if (commitDeleteRSPool ($_REQUEST['pool_id']) === FALSE)
135080d8 1367 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1368 else
135080d8 1369 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1370}
1371
00e93d63
DO
1372$msgcode['updateRSPool']['OK'] = 51;
1373$msgcode['updateRSPool']['ERR'] = 109;
5ad76f01
DO
1374function updateRSPool ()
1375{
0cc24e9a
DY
1376 assertStringArg ('name', TRUE);
1377 assertStringArg ('vsconfig', TRUE);
1378 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1379 if (FALSE === commitUpdateRSPool
1380 (
1381 $_REQUEST['pool_id'],
1382 $_REQUEST['name'],
1383 $_REQUEST['vsconfig'],
1384 $_REQUEST['rsconfig']
1385 )
1386 )
135080d8 1387 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1388 else
135080d8 1389 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1390}
1391
00e93d63
DO
1392$msgcode['updateRSInService']['OK'] = 26;
1393$msgcode['updateRSInService']['ERR'] = 141;
1f7d18fa
DO
1394function updateRSInService ()
1395{
0cc24e9a 1396 assertUIntArg ('rscount');
841a7a7a 1397 $pool_id = $_REQUEST['pool_id'];
a6e91ac2
DO
1398 $orig = spotEntity ('ipv4rspool', $pool_id);
1399 amplifyCell ($orig);
1f7d18fa
DO
1400 $nbad = $ngood = 0;
1401 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1402 {
1403 $rs_id = $_REQUEST["rsid_${i}"];
1404 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1405 $newval = 'yes';
1406 else
1407 $newval = 'no';
1408 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1409 {
a5c589d2 1410 if (FALSE !== commitSetInService ($rs_id, $newval))
1f7d18fa
DO
1411 $ngood++;
1412 else
1413 $nbad++;
1414 }
1415 }
1416 if (!$nbad)
135080d8 1417 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1418 else
135080d8 1419 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1420}
1421
b6a7d936
DO
1422$msgcode['importPTRData']['OK'] = 26;
1423$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1424// FIXME: check, that each submitted address belongs to the prefix we
1425// are operating on.
03eb5209
DO
1426function importPTRData ()
1427{
0cc24e9a 1428 assertUIntArg ('addrcount');
03eb5209 1429 $nbad = $ngood = 0;
03eb5209
DO
1430 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1431 {
3f3bd41e 1432 $inputname = "import_${i}";
03eb5209
DO
1433 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1434 continue;
0cc24e9a
DY
1435 assertIPv4Arg ("addr_${i}");
1436 assertStringArg ("descr_${i}", TRUE);
1437 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1438 // Non-existent addresses will not have this argument set in request.
1439 $rsvd = 'no';
03eb5209
DO
1440 if ($_REQUEST["rsvd_${i}"] == 'yes')
1441 $rsvd = 'yes';
3f3bd41e 1442 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1443 $ngood++;
1444 else
1445 $nbad++;
1446 }
1447 if (!$nbad)
135080d8 1448 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1449 else
135080d8 1450 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1451}
1452
b6a7d936
DO
1453$msgcode['generateAutoPorts']['OK'] = 21;
1454$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1455function generateAutoPorts ()
1456{
103b1e1e 1457 global $pageno;
6297d584 1458 $info = spotEntity ('object', $_REQUEST['object_id']);
f3f0161f 1459 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1460 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1461 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1462 else
135080d8 1463 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1464}
1465
00e93d63 1466$msgcode['saveEntityTags']['OK'] = 26;
b6a7d936 1467$msgcode['saveEntityTags']['ERR1'] = 143;
24cbe8af 1468// Filter out implicit tags before storing the new tag set.
3355ca56 1469function saveEntityTags ()
24cbe8af 1470{
1d356026
DO
1471 global $pageno, $etype_by_pageno;
1472 if (!isset ($etype_by_pageno[$pageno]))
1473 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
be2ae2a2 1474 $realm = $etype_by_pageno[$pageno];
1d356026 1475 $entity_id = getBypassValue();
e04931c8 1476 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1477 // Build a chain from the submitted data, minimize it,
ab379543 1478 // then wipe existing records and store the new set instead.
01b6b4d6 1479 destroyTagsForEntity ($realm, $entity_id);
eb27afad
DO
1480 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1481 // so why not use it?
6e49bd1f 1482 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1483 $n_succeeds = $n_errors = 0;
6e49bd1f 1484 foreach ($newchain as $taginfo)
eb6ea26f 1485 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1486 $n_succeeds++;
1487 else
1488 $n_errors++;
ab379543 1489 if ($n_errors)
9a61c175 1490 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1491 else
135080d8 1492 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1493}
1494
00e93d63 1495$msgcode['updateTag']['OK'] = 7;
a5c589d2 1496$msgcode['updateTag']['ERR2'] = 109;
49fb1027
DO
1497function updateTag ()
1498{
0cc24e9a
DY
1499 assertUIntArg ('tag_id');
1500 assertUIntArg ('parent_id', TRUE);
00e93d63 1501 genericAssertion ('tag_name', 'tag');
49fb1027
DO
1502 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1503 $parent_id = 'NULL';
a5c589d2 1504 if (FALSE !== commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id))
135080d8 1505 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
590e1281
DO
1506 // Use old name in the message, cause update failed.
1507 global $taglist;
a5c589d2 1508 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag']));
49fb1027
DO
1509}
1510
b6a7d936
DO
1511$msgcode['rollTags']['OK'] = 67;
1512$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1513function rollTags ()
1514{
0cc24e9a
DY
1515 assertStringArg ('sum', TRUE);
1516 assertUIntArg ('realsum');
eb6ea26f 1517 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1518 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1519 // Even if the user requested an empty tag list, don't bail out, but process existing
1520 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1521 // have its chains refined to "normal" form.
1522 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1523 $n_ok = 0;
1524 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1525 // filter out the same tag again and again. It will have own noise to cancel.
1526 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
61a1d996 1527 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
abef7149 1528 {
61a1d996 1529 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
abef7149 1530 $n_ok++;
61a1d996
DO
1531 amplifyCell ($rack);
1532 foreach ($rack['mountedObjects'] as $object_id)
abef7149
DO
1533 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1534 $n_ok++;
1535 }
135080d8 1536 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1537}
1538
00e93d63 1539$msgcode['changeMyPassword']['OK'] = 51;
b6a7d936
DO
1540$msgcode['changeMyPassword']['ERR1'] = 150;
1541$msgcode['changeMyPassword']['ERR2'] = 151;
1542$msgcode['changeMyPassword']['ERR3'] = 152;
1543$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1544function changeMyPassword ()
cced6b7d 1545{
b82cce3f 1546 global $remote_username, $user_auth_src;
204284ba 1547 if ($user_auth_src != 'database')
135080d8 1548 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1549 assertStringArg ('oldpassword');
1550 assertStringArg ('newpassword1');
1551 assertStringArg ('newpassword2');
b82cce3f 1552 $remote_userid = getUserIDByUsername ($remote_username);
0b2c74cb 1553 $userinfo = spotEntity ('user', $remote_userid);
b82cce3f 1554 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1555 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1556 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1557 return buildRedirectURL (__FUNCTION__, 'ERR3');
32832c0e 1558 if (FALSE !== commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1559 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1560 else
135080d8 1561 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1562}
1563
b6a7d936
DO
1564$msgcode['saveRackCode']['OK'] = 43;
1565$msgcode['saveRackCode']['ERR1'] = 154;
1566$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1567function saveRackCode ()
1568{
cced6b7d 1569 assertStringArg ('rackcode');
e6a4adb9 1570 // For the test to succeed, unescape LFs, strip CRs.
7f42d792 1571 $newcode = dos2unix ($_REQUEST['rackcode']);
cf25e649
DO
1572 $parseTree = getRackCode ($newcode);
1573 if ($parseTree['result'] != 'ACK')
135080d8 1574 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
bb09f49c
DO
1575 if (FALSE !== saveScript ('RackCode', $newcode))
1576 {
1577 saveScript ('RackCodeCache', base64_encode (serialize ($parseTree)));
135080d8 1578 return buildRedirectURL (__FUNCTION__, 'OK');
bb09f49c
DO
1579 }
1580 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1581}
1582
b6a7d936 1583$msgcode['setPortVLAN']['ERR1'] = 156;
46f92ff7
DO
1584// This handler's context is pre-built, but not authorized. It is assumed, that the
1585// handler will take existing context and before each commit check authorization
1586// on the base chain plus necessary context added.
1587function setPortVLAN ()
1588{
0cc24e9a 1589 assertUIntArg ('portcount');
46f92ff7
DO
1590 $data = getSwitchVLANs ($_REQUEST['object_id']);
1591 if ($data === NULL)
135080d8 1592 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1593 list ($vlanlist, $portlist) = $data;
1594 // Here we just build up 1 set command for the gateway with all of the ports
1595 // included. The gateway is expected to filter unnecessary changes silently
1596 // and to provide a list of responses with either error or success message
1597 // for each of the rest.
1598 $nports = $_REQUEST['portcount'];
1599 $prefix = 'set ';
db55cf54 1600 $log = emptyLog();
46f92ff7
DO
1601 $setcmd = '';
1602 for ($i = 0; $i < $nports; $i++)
1603 if
1604 (
1605 !isset ($_REQUEST['portname_' . $i]) ||
1606 !isset ($_REQUEST['vlanid_' . $i]) ||
1607 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1608 )
24dcb9d8 1609 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1610 elseif
1611 (
1612 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1613 $portlist[$i]['vlaind'] == 'TRUNK'
1614 )
1615 continue;
1616 else
1617 {
1618 $portname = $_REQUEST['portname_' . $i];
1619 $oldvlanid = $portlist[$i]['vlanid'];
1620 $newvlanid = $_REQUEST['vlanid_' . $i];
647635ad
DO
1621 if
1622 (
1623 !permitted (NULL, NULL, NULL, array (array ('tag' => '$fromvlan_' . $oldvlanid))) or
1624 !permitted (NULL, NULL, NULL, array (array ('tag' => '$tovlan_' . $newvlanid)))
1625 )
46f92ff7 1626 {
24dcb9d8 1627 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1628 continue;
1629 }
1630 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1631 $prefix = ';';
1632 }
1633 // Feed the gateway and interpret its (non)response.
1634 if ($setcmd != '')
f0ff4930 1635 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1636 else
24dcb9d8 1637 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1638 return buildWideRedirectURL ($log);
1639}
1640
12b0c847
DO
1641$msgcode['submitSLBConfig']['OK'] = 66;
1642$msgcode['submitSLBConfig']['ERR'] = 164;
2987fc1f
DO
1643function submitSLBConfig ()
1644{
2987fc1f 1645 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
12b0c847
DO
1646 try
1647 {
1648 gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
1649 }
3a089a44 1650 catch (RTGatewayError $e)
12b0c847 1651 {
3a089a44 1652 return buildRedirectURL (__FUNCTION__, 'ERR', array ($e->getMessage()));
12b0c847
DO
1653 }
1654 return buildRedirectURL (__FUNCTION__, 'OK', array ('slbconfig'));
2987fc1f
DO
1655}
1656
00e93d63 1657$msgcode['updateRow']['OK'] = 7;
b6a7d936 1658$msgcode['updateRow']['ERR'] = 100;
345fd640
AD
1659function updateRow ()
1660{
0cc24e9a
DY
1661 assertUIntArg ('row_id');
1662 assertStringArg ('name');
345fd640 1663
39eadd27 1664 if (FALSE !== commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1665 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1666 else
135080d8 1667 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1668}
1669
00e93d63 1670$msgcode['addRack']['OK'] = 51;
b6a7d936 1671$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1672function addRack ()
1673{
f19c75d6
DO
1674 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1675 if (isset ($_REQUEST['got_data']))
1676 {
0cc24e9a
DY
1677 assertStringArg ('rack_name');
1678 assertUIntArg ('rack_height1');
1679 assertStringArg ('rack_comment', TRUE);
00e93d63
DO
1680 commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist);
1681 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6
DO
1682 }
1683 elseif (isset ($_REQUEST['got_mdata']))
1684 {
0cc24e9a
DY
1685 assertUIntArg ('rack_height2');
1686 assertStringArg ('rack_names', TRUE);
db55cf54 1687 $log = emptyLog();
f19c75d6 1688 // copy-and-paste from renderAddMultipleObjectsForm()
7f42d792 1689 $names1 = explode ("\n", $_REQUEST['rack_names']);
f19c75d6
DO
1690 $names2 = array();
1691 foreach ($names1 as $line)
1692 {
1693 $parts = explode ('\r', $line);
1694 reset ($parts);
59a83bd8 1695 if (!strlen ($parts[0]))
f19c75d6
DO
1696 continue;
1697 else
1698 $names2[] = rtrim ($parts[0]);
1699 }
b3029d5c 1700 global $msgcode;
f19c75d6
DO
1701 foreach ($names2 as $cname)
1702 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1703 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1704 else
b3029d5c 1705 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1706 return buildWideRedirectURL ($log);
1707 }
1708 else
135080d8 1709 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1710}
1711
00e93d63 1712$msgcode['deleteRack']['OK'] = 6;
b6a7d936
DO
1713$msgcode['deleteRack']['ERR'] = 100;
1714$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1715function deleteRack ()
1716{
0cc24e9a 1717 assertUIntArg ('rack_id');
61a1d996
DO
1718 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1719 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1720 amplifyCell ($rackData);
1721 if (count ($rackData['mountedObjects']))
c8187437 1722 return buildRedirectURL (__FUNCTION__, 'ERR1');
61a1d996
DO
1723 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1724 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1725 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
c8187437
DY
1726}
1727
00e93d63
DO
1728$msgcode['updateRack']['OK'] = 7;
1729$msgcode['updateRack']['ERR'] = 109;
7056988c
DO
1730function updateRack ()
1731{
0cc24e9a
DY
1732 assertUIntArg ('rack_row_id');
1733 assertUIntArg ('rack_height');
1734 assertStringArg ('rack_name');
1735 assertStringArg ('rack_comment', TRUE);
7056988c 1736
7056988c 1737 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1738 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1739 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1740 else
135080d8 1741 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1742}
1743
4fbb5a00
DY
1744function updateRackDesign ()
1745{
6cfedb71 1746 $rackData = spotEntity ('rack', $_REQUEST['rack_id']);
61a1d996 1747 amplifyCell ($rackData);
4fbb5a00
DY
1748 applyRackDesignMask($rackData);
1749 markupObjectProblems ($rackData);
1750 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1751 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1752}
1753
1754function updateRackProblems ()
1755{
6cfedb71 1756 $rackData = spotEntity ('rack', $_REQUEST['rack_id']);
61a1d996 1757 amplifyCell ($rackData);
4fbb5a00
DY
1758 applyRackProblemMask($rackData);
1759 markupObjectProblems ($rackData);
1760 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1761 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1762}
1763
7056988c
DO
1764function querySNMPData ()
1765{
8228bdb4
DO
1766 assertStringArg ('community', TRUE);
1767
1768 $snmpsetup = array ();
1769 if ( strlen($_REQUEST['community']) == 0 ) {
1770 assertStringArg ('sec_name');
1771 assertStringArg ('sec_level');
1772 assertStringArg ('auth_protocol');
1773 assertStringArg ('auth_passphrase', TRUE);
1774 assertStringArg ('priv_protocol');
1775 assertStringArg ('priv_passphrase', TRUE);
1776
1777 $snmpsetup['sec_name'] = $_REQUEST['sec_name'];
1778 $snmpsetup['sec_level'] = $_REQUEST['sec_level'];
1779 $snmpsetup['auth_protocol'] = $_REQUEST['auth_protocol'];
1780 $snmpsetup['auth_passphrase'] = $_REQUEST['auth_passphrase'];
1781 $snmpsetup['priv_protocol'] = $_REQUEST['priv_protocol'];
1782 $snmpsetup['priv_passphrase'] = $_REQUEST['priv_passphrase'];
1783 }
1784 else {
1785 $snmpsetup['community'] = $_REQUEST['community'];
1786 }
1787
1788
1789 return doSNMPmining ($_REQUEST['object_id'], $snmpsetup);
7056988c
DO
1790}
1791
00e93d63 1792$msgcode['addFileWithoutLink']['OK'] = 5;
6cfedb71 1793$msgcode['addFileWithoutLink']['ERR2'] = 110;
e1ae3fb4
AD
1794// File-related functions
1795function addFileWithoutLink ()
1796{
0cc24e9a 1797 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1798
1799 // Make sure the file can be uploaded
1800 if (get_cfg_var('file_uploads') != 1)
00e93d63 1801 throw new RackTablesError ('file uploads not allowed, change "file_uploads" parameter in php.ini', RackTablesError::MISCONFIGURED);
e1ae3fb4
AD
1802
1803 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650 1804 global $sic;
6cfedb71
DO
1805 if (FALSE === commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']))
1806 return buildRedirectURL (__FUNCTION__, 'ERR2');
f857f71f
DO
1807 if (isset ($_REQUEST['taglist']))
1808 produceTagsForLastRecord ('file', $_REQUEST['taglist']);
6cfedb71 1809 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($_FILES['file']['name'])));
e1ae3fb4
AD
1810}
1811
00e93d63 1812$msgcode['addFileToEntity']['OK'] = 5;
b6a7d936 1813$msgcode['addFileToEntity']['ERR2'] = 181;
6cfedb71 1814$msgcode['addFileToEntity']['ERR3'] = 110;
e1ae3fb4
AD
1815function addFileToEntity ()
1816{
1d356026
DO
1817 global $pageno, $etype_by_pageno;
1818 if (!isset ($etype_by_pageno[$pageno]))
1819 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
9a61c175 1820 $realm = $etype_by_pageno[$pageno];
1d356026 1821 $entity_id = getBypassValue();
0cc24e9a 1822 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1823
1824 // Make sure the file can be uploaded
1825 if (get_cfg_var('file_uploads') != 1)
9a61c175 1826 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
1827
1828 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650 1829 global $sic;
6cfedb71
DO
1830 if (FALSE === commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']))
1831 return buildRedirectURL (__FUNCTION__, 'ERR3');
1832 if (FALSE === commitLinkFile (lastInsertID(), $realm, $entity_id))
1833 return buildRedirectURL (__FUNCTION__, 'ERR3');
e1ae3fb4 1834
6cfedb71 1835 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($_FILES['file']['name'])));
e1ae3fb4
AD
1836}
1837
b6a7d936 1838$msgcode['linkFileToEntity']['OK'] = 71;
6cfedb71 1839$msgcode['linkFileToEntity']['ERR2'] = 110;
e1ae3fb4
AD
1840function linkFileToEntity ()
1841{
0cc24e9a 1842 assertUIntArg ('file_id');
1d356026
DO
1843 global $pageno, $etype_by_pageno;
1844 if (!isset ($etype_by_pageno[$pageno]))
1845 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
121496b6 1846
d3b5008b 1847 $fi = spotEntity ('file', $_REQUEST['file_id']);
1d356026 1848 if (FALSE === commitLinkFile ($_REQUEST['file_id'], $etype_by_pageno[$pageno], getBypassValue()))
6cfedb71 1849 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4 1850
6cfedb71 1851 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($fi['name'])));
e1ae3fb4
AD
1852}
1853
00e93d63 1854$msgcode['replaceFile']['OK'] = 7;
b6a7d936
DO
1855$msgcode['replaceFile']['ERR1'] = 181;
1856$msgcode['replaceFile']['ERR2'] = 207;
6cfedb71 1857$msgcode['replaceFile']['ERR3'] = 109;
fd1fb420
AD
1858function replaceFile ()
1859{
db55cf54 1860 global $sic;
fd1fb420
AD
1861
1862 // Make sure the file can be uploaded
1863 if (get_cfg_var('file_uploads') != 1)
db55cf54 1864 return buildRedirectURL (__FUNCTION__, 'ERR1');
d3b5008b 1865 $shortInfo = spotEntity ('file', $sic['file_id']);
fd1fb420
AD
1866
1867 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1868 if ($fp === FALSE)
1869 return buildRedirectURL (__FUNCTION__, 'ERR2');
6cfedb71
DO
1870 if (FALSE === commitReplaceFile ($sic['file_id'], $fp))
1871 return buildRedirectURL (__FUNCTION__, 'ERR3');
fd1fb420 1872
d3346ce2
DO
1873 usePreparedExecuteBlade
1874 (
1875 'UPDATE File SET thumbnail = NULL WHERE id = ?',
1876 $sic['file_id']
1877 );
1878
db55cf54 1879 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1880}
1881
b6a7d936 1882$msgcode['updateFile']['OK'] = 70;
6cfedb71 1883$msgcode['updateFile']['ERR'] = 109;
e1ae3fb4
AD
1884function updateFile ()
1885{
0cc24e9a
DY
1886 assertStringArg ('file_name');
1887 assertStringArg ('file_type');
1888 assertStringArg ('file_comment', TRUE);
f3d274bf 1889 global $sic;
6cfedb71
DO
1890 if (FALSE === commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']))
1891 return buildRedirectURL (__FUNCTION__, 'ERR');
7221c918 1892 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['file_name']));
e1ae3fb4
AD
1893}
1894
b6a7d936 1895$msgcode['unlinkFile']['OK'] = 72;
00e93d63 1896$msgcode['unlinkFile']['ERR'] = 111;
e1ae3fb4
AD
1897function unlinkFile ()
1898{
0cc24e9a 1899 assertUIntArg ('link_id');
00e93d63 1900 return buildRedirectURL (__FUNCTION__, commitUnlinkFile ($_REQUEST['link_id']) === FALSE ? 'ERR' : 'OK');
e1ae3fb4
AD
1901}
1902
00e93d63 1903$msgcode['deleteFile']['OK'] = 6;
b6a7d936 1904$msgcode['deleteFile']['ERR'] = 100;
e1ae3fb4
AD
1905function deleteFile ()
1906{
0cc24e9a 1907 assertUIntArg ('file_id');
d3b5008b 1908 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
e1ae3fb4
AD
1909 $error = commitDeleteFile ($_REQUEST['file_id']);
1910
1911 if ($error != '')
135080d8 1912 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1913
f8874cdb 1914 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1915}
1916
00e93d63 1917$msgcode['updateFileText']['OK'] = 7;
b6a7d936 1918$msgcode['updateFileText']['ERR1'] = 179;
00e93d63 1919$msgcode['updateFileText']['ERR2'] = 155;
8628ae44
DO
1920function updateFileText ()
1921{
0cc24e9a
DY
1922 assertStringArg ('mtime_copy');
1923 assertStringArg ('file_text', TRUE); // it's Ok to save empty
d3b5008b 1924 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
70cb9b56
DO
1925 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1926 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb 1927 global $sic;
6cfedb71
DO
1928 if (FALSE === commitReplaceFile ($sic['file_id'], $sic['file_text']))
1929 return buildRedirectURL (__FUNCTION__, 'ERR2');
1930 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
8628ae44
DO
1931}
1932
2dfa1b73
DO
1933$msgcode['addPortInterfaceCompat']['OK'] = 48;
1934$msgcode['addPortInterfaceCompat']['ERR'] = 110;
1935function addPortInterfaceCompat ()
1936{
0cc24e9a
DY
1937 assertUIntArg ('iif_id');
1938 assertUIntArg ('oif_id');
2dfa1b73
DO
1939 if (commitSupplementPIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
1940 return buildRedirectURL (__FUNCTION__, 'OK');
1941 return buildRedirectURL (__FUNCTION__, 'ERR');
1942}
1943
2400d7ec
DO
1944$ifcompatpack = array
1945(
1946 '1000cwdm80' => array (1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216),
1947 '1000dwdm80' => array // ITU channels 20~61
1948 (
1949 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226,
1950 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236,
1951 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
1952 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
1953 1257, 1258
1954 ),
1955 '10000dwdm80' => array // same channels for 10GE
1956 (
1957 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268,
1958 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278,
1959 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
1960 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298,
1961 1299, 1300
1962 ),
1963);
1964
3d9ac3d4 1965$msgcode['addPortInterfaceCompatPack']['OK'] = 44;
2400d7ec
DO
1966function addPortInterfaceCompatPack ()
1967{
00e93d63
DO
1968 genericAssertion ('standard', 'enum/wdmstd');
1969 genericAssertion ('iif_id', 'iif');
3d9ac3d4 1970 global $ifcompatpack;
3d9ac3d4
DO
1971 $ngood = $nbad = 0;
1972 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
1973 if (commitSupplementPIC ($_REQUEST['iif_id'], $oif_id))
1974 $ngood++;
1975 else
1976 $nbad++;
1977 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
1978}
1979
3d9ac3d4
DO
1980$msgcode['delPortInterfaceCompatPack']['OK'] = 44;
1981$msgcode['delPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
1982function delPortInterfaceCompatPack ()
1983{
0cc24e9a
DY
1984 assertStringArg ('standard');
1985 assertUIntArg ('iif_id');
10005279
DO
1986 global $ifcompatpack, $sic;
1987 if (!array_key_exists ($sic['standard'], $ifcompatpack) or !array_key_exists ($sic['iif_id'], getPortIIFOptions()))
3d9ac3d4
DO
1988 return buildRedirectURL (__FUNCTION__, 'ERR');
1989 $ngood = $nbad = 0;
10005279
DO
1990 foreach ($ifcompatpack[$sic['standard']] as $oif_id)
1991 if (usePreparedDeleteBlade ('PortInterfaceCompat', array ('iif_id' => $sic['iif_id'], 'oif_id' => $oif_id)))
3d9ac3d4
DO
1992 $ngood++;
1993 else
1994 $nbad++;
1995 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
1996}
1997
38cd7704 1998$msgcode['add8021QOrder']['OK'] = 48;
00e93d63 1999$msgcode['add8021QOrder']['ERR'] = 110;
38cd7704 2000function add8021QOrder ()
8198f2c6 2001{
80ec1f4f
DO
2002 assertUIntArg ('vdom_id');
2003 assertUIntArg ('object_id');
38cd7704 2004 assertUIntArg ('vst_id');
c1aa3ada
DO
2005 global $sic;
2006 $result = usePreparedExecuteBlade
8198f2c6 2007 (
c1aa3ada
DO
2008 'INSERT INTO VLANSwitch (domain_id, object_id, template_id, last_change, out_of_sync) ' .
2009 'VALUES (?, ?, ?, NOW(), "yes")',
2010 array ($sic['vdom_id'], $sic['object_id'], $sic['vst_id'])
8198f2c6 2011 );
c1aa3ada 2012 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
8198f2c6
DO
2013}
2014
38cd7704 2015$msgcode['del8021QOrder']['OK'] = 49;
00e93d63 2016$msgcode['del8021QOrder']['ERR'] = 111;
38cd7704 2017function del8021QOrder ()
8198f2c6 2018{
80ec1f4f 2019 assertUIntArg ('object_id');
4191d7ab
AA
2020 assertUIntArg ('vdom_id');
2021 assertUIntArg ('vst_id');
8198f2c6 2022 global $sic;
38cd7704 2023 $result = usePreparedDeleteBlade ('VLANSwitch', array ('object_id' => $sic['object_id']));
4191d7ab
AA
2024 $focus_hints = array
2025 (
2026 'prev_objid' => $_REQUEST['object_id'],
2027 'prev_vstid' => $_REQUEST['vst_id'],
2028 'prev_vdid' => $_REQUEST['vdom_id'],
2029 );
2030 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR', array(), NULL, NULL, $focus_hints);
8198f2c6
DO
2031}
2032
8198f2c6 2033$msgcode['delVLANDescription']['OK'] = 49;
c48f2e18
DO
2034$msgcode['delVLANDescription']['ERR1'] = 105;
2035$msgcode['delVLANDescription']['ERR2'] = 111;
8198f2c6
DO
2036function delVLANDescription ()
2037{
80ec1f4f 2038 assertUIntArg ('vlan_id');
8198f2c6 2039 global $sic;
c48f2e18 2040 if ($sic['vlan_id'] == VLAN_DFL_ID)
048b0420 2041 return buildRedirectURL (__FUNCTION__, 'ERR1');
8198f2c6 2042 $result = commitReduceVLANDescription ($sic['vdom_id'], $sic['vlan_id']);
c48f2e18 2043 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR2');
8198f2c6
DO
2044}
2045
2046$msgcode['updVLANDescription']['OK'] = 51;
c48f2e18
DO
2047$msgcode['updVLANDescription']['ERR1'] = 105;
2048$msgcode['updVLANDescription']['ERR2'] = 109;
8198f2c6
DO
2049function updVLANDescription ()
2050{
80ec1f4f
DO
2051 assertUIntArg ('vlan_id');
2052 assertStringArg ('vlan_type');
2053 assertStringArg ('vlan_descr', TRUE);
8198f2c6 2054 global $sic;
c48f2e18 2055 if ($sic['vlan_id'] == VLAN_DFL_ID)
048b0420 2056 return buildRedirectURL (__FUNCTION__, 'ERR1');
8198f2c6
DO
2057 $result = commitUpdateVLANDescription
2058 (
2059 $sic['vdom_id'],
2060 $sic['vlan_id'],
0dabdc53 2061 $sic['vlan_type'],
8198f2c6
DO
2062 $sic['vlan_descr']
2063 );
6cfedb71 2064 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR2');
8198f2c6
DO
2065}
2066
2067$msgcode['createVLANDomain']['OK'] = 48;
2068$msgcode['createVLANDomain']['ERR'] = 110;
2069function createVLANDomain ()
2070{
80ec1f4f 2071 assertStringArg ('vdom_descr');
8198f2c6
DO
2072 global $sic;
2073 $result = usePreparedInsertBlade
2074 (
2075 'VLANDomain',
2076 array
2077 (
2078 'description' => $sic['vdom_descr'],
2079 )
2080 );
10c7f49e
DO
2081 $result = $result and usePreparedInsertBlade
2082 (
2083 'VLANDescription',
2084 array
2085 (
2086 'domain_id' => lastInsertID(),
2087 'vlan_id' => VLAN_DFL_ID,
2088 'vlan_type' => 'compulsory',
c48f2e18 2089 'vlan_descr' => 'default',
10c7f49e
DO
2090 )
2091 );
8198f2c6
DO
2092 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2093}
2094
2095$msgcode['destroyVLANDomain']['OK'] = 49;
2096$msgcode['destroyVLANDomain']['ERR'] = 111;
2097function destroyVLANDomain ()
2098{
80ec1f4f 2099 assertUIntArg ('vdom_id');
8198f2c6 2100 global $sic;
bbae3611 2101 $result = FALSE !== usePreparedDeleteBlade ('VLANDomain', array ('id' => $sic['vdom_id']));
8198f2c6
DO
2102 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2103}
2104
2105$msgcode['updateVLANDomain']['OK'] = 51;
2106$msgcode['updateVLANDomain']['ERR'] = 109;
2107function updateVLANDomain ()
2108{
80ec1f4f
DO
2109 assertUIntArg ('vdom_id');
2110 assertStringArg ('vdom_descr');
8198f2c6 2111 global $sic;
d3192636 2112 $result = FALSE !== commitUpdateVLANDomain ($sic['vdom_id'], $sic['vdom_descr']);
8198f2c6
DO
2113 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2114}
2115
4492050b
DO
2116$msgcode['save8021QPorts']['OK1'] = 63;
2117$msgcode['save8021QPorts']['OK2'] = 41;
706d9175
DO
2118$msgcode['save8021QPorts']['ERR2'] = 109;
2119function save8021QPorts ()
8198f2c6 2120{
a7492e95 2121 global $sic, $dbxlink;
70cff23f 2122 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de8aa722
DO
2123 assertStringArg ('form_mode');
2124 if ($sic['form_mode'] != 'save' and $sic['form_mode'] != 'duplicate')
2125 throw new InvalidRequestArgException ('form_mode', $sic['form_mode']);
2126 $extra = array();
a7492e95 2127 $dbxlink->beginTransaction();
2996cbbb
DO
2128 try
2129 {
a7492e95
DO
2130 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
2131 throw new InvalidArgException ('object_id', $object_id, 'VLAN domain is not set for this object');
9c45ea37 2132 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
1a43ce88 2133 throw new InvalidRequestArgException ('mutex_rev', $sic['mutex_rev'], 'expired form data');
de8aa722 2134 $after = $before = apply8021QOrder ($vswitch['template_id'], getStored8021QConfig ($sic['object_id'], 'desired'));
af204724 2135 $changes = array();
de8aa722 2136 switch ($sic['form_mode'])
706d9175 2137 {
de8aa722
DO
2138 case 'save':
2139 assertUIntArg ('nports');
2140 if ($sic['nports'] == 1)
706d9175 2141 {
de8aa722
DO
2142 assertStringArg ('pn_0');
2143 $extra = array ('port_name' => $sic['pn_0']);
2144 }
2145 for ($i = 0; $i < $sic['nports']; $i++)
2146 {
2147 assertStringArg ('pn_' . $i);
2148 assertStringArg ('pm_' . $i);
2149 // An access port only generates form input for its native VLAN,
2150 // which we derive allowed VLAN list from.
2151 $native = isset ($sic['pnv_' . $i]) ? $sic['pnv_' . $i] : 0;
2152 switch ($sic["pm_${i}"])
2153 {
2154 case 'trunk':
b36fc896 2155# assertArrayArg ('pav_' . $i);
de8aa722
DO
2156 $allowed = isset ($sic['pav_' . $i]) ? $sic['pav_' . $i] : array();
2157 break;
2158 case 'access':
2159 if ($native == 'same')
2160 continue 2;
2161 assertUIntArg ('pnv_' . $i);
2162 $allowed = array ($native);
2163 break;
2164 default:
2165 throw new InvalidRequestArgException ("pm_${i}", $_REQUEST["pm_${i}"], 'unknown port mode');
2166 }
2167 $changes[$sic['pn_' . $i]] = array
2168 (
2169 'mode' => $sic['pm_' . $i],
2170 'allowed' => $allowed,
2171 'native' => $native,
2172 );
706d9175 2173 }
de8aa722
DO
2174 break;
2175 case 'duplicate':
2176 assertStringArg ('from_port');
2177# assertArrayArg ('to_ports');
2178 if (!array_key_exists ($sic['from_port'], $before))
2179 throw new InvalidArgException ('from_port', $sic['from_port'], 'this port does not exist');
2180 foreach ($sic['to_ports'] as $tpn)
2181 if (!array_key_exists ($tpn, $before))
2182 throw new InvalidArgException ('to_ports[]', $tpn, 'this port does not exist');
2183 elseif ($tpn != $sic['from_port'])
2184 $changes[$tpn] = $before[$sic['from_port']];
2185 break;
706d9175 2186 }
af204724
DO
2187 $domain_vlanlist = getDomainVLANs ($vswitch['domain_id']);
2188 $changes = filter8021QChangeRequests
a7492e95 2189 (
af204724 2190 $domain_vlanlist,
9165e8f7 2191 $before,
af204724 2192 apply8021QOrder ($vswitch['template_id'], $changes)
a7492e95 2193 );
bb2024b9 2194 $changes = authorize8021QChangeRequests ($before, $changes);
af204724
DO
2195 foreach ($changes as $port_name => $port)
2196 $after[$port_name] = $port;
b3a27170 2197 $new_uplinks = filter8021QChangeRequests ($domain_vlanlist, $after, produceUplinkPorts ($domain_vlanlist, $after));
7455e4c0 2198 $npulled = replace8021QPorts ('desired', $vswitch['object_id'], $before, $changes);
b3a27170 2199 $nsaved_uplinks = replace8021QPorts ('desired', $vswitch['object_id'], $before, $new_uplinks);
2996cbbb
DO
2200 }
2201 catch (Exception $e)
861292ee 2202 {
a7492e95 2203 $dbxlink->rollBack();
706d9175 2204 return buildRedirectURL (__FUNCTION__, 'ERR2', array(), NULL, NULL, $extra);
861292ee 2205 }
b3a27170 2206 if ($npulled + $nsaved_uplinks)
c1aa3ada
DO
2207 $result = usePreparedExecuteBlade
2208 (
2209 'UPDATE VLANSwitch SET mutex_rev=mutex_rev+1, last_change=NOW(), out_of_sync="yes" WHERE object_id=?',
2210 array ($sic['object_id'])
2211 );
a7492e95 2212 $dbxlink->commit();
4492050b 2213 $log = oneLiner (63, array ($npulled + $nsaved_uplinks));
b3a27170 2214 if ($nsaved_uplinks)
4492050b 2215 {
b3a27170 2216 initiateUplinksReverb ($vswitch['object_id'], $new_uplinks);
4492050b
DO
2217 $log = mergeLogs ($log, oneLiner (41));
2218 }
2219 if ($npulled + $nsaved_uplinks > 0 and getConfigVar ('8021Q_INSTANT_DEPLOY') == 'yes')
2220 {
2221 try
2222 {
2223 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], TRUE))
2224 $log = mergeLogs ($log, oneLiner (191));
2225 else
2226 $log = mergeLogs ($log, oneLiner (63, array ($done)));
2227 }
2228 catch (Exception $e)
2229 {
2230 $log = mergeLogs ($log, oneLiner (109));
2231 }
2232 }
2233 return buildWideRedirectURL ($log, NULL, NULL, $extra);
8198f2c6
DO
2234}
2235
8846b060
DO
2236$msgcode['bindVLANtoIPv4']['OK'] = 48;
2237$msgcode['bindVLANtoIPv4']['ERR'] = 110;
2238function bindVLANtoIPv4 ()
2239{
2240 assertUIntArg ('id'); // network id
2241 global $sic;
2242 $result = commitSupplementVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2243 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2244}
2245
21ee3351
AA
2246$msgcode['bindVLANtoIPv6']['OK'] = 48;
2247$msgcode['bindVLANtoIPv6']['ERR'] = 110;
2248function bindVLANtoIPv6 ()
2249{
2250 assertUIntArg ('id'); // network id
2251 global $sic;
2252 $result = commitSupplementVLANIPv6 ($sic['vlan_ck'], $_REQUEST['id']);
2253 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2254}
2255
8846b060
DO
2256$msgcode['unbindVLANfromIPv4']['OK'] = 49;
2257$msgcode['unbindVLANfromIPv4']['ERR'] = 111;
2258function unbindVLANfromIPv4 ()
2259{
2260 assertUIntArg ('id'); // network id
2261 global $sic;
2262 $result = commitReduceVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2263 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2264}
2265
21ee3351
AA
2266$msgcode['unbindVLANfromIPv6']['OK'] = 49;
2267$msgcode['unbindVLANfromIPv6']['ERR'] = 111;
2268function unbindVLANfromIPv6 ()
2269{
2270 assertUIntArg ('id'); // network id
2271 global $sic;
2272 $result = commitReduceVLANIPv6 ($sic['vlan_ck'], $sic['id']);
2273 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2274}
2275
228c430c 2276$msgcode['process8021QSyncRequest']['OK'] = 63;
ec523868 2277$msgcode['process8021QSyncRequest']['ERR'] = 191;
228c430c 2278function process8021QSyncRequest ()
be28b696 2279{
2e84a05b
DO
2280 // behave depending on current operation: exec8021QPull or exec8021QPush
2281 global $sic, $op;
ec523868
DO
2282 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], $op == 'exec8021QPush'))
2283 return buildRedirectURL (__FUNCTION__, 'ERR');
2284 return buildRedirectURL (__FUNCTION__, 'OK', array ($done));
be28b696
DO
2285}
2286
f9428bc6
AA
2287$msgcode['process8021QRecalcRequest']['CHANGED'] = 87;
2288$msgcode['process8021QRecalcRequest']['NO_CHANGES'] = 300;
00e93d63 2289$msgcode['process8021QRecalcRequest']['ERR'] = 157;
f9428bc6
AA
2290function process8021QRecalcRequest ()
2291{
f9428bc6 2292 global $sic;
fb3e3731
AA
2293 if (! permitted (NULL, NULL, NULL, array (array ('tag' => '$op_recalc8021Q'))))
2294 return buildRedirectURL (__FUNCTION__, 'ERR');
f9428bc6
AA
2295 $counters = recalc8021QPorts ($sic['object_id']);
2296 if ($counters['ports'])
2297 return buildRedirectURL (__FUNCTION__, 'CHANGED', array ($counters['ports'], $counters['switches']));
2298 else
2299 return buildRedirectURL (__FUNCTION__, 'NO_CHANGES', array ('No changes were made'));
2300}
2301
be28b696 2302$msgcode['resolve8021QConflicts']['OK'] = 63;
bcd14540
DO
2303$msgcode['resolve8021QConflicts']['ERR1'] = 179;
2304$msgcode['resolve8021QConflicts']['ERR2'] = 109;
be28b696 2305function resolve8021QConflicts ()
07de6bb3 2306{
a7492e95 2307 global $sic, $dbxlink;
70cff23f 2308 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de47b574
DO
2309 assertUIntArg ('nrows');
2310 // Divide submitted radio buttons into 3 groups:
19d1e731 2311 // left (saved version wins)
07de6bb3 2312 // asis (ignore)
19d1e731
DO
2313 // right (running version wins)
2314 $F = array();
339534a0 2315 for ($i = 0; $i < $sic['nrows']; $i++)
07de6bb3 2316 {
de47b574 2317 if (!array_key_exists ("i_${i}", $sic))
07de6bb3 2318 continue;
de47b574
DO
2319 // let's hope other inputs are in place
2320 switch ($sic["i_${i}"])
07de6bb3
DO
2321 {
2322 case 'left':
07de6bb3 2323 case 'right':
bcd14540 2324 $F[$sic["pn_${i}"]] = array
07de6bb3 2325 (
ca9e1c9d 2326 'mode' => $sic["rm_${i}"],
de47b574
DO
2327 'allowed' => $sic["ra_${i}"],
2328 'native' => $sic["rn_${i}"],
bcd14540 2329 'decision' => $sic["i_${i}"],
07de6bb3
DO
2330 );
2331 break;
2332 default:
2333 // don't care
2334 }
2335 }
a7492e95 2336 $dbxlink->beginTransaction();
07de6bb3
DO
2337 try
2338 {
a7492e95 2339 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
ec4d604c 2340 throw new InvalidArgException ('object_id', $sic['object_id'], 'VLAN domain is not set for this object');
9c45ea37 2341 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
1a43ce88 2342 throw new InvalidRequestArgException ('mutex_rev', $sic['mutex_rev'], 'expired form (table data has changed)');
9c2201ba
DO
2343 $D = getStored8021QConfig ($vswitch['object_id'], 'desired');
2344 $C = getStored8021QConfig ($vswitch['object_id'], 'cached');
2345 $R = getRunning8021QConfig ($vswitch['object_id']);
d5e306b2 2346 $plan = get8021QSyncOptions ($vswitch, $D, $C, $R['portdata']);
bcd14540
DO
2347 $ndone = 0;
2348 foreach ($F as $port_name => $port)
2349 {
9c2201ba
DO
2350 if (!array_key_exists ($port_name, $plan))
2351 continue;
2352 elseif ($plan[$port_name]['status'] == 'merge_conflict')
bcd14540 2353 {
9c2201ba
DO
2354 // for R neither mutex nor revisions can be emulated, but revision change can be
2355 if (!same8021QConfigs ($port, $R['portdata'][$port_name]))
1a43ce88 2356 throw new InvalidRequestArgException ("port ${port_name}", '(hidden)', 'expired form (switch data has changed)');
1f8f67c2 2357 if ($port['decision'] == 'right') // D wins, frame R by writing value of R to C
9c2201ba 2358 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $port);
1f8f67c2 2359 elseif ($port['decision'] == 'left') // R wins, cross D up
9c2201ba
DO
2360 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $D[$port_name]);
2361 // otherwise there was no decision made
2362 }
1f8f67c2
DO
2363 elseif
2364 (
2365 $plan[$port_name]['status'] == 'delete_conflict' or
2366 $plan[$port_name]['status'] == 'martian_conflict'
2367 )
9c2201ba 2368 {
1f8f67c2 2369 if ($port['decision'] == 'left') // confirm deletion of local copy
9c2201ba 2370 $ndone += del8021QPort ($vswitch['object_id'], $port_name);
bcd14540 2371 }
9c2201ba 2372 // otherwise ignore a decision, which doesn't address a conflict
bcd14540 2373 }
07de6bb3 2374 }
1a43ce88 2375 catch (InvalidRequestArgException $e)
07de6bb3 2376 {
a7492e95 2377 $dbxlink->rollBack();
bcd14540 2378 return buildRedirectURL (__FUNCTION__, 'ERR1');
ec4d604c 2379 }
bcd14540 2380 catch (Exception $e)
ec4d604c 2381 {
bcd14540
DO
2382 $dbxlink->rollBack();
2383 return buildRedirectURL (__FUNCTION__, 'ERR2');
ec4d604c 2384 }
a7492e95 2385 $dbxlink->commit();
bcd14540 2386 return buildRedirectURL (__FUNCTION__, 'OK', array ($ndone));
07de6bb3
DO
2387}
2388
e0d188ef
DO
2389$msgcode['addVLANSwitchTemplate']['OK'] = 48;
2390$msgcode['addVLANSwitchTemplate']['ERR'] = 110;
2391function addVLANSwitchTemplate()
2392{
2393 assertStringArg ('vst_descr');
2394 global $sic;
2395 $max_local_vlans = NULL;
2396 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2397 {
2398 assertUIntArg ('vst_maxvlans');
2399 $max_local_vlans = $sic['vst_maxvlans'];
2400 }
2401 $result = usePreparedInsertBlade
2402 (
2403 'VLANSwitchTemplate',
2404 array
2405 (
2406 'max_local_vlans' => $max_local_vlans,
2407 'description' => $sic['vst_descr'],
2408 )
2409 );
2410 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2411}
2412
2413$msgcode['delVLANSwitchTemplate']['OK'] = 49;
2414$msgcode['delVLANSwitchTemplate']['ERR'] = 111;
2415function delVLANSwitchTemplate()
2416{
2417 assertUIntArg ('vst_id');
2418 global $sic;
2419 $result = FALSE !== usePreparedDeleteBlade ('VLANSwitchTemplate', array ('id' => $sic['vst_id']));
2420 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2421}
2422
2423$msgcode['updVLANSwitchTemplate']['OK'] = 51;
2424$msgcode['updVLANSwitchTemplate']['ERR'] = 109;
2425function updVLANSwitchTemplate()
2426{
2427 assertUIntArg ('vst_id');
2428 assertStringArg ('vst_descr');
2429 global $sic;
2430 $max_local_vlans = NULL;
2431 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2432 {
2433 assertUIntArg ('vst_maxvlans');
2434 $max_local_vlans = $sic['vst_maxvlans'];
2435 }
2436 $result = commitUpdateVST ($sic['vst_id'], $max_local_vlans, $sic['vst_descr']);
6cfedb71 2437 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
e0d188ef
DO
2438}
2439
09ec2e59
AA
2440$msgcode['cloneVSTRule']['OK'] = 48;
2441$msgcode['cloneVSTRule']['ERR'] = 179;
2442function cloneVSTRule()
e0d188ef 2443{
09ec2e59
AA
2444 global $dbxlink;
2445 $message = '';
09ec2e59
AA
2446 assertUIntArg ('mutex_rev', TRUE);
2447 $dst_vst = getVLANSwitchTemplate ($_REQUEST['vst_id']);
2448 if ($dst_vst['mutex_rev'] != $_REQUEST['mutex_rev'])
2449 $message = "User ${dst_vst['saved_by']} saved this template after you started to edit it. Please concern differencies";
2450 else
eb51ceff 2451 {
eb51ceff
DO
2452 assertUIntArg ('from_id');
2453 $src_vst = getVLANSwitchTemplate ($_REQUEST['from_id']);
09ec2e59
AA
2454 if (! commitUpdateVSTRules ($_REQUEST['vst_id'], $src_vst['rules']))
2455 $message = 'DB error';
eb51ceff 2456 }
09ec2e59
AA
2457 $result = !(BOOL) $message;
2458 if ($result)
2459 $message = 'Supplement succeeded';
2460 return buildWideRedirectURL (array (array ('code' => $result ? 'success' : 'error', 'message' => $message)));
e0d188ef
DO
2461}
2462
e0d188ef
DO
2463function updVSTRule()
2464{
09ec2e59 2465 global $port_role_options;
09ec2e59
AA
2466 assertUIntArg ('mutex_rev', TRUE);
2467 assertStringArg ('template_json');
2468 $message = '';
2469 $data = json_decode ($_POST['template_json'], TRUE);
2470 if (! isset ($data) or ! is_array ($data))
2471 $message = 'Invalid JSON code received from client';
2472 else
2473 {
2474 $rule_no = 0;
2475 foreach ($data as $rule)
2476 {
2477 $rule_no++;
2478 if (! isInteger ($rule['rule_no']))
2479 $message = 'Invalid rule number';
2480 elseif (! isPCRE ($rule['port_pcre']))
2481 $message = 'Invalid port regexp';
2482 elseif (! isset ($rule['port_role']) or ! array_key_exists ($rule['port_role'], $port_role_options))
2483 $message = 'Invalid port role';
2484 elseif (! isset ($rule['wrt_vlans']) or ! preg_match ('/^[ 0-9\-,]*$/', $rule['wrt_vlans']))
2485 $message = 'Invalid port vlan mask';
2486 elseif (! isset ($rule['description']))
2487 $message = 'Invalid description';
2488
2489 if ($message)
2490 {
2491 $message .= " in rule $rule_no";
2492 break;
2493 }
2494 }
2495 if (! $message)
2496 {
2497 $vst = getVLANSwitchTemplate ($_REQUEST['vst_id']);
2498 if ($vst['mutex_rev'] != $_REQUEST['mutex_rev'])
2499 $message = "User ${vst['saved_by']} saved this template after you started to edit it. Please concern differencies";
2500 }
2501 if (! $message)
2502 if (! commitUpdateVSTRules ($_REQUEST['vst_id'], $data))
2503 $message = 'DB update error';
2504 if ($message)
2505 $_SESSION['vst_edited'] = $data;
2506 }
2507 if ($message)
2508 {
2509 $result = FALSE;
2510 $message = "Template update failed: $message";
2511 }
2512 else
2513 {
2514 $result = TRUE;
2515 $message = "Update success";
2516 }
2517 return buildWideRedirectURL (array (array ('code' => $result ? 'success' : 'error', 'message' => $message)));
e0d188ef
DO
2518}
2519
5b5b1cab
DO
2520$msgcode['importDPData']['OK'] = 44;
2521function importDPData()
b3247480
DO
2522{
2523 global $sic;
2524 assertUIntArg ('nports');
2525 $nignored = $ndone = 0;
1dc057f8 2526 $POIFC = getPortOIFCompat();
b3247480
DO
2527 for ($i = 0; $i < $sic['nports']; $i++)
2528 if (array_key_exists ("do_${i}", $sic))
2529 {
2530 assertUIntArg ("pid1_${i}");
2531 assertUIntArg ("pid2_${i}");
2532 $porta = getPortInfo ($_REQUEST["pid1_${i}"]);
2533 $portb = getPortInfo ($_REQUEST["pid2_${i}"]);
2534 if
2535 (
2536 $porta['linked'] or
2537 $portb['linked'] or
2538 ($porta['object_id'] != $sic['object_id'] and $portb['object_id'] != $sic['object_id'])
2539 )
2540 {
2541 $nignored++;
2542 continue;
2543 }
1dc057f8
DO
2544 foreach ($POIFC as $item)
2545 if ($item['type1'] == $porta['oif_id'] and $item['type2'] == $portb['oif_id'])
2546 {
2547 linkPorts ($_REQUEST["pid1_${i}"], $_REQUEST["pid2_${i}"]);
2548 $ndone++;
524170a8 2549 continue 2; // next port
1dc057f8
DO
2550 }
2551 $nignored++;
b3247480
DO
2552 }
2553 return buildRedirectURL (__FUNCTION__, 'OK', array ($nignored, $ndone));
2554}
2555
9a90adc4
DO
2556$msgcode['addObjectlog']['OK'] = 0;
2557function addObjectlog ()
2558{
9a90adc4
DO
2559 assertStringArg ('logentry');
2560 global $remote_username, $sic;
2561 $oi = spotEntity ('object', $sic['object_id']);
2562 usePreparedExecuteBlade ('INSERT INTO ObjectLog SET object_id=?, user=?, date=NOW(), content=?', array ($sic['object_id'], $remote_username, $sic['logentry']));
2563 $ob_url = makeHref (array ('page' => 'object', 'tab' => 'objectlog', 'object_id' => $sic['object_id']));
2564 return buildRedirectURL (__FUNCTION__, 'OK', array ("Log entry for <a href=" . ${ob_url} . ">${oi['dname']}</a> added by ${remote_username}"));
2565}
2566
cde28cf0
DO
2567function tableHandler ($opspec)
2568{
2569 global $sic;
2570 if (!array_key_exists ('table', $opspec))
2571 throw new InvalidArgException ('opspec', '(malformed array structure)', '"table" not set');
2572 $columns = array();
2573 foreach ($opspec['arglist'] as $argspec)
2574 {
2575 genericAssertion ($argspec['url_argname'], $argspec['assertion']);
4eba4f82
DO
2576 // "table_colname" is normally used for an override, if it is not
2577 // set, use the URL argument name
2578 $table_colname = array_key_exists ('table_colname', $argspec) ?
2579 $argspec['table_colname'] :
2580 $argspec['url_argname'];
2581 $arg_value = $sic[$argspec['url_argname']];
2582 if
2583 (
2584 ($argspec['assertion'] == 'uint0' and $arg_value == 0)
2585 or ($argspec['assertion'] == 'string0' and $arg_value == '')
2586 )
2587 switch (TRUE)
2588 {
2589 case !array_key_exists ('if_empty', $argspec): // no action requested
2590 break;
2591 case $argspec['if_empty'] == 'NULL':
2592 $arg_value = NULL;
2593 break;
2594// A trick below is likely to break non-INSERT queries.
2595// case $argspec['if_empty'] == 'omit':
2596// continue 2;
2597 default:
2598 throw new InvalidArgException ('opspec', '(malformed array structure)', '"if_empty" not recognized');
2599 }
2600 $columns[$table_colname] = $arg_value;
cde28cf0
DO
2601 }
2602 switch ($opspec['action'])
2603 {
2604 case 'INSERT':
2605 $retcode = TRUE === usePreparedInsertBlade ($opspec['table'], $columns) ? 48 : 110;
2606 break;
10005279
DO
2607 case 'DELETE':
2608 $conjunction = array_key_exists ('conjunction', $opspec) ? $opspec['conjunction'] : 'AND';
2609 $retcode = FALSE !== usePreparedDeleteBlade ($opspec['table'], $columns, $conjunction) ? 49 : 111;
2610 break;
cde28cf0
DO
2611 default:
2612 throw new InvalidArgException ('opspec/action', '(malformed array structure)');
2613 }
2614 return buildWideRedirectURL (oneLiner ($retcode));
2615}
2616
e673ee24 2617?>