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