r2278 fix typos
[racktables] / inc / ophandlers.php
1 <?php
2 /*
3 *
4 * This file is a library of operation handlers for RackTables.
5 *
6 */
7
8 // This function assures that specified argument was passed
9 // and is a number greater than zero.
10 function assertUIntArg ($argname, $caller = 'N/A', $allow_zero = FALSE)
11 {
12 if (!isset ($_REQUEST[$argname]))
13 {
14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__);
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__);
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__);
30 die();
31 }
32 }
33
34 // This function assures that specified argument was passed
35 // and is a non-empty string.
36 function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
37 {
38 if (!isset ($_REQUEST[$argname]))
39 {
40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
51 die();
52 }
53 }
54
55 function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
56 {
57 if (!isset ($_REQUEST[$argname]))
58 {
59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
70 die();
71 }
72 }
73
74 function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
75 {
76 assertStringArg ($argname, $caller, $ok_if_empty);
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "' (calling function is [${caller}]).", __FUNCTION__);
80 die();
81 }
82 }
83
84 function addPortForwarding ()
85 {
86 assertUIntArg ('object_id', __FUNCTION__);
87 assertIPv4Arg ('localip', __FUNCTION__);
88 assertIPv4Arg ('remoteip', __FUNCTION__);
89 assertUIntArg ('localport', __FUNCTION__);
90 assertStringArg ('proto', __FUNCTION__);
91 assertStringArg ('description', __FUNCTION__, TRUE);
92 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
93 if (empty ($remoteport))
94 $remoteport = $_REQUEST['localport'];
95
96 $error = newPortForwarding
97 (
98 $_REQUEST['object_id'],
99 $_REQUEST['localip'],
100 $_REQUEST['localport'],
101 $_REQUEST['remoteip'],
102 $remoteport,
103 $_REQUEST['proto'],
104 $_REQUEST['description']
105 );
106
107 if ($error == '')
108 return buildRedirectURL ('OK');
109 else
110 return buildRedirectURL ('ERR', array ($error));
111 }
112
113 function delPortForwarding ()
114 {
115 assertUIntArg ('object_id', __FUNCTION__);
116 assertIPv4Arg ('localip', __FUNCTION__);
117 assertIPv4Arg ('remoteip', __FUNCTION__);
118 assertUIntArg ('localport', __FUNCTION__);
119 assertUIntArg ('remoteport', __FUNCTION__);
120 assertStringArg ('proto', __FUNCTION__);
121
122 $error = deletePortForwarding
123 (
124 $_REQUEST['object_id'],
125 $_REQUEST['localip'],
126 $_REQUEST['localport'],
127 $_REQUEST['remoteip'],
128 $_REQUEST['remoteport'],
129 $_REQUEST['proto']
130 );
131 if ($error == '')
132 return buildRedirectURL ('OK');
133 else
134 return buildRedirectURL ('ERR', array ($error));
135 }
136
137 function updPortForwarding ()
138 {
139 assertUIntArg ('object_id', __FUNCTION__);
140 assertIPv4Arg ('localip', __FUNCTION__);
141 assertIPv4Arg ('remoteip', __FUNCTION__);
142 assertUIntArg ('localport', __FUNCTION__);
143 assertUIntArg ('remoteport', __FUNCTION__);
144 assertStringArg ('proto', __FUNCTION__);
145 assertStringArg ('description', __FUNCTION__);
146
147 $error = updatePortForwarding
148 (
149 $_REQUEST['object_id'],
150 $_REQUEST['localip'],
151 $_REQUEST['localport'],
152 $_REQUEST['remoteip'],
153 $_REQUEST['remoteport'],
154 $_REQUEST['proto'],
155 $_REQUEST['description']
156 );
157 if ($error == '')
158 return buildRedirectURL ('OK');
159 else
160 return buildRedirectURL ('ERR', array ($error));
161 }
162
163 function addPortForObject ()
164 {
165 assertUIntArg ('object_id', __FUNCTION__);
166 assertStringArg ('port_name', __FUNCTION__, TRUE);
167 if (empty ($_REQUEST['port_name']))
168 return buildRedirectURL ('ERR1');
169 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
170 if ($error != '')
171 return buildRedirectURL ('ERR2', array ($error));
172 else
173 return buildRedirectURL ('OK', array ($_REQUEST['port_name']));
174 }
175
176 function editPortForObject ()
177 {
178 assertUIntArg ('port_id', __FUNCTION__);
179 // tolerate empty value now to produce custom informative message later
180 assertStringArg ('name', __FUNCTION__, TRUE);
181 if (empty ($_REQUEST['name']))
182 return buildRedirectURL ('ERR1');
183
184 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
185 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
186 else
187 $port_rc = 'NULL';
188 $error = commitUpdatePort ($_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
189 if ($error != '')
190 return buildRedirectURL ('ERR2', array ($error));
191 else
192 return buildRedirectURL ('OK', array ($_REQUEST['name']));
193 }
194
195 function delPortFromObject ()
196 {
197 assertUIntArg ('port_id', __FUNCTION__);
198 $error = delObjectPort ($_REQUEST['port_id']);
199
200 if ($error != '')
201 return buildRedirectURL ('ERR', array ($error));
202 else
203 return buildRedirectURL ('OK', array ($_REQUEST['port_name']));
204 }
205
206 function linkPortForObject ()
207 {
208 assertUIntArg ('port_id', __FUNCTION__);
209 assertUIntArg ('remote_port_id', __FUNCTION__);
210 assertStringArg ('port_name', __FUNCTION__, TRUE);
211 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
212 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
213
214 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
215 if ($error != '')
216 return buildRedirectURL ('ERR', array ($error));
217 else
218 return buildRedirectURL ('OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
219 }
220
221 function unlinkPortForObject ()
222 {
223 assertUIntArg ('port_id', __FUNCTION__);
224 assertStringArg ('port_name', __FUNCTION__, TRUE);
225 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
226 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
227
228 $error = unlinkPort ($_REQUEST['port_id']);
229 if ($error != '')
230 return buildRedirectURL ('ERR', array ($error));
231 else
232 return buildRedirectURL ('OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
233 }
234
235 function addMultiPorts ()
236 {
237 assertStringArg ('format', __FUNCTION__);
238 assertStringArg ('input', __FUNCTION__);
239 assertUIntArg ('port_type', __FUNCTION__);
240 assertUIntArg ('object_id', __FUNCTION__);
241 $format = $_REQUEST['format'];
242 $port_type = $_REQUEST['port_type'];
243 $object_id = $_REQUEST['object_id'];
244 // Input lines are escaped, so we have to explode and to chop by 2-char
245 // \n and \r respectively.
246 $lines1 = explode ('\n', $_REQUEST['input']);
247 foreach ($lines1 as $line)
248 {
249 $parts = explode ('\r', $line);
250 reset ($parts);
251 if (empty ($parts[0]))
252 continue;
253 else
254 $lines2[] = rtrim ($parts[0]);
255 }
256 $ports = array();
257 foreach ($lines2 as $line)
258 {
259 switch ($format)
260 {
261 case 'fisxii':
262 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
263 list ($slot, $port) = explode ('/', $words[0]);
264 $ports[] = array
265 (
266 'name' => "e ${slot}/${port}",
267 'l2address' => $words[8],
268 'label' => "slot ${slot} port ${port}"
269 );
270 break;
271 case 'c3600asy':
272 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
273 /*
274 How Async Lines are Numbered in Cisco 3600 Series Routers
275 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
276
277 Understanding 16- and 32-Port Async Network Modules
278 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
279 */
280 $async = $words[0];
281 $slot = floor (($async - 1) / 32);
282 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
283 $cable = $async - $slot * 32 - $octalgroup * 8;
284 $og_label[0] = 'async 0-7';
285 $og_label[1] = 'async 8-15';
286 $og_label[2] = 'async 16-23';
287 $og_label[3] = 'async 24-31';
288 $ports[] = array
289 (
290 'name' => "async ${async}",
291 'l2address' => '',
292 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
293 );
294 break;
295 case 'fiwg':
296 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
297 $ifnumber = $words[0] * 1;
298 $ports[] = array
299 (
300 'name' => "e ${ifnumber}",
301 'l2address' => "${words[8]}",
302 'label' => "${ifnumber}"
303 );
304 break;
305 case 'ssv1':
306 $words = explode (' ', $line);
307 if (empty ($words[0]) or empty ($words[1]))
308 continue;
309 $ports[] = array
310 (
311 'name' => $words[0],
312 'l2address' => $words[1],
313 'label' => ''
314 );
315 break;
316 default:
317 return buildRedirectURL ('ERR');
318 break;
319 }
320 }
321 // Create ports, if they don't exist.
322 $added_count = $updated_count = $error_count = 0;
323 foreach ($ports as $port)
324 {
325 $port_id = getPortID ($object_id, $port['name']);
326 if ($port_id === NULL)
327 {
328 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
329 if ($result == '')
330 $added_count++;
331 else
332 $error_count++;
333 }
334 else
335 {
336 $result = commitUpdatePort ($port_id, $port['name'], $port['label'], $port['l2address']);
337 if ($result == '')
338 $updated_count++;
339 else
340 $error_count++;
341 }
342 }
343 return buildRedirectURL ('OK', array ($added_count, $updated_count, $error_count));
344 }
345
346 function updIPv4Allocation ()
347 {
348 assertIPv4Arg ('ip', __FUNCTION__);
349 assertUIntArg ('object_id', __FUNCTION__);
350 assertStringArg ('bond_name', __FUNCTION__, TRUE);
351 assertStringArg ('bond_type', __FUNCTION__);
352
353 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
354 if ($error != '')
355 return buildRedirectURL ('ERR', array ($error));
356 else
357 return buildRedirectURL ('OK');
358 }
359
360 function delIPv4Allocation ()
361 {
362 assertIPv4Arg ('ip', __FUNCTION__);
363 assertUIntArg ('object_id', __FUNCTION__);
364
365 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
366 if ($error != '')
367 return buildRedirectURL ('ERR', array ($error));
368 else
369 return buildRedirectURL ('OK');
370 }
371
372 function addIPv4Allocation ()
373 {
374 assertIPv4Arg ('ip', __FUNCTION__);
375 assertUIntArg ('object_id', __FUNCTION__);
376 assertStringArg ('bond_name', __FUNCTION__, TRUE);
377 assertStringArg ('bond_type', __FUNCTION__);
378
379 // Strip masklen.
380 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
381 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
382 return buildRedirectURL ('ERR1', array ($ip));
383
384 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
385 if ($error != '')
386 return buildRedirectURL ('ERR2', array ($error));
387 $address = getIPv4Address ($ip);
388 if ($address['reserved'] == 'yes' or !empty ($address['name']))
389 {
390 $release = getConfigVar ('IPV4_AUTO_RELEASE');
391 if ($release >= 1)
392 $address['reserved'] = 'no';
393 if ($release >= 2)
394 $address['name'] = '';
395 updateAddress ($ip, $address['name'], $address['reserved']);
396 }
397 return buildRedirectURL ('OK');
398 }
399
400 function addIPv4Prefix ()
401 {
402 assertStringArg ('range', __FUNCTION__);
403 assertStringArg ('name', __FUNCTION__, TRUE);
404
405 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
406 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
407 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
408 if ($error != '')
409 return buildRedirectURL ('ERR', array ($error));
410 else
411 return buildRedirectURL ('OK');
412 }
413
414 function delIPv4Prefix ()
415 {
416 assertUIntArg ('id', __FUNCTION__);
417 $error = destroyIPv4Prefix ($_REQUEST['id']);
418 if ($error != '')
419 return buildRedirectURL ('ERR', array ($error));
420 else
421 return buildRedirectURL ('OK');
422 }
423
424 function updIPv4Prefix ()
425 {
426 assertUIntArg ('id', __FUNCTION__);
427 assertStringArg ('name', __FUNCTION__, TRUE);
428
429 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
430 if ($error != '')
431 return buildRedirectURL ('ERR', array ($error));
432 else
433 return buildRedirectURL ('OK');
434 }
435
436 function editAddress ()
437 {
438 assertIPv4Arg ('ip', __FUNCTION__);
439 assertStringArg ('name', __FUNCTION__, TRUE);
440
441 if (isset ($_REQUEST['reserved']))
442 $reserved = $_REQUEST['reserved'];
443 else
444 $reserved = 'off';
445 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
446 if ($error != '')
447 return buildRedirectURL ('ERR', array ($error));
448 else
449 return buildRedirectURL ('OK');
450 }
451
452 function createUser ()
453 {
454 assertStringArg ('username', __FUNCTION__);
455 assertStringArg ('realname', __FUNCTION__, TRUE);
456 assertStringArg ('password', __FUNCTION__);
457 $username = $_REQUEST['username'];
458 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
459 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
460 if ($result == TRUE)
461 return buildRedirectURL ('OK', array ($username));
462 else
463 return buildRedirectURL ('ERR', array ($username));
464 }
465
466 function updateUser ()
467 {
468 assertUIntArg ('user_id', __FUNCTION__);
469 assertStringArg ('username', __FUNCTION__);
470 assertStringArg ('realname', __FUNCTION__, TRUE);
471 assertStringArg ('password', __FUNCTION__);
472 $username = $_REQUEST['username'];
473 $new_password = $_REQUEST['password'];
474 $old_hash = getHashByID ($_REQUEST['user_id']);
475 if ($old_hash == NULL)
476 return buildRedirectURL ('ERR1');
477 // Update user password only if provided password is not the same as current password hash.
478 if ($new_password != $old_hash)
479 $new_password = hash (PASSWORD_HASH, $new_password);
480 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
481 if ($result == TRUE)
482 return buildRedirectURL ('OK', array ($username));
483 else
484 return buildRedirectURL ('ERR2', array ($username));
485 }
486
487 function enableUser ()
488 {
489 assertUIntArg ('user_id', __FUNCTION__);
490 if (commitEnableUserAccount ($_REQUEST['user_id'], 'yes') == TRUE)
491 return buildRedirectURL ('OK');
492 else
493 return buildRedirectURL ('ERR');
494 }
495
496 function disableUser ()
497 {
498 assertUIntArg ('user_id', __FUNCTION__);
499 if ($_REQUEST['user_id'] == 1)
500 return buildRedirectURL ('ERR1');
501 if (commitEnableUserAccount ($_REQUEST['user_id'], 'no'))
502 return buildRedirectURL ('OK');
503 else
504 return buildRedirectURL ('ERR2');
505 }
506
507 // This function find differences in users's submit and PortCompat table
508 // and modifies database accordingly.
509 function savePortMap ()
510 {
511 $ptlist = getPortTypes();
512 $oldCompat = getPortCompat();
513 $newCompat = array();
514 foreach (array_keys ($ptlist) as $leftKey)
515 foreach (array_keys ($ptlist) as $rightKey)
516 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
517 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
518 // Build the new matrix from user's submit and compare it to
519 // the old matrix. Those pairs which appear on
520 // new matrix only, have to be stored in PortCompat table. Those who appear
521 // on the old matrix only, should be removed from PortCompat table.
522 // Those present in both matrices should be skipped.
523 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
524 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
525 $error_count = $success_count = 0;
526 foreach (array_keys ($ptlist) as $type1)
527 foreach (array_keys ($ptlist) as $type2)
528 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
529 switch ($oldCompatTable[$type1][$type2])
530 {
531 case TRUE: // new value is FALSE
532 if (removePortCompat ($type1, $type2) === TRUE)
533 $success_count++;
534 else
535 $error_count++;
536 break;
537 case FALSE: // new value is TRUE
538 if (addPortCompat ($type1, $type2) === TRUE)
539 $success_count++;
540 else
541 $error_count++;
542 break;
543 default:
544 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
545 break;
546 }
547 if ($error_count == 0)
548 return buildRedirectURL ('OK', array ($error_count, $success_count));
549 else
550 return buildRedirectURL ('ERR', array ($error_count, $success_count));
551 }
552
553 function updateDictionary ()
554 {
555 assertUIntArg ('chapter_no', __FUNCTION__);
556 assertUIntArg ('dict_key', __FUNCTION__);
557 assertStringArg ('dict_value', __FUNCTION__);
558 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
559 return buildRedirectURL ('OK');
560 else
561 return buildRedirectURL ('ERR');
562 }
563
564 function supplementDictionary ()
565 {
566 assertUIntArg ('chapter_no', __FUNCTION__);
567 assertStringArg ('dict_value', __FUNCTION__);
568 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
569 return buildRedirectURL ('OK');
570 else
571 return buildRedirectURL ('ERR');
572 }
573
574 function reduceDictionary ()
575 {
576 assertUIntArg ('chapter_no', __FUNCTION__);
577 assertUIntArg ('dict_key', __FUNCTION__);
578 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
579 return buildRedirectURL ('OK');
580 else
581 return buildRedirectURL ('ERR');
582 }
583
584 function addChapter ()
585 {
586 assertStringArg ('chapter_name', __FUNCTION__);
587 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
588 return buildRedirectURL ('OK');
589 else
590 return buildRedirectURL ('ERR');
591 }
592
593 function updateChapter ()
594 {
595 assertUIntArg ('chapter_no', __FUNCTION__);
596 assertStringArg ('chapter_name', __FUNCTION__);
597 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
598 return buildRedirectURL ('OK');
599 else
600 return buildRedirectURL ('ERR');
601 }
602
603 function delChapter ()
604 {
605 assertUIntArg ('chapter_no', __FUNCTION__);
606 if (commitDeleteChapter ($_REQUEST['chapter_no']))
607 return buildRedirectURL ('OK');
608 else
609 return buildRedirectURL ('ERR');
610 }
611
612 function changeAttribute ()
613 {
614 assertUIntArg ('attr_id', __FUNCTION__);
615 assertStringArg ('attr_name', __FUNCTION__);
616 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
617 return buildRedirectURL ('OK');
618 else
619 return buildRedirectURL ('ERR');
620 }
621
622 function createAttribute ()
623 {
624 assertStringArg ('attr_name', __FUNCTION__);
625 assertStringArg ('attr_type', __FUNCTION__);
626 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
627 return buildRedirectURL ('OK', array ($_REQUEST['attr_name']));
628 else
629 return buildRedirectURL ('ERR');
630 }
631
632 function deleteAttribute ()
633 {
634 assertUIntArg ('attr_id', __FUNCTION__);
635 if (commitDeleteAttribute ($_REQUEST['attr_id']))
636 return buildRedirectURL ('OK');
637 else
638 return buildRedirectURL ('ERR');
639 }
640
641 function supplementAttrMap ()
642 {
643 assertUIntArg ('attr_id', __FUNCTION__);
644 assertUIntArg ('objtype_id', __FUNCTION__);
645 assertUIntArg ('chapter_no', __FUNCTION__);
646 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
647 return buildRedirectURL ('OK');
648 else
649 return buildRedirectURL ('ERR');
650 }
651
652 function reduceAttrMap ()
653 {
654 assertUIntArg ('attr_id', __FUNCTION__);
655 assertUIntArg ('objtype_id', __FUNCTION__);
656 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
657 return buildRedirectURL ('OK');
658 else
659 return buildRedirectURL ('ERR');
660 }
661
662 function clearSticker ()
663 {
664 assertUIntArg ('attr_id', __FUNCTION__);
665 assertUIntArg ('object_id', __FUNCTION__);
666 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
667 return buildRedirectURL ('OK');
668 else
669 return buildRedirectURL ('ERR');
670 }
671
672 function updateObject ()
673 {
674 assertUIntArg ('object_id', __FUNCTION__);
675 assertUIntArg ('object_type_id', __FUNCTION__);
676 assertStringArg ('object_name', __FUNCTION__, TRUE);
677 assertStringArg ('object_label', __FUNCTION__, TRUE);
678 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
679 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
680 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
681 $has_problems = 'yes';
682 else
683 $has_problems = 'no';
684
685 if (commitUpdateObject (
686 $_REQUEST['object_id'],
687 $_REQUEST['object_name'],
688 $_REQUEST['object_label'],
689 $_REQUEST['object_barcode'],
690 $_REQUEST['object_type_id'],
691 $has_problems,
692 $_REQUEST['object_asset_no'],
693 $_REQUEST['object_comment']
694 ) !== TRUE)
695 return buildRedirectURL ('ERR');
696 // Invalidate thumb cache of all racks objects could occupy.
697 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
698 resetThumbCache ($rack_id);
699 return buildRedirectURL ('OK');
700 }
701
702 function updateStickers ()
703 {
704 assertUIntArg ('object_id', __FUNCTION__);
705 assertUIntArg ('num_attrs', __FUNCTION__);
706 $oldvalues = getAttrValues ($_REQUEST['object_id']);
707 $result = array();
708
709 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
710 {
711 assertUIntArg ("${i}_attr_id", __FUNCTION__);
712 $attr_id = $_REQUEST["${i}_attr_id"];
713
714 // Field is empty, delete attribute and move on.
715 if (empty($_REQUEST["${i}_value"]))
716 {
717 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
718 continue;
719 }
720
721 // The value could be uint/float, but we don't know ATM. Let SQL
722 // server check this and complain.
723 assertStringArg ("${i}_value", __FUNCTION__);
724 $value = $_REQUEST["${i}_value"];
725 switch ($oldvalues[$attr_id]['type'])
726 {
727 case 'uint':
728 case 'float':
729 case 'string':
730 $oldvalue = $oldvalues[$attr_id]['value'];
731 break;
732 case 'dict':
733 $oldvalue = $oldvalues[$attr_id]['key'];
734 break;
735 default:
736 showError ('Internal structure error', __FUNCTION__);
737 die;
738 }
739 if ($value == $oldvalue)
740 continue;
741
742 // Note if the queries succeed or not, it determines which page they see.
743 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
744 }
745
746 if (in_array (FALSE, $result))
747 return buildRedirectURL ('ERR');
748
749 return buildRedirectURL ('OK');
750 }
751
752 function useupPort ()
753 {
754 assertUIntArg ('port_id', __FUNCTION__);
755 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
756 return buildRedirectURL ('OK');
757 else
758 return buildRedirectURL ('ERR');
759 }
760
761 function updateUI ()
762 {
763 assertUIntArg ('num_vars', __FUNCTION__);
764 $error = '';
765
766 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
767 {
768 assertStringArg ("${i}_varname", __FUNCTION__);
769 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
770 $varname = $_REQUEST["${i}_varname"];
771 $varvalue = $_REQUEST["${i}_varvalue"];
772
773 // If form value = value in DB, don't bother updating DB
774 if ($varvalue == getConfigVar ($varname))
775 continue;
776
777 // Note if the queries succeed or not, it determines which page they see.
778 $error = setConfigVar ($varname, $varvalue, TRUE);
779 if (!empty ($error))
780 break;
781 }
782
783 if ($error != '')
784 return buildRedirectURL ('ERR', array ($error));
785
786 return buildRedirectURL ('OK');
787 }
788
789 function resetUIConfig()
790 {
791 setConfigVar ('default_port_type','24');
792 setConfigVar ('MASSCOUNT','15');
793 setConfigVar ('MAXSELSIZE','30');
794 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
795 setConfigVar ('ROW_SCALE','2');
796 setConfigVar ('PORTS_PER_ROW','12');
797 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
798 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
799 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
800 setConfigVar ('USER_AUTH_SRC','database');
801 setConfigVar ('DEFAULT_SLB_VS_PORT','');
802 setConfigVar ('DEFAULT_SLB_RS_PORT','');
803 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
804 setConfigVar ('NATV4_PERFORMERS','4,7,8');
805 setConfigVar ('DETECT_URLS','no');
806 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
807 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
808 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
809 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
810 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
811 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
812 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
813 setConfigVar ('IPV4_AUTO_RELEASE','1');
814 setConfigVar ('SHOW_LAST_TAB', 'no');
815 setConfigVar ('COOKIE_TTL', '1209600');
816 setConfigVar ('EXT_IPV4_VIEW', 'yes');
817 setConfigVar ('TREE_THRESHOLD', '25');
818 setConfigVar ('IPV4_JAYWALK', 'no');
819 setConfigVar ('ADDNEW_AT_TOP', 'yes');
820 return buildRedirectURL ('OK');
821 }
822
823 // Add single record.
824 function addRealServer ()
825 {
826 assertUIntArg ('pool_id', __FUNCTION__);
827 assertIPv4Arg ('remoteip', __FUNCTION__);
828 assertStringArg ('rsport', __FUNCTION__, TRUE);
829 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
830 if (!addRStoRSPool (
831 $_REQUEST['pool_id'],
832 $_REQUEST['remoteip'],
833 $_REQUEST['rsport'],
834 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
835 $_REQUEST['rsconfig']
836 ))
837 return buildRedirectURL ('ERR');
838 else
839 return buildRedirectURL ('OK');
840 }
841
842 // Parse textarea submitted and try adding a real server for each line.
843 function addRealServers ()
844 {
845 assertUIntArg ('pool_id', __FUNCTION__);
846 assertStringArg ('format', __FUNCTION__);
847 assertStringArg ('rawtext', __FUNCTION__);
848 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
849 $ngood = $nbad = 0;
850 $rsconfig = '';
851 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
852 foreach (explode ('\n', $rawtext) as $line)
853 {
854 if (empty ($line))
855 continue;
856 $match = array ();
857 switch ($_REQUEST['format'])
858 {
859 case 'ipvs_2': // address and port only
860 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
861 continue;
862 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
863 $ngood++;
864 else
865 $nbad++;
866 break;
867 case 'ipvs_3': // address, port and weight
868 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
869 continue;
870 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
871 $ngood++;
872 else
873 $nbad++;
874 break;
875 case 'ssv_2': // IP address and port
876 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
877 continue;
878 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
879 $ngood++;
880 else
881 $nbad++;
882 break;
883 case 'ssv_1': // IP address
884 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
885 continue;
886 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
887 $ngood++;
888 else
889 $nbad++;
890 break;
891 default:
892 return buildRedirectURL ('ERR1');
893 break;
894 }
895 }
896 if ($nbad == 0 and $ngood > 0)
897 return buildRedirectURL ('OK', array ($ngood));
898 else
899 return buildRedirectURL ('ERR2', array ($ngood, $nbad));
900 }
901
902 function addVService ()
903 {
904 assertIPv4Arg ('vip', __FUNCTION__);
905 assertUIntArg ('vport', __FUNCTION__);
906 assertStringArg ('proto', __FUNCTION__);
907 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
908 return buildRedirectURL ('ERR1');
909 assertStringArg ('name', __FUNCTION__, TRUE);
910 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
911 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
912 $error = commitCreateVS
913 (
914 $_REQUEST['vip'],
915 $_REQUEST['vport'],
916 $_REQUEST['proto'],
917 $_REQUEST['name'],
918 $_REQUEST['vsconfig'],
919 $_REQUEST['rsconfig'],
920 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
921 );
922 if ($error != '')
923 return buildRedirectURL ('ERR2', array ($error));
924 else
925 return buildRedirectURL ('OK');
926 }
927
928 function deleteRealServer ()
929 {
930 assertUIntArg ('id', __FUNCTION__);
931 if (!commitDeleteRS ($_REQUEST['id']))
932 return buildRedirectURL ('ERR');
933 else
934 return buildRedirectURL ('OK');
935 }
936
937 function deleteLoadBalancer ()
938 {
939 assertUIntArg ('object_id', __FUNCTION__);
940 assertUIntArg ('pool_id', __FUNCTION__);
941 assertUIntArg ('vs_id', __FUNCTION__);
942 if (!commitDeleteLB (
943 $_REQUEST['object_id'],
944 $_REQUEST['pool_id'],
945 $_REQUEST['vs_id']
946 ))
947 return buildRedirectURL ('ERR');
948 else
949 return buildRedirectURL ('OK');
950 }
951
952 function deleteVService ()
953 {
954 assertUIntArg ('vs_id', __FUNCTION__);
955 if (!commitDeleteVS ($_REQUEST['vs_id']))
956 return buildRedirectURL ('ERR');
957 else
958 return buildRedirectURL ('OK');
959 }
960
961 function updateRealServer ()
962 {
963 assertUIntArg ('rs_id', __FUNCTION__);
964 assertIPv4Arg ('rsip', __FUNCTION__);
965 assertStringArg ('rsport', __FUNCTION__, TRUE);
966 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
967 if (!commitUpdateRS (
968 $_REQUEST['rs_id'],
969 $_REQUEST['rsip'],
970 $_REQUEST['rsport'],
971 $_REQUEST['rsconfig']
972 ))
973 return buildRedirectURL ('ERR');
974 else
975 return buildRedirectURL ('OK');
976 }
977
978 function updateLoadBalancer ()
979 {
980 assertUIntArg ('object_id', __FUNCTION__);
981 assertUIntArg ('pool_id', __FUNCTION__);
982 assertUIntArg ('vs_id', __FUNCTION__);
983 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
984 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
985 if (!commitUpdateLB (
986 $_REQUEST['object_id'],
987 $_REQUEST['pool_id'],
988 $_REQUEST['vs_id'],
989 $_REQUEST['vsconfig'],
990 $_REQUEST['rsconfig']
991 ))
992 return buildRedirectURL ('ERR');
993 else
994 return buildRedirectURL ('OK');
995 }
996
997 function updateVService ()
998 {
999 assertUIntArg ('vs_id', __FUNCTION__);
1000 assertIPv4Arg ('vip', __FUNCTION__);
1001 assertUIntArg ('vport', __FUNCTION__);
1002 assertStringArg ('proto', __FUNCTION__);
1003 assertStringArg ('name', __FUNCTION__, TRUE);
1004 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1005 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1006 if (!commitUpdateVS (
1007 $_REQUEST['vs_id'],
1008 $_REQUEST['vip'],
1009 $_REQUEST['vport'],
1010 $_REQUEST['proto'],
1011 $_REQUEST['name'],
1012 $_REQUEST['vsconfig'],
1013 $_REQUEST['rsconfig']
1014 ))
1015 return buildRedirectURL ('ERR');
1016 else
1017 return buildRedirectURL ('OK');
1018 }
1019
1020 function addLoadBalancer ()
1021 {
1022 assertUIntArg ('pool_id', __FUNCTION__);
1023 assertUIntArg ('object_id', __FUNCTION__);
1024 assertUIntArg ('vs_id', __FUNCTION__);
1025 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1026 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1027 if (!addLBtoRSPool (
1028 $_REQUEST['pool_id'],
1029 $_REQUEST['object_id'],
1030 $_REQUEST['vs_id'],
1031 $_REQUEST['vsconfig'],
1032 $_REQUEST['rsconfig']
1033 ))
1034 return buildRedirectURL ('ERR');
1035 else
1036 return buildRedirectURL ('OK');
1037 }
1038
1039 function addRSPool ()
1040 {
1041 assertStringArg ('name', __FUNCTION__, TRUE);
1042 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1043 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1044 $error = commitCreateRSPool
1045 (
1046 $_REQUEST['name'],
1047 $_REQUEST['vsconfig'],
1048 $_REQUEST['rsconfig'],
1049 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1050 );
1051 if ($error != '')
1052 return buildRedirectURL ('ERR', array ($error));
1053 else
1054 return buildRedirectURL ('OK');
1055 }
1056
1057 function deleteRSPool ()
1058 {
1059 assertUIntArg ('pool_id', __FUNCTION__);
1060 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1061 return buildRedirectURL ('ERR');
1062 else
1063 return buildRedirectURL ('OK');
1064 }
1065
1066 function updateRSPool ()
1067 {
1068 assertUIntArg ('pool_id', __FUNCTION__);
1069 assertStringArg ('name', __FUNCTION__, TRUE);
1070 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1071 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1072 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1073 return buildRedirectURL ('ERR');
1074 else
1075 return buildRedirectURL ('OK');
1076 }
1077
1078 function updateRSInService ()
1079 {
1080 assertUIntArg ('rscount', __FUNCTION__);
1081 $pool_id = $_REQUEST['pool_id'];
1082 $orig = getRSPoolInfo ($pool_id);
1083 $nbad = $ngood = 0;
1084 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1085 {
1086 $rs_id = $_REQUEST["rsid_${i}"];
1087 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1088 $newval = 'yes';
1089 else
1090 $newval = 'no';
1091 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1092 {
1093 if (commitSetInService ($rs_id, $newval))
1094 $ngood++;
1095 else
1096 $nbad++;
1097 }
1098 }
1099 if (!$nbad)
1100 return buildRedirectURL ('OK', array ($ngood));
1101 else
1102 return buildRedirectURL ('ERR', array ($nbad, $ngood));
1103 }
1104
1105 // FIXME: check, that each submitted address belongs to the prefix we
1106 // are operating on.
1107 function importPTRData ()
1108 {
1109 assertUIntArg ('addrcount', __FUNCTION__);
1110 $nbad = $ngood = 0;
1111 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1112 {
1113 $inputname = "import_${i}";
1114 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1115 continue;
1116 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1117 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1118 assertStringArg ("rsvd_${i}", __FUNCTION__);
1119 // Non-existent addresses will not have this argument set in request.
1120 $rsvd = 'no';
1121 if ($_REQUEST["rsvd_${i}"] == 'yes')
1122 $rsvd = 'yes';
1123 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1124 $ngood++;
1125 else
1126 $nbad++;
1127 }
1128 if (!$nbad)
1129 return buildRedirectURL ('OK', array ($ngood));
1130 else
1131 return buildRedirectURL ('ERR', array ($nbad, $ngood));
1132 }
1133
1134 function generateAutoPorts ()
1135 {
1136 global $pageno;
1137 assertUIntArg ('object_id', __FUNCTION__);
1138 $info = getObjectInfo ($_REQUEST['object_id']);
1139 // Navigate away in case of success, stay at the place otherwise.
1140 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
1141 return buildRedirectURL ('OK', array(), $pageno, 'ports');
1142 else
1143 return buildRedirectURL ('ERR');
1144 }
1145
1146 // Filter out implicit tags before storing the new tag set.
1147 function saveEntityTags ()
1148 {
1149 $page2realm = array
1150 (
1151 'file' => 'file',
1152 'ipv4net' => 'ipv4net',
1153 'ipv4rspool' => 'ipv4rspool',
1154 'ipv4vs' => 'ipv4vs',
1155 'object' => 'object',
1156 'rack' => 'rack',
1157 'user' => 'user'
1158 );
1159 global $explicit_tags, $implicit_tags, $page, $pageno;
1160 if (!isset ($page2realm[$pageno]) or !isset ($page[$pageno]['bypass']))
1161 {
1162 showError ('Internal error', __FUNCTION__);
1163 die;
1164 }
1165 $realm = $page2realm[$pageno];
1166 $bypass = $page[$pageno]['bypass'];
1167 assertUIntArg ($bypass, __FUNCTION__);
1168 $entity_id = $_REQUEST[$bypass];
1169 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1170 // Build a chain from the submitted data, minimize it,
1171 // then wipe existing records and store the new set instead.
1172 destroyTagsForEntity ($realm, $entity_id);
1173 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
1174 $n_succeeds = $n_errors = 0;
1175 foreach ($newchain as $taginfo)
1176 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
1177 $n_succeeds++;
1178 else
1179 $n_errors++;
1180 if ($n_errors)
1181 return buildRedirectURL ('ERR', array ($n_succeeds, $n_errors));
1182 else
1183 return buildRedirectURL ('OK', array ($n_succeeds));
1184 }
1185
1186 function destroyTag ()
1187 {
1188 assertUIntArg ('tag_id', __FUNCTION__);
1189 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
1190 return buildRedirectURL ('OK');
1191 else
1192 return buildRedirectURL ('ERR');
1193 }
1194
1195 function createTag ()
1196 {
1197 assertStringArg ('tag_name', __FUNCTION__);
1198 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1199 $tagname = trim ($_REQUEST['tag_name']);
1200 if (!validTagName ($tagname))
1201 return buildRedirectURL ('ERR1', array ($tagname));
1202 if (tagExistsInDatabase ($tagname))
1203 return buildRedirectURL ('ERR2', array ($tagname));
1204 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1205 $parent_id = 'NULL';
1206 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1207 return buildRedirectURL ('OK', array ($tagname));
1208 else
1209 return buildRedirectURL ('ERR3', array ($tagname, $ret));
1210 }
1211
1212 function updateTag ()
1213 {
1214 assertUIntArg ('tag_id', __FUNCTION__);
1215 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1216 assertStringArg ('tag_name', __FUNCTION__);
1217 $tagname = trim ($_REQUEST['tag_name']);
1218 if (!validTagName ($tagname))
1219 return buildRedirectURL ('ERR1', array ($tagname));
1220 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1221 $parent_id = 'NULL';
1222 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1223 return buildRedirectURL ('OK', array ($tagname));
1224 else
1225 return buildRedirectURL ('ERR2', array ($tagname, $ret));
1226 }
1227
1228 function rollTags ()
1229 {
1230 assertUIntArg ('row_id', __FUNCTION__);
1231 assertStringArg ('sum', __FUNCTION__, TRUE);
1232 assertUIntArg ('realsum', __FUNCTION__);
1233 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1234 return buildRedirectURL ('ERR');
1235 // Even if the user requested an empty tag list, don't bail out, but process existing
1236 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1237 // have its chains refined to "normal" form.
1238 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1239 $n_ok = 0;
1240 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1241 // filter out the same tag again and again. It will have own noise to cancel.
1242 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
1243 foreach (getRacksForRow ($_REQUEST['row_id']) as $rackInfo)
1244 {
1245 if (rebuildTagChainForEntity ('rack', $rackInfo['id'], $extrachain))
1246 $n_ok++;
1247 foreach (stuffInRackspace (getRackData ($rackInfo['id'])) as $object_id)
1248 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1249 $n_ok++;
1250 }
1251 return buildRedirectURL ('OK', array ($n_ok));
1252 }
1253
1254 function changeMyPassword ()
1255 {
1256 global $accounts, $remote_username;
1257 if (getConfigVar ('USER_AUTH_SRC') != 'database')
1258 return buildRedirectURL ('ERR1');
1259 assertStringArg ('oldpassword', __FUNCTION__);
1260 assertStringArg ('newpassword1', __FUNCTION__);
1261 assertStringArg ('newpassword2', __FUNCTION__);
1262 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
1263 return buildRedirectURL ('ERR2');
1264 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
1265 return buildRedirectURL ('ERR3');
1266 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
1267 return buildRedirectURL ('OK');
1268 else
1269 return buildRedirectURL ('ERR4');
1270 }
1271
1272 function saveRackCode ()
1273 {
1274 assertStringArg ('rackcode');
1275 // For the test to succeed, unescape LFs, strip CRs.
1276 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
1277 $parseTree = getRackCode ($newcode);
1278 if ($parseTree['result'] != 'ACK')
1279 return buildRedirectURL ('ERR1', array ($parseTree['load']));
1280 saveScript ('RackCodeCache', '');
1281 if (saveScript ('RackCode', $newcode))
1282 return buildRedirectURL ('OK');
1283 else
1284 return buildRedirectURL ('ERR2');
1285 }
1286
1287 // This handler's context is pre-built, but not authorized. It is assumed, that the
1288 // handler will take existing context and before each commit check authorization
1289 // on the base chain plus necessary context added.
1290 function setPortVLAN ()
1291 {
1292 assertUIntArg ('portcount', __FUNCTION__);
1293 $data = getSwitchVLANs ($_REQUEST['object_id']);
1294 if ($data === NULL)
1295 return buildRedirectURL ('ERR1');
1296 list ($vlanlist, $portlist) = $data;
1297 // Here we just build up 1 set command for the gateway with all of the ports
1298 // included. The gateway is expected to filter unnecessary changes silently
1299 // and to provide a list of responses with either error or success message
1300 // for each of the rest.
1301 $nports = $_REQUEST['portcount'];
1302 $prefix = 'set ';
1303 $log = array ('v' => 2, 'm' => array());
1304 $setcmd = '';
1305 for ($i = 0; $i < $nports; $i++)
1306 if
1307 (
1308 !isset ($_REQUEST['portname_' . $i]) ||
1309 !isset ($_REQUEST['vlanid_' . $i]) ||
1310 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1311 )
1312 $log['m'][] = array ('c' => 158, 'a' => array ($i));
1313 elseif
1314 (
1315 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1316 $portlist[$i]['vlaind'] == 'TRUNK'
1317 )
1318 continue;
1319 else
1320 {
1321 $portname = $_REQUEST['portname_' . $i];
1322 $oldvlanid = $portlist[$i]['vlanid'];
1323 $newvlanid = $_REQUEST['vlanid_' . $i];
1324 // Finish the security context and evaluate it.
1325 $annex = array();
1326 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1327 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1328 if (!permitted (NULL, NULL, NULL, $annex))
1329 {
1330 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
1331 continue;
1332 }
1333 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1334 $prefix = ';';
1335 }
1336 // Feed the gateway and interpret its (non)response.
1337 if ($setcmd != '')
1338 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1339 else
1340 $log['m'][] = array ('c' => 201);
1341 return buildWideRedirectURL ($log);
1342 }
1343
1344 function submitSLBConfig ()
1345 {
1346 assertUIntArg ('object_id', __FUNCTION__);
1347 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
1348 $msglog = gwSendFile ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
1349 return buildWideRedirectURL ($msglog);
1350 }
1351
1352 function addRow ()
1353 {
1354 assertStringArg ('name', __FUNCTION__);
1355
1356 if (commitSupplementDictionary (3, $_REQUEST['name']) === TRUE)
1357 return buildRedirectURL ('OK', array ($_REQUEST['name']));
1358 else
1359 return buildRedirectURL ('ERR', array ($_REQUEST['name']));
1360 }
1361
1362 function updateRow ()
1363 {
1364 assertUIntArg ('row_id', __FUNCTION__);
1365 assertStringArg ('name', __FUNCTION__);
1366
1367 if (TRUE === commitUpdateDictionary (3, $_REQUEST['row_id'], $_REQUEST['name']))
1368 return buildRedirectURL ('OK', array ($_REQUEST['name']));
1369 else
1370 return buildRedirectURL ('ERR', array ($_REQUEST['name']));
1371 }
1372
1373 function addRack ()
1374 {
1375 assertUIntArg ('row_id', __FUNCTION__);
1376 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1377 if (isset ($_REQUEST['got_data']))
1378 {
1379 assertStringArg ('rack_name', __FUNCTION__);
1380 assertUIntArg ('rack_height1', __FUNCTION__);
1381 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1382
1383 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
1384 return buildRedirectURL ('OK', array ($_REQUEST['rack_name']));
1385 else
1386 return buildRedirectURL ('ERR1', array ($_REQUEST['rack_name']));
1387 }
1388 elseif (isset ($_REQUEST['got_mdata']))
1389 {
1390 assertUIntArg ('rack_height2', __FUNCTION__);
1391 assertStringArg ('rack_names', __FUNCTION__, TRUE);
1392 $log = array ('v' => 2);
1393 // copy-and-paste from renderAddMultipleObjectsForm()
1394 $names1 = explode ('\n', $_REQUEST['rack_names']);
1395 $names2 = array();
1396 foreach ($names1 as $line)
1397 {
1398 $parts = explode ('\r', $line);
1399 reset ($parts);
1400 if (empty ($parts[0]))
1401 continue;
1402 else
1403 $names2[] = rtrim ($parts[0]);
1404 }
1405 foreach ($names2 as $cname)
1406 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
1407 $log['m'][] = array ('c' => getMessageCode ('OK'), 'a' => array ($cname));
1408 else
1409 $log['m'][] = array ('c' => getMessageCode ('ERR1'), 'a' => array ($cname));
1410 return buildWideRedirectURL ($log);
1411 }
1412 else
1413 return buildRedirectURL ('ERR2');
1414 }
1415
1416 function updateRack ()
1417 {
1418 assertUIntArg ('rack_id', __FUNCTION__);
1419 assertUIntArg ('rack_row_id', __FUNCTION__);
1420 assertUIntArg ('rack_height', __FUNCTION__);
1421 assertStringArg ('rack_name', __FUNCTION__);
1422 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1423
1424 resetThumbCache ($_REQUEST['rack_id']);
1425 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
1426 return buildRedirectURL ('OK', array ($_REQUEST['rack_name']));
1427 else
1428 return buildRedirectURL ('ERR');
1429 }
1430
1431 function querySNMPData ()
1432 {
1433 assertUIntArg ('object_id', __FUNCTION__);
1434 assertStringArg ('community', __FUNCTION__);
1435 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
1436 }
1437
1438 // File-related functions
1439 function addFileWithoutLink ()
1440 {
1441 assertStringArg ('comment', __FUNCTION__, TRUE);
1442
1443 // Make sure the file can be uploaded
1444 if (get_cfg_var('file_uploads') != 1)
1445 return buildRedirectURL ('ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1446
1447 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1448 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1449
1450 if ($error != '')
1451 return buildRedirectURL ('ERR', array ($error));
1452
1453 return buildRedirectURL ('OK', array ($_FILES['file']['name']));
1454 }
1455
1456 function addFileToEntity ()
1457 {
1458 assertStringArg ('entity_type', __FUNCTION__);
1459 assertUIntArg ('entity_id', __FUNCTION__);
1460 assertStringArg ('comment', __FUNCTION__, TRUE);
1461 if (empty ($_REQUEST['entity_type']) || empty ($_REQUEST['entity_id']))
1462 return buildRedirectURL ('ERR');
1463
1464 // Make sure the file can be uploaded
1465 if (get_cfg_var('file_uploads') != 1)
1466 return buildRedirectURL ('ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1467
1468 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1469 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1470 if ($error != '')
1471 return buildRedirectURL ('ERR', array ($error));
1472
1473 $file_id = lastInsertID();
1474 $error = commitLinkFile ($file_id, $_REQUEST['entity_type'], $_REQUEST['entity_id']);
1475 if ($error != '')
1476 return buildRedirectURL ('ERR', array ($error));
1477
1478 return buildRedirectURL ('OK', array ($_FILES['file']['name']));
1479 }
1480
1481 function linkFileToEntity ()
1482 {
1483 assertUIntArg ('entity_id', __FUNCTION__);
1484 assertUIntArg ('file_id', __FUNCTION__);
1485 assertStringArg ('file_name', __FUNCTION__);
1486
1487 $error = commitLinkFile ($_REQUEST['file_id'], $_REQUEST['entity_type'], $_REQUEST['entity_id']);
1488 if ($error != '')
1489 return buildRedirectURL ('ERR', array ($error));
1490
1491 return buildRedirectURL ('OK', array ($_REQUEST['file_name']));
1492 }
1493
1494 function updateFile ()
1495 {
1496 assertUIntArg ('file_id', __FUNCTION__);
1497 assertStringArg ('name', __FUNCTION__);
1498 assertStringArg ('comment', __FUNCTION__, TRUE);
1499 $error = commitUpdateFile ($_REQUEST['file_id'], $_REQUEST['comment']);
1500 if ($error != '')
1501 return buildRedirectURL ('ERR', array ($error));
1502
1503 return buildRedirectURL ('OK', array ($_REQUEST['name']));
1504 }
1505
1506 function unlinkFile ()
1507 {
1508 assertUIntArg ('link_id', __FUNCTION__);
1509 $error = commitUnlinkFile ($_REQUEST['link_id']);
1510
1511 if ($error != '')
1512 return buildRedirectURL ('ERR', array ($error));
1513
1514 return buildRedirectURL ('OK', array ($_REQUEST['name']));
1515 }
1516
1517 function deleteFile ()
1518 {
1519 assertUIntArg ('file_id', __FUNCTION__);
1520 $error = commitDeleteFile ($_REQUEST['file_id']);
1521
1522 if ($error != '')
1523 return buildRedirectURL ('ERR', array ($error));
1524
1525 return buildRedirectURL ('OK', array ($_REQUEST['name']));
1526 }
1527
1528 ?>