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