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