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