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