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