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