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