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