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