r2390 - findTagWarnings(): employ getTagByName()
[racktables] / inc / ophandlers.php
CommitLineData
b325120a 1<?php
e673ee24
DO
2/*
3*
4* This file is a library of operation handlers for RackTables.
5*
6*/
7
17c32f4c
DO
8// This function assures that specified argument was passed
9// and is a number greater than zero.
2c6c7645 10function assertUIntArg ($argname, $caller = 'N/A', $allow_zero = FALSE)
17c32f4c
DO
11{
12 if (!isset ($_REQUEST[$argname]))
13 {
2c6c7645 14 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
15 die();
16 }
17 if (!is_numeric ($_REQUEST[$argname]))
18 {
2c6c7645 19 showError ("Parameter '${argname}' is not a number (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
20 die();
21 }
22 if ($_REQUEST[$argname] < 0)
23 {
2c6c7645 24 showError ("Parameter '${argname}' is less than zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
25 die();
26 }
27 if (!$allow_zero and $_REQUEST[$argname] == 0)
28 {
2c6c7645 29 showError ("Parameter '${argname}' is equal to zero (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
30 die();
31 }
32}
33
34// This function assures that specified argument was passed
35// and is a non-empty string.
2c6c7645 36function assertStringArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
37{
38 if (!isset ($_REQUEST[$argname]))
39 {
2c6c7645 40 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
41 die();
42 }
43 if (!is_string ($_REQUEST[$argname]))
44 {
2c6c7645 45 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
46 die();
47 }
48 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
49 {
2c6c7645 50 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
51 die();
52 }
53}
54
2c6c7645 55function assertBoolArg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c
DO
56{
57 if (!isset ($_REQUEST[$argname]))
58 {
2c6c7645 59 showError ("Parameter '${argname}' is missing (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
60 die();
61 }
62 if (!is_string ($_REQUEST[$argname]) or $_REQUEST[$argname] != 'on')
63 {
2c6c7645 64 showError ("Parameter '${argname}' is not a string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
65 die();
66 }
67 if (!$ok_if_empty and empty ($_REQUEST[$argname]))
68 {
2c6c7645 69 showError ("Parameter '${argname}' is an empty string (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
70 die();
71 }
72}
73
2c6c7645 74function assertIPv4Arg ($argname, $caller = 'N/A', $ok_if_empty = FALSE)
17c32f4c 75{
2c6c7645 76 assertStringArg ($argname, $caller, $ok_if_empty);
17c32f4c
DO
77 if (!empty ($_REQUEST[$argname]) and long2ip (ip2long ($_REQUEST[$argname])) !== $_REQUEST[$argname])
78 {
2c6c7645 79 showError ("IPv4 address validation failed for value '" . $_REQUEST[$argname] . "' (calling function is [${caller}]).", __FUNCTION__);
17c32f4c
DO
80 die();
81 }
82}
83
e673ee24
DO
84function addPortForwarding ()
85{
2c6c7645
DO
86 assertUIntArg ('object_id', __FUNCTION__);
87 assertIPv4Arg ('localip', __FUNCTION__);
88 assertIPv4Arg ('remoteip', __FUNCTION__);
89 assertUIntArg ('localport', __FUNCTION__);
9bea3a8b 90 assertStringArg ('proto', __FUNCTION__);
2c6c7645 91 assertStringArg ('description', __FUNCTION__, TRUE);
103b1e1e 92 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
31fc4157 93 if (empty ($remoteport))
f75eb878 94 $remoteport = $_REQUEST['localport'];
e673ee24 95
9bea3a8b
DO
96 $error = newPortForwarding
97 (
103b1e1e 98 $_REQUEST['object_id'],
9bea3a8b
DO
99 $_REQUEST['localip'],
100 $_REQUEST['localport'],
101 $_REQUEST['remoteip'],
102 $remoteport,
103 $_REQUEST['proto'],
104 $_REQUEST['description']
105 );
e673ee24
DO
106
107 if ($error == '')
135080d8 108 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 109 else
135080d8 110 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
111}
112
113function delPortForwarding ()
114{
103b1e1e
DO
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__);
e673ee24 121
103b1e1e
DO
122 $error = deletePortForwarding
123 (
124 $_REQUEST['object_id'],
125 $_REQUEST['localip'],
126 $_REQUEST['localport'],
127 $_REQUEST['remoteip'],
128 $_REQUEST['remoteport'],
129 $_REQUEST['proto']
130 );
e673ee24 131 if ($error == '')
135080d8 132 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 133 else
135080d8 134 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
135}
136
137function updPortForwarding ()
138{
103b1e1e
DO
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__);
e673ee24 146
103b1e1e
DO
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 );
e673ee24 157 if ($error == '')
135080d8 158 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 159 else
135080d8 160 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
161}
162
e673ee24
DO
163function addPortForObject ()
164{
103b1e1e
DO
165 assertUIntArg ('object_id', __FUNCTION__);
166 assertStringArg ('port_name', __FUNCTION__, TRUE);
167 if (empty ($_REQUEST['port_name']))
135080d8 168 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e
DO
169 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
170 if ($error != '')
135080d8 171 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 172 else
135080d8 173 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
174}
175
176function editPortForObject ()
177{
2c6c7645 178 assertUIntArg ('port_id', __FUNCTION__);
611170af 179 assertUIntArg ('port_type_id', __FUNCTION__);
d0a69ce8 180 // tolerate empty value now to produce custom informative message later
103b1e1e
DO
181 assertStringArg ('name', __FUNCTION__, TRUE);
182 if (empty ($_REQUEST['name']))
135080d8 183 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e 184
e673ee24
DO
185 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
186 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
187 else
188 $port_rc = 'NULL';
611170af 189 $error = commitUpdatePort ($_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['port_type_id'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
103b1e1e 190 if ($error != '')
135080d8 191 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 192 else
135080d8 193 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e673ee24
DO
194}
195
196function delPortFromObject ()
197{
103b1e1e 198 assertUIntArg ('port_id', __FUNCTION__);
103b1e1e 199 $error = delObjectPort ($_REQUEST['port_id']);
e673ee24
DO
200
201 if ($error != '')
135080d8 202 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 203 else
135080d8 204 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
205}
206
207function linkPortForObject ()
208{
103b1e1e
DO
209 assertUIntArg ('port_id', __FUNCTION__);
210 assertUIntArg ('remote_port_id', __FUNCTION__);
baf37d01
DO
211 assertStringArg ('port_name', __FUNCTION__, TRUE);
212 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
213 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 214
103b1e1e 215 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
e673ee24 216 if ($error != '')
135080d8 217 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 218 else
135080d8 219 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
220}
221
222function unlinkPortForObject ()
223{
103b1e1e 224 assertUIntArg ('port_id', __FUNCTION__);
baf37d01
DO
225 assertStringArg ('port_name', __FUNCTION__, TRUE);
226 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
227 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 228
103b1e1e 229 $error = unlinkPort ($_REQUEST['port_id']);
e673ee24 230 if ($error != '')
135080d8 231 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 232 else
135080d8 233 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
234}
235
236function addMultiPorts ()
237{
2c6c7645
DO
238 assertStringArg ('format', __FUNCTION__);
239 assertStringArg ('input', __FUNCTION__);
240 assertUIntArg ('port_type', __FUNCTION__);
241 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
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/*
275How Async Lines are Numbered in Cisco 3600 Series Routers
276http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
277
278Understanding 16- and 32-Port Async Network Modules
279http://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;
351d4dbf 306 case 'ssv1':
5b585342 307 $words = explode (' ', $line);
351d4dbf
DO
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;
e673ee24 317 default:
135080d8 318 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
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 {
611170af 337 $result = commitUpdatePort ($port_id, $port['name'], $port_type, $port['label'], $port['l2address']);
e673ee24
DO
338 if ($result == '')
339 $updated_count++;
340 else
341 $error_count++;
342 }
343 }
135080d8 344 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $updated_count, $error_count));
e673ee24
DO
345}
346
b4c1ef87 347function updIPv4Allocation ()
e673ee24 348{
b4c1ef87
DO
349 assertIPv4Arg ('ip', __FUNCTION__);
350 assertUIntArg ('object_id', __FUNCTION__);
351 assertStringArg ('bond_name', __FUNCTION__, TRUE);
352 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 353
b4c1ef87 354 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 355 if ($error != '')
135080d8 356 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 357 else
135080d8 358 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
359}
360
b4c1ef87 361function delIPv4Allocation ()
e673ee24 362{
b4c1ef87
DO
363 assertIPv4Arg ('ip', __FUNCTION__);
364 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 365
b4c1ef87 366 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 367 if ($error != '')
135080d8 368 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 369 else
135080d8 370 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
371}
372
b4c1ef87 373function addIPv4Allocation ()
e673ee24 374{
b4c1ef87
DO
375 assertIPv4Arg ('ip', __FUNCTION__);
376 assertUIntArg ('object_id', __FUNCTION__);
377 assertStringArg ('bond_name', __FUNCTION__, TRUE);
378 assertStringArg ('bond_type', __FUNCTION__);
da958e52 379
b4c1ef87
DO
380 // Strip masklen.
381 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
2be3fd23 382 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
135080d8 383 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
5222f192 384
b4c1ef87 385 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
5222f192 386 if ($error != '')
135080d8 387 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
53ef3908
DO
388 $address = getIPv4Address ($ip);
389 if ($address['reserved'] == 'yes' or !empty ($address['name']))
b4c1ef87
DO
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 }
135080d8 398 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
399}
400
42023f03 401function addIPv4Prefix ()
e673ee24 402{
2c6c7645 403 assertStringArg ('range', __FUNCTION__);
e4b9a099 404 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 405
fa05e3de
DO
406 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
407 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 408 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 409 if ($error != '')
135080d8 410 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 411 else
135080d8 412 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
413}
414
42023f03 415function delIPv4Prefix ()
e673ee24 416{
da958e52 417 assertUIntArg ('id', __FUNCTION__);
42023f03 418 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 419 if ($error != '')
135080d8 420 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 421 else
135080d8 422 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
423}
424
04d619d0 425function updIPv4Prefix ()
e673ee24 426{
da958e52 427 assertUIntArg ('id', __FUNCTION__);
e4b9a099 428 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 429
da958e52 430 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
e673ee24 431 if ($error != '')
135080d8 432 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 433 else
135080d8 434 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
435}
436
437function editAddress ()
438{
da958e52
DO
439 assertIPv4Arg ('ip', __FUNCTION__);
440 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 441
e673ee24
DO
442 if (isset ($_REQUEST['reserved']))
443 $reserved = $_REQUEST['reserved'];
444 else
445 $reserved = 'off';
da958e52 446 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 447 if ($error != '')
135080d8 448 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 449 else
135080d8 450 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
451}
452
cced6b7d 453function createUser ()
e673ee24 454{
2c6c7645
DO
455 assertStringArg ('username', __FUNCTION__);
456 assertStringArg ('realname', __FUNCTION__, TRUE);
457 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
458 $username = $_REQUEST['username'];
459 $password = hash (PASSWORD_HASH, $_REQUEST['password']);
460 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
461 if ($result == TRUE)
135080d8 462 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 463 else
135080d8 464 return buildRedirectURL (__FUNCTION__, 'ERR', array ($username));
e673ee24
DO
465}
466
cced6b7d 467function updateUser ()
e673ee24 468{
cced6b7d 469 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
470 assertStringArg ('username', __FUNCTION__);
471 assertStringArg ('realname', __FUNCTION__, TRUE);
472 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
473 $username = $_REQUEST['username'];
474 $new_password = $_REQUEST['password'];
103b1e1e 475 $old_hash = getHashByID ($_REQUEST['user_id']);
e673ee24 476 if ($old_hash == NULL)
135080d8 477 return buildRedirectURL (__FUNCTION__, 'ERR1');
e673ee24
DO
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);
103b1e1e 481 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
e673ee24 482 if ($result == TRUE)
135080d8 483 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 484 else
135080d8 485 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($username));
e673ee24
DO
486}
487
e673ee24
DO
488// This function find differences in users's submit and PortCompat table
489// and modifies database accordingly.
490function savePortMap ()
491{
e673ee24
DO
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:
ebb5441d 525 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
526 break;
527 }
103b1e1e 528 if ($error_count == 0)
135080d8 529 return buildRedirectURL (__FUNCTION__, 'OK', array ($error_count, $success_count));
103b1e1e 530 else
135080d8 531 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error_count, $success_count));
e673ee24
DO
532}
533
534function updateDictionary ()
535{
2c6c7645
DO
536 assertUIntArg ('chapter_no', __FUNCTION__);
537 assertUIntArg ('dict_key', __FUNCTION__);
538 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 539 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
135080d8 540 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 541 else
135080d8 542 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
543}
544
545function supplementDictionary ()
546{
2c6c7645
DO
547 assertUIntArg ('chapter_no', __FUNCTION__);
548 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 549 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
135080d8 550 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 551 else
135080d8 552 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
553}
554
555function reduceDictionary ()
556{
2c6c7645
DO
557 assertUIntArg ('chapter_no', __FUNCTION__);
558 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24 559 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
135080d8 560 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 561 else
135080d8 562 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
563}
564
565function addChapter ()
566{
2c6c7645 567 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 568 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
135080d8 569 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 570 else
135080d8 571 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
572}
573
574function updateChapter ()
575{
2c6c7645
DO
576 assertUIntArg ('chapter_no', __FUNCTION__);
577 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 578 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
135080d8 579 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 580 else
135080d8 581 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
582}
583
584function delChapter ()
585{
2c6c7645 586 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 587 if (commitDeleteChapter ($_REQUEST['chapter_no']))
135080d8 588 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 589 else
135080d8 590 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
591}
592
593function changeAttribute ()
594{
2c6c7645
DO
595 assertUIntArg ('attr_id', __FUNCTION__);
596 assertStringArg ('attr_name', __FUNCTION__);
e673ee24 597 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
135080d8 598 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 599 else
135080d8 600 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
601}
602
603function createAttribute ()
604{
2c6c7645
DO
605 assertStringArg ('attr_name', __FUNCTION__);
606 assertStringArg ('attr_type', __FUNCTION__);
e673ee24 607 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
135080d8 608 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
e673ee24 609 else
135080d8 610 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
611}
612
613function deleteAttribute ()
614{
2c6c7645 615 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24 616 if (commitDeleteAttribute ($_REQUEST['attr_id']))
135080d8 617 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 618 else
135080d8 619 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
620}
621
622function supplementAttrMap ()
623{
2c6c7645
DO
624 assertUIntArg ('attr_id', __FUNCTION__);
625 assertUIntArg ('objtype_id', __FUNCTION__);
626 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 627 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
135080d8 628 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 629 else
135080d8 630 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
631}
632
633function reduceAttrMap ()
634{
2c6c7645
DO
635 assertUIntArg ('attr_id', __FUNCTION__);
636 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24 637 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
135080d8 638 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 639 else
135080d8 640 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
641}
642
22bb04da 643function clearSticker ()
e673ee24 644{
2c6c7645
DO
645 assertUIntArg ('attr_id', __FUNCTION__);
646 assertUIntArg ('object_id', __FUNCTION__);
22bb04da 647 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
135080d8 648 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 649 else
135080d8 650 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
651}
652
22bb04da
DO
653function updateObject ()
654{
cdd3efe9 655 assertUIntArg ('num_attrs', __FUNCTION__);
22bb04da
DO
656 assertUIntArg ('object_id', __FUNCTION__);
657 assertUIntArg ('object_type_id', __FUNCTION__);
658 assertStringArg ('object_name', __FUNCTION__, TRUE);
659 assertStringArg ('object_label', __FUNCTION__, TRUE);
660 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
661 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
662 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
663 $has_problems = 'yes';
664 else
665 $has_problems = 'no';
666
667 if (commitUpdateObject (
668 $_REQUEST['object_id'],
669 $_REQUEST['object_name'],
670 $_REQUEST['object_label'],
671 $_REQUEST['object_barcode'],
672 $_REQUEST['object_type_id'],
673 $has_problems,
674 $_REQUEST['object_asset_no'],
675 $_REQUEST['object_comment']
676 ) !== TRUE)
135080d8 677 return buildRedirectURL (__FUNCTION__, 'ERR');
22bb04da 678
cdd3efe9 679 // Update optional attributes
f59e8cf5 680 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a 681 $result = array();
103b1e1e 682 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
0dfb8a2a 683 {
2c6c7645 684 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
685 $attr_id = $_REQUEST["${i}_attr_id"];
686
687 // Field is empty, delete attribute and move on.
688 if (empty($_REQUEST["${i}_value"]))
689 {
103b1e1e 690 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
691 continue;
692 }
693
694 // The value could be uint/float, but we don't know ATM. Let SQL
695 // server check this and complain.
2c6c7645 696 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
697 $value = $_REQUEST["${i}_value"];
698 switch ($oldvalues[$attr_id]['type'])
699 {
700 case 'uint':
701 case 'float':
702 case 'string':
703 $oldvalue = $oldvalues[$attr_id]['value'];
704 break;
705 case 'dict':
706 $oldvalue = $oldvalues[$attr_id]['key'];
707 break;
708 default:
61e269b5 709 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
710 die;
711 }
712 if ($value == $oldvalue)
713 continue;
714
715 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 716 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a 717 }
103b1e1e 718 if (in_array (FALSE, $result))
135080d8 719 return buildRedirectURL (__FUNCTION__, 'ERR');
0dfb8a2a 720
cdd3efe9
AD
721 // Invalidate thumb cache of all racks objects could occupy.
722 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
723 resetThumbCache ($rack_id);
724
135080d8 725 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
726}
727
52b34485
AD
728function deleteObject ()
729{
730 assertUIntArg ('object_id', __FUNCTION__);
731 $error = commitDeleteObject ($_REQUEST['object_id']);
732
733 if ($error != '')
135080d8 734 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
52b34485 735
135080d8 736 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
52b34485
AD
737}
738
e673ee24
DO
739function useupPort ()
740{
2c6c7645 741 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 742 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
135080d8 743 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 744 else
135080d8 745 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
746}
747
4fe32e78
DY
748function updateUI ()
749{
2c6c7645 750 assertUIntArg ('num_vars', __FUNCTION__);
c461c579 751 $error = '';
4fe32e78 752
103b1e1e 753 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 754 {
2c6c7645
DO
755 assertStringArg ("${i}_varname", __FUNCTION__);
756 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 757 $varname = $_REQUEST["${i}_varname"];
4fe32e78 758 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 759
c461c579
DO
760 // If form value = value in DB, don't bother updating DB
761 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
762 continue;
763
764 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
765 $error = setConfigVar ($varname, $varvalue, TRUE);
766 if (!empty ($error))
767 break;
4fe32e78
DY
768 }
769
c461c579 770 if ($error != '')
135080d8 771 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
4fe32e78 772
135080d8 773 return buildRedirectURL (__FUNCTION__, 'OK');
4fe32e78
DY
774}
775
b07f617c
DO
776function resetUIConfig()
777{
1f86e996 778 setConfigVar ('default_port_type','24');
b07f617c
DO
779 setConfigVar ('MASSCOUNT','15');
780 setConfigVar ('MAXSELSIZE','30');
781 setConfigVar ('NAMEFUL_OBJTYPES','4,7,8');
782 setConfigVar ('ROW_SCALE','2');
783 setConfigVar ('PORTS_PER_ROW','12');
784 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
785 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
a6305acc 786 setConfigVar ('REQUIRE_ASSET_TAG_FOR','4,7,8');
b5ce46aa
DO
787 setConfigVar ('DEFAULT_SLB_VS_PORT','');
788 setConfigVar ('DEFAULT_SLB_RS_PORT','');
d48e0a20 789 setConfigVar ('IPV4_PERFORMERS','1,4,7,8,12,14,445,447');
b5ce46aa 790 setConfigVar ('NATV4_PERFORMERS','4,7,8');
72281138 791 setConfigVar ('DETECT_URLS','no');
4b8d413e 792 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 793 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 794 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
795 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
796 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
797 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 798 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 799 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df
DO
800 setConfigVar ('SHOW_LAST_TAB', 'no');
801 setConfigVar ('COOKIE_TTL', '1209600');
f0ed1181 802 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 803 setConfigVar ('TREE_THRESHOLD', '25');
2be3fd23 804 setConfigVar ('IPV4_JAYWALK', 'no');
9318d2ef 805 setConfigVar ('ADDNEW_AT_TOP', 'yes');
64347dcf 806 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
37e59768
DO
807 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
808 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
809 setConfigVar ('PREVIEW_TEXT_COLS', '80');
810 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
135080d8 811 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
812}
813
732e4578 814// Add single record.
ca461127
DO
815function addRealServer ()
816{
2c6c7645
DO
817 assertUIntArg ('pool_id', __FUNCTION__);
818 assertIPv4Arg ('remoteip', __FUNCTION__);
79a9edb4 819 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 820 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
821 if (!addRStoRSPool (
822 $_REQUEST['pool_id'],
823 $_REQUEST['remoteip'],
824 $_REQUEST['rsport'],
825 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
826 $_REQUEST['rsconfig']
827 ))
135080d8 828 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 829 else
135080d8 830 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
831}
832
732e4578
DO
833// Parse textarea submitted and try adding a real server for each line.
834function addRealServers ()
835{
cbadbb38 836 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
837 assertStringArg ('format', __FUNCTION__);
838 assertStringArg ('rawtext', __FUNCTION__);
732e4578
DO
839 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
840 $ngood = $nbad = 0;
841 $rsconfig = '';
842 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
843 foreach (explode ('\n', $rawtext) as $line)
844 {
845 if (empty ($line))
846 continue;
847 $match = array ();
848 switch ($_REQUEST['format'])
849 {
850 case 'ipvs_2': // address and port only
851 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
852 continue;
103b1e1e 853 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
854 $ngood++;
855 else
856 $nbad++;
857 break;
858 case 'ipvs_3': // address, port and weight
859 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
860 continue;
103b1e1e 861 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
862 $ngood++;
863 else
864 $nbad++;
865 break;
e69c2aa3
DO
866 case 'ssv_2': // IP address and port
867 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
868 continue;
103b1e1e 869 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
870 $ngood++;
871 else
872 $nbad++;
873 break;
948c37e3
DO
874 case 'ssv_1': // IP address
875 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
876 continue;
877 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
878 $ngood++;
879 else
880 $nbad++;
881 break;
732e4578 882 default:
135080d8 883 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
884 break;
885 }
886 }
887 if ($nbad == 0 and $ngood > 0)
135080d8 888 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 889 else
135080d8 890 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
891}
892
d6517a21
DO
893function addVService ()
894{
2c6c7645
DO
895 assertIPv4Arg ('vip', __FUNCTION__);
896 assertUIntArg ('vport', __FUNCTION__);
897 assertStringArg ('proto', __FUNCTION__);
103b1e1e 898 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 899 return buildRedirectURL (__FUNCTION__, 'ERR1');
2c6c7645
DO
900 assertStringArg ('name', __FUNCTION__, TRUE);
901 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
902 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
903 $error = commitCreateVS
904 (
103b1e1e
DO
905 $_REQUEST['vip'],
906 $_REQUEST['vport'],
7320b661 907 $_REQUEST['proto'],
103b1e1e
DO
908 $_REQUEST['name'],
909 $_REQUEST['vsconfig'],
c63a8d6e
DO
910 $_REQUEST['rsconfig'],
911 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
912 );
913 if ($error != '')
135080d8 914 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 915 else
135080d8 916 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
917}
918
919function deleteRealServer ()
920{
2c6c7645 921 assertUIntArg ('id', __FUNCTION__);
d6517a21 922 if (!commitDeleteRS ($_REQUEST['id']))
135080d8 923 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 924 else
135080d8 925 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
926}
927
3241551e
DO
928function deleteLoadBalancer ()
929{
2c6c7645
DO
930 assertUIntArg ('object_id', __FUNCTION__);
931 assertUIntArg ('pool_id', __FUNCTION__);
932 assertUIntArg ('vs_id', __FUNCTION__);
103b1e1e
DO
933 if (!commitDeleteLB (
934 $_REQUEST['object_id'],
935 $_REQUEST['pool_id'],
936 $_REQUEST['vs_id']
937 ))
135080d8 938 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 939 else
135080d8 940 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
941}
942
943function deleteVService ()
944{
e02e1941
DO
945 assertUIntArg ('vs_id', __FUNCTION__);
946 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 947 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 948 else
135080d8 949 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
950}
951
fb1c4a54
DO
952function updateRealServer ()
953{
2c6c7645
DO
954 assertUIntArg ('rs_id', __FUNCTION__);
955 assertIPv4Arg ('rsip', __FUNCTION__);
79a9edb4 956 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 957 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
958 if (!commitUpdateRS (
959 $_REQUEST['rs_id'],
960 $_REQUEST['rsip'],
961 $_REQUEST['rsport'],
962 $_REQUEST['rsconfig']
963 ))
135080d8 964 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 965 else
135080d8 966 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
967}
968
c1ca768c 969function updateLoadBalancer ()
3241551e 970{
2c6c7645
DO
971 assertUIntArg ('object_id', __FUNCTION__);
972 assertUIntArg ('pool_id', __FUNCTION__);
973 assertUIntArg ('vs_id', __FUNCTION__);
974 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
975 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
976 if (!commitUpdateLB (
977 $_REQUEST['object_id'],
978 $_REQUEST['pool_id'],
979 $_REQUEST['vs_id'],
980 $_REQUEST['vsconfig'],
981 $_REQUEST['rsconfig']
982 ))
135080d8 983 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 984 else
135080d8 985 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
986}
987
988function updateVService ()
989{
e02e1941 990 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
991 assertIPv4Arg ('vip', __FUNCTION__);
992 assertUIntArg ('vport', __FUNCTION__);
993 assertStringArg ('proto', __FUNCTION__);
994 assertStringArg ('name', __FUNCTION__, TRUE);
995 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
996 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
997 if (!commitUpdateVS (
998 $_REQUEST['vs_id'],
999 $_REQUEST['vip'],
1000 $_REQUEST['vport'],
1001 $_REQUEST['proto'],
1002 $_REQUEST['name'],
1003 $_REQUEST['vsconfig'],
1004 $_REQUEST['rsconfig']
1005 ))
135080d8 1006 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1007 else
135080d8 1008 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1009}
1010
1011function addLoadBalancer ()
1012{
2c6c7645
DO
1013 assertUIntArg ('pool_id', __FUNCTION__);
1014 assertUIntArg ('object_id', __FUNCTION__);
1015 assertUIntArg ('vs_id', __FUNCTION__);
1016 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1017 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1018 if (!addLBtoRSPool (
1019 $_REQUEST['pool_id'],
1020 $_REQUEST['object_id'],
1021 $_REQUEST['vs_id'],
1022 $_REQUEST['vsconfig'],
1023 $_REQUEST['rsconfig']
1024 ))
135080d8 1025 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1026 else
135080d8 1027 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1028}
1029
5ad76f01
DO
1030function addRSPool ()
1031{
2c6c7645
DO
1032 assertStringArg ('name', __FUNCTION__, TRUE);
1033 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1034 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1035 $error = commitCreateRSPool
1036 (
103b1e1e
DO
1037 $_REQUEST['name'],
1038 $_REQUEST['vsconfig'],
c63a8d6e
DO
1039 $_REQUEST['rsconfig'],
1040 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1041 );
1042 if ($error != '')
135080d8 1043 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1044 else
135080d8 1045 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1046}
1047
1048function deleteRSPool ()
1049{
2c6c7645 1050 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1051 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
135080d8 1052 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1053 else
135080d8 1054 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1055}
1056
1057function updateRSPool ()
1058{
2c6c7645
DO
1059 assertUIntArg ('pool_id', __FUNCTION__);
1060 assertStringArg ('name', __FUNCTION__, TRUE);
1061 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1062 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1063 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
135080d8 1064 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1065 else
135080d8 1066 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1067}
1068
1f7d18fa
DO
1069function updateRSInService ()
1070{
2c6c7645 1071 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1072 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1073 $orig = getRSPoolInfo ($pool_id);
1074 $nbad = $ngood = 0;
1075 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1076 {
1077 $rs_id = $_REQUEST["rsid_${i}"];
1078 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1079 $newval = 'yes';
1080 else
1081 $newval = 'no';
1082 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1083 {
1084 if (commitSetInService ($rs_id, $newval))
1085 $ngood++;
1086 else
1087 $nbad++;
1088 }
1089 }
1090 if (!$nbad)
135080d8 1091 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1092 else
135080d8 1093 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1094}
1095
8d9c16e7
DO
1096// FIXME: check, that each submitted address belongs to the prefix we
1097// are operating on.
03eb5209
DO
1098function importPTRData ()
1099{
2c6c7645 1100 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209 1101 $nbad = $ngood = 0;
03eb5209
DO
1102 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1103 {
3f3bd41e 1104 $inputname = "import_${i}";
03eb5209
DO
1105 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1106 continue;
2c6c7645
DO
1107 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1108 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1109 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1110 // Non-existent addresses will not have this argument set in request.
1111 $rsvd = 'no';
03eb5209
DO
1112 if ($_REQUEST["rsvd_${i}"] == 'yes')
1113 $rsvd = 'yes';
3f3bd41e 1114 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1115 $ngood++;
1116 else
1117 $nbad++;
1118 }
1119 if (!$nbad)
135080d8 1120 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1121 else
135080d8 1122 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1123}
1124
f3f0161f
DO
1125function generateAutoPorts ()
1126{
103b1e1e 1127 global $pageno;
2c6c7645 1128 assertUIntArg ('object_id', __FUNCTION__);
103b1e1e 1129 $info = getObjectInfo ($_REQUEST['object_id']);
f3f0161f 1130 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1131 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1132 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1133 else
135080d8 1134 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1135}
1136
24cbe8af 1137// Filter out implicit tags before storing the new tag set.
3355ca56 1138function saveEntityTags ()
24cbe8af 1139{
be2ae2a2
DO
1140 global $explicit_tags, $implicit_tags, $page, $pageno, $etype_by_pageno;
1141 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
3355ca56
DO
1142 {
1143 showError ('Internal error', __FUNCTION__);
1144 die;
1145 }
be2ae2a2 1146 $realm = $etype_by_pageno[$pageno];
3355ca56 1147 $bypass = $page[$pageno]['bypass'];
2c6c7645 1148 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1149 $entity_id = $_REQUEST[$bypass];
e04931c8 1150 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1151 // Build a chain from the submitted data, minimize it,
ab379543 1152 // then wipe existing records and store the new set instead.
01b6b4d6 1153 destroyTagsForEntity ($realm, $entity_id);
6e49bd1f 1154 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1155 $n_succeeds = $n_errors = 0;
6e49bd1f 1156 foreach ($newchain as $taginfo)
eb6ea26f 1157 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1158 $n_succeeds++;
1159 else
1160 $n_errors++;
ab379543 1161 if ($n_errors)
135080d8 1162 return buildRedirectURL (__FUNCTION__, 'ERR', array ($n_succeeds, $n_errors));
ab379543 1163 else
135080d8 1164 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1165}
1166
fe7044ad
DO
1167function destroyTag ()
1168{
49fb1027
DO
1169 assertUIntArg ('tag_id', __FUNCTION__);
1170 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
135080d8 1171 return buildRedirectURL (__FUNCTION__, 'OK');
fe7044ad 1172 else
135080d8 1173 return buildRedirectURL (__FUNCTION__, 'ERR');
fe7044ad
DO
1174}
1175
1176function createTag ()
1177{
49fb1027 1178 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1179 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1180 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1181 if (!validTagName ($tagname))
135080d8 1182 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
0f3c6639 1183 if (tagExistsInDatabase ($tagname))
135080d8 1184 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname));
fe7044ad
DO
1185 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1186 $parent_id = 'NULL';
1187 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
135080d8 1188 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
fe7044ad 1189 else
135080d8 1190 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
fe7044ad
DO
1191}
1192
49fb1027
DO
1193function updateTag ()
1194{
49fb1027
DO
1195 assertUIntArg ('tag_id', __FUNCTION__);
1196 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1197 assertStringArg ('tag_name', __FUNCTION__);
1198 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1199 if (!validTagName ($tagname))
135080d8 1200 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1201 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1202 $parent_id = 'NULL';
1203 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
135080d8 1204 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
49fb1027 1205 else
135080d8 1206 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($tagname, $ret));
49fb1027
DO
1207}
1208
eb6ea26f
DO
1209function rollTags ()
1210{
eb6ea26f
DO
1211 assertUIntArg ('row_id', __FUNCTION__);
1212 assertStringArg ('sum', __FUNCTION__, TRUE);
1213 assertUIntArg ('realsum', __FUNCTION__);
eb6ea26f 1214 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1215 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1216 // Even if the user requested an empty tag list, don't bail out, but process existing
1217 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1218 // have its chains refined to "normal" form.
1219 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1220 $n_ok = 0;
1221 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1222 // filter out the same tag again and again. It will have own noise to cancel.
1223 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
1224 foreach (getRacksForRow ($_REQUEST['row_id']) as $rackInfo)
1225 {
1226 if (rebuildTagChainForEntity ('rack', $rackInfo['id'], $extrachain))
1227 $n_ok++;
1228 foreach (stuffInRackspace (getRackData ($rackInfo['id'])) as $object_id)
1229 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1230 $n_ok++;
1231 }
135080d8 1232 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1233}
1234
9457ca59 1235function changeMyPassword ()
cced6b7d 1236{
204284ba
DO
1237 global $accounts, $remote_username, $user_auth_src;
1238 if ($user_auth_src != 'database')
135080d8 1239 return buildRedirectURL (__FUNCTION__, 'ERR1');
0ac7e4b3
DO
1240 assertStringArg ('oldpassword', __FUNCTION__);
1241 assertStringArg ('newpassword1', __FUNCTION__);
1242 assertStringArg ('newpassword2', __FUNCTION__);
1243 if ($accounts[$remote_username]['user_password_hash'] != hash (PASSWORD_HASH, $_REQUEST['oldpassword']))
135080d8 1244 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1245 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1246 return buildRedirectURL (__FUNCTION__, 'ERR3');
0ac7e4b3 1247 if (commitUpdateUserAccount ($accounts[$remote_username]['user_id'], $accounts[$remote_username]['user_name'], $accounts[$remote_username]['user_realname'], hash (PASSWORD_HASH, $_REQUEST['newpassword1'])))
135080d8 1248 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1249 else
135080d8 1250 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1251}
1252
1253function saveRackCode ()
1254{
cced6b7d 1255 assertStringArg ('rackcode');
e6a4adb9
DO
1256 // For the test to succeed, unescape LFs, strip CRs.
1257 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1258 $parseTree = getRackCode ($newcode);
1259 if ($parseTree['result'] != 'ACK')
135080d8 1260 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
4a6a28f1 1261 saveScript ('RackCodeCache', '');
cf25e649 1262 if (saveScript ('RackCode', $newcode))
135080d8 1263 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1264 else
135080d8 1265 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1266}
1267
46f92ff7
DO
1268// This handler's context is pre-built, but not authorized. It is assumed, that the
1269// handler will take existing context and before each commit check authorization
1270// on the base chain plus necessary context added.
1271function setPortVLAN ()
1272{
1273 assertUIntArg ('portcount', __FUNCTION__);
1274 $data = getSwitchVLANs ($_REQUEST['object_id']);
1275 if ($data === NULL)
135080d8 1276 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1277 list ($vlanlist, $portlist) = $data;
1278 // Here we just build up 1 set command for the gateway with all of the ports
1279 // included. The gateway is expected to filter unnecessary changes silently
1280 // and to provide a list of responses with either error or success message
1281 // for each of the rest.
1282 $nports = $_REQUEST['portcount'];
1283 $prefix = 'set ';
73067b6a 1284 $log = array ('v' => 2, 'm' => array());
46f92ff7
DO
1285 $setcmd = '';
1286 for ($i = 0; $i < $nports; $i++)
1287 if
1288 (
1289 !isset ($_REQUEST['portname_' . $i]) ||
1290 !isset ($_REQUEST['vlanid_' . $i]) ||
1291 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1292 )
24dcb9d8 1293 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1294 elseif
1295 (
1296 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1297 $portlist[$i]['vlaind'] == 'TRUNK'
1298 )
1299 continue;
1300 else
1301 {
1302 $portname = $_REQUEST['portname_' . $i];
1303 $oldvlanid = $portlist[$i]['vlanid'];
1304 $newvlanid = $_REQUEST['vlanid_' . $i];
1305 // Finish the security context and evaluate it.
1306 $annex = array();
1307 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1308 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1309 if (!permitted (NULL, NULL, NULL, $annex))
1310 {
24dcb9d8 1311 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1312 continue;
1313 }
1314 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1315 $prefix = ';';
1316 }
1317 // Feed the gateway and interpret its (non)response.
1318 if ($setcmd != '')
f0ff4930 1319 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1320 else
24dcb9d8 1321 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1322 return buildWideRedirectURL ($log);
1323}
1324
2987fc1f
DO
1325function submitSLBConfig ()
1326{
1327 assertUIntArg ('object_id', __FUNCTION__);
1328 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
3dd72e34 1329 $msglog = gwSendFile ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1330 return buildWideRedirectURL ($msglog);
1331}
1332
345fd640
AD
1333function addRow ()
1334{
1335 assertStringArg ('name', __FUNCTION__);
1336
1337 if (commitSupplementDictionary (3, $_REQUEST['name']) === TRUE)
135080d8 1338 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1339 else
135080d8 1340 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1341}
1342
1343function updateRow ()
1344{
1345 assertUIntArg ('row_id', __FUNCTION__);
1346 assertStringArg ('name', __FUNCTION__);
1347
1348 if (TRUE === commitUpdateDictionary (3, $_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1349 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1350 else
135080d8 1351 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1352}
1353
f19c75d6
DO
1354function addRack ()
1355{
1356 assertUIntArg ('row_id', __FUNCTION__);
1357 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1358 if (isset ($_REQUEST['got_data']))
1359 {
1360 assertStringArg ('rack_name', __FUNCTION__);
1361 assertUIntArg ('rack_height1', __FUNCTION__);
1362 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1363
1364 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1365 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1366 else
135080d8 1367 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1368 }
1369 elseif (isset ($_REQUEST['got_mdata']))
1370 {
1371 assertUIntArg ('rack_height2', __FUNCTION__);
1372 assertStringArg ('rack_names', __FUNCTION__, TRUE);
1373 $log = array ('v' => 2);
1374 // copy-and-paste from renderAddMultipleObjectsForm()
1375 $names1 = explode ('\n', $_REQUEST['rack_names']);
1376 $names2 = array();
1377 foreach ($names1 as $line)
1378 {
1379 $parts = explode ('\r', $line);
1380 reset ($parts);
1381 if (empty ($parts[0]))
1382 continue;
1383 else
1384 $names2[] = rtrim ($parts[0]);
1385 }
1386 foreach ($names2 as $cname)
1387 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
135080d8 1388 $log['m'][] = array ('c' => getMessageCode (__FUNCTION__, 'OK'), 'a' => array ($cname));
f19c75d6 1389 else
135080d8 1390 $log['m'][] = array ('c' => getMessageCode (__FUNCTION__, 'ERR1'), 'a' => array ($cname));
f19c75d6
DO
1391 return buildWideRedirectURL ($log);
1392 }
1393 else
135080d8 1394 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1395}
1396
7056988c
DO
1397function updateRack ()
1398{
7056988c
DO
1399 assertUIntArg ('rack_id', __FUNCTION__);
1400 assertUIntArg ('rack_row_id', __FUNCTION__);
1401 assertUIntArg ('rack_height', __FUNCTION__);
1402 assertStringArg ('rack_name', __FUNCTION__);
1403 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
7056988c 1404
7056988c 1405 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1406 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1407 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1408 else
135080d8 1409 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1410}
1411
1412function querySNMPData ()
1413{
1414 assertUIntArg ('object_id', __FUNCTION__);
1415 assertStringArg ('community', __FUNCTION__);
83fdf85f 1416 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
7056988c
DO
1417}
1418
e1ae3fb4
AD
1419// File-related functions
1420function addFileWithoutLink ()
1421{
1422 assertStringArg ('comment', __FUNCTION__, TRUE);
1423
1424 // Make sure the file can be uploaded
1425 if (get_cfg_var('file_uploads') != 1)
135080d8 1426 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1427
1428 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1429 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1430
1431 if ($error != '')
135080d8 1432 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1433
135080d8 1434 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1435}
1436
1437function addFileToEntity ()
1438{
1439 assertStringArg ('entity_type', __FUNCTION__);
1440 assertUIntArg ('entity_id', __FUNCTION__);
1441 assertStringArg ('comment', __FUNCTION__, TRUE);
1442 if (empty ($_REQUEST['entity_type']) || empty ($_REQUEST['entity_id']))
135080d8 1443 return buildRedirectURL (__FUNCTION__, 'ERR');
e1ae3fb4
AD
1444
1445 // Make sure the file can be uploaded
1446 if (get_cfg_var('file_uploads') != 1)
135080d8 1447 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1448
1449 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1450 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1451 if ($error != '')
135080d8 1452 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4
AD
1453
1454 $file_id = lastInsertID();
1455 $error = commitLinkFile ($file_id, $_REQUEST['entity_type'], $_REQUEST['entity_id']);
1456 if ($error != '')
135080d8 1457 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1458
135080d8 1459 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1460}
1461
1462function linkFileToEntity ()
1463{
e1ae3fb4 1464 assertUIntArg ('file_id', __FUNCTION__);
482c7f35 1465 global $page, $pageno, $etype_by_pageno;
121496b6
DO
1466 $entity_type = $etype_by_pageno[$pageno];
1467 $bypass_name = $page[$pageno]['bypass'];
1468 assertUIntArg ($bypass_name, __FUNCTION__);
1469
1470 $fi = getFileInfo ($_REQUEST['file_id']);
1471 if ($fi === NULL)
135080d8 1472 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
121496b6 1473 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
e1ae3fb4 1474 if ($error != '')
135080d8 1475 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
e1ae3fb4 1476
135080d8 1477 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
e1ae3fb4
AD
1478}
1479
1480function updateFile ()
1481{
1482 assertUIntArg ('file_id', __FUNCTION__);
1483 assertStringArg ('name', __FUNCTION__);
1484 assertStringArg ('comment', __FUNCTION__, TRUE);
1485 $error = commitUpdateFile ($_REQUEST['file_id'], $_REQUEST['comment']);
1486 if ($error != '')
135080d8 1487 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1488
135080d8 1489 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1490}
1491
1492function unlinkFile ()
1493{
1494 assertUIntArg ('link_id', __FUNCTION__);
1495 $error = commitUnlinkFile ($_REQUEST['link_id']);
1496
1497 if ($error != '')
135080d8 1498 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1499
135080d8 1500 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1501}
1502
1503function deleteFile ()
1504{
1505 assertUIntArg ('file_id', __FUNCTION__);
1506 $error = commitDeleteFile ($_REQUEST['file_id']);
1507
1508 if ($error != '')
135080d8 1509 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1510
135080d8 1511 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1512}
1513
e673ee24 1514?>