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