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