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