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