r4122 addTagForEntity(): use exception
[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{
e97e8866
DO
957 if (!isset ($_REQUEST['got_atoms']))
958 {
959 unset($_GET['page']);
960 unset($_GET['tab']);
961 unset($_GET['op']);
962 unset($_POST['page']);
963 unset($_POST['tab']);
964 unset($_POST['op']);
965 return buildWideRedirectURL (array(), NULL, NULL, array_merge ($_GET, $_POST));
966 }
967 $object_id = $_REQUEST['object_id'];
968 $workingRacksData = array();
969 foreach ($_REQUEST['rackmulti'] as $cand_id)
4fbb5a00 970 {
e97e8866 971 if (!isset ($workingRacksData[$cand_id]))
4fbb5a00 972 {
e97e8866 973 $rackData = spotEntity ('rack', $cand_id);
e97e8866
DO
974 amplifyCell ($rackData);
975 $workingRacksData[$cand_id] = $rackData;
4fbb5a00 976 }
e97e8866 977 }
2135fd83 978
e97e8866
DO
979 foreach ($workingRacksData as &$rd)
980 applyObjectMountMask ($rd, $object_id);
4fbb5a00 981
e97e8866
DO
982 $oldMolecule = getMoleculeForObject ($object_id);
983 $changecnt = 0;
984 $log = array();
985 foreach ($workingRacksData as $rack_id => $rackData)
4fbb5a00 986 {
e97e8866
DO
987 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
988 $log[] = $logrecord;
989 if ($logrecord['code'] == 300)
990 continue;
991 $changecnt++;
992 // Reload our working copy after form processing.
993 $rackData = spotEntity ('rack', $cand_id);
994 amplifyCell ($rackData);
995 applyObjectMountMask ($rackData, $object_id);
996 $workingRacksData[$rack_id] = $rackData;
4fbb5a00 997 }
e97e8866
DO
998 if (!$changecnt)
999 return buildRedirectURL (__FUNCTION__, 'OK', $changecnt);
1000 // Log a record.
1001 $newMolecule = getMoleculeForObject ($object_id);
1002 $oc = count ($oldMolecule);
1003 $nc = count ($newMolecule);
1004 $omid = $oc ? createMolecule ($oldMolecule) : 'NULL';
1005 $nmid = $nc ? createMolecule ($newMolecule) : 'NULL';
1006 global $remote_username;
1007 $comment = empty ($_REQUEST['comment']) ? 'NULL' : "'${_REQUEST['comment']}'";
1008 $query =
1009 "insert into MountOperation(object_id, old_molecule_id, new_molecule_id, user_name, comment) " .
1010 "values (${object_id}, ${omid}, ${nmid}, '${remote_username}', ${comment})";
1011 global $dbxlink;
1012 $result = $dbxlink->query ($query);
1013 if ($result == NULL)
1014 $log[] = array ('code' => 500, 'message' => 'SQL query failed during history logging.');
1015 else
1016 $log[] = array ('code' => 200, 'message' => 'History logged.');
1017 return buildWideRedirectURL ($log);
4fbb5a00
DY
1018}
1019
00e93d63
DO
1020$msgcode['updateObject']['OK'] = 51;
1021$msgcode['updateObject']['ERR'] = 109;
22bb04da
DO
1022function updateObject ()
1023{
0cc24e9a 1024 assertUIntArg ('num_attrs', TRUE);
0cc24e9a
DY
1025 assertStringArg ('object_name', TRUE);
1026 assertStringArg ('object_label', TRUE);
1027 assertStringArg ('object_barcode', TRUE);
1028 assertStringArg ('object_asset_no', TRUE);
22bb04da
DO
1029 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
1030 $has_problems = 'yes';
1031 else
1032 $has_problems = 'no';
1033
1034 if (commitUpdateObject (
1035 $_REQUEST['object_id'],
1036 $_REQUEST['object_name'],
1037 $_REQUEST['object_label'],
1038 $_REQUEST['object_barcode'],
22bb04da
DO
1039 $has_problems,
1040 $_REQUEST['object_asset_no'],
1041 $_REQUEST['object_comment']
1042 ) !== TRUE)
135080d8 1043 return buildRedirectURL (__FUNCTION__, 'ERR');
22bb04da 1044
cdd3efe9 1045 // Update optional attributes
f59e8cf5 1046 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a 1047 $result = array();
0f63538a
DO
1048 $num_attrs = isset ($_REQUEST['num_attrs']) ? $_REQUEST['num_attrs'] : 0;
1049 for ($i = 0; $i < $num_attrs; $i++)
0dfb8a2a 1050 {
0cc24e9a 1051 assertUIntArg ("${i}_attr_id");
0dfb8a2a
DO
1052 $attr_id = $_REQUEST["${i}_attr_id"];
1053
01d82bd2
RF
1054 // 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
1055 if (!strlen ($_REQUEST["${i}_value"]) || ($oldvalues[$attr_id]['type']=='dict' && $_REQUEST["${i}_value"] == 0))
0dfb8a2a 1056 {
103b1e1e 1057 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
1058 continue;
1059 }
1060
1061 // The value could be uint/float, but we don't know ATM. Let SQL
1062 // server check this and complain.
0cc24e9a 1063 assertStringArg ("${i}_value");
0dfb8a2a
DO
1064 $value = $_REQUEST["${i}_value"];
1065 switch ($oldvalues[$attr_id]['type'])
1066 {
1067 case 'uint':
1068 case 'float':
1069 case 'string':
1070 $oldvalue = $oldvalues[$attr_id]['value'];
1071 break;
1072 case 'dict':
1073 $oldvalue = $oldvalues[$attr_id]['key'];
1074 break;
1075 default:
0dfb8a2a 1076 }
59a83bd8 1077 if ($value === $oldvalue) // ('' == 0), but ('' !== 0)
0dfb8a2a
DO
1078 continue;
1079
1080 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 1081 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a 1082 }
103b1e1e 1083 if (in_array (FALSE, $result))
135080d8 1084 return buildRedirectURL (__FUNCTION__, 'ERR');
0dfb8a2a 1085
cdd3efe9
AD
1086 // Invalidate thumb cache of all racks objects could occupy.
1087 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
1088 resetThumbCache ($rack_id);
1089
135080d8 1090 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
1091}
1092
42d73cbf
DY
1093function addMultipleObjects()
1094{
7abce0b7 1095 $log = emptyLog();
42d73cbf 1096 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
1097 $max = getConfigVar ('MASSCOUNT');
1098 for ($i = 0; $i < $max; $i++)
1099 {
1100 if (!isset ($_REQUEST["${i}_object_type_id"]))
1101 {
57777e4a 1102 $log = mergeLogs ($log, oneLiner (184, array ($i + 1)));
42d73cbf
DY
1103 break;
1104 }
0cc24e9a
DY
1105 assertUIntArg ("${i}_object_type_id", TRUE);
1106 assertStringArg ("${i}_object_name", TRUE);
1107 assertStringArg ("${i}_object_label", TRUE);
1108 assertStringArg ("${i}_object_asset_no", TRUE);
1109 assertStringArg ("${i}_object_barcode", TRUE);
7abce0b7 1110 $name = $_REQUEST["${i}_object_name"];
42d73cbf
DY
1111
1112 // It's better to skip silently, than to print a notice.
7abce0b7 1113 if ($_REQUEST["${i}_object_type_id"] == 0)
42d73cbf 1114 continue;
fee7977b 1115 if (($object_id = commitAddObject
7abce0b7
DO
1116 (
1117 $name,
1118 $_REQUEST["${i}_object_label"],
1119 $_REQUEST["${i}_object_barcode"],
1120 $_REQUEST["${i}_object_type_id"],
1121 $_REQUEST["${i}_object_asset_no"],
1122 $taglist
fee7977b
DO
1123 )) !== FALSE){
1124 $info = spotEntity ('object', $object_id);
1125 // FIXME: employ amplifyCell() instead of calling loader functions directly
1126 amplifyCell ($info);
00e93d63 1127 $log = mergeLogs ($log, oneLiner (5, array ('<a href="' . makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) . '">' . $info['dname'] . '</a>')));
fee7977b 1128 }else{
00e93d63 1129 $log = mergeLogs ($log, oneLiner (147, array ($name)));
fee7977b 1130 }
42d73cbf 1131 }
7abce0b7 1132 return buildWideRedirectURL ($log);
42d73cbf
DY
1133}
1134
1135function addLotOfObjects()
1136{
7abce0b7 1137 $log = emptyLog();
42d73cbf 1138 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
0cc24e9a
DY
1139 assertUIntArg ('global_type_id', TRUE);
1140 assertStringArg ('namelist', TRUE);
42d73cbf 1141 $global_type_id = $_REQUEST['global_type_id'];
59a83bd8 1142 if ($global_type_id == 0 or !strlen ($_REQUEST['namelist']))
57777e4a 1143 $log = mergeLogs ($log, oneLiner (186));
42d73cbf
DY
1144 else
1145 {
1146 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
7f42d792 1147 $names1 = explode ("\n", $_REQUEST['namelist']);
42d73cbf
DY
1148 $names2 = array();
1149 foreach ($names1 as $line)
1150 {
1151 $parts = explode ('\r', $line);
1152 reset ($parts);
59a83bd8 1153 if (!strlen ($parts[0]))
42d73cbf
DY
1154 continue;
1155 else
1156 $names2[] = rtrim ($parts[0]);
1157 }
57777e4a 1158 foreach ($names2 as $name)
9d535a0c
DO
1159 if (($object_id = commitAddObject ($name, '', '', $global_type_id, '', $taglist)) !== FALSE)
1160 {
1161 $info = spotEntity ('object', $object_id);
1162 amplifyCell ($info);
00e93d63 1163 $log = mergeLogs ($log, oneLiner (5, array ('<a href="' . makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) . '">' . $info['dname'] . '</a>')));
9d535a0c 1164 }
42d73cbf 1165 else
00e93d63 1166 $log = mergeLogs ($log, oneLiner (147, array ($name)));
42d73cbf 1167 }
7abce0b7 1168 return buildWideRedirectURL ($log);
42d73cbf
DY
1169}
1170
00e93d63 1171$msgcode['deleteObject']['OK'] = 6;
52b34485
AD
1172function deleteObject ()
1173{
0cc24e9a 1174 assertUIntArg ('object_id');
4bb5b87f 1175 $oinfo = spotEntity ('object', $_REQUEST['object_id']);
c78a40ec 1176
f0e2c99f 1177 $racklist = getResidentRacksData ($_REQUEST['object_id'], FALSE);
4bb5b87f 1178 commitDeleteObject ($_REQUEST['object_id']);
f0e2c99f
DO
1179 foreach ($racklist as $rack_id)
1180 resetThumbCache ($rack_id);
abd1e9ac
DO
1181 return buildRedirectURL (__FUNCTION__, 'OK', array ($oinfo['dname']));
1182}
52b34485 1183
00e93d63 1184$msgcode['resetObject']['OK'] = 57;
abd1e9ac
DO
1185function resetObject ()
1186{
abd1e9ac
DO
1187 $oinfo = spotEntity ('object', $_REQUEST['object_id']);
1188
1189 $racklist = getResidentRacksData ($_REQUEST['object_id'], FALSE);
1190 commitResetObject ($_REQUEST['object_id']);
1191 foreach ($racklist as $rack_id)
1192 resetThumbCache ($rack_id);
00e93d63 1193 return buildRedirectURL (__FUNCTION__, 'OK');
52b34485
AD
1194}
1195
00e93d63
DO
1196$msgcode['useupPort']['OK'] = 49;
1197$msgcode['useupPort']['ERR'] = 111;
e673ee24
DO
1198function useupPort ()
1199{
0cc24e9a 1200 assertUIntArg ('port_id');
32832c0e 1201 if (FALSE !== commitUseupPort ($_REQUEST['port_id']))
135080d8 1202 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 1203 else
135080d8 1204 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
1205}
1206
00e93d63 1207$msgcode['updateUI']['OK'] = 51;
4fe32e78
DY
1208function updateUI ()
1209{
0cc24e9a 1210 assertUIntArg ('num_vars');
4fe32e78 1211
103b1e1e 1212 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 1213 {
0cc24e9a
DY
1214 assertStringArg ("${i}_varname");
1215 assertStringArg ("${i}_varvalue", TRUE);
4fe32e78 1216 $varname = $_REQUEST["${i}_varname"];
4fe32e78 1217 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 1218
c461c579 1219 // If form value = value in DB, don't bother updating DB
ed941e67 1220 if (!isConfigVarChanged($varname, $varvalue))
4fe32e78 1221 continue;
3a089a44
DO
1222 // any exceptions will be handled by process.php
1223 setConfigVar ($varname, $varvalue, TRUE);
3540d15c
DY
1224 }
1225 return buildRedirectURL (__FUNCTION__, 'OK');
1226}
1227
1228$msgcode['saveMyPreferences']['OK'] = 56;
3540d15c
DY
1229function saveMyPreferences ()
1230{
1231 assertUIntArg ('num_vars');
1232
1233 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
1234 {
1235 assertStringArg ("${i}_varname");
1236 assertStringArg ("${i}_varvalue", TRUE);
1237 $varname = $_REQUEST["${i}_varname"];
1238 $varvalue = $_REQUEST["${i}_varvalue"];
1239
1240 // If form value = value in DB, don't bother updating DB
ed941e67 1241 if (!isConfigVarChanged($varname, $varvalue))
3540d15c 1242 continue;
3a089a44 1243 setUserConfigVar ($varname, $varvalue);
3540d15c
DY
1244 }
1245 return buildRedirectURL (__FUNCTION__, 'OK');
1246}
1247
1248$msgcode['resetMyPreference']['OK'] = 56;
3540d15c
DY
1249function resetMyPreference ()
1250{
1251 assertStringArg ("varname");
3a089a44 1252 resetUserConfigVar ($_REQUEST["varname"]);
135080d8 1253 return buildRedirectURL (__FUNCTION__, 'OK');
4fe32e78
DY
1254}
1255
b6a7d936 1256$msgcode['resetUIConfig']['OK'] = 57;
b07f617c
DO
1257function resetUIConfig()
1258{
b07f617c
DO
1259 setConfigVar ('MASSCOUNT','15');
1260 setConfigVar ('MAXSELSIZE','30');
b07f617c
DO
1261 setConfigVar ('ROW_SCALE','2');
1262 setConfigVar ('PORTS_PER_ROW','12');
1263 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
1264 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
b5ce46aa
DO
1265 setConfigVar ('DEFAULT_SLB_VS_PORT','');
1266 setConfigVar ('DEFAULT_SLB_RS_PORT','');
72281138 1267 setConfigVar ('DETECT_URLS','no');
4b8d413e 1268 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 1269 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 1270 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
1271 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
1272 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
1273 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 1274 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 1275 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df 1276 setConfigVar ('SHOW_LAST_TAB', 'no');
f0ed1181 1277 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 1278 setConfigVar ('TREE_THRESHOLD', '25');
2be3fd23 1279 setConfigVar ('IPV4_JAYWALK', 'no');
9318d2ef 1280 setConfigVar ('ADDNEW_AT_TOP', 'yes');
64347dcf 1281 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
37e59768
DO
1282 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
1283 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
1284 setConfigVar ('PREVIEW_TEXT_COLS', '80');
1285 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
f3d274bf 1286 setConfigVar ('VENDOR_SIEVE', '');
35bf153d 1287 setConfigVar ('IPV4LB_LISTSRC', '{$typeid_4}');
8fee82b4
DO
1288 setConfigVar ('IPV4OBJ_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_12} or {$typeid_445} or {$typeid_447} or {$typeid_798}');
1289 setConfigVar ('IPV4NAT_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_798}');
35bf153d
DO
1290 setConfigVar ('ASSETWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
1291 setConfigVar ('NAMEWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
f5883ec1 1292 setConfigVar ('RACKS_PER_ROW','12');
590e1281 1293 setConfigVar ('FILTER_PREDICATE_SIEVE','');
23cdc7e9
DO
1294 setConfigVar ('FILTER_DEFAULT_ANDOR','or');
1295 setConfigVar ('FILTER_SUGGEST_ANDOR','yes');
1296 setConfigVar ('FILTER_SUGGEST_TAGS','yes');
1297 setConfigVar ('FILTER_SUGGEST_PREDICATES','yes');
1298 setConfigVar ('FILTER_SUGGEST_EXTRA','no');
8fee82b4 1299 setConfigVar ('DEFAULT_SNMP_COMMUNITY','public');
f06fe423 1300 setConfigVar ('IPV4_ENABLE_KNIGHT','yes');
9e51318b
DO
1301 setConfigVar ('TAGS_TOPLIST_SIZE','50');
1302 setConfigVar ('TAGS_QUICKLIST_SIZE','20');
1303 setConfigVar ('TAGS_QUICKLIST_THRESHOLD','50');
f44fdef9 1304 setConfigVar ('ENABLE_MULTIPORT_FORM', 'no');
2400d7ec
DO
1305 setConfigVar ('DEFAULT_PORT_IIF_ID', '1');
1306 setConfigVar ('DEFAULT_PORT_OIF_IDS', '1=24; 3=1078; 4=1077; 5=1079; 6=1080; 8=1082; 9=1084');
4a47d34b 1307 setConfigVar ('IPV4_TREE_RTR_AS_CELL', 'yes');
cd3775e9 1308 setConfigVar ('PROXIMITY_RANGE', 0);
ca4eb604 1309 setConfigVar ('IPV4_TREE_SHOW_VLAN', 'yes');
407ed7bb
DO
1310 setConfigVar ('VLANSWITCH_LISTSRC', '');
1311 setConfigVar ('VLANIPV4NET_LISTSRC', '');
2fb1f79b
DO
1312 setConfigVar ('DEFAULT_VDOM_ID', '');
1313 setConfigVar ('DEFAULT_VST_ID', '');
01b9a31a 1314 setConfigVar ('STATIC_FILTER', 'yes');
37cb9e18
DO
1315 setConfigVar ('8021Q_DEPLOY_MINAGE', '300');
1316 setConfigVar ('8021Q_DEPLOY_MAXAGE', '3600');
1317 setConfigVar ('8021Q_DEPLOY_RETRY', '10800');
2582446d 1318 setConfigVar ('8021Q_WRI_AFTER_CONFT_LISTSRC', 'false');
4492050b 1319 setConfigVar ('8021Q_INSTANT_DEPLOY', 'no');
b49a479e
DO
1320 setConfigVar ('CDP_RUNNERS_LISTSRC', '');
1321 setConfigVar ('LLDP_RUNNERS_LISTSRC', '');
0328f6d6 1322 setConfigVar ('HNDP_RUNNERS_LISTSRC', '');
95857b5c 1323 setConfigVar ('SHRINK_TAG_TREE_ON_CLICK', 'yes');
1ebbf889 1324 setConfigVar ('MAX_UNFILTERED_ENTITIES', '0');
61e79d63 1325 setConfigVar ('SYNCDOMAIN_MAX_PROCESSES', '0');
135080d8 1326 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
1327}
1328
00e93d63
DO
1329$msgcode['addRealServer']['OK'] = 48;
1330$msgcode['addRealServer']['ERR'] = 110;
732e4578 1331// Add single record.
ca461127
DO
1332function addRealServer ()
1333{
0cc24e9a
DY
1334 assertUIntArg ('pool_id');
1335 assertIPv4Arg ('remoteip');
1336 assertStringArg ('rsport', TRUE);
1337 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1338 if (!addRStoRSPool (
1339 $_REQUEST['pool_id'],
1340 $_REQUEST['remoteip'],
1341 $_REQUEST['rsport'],
1342 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1343 $_REQUEST['rsconfig']
1344 ))
135080d8 1345 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1346 else
135080d8 1347 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1348}
1349
b6a7d936
DO
1350$msgcode['addRealServers']['OK'] = 37;
1351$msgcode['addRealServers']['ERR1'] = 131;
1352$msgcode['addRealServers']['ERR2'] = 127;
732e4578
DO
1353// Parse textarea submitted and try adding a real server for each line.
1354function addRealServers ()
1355{
0cc24e9a
DY
1356 assertStringArg ('format');
1357 assertStringArg ('rawtext');
732e4578
DO
1358 $ngood = $nbad = 0;
1359 $rsconfig = '';
1360 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
7f42d792 1361 foreach (explode ("\n", dos2unix ($_REQUEST['rawtext'])) as $line)
732e4578 1362 {
59a83bd8 1363 if (!strlen ($line))
732e4578
DO
1364 continue;
1365 $match = array ();
1366 switch ($_REQUEST['format'])
1367 {
1368 case 'ipvs_2': // address and port only
1369 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1370 continue;
103b1e1e 1371 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1372 $ngood++;
1373 else
1374 $nbad++;
1375 break;
1376 case 'ipvs_3': // address, port and weight
1377 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1378 continue;
103b1e1e 1379 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1380 $ngood++;
1381 else
1382 $nbad++;
1383 break;
e69c2aa3
DO
1384 case 'ssv_2': // IP address and port
1385 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1386 continue;
103b1e1e 1387 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1388 $ngood++;
1389 else
1390 $nbad++;
1391 break;
948c37e3
DO
1392 case 'ssv_1': // IP address
1393 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1394 continue;
1395 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1396 $ngood++;
1397 else
1398 $nbad++;
1399 break;
732e4578 1400 default:
135080d8 1401 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1402 break;
1403 }
1404 }
1405 if ($nbad == 0 and $ngood > 0)
135080d8 1406 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1407 else
135080d8 1408 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1409}
1410
00e93d63 1411$msgcode['addVService']['OK'] = 48;
d6517a21
DO
1412function addVService ()
1413{
0cc24e9a
DY
1414 assertIPv4Arg ('vip');
1415 assertUIntArg ('vport');
a9ec91cf 1416 genericAssertion ('proto', 'enum/ipproto');
0cc24e9a
DY
1417 assertStringArg ('name', TRUE);
1418 assertStringArg ('vsconfig', TRUE);
1419 assertStringArg ('rsconfig', TRUE);
a9ec91cf 1420 usePreparedExecuteBlade
c63a8d6e 1421 (
a9ec91cf
DO
1422 'INSERT INTO IPv4VS (vip, vport, proto, name, vsconfig, rsconfig) VALUES (INET_ATON(?), ?, ?, ?, ?, ?)',
1423 array
1424 (
1425 $_REQUEST['vip'],
1426 $_REQUEST['vport'],
1427 $_REQUEST['proto'],
1428 !mb_strlen ($_REQUEST['name']) ? NULL : $_REQUEST['name'],
1429 !strlen ($_REQUEST['vsconfig']) ? NULL : $_REQUEST['vsconfig'],
1430 !strlen ($_REQUEST['rsconfig']) ? NULL : $_REQUEST['rsconfig'],
1431 )
c63a8d6e 1432 );
a9ec91cf
DO
1433 produceTagsForLastRecord ('ipv4vs', isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array());
1434 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1435}
1436
00e93d63
DO
1437$msgcode['deleteVService']['OK'] = 49;
1438$msgcode['deleteVService']['ERR'] = 111;
d6517a21
DO
1439function deleteVService ()
1440{
0cc24e9a 1441 assertUIntArg ('vs_id');
e02e1941 1442 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1443 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1444 else
135080d8 1445 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1446}
1447
1f54e1ba
DO
1448$msgcode['updateSLBDefConfig']['OK'] = 43;
1449$msgcode['updateSLBDefConfig']['ERR'] = 109;
1450function updateSLBDefConfig ()
1451{
1452 $data = array(
1453 'vs' => $_REQUEST['vsconfig'],
1454 'rs' => $_REQUEST['rsconfig']
1455 );
1456 if (!commitUpdateSLBDefConf ($data))
1457 return buildRedirectURL (__FUNCTION__, 'ERR');
1458 else
1459 return buildRedirectURL (__FUNCTION__, 'OK');
1460}
1461
00e93d63
DO
1462$msgcode['updateRealServer']['OK'] = 51;
1463$msgcode['updateRealServer']['ERR'] = 109;
fb1c4a54
DO
1464function updateRealServer ()
1465{
0cc24e9a
DY
1466 assertUIntArg ('rs_id');
1467 assertIPv4Arg ('rsip');
1468 assertStringArg ('rsport', TRUE);
1469 assertStringArg ('rsconfig', TRUE);
a5c589d2 1470 if (FALSE === commitUpdateRS (
103b1e1e
DO
1471 $_REQUEST['rs_id'],
1472 $_REQUEST['rsip'],
1473 $_REQUEST['rsport'],
1474 $_REQUEST['rsconfig']
1475 ))
135080d8 1476 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1477 else
135080d8 1478 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1479}
1480
00e93d63
DO
1481$msgcode['updateLoadBalancer']['OK'] = 51;
1482$msgcode['updateLoadBalancer']['ERR'] = 109;
c1ca768c 1483function updateLoadBalancer ()
3241551e 1484{
0cc24e9a
DY
1485 assertUIntArg ('object_id');
1486 assertUIntArg ('pool_id');
1487 assertUIntArg ('vs_id');
1488 assertStringArg ('vsconfig', TRUE);
1489 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1490 if (! empty($_REQUEST['prio']))
1491 assertUIntArg('prio', TRUE);
1492
a5c589d2 1493 if (FALSE === commitUpdateLB (
103b1e1e
DO
1494 $_REQUEST['object_id'],
1495 $_REQUEST['pool_id'],
1496 $_REQUEST['vs_id'],
1497 $_REQUEST['vsconfig'],
1f54e1ba
DO
1498 $_REQUEST['rsconfig'],
1499 $_REQUEST['prio']
103b1e1e 1500 ))
135080d8 1501 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1502 else
135080d8 1503 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1504}
1505
00e93d63
DO
1506$msgcode['updateVService']['OK'] = 51;
1507$msgcode['updateVService']['ERR'] = 109;
d6517a21
DO
1508function updateVService ()
1509{
0cc24e9a
DY
1510 assertUIntArg ('vs_id');
1511 assertIPv4Arg ('vip');
1512 assertUIntArg ('vport');
1d84140d 1513 genericAssertion ('proto', 'enum/ipproto');
0cc24e9a
DY
1514 assertStringArg ('name', TRUE);
1515 assertStringArg ('vsconfig', TRUE);
1516 assertStringArg ('rsconfig', TRUE);
a5c589d2 1517 if (FALSE === commitUpdateVS (
103b1e1e
DO
1518 $_REQUEST['vs_id'],
1519 $_REQUEST['vip'],
1520 $_REQUEST['vport'],
1521 $_REQUEST['proto'],
1522 $_REQUEST['name'],
1523 $_REQUEST['vsconfig'],
1524 $_REQUEST['rsconfig']
1525 ))
135080d8 1526 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1527 else
135080d8 1528 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1529}
1530
00e93d63
DO
1531$msgcode['addLoadBalancer']['OK'] = 48;
1532$msgcode['addLoadBalancer']['ERR'] = 110;
3241551e
DO
1533function addLoadBalancer ()
1534{
0cc24e9a
DY
1535 assertUIntArg ('pool_id');
1536 assertUIntArg ('object_id');
1537 assertUIntArg ('vs_id');
1538 assertStringArg ('vsconfig', TRUE);
1539 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1540 if (! empty($_REQUEST['prio']))
1541 assertUIntArg('prio', TRUE);
1542
103b1e1e
DO
1543 if (!addLBtoRSPool (
1544 $_REQUEST['pool_id'],
1545 $_REQUEST['object_id'],
1546 $_REQUEST['vs_id'],
1547 $_REQUEST['vsconfig'],
1f54e1ba
DO
1548 $_REQUEST['rsconfig'],
1549 $_REQUEST['prio']
103b1e1e 1550 ))
135080d8 1551 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1552 else
135080d8 1553 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1554}
1555
00e93d63 1556$msgcode['addRSPool']['OK'] = 48;
b6a7d936 1557$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1558function addRSPool ()
1559{
0cc24e9a
DY
1560 assertStringArg ('name', TRUE);
1561 assertStringArg ('vsconfig', TRUE);
1562 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1563 $error = commitCreateRSPool
1564 (
103b1e1e
DO
1565 $_REQUEST['name'],
1566 $_REQUEST['vsconfig'],
c63a8d6e
DO
1567 $_REQUEST['rsconfig'],
1568 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1569 );
1570 if ($error != '')
135080d8 1571 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1572 else
135080d8 1573 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1574}
1575
00e93d63
DO
1576$msgcode['deleteRSPool']['OK'] = 49;
1577$msgcode['deleteRSPool']['ERR'] = 111;
5ad76f01
DO
1578function deleteRSPool ()
1579{
0cc24e9a 1580 assertUIntArg ('pool_id');
654e4636 1581 if (commitDeleteRSPool ($_REQUEST['pool_id']) === FALSE)
135080d8 1582 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1583 else
135080d8 1584 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1585}
1586
00e93d63
DO
1587$msgcode['updateRSPool']['OK'] = 51;
1588$msgcode['updateRSPool']['ERR'] = 109;
5ad76f01
DO
1589function updateRSPool ()
1590{
0cc24e9a
DY
1591 assertStringArg ('name', TRUE);
1592 assertStringArg ('vsconfig', TRUE);
1593 assertStringArg ('rsconfig', TRUE);
1f54e1ba
DO
1594 if (FALSE === commitUpdateRSPool
1595 (
1596 $_REQUEST['pool_id'],
1597 $_REQUEST['name'],
1598 $_REQUEST['vsconfig'],
1599 $_REQUEST['rsconfig']
1600 )
1601 )
135080d8 1602 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1603 else
135080d8 1604 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1605}
1606
00e93d63
DO
1607$msgcode['updateRSInService']['OK'] = 26;
1608$msgcode['updateRSInService']['ERR'] = 141;
1f7d18fa
DO
1609function updateRSInService ()
1610{
0cc24e9a 1611 assertUIntArg ('rscount');
841a7a7a 1612 $pool_id = $_REQUEST['pool_id'];
a6e91ac2
DO
1613 $orig = spotEntity ('ipv4rspool', $pool_id);
1614 amplifyCell ($orig);
1f7d18fa
DO
1615 $nbad = $ngood = 0;
1616 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1617 {
1618 $rs_id = $_REQUEST["rsid_${i}"];
1619 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1620 $newval = 'yes';
1621 else
1622 $newval = 'no';
1623 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1624 {
a5c589d2 1625 if (FALSE !== commitSetInService ($rs_id, $newval))
1f7d18fa
DO
1626 $ngood++;
1627 else
1628 $nbad++;
1629 }
1630 }
1631 if (!$nbad)
135080d8 1632 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1633 else
135080d8 1634 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1635}
1636
b6a7d936
DO
1637$msgcode['importPTRData']['OK'] = 26;
1638$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1639// FIXME: check, that each submitted address belongs to the prefix we
1640// are operating on.
03eb5209
DO
1641function importPTRData ()
1642{
0cc24e9a 1643 assertUIntArg ('addrcount');
03eb5209 1644 $nbad = $ngood = 0;
03eb5209
DO
1645 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1646 {
3f3bd41e 1647 $inputname = "import_${i}";
03eb5209
DO
1648 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1649 continue;
0cc24e9a
DY
1650 assertIPv4Arg ("addr_${i}");
1651 assertStringArg ("descr_${i}", TRUE);
1652 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1653 // Non-existent addresses will not have this argument set in request.
1654 $rsvd = 'no';
03eb5209
DO
1655 if ($_REQUEST["rsvd_${i}"] == 'yes')
1656 $rsvd = 'yes';
3f3bd41e 1657 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1658 $ngood++;
1659 else
1660 $nbad++;
1661 }
1662 if (!$nbad)
135080d8 1663 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1664 else
135080d8 1665 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1666}
1667
b6a7d936
DO
1668$msgcode['generateAutoPorts']['OK'] = 21;
1669$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1670function generateAutoPorts ()
1671{
103b1e1e 1672 global $pageno;
6297d584 1673 $info = spotEntity ('object', $_REQUEST['object_id']);
f3f0161f 1674 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1675 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1676 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1677 else
135080d8 1678 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1679}
1680
00e93d63 1681$msgcode['saveEntityTags']['OK'] = 26;
b6a7d936 1682$msgcode['saveEntityTags']['ERR1'] = 143;
24cbe8af 1683// Filter out implicit tags before storing the new tag set.
3355ca56 1684function saveEntityTags ()
24cbe8af 1685{
1d356026
DO
1686 global $pageno, $etype_by_pageno;
1687 if (!isset ($etype_by_pageno[$pageno]))
1688 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
be2ae2a2 1689 $realm = $etype_by_pageno[$pageno];
1d356026 1690 $entity_id = getBypassValue();
e04931c8 1691 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1692 // Build a chain from the submitted data, minimize it,
ab379543 1693 // then wipe existing records and store the new set instead.
01b6b4d6 1694 destroyTagsForEntity ($realm, $entity_id);
eb27afad
DO
1695 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1696 // so why not use it?
6e49bd1f 1697 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1698 $n_succeeds = $n_errors = 0;
6e49bd1f 1699 foreach ($newchain as $taginfo)
eb6ea26f 1700 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1701 $n_succeeds++;
1702 else
1703 $n_errors++;
ab379543 1704 if ($n_errors)
9a61c175 1705 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1706 else
135080d8 1707 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1708}
1709
b6a7d936
DO
1710$msgcode['rollTags']['OK'] = 67;
1711$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1712function rollTags ()
1713{
0cc24e9a
DY
1714 assertStringArg ('sum', TRUE);
1715 assertUIntArg ('realsum');
eb6ea26f 1716 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1717 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1718 // Even if the user requested an empty tag list, don't bail out, but process existing
1719 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1720 // have its chains refined to "normal" form.
1721 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1722 $n_ok = 0;
1723 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1724 // filter out the same tag again and again. It will have own noise to cancel.
1725 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
61a1d996 1726 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
abef7149 1727 {
61a1d996 1728 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
abef7149 1729 $n_ok++;
61a1d996
DO
1730 amplifyCell ($rack);
1731 foreach ($rack['mountedObjects'] as $object_id)
abef7149
DO
1732 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1733 $n_ok++;
1734 }
135080d8 1735 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1736}
1737
00e93d63 1738$msgcode['changeMyPassword']['OK'] = 51;
b6a7d936
DO
1739$msgcode['changeMyPassword']['ERR1'] = 150;
1740$msgcode['changeMyPassword']['ERR2'] = 151;
1741$msgcode['changeMyPassword']['ERR3'] = 152;
1742$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1743function changeMyPassword ()
cced6b7d 1744{
b82cce3f 1745 global $remote_username, $user_auth_src;
204284ba 1746 if ($user_auth_src != 'database')
135080d8 1747 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1748 assertStringArg ('oldpassword');
1749 assertStringArg ('newpassword1');
1750 assertStringArg ('newpassword2');
b82cce3f 1751 $remote_userid = getUserIDByUsername ($remote_username);
0b2c74cb 1752 $userinfo = spotEntity ('user', $remote_userid);
b82cce3f 1753 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1754 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1755 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1756 return buildRedirectURL (__FUNCTION__, 'ERR3');
32832c0e 1757 if (FALSE !== commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1758 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1759 else
135080d8 1760 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1761}
1762
b6a7d936
DO
1763$msgcode['saveRackCode']['OK'] = 43;
1764$msgcode['saveRackCode']['ERR1'] = 154;
1765$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1766function saveRackCode ()
1767{
cced6b7d 1768 assertStringArg ('rackcode');
e6a4adb9 1769 // For the test to succeed, unescape LFs, strip CRs.
7f42d792 1770 $newcode = dos2unix ($_REQUEST['rackcode']);
cf25e649
DO
1771 $parseTree = getRackCode ($newcode);
1772 if ($parseTree['result'] != 'ACK')
135080d8 1773 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
bb09f49c
DO
1774 if (FALSE !== saveScript ('RackCode', $newcode))
1775 {
1776 saveScript ('RackCodeCache', base64_encode (serialize ($parseTree)));
135080d8 1777 return buildRedirectURL (__FUNCTION__, 'OK');
bb09f49c
DO
1778 }
1779 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1780}
1781
3f052a67 1782$msgcode['setPortVLAN']['ERR'] = 164;
46f92ff7
DO
1783// This handler's context is pre-built, but not authorized. It is assumed, that the
1784// handler will take existing context and before each commit check authorization
1785// on the base chain plus necessary context added.
1786function setPortVLAN ()
1787{
0cc24e9a 1788 assertUIntArg ('portcount');
3f052a67
DO
1789 try
1790 {
1791 $data = getSwitchVLANs ($_REQUEST['object_id']);
1792 }
1793 catch (RTGatewayError $re)
1794 {
1795 return buildRedirectURL (__FUNCTION__, 'ERR', array ($re->getMessage()));
1796 }
46f92ff7
DO
1797 list ($vlanlist, $portlist) = $data;
1798 // Here we just build up 1 set command for the gateway with all of the ports
1799 // included. The gateway is expected to filter unnecessary changes silently
1800 // and to provide a list of responses with either error or success message
1801 // for each of the rest.
1802 $nports = $_REQUEST['portcount'];
1803 $prefix = 'set ';
db55cf54 1804 $log = emptyLog();
46f92ff7
DO
1805 $setcmd = '';
1806 for ($i = 0; $i < $nports; $i++)
3f052a67
DO
1807 {
1808 genericAssertion ('portname_' . $i, 'string');
1809 genericAssertion ('vlanid_' . $i, 'string');
1810 if ($_REQUEST['portname_' . $i] != $portlist[$i]['portname'])
1811 throw new InvalidRequestArgException ('portname_' . $i, $_REQUEST['portname_' . $i], 'expected to be ' . $portlist[$i]['portname']);
46f92ff7 1812 if
46f92ff7
DO
1813 (
1814 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1815 $portlist[$i]['vlaind'] == 'TRUNK'
1816 )
1817 continue;
3f052a67
DO
1818 $portname = $_REQUEST['portname_' . $i];
1819 $oldvlanid = $portlist[$i]['vlanid'];
1820 $newvlanid = $_REQUEST['vlanid_' . $i];
1821 if
1822 (
1823 !permitted (NULL, NULL, NULL, array (array ('tag' => '$fromvlan_' . $oldvlanid))) or
1824 !permitted (NULL, NULL, NULL, array (array ('tag' => '$tovlan_' . $newvlanid)))
1825 )
46f92ff7 1826 {
1d84140d 1827 $log = mergeLogs ($log, oneLiner (159, array ($portname, $oldvlanid, $newvlanid)));
3f052a67 1828 continue;
46f92ff7 1829 }
3f052a67
DO
1830 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1831 $prefix = ';';
1832 }
46f92ff7 1833 // Feed the gateway and interpret its (non)response.
1d84140d
DO
1834 if ($setcmd == '')
1835 $log = mergeLogs ($log, oneLiner (201));
46f92ff7 1836 else
1d84140d
DO
1837 {
1838 try
1839 {
1840 $log = mergeLogs ($log, setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1841 }
1842 catch (RTGatewayError $e)
1843 {
1844 $log = mergeLogs ($log, oneLiner (164, $e->getMessage()));
1845 }
1846 }
46f92ff7
DO
1847 return buildWideRedirectURL ($log);
1848}
1849
12b0c847
DO
1850$msgcode['submitSLBConfig']['OK'] = 66;
1851$msgcode['submitSLBConfig']['ERR'] = 164;
2987fc1f
DO
1852function submitSLBConfig ()
1853{
2987fc1f 1854 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
12b0c847
DO
1855 try
1856 {
1857 gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
1858 }
3a089a44 1859 catch (RTGatewayError $e)
12b0c847 1860 {
3a089a44 1861 return buildRedirectURL (__FUNCTION__, 'ERR', array ($e->getMessage()));
12b0c847
DO
1862 }
1863 return buildRedirectURL (__FUNCTION__, 'OK', array ('slbconfig'));
2987fc1f
DO
1864}
1865
00e93d63 1866$msgcode['addRack']['OK'] = 51;
b6a7d936 1867$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1868function addRack ()
1869{
f19c75d6
DO
1870 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1871 if (isset ($_REQUEST['got_data']))
1872 {
0cc24e9a
DY
1873 assertStringArg ('rack_name');
1874 assertUIntArg ('rack_height1');
1875 assertStringArg ('rack_comment', TRUE);
00e93d63
DO
1876 commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist);
1877 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6
DO
1878 }
1879 elseif (isset ($_REQUEST['got_mdata']))
1880 {
0cc24e9a
DY
1881 assertUIntArg ('rack_height2');
1882 assertStringArg ('rack_names', TRUE);
db55cf54 1883 $log = emptyLog();
f19c75d6 1884 // copy-and-paste from renderAddMultipleObjectsForm()
7f42d792 1885 $names1 = explode ("\n", $_REQUEST['rack_names']);
f19c75d6
DO
1886 $names2 = array();
1887 foreach ($names1 as $line)
1888 {
1889 $parts = explode ('\r', $line);
1890 reset ($parts);
59a83bd8 1891 if (!strlen ($parts[0]))
f19c75d6
DO
1892 continue;
1893 else
1894 $names2[] = rtrim ($parts[0]);
1895 }
b3029d5c 1896 global $msgcode;
f19c75d6 1897 foreach ($names2 as $cname)
1d84140d
DO
1898 {
1899 commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist);
1900 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
1901 }
f19c75d6
DO
1902 return buildWideRedirectURL ($log);
1903 }
1904 else
135080d8 1905 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1906}
1907
00e93d63 1908$msgcode['deleteRack']['OK'] = 6;
b6a7d936
DO
1909$msgcode['deleteRack']['ERR'] = 100;
1910$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1911function deleteRack ()
1912{
0cc24e9a 1913 assertUIntArg ('rack_id');
6538d363 1914 $rackData = spotEntity ('rack', $_REQUEST['rack_id']);
61a1d996
DO
1915 amplifyCell ($rackData);
1916 if (count ($rackData['mountedObjects']))
c8187437 1917 return buildRedirectURL (__FUNCTION__, 'ERR1');
61a1d996
DO
1918 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1919 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1920 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
c8187437
DY
1921}
1922
00e93d63
DO
1923$msgcode['updateRack']['OK'] = 7;
1924$msgcode['updateRack']['ERR'] = 109;
7056988c
DO
1925function updateRack ()
1926{
0cc24e9a
DY
1927 assertUIntArg ('rack_row_id');
1928 assertUIntArg ('rack_height');
1929 assertStringArg ('rack_name');
1930 assertStringArg ('rack_comment', TRUE);
7056988c 1931
7056988c 1932 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1933 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1934 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1935 else
135080d8 1936 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1937}
1938
4fbb5a00
DY
1939function updateRackDesign ()
1940{
6cfedb71 1941 $rackData = spotEntity ('rack', $_REQUEST['rack_id']);
61a1d996 1942 amplifyCell ($rackData);
4fbb5a00
DY
1943 applyRackDesignMask($rackData);
1944 markupObjectProblems ($rackData);
1945 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1946 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1947}
1948
1949function updateRackProblems ()
1950{
6cfedb71 1951 $rackData = spotEntity ('rack', $_REQUEST['rack_id']);
61a1d996 1952 amplifyCell ($rackData);
4fbb5a00
DY
1953 applyRackProblemMask($rackData);
1954 markupObjectProblems ($rackData);
1955 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1956 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1957}
1958
7056988c
DO
1959function querySNMPData ()
1960{
8228bdb4
DO
1961 assertStringArg ('community', TRUE);
1962
1963 $snmpsetup = array ();
011b6699 1964 if ($_REQUEST['community'] != '')
1d84140d
DO
1965 $snmpsetup['community'] = $_REQUEST['community'];
1966 else
1967 {
8228bdb4
DO
1968 assertStringArg ('sec_name');
1969 assertStringArg ('sec_level');
1970 assertStringArg ('auth_protocol');
1971 assertStringArg ('auth_passphrase', TRUE);
1972 assertStringArg ('priv_protocol');
1973 assertStringArg ('priv_passphrase', TRUE);
1974
1975 $snmpsetup['sec_name'] = $_REQUEST['sec_name'];
1976 $snmpsetup['sec_level'] = $_REQUEST['sec_level'];
1977 $snmpsetup['auth_protocol'] = $_REQUEST['auth_protocol'];
1978 $snmpsetup['auth_passphrase'] = $_REQUEST['auth_passphrase'];
1979 $snmpsetup['priv_protocol'] = $_REQUEST['priv_protocol'];
1980 $snmpsetup['priv_passphrase'] = $_REQUEST['priv_passphrase'];
1981 }
8228bdb4 1982 return doSNMPmining ($_REQUEST['object_id'], $snmpsetup);
7056988c
DO
1983}
1984
00e93d63 1985$msgcode['addFileWithoutLink']['OK'] = 5;
6cfedb71 1986$msgcode['addFileWithoutLink']['ERR2'] = 110;
e1ae3fb4
AD
1987// File-related functions
1988function addFileWithoutLink ()
1989{
0cc24e9a 1990 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1991
1992 // Make sure the file can be uploaded
1993 if (get_cfg_var('file_uploads') != 1)
00e93d63 1994 throw new RackTablesError ('file uploads not allowed, change "file_uploads" parameter in php.ini', RackTablesError::MISCONFIGURED);
e1ae3fb4
AD
1995
1996 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650 1997 global $sic;
6cfedb71
DO
1998 if (FALSE === commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']))
1999 return buildRedirectURL (__FUNCTION__, 'ERR2');
f857f71f
DO
2000 if (isset ($_REQUEST['taglist']))
2001 produceTagsForLastRecord ('file', $_REQUEST['taglist']);
6cfedb71 2002 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($_FILES['file']['name'])));
e1ae3fb4
AD
2003}
2004
00e93d63 2005$msgcode['addFileToEntity']['OK'] = 5;
b6a7d936 2006$msgcode['addFileToEntity']['ERR2'] = 181;
6cfedb71 2007$msgcode['addFileToEntity']['ERR3'] = 110;
e1ae3fb4
AD
2008function addFileToEntity ()
2009{
1d356026
DO
2010 global $pageno, $etype_by_pageno;
2011 if (!isset ($etype_by_pageno[$pageno]))
2012 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
9a61c175 2013 $realm = $etype_by_pageno[$pageno];
1d356026 2014 $entity_id = getBypassValue();
0cc24e9a 2015 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
2016
2017 // Make sure the file can be uploaded
2018 if (get_cfg_var('file_uploads') != 1)
9a61c175 2019 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
2020
2021 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650 2022 global $sic;
6cfedb71
DO
2023 if (FALSE === commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']))
2024 return buildRedirectURL (__FUNCTION__, 'ERR3');
2025 if (FALSE === commitLinkFile (lastInsertID(), $realm, $entity_id))
2026 return buildRedirectURL (__FUNCTION__, 'ERR3');
e1ae3fb4 2027
6cfedb71 2028 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($_FILES['file']['name'])));
e1ae3fb4
AD
2029}
2030
b6a7d936 2031$msgcode['linkFileToEntity']['OK'] = 71;
6cfedb71 2032$msgcode['linkFileToEntity']['ERR2'] = 110;
e1ae3fb4
AD
2033function linkFileToEntity ()
2034{
0cc24e9a 2035 assertUIntArg ('file_id');
1d356026
DO
2036 global $pageno, $etype_by_pageno;
2037 if (!isset ($etype_by_pageno[$pageno]))
2038 throw new RackTablesError ('key not found in etype_by_pageno', RackTablesError::INTERNAL);
121496b6 2039
d3b5008b 2040 $fi = spotEntity ('file', $_REQUEST['file_id']);
1d356026 2041 if (FALSE === commitLinkFile ($_REQUEST['file_id'], $etype_by_pageno[$pageno], getBypassValue()))
6cfedb71 2042 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4 2043
6cfedb71 2044 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($fi['name'])));
e1ae3fb4
AD
2045}
2046
00e93d63 2047$msgcode['replaceFile']['OK'] = 7;
b6a7d936
DO
2048$msgcode['replaceFile']['ERR1'] = 181;
2049$msgcode['replaceFile']['ERR2'] = 207;
6cfedb71 2050$msgcode['replaceFile']['ERR3'] = 109;
fd1fb420
AD
2051function replaceFile ()
2052{
db55cf54 2053 global $sic;
fd1fb420
AD
2054
2055 // Make sure the file can be uploaded
2056 if (get_cfg_var('file_uploads') != 1)
db55cf54 2057 return buildRedirectURL (__FUNCTION__, 'ERR1');
d3b5008b 2058 $shortInfo = spotEntity ('file', $sic['file_id']);
fd1fb420
AD
2059
2060 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
2061 if ($fp === FALSE)
2062 return buildRedirectURL (__FUNCTION__, 'ERR2');
6cfedb71
DO
2063 if (FALSE === commitReplaceFile ($sic['file_id'], $fp))
2064 return buildRedirectURL (__FUNCTION__, 'ERR3');
fd1fb420 2065
d3346ce2
DO
2066 usePreparedExecuteBlade
2067 (
2068 'UPDATE File SET thumbnail = NULL WHERE id = ?',
2069 $sic['file_id']
2070 );
2071
db55cf54 2072 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
2073}
2074
b6a7d936 2075$msgcode['updateFile']['OK'] = 70;
6cfedb71 2076$msgcode['updateFile']['ERR'] = 109;
e1ae3fb4
AD
2077function updateFile ()
2078{
0cc24e9a
DY
2079 assertStringArg ('file_name');
2080 assertStringArg ('file_type');
2081 assertStringArg ('file_comment', TRUE);
f3d274bf 2082 global $sic;
6cfedb71
DO
2083 if (FALSE === commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']))
2084 return buildRedirectURL (__FUNCTION__, 'ERR');
7221c918 2085 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['file_name']));
e1ae3fb4
AD
2086}
2087
b6a7d936 2088$msgcode['unlinkFile']['OK'] = 72;
00e93d63 2089$msgcode['unlinkFile']['ERR'] = 111;
e1ae3fb4
AD
2090function unlinkFile ()
2091{
0cc24e9a 2092 assertUIntArg ('link_id');
00e93d63 2093 return buildRedirectURL (__FUNCTION__, commitUnlinkFile ($_REQUEST['link_id']) === FALSE ? 'ERR' : 'OK');
e1ae3fb4
AD
2094}
2095
00e93d63 2096$msgcode['deleteFile']['OK'] = 6;
1d84140d 2097$msgcode['deleteFile']['ERR'] = 111;
e1ae3fb4
AD
2098function deleteFile ()
2099{
0cc24e9a 2100 assertUIntArg ('file_id');
d3b5008b 2101 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
e1ae3fb4
AD
2102 $error = commitDeleteFile ($_REQUEST['file_id']);
2103
2104 if ($error != '')
135080d8 2105 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 2106
f8874cdb 2107 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
2108}
2109
00e93d63 2110$msgcode['updateFileText']['OK'] = 7;
b6a7d936 2111$msgcode['updateFileText']['ERR1'] = 179;
00e93d63 2112$msgcode['updateFileText']['ERR2'] = 155;
8628ae44
DO
2113function updateFileText ()
2114{
0cc24e9a
DY
2115 assertStringArg ('mtime_copy');
2116 assertStringArg ('file_text', TRUE); // it's Ok to save empty
d3b5008b 2117 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
70cb9b56
DO
2118 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
2119 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb 2120 global $sic;
6cfedb71
DO
2121 if (FALSE === commitReplaceFile ($sic['file_id'], $sic['file_text']))
2122 return buildRedirectURL (__FUNCTION__, 'ERR2');
2123 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
8628ae44
DO
2124}
2125
2dfa1b73
DO
2126$msgcode['addPortInterfaceCompat']['OK'] = 48;
2127$msgcode['addPortInterfaceCompat']['ERR'] = 110;
2128function addPortInterfaceCompat ()
2129{
0cc24e9a
DY
2130 assertUIntArg ('iif_id');
2131 assertUIntArg ('oif_id');
2dfa1b73
DO
2132 if (commitSupplementPIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
2133 return buildRedirectURL (__FUNCTION__, 'OK');
2134 return buildRedirectURL (__FUNCTION__, 'ERR');
2135}
2136
2400d7ec
DO
2137$ifcompatpack = array
2138(
2139 '1000cwdm80' => array (1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216),
2140 '1000dwdm80' => array // ITU channels 20~61
2141 (
2142 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226,
2143 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236,
2144 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
2145 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
2146 1257, 1258
2147 ),
2148 '10000dwdm80' => array // same channels for 10GE
2149 (
2150 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268,
2151 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278,
2152 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
2153 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298,
2154 1299, 1300
2155 ),
2156);
2157
3d9ac3d4 2158$msgcode['addPortInterfaceCompatPack']['OK'] = 44;
2400d7ec
DO
2159function addPortInterfaceCompatPack ()
2160{
00e93d63
DO
2161 genericAssertion ('standard', 'enum/wdmstd');
2162 genericAssertion ('iif_id', 'iif');
3d9ac3d4 2163 global $ifcompatpack;
3d9ac3d4
DO
2164 $ngood = $nbad = 0;
2165 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2166 if (commitSupplementPIC ($_REQUEST['iif_id'], $oif_id))
2167 $ngood++;
2168 else
2169 $nbad++;
2170 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2171}
2172
3d9ac3d4
DO
2173$msgcode['delPortInterfaceCompatPack']['OK'] = 44;
2174$msgcode['delPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2175function delPortInterfaceCompatPack ()
2176{
0cc24e9a
DY
2177 assertStringArg ('standard');
2178 assertUIntArg ('iif_id');
10005279
DO
2179 global $ifcompatpack, $sic;
2180 if (!array_key_exists ($sic['standard'], $ifcompatpack) or !array_key_exists ($sic['iif_id'], getPortIIFOptions()))
3d9ac3d4
DO
2181 return buildRedirectURL (__FUNCTION__, 'ERR');
2182 $ngood = $nbad = 0;
10005279
DO
2183 foreach ($ifcompatpack[$sic['standard']] as $oif_id)
2184 if (usePreparedDeleteBlade ('PortInterfaceCompat', array ('iif_id' => $sic['iif_id'], 'oif_id' => $oif_id)))
3d9ac3d4
DO
2185 $ngood++;
2186 else
2187 $nbad++;
2188 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2189}
2190
38cd7704 2191$msgcode['add8021QOrder']['OK'] = 48;
00e93d63 2192$msgcode['add8021QOrder']['ERR'] = 110;
38cd7704 2193function add8021QOrder ()
8198f2c6 2194{
80ec1f4f
DO
2195 assertUIntArg ('vdom_id');
2196 assertUIntArg ('object_id');
38cd7704 2197 assertUIntArg ('vst_id');
c1aa3ada
DO
2198 global $sic;
2199 $result = usePreparedExecuteBlade
8198f2c6 2200 (
c1aa3ada
DO
2201 'INSERT INTO VLANSwitch (domain_id, object_id, template_id, last_change, out_of_sync) ' .
2202 'VALUES (?, ?, ?, NOW(), "yes")',
2203 array ($sic['vdom_id'], $sic['object_id'], $sic['vst_id'])
8198f2c6 2204 );
c1aa3ada 2205 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
8198f2c6
DO
2206}
2207
38cd7704 2208$msgcode['del8021QOrder']['OK'] = 49;
00e93d63 2209$msgcode['del8021QOrder']['ERR'] = 111;
38cd7704 2210function del8021QOrder ()
8198f2c6 2211{
80ec1f4f 2212 assertUIntArg ('object_id');
4191d7ab
AA
2213 assertUIntArg ('vdom_id');
2214 assertUIntArg ('vst_id');
8198f2c6 2215 global $sic;
38cd7704 2216 $result = usePreparedDeleteBlade ('VLANSwitch', array ('object_id' => $sic['object_id']));
4191d7ab
AA
2217 $focus_hints = array
2218 (
2219 'prev_objid' => $_REQUEST['object_id'],
2220 'prev_vstid' => $_REQUEST['vst_id'],
2221 'prev_vdid' => $_REQUEST['vdom_id'],
2222 );
2223 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR', array(), NULL, NULL, $focus_hints);
8198f2c6
DO
2224}
2225
8198f2c6 2226$msgcode['delVLANDescription']['OK'] = 49;
c48f2e18
DO
2227$msgcode['delVLANDescription']['ERR1'] = 105;
2228$msgcode['delVLANDescription']['ERR2'] = 111;
8198f2c6
DO
2229function delVLANDescription ()
2230{
80ec1f4f 2231 assertUIntArg ('vlan_id');
8198f2c6 2232 global $sic;
c48f2e18 2233 if ($sic['vlan_id'] == VLAN_DFL_ID)
048b0420 2234 return buildRedirectURL (__FUNCTION__, 'ERR1');
8198f2c6 2235 $result = commitReduceVLANDescription ($sic['vdom_id'], $sic['vlan_id']);
c48f2e18 2236 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR2');
8198f2c6
DO
2237}
2238
2239$msgcode['updVLANDescription']['OK'] = 51;
c48f2e18
DO
2240$msgcode['updVLANDescription']['ERR1'] = 105;
2241$msgcode['updVLANDescription']['ERR2'] = 109;
8198f2c6
DO
2242function updVLANDescription ()
2243{
80ec1f4f
DO
2244 assertUIntArg ('vlan_id');
2245 assertStringArg ('vlan_type');
2246 assertStringArg ('vlan_descr', TRUE);
8198f2c6 2247 global $sic;
c48f2e18 2248 if ($sic['vlan_id'] == VLAN_DFL_ID)
048b0420 2249 return buildRedirectURL (__FUNCTION__, 'ERR1');
8198f2c6
DO
2250 $result = commitUpdateVLANDescription
2251 (
2252 $sic['vdom_id'],
2253 $sic['vlan_id'],
0dabdc53 2254 $sic['vlan_type'],
8198f2c6
DO
2255 $sic['vlan_descr']
2256 );
6cfedb71 2257 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR2');
8198f2c6
DO
2258}
2259
2260$msgcode['createVLANDomain']['OK'] = 48;
2261$msgcode['createVLANDomain']['ERR'] = 110;
2262function createVLANDomain ()
2263{
80ec1f4f 2264 assertStringArg ('vdom_descr');
8198f2c6
DO
2265 global $sic;
2266 $result = usePreparedInsertBlade
2267 (
2268 'VLANDomain',
2269 array
2270 (
2271 'description' => $sic['vdom_descr'],
2272 )
2273 );
10c7f49e
DO
2274 $result = $result and usePreparedInsertBlade
2275 (
2276 'VLANDescription',
2277 array
2278 (
2279 'domain_id' => lastInsertID(),
2280 'vlan_id' => VLAN_DFL_ID,
2281 'vlan_type' => 'compulsory',
c48f2e18 2282 'vlan_descr' => 'default',
10c7f49e
DO
2283 )
2284 );
8198f2c6
DO
2285 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2286}
2287
2288$msgcode['destroyVLANDomain']['OK'] = 49;
2289$msgcode['destroyVLANDomain']['ERR'] = 111;
2290function destroyVLANDomain ()
2291{
80ec1f4f 2292 assertUIntArg ('vdom_id');
8198f2c6 2293 global $sic;
bbae3611 2294 $result = FALSE !== usePreparedDeleteBlade ('VLANDomain', array ('id' => $sic['vdom_id']));
8198f2c6
DO
2295 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2296}
2297
2298$msgcode['updateVLANDomain']['OK'] = 51;
2299$msgcode['updateVLANDomain']['ERR'] = 109;
2300function updateVLANDomain ()
2301{
80ec1f4f
DO
2302 assertUIntArg ('vdom_id');
2303 assertStringArg ('vdom_descr');
8198f2c6 2304 global $sic;
d3192636 2305 $result = FALSE !== commitUpdateVLANDomain ($sic['vdom_id'], $sic['vdom_descr']);
8198f2c6
DO
2306 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2307}
2308
4492050b
DO
2309$msgcode['save8021QPorts']['OK1'] = 63;
2310$msgcode['save8021QPorts']['OK2'] = 41;
706d9175
DO
2311$msgcode['save8021QPorts']['ERR2'] = 109;
2312function save8021QPorts ()
8198f2c6 2313{
a7492e95 2314 global $sic, $dbxlink;
70cff23f 2315 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de8aa722
DO
2316 assertStringArg ('form_mode');
2317 if ($sic['form_mode'] != 'save' and $sic['form_mode'] != 'duplicate')
2318 throw new InvalidRequestArgException ('form_mode', $sic['form_mode']);
2319 $extra = array();
a7492e95 2320 $dbxlink->beginTransaction();
2996cbbb
DO
2321 try
2322 {
a7492e95
DO
2323 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
2324 throw new InvalidArgException ('object_id', $object_id, 'VLAN domain is not set for this object');
9c45ea37 2325 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
1a43ce88 2326 throw new InvalidRequestArgException ('mutex_rev', $sic['mutex_rev'], 'expired form data');
de8aa722 2327 $after = $before = apply8021QOrder ($vswitch['template_id'], getStored8021QConfig ($sic['object_id'], 'desired'));
af204724 2328 $changes = array();
de8aa722 2329 switch ($sic['form_mode'])
706d9175 2330 {
de8aa722
DO
2331 case 'save':
2332 assertUIntArg ('nports');
2333 if ($sic['nports'] == 1)
706d9175 2334 {
de8aa722
DO
2335 assertStringArg ('pn_0');
2336 $extra = array ('port_name' => $sic['pn_0']);
2337 }
2338 for ($i = 0; $i < $sic['nports']; $i++)
2339 {
2340 assertStringArg ('pn_' . $i);
2341 assertStringArg ('pm_' . $i);
2342 // An access port only generates form input for its native VLAN,
2343 // which we derive allowed VLAN list from.
2344 $native = isset ($sic['pnv_' . $i]) ? $sic['pnv_' . $i] : 0;
2345 switch ($sic["pm_${i}"])
2346 {
2347 case 'trunk':
b36fc896 2348# assertArrayArg ('pav_' . $i);
de8aa722
DO
2349 $allowed = isset ($sic['pav_' . $i]) ? $sic['pav_' . $i] : array();
2350 break;
2351 case 'access':
2352 if ($native == 'same')
2353 continue 2;
2354 assertUIntArg ('pnv_' . $i);
2355 $allowed = array ($native);
2356 break;
2357 default:
2358 throw new InvalidRequestArgException ("pm_${i}", $_REQUEST["pm_${i}"], 'unknown port mode');
2359 }
2360 $changes[$sic['pn_' . $i]] = array
2361 (
2362 'mode' => $sic['pm_' . $i],
2363 'allowed' => $allowed,
2364 'native' => $native,
2365 );
706d9175 2366 }
de8aa722
DO
2367 break;
2368 case 'duplicate':
2369 assertStringArg ('from_port');
2370# assertArrayArg ('to_ports');
2371 if (!array_key_exists ($sic['from_port'], $before))
2372 throw new InvalidArgException ('from_port', $sic['from_port'], 'this port does not exist');
2373 foreach ($sic['to_ports'] as $tpn)
2374 if (!array_key_exists ($tpn, $before))
2375 throw new InvalidArgException ('to_ports[]', $tpn, 'this port does not exist');
2376 elseif ($tpn != $sic['from_port'])
2377 $changes[$tpn] = $before[$sic['from_port']];
2378 break;
706d9175 2379 }
af204724
DO
2380 $domain_vlanlist = getDomainVLANs ($vswitch['domain_id']);
2381 $changes = filter8021QChangeRequests
a7492e95 2382 (
af204724 2383 $domain_vlanlist,
9165e8f7 2384 $before,
af204724 2385 apply8021QOrder ($vswitch['template_id'], $changes)
a7492e95 2386 );
bb2024b9 2387 $changes = authorize8021QChangeRequests ($before, $changes);
af204724
DO
2388 foreach ($changes as $port_name => $port)
2389 $after[$port_name] = $port;
b3a27170 2390 $new_uplinks = filter8021QChangeRequests ($domain_vlanlist, $after, produceUplinkPorts ($domain_vlanlist, $after));
7455e4c0 2391 $npulled = replace8021QPorts ('desired', $vswitch['object_id'], $before, $changes);
b3a27170 2392 $nsaved_uplinks = replace8021QPorts ('desired', $vswitch['object_id'], $before, $new_uplinks);
2996cbbb
DO
2393 }
2394 catch (Exception $e)
861292ee 2395 {
a7492e95 2396 $dbxlink->rollBack();
706d9175 2397 return buildRedirectURL (__FUNCTION__, 'ERR2', array(), NULL, NULL, $extra);
861292ee 2398 }
b3a27170 2399 if ($npulled + $nsaved_uplinks)
c1aa3ada
DO
2400 $result = usePreparedExecuteBlade
2401 (
2402 'UPDATE VLANSwitch SET mutex_rev=mutex_rev+1, last_change=NOW(), out_of_sync="yes" WHERE object_id=?',
2403 array ($sic['object_id'])
2404 );
a7492e95 2405 $dbxlink->commit();
4492050b 2406 $log = oneLiner (63, array ($npulled + $nsaved_uplinks));
b3a27170 2407 if ($nsaved_uplinks)
4492050b 2408 {
b3a27170 2409 initiateUplinksReverb ($vswitch['object_id'], $new_uplinks);
4492050b
DO
2410 $log = mergeLogs ($log, oneLiner (41));
2411 }
2412 if ($npulled + $nsaved_uplinks > 0 and getConfigVar ('8021Q_INSTANT_DEPLOY') == 'yes')
2413 {
2414 try
2415 {
2416 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], TRUE))
2417 $log = mergeLogs ($log, oneLiner (191));
2418 else
2419 $log = mergeLogs ($log, oneLiner (63, array ($done)));
2420 }
2421 catch (Exception $e)
2422 {
2423 $log = mergeLogs ($log, oneLiner (109));
2424 }
2425 }
2426 return buildWideRedirectURL ($log, NULL, NULL, $extra);
8198f2c6
DO
2427}
2428
8846b060
DO
2429$msgcode['bindVLANtoIPv4']['OK'] = 48;
2430$msgcode['bindVLANtoIPv4']['ERR'] = 110;
2431function bindVLANtoIPv4 ()
2432{
2433 assertUIntArg ('id'); // network id
2434 global $sic;
2435 $result = commitSupplementVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2436 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2437}
2438
21ee3351
AA
2439$msgcode['bindVLANtoIPv6']['OK'] = 48;
2440$msgcode['bindVLANtoIPv6']['ERR'] = 110;
2441function bindVLANtoIPv6 ()
2442{
2443 assertUIntArg ('id'); // network id
2444 global $sic;
2445 $result = commitSupplementVLANIPv6 ($sic['vlan_ck'], $_REQUEST['id']);
2446 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2447}
2448
8846b060
DO
2449$msgcode['unbindVLANfromIPv4']['OK'] = 49;
2450$msgcode['unbindVLANfromIPv4']['ERR'] = 111;
2451function unbindVLANfromIPv4 ()
2452{
2453 assertUIntArg ('id'); // network id
2454 global $sic;
2455 $result = commitReduceVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2456 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2457}
2458
21ee3351
AA
2459$msgcode['unbindVLANfromIPv6']['OK'] = 49;
2460$msgcode['unbindVLANfromIPv6']['ERR'] = 111;
2461function unbindVLANfromIPv6 ()
2462{
2463 assertUIntArg ('id'); // network id
2464 global $sic;
2465 $result = commitReduceVLANIPv6 ($sic['vlan_ck'], $sic['id']);
2466 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2467}
2468
228c430c 2469$msgcode['process8021QSyncRequest']['OK'] = 63;
ec523868 2470$msgcode['process8021QSyncRequest']['ERR'] = 191;
228c430c 2471function process8021QSyncRequest ()
be28b696 2472{
2e84a05b
DO
2473 // behave depending on current operation: exec8021QPull or exec8021QPush
2474 global $sic, $op;
ec523868
DO
2475 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], $op == 'exec8021QPush'))
2476 return buildRedirectURL (__FUNCTION__, 'ERR');
2477 return buildRedirectURL (__FUNCTION__, 'OK', array ($done));
be28b696
DO
2478}
2479
f9428bc6
AA
2480$msgcode['process8021QRecalcRequest']['CHANGED'] = 87;
2481$msgcode['process8021QRecalcRequest']['NO_CHANGES'] = 300;
00e93d63 2482$msgcode['process8021QRecalcRequest']['ERR'] = 157;
f9428bc6
AA
2483function process8021QRecalcRequest ()
2484{
f9428bc6 2485 global $sic;
fb3e3731
AA
2486 if (! permitted (NULL, NULL, NULL, array (array ('tag' => '$op_recalc8021Q'))))
2487 return buildRedirectURL (__FUNCTION__, 'ERR');
f9428bc6
AA
2488 $counters = recalc8021QPorts ($sic['object_id']);
2489 if ($counters['ports'])
2490 return buildRedirectURL (__FUNCTION__, 'CHANGED', array ($counters['ports'], $counters['switches']));
2491 else
2492 return buildRedirectURL (__FUNCTION__, 'NO_CHANGES', array ('No changes were made'));
2493}
2494
be28b696 2495$msgcode['resolve8021QConflicts']['OK'] = 63;
bcd14540
DO
2496$msgcode['resolve8021QConflicts']['ERR1'] = 179;
2497$msgcode['resolve8021QConflicts']['ERR2'] = 109;
be28b696 2498function resolve8021QConflicts ()
07de6bb3 2499{
a7492e95 2500 global $sic, $dbxlink;
70cff23f 2501 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de47b574
DO
2502 assertUIntArg ('nrows');
2503 // Divide submitted radio buttons into 3 groups:
19d1e731 2504 // left (saved version wins)
07de6bb3 2505 // asis (ignore)
19d1e731
DO
2506 // right (running version wins)
2507 $F = array();
339534a0 2508 for ($i = 0; $i < $sic['nrows']; $i++)
07de6bb3 2509 {
de47b574 2510 if (!array_key_exists ("i_${i}", $sic))
07de6bb3 2511 continue;
de47b574
DO
2512 // let's hope other inputs are in place
2513 switch ($sic["i_${i}"])
07de6bb3
DO
2514 {
2515 case 'left':
07de6bb3 2516 case 'right':
bcd14540 2517 $F[$sic["pn_${i}"]] = array
07de6bb3 2518 (
ca9e1c9d 2519 'mode' => $sic["rm_${i}"],
de47b574
DO
2520 'allowed' => $sic["ra_${i}"],
2521 'native' => $sic["rn_${i}"],
bcd14540 2522 'decision' => $sic["i_${i}"],
07de6bb3
DO
2523 );
2524 break;
2525 default:
2526 // don't care
2527 }
2528 }
a7492e95 2529 $dbxlink->beginTransaction();
07de6bb3
DO
2530 try
2531 {
a7492e95 2532 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
ec4d604c 2533 throw new InvalidArgException ('object_id', $sic['object_id'], 'VLAN domain is not set for this object');
9c45ea37 2534 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
1a43ce88 2535 throw new InvalidRequestArgException ('mutex_rev', $sic['mutex_rev'], 'expired form (table data has changed)');
9c2201ba
DO
2536 $D = getStored8021QConfig ($vswitch['object_id'], 'desired');
2537 $C = getStored8021QConfig ($vswitch['object_id'], 'cached');
2538 $R = getRunning8021QConfig ($vswitch['object_id']);
d5e306b2 2539 $plan = get8021QSyncOptions ($vswitch, $D, $C, $R['portdata']);
bcd14540
DO
2540 $ndone = 0;
2541 foreach ($F as $port_name => $port)
2542 {
9c2201ba
DO
2543 if (!array_key_exists ($port_name, $plan))
2544 continue;
2545 elseif ($plan[$port_name]['status'] == 'merge_conflict')
bcd14540 2546 {
9c2201ba
DO
2547 // for R neither mutex nor revisions can be emulated, but revision change can be
2548 if (!same8021QConfigs ($port, $R['portdata'][$port_name]))
1a43ce88 2549 throw new InvalidRequestArgException ("port ${port_name}", '(hidden)', 'expired form (switch data has changed)');
1f8f67c2 2550 if ($port['decision'] == 'right') // D wins, frame R by writing value of R to C
9c2201ba 2551 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $port);
1f8f67c2 2552 elseif ($port['decision'] == 'left') // R wins, cross D up
9c2201ba
DO
2553 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $D[$port_name]);
2554 // otherwise there was no decision made
2555 }
1f8f67c2
DO
2556 elseif
2557 (
2558 $plan[$port_name]['status'] == 'delete_conflict' or
2559 $plan[$port_name]['status'] == 'martian_conflict'
2560 )
9c2201ba 2561 {
1f8f67c2 2562 if ($port['decision'] == 'left') // confirm deletion of local copy
9c2201ba 2563 $ndone += del8021QPort ($vswitch['object_id'], $port_name);
bcd14540 2564 }
9c2201ba 2565 // otherwise ignore a decision, which doesn't address a conflict
bcd14540 2566 }
07de6bb3 2567 }
1a43ce88 2568 catch (InvalidRequestArgException $e)
07de6bb3 2569 {
a7492e95 2570 $dbxlink->rollBack();
bcd14540 2571 return buildRedirectURL (__FUNCTION__, 'ERR1');
ec4d604c 2572 }
bcd14540 2573 catch (Exception $e)
ec4d604c 2574 {
bcd14540
DO
2575 $dbxlink->rollBack();
2576 return buildRedirectURL (__FUNCTION__, 'ERR2');
ec4d604c 2577 }
a7492e95 2578 $dbxlink->commit();
bcd14540 2579 return buildRedirectURL (__FUNCTION__, 'OK', array ($ndone));
07de6bb3
DO
2580}
2581
e0d188ef
DO
2582$msgcode['addVLANSwitchTemplate']['OK'] = 48;
2583$msgcode['addVLANSwitchTemplate']['ERR'] = 110;
2584function addVLANSwitchTemplate()
2585{
2586 assertStringArg ('vst_descr');
2587 global $sic;
2588 $max_local_vlans = NULL;
2589 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2590 {
2591 assertUIntArg ('vst_maxvlans');
2592 $max_local_vlans = $sic['vst_maxvlans'];
2593 }
2594 $result = usePreparedInsertBlade
2595 (
2596 'VLANSwitchTemplate',
2597 array
2598 (
2599 'max_local_vlans' => $max_local_vlans,
2600 'description' => $sic['vst_descr'],
2601 )
2602 );
2603 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2604}
2605
2606$msgcode['delVLANSwitchTemplate']['OK'] = 49;
2607$msgcode['delVLANSwitchTemplate']['ERR'] = 111;
2608function delVLANSwitchTemplate()
2609{
2610 assertUIntArg ('vst_id');
2611 global $sic;
2612 $result = FALSE !== usePreparedDeleteBlade ('VLANSwitchTemplate', array ('id' => $sic['vst_id']));
2613 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2614}
2615
2616$msgcode['updVLANSwitchTemplate']['OK'] = 51;
2617$msgcode['updVLANSwitchTemplate']['ERR'] = 109;
2618function updVLANSwitchTemplate()
2619{
2620 assertUIntArg ('vst_id');
2621 assertStringArg ('vst_descr');
2622 global $sic;
2623 $max_local_vlans = NULL;
2624 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2625 {
2626 assertUIntArg ('vst_maxvlans');
2627 $max_local_vlans = $sic['vst_maxvlans'];
2628 }
2629 $result = commitUpdateVST ($sic['vst_id'], $max_local_vlans, $sic['vst_descr']);
6cfedb71 2630 return buildRedirectURL (__FUNCTION__, $result !== FALSE ? 'OK' : 'ERR');
e0d188ef
DO
2631}
2632
09ec2e59
AA
2633$msgcode['cloneVSTRule']['OK'] = 48;
2634$msgcode['cloneVSTRule']['ERR'] = 179;
2635function cloneVSTRule()
e0d188ef 2636{
09ec2e59
AA
2637 global $dbxlink;
2638 $message = '';
09ec2e59
AA
2639 assertUIntArg ('mutex_rev', TRUE);
2640 $dst_vst = getVLANSwitchTemplate ($_REQUEST['vst_id']);
2641 if ($dst_vst['mutex_rev'] != $_REQUEST['mutex_rev'])
2642 $message = "User ${dst_vst['saved_by']} saved this template after you started to edit it. Please concern differencies";
2643 else
eb51ceff 2644 {
eb51ceff
DO
2645 assertUIntArg ('from_id');
2646 $src_vst = getVLANSwitchTemplate ($_REQUEST['from_id']);
09ec2e59
AA
2647 if (! commitUpdateVSTRules ($_REQUEST['vst_id'], $src_vst['rules']))
2648 $message = 'DB error';
eb51ceff 2649 }
09ec2e59
AA
2650 $result = !(BOOL) $message;
2651 if ($result)
2652 $message = 'Supplement succeeded';
2653 return buildWideRedirectURL (array (array ('code' => $result ? 'success' : 'error', 'message' => $message)));
e0d188ef
DO
2654}
2655
405ac32c 2656$msgcode['updVSTRule']['OK'] = 43;
e0d188ef
DO
2657function updVSTRule()
2658{
405ac32c 2659 global $port_role_options, $sic;
09ec2e59 2660 assertUIntArg ('mutex_rev', TRUE);
405ac32c
DO
2661 genericAssertion ('template_json', 'json');
2662 $data = json_decode ($sic['template_json'], TRUE);
2663 $rule_no = 0;
2664 try
09ec2e59 2665 {
09ec2e59
AA
2666 foreach ($data as $rule)
2667 {
2668 $rule_no++;
405ac32c
DO
2669 if
2670 (
2671 ! isInteger ($rule['rule_no'])
2672 or ! isPCRE ($rule['port_pcre'])
2673 or ! isset ($rule['port_role'])
2674 or ! array_key_exists ($rule['port_role'], $port_role_options)
2675 or ! isset ($rule['wrt_vlans'])
2676 or ! preg_match ('/^[ 0-9\-,]*$/', $rule['wrt_vlans'])
2677 or ! isset ($rule['description'])
2678 )
2679 throw new InvalidRequestArgException ('form', '(JSON)', "invalid rule #$rule_no");
09ec2e59 2680 }
405ac32c 2681 commitUpdateVSTRules ($_REQUEST['vst_id'], $_REQUEST['mutex_rev'], $data);
09ec2e59 2682 }
405ac32c 2683 catch (Exception $e)
09ec2e59 2684 {
405ac32c
DO
2685 // Every case, which is soft-processed in process.php, will have the working copy available for a retry.
2686 if ($e instanceof InvalidRequestArgException or $e instanceof RTDatabaseError)
2687 $_SESSION['vst_edited'] = $data;
2688 throw $e;
09ec2e59 2689 }
405ac32c 2690 return buildRedirectURL (__FUNCTION__, 'OK');
e0d188ef
DO
2691}
2692
5b5b1cab
DO
2693$msgcode['importDPData']['OK'] = 44;
2694function importDPData()
b3247480
DO
2695{
2696 global $sic;
2697 assertUIntArg ('nports');
2698 $nignored = $ndone = 0;
1dc057f8 2699 $POIFC = getPortOIFCompat();
b3247480
DO
2700 for ($i = 0; $i < $sic['nports']; $i++)
2701 if (array_key_exists ("do_${i}", $sic))
2702 {
2703 assertUIntArg ("pid1_${i}");
2704 assertUIntArg ("pid2_${i}");
2705 $porta = getPortInfo ($_REQUEST["pid1_${i}"]);
2706 $portb = getPortInfo ($_REQUEST["pid2_${i}"]);
2707 if
2708 (
2709 $porta['linked'] or
2710 $portb['linked'] or
2711 ($porta['object_id'] != $sic['object_id'] and $portb['object_id'] != $sic['object_id'])
2712 )
2713 {
2714 $nignored++;
2715 continue;
2716 }
1dc057f8
DO
2717 foreach ($POIFC as $item)
2718 if ($item['type1'] == $porta['oif_id'] and $item['type2'] == $portb['oif_id'])
2719 {
2720 linkPorts ($_REQUEST["pid1_${i}"], $_REQUEST["pid2_${i}"]);
2721 $ndone++;
524170a8 2722 continue 2; // next port
1dc057f8
DO
2723 }
2724 $nignored++;
b3247480
DO
2725 }
2726 return buildRedirectURL (__FUNCTION__, 'OK', array ($nignored, $ndone));
2727}
2728
9a90adc4
DO
2729$msgcode['addObjectlog']['OK'] = 0;
2730function addObjectlog ()
2731{
9a90adc4
DO
2732 assertStringArg ('logentry');
2733 global $remote_username, $sic;
2734 $oi = spotEntity ('object', $sic['object_id']);
2735 usePreparedExecuteBlade ('INSERT INTO ObjectLog SET object_id=?, user=?, date=NOW(), content=?', array ($sic['object_id'], $remote_username, $sic['logentry']));
2736 $ob_url = makeHref (array ('page' => 'object', 'tab' => 'objectlog', 'object_id' => $sic['object_id']));
2737 return buildRedirectURL (__FUNCTION__, 'OK', array ("Log entry for <a href=" . ${ob_url} . ">${oi['dname']}</a> added by ${remote_username}"));
2738}
2739
e38cd1da
DO
2740function getOpspec()
2741{
2742 global $pageno, $tabno, $op, $opspec_list;
2743 if (!array_key_exists ($pageno . '-' . $tabno . '-' . $op, $opspec_list))
2744 throw new RackTablesError ('key not found in opspec_list', RackTablesError::INTERNAL);
2745 $ret = $opspec_list[$pageno . '-' . $tabno . '-' . $op];
2746 if
2747 (
2748 !array_key_exists ('table', $ret)
2749 or !array_key_exists ('action', $ret)
2750 // add further checks here
2751 )
2752 throw new RackTablesError ('malformed array structure in opspec_list', RackTablesError::INTERNAL);
2753 return $ret;
2754}
2755
2756function tableHandler()
cde28cf0 2757{
548c9349 2758 $opspec = getOpspec();
cde28cf0 2759 global $sic;
cde28cf0 2760 $columns = array();
09ecff69
DO
2761 foreach (array ('arglist', 'set_arglist', 'where_arglist') as $listname)
2762 foreach ($opspec[$listname] as $argspec)
2763 {
2764 genericAssertion ($argspec['url_argname'], $argspec['assertion']);
2765 // "table_colname" is normally used for an override, if it is not
2766 // set, use the URL argument name
2767 $table_colname = array_key_exists ('table_colname', $argspec) ?
2768 $argspec['table_colname'] :
2769 $argspec['url_argname'];
2770 $arg_value = $sic[$argspec['url_argname']];
2771 if
2772 (
2773 ($argspec['assertion'] == 'uint0' and $arg_value == 0)
2774 or ($argspec['assertion'] == 'string0' and $arg_value == '')
2775 )
2776 switch (TRUE)
2777 {
2778 case !array_key_exists ('if_empty', $argspec): // no action requested
2779 break;
2780 case $argspec['if_empty'] == 'NULL':
2781 $arg_value = NULL;
2782 break;
2783 default:
2784 throw new InvalidArgException ('opspec', '(malformed array structure)', '"if_empty" not recognized');
2785 }
2786 $columns[$listname][$table_colname] = $arg_value;
2787 }
cde28cf0
DO
2788 switch ($opspec['action'])
2789 {
2790 case 'INSERT':
09ecff69 2791 $retcode = TRUE === usePreparedInsertBlade ($opspec['table'], $columns['arglist']) ? 48 : 110;
cde28cf0 2792 break;
10005279
DO
2793 case 'DELETE':
2794 $conjunction = array_key_exists ('conjunction', $opspec) ? $opspec['conjunction'] : 'AND';
09ecff69
DO
2795 $retcode = FALSE !== usePreparedDeleteBlade ($opspec['table'], $columns['arglist'], $conjunction) ? 49 : 111;
2796 break;
2797 case 'UPDATE':
2798 usePreparedUpdateBlade
2799 (
2800 $opspec['table'],
2801 $columns['set_arglist'],
2802 $columns['where_arglist'],
2803 array_key_exists ('conjunction', $opspec) ? $opspec['conjunction'] : 'AND'
2804 );
2805 $retcode = 51;
10005279 2806 break;
cde28cf0 2807 default:
548c9349 2808 throw new InvalidArgException ('opspec/action', $opspec['action']);
cde28cf0
DO
2809 }
2810 return buildWideRedirectURL (oneLiner ($retcode));
2811}
2812
e673ee24 2813?>