r3576 ios12PickSwitchportCommand(): never assume switchport mode, but don't
[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
780 // Field is empty, delete attribute and move on.
59a83bd8 781 if (!strlen ($_REQUEST["${i}_value"]))
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', '');
222011be
DO
1056 setConfigVar ('8021Q_PULL_AROUND_CONFLICTS', 'yes');
1057 setConfigVar ('8021Q_PUSH_AROUND_CONFLICTS', 'yes');
01b9a31a 1058 setConfigVar ('STATIC_FILTER', 'yes');
135080d8 1059 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
1060}
1061
b6a7d936
DO
1062$msgcode['addRealServer']['OK'] = 34;
1063$msgcode['addRealServer']['ERR'] = 126;
732e4578 1064// Add single record.
ca461127
DO
1065function addRealServer ()
1066{
0cc24e9a
DY
1067 assertUIntArg ('pool_id');
1068 assertIPv4Arg ('remoteip');
1069 assertStringArg ('rsport', TRUE);
1070 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1071 if (!addRStoRSPool (
1072 $_REQUEST['pool_id'],
1073 $_REQUEST['remoteip'],
1074 $_REQUEST['rsport'],
1075 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1076 $_REQUEST['rsconfig']
1077 ))
135080d8 1078 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1079 else
135080d8 1080 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1081}
1082
b6a7d936
DO
1083$msgcode['addRealServers']['OK'] = 37;
1084$msgcode['addRealServers']['ERR1'] = 131;
1085$msgcode['addRealServers']['ERR2'] = 127;
732e4578
DO
1086// Parse textarea submitted and try adding a real server for each line.
1087function addRealServers ()
1088{
0cc24e9a
DY
1089 assertUIntArg ('pool_id');
1090 assertStringArg ('format');
1091 assertStringArg ('rawtext');
732e4578
DO
1092 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1093 $ngood = $nbad = 0;
1094 $rsconfig = '';
1095 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1096 foreach (explode ('\n', $rawtext) as $line)
1097 {
59a83bd8 1098 if (!strlen ($line))
732e4578
DO
1099 continue;
1100 $match = array ();
1101 switch ($_REQUEST['format'])
1102 {
1103 case 'ipvs_2': // address and port only
1104 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1105 continue;
103b1e1e 1106 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1107 $ngood++;
1108 else
1109 $nbad++;
1110 break;
1111 case 'ipvs_3': // address, port and weight
1112 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1113 continue;
103b1e1e 1114 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1115 $ngood++;
1116 else
1117 $nbad++;
1118 break;
e69c2aa3
DO
1119 case 'ssv_2': // IP address and port
1120 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1121 continue;
103b1e1e 1122 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1123 $ngood++;
1124 else
1125 $nbad++;
1126 break;
948c37e3
DO
1127 case 'ssv_1': // IP address
1128 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1129 continue;
1130 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1131 $ngood++;
1132 else
1133 $nbad++;
1134 break;
732e4578 1135 default:
135080d8 1136 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1137 break;
1138 }
1139 }
1140 if ($nbad == 0 and $ngood > 0)
135080d8 1141 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1142 else
135080d8 1143 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1144}
1145
b6a7d936
DO
1146$msgcode['addVService']['OK'] = 28;
1147$msgcode['addVService']['ERR1'] = 132;
1148$msgcode['addVService']['ERR2'] = 100;
d6517a21
DO
1149function addVService ()
1150{
0cc24e9a
DY
1151 assertIPv4Arg ('vip');
1152 assertUIntArg ('vport');
1153 assertStringArg ('proto');
103b1e1e 1154 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1155 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1156 assertStringArg ('name', TRUE);
1157 assertStringArg ('vsconfig', TRUE);
1158 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1159 $error = commitCreateVS
1160 (
103b1e1e
DO
1161 $_REQUEST['vip'],
1162 $_REQUEST['vport'],
7320b661 1163 $_REQUEST['proto'],
103b1e1e
DO
1164 $_REQUEST['name'],
1165 $_REQUEST['vsconfig'],
c63a8d6e
DO
1166 $_REQUEST['rsconfig'],
1167 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1168 );
1169 if ($error != '')
135080d8 1170 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1171 else
135080d8 1172 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1173}
1174
b6a7d936
DO
1175$msgcode['deleteRealServer']['OK'] = 35;
1176$msgcode['deleteRealServer']['ERR'] = 128;
fb1c4a54
DO
1177function deleteRealServer ()
1178{
0cc24e9a 1179 assertUIntArg ('id');
d6517a21 1180 if (!commitDeleteRS ($_REQUEST['id']))
135080d8 1181 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1182 else
135080d8 1183 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1184}
1185
b6a7d936
DO
1186$msgcode['deleteLoadBalancer']['OK'] = 19;
1187$msgcode['deleteLoadBalancer']['ERR'] = 129;
3241551e
DO
1188function deleteLoadBalancer ()
1189{
0cc24e9a
DY
1190 assertUIntArg ('object_id');
1191 assertUIntArg ('pool_id');
1192 assertUIntArg ('vs_id');
103b1e1e
DO
1193 if (!commitDeleteLB (
1194 $_REQUEST['object_id'],
1195 $_REQUEST['pool_id'],
1196 $_REQUEST['vs_id']
1197 ))
135080d8 1198 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1199 else
135080d8 1200 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1201}
1202
b6a7d936
DO
1203$msgcode['deleteVService']['OK'] = 29;
1204$msgcode['deleteVService']['ERR'] = 130;
d6517a21
DO
1205function deleteVService ()
1206{
0cc24e9a 1207 assertUIntArg ('vs_id');
e02e1941 1208 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1209 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1210 else
135080d8 1211 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1212}
1213
b6a7d936
DO
1214$msgcode['updateRealServer']['OK'] = 36;
1215$msgcode['updateRealServer']['ERR'] = 133;
fb1c4a54
DO
1216function updateRealServer ()
1217{
0cc24e9a
DY
1218 assertUIntArg ('rs_id');
1219 assertIPv4Arg ('rsip');
1220 assertStringArg ('rsport', TRUE);
1221 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1222 if (!commitUpdateRS (
1223 $_REQUEST['rs_id'],
1224 $_REQUEST['rsip'],
1225 $_REQUEST['rsport'],
1226 $_REQUEST['rsconfig']
1227 ))
135080d8 1228 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1229 else
135080d8 1230 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1231}
1232
b6a7d936
DO
1233$msgcode['updateLoadBalancer']['OK'] = 20;
1234$msgcode['updateLoadBalancer']['ERR'] = 134;
c1ca768c 1235function updateLoadBalancer ()
3241551e 1236{
0cc24e9a
DY
1237 assertUIntArg ('object_id');
1238 assertUIntArg ('pool_id');
1239 assertUIntArg ('vs_id');
1240 assertStringArg ('vsconfig', TRUE);
1241 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1242 if (!commitUpdateLB (
1243 $_REQUEST['object_id'],
1244 $_REQUEST['pool_id'],
1245 $_REQUEST['vs_id'],
1246 $_REQUEST['vsconfig'],
1247 $_REQUEST['rsconfig']
1248 ))
135080d8 1249 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1250 else
135080d8 1251 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1252}
1253
b6a7d936
DO
1254$msgcode['updateVService']['OK'] = 30;
1255$msgcode['updateVService']['ERR'] = 135;
d6517a21
DO
1256function updateVService ()
1257{
0cc24e9a
DY
1258 assertUIntArg ('vs_id');
1259 assertIPv4Arg ('vip');
1260 assertUIntArg ('vport');
1261 assertStringArg ('proto');
1262 assertStringArg ('name', TRUE);
1263 assertStringArg ('vsconfig', TRUE);
1264 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1265 if (!commitUpdateVS (
1266 $_REQUEST['vs_id'],
1267 $_REQUEST['vip'],
1268 $_REQUEST['vport'],
1269 $_REQUEST['proto'],
1270 $_REQUEST['name'],
1271 $_REQUEST['vsconfig'],
1272 $_REQUEST['rsconfig']
1273 ))
135080d8 1274 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1275 else
135080d8 1276 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1277}
1278
b6a7d936
DO
1279$msgcode['addLoadBalancer']['OK'] = 18;
1280$msgcode['addLoadBalancer']['ERR'] = 137;
3241551e
DO
1281function addLoadBalancer ()
1282{
0cc24e9a
DY
1283 assertUIntArg ('pool_id');
1284 assertUIntArg ('object_id');
1285 assertUIntArg ('vs_id');
1286 assertStringArg ('vsconfig', TRUE);
1287 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1288 if (!addLBtoRSPool (
1289 $_REQUEST['pool_id'],
1290 $_REQUEST['object_id'],
1291 $_REQUEST['vs_id'],
1292 $_REQUEST['vsconfig'],
1293 $_REQUEST['rsconfig']
1294 ))
135080d8 1295 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1296 else
135080d8 1297 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1298}
1299
b6a7d936
DO
1300$msgcode['addRSPool']['OK'] = 31;
1301$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1302function addRSPool ()
1303{
0cc24e9a
DY
1304 assertStringArg ('name', TRUE);
1305 assertStringArg ('vsconfig', TRUE);
1306 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1307 $error = commitCreateRSPool
1308 (
103b1e1e
DO
1309 $_REQUEST['name'],
1310 $_REQUEST['vsconfig'],
c63a8d6e
DO
1311 $_REQUEST['rsconfig'],
1312 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1313 );
1314 if ($error != '')
135080d8 1315 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1316 else
135080d8 1317 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1318}
1319
b6a7d936
DO
1320$msgcode['deleteRSPool']['OK'] = 32;
1321$msgcode['deleteRSPool']['ERR'] = 138;
5ad76f01
DO
1322function deleteRSPool ()
1323{
0cc24e9a 1324 assertUIntArg ('pool_id');
841a7a7a 1325 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
135080d8 1326 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1327 else
135080d8 1328 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1329}
1330
b6a7d936
DO
1331$msgcode['updateRSPool']['OK'] = 33;
1332$msgcode['updateRSPool']['ERR'] = 139;
5ad76f01
DO
1333function updateRSPool ()
1334{
0cc24e9a
DY
1335 assertUIntArg ('pool_id');
1336 assertStringArg ('name', TRUE);
1337 assertStringArg ('vsconfig', TRUE);
1338 assertStringArg ('rsconfig', TRUE);
841a7a7a 1339 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
135080d8 1340 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1341 else
135080d8 1342 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1343}
1344
b6a7d936
DO
1345$msgcode['updateRSInService']['OK'] = 38;
1346$msgcode['updateRSInService']['ERR'] = 140;
1f7d18fa
DO
1347function updateRSInService ()
1348{
0cc24e9a 1349 assertUIntArg ('rscount');
841a7a7a 1350 $pool_id = $_REQUEST['pool_id'];
a6e91ac2
DO
1351 $orig = spotEntity ('ipv4rspool', $pool_id);
1352 amplifyCell ($orig);
1f7d18fa
DO
1353 $nbad = $ngood = 0;
1354 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1355 {
1356 $rs_id = $_REQUEST["rsid_${i}"];
1357 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1358 $newval = 'yes';
1359 else
1360 $newval = 'no';
1361 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1362 {
1363 if (commitSetInService ($rs_id, $newval))
1364 $ngood++;
1365 else
1366 $nbad++;
1367 }
1368 }
1369 if (!$nbad)
135080d8 1370 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1371 else
135080d8 1372 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1373}
1374
b6a7d936
DO
1375$msgcode['importPTRData']['OK'] = 26;
1376$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1377// FIXME: check, that each submitted address belongs to the prefix we
1378// are operating on.
03eb5209
DO
1379function importPTRData ()
1380{
0cc24e9a 1381 assertUIntArg ('addrcount');
03eb5209 1382 $nbad = $ngood = 0;
03eb5209
DO
1383 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1384 {
3f3bd41e 1385 $inputname = "import_${i}";
03eb5209
DO
1386 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1387 continue;
0cc24e9a
DY
1388 assertIPv4Arg ("addr_${i}");
1389 assertStringArg ("descr_${i}", TRUE);
1390 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1391 // Non-existent addresses will not have this argument set in request.
1392 $rsvd = 'no';
03eb5209
DO
1393 if ($_REQUEST["rsvd_${i}"] == 'yes')
1394 $rsvd = 'yes';
3f3bd41e 1395 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1396 $ngood++;
1397 else
1398 $nbad++;
1399 }
1400 if (!$nbad)
135080d8 1401 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1402 else
135080d8 1403 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1404}
1405
b6a7d936
DO
1406$msgcode['generateAutoPorts']['OK'] = 21;
1407$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1408function generateAutoPorts ()
1409{
103b1e1e 1410 global $pageno;
0cc24e9a 1411 assertUIntArg ('object_id');
6297d584 1412 $info = spotEntity ('object', $_REQUEST['object_id']);
f3f0161f 1413 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1414 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1415 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1416 else
135080d8 1417 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1418}
1419
b6a7d936
DO
1420$msgcode['saveEntityTags']['OK'] = 22;
1421$msgcode['saveEntityTags']['ERR1'] = 143;
1422$msgcode['saveEntityTags']['ERR2'] = 187;
24cbe8af 1423// Filter out implicit tags before storing the new tag set.
3355ca56 1424function saveEntityTags ()
24cbe8af 1425{
eb27afad 1426 global $page, $pageno, $etype_by_pageno;
be2ae2a2 1427 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
9a61c175 1428 return buildRedirectURL (__FUNCTION__, 'ERR2', array (__FUNCTION__));
be2ae2a2 1429 $realm = $etype_by_pageno[$pageno];
3355ca56 1430 $bypass = $page[$pageno]['bypass'];
0cc24e9a 1431 assertUIntArg ($bypass);
2034d968 1432 $entity_id = $_REQUEST[$bypass];
e04931c8 1433 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1434 // Build a chain from the submitted data, minimize it,
ab379543 1435 // then wipe existing records and store the new set instead.
01b6b4d6 1436 destroyTagsForEntity ($realm, $entity_id);
eb27afad
DO
1437 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1438 // so why not use it?
6e49bd1f 1439 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1440 $n_succeeds = $n_errors = 0;
6e49bd1f 1441 foreach ($newchain as $taginfo)
eb6ea26f 1442 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1443 $n_succeeds++;
1444 else
1445 $n_errors++;
ab379543 1446 if ($n_errors)
9a61c175 1447 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1448 else
135080d8 1449 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1450}
1451
b6a7d936
DO
1452$msgcode['destroyTag']['OK'] = 58;
1453$msgcode['destroyTag']['ERR1'] = 183;
1454$msgcode['destroyTag']['ERR2'] = 144;
fe7044ad
DO
1455function destroyTag ()
1456{
0cc24e9a 1457 assertUIntArg ('tag_id');
c615a655
DO
1458 global $taglist;
1459 if (!isset ($taglist[$_REQUEST['tag_id']]))
1460 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['tag_id']));
49fb1027 1461 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
c615a655 1462 return buildRedirectURL (__FUNCTION__, 'OK', array ($taglist[$_REQUEST['tag_id']]['tag']));
fe7044ad 1463 else
c615a655 1464 return buildRedirectURL (__FUNCTION__, 'ERR2');
fe7044ad
DO
1465}
1466
b6a7d936
DO
1467$msgcode['createTag']['OK'] = 59;
1468$msgcode['createTag']['ERR1'] = 145;
1469$msgcode['createTag']['ERR3'] = 147;
fe7044ad
DO
1470function createTag ()
1471{
0cc24e9a
DY
1472 assertStringArg ('tag_name');
1473 assertUIntArg ('parent_id', TRUE);
49fb1027 1474 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1475 if (!validTagName ($tagname))
135080d8 1476 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
fe7044ad
DO
1477 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1478 $parent_id = 'NULL';
1479 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
135080d8 1480 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
fe7044ad 1481 else
135080d8 1482 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
fe7044ad
DO
1483}
1484
b6a7d936
DO
1485$msgcode['updateTag']['OK'] = 60;
1486$msgcode['updateTag']['ERR1'] = 145;
1487$msgcode['updateTag']['ERR2'] = 148;
49fb1027
DO
1488function updateTag ()
1489{
0cc24e9a
DY
1490 assertUIntArg ('tag_id');
1491 assertUIntArg ('parent_id', TRUE);
1492 assertStringArg ('tag_name');
49fb1027 1493 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1494 if (!validTagName ($tagname))
135080d8 1495 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1496 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1497 $parent_id = 'NULL';
1498 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
135080d8 1499 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
590e1281
DO
1500 // Use old name in the message, cause update failed.
1501 global $taglist;
1502 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag'], $ret));
49fb1027
DO
1503}
1504
b6a7d936
DO
1505$msgcode['rollTags']['OK'] = 67;
1506$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1507function rollTags ()
1508{
0cc24e9a
DY
1509 assertUIntArg ('row_id');
1510 assertStringArg ('sum', TRUE);
1511 assertUIntArg ('realsum');
eb6ea26f 1512 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1513 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1514 // Even if the user requested an empty tag list, don't bail out, but process existing
1515 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1516 // have its chains refined to "normal" form.
1517 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1518 $n_ok = 0;
1519 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1520 // filter out the same tag again and again. It will have own noise to cancel.
1521 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
61a1d996 1522 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
abef7149 1523 {
61a1d996 1524 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
abef7149 1525 $n_ok++;
61a1d996
DO
1526 amplifyCell ($rack);
1527 foreach ($rack['mountedObjects'] as $object_id)
abef7149
DO
1528 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1529 $n_ok++;
1530 }
135080d8 1531 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1532}
1533
b6a7d936
DO
1534$msgcode['changeMyPassword']['OK'] = 61;
1535$msgcode['changeMyPassword']['ERR1'] = 150;
1536$msgcode['changeMyPassword']['ERR2'] = 151;
1537$msgcode['changeMyPassword']['ERR3'] = 152;
1538$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1539function changeMyPassword ()
cced6b7d 1540{
b82cce3f 1541 global $remote_username, $user_auth_src;
204284ba 1542 if ($user_auth_src != 'database')
135080d8 1543 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1544 assertStringArg ('oldpassword');
1545 assertStringArg ('newpassword1');
1546 assertStringArg ('newpassword2');
b82cce3f 1547 $remote_userid = getUserIDByUsername ($remote_username);
0b2c74cb 1548 $userinfo = spotEntity ('user', $remote_userid);
b82cce3f 1549 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1550 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1551 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1552 return buildRedirectURL (__FUNCTION__, 'ERR3');
b82cce3f 1553 if (commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1554 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1555 else
135080d8 1556 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1557}
1558
b6a7d936
DO
1559$msgcode['saveRackCode']['OK'] = 43;
1560$msgcode['saveRackCode']['ERR1'] = 154;
1561$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1562function saveRackCode ()
1563{
cced6b7d 1564 assertStringArg ('rackcode');
e6a4adb9
DO
1565 // For the test to succeed, unescape LFs, strip CRs.
1566 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1567 $parseTree = getRackCode ($newcode);
1568 if ($parseTree['result'] != 'ACK')
135080d8 1569 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
4a6a28f1 1570 saveScript ('RackCodeCache', '');
cf25e649 1571 if (saveScript ('RackCode', $newcode))
135080d8 1572 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1573 else
135080d8 1574 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1575}
1576
b6a7d936 1577$msgcode['setPortVLAN']['ERR1'] = 156;
46f92ff7
DO
1578// This handler's context is pre-built, but not authorized. It is assumed, that the
1579// handler will take existing context and before each commit check authorization
1580// on the base chain plus necessary context added.
1581function setPortVLAN ()
1582{
0cc24e9a 1583 assertUIntArg ('portcount');
46f92ff7
DO
1584 $data = getSwitchVLANs ($_REQUEST['object_id']);
1585 if ($data === NULL)
135080d8 1586 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1587 list ($vlanlist, $portlist) = $data;
1588 // Here we just build up 1 set command for the gateway with all of the ports
1589 // included. The gateway is expected to filter unnecessary changes silently
1590 // and to provide a list of responses with either error or success message
1591 // for each of the rest.
1592 $nports = $_REQUEST['portcount'];
1593 $prefix = 'set ';
db55cf54 1594 $log = emptyLog();
46f92ff7
DO
1595 $setcmd = '';
1596 for ($i = 0; $i < $nports; $i++)
1597 if
1598 (
1599 !isset ($_REQUEST['portname_' . $i]) ||
1600 !isset ($_REQUEST['vlanid_' . $i]) ||
1601 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1602 )
24dcb9d8 1603 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1604 elseif
1605 (
1606 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1607 $portlist[$i]['vlaind'] == 'TRUNK'
1608 )
1609 continue;
1610 else
1611 {
1612 $portname = $_REQUEST['portname_' . $i];
1613 $oldvlanid = $portlist[$i]['vlanid'];
1614 $newvlanid = $_REQUEST['vlanid_' . $i];
1615 // Finish the security context and evaluate it.
1616 $annex = array();
1617 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1618 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1619 if (!permitted (NULL, NULL, NULL, $annex))
1620 {
24dcb9d8 1621 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1622 continue;
1623 }
1624 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1625 $prefix = ';';
1626 }
1627 // Feed the gateway and interpret its (non)response.
1628 if ($setcmd != '')
f0ff4930 1629 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1630 else
24dcb9d8 1631 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1632 return buildWideRedirectURL ($log);
1633}
1634
2987fc1f
DO
1635function submitSLBConfig ()
1636{
0cc24e9a 1637 assertUIntArg ('object_id');
2987fc1f 1638 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
ff0df5c3 1639 $msglog = gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1640 return buildWideRedirectURL ($msglog);
1641}
1642
b6a7d936
DO
1643$msgcode['addRow']['OK'] = 74;
1644$msgcode['addRow']['ERR'] = 100;
345fd640
AD
1645function addRow ()
1646{
0cc24e9a 1647 assertStringArg ('name');
345fd640 1648
10bac82a 1649 if (commitAddRow ($_REQUEST['name']) === TRUE)
135080d8 1650 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1651 else
135080d8 1652 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1653}
1654
b6a7d936
DO
1655$msgcode['updateRow']['OK'] = 75;
1656$msgcode['updateRow']['ERR'] = 100;
345fd640
AD
1657function updateRow ()
1658{
0cc24e9a
DY
1659 assertUIntArg ('row_id');
1660 assertStringArg ('name');
345fd640 1661
10bac82a 1662 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1663 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1664 else
135080d8 1665 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1666}
1667
b6a7d936
DO
1668$msgcode['deleteRow']['OK'] = 77;
1669$msgcode['deleteRow']['ERR'] = 100;
9f14a7ef
DY
1670function deleteRow ()
1671{
0cc24e9a 1672 assertUIntArg ('row_id');
7be51fea 1673 $rowinfo = getRackRowInfo ($_REQUEST['row_id']);
9f14a7ef
DY
1674
1675 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
7be51fea 1676 return buildRedirectURL (__FUNCTION__, 'OK', array ($rowinfo['name']));
9f14a7ef 1677 else
7be51fea 1678 return buildRedirectURL (__FUNCTION__, 'ERR', array ($rowinfo['name']));
9f14a7ef
DY
1679}
1680
b6a7d936
DO
1681$msgcode['addRack']['OK'] = 65;
1682$msgcode['addRack']['ERR1'] = 171;
1683$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1684function addRack ()
1685{
0cc24e9a 1686 assertUIntArg ('row_id');
f19c75d6
DO
1687 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1688 if (isset ($_REQUEST['got_data']))
1689 {
0cc24e9a
DY
1690 assertStringArg ('rack_name');
1691 assertUIntArg ('rack_height1');
1692 assertStringArg ('rack_comment', TRUE);
f19c75d6
DO
1693
1694 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1695 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1696 else
135080d8 1697 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1698 }
1699 elseif (isset ($_REQUEST['got_mdata']))
1700 {
0cc24e9a
DY
1701 assertUIntArg ('rack_height2');
1702 assertStringArg ('rack_names', TRUE);
db55cf54 1703 $log = emptyLog();
f19c75d6
DO
1704 // copy-and-paste from renderAddMultipleObjectsForm()
1705 $names1 = explode ('\n', $_REQUEST['rack_names']);
1706 $names2 = array();
1707 foreach ($names1 as $line)
1708 {
1709 $parts = explode ('\r', $line);
1710 reset ($parts);
59a83bd8 1711 if (!strlen ($parts[0]))
f19c75d6
DO
1712 continue;
1713 else
1714 $names2[] = rtrim ($parts[0]);
1715 }
b3029d5c 1716 global $msgcode;
f19c75d6
DO
1717 foreach ($names2 as $cname)
1718 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1719 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1720 else
b3029d5c 1721 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1722 return buildWideRedirectURL ($log);
1723 }
1724 else
135080d8 1725 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1726}
1727
b6a7d936
DO
1728$msgcode['deleteRack']['OK'] = 79;
1729$msgcode['deleteRack']['ERR'] = 100;
1730$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1731function deleteRack ()
1732{
0cc24e9a 1733 assertUIntArg ('rack_id');
61a1d996
DO
1734 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1735 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1736 amplifyCell ($rackData);
1737 if (count ($rackData['mountedObjects']))
c8187437 1738 return buildRedirectURL (__FUNCTION__, 'ERR1');
61a1d996
DO
1739 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1740 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1741 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
c8187437
DY
1742}
1743
b6a7d936
DO
1744$msgcode['updateRack']['OK'] = 68;
1745$msgcode['updateRack']['ERR'] = 177;
7056988c
DO
1746function updateRack ()
1747{
0cc24e9a
DY
1748 assertUIntArg ('rack_id');
1749 assertUIntArg ('rack_row_id');
1750 assertUIntArg ('rack_height');
1751 assertStringArg ('rack_name');
1752 assertStringArg ('rack_comment', TRUE);
7056988c 1753
7056988c 1754 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1755 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1756 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1757 else
135080d8 1758 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1759}
1760
61a1d996 1761$msgcode['updateRackDesign']['ERR'] = 100;
4fbb5a00
DY
1762function updateRackDesign ()
1763{
0cc24e9a 1764 assertUIntArg ('rack_id');
61a1d996
DO
1765 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1766 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1767 amplifyCell ($rackData);
4fbb5a00
DY
1768 applyRackDesignMask($rackData);
1769 markupObjectProblems ($rackData);
1770 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1771 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1772}
1773
61a1d996 1774$msgcode['updateRackProblems']['ERR'] = 100;
4fbb5a00
DY
1775function updateRackProblems ()
1776{
0cc24e9a 1777 assertUIntArg ('rack_id');
61a1d996
DO
1778 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1779 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1780 amplifyCell ($rackData);
4fbb5a00
DY
1781 applyRackProblemMask($rackData);
1782 markupObjectProblems ($rackData);
1783 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1784 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1785}
1786
7056988c
DO
1787function querySNMPData ()
1788{
0cc24e9a
DY
1789 assertUIntArg ('object_id');
1790 assertStringArg ('community');
8536e20a 1791 return doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']);
7056988c
DO
1792}
1793
b6a7d936
DO
1794$msgcode['addFileWithoutLink']['OK'] = 69;
1795$msgcode['addFileWithoutLink']['ERR'] = 100;
e1ae3fb4
AD
1796// File-related functions
1797function addFileWithoutLink ()
1798{
0cc24e9a 1799 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1800
1801 // Make sure the file can be uploaded
1802 if (get_cfg_var('file_uploads') != 1)
135080d8 1803 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1804
1805 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1806 global $sic;
1807 // commitAddFile() uses prepared statements
1808 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
f857f71f
DO
1809 if (isset ($_REQUEST['taglist']))
1810 produceTagsForLastRecord ('file', $_REQUEST['taglist']);
e1ae3fb4
AD
1811
1812 if ($error != '')
135080d8 1813 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1814
135080d8 1815 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1816}
1817
b6a7d936
DO
1818$msgcode['addFileToEntity']['OK'] = 69;
1819$msgcode['addFileToEntity']['ERR1'] = 187;
1820$msgcode['addFileToEntity']['ERR2'] = 181;
1821$msgcode['addFileToEntity']['ERR3'] = 182;
e1ae3fb4
AD
1822function addFileToEntity ()
1823{
9a61c175
DO
1824 global $page, $pageno, $etype_by_pageno;
1825 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1826 return buildRedirectURL (__FUNCTION__, 'ERR1', array (__FUNCTION__));
1827 $realm = $etype_by_pageno[$pageno];
1828 $bypass = $page[$pageno]['bypass'];
0cc24e9a 1829 assertUIntArg ($bypass);
9a61c175 1830 $entity_id = $_REQUEST[$bypass];
0cc24e9a 1831 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1832
1833 // Make sure the file can be uploaded
1834 if (get_cfg_var('file_uploads') != 1)
9a61c175 1835 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
1836
1837 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1838 global $sic;
1839 // commitAddFile() uses prepared statements
1840 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
e1ae3fb4 1841 if ($error != '')
9a61c175 1842 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1843
9a61c175 1844 $error = commitLinkFile (lastInsertID(), $realm, $entity_id);
e1ae3fb4 1845 if ($error != '')
9a61c175 1846 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1847
135080d8 1848 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1849}
1850
b6a7d936
DO
1851$msgcode['linkFileToEntity']['OK'] = 71;
1852$msgcode['linkFileToEntity']['ERR1'] = 178;
1853$msgcode['linkFileToEntity']['ERR2'] = 100;
e1ae3fb4
AD
1854function linkFileToEntity ()
1855{
0cc24e9a 1856 assertUIntArg ('file_id');
482c7f35 1857 global $page, $pageno, $etype_by_pageno;
121496b6
DO
1858 $entity_type = $etype_by_pageno[$pageno];
1859 $bypass_name = $page[$pageno]['bypass'];
0cc24e9a 1860 assertUIntArg ($bypass_name);
121496b6 1861
d3b5008b 1862 $fi = spotEntity ('file', $_REQUEST['file_id']);
121496b6 1863 if ($fi === NULL)
135080d8 1864 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
121496b6 1865 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
e1ae3fb4 1866 if ($error != '')
135080d8 1867 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
e1ae3fb4 1868
135080d8 1869 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
e1ae3fb4
AD
1870}
1871
b6a7d936
DO
1872$msgcode['replaceFile']['OK'] = 70;
1873$msgcode['replaceFile']['ERR1'] = 181;
1874$msgcode['replaceFile']['ERR2'] = 207;
1875$msgcode['replaceFile']['ERR3'] = 182;
fd1fb420
AD
1876function replaceFile ()
1877{
db55cf54 1878 global $sic;
0cc24e9a 1879 assertUIntArg ('file_id');
fd1fb420
AD
1880
1881 // Make sure the file can be uploaded
1882 if (get_cfg_var('file_uploads') != 1)
db55cf54 1883 return buildRedirectURL (__FUNCTION__, 'ERR1');
d3b5008b 1884 $shortInfo = spotEntity ('file', $sic['file_id']);
fd1fb420
AD
1885
1886 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1887 if ($fp === FALSE)
1888 return buildRedirectURL (__FUNCTION__, 'ERR2');
f8874cdb 1889 $error = commitReplaceFile ($sic['file_id'], $fp);
fd1fb420 1890 if ($error != '')
db55cf54 1891 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
fd1fb420 1892
db55cf54 1893 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1894}
1895
b6a7d936
DO
1896$msgcode['updateFile']['OK'] = 70;
1897$msgcode['updateFile']['ERR'] = 100;
e1ae3fb4
AD
1898function updateFile ()
1899{
0cc24e9a
DY
1900 assertUIntArg ('file_id');
1901 assertStringArg ('file_name');
1902 assertStringArg ('file_type');
1903 assertStringArg ('file_comment', TRUE);
f3d274bf
DO
1904 // prepared statement params below
1905 global $sic;
1906 $error = commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']);
e1ae3fb4 1907 if ($error != '')
135080d8 1908 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1909
135080d8 1910 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1911}
1912
b6a7d936
DO
1913$msgcode['unlinkFile']['OK'] = 72;
1914$msgcode['unlinkFile']['ERR'] = 182;
e1ae3fb4
AD
1915function unlinkFile ()
1916{
0cc24e9a 1917 assertUIntArg ('link_id');
e1ae3fb4
AD
1918 $error = commitUnlinkFile ($_REQUEST['link_id']);
1919
1920 if ($error != '')
135080d8 1921 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1922
8bc5d1e4 1923 return buildRedirectURL (__FUNCTION__, 'OK');
e1ae3fb4
AD
1924}
1925
b6a7d936
DO
1926$msgcode['deleteFile']['OK'] = 73;
1927$msgcode['deleteFile']['ERR'] = 100;
e1ae3fb4
AD
1928function deleteFile ()
1929{
0cc24e9a 1930 assertUIntArg ('file_id');
d3b5008b 1931 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
e1ae3fb4
AD
1932 $error = commitDeleteFile ($_REQUEST['file_id']);
1933
1934 if ($error != '')
135080d8 1935 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1936
f8874cdb 1937 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1938}
1939
b6a7d936
DO
1940$msgcode['updateFileText']['OK'] = 78;
1941$msgcode['updateFileText']['ERR1'] = 179;
1942$msgcode['updateFileText']['ERR2'] = 180;
8628ae44
DO
1943function updateFileText ()
1944{
0cc24e9a
DY
1945 assertUIntArg ('file_id');
1946 assertStringArg ('mtime_copy');
1947 assertStringArg ('file_text', TRUE); // it's Ok to save empty
d3b5008b 1948 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
70cb9b56
DO
1949 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1950 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb
DO
1951 global $sic;
1952 $error = commitReplaceFile ($sic['file_id'], $sic['file_text']);
8628ae44 1953 if ($error == '')
f8874cdb 1954 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
70cb9b56 1955 return buildRedirectURL (__FUNCTION__, 'ERR2');
8628ae44
DO
1956}
1957
2dfa1b73
DO
1958$msgcode['addPortInterfaceCompat']['OK'] = 48;
1959$msgcode['addPortInterfaceCompat']['ERR'] = 110;
1960function addPortInterfaceCompat ()
1961{
0cc24e9a
DY
1962 assertUIntArg ('iif_id');
1963 assertUIntArg ('oif_id');
2dfa1b73
DO
1964 if (commitSupplementPIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
1965 return buildRedirectURL (__FUNCTION__, 'OK');
1966 return buildRedirectURL (__FUNCTION__, 'ERR');
1967}
1968
1969$msgcode['delPortInterfaceCompat']['OK'] = 49;
1970$msgcode['delPortInterfaceCompat']['ERR'] = 111;
1971function delPortInterfaceCompat ()
1972{
0cc24e9a
DY
1973 assertUIntArg ('iif_id');
1974 assertUIntArg ('oif_id');
2dfa1b73
DO
1975 if (commitReducePIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
1976 return buildRedirectURL (__FUNCTION__, 'OK');
1977 return buildRedirectURL (__FUNCTION__, 'ERR');
1978}
1979
2400d7ec
DO
1980$ifcompatpack = array
1981(
1982 '1000cwdm80' => array (1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216),
1983 '1000dwdm80' => array // ITU channels 20~61
1984 (
1985 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226,
1986 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236,
1987 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
1988 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
1989 1257, 1258
1990 ),
1991 '10000dwdm80' => array // same channels for 10GE
1992 (
1993 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268,
1994 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278,
1995 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
1996 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298,
1997 1299, 1300
1998 ),
1999);
2000
3d9ac3d4
DO
2001$msgcode['addPortInterfaceCompatPack']['OK'] = 44;
2002$msgcode['addPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2003function addPortInterfaceCompatPack ()
2004{
0cc24e9a
DY
2005 assertStringArg ('standard');
2006 assertUIntArg ('iif_id');
3d9ac3d4
DO
2007 global $ifcompatpack;
2008 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
2009 return buildRedirectURL (__FUNCTION__, 'ERR');
2010 $ngood = $nbad = 0;
2011 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2012 if (commitSupplementPIC ($_REQUEST['iif_id'], $oif_id))
2013 $ngood++;
2014 else
2015 $nbad++;
2016 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2017}
2018
3d9ac3d4
DO
2019$msgcode['delPortInterfaceCompatPack']['OK'] = 44;
2020$msgcode['delPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2021function delPortInterfaceCompatPack ()
2022{
0cc24e9a
DY
2023 assertStringArg ('standard');
2024 assertUIntArg ('iif_id');
3d9ac3d4
DO
2025 global $ifcompatpack;
2026 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
2027 return buildRedirectURL (__FUNCTION__, 'ERR');
2028 $ngood = $nbad = 0;
2029 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2030 if (commitReducePIC ($_REQUEST['iif_id'], $oif_id))
2031 $ngood++;
2032 else
2033 $nbad++;
2034 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2035}
2036
a310dd80
DO
2037$msgcode['addPortOIFCompat']['OK'] = 48;
2038$msgcode['addPortOIFCompat']['ERR'] = 110;
2039function addPortOIFCompat()
2040{
80ec1f4f
DO
2041 assertUIntArg('type1');
2042 assertUIntArg('type2');
a310dd80
DO
2043 if (commitSupplementPOIFC($_REQUEST['type1'], $_REQUEST['type2']))
2044 return buildRedirectURL(__FUNCTION__, 'OK');
2045 return buildRedirectURL(__FUNCTION__, 'ERR');
2046}
2047
2048$msgcode['delPortOIFCompat']['OK'] = 49;
2049$msgcode['delPortOIFCompat']['ERR'] = 111;
2050function delPortOIFCompat ()
2051{
80ec1f4f
DO
2052 assertUIntArg('type1');
2053 assertUIntArg('type2');
a310dd80
DO
2054 if (commitReducePOIFC ($_REQUEST['type1'], $_REQUEST['type2']))
2055 return buildRedirectURL (__FUNCTION__, 'OK');
2056 return buildRedirectURL (__FUNCTION__, 'ERR');
2057
2058}
2059
38cd7704
DO
2060$msgcode['add8021QOrder']['OK'] = 48;
2061$msgcode['add8021QOrder']['ERR'] = 118;
2062function add8021QOrder ()
8198f2c6 2063{
80ec1f4f
DO
2064 assertUIntArg ('vdom_id');
2065 assertUIntArg ('object_id');
38cd7704 2066 assertUIntArg ('vst_id');
d6ac0bea 2067 global $sic, $dbxlink;
8198f2c6
DO
2068 $result = usePreparedInsertBlade
2069 (
2070 'VLANSwitch',
2071 array
2072 (
2073 'domain_id' => $sic['vdom_id'],
2074 'object_id' => $sic['object_id'],
38cd7704 2075 'template_id' => $sic['vst_id'],
8198f2c6
DO
2076 )
2077 );
d6ac0bea
DO
2078 $query = $dbxlink->prepare ('UPDATE VLANSwitch SET last_change = NOW(), out_of_sync = "yes" WHERE object_id = ?');
2079 $query->execute (array ($sic['object_id']));
8198f2c6
DO
2080 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2081}
2082
38cd7704
DO
2083$msgcode['del8021QOrder']['OK'] = 49;
2084$msgcode['del8021QOrder']['ERR'] = 119;
2085function del8021QOrder ()
8198f2c6 2086{
80ec1f4f 2087 assertUIntArg ('object_id');
8198f2c6 2088 global $sic;
38cd7704 2089 $result = usePreparedDeleteBlade ('VLANSwitch', array ('object_id' => $sic['object_id']));
8198f2c6
DO
2090 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2091}
2092
2093$msgcode['addVLANDescription']['OK'] = 48;
2094$msgcode['addVLANDescription']['ERR1'] = 190;
2095$msgcode['addVLANDescription']['ERR2'] = 110;
2096function addVLANDescription ()
2097{
80ec1f4f 2098 assertUIntArg ('vlan_id');
5b989e4d 2099 assertStringArg ('vlan_type', TRUE);
80ec1f4f 2100 assertStringArg ('vlan_descr', TRUE);
8198f2c6
DO
2101 global $sic;
2102 if (!($sic['vlan_id'] >= VLAN_MIN_ID and $sic['vlan_id'] <= VLAN_MAX_ID))
2103 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($sic['vlan_id']));
2104 $result = usePreparedInsertBlade
2105 (
2106 'VLANDescription',
2107 array
2108 (
2109 'domain_id' => $sic['vdom_id'],
2110 'vlan_id' => $sic['vlan_id'],
5b989e4d 2111 'vlan_type' => $sic['vlan_type'],
5286b29f 2112 'vlan_descr' => mb_strlen ($sic['vlan_descr']) ? $sic['vlan_descr'] : NULL
8198f2c6
DO
2113 )
2114 );
2115 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR2');
2116}
2117
2118$msgcode['delVLANDescription']['OK'] = 49;
2119$msgcode['delVLANDescription']['ERR'] = 111;
2120function delVLANDescription ()
2121{
80ec1f4f 2122 assertUIntArg ('vlan_id');
8198f2c6
DO
2123 global $sic;
2124 $result = commitReduceVLANDescription ($sic['vdom_id'], $sic['vlan_id']);
2125 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2126}
2127
2128$msgcode['updVLANDescription']['OK'] = 51;
2129$msgcode['updVLANDescription']['ERR'] = 109;
2130function updVLANDescription ()
2131{
80ec1f4f
DO
2132 assertUIntArg ('vlan_id');
2133 assertStringArg ('vlan_type');
2134 assertStringArg ('vlan_descr', TRUE);
8198f2c6
DO
2135 global $sic;
2136 $result = commitUpdateVLANDescription
2137 (
2138 $sic['vdom_id'],
2139 $sic['vlan_id'],
0dabdc53 2140 $sic['vlan_type'],
8198f2c6
DO
2141 $sic['vlan_descr']
2142 );
2143 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2144}
2145
2146$msgcode['createVLANDomain']['OK'] = 48;
2147$msgcode['createVLANDomain']['ERR'] = 110;
2148function createVLANDomain ()
2149{
80ec1f4f 2150 assertStringArg ('vdom_descr');
8198f2c6
DO
2151 global $sic;
2152 $result = usePreparedInsertBlade
2153 (
2154 'VLANDomain',
2155 array
2156 (
2157 'description' => $sic['vdom_descr'],
2158 )
2159 );
2160 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2161}
2162
2163$msgcode['destroyVLANDomain']['OK'] = 49;
2164$msgcode['destroyVLANDomain']['ERR'] = 111;
2165function destroyVLANDomain ()
2166{
80ec1f4f 2167 assertUIntArg ('vdom_id');
8198f2c6 2168 global $sic;
bbae3611 2169 $result = FALSE !== usePreparedDeleteBlade ('VLANDomain', array ('id' => $sic['vdom_id']));
8198f2c6
DO
2170 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2171}
2172
2173$msgcode['updateVLANDomain']['OK'] = 51;
2174$msgcode['updateVLANDomain']['ERR'] = 109;
2175function updateVLANDomain ()
2176{
80ec1f4f
DO
2177 assertUIntArg ('vdom_id');
2178 assertStringArg ('vdom_descr');
8198f2c6
DO
2179 global $sic;
2180 $result = commitUpdateVLANDomain ($sic['vdom_id'], $sic['vdom_descr']);
2181 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2182}
2183
18c26ec5 2184$msgcode['save8021QPorts']['OK'] = 63;
706d9175
DO
2185$msgcode['save8021QPorts']['ERR1'] = 160;
2186$msgcode['save8021QPorts']['ERR2'] = 109;
2187function save8021QPorts ()
8198f2c6 2188{
a7492e95 2189 global $sic, $dbxlink;
706d9175
DO
2190 assertUIntArg ('nports');
2191 assertUIntArg ('mutex_rev');
2192 if ($sic['nports'] == 1)
2193 {
2194 assertStringArg ('pn_0');
2195 $extra = array ('port_name' => $sic['pn_0']);
2196 }
2197 else
2198 $extra = array();
a7492e95 2199 $dbxlink->beginTransaction();
2996cbbb
DO
2200 try
2201 {
a7492e95
DO
2202 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
2203 throw new InvalidArgException ('object_id', $object_id, 'VLAN domain is not set for this object');
9c45ea37
DO
2204 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
2205 throw new RuntimeException ('expired form data');
af204724 2206 $changes = array();
706d9175
DO
2207 for ($i = 0; $i < $sic['nports']; $i++)
2208 {
2209 assertStringArg ('pn_' . $i);
2210 assertStringArg ('pm_' . $i);
706d9175
DO
2211 // An access port only generates form input for its native VLAN,
2212 // which we derive allowed VLAN list from.
2213 $native = isset ($sic['pnv_' . $i]) ? $sic['pnv_' . $i] : 0;
b36fc896 2214 switch ($sic["pm_${i}"])
706d9175 2215 {
b36fc896
DO
2216 case 'trunk':
2217# assertArrayArg ('pav_' . $i);
2218 $allowed = isset ($sic['pav_' . $i]) ? $sic['pav_' . $i] : array();
2219 break;
2220 case 'access':
2221 if ($native == 'same')
2222 continue 2;
706d9175
DO
2223 assertUIntArg ('pnv_' . $i);
2224 $allowed = array ($native);
b36fc896
DO
2225 break;
2226 default:
2227 throw new InvalidRequestArgException ("pm_${i}", $_REQUEST["pm_${i}"], 'unknown port mode');
706d9175 2228 }
af204724 2229 $changes[$sic['pn_' . $i]] = array
706d9175
DO
2230 (
2231 'mode' => $sic['pm_' . $i],
2232 'allowed' => $allowed,
2233 'native' => $native,
2234 );
2235 }
af204724 2236 $domain_vlanlist = getDomainVLANs ($vswitch['domain_id']);
9165e8f7 2237 $after = $before = apply8021QOrder ($vswitch['template_id'], getStored8021QConfig ($sic['object_id'], 'desired'));
af204724 2238 $changes = filter8021QChangeRequests
a7492e95 2239 (
af204724 2240 $domain_vlanlist,
9165e8f7 2241 $before,
af204724 2242 apply8021QOrder ($vswitch['template_id'], $changes)
a7492e95 2243 );
bb2024b9 2244 $changes = authorize8021QChangeRequests ($before, $changes);
af204724
DO
2245 foreach ($changes as $port_name => $port)
2246 $after[$port_name] = $port;
b3a27170 2247 $new_uplinks = filter8021QChangeRequests ($domain_vlanlist, $after, produceUplinkPorts ($domain_vlanlist, $after));
7455e4c0 2248 $npulled = replace8021QPorts ('desired', $vswitch['object_id'], $before, $changes);
b3a27170 2249 $nsaved_uplinks = replace8021QPorts ('desired', $vswitch['object_id'], $before, $new_uplinks);
2996cbbb
DO
2250 }
2251 catch (Exception $e)
861292ee 2252 {
a7492e95 2253 $dbxlink->rollBack();
706d9175 2254 return buildRedirectURL (__FUNCTION__, 'ERR2', array(), NULL, NULL, $extra);
861292ee 2255 }
b3a27170 2256 if ($npulled + $nsaved_uplinks)
ec4d604c 2257 {
d973196a 2258 $query = $dbxlink->prepare ('UPDATE VLANSwitch SET mutex_rev = mutex_rev + 1, last_change = NOW(), out_of_sync = "yes" WHERE object_id = ?');
ec4d604c
DO
2259 $query->execute (array ($sic['object_id']));
2260 }
a7492e95 2261 $dbxlink->commit();
b3a27170
DO
2262 if ($nsaved_uplinks)
2263 initiateUplinksReverb ($vswitch['object_id'], $new_uplinks);
2264 return buildRedirectURL (__FUNCTION__, 'OK', array ($npulled + $nsaved_uplinks), NULL, NULL, $extra);
8198f2c6
DO
2265}
2266
8846b060
DO
2267$msgcode['bindVLANtoIPv4']['OK'] = 48;
2268$msgcode['bindVLANtoIPv4']['ERR'] = 110;
2269function bindVLANtoIPv4 ()
2270{
2271 assertUIntArg ('id'); // network id
2272 global $sic;
2273 $result = commitSupplementVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2274 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2275}
2276
2277$msgcode['unbindVLANfromIPv4']['OK'] = 49;
2278$msgcode['unbindVLANfromIPv4']['ERR'] = 111;
2279function unbindVLANfromIPv4 ()
2280{
2281 assertUIntArg ('id'); // network id
2282 global $sic;
2283 $result = commitReduceVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2284 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2285}
2286
228c430c 2287$msgcode['process8021QSyncRequest']['OK'] = 63;
be28b696 2288$msgcode['process8021QSyncRequest']['ERR1'] = 109;
ca5d4cbc 2289$msgcode['process8021QSyncRequest']['ERR2'] = 191;
228c430c 2290function process8021QSyncRequest ()
be28b696 2291{
ca5d4cbc 2292 global $sic;
be28b696
DO
2293 try
2294 {
d973196a 2295 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], array_key_exists ('do_push', $sic)))
ca5d4cbc
DO
2296 return buildRedirectURL (__FUNCTION__, 'ERR2'); // specific case
2297 return buildRedirectURL (__FUNCTION__, 'OK', array ($done));
be28b696
DO
2298 }
2299 catch (Exception $e)
2300 {
ca5d4cbc 2301 return buildRedirectURL (__FUNCTION__, 'ERR1'); // generic failure
be28b696 2302 }
be28b696
DO
2303}
2304
2305$msgcode['resolve8021QConflicts']['OK'] = 63;
bcd14540
DO
2306$msgcode['resolve8021QConflicts']['ERR1'] = 179;
2307$msgcode['resolve8021QConflicts']['ERR2'] = 109;
be28b696 2308function resolve8021QConflicts ()
07de6bb3 2309{
a7492e95 2310 global $sic, $dbxlink;
07de6bb3 2311 assertUIntArg ('mutex_rev');
de47b574
DO
2312 assertUIntArg ('nrows');
2313 // Divide submitted radio buttons into 3 groups:
19d1e731 2314 // left (saved version wins)
07de6bb3 2315 // asis (ignore)
19d1e731
DO
2316 // right (running version wins)
2317 $F = array();
339534a0 2318 for ($i = 0; $i < $sic['nrows']; $i++)
07de6bb3 2319 {
de47b574 2320 if (!array_key_exists ("i_${i}", $sic))
07de6bb3 2321 continue;
de47b574
DO
2322 // let's hope other inputs are in place
2323 switch ($sic["i_${i}"])
07de6bb3
DO
2324 {
2325 case 'left':
07de6bb3 2326 case 'right':
bcd14540 2327 $F[$sic["pn_${i}"]] = array
07de6bb3 2328 (
ca9e1c9d 2329 'mode' => $sic["rm_${i}"],
de47b574
DO
2330 'allowed' => $sic["ra_${i}"],
2331 'native' => $sic["rn_${i}"],
bcd14540 2332 'decision' => $sic["i_${i}"],
07de6bb3
DO
2333 );
2334 break;
2335 default:
2336 // don't care
2337 }
2338 }
a7492e95 2339 $dbxlink->beginTransaction();
07de6bb3
DO
2340 try
2341 {
a7492e95 2342 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
ec4d604c 2343 throw new InvalidArgException ('object_id', $sic['object_id'], 'VLAN domain is not set for this object');
9c45ea37 2344 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
bcd14540
DO
2345 {
2346 throw new RuntimeException ('expired form (table data has changed)');
2347 }
9c2201ba
DO
2348 $D = getStored8021QConfig ($vswitch['object_id'], 'desired');
2349 $C = getStored8021QConfig ($vswitch['object_id'], 'cached');
2350 $R = getRunning8021QConfig ($vswitch['object_id']);
d5e306b2 2351 $plan = get8021QSyncOptions ($vswitch, $D, $C, $R['portdata']);
bcd14540
DO
2352 $ndone = 0;
2353 foreach ($F as $port_name => $port)
2354 {
9c2201ba
DO
2355 if (!array_key_exists ($port_name, $plan))
2356 continue;
2357 elseif ($plan[$port_name]['status'] == 'merge_conflict')
bcd14540 2358 {
9c2201ba
DO
2359 // for R neither mutex nor revisions can be emulated, but revision change can be
2360 if (!same8021QConfigs ($port, $R['portdata'][$port_name]))
2361 throw new RuntimeException ('expired form (switch data has changed)');
2362 if ($port['decision'] == 'left') // D wins, frame R by writing value of R to C
2363 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $port);
2364 elseif ($port['decision'] == 'right') // R wins, cross D up
2365 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $D[$port_name]);
2366 // otherwise there was no decision made
2367 }
2368 elseif ($plan[$port_name]['status'] == 'delete_conflict')
2369 {
2370 if ($port['decision'] == 'right') // confirm deletion of local copy
2371 $ndone += del8021QPort ($vswitch['object_id'], $port_name);
bcd14540 2372 }
9c2201ba 2373 // otherwise ignore a decision, which doesn't address a conflict
bcd14540 2374 }
07de6bb3 2375 }
bcd14540 2376 catch (RuntimeException $e)
07de6bb3 2377 {
a7492e95 2378 $dbxlink->rollBack();
bcd14540 2379 return buildRedirectURL (__FUNCTION__, 'ERR1');
ec4d604c 2380 }
bcd14540 2381 catch (Exception $e)
ec4d604c 2382 {
bcd14540
DO
2383 $dbxlink->rollBack();
2384 return buildRedirectURL (__FUNCTION__, 'ERR2');
ec4d604c 2385 }
a7492e95 2386 $dbxlink->commit();
bcd14540 2387 return buildRedirectURL (__FUNCTION__, 'OK', array ($ndone));
07de6bb3
DO
2388}
2389
e0d188ef
DO
2390$msgcode['addVLANSwitchTemplate']['OK'] = 48;
2391$msgcode['addVLANSwitchTemplate']['ERR'] = 110;
2392function addVLANSwitchTemplate()
2393{
2394 assertStringArg ('vst_descr');
2395 global $sic;
2396 $max_local_vlans = NULL;
2397 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2398 {
2399 assertUIntArg ('vst_maxvlans');
2400 $max_local_vlans = $sic['vst_maxvlans'];
2401 }
2402 $result = usePreparedInsertBlade
2403 (
2404 'VLANSwitchTemplate',
2405 array
2406 (
2407 'max_local_vlans' => $max_local_vlans,
2408 'description' => $sic['vst_descr'],
2409 )
2410 );
2411 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2412}
2413
2414$msgcode['delVLANSwitchTemplate']['OK'] = 49;
2415$msgcode['delVLANSwitchTemplate']['ERR'] = 111;
2416function delVLANSwitchTemplate()
2417{
2418 assertUIntArg ('vst_id');
2419 global $sic;
2420 $result = FALSE !== usePreparedDeleteBlade ('VLANSwitchTemplate', array ('id' => $sic['vst_id']));
2421 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2422}
2423
2424$msgcode['updVLANSwitchTemplate']['OK'] = 51;
2425$msgcode['updVLANSwitchTemplate']['ERR'] = 109;
2426function updVLANSwitchTemplate()
2427{
2428 assertUIntArg ('vst_id');
2429 assertStringArg ('vst_descr');
2430 global $sic;
2431 $max_local_vlans = NULL;
2432 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2433 {
2434 assertUIntArg ('vst_maxvlans');
2435 $max_local_vlans = $sic['vst_maxvlans'];
2436 }
2437 $result = commitUpdateVST ($sic['vst_id'], $max_local_vlans, $sic['vst_descr']);
2438 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2439}
2440
2441$msgcode['addVSTRule']['OK'] = 48;
2442$msgcode['addVSTRule']['ERR'] = 110;
2443function addVSTRule()
2444{
2445 assertUIntArg ('vst_id');
2446 assertUIntArg ('rule_no');
2447 assertPCREArg ('port_pcre');
2448 assertStringArg ('port_role');
2449 assertStringArg ('wrt_vlans', TRUE);
2450 global $sic;
2451 $result = usePreparedInsertBlade
2452 (
2453 'VLANSTRule',
2454 array
2455 (
2456 'vst_id' => $sic['vst_id'],
2457 'rule_no' => $sic['rule_no'],
2458 'port_pcre' => $sic['port_pcre'],
2459 'port_role' => $sic['port_role'],
2460 'wrt_vlans' => $sic['wrt_vlans'],
2461 )
2462 );
2463 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2464}
2465
2466$msgcode['delVSTRule']['OK'] = 49;
2467$msgcode['delVSTRule']['ERR'] = 111;
2468function delVSTRule()
2469{
2470 assertUIntArg ('vst_id');
2471 assertUIntArg ('rule_no');
2472 global $sic;
2473 $result = FALSE !== usePreparedDeleteBlade
2474 (
2475 'VLANSTRule',
2476 array
2477 (
2478 'vst_id' => $sic['vst_id'],
2479 'rule_no' => $sic['rule_no']
2480 )
2481 );
2482 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2483}
2484
2485$msgcode['updVSTRule']['OK'] = 51;
2486$msgcode['updVSTRule']['ERR'] = 109;
2487function updVSTRule()
2488{
2489 assertUIntArg ('vst_id');
2490 assertUIntArg ('rule_no');
2491 assertUIntArg ('new_rule_no');
2492 assertPCREArg ('port_pcre');
2493 assertStringArg ('port_role');
2494 assertStringArg ('wrt_vlans', TRUE);
2495 global $sic;
2496 $result = commitUpdateVSTRule
2497 (
2498 $sic['vst_id'],
2499 $sic['rule_no'],
2500 $sic['new_rule_no'],
2501 $sic['port_pcre'],
2502 $sic['port_role'],
2503 $sic['wrt_vlans']
2504 );
2505 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2506}
2507
e673ee24 2508?>