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