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