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