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