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