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