r3623 add a reminder for 0.18.0 batch
[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');
407ed7bb
DO
1091 setConfigVar ('VLANSWITCH_LISTSRC', '');
1092 setConfigVar ('VLANIPV4NET_LISTSRC', '');
2fb1f79b
DO
1093 setConfigVar ('DEFAULT_VDOM_ID', '');
1094 setConfigVar ('DEFAULT_VST_ID', '');
01b9a31a 1095 setConfigVar ('STATIC_FILTER', 'yes');
37cb9e18
DO
1096 setConfigVar ('8021Q_DEPLOY_MINAGE', '300');
1097 setConfigVar ('8021Q_DEPLOY_MAXAGE', '3600');
1098 setConfigVar ('8021Q_DEPLOY_RETRY', '10800');
407ed7bb 1099 setConfigVar ('8021Q_WRI_AFTER_CONFT', 'no');
135080d8 1100 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
1101}
1102
b6a7d936
DO
1103$msgcode['addRealServer']['OK'] = 34;
1104$msgcode['addRealServer']['ERR'] = 126;
732e4578 1105// Add single record.
ca461127
DO
1106function addRealServer ()
1107{
0cc24e9a
DY
1108 assertUIntArg ('pool_id');
1109 assertIPv4Arg ('remoteip');
1110 assertStringArg ('rsport', TRUE);
1111 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1112 if (!addRStoRSPool (
1113 $_REQUEST['pool_id'],
1114 $_REQUEST['remoteip'],
1115 $_REQUEST['rsport'],
1116 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1117 $_REQUEST['rsconfig']
1118 ))
135080d8 1119 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1120 else
135080d8 1121 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1122}
1123
b6a7d936
DO
1124$msgcode['addRealServers']['OK'] = 37;
1125$msgcode['addRealServers']['ERR1'] = 131;
1126$msgcode['addRealServers']['ERR2'] = 127;
732e4578
DO
1127// Parse textarea submitted and try adding a real server for each line.
1128function addRealServers ()
1129{
0cc24e9a
DY
1130 assertUIntArg ('pool_id');
1131 assertStringArg ('format');
1132 assertStringArg ('rawtext');
732e4578
DO
1133 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1134 $ngood = $nbad = 0;
1135 $rsconfig = '';
1136 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1137 foreach (explode ('\n', $rawtext) as $line)
1138 {
59a83bd8 1139 if (!strlen ($line))
732e4578
DO
1140 continue;
1141 $match = array ();
1142 switch ($_REQUEST['format'])
1143 {
1144 case 'ipvs_2': // address and port only
1145 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1146 continue;
103b1e1e 1147 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1148 $ngood++;
1149 else
1150 $nbad++;
1151 break;
1152 case 'ipvs_3': // address, port and weight
1153 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1154 continue;
103b1e1e 1155 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1156 $ngood++;
1157 else
1158 $nbad++;
1159 break;
e69c2aa3
DO
1160 case 'ssv_2': // IP address and port
1161 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1162 continue;
103b1e1e 1163 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1164 $ngood++;
1165 else
1166 $nbad++;
1167 break;
948c37e3
DO
1168 case 'ssv_1': // IP address
1169 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1170 continue;
1171 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1172 $ngood++;
1173 else
1174 $nbad++;
1175 break;
732e4578 1176 default:
135080d8 1177 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1178 break;
1179 }
1180 }
1181 if ($nbad == 0 and $ngood > 0)
135080d8 1182 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1183 else
135080d8 1184 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1185}
1186
b6a7d936
DO
1187$msgcode['addVService']['OK'] = 28;
1188$msgcode['addVService']['ERR1'] = 132;
1189$msgcode['addVService']['ERR2'] = 100;
d6517a21
DO
1190function addVService ()
1191{
0cc24e9a
DY
1192 assertIPv4Arg ('vip');
1193 assertUIntArg ('vport');
1194 assertStringArg ('proto');
103b1e1e 1195 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1196 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1197 assertStringArg ('name', TRUE);
1198 assertStringArg ('vsconfig', TRUE);
1199 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1200 $error = commitCreateVS
1201 (
103b1e1e
DO
1202 $_REQUEST['vip'],
1203 $_REQUEST['vport'],
7320b661 1204 $_REQUEST['proto'],
103b1e1e
DO
1205 $_REQUEST['name'],
1206 $_REQUEST['vsconfig'],
c63a8d6e
DO
1207 $_REQUEST['rsconfig'],
1208 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1209 );
1210 if ($error != '')
135080d8 1211 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1212 else
135080d8 1213 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1214}
1215
b6a7d936
DO
1216$msgcode['deleteRealServer']['OK'] = 35;
1217$msgcode['deleteRealServer']['ERR'] = 128;
fb1c4a54
DO
1218function deleteRealServer ()
1219{
0cc24e9a 1220 assertUIntArg ('id');
d6517a21 1221 if (!commitDeleteRS ($_REQUEST['id']))
135080d8 1222 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1223 else
135080d8 1224 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1225}
1226
b6a7d936
DO
1227$msgcode['deleteLoadBalancer']['OK'] = 19;
1228$msgcode['deleteLoadBalancer']['ERR'] = 129;
3241551e
DO
1229function deleteLoadBalancer ()
1230{
0cc24e9a
DY
1231 assertUIntArg ('object_id');
1232 assertUIntArg ('pool_id');
1233 assertUIntArg ('vs_id');
103b1e1e
DO
1234 if (!commitDeleteLB (
1235 $_REQUEST['object_id'],
1236 $_REQUEST['pool_id'],
1237 $_REQUEST['vs_id']
1238 ))
135080d8 1239 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1240 else
135080d8 1241 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1242}
1243
b6a7d936
DO
1244$msgcode['deleteVService']['OK'] = 29;
1245$msgcode['deleteVService']['ERR'] = 130;
d6517a21
DO
1246function deleteVService ()
1247{
0cc24e9a 1248 assertUIntArg ('vs_id');
e02e1941 1249 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1250 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1251 else
135080d8 1252 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1253}
1254
b6a7d936
DO
1255$msgcode['updateRealServer']['OK'] = 36;
1256$msgcode['updateRealServer']['ERR'] = 133;
fb1c4a54
DO
1257function updateRealServer ()
1258{
0cc24e9a
DY
1259 assertUIntArg ('rs_id');
1260 assertIPv4Arg ('rsip');
1261 assertStringArg ('rsport', TRUE);
1262 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1263 if (!commitUpdateRS (
1264 $_REQUEST['rs_id'],
1265 $_REQUEST['rsip'],
1266 $_REQUEST['rsport'],
1267 $_REQUEST['rsconfig']
1268 ))
135080d8 1269 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1270 else
135080d8 1271 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1272}
1273
b6a7d936
DO
1274$msgcode['updateLoadBalancer']['OK'] = 20;
1275$msgcode['updateLoadBalancer']['ERR'] = 134;
c1ca768c 1276function updateLoadBalancer ()
3241551e 1277{
0cc24e9a
DY
1278 assertUIntArg ('object_id');
1279 assertUIntArg ('pool_id');
1280 assertUIntArg ('vs_id');
1281 assertStringArg ('vsconfig', TRUE);
1282 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1283 if (!commitUpdateLB (
1284 $_REQUEST['object_id'],
1285 $_REQUEST['pool_id'],
1286 $_REQUEST['vs_id'],
1287 $_REQUEST['vsconfig'],
1288 $_REQUEST['rsconfig']
1289 ))
135080d8 1290 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1291 else
135080d8 1292 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1293}
1294
b6a7d936
DO
1295$msgcode['updateVService']['OK'] = 30;
1296$msgcode['updateVService']['ERR'] = 135;
d6517a21
DO
1297function updateVService ()
1298{
0cc24e9a
DY
1299 assertUIntArg ('vs_id');
1300 assertIPv4Arg ('vip');
1301 assertUIntArg ('vport');
1302 assertStringArg ('proto');
1303 assertStringArg ('name', TRUE);
1304 assertStringArg ('vsconfig', TRUE);
1305 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1306 if (!commitUpdateVS (
1307 $_REQUEST['vs_id'],
1308 $_REQUEST['vip'],
1309 $_REQUEST['vport'],
1310 $_REQUEST['proto'],
1311 $_REQUEST['name'],
1312 $_REQUEST['vsconfig'],
1313 $_REQUEST['rsconfig']
1314 ))
135080d8 1315 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1316 else
135080d8 1317 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1318}
1319
b6a7d936
DO
1320$msgcode['addLoadBalancer']['OK'] = 18;
1321$msgcode['addLoadBalancer']['ERR'] = 137;
3241551e
DO
1322function addLoadBalancer ()
1323{
0cc24e9a
DY
1324 assertUIntArg ('pool_id');
1325 assertUIntArg ('object_id');
1326 assertUIntArg ('vs_id');
1327 assertStringArg ('vsconfig', TRUE);
1328 assertStringArg ('rsconfig', TRUE);
103b1e1e
DO
1329 if (!addLBtoRSPool (
1330 $_REQUEST['pool_id'],
1331 $_REQUEST['object_id'],
1332 $_REQUEST['vs_id'],
1333 $_REQUEST['vsconfig'],
1334 $_REQUEST['rsconfig']
1335 ))
135080d8 1336 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1337 else
135080d8 1338 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1339}
1340
b6a7d936
DO
1341$msgcode['addRSPool']['OK'] = 31;
1342$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1343function addRSPool ()
1344{
0cc24e9a
DY
1345 assertStringArg ('name', TRUE);
1346 assertStringArg ('vsconfig', TRUE);
1347 assertStringArg ('rsconfig', TRUE);
c63a8d6e
DO
1348 $error = commitCreateRSPool
1349 (
103b1e1e
DO
1350 $_REQUEST['name'],
1351 $_REQUEST['vsconfig'],
c63a8d6e
DO
1352 $_REQUEST['rsconfig'],
1353 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1354 );
1355 if ($error != '')
135080d8 1356 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1357 else
135080d8 1358 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1359}
1360
b6a7d936
DO
1361$msgcode['deleteRSPool']['OK'] = 32;
1362$msgcode['deleteRSPool']['ERR'] = 138;
5ad76f01
DO
1363function deleteRSPool ()
1364{
0cc24e9a 1365 assertUIntArg ('pool_id');
841a7a7a 1366 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
135080d8 1367 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1368 else
135080d8 1369 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1370}
1371
b6a7d936
DO
1372$msgcode['updateRSPool']['OK'] = 33;
1373$msgcode['updateRSPool']['ERR'] = 139;
5ad76f01
DO
1374function updateRSPool ()
1375{
0cc24e9a
DY
1376 assertUIntArg ('pool_id');
1377 assertStringArg ('name', TRUE);
1378 assertStringArg ('vsconfig', TRUE);
1379 assertStringArg ('rsconfig', TRUE);
841a7a7a 1380 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
135080d8 1381 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1382 else
135080d8 1383 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1384}
1385
b6a7d936
DO
1386$msgcode['updateRSInService']['OK'] = 38;
1387$msgcode['updateRSInService']['ERR'] = 140;
1f7d18fa
DO
1388function updateRSInService ()
1389{
0cc24e9a 1390 assertUIntArg ('rscount');
841a7a7a 1391 $pool_id = $_REQUEST['pool_id'];
a6e91ac2
DO
1392 $orig = spotEntity ('ipv4rspool', $pool_id);
1393 amplifyCell ($orig);
1f7d18fa
DO
1394 $nbad = $ngood = 0;
1395 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1396 {
1397 $rs_id = $_REQUEST["rsid_${i}"];
1398 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1399 $newval = 'yes';
1400 else
1401 $newval = 'no';
1402 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1403 {
1404 if (commitSetInService ($rs_id, $newval))
1405 $ngood++;
1406 else
1407 $nbad++;
1408 }
1409 }
1410 if (!$nbad)
135080d8 1411 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1412 else
135080d8 1413 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1414}
1415
b6a7d936
DO
1416$msgcode['importPTRData']['OK'] = 26;
1417$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1418// FIXME: check, that each submitted address belongs to the prefix we
1419// are operating on.
03eb5209
DO
1420function importPTRData ()
1421{
0cc24e9a 1422 assertUIntArg ('addrcount');
03eb5209 1423 $nbad = $ngood = 0;
03eb5209
DO
1424 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1425 {
3f3bd41e 1426 $inputname = "import_${i}";
03eb5209
DO
1427 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1428 continue;
0cc24e9a
DY
1429 assertIPv4Arg ("addr_${i}");
1430 assertStringArg ("descr_${i}", TRUE);
1431 assertStringArg ("rsvd_${i}");
3f3bd41e
DO
1432 // Non-existent addresses will not have this argument set in request.
1433 $rsvd = 'no';
03eb5209
DO
1434 if ($_REQUEST["rsvd_${i}"] == 'yes')
1435 $rsvd = 'yes';
3f3bd41e 1436 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1437 $ngood++;
1438 else
1439 $nbad++;
1440 }
1441 if (!$nbad)
135080d8 1442 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1443 else
135080d8 1444 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1445}
1446
b6a7d936
DO
1447$msgcode['generateAutoPorts']['OK'] = 21;
1448$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1449function generateAutoPorts ()
1450{
103b1e1e 1451 global $pageno;
0cc24e9a 1452 assertUIntArg ('object_id');
6297d584 1453 $info = spotEntity ('object', $_REQUEST['object_id']);
f3f0161f 1454 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1455 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1456 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1457 else
135080d8 1458 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1459}
1460
b6a7d936
DO
1461$msgcode['saveEntityTags']['OK'] = 22;
1462$msgcode['saveEntityTags']['ERR1'] = 143;
1463$msgcode['saveEntityTags']['ERR2'] = 187;
24cbe8af 1464// Filter out implicit tags before storing the new tag set.
3355ca56 1465function saveEntityTags ()
24cbe8af 1466{
eb27afad 1467 global $page, $pageno, $etype_by_pageno;
be2ae2a2 1468 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
9a61c175 1469 return buildRedirectURL (__FUNCTION__, 'ERR2', array (__FUNCTION__));
be2ae2a2 1470 $realm = $etype_by_pageno[$pageno];
3355ca56 1471 $bypass = $page[$pageno]['bypass'];
0cc24e9a 1472 assertUIntArg ($bypass);
2034d968 1473 $entity_id = $_REQUEST[$bypass];
e04931c8 1474 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1475 // Build a chain from the submitted data, minimize it,
ab379543 1476 // then wipe existing records and store the new set instead.
01b6b4d6 1477 destroyTagsForEntity ($realm, $entity_id);
eb27afad
DO
1478 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1479 // so why not use it?
6e49bd1f 1480 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1481 $n_succeeds = $n_errors = 0;
6e49bd1f 1482 foreach ($newchain as $taginfo)
eb6ea26f 1483 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1484 $n_succeeds++;
1485 else
1486 $n_errors++;
ab379543 1487 if ($n_errors)
9a61c175 1488 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1489 else
135080d8 1490 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1491}
1492
b6a7d936
DO
1493$msgcode['destroyTag']['OK'] = 58;
1494$msgcode['destroyTag']['ERR1'] = 183;
1495$msgcode['destroyTag']['ERR2'] = 144;
fe7044ad
DO
1496function destroyTag ()
1497{
0cc24e9a 1498 assertUIntArg ('tag_id');
c615a655
DO
1499 global $taglist;
1500 if (!isset ($taglist[$_REQUEST['tag_id']]))
1501 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['tag_id']));
49fb1027 1502 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
c615a655 1503 return buildRedirectURL (__FUNCTION__, 'OK', array ($taglist[$_REQUEST['tag_id']]['tag']));
fe7044ad 1504 else
c615a655 1505 return buildRedirectURL (__FUNCTION__, 'ERR2');
fe7044ad
DO
1506}
1507
b6a7d936
DO
1508$msgcode['createTag']['OK'] = 59;
1509$msgcode['createTag']['ERR1'] = 145;
1510$msgcode['createTag']['ERR3'] = 147;
fe7044ad
DO
1511function createTag ()
1512{
0cc24e9a
DY
1513 assertStringArg ('tag_name');
1514 assertUIntArg ('parent_id', TRUE);
49fb1027 1515 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1516 if (!validTagName ($tagname))
135080d8 1517 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
fe7044ad
DO
1518 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1519 $parent_id = 'NULL';
1520 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
135080d8 1521 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
fe7044ad 1522 else
135080d8 1523 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
fe7044ad
DO
1524}
1525
b6a7d936
DO
1526$msgcode['updateTag']['OK'] = 60;
1527$msgcode['updateTag']['ERR1'] = 145;
1528$msgcode['updateTag']['ERR2'] = 148;
49fb1027
DO
1529function updateTag ()
1530{
0cc24e9a
DY
1531 assertUIntArg ('tag_id');
1532 assertUIntArg ('parent_id', TRUE);
1533 assertStringArg ('tag_name');
49fb1027 1534 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1535 if (!validTagName ($tagname))
135080d8 1536 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1537 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1538 $parent_id = 'NULL';
1539 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
135080d8 1540 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
590e1281
DO
1541 // Use old name in the message, cause update failed.
1542 global $taglist;
1543 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag'], $ret));
49fb1027
DO
1544}
1545
b6a7d936
DO
1546$msgcode['rollTags']['OK'] = 67;
1547$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1548function rollTags ()
1549{
0cc24e9a
DY
1550 assertUIntArg ('row_id');
1551 assertStringArg ('sum', TRUE);
1552 assertUIntArg ('realsum');
eb6ea26f 1553 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1554 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1555 // Even if the user requested an empty tag list, don't bail out, but process existing
1556 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1557 // have its chains refined to "normal" form.
1558 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1559 $n_ok = 0;
1560 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1561 // filter out the same tag again and again. It will have own noise to cancel.
1562 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
61a1d996 1563 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
abef7149 1564 {
61a1d996 1565 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
abef7149 1566 $n_ok++;
61a1d996
DO
1567 amplifyCell ($rack);
1568 foreach ($rack['mountedObjects'] as $object_id)
abef7149
DO
1569 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1570 $n_ok++;
1571 }
135080d8 1572 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1573}
1574
b6a7d936
DO
1575$msgcode['changeMyPassword']['OK'] = 61;
1576$msgcode['changeMyPassword']['ERR1'] = 150;
1577$msgcode['changeMyPassword']['ERR2'] = 151;
1578$msgcode['changeMyPassword']['ERR3'] = 152;
1579$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1580function changeMyPassword ()
cced6b7d 1581{
b82cce3f 1582 global $remote_username, $user_auth_src;
204284ba 1583 if ($user_auth_src != 'database')
135080d8 1584 return buildRedirectURL (__FUNCTION__, 'ERR1');
0cc24e9a
DY
1585 assertStringArg ('oldpassword');
1586 assertStringArg ('newpassword1');
1587 assertStringArg ('newpassword2');
b82cce3f 1588 $remote_userid = getUserIDByUsername ($remote_username);
0b2c74cb 1589 $userinfo = spotEntity ('user', $remote_userid);
b82cce3f 1590 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1591 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1592 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1593 return buildRedirectURL (__FUNCTION__, 'ERR3');
b82cce3f 1594 if (commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1595 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1596 else
135080d8 1597 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1598}
1599
b6a7d936
DO
1600$msgcode['saveRackCode']['OK'] = 43;
1601$msgcode['saveRackCode']['ERR1'] = 154;
1602$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1603function saveRackCode ()
1604{
cced6b7d 1605 assertStringArg ('rackcode');
e6a4adb9
DO
1606 // For the test to succeed, unescape LFs, strip CRs.
1607 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1608 $parseTree = getRackCode ($newcode);
1609 if ($parseTree['result'] != 'ACK')
135080d8 1610 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
4a6a28f1 1611 saveScript ('RackCodeCache', '');
cf25e649 1612 if (saveScript ('RackCode', $newcode))
135080d8 1613 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1614 else
135080d8 1615 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1616}
1617
b6a7d936 1618$msgcode['setPortVLAN']['ERR1'] = 156;
46f92ff7
DO
1619// This handler's context is pre-built, but not authorized. It is assumed, that the
1620// handler will take existing context and before each commit check authorization
1621// on the base chain plus necessary context added.
1622function setPortVLAN ()
1623{
0cc24e9a 1624 assertUIntArg ('portcount');
46f92ff7
DO
1625 $data = getSwitchVLANs ($_REQUEST['object_id']);
1626 if ($data === NULL)
135080d8 1627 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1628 list ($vlanlist, $portlist) = $data;
1629 // Here we just build up 1 set command for the gateway with all of the ports
1630 // included. The gateway is expected to filter unnecessary changes silently
1631 // and to provide a list of responses with either error or success message
1632 // for each of the rest.
1633 $nports = $_REQUEST['portcount'];
1634 $prefix = 'set ';
db55cf54 1635 $log = emptyLog();
46f92ff7
DO
1636 $setcmd = '';
1637 for ($i = 0; $i < $nports; $i++)
1638 if
1639 (
1640 !isset ($_REQUEST['portname_' . $i]) ||
1641 !isset ($_REQUEST['vlanid_' . $i]) ||
1642 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1643 )
24dcb9d8 1644 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1645 elseif
1646 (
1647 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1648 $portlist[$i]['vlaind'] == 'TRUNK'
1649 )
1650 continue;
1651 else
1652 {
1653 $portname = $_REQUEST['portname_' . $i];
1654 $oldvlanid = $portlist[$i]['vlanid'];
1655 $newvlanid = $_REQUEST['vlanid_' . $i];
1656 // Finish the security context and evaluate it.
1657 $annex = array();
1658 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1659 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1660 if (!permitted (NULL, NULL, NULL, $annex))
1661 {
24dcb9d8 1662 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1663 continue;
1664 }
1665 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1666 $prefix = ';';
1667 }
1668 // Feed the gateway and interpret its (non)response.
1669 if ($setcmd != '')
f0ff4930 1670 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1671 else
24dcb9d8 1672 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1673 return buildWideRedirectURL ($log);
1674}
1675
2987fc1f
DO
1676function submitSLBConfig ()
1677{
0cc24e9a 1678 assertUIntArg ('object_id');
2987fc1f 1679 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
ff0df5c3 1680 $msglog = gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1681 return buildWideRedirectURL ($msglog);
1682}
1683
b6a7d936
DO
1684$msgcode['addRow']['OK'] = 74;
1685$msgcode['addRow']['ERR'] = 100;
345fd640
AD
1686function addRow ()
1687{
0cc24e9a 1688 assertStringArg ('name');
345fd640 1689
10bac82a 1690 if (commitAddRow ($_REQUEST['name']) === TRUE)
135080d8 1691 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1692 else
135080d8 1693 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1694}
1695
b6a7d936
DO
1696$msgcode['updateRow']['OK'] = 75;
1697$msgcode['updateRow']['ERR'] = 100;
345fd640
AD
1698function updateRow ()
1699{
0cc24e9a
DY
1700 assertUIntArg ('row_id');
1701 assertStringArg ('name');
345fd640 1702
10bac82a 1703 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1704 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1705 else
135080d8 1706 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1707}
1708
b6a7d936
DO
1709$msgcode['deleteRow']['OK'] = 77;
1710$msgcode['deleteRow']['ERR'] = 100;
9f14a7ef
DY
1711function deleteRow ()
1712{
0cc24e9a 1713 assertUIntArg ('row_id');
7be51fea 1714 $rowinfo = getRackRowInfo ($_REQUEST['row_id']);
9f14a7ef
DY
1715
1716 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
7be51fea 1717 return buildRedirectURL (__FUNCTION__, 'OK', array ($rowinfo['name']));
9f14a7ef 1718 else
7be51fea 1719 return buildRedirectURL (__FUNCTION__, 'ERR', array ($rowinfo['name']));
9f14a7ef
DY
1720}
1721
b6a7d936
DO
1722$msgcode['addRack']['OK'] = 65;
1723$msgcode['addRack']['ERR1'] = 171;
1724$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1725function addRack ()
1726{
0cc24e9a 1727 assertUIntArg ('row_id');
f19c75d6
DO
1728 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1729 if (isset ($_REQUEST['got_data']))
1730 {
0cc24e9a
DY
1731 assertStringArg ('rack_name');
1732 assertUIntArg ('rack_height1');
1733 assertStringArg ('rack_comment', TRUE);
f19c75d6
DO
1734
1735 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1736 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1737 else
135080d8 1738 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1739 }
1740 elseif (isset ($_REQUEST['got_mdata']))
1741 {
0cc24e9a
DY
1742 assertUIntArg ('rack_height2');
1743 assertStringArg ('rack_names', TRUE);
db55cf54 1744 $log = emptyLog();
f19c75d6
DO
1745 // copy-and-paste from renderAddMultipleObjectsForm()
1746 $names1 = explode ('\n', $_REQUEST['rack_names']);
1747 $names2 = array();
1748 foreach ($names1 as $line)
1749 {
1750 $parts = explode ('\r', $line);
1751 reset ($parts);
59a83bd8 1752 if (!strlen ($parts[0]))
f19c75d6
DO
1753 continue;
1754 else
1755 $names2[] = rtrim ($parts[0]);
1756 }
b3029d5c 1757 global $msgcode;
f19c75d6
DO
1758 foreach ($names2 as $cname)
1759 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1760 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1761 else
b3029d5c 1762 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1763 return buildWideRedirectURL ($log);
1764 }
1765 else
135080d8 1766 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1767}
1768
b6a7d936
DO
1769$msgcode['deleteRack']['OK'] = 79;
1770$msgcode['deleteRack']['ERR'] = 100;
1771$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1772function deleteRack ()
1773{
0cc24e9a 1774 assertUIntArg ('rack_id');
61a1d996
DO
1775 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1776 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1777 amplifyCell ($rackData);
1778 if (count ($rackData['mountedObjects']))
c8187437 1779 return buildRedirectURL (__FUNCTION__, 'ERR1');
61a1d996
DO
1780 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1781 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1782 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
c8187437
DY
1783}
1784
b6a7d936
DO
1785$msgcode['updateRack']['OK'] = 68;
1786$msgcode['updateRack']['ERR'] = 177;
7056988c
DO
1787function updateRack ()
1788{
0cc24e9a
DY
1789 assertUIntArg ('rack_id');
1790 assertUIntArg ('rack_row_id');
1791 assertUIntArg ('rack_height');
1792 assertStringArg ('rack_name');
1793 assertStringArg ('rack_comment', TRUE);
7056988c 1794
7056988c 1795 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1796 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1797 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1798 else
135080d8 1799 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1800}
1801
61a1d996 1802$msgcode['updateRackDesign']['ERR'] = 100;
4fbb5a00
DY
1803function updateRackDesign ()
1804{
0cc24e9a 1805 assertUIntArg ('rack_id');
61a1d996
DO
1806 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1807 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1808 amplifyCell ($rackData);
4fbb5a00
DY
1809 applyRackDesignMask($rackData);
1810 markupObjectProblems ($rackData);
1811 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1812 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1813}
1814
61a1d996 1815$msgcode['updateRackProblems']['ERR'] = 100;
4fbb5a00
DY
1816function updateRackProblems ()
1817{
0cc24e9a 1818 assertUIntArg ('rack_id');
61a1d996
DO
1819 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1820 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1821 amplifyCell ($rackData);
4fbb5a00
DY
1822 applyRackProblemMask($rackData);
1823 markupObjectProblems ($rackData);
1824 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1825 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1826}
1827
7056988c
DO
1828function querySNMPData ()
1829{
0cc24e9a
DY
1830 assertUIntArg ('object_id');
1831 assertStringArg ('community');
8536e20a 1832 return doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']);
7056988c
DO
1833}
1834
b6a7d936
DO
1835$msgcode['addFileWithoutLink']['OK'] = 69;
1836$msgcode['addFileWithoutLink']['ERR'] = 100;
e1ae3fb4
AD
1837// File-related functions
1838function addFileWithoutLink ()
1839{
0cc24e9a 1840 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1841
1842 // Make sure the file can be uploaded
1843 if (get_cfg_var('file_uploads') != 1)
135080d8 1844 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1845
1846 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1847 global $sic;
1848 // commitAddFile() uses prepared statements
1849 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
f857f71f
DO
1850 if (isset ($_REQUEST['taglist']))
1851 produceTagsForLastRecord ('file', $_REQUEST['taglist']);
e1ae3fb4
AD
1852
1853 if ($error != '')
135080d8 1854 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1855
135080d8 1856 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1857}
1858
b6a7d936
DO
1859$msgcode['addFileToEntity']['OK'] = 69;
1860$msgcode['addFileToEntity']['ERR1'] = 187;
1861$msgcode['addFileToEntity']['ERR2'] = 181;
1862$msgcode['addFileToEntity']['ERR3'] = 182;
e1ae3fb4
AD
1863function addFileToEntity ()
1864{
9a61c175
DO
1865 global $page, $pageno, $etype_by_pageno;
1866 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1867 return buildRedirectURL (__FUNCTION__, 'ERR1', array (__FUNCTION__));
1868 $realm = $etype_by_pageno[$pageno];
1869 $bypass = $page[$pageno]['bypass'];
0cc24e9a 1870 assertUIntArg ($bypass);
9a61c175 1871 $entity_id = $_REQUEST[$bypass];
0cc24e9a 1872 assertStringArg ('comment', TRUE);
e1ae3fb4
AD
1873
1874 // Make sure the file can be uploaded
1875 if (get_cfg_var('file_uploads') != 1)
9a61c175 1876 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
1877
1878 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1879 global $sic;
1880 // commitAddFile() uses prepared statements
1881 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
e1ae3fb4 1882 if ($error != '')
9a61c175 1883 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1884
9a61c175 1885 $error = commitLinkFile (lastInsertID(), $realm, $entity_id);
e1ae3fb4 1886 if ($error != '')
9a61c175 1887 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1888
135080d8 1889 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1890}
1891
b6a7d936
DO
1892$msgcode['linkFileToEntity']['OK'] = 71;
1893$msgcode['linkFileToEntity']['ERR1'] = 178;
1894$msgcode['linkFileToEntity']['ERR2'] = 100;
e1ae3fb4
AD
1895function linkFileToEntity ()
1896{
0cc24e9a 1897 assertUIntArg ('file_id');
482c7f35 1898 global $page, $pageno, $etype_by_pageno;
121496b6
DO
1899 $entity_type = $etype_by_pageno[$pageno];
1900 $bypass_name = $page[$pageno]['bypass'];
0cc24e9a 1901 assertUIntArg ($bypass_name);
121496b6 1902
d3b5008b 1903 $fi = spotEntity ('file', $_REQUEST['file_id']);
121496b6 1904 if ($fi === NULL)
135080d8 1905 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
121496b6 1906 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
e1ae3fb4 1907 if ($error != '')
135080d8 1908 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
e1ae3fb4 1909
135080d8 1910 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
e1ae3fb4
AD
1911}
1912
b6a7d936
DO
1913$msgcode['replaceFile']['OK'] = 70;
1914$msgcode['replaceFile']['ERR1'] = 181;
1915$msgcode['replaceFile']['ERR2'] = 207;
1916$msgcode['replaceFile']['ERR3'] = 182;
fd1fb420
AD
1917function replaceFile ()
1918{
db55cf54 1919 global $sic;
0cc24e9a 1920 assertUIntArg ('file_id');
fd1fb420
AD
1921
1922 // Make sure the file can be uploaded
1923 if (get_cfg_var('file_uploads') != 1)
db55cf54 1924 return buildRedirectURL (__FUNCTION__, 'ERR1');
d3b5008b 1925 $shortInfo = spotEntity ('file', $sic['file_id']);
fd1fb420
AD
1926
1927 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1928 if ($fp === FALSE)
1929 return buildRedirectURL (__FUNCTION__, 'ERR2');
f8874cdb 1930 $error = commitReplaceFile ($sic['file_id'], $fp);
fd1fb420 1931 if ($error != '')
db55cf54 1932 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
fd1fb420 1933
db55cf54 1934 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1935}
1936
b6a7d936
DO
1937$msgcode['updateFile']['OK'] = 70;
1938$msgcode['updateFile']['ERR'] = 100;
e1ae3fb4
AD
1939function updateFile ()
1940{
0cc24e9a
DY
1941 assertUIntArg ('file_id');
1942 assertStringArg ('file_name');
1943 assertStringArg ('file_type');
1944 assertStringArg ('file_comment', TRUE);
f3d274bf
DO
1945 // prepared statement params below
1946 global $sic;
1947 $error = commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']);
e1ae3fb4 1948 if ($error != '')
135080d8 1949 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1950
135080d8 1951 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1952}
1953
b6a7d936
DO
1954$msgcode['unlinkFile']['OK'] = 72;
1955$msgcode['unlinkFile']['ERR'] = 182;
e1ae3fb4
AD
1956function unlinkFile ()
1957{
0cc24e9a 1958 assertUIntArg ('link_id');
e1ae3fb4
AD
1959 $error = commitUnlinkFile ($_REQUEST['link_id']);
1960
1961 if ($error != '')
135080d8 1962 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1963
8bc5d1e4 1964 return buildRedirectURL (__FUNCTION__, 'OK');
e1ae3fb4
AD
1965}
1966
b6a7d936
DO
1967$msgcode['deleteFile']['OK'] = 73;
1968$msgcode['deleteFile']['ERR'] = 100;
e1ae3fb4
AD
1969function deleteFile ()
1970{
0cc24e9a 1971 assertUIntArg ('file_id');
d3b5008b 1972 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
e1ae3fb4
AD
1973 $error = commitDeleteFile ($_REQUEST['file_id']);
1974
1975 if ($error != '')
135080d8 1976 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1977
f8874cdb 1978 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1979}
1980
b6a7d936
DO
1981$msgcode['updateFileText']['OK'] = 78;
1982$msgcode['updateFileText']['ERR1'] = 179;
1983$msgcode['updateFileText']['ERR2'] = 180;
8628ae44
DO
1984function updateFileText ()
1985{
0cc24e9a
DY
1986 assertUIntArg ('file_id');
1987 assertStringArg ('mtime_copy');
1988 assertStringArg ('file_text', TRUE); // it's Ok to save empty
d3b5008b 1989 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
70cb9b56
DO
1990 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1991 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb
DO
1992 global $sic;
1993 $error = commitReplaceFile ($sic['file_id'], $sic['file_text']);
8628ae44 1994 if ($error == '')
f8874cdb 1995 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
70cb9b56 1996 return buildRedirectURL (__FUNCTION__, 'ERR2');
8628ae44
DO
1997}
1998
2dfa1b73
DO
1999$msgcode['addPortInterfaceCompat']['OK'] = 48;
2000$msgcode['addPortInterfaceCompat']['ERR'] = 110;
2001function addPortInterfaceCompat ()
2002{
0cc24e9a
DY
2003 assertUIntArg ('iif_id');
2004 assertUIntArg ('oif_id');
2dfa1b73
DO
2005 if (commitSupplementPIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
2006 return buildRedirectURL (__FUNCTION__, 'OK');
2007 return buildRedirectURL (__FUNCTION__, 'ERR');
2008}
2009
2010$msgcode['delPortInterfaceCompat']['OK'] = 49;
2011$msgcode['delPortInterfaceCompat']['ERR'] = 111;
2012function delPortInterfaceCompat ()
2013{
0cc24e9a
DY
2014 assertUIntArg ('iif_id');
2015 assertUIntArg ('oif_id');
2dfa1b73
DO
2016 if (commitReducePIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
2017 return buildRedirectURL (__FUNCTION__, 'OK');
2018 return buildRedirectURL (__FUNCTION__, 'ERR');
2019}
2020
2400d7ec
DO
2021$ifcompatpack = array
2022(
2023 '1000cwdm80' => array (1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216),
2024 '1000dwdm80' => array // ITU channels 20~61
2025 (
2026 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226,
2027 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236,
2028 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
2029 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
2030 1257, 1258
2031 ),
2032 '10000dwdm80' => array // same channels for 10GE
2033 (
2034 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268,
2035 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278,
2036 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
2037 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298,
2038 1299, 1300
2039 ),
2040);
2041
3d9ac3d4
DO
2042$msgcode['addPortInterfaceCompatPack']['OK'] = 44;
2043$msgcode['addPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2044function addPortInterfaceCompatPack ()
2045{
0cc24e9a
DY
2046 assertStringArg ('standard');
2047 assertUIntArg ('iif_id');
3d9ac3d4
DO
2048 global $ifcompatpack;
2049 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
2050 return buildRedirectURL (__FUNCTION__, 'ERR');
2051 $ngood = $nbad = 0;
2052 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2053 if (commitSupplementPIC ($_REQUEST['iif_id'], $oif_id))
2054 $ngood++;
2055 else
2056 $nbad++;
2057 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2058}
2059
3d9ac3d4
DO
2060$msgcode['delPortInterfaceCompatPack']['OK'] = 44;
2061$msgcode['delPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
2062function delPortInterfaceCompatPack ()
2063{
0cc24e9a
DY
2064 assertStringArg ('standard');
2065 assertUIntArg ('iif_id');
3d9ac3d4
DO
2066 global $ifcompatpack;
2067 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
2068 return buildRedirectURL (__FUNCTION__, 'ERR');
2069 $ngood = $nbad = 0;
2070 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2071 if (commitReducePIC ($_REQUEST['iif_id'], $oif_id))
2072 $ngood++;
2073 else
2074 $nbad++;
2075 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2076}
2077
a310dd80
DO
2078$msgcode['addPortOIFCompat']['OK'] = 48;
2079$msgcode['addPortOIFCompat']['ERR'] = 110;
2080function addPortOIFCompat()
2081{
80ec1f4f
DO
2082 assertUIntArg('type1');
2083 assertUIntArg('type2');
a310dd80
DO
2084 if (commitSupplementPOIFC($_REQUEST['type1'], $_REQUEST['type2']))
2085 return buildRedirectURL(__FUNCTION__, 'OK');
2086 return buildRedirectURL(__FUNCTION__, 'ERR');
2087}
2088
2089$msgcode['delPortOIFCompat']['OK'] = 49;
2090$msgcode['delPortOIFCompat']['ERR'] = 111;
2091function delPortOIFCompat ()
2092{
80ec1f4f
DO
2093 assertUIntArg('type1');
2094 assertUIntArg('type2');
a310dd80
DO
2095 if (commitReducePOIFC ($_REQUEST['type1'], $_REQUEST['type2']))
2096 return buildRedirectURL (__FUNCTION__, 'OK');
2097 return buildRedirectURL (__FUNCTION__, 'ERR');
2098
2099}
2100
38cd7704
DO
2101$msgcode['add8021QOrder']['OK'] = 48;
2102$msgcode['add8021QOrder']['ERR'] = 118;
2103function add8021QOrder ()
8198f2c6 2104{
80ec1f4f
DO
2105 assertUIntArg ('vdom_id');
2106 assertUIntArg ('object_id');
38cd7704 2107 assertUIntArg ('vst_id');
d6ac0bea 2108 global $sic, $dbxlink;
8198f2c6
DO
2109 $result = usePreparedInsertBlade
2110 (
2111 'VLANSwitch',
2112 array
2113 (
2114 'domain_id' => $sic['vdom_id'],
2115 'object_id' => $sic['object_id'],
38cd7704 2116 'template_id' => $sic['vst_id'],
8198f2c6
DO
2117 )
2118 );
d6ac0bea
DO
2119 $query = $dbxlink->prepare ('UPDATE VLANSwitch SET last_change = NOW(), out_of_sync = "yes" WHERE object_id = ?');
2120 $query->execute (array ($sic['object_id']));
8198f2c6
DO
2121 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2122}
2123
38cd7704
DO
2124$msgcode['del8021QOrder']['OK'] = 49;
2125$msgcode['del8021QOrder']['ERR'] = 119;
2126function del8021QOrder ()
8198f2c6 2127{
80ec1f4f 2128 assertUIntArg ('object_id');
8198f2c6 2129 global $sic;
38cd7704 2130 $result = usePreparedDeleteBlade ('VLANSwitch', array ('object_id' => $sic['object_id']));
8198f2c6
DO
2131 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2132}
2133
2134$msgcode['addVLANDescription']['OK'] = 48;
2135$msgcode['addVLANDescription']['ERR1'] = 190;
2136$msgcode['addVLANDescription']['ERR2'] = 110;
2137function addVLANDescription ()
2138{
80ec1f4f 2139 assertUIntArg ('vlan_id');
5b989e4d 2140 assertStringArg ('vlan_type', TRUE);
80ec1f4f 2141 assertStringArg ('vlan_descr', TRUE);
8198f2c6
DO
2142 global $sic;
2143 if (!($sic['vlan_id'] >= VLAN_MIN_ID and $sic['vlan_id'] <= VLAN_MAX_ID))
2144 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($sic['vlan_id']));
2145 $result = usePreparedInsertBlade
2146 (
2147 'VLANDescription',
2148 array
2149 (
2150 'domain_id' => $sic['vdom_id'],
2151 'vlan_id' => $sic['vlan_id'],
5b989e4d 2152 'vlan_type' => $sic['vlan_type'],
5286b29f 2153 'vlan_descr' => mb_strlen ($sic['vlan_descr']) ? $sic['vlan_descr'] : NULL
8198f2c6
DO
2154 )
2155 );
2156 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR2');
2157}
2158
2159$msgcode['delVLANDescription']['OK'] = 49;
2160$msgcode['delVLANDescription']['ERR'] = 111;
2161function delVLANDescription ()
2162{
80ec1f4f 2163 assertUIntArg ('vlan_id');
8198f2c6
DO
2164 global $sic;
2165 $result = commitReduceVLANDescription ($sic['vdom_id'], $sic['vlan_id']);
2166 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2167}
2168
2169$msgcode['updVLANDescription']['OK'] = 51;
2170$msgcode['updVLANDescription']['ERR'] = 109;
2171function updVLANDescription ()
2172{
80ec1f4f
DO
2173 assertUIntArg ('vlan_id');
2174 assertStringArg ('vlan_type');
2175 assertStringArg ('vlan_descr', TRUE);
8198f2c6
DO
2176 global $sic;
2177 $result = commitUpdateVLANDescription
2178 (
2179 $sic['vdom_id'],
2180 $sic['vlan_id'],
0dabdc53 2181 $sic['vlan_type'],
8198f2c6
DO
2182 $sic['vlan_descr']
2183 );
2184 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2185}
2186
2187$msgcode['createVLANDomain']['OK'] = 48;
2188$msgcode['createVLANDomain']['ERR'] = 110;
2189function createVLANDomain ()
2190{
80ec1f4f 2191 assertStringArg ('vdom_descr');
8198f2c6
DO
2192 global $sic;
2193 $result = usePreparedInsertBlade
2194 (
2195 'VLANDomain',
2196 array
2197 (
2198 'description' => $sic['vdom_descr'],
2199 )
2200 );
2201 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2202}
2203
2204$msgcode['destroyVLANDomain']['OK'] = 49;
2205$msgcode['destroyVLANDomain']['ERR'] = 111;
2206function destroyVLANDomain ()
2207{
80ec1f4f 2208 assertUIntArg ('vdom_id');
8198f2c6 2209 global $sic;
bbae3611 2210 $result = FALSE !== usePreparedDeleteBlade ('VLANDomain', array ('id' => $sic['vdom_id']));
8198f2c6
DO
2211 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2212}
2213
2214$msgcode['updateVLANDomain']['OK'] = 51;
2215$msgcode['updateVLANDomain']['ERR'] = 109;
2216function updateVLANDomain ()
2217{
80ec1f4f
DO
2218 assertUIntArg ('vdom_id');
2219 assertStringArg ('vdom_descr');
8198f2c6
DO
2220 global $sic;
2221 $result = commitUpdateVLANDomain ($sic['vdom_id'], $sic['vdom_descr']);
2222 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2223}
2224
18c26ec5 2225$msgcode['save8021QPorts']['OK'] = 63;
706d9175
DO
2226$msgcode['save8021QPorts']['ERR1'] = 160;
2227$msgcode['save8021QPorts']['ERR2'] = 109;
2228function save8021QPorts ()
8198f2c6 2229{
a7492e95 2230 global $sic, $dbxlink;
706d9175 2231 assertUIntArg ('nports');
70cff23f 2232 assertUIntArg ('mutex_rev', TRUE); // counts from 0
706d9175
DO
2233 if ($sic['nports'] == 1)
2234 {
2235 assertStringArg ('pn_0');
2236 $extra = array ('port_name' => $sic['pn_0']);
2237 }
2238 else
2239 $extra = array();
a7492e95 2240 $dbxlink->beginTransaction();
2996cbbb
DO
2241 try
2242 {
a7492e95
DO
2243 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
2244 throw new InvalidArgException ('object_id', $object_id, 'VLAN domain is not set for this object');
9c45ea37
DO
2245 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
2246 throw new RuntimeException ('expired form data');
af204724 2247 $changes = array();
706d9175
DO
2248 for ($i = 0; $i < $sic['nports']; $i++)
2249 {
2250 assertStringArg ('pn_' . $i);
2251 assertStringArg ('pm_' . $i);
706d9175
DO
2252 // An access port only generates form input for its native VLAN,
2253 // which we derive allowed VLAN list from.
2254 $native = isset ($sic['pnv_' . $i]) ? $sic['pnv_' . $i] : 0;
b36fc896 2255 switch ($sic["pm_${i}"])
706d9175 2256 {
b36fc896
DO
2257 case 'trunk':
2258# assertArrayArg ('pav_' . $i);
2259 $allowed = isset ($sic['pav_' . $i]) ? $sic['pav_' . $i] : array();
2260 break;
2261 case 'access':
2262 if ($native == 'same')
2263 continue 2;
706d9175
DO
2264 assertUIntArg ('pnv_' . $i);
2265 $allowed = array ($native);
b36fc896
DO
2266 break;
2267 default:
2268 throw new InvalidRequestArgException ("pm_${i}", $_REQUEST["pm_${i}"], 'unknown port mode');
706d9175 2269 }
af204724 2270 $changes[$sic['pn_' . $i]] = array
706d9175
DO
2271 (
2272 'mode' => $sic['pm_' . $i],
2273 'allowed' => $allowed,
2274 'native' => $native,
2275 );
2276 }
af204724 2277 $domain_vlanlist = getDomainVLANs ($vswitch['domain_id']);
9165e8f7 2278 $after = $before = apply8021QOrder ($vswitch['template_id'], getStored8021QConfig ($sic['object_id'], 'desired'));
af204724 2279 $changes = filter8021QChangeRequests
a7492e95 2280 (
af204724 2281 $domain_vlanlist,
9165e8f7 2282 $before,
af204724 2283 apply8021QOrder ($vswitch['template_id'], $changes)
a7492e95 2284 );
bb2024b9 2285 $changes = authorize8021QChangeRequests ($before, $changes);
af204724
DO
2286 foreach ($changes as $port_name => $port)
2287 $after[$port_name] = $port;
b3a27170 2288 $new_uplinks = filter8021QChangeRequests ($domain_vlanlist, $after, produceUplinkPorts ($domain_vlanlist, $after));
7455e4c0 2289 $npulled = replace8021QPorts ('desired', $vswitch['object_id'], $before, $changes);
b3a27170 2290 $nsaved_uplinks = replace8021QPorts ('desired', $vswitch['object_id'], $before, $new_uplinks);
2996cbbb
DO
2291 }
2292 catch (Exception $e)
861292ee 2293 {
a7492e95 2294 $dbxlink->rollBack();
706d9175 2295 return buildRedirectURL (__FUNCTION__, 'ERR2', array(), NULL, NULL, $extra);
861292ee 2296 }
b3a27170 2297 if ($npulled + $nsaved_uplinks)
ec4d604c 2298 {
d973196a 2299 $query = $dbxlink->prepare ('UPDATE VLANSwitch SET mutex_rev = mutex_rev + 1, last_change = NOW(), out_of_sync = "yes" WHERE object_id = ?');
ec4d604c
DO
2300 $query->execute (array ($sic['object_id']));
2301 }
a7492e95 2302 $dbxlink->commit();
b3a27170
DO
2303 if ($nsaved_uplinks)
2304 initiateUplinksReverb ($vswitch['object_id'], $new_uplinks);
2305 return buildRedirectURL (__FUNCTION__, 'OK', array ($npulled + $nsaved_uplinks), NULL, NULL, $extra);
8198f2c6
DO
2306}
2307
8846b060
DO
2308$msgcode['bindVLANtoIPv4']['OK'] = 48;
2309$msgcode['bindVLANtoIPv4']['ERR'] = 110;
2310function bindVLANtoIPv4 ()
2311{
2312 assertUIntArg ('id'); // network id
2313 global $sic;
2314 $result = commitSupplementVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2315 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2316}
2317
2318$msgcode['unbindVLANfromIPv4']['OK'] = 49;
2319$msgcode['unbindVLANfromIPv4']['ERR'] = 111;
2320function unbindVLANfromIPv4 ()
2321{
2322 assertUIntArg ('id'); // network id
2323 global $sic;
2324 $result = commitReduceVLANIPv4 ($sic['vlan_ck'], $sic['id']);
2325 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2326}
2327
228c430c 2328$msgcode['process8021QSyncRequest']['OK'] = 63;
be28b696 2329$msgcode['process8021QSyncRequest']['ERR1'] = 109;
ca5d4cbc 2330$msgcode['process8021QSyncRequest']['ERR2'] = 191;
228c430c 2331function process8021QSyncRequest ()
be28b696 2332{
2e84a05b
DO
2333 // behave depending on current operation: exec8021QPull or exec8021QPush
2334 global $sic, $op;
be28b696
DO
2335 try
2336 {
2e84a05b 2337 if (FALSE === $done = exec8021QDeploy ($sic['object_id'], $op == 'exec8021QPush'))
ca5d4cbc
DO
2338 return buildRedirectURL (__FUNCTION__, 'ERR2'); // specific case
2339 return buildRedirectURL (__FUNCTION__, 'OK', array ($done));
be28b696
DO
2340 }
2341 catch (Exception $e)
2342 {
ca5d4cbc 2343 return buildRedirectURL (__FUNCTION__, 'ERR1'); // generic failure
be28b696 2344 }
be28b696
DO
2345}
2346
2347$msgcode['resolve8021QConflicts']['OK'] = 63;
bcd14540
DO
2348$msgcode['resolve8021QConflicts']['ERR1'] = 179;
2349$msgcode['resolve8021QConflicts']['ERR2'] = 109;
be28b696 2350function resolve8021QConflicts ()
07de6bb3 2351{
a7492e95 2352 global $sic, $dbxlink;
70cff23f 2353 assertUIntArg ('mutex_rev', TRUE); // counts from 0
de47b574
DO
2354 assertUIntArg ('nrows');
2355 // Divide submitted radio buttons into 3 groups:
19d1e731 2356 // left (saved version wins)
07de6bb3 2357 // asis (ignore)
19d1e731
DO
2358 // right (running version wins)
2359 $F = array();
339534a0 2360 for ($i = 0; $i < $sic['nrows']; $i++)
07de6bb3 2361 {
de47b574 2362 if (!array_key_exists ("i_${i}", $sic))
07de6bb3 2363 continue;
de47b574
DO
2364 // let's hope other inputs are in place
2365 switch ($sic["i_${i}"])
07de6bb3
DO
2366 {
2367 case 'left':
07de6bb3 2368 case 'right':
bcd14540 2369 $F[$sic["pn_${i}"]] = array
07de6bb3 2370 (
ca9e1c9d 2371 'mode' => $sic["rm_${i}"],
de47b574
DO
2372 'allowed' => $sic["ra_${i}"],
2373 'native' => $sic["rn_${i}"],
bcd14540 2374 'decision' => $sic["i_${i}"],
07de6bb3
DO
2375 );
2376 break;
2377 default:
2378 // don't care
2379 }
2380 }
a7492e95 2381 $dbxlink->beginTransaction();
07de6bb3
DO
2382 try
2383 {
a7492e95 2384 if (NULL === $vswitch = getVLANSwitchInfo ($sic['object_id'], 'FOR UPDATE'))
ec4d604c 2385 throw new InvalidArgException ('object_id', $sic['object_id'], 'VLAN domain is not set for this object');
9c45ea37 2386 if ($vswitch['mutex_rev'] != $sic['mutex_rev'])
bcd14540
DO
2387 {
2388 throw new RuntimeException ('expired form (table data has changed)');
2389 }
9c2201ba
DO
2390 $D = getStored8021QConfig ($vswitch['object_id'], 'desired');
2391 $C = getStored8021QConfig ($vswitch['object_id'], 'cached');
2392 $R = getRunning8021QConfig ($vswitch['object_id']);
d5e306b2 2393 $plan = get8021QSyncOptions ($vswitch, $D, $C, $R['portdata']);
bcd14540
DO
2394 $ndone = 0;
2395 foreach ($F as $port_name => $port)
2396 {
9c2201ba
DO
2397 if (!array_key_exists ($port_name, $plan))
2398 continue;
2399 elseif ($plan[$port_name]['status'] == 'merge_conflict')
bcd14540 2400 {
9c2201ba
DO
2401 // for R neither mutex nor revisions can be emulated, but revision change can be
2402 if (!same8021QConfigs ($port, $R['portdata'][$port_name]))
2403 throw new RuntimeException ('expired form (switch data has changed)');
1f8f67c2 2404 if ($port['decision'] == 'right') // D wins, frame R by writing value of R to C
9c2201ba 2405 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $port);
1f8f67c2 2406 elseif ($port['decision'] == 'left') // R wins, cross D up
9c2201ba
DO
2407 $ndone += upd8021QPort ('cached', $vswitch['object_id'], $port_name, $D[$port_name]);
2408 // otherwise there was no decision made
2409 }
1f8f67c2
DO
2410 elseif
2411 (
2412 $plan[$port_name]['status'] == 'delete_conflict' or
2413 $plan[$port_name]['status'] == 'martian_conflict'
2414 )
9c2201ba 2415 {
1f8f67c2 2416 if ($port['decision'] == 'left') // confirm deletion of local copy
9c2201ba 2417 $ndone += del8021QPort ($vswitch['object_id'], $port_name);
bcd14540 2418 }
9c2201ba 2419 // otherwise ignore a decision, which doesn't address a conflict
bcd14540 2420 }
07de6bb3 2421 }
bcd14540 2422 catch (RuntimeException $e)
07de6bb3 2423 {
a7492e95 2424 $dbxlink->rollBack();
bcd14540 2425 return buildRedirectURL (__FUNCTION__, 'ERR1');
ec4d604c 2426 }
bcd14540 2427 catch (Exception $e)
ec4d604c 2428 {
bcd14540
DO
2429 $dbxlink->rollBack();
2430 return buildRedirectURL (__FUNCTION__, 'ERR2');
ec4d604c 2431 }
a7492e95 2432 $dbxlink->commit();
bcd14540 2433 return buildRedirectURL (__FUNCTION__, 'OK', array ($ndone));
07de6bb3
DO
2434}
2435
e0d188ef
DO
2436$msgcode['addVLANSwitchTemplate']['OK'] = 48;
2437$msgcode['addVLANSwitchTemplate']['ERR'] = 110;
2438function addVLANSwitchTemplate()
2439{
2440 assertStringArg ('vst_descr');
2441 global $sic;
2442 $max_local_vlans = NULL;
2443 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2444 {
2445 assertUIntArg ('vst_maxvlans');
2446 $max_local_vlans = $sic['vst_maxvlans'];
2447 }
2448 $result = usePreparedInsertBlade
2449 (
2450 'VLANSwitchTemplate',
2451 array
2452 (
2453 'max_local_vlans' => $max_local_vlans,
2454 'description' => $sic['vst_descr'],
2455 )
2456 );
2457 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2458}
2459
2460$msgcode['delVLANSwitchTemplate']['OK'] = 49;
2461$msgcode['delVLANSwitchTemplate']['ERR'] = 111;
2462function delVLANSwitchTemplate()
2463{
2464 assertUIntArg ('vst_id');
2465 global $sic;
2466 $result = FALSE !== usePreparedDeleteBlade ('VLANSwitchTemplate', array ('id' => $sic['vst_id']));
2467 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2468}
2469
2470$msgcode['updVLANSwitchTemplate']['OK'] = 51;
2471$msgcode['updVLANSwitchTemplate']['ERR'] = 109;
2472function updVLANSwitchTemplate()
2473{
2474 assertUIntArg ('vst_id');
2475 assertStringArg ('vst_descr');
2476 global $sic;
2477 $max_local_vlans = NULL;
2478 if (array_key_exists ('vst_maxvlans', $sic) && mb_strlen ($sic['vst_maxvlans']))
2479 {
2480 assertUIntArg ('vst_maxvlans');
2481 $max_local_vlans = $sic['vst_maxvlans'];
2482 }
2483 $result = commitUpdateVST ($sic['vst_id'], $max_local_vlans, $sic['vst_descr']);
2484 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2485}
2486
2487$msgcode['addVSTRule']['OK'] = 48;
2488$msgcode['addVSTRule']['ERR'] = 110;
2489function addVSTRule()
2490{
2491 assertUIntArg ('vst_id');
2492 assertUIntArg ('rule_no');
2493 assertPCREArg ('port_pcre');
2494 assertStringArg ('port_role');
2495 assertStringArg ('wrt_vlans', TRUE);
638f198f 2496 assertStringArg ('description', TRUE);
e0d188ef
DO
2497 global $sic;
2498 $result = usePreparedInsertBlade
2499 (
2500 'VLANSTRule',
2501 array
2502 (
2503 'vst_id' => $sic['vst_id'],
2504 'rule_no' => $sic['rule_no'],
2505 'port_pcre' => $sic['port_pcre'],
2506 'port_role' => $sic['port_role'],
2507 'wrt_vlans' => $sic['wrt_vlans'],
638f198f 2508 'description' => $sic['description'],
e0d188ef
DO
2509 )
2510 );
2511 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2512}
2513
2514$msgcode['delVSTRule']['OK'] = 49;
2515$msgcode['delVSTRule']['ERR'] = 111;
2516function delVSTRule()
2517{
2518 assertUIntArg ('vst_id');
2519 assertUIntArg ('rule_no');
2520 global $sic;
2521 $result = FALSE !== usePreparedDeleteBlade
2522 (
2523 'VLANSTRule',
2524 array
2525 (
2526 'vst_id' => $sic['vst_id'],
2527 'rule_no' => $sic['rule_no']
2528 )
2529 );
2530 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2531}
2532
2533$msgcode['updVSTRule']['OK'] = 51;
2534$msgcode['updVSTRule']['ERR'] = 109;
2535function updVSTRule()
2536{
2537 assertUIntArg ('vst_id');
2538 assertUIntArg ('rule_no');
2539 assertUIntArg ('new_rule_no');
2540 assertPCREArg ('port_pcre');
2541 assertStringArg ('port_role');
2542 assertStringArg ('wrt_vlans', TRUE);
d0751ba2 2543 assertStringArg ('description', TRUE);
e0d188ef
DO
2544 global $sic;
2545 $result = commitUpdateVSTRule
2546 (
2547 $sic['vst_id'],
2548 $sic['rule_no'],
2549 $sic['new_rule_no'],
2550 $sic['port_pcre'],
2551 $sic['port_role'],
d0751ba2
DO
2552 $sic['wrt_vlans'],
2553 $sic['description']
e0d188ef
DO
2554 );
2555 return buildRedirectURL (__FUNCTION__, $result ? 'OK' : 'ERR');
2556}
2557
e673ee24 2558?>