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