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