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