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