r2357 - implement previews for files
[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 (__FUNCTION__, 'OK');
109 else
110 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
133 else
134 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
159 else
160 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, 'ERR2', array ($error));
172 else
173 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, 'ERR2', array ($error));
192 else
193 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
203 else
204 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
218 else
219 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
232 else
233 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
357 else
358 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
369 else
370 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'ERR1', array ($ip));
384
385 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
386 if ($error != '')
387 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
411 else
412 return buildRedirectURL (__FUNCTION__, 'OK');
413 }
414
415 function delIPv4Prefix ()
416 {
417 assertUIntArg ('id', __FUNCTION__);
418 $error = destroyIPv4Prefix ($_REQUEST['id']);
419 if ($error != '')
420 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
421 else
422 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
433 else
434 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'ERR', array ($error));
449 else
450 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK', array ($username));
463 else
464 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, 'OK', array ($username));
484 else
485 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
493 else
494 return buildRedirectURL (__FUNCTION__, 'ERR');
495 }
496
497 function disableUser ()
498 {
499 assertUIntArg ('user_id', __FUNCTION__);
500 if ($_REQUEST['user_id'] == 1)
501 return buildRedirectURL (__FUNCTION__, 'ERR1');
502 if (commitEnableUserAccount ($_REQUEST['user_id'], 'no'))
503 return buildRedirectURL (__FUNCTION__, 'OK');
504 else
505 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK', array ($error_count, $success_count));
550 else
551 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
561 else
562 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
571 else
572 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
581 else
582 return buildRedirectURL (__FUNCTION__, 'ERR');
583 }
584
585 function addChapter ()
586 {
587 assertStringArg ('chapter_name', __FUNCTION__);
588 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
589 return buildRedirectURL (__FUNCTION__, 'OK');
590 else
591 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
600 else
601 return buildRedirectURL (__FUNCTION__, 'ERR');
602 }
603
604 function delChapter ()
605 {
606 assertUIntArg ('chapter_no', __FUNCTION__);
607 if (commitDeleteChapter ($_REQUEST['chapter_no']))
608 return buildRedirectURL (__FUNCTION__, 'OK');
609 else
610 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
619 else
620 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
629 else
630 return buildRedirectURL (__FUNCTION__, 'ERR');
631 }
632
633 function deleteAttribute ()
634 {
635 assertUIntArg ('attr_id', __FUNCTION__);
636 if (commitDeleteAttribute ($_REQUEST['attr_id']))
637 return buildRedirectURL (__FUNCTION__, 'OK');
638 else
639 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
649 else
650 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
659 else
660 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, 'OK');
669 else
670 return buildRedirectURL (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, '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 (__FUNCTION__, 'OK');
746 }
747
748 function deleteObject ()
749 {
750 assertUIntArg ('object_id', __FUNCTION__);
751 $error = commitDeleteObject ($_REQUEST['object_id']);
752
753 if ($error != '')
754 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
755
756 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
757 }
758
759 function useupPort ()
760 {
761 assertUIntArg ('port_id', __FUNCTION__);
762 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
763 return buildRedirectURL (__FUNCTION__, 'OK');
764 else
765 return buildRedirectURL (__FUNCTION__, 'ERR');
766 }
767
768 function updateUI ()
769 {
770 assertUIntArg ('num_vars', __FUNCTION__);
771 $error = '';
772
773 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
774 {
775 assertStringArg ("${i}_varname", __FUNCTION__);
776 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
777 $varname = $_REQUEST["${i}_varname"];
778 $varvalue = $_REQUEST["${i}_varvalue"];
779
780 // If form value = value in DB, don't bother updating DB
781 if ($varvalue == getConfigVar ($varname))
782 continue;
783
784 // Note if the queries succeed or not, it determines which page they see.
785 $error = setConfigVar ($varname, $varvalue, TRUE);
786 if (!empty ($error))
787 break;
788 }
789
790 if ($error != '')
791 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
792
793 return buildRedirectURL (__FUNCTION__, 'OK');
794 }
795
796 function resetUIConfig()
797 {
798 setConfigVar ('default_port_type','24');
799 setConfigVar ('MASSCOUNT','15');
800 setConfigVar ('MAXSELSIZE','30');
801 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
802 setConfigVar ('ROW_SCALE','2');
803 setConfigVar ('PORTS_PER_ROW','12');
804 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
805 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
806 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
807 setConfigVar ('DEFAULT_SLB_VS_PORT','');
808 setConfigVar ('DEFAULT_SLB_RS_PORT','');
809 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
810 setConfigVar ('NATV4_PERFORMERS','4,7,8');
811 setConfigVar ('DETECT_URLS','no');
812 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
813 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
814 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
815 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
816 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
817 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
818 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
819 setConfigVar ('IPV4_AUTO_RELEASE','1');
820 setConfigVar ('SHOW_LAST_TAB', 'no');
821 setConfigVar ('COOKIE_TTL', '1209600');
822 setConfigVar ('EXT_IPV4_VIEW', 'yes');
823 setConfigVar ('TREE_THRESHOLD', '25');
824 setConfigVar ('IPV4_JAYWALK', 'no');
825 setConfigVar ('ADDNEW_AT_TOP', 'yes');
826 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
827 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
828 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
829 setConfigVar ('PREVIEW_TEXT_COLS', '80');
830 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
831 return buildRedirectURL (__FUNCTION__, 'OK');
832 }
833
834 // Add single record.
835 function addRealServer ()
836 {
837 assertUIntArg ('pool_id', __FUNCTION__);
838 assertIPv4Arg ('remoteip', __FUNCTION__);
839 assertStringArg ('rsport', __FUNCTION__, TRUE);
840 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841 if (!addRStoRSPool (
842 $_REQUEST['pool_id'],
843 $_REQUEST['remoteip'],
844 $_REQUEST['rsport'],
845 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
846 $_REQUEST['rsconfig']
847 ))
848 return buildRedirectURL (__FUNCTION__, 'ERR');
849 else
850 return buildRedirectURL (__FUNCTION__, 'OK');
851 }
852
853 // Parse textarea submitted and try adding a real server for each line.
854 function addRealServers ()
855 {
856 assertUIntArg ('pool_id', __FUNCTION__);
857 assertStringArg ('format', __FUNCTION__);
858 assertStringArg ('rawtext', __FUNCTION__);
859 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
860 $ngood = $nbad = 0;
861 $rsconfig = '';
862 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
863 foreach (explode ('\n', $rawtext) as $line)
864 {
865 if (empty ($line))
866 continue;
867 $match = array ();
868 switch ($_REQUEST['format'])
869 {
870 case 'ipvs_2': // address and port only
871 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
872 continue;
873 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
874 $ngood++;
875 else
876 $nbad++;
877 break;
878 case 'ipvs_3': // address, port and weight
879 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
880 continue;
881 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
882 $ngood++;
883 else
884 $nbad++;
885 break;
886 case 'ssv_2': // IP address and port
887 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
888 continue;
889 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
890 $ngood++;
891 else
892 $nbad++;
893 break;
894 case 'ssv_1': // IP address
895 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
896 continue;
897 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
898 $ngood++;
899 else
900 $nbad++;
901 break;
902 default:
903 return buildRedirectURL (__FUNCTION__, 'ERR1');
904 break;
905 }
906 }
907 if ($nbad == 0 and $ngood > 0)
908 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
909 else
910 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
911 }
912
913 function addVService ()
914 {
915 assertIPv4Arg ('vip', __FUNCTION__);
916 assertUIntArg ('vport', __FUNCTION__);
917 assertStringArg ('proto', __FUNCTION__);
918 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
919 return buildRedirectURL (__FUNCTION__, 'ERR1');
920 assertStringArg ('name', __FUNCTION__, TRUE);
921 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
922 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
923 $error = commitCreateVS
924 (
925 $_REQUEST['vip'],
926 $_REQUEST['vport'],
927 $_REQUEST['proto'],
928 $_REQUEST['name'],
929 $_REQUEST['vsconfig'],
930 $_REQUEST['rsconfig'],
931 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
932 );
933 if ($error != '')
934 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
935 else
936 return buildRedirectURL (__FUNCTION__, 'OK');
937 }
938
939 function deleteRealServer ()
940 {
941 assertUIntArg ('id', __FUNCTION__);
942 if (!commitDeleteRS ($_REQUEST['id']))
943 return buildRedirectURL (__FUNCTION__, 'ERR');
944 else
945 return buildRedirectURL (__FUNCTION__, 'OK');
946 }
947
948 function deleteLoadBalancer ()
949 {
950 assertUIntArg ('object_id', __FUNCTION__);
951 assertUIntArg ('pool_id', __FUNCTION__);
952 assertUIntArg ('vs_id', __FUNCTION__);
953 if (!commitDeleteLB (
954 $_REQUEST['object_id'],
955 $_REQUEST['pool_id'],
956 $_REQUEST['vs_id']
957 ))
958 return buildRedirectURL (__FUNCTION__, 'ERR');
959 else
960 return buildRedirectURL (__FUNCTION__, 'OK');
961 }
962
963 function deleteVService ()
964 {
965 assertUIntArg ('vs_id', __FUNCTION__);
966 if (!commitDeleteVS ($_REQUEST['vs_id']))
967 return buildRedirectURL (__FUNCTION__, 'ERR');
968 else
969 return buildRedirectURL (__FUNCTION__, 'OK');
970 }
971
972 function updateRealServer ()
973 {
974 assertUIntArg ('rs_id', __FUNCTION__);
975 assertIPv4Arg ('rsip', __FUNCTION__);
976 assertStringArg ('rsport', __FUNCTION__, TRUE);
977 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
978 if (!commitUpdateRS (
979 $_REQUEST['rs_id'],
980 $_REQUEST['rsip'],
981 $_REQUEST['rsport'],
982 $_REQUEST['rsconfig']
983 ))
984 return buildRedirectURL (__FUNCTION__, 'ERR');
985 else
986 return buildRedirectURL (__FUNCTION__, 'OK');
987 }
988
989 function updateLoadBalancer ()
990 {
991 assertUIntArg ('object_id', __FUNCTION__);
992 assertUIntArg ('pool_id', __FUNCTION__);
993 assertUIntArg ('vs_id', __FUNCTION__);
994 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
995 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
996 if (!commitUpdateLB (
997 $_REQUEST['object_id'],
998 $_REQUEST['pool_id'],
999 $_REQUEST['vs_id'],
1000 $_REQUEST['vsconfig'],
1001 $_REQUEST['rsconfig']
1002 ))
1003 return buildRedirectURL (__FUNCTION__, 'ERR');
1004 else
1005 return buildRedirectURL (__FUNCTION__, 'OK');
1006 }
1007
1008 function updateVService ()
1009 {
1010 assertUIntArg ('vs_id', __FUNCTION__);
1011 assertIPv4Arg ('vip', __FUNCTION__);
1012 assertUIntArg ('vport', __FUNCTION__);
1013 assertStringArg ('proto', __FUNCTION__);
1014 assertStringArg ('name', __FUNCTION__, TRUE);
1015 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1016 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1017 if (!commitUpdateVS (
1018 $_REQUEST['vs_id'],
1019 $_REQUEST['vip'],
1020 $_REQUEST['vport'],
1021 $_REQUEST['proto'],
1022 $_REQUEST['name'],
1023 $_REQUEST['vsconfig'],
1024 $_REQUEST['rsconfig']
1025 ))
1026 return buildRedirectURL (__FUNCTION__, 'ERR');
1027 else
1028 return buildRedirectURL (__FUNCTION__, 'OK');
1029 }
1030
1031 function addLoadBalancer ()
1032 {
1033 assertUIntArg ('pool_id', __FUNCTION__);
1034 assertUIntArg ('object_id', __FUNCTION__);
1035 assertUIntArg ('vs_id', __FUNCTION__);
1036 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1037 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1038 if (!addLBtoRSPool (
1039 $_REQUEST['pool_id'],
1040 $_REQUEST['object_id'],
1041 $_REQUEST['vs_id'],
1042 $_REQUEST['vsconfig'],
1043 $_REQUEST['rsconfig']
1044 ))
1045 return buildRedirectURL (__FUNCTION__, 'ERR');
1046 else
1047 return buildRedirectURL (__FUNCTION__, 'OK');
1048 }
1049
1050 function addRSPool ()
1051 {
1052 assertStringArg ('name', __FUNCTION__, TRUE);
1053 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1054 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1055 $error = commitCreateRSPool
1056 (
1057 $_REQUEST['name'],
1058 $_REQUEST['vsconfig'],
1059 $_REQUEST['rsconfig'],
1060 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1061 );
1062 if ($error != '')
1063 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1064 else
1065 return buildRedirectURL (__FUNCTION__, 'OK');
1066 }
1067
1068 function deleteRSPool ()
1069 {
1070 assertUIntArg ('pool_id', __FUNCTION__);
1071 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1072 return buildRedirectURL (__FUNCTION__, 'ERR');
1073 else
1074 return buildRedirectURL (__FUNCTION__, 'OK');
1075 }
1076
1077 function updateRSPool ()
1078 {
1079 assertUIntArg ('pool_id', __FUNCTION__);
1080 assertStringArg ('name', __FUNCTION__, TRUE);
1081 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1082 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1083 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1084 return buildRedirectURL (__FUNCTION__, 'ERR');
1085 else
1086 return buildRedirectURL (__FUNCTION__, 'OK');
1087 }
1088
1089 function updateRSInService ()
1090 {
1091 assertUIntArg ('rscount', __FUNCTION__);
1092 $pool_id = $_REQUEST['pool_id'];
1093 $orig = getRSPoolInfo ($pool_id);
1094 $nbad = $ngood = 0;
1095 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1096 {
1097 $rs_id = $_REQUEST["rsid_${i}"];
1098 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1099 $newval = 'yes';
1100 else
1101 $newval = 'no';
1102 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1103 {
1104 if (commitSetInService ($rs_id, $newval))
1105 $ngood++;
1106 else
1107 $nbad++;
1108 }
1109 }
1110 if (!$nbad)
1111 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1112 else
1113 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1114 }
1115
1116 // FIXME: check, that each submitted address belongs to the prefix we
1117 // are operating on.
1118 function importPTRData ()
1119 {
1120 assertUIntArg ('addrcount', __FUNCTION__);
1121 $nbad = $ngood = 0;
1122 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1123 {
1124 $inputname = "import_${i}";
1125 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1126 continue;
1127 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1128 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1129 assertStringArg ("rsvd_${i}", __FUNCTION__);
1130 // Non-existent addresses will not have this argument set in request.
1131 $rsvd = 'no';
1132 if ($_REQUEST["rsvd_${i}"] == 'yes')
1133 $rsvd = 'yes';
1134 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1135 $ngood++;
1136 else
1137 $nbad++;
1138 }
1139 if (!$nbad)
1140 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1141 else
1142 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1143 }
1144
1145 function generateAutoPorts ()
1146 {
1147 global $pageno;
1148 assertUIntArg ('object_id', __FUNCTION__);
1149 $info = getObjectInfo ($_REQUEST['object_id']);
1150 // Navigate away in case of success, stay at the place otherwise.
1151 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
1152 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
1153 else
1154 return buildRedirectURL (__FUNCTION__, 'ERR');
1155 }
1156
1157 // Filter out implicit tags before storing the new tag set.
1158 function saveEntityTags ()
1159 {
1160 $page2realm = array
1161 (
1162 'file' => 'file',
1163 'ipv4net' => 'ipv4net',
1164 'ipv4rspool' => 'ipv4rspool',
1165 'ipv4vs' => 'ipv4vs',
1166 'object' => 'object',
1167 'rack' => 'rack',
1168 'user' => 'user'
1169 );
1170 global $explicit_tags, $implicit_tags, $page, $pageno;
1171 if (!isset ($page2realm[$pageno]) or !isset ($page[$pageno]['bypass']))
1172 {
1173 showError ('Internal error', __FUNCTION__);
1174 die;
1175 }
1176 $realm = $page2realm[$pageno];
1177 $bypass = $page[$pageno]['bypass'];
1178 assertUIntArg ($bypass, __FUNCTION__);
1179 $entity_id = $_REQUEST[$bypass];
1180 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1181 // Build a chain from the submitted data, minimize it,
1182 // then wipe existing records and store the new set instead.
1183 destroyTagsForEntity ($realm, $entity_id);
1184 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
1185 $n_succeeds = $n_errors = 0;
1186 foreach ($newchain as $taginfo)
1187 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
1188 $n_succeeds++;
1189 else
1190 $n_errors++;
1191 if ($n_errors)
1192 return buildRedirectURL (__FUNCTION__, 'ERR', array ($n_succeeds, $n_errors));
1193 else
1194 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
1195 }
1196
1197 function destroyTag ()
1198 {
1199 assertUIntArg ('tag_id', __FUNCTION__);
1200 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
1201 return buildRedirectURL (__FUNCTION__, 'OK');
1202 else
1203 return buildRedirectURL (__FUNCTION__, 'ERR');
1204 }
1205
1206 function createTag ()
1207 {
1208 assertStringArg ('tag_name', __FUNCTION__);
1209 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1210 $tagname = trim ($_REQUEST['tag_name']);
1211 if (!validTagName ($tagname))
1212 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
1213 if (tagExistsInDatabase ($tagname))
1214 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname));
1215 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1216 $parent_id = 'NULL';
1217 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1218 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
1219 else
1220 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
1221 }
1222
1223 function updateTag ()
1224 {
1225 assertUIntArg ('tag_id', __FUNCTION__);
1226 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1227 assertStringArg ('tag_name', __FUNCTION__);
1228 $tagname = trim ($_REQUEST['tag_name']);
1229 if (!validTagName ($tagname))
1230 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
1231 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1232 $parent_id = 'NULL';
1233 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1234 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
1235 else
1236 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname, $ret));
1237 }
1238
1239 function rollTags ()
1240 {
1241 assertUIntArg ('row_id', __FUNCTION__);
1242 assertStringArg ('sum', __FUNCTION__, TRUE);
1243 assertUIntArg ('realsum', __FUNCTION__);
1244 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1245 return buildRedirectURL (__FUNCTION__, 'ERR');
1246 // Even if the user requested an empty tag list, don't bail out, but process existing
1247 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1248 // have its chains refined to "normal" form.
1249 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1250 $n_ok = 0;
1251 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1252 // filter out the same tag again and again. It will have own noise to cancel.
1253 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
1254 foreach (getRacksForRow ($_REQUEST['row_id']) as $rackInfo)
1255 {
1256 if (rebuildTagChainForEntity ('rack', $rackInfo['id'], $extrachain))
1257 $n_ok++;
1258 foreach (stuffInRackspace (getRackData ($rackInfo['id'])) as $object_id)
1259 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1260 $n_ok++;
1261 }
1262 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
1263 }
1264
1265 function changeMyPassword ()
1266 {
1267 global $accounts, $remote_username, $user_auth_src;
1268 if ($user_auth_src != 'database')
1269 return buildRedirectURL (__FUNCTION__, 'ERR1');
1270 assertStringArg ('oldpassword', __FUNCTION__);
1271 assertStringArg ('newpassword1', __FUNCTION__);
1272 assertStringArg ('newpassword2', __FUNCTION__);
1273 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
1274 return buildRedirectURL (__FUNCTION__, 'ERR2');
1275 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
1276 return buildRedirectURL (__FUNCTION__, 'ERR3');
1277 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
1278 return buildRedirectURL (__FUNCTION__, 'OK');
1279 else
1280 return buildRedirectURL (__FUNCTION__, 'ERR4');
1281 }
1282
1283 function saveRackCode ()
1284 {
1285 assertStringArg ('rackcode');
1286 // For the test to succeed, unescape LFs, strip CRs.
1287 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
1288 $parseTree = getRackCode ($newcode);
1289 if ($parseTree['result'] != 'ACK')
1290 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
1291 saveScript ('RackCodeCache', '');
1292 if (saveScript ('RackCode', $newcode))
1293 return buildRedirectURL (__FUNCTION__, 'OK');
1294 else
1295 return buildRedirectURL (__FUNCTION__, 'ERR2');
1296 }
1297
1298 // This handler's context is pre-built, but not authorized. It is assumed, that the
1299 // handler will take existing context and before each commit check authorization
1300 // on the base chain plus necessary context added.
1301 function setPortVLAN ()
1302 {
1303 assertUIntArg ('portcount', __FUNCTION__);
1304 $data = getSwitchVLANs ($_REQUEST['object_id']);
1305 if ($data === NULL)
1306 return buildRedirectURL (__FUNCTION__, 'ERR1');
1307 list ($vlanlist, $portlist) = $data;
1308 // Here we just build up 1 set command for the gateway with all of the ports
1309 // included. The gateway is expected to filter unnecessary changes silently
1310 // and to provide a list of responses with either error or success message
1311 // for each of the rest.
1312 $nports = $_REQUEST['portcount'];
1313 $prefix = 'set ';
1314 $log = array ('v' => 2, 'm' => array());
1315 $setcmd = '';
1316 for ($i = 0; $i < $nports; $i++)
1317 if
1318 (
1319 !isset ($_REQUEST['portname_' . $i]) ||
1320 !isset ($_REQUEST['vlanid_' . $i]) ||
1321 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1322 )
1323 $log['m'][] = array ('c' => 158, 'a' => array ($i));
1324 elseif
1325 (
1326 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1327 $portlist[$i]['vlaind'] == 'TRUNK'
1328 )
1329 continue;
1330 else
1331 {
1332 $portname = $_REQUEST['portname_' . $i];
1333 $oldvlanid = $portlist[$i]['vlanid'];
1334 $newvlanid = $_REQUEST['vlanid_' . $i];
1335 // Finish the security context and evaluate it.
1336 $annex = array();
1337 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1338 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1339 if (!permitted (NULL, NULL, NULL, $annex))
1340 {
1341 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
1342 continue;
1343 }
1344 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1345 $prefix = ';';
1346 }
1347 // Feed the gateway and interpret its (non)response.
1348 if ($setcmd != '')
1349 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1350 else
1351 $log['m'][] = array ('c' => 201);
1352 return buildWideRedirectURL ($log);
1353 }
1354
1355 function submitSLBConfig ()
1356 {
1357 assertUIntArg ('object_id', __FUNCTION__);
1358 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
1359 $msglog = gwSendFile ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
1360 return buildWideRedirectURL ($msglog);
1361 }
1362
1363 function addRow ()
1364 {
1365 assertStringArg ('name', __FUNCTION__);
1366
1367 if (commitSupplementDictionary (3, $_REQUEST['name']) === TRUE)
1368 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1369 else
1370 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
1371 }
1372
1373 function updateRow ()
1374 {
1375 assertUIntArg ('row_id', __FUNCTION__);
1376 assertStringArg ('name', __FUNCTION__);
1377
1378 if (TRUE === commitUpdateDictionary (3, $_REQUEST['row_id'], $_REQUEST['name']))
1379 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1380 else
1381 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
1382 }
1383
1384 function addRack ()
1385 {
1386 assertUIntArg ('row_id', __FUNCTION__);
1387 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1388 if (isset ($_REQUEST['got_data']))
1389 {
1390 assertStringArg ('rack_name', __FUNCTION__);
1391 assertUIntArg ('rack_height1', __FUNCTION__);
1392 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1393
1394 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
1395 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
1396 else
1397 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
1398 }
1399 elseif (isset ($_REQUEST['got_mdata']))
1400 {
1401 assertUIntArg ('rack_height2', __FUNCTION__);
1402 assertStringArg ('rack_names', __FUNCTION__, TRUE);
1403 $log = array ('v' => 2);
1404 // copy-and-paste from renderAddMultipleObjectsForm()
1405 $names1 = explode ('\n', $_REQUEST['rack_names']);
1406 $names2 = array();
1407 foreach ($names1 as $line)
1408 {
1409 $parts = explode ('\r', $line);
1410 reset ($parts);
1411 if (empty ($parts[0]))
1412 continue;
1413 else
1414 $names2[] = rtrim ($parts[0]);
1415 }
1416 foreach ($names2 as $cname)
1417 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
1418 $log['m'][] = array ('c' => getMessageCode (__FUNCTION__, 'OK'), 'a' => array ($cname));
1419 else
1420 $log['m'][] = array ('c' => getMessageCode (__FUNCTION__, 'ERR1'), 'a' => array ($cname));
1421 return buildWideRedirectURL ($log);
1422 }
1423 else
1424 return buildRedirectURL (__FUNCTION__, 'ERR2');
1425 }
1426
1427 function updateRack ()
1428 {
1429 assertUIntArg ('rack_id', __FUNCTION__);
1430 assertUIntArg ('rack_row_id', __FUNCTION__);
1431 assertUIntArg ('rack_height', __FUNCTION__);
1432 assertStringArg ('rack_name', __FUNCTION__);
1433 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1434
1435 resetThumbCache ($_REQUEST['rack_id']);
1436 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
1437 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
1438 else
1439 return buildRedirectURL (__FUNCTION__, 'ERR');
1440 }
1441
1442 function querySNMPData ()
1443 {
1444 assertUIntArg ('object_id', __FUNCTION__);
1445 assertStringArg ('community', __FUNCTION__);
1446 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
1447 }
1448
1449 // File-related functions
1450 function addFileWithoutLink ()
1451 {
1452 assertStringArg ('comment', __FUNCTION__, TRUE);
1453
1454 // Make sure the file can be uploaded
1455 if (get_cfg_var('file_uploads') != 1)
1456 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1457
1458 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1459 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1460
1461 if ($error != '')
1462 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1463
1464 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
1465 }
1466
1467 function addFileToEntity ()
1468 {
1469 assertStringArg ('entity_type', __FUNCTION__);
1470 assertUIntArg ('entity_id', __FUNCTION__);
1471 assertStringArg ('comment', __FUNCTION__, TRUE);
1472 if (empty ($_REQUEST['entity_type']) || empty ($_REQUEST['entity_id']))
1473 return buildRedirectURL (__FUNCTION__, 'ERR');
1474
1475 // Make sure the file can be uploaded
1476 if (get_cfg_var('file_uploads') != 1)
1477 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1478
1479 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1480 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1481 if ($error != '')
1482 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1483
1484 $file_id = lastInsertID();
1485 $error = commitLinkFile ($file_id, $_REQUEST['entity_type'], $_REQUEST['entity_id']);
1486 if ($error != '')
1487 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1488
1489 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
1490 }
1491
1492 function linkFileToEntity ()
1493 {
1494 assertUIntArg ('file_id', __FUNCTION__);
1495 $etype_by_pageno = array // this 1:1 mapping may change later
1496 (
1497 'ipv4net' => 'ipv4net',
1498 'ipv4rspool' => 'ipv4rspool',
1499 'ipv4vs' => 'ipv4vs',
1500 'object' => 'object',
1501 'rack' => 'rack',
1502 'user' => 'user',
1503 );
1504 global $page, $pageno;
1505 $entity_type = $etype_by_pageno[$pageno];
1506 $bypass_name = $page[$pageno]['bypass'];
1507 assertUIntArg ($bypass_name, __FUNCTION__);
1508
1509 $fi = getFileInfo ($_REQUEST['file_id']);
1510 if ($fi === NULL)
1511 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
1512 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
1513 if ($error != '')
1514 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
1515
1516 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
1517 }
1518
1519 function updateFile ()
1520 {
1521 assertUIntArg ('file_id', __FUNCTION__);
1522 assertStringArg ('name', __FUNCTION__);
1523 assertStringArg ('comment', __FUNCTION__, TRUE);
1524 $error = commitUpdateFile ($_REQUEST['file_id'], $_REQUEST['comment']);
1525 if ($error != '')
1526 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1527
1528 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1529 }
1530
1531 function unlinkFile ()
1532 {
1533 assertUIntArg ('link_id', __FUNCTION__);
1534 $error = commitUnlinkFile ($_REQUEST['link_id']);
1535
1536 if ($error != '')
1537 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1538
1539 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1540 }
1541
1542 function deleteFile ()
1543 {
1544 assertUIntArg ('file_id', __FUNCTION__);
1545 $error = commitDeleteFile ($_REQUEST['file_id']);
1546
1547 if ($error != '')
1548 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1549
1550 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1551 }
1552
1553 ?>