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