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