r2430 Add multiple objects are refactored to post-regirect-get pattern
[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 // This function find differences in users's submit and PortCompat table
489 // and modifies database accordingly.
490 function savePortMap ()
491 {
492 $ptlist = getPortTypes();
493 $oldCompat = getPortCompat();
494 $newCompat = array();
495 foreach (array_keys ($ptlist) as $leftKey)
496 foreach (array_keys ($ptlist) as $rightKey)
497 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
498 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
499 // Build the new matrix from user's submit and compare it to
500 // the old matrix. Those pairs which appear on
501 // new matrix only, have to be stored in PortCompat table. Those who appear
502 // on the old matrix only, should be removed from PortCompat table.
503 // Those present in both matrices should be skipped.
504 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
505 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
506 $error_count = $success_count = 0;
507 foreach (array_keys ($ptlist) as $type1)
508 foreach (array_keys ($ptlist) as $type2)
509 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
510 switch ($oldCompatTable[$type1][$type2])
511 {
512 case TRUE: // new value is FALSE
513 if (removePortCompat ($type1, $type2) === TRUE)
514 $success_count++;
515 else
516 $error_count++;
517 break;
518 case FALSE: // new value is TRUE
519 if (addPortCompat ($type1, $type2) === TRUE)
520 $success_count++;
521 else
522 $error_count++;
523 break;
524 default:
525 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
526 break;
527 }
528 if ($error_count == 0)
529 return buildRedirectURL (__FUNCTION__, 'OK', array ($error_count, $success_count));
530 else
531 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error_count, $success_count));
532 }
533
534 function updateDictionary ()
535 {
536 assertUIntArg ('chapter_no', __FUNCTION__);
537 assertUIntArg ('dict_key', __FUNCTION__);
538 assertStringArg ('dict_value', __FUNCTION__);
539 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
540 return buildRedirectURL (__FUNCTION__, 'OK');
541 else
542 return buildRedirectURL (__FUNCTION__, 'ERR');
543 }
544
545 function supplementDictionary ()
546 {
547 assertUIntArg ('chapter_no', __FUNCTION__);
548 assertStringArg ('dict_value', __FUNCTION__);
549 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
550 return buildRedirectURL (__FUNCTION__, 'OK');
551 else
552 return buildRedirectURL (__FUNCTION__, 'ERR');
553 }
554
555 function reduceDictionary ()
556 {
557 assertUIntArg ('chapter_no', __FUNCTION__);
558 assertUIntArg ('dict_key', __FUNCTION__);
559 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
560 return buildRedirectURL (__FUNCTION__, 'OK');
561 else
562 return buildRedirectURL (__FUNCTION__, 'ERR');
563 }
564
565 function addChapter ()
566 {
567 assertStringArg ('chapter_name', __FUNCTION__);
568 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
569 return buildRedirectURL (__FUNCTION__, 'OK');
570 else
571 return buildRedirectURL (__FUNCTION__, 'ERR');
572 }
573
574 function updateChapter ()
575 {
576 assertUIntArg ('chapter_no', __FUNCTION__);
577 assertStringArg ('chapter_name', __FUNCTION__);
578 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
579 return buildRedirectURL (__FUNCTION__, 'OK');
580 else
581 return buildRedirectURL (__FUNCTION__, 'ERR');
582 }
583
584 function delChapter ()
585 {
586 assertUIntArg ('chapter_no', __FUNCTION__);
587 if (commitDeleteChapter ($_REQUEST['chapter_no']))
588 return buildRedirectURL (__FUNCTION__, 'OK');
589 else
590 return buildRedirectURL (__FUNCTION__, 'ERR');
591 }
592
593 function changeAttribute ()
594 {
595 assertUIntArg ('attr_id', __FUNCTION__);
596 assertStringArg ('attr_name', __FUNCTION__);
597 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
598 return buildRedirectURL (__FUNCTION__, 'OK');
599 else
600 return buildRedirectURL (__FUNCTION__, 'ERR');
601 }
602
603 function createAttribute ()
604 {
605 assertStringArg ('attr_name', __FUNCTION__);
606 assertStringArg ('attr_type', __FUNCTION__);
607 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
608 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
609 else
610 return buildRedirectURL (__FUNCTION__, 'ERR');
611 }
612
613 function deleteAttribute ()
614 {
615 assertUIntArg ('attr_id', __FUNCTION__);
616 if (commitDeleteAttribute ($_REQUEST['attr_id']))
617 return buildRedirectURL (__FUNCTION__, 'OK');
618 else
619 return buildRedirectURL (__FUNCTION__, 'ERR');
620 }
621
622 function supplementAttrMap ()
623 {
624 assertUIntArg ('attr_id', __FUNCTION__);
625 assertUIntArg ('objtype_id', __FUNCTION__);
626 assertUIntArg ('chapter_no', __FUNCTION__);
627 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
628 return buildRedirectURL (__FUNCTION__, 'OK');
629 else
630 return buildRedirectURL (__FUNCTION__, 'ERR');
631 }
632
633 function reduceAttrMap ()
634 {
635 assertUIntArg ('attr_id', __FUNCTION__);
636 assertUIntArg ('objtype_id', __FUNCTION__);
637 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
638 return buildRedirectURL (__FUNCTION__, 'OK');
639 else
640 return buildRedirectURL (__FUNCTION__, 'ERR');
641 }
642
643 function clearSticker ()
644 {
645 assertUIntArg ('attr_id', __FUNCTION__);
646 assertUIntArg ('object_id', __FUNCTION__);
647 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
648 return buildRedirectURL (__FUNCTION__, 'OK');
649 else
650 return buildRedirectURL (__FUNCTION__, 'ERR');
651 }
652
653 function updateObjectAllocation ()
654 {
655 assertUIntArg ('object_id', __FUNCTION__);
656
657 $is_submit = isset ($_REQUEST['got_atoms']);
658 $is_update = isset ($_REQUEST['rackmulti'][0]);
659
660 if ($is_submit)
661 {
662 $object_id = $_REQUEST['object_id'];
663 $workingRacksData = getResidentRacksData ($object_id);
664 if ($workingRacksData === NULL)
665 {
666 print_r ($workingRacksData);
667 showError ('getResidentRacksData() failed', __FUNCTION__);
668 return;
669 }
670 foreach ($_REQUEST['rackmulti'] as $cand_id)
671 {
672 if (!isset ($workingRacksData[$cand_id]))
673 {
674 $rackData = getRackData ($cand_id);
675 if ($rackData == NULL)
676 {
677 showError ('getRackData() failed', __FUNCTION__);
678 return;
679 }
680 $workingRacksData[$cand_id] = $rackData;
681 }
682 }
683 foreach ($workingRacksData as &$rackData)
684 applyObjectMountMask ($rackData, $object_id);
685
686 $oldMolecule = getMoleculeForObject ($object_id);
687 $worldchanged = FALSE;
688 $log = array();
689 foreach ($workingRacksData as $rack_id => $rackData)
690 {
691 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
692 $log[] = $logrecord;
693 }
694 return buildWideRedirectURL($log);
695
696 }
697 else
698 {
699 unset($_REQUEST['page']);
700 unset($_REQUEST['tab']);
701 unset($_REQUEST['op']);
702 return buildWideRedirectURL(array(), NULL, NULL, $_REQUEST);
703 }
704
705 }
706
707
708 function updateObject ()
709 {
710 assertUIntArg ('num_attrs', __FUNCTION__);
711 assertUIntArg ('object_id', __FUNCTION__);
712 assertUIntArg ('object_type_id', __FUNCTION__);
713 assertStringArg ('object_name', __FUNCTION__, TRUE);
714 assertStringArg ('object_label', __FUNCTION__, TRUE);
715 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
716 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
717 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
718 $has_problems = 'yes';
719 else
720 $has_problems = 'no';
721
722 if (commitUpdateObject (
723 $_REQUEST['object_id'],
724 $_REQUEST['object_name'],
725 $_REQUEST['object_label'],
726 $_REQUEST['object_barcode'],
727 $_REQUEST['object_type_id'],
728 $has_problems,
729 $_REQUEST['object_asset_no'],
730 $_REQUEST['object_comment']
731 ) !== TRUE)
732 return buildRedirectURL (__FUNCTION__, 'ERR');
733
734 // Update optional attributes
735 $oldvalues = getAttrValues ($_REQUEST['object_id']);
736 $result = array();
737 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
738 {
739 assertUIntArg ("${i}_attr_id", __FUNCTION__);
740 $attr_id = $_REQUEST["${i}_attr_id"];
741
742 // Field is empty, delete attribute and move on.
743 if (empty($_REQUEST["${i}_value"]))
744 {
745 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
746 continue;
747 }
748
749 // The value could be uint/float, but we don't know ATM. Let SQL
750 // server check this and complain.
751 assertStringArg ("${i}_value", __FUNCTION__);
752 $value = $_REQUEST["${i}_value"];
753 switch ($oldvalues[$attr_id]['type'])
754 {
755 case 'uint':
756 case 'float':
757 case 'string':
758 $oldvalue = $oldvalues[$attr_id]['value'];
759 break;
760 case 'dict':
761 $oldvalue = $oldvalues[$attr_id]['key'];
762 break;
763 default:
764 showError ('Internal structure error', __FUNCTION__);
765 die;
766 }
767 if ($value == $oldvalue)
768 continue;
769
770 // Note if the queries succeed or not, it determines which page they see.
771 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
772 }
773 if (in_array (FALSE, $result))
774 return buildRedirectURL (__FUNCTION__, 'ERR');
775
776 // Invalidate thumb cache of all racks objects could occupy.
777 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
778 resetThumbCache ($rack_id);
779
780 return buildRedirectURL (__FUNCTION__, 'OK');
781 }
782
783
784 function addMultipleObjects()
785 {
786
787 $log = array();
788 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
789 $keepvalues1 = TRUE;
790 $max = getConfigVar ('MASSCOUNT');
791 for ($i = 0; $i < $max; $i++)
792 {
793 if (!isset ($_REQUEST["${i}_object_type_id"]))
794 {
795 $log[] = array ('code' => 'error', 'message' => "Submitted form is invalid at line " . $i + 1);
796 break;
797 }
798 assertUIntArg ("${i}_object_type_id", __FUNCTION__, TRUE);
799 assertStringArg ("${i}_object_name", __FUNCTION__, TRUE);
800 assertStringArg ("${i}_object_label", __FUNCTION__, TRUE);
801 assertStringArg ("${i}_object_asset_no", __FUNCTION__, TRUE);
802 assertStringArg ("${i}_object_barcode", __FUNCTION__, TRUE);
803 $type_id[$i] = $_REQUEST["${i}_object_type_id"];
804 // Save user input for possible rendering.
805 $name[$i] = $_REQUEST["${i}_object_name"];
806 $label[$i] = $_REQUEST["${i}_object_label"];
807 $asset_no[$i] = $_REQUEST["${i}_object_asset_no"];
808 $barcode[$i] = $_REQUEST["${i}_object_barcode"];
809
810 // It's better to skip silently, than to print a notice.
811 if ($type_id[$i] == 0)
812 continue;
813 if (commitAddObject ($name[$i], $label[$i], $barcode[$i], $type_id[$i], $asset_no[$i], $taglist) === TRUE)
814 $log[] = array ('code' => 'success', 'message' => "Added new object '${name[$i]}'");
815 else
816 $log[] = array ('code' => 'error', 'message' => __FUNCTION__ . ': commitAddObject() failed');
817 }
818
819 return buildWideRedirectURL($log);
820 }
821
822 function addLotOfObjects()
823 {
824 $log = array();
825 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
826 $keepvalues2 = TRUE;
827 assertUIntArg ('global_type_id', __FUNCTION__, TRUE);
828 assertStringArg ('namelist', __FUNCTION__, TRUE);
829 $global_type_id = $_REQUEST['global_type_id'];
830 if ($global_type_id == 0)
831 {
832 if (!empty ($_REQUEST['namelist']))
833 $log[] = array ('code' => 'error', 'message' => 'Object type is not selected, check the form below');
834 else
835 $log[] = array ('code' => 'error', 'message' => 'Empty form has been ignored. Cheers.');
836 }
837 else
838 {
839 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
840 $names1 = explode ('\n', $_REQUEST['namelist']);
841 $names2 = array();
842 foreach ($names1 as $line)
843 {
844 $parts = explode ('\r', $line);
845 reset ($parts);
846 if (empty ($parts[0]))
847 continue;
848 else
849 $names2[] = rtrim ($parts[0]);
850 }
851 foreach ($names2 as $cname)
852 if (commitAddObject ($cname, '', '', $global_type_id, '', $taglist) === TRUE)
853 $log[] = array ('code' => 'success', 'message' => "Added new object '${cname}'");
854 else
855 $log[] = array ('code' => 'error', 'message' => "Could not add '${cname}'");
856 }
857 return buildWideRedirectURL($log);
858 }
859
860
861 function deleteObject ()
862 {
863 assertUIntArg ('object_id', __FUNCTION__);
864 $error = commitDeleteObject ($_REQUEST['object_id']);
865
866 if ($error != '')
867 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
868
869 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
870 }
871
872 function useupPort ()
873 {
874 assertUIntArg ('port_id', __FUNCTION__);
875 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
876 return buildRedirectURL (__FUNCTION__, 'OK');
877 else
878 return buildRedirectURL (__FUNCTION__, 'ERR');
879 }
880
881 function updateUI ()
882 {
883 assertUIntArg ('num_vars', __FUNCTION__);
884 $error = '';
885
886 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
887 {
888 assertStringArg ("${i}_varname", __FUNCTION__);
889 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
890 $varname = $_REQUEST["${i}_varname"];
891 $varvalue = $_REQUEST["${i}_varvalue"];
892
893 // If form value = value in DB, don't bother updating DB
894 if ($varvalue == getConfigVar ($varname))
895 continue;
896
897 // Note if the queries succeed or not, it determines which page they see.
898 $error = setConfigVar ($varname, $varvalue, TRUE);
899 if (!empty ($error))
900 break;
901 }
902
903 if ($error != '')
904 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
905
906 return buildRedirectURL (__FUNCTION__, 'OK');
907 }
908
909 function resetUIConfig()
910 {
911 setConfigVar ('default_port_type','24');
912 setConfigVar ('MASSCOUNT','15');
913 setConfigVar ('MAXSELSIZE','30');
914 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
915 setConfigVar ('ROW_SCALE','2');
916 setConfigVar ('PORTS_PER_ROW','12');
917 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
918 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
919 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
920 setConfigVar ('DEFAULT_SLB_VS_PORT','');
921 setConfigVar ('DEFAULT_SLB_RS_PORT','');
922 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
923 setConfigVar ('NATV4_PERFORMERS','4,7,8');
924 setConfigVar ('DETECT_URLS','no');
925 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
926 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
927 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
928 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
929 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
930 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
931 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
932 setConfigVar ('IPV4_AUTO_RELEASE','1');
933 setConfigVar ('SHOW_LAST_TAB', 'no');
934 setConfigVar ('COOKIE_TTL', '1209600');
935 setConfigVar ('EXT_IPV4_VIEW', 'yes');
936 setConfigVar ('TREE_THRESHOLD', '25');
937 setConfigVar ('IPV4_JAYWALK', 'no');
938 setConfigVar ('ADDNEW_AT_TOP', 'yes');
939 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
940 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
941 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
942 setConfigVar ('PREVIEW_TEXT_COLS', '80');
943 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
944 return buildRedirectURL (__FUNCTION__, 'OK');
945 }
946
947 // Add single record.
948 function addRealServer ()
949 {
950 assertUIntArg ('pool_id', __FUNCTION__);
951 assertIPv4Arg ('remoteip', __FUNCTION__);
952 assertStringArg ('rsport', __FUNCTION__, TRUE);
953 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
954 if (!addRStoRSPool (
955 $_REQUEST['pool_id'],
956 $_REQUEST['remoteip'],
957 $_REQUEST['rsport'],
958 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
959 $_REQUEST['rsconfig']
960 ))
961 return buildRedirectURL (__FUNCTION__, 'ERR');
962 else
963 return buildRedirectURL (__FUNCTION__, 'OK');
964 }
965
966 // Parse textarea submitted and try adding a real server for each line.
967 function addRealServers ()
968 {
969 assertUIntArg ('pool_id', __FUNCTION__);
970 assertStringArg ('format', __FUNCTION__);
971 assertStringArg ('rawtext', __FUNCTION__);
972 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
973 $ngood = $nbad = 0;
974 $rsconfig = '';
975 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
976 foreach (explode ('\n', $rawtext) as $line)
977 {
978 if (empty ($line))
979 continue;
980 $match = array ();
981 switch ($_REQUEST['format'])
982 {
983 case 'ipvs_2': // address and port only
984 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
985 continue;
986 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
987 $ngood++;
988 else
989 $nbad++;
990 break;
991 case 'ipvs_3': // address, port and weight
992 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
993 continue;
994 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
995 $ngood++;
996 else
997 $nbad++;
998 break;
999 case 'ssv_2': // IP address and port
1000 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1001 continue;
1002 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1003 $ngood++;
1004 else
1005 $nbad++;
1006 break;
1007 case 'ssv_1': // IP address
1008 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1009 continue;
1010 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1011 $ngood++;
1012 else
1013 $nbad++;
1014 break;
1015 default:
1016 return buildRedirectURL (__FUNCTION__, 'ERR1');
1017 break;
1018 }
1019 }
1020 if ($nbad == 0 and $ngood > 0)
1021 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1022 else
1023 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
1024 }
1025
1026 function addVService ()
1027 {
1028 assertIPv4Arg ('vip', __FUNCTION__);
1029 assertUIntArg ('vport', __FUNCTION__);
1030 assertStringArg ('proto', __FUNCTION__);
1031 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
1032 return buildRedirectURL (__FUNCTION__, 'ERR1');
1033 assertStringArg ('name', __FUNCTION__, TRUE);
1034 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1035 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1036 $error = commitCreateVS
1037 (
1038 $_REQUEST['vip'],
1039 $_REQUEST['vport'],
1040 $_REQUEST['proto'],
1041 $_REQUEST['name'],
1042 $_REQUEST['vsconfig'],
1043 $_REQUEST['rsconfig'],
1044 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1045 );
1046 if ($error != '')
1047 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
1048 else
1049 return buildRedirectURL (__FUNCTION__, 'OK');
1050 }
1051
1052 function deleteRealServer ()
1053 {
1054 assertUIntArg ('id', __FUNCTION__);
1055 if (!commitDeleteRS ($_REQUEST['id']))
1056 return buildRedirectURL (__FUNCTION__, 'ERR');
1057 else
1058 return buildRedirectURL (__FUNCTION__, 'OK');
1059 }
1060
1061 function deleteLoadBalancer ()
1062 {
1063 assertUIntArg ('object_id', __FUNCTION__);
1064 assertUIntArg ('pool_id', __FUNCTION__);
1065 assertUIntArg ('vs_id', __FUNCTION__);
1066 if (!commitDeleteLB (
1067 $_REQUEST['object_id'],
1068 $_REQUEST['pool_id'],
1069 $_REQUEST['vs_id']
1070 ))
1071 return buildRedirectURL (__FUNCTION__, 'ERR');
1072 else
1073 return buildRedirectURL (__FUNCTION__, 'OK');
1074 }
1075
1076 function deleteVService ()
1077 {
1078 assertUIntArg ('vs_id', __FUNCTION__);
1079 if (!commitDeleteVS ($_REQUEST['vs_id']))
1080 return buildRedirectURL (__FUNCTION__, 'ERR');
1081 else
1082 return buildRedirectURL (__FUNCTION__, 'OK');
1083 }
1084
1085 function updateRealServer ()
1086 {
1087 assertUIntArg ('rs_id', __FUNCTION__);
1088 assertIPv4Arg ('rsip', __FUNCTION__);
1089 assertStringArg ('rsport', __FUNCTION__, TRUE);
1090 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1091 if (!commitUpdateRS (
1092 $_REQUEST['rs_id'],
1093 $_REQUEST['rsip'],
1094 $_REQUEST['rsport'],
1095 $_REQUEST['rsconfig']
1096 ))
1097 return buildRedirectURL (__FUNCTION__, 'ERR');
1098 else
1099 return buildRedirectURL (__FUNCTION__, 'OK');
1100 }
1101
1102 function updateLoadBalancer ()
1103 {
1104 assertUIntArg ('object_id', __FUNCTION__);
1105 assertUIntArg ('pool_id', __FUNCTION__);
1106 assertUIntArg ('vs_id', __FUNCTION__);
1107 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1108 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1109 if (!commitUpdateLB (
1110 $_REQUEST['object_id'],
1111 $_REQUEST['pool_id'],
1112 $_REQUEST['vs_id'],
1113 $_REQUEST['vsconfig'],
1114 $_REQUEST['rsconfig']
1115 ))
1116 return buildRedirectURL (__FUNCTION__, 'ERR');
1117 else
1118 return buildRedirectURL (__FUNCTION__, 'OK');
1119 }
1120
1121 function updateVService ()
1122 {
1123 assertUIntArg ('vs_id', __FUNCTION__);
1124 assertIPv4Arg ('vip', __FUNCTION__);
1125 assertUIntArg ('vport', __FUNCTION__);
1126 assertStringArg ('proto', __FUNCTION__);
1127 assertStringArg ('name', __FUNCTION__, TRUE);
1128 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1129 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1130 if (!commitUpdateVS (
1131 $_REQUEST['vs_id'],
1132 $_REQUEST['vip'],
1133 $_REQUEST['vport'],
1134 $_REQUEST['proto'],
1135 $_REQUEST['name'],
1136 $_REQUEST['vsconfig'],
1137 $_REQUEST['rsconfig']
1138 ))
1139 return buildRedirectURL (__FUNCTION__, 'ERR');
1140 else
1141 return buildRedirectURL (__FUNCTION__, 'OK');
1142 }
1143
1144 function addLoadBalancer ()
1145 {
1146 assertUIntArg ('pool_id', __FUNCTION__);
1147 assertUIntArg ('object_id', __FUNCTION__);
1148 assertUIntArg ('vs_id', __FUNCTION__);
1149 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1150 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1151 if (!addLBtoRSPool (
1152 $_REQUEST['pool_id'],
1153 $_REQUEST['object_id'],
1154 $_REQUEST['vs_id'],
1155 $_REQUEST['vsconfig'],
1156 $_REQUEST['rsconfig']
1157 ))
1158 return buildRedirectURL (__FUNCTION__, 'ERR');
1159 else
1160 return buildRedirectURL (__FUNCTION__, 'OK');
1161 }
1162
1163 function addRSPool ()
1164 {
1165 assertStringArg ('name', __FUNCTION__, TRUE);
1166 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1167 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1168 $error = commitCreateRSPool
1169 (
1170 $_REQUEST['name'],
1171 $_REQUEST['vsconfig'],
1172 $_REQUEST['rsconfig'],
1173 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1174 );
1175 if ($error != '')
1176 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1177 else
1178 return buildRedirectURL (__FUNCTION__, 'OK');
1179 }
1180
1181 function deleteRSPool ()
1182 {
1183 assertUIntArg ('pool_id', __FUNCTION__);
1184 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1185 return buildRedirectURL (__FUNCTION__, 'ERR');
1186 else
1187 return buildRedirectURL (__FUNCTION__, 'OK');
1188 }
1189
1190 function updateRSPool ()
1191 {
1192 assertUIntArg ('pool_id', __FUNCTION__);
1193 assertStringArg ('name', __FUNCTION__, TRUE);
1194 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1195 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1196 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1197 return buildRedirectURL (__FUNCTION__, 'ERR');
1198 else
1199 return buildRedirectURL (__FUNCTION__, 'OK');
1200 }
1201
1202 function updateRSInService ()
1203 {
1204 assertUIntArg ('rscount', __FUNCTION__);
1205 $pool_id = $_REQUEST['pool_id'];
1206 $orig = getRSPoolInfo ($pool_id);
1207 $nbad = $ngood = 0;
1208 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1209 {
1210 $rs_id = $_REQUEST["rsid_${i}"];
1211 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1212 $newval = 'yes';
1213 else
1214 $newval = 'no';
1215 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1216 {
1217 if (commitSetInService ($rs_id, $newval))
1218 $ngood++;
1219 else
1220 $nbad++;
1221 }
1222 }
1223 if (!$nbad)
1224 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1225 else
1226 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1227 }
1228
1229 // FIXME: check, that each submitted address belongs to the prefix we
1230 // are operating on.
1231 function importPTRData ()
1232 {
1233 assertUIntArg ('addrcount', __FUNCTION__);
1234 $nbad = $ngood = 0;
1235 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1236 {
1237 $inputname = "import_${i}";
1238 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1239 continue;
1240 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1241 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1242 assertStringArg ("rsvd_${i}", __FUNCTION__);
1243 // Non-existent addresses will not have this argument set in request.
1244 $rsvd = 'no';
1245 if ($_REQUEST["rsvd_${i}"] == 'yes')
1246 $rsvd = 'yes';
1247 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1248 $ngood++;
1249 else
1250 $nbad++;
1251 }
1252 if (!$nbad)
1253 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1254 else
1255 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1256 }
1257
1258 function generateAutoPorts ()
1259 {
1260 global $pageno;
1261 assertUIntArg ('object_id', __FUNCTION__);
1262 $info = getObjectInfo ($_REQUEST['object_id']);
1263 // Navigate away in case of success, stay at the place otherwise.
1264 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
1265 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
1266 else
1267 return buildRedirectURL (__FUNCTION__, 'ERR');
1268 }
1269
1270 // Filter out implicit tags before storing the new tag set.
1271 function saveEntityTags ()
1272 {
1273 global $explicit_tags, $implicit_tags, $page, $pageno, $etype_by_pageno;
1274 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1275 {
1276 showError ('Internal error', __FUNCTION__);
1277 die;
1278 }
1279 $realm = $etype_by_pageno[$pageno];
1280 $bypass = $page[$pageno]['bypass'];
1281 assertUIntArg ($bypass, __FUNCTION__);
1282 $entity_id = $_REQUEST[$bypass];
1283 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1284 // Build a chain from the submitted data, minimize it,
1285 // then wipe existing records and store the new set instead.
1286 destroyTagsForEntity ($realm, $entity_id);
1287 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
1288 $n_succeeds = $n_errors = 0;
1289 foreach ($newchain as $taginfo)
1290 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
1291 $n_succeeds++;
1292 else
1293 $n_errors++;
1294 if ($n_errors)
1295 return buildRedirectURL (__FUNCTION__, 'ERR', array ($n_succeeds, $n_errors));
1296 else
1297 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
1298 }
1299
1300 function destroyTag ()
1301 {
1302 assertUIntArg ('tag_id', __FUNCTION__);
1303 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
1304 return buildRedirectURL (__FUNCTION__, 'OK');
1305 else
1306 return buildRedirectURL (__FUNCTION__, 'ERR');
1307 }
1308
1309 function createTag ()
1310 {
1311 assertStringArg ('tag_name', __FUNCTION__);
1312 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1313 $tagname = trim ($_REQUEST['tag_name']);
1314 if (!validTagName ($tagname))
1315 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
1316 if (tagExistsInDatabase ($tagname))
1317 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname));
1318 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1319 $parent_id = 'NULL';
1320 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1321 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
1322 else
1323 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
1324 }
1325
1326 function updateTag ()
1327 {
1328 assertUIntArg ('tag_id', __FUNCTION__);
1329 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1330 assertStringArg ('tag_name', __FUNCTION__);
1331 $tagname = trim ($_REQUEST['tag_name']);
1332 if (!validTagName ($tagname))
1333 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
1334 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1335 $parent_id = 'NULL';
1336 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1337 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
1338 else
1339 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname, $ret));
1340 }
1341
1342 function rollTags ()
1343 {
1344 assertUIntArg ('row_id', __FUNCTION__);
1345 assertStringArg ('sum', __FUNCTION__, TRUE);
1346 assertUIntArg ('realsum', __FUNCTION__);
1347 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1348 return buildRedirectURL (__FUNCTION__, 'ERR');
1349 // Even if the user requested an empty tag list, don't bail out, but process existing
1350 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1351 // have its chains refined to "normal" form.
1352 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1353 $n_ok = 0;
1354 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1355 // filter out the same tag again and again. It will have own noise to cancel.
1356 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
1357 foreach (getRacksForRow ($_REQUEST['row_id']) as $rackInfo)
1358 {
1359 if (rebuildTagChainForEntity ('rack', $rackInfo['id'], $extrachain))
1360 $n_ok++;
1361 foreach (stuffInRackspace (getRackData ($rackInfo['id'])) as $object_id)
1362 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1363 $n_ok++;
1364 }
1365 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
1366 }
1367
1368 function changeMyPassword ()
1369 {
1370 global $accounts, $remote_username, $user_auth_src;
1371 if ($user_auth_src != 'database')
1372 return buildRedirectURL (__FUNCTION__, 'ERR1');
1373 assertStringArg ('oldpassword', __FUNCTION__);
1374 assertStringArg ('newpassword1', __FUNCTION__);
1375 assertStringArg ('newpassword2', __FUNCTION__);
1376 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
1377 return buildRedirectURL (__FUNCTION__, 'ERR2');
1378 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
1379 return buildRedirectURL (__FUNCTION__, 'ERR3');
1380 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
1381 return buildRedirectURL (__FUNCTION__, 'OK');
1382 else
1383 return buildRedirectURL (__FUNCTION__, 'ERR4');
1384 }
1385
1386 function saveRackCode ()
1387 {
1388 assertStringArg ('rackcode');
1389 // For the test to succeed, unescape LFs, strip CRs.
1390 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
1391 $parseTree = getRackCode ($newcode);
1392 if ($parseTree['result'] != 'ACK')
1393 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
1394 saveScript ('RackCodeCache', '');
1395 if (saveScript ('RackCode', $newcode))
1396 return buildRedirectURL (__FUNCTION__, 'OK');
1397 else
1398 return buildRedirectURL (__FUNCTION__, 'ERR2');
1399 }
1400
1401 // This handler's context is pre-built, but not authorized. It is assumed, that the
1402 // handler will take existing context and before each commit check authorization
1403 // on the base chain plus necessary context added.
1404 function setPortVLAN ()
1405 {
1406 assertUIntArg ('portcount', __FUNCTION__);
1407 $data = getSwitchVLANs ($_REQUEST['object_id']);
1408 if ($data === NULL)
1409 return buildRedirectURL (__FUNCTION__, 'ERR1');
1410 list ($vlanlist, $portlist) = $data;
1411 // Here we just build up 1 set command for the gateway with all of the ports
1412 // included. The gateway is expected to filter unnecessary changes silently
1413 // and to provide a list of responses with either error or success message
1414 // for each of the rest.
1415 $nports = $_REQUEST['portcount'];
1416 $prefix = 'set ';
1417 $log = array ('v' => 2, 'm' => array());
1418 $setcmd = '';
1419 for ($i = 0; $i < $nports; $i++)
1420 if
1421 (
1422 !isset ($_REQUEST['portname_' . $i]) ||
1423 !isset ($_REQUEST['vlanid_' . $i]) ||
1424 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1425 )
1426 $log['m'][] = array ('c' => 158, 'a' => array ($i));
1427 elseif
1428 (
1429 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1430 $portlist[$i]['vlaind'] == 'TRUNK'
1431 )
1432 continue;
1433 else
1434 {
1435 $portname = $_REQUEST['portname_' . $i];
1436 $oldvlanid = $portlist[$i]['vlanid'];
1437 $newvlanid = $_REQUEST['vlanid_' . $i];
1438 // Finish the security context and evaluate it.
1439 $annex = array();
1440 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1441 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1442 if (!permitted (NULL, NULL, NULL, $annex))
1443 {
1444 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
1445 continue;
1446 }
1447 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1448 $prefix = ';';
1449 }
1450 // Feed the gateway and interpret its (non)response.
1451 if ($setcmd != '')
1452 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1453 else
1454 $log['m'][] = array ('c' => 201);
1455 return buildWideRedirectURL ($log);
1456 }
1457
1458 function submitSLBConfig ()
1459 {
1460 assertUIntArg ('object_id', __FUNCTION__);
1461 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
1462 $msglog = gwSendFile ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
1463 return buildWideRedirectURL ($msglog);
1464 }
1465
1466 function addRow ()
1467 {
1468 assertStringArg ('name', __FUNCTION__);
1469
1470 if (commitAddRow ($_REQUEST['name']) === TRUE)
1471 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1472 else
1473 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
1474 }
1475
1476 function updateRow ()
1477 {
1478 assertUIntArg ('row_id', __FUNCTION__);
1479 assertStringArg ('name', __FUNCTION__);
1480
1481 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
1482 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1483 else
1484 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
1485 }
1486
1487 function deleteRow ()
1488 {
1489 assertUIntArg ('row_id', __FUNCTION__);
1490
1491 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
1492 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1493 else
1494 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
1495 }
1496
1497
1498
1499 function addRack ()
1500 {
1501 assertUIntArg ('row_id', __FUNCTION__);
1502 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1503 if (isset ($_REQUEST['got_data']))
1504 {
1505 assertStringArg ('rack_name', __FUNCTION__);
1506 assertUIntArg ('rack_height1', __FUNCTION__);
1507 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1508
1509 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
1510 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
1511 else
1512 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
1513 }
1514 elseif (isset ($_REQUEST['got_mdata']))
1515 {
1516 assertUIntArg ('rack_height2', __FUNCTION__);
1517 assertStringArg ('rack_names', __FUNCTION__, TRUE);
1518 $log = array ('v' => 2);
1519 // copy-and-paste from renderAddMultipleObjectsForm()
1520 $names1 = explode ('\n', $_REQUEST['rack_names']);
1521 $names2 = array();
1522 foreach ($names1 as $line)
1523 {
1524 $parts = explode ('\r', $line);
1525 reset ($parts);
1526 if (empty ($parts[0]))
1527 continue;
1528 else
1529 $names2[] = rtrim ($parts[0]);
1530 }
1531 foreach ($names2 as $cname)
1532 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
1533 $log['m'][] = array ('c' => getMessageCode (__FUNCTION__, 'OK'), 'a' => array ($cname));
1534 else
1535 $log['m'][] = array ('c' => getMessageCode (__FUNCTION__, 'ERR1'), 'a' => array ($cname));
1536 return buildWideRedirectURL ($log);
1537 }
1538 else
1539 return buildRedirectURL (__FUNCTION__, 'ERR2');
1540 }
1541
1542 function updateRack ()
1543 {
1544 assertUIntArg ('rack_id', __FUNCTION__);
1545 assertUIntArg ('rack_row_id', __FUNCTION__);
1546 assertUIntArg ('rack_height', __FUNCTION__);
1547 assertStringArg ('rack_name', __FUNCTION__);
1548 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1549
1550 resetThumbCache ($_REQUEST['rack_id']);
1551 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
1552 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
1553 else
1554 return buildRedirectURL (__FUNCTION__, 'ERR');
1555 }
1556
1557 function updateRackDesign ()
1558 {
1559 assertUIntArg ('rack_id', __FUNCTION__);
1560 if (($rackData = getRackData ($_REQUEST['rack_id'])) == NULL)
1561 {
1562 showError ('getRackData() failed', __FUNCTION__);
1563 return;
1564 }
1565 applyRackDesignMask($rackData);
1566 markupObjectProblems ($rackData);
1567 $response = processGridForm ($rackData, 'A', 'F');
1568 return buildWideRedirectURL (array($response));
1569 }
1570
1571 function updateRackProblems ()
1572 {
1573 assertUIntArg ('rack_id', __FUNCTION__);
1574 if (($rackData = getRackData ($_REQUEST['rack_id'])) == NULL)
1575 {
1576 showError ('getRackData() failed', __FUNCTION__);
1577 return;
1578 }
1579 applyRackProblemMask($rackData);
1580 markupObjectProblems ($rackData);
1581 $response = processGridForm ($rackData, 'F', 'U');
1582 return buildWideRedirectURL (array($response));
1583 }
1584
1585
1586
1587 function querySNMPData ()
1588 {
1589 assertUIntArg ('object_id', __FUNCTION__);
1590 assertStringArg ('community', __FUNCTION__);
1591 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
1592 }
1593
1594 // File-related functions
1595 function addFileWithoutLink ()
1596 {
1597 assertStringArg ('comment', __FUNCTION__, TRUE);
1598
1599 // Make sure the file can be uploaded
1600 if (get_cfg_var('file_uploads') != 1)
1601 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1602
1603 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1604 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1605
1606 if ($error != '')
1607 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1608
1609 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
1610 }
1611
1612 function addFileToEntity ()
1613 {
1614 assertStringArg ('entity_type', __FUNCTION__);
1615 assertUIntArg ('entity_id', __FUNCTION__);
1616 assertStringArg ('comment', __FUNCTION__, TRUE);
1617 if (empty ($_REQUEST['entity_type']) || empty ($_REQUEST['entity_id']))
1618 return buildRedirectURL (__FUNCTION__, 'ERR');
1619
1620 // Make sure the file can be uploaded
1621 if (get_cfg_var('file_uploads') != 1)
1622 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1623
1624 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1625 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1626 if ($error != '')
1627 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1628
1629 $file_id = lastInsertID();
1630 $error = commitLinkFile ($file_id, $_REQUEST['entity_type'], $_REQUEST['entity_id']);
1631 if ($error != '')
1632 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1633
1634 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
1635 }
1636
1637 function linkFileToEntity ()
1638 {
1639 assertUIntArg ('file_id', __FUNCTION__);
1640 global $page, $pageno, $etype_by_pageno;
1641 $entity_type = $etype_by_pageno[$pageno];
1642 $bypass_name = $page[$pageno]['bypass'];
1643 assertUIntArg ($bypass_name, __FUNCTION__);
1644
1645 $fi = getFileInfo ($_REQUEST['file_id']);
1646 if ($fi === NULL)
1647 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
1648 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
1649 if ($error != '')
1650 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
1651
1652 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
1653 }
1654
1655 function replaceFile ()
1656 {
1657 assertUIntArg ('file_id', __FUNCTION__);
1658
1659 // Make sure the file can be uploaded
1660 if (get_cfg_var('file_uploads') != 1)
1661 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1662
1663 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1664 $error = commitReplaceFile ($_REQUEST['file_id'], $_FILES['file']['size'], $fp);
1665 if ($error != '')
1666 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1667
1668 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1669 }
1670
1671 function updateFile ()
1672 {
1673 assertUIntArg ('file_id', __FUNCTION__);
1674 assertStringArg ('file_name', __FUNCTION__);
1675 assertStringArg ('file_type', __FUNCTION__);
1676 assertStringArg ('file_comment', __FUNCTION__, TRUE);
1677 $error = commitUpdateFile ($_REQUEST['file_id'], $_REQUEST['file_name'], $_REQUEST['file_type'], $_REQUEST['file_comment']);
1678 if ($error != '')
1679 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1680
1681 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1682 }
1683
1684 function unlinkFile ()
1685 {
1686 assertUIntArg ('link_id', __FUNCTION__);
1687 $error = commitUnlinkFile ($_REQUEST['link_id']);
1688
1689 if ($error != '')
1690 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1691
1692 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1693 }
1694
1695 function deleteFile ()
1696 {
1697 assertUIntArg ('file_id', __FUNCTION__);
1698 $error = commitDeleteFile ($_REQUEST['file_id']);
1699
1700 if ($error != '')
1701 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1702
1703 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1704 }
1705
1706 function updateFileText ()
1707 {
1708 assertUIntArg ('file_id', __FUNCTION__);
1709 assertStringArg ('file_text', __FUNCTION__, TRUE); // it's Ok to save empty
1710 $shortInfo = getFileInfo ($_REQUEST['file_id']);
1711 // Is this bytes or chars?
1712 $error = commitUpdateFileText ($_REQUEST['file_id'], $_REQUEST['file_text']);
1713 if ($error == '')
1714 return buildRedirectURL (__FUNCTION__, 'OK', array ($shortInfo['name']));
1715 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1716 }
1717
1718 ?>