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