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