r3616 Config: restore lost is_userdefined column
[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':
84986395 273 $words = explode (' ', preg_replace ('/[[:space:]]+/', ' ', $line));
e673ee24
DO
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':
84986395 283 $words = explode (' ', preg_replace ('/[[:space:]]+/', ' ', trim (substr ($line, 3))));
e673ee24
DO
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':
84986395 307 $words = explode (' ', preg_replace ('/[[:space:]]+/', ' ', $line));
e673ee24
DO
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
a1fc539a
RF
357$msgcode['addBulkPorts']['OK'] = 82;
358$msgcode['addBulkPorts']['ERR'] = 192;
359
360function addBulkPorts ()
361{
362 assertUIntArg ('object_id', __FUNCTION__);
363 assertStringArg ('port_name', __FUNCTION__, TRUE);
364 assertStringArg ('port_label', __FUNCTION__, TRUE);
365 assertUIntArg('port_numbering_start', __FUNCTION__);
366 assertUIntArg('port_numbering_count', __FUNCTION__);
367
368 $object_id = $_REQUEST['object_id'];
369 $port_name = $_REQUEST['port_name'];
370 $port_type_id = $_REQUEST['port_type_id'];
371 $port_label = $_REQUEST['port_label'];
372 $port_numbering_start = $_REQUEST['port_numbering_start'];
373 $port_numbering_count = $_REQUEST['port_numbering_count'];
374
375 if ($object_id == 0 or $port_type_id == 0 or $port_name == '' or $port_numbering_count == 0)
376 {
377 return buildRedirectURL (__FUNCTION__, 'ERR');
378 }
379 $added_count = $error_count = 0;
380 if(strrpos($port_name, "%u") === false )
381 $port_name .= '%u';
382 for ($i=0,$c=$port_numbering_start; $i<=$port_numbering_count; $i++,$c++)
383 {
384 $ret = commitAddPort ($object_id, @sprintf($port_name,$c), $port_type_id, @sprintf($port_label,$c), '');
385 if ($result == '')
386 $added_count++;
387 else
388 $error_count++;
389 }
390 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $error_count));
391}
392
393
b6a7d936
DO
394$msgcode['updIPv4Allocation']['OK'] = 12;
395$msgcode['updIPv4Allocation']['ERR'] = 100;
b4c1ef87 396function updIPv4Allocation ()
e673ee24 397{
0cc24e9a
DY
398 assertIPv4Arg ('ip');
399 assertUIntArg ('object_id');
400 assertStringArg ('bond_name', TRUE);
401 assertStringArg ('bond_type');
e673ee24 402
b4c1ef87 403 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 404 if ($error != '')
135080d8 405 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 406 else
135080d8 407 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
408}
409
b6a7d936
DO
410$msgcode['delIPv4Allocation']['OK'] = 14;
411$msgcode['delIPv4Allocation']['ERR'] = 100;
b4c1ef87 412function delIPv4Allocation ()
e673ee24 413{
0cc24e9a
DY
414 assertIPv4Arg ('ip');
415 assertUIntArg ('object_id');
e673ee24 416
b4c1ef87 417 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 418 if ($error != '')
135080d8 419 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 420 else
135080d8 421 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
422}
423
b6a7d936
DO
424$msgcode['addIPv4Allocation']['OK'] = 13;
425$msgcode['addIPv4Allocation']['ERR1'] = 170;
426$msgcode['addIPv4Allocation']['ERR2'] = 100;
b4c1ef87 427function addIPv4Allocation ()
e673ee24 428{
0cc24e9a
DY
429 assertIPv4Arg ('ip');
430 assertUIntArg ('object_id');
431 assertStringArg ('bond_name', TRUE);
432 assertStringArg ('bond_type');
da958e52 433
b4c1ef87 434 // Strip masklen.
84986395 435 $ip = preg_replace ('@/[[:digit:]]+$@', '', $_REQUEST['ip']);
2be3fd23 436 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
135080d8 437 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
5222f192 438
b4c1ef87 439 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
5222f192 440 if ($error != '')
135080d8 441 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
53ef3908 442 $address = getIPv4Address ($ip);
59a83bd8 443 if ($address['reserved'] == 'yes' or strlen ($address['name']))
b4c1ef87
DO
444 {
445 $release = getConfigVar ('IPV4_AUTO_RELEASE');
446 if ($release >= 1)
447 $address['reserved'] = 'no';
448 if ($release >= 2)
449 $address['name'] = '';
450 updateAddress ($ip, $address['name'], $address['reserved']);
451 }
135080d8 452 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
453}
454
b6a7d936
DO
455$msgcode['addIPv4Prefix']['OK'] = 23;
456$msgcode['addIPv4Prefix']['ERR'] = 100;
457$msgcode['addIPv4Prefix']['ERR1'] = 173;
458$msgcode['addIPv4Prefix']['ERR2'] = 174;
459$msgcode['addIPv4Prefix']['ERR3'] = 175;
460$msgcode['addIPv4Prefix']['ERR4'] = 176;
42023f03 461function addIPv4Prefix ()
e673ee24 462{
0cc24e9a
DY
463 assertStringArg ('range');
464 assertStringArg ('name', TRUE);
e673ee24 465
fa05e3de
DO
466 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
467 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
357eb2ea
DO
468 global $sic;
469 $error = createIPv4Prefix ($_REQUEST['range'], $sic['name'], $is_bcast == 'on', $taglist);
e673ee24 470 if ($error != '')
135080d8 471 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 472 else
135080d8 473 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
474}
475
b6a7d936
DO
476$msgcode['delIPv4Prefix']['OK'] = 24;
477$msgcode['delIPv4Prefix']['ERR'] = 100;
42023f03 478function delIPv4Prefix ()
e673ee24 479{
0cc24e9a 480 assertUIntArg ('id');
42023f03 481 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 482 if ($error != '')
135080d8 483 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 484 else
135080d8 485 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
486}
487
b6a7d936
DO
488$msgcode['updIPv4Prefix']['OK'] = 25;
489$msgcode['updIPv4Prefix']['ERR'] = 100;
04d619d0 490function updIPv4Prefix ()
e673ee24 491{
0cc24e9a
DY
492 assertUIntArg ('id');
493 assertStringArg ('name', TRUE);
494 assertStringArg ('comment', TRUE);
99ab184f
DO
495 global $sic;
496 if (strlen ($error = updateIPv4Network_real ($sic['id'], $sic['name'], $sic['comment'])))
135080d8 497 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 498 else
135080d8 499 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
500}
501
b6a7d936
DO
502$msgcode['editAddress']['OK'] = 27;
503$msgcode['editAddress']['ERR'] = 100;
e673ee24
DO
504function editAddress ()
505{
0cc24e9a
DY
506 assertIPv4Arg ('ip');
507 assertStringArg ('name', TRUE);
e673ee24 508
e673ee24
DO
509 if (isset ($_REQUEST['reserved']))
510 $reserved = $_REQUEST['reserved'];
511 else
512 $reserved = 'off';
da958e52 513 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 514 if ($error != '')
135080d8 515 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 516 else
135080d8 517 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
518}
519
b6a7d936
DO
520$msgcode['createUser']['OK'] = 40;
521$msgcode['createUser']['ERR'] = 102;
cced6b7d 522function createUser ()
e673ee24 523{
0cc24e9a
DY
524 assertStringArg ('username');
525 assertStringArg ('realname', TRUE);
526 assertStringArg ('password');
e673ee24 527 $username = $_REQUEST['username'];
93bdb7ba 528 $password = sha1 ($_REQUEST['password']);
e673ee24 529 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
f857f71f 530 if ($result != TRUE)
135080d8 531 return buildRedirectURL (__FUNCTION__, 'ERR', array ($username));
f857f71f
DO
532 if (isset ($_REQUEST['taglist']))
533 produceTagsForLastRecord ('user', $_REQUEST['taglist']);
534 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24
DO
535}
536
b6a7d936
DO
537$msgcode['updateUser']['OK'] = 39;
538$msgcode['updateUser']['ERR1'] = 103;
539$msgcode['updateUser']['ERR1'] = 104;
cced6b7d 540function updateUser ()
e673ee24 541{
0cc24e9a
DY
542 assertUIntArg ('user_id');
543 assertStringArg ('username');
544 assertStringArg ('realname', TRUE);
545 assertStringArg ('password');
e673ee24
DO
546 $username = $_REQUEST['username'];
547 $new_password = $_REQUEST['password'];
0b2c74cb 548 if (NULL == ($userinfo = spotEntity ('user', $_REQUEST['user_id'])))
135080d8 549 return buildRedirectURL (__FUNCTION__, 'ERR1');
e673ee24 550 // Update user password only if provided password is not the same as current password hash.
b82cce3f 551 if ($new_password != $userinfo['user_password_hash'])
93bdb7ba 552 $new_password = sha1 ($new_password);
103b1e1e 553 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
e673ee24 554 if ($result == TRUE)
135080d8 555 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 556 else
135080d8 557 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($username));
e673ee24
DO
558}
559
b6a7d936
DO
560$msgcode['updateDictionary']['OK'] = 51;
561$msgcode['updateDictionary']['ERR'] = 109;
e673ee24
DO
562function updateDictionary ()
563{
0cc24e9a
DY
564 assertUIntArg ('chapter_no');
565 assertUIntArg ('dict_key');
566 assertStringArg ('dict_value');
e673ee24 567 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
135080d8 568 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 569 else
135080d8 570 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
571}
572
b6a7d936
DO
573$msgcode['supplementDictionary']['OK'] = 52;
574$msgcode['supplementDictionary']['ERR'] = 110;
e673ee24
DO
575function supplementDictionary ()
576{
0cc24e9a
DY
577 assertUIntArg ('chapter_no');
578 assertStringArg ('dict_value');
e673ee24 579 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
135080d8 580 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 581 else
135080d8 582 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
583}
584
b6a7d936
DO
585$msgcode['reduceDictionary']['OK'] = 50;
586$msgcode['reduceDictionary']['ERR'] = 111;
e673ee24
DO
587function reduceDictionary ()
588{
0cc24e9a
DY
589 assertUIntArg ('chapter_no');
590 assertUIntArg ('dict_key');
e673ee24 591 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
135080d8 592 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 593 else
135080d8 594 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
595}
596
b6a7d936
DO
597$msgcode['addChapter']['OK'] = 55;
598$msgcode['addChapter']['ERR'] = 112;
e673ee24
DO
599function addChapter ()
600{
0cc24e9a 601 assertStringArg ('chapter_name');
e673ee24 602 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
135080d8 603 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 604 else
135080d8 605 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
606}
607
b6a7d936
DO
608$msgcode['updateChapter']['OK'] = 54;
609$msgcode['updateChapter']['ERR'] = 113;
e673ee24
DO
610function updateChapter ()
611{
0cc24e9a
DY
612 assertUIntArg ('chapter_no');
613 assertStringArg ('chapter_name');
e673ee24 614 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
135080d8 615 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 616 else
135080d8 617 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
618}
619
b6a7d936
DO
620$msgcode['delChapter']['OK'] = 53;
621$msgcode['delChapter']['ERR'] = 114;
e673ee24
DO
622function delChapter ()
623{
0cc24e9a 624 assertUIntArg ('chapter_no');
e673ee24 625 if (commitDeleteChapter ($_REQUEST['chapter_no']))
135080d8 626 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 627 else
135080d8 628 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
629}
630
b6a7d936
DO
631$msgcode['changeAttribute']['OK'] = 46;
632$msgcode['changeAttribute']['ERR'] = 115;
e673ee24
DO
633function changeAttribute ()
634{
0cc24e9a
DY
635 assertUIntArg ('attr_id');
636 assertStringArg ('attr_name');
e673ee24 637 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
135080d8 638 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 639 else
135080d8 640 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
641}
642
b6a7d936
DO
643$msgcode['createAttribute']['OK'] = 45;
644$msgcode['createAttribute']['ERR'] = 116;
e673ee24
DO
645function createAttribute ()
646{
0cc24e9a
DY
647 assertStringArg ('attr_name');
648 assertStringArg ('attr_type');
e673ee24 649 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
135080d8 650 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
e673ee24 651 else
135080d8 652 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
653}
654
b6a7d936
DO
655$msgcode['deleteAttribute']['OK'] = 47;
656$msgcode['deleteAttribute']['ERR'] = 117;
e673ee24
DO
657function deleteAttribute ()
658{
0cc24e9a 659 assertUIntArg ('attr_id');
e673ee24 660 if (commitDeleteAttribute ($_REQUEST['attr_id']))
135080d8 661 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 662 else
135080d8 663 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
664}
665
b6a7d936 666$msgcode['supplementAttrMap']['OK'] = 48;
7028a42c
DO
667$msgcode['supplementAttrMap']['ERR1'] = 154;
668$msgcode['supplementAttrMap']['ERR2'] = 118;
e673ee24
DO
669function supplementAttrMap ()
670{
0cc24e9a
DY
671 assertUIntArg ('attr_id');
672 assertUIntArg ('objtype_id');
7028a42c
DO
673 $attrMap = getAttrMap();
674 if ($attrMap[$_REQUEST['attr_id']]['type'] != 'dict')
675 $chapter_id = 'NULL';
676 else
677 {
0cc24e9a 678 assertUIntArg ('chapter_no'); // FIXME: this doesn't fail on 0 (ticket:272)
7028a42c
DO
679 if (0 == ($chapter_id = $_REQUEST['chapter_no']))
680 return buildRedirectURL (__FUNCTION__, 'ERR1', array ('chapter not selected'));
681 }
682 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $chapter_id) === TRUE)
135080d8 683 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 684 else
7028a42c 685 return buildRedirectURL (__FUNCTION__, 'ERR2');
e673ee24
DO
686}
687
b6a7d936
DO
688$msgcode['reduceAttrMap']['OK'] = 49;
689$msgcode['reduceAttrMap']['ERR'] = 119;
e673ee24
DO
690function reduceAttrMap ()
691{
0cc24e9a
DY
692 assertUIntArg ('attr_id');
693 assertUIntArg ('objtype_id');
e673ee24 694 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
135080d8 695 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 696 else
135080d8 697 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
698}
699
b6a7d936
DO
700$msgcode['clearSticker']['OK'] = 15;
701$msgcode['clearSticker']['ERR'] = 120;
22bb04da 702function clearSticker ()
e673ee24 703{
0cc24e9a
DY
704 assertUIntArg ('attr_id');
705 assertUIntArg ('object_id');
22bb04da 706 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
135080d8 707 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 708 else
135080d8 709 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
710}
711
e97e8866 712$msgcode['updateObjectAllocation']['OK'] = 63;
4fbb5a00
DY
713function updateObjectAllocation ()
714{
0cc24e9a 715 assertUIntArg ('object_id');
4fbb5a00 716
e97e8866
DO
717 if (!isset ($_REQUEST['got_atoms']))
718 {
719 unset($_GET['page']);
720 unset($_GET['tab']);
721 unset($_GET['op']);
722 unset($_POST['page']);
723 unset($_POST['tab']);
724 unset($_POST['op']);
725 return buildWideRedirectURL (array(), NULL, NULL, array_merge ($_GET, $_POST));
726 }
727 $object_id = $_REQUEST['object_id'];
728 $workingRacksData = array();
729 foreach ($_REQUEST['rackmulti'] as $cand_id)
4fbb5a00 730 {
e97e8866 731 if (!isset ($workingRacksData[$cand_id]))
4fbb5a00 732 {
e97e8866 733 $rackData = spotEntity ('rack', $cand_id);
e97e8866
DO
734 amplifyCell ($rackData);
735 $workingRacksData[$cand_id] = $rackData;
4fbb5a00 736 }
e97e8866 737 }
2135fd83 738
e97e8866
DO
739 foreach ($workingRacksData as &$rd)
740 applyObjectMountMask ($rd, $object_id);
4fbb5a00 741
e97e8866
DO
742 $oldMolecule = getMoleculeForObject ($object_id);
743 $changecnt = 0;
744 $log = array();
745 foreach ($workingRacksData as $rack_id => $rackData)
4fbb5a00 746 {
e97e8866
DO
747 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
748 $log[] = $logrecord;
749 if ($logrecord['code'] == 300)
750 continue;
751 $changecnt++;
752 // Reload our working copy after form processing.
753 $rackData = spotEntity ('rack', $cand_id);
754 amplifyCell ($rackData);
755 applyObjectMountMask ($rackData, $object_id);
756 $workingRacksData[$rack_id] = $rackData;
4fbb5a00 757 }
e97e8866
DO
758 if (!$changecnt)
759 return buildRedirectURL (__FUNCTION__, 'OK', $changecnt);
760 // Log a record.
761 $newMolecule = getMoleculeForObject ($object_id);
762 $oc = count ($oldMolecule);
763 $nc = count ($newMolecule);
764 $omid = $oc ? createMolecule ($oldMolecule) : 'NULL';
765 $nmid = $nc ? createMolecule ($newMolecule) : 'NULL';
766 global $remote_username;
767 $comment = empty ($_REQUEST['comment']) ? 'NULL' : "'${_REQUEST['comment']}'";
768 $query =
769 "insert into MountOperation(object_id, old_molecule_id, new_molecule_id, user_name, comment) " .
770 "values (${object_id}, ${omid}, ${nmid}, '${remote_username}', ${comment})";
771 global $dbxlink;
772 $result = $dbxlink->query ($query);
773 if ($result == NULL)
774 $log[] = array ('code' => 500, 'message' => 'SQL query failed during history logging.');
775 else
776 $log[] = array ('code' => 200, 'message' => 'History logged.');
777 return buildWideRedirectURL ($log);
4fbb5a00
DY
778}
779
b6a7d936
DO
780$msgcode['updateObject']['OK'] = 16;
781$msgcode['updateObject']['ERR'] = 121;
22bb04da
DO
782function updateObject ()
783{
0cc24e9a
DY
784 assertUIntArg ('num_attrs', TRUE);
785 assertUIntArg ('object_id');
786 assertUIntArg ('object_type_id');
787 assertStringArg ('object_name', TRUE);
788 assertStringArg ('object_label', TRUE);
789 assertStringArg ('object_barcode', TRUE);
790 assertStringArg ('object_asset_no', TRUE);
22bb04da
DO
791 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
792 $has_problems = 'yes';
793 else
794 $has_problems = 'no';
795
796 if (commitUpdateObject (
797 $_REQUEST['object_id'],
798 $_REQUEST['object_name'],
799 $_REQUEST['object_label'],
800 $_REQUEST['object_barcode'],
801 $_REQUEST['object_type_id'],
802 $has_problems,
803 $_REQUEST['object_asset_no'],
804 $_REQUEST['object_comment']
805 ) !== TRUE)
135080d8 806 return buildRedirectURL (__FUNCTION__, 'ERR');
22bb04da 807
cdd3efe9 808 // Update optional attributes
f59e8cf5 809 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a 810 $result = array();
0f63538a
DO
811 $num_attrs = isset ($_REQUEST['num_attrs']) ? $_REQUEST['num_attrs'] : 0;
812 for ($i = 0; $i < $num_attrs; $i++)
0dfb8a2a 813 {
0cc24e9a 814 assertUIntArg ("${i}_attr_id");
0dfb8a2a
DO
815 $attr_id = $_REQUEST["${i}_attr_id"];
816
01d82bd2
RF
817 // Field is empty, delete attribute and move on. OR if the field type is a dictionary and it is the --NOT SET-- value of 0
818 if (!strlen ($_REQUEST["${i}_value"]) || ($oldvalues[$attr_id]['type']=='dict' && $_REQUEST["${i}_value"] == 0))
0dfb8a2a 819 {
103b1e1e 820 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
821 continue;
822 }
823
824 // The value could be uint/float, but we don't know ATM. Let SQL
825 // server check this and complain.
0cc24e9a 826 assertStringArg ("${i}_value");
0dfb8a2a
DO
827 $value = $_REQUEST["${i}_value"];
828 switch ($oldvalues[$attr_id]['type'])
829 {
830 case 'uint':
831 case 'float':
832 case 'string':
833 $oldvalue = $oldvalues[$attr_id]['value'];
834 break;
835 case 'dict':
836 $oldvalue = $oldvalues[$attr_id]['key'];
837 break;
838 default:
0cc24e9a 839 throw new RuntimeException('Internal structure error');
0dfb8a2a 840 }
59a83bd8 841 if ($value === $oldvalue) // ('' == 0), but ('' !== 0)
0dfb8a2a
DO
842 continue;
843
844 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 845 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a 846 }
103b1e1e 847 if (in_array (FALSE, $result))
135080d8 848 return buildRedirectURL (__FUNCTION__, 'ERR');
0dfb8a2a 849
cdd3efe9
AD
850 // Invalidate thumb cache of all racks objects could occupy.
851 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
852 resetThumbCache ($rack_id);
853
135080d8 854 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
855}
856
42d73cbf
DY
857
858function addMultipleObjects()
859{
7abce0b7 860 $log = emptyLog();
42d73cbf 861 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
862 $max = getConfigVar ('MASSCOUNT');
863 for ($i = 0; $i < $max; $i++)
864 {
865 if (!isset ($_REQUEST["${i}_object_type_id"]))
866 {
57777e4a 867 $log = mergeLogs ($log, oneLiner (184, array ($i + 1)));
42d73cbf
DY
868 break;
869 }
0cc24e9a
DY
870 assertUIntArg ("${i}_object_type_id", TRUE);
871 assertStringArg ("${i}_object_name", TRUE);
872 assertStringArg ("${i}_object_label", TRUE);
873 assertStringArg ("${i}_object_asset_no", TRUE);
874 assertStringArg ("${i}_object_barcode", TRUE);
7abce0b7 875 $name = $_REQUEST["${i}_object_name"];
42d73cbf
DY
876
877 // It's better to skip silently, than to print a notice.
7abce0b7 878 if ($_REQUEST["${i}_object_type_id"] == 0)
42d73cbf 879 continue;
fee7977b 880 if (($object_id = commitAddObject
7abce0b7
DO
881 (
882 $name,
883 $_REQUEST["${i}_object_label"],
884 $_REQUEST["${i}_object_barcode"],
885 $_REQUEST["${i}_object_type_id"],
886 $_REQUEST["${i}_object_asset_no"],
887 $taglist
fee7977b
DO
888 )) !== FALSE){
889 $info = spotEntity ('object', $object_id);
890 // FIXME: employ amplifyCell() instead of calling loader functions directly
891 amplifyCell ($info);
892 $log = mergeLogs ($log, oneLiner (80, array ('<a href="' . makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) . '">' . $info['dname'] . '</a>')));
893 }else{
57777e4a 894 $log = mergeLogs ($log, oneLiner (185, array ($name)));
fee7977b 895 }
42d73cbf 896 }
7abce0b7 897 return buildWideRedirectURL ($log);
42d73cbf
DY
898}
899
900function addLotOfObjects()
901{
7abce0b7 902 $log = emptyLog();
42d73cbf 903 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
0cc24e9a
DY
904 assertUIntArg ('global_type_id', TRUE);
905 assertStringArg ('namelist', TRUE);
42d73cbf 906 $global_type_id = $_REQUEST['global_type_id'];
59a83bd8 907 if ($global_type_id == 0 or !strlen ($_REQUEST['namelist']))
57777e4a 908 $log = mergeLogs ($log, oneLiner (186));
42d73cbf
DY
909 else
910 {
911 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
912 $names1 = explode ('\n', $_REQUEST['namelist']);
913 $names2 = array();
914 foreach ($names1 as $line)
915 {
916 $parts = explode ('\r', $line);
917 reset ($parts);
59a83bd8 918 if (!strlen ($parts[0]))
42d73cbf
DY
919 continue;
920 else
921 $names2[] = rtrim ($parts[0]);
922 }
57777e4a 923 foreach ($names2 as $name)
9d535a0c
DO
924 if (($object_id = commitAddObject ($name, '', '', $global_type_id, '', $taglist)) !== FALSE)
925 {
926 $info = spotEntity ('object', $object_id);
927 amplifyCell ($info);
928 $log = mergeLogs ($log, oneLiner (80, array ('<a href="' . makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) . '">' . $info['dname'] . '</a>')));
929 }
42d73cbf 930 else
57777e4a 931 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 932 }
7abce0b7 933 return buildWideRedirectURL ($log);
42d73cbf
DY
934}
935
b6a7d936
DO
936$msgcode['deleteObject']['OK'] = 76;
937$msgcode['deleteObject']['ERR'] = 100;
52b34485
AD
938function deleteObject ()
939{
0cc24e9a 940 assertUIntArg ('object_id');
6297d584 941 if (NULL === ($oinfo = spotEntity ('object', $_REQUEST['object_id'])))
c78a40ec
DO
942 return buildRedirectURL (__FUNCTION__, 'ERR', array ('object not found'));
943
f0e2c99f 944 $racklist = getResidentRacksData ($_REQUEST['object_id'], FALSE);
52b34485 945 $error = commitDeleteObject ($_REQUEST['object_id']);
f0e2c99f
DO
946 foreach ($racklist as $rack_id)
947 resetThumbCache ($rack_id);
52b34485
AD
948
949 if ($error != '')
135080d8 950 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
52b34485 951
c78a40ec 952 return buildRedirectURL (__FUNCTION__, 'OK', array ($oinfo['dname']));
52b34485
AD
953}
954
b6a7d936
DO
955$msgcode['useupPort']['OK'] = 11;
956$msgcode['useupPort']['ERR'] = 124;
e673ee24
DO
957function useupPort ()
958{
0cc24e9a 959 assertUIntArg ('port_id');
e673ee24 960 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
135080d8 961 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 962 else
135080d8 963 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
964}
965
b6a7d936
DO
966$msgcode['updateUI']['OK'] = 56;
967$msgcode['updateUI']['ERR'] = 125;
4fe32e78
DY
968function updateUI ()
969{
0cc24e9a 970 assertUIntArg ('num_vars');
4fe32e78 971
103b1e1e 972 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 973 {
0cc24e9a
DY
974 assertStringArg ("${i}_varname");
975 assertStringArg ("${i}_varvalue", TRUE);
4fe32e78 976 $varname = $_REQUEST["${i}_varname"];
4fe32e78 977 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 978
c461c579 979 // If form value = value in DB, don't bother updating DB
ed941e67 980 if (!isConfigVarChanged($varname, $varvalue))
4fe32e78
DY
981 continue;
982
983 // Note if the queries succeed or not, it determines which page they see.
3540d15c
DY
984 try {
985 setConfigVar ($varname, $varvalue, TRUE);
986 } catch (InvalidArgException $e) {
987 return buildRedirectURL (__FUNCTION__, 'ERR', array ($e->getMessage()));
988 }
989 }
990 return buildRedirectURL (__FUNCTION__, 'OK');
991}
992
993$msgcode['saveMyPreferences']['OK'] = 56;
994$msgcode['saveMyPreferences']['ERR'] = 125;
995function saveMyPreferences ()
996{
997 assertUIntArg ('num_vars');
998
999 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
1000 {
1001 assertStringArg ("${i}_varname");
1002 assertStringArg ("${i}_varvalue", TRUE);
1003 $varname = $_REQUEST["${i}_varname"];
1004 $varvalue = $_REQUEST["${i}_varvalue"];
1005
1006 // If form value = value in DB, don't bother updating DB
ed941e67 1007 if (!isConfigVarChanged($varname, $varvalue))
3540d15c 1008 continue;
3540d15c
DY
1009 // Note if the queries succeed or not, it determines which page they see.
1010 try {
1011 setUserConfigVar ($varname, $varvalue);
1012 } catch (InvalidArgException $e) {
1013 return buildRedirectURL (__FUNCTION__, 'ERR', array ($e->getMessage()));
1014 }
1015 }
1016 return buildRedirectURL (__FUNCTION__, 'OK');
1017}
1018
1019$msgcode['resetMyPreference']['OK'] = 56;
1020$msgcode['resetMyPreference']['ERR'] = 125;
1021function resetMyPreference ()
1022{
1023 assertStringArg ("varname");
1024 $varname = $_REQUEST["varname"];
1025
1026 try {
1027 resetUserConfigVar ($varname);
1028 } catch (InvalidArgException $e) {
1029 return buildRedirectURL (__FUNCTION__, 'ERR', array ($e->getMessage()));
4fe32e78 1030 }
135080d8 1031 return buildRedirectURL (__FUNCTION__, 'OK');
4fe32e78
DY
1032}
1033
3540d15c
DY
1034
1035
1036
b6a7d936 1037$msgcode['resetUIConfig']['OK'] = 57;
b07f617c
DO
1038function resetUIConfig()
1039{
b07f617c
DO
1040 setConfigVar ('MASSCOUNT','15');
1041 setConfigVar ('MAXSELSIZE','30');
b07f617c
DO
1042 setConfigVar ('ROW_SCALE','2');
1043 setConfigVar ('PORTS_PER_ROW','12');
1044 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
1045 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
b5ce46aa
DO
1046 setConfigVar ('DEFAULT_SLB_VS_PORT','');
1047 setConfigVar ('DEFAULT_SLB_RS_PORT','');
72281138 1048 setConfigVar ('DETECT_URLS','no');
4b8d413e 1049 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 1050 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 1051 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
1052 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
1053 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
1054 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 1055 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 1056 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df 1057 setConfigVar ('SHOW_LAST_TAB', 'no');
f0ed1181 1058 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 1059 setConfigVar ('TREE_THRESHOLD', '25');
2be3fd23 1060 setConfigVar ('IPV4_JAYWALK', 'no');
9318d2ef 1061 setConfigVar ('ADDNEW_AT_TOP', 'yes');
64347dcf 1062 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
37e59768
DO
1063 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
1064 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
1065 setConfigVar ('PREVIEW_TEXT_COLS', '80');
1066 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
f3d274bf 1067 setConfigVar ('VENDOR_SIEVE', '');
35bf153d 1068 setConfigVar ('IPV4LB_LISTSRC', '{$typeid_4}');
8fee82b4
DO
1069 setConfigVar ('IPV4OBJ_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_12} or {$typeid_445} or {$typeid_447} or {$typeid_798}');
1070 setConfigVar ('IPV4NAT_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_798}');
35bf153d
DO
1071 setConfigVar ('ASSETWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
1072 setConfigVar ('NAMEWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
f5883ec1 1073 setConfigVar ('RACKS_PER_ROW','12');
590e1281 1074 setConfigVar ('FILTER_PREDICATE_SIEVE','');
23cdc7e9
DO
1075 setConfigVar ('FILTER_DEFAULT_ANDOR','or');
1076 setConfigVar ('FILTER_SUGGEST_ANDOR','yes');
1077 setConfigVar ('FILTER_SUGGEST_TAGS','yes');
1078 setConfigVar ('FILTER_SUGGEST_PREDICATES','yes');
1079 setConfigVar ('FILTER_SUGGEST_EXTRA','no');
8fee82b4 1080 setConfigVar ('DEFAULT_SNMP_COMMUNITY','public');
f06fe423 1081 setConfigVar ('IPV4_ENABLE_KNIGHT','yes');
9e51318b
DO
1082 setConfigVar ('TAGS_TOPLIST_SIZE','50');
1083 setConfigVar ('TAGS_QUICKLIST_SIZE','20');
1084 setConfigVar ('TAGS_QUICKLIST_THRESHOLD','50');
f44fdef9 1085 setConfigVar ('ENABLE_MULTIPORT_FORM', 'no');
2400d7ec
DO
1086 setConfigVar ('DEFAULT_PORT_IIF_ID', '1');
1087 setConfigVar ('DEFAULT_PORT_OIF_IDS', '1=24; 3=1078; 4=1077; 5=1079; 6=1080; 8=1082; 9=1084');
4a47d34b 1088 setConfigVar ('IPV4_TREE_RTR_AS_CELL', 'yes');
cd3775e9 1089 setConfigVar ('PROXIMITY_RANGE', 0);
ca4eb604 1090 setConfigVar ('IPV4_TREE_SHOW_VLAN', 'yes');
2fb1f79b
DO
1091 setConfigVar ('DEFAULT_VDOM_ID', '');
1092 setConfigVar ('DEFAULT_VST_ID', '');
01b9a31a 1093 setConfigVar ('STATIC_FILTER', 'yes');
135080d8 1094 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
1095}
1096
b6a7d936
DO
1097$msgcode['addRealServer']['OK'] = 34;
1098$msgcode['addRealServer']['ERR'] = 126;
732e4578 1099// Add single record.
ca461127
DO
1100function addRealServer ()
1101{
0cc24e9a
DY
1102 assertUIntArg ('pool_id');
1103 assertIPv4Arg ('remoteip');
1104 assertStringArg ('rsport', TRUE);
1105 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1106 if (!addRStoRSPool (
1107 $_REQUEST['pool_id'],
1108 $_REQUEST['remoteip'],
1109 $_REQUEST['rsport'],
1110 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1111 $_REQUEST['rsconfig']
1112 ))
135080d8 1113 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1114 else
135080d8 1115 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1116}
1117
b6a7d936
DO
1118$msgcode['addRealServers']['OK'] = 37;
1119$msgcode['addRealServers']['ERR1'] = 131;
1120$msgcode['addRealServers']['ERR2'] = 127;
732e4578
DO
1121// Parse textarea submitted and try adding a real server for each line.
1122function addRealServers ()
1123{
0cc24e9a
DY
1124 assertUIntArg ('pool_id');
1125 assertStringArg ('format');
1126 assertStringArg ('rawtext');
732e4578
DO
1127 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1128 $ngood = $nbad = 0;
1129 $rsconfig = '';
1130 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1131 foreach (explode ('\n', $rawtext) as $line)
1132 {
59a83bd8 1133 if (!strlen ($line))
732e4578
DO
1134 continue;
1135 $match = array ();
1136 switch ($_REQUEST['format'])
1137 {
1138 case 'ipvs_2': // address and port only
1139 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1140 continue;
103b1e1e 1141 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1142 $ngood++;
1143 else
1144 $nbad++;
1145 break;
1146 case 'ipvs_3': // address, port and weight
1147 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1148 continue;
103b1e1e 1149 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1150 $ngood++;
1151 else
1152 $nbad++;
1153 break;
e69c2aa3
DO
1154 case 'ssv_2': // IP address and port
1155 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1156 continue;
103b1e1e 1157 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1158 $ngood++;
1159 else
1160 $nbad++;
1161 break;
948c37e3
DO
1162 case 'ssv_1': // IP address
1163 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1164 continue;
1165 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1166 $ngood++;
1167 else
1168 $nbad++;
1169 break;
732e4578 1170 default:
135080d8 1171 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1172 break;
1173 }
1174 }
1175 if ($nbad == 0 and $ngood > 0)
135080d8 1176 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1177 else
135080d8 1178 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1179}
1180
b6a7d936
DO
1181$msgcode['addVService']['OK'] = 28;
1182$msgcode['addVService']['ERR1'] = 132;
1183$msgcode['addVService']['ERR2'] = 100;
d6517a21
DO
1184function addVService ()
1185{
0cc24e9a
DY
1186 assertIPv4Arg ('vip');
1187 assertUIntArg ('vport');
1188 assertStringArg ('proto');
103b1e1e 1189 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1190 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1191 assertStringArg ('name', TRUE);
1192 assertStringArg ('vsconfig', TRUE);
1193 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1194 $error = commitCreateVS
1195 (
103b1e1e
DO
1196 $_REQUEST['vip'],
1197 $_REQUEST['vport'],
7320b661 1198 $_REQUEST['proto'],
103b1e1e
DO
1199 $_REQUEST['name'],
1200 $_REQUEST['vsconfig'],
c63a8d6e
DO
1201 $_REQUEST['rsconfig'],
1202 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1203 );
1204 if ($error != '')
135080d8 1205 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1206 else
135080d8 1207 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1208}
1209
b6a7d936
DO
1210$msgcode['deleteRealServer']['OK'] = 35;
1211$msgcode['deleteRealServer']['ERR'] = 128;
fb1c4a54
DO
1212function deleteRealServer ()
1213{
0cc24e9a 1214 assertUIntArg ('id');
d6517a21 1215 if (!commitDeleteRS ($_REQUEST['id']))
135080d8 1216 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1217 else
135080d8 1218 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1219}
1220
b6a7d936
DO
1221$msgcode['deleteLoadBalancer']['OK'] = 19;
1222$msgcode['deleteLoadBalancer']['ERR'] = 129;
3241551e
DO
1223function deleteLoadBalancer ()
1224{
0cc24e9a
DY
1225 assertUIntArg ('object_id');
1226 assertUIntArg ('pool_id');
1227 assertUIntArg ('vs_id');
103b1e1e
DO
1228 if (!commitDeleteLB (
1229 $_REQUEST['object_id'],
1230 $_REQUEST['pool_id'],
1231 $_REQUEST['vs_id']
1232 ))
135080d8 1233 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1234 else
135080d8 1235 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1236}
1237
b6a7d936
DO
1238$msgcode['deleteVService']['OK'] = 29;
1239$msgcode['deleteVService']['ERR'] = 130;
d6517a21
DO
1240function deleteVService ()
1241{
0cc24e9a 1242 assertUIntArg ('vs_id');
e02e1941 1243 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1244 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1245 else
135080d8 1246 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1247}
1248
b6a7d936
DO
1249$msgcode['updateRealServer']['OK'] = 36;
1250$msgcode['updateRealServer']['ERR'] = 133;
fb1c4a54
DO
1251function updateRealServer ()
1252{
0cc24e9a
DY
1253 assertUIntArg ('rs_id');
1254 assertIPv4Arg ('rsip');
1255 assertStringArg ('rsport', TRUE);
1256 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1257 if (!commitUpdateRS (
1258 $_REQUEST['rs_id'],
1259 $_REQUEST['rsip'],
1260 $_REQUEST['rsport'],
1261 $_REQUEST['rsconfig']
1262 ))
135080d8 1263 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1264 else
135080d8 1265 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1266}
1267
b6a7d936
DO
1268$msgcode['updateLoadBalancer']['OK'] = 20;
1269$msgcode['updateLoadBalancer']['ERR'] = 134;
c1ca768c 1270function updateLoadBalancer ()
3241551e 1271{
0cc24e9a
DY
1272 assertUIntArg ('object_id');
1273 assertUIntArg ('pool_id');
1274 assertUIntArg ('vs_id');
1275 assertStringArg ('vsconfig', TRUE);
1276 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1277 if (!commitUpdateLB (
1278 $_REQUEST['object_id'],
1279 $_REQUEST['pool_id'],
1280 $_REQUEST['vs_id'],
1281 $_REQUEST['vsconfig'],
1282 $_REQUEST['rsconfig']
1283 ))
135080d8 1284 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1285 else
135080d8 1286 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1287}
1288
b6a7d936
DO
1289$msgcode['updateVService']['OK'] = 30;
1290$msgcode['updateVService']['ERR'] = 135;
d6517a21
DO
1291function updateVService ()
1292{
0cc24e9a
DY
1293 assertUIntArg ('vs_id');
1294 assertIPv4Arg ('vip');
1295 assertUIntArg ('vport');
1296 assertStringArg ('proto');
1297 assertStringArg ('name', TRUE);
1298 assertStringArg ('vsconfig', TRUE);
1299 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1300 if (!commitUpdateVS (
1301 $_REQUEST['vs_id'],
1302 $_REQUEST['vip'],
1303 $_REQUEST['vport'],
1304 $_REQUEST['proto'],
1305 $_REQUEST['name'],
1306 $_REQUEST['vsconfig'],
1307 $_REQUEST['rsconfig']
1308 ))
135080d8 1309 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1310 else
135080d8 1311 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1312}
1313
b6a7d936
DO
1314$msgcode['addLoadBalancer']['OK'] = 18;
1315$msgcode['addLoadBalancer']['ERR'] = 137;
3241551e
DO
1316function addLoadBalancer ()
1317{
0cc24e9a
DY
1318 assertUIntArg ('pool_id');
1319 assertUIntArg ('object_id');
1320 assertUIntArg ('vs_id');
1321 assertStringArg ('vsconfig', TRUE);
1322 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1323 if (!addLBtoRSPool (
1324 $_REQUEST['pool_id'],
1325 $_REQUEST['object_id'],
1326 $_REQUEST['vs_id'],
1327 $_REQUEST['vsconfig'],
1328 $_REQUEST['rsconfig']
1329 ))
135080d8 1330 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1331 else
135080d8 1332 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1333}
1334
b6a7d936
DO
1335$msgcode['addRSPool']['OK'] = 31;
1336$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1337function addRSPool ()
1338{
0cc24e9a
DY
1339 assertStringArg ('name', TRUE);
1340 assertStringArg ('vsconfig', TRUE);
1341 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1342 $error = commitCreateRSPool
1343 (
103b1e1e
DO
1344 $_REQUEST['name'],
1345 $_REQUEST['vsconfig'],
c63a8d6e
DO
1346 $_REQUEST['rsconfig'],
1347 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1348 );
1349 if ($error != '')
135080d8 1350 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1351 else
135080d8 1352 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1353}
1354
b6a7d936
DO
1355$msgcode['deleteRSPool']['OK'] = 32;
1356$msgcode['deleteRSPool']['ERR'] = 138;
5ad76f01
DO
1357function deleteRSPool ()
1358{
0cc24e9a 1359 assertUIntArg ('pool_id');
841a7a7a 1360 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
135080d8 1361 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1362 else
135080d8 1363 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1364}
1365
b6a7d936
DO
1366$msgcode['updateRSPool']['OK'] = 33;
1367$msgcode['updateRSPool']['ERR'] = 139;
5ad76f01
DO
1368function updateRSPool ()
1369{
0cc24e9a
DY
1370 assertUIntArg ('pool_id');
1371 assertStringArg ('name', TRUE);
1372 assertStringArg ('vsconfig', TRUE);
1373 assertStringArg ('rsconfig', TRUE);
841a7a7a 1374 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
135080d8 1375 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1376 else
135080d8 1377 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1378}
1379
b6a7d936
DO
1380$msgcode['updateRSInService']['OK'] = 38;
1381$msgcode['updateRSInService']['ERR'] = 140;
1f7d18fa
DO
1382function updateRSInService ()
1383{
0cc24e9a 1384 assertUIntArg ('rscount');
841a7a7a 1385 $pool_id = $_REQUEST['pool_id'];
a6e91ac2
DO
1386 $orig = spotEntity ('ipv4rspool', $pool_id);
1387 amplifyCell ($orig);
1f7d18fa
DO
1388 $nbad = $ngood = 0;
1389 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1390 {
1391 $rs_id = $_REQUEST["rsid_${i}"];
1392 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1393 $newval = 'yes';
1394 else
1395 $newval = 'no';
1396 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1397 {
1398 if (commitSetInService ($rs_id, $newval))
1399 $ngood++;
1400 else
1401 $nbad++;
1402 }
1403 }
1404 if (!$nbad)
135080d8 1405 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1406 else
135080d8 1407 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1408}
1409
b6a7d936
DO
1410$msgcode['importPTRData']['OK'] = 26;
1411$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1412// FIXME: check, that each submitted address belongs to the prefix we
1413// are operating on.
03eb5209
DO
1414function importPTRData ()
1415{
0cc24e9a 1416 assertUIntArg ('addrcount');
03eb5209 1417 $nbad = $ngood = 0;
03eb5209
DO
1418 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1419 {
3f3bd41e 1420 $inputname = "import_${i}";
03eb5209
DO
1421 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1422 continue;
0cc24e9a
DY
1423 assertIPv4Arg ("addr_${i}");
1424 assertStringArg ("descr_${i}", TRUE);
1425 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1426 // Non-existent addresses will not have this argument set in request.
1427 $rsvd = 'no';
03eb5209
DO
1428 if ($_REQUEST["rsvd_${i}"] == 'yes')
1429 $rsvd = 'yes';
3f3bd41e 1430 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1431 $ngood++;
1432 else
1433 $nbad++;
1434 }
1435 if (!$nbad)
135080d8 1436 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1437 else
135080d8 1438 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1439}
1440
b6a7d936
DO
1441$msgcode['generateAutoPorts']['OK'] = 21;
1442$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1443function generateAutoPorts ()
1444{
103b1e1e 1445 global $pageno;
0cc24e9a 1446 assertUIntArg ('object_id');
6297d584 1447 $info = spotEntity ('object', $_REQUEST['object_id']);
f3f0161f 1448 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1449 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1450 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1451 else
135080d8 1452 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1453}
1454
b6a7d936
DO
1455$msgcode['saveEntityTags']['OK'] = 22;
1456$msgcode['saveEntityTags']['ERR1'] = 143;
1457$msgcode['saveEntityTags']['ERR2'] = 187;
24cbe8af 1458// Filter out implicit tags before storing the new tag set.
3355ca56 1459function saveEntityTags ()
24cbe8af 1460{
eb27afad 1461 global $page, $pageno, $etype_by_pageno;
be2ae2a2 1462 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
9a61c175 1463 return buildRedirectURL (__FUNCTION__, 'ERR2', array (__FUNCTION__));
be2ae2a2 1464 $realm = $etype_by_pageno[$pageno];
3355ca56 1465 $bypass = $page[$pageno]['bypass'];
0cc24e9a 1466 assertUIntArg ($bypass);
2034d968 1467 $entity_id = $_REQUEST[$bypass];
e04931c8 1468 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1469 // Build a chain from the submitted data, minimize it,
ab379543 1470 // then wipe existing records and store the new set instead.
01b6b4d6 1471 destroyTagsForEntity ($realm, $entity_id);
eb27afad
DO
1472 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1473 // so why not use it?
6e49bd1f 1474 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1475 $n_succeeds = $n_errors = 0;
6e49bd1f 1476 foreach ($newchain as $taginfo)
eb6ea26f 1477 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1478 $n_succeeds++;
1479 else
1480 $n_errors++;
ab379543 1481 if ($n_errors)
9a61c175 1482 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1483 else
135080d8 1484 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1485}
1486
b6a7d936
DO
1487$msgcode['destroyTag']['OK'] = 58;
1488$msgcode['destroyTag']['ERR1'] = 183;
1489$msgcode['destroyTag']['ERR2'] = 144;
fe7044ad
DO
1490function destroyTag ()
1491{
0cc24e9a 1492 assertUIntArg ('tag_id');
c615a655
DO
1493 global $taglist;
1494 if (!isset ($taglist[$_REQUEST['tag_id']]))
1495 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['tag_id']));
49fb1027 1496 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
c615a655 1497 return buildRedirectURL (__FUNCTION__, 'OK', array ($taglist[$_REQUEST['tag_id']]['tag']));
fe7044ad 1498 else
c615a655 1499 return buildRedirectURL (__FUNCTION__, 'ERR2');
fe7044ad
DO
1500}
1501
b6a7d936
DO
1502$msgcode['createTag']['OK'] = 59;
1503$msgcode['createTag']['ERR1'] = 145;
1504$msgcode['createTag']['ERR3'] = 147;
fe7044ad
DO
1505function createTag ()
1506{
0cc24e9a
DY
1507 assertStringArg ('tag_name');
1508 assertUIntArg ('parent_id', TRUE);
49fb1027 1509 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1510 if (!validTagName ($tagname))
135080d8 1511 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
fe7044ad
DO
1512 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1513 $parent_id = 'NULL';
1514 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
135080d8 1515 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
fe7044ad 1516 else
135080d8 1517 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
fe7044ad
DO
1518}
1519
b6a7d936
DO
1520$msgcode['updateTag']['OK'] = 60;
1521$msgcode['updateTag']['ERR1'] = 145;
1522$msgcode['updateTag']['ERR2'] = 148;
49fb1027
DO
1523function updateTag ()
1524{
0cc24e9a
DY
1525 assertUIntArg ('tag_id');
1526 assertUIntArg ('parent_id', TRUE);
1527 assertStringArg ('tag_name');
49fb1027 1528 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1529 if (!validTagName ($tagname))
135080d8 1530 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1531 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1532 $parent_id = 'NULL';
1533 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
135080d8 1534 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
590e1281
DO
1535 // Use old name in the message, cause update failed.
1536 global $taglist;
1537 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag'], $ret));
49fb1027
DO
1538}
1539
b6a7d936
DO
1540$msgcode['rollTags']['OK'] = 67;
1541$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1542function rollTags ()
1543{
0cc24e9a
DY
1544 assertUIntArg ('row_id');
1545 assertStringArg ('sum', TRUE);
1546 assertUIntArg ('realsum');
eb6ea26f 1547 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1548 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1549 // Even if the user requested an empty tag list, don't bail out, but process existing
1550 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1551 // have its chains refined to "normal" form.
1552 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1553 $n_ok = 0;
1554 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1555 // filter out the same tag again and again. It will have own noise to cancel.
1556 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
61a1d996 1557 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
abef7149 1558 {
61a1d996 1559 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
abef7149 1560 $n_ok++;
61a1d996
DO
1561 amplifyCell ($rack);
1562 foreach ($rack['mountedObjects'] as $object_id)
abef7149
DO
1563 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1564 $n_ok++;
1565 }
135080d8 1566 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1567}
1568
b6a7d936
DO
1569$msgcode['changeMyPassword']['OK'] = 61;
1570$msgcode['changeMyPassword']['ERR1'] = 150;
1571$msgcode['changeMyPassword']['ERR2'] = 151;
1572$msgcode['changeMyPassword']['ERR3'] = 152;
1573$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1574function changeMyPassword ()
cced6b7d 1575{
b82cce3f 1576 global $remote_username, $user_auth_src;
204284ba 1577 if ($user_auth_src != 'database')
135080d8 1578 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1579 assertStringArg ('oldpassword');
1580 assertStringArg ('newpassword1');
1581 assertStringArg ('newpassword2');
b82cce3f 1582 $remote_userid = getUserIDByUsername ($remote_username);
0b2c74cb 1583 $userinfo = spotEntity ('user', $remote_userid);
b82cce3f 1584 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1585 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1586 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1587 return buildRedirectURL (__FUNCTION__, 'ERR3');
b82cce3f 1588 if (commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1589 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1590 else
135080d8 1591 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1592}
1593
b6a7d936
DO
1594$msgcode['saveRackCode']['OK'] = 43;
1595$msgcode['saveRackCode']['ERR1'] = 154;
1596$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1597function saveRackCode ()
1598{
cced6b7d 1599 assertStringArg ('rackcode');
e6a4adb9
DO
1600 // For the test to succeed, unescape LFs, strip CRs.
1601 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1602 $parseTree = getRackCode ($newcode);
1603 if ($parseTree['result'] != 'ACK')
135080d8 1604 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
4a6a28f1 1605 saveScript ('RackCodeCache', '');
cf25e649 1606 if (saveScript ('RackCode', $newcode))
135080d8 1607 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1608 else
135080d8 1609 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1610}
1611
b6a7d936 1612$msgcode['setPortVLAN']['ERR1'] = 156;
46f92ff7
DO
1613// This handler's context is pre-built, but not authorized. It is assumed, that the
1614// handler will take existing context and before each commit check authorization
1615// on the base chain plus necessary context added.
1616function setPortVLAN ()
1617{
0cc24e9a 1618 assertUIntArg ('portcount');
46f92ff7
DO
1619 $data = getSwitchVLANs ($_REQUEST['object_id']);
1620 if ($data === NULL)
135080d8 1621 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1622 list ($vlanlist, $portlist) = $data;
1623 // Here we just build up 1 set command for the gateway with all of the ports
1624 // included. The gateway is expected to filter unnecessary changes silently
1625 // and to provide a list of responses with either error or success message
1626 // for each of the rest.
1627 $nports = $_REQUEST['portcount'];
1628 $prefix = 'set ';
db55cf54 1629 $log = emptyLog();
46f92ff7
DO
1630 $setcmd = '';
1631 for ($i = 0; $i < $nports; $i++)
1632 if
1633 (
1634 !isset ($_REQUEST['portname_' . $i]) ||
1635 !isset ($_REQUEST['vlanid_' . $i]) ||
1636 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1637 )
24dcb9d8 1638 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1639 elseif
1640 (
1641 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1642 $portlist[$i]['vlaind'] == 'TRUNK'
1643 )
1644 continue;
1645 else
1646 {
1647 $portname = $_REQUEST['portname_' . $i];
1648 $oldvlanid = $portlist[$i]['vlanid'];
1649 $newvlanid = $_REQUEST['vlanid_' . $i];
1650 // Finish the security context and evaluate it.
1651 $annex = array();
1652 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1653 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1654 if (!permitted (NULL, NULL, NULL, $annex))
1655 {
24dcb9d8 1656 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1657 continue;
1658 }
1659 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1660 $prefix = ';';
1661 }
1662 // Feed the gateway and interpret its (non)response.
1663 if ($setcmd != '')
f0ff4930 1664 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1665 else
24dcb9d8 1666 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1667 return buildWideRedirectURL ($log);
1668}
1669
2987fc1f
DO
1670function submitSLBConfig ()
1671{
0cc24e9a 1672 assertUIntArg ('object_id');
2987fc1f 1673 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
ff0df5c3 1674 $msglog = gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1675 return buildWideRedirectURL ($msglog);
1676}
1677
b6a7d936
DO
1678$msgcode['addRow']['OK'] = 74;
1679$msgcode['addRow']['ERR'] = 100;
345fd640
AD
1680function addRow ()
1681{
0cc24e9a 1682 assertStringArg ('name');
345fd640 1683
10bac82a 1684 if (commitAddRow ($_REQUEST['name']) === TRUE)
135080d8 1685 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1686 else
135080d8 1687 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1688}
1689
b6a7d936
DO
1690$msgcode['updateRow']['OK'] = 75;
1691$msgcode['updateRow']['ERR'] = 100;
345fd640
AD
1692function updateRow ()
1693{
0cc24e9a
DY
1694 assertUIntArg ('row_id');
1695 assertStringArg ('name');
345fd640 1696
10bac82a 1697 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1698 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1699 else
135080d8 1700 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1701}
1702
b6a7d936
DO
1703$msgcode['deleteRow']['OK'] = 77;
1704$msgcode['deleteRow']['ERR'] = 100;
9f14a7ef
DY
1705function deleteRow ()
1706{
0cc24e9a 1707 assertUIntArg ('row_id');
7be51fea 1708 $rowinfo = getRackRowInfo ($_REQUEST['row_id']);
9f14a7ef
DY
1709
1710 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
7be51fea 1711 return buildRedirectURL (__FUNCTION__, 'OK', array ($rowinfo['name']));
9f14a7ef 1712 else
7be51fea 1713 return buildRedirectURL (__FUNCTION__, 'ERR', array ($rowinfo['name']));
9f14a7ef
DY
1714}
1715
b6a7d936
DO
1716$msgcode['addRack']['OK'] = 65;
1717$msgcode['addRack']['ERR1'] = 171;
1718$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1719function addRack ()
1720{
0cc24e9a 1721 assertUIntArg ('row_id');
f19c75d6
DO
1722 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1723 if (isset ($_REQUEST['got_data']))
1724 {
0cc24e9a
DY
1725 assertStringArg ('rack_name');
1726 assertUIntArg ('rack_height1');
1727 assertStringArg ('rack_comment', TRUE);
f19c75d6
DO
1728
1729 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1730 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1731 else
135080d8 1732 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1733 }
1734 elseif (isset ($_REQUEST['got_mdata']))
1735 {
0cc24e9a
DY
1736 assertUIntArg ('rack_height2');
1737 assertStringArg ('rack_names', TRUE);
db55cf54 1738 $log = emptyLog();
f19c75d6
DO
1739 // copy-and-paste from renderAddMultipleObjectsForm()
1740 $names1 = explode ('\n', $_REQUEST['rack_names']);
1741 $names2 = array();
1742 foreach ($names1 as $line)
1743 {
1744 $parts = explode ('\r', $line);
1745 reset ($parts);
59a83bd8 1746 if (!strlen ($parts[0]))
f19c75d6
DO
1747 continue;
1748 else
1749 $names2[] = rtrim ($parts[0]);
1750 }
b3029d5c 1751 global $msgcode;
f19c75d6
DO
1752 foreach ($names2 as $cname)
1753 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1754 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1755 else
b3029d5c 1756 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1757 return buildWideRedirectURL ($log);
1758 }
1759 else
135080d8 1760 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1761}
1762
b6a7d936
DO
1763$msgcode['deleteRack']['OK'] = 79;
1764$msgcode['deleteRack']['ERR'] = 100;
1765$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1766function deleteRack ()
1767{
0cc24e9a 1768 assertUIntArg ('rack_id');
61a1d996
DO
1769 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1770 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1771 amplifyCell ($rackData);
1772 if (count ($rackData['mountedObjects']))
c8187437 1773 return buildRedirectURL (__FUNCTION__, 'ERR1');
61a1d996
DO
1774 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1775 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1776 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
c8187437
DY
1777}
1778
b6a7d936
DO
1779$msgcode['updateRack']['OK'] = 68;
1780$msgcode['updateRack']['ERR'] = 177;
7056988c
DO
1781function updateRack ()
1782{
0cc24e9a
DY
1783 assertUIntArg ('rack_id');
1784 assertUIntArg ('rack_row_id');
1785 assertUIntArg ('rack_height');
1786 assertStringArg ('rack_name');
1787 assertStringArg ('rack_comment', TRUE);
7056988c 1788
7056988c 1789 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1790 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1791 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1792 else
135080d8 1793 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1794}
1795
61a1d996 1796$msgcode['updateRackDesign']['ERR'] = 100;
4fbb5a00
DY
1797function updateRackDesign ()
1798{
0cc24e9a 1799 assertUIntArg ('rack_id');
61a1d996
DO
1800 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1801 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1802 amplifyCell ($rackData);
4fbb5a00
DY
1803 applyRackDesignMask($rackData);
1804 markupObjectProblems ($rackData);
1805 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1806 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1807}
1808
61a1d996 1809$msgcode['updateRackProblems']['ERR'] = 100;
4fbb5a00
DY
1810function updateRackProblems ()
1811{
0cc24e9a 1812 assertUIntArg ('rack_id');
61a1d996
DO
1813 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1814 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1815 amplifyCell ($rackData);
4fbb5a00
DY
1816 applyRackProblemMask($rackData);
1817 markupObjectProblems ($rackData);
1818 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1819 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1820}
1821
7056988c
DO
1822function querySNMPData ()
1823{
0cc24e9a
DY
1824 assertUIntArg ('object_id');
1825 assertStringArg ('community');
8536e20a 1826 return doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']);
7056988c
DO
1827}
1828
b6a7d936
DO
1829$msgcode['addFileWithoutLink']['OK'] = 69;
1830$msgcode['addFileWithoutLink']['ERR'] = 100;
e1ae3fb4
AD
1831// File-related functions
1832function addFileWithoutLink ()
1833{
0cc24e9a 1834 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1835
1836 // Make sure the file can be uploaded
1837 if (get_cfg_var('file_uploads') != 1)
135080d8 1838 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1839
1840 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1841 global $sic;
1842 // commitAddFile() uses prepared statements
1843 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
f857f71f
DO
1844 if (isset ($_REQUEST['taglist']))
1845 produceTagsForLastRecord ('file', $_REQUEST['taglist']);
e1ae3fb4
AD
1846
1847 if ($error != '')
135080d8 1848 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1849
135080d8 1850 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1851}
1852
b6a7d936
DO
1853$msgcode['addFileToEntity']['OK'] = 69;
1854$msgcode['addFileToEntity']['ERR1'] = 187;
1855$msgcode['addFileToEntity']['ERR2'] = 181;
1856$msgcode['addFileToEntity']['ERR3'] = 182;
e1ae3fb4
AD
1857function addFileToEntity ()
1858{
9a61c175
DO
1859 global $page, $pageno, $etype_by_pageno;
1860 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1861 return buildRedirectURL (__FUNCTION__, 'ERR1', array (__FUNCTION__));
1862 $realm = $etype_by_pageno[$pageno];
1863 $bypass = $page[$pageno]['bypass'];
0cc24e9a 1864 assertUIntArg ($bypass);
9a61c175 1865 $entity_id = $_REQUEST[$bypass];
0cc24e9a 1866 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1867
1868 // Make sure the file can be uploaded
1869 if (get_cfg_var('file_uploads') != 1)
9a61c175 1870 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
1871
1872 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1873 global $sic;
1874 // commitAddFile() uses prepared statements
1875 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
e1ae3fb4 1876 if ($error != '')
9a61c175 1877 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1878
9a61c175 1879 $error = commitLinkFile (lastInsertID(), $realm, $entity_id);
e1ae3fb4 1880 if ($error != '')
9a61c175 1881 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1882
135080d8 1883 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1884}
1885
b6a7d936
DO
1886$msgcode['linkFileToEntity']['OK'] = 71;
1887$msgcode['linkFileToEntity']['ERR1'] = 178;
1888$msgcode['linkFileToEntity']['ERR2'] = 100;
e1ae3fb4
AD
1889function linkFileToEntity ()
1890{
0cc24e9a 1891 assertUIntArg ('file_id');
482c7f35 1892 global $page, $pageno, $etype_by_pageno;
121496b6
DO
1893 $entity_type = $etype_by_pageno[$pageno];
1894 $bypass_name = $page[$pageno]['bypass'];
0cc24e9a 1895 assertUIntArg ($bypass_name);
121496b6 1896
d3b5008b 1897 $fi = spotEntity ('file', $_REQUEST['file_id']);
121496b6 1898 if ($fi === NULL)
135080d8 1899 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
121496b6 1900 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
e1ae3fb4 1901 if ($error != '')
135080d8 1902 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
e1ae3fb4 1903
135080d8 1904 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
e1ae3fb4
AD
1905}
1906
b6a7d936
DO
1907$msgcode['replaceFile']['OK'] = 70;
1908$msgcode['replaceFile']['ERR1'] = 181;
1909$msgcode['replaceFile']['ERR2'] = 207;
1910$msgcode['replaceFile']['ERR3'] = 182;
fd1fb420
AD
1911function replaceFile ()
1912{
db55cf54 1913 global $sic;
0cc24e9a 1914 assertUIntArg ('file_id');
fd1fb420
AD
1915
1916 // Make sure the file can be uploaded
1917 if (get_cfg_var('file_uploads') != 1)
db55cf54 1918 return buildRedirectURL (__FUNCTION__, 'ERR1');
d3b5008b 1919 $shortInfo = spotEntity ('file', $sic['file_id']);
fd1fb420
AD
1920
1921 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1922 if ($fp === FALSE)
1923 return buildRedirectURL (__FUNCTION__, 'ERR2');
f8874cdb 1924 $error = commitReplaceFile ($sic['file_id'], $fp);
fd1fb420 1925 if ($error != '')
db55cf54 1926 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
fd1fb420 1927
db55cf54 1928 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1929}
1930
b6a7d936
DO
1931$msgcode['updateFile']['OK'] = 70;
1932$msgcode['updateFile']['ERR'] = 100;
e1ae3fb4
AD
1933function updateFile ()
1934{
0cc24e9a
DY
1935 assertUIntArg ('file_id');
1936 assertStringArg ('file_name');
1937 assertStringArg ('file_type');
1938 assertStringArg ('file_comment', TRUE);
f3d274bf
DO
1939 // prepared statement params below
1940 global $sic;
1941 $error = commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']);
e1ae3fb4 1942 if ($error != '')
135080d8 1943 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1944
135080d8 1945 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1946}
1947
b6a7d936
DO
1948$msgcode['unlinkFile']['OK'] = 72;
1949$msgcode['unlinkFile']['ERR'] = 182;
e1ae3fb4
AD
1950function unlinkFile ()
1951{
0cc24e9a 1952 assertUIntArg ('link_id');
e1ae3fb4
AD
1953 $error = commitUnlinkFile ($_REQUEST['link_id']);
1954
1955 if ($error != '')
135080d8 1956 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1957
8bc5d1e4 1958 return buildRedirectURL (__FUNCTION__, 'OK');
e1ae3fb4
AD
1959}
1960
b6a7d936
DO
1961$msgcode['deleteFile']['OK'] = 73;
1962$msgcode['deleteFile']['ERR'] = 100;
e1ae3fb4
AD
1963function deleteFile ()
1964{
0cc24e9a 1965 assertUIntArg ('file_id');
d3b5008b 1966 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
e1ae3fb4
AD
1967 $error = commitDeleteFile ($_REQUEST['file_id']);
1968
1969 if ($error != '')
135080d8 1970 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1971
f8874cdb 1972 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1973}
1974
b6a7d936
DO
1975$msgcode['updateFileText']['OK'] = 78;
1976$msgcode['updateFileText']['ERR1'] = 179;
1977$msgcode['updateFileText']['ERR2'] = 180;
8628ae44
DO
1978function updateFileText ()
1979{
0cc24e9a
DY
1980 assertUIntArg ('file_id');
1981 assertStringArg ('mtime_copy');
1982 assertStringArg ('file_text', TRUE); // it's Ok to save empty
d3b5008b 1983 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
70cb9b56
DO
1984 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1985 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb
DO
1986 global $sic;
1987 $error = commitReplaceFile ($sic['file_id'], $sic['file_text']);
8628ae44 1988 if ($error == '')
f8874cdb 1989 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
70cb9b56 1990 return buildRedirectURL (__FUNCTION__, 'ERR2');
8628ae44
DO
1991}
1992
2dfa1b73
DO
1993$msgcode['addPortInterfaceCompat']['OK'] = 48;
1994$msgcode['addPortInterfaceCompat']['ERR'] = 110;
1995function addPortInterfaceCompat ()
1996{
0cc24e9a
DY
1997 assertUIntArg ('iif_id');
1998 assertUIntArg ('oif_id');
2dfa1b73
DO
1999 if (commitSupplementPIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
2000 return buildRedirectURL (__FUNCTION__, 'OK');
2001 return buildRedirectURL (__FUNCTION__, 'ERR');
2002}
2003
2004$msgcode['delPortInterfaceCompat']['OK'] = 49;
2005$msgcode['delPortInterfaceCompat']['ERR'] = 111;
2006function delPortInterfaceCompat ()
2007{
0cc24e9a
DY
2008 assertUIntArg ('iif_id');
2009 assertUIntArg ('oif_id');
2dfa1b73
DO
2010 if (commitReducePIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
2011 return buildRedirectURL (__FUNCTION__, 'OK');
2012 return buildRedirectURL (__FUNCTION__, 'ERR');
2013}
2014
2400d7ec
DO
2015$ifcompatpack = array
2016(
2017 '1000cwdm80' => array (1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216),
2018 '1000dwdm80' => array // ITU channels 20~61
2019 (
2020 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226,
2021 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236,
2022 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
2023 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
2024 1257, 1258
2025 ),
2026 '10000dwdm80' => array // same channels for 10GE
2027 (
2028 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268,
2029 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278,
2030 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
2031 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298,
2032 1299, 1300
2033 ),
2034);
2035
3d9ac3d4
DO
2036$msgcode['addPortInterfaceCompatPack']['OK'] = 44;
2037$msgcode['addPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2038function addPortInterfaceCompatPack ()
2039{
0cc24e9a
DY
2040 assertStringArg ('standard');
2041 assertUIntArg ('iif_id');
3d9ac3d4
DO
2042 global $ifcompatpack;
2043 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
2044 return buildRedirectURL (__FUNCTION__, 'ERR');
2045 $ngood = $nbad = 0;
2046 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2047 if (commitSupplementPIC ($_REQUEST['iif_id'], $oif_id))
2048 $ngood++;
2049 else
2050 $nbad++;
2051 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2052}
2053
3d9ac3d4
DO
2054$msgcode['delPortInterfaceCompatPack']['OK'] = 44;
2055$msgcode['delPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2056function delPortInterfaceCompatPack ()
2057{
0cc24e9a
DY
2058 assertStringArg ('standard');
2059 assertUIntArg ('iif_id');
3d9ac3d4
DO
2060 global $ifcompatpack;
2061 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
2062 return buildRedirectURL (__FUNCTION__, 'ERR');
2063 $ngood = $nbad = 0;
2064 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2065 if (commitReducePIC ($_REQUEST['iif_id'], $oif_id))
2066 $ngood++;
2067 else
2068 $nbad++;
2069 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2070}
2071
a310dd80
DO
2072$msgcode['addPortOIFCompat']['OK'] = 48;
2073$msgcode['addPortOIFCompat']['ERR'] = 110;
2074function addPortOIFCompat()
2075{
80ec1f4f
DO
2076 assertUIntArg('type1');
2077 assertUIntArg('type2');
a310dd80
DO
2078 if (commitSupplementPOIFC($_REQUEST['type1'], $_REQUEST['type2']))
2079 return buildRedirectURL(__FUNCTION__, 'OK');
2080 return buildRedirectURL(__FUNCTION__, 'ERR');
2081}
2082
2083$msgcode['delPortOIFCompat']['OK'] = 49;
2084$msgcode['delPortOIFCompat']['ERR'] = 111;
2085function delPortOIFCompat ()
2086{
80ec1f4f
DO
2087 assertUIntArg('type1');
2088 assertUIntArg('type2');
a310dd80
DO
2089 if (commitReducePOIFC ($_REQUEST['type1'], $_REQUEST['type2']))
2090 return buildRedirectURL (__FUNCTION__, 'OK');
2091 return buildRedirectURL (__FUNCTION__, 'ERR');
2092
2093}
2094
38cd7704
DO
2095$msgcode['add8021QOrder']['OK'] = 48;
2096$msgcode['add8021QOrder']['ERR'] = 118;
2097function add8021QOrder ()
8198f2c6 2098{
80ec1f4f
DO
2099 assertUIntArg ('vdom_id');
2100 assertUIntArg ('object_id');
38cd7704 2101 assertUIntArg ('vst_id');
d6ac0bea 2102 global $sic, $dbxlink;
8198f2c6
DO
2103 $result = usePreparedInsertBlade
2104 (
2105 'VLANSwitch',
2106 array
2107 (
2108 'domain_id' => $sic['vdom_id'],
2109 'object_id' => $sic['object_id'],
38cd7704 2110 'template_id' => $sic['vst_id'],
8198f2c6
DO
2111 )
2112 );
d6ac0bea
DO
2113 $query = $dbxlink->prepare ('UPDATE VLANSwitch SET last_change = NOW(), out_of_sync = "yes" WHERE object_id = ?');
2114 $query->execute (array ($sic['object_id']));
8198f2c6
DO
2115 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2116}
2117
38cd7704
DO
2118$msgcode['del8021QOrder']['OK'] = 49;
2119$msgcode['del8021QOrder']['ERR'] = 119;
2120function del8021QOrder ()
8198f2c6 2121{
80ec1f4f 2122 assertUIntArg ('object_id');
8198f2c6 2123 global $sic;
38cd7704 2124 $result = usePreparedDeleteBlade ('VLANSwitch', array ('object_id' => $sic['object_id']));
8198f2c6
DO
2125 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2126}
2127
2128$msgcode['addVLANDescription']['OK'] = 48;
2129$msgcode['addVLANDescription']['ERR1'] = 190;
2130$msgcode['addVLANDescription']['ERR2'] = 110;
2131function addVLANDescription ()
2132{
80ec1f4f 2133 assertUIntArg ('vlan_id');
5b989e4d 2134 assertStringArg ('vlan_type', TRUE);
80ec1f4f 2135 assertStringArg ('vlan_descr', TRUE);
8198f2c6
DO
2136 global $sic;
2137 if (!($sic['vlan_id'] >= VLAN_MIN_ID and $sic['vlan_id'] <= VLAN_MAX_ID))
2138 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($sic['vlan_id']));
2139 $result = usePreparedInsertBlade
2140 (
2141 'VLANDescription',
2142 array
2143 (
2144 'domain_id' => $sic['vdom_id'],
2145 'vlan_id' => $sic['vlan_id'],
5b989e4d 2146 'vlan_type' => $sic['vlan_type'],
5286b29f 2147 'vlan_descr' => mb_strlen ($sic['vlan_descr']) ? $sic['vlan_descr'] : NULL
8198f2c6
DO
2148 )
2149 );
2150 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR2');
2151}
2152
2153$msgcode['delVLANDescription']['OK'] = 49;
2154$msgcode['delVLANDescription']['ERR'] = 111;
2155function delVLANDescription ()
2156{
80ec1f4f 2157 assertUIntArg ('vlan_id');
8198f2c6
DO
2158 global $sic;
2159 $result = commitReduceVLANDescription ($sic['vdom_id'], $sic['vlan_id']);
2160 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2161}
2162
2163$msgcode['updVLANDescription']['OK'] = 51;
2164$msgcode['updVLANDescription']['ERR'] = 109;
2165function updVLANDescription ()
2166{
80ec1f4f
DO
2167 assertUIntArg ('vlan_id');
2168 assertStringArg ('vlan_type');
2169 assertStringArg ('vlan_descr', TRUE);
8198f2c6
DO
2170 global $sic;
2171 $result = commitUpdateVLANDescription
2172 (
2173 $sic['vdom_id'],
2174 $sic['vlan_id'],
0dabdc53 2175 $sic['vlan_type'],
8198f2c6
DO
2176 $sic['vlan_descr']
2177 );
2178 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2179}
2180
2181$msgcode['createVLANDomain']['OK'] = 48;
2182$msgcode['createVLANDomain']['ERR'] = 110;
2183function createVLANDomain ()
2184{
80ec1f4f 2185 assertStringArg ('vdom_descr');
8198f2c6
DO
2186 global $sic;
2187 $result = usePreparedInsertBlade
2188 (
2189 'VLANDomain',
2190 array
2191 (
2192 'description' => $sic['vdom_descr'],
2193 )
2194 );
2195 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2196}
2197
2198$msgcode['destroyVLANDomain']['OK'] = 49;
2199$msgcode['destroyVLANDomain']['ERR'] = 111;
2200function destroyVLANDomain ()
2201{
80ec1f4f 2202 assertUIntArg ('vdom_id');
8198f2c6 2203 global $sic;
bbae3611 2204 $result = FALSE !== usePreparedDeleteBlade ('VLANDomain', array ('id' => $sic['vdom_id']));
8198f2c6
DO
2205 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2206}
2207
2208$msgcode['updateVLANDomain']['OK'] = 51;
2209$msgcode['updateVLANDomain']['ERR'] = 109;
2210function updateVLANDomain ()
2211{
80ec1f4f
DO
2212 assertUIntArg ('vdom_id');
2213 assertStringArg ('vdom_descr');
8198f2c6
DO
2214 global $sic;
2215 $result = commitUpdateVLANDomain ($sic['vdom_id'], $sic['vdom_descr']);
2216 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2217}
2218
18c26ec5 2219$msgcode['save8021QPorts']['OK'] = 63;
706d9175
DO
2220$msgcode['save8021QPorts']['ERR1'] = 160;
2221$msgcode['save8021QPorts']['ERR2'] = 109;
2222function save8021QPorts ()
8198f2c6 2223{
a7492e95 2224 global $sic, $dbxlink;
706d9175 2225 assertUIntArg ('nports');
70cff23f 2226 assertUIntArg ('mutex_rev', TRUE); // counts from 0
706d9175
DO
2227 if ($sic['nports'] == 1)
2228 {
2229 assertStringArg ('pn_0');
2230 $extra = array ('port_name' => $sic['pn_0']);
2231 }
2232 else
2233 $extra = array();
a7492e95 2234 $dbxlink->beginTransaction();
2996cbbb
DO
2235 try
2236 {
a7492e95
DO
2237 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
2238 throw new InvalidArgException ('object_id', $object_id, 'VLAN domain is not set for this object');
9c45ea37
DO
2239 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
2240 throw new RuntimeException ('expired form data');
af204724 2241 $changes = array();
706d9175
DO
2242 for ($i = 0; $i < $sic['nports']; $i++)
2243 {
2244 assertStringArg ('pn_' . $i);
2245 assertStringArg ('pm_' . $i);
706d9175
DO
2246 // An access port only generates form input for its native VLAN,
2247 // which we derive allowed VLAN list from.
2248 $native = isset ($sic['pnv_' . $i]) ? $sic['pnv_' . $i] : 0;
b36fc896 2249 switch ($sic["pm_${i}"])
706d9175 2250 {
b36fc896
DO
2251 case 'trunk':
2252# assertArrayArg ('pav_' . $i);
2253 $allowed = isset ($sic['pav_' . $i]) ? $sic['pav_' . $i] : array();
2254 break;
2255 case 'access':
2256 if ($native == 'same')
2257 continue 2;
706d9175
DO
2258 assertUIntArg ('pnv_' . $i);
2259 $allowed = array ($native);
b36fc896
DO
2260 break;
2261 default:
2262 throw new InvalidRequestArgException ("pm_${i}", $_REQUEST["pm_${i}"], 'unknown port mode');
706d9175 2263 }
af204724 2264 $changes[$sic['pn_' . $i]] = array
706d9175
DO
2265 (
2266 'mode' => $sic['pm_' . $i],
2267 'allowed' => $allowed,
2268 'native' => $native,
2269 );
2270 }
af204724 2271 $domain_vlanlist = getDomainVLANs ($vswitch['domain_id']);
9165e8f7 2272 $after = $before = apply8021QOrder ($vswitch['template_id'], getStored8021QConfig ($sic['object_id'], 'desired'));
af204724 2273 $changes = filter8021QChangeRequests
a7492e95 2274 (
af204724 2275 $domain_vlanlist,
9165e8f7 2276 $before,
af204724 2277 apply8021QOrder ($vswitch['template_id'], $changes)
a7492e95 2278 );
bb2024b9 2279 $changes = authorize8021QChangeRequests ($before, $changes);
af204724
DO
2280 foreach ($changes as $port_name => $port)
2281 $after[$port_name] = $port;
b3a27170 2282 $new_uplinks = filter8021QChangeRequests ($domain_vlanlist, $after, produceUplinkPorts ($domain_vlanlist, $after));
7455e4c0 2283 $npulled = replace8021QPorts ('desired', $vswitch['object_id'], $before, $changes);
b3a27170 2284 $nsaved_uplinks = replace8021QPorts ('desired', $vswitch['object_id'], $before, $new_uplinks);
2996cbbb
DO
2285 }
2286 catch (Exception $e)
861292ee 2287 {
a7492e95 2288 $dbxlink->rollBack();
706d9175 2289 return buildRedirectURL (__FUNCTION__, 'ERR2', array(), NULL, NULL, $extra);
861292ee 2290 }
b3a27170 2291 if ($npulled + $nsaved_uplinks)
ec4d604c 2292 {
d973196a 2293 $query = $dbxlink->prepare ('UPDATE VLANSwitch SET mutex_rev = mutex_rev + 1, last_change = NOW(), out_of_sync = "yes" WHERE object_id = ?');
ec4d604c
DO
2294 $query->execute (array ($sic['object_id']));
2295 }
a7492e95 2296 $dbxlink->commit();
b3a27170
DO
2297 if ($nsaved_uplinks)
2298 initiateUplinksReverb ($vswitch['object_id'], $new_uplinks);
2299 return buildRedirectURL (__FUNCTION__, 'OK', array ($npulled + $nsaved_uplinks), NULL, NULL, $extra);
8198f2c6
DO
2300}
2301
8846b060
DO
2302$msgcode['bindVLANtoIPv4']['OK'] = 48;
2303$msgcode['bindVLANtoIPv4']['ERR'] = 110;
2304function bindVLANtoIPv4 ()
2305{
2306 assertUIntArg ('id'); // network id
2307 global $sic;
2308 $result = commitSupplementVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2309 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2310}
2311
2312$msgcode['unbindVLANfromIPv4']['OK'] = 49;
2313$msgcode['unbindVLANfromIPv4']['ERR'] = 111;
2314function unbindVLANfromIPv4 ()
2315{
2316 assertUIntArg ('id'); // network id
2317 global $sic;
2318 $result = commitReduceVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2319 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2320}
2321
228c430c 2322$msgcode['process8021QSyncRequest']['OK'] = 63;
be28b696 2323$msgcode['process8021QSyncRequest']['ERR1'] = 109;
ca5d4cbc 2324$msgcode['process8021QSyncRequest']['ERR2'] = 191;
228c430c 2325function process8021QSyncRequest ()
be28b696 2326{
2e84a05b
DO
2327 // behave depending on current operation: exec8021QPull or exec8021QPush
2328 global $sic, $op;
be28b696
DO
2329 try
2330 {
2e84a05b 2331 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], $op == 'exec8021QPush'))
ca5d4cbc
DO
2332 return buildRedirectURL (__FUNCTION__, 'ERR2'); // specific case
2333 return buildRedirectURL (__FUNCTION__, 'OK', array ($done));
be28b696
DO
2334 }
2335 catch (Exception $e)
2336 {
ca5d4cbc 2337 return buildRedirectURL (__FUNCTION__, 'ERR1'); // generic failure
be28b696 2338 }
be28b696
DO
2339}
2340
2341$msgcode['resolve8021QConflicts']['OK'] = 63;
bcd14540
DO
2342$msgcode['resolve8021QConflicts']['ERR1'] = 179;
2343$msgcode['resolve8021QConflicts']['ERR2'] = 109;
be28b696 2344function resolve8021QConflicts ()
07de6bb3 2345{
a7492e95 2346 global $sic, $dbxlink;
70cff23f 2347 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de47b574
DO
2348 assertUIntArg ('nrows');
2349 // Divide submitted radio buttons into 3 groups:
19d1e731 2350 // left (saved version wins)
07de6bb3 2351 // asis (ignore)
19d1e731
DO
2352 // right (running version wins)
2353 $F = array();
339534a0 2354 for ($i = 0; $i < $sic['nrows']; $i++)
07de6bb3 2355 {
de47b574 2356 if (!array_key_exists ("i_${i}", $sic))
07de6bb3 2357 continue;
de47b574
DO
2358 // let's hope other inputs are in place
2359 switch ($sic["i_${i}"])
07de6bb3
DO
2360 {
2361 case 'left':
07de6bb3 2362 case 'right':
bcd14540 2363 $F[$sic["pn_${i}"]] = array
07de6bb3 2364 (
ca9e1c9d 2365 'mode' => $sic["rm_${i}"],
de47b574
DO
2366 'allowed' => $sic["ra_${i}"],
2367 'native' => $sic["rn_${i}"],
bcd14540 2368 'decision' => $sic["i_${i}"],
07de6bb3
DO
2369 );
2370 break;
2371 default:
2372 // don't care
2373 }
2374 }
a7492e95 2375 $dbxlink->beginTransaction();
07de6bb3
DO
2376 try
2377 {
a7492e95 2378 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
ec4d604c 2379 throw new InvalidArgException ('object_id', $sic['object_id'], 'VLAN domain is not set for this object');
9c45ea37 2380 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
bcd14540
DO
2381 {
2382 throw new RuntimeException ('expired form (table data has changed)');
2383 }
9c2201ba
DO
2384 $D = getStored8021QConfig ($vswitch['object_id'], 'desired');
2385 $C = getStored8021QConfig ($vswitch['object_id'], 'cached');
2386 $R = getRunning8021QConfig ($vswitch['object_id']);
d5e306b2 2387 $plan = get8021QSyncOptions ($vswitch, $D, $C, $R['portdata']);
bcd14540
DO
2388 $ndone = 0;
2389 foreach ($F as $port_name => $port)
2390 {
9c2201ba
DO
2391 if (!array_key_exists ($port_name, $plan))
2392 continue;
2393 elseif ($plan[$port_name]['status'] == 'merge_conflict')
bcd14540 2394 {
9c2201ba
DO
2395 // for R neither mutex nor revisions can be emulated, but revision change can be
2396 if (!same8021QConfigs ($port, $R['portdata'][$port_name]))
2397 throw new RuntimeException ('expired form (switch data has changed)');
1f8f67c2 2398 if ($port['decision'] == 'right') // D wins, frame R by writing value of R to C
9c2201ba 2399 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $port);
1f8f67c2 2400 elseif ($port['decision'] == 'left') // R wins, cross D up
9c2201ba
DO
2401 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $D[$port_name]);
2402 // otherwise there was no decision made
2403 }
1f8f67c2
DO
2404 elseif
2405 (
2406 $plan[$port_name]['status'] == 'delete_conflict' or
2407 $plan[$port_name]['status'] == 'martian_conflict'
2408 )
9c2201ba 2409 {
1f8f67c2 2410 if ($port['decision'] == 'left') // confirm deletion of local copy
9c2201ba 2411 $ndone += del8021QPort ($vswitch['object_id'], $port_name);
bcd14540 2412 }
9c2201ba 2413 // otherwise ignore a decision, which doesn't address a conflict
bcd14540 2414 }
07de6bb3 2415 }
bcd14540 2416 catch (RuntimeException $e)
07de6bb3 2417 {
a7492e95 2418 $dbxlink->rollBack();
bcd14540 2419 return buildRedirectURL (__FUNCTION__, 'ERR1');
ec4d604c 2420 }
bcd14540 2421 catch (Exception $e)
ec4d604c 2422 {
bcd14540
DO
2423 $dbxlink->rollBack();
2424 return buildRedirectURL (__FUNCTION__, 'ERR2');
ec4d604c 2425 }
a7492e95 2426 $dbxlink->commit();
bcd14540 2427 return buildRedirectURL (__FUNCTION__, 'OK', array ($ndone));
07de6bb3
DO
2428}
2429
e0d188ef
DO
2430$msgcode['addVLANSwitchTemplate']['OK'] = 48;
2431$msgcode['addVLANSwitchTemplate']['ERR'] = 110;
2432function addVLANSwitchTemplate()
2433{
2434 assertStringArg ('vst_descr');
2435 global $sic;
2436 $max_local_vlans = NULL;
2437 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2438 {
2439 assertUIntArg ('vst_maxvlans');
2440 $max_local_vlans = $sic['vst_maxvlans'];
2441 }
2442 $result = usePreparedInsertBlade
2443 (
2444 'VLANSwitchTemplate',
2445 array
2446 (
2447 'max_local_vlans' => $max_local_vlans,
2448 'description' => $sic['vst_descr'],
2449 )
2450 );
2451 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2452}
2453
2454$msgcode['delVLANSwitchTemplate']['OK'] = 49;
2455$msgcode['delVLANSwitchTemplate']['ERR'] = 111;
2456function delVLANSwitchTemplate()
2457{
2458 assertUIntArg ('vst_id');
2459 global $sic;
2460 $result = FALSE !== usePreparedDeleteBlade ('VLANSwitchTemplate', array ('id' => $sic['vst_id']));
2461 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2462}
2463
2464$msgcode['updVLANSwitchTemplate']['OK'] = 51;
2465$msgcode['updVLANSwitchTemplate']['ERR'] = 109;
2466function updVLANSwitchTemplate()
2467{
2468 assertUIntArg ('vst_id');
2469 assertStringArg ('vst_descr');
2470 global $sic;
2471 $max_local_vlans = NULL;
2472 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2473 {
2474 assertUIntArg ('vst_maxvlans');
2475 $max_local_vlans = $sic['vst_maxvlans'];
2476 }
2477 $result = commitUpdateVST ($sic['vst_id'], $max_local_vlans, $sic['vst_descr']);
2478 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2479}
2480
2481$msgcode['addVSTRule']['OK'] = 48;
2482$msgcode['addVSTRule']['ERR'] = 110;
2483function addVSTRule()
2484{
2485 assertUIntArg ('vst_id');
2486 assertUIntArg ('rule_no');
2487 assertPCREArg ('port_pcre');
2488 assertStringArg ('port_role');
2489 assertStringArg ('wrt_vlans', TRUE);
638f198f 2490 assertStringArg ('description', TRUE);
e0d188ef
DO
2491 global $sic;
2492 $result = usePreparedInsertBlade
2493 (
2494 'VLANSTRule',
2495 array
2496 (
2497 'vst_id' => $sic['vst_id'],
2498 'rule_no' => $sic['rule_no'],
2499 'port_pcre' => $sic['port_pcre'],
2500 'port_role' => $sic['port_role'],
2501 'wrt_vlans' => $sic['wrt_vlans'],
638f198f 2502 'description' => $sic['description'],
e0d188ef
DO
2503 )
2504 );
2505 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2506}
2507
2508$msgcode['delVSTRule']['OK'] = 49;
2509$msgcode['delVSTRule']['ERR'] = 111;
2510function delVSTRule()
2511{
2512 assertUIntArg ('vst_id');
2513 assertUIntArg ('rule_no');
2514 global $sic;
2515 $result = FALSE !== usePreparedDeleteBlade
2516 (
2517 'VLANSTRule',
2518 array
2519 (
2520 'vst_id' => $sic['vst_id'],
2521 'rule_no' => $sic['rule_no']
2522 )
2523 );
2524 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2525}
2526
2527$msgcode['updVSTRule']['OK'] = 51;
2528$msgcode['updVSTRule']['ERR'] = 109;
2529function updVSTRule()
2530{
2531 assertUIntArg ('vst_id');
2532 assertUIntArg ('rule_no');
2533 assertUIntArg ('new_rule_no');
2534 assertPCREArg ('port_pcre');
2535 assertStringArg ('port_role');
2536 assertStringArg ('wrt_vlans', TRUE);
d0751ba2 2537 assertStringArg ('description', TRUE);
e0d188ef
DO
2538 global $sic;
2539 $result = commitUpdateVSTRule
2540 (
2541 $sic['vst_id'],
2542 $sic['rule_no'],
2543 $sic['new_rule_no'],
2544 $sic['port_pcre'],
2545 $sic['port_role'],
d0751ba2
DO
2546 $sic['wrt_vlans'],
2547 $sic['description']
e0d188ef
DO
2548 );
2549 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2550}
2551
e673ee24 2552?>