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