r2697 - listCells(): accept optional parent ID, handle 'rack' realm
[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
b6a7d936 8$msgcode = array();
17c32f4c 9
b6a7d936 10function buildWideRedirectURL ($log, $nextpage = NULL, $nexttab = NULL, $moreArgs = array())
17c32f4c 11{
b6a7d936
DO
12 global $root, $page, $pageno, $tabno;
13 if ($nextpage === NULL)
14 $nextpage = $pageno;
15 if ($nexttab === NULL)
16 $nexttab = $tabno;
17 $url = "${root}?page=${nextpage}&tab=${nexttab}";
18 if (isset ($page[$nextpage]['bypass']))
19 $url .= '&' . $page[$nextpage]['bypass'] . '=' . $_REQUEST[$page[$nextpage]['bypass']];
17c32f4c 20
b6a7d936 21 if (count($moreArgs)>0)
17c32f4c 22 {
b6a7d936
DO
23 foreach($moreArgs as $arg=>$value)
24 {
25 if (gettype($value) == 'array')
26 {
27 foreach ($value as $v)
28 {
29 $url .= '&'.urlencode($arg.'[]').'='.urlencode($v);
30 }
31 }
32 else
33 $url .= '&'.urlencode($arg).'='.urlencode($value);
34 }
17c32f4c 35 }
b6a7d936
DO
36
37 $_SESSION['log'] = $log;
38 return $url;
17c32f4c
DO
39}
40
b6a7d936 41function buildRedirectURL ($callfunc, $status, $args = array(), $nextpage = NULL, $nexttab = NULL)
17c32f4c 42{
b6a7d936
DO
43 global $pageno, $tabno, $msgcode;
44 if ($nextpage === NULL)
45 $nextpage = $pageno;
46 if ($nexttab === NULL)
47 $nexttab = $tabno;
48 return buildWideRedirectURL (oneLiner ($msgcode[$callfunc][$status], $args), $nextpage, $nexttab);
17c32f4c
DO
49}
50
b6a7d936
DO
51$msgcode['addPortForwarding']['OK'] = 2;
52$msgcode['addPortForwarding']['ERR'] = 100;
e673ee24
DO
53function addPortForwarding ()
54{
2c6c7645
DO
55 assertUIntArg ('object_id', __FUNCTION__);
56 assertIPv4Arg ('localip', __FUNCTION__);
57 assertIPv4Arg ('remoteip', __FUNCTION__);
58 assertUIntArg ('localport', __FUNCTION__);
9bea3a8b 59 assertStringArg ('proto', __FUNCTION__);
2c6c7645 60 assertStringArg ('description', __FUNCTION__, TRUE);
103b1e1e 61 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
31fc4157 62 if (empty ($remoteport))
f75eb878 63 $remoteport = $_REQUEST['localport'];
e673ee24 64
9bea3a8b
DO
65 $error = newPortForwarding
66 (
103b1e1e 67 $_REQUEST['object_id'],
9bea3a8b
DO
68 $_REQUEST['localip'],
69 $_REQUEST['localport'],
70 $_REQUEST['remoteip'],
71 $remoteport,
72 $_REQUEST['proto'],
73 $_REQUEST['description']
74 );
e673ee24
DO
75
76 if ($error == '')
135080d8 77 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 78 else
135080d8 79 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
80}
81
b6a7d936
DO
82$msgcode['delPortForwarding']['OK'] = 3;
83$msgcode['delPortForwarding']['ERR'] = 100;
e673ee24
DO
84function delPortForwarding ()
85{
103b1e1e
DO
86 assertUIntArg ('object_id', __FUNCTION__);
87 assertIPv4Arg ('localip', __FUNCTION__);
88 assertIPv4Arg ('remoteip', __FUNCTION__);
89 assertUIntArg ('localport', __FUNCTION__);
90 assertUIntArg ('remoteport', __FUNCTION__);
91 assertStringArg ('proto', __FUNCTION__);
e673ee24 92
103b1e1e
DO
93 $error = deletePortForwarding
94 (
95 $_REQUEST['object_id'],
96 $_REQUEST['localip'],
97 $_REQUEST['localport'],
98 $_REQUEST['remoteip'],
99 $_REQUEST['remoteport'],
100 $_REQUEST['proto']
101 );
e673ee24 102 if ($error == '')
135080d8 103 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 104 else
135080d8 105 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
106}
107
b6a7d936
DO
108$msgcode['updPortForwarding']['OK'] = 4;
109$msgcode['updPortForwarding']['ERR'] = 100;
e673ee24
DO
110function updPortForwarding ()
111{
103b1e1e
DO
112 assertUIntArg ('object_id', __FUNCTION__);
113 assertIPv4Arg ('localip', __FUNCTION__);
114 assertIPv4Arg ('remoteip', __FUNCTION__);
115 assertUIntArg ('localport', __FUNCTION__);
116 assertUIntArg ('remoteport', __FUNCTION__);
117 assertStringArg ('proto', __FUNCTION__);
118 assertStringArg ('description', __FUNCTION__);
e673ee24 119
103b1e1e
DO
120 $error = updatePortForwarding
121 (
122 $_REQUEST['object_id'],
123 $_REQUEST['localip'],
124 $_REQUEST['localport'],
125 $_REQUEST['remoteip'],
126 $_REQUEST['remoteport'],
127 $_REQUEST['proto'],
128 $_REQUEST['description']
129 );
e673ee24 130 if ($error == '')
135080d8 131 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 132 else
135080d8 133 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
134}
135
b6a7d936
DO
136$msgcode['addPortForObject']['OK'] = 5;
137$msgcode['addPortForObject']['ERR1'] = 101;
138$msgcode['addPortForObject']['ERR2'] = 100;
e673ee24
DO
139function addPortForObject ()
140{
103b1e1e
DO
141 assertUIntArg ('object_id', __FUNCTION__);
142 assertStringArg ('port_name', __FUNCTION__, TRUE);
143 if (empty ($_REQUEST['port_name']))
135080d8 144 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e
DO
145 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
146 if ($error != '')
135080d8 147 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 148 else
135080d8 149 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
150}
151
b6a7d936
DO
152$msgcode['editPortForObject']['OK'] = 6;
153$msgcode['editPortForObject']['ERR1'] = 101;
154$msgcode['editPortForObject']['ERR2'] = 100;
e673ee24
DO
155function editPortForObject ()
156{
2c6c7645 157 assertUIntArg ('port_id', __FUNCTION__);
611170af 158 assertUIntArg ('port_type_id', __FUNCTION__);
d0a69ce8 159 // tolerate empty value now to produce custom informative message later
103b1e1e
DO
160 assertStringArg ('name', __FUNCTION__, TRUE);
161 if (empty ($_REQUEST['name']))
135080d8 162 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e 163
e673ee24
DO
164 if (isset ($_REQUEST['reservation_comment']) and !empty ($_REQUEST['reservation_comment']))
165 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
166 else
167 $port_rc = 'NULL';
611170af 168 $error = commitUpdatePort ($_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['port_type_id'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
103b1e1e 169 if ($error != '')
135080d8 170 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 171 else
135080d8 172 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e673ee24
DO
173}
174
b6a7d936
DO
175$msgcode['delPortFromObject']['OK'] = 7;
176$msgcode['delPortFromObject']['ERR'] = 100;
e673ee24
DO
177function delPortFromObject ()
178{
103b1e1e 179 assertUIntArg ('port_id', __FUNCTION__);
103b1e1e 180 $error = delObjectPort ($_REQUEST['port_id']);
e673ee24
DO
181
182 if ($error != '')
135080d8 183 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 184 else
135080d8 185 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
186}
187
b6a7d936
DO
188$msgcode['linkPortForObject']['OK'] = 8;
189$msgcode['linkPortForObject']['ERR'] = 100;
e673ee24
DO
190function linkPortForObject ()
191{
103b1e1e
DO
192 assertUIntArg ('port_id', __FUNCTION__);
193 assertUIntArg ('remote_port_id', __FUNCTION__);
baf37d01
DO
194 assertStringArg ('port_name', __FUNCTION__, TRUE);
195 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
196 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 197
103b1e1e 198 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
e673ee24 199 if ($error != '')
135080d8 200 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 201 else
135080d8 202 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
203}
204
b6a7d936
DO
205$msgcode['unlinkPortForObject']['OK'] = 9;
206$msgcode['unlinkPortForObject']['ERR'] = 100;
e673ee24
DO
207function unlinkPortForObject ()
208{
103b1e1e 209 assertUIntArg ('port_id', __FUNCTION__);
baf37d01
DO
210 assertStringArg ('port_name', __FUNCTION__, TRUE);
211 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
212 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 213
103b1e1e 214 $error = unlinkPort ($_REQUEST['port_id']);
e673ee24 215 if ($error != '')
135080d8 216 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 217 else
135080d8 218 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
219}
220
b6a7d936
DO
221$msgcode['addMultiPorts']['OK'] = 10;
222$msgcode['addMultiPorts']['ERR'] = 123;
e673ee24
DO
223function addMultiPorts ()
224{
2c6c7645
DO
225 assertStringArg ('format', __FUNCTION__);
226 assertStringArg ('input', __FUNCTION__);
227 assertUIntArg ('port_type', __FUNCTION__);
228 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
229 $format = $_REQUEST['format'];
230 $port_type = $_REQUEST['port_type'];
231 $object_id = $_REQUEST['object_id'];
232 // Input lines are escaped, so we have to explode and to chop by 2-char
233 // \n and \r respectively.
234 $lines1 = explode ('\n', $_REQUEST['input']);
235 foreach ($lines1 as $line)
236 {
237 $parts = explode ('\r', $line);
238 reset ($parts);
239 if (empty ($parts[0]))
240 continue;
241 else
242 $lines2[] = rtrim ($parts[0]);
243 }
244 $ports = array();
245 foreach ($lines2 as $line)
246 {
247 switch ($format)
248 {
249 case 'fisxii':
250 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
251 list ($slot, $port) = explode ('/', $words[0]);
252 $ports[] = array
253 (
254 'name' => "e ${slot}/${port}",
255 'l2address' => $words[8],
256 'label' => "slot ${slot} port ${port}"
257 );
258 break;
259 case 'c3600asy':
260 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
261/*
262How Async Lines are Numbered in Cisco 3600 Series Routers
263http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
264
265Understanding 16- and 32-Port Async Network Modules
266http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
267*/
268 $async = $words[0];
269 $slot = floor (($async - 1) / 32);
270 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
271 $cable = $async - $slot * 32 - $octalgroup * 8;
272 $og_label[0] = 'async 0-7';
273 $og_label[1] = 'async 8-15';
274 $og_label[2] = 'async 16-23';
275 $og_label[3] = 'async 24-31';
276 $ports[] = array
277 (
278 'name' => "async ${async}",
279 'l2address' => '',
280 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
281 );
282 break;
283 case 'fiwg':
284 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
285 $ifnumber = $words[0] * 1;
286 $ports[] = array
287 (
288 'name' => "e ${ifnumber}",
289 'l2address' => "${words[8]}",
290 'label' => "${ifnumber}"
291 );
292 break;
351d4dbf 293 case 'ssv1':
5b585342 294 $words = explode (' ', $line);
351d4dbf
DO
295 if (empty ($words[0]) or empty ($words[1]))
296 continue;
297 $ports[] = array
298 (
299 'name' => $words[0],
300 'l2address' => $words[1],
301 'label' => ''
302 );
303 break;
e673ee24 304 default:
135080d8 305 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
306 break;
307 }
308 }
309 // Create ports, if they don't exist.
310 $added_count = $updated_count = $error_count = 0;
311 foreach ($ports as $port)
312 {
313 $port_id = getPortID ($object_id, $port['name']);
314 if ($port_id === NULL)
315 {
316 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
317 if ($result == '')
318 $added_count++;
319 else
320 $error_count++;
321 }
322 else
323 {
611170af 324 $result = commitUpdatePort ($port_id, $port['name'], $port_type, $port['label'], $port['l2address']);
e673ee24
DO
325 if ($result == '')
326 $updated_count++;
327 else
328 $error_count++;
329 }
330 }
135080d8 331 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $updated_count, $error_count));
e673ee24
DO
332}
333
b6a7d936
DO
334$msgcode['updIPv4Allocation']['OK'] = 12;
335$msgcode['updIPv4Allocation']['ERR'] = 100;
b4c1ef87 336function updIPv4Allocation ()
e673ee24 337{
b4c1ef87
DO
338 assertIPv4Arg ('ip', __FUNCTION__);
339 assertUIntArg ('object_id', __FUNCTION__);
340 assertStringArg ('bond_name', __FUNCTION__, TRUE);
341 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 342
b4c1ef87 343 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 344 if ($error != '')
135080d8 345 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 346 else
135080d8 347 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
348}
349
b6a7d936
DO
350$msgcode['delIPv4Allocation']['OK'] = 14;
351$msgcode['delIPv4Allocation']['ERR'] = 100;
b4c1ef87 352function delIPv4Allocation ()
e673ee24 353{
b4c1ef87
DO
354 assertIPv4Arg ('ip', __FUNCTION__);
355 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 356
b4c1ef87 357 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 358 if ($error != '')
135080d8 359 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 360 else
135080d8 361 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
362}
363
b6a7d936
DO
364$msgcode['addIPv4Allocation']['OK'] = 13;
365$msgcode['addIPv4Allocation']['ERR1'] = 170;
366$msgcode['addIPv4Allocation']['ERR2'] = 100;
b4c1ef87 367function addIPv4Allocation ()
e673ee24 368{
b4c1ef87
DO
369 assertIPv4Arg ('ip', __FUNCTION__);
370 assertUIntArg ('object_id', __FUNCTION__);
371 assertStringArg ('bond_name', __FUNCTION__, TRUE);
372 assertStringArg ('bond_type', __FUNCTION__);
da958e52 373
b4c1ef87
DO
374 // Strip masklen.
375 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
2be3fd23 376 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
135080d8 377 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
5222f192 378
b4c1ef87 379 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
5222f192 380 if ($error != '')
135080d8 381 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
53ef3908
DO
382 $address = getIPv4Address ($ip);
383 if ($address['reserved'] == 'yes' or !empty ($address['name']))
b4c1ef87
DO
384 {
385 $release = getConfigVar ('IPV4_AUTO_RELEASE');
386 if ($release >= 1)
387 $address['reserved'] = 'no';
388 if ($release >= 2)
389 $address['name'] = '';
390 updateAddress ($ip, $address['name'], $address['reserved']);
391 }
135080d8 392 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
393}
394
b6a7d936
DO
395$msgcode['addIPv4Prefix']['OK'] = 23;
396$msgcode['addIPv4Prefix']['ERR'] = 100;
397$msgcode['addIPv4Prefix']['ERR1'] = 173;
398$msgcode['addIPv4Prefix']['ERR2'] = 174;
399$msgcode['addIPv4Prefix']['ERR3'] = 175;
400$msgcode['addIPv4Prefix']['ERR4'] = 176;
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
b6a7d936
DO
415$msgcode['delIPv4Prefix']['OK'] = 24;
416$msgcode['delIPv4Prefix']['ERR'] = 100;
42023f03 417function delIPv4Prefix ()
e673ee24 418{
da958e52 419 assertUIntArg ('id', __FUNCTION__);
42023f03 420 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 421 if ($error != '')
135080d8 422 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 423 else
135080d8 424 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
425}
426
b6a7d936
DO
427$msgcode['updIPv4Prefix']['OK'] = 25;
428$msgcode['updIPv4Prefix']['ERR'] = 100;
04d619d0 429function updIPv4Prefix ()
e673ee24 430{
da958e52 431 assertUIntArg ('id', __FUNCTION__);
e4b9a099 432 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 433
da958e52 434 $error = updateRange ($_REQUEST['id'], $_REQUEST['name']);
e673ee24 435 if ($error != '')
135080d8 436 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 437 else
135080d8 438 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
439}
440
b6a7d936
DO
441$msgcode['editAddress']['OK'] = 27;
442$msgcode['editAddress']['ERR'] = 100;
e673ee24
DO
443function editAddress ()
444{
da958e52
DO
445 assertIPv4Arg ('ip', __FUNCTION__);
446 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 447
e673ee24
DO
448 if (isset ($_REQUEST['reserved']))
449 $reserved = $_REQUEST['reserved'];
450 else
451 $reserved = 'off';
da958e52 452 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 453 if ($error != '')
135080d8 454 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 455 else
135080d8 456 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
457}
458
b6a7d936
DO
459$msgcode['createUser']['OK'] = 40;
460$msgcode['createUser']['ERR'] = 102;
cced6b7d 461function createUser ()
e673ee24 462{
2c6c7645
DO
463 assertStringArg ('username', __FUNCTION__);
464 assertStringArg ('realname', __FUNCTION__, TRUE);
465 assertStringArg ('password', __FUNCTION__);
e673ee24 466 $username = $_REQUEST['username'];
93bdb7ba 467 $password = sha1 ($_REQUEST['password']);
e673ee24
DO
468 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
469 if ($result == TRUE)
135080d8 470 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 471 else
135080d8 472 return buildRedirectURL (__FUNCTION__, 'ERR', array ($username));
e673ee24
DO
473}
474
b6a7d936
DO
475$msgcode['updateUser']['OK'] = 39;
476$msgcode['updateUser']['ERR1'] = 103;
477$msgcode['updateUser']['ERR1'] = 104;
cced6b7d 478function updateUser ()
e673ee24 479{
cced6b7d 480 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
481 assertStringArg ('username', __FUNCTION__);
482 assertStringArg ('realname', __FUNCTION__, TRUE);
483 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
484 $username = $_REQUEST['username'];
485 $new_password = $_REQUEST['password'];
b82cce3f 486 if (NULL == ($userinfo = getUserInfo ($_REQUEST['user_id'])))
135080d8 487 return buildRedirectURL (__FUNCTION__, 'ERR1');
e673ee24 488 // Update user password only if provided password is not the same as current password hash.
b82cce3f 489 if ($new_password != $userinfo['user_password_hash'])
93bdb7ba 490 $new_password = sha1 ($new_password);
103b1e1e 491 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
e673ee24 492 if ($result == TRUE)
135080d8 493 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 494 else
135080d8 495 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($username));
e673ee24
DO
496}
497
b6a7d936
DO
498$msgcode['savePortMap']['OK'] = 44;
499$msgcode['savePortMap']['ERR'] = 108;
e673ee24
DO
500// This function find differences in users's submit and PortCompat table
501// and modifies database accordingly.
502function savePortMap ()
503{
e673ee24
DO
504 $ptlist = getPortTypes();
505 $oldCompat = getPortCompat();
506 $newCompat = array();
507 foreach (array_keys ($ptlist) as $leftKey)
508 foreach (array_keys ($ptlist) as $rightKey)
509 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
510 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
511 // Build the new matrix from user's submit and compare it to
512 // the old matrix. Those pairs which appear on
513 // new matrix only, have to be stored in PortCompat table. Those who appear
514 // on the old matrix only, should be removed from PortCompat table.
515 // Those present in both matrices should be skipped.
516 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
517 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
518 $error_count = $success_count = 0;
519 foreach (array_keys ($ptlist) as $type1)
520 foreach (array_keys ($ptlist) as $type2)
521 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
522 switch ($oldCompatTable[$type1][$type2])
523 {
524 case TRUE: // new value is FALSE
525 if (removePortCompat ($type1, $type2) === TRUE)
526 $success_count++;
527 else
528 $error_count++;
529 break;
530 case FALSE: // new value is TRUE
531 if (addPortCompat ($type1, $type2) === TRUE)
532 $success_count++;
533 else
534 $error_count++;
535 break;
536 default:
ebb5441d 537 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
538 break;
539 }
103b1e1e 540 if ($error_count == 0)
135080d8 541 return buildRedirectURL (__FUNCTION__, 'OK', array ($error_count, $success_count));
103b1e1e 542 else
135080d8 543 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error_count, $success_count));
e673ee24
DO
544}
545
b6a7d936
DO
546$msgcode['updateDictionary']['OK'] = 51;
547$msgcode['updateDictionary']['ERR'] = 109;
e673ee24
DO
548function updateDictionary ()
549{
2c6c7645
DO
550 assertUIntArg ('chapter_no', __FUNCTION__);
551 assertUIntArg ('dict_key', __FUNCTION__);
552 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 553 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
135080d8 554 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 555 else
135080d8 556 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
557}
558
b6a7d936
DO
559$msgcode['supplementDictionary']['OK'] = 52;
560$msgcode['supplementDictionary']['ERR'] = 110;
e673ee24
DO
561function supplementDictionary ()
562{
2c6c7645
DO
563 assertUIntArg ('chapter_no', __FUNCTION__);
564 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 565 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
135080d8 566 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 567 else
135080d8 568 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
569}
570
b6a7d936
DO
571$msgcode['reduceDictionary']['OK'] = 50;
572$msgcode['reduceDictionary']['ERR'] = 111;
e673ee24
DO
573function reduceDictionary ()
574{
2c6c7645
DO
575 assertUIntArg ('chapter_no', __FUNCTION__);
576 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24 577 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
135080d8 578 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 579 else
135080d8 580 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
581}
582
b6a7d936
DO
583$msgcode['addChapter']['OK'] = 55;
584$msgcode['addChapter']['ERR'] = 112;
e673ee24
DO
585function addChapter ()
586{
2c6c7645 587 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 588 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
135080d8 589 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 590 else
135080d8 591 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
592}
593
b6a7d936
DO
594$msgcode['updateChapter']['OK'] = 54;
595$msgcode['updateChapter']['ERR'] = 113;
e673ee24
DO
596function updateChapter ()
597{
2c6c7645
DO
598 assertUIntArg ('chapter_no', __FUNCTION__);
599 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 600 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
135080d8 601 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 602 else
135080d8 603 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
604}
605
b6a7d936
DO
606$msgcode['delChapter']['OK'] = 53;
607$msgcode['delChapter']['ERR'] = 114;
e673ee24
DO
608function delChapter ()
609{
2c6c7645 610 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 611 if (commitDeleteChapter ($_REQUEST['chapter_no']))
135080d8 612 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 613 else
135080d8 614 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
615}
616
b6a7d936
DO
617$msgcode['changeAttribute']['OK'] = 46;
618$msgcode['changeAttribute']['ERR'] = 115;
e673ee24
DO
619function changeAttribute ()
620{
2c6c7645
DO
621 assertUIntArg ('attr_id', __FUNCTION__);
622 assertStringArg ('attr_name', __FUNCTION__);
e673ee24 623 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
135080d8 624 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 625 else
135080d8 626 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
627}
628
b6a7d936
DO
629$msgcode['createAttribute']['OK'] = 45;
630$msgcode['createAttribute']['ERR'] = 116;
e673ee24
DO
631function createAttribute ()
632{
2c6c7645
DO
633 assertStringArg ('attr_name', __FUNCTION__);
634 assertStringArg ('attr_type', __FUNCTION__);
e673ee24 635 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
135080d8 636 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
e673ee24 637 else
135080d8 638 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
639}
640
b6a7d936
DO
641$msgcode['deleteAttribute']['OK'] = 47;
642$msgcode['deleteAttribute']['ERR'] = 117;
e673ee24
DO
643function deleteAttribute ()
644{
2c6c7645 645 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24 646 if (commitDeleteAttribute ($_REQUEST['attr_id']))
135080d8 647 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 648 else
135080d8 649 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
650}
651
b6a7d936
DO
652$msgcode['supplementAttrMap']['OK'] = 48;
653$msgcode['supplementAttrMap']['ERR'] = 118;
e673ee24
DO
654function supplementAttrMap ()
655{
2c6c7645
DO
656 assertUIntArg ('attr_id', __FUNCTION__);
657 assertUIntArg ('objtype_id', __FUNCTION__);
658 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 659 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
135080d8 660 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 661 else
135080d8 662 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
663}
664
b6a7d936
DO
665$msgcode['reduceAttrMap']['OK'] = 49;
666$msgcode['reduceAttrMap']['ERR'] = 119;
e673ee24
DO
667function reduceAttrMap ()
668{
2c6c7645
DO
669 assertUIntArg ('attr_id', __FUNCTION__);
670 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24 671 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
135080d8 672 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 673 else
135080d8 674 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
675}
676
b6a7d936
DO
677$msgcode['clearSticker']['OK'] = 15;
678$msgcode['clearSticker']['ERR'] = 120;
22bb04da 679function clearSticker ()
e673ee24 680{
2c6c7645
DO
681 assertUIntArg ('attr_id', __FUNCTION__);
682 assertUIntArg ('object_id', __FUNCTION__);
22bb04da 683 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
135080d8 684 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 685 else
135080d8 686 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
687}
688
4fbb5a00
DY
689function updateObjectAllocation ()
690{
691 assertUIntArg ('object_id', __FUNCTION__);
692
693 $is_submit = isset ($_REQUEST['got_atoms']);
694 $is_update = isset ($_REQUEST['rackmulti'][0]);
695
4fbb5a00
DY
696 if ($is_submit)
697 {
698 $object_id = $_REQUEST['object_id'];
699 $workingRacksData = getResidentRacksData ($object_id);
700 if ($workingRacksData === NULL)
701 {
702 print_r ($workingRacksData);
703 showError ('getResidentRacksData() failed', __FUNCTION__);
704 return;
705 }
2135fd83 706 $workingRacksData = array();
4fbb5a00
DY
707 foreach ($_REQUEST['rackmulti'] as $cand_id)
708 {
709 if (!isset ($workingRacksData[$cand_id]))
710 {
711 $rackData = getRackData ($cand_id);
712 if ($rackData == NULL)
713 {
714 showError ('getRackData() failed', __FUNCTION__);
715 return;
716 }
717 $workingRacksData[$cand_id] = $rackData;
718 }
719 }
2135fd83
DY
720
721 foreach ($workingRacksData as &$rd)
722 applyObjectMountMask ($rd, $object_id);
4fbb5a00
DY
723
724 $oldMolecule = getMoleculeForObject ($object_id);
725 $worldchanged = FALSE;
726 $log = array();
727 foreach ($workingRacksData as $rack_id => $rackData)
728 {
729 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
730 $log[] = $logrecord;
731 }
732 return buildWideRedirectURL($log);
733
734 }
735 else
736 {
2135fd83
DY
737 unset($_GET['page']);
738 unset($_GET['tab']);
739 unset($_GET['op']);
740 unset($_POST['page']);
741 unset($_POST['tab']);
742 unset($_POST['op']);
743 return buildWideRedirectURL(array(), NULL, NULL, array_merge($_GET, $_POST));
4fbb5a00
DY
744 }
745
746}
747
b6a7d936
DO
748$msgcode['updateObject']['OK'] = 16;
749$msgcode['updateObject']['ERR'] = 121;
22bb04da
DO
750function updateObject ()
751{
cdd3efe9 752 assertUIntArg ('num_attrs', __FUNCTION__);
22bb04da
DO
753 assertUIntArg ('object_id', __FUNCTION__);
754 assertUIntArg ('object_type_id', __FUNCTION__);
755 assertStringArg ('object_name', __FUNCTION__, TRUE);
756 assertStringArg ('object_label', __FUNCTION__, TRUE);
757 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
758 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
759 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
760 $has_problems = 'yes';
761 else
762 $has_problems = 'no';
763
764 if (commitUpdateObject (
765 $_REQUEST['object_id'],
766 $_REQUEST['object_name'],
767 $_REQUEST['object_label'],
768 $_REQUEST['object_barcode'],
769 $_REQUEST['object_type_id'],
770 $has_problems,
771 $_REQUEST['object_asset_no'],
772 $_REQUEST['object_comment']
773 ) !== TRUE)
135080d8 774 return buildRedirectURL (__FUNCTION__, 'ERR');
22bb04da 775
cdd3efe9 776 // Update optional attributes
f59e8cf5 777 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a 778 $result = array();
103b1e1e 779 for ($i = 0; $i < $_REQUEST['num_attrs']; $i++)
0dfb8a2a 780 {
2c6c7645 781 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
782 $attr_id = $_REQUEST["${i}_attr_id"];
783
784 // Field is empty, delete attribute and move on.
785 if (empty($_REQUEST["${i}_value"]))
786 {
103b1e1e 787 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
788 continue;
789 }
790
791 // The value could be uint/float, but we don't know ATM. Let SQL
792 // server check this and complain.
2c6c7645 793 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
794 $value = $_REQUEST["${i}_value"];
795 switch ($oldvalues[$attr_id]['type'])
796 {
797 case 'uint':
798 case 'float':
799 case 'string':
800 $oldvalue = $oldvalues[$attr_id]['value'];
801 break;
802 case 'dict':
803 $oldvalue = $oldvalues[$attr_id]['key'];
804 break;
805 default:
61e269b5 806 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
807 die;
808 }
809 if ($value == $oldvalue)
810 continue;
811
812 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 813 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a 814 }
103b1e1e 815 if (in_array (FALSE, $result))
135080d8 816 return buildRedirectURL (__FUNCTION__, 'ERR');
0dfb8a2a 817
cdd3efe9
AD
818 // Invalidate thumb cache of all racks objects could occupy.
819 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
820 resetThumbCache ($rack_id);
821
135080d8 822 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
823}
824
42d73cbf
DY
825
826function addMultipleObjects()
827{
7abce0b7 828 $log = emptyLog();
42d73cbf 829 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
830 $max = getConfigVar ('MASSCOUNT');
831 for ($i = 0; $i < $max; $i++)
832 {
833 if (!isset ($_REQUEST["${i}_object_type_id"]))
834 {
57777e4a 835 $log = mergeLogs ($log, oneLiner (184, array ($i + 1)));
42d73cbf
DY
836 break;
837 }
838 assertUIntArg ("${i}_object_type_id", __FUNCTION__, TRUE);
839 assertStringArg ("${i}_object_name", __FUNCTION__, TRUE);
840 assertStringArg ("${i}_object_label", __FUNCTION__, TRUE);
841 assertStringArg ("${i}_object_asset_no", __FUNCTION__, TRUE);
842 assertStringArg ("${i}_object_barcode", __FUNCTION__, TRUE);
7abce0b7 843 $name = $_REQUEST["${i}_object_name"];
42d73cbf
DY
844
845 // It's better to skip silently, than to print a notice.
7abce0b7 846 if ($_REQUEST["${i}_object_type_id"] == 0)
42d73cbf 847 continue;
7abce0b7
DO
848 if (commitAddObject
849 (
850 $name,
851 $_REQUEST["${i}_object_label"],
852 $_REQUEST["${i}_object_barcode"],
853 $_REQUEST["${i}_object_type_id"],
854 $_REQUEST["${i}_object_asset_no"],
855 $taglist
856 ) === TRUE)
57777e4a 857 $log = mergeLogs ($log, oneLiner (80, array ($name)));
42d73cbf 858 else
57777e4a 859 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 860 }
7abce0b7 861 return buildWideRedirectURL ($log);
42d73cbf
DY
862}
863
864function addLotOfObjects()
865{
7abce0b7 866 $log = emptyLog();
42d73cbf 867 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
868 assertUIntArg ('global_type_id', __FUNCTION__, TRUE);
869 assertStringArg ('namelist', __FUNCTION__, TRUE);
870 $global_type_id = $_REQUEST['global_type_id'];
7abce0b7 871 if ($global_type_id == 0 or empty ($_REQUEST['namelist']))
57777e4a 872 $log = mergeLogs ($log, oneLiner (186));
42d73cbf
DY
873 else
874 {
875 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
876 $names1 = explode ('\n', $_REQUEST['namelist']);
877 $names2 = array();
878 foreach ($names1 as $line)
879 {
880 $parts = explode ('\r', $line);
881 reset ($parts);
882 if (empty ($parts[0]))
883 continue;
884 else
885 $names2[] = rtrim ($parts[0]);
886 }
57777e4a
DO
887 foreach ($names2 as $name)
888 if (commitAddObject ($name, '', '', $global_type_id, '', $taglist) === TRUE)
889 $log = mergeLogs ($log, oneLiner (80, array ($name)));
42d73cbf 890 else
57777e4a 891 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 892 }
7abce0b7 893 return buildWideRedirectURL ($log);
42d73cbf
DY
894}
895
b6a7d936
DO
896$msgcode['deleteObject']['OK'] = 76;
897$msgcode['deleteObject']['ERR'] = 100;
52b34485
AD
898function deleteObject ()
899{
900 assertUIntArg ('object_id', __FUNCTION__);
c78a40ec
DO
901 if (NULL === ($oinfo = getObjectInfo ($_REQUEST['object_id'])))
902 return buildRedirectURL (__FUNCTION__, 'ERR', array ('object not found'));
903
52b34485
AD
904 $error = commitDeleteObject ($_REQUEST['object_id']);
905
906 if ($error != '')
135080d8 907 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
52b34485 908
c78a40ec 909 return buildRedirectURL (__FUNCTION__, 'OK', array ($oinfo['dname']));
52b34485
AD
910}
911
b6a7d936
DO
912$msgcode['useupPort']['OK'] = 11;
913$msgcode['useupPort']['ERR'] = 124;
e673ee24
DO
914function useupPort ()
915{
2c6c7645 916 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 917 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
135080d8 918 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 919 else
135080d8 920 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
921}
922
b6a7d936
DO
923$msgcode['updateUI']['OK'] = 56;
924$msgcode['updateUI']['ERR'] = 125;
4fe32e78
DY
925function updateUI ()
926{
2c6c7645 927 assertUIntArg ('num_vars', __FUNCTION__);
c461c579 928 $error = '';
4fe32e78 929
103b1e1e 930 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 931 {
2c6c7645
DO
932 assertStringArg ("${i}_varname", __FUNCTION__);
933 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 934 $varname = $_REQUEST["${i}_varname"];
4fe32e78 935 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 936
c461c579
DO
937 // If form value = value in DB, don't bother updating DB
938 if ($varvalue == getConfigVar ($varname))
4fe32e78
DY
939 continue;
940
941 // Note if the queries succeed or not, it determines which page they see.
c461c579
DO
942 $error = setConfigVar ($varname, $varvalue, TRUE);
943 if (!empty ($error))
944 break;
4fe32e78
DY
945 }
946
c461c579 947 if ($error != '')
135080d8 948 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
4fe32e78 949
135080d8 950 return buildRedirectURL (__FUNCTION__, 'OK');
4fe32e78
DY
951}
952
b6a7d936 953$msgcode['resetUIConfig']['OK'] = 57;
b07f617c
DO
954function resetUIConfig()
955{
1f86e996 956 setConfigVar ('default_port_type','24');
b07f617c
DO
957 setConfigVar ('MASSCOUNT','15');
958 setConfigVar ('MAXSELSIZE','30');
b07f617c
DO
959 setConfigVar ('ROW_SCALE','2');
960 setConfigVar ('PORTS_PER_ROW','12');
961 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
962 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
b5ce46aa
DO
963 setConfigVar ('DEFAULT_SLB_VS_PORT','');
964 setConfigVar ('DEFAULT_SLB_RS_PORT','');
72281138 965 setConfigVar ('DETECT_URLS','no');
4b8d413e 966 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 967 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 968 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
969 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
970 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
971 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 972 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 973 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df 974 setConfigVar ('SHOW_LAST_TAB', 'no');
f0ed1181 975 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 976 setConfigVar ('TREE_THRESHOLD', '25');
2be3fd23 977 setConfigVar ('IPV4_JAYWALK', 'no');
9318d2ef 978 setConfigVar ('ADDNEW_AT_TOP', 'yes');
64347dcf 979 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
37e59768
DO
980 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
981 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
982 setConfigVar ('PREVIEW_TEXT_COLS', '80');
983 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
f3d274bf 984 setConfigVar ('VENDOR_SIEVE', '');
35bf153d
DO
985 setConfigVar ('IPV4LB_LISTSRC', '{$typeid_4}');
986 setConfigVar ('IPV4OBJ_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_12} or {$typeid_445} or {$typeid_447}');
987 setConfigVar ('IPV4NAT_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
988 setConfigVar ('ASSETWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
989 setConfigVar ('NAMEWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
f5883ec1 990 setConfigVar ('RACKS_PER_ROW','12');
590e1281 991 setConfigVar ('FILTER_PREDICATE_SIEVE','');
23cdc7e9
DO
992 setConfigVar ('FILTER_DEFAULT_ANDOR','or');
993 setConfigVar ('FILTER_SUGGEST_ANDOR','yes');
994 setConfigVar ('FILTER_SUGGEST_TAGS','yes');
995 setConfigVar ('FILTER_SUGGEST_PREDICATES','yes');
996 setConfigVar ('FILTER_SUGGEST_EXTRA','no');
135080d8 997 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
998}
999
b6a7d936
DO
1000$msgcode['addRealServer']['OK'] = 34;
1001$msgcode['addRealServer']['ERR'] = 126;
732e4578 1002// Add single record.
ca461127
DO
1003function addRealServer ()
1004{
2c6c7645
DO
1005 assertUIntArg ('pool_id', __FUNCTION__);
1006 assertIPv4Arg ('remoteip', __FUNCTION__);
79a9edb4 1007 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 1008 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1009 if (!addRStoRSPool (
1010 $_REQUEST['pool_id'],
1011 $_REQUEST['remoteip'],
1012 $_REQUEST['rsport'],
1013 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1014 $_REQUEST['rsconfig']
1015 ))
135080d8 1016 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1017 else
135080d8 1018 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1019}
1020
b6a7d936
DO
1021$msgcode['addRealServers']['OK'] = 37;
1022$msgcode['addRealServers']['ERR1'] = 131;
1023$msgcode['addRealServers']['ERR2'] = 127;
732e4578
DO
1024// Parse textarea submitted and try adding a real server for each line.
1025function addRealServers ()
1026{
cbadbb38 1027 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
1028 assertStringArg ('format', __FUNCTION__);
1029 assertStringArg ('rawtext', __FUNCTION__);
732e4578
DO
1030 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1031 $ngood = $nbad = 0;
1032 $rsconfig = '';
1033 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1034 foreach (explode ('\n', $rawtext) as $line)
1035 {
1036 if (empty ($line))
1037 continue;
1038 $match = array ();
1039 switch ($_REQUEST['format'])
1040 {
1041 case 'ipvs_2': // address and port only
1042 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1043 continue;
103b1e1e 1044 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1045 $ngood++;
1046 else
1047 $nbad++;
1048 break;
1049 case 'ipvs_3': // address, port and weight
1050 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1051 continue;
103b1e1e 1052 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1053 $ngood++;
1054 else
1055 $nbad++;
1056 break;
e69c2aa3
DO
1057 case 'ssv_2': // IP address and port
1058 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1059 continue;
103b1e1e 1060 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1061 $ngood++;
1062 else
1063 $nbad++;
1064 break;
948c37e3
DO
1065 case 'ssv_1': // IP address
1066 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1067 continue;
1068 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1069 $ngood++;
1070 else
1071 $nbad++;
1072 break;
732e4578 1073 default:
135080d8 1074 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1075 break;
1076 }
1077 }
1078 if ($nbad == 0 and $ngood > 0)
135080d8 1079 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1080 else
135080d8 1081 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1082}
1083
b6a7d936
DO
1084$msgcode['addVService']['OK'] = 28;
1085$msgcode['addVService']['ERR1'] = 132;
1086$msgcode['addVService']['ERR2'] = 100;
d6517a21
DO
1087function addVService ()
1088{
2c6c7645
DO
1089 assertIPv4Arg ('vip', __FUNCTION__);
1090 assertUIntArg ('vport', __FUNCTION__);
1091 assertStringArg ('proto', __FUNCTION__);
103b1e1e 1092 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1093 return buildRedirectURL (__FUNCTION__, 'ERR1');
2c6c7645
DO
1094 assertStringArg ('name', __FUNCTION__, TRUE);
1095 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1096 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1097 $error = commitCreateVS
1098 (
103b1e1e
DO
1099 $_REQUEST['vip'],
1100 $_REQUEST['vport'],
7320b661 1101 $_REQUEST['proto'],
103b1e1e
DO
1102 $_REQUEST['name'],
1103 $_REQUEST['vsconfig'],
c63a8d6e
DO
1104 $_REQUEST['rsconfig'],
1105 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1106 );
1107 if ($error != '')
135080d8 1108 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1109 else
135080d8 1110 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1111}
1112
b6a7d936
DO
1113$msgcode['deleteRealServer']['OK'] = 35;
1114$msgcode['deleteRealServer']['ERR'] = 128;
fb1c4a54
DO
1115function deleteRealServer ()
1116{
2c6c7645 1117 assertUIntArg ('id', __FUNCTION__);
d6517a21 1118 if (!commitDeleteRS ($_REQUEST['id']))
135080d8 1119 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1120 else
135080d8 1121 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1122}
1123
b6a7d936
DO
1124$msgcode['deleteLoadBalancer']['OK'] = 19;
1125$msgcode['deleteLoadBalancer']['ERR'] = 129;
3241551e
DO
1126function deleteLoadBalancer ()
1127{
2c6c7645
DO
1128 assertUIntArg ('object_id', __FUNCTION__);
1129 assertUIntArg ('pool_id', __FUNCTION__);
1130 assertUIntArg ('vs_id', __FUNCTION__);
103b1e1e
DO
1131 if (!commitDeleteLB (
1132 $_REQUEST['object_id'],
1133 $_REQUEST['pool_id'],
1134 $_REQUEST['vs_id']
1135 ))
135080d8 1136 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1137 else
135080d8 1138 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1139}
1140
b6a7d936
DO
1141$msgcode['deleteVService']['OK'] = 29;
1142$msgcode['deleteVService']['ERR'] = 130;
d6517a21
DO
1143function deleteVService ()
1144{
e02e1941
DO
1145 assertUIntArg ('vs_id', __FUNCTION__);
1146 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1147 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1148 else
135080d8 1149 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1150}
1151
b6a7d936
DO
1152$msgcode['updateRealServer']['OK'] = 36;
1153$msgcode['updateRealServer']['ERR'] = 133;
fb1c4a54
DO
1154function updateRealServer ()
1155{
2c6c7645
DO
1156 assertUIntArg ('rs_id', __FUNCTION__);
1157 assertIPv4Arg ('rsip', __FUNCTION__);
79a9edb4 1158 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 1159 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1160 if (!commitUpdateRS (
1161 $_REQUEST['rs_id'],
1162 $_REQUEST['rsip'],
1163 $_REQUEST['rsport'],
1164 $_REQUEST['rsconfig']
1165 ))
135080d8 1166 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1167 else
135080d8 1168 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1169}
1170
b6a7d936
DO
1171$msgcode['updateLoadBalancer']['OK'] = 20;
1172$msgcode['updateLoadBalancer']['ERR'] = 134;
c1ca768c 1173function updateLoadBalancer ()
3241551e 1174{
2c6c7645
DO
1175 assertUIntArg ('object_id', __FUNCTION__);
1176 assertUIntArg ('pool_id', __FUNCTION__);
1177 assertUIntArg ('vs_id', __FUNCTION__);
1178 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1179 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1180 if (!commitUpdateLB (
1181 $_REQUEST['object_id'],
1182 $_REQUEST['pool_id'],
1183 $_REQUEST['vs_id'],
1184 $_REQUEST['vsconfig'],
1185 $_REQUEST['rsconfig']
1186 ))
135080d8 1187 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1188 else
135080d8 1189 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1190}
1191
b6a7d936
DO
1192$msgcode['updateVService']['OK'] = 30;
1193$msgcode['updateVService']['ERR'] = 135;
d6517a21
DO
1194function updateVService ()
1195{
e02e1941 1196 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
1197 assertIPv4Arg ('vip', __FUNCTION__);
1198 assertUIntArg ('vport', __FUNCTION__);
1199 assertStringArg ('proto', __FUNCTION__);
1200 assertStringArg ('name', __FUNCTION__, TRUE);
1201 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1202 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1203 if (!commitUpdateVS (
1204 $_REQUEST['vs_id'],
1205 $_REQUEST['vip'],
1206 $_REQUEST['vport'],
1207 $_REQUEST['proto'],
1208 $_REQUEST['name'],
1209 $_REQUEST['vsconfig'],
1210 $_REQUEST['rsconfig']
1211 ))
135080d8 1212 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1213 else
135080d8 1214 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1215}
1216
b6a7d936
DO
1217$msgcode['addLoadBalancer']['OK'] = 18;
1218$msgcode['addLoadBalancer']['ERR'] = 137;
3241551e
DO
1219function addLoadBalancer ()
1220{
2c6c7645
DO
1221 assertUIntArg ('pool_id', __FUNCTION__);
1222 assertUIntArg ('object_id', __FUNCTION__);
1223 assertUIntArg ('vs_id', __FUNCTION__);
1224 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1225 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1226 if (!addLBtoRSPool (
1227 $_REQUEST['pool_id'],
1228 $_REQUEST['object_id'],
1229 $_REQUEST['vs_id'],
1230 $_REQUEST['vsconfig'],
1231 $_REQUEST['rsconfig']
1232 ))
135080d8 1233 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1234 else
135080d8 1235 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1236}
1237
b6a7d936
DO
1238$msgcode['addRSPool']['OK'] = 31;
1239$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1240function addRSPool ()
1241{
2c6c7645
DO
1242 assertStringArg ('name', __FUNCTION__, TRUE);
1243 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1244 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1245 $error = commitCreateRSPool
1246 (
103b1e1e
DO
1247 $_REQUEST['name'],
1248 $_REQUEST['vsconfig'],
c63a8d6e
DO
1249 $_REQUEST['rsconfig'],
1250 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1251 );
1252 if ($error != '')
135080d8 1253 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1254 else
135080d8 1255 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1256}
1257
b6a7d936
DO
1258$msgcode['deleteRSPool']['OK'] = 32;
1259$msgcode['deleteRSPool']['ERR'] = 138;
5ad76f01
DO
1260function deleteRSPool ()
1261{
2c6c7645 1262 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1263 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
135080d8 1264 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1265 else
135080d8 1266 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1267}
1268
b6a7d936
DO
1269$msgcode['updateRSPool']['OK'] = 33;
1270$msgcode['updateRSPool']['ERR'] = 139;
5ad76f01
DO
1271function updateRSPool ()
1272{
2c6c7645
DO
1273 assertUIntArg ('pool_id', __FUNCTION__);
1274 assertStringArg ('name', __FUNCTION__, TRUE);
1275 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1276 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1277 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
135080d8 1278 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1279 else
135080d8 1280 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1281}
1282
b6a7d936
DO
1283$msgcode['updateRSInService']['OK'] = 38;
1284$msgcode['updateRSInService']['ERR'] = 140;
1f7d18fa
DO
1285function updateRSInService ()
1286{
2c6c7645 1287 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1288 $pool_id = $_REQUEST['pool_id'];
1f7d18fa
DO
1289 $orig = getRSPoolInfo ($pool_id);
1290 $nbad = $ngood = 0;
1291 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1292 {
1293 $rs_id = $_REQUEST["rsid_${i}"];
1294 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1295 $newval = 'yes';
1296 else
1297 $newval = 'no';
1298 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1299 {
1300 if (commitSetInService ($rs_id, $newval))
1301 $ngood++;
1302 else
1303 $nbad++;
1304 }
1305 }
1306 if (!$nbad)
135080d8 1307 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1308 else
135080d8 1309 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1310}
1311
b6a7d936
DO
1312$msgcode['importPTRData']['OK'] = 26;
1313$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1314// FIXME: check, that each submitted address belongs to the prefix we
1315// are operating on.
03eb5209
DO
1316function importPTRData ()
1317{
2c6c7645 1318 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209 1319 $nbad = $ngood = 0;
03eb5209
DO
1320 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1321 {
3f3bd41e 1322 $inputname = "import_${i}";
03eb5209
DO
1323 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1324 continue;
2c6c7645
DO
1325 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1326 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1327 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1328 // Non-existent addresses will not have this argument set in request.
1329 $rsvd = 'no';
03eb5209
DO
1330 if ($_REQUEST["rsvd_${i}"] == 'yes')
1331 $rsvd = 'yes';
3f3bd41e 1332 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1333 $ngood++;
1334 else
1335 $nbad++;
1336 }
1337 if (!$nbad)
135080d8 1338 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1339 else
135080d8 1340 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1341}
1342
b6a7d936
DO
1343$msgcode['generateAutoPorts']['OK'] = 21;
1344$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1345function generateAutoPorts ()
1346{
103b1e1e 1347 global $pageno;
2c6c7645 1348 assertUIntArg ('object_id', __FUNCTION__);
7ddbcf59 1349 $info = getObjectInfo ($_REQUEST['object_id'], FALSE);
f3f0161f 1350 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1351 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1352 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1353 else
135080d8 1354 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1355}
1356
b6a7d936
DO
1357$msgcode['saveEntityTags']['OK'] = 22;
1358$msgcode['saveEntityTags']['ERR1'] = 143;
1359$msgcode['saveEntityTags']['ERR2'] = 187;
24cbe8af 1360// Filter out implicit tags before storing the new tag set.
3355ca56 1361function saveEntityTags ()
24cbe8af 1362{
be2ae2a2
DO
1363 global $explicit_tags, $implicit_tags, $page, $pageno, $etype_by_pageno;
1364 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
9a61c175 1365 return buildRedirectURL (__FUNCTION__, 'ERR2', array (__FUNCTION__));
be2ae2a2 1366 $realm = $etype_by_pageno[$pageno];
3355ca56 1367 $bypass = $page[$pageno]['bypass'];
2c6c7645 1368 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1369 $entity_id = $_REQUEST[$bypass];
e04931c8 1370 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1371 // Build a chain from the submitted data, minimize it,
ab379543 1372 // then wipe existing records and store the new set instead.
01b6b4d6 1373 destroyTagsForEntity ($realm, $entity_id);
6e49bd1f 1374 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1375 $n_succeeds = $n_errors = 0;
6e49bd1f 1376 foreach ($newchain as $taginfo)
eb6ea26f 1377 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1378 $n_succeeds++;
1379 else
1380 $n_errors++;
ab379543 1381 if ($n_errors)
9a61c175 1382 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1383 else
135080d8 1384 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1385}
1386
b6a7d936
DO
1387$msgcode['destroyTag']['OK'] = 58;
1388$msgcode['destroyTag']['ERR1'] = 183;
1389$msgcode['destroyTag']['ERR2'] = 144;
fe7044ad
DO
1390function destroyTag ()
1391{
49fb1027 1392 assertUIntArg ('tag_id', __FUNCTION__);
c615a655
DO
1393 global $taglist;
1394 if (!isset ($taglist[$_REQUEST['tag_id']]))
1395 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['tag_id']));
49fb1027 1396 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
c615a655 1397 return buildRedirectURL (__FUNCTION__, 'OK', array ($taglist[$_REQUEST['tag_id']]['tag']));
fe7044ad 1398 else
c615a655 1399 return buildRedirectURL (__FUNCTION__, 'ERR2');
fe7044ad
DO
1400}
1401
b6a7d936
DO
1402$msgcode['createTag']['OK'] = 59;
1403$msgcode['createTag']['ERR1'] = 145;
1404$msgcode['createTag']['ERR3'] = 147;
fe7044ad
DO
1405function createTag ()
1406{
49fb1027 1407 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1408 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1409 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1410 if (!validTagName ($tagname))
135080d8 1411 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
fe7044ad
DO
1412 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1413 $parent_id = 'NULL';
1414 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
135080d8 1415 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
fe7044ad 1416 else
135080d8 1417 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
fe7044ad
DO
1418}
1419
b6a7d936
DO
1420$msgcode['updateTag']['OK'] = 60;
1421$msgcode['updateTag']['ERR1'] = 145;
1422$msgcode['updateTag']['ERR2'] = 148;
49fb1027
DO
1423function updateTag ()
1424{
49fb1027
DO
1425 assertUIntArg ('tag_id', __FUNCTION__);
1426 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1427 assertStringArg ('tag_name', __FUNCTION__);
1428 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1429 if (!validTagName ($tagname))
135080d8 1430 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1431 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1432 $parent_id = 'NULL';
1433 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
135080d8 1434 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
590e1281
DO
1435 // Use old name in the message, cause update failed.
1436 global $taglist;
1437 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag'], $ret));
49fb1027
DO
1438}
1439
b6a7d936
DO
1440$msgcode['rollTags']['OK'] = 67;
1441$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1442function rollTags ()
1443{
eb6ea26f
DO
1444 assertUIntArg ('row_id', __FUNCTION__);
1445 assertStringArg ('sum', __FUNCTION__, TRUE);
1446 assertUIntArg ('realsum', __FUNCTION__);
eb6ea26f 1447 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1448 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1449 // Even if the user requested an empty tag list, don't bail out, but process existing
1450 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1451 // have its chains refined to "normal" form.
1452 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1453 $n_ok = 0;
1454 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1455 // filter out the same tag again and again. It will have own noise to cancel.
1456 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
a8efc03e 1457 foreach (array_keys (listCells ('rack', $_REQUEST['row_id'])) as $rack_id)
abef7149 1458 {
a8efc03e 1459 if (rebuildTagChainForEntity ('rack', $rack_id, $extrachain))
abef7149 1460 $n_ok++;
a8efc03e 1461 foreach (stuffInRackspace (getRackData ($rack_id)) as $object_id)
abef7149
DO
1462 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1463 $n_ok++;
1464 }
135080d8 1465 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1466}
1467
b6a7d936
DO
1468$msgcode['changeMyPassword']['OK'] = 61;
1469$msgcode['changeMyPassword']['ERR1'] = 150;
1470$msgcode['changeMyPassword']['ERR2'] = 151;
1471$msgcode['changeMyPassword']['ERR3'] = 152;
1472$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1473function changeMyPassword ()
cced6b7d 1474{
b82cce3f 1475 global $remote_username, $user_auth_src;
204284ba 1476 if ($user_auth_src != 'database')
135080d8 1477 return buildRedirectURL (__FUNCTION__, 'ERR1');
0ac7e4b3
DO
1478 assertStringArg ('oldpassword', __FUNCTION__);
1479 assertStringArg ('newpassword1', __FUNCTION__);
1480 assertStringArg ('newpassword2', __FUNCTION__);
b82cce3f
DO
1481 $remote_userid = getUserIDByUsername ($remote_username);
1482 $userinfo = getUserInfo ($remote_userid);
1483 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1484 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1485 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1486 return buildRedirectURL (__FUNCTION__, 'ERR3');
b82cce3f 1487 if (commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1488 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1489 else
135080d8 1490 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1491}
1492
b6a7d936
DO
1493$msgcode['saveRackCode']['OK'] = 43;
1494$msgcode['saveRackCode']['ERR1'] = 154;
1495$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1496function saveRackCode ()
1497{
cced6b7d 1498 assertStringArg ('rackcode');
e6a4adb9
DO
1499 // For the test to succeed, unescape LFs, strip CRs.
1500 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1501 $parseTree = getRackCode ($newcode);
1502 if ($parseTree['result'] != 'ACK')
135080d8 1503 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
4a6a28f1 1504 saveScript ('RackCodeCache', '');
cf25e649 1505 if (saveScript ('RackCode', $newcode))
135080d8 1506 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1507 else
135080d8 1508 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1509}
1510
b6a7d936 1511$msgcode['setPortVLAN']['ERR1'] = 156;
46f92ff7
DO
1512// This handler's context is pre-built, but not authorized. It is assumed, that the
1513// handler will take existing context and before each commit check authorization
1514// on the base chain plus necessary context added.
1515function setPortVLAN ()
1516{
1517 assertUIntArg ('portcount', __FUNCTION__);
1518 $data = getSwitchVLANs ($_REQUEST['object_id']);
1519 if ($data === NULL)
135080d8 1520 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1521 list ($vlanlist, $portlist) = $data;
1522 // Here we just build up 1 set command for the gateway with all of the ports
1523 // included. The gateway is expected to filter unnecessary changes silently
1524 // and to provide a list of responses with either error or success message
1525 // for each of the rest.
1526 $nports = $_REQUEST['portcount'];
1527 $prefix = 'set ';
db55cf54 1528 $log = emptyLog();
46f92ff7
DO
1529 $setcmd = '';
1530 for ($i = 0; $i < $nports; $i++)
1531 if
1532 (
1533 !isset ($_REQUEST['portname_' . $i]) ||
1534 !isset ($_REQUEST['vlanid_' . $i]) ||
1535 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1536 )
24dcb9d8 1537 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1538 elseif
1539 (
1540 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1541 $portlist[$i]['vlaind'] == 'TRUNK'
1542 )
1543 continue;
1544 else
1545 {
1546 $portname = $_REQUEST['portname_' . $i];
1547 $oldvlanid = $portlist[$i]['vlanid'];
1548 $newvlanid = $_REQUEST['vlanid_' . $i];
1549 // Finish the security context and evaluate it.
1550 $annex = array();
1551 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1552 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1553 if (!permitted (NULL, NULL, NULL, $annex))
1554 {
24dcb9d8 1555 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1556 continue;
1557 }
1558 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1559 $prefix = ';';
1560 }
1561 // Feed the gateway and interpret its (non)response.
1562 if ($setcmd != '')
f0ff4930 1563 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1564 else
24dcb9d8 1565 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1566 return buildWideRedirectURL ($log);
1567}
1568
2987fc1f
DO
1569function submitSLBConfig ()
1570{
1571 assertUIntArg ('object_id', __FUNCTION__);
1572 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
ff0df5c3 1573 $msglog = gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1574 return buildWideRedirectURL ($msglog);
1575}
1576
b6a7d936
DO
1577$msgcode['addRow']['OK'] = 74;
1578$msgcode['addRow']['ERR'] = 100;
345fd640
AD
1579function addRow ()
1580{
1581 assertStringArg ('name', __FUNCTION__);
1582
10bac82a 1583 if (commitAddRow ($_REQUEST['name']) === TRUE)
135080d8 1584 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1585 else
135080d8 1586 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1587}
1588
b6a7d936
DO
1589$msgcode['updateRow']['OK'] = 75;
1590$msgcode['updateRow']['ERR'] = 100;
345fd640
AD
1591function updateRow ()
1592{
1593 assertUIntArg ('row_id', __FUNCTION__);
1594 assertStringArg ('name', __FUNCTION__);
1595
10bac82a 1596 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1597 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1598 else
135080d8 1599 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1600}
1601
b6a7d936
DO
1602$msgcode['deleteRow']['OK'] = 77;
1603$msgcode['deleteRow']['ERR'] = 100;
9f14a7ef
DY
1604function deleteRow ()
1605{
1606 assertUIntArg ('row_id', __FUNCTION__);
7be51fea 1607 $rowinfo = getRackRowInfo ($_REQUEST['row_id']);
9f14a7ef
DY
1608
1609 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
7be51fea 1610 return buildRedirectURL (__FUNCTION__, 'OK', array ($rowinfo['name']));
9f14a7ef 1611 else
7be51fea 1612 return buildRedirectURL (__FUNCTION__, 'ERR', array ($rowinfo['name']));
9f14a7ef
DY
1613}
1614
b6a7d936
DO
1615$msgcode['addRack']['OK'] = 65;
1616$msgcode['addRack']['ERR1'] = 171;
1617$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1618function addRack ()
1619{
1620 assertUIntArg ('row_id', __FUNCTION__);
1621 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1622 if (isset ($_REQUEST['got_data']))
1623 {
1624 assertStringArg ('rack_name', __FUNCTION__);
1625 assertUIntArg ('rack_height1', __FUNCTION__);
1626 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1627
1628 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1629 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1630 else
135080d8 1631 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1632 }
1633 elseif (isset ($_REQUEST['got_mdata']))
1634 {
1635 assertUIntArg ('rack_height2', __FUNCTION__);
1636 assertStringArg ('rack_names', __FUNCTION__, TRUE);
db55cf54 1637 $log = emptyLog();
f19c75d6
DO
1638 // copy-and-paste from renderAddMultipleObjectsForm()
1639 $names1 = explode ('\n', $_REQUEST['rack_names']);
1640 $names2 = array();
1641 foreach ($names1 as $line)
1642 {
1643 $parts = explode ('\r', $line);
1644 reset ($parts);
1645 if (empty ($parts[0]))
1646 continue;
1647 else
1648 $names2[] = rtrim ($parts[0]);
1649 }
b3029d5c 1650 global $msgcode;
f19c75d6
DO
1651 foreach ($names2 as $cname)
1652 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1653 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1654 else
b3029d5c 1655 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1656 return buildWideRedirectURL ($log);
1657 }
1658 else
135080d8 1659 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1660}
1661
b6a7d936
DO
1662$msgcode['deleteRack']['OK'] = 79;
1663$msgcode['deleteRack']['ERR'] = 100;
1664$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1665function deleteRack ()
1666{
1667 assertUIntArg ('rack_id', __FUNCTION__);
1668 $rack_id = $_REQUEST['rack_id'];
1669 $rackData = getRackData ($rack_id);
1670 if ($rackData == NULL)
1671 {
1672 showError ('getRackData() failed', __FUNCTION__);
1673 return;
1674 }
1675 if (count ($rackData['mountedObjects']) == 0)
1676 {
1677 resetThumbCache ($rack_id);
1678 if (TRUE === commitDeleteRack ($rack_id))
1679 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
1680 else
1681 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1682 }
1683 else
1684 {
1685 return buildRedirectURL (__FUNCTION__, 'ERR1');
1686 }
1687}
1688
b6a7d936
DO
1689$msgcode['updateRack']['OK'] = 68;
1690$msgcode['updateRack']['ERR'] = 177;
7056988c
DO
1691function updateRack ()
1692{
7056988c
DO
1693 assertUIntArg ('rack_id', __FUNCTION__);
1694 assertUIntArg ('rack_row_id', __FUNCTION__);
1695 assertUIntArg ('rack_height', __FUNCTION__);
1696 assertStringArg ('rack_name', __FUNCTION__);
1697 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
7056988c 1698
7056988c 1699 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1700 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1701 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1702 else
135080d8 1703 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1704}
1705
4fbb5a00
DY
1706function updateRackDesign ()
1707{
1708 assertUIntArg ('rack_id', __FUNCTION__);
1709 if (($rackData = getRackData ($_REQUEST['rack_id'])) == NULL)
1710 {
1711 showError ('getRackData() failed', __FUNCTION__);
1712 return;
1713 }
1714 applyRackDesignMask($rackData);
1715 markupObjectProblems ($rackData);
1716 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1717 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1718}
1719
1720function updateRackProblems ()
1721{
1722 assertUIntArg ('rack_id', __FUNCTION__);
1723 if (($rackData = getRackData ($_REQUEST['rack_id'])) == NULL)
1724 {
1725 showError ('getRackData() failed', __FUNCTION__);
1726 return;
1727 }
1728 applyRackProblemMask($rackData);
1729 markupObjectProblems ($rackData);
1730 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1731 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1732}
1733
7056988c
DO
1734function querySNMPData ()
1735{
1736 assertUIntArg ('object_id', __FUNCTION__);
1737 assertStringArg ('community', __FUNCTION__);
b6a7d936 1738 require 'inc/snmp.php';
83fdf85f 1739 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
7056988c
DO
1740}
1741
b6a7d936
DO
1742$msgcode['addFileWithoutLink']['OK'] = 69;
1743$msgcode['addFileWithoutLink']['ERR'] = 100;
e1ae3fb4
AD
1744// File-related functions
1745function addFileWithoutLink ()
1746{
1747 assertStringArg ('comment', __FUNCTION__, TRUE);
1748
1749 // Make sure the file can be uploaded
1750 if (get_cfg_var('file_uploads') != 1)
135080d8 1751 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1752
1753 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1754 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1755
1756 if ($error != '')
135080d8 1757 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1758
135080d8 1759 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1760}
1761
b6a7d936
DO
1762$msgcode['addFileToEntity']['OK'] = 69;
1763$msgcode['addFileToEntity']['ERR1'] = 187;
1764$msgcode['addFileToEntity']['ERR2'] = 181;
1765$msgcode['addFileToEntity']['ERR3'] = 182;
e1ae3fb4
AD
1766function addFileToEntity ()
1767{
9a61c175
DO
1768 global $page, $pageno, $etype_by_pageno;
1769 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1770 return buildRedirectURL (__FUNCTION__, 'ERR1', array (__FUNCTION__));
1771 $realm = $etype_by_pageno[$pageno];
1772 $bypass = $page[$pageno]['bypass'];
1773 assertUIntArg ($bypass, __FUNCTION__);
1774 $entity_id = $_REQUEST[$bypass];
e1ae3fb4 1775 assertStringArg ('comment', __FUNCTION__, TRUE);
e1ae3fb4
AD
1776
1777 // Make sure the file can be uploaded
1778 if (get_cfg_var('file_uploads') != 1)
9a61c175 1779 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
1780
1781 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1782 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $_REQUEST['comment']);
1783 if ($error != '')
9a61c175 1784 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1785
9a61c175 1786 $error = commitLinkFile (lastInsertID(), $realm, $entity_id);
e1ae3fb4 1787 if ($error != '')
9a61c175 1788 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1789
135080d8 1790 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1791}
1792
b6a7d936
DO
1793$msgcode['linkFileToEntity']['OK'] = 71;
1794$msgcode['linkFileToEntity']['ERR1'] = 178;
1795$msgcode['linkFileToEntity']['ERR2'] = 100;
e1ae3fb4
AD
1796function linkFileToEntity ()
1797{
e1ae3fb4 1798 assertUIntArg ('file_id', __FUNCTION__);
482c7f35 1799 global $page, $pageno, $etype_by_pageno;
121496b6
DO
1800 $entity_type = $etype_by_pageno[$pageno];
1801 $bypass_name = $page[$pageno]['bypass'];
1802 assertUIntArg ($bypass_name, __FUNCTION__);
1803
1804 $fi = getFileInfo ($_REQUEST['file_id']);
1805 if ($fi === NULL)
135080d8 1806 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
121496b6 1807 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
e1ae3fb4 1808 if ($error != '')
135080d8 1809 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
e1ae3fb4 1810
135080d8 1811 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
e1ae3fb4
AD
1812}
1813
b6a7d936
DO
1814$msgcode['replaceFile']['OK'] = 70;
1815$msgcode['replaceFile']['ERR1'] = 181;
1816$msgcode['replaceFile']['ERR2'] = 207;
1817$msgcode['replaceFile']['ERR3'] = 182;
fd1fb420
AD
1818function replaceFile ()
1819{
db55cf54 1820 global $sic;
fd1fb420
AD
1821 assertUIntArg ('file_id', __FUNCTION__);
1822
1823 // Make sure the file can be uploaded
1824 if (get_cfg_var('file_uploads') != 1)
db55cf54
DO
1825 return buildRedirectURL (__FUNCTION__, 'ERR1');
1826 $shortInfo = getFileInfo ($sic['file_id']);
fd1fb420
AD
1827
1828 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1829 if ($fp === FALSE)
1830 return buildRedirectURL (__FUNCTION__, 'ERR2');
f8874cdb 1831 $error = commitReplaceFile ($sic['file_id'], $fp);
fd1fb420 1832 if ($error != '')
db55cf54 1833 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
fd1fb420 1834
db55cf54 1835 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1836}
1837
b6a7d936
DO
1838$msgcode['updateFile']['OK'] = 70;
1839$msgcode['updateFile']['ERR'] = 100;
e1ae3fb4
AD
1840function updateFile ()
1841{
1842 assertUIntArg ('file_id', __FUNCTION__);
9db67d95
DO
1843 assertStringArg ('file_name', __FUNCTION__);
1844 assertStringArg ('file_type', __FUNCTION__);
1845 assertStringArg ('file_comment', __FUNCTION__, TRUE);
f3d274bf
DO
1846 // prepared statement params below
1847 global $sic;
1848 $error = commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']);
e1ae3fb4 1849 if ($error != '')
135080d8 1850 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1851
135080d8 1852 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1853}
1854
b6a7d936
DO
1855$msgcode['unlinkFile']['OK'] = 72;
1856$msgcode['unlinkFile']['ERR'] = 182;
e1ae3fb4
AD
1857function unlinkFile ()
1858{
1859 assertUIntArg ('link_id', __FUNCTION__);
1860 $error = commitUnlinkFile ($_REQUEST['link_id']);
1861
1862 if ($error != '')
135080d8 1863 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1864
8bc5d1e4 1865 return buildRedirectURL (__FUNCTION__, 'OK');
e1ae3fb4
AD
1866}
1867
b6a7d936
DO
1868$msgcode['deleteFile']['OK'] = 73;
1869$msgcode['deleteFile']['ERR'] = 100;
e1ae3fb4
AD
1870function deleteFile ()
1871{
1872 assertUIntArg ('file_id', __FUNCTION__);
f8874cdb 1873 $shortInfo = getFileInfo ($_REQUEST['file_id']);
e1ae3fb4
AD
1874 $error = commitDeleteFile ($_REQUEST['file_id']);
1875
1876 if ($error != '')
135080d8 1877 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1878
f8874cdb 1879 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1880}
1881
b6a7d936
DO
1882$msgcode['updateFileText']['OK'] = 78;
1883$msgcode['updateFileText']['ERR1'] = 179;
1884$msgcode['updateFileText']['ERR2'] = 180;
8628ae44
DO
1885function updateFileText ()
1886{
1887 assertUIntArg ('file_id', __FUNCTION__);
70cb9b56 1888 assertStringArg ('mtime_copy', __FUNCTION__);
8628ae44
DO
1889 assertStringArg ('file_text', __FUNCTION__, TRUE); // it's Ok to save empty
1890 $shortInfo = getFileInfo ($_REQUEST['file_id']);
70cb9b56
DO
1891 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1892 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb
DO
1893 global $sic;
1894 $error = commitReplaceFile ($sic['file_id'], $sic['file_text']);
8628ae44 1895 if ($error == '')
f8874cdb 1896 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
70cb9b56 1897 return buildRedirectURL (__FUNCTION__, 'ERR2');
8628ae44
DO
1898}
1899
e673ee24 1900?>