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