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