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