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