r3090 - maintenance->trunk sync of changeset:3087
[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{
b6a7d936
DO
12 global $root, $page, $pageno, $tabno;
13 if ($nextpage === NULL)
14 $nextpage = $pageno;
15 if ($nexttab === NULL)
16 $nexttab = $tabno;
17 $url = "${root}?page=${nextpage}&tab=${nexttab}";
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
b6a7d936 41function buildRedirectURL ($callfunc, $status, $args = array(), $nextpage = NULL, $nexttab = NULL)
17c32f4c 42{
b6a7d936
DO
43 global $pageno, $tabno, $msgcode;
44 if ($nextpage === NULL)
45 $nextpage = $pageno;
46 if ($nexttab === NULL)
47 $nexttab = $tabno;
48 return buildWideRedirectURL (oneLiner ($msgcode[$callfunc][$status], $args), $nextpage, $nexttab);
17c32f4c
DO
49}
50
b6a7d936
DO
51$msgcode['addPortForwarding']['OK'] = 2;
52$msgcode['addPortForwarding']['ERR'] = 100;
e673ee24
DO
53function addPortForwarding ()
54{
2c6c7645
DO
55 assertUIntArg ('object_id', __FUNCTION__);
56 assertIPv4Arg ('localip', __FUNCTION__);
57 assertIPv4Arg ('remoteip', __FUNCTION__);
58 assertUIntArg ('localport', __FUNCTION__);
9bea3a8b 59 assertStringArg ('proto', __FUNCTION__);
2c6c7645 60 assertStringArg ('description', __FUNCTION__, TRUE);
103b1e1e 61 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
59a83bd8 62 if (!strlen ($remoteport))
f75eb878 63 $remoteport = $_REQUEST['localport'];
e673ee24 64
9bea3a8b
DO
65 $error = newPortForwarding
66 (
103b1e1e 67 $_REQUEST['object_id'],
9bea3a8b
DO
68 $_REQUEST['localip'],
69 $_REQUEST['localport'],
70 $_REQUEST['remoteip'],
71 $remoteport,
72 $_REQUEST['proto'],
73 $_REQUEST['description']
74 );
e673ee24
DO
75
76 if ($error == '')
135080d8 77 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 78 else
135080d8 79 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
80}
81
b6a7d936
DO
82$msgcode['delPortForwarding']['OK'] = 3;
83$msgcode['delPortForwarding']['ERR'] = 100;
e673ee24
DO
84function delPortForwarding ()
85{
103b1e1e
DO
86 assertUIntArg ('object_id', __FUNCTION__);
87 assertIPv4Arg ('localip', __FUNCTION__);
88 assertIPv4Arg ('remoteip', __FUNCTION__);
89 assertUIntArg ('localport', __FUNCTION__);
90 assertUIntArg ('remoteport', __FUNCTION__);
91 assertStringArg ('proto', __FUNCTION__);
e673ee24 92
103b1e1e
DO
93 $error = deletePortForwarding
94 (
95 $_REQUEST['object_id'],
96 $_REQUEST['localip'],
97 $_REQUEST['localport'],
98 $_REQUEST['remoteip'],
99 $_REQUEST['remoteport'],
100 $_REQUEST['proto']
101 );
e673ee24 102 if ($error == '')
135080d8 103 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 104 else
135080d8 105 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
106}
107
b6a7d936
DO
108$msgcode['updPortForwarding']['OK'] = 4;
109$msgcode['updPortForwarding']['ERR'] = 100;
e673ee24
DO
110function updPortForwarding ()
111{
103b1e1e
DO
112 assertUIntArg ('object_id', __FUNCTION__);
113 assertIPv4Arg ('localip', __FUNCTION__);
114 assertIPv4Arg ('remoteip', __FUNCTION__);
115 assertUIntArg ('localport', __FUNCTION__);
116 assertUIntArg ('remoteport', __FUNCTION__);
117 assertStringArg ('proto', __FUNCTION__);
118 assertStringArg ('description', __FUNCTION__);
e673ee24 119
103b1e1e
DO
120 $error = updatePortForwarding
121 (
122 $_REQUEST['object_id'],
123 $_REQUEST['localip'],
124 $_REQUEST['localport'],
125 $_REQUEST['remoteip'],
126 $_REQUEST['remoteport'],
127 $_REQUEST['proto'],
128 $_REQUEST['description']
129 );
e673ee24 130 if ($error == '')
135080d8 131 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 132 else
135080d8 133 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24
DO
134}
135
b6a7d936
DO
136$msgcode['addPortForObject']['OK'] = 5;
137$msgcode['addPortForObject']['ERR1'] = 101;
138$msgcode['addPortForObject']['ERR2'] = 100;
e673ee24
DO
139function addPortForObject ()
140{
103b1e1e
DO
141 assertUIntArg ('object_id', __FUNCTION__);
142 assertStringArg ('port_name', __FUNCTION__, TRUE);
59a83bd8 143 if (!strlen ($_REQUEST['port_name']))
135080d8 144 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e
DO
145 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
146 if ($error != '')
135080d8 147 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 148 else
135080d8 149 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
150}
151
b6a7d936
DO
152$msgcode['editPortForObject']['OK'] = 6;
153$msgcode['editPortForObject']['ERR1'] = 101;
154$msgcode['editPortForObject']['ERR2'] = 100;
e673ee24
DO
155function editPortForObject ()
156{
6be06cbb 157 assertUIntArg ('object_id', __FUNCTION__);
2c6c7645 158 assertUIntArg ('port_id', __FUNCTION__);
611170af 159 assertUIntArg ('port_type_id', __FUNCTION__);
d0a69ce8 160 // tolerate empty value now to produce custom informative message later
103b1e1e 161 assertStringArg ('name', __FUNCTION__, TRUE);
59a83bd8 162 if (!strlen ($_REQUEST['name']))
135080d8 163 return buildRedirectURL (__FUNCTION__, 'ERR1');
103b1e1e 164
59a83bd8 165 if (isset ($_REQUEST['reservation_comment']) and strlen ($_REQUEST['reservation_comment']))
e673ee24
DO
166 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
167 else
168 $port_rc = 'NULL';
6be06cbb 169 $error = commitUpdatePort ($_REQUEST['object_id'], $_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['port_type_id'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
103b1e1e 170 if ($error != '')
135080d8 171 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
e673ee24 172 else
135080d8 173 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e673ee24
DO
174}
175
b6a7d936
DO
176$msgcode['delPortFromObject']['OK'] = 7;
177$msgcode['delPortFromObject']['ERR'] = 100;
e673ee24
DO
178function delPortFromObject ()
179{
103b1e1e 180 assertUIntArg ('port_id', __FUNCTION__);
103b1e1e 181 $error = delObjectPort ($_REQUEST['port_id']);
e673ee24
DO
182
183 if ($error != '')
135080d8 184 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 185 else
135080d8 186 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
e673ee24
DO
187}
188
b6a7d936
DO
189$msgcode['linkPortForObject']['OK'] = 8;
190$msgcode['linkPortForObject']['ERR'] = 100;
e673ee24
DO
191function linkPortForObject ()
192{
103b1e1e
DO
193 assertUIntArg ('port_id', __FUNCTION__);
194 assertUIntArg ('remote_port_id', __FUNCTION__);
baf37d01
DO
195 assertStringArg ('port_name', __FUNCTION__, TRUE);
196 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
197 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 198
103b1e1e 199 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
e673ee24 200 if ($error != '')
135080d8 201 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 202 else
135080d8 203 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
204}
205
b6a7d936
DO
206$msgcode['unlinkPortForObject']['OK'] = 9;
207$msgcode['unlinkPortForObject']['ERR'] = 100;
e673ee24
DO
208function unlinkPortForObject ()
209{
103b1e1e 210 assertUIntArg ('port_id', __FUNCTION__);
baf37d01
DO
211 assertStringArg ('port_name', __FUNCTION__, TRUE);
212 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
213 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
e673ee24 214
103b1e1e 215 $error = unlinkPort ($_REQUEST['port_id']);
e673ee24 216 if ($error != '')
135080d8 217 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 218 else
135080d8 219 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
e673ee24
DO
220}
221
b6a7d936
DO
222$msgcode['addMultiPorts']['OK'] = 10;
223$msgcode['addMultiPorts']['ERR'] = 123;
e673ee24
DO
224function addMultiPorts ()
225{
2c6c7645
DO
226 assertStringArg ('format', __FUNCTION__);
227 assertStringArg ('input', __FUNCTION__);
228 assertUIntArg ('port_type', __FUNCTION__);
229 assertUIntArg ('object_id', __FUNCTION__);
e673ee24
DO
230 $format = $_REQUEST['format'];
231 $port_type = $_REQUEST['port_type'];
232 $object_id = $_REQUEST['object_id'];
233 // Input lines are escaped, so we have to explode and to chop by 2-char
234 // \n and \r respectively.
235 $lines1 = explode ('\n', $_REQUEST['input']);
236 foreach ($lines1 as $line)
237 {
238 $parts = explode ('\r', $line);
239 reset ($parts);
59a83bd8 240 if (!strlen ($parts[0]))
e673ee24
DO
241 continue;
242 else
243 $lines2[] = rtrim ($parts[0]);
244 }
245 $ports = array();
246 foreach ($lines2 as $line)
247 {
248 switch ($format)
249 {
250 case 'fisxii':
251 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
252 list ($slot, $port) = explode ('/', $words[0]);
253 $ports[] = array
254 (
255 'name' => "e ${slot}/${port}",
256 'l2address' => $words[8],
257 'label' => "slot ${slot} port ${port}"
258 );
259 break;
260 case 'c3600asy':
261 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', trim (substr ($line, 3))));
262/*
263How Async Lines are Numbered in Cisco 3600 Series Routers
264http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
265
266Understanding 16- and 32-Port Async Network Modules
267http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00800a93f0.shtml
268*/
269 $async = $words[0];
270 $slot = floor (($async - 1) / 32);
271 $octalgroup = floor (($async - 1 - $slot * 32) / 8);
272 $cable = $async - $slot * 32 - $octalgroup * 8;
273 $og_label[0] = 'async 0-7';
274 $og_label[1] = 'async 8-15';
275 $og_label[2] = 'async 16-23';
276 $og_label[3] = 'async 24-31';
277 $ports[] = array
278 (
279 'name' => "async ${async}",
280 'l2address' => '',
281 'label' => "slot ${slot} " . $og_label[$octalgroup] . " cable ${cable}"
282 );
283 break;
284 case 'fiwg':
285 $words = explode (' ', ereg_replace ('[[:space:]]+', ' ', $line));
286 $ifnumber = $words[0] * 1;
287 $ports[] = array
288 (
289 'name' => "e ${ifnumber}",
290 'l2address' => "${words[8]}",
291 'label' => "${ifnumber}"
292 );
293 break;
351d4dbf 294 case 'ssv1':
5b585342 295 $words = explode (' ', $line);
59a83bd8 296 if (!strlen ($words[0]) or !strlen ($words[1]))
351d4dbf
DO
297 continue;
298 $ports[] = array
299 (
300 'name' => $words[0],
301 'l2address' => $words[1],
302 'label' => ''
303 );
304 break;
e673ee24 305 default:
135080d8 306 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
307 break;
308 }
309 }
310 // Create ports, if they don't exist.
311 $added_count = $updated_count = $error_count = 0;
312 foreach ($ports as $port)
313 {
314 $port_id = getPortID ($object_id, $port['name']);
315 if ($port_id === NULL)
316 {
317 $result = commitAddPort ($object_id, $port['name'], $port_type, $port['label'], $port['l2address']);
318 if ($result == '')
319 $added_count++;
320 else
321 $error_count++;
322 }
323 else
324 {
6be06cbb 325 $result = commitUpdatePort ($object_id, $port_id, $port['name'], $port_type, $port['label'], $port['l2address']);
e673ee24
DO
326 if ($result == '')
327 $updated_count++;
328 else
329 $error_count++;
330 }
331 }
135080d8 332 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $updated_count, $error_count));
e673ee24
DO
333}
334
b6a7d936
DO
335$msgcode['updIPv4Allocation']['OK'] = 12;
336$msgcode['updIPv4Allocation']['ERR'] = 100;
b4c1ef87 337function updIPv4Allocation ()
e673ee24 338{
b4c1ef87
DO
339 assertIPv4Arg ('ip', __FUNCTION__);
340 assertUIntArg ('object_id', __FUNCTION__);
341 assertStringArg ('bond_name', __FUNCTION__, TRUE);
342 assertStringArg ('bond_type', __FUNCTION__);
e673ee24 343
b4c1ef87 344 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
e673ee24 345 if ($error != '')
135080d8 346 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 347 else
135080d8 348 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
349}
350
b6a7d936
DO
351$msgcode['delIPv4Allocation']['OK'] = 14;
352$msgcode['delIPv4Allocation']['ERR'] = 100;
b4c1ef87 353function delIPv4Allocation ()
e673ee24 354{
b4c1ef87
DO
355 assertIPv4Arg ('ip', __FUNCTION__);
356 assertUIntArg ('object_id', __FUNCTION__);
e673ee24 357
b4c1ef87 358 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
e673ee24 359 if ($error != '')
135080d8 360 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 361 else
135080d8 362 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
363}
364
b6a7d936
DO
365$msgcode['addIPv4Allocation']['OK'] = 13;
366$msgcode['addIPv4Allocation']['ERR1'] = 170;
367$msgcode['addIPv4Allocation']['ERR2'] = 100;
b4c1ef87 368function addIPv4Allocation ()
e673ee24 369{
b4c1ef87
DO
370 assertIPv4Arg ('ip', __FUNCTION__);
371 assertUIntArg ('object_id', __FUNCTION__);
372 assertStringArg ('bond_name', __FUNCTION__, TRUE);
373 assertStringArg ('bond_type', __FUNCTION__);
da958e52 374
b4c1ef87
DO
375 // Strip masklen.
376 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
2be3fd23 377 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
135080d8 378 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
5222f192 379
b4c1ef87 380 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
5222f192 381 if ($error != '')
135080d8 382 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
53ef3908 383 $address = getIPv4Address ($ip);
59a83bd8 384 if ($address['reserved'] == 'yes' or strlen ($address['name']))
b4c1ef87
DO
385 {
386 $release = getConfigVar ('IPV4_AUTO_RELEASE');
387 if ($release >= 1)
388 $address['reserved'] = 'no';
389 if ($release >= 2)
390 $address['name'] = '';
391 updateAddress ($ip, $address['name'], $address['reserved']);
392 }
135080d8 393 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
394}
395
b6a7d936
DO
396$msgcode['addIPv4Prefix']['OK'] = 23;
397$msgcode['addIPv4Prefix']['ERR'] = 100;
398$msgcode['addIPv4Prefix']['ERR1'] = 173;
399$msgcode['addIPv4Prefix']['ERR2'] = 174;
400$msgcode['addIPv4Prefix']['ERR3'] = 175;
401$msgcode['addIPv4Prefix']['ERR4'] = 176;
42023f03 402function addIPv4Prefix ()
e673ee24 403{
2c6c7645 404 assertStringArg ('range', __FUNCTION__);
e4b9a099 405 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 406
fa05e3de
DO
407 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
408 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42023f03 409 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
e673ee24 410 if ($error != '')
135080d8 411 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 412 else
135080d8 413 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
414}
415
b6a7d936
DO
416$msgcode['delIPv4Prefix']['OK'] = 24;
417$msgcode['delIPv4Prefix']['ERR'] = 100;
42023f03 418function delIPv4Prefix ()
e673ee24 419{
da958e52 420 assertUIntArg ('id', __FUNCTION__);
42023f03 421 $error = destroyIPv4Prefix ($_REQUEST['id']);
e673ee24 422 if ($error != '')
135080d8 423 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 424 else
135080d8 425 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
426}
427
b6a7d936
DO
428$msgcode['updIPv4Prefix']['OK'] = 25;
429$msgcode['updIPv4Prefix']['ERR'] = 100;
04d619d0 430function updIPv4Prefix ()
e673ee24 431{
da958e52 432 assertUIntArg ('id', __FUNCTION__);
e4b9a099 433 assertStringArg ('name', __FUNCTION__, TRUE);
99ab184f
DO
434 assertStringArg ('comment', __FUNCTION__, TRUE);
435 global $sic;
436 if (strlen ($error = updateIPv4Network_real ($sic['id'], $sic['name'], $sic['comment'])))
135080d8 437 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 438 else
135080d8 439 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
440}
441
b6a7d936
DO
442$msgcode['editAddress']['OK'] = 27;
443$msgcode['editAddress']['ERR'] = 100;
e673ee24
DO
444function editAddress ()
445{
da958e52
DO
446 assertIPv4Arg ('ip', __FUNCTION__);
447 assertStringArg ('name', __FUNCTION__, TRUE);
e673ee24 448
e673ee24
DO
449 if (isset ($_REQUEST['reserved']))
450 $reserved = $_REQUEST['reserved'];
451 else
452 $reserved = 'off';
da958e52 453 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
e673ee24 454 if ($error != '')
135080d8 455 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e673ee24 456 else
135080d8 457 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
458}
459
b6a7d936
DO
460$msgcode['createUser']['OK'] = 40;
461$msgcode['createUser']['ERR'] = 102;
cced6b7d 462function createUser ()
e673ee24 463{
2c6c7645
DO
464 assertStringArg ('username', __FUNCTION__);
465 assertStringArg ('realname', __FUNCTION__, TRUE);
466 assertStringArg ('password', __FUNCTION__);
e673ee24 467 $username = $_REQUEST['username'];
93bdb7ba 468 $password = sha1 ($_REQUEST['password']);
e673ee24 469 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
f857f71f 470 if ($result != TRUE)
135080d8 471 return buildRedirectURL (__FUNCTION__, 'ERR', array ($username));
f857f71f
DO
472 if (isset ($_REQUEST['taglist']))
473 produceTagsForLastRecord ('user', $_REQUEST['taglist']);
474 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24
DO
475}
476
b6a7d936
DO
477$msgcode['updateUser']['OK'] = 39;
478$msgcode['updateUser']['ERR1'] = 103;
479$msgcode['updateUser']['ERR1'] = 104;
cced6b7d 480function updateUser ()
e673ee24 481{
cced6b7d 482 assertUIntArg ('user_id', __FUNCTION__);
2c6c7645
DO
483 assertStringArg ('username', __FUNCTION__);
484 assertStringArg ('realname', __FUNCTION__, TRUE);
485 assertStringArg ('password', __FUNCTION__);
e673ee24
DO
486 $username = $_REQUEST['username'];
487 $new_password = $_REQUEST['password'];
0b2c74cb 488 if (NULL == ($userinfo = spotEntity ('user', $_REQUEST['user_id'])))
135080d8 489 return buildRedirectURL (__FUNCTION__, 'ERR1');
e673ee24 490 // Update user password only if provided password is not the same as current password hash.
b82cce3f 491 if ($new_password != $userinfo['user_password_hash'])
93bdb7ba 492 $new_password = sha1 ($new_password);
103b1e1e 493 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
e673ee24 494 if ($result == TRUE)
135080d8 495 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
e673ee24 496 else
135080d8 497 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($username));
e673ee24
DO
498}
499
b6a7d936
DO
500$msgcode['savePortMap']['OK'] = 44;
501$msgcode['savePortMap']['ERR'] = 108;
e673ee24
DO
502// This function find differences in users's submit and PortCompat table
503// and modifies database accordingly.
504function savePortMap ()
505{
7fa7047a
DO
506 // values' format doesn't matter, only keys are used
507 $ptlist = readChapter (CHAP_PORTTYPE);
e673ee24
DO
508 $oldCompat = getPortCompat();
509 $newCompat = array();
510 foreach (array_keys ($ptlist) as $leftKey)
511 foreach (array_keys ($ptlist) as $rightKey)
512 if (isset ($_REQUEST["atom_${leftKey}_${rightKey}"]))
513 $newCompat[] = array ('type1' => $leftKey, 'type2' => $rightKey);
514 // Build the new matrix from user's submit and compare it to
515 // the old matrix. Those pairs which appear on
516 // new matrix only, have to be stored in PortCompat table. Those who appear
517 // on the old matrix only, should be removed from PortCompat table.
518 // Those present in both matrices should be skipped.
519 $oldCompatTable = buildPortCompatMatrixFromList ($ptlist, $oldCompat);
520 $newCompatTable = buildPortCompatMatrixFromList ($ptlist, $newCompat);
521 $error_count = $success_count = 0;
522 foreach (array_keys ($ptlist) as $type1)
523 foreach (array_keys ($ptlist) as $type2)
524 if ($oldCompatTable[$type1][$type2] != $newCompatTable[$type1][$type2])
525 switch ($oldCompatTable[$type1][$type2])
526 {
527 case TRUE: // new value is FALSE
528 if (removePortCompat ($type1, $type2) === TRUE)
529 $success_count++;
530 else
531 $error_count++;
532 break;
533 case FALSE: // new value is TRUE
534 if (addPortCompat ($type1, $type2) === TRUE)
535 $success_count++;
536 else
537 $error_count++;
538 break;
539 default:
ebb5441d 540 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
e673ee24
DO
541 break;
542 }
103b1e1e 543 if ($error_count == 0)
135080d8 544 return buildRedirectURL (__FUNCTION__, 'OK', array ($error_count, $success_count));
103b1e1e 545 else
135080d8 546 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error_count, $success_count));
e673ee24
DO
547}
548
b6a7d936
DO
549$msgcode['updateDictionary']['OK'] = 51;
550$msgcode['updateDictionary']['ERR'] = 109;
e673ee24
DO
551function updateDictionary ()
552{
2c6c7645
DO
553 assertUIntArg ('chapter_no', __FUNCTION__);
554 assertUIntArg ('dict_key', __FUNCTION__);
555 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 556 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
135080d8 557 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 558 else
135080d8 559 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
560}
561
b6a7d936
DO
562$msgcode['supplementDictionary']['OK'] = 52;
563$msgcode['supplementDictionary']['ERR'] = 110;
e673ee24
DO
564function supplementDictionary ()
565{
2c6c7645
DO
566 assertUIntArg ('chapter_no', __FUNCTION__);
567 assertStringArg ('dict_value', __FUNCTION__);
e673ee24 568 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
135080d8 569 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 570 else
135080d8 571 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
572}
573
b6a7d936
DO
574$msgcode['reduceDictionary']['OK'] = 50;
575$msgcode['reduceDictionary']['ERR'] = 111;
e673ee24
DO
576function reduceDictionary ()
577{
2c6c7645
DO
578 assertUIntArg ('chapter_no', __FUNCTION__);
579 assertUIntArg ('dict_key', __FUNCTION__);
e673ee24 580 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
135080d8 581 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 582 else
135080d8 583 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
584}
585
b6a7d936
DO
586$msgcode['addChapter']['OK'] = 55;
587$msgcode['addChapter']['ERR'] = 112;
e673ee24
DO
588function addChapter ()
589{
2c6c7645 590 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 591 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
135080d8 592 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 593 else
135080d8 594 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
595}
596
b6a7d936
DO
597$msgcode['updateChapter']['OK'] = 54;
598$msgcode['updateChapter']['ERR'] = 113;
e673ee24
DO
599function updateChapter ()
600{
2c6c7645
DO
601 assertUIntArg ('chapter_no', __FUNCTION__);
602 assertStringArg ('chapter_name', __FUNCTION__);
e673ee24 603 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
135080d8 604 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 605 else
135080d8 606 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
607}
608
b6a7d936
DO
609$msgcode['delChapter']['OK'] = 53;
610$msgcode['delChapter']['ERR'] = 114;
e673ee24
DO
611function delChapter ()
612{
2c6c7645 613 assertUIntArg ('chapter_no', __FUNCTION__);
e673ee24 614 if (commitDeleteChapter ($_REQUEST['chapter_no']))
135080d8 615 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 616 else
135080d8 617 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
618}
619
b6a7d936
DO
620$msgcode['changeAttribute']['OK'] = 46;
621$msgcode['changeAttribute']['ERR'] = 115;
e673ee24
DO
622function changeAttribute ()
623{
2c6c7645
DO
624 assertUIntArg ('attr_id', __FUNCTION__);
625 assertStringArg ('attr_name', __FUNCTION__);
e673ee24 626 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
135080d8 627 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 628 else
135080d8 629 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
630}
631
b6a7d936
DO
632$msgcode['createAttribute']['OK'] = 45;
633$msgcode['createAttribute']['ERR'] = 116;
e673ee24
DO
634function createAttribute ()
635{
2c6c7645
DO
636 assertStringArg ('attr_name', __FUNCTION__);
637 assertStringArg ('attr_type', __FUNCTION__);
e673ee24 638 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
135080d8 639 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
e673ee24 640 else
135080d8 641 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
642}
643
b6a7d936
DO
644$msgcode['deleteAttribute']['OK'] = 47;
645$msgcode['deleteAttribute']['ERR'] = 117;
e673ee24
DO
646function deleteAttribute ()
647{
2c6c7645 648 assertUIntArg ('attr_id', __FUNCTION__);
e673ee24 649 if (commitDeleteAttribute ($_REQUEST['attr_id']))
135080d8 650 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 651 else
135080d8 652 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
653}
654
b6a7d936 655$msgcode['supplementAttrMap']['OK'] = 48;
7028a42c
DO
656$msgcode['supplementAttrMap']['ERR1'] = 154;
657$msgcode['supplementAttrMap']['ERR2'] = 118;
e673ee24
DO
658function supplementAttrMap ()
659{
2c6c7645
DO
660 assertUIntArg ('attr_id', __FUNCTION__);
661 assertUIntArg ('objtype_id', __FUNCTION__);
7028a42c
DO
662 $attrMap = getAttrMap();
663 if ($attrMap[$_REQUEST['attr_id']]['type'] != 'dict')
664 $chapter_id = 'NULL';
665 else
666 {
667 assertUIntArg ('chapter_no', __FUNCTION__); // FIXME: this doesn't fail on 0 (ticket:272)
668 if (0 == ($chapter_id = $_REQUEST['chapter_no']))
669 return buildRedirectURL (__FUNCTION__, 'ERR1', array ('chapter not selected'));
670 }
671 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $chapter_id) === TRUE)
135080d8 672 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 673 else
7028a42c 674 return buildRedirectURL (__FUNCTION__, 'ERR2');
e673ee24
DO
675}
676
b6a7d936
DO
677$msgcode['reduceAttrMap']['OK'] = 49;
678$msgcode['reduceAttrMap']['ERR'] = 119;
e673ee24
DO
679function reduceAttrMap ()
680{
2c6c7645
DO
681 assertUIntArg ('attr_id', __FUNCTION__);
682 assertUIntArg ('objtype_id', __FUNCTION__);
e673ee24 683 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
135080d8 684 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 685 else
135080d8 686 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
687}
688
b6a7d936
DO
689$msgcode['clearSticker']['OK'] = 15;
690$msgcode['clearSticker']['ERR'] = 120;
22bb04da 691function clearSticker ()
e673ee24 692{
2c6c7645
DO
693 assertUIntArg ('attr_id', __FUNCTION__);
694 assertUIntArg ('object_id', __FUNCTION__);
22bb04da 695 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
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{
704 assertUIntArg ('object_id', __FUNCTION__);
705
e97e8866
DO
706 if (!isset ($_REQUEST['got_atoms']))
707 {
708 unset($_GET['page']);
709 unset($_GET['tab']);
710 unset($_GET['op']);
711 unset($_POST['page']);
712 unset($_POST['tab']);
713 unset($_POST['op']);
714 return buildWideRedirectURL (array(), NULL, NULL, array_merge ($_GET, $_POST));
715 }
716 $object_id = $_REQUEST['object_id'];
717 $workingRacksData = array();
718 foreach ($_REQUEST['rackmulti'] as $cand_id)
4fbb5a00 719 {
e97e8866 720 if (!isset ($workingRacksData[$cand_id]))
4fbb5a00 721 {
e97e8866
DO
722 $rackData = spotEntity ('rack', $cand_id);
723 if ($rackData == NULL)
4fbb5a00 724 {
e97e8866
DO
725 showError ('rack not found', __FUNCTION__);
726 return;
4fbb5a00 727 }
e97e8866
DO
728 amplifyCell ($rackData);
729 $workingRacksData[$cand_id] = $rackData;
4fbb5a00 730 }
e97e8866 731 }
2135fd83 732
e97e8866
DO
733 foreach ($workingRacksData as &$rd)
734 applyObjectMountMask ($rd, $object_id);
4fbb5a00 735
e97e8866
DO
736 $oldMolecule = getMoleculeForObject ($object_id);
737 $changecnt = 0;
738 $log = array();
739 foreach ($workingRacksData as $rack_id => $rackData)
4fbb5a00 740 {
e97e8866
DO
741 $logrecord = processGridForm ($rackData, 'F', 'T', $object_id);
742 $log[] = $logrecord;
743 if ($logrecord['code'] == 300)
744 continue;
745 $changecnt++;
746 // Reload our working copy after form processing.
747 $rackData = spotEntity ('rack', $cand_id);
748 amplifyCell ($rackData);
749 applyObjectMountMask ($rackData, $object_id);
750 $workingRacksData[$rack_id] = $rackData;
4fbb5a00 751 }
e97e8866
DO
752 if (!$changecnt)
753 return buildRedirectURL (__FUNCTION__, 'OK', $changecnt);
754 // Log a record.
755 $newMolecule = getMoleculeForObject ($object_id);
756 $oc = count ($oldMolecule);
757 $nc = count ($newMolecule);
758 $omid = $oc ? createMolecule ($oldMolecule) : 'NULL';
759 $nmid = $nc ? createMolecule ($newMolecule) : 'NULL';
760 global $remote_username;
761 $comment = empty ($_REQUEST['comment']) ? 'NULL' : "'${_REQUEST['comment']}'";
762 $query =
763 "insert into MountOperation(object_id, old_molecule_id, new_molecule_id, user_name, comment) " .
764 "values (${object_id}, ${omid}, ${nmid}, '${remote_username}', ${comment})";
765 global $dbxlink;
766 $result = $dbxlink->query ($query);
767 if ($result == NULL)
768 $log[] = array ('code' => 500, 'message' => 'SQL query failed during history logging.');
769 else
770 $log[] = array ('code' => 200, 'message' => 'History logged.');
771 return buildWideRedirectURL ($log);
4fbb5a00
DY
772}
773
b6a7d936
DO
774$msgcode['updateObject']['OK'] = 16;
775$msgcode['updateObject']['ERR'] = 121;
22bb04da
DO
776function updateObject ()
777{
0f63538a 778 assertUIntArg ('num_attrs', __FUNCTION__, TRUE);
22bb04da
DO
779 assertUIntArg ('object_id', __FUNCTION__);
780 assertUIntArg ('object_type_id', __FUNCTION__);
781 assertStringArg ('object_name', __FUNCTION__, TRUE);
782 assertStringArg ('object_label', __FUNCTION__, TRUE);
783 assertStringArg ('object_barcode', __FUNCTION__, TRUE);
784 assertStringArg ('object_asset_no', __FUNCTION__, TRUE);
785 if (isset ($_REQUEST['object_has_problems']) and $_REQUEST['object_has_problems'] == 'on')
786 $has_problems = 'yes';
787 else
788 $has_problems = 'no';
789
790 if (commitUpdateObject (
791 $_REQUEST['object_id'],
792 $_REQUEST['object_name'],
793 $_REQUEST['object_label'],
794 $_REQUEST['object_barcode'],
795 $_REQUEST['object_type_id'],
796 $has_problems,
797 $_REQUEST['object_asset_no'],
798 $_REQUEST['object_comment']
799 ) !== TRUE)
135080d8 800 return buildRedirectURL (__FUNCTION__, 'ERR');
22bb04da 801
cdd3efe9 802 // Update optional attributes
f59e8cf5 803 $oldvalues = getAttrValues ($_REQUEST['object_id']);
0dfb8a2a 804 $result = array();
0f63538a
DO
805 $num_attrs = isset ($_REQUEST['num_attrs']) ? $_REQUEST['num_attrs'] : 0;
806 for ($i = 0; $i < $num_attrs; $i++)
0dfb8a2a 807 {
2c6c7645 808 assertUIntArg ("${i}_attr_id", __FUNCTION__);
0dfb8a2a
DO
809 $attr_id = $_REQUEST["${i}_attr_id"];
810
811 // Field is empty, delete attribute and move on.
59a83bd8 812 if (!strlen ($_REQUEST["${i}_value"]))
0dfb8a2a 813 {
103b1e1e 814 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
0dfb8a2a
DO
815 continue;
816 }
817
818 // The value could be uint/float, but we don't know ATM. Let SQL
819 // server check this and complain.
2c6c7645 820 assertStringArg ("${i}_value", __FUNCTION__);
0dfb8a2a
DO
821 $value = $_REQUEST["${i}_value"];
822 switch ($oldvalues[$attr_id]['type'])
823 {
824 case 'uint':
825 case 'float':
826 case 'string':
827 $oldvalue = $oldvalues[$attr_id]['value'];
828 break;
829 case 'dict':
830 $oldvalue = $oldvalues[$attr_id]['key'];
831 break;
832 default:
61e269b5 833 showError ('Internal structure error', __FUNCTION__);
0dfb8a2a
DO
834 die;
835 }
59a83bd8 836 if ($value === $oldvalue) // ('' == 0), but ('' !== 0)
0dfb8a2a
DO
837 continue;
838
839 // Note if the queries succeed or not, it determines which page they see.
103b1e1e 840 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
0dfb8a2a 841 }
103b1e1e 842 if (in_array (FALSE, $result))
135080d8 843 return buildRedirectURL (__FUNCTION__, 'ERR');
0dfb8a2a 844
cdd3efe9
AD
845 // Invalidate thumb cache of all racks objects could occupy.
846 foreach (getResidentRacksData ($_REQUEST['object_id'], FALSE) as $rack_id)
847 resetThumbCache ($rack_id);
848
135080d8 849 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24
DO
850}
851
42d73cbf
DY
852
853function addMultipleObjects()
854{
7abce0b7 855 $log = emptyLog();
42d73cbf 856 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
857 $max = getConfigVar ('MASSCOUNT');
858 for ($i = 0; $i < $max; $i++)
859 {
860 if (!isset ($_REQUEST["${i}_object_type_id"]))
861 {
57777e4a 862 $log = mergeLogs ($log, oneLiner (184, array ($i + 1)));
42d73cbf
DY
863 break;
864 }
865 assertUIntArg ("${i}_object_type_id", __FUNCTION__, TRUE);
866 assertStringArg ("${i}_object_name", __FUNCTION__, TRUE);
867 assertStringArg ("${i}_object_label", __FUNCTION__, TRUE);
868 assertStringArg ("${i}_object_asset_no", __FUNCTION__, TRUE);
869 assertStringArg ("${i}_object_barcode", __FUNCTION__, TRUE);
7abce0b7 870 $name = $_REQUEST["${i}_object_name"];
42d73cbf
DY
871
872 // It's better to skip silently, than to print a notice.
7abce0b7 873 if ($_REQUEST["${i}_object_type_id"] == 0)
42d73cbf 874 continue;
7abce0b7
DO
875 if (commitAddObject
876 (
877 $name,
878 $_REQUEST["${i}_object_label"],
879 $_REQUEST["${i}_object_barcode"],
880 $_REQUEST["${i}_object_type_id"],
881 $_REQUEST["${i}_object_asset_no"],
882 $taglist
883 ) === TRUE)
57777e4a 884 $log = mergeLogs ($log, oneLiner (80, array ($name)));
42d73cbf 885 else
57777e4a 886 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 887 }
7abce0b7 888 return buildWideRedirectURL ($log);
42d73cbf
DY
889}
890
891function addLotOfObjects()
892{
7abce0b7 893 $log = emptyLog();
42d73cbf 894 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
42d73cbf
DY
895 assertUIntArg ('global_type_id', __FUNCTION__, TRUE);
896 assertStringArg ('namelist', __FUNCTION__, TRUE);
897 $global_type_id = $_REQUEST['global_type_id'];
59a83bd8 898 if ($global_type_id == 0 or !strlen ($_REQUEST['namelist']))
57777e4a 899 $log = mergeLogs ($log, oneLiner (186));
42d73cbf
DY
900 else
901 {
902 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
903 $names1 = explode ('\n', $_REQUEST['namelist']);
904 $names2 = array();
905 foreach ($names1 as $line)
906 {
907 $parts = explode ('\r', $line);
908 reset ($parts);
59a83bd8 909 if (!strlen ($parts[0]))
42d73cbf
DY
910 continue;
911 else
912 $names2[] = rtrim ($parts[0]);
913 }
57777e4a
DO
914 foreach ($names2 as $name)
915 if (commitAddObject ($name, '', '', $global_type_id, '', $taglist) === TRUE)
916 $log = mergeLogs ($log, oneLiner (80, array ($name)));
42d73cbf 917 else
57777e4a 918 $log = mergeLogs ($log, oneLiner (185, array ($name)));
42d73cbf 919 }
7abce0b7 920 return buildWideRedirectURL ($log);
42d73cbf
DY
921}
922
b6a7d936
DO
923$msgcode['deleteObject']['OK'] = 76;
924$msgcode['deleteObject']['ERR'] = 100;
52b34485
AD
925function deleteObject ()
926{
927 assertUIntArg ('object_id', __FUNCTION__);
6297d584 928 if (NULL === ($oinfo = spotEntity ('object', $_REQUEST['object_id'])))
c78a40ec
DO
929 return buildRedirectURL (__FUNCTION__, 'ERR', array ('object not found'));
930
f0e2c99f 931 $racklist = getResidentRacksData ($_REQUEST['object_id'], FALSE);
52b34485 932 $error = commitDeleteObject ($_REQUEST['object_id']);
f0e2c99f
DO
933 foreach ($racklist as $rack_id)
934 resetThumbCache ($rack_id);
52b34485
AD
935
936 if ($error != '')
135080d8 937 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
52b34485 938
c78a40ec 939 return buildRedirectURL (__FUNCTION__, 'OK', array ($oinfo['dname']));
52b34485
AD
940}
941
b6a7d936
DO
942$msgcode['useupPort']['OK'] = 11;
943$msgcode['useupPort']['ERR'] = 124;
e673ee24
DO
944function useupPort ()
945{
2c6c7645 946 assertUIntArg ('port_id', __FUNCTION__);
e673ee24 947 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
135080d8 948 return buildRedirectURL (__FUNCTION__, 'OK');
e673ee24 949 else
135080d8 950 return buildRedirectURL (__FUNCTION__, 'ERR');
e673ee24
DO
951}
952
b6a7d936
DO
953$msgcode['updateUI']['OK'] = 56;
954$msgcode['updateUI']['ERR'] = 125;
4fe32e78
DY
955function updateUI ()
956{
2c6c7645 957 assertUIntArg ('num_vars', __FUNCTION__);
4fe32e78 958
103b1e1e 959 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
4fe32e78 960 {
2c6c7645
DO
961 assertStringArg ("${i}_varname", __FUNCTION__);
962 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
4fe32e78 963 $varname = $_REQUEST["${i}_varname"];
4fe32e78 964 $varvalue = $_REQUEST["${i}_varvalue"];
4fe32e78 965
c461c579 966 // If form value = value in DB, don't bother updating DB
59a83bd8 967 if ($varvalue === getConfigVar ($varname))
4fe32e78
DY
968 continue;
969
970 // Note if the queries succeed or not, it determines which page they see.
c461c579 971 $error = setConfigVar ($varname, $varvalue, TRUE);
59a83bd8
DO
972 if (strlen ($error))
973 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
4fe32e78 974 }
135080d8 975 return buildRedirectURL (__FUNCTION__, 'OK');
4fe32e78
DY
976}
977
b6a7d936 978$msgcode['resetUIConfig']['OK'] = 57;
b07f617c
DO
979function resetUIConfig()
980{
b07f617c
DO
981 setConfigVar ('MASSCOUNT','15');
982 setConfigVar ('MAXSELSIZE','30');
b07f617c
DO
983 setConfigVar ('ROW_SCALE','2');
984 setConfigVar ('PORTS_PER_ROW','12');
985 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
986 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
b5ce46aa
DO
987 setConfigVar ('DEFAULT_SLB_VS_PORT','');
988 setConfigVar ('DEFAULT_SLB_RS_PORT','');
72281138 989 setConfigVar ('DETECT_URLS','no');
4b8d413e 990 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
b108de49 991 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
8dfb997c 992 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
194e3748
DO
993 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
994 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
995 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
a477e405 996 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
2754cefc 997 setConfigVar ('IPV4_AUTO_RELEASE','1');
a53dc9df 998 setConfigVar ('SHOW_LAST_TAB', 'no');
f0ed1181 999 setConfigVar ('EXT_IPV4_VIEW', 'yes');
efee2692 1000 setConfigVar ('TREE_THRESHOLD', '25');
2be3fd23 1001 setConfigVar ('IPV4_JAYWALK', 'no');
9318d2ef 1002 setConfigVar ('ADDNEW_AT_TOP', 'yes');
64347dcf 1003 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
37e59768
DO
1004 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
1005 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
1006 setConfigVar ('PREVIEW_TEXT_COLS', '80');
1007 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
f3d274bf 1008 setConfigVar ('VENDOR_SIEVE', '');
35bf153d
DO
1009 setConfigVar ('IPV4LB_LISTSRC', '{$typeid_4}');
1010 setConfigVar ('IPV4OBJ_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8} or {$typeid_12} or {$typeid_445} or {$typeid_447}');
1011 setConfigVar ('IPV4NAT_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
1012 setConfigVar ('ASSETWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
1013 setConfigVar ('NAMEWARN_LISTSRC','{$typeid_4} or {$typeid_7} or {$typeid_8}');
f5883ec1 1014 setConfigVar ('RACKS_PER_ROW','12');
590e1281 1015 setConfigVar ('FILTER_PREDICATE_SIEVE','');
23cdc7e9
DO
1016 setConfigVar ('FILTER_DEFAULT_ANDOR','or');
1017 setConfigVar ('FILTER_SUGGEST_ANDOR','yes');
1018 setConfigVar ('FILTER_SUGGEST_TAGS','yes');
1019 setConfigVar ('FILTER_SUGGEST_PREDICATES','yes');
1020 setConfigVar ('FILTER_SUGGEST_EXTRA','no');
49b605d9 1021 setConfigVar ('DEFAULT_SNMP_COMMUNITY','public');
f06fe423 1022 setConfigVar ('IPV4_ENABLE_KNIGHT','yes');
9e51318b
DO
1023 setConfigVar ('TAGS_TOPLIST_SIZE','50');
1024 setConfigVar ('TAGS_QUICKLIST_SIZE','20');
1025 setConfigVar ('TAGS_QUICKLIST_THRESHOLD','50');
f44fdef9 1026 setConfigVar ('ENABLE_MULTIPORT_FORM', 'no');
2400d7ec
DO
1027 setConfigVar ('DEFAULT_PORT_IIF_ID', '1');
1028 setConfigVar ('DEFAULT_PORT_OIF_IDS', '1=24; 3=1078; 4=1077; 5=1079; 6=1080; 8=1082; 9=1084');
135080d8 1029 return buildRedirectURL (__FUNCTION__, 'OK');
b07f617c
DO
1030}
1031
b6a7d936
DO
1032$msgcode['addRealServer']['OK'] = 34;
1033$msgcode['addRealServer']['ERR'] = 126;
732e4578 1034// Add single record.
ca461127
DO
1035function addRealServer ()
1036{
2c6c7645
DO
1037 assertUIntArg ('pool_id', __FUNCTION__);
1038 assertIPv4Arg ('remoteip', __FUNCTION__);
79a9edb4 1039 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 1040 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1041 if (!addRStoRSPool (
1042 $_REQUEST['pool_id'],
1043 $_REQUEST['remoteip'],
1044 $_REQUEST['rsport'],
1045 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1046 $_REQUEST['rsconfig']
1047 ))
135080d8 1048 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1049 else
135080d8 1050 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1051}
1052
b6a7d936
DO
1053$msgcode['addRealServers']['OK'] = 37;
1054$msgcode['addRealServers']['ERR1'] = 131;
1055$msgcode['addRealServers']['ERR2'] = 127;
732e4578
DO
1056// Parse textarea submitted and try adding a real server for each line.
1057function addRealServers ()
1058{
cbadbb38 1059 assertUIntArg ('pool_id', __FUNCTION__);
2c6c7645
DO
1060 assertStringArg ('format', __FUNCTION__);
1061 assertStringArg ('rawtext', __FUNCTION__);
732e4578
DO
1062 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1063 $ngood = $nbad = 0;
1064 $rsconfig = '';
1065 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1066 foreach (explode ('\n', $rawtext) as $line)
1067 {
59a83bd8 1068 if (!strlen ($line))
732e4578
DO
1069 continue;
1070 $match = array ();
1071 switch ($_REQUEST['format'])
1072 {
1073 case 'ipvs_2': // address and port only
1074 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1075 continue;
103b1e1e 1076 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
732e4578
DO
1077 $ngood++;
1078 else
1079 $nbad++;
1080 break;
1081 case 'ipvs_3': // address, port and weight
1082 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1083 continue;
103b1e1e 1084 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
732e4578
DO
1085 $ngood++;
1086 else
1087 $nbad++;
1088 break;
e69c2aa3
DO
1089 case 'ssv_2': // IP address and port
1090 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1091 continue;
103b1e1e 1092 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
e69c2aa3
DO
1093 $ngood++;
1094 else
1095 $nbad++;
1096 break;
948c37e3
DO
1097 case 'ssv_1': // IP address
1098 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1099 continue;
1100 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1101 $ngood++;
1102 else
1103 $nbad++;
1104 break;
732e4578 1105 default:
135080d8 1106 return buildRedirectURL (__FUNCTION__, 'ERR1');
732e4578
DO
1107 break;
1108 }
1109 }
1110 if ($nbad == 0 and $ngood > 0)
135080d8 1111 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
732e4578 1112 else
135080d8 1113 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
732e4578
DO
1114}
1115
b6a7d936
DO
1116$msgcode['addVService']['OK'] = 28;
1117$msgcode['addVService']['ERR1'] = 132;
1118$msgcode['addVService']['ERR2'] = 100;
d6517a21
DO
1119function addVService ()
1120{
2c6c7645
DO
1121 assertIPv4Arg ('vip', __FUNCTION__);
1122 assertUIntArg ('vport', __FUNCTION__);
1123 assertStringArg ('proto', __FUNCTION__);
103b1e1e 1124 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
135080d8 1125 return buildRedirectURL (__FUNCTION__, 'ERR1');
2c6c7645
DO
1126 assertStringArg ('name', __FUNCTION__, TRUE);
1127 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1128 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1129 $error = commitCreateVS
1130 (
103b1e1e
DO
1131 $_REQUEST['vip'],
1132 $_REQUEST['vport'],
7320b661 1133 $_REQUEST['proto'],
103b1e1e
DO
1134 $_REQUEST['name'],
1135 $_REQUEST['vsconfig'],
c63a8d6e
DO
1136 $_REQUEST['rsconfig'],
1137 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1138 );
1139 if ($error != '')
135080d8 1140 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
ca461127 1141 else
135080d8 1142 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1143}
1144
b6a7d936
DO
1145$msgcode['deleteRealServer']['OK'] = 35;
1146$msgcode['deleteRealServer']['ERR'] = 128;
fb1c4a54
DO
1147function deleteRealServer ()
1148{
2c6c7645 1149 assertUIntArg ('id', __FUNCTION__);
d6517a21 1150 if (!commitDeleteRS ($_REQUEST['id']))
135080d8 1151 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1152 else
135080d8 1153 return buildRedirectURL (__FUNCTION__, 'OK');
fb1c4a54
DO
1154}
1155
b6a7d936
DO
1156$msgcode['deleteLoadBalancer']['OK'] = 19;
1157$msgcode['deleteLoadBalancer']['ERR'] = 129;
3241551e
DO
1158function deleteLoadBalancer ()
1159{
2c6c7645
DO
1160 assertUIntArg ('object_id', __FUNCTION__);
1161 assertUIntArg ('pool_id', __FUNCTION__);
1162 assertUIntArg ('vs_id', __FUNCTION__);
103b1e1e
DO
1163 if (!commitDeleteLB (
1164 $_REQUEST['object_id'],
1165 $_REQUEST['pool_id'],
1166 $_REQUEST['vs_id']
1167 ))
135080d8 1168 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1169 else
135080d8 1170 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1171}
1172
b6a7d936
DO
1173$msgcode['deleteVService']['OK'] = 29;
1174$msgcode['deleteVService']['ERR'] = 130;
d6517a21
DO
1175function deleteVService ()
1176{
e02e1941
DO
1177 assertUIntArg ('vs_id', __FUNCTION__);
1178 if (!commitDeleteVS ($_REQUEST['vs_id']))
135080d8 1179 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1180 else
135080d8 1181 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1182}
1183
b6a7d936
DO
1184$msgcode['updateRealServer']['OK'] = 36;
1185$msgcode['updateRealServer']['ERR'] = 133;
fb1c4a54
DO
1186function updateRealServer ()
1187{
2c6c7645
DO
1188 assertUIntArg ('rs_id', __FUNCTION__);
1189 assertIPv4Arg ('rsip', __FUNCTION__);
79a9edb4 1190 assertStringArg ('rsport', __FUNCTION__, TRUE);
2c6c7645 1191 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1192 if (!commitUpdateRS (
1193 $_REQUEST['rs_id'],
1194 $_REQUEST['rsip'],
1195 $_REQUEST['rsport'],
1196 $_REQUEST['rsconfig']
1197 ))
135080d8 1198 return buildRedirectURL (__FUNCTION__, 'ERR');
fb1c4a54 1199 else
135080d8 1200 return buildRedirectURL (__FUNCTION__, 'OK');
ca461127
DO
1201}
1202
b6a7d936
DO
1203$msgcode['updateLoadBalancer']['OK'] = 20;
1204$msgcode['updateLoadBalancer']['ERR'] = 134;
c1ca768c 1205function updateLoadBalancer ()
3241551e 1206{
2c6c7645
DO
1207 assertUIntArg ('object_id', __FUNCTION__);
1208 assertUIntArg ('pool_id', __FUNCTION__);
1209 assertUIntArg ('vs_id', __FUNCTION__);
1210 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1211 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1212 if (!commitUpdateLB (
1213 $_REQUEST['object_id'],
1214 $_REQUEST['pool_id'],
1215 $_REQUEST['vs_id'],
1216 $_REQUEST['vsconfig'],
1217 $_REQUEST['rsconfig']
1218 ))
135080d8 1219 return buildRedirectURL (__FUNCTION__, 'ERR');
d6517a21 1220 else
135080d8 1221 return buildRedirectURL (__FUNCTION__, 'OK');
d6517a21
DO
1222}
1223
b6a7d936
DO
1224$msgcode['updateVService']['OK'] = 30;
1225$msgcode['updateVService']['ERR'] = 135;
d6517a21
DO
1226function updateVService ()
1227{
e02e1941 1228 assertUIntArg ('vs_id', __FUNCTION__);
2c6c7645
DO
1229 assertIPv4Arg ('vip', __FUNCTION__);
1230 assertUIntArg ('vport', __FUNCTION__);
1231 assertStringArg ('proto', __FUNCTION__);
1232 assertStringArg ('name', __FUNCTION__, TRUE);
1233 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1234 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1235 if (!commitUpdateVS (
1236 $_REQUEST['vs_id'],
1237 $_REQUEST['vip'],
1238 $_REQUEST['vport'],
1239 $_REQUEST['proto'],
1240 $_REQUEST['name'],
1241 $_REQUEST['vsconfig'],
1242 $_REQUEST['rsconfig']
1243 ))
135080d8 1244 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1245 else
135080d8 1246 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1247}
1248
b6a7d936
DO
1249$msgcode['addLoadBalancer']['OK'] = 18;
1250$msgcode['addLoadBalancer']['ERR'] = 137;
3241551e
DO
1251function addLoadBalancer ()
1252{
2c6c7645
DO
1253 assertUIntArg ('pool_id', __FUNCTION__);
1254 assertUIntArg ('object_id', __FUNCTION__);
1255 assertUIntArg ('vs_id', __FUNCTION__);
1256 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1257 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
103b1e1e
DO
1258 if (!addLBtoRSPool (
1259 $_REQUEST['pool_id'],
1260 $_REQUEST['object_id'],
1261 $_REQUEST['vs_id'],
1262 $_REQUEST['vsconfig'],
1263 $_REQUEST['rsconfig']
1264 ))
135080d8 1265 return buildRedirectURL (__FUNCTION__, 'ERR');
3241551e 1266 else
135080d8 1267 return buildRedirectURL (__FUNCTION__, 'OK');
3241551e
DO
1268}
1269
b6a7d936
DO
1270$msgcode['addRSPool']['OK'] = 31;
1271$msgcode['addRSPool']['ERR'] = 100;
5ad76f01
DO
1272function addRSPool ()
1273{
2c6c7645
DO
1274 assertStringArg ('name', __FUNCTION__, TRUE);
1275 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1276 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
c63a8d6e
DO
1277 $error = commitCreateRSPool
1278 (
103b1e1e
DO
1279 $_REQUEST['name'],
1280 $_REQUEST['vsconfig'],
c63a8d6e
DO
1281 $_REQUEST['rsconfig'],
1282 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1283 );
1284 if ($error != '')
135080d8 1285 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
5ad76f01 1286 else
135080d8 1287 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1288}
1289
b6a7d936
DO
1290$msgcode['deleteRSPool']['OK'] = 32;
1291$msgcode['deleteRSPool']['ERR'] = 138;
5ad76f01
DO
1292function deleteRSPool ()
1293{
2c6c7645 1294 assertUIntArg ('pool_id', __FUNCTION__);
841a7a7a 1295 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
135080d8 1296 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1297 else
135080d8 1298 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1299}
1300
b6a7d936
DO
1301$msgcode['updateRSPool']['OK'] = 33;
1302$msgcode['updateRSPool']['ERR'] = 139;
5ad76f01
DO
1303function updateRSPool ()
1304{
2c6c7645
DO
1305 assertUIntArg ('pool_id', __FUNCTION__);
1306 assertStringArg ('name', __FUNCTION__, TRUE);
1307 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1308 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
841a7a7a 1309 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
135080d8 1310 return buildRedirectURL (__FUNCTION__, 'ERR');
5ad76f01 1311 else
135080d8 1312 return buildRedirectURL (__FUNCTION__, 'OK');
5ad76f01
DO
1313}
1314
b6a7d936
DO
1315$msgcode['updateRSInService']['OK'] = 38;
1316$msgcode['updateRSInService']['ERR'] = 140;
1f7d18fa
DO
1317function updateRSInService ()
1318{
2c6c7645 1319 assertUIntArg ('rscount', __FUNCTION__);
841a7a7a 1320 $pool_id = $_REQUEST['pool_id'];
a6e91ac2
DO
1321 $orig = spotEntity ('ipv4rspool', $pool_id);
1322 amplifyCell ($orig);
1f7d18fa
DO
1323 $nbad = $ngood = 0;
1324 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1325 {
1326 $rs_id = $_REQUEST["rsid_${i}"];
1327 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1328 $newval = 'yes';
1329 else
1330 $newval = 'no';
1331 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1332 {
1333 if (commitSetInService ($rs_id, $newval))
1334 $ngood++;
1335 else
1336 $nbad++;
1337 }
1338 }
1339 if (!$nbad)
135080d8 1340 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1f7d18fa 1341 else
135080d8 1342 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1f7d18fa
DO
1343}
1344
b6a7d936
DO
1345$msgcode['importPTRData']['OK'] = 26;
1346$msgcode['importPTRData']['ERR'] = 141;
8d9c16e7
DO
1347// FIXME: check, that each submitted address belongs to the prefix we
1348// are operating on.
03eb5209
DO
1349function importPTRData ()
1350{
2c6c7645 1351 assertUIntArg ('addrcount', __FUNCTION__);
03eb5209 1352 $nbad = $ngood = 0;
03eb5209
DO
1353 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1354 {
3f3bd41e 1355 $inputname = "import_${i}";
03eb5209
DO
1356 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1357 continue;
2c6c7645
DO
1358 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1359 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1360 assertStringArg ("rsvd_${i}", __FUNCTION__);
3f3bd41e
DO
1361 // Non-existent addresses will not have this argument set in request.
1362 $rsvd = 'no';
03eb5209
DO
1363 if ($_REQUEST["rsvd_${i}"] == 'yes')
1364 $rsvd = 'yes';
3f3bd41e 1365 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
03eb5209
DO
1366 $ngood++;
1367 else
1368 $nbad++;
1369 }
1370 if (!$nbad)
135080d8 1371 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
03eb5209 1372 else
135080d8 1373 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
03eb5209
DO
1374}
1375
b6a7d936
DO
1376$msgcode['generateAutoPorts']['OK'] = 21;
1377$msgcode['generateAutoPorts']['ERR'] = 142;
f3f0161f
DO
1378function generateAutoPorts ()
1379{
103b1e1e 1380 global $pageno;
2c6c7645 1381 assertUIntArg ('object_id', __FUNCTION__);
6297d584 1382 $info = spotEntity ('object', $_REQUEST['object_id']);
f3f0161f 1383 // Navigate away in case of success, stay at the place otherwise.
103b1e1e 1384 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
135080d8 1385 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
f3f0161f 1386 else
135080d8 1387 return buildRedirectURL (__FUNCTION__, 'ERR');
f3f0161f
DO
1388}
1389
b6a7d936
DO
1390$msgcode['saveEntityTags']['OK'] = 22;
1391$msgcode['saveEntityTags']['ERR1'] = 143;
1392$msgcode['saveEntityTags']['ERR2'] = 187;
24cbe8af 1393// Filter out implicit tags before storing the new tag set.
3355ca56 1394function saveEntityTags ()
24cbe8af 1395{
eb27afad 1396 global $page, $pageno, $etype_by_pageno;
be2ae2a2 1397 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
9a61c175 1398 return buildRedirectURL (__FUNCTION__, 'ERR2', array (__FUNCTION__));
be2ae2a2 1399 $realm = $etype_by_pageno[$pageno];
3355ca56 1400 $bypass = $page[$pageno]['bypass'];
2c6c7645 1401 assertUIntArg ($bypass, __FUNCTION__);
2034d968 1402 $entity_id = $_REQUEST[$bypass];
e04931c8 1403 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
6e49bd1f 1404 // Build a chain from the submitted data, minimize it,
ab379543 1405 // then wipe existing records and store the new set instead.
01b6b4d6 1406 destroyTagsForEntity ($realm, $entity_id);
eb27afad
DO
1407 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1408 // so why not use it?
6e49bd1f 1409 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
ab379543 1410 $n_succeeds = $n_errors = 0;
6e49bd1f 1411 foreach ($newchain as $taginfo)
eb6ea26f 1412 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
ab379543
DO
1413 $n_succeeds++;
1414 else
1415 $n_errors++;
ab379543 1416 if ($n_errors)
9a61c175 1417 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
ab379543 1418 else
135080d8 1419 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
2034d968
DO
1420}
1421
b6a7d936
DO
1422$msgcode['destroyTag']['OK'] = 58;
1423$msgcode['destroyTag']['ERR1'] = 183;
1424$msgcode['destroyTag']['ERR2'] = 144;
fe7044ad
DO
1425function destroyTag ()
1426{
49fb1027 1427 assertUIntArg ('tag_id', __FUNCTION__);
c615a655
DO
1428 global $taglist;
1429 if (!isset ($taglist[$_REQUEST['tag_id']]))
1430 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['tag_id']));
49fb1027 1431 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
c615a655 1432 return buildRedirectURL (__FUNCTION__, 'OK', array ($taglist[$_REQUEST['tag_id']]['tag']));
fe7044ad 1433 else
c615a655 1434 return buildRedirectURL (__FUNCTION__, 'ERR2');
fe7044ad
DO
1435}
1436
b6a7d936
DO
1437$msgcode['createTag']['OK'] = 59;
1438$msgcode['createTag']['ERR1'] = 145;
1439$msgcode['createTag']['ERR3'] = 147;
fe7044ad
DO
1440function createTag ()
1441{
49fb1027 1442 assertStringArg ('tag_name', __FUNCTION__);
2c6c7645 1443 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
49fb1027 1444 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1445 if (!validTagName ($tagname))
135080d8 1446 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
fe7044ad
DO
1447 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1448 $parent_id = 'NULL';
1449 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
135080d8 1450 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
fe7044ad 1451 else
135080d8 1452 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
fe7044ad
DO
1453}
1454
b6a7d936
DO
1455$msgcode['updateTag']['OK'] = 60;
1456$msgcode['updateTag']['ERR1'] = 145;
1457$msgcode['updateTag']['ERR2'] = 148;
49fb1027
DO
1458function updateTag ()
1459{
49fb1027
DO
1460 assertUIntArg ('tag_id', __FUNCTION__);
1461 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1462 assertStringArg ('tag_name', __FUNCTION__);
1463 $tagname = trim ($_REQUEST['tag_name']);
2eeeca80 1464 if (!validTagName ($tagname))
135080d8 1465 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
49fb1027
DO
1466 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1467 $parent_id = 'NULL';
1468 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
135080d8 1469 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
590e1281
DO
1470 // Use old name in the message, cause update failed.
1471 global $taglist;
1472 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag'], $ret));
49fb1027
DO
1473}
1474
b6a7d936
DO
1475$msgcode['rollTags']['OK'] = 67;
1476$msgcode['rollTags']['ERR'] = 149;
eb6ea26f
DO
1477function rollTags ()
1478{
eb6ea26f
DO
1479 assertUIntArg ('row_id', __FUNCTION__);
1480 assertStringArg ('sum', __FUNCTION__, TRUE);
1481 assertUIntArg ('realsum', __FUNCTION__);
eb6ea26f 1482 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
135080d8 1483 return buildRedirectURL (__FUNCTION__, 'ERR');
abef7149
DO
1484 // Even if the user requested an empty tag list, don't bail out, but process existing
1485 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1486 // have its chains refined to "normal" form.
1487 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1488 $n_ok = 0;
1489 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1490 // filter out the same tag again and again. It will have own noise to cancel.
1491 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
61a1d996 1492 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
abef7149 1493 {
61a1d996 1494 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
abef7149 1495 $n_ok++;
61a1d996
DO
1496 amplifyCell ($rack);
1497 foreach ($rack['mountedObjects'] as $object_id)
abef7149
DO
1498 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1499 $n_ok++;
1500 }
135080d8 1501 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
eb6ea26f
DO
1502}
1503
b6a7d936
DO
1504$msgcode['changeMyPassword']['OK'] = 61;
1505$msgcode['changeMyPassword']['ERR1'] = 150;
1506$msgcode['changeMyPassword']['ERR2'] = 151;
1507$msgcode['changeMyPassword']['ERR3'] = 152;
1508$msgcode['changeMyPassword']['ERR4'] = 153;
9457ca59 1509function changeMyPassword ()
cced6b7d 1510{
b82cce3f 1511 global $remote_username, $user_auth_src;
204284ba 1512 if ($user_auth_src != 'database')
135080d8 1513 return buildRedirectURL (__FUNCTION__, 'ERR1');
0ac7e4b3
DO
1514 assertStringArg ('oldpassword', __FUNCTION__);
1515 assertStringArg ('newpassword1', __FUNCTION__);
1516 assertStringArg ('newpassword2', __FUNCTION__);
b82cce3f 1517 $remote_userid = getUserIDByUsername ($remote_username);
0b2c74cb 1518 $userinfo = spotEntity ('user', $remote_userid);
b82cce3f 1519 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
135080d8 1520 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d 1521 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
135080d8 1522 return buildRedirectURL (__FUNCTION__, 'ERR3');
b82cce3f 1523 if (commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
135080d8 1524 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1525 else
135080d8 1526 return buildRedirectURL (__FUNCTION__, 'ERR4');
cced6b7d
DO
1527}
1528
b6a7d936
DO
1529$msgcode['saveRackCode']['OK'] = 43;
1530$msgcode['saveRackCode']['ERR1'] = 154;
1531$msgcode['saveRackCode']['ERR2'] = 155;
cced6b7d
DO
1532function saveRackCode ()
1533{
cced6b7d 1534 assertStringArg ('rackcode');
e6a4adb9
DO
1535 // For the test to succeed, unescape LFs, strip CRs.
1536 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
cf25e649
DO
1537 $parseTree = getRackCode ($newcode);
1538 if ($parseTree['result'] != 'ACK')
135080d8 1539 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
4a6a28f1 1540 saveScript ('RackCodeCache', '');
cf25e649 1541 if (saveScript ('RackCode', $newcode))
135080d8 1542 return buildRedirectURL (__FUNCTION__, 'OK');
cced6b7d 1543 else
135080d8 1544 return buildRedirectURL (__FUNCTION__, 'ERR2');
cced6b7d
DO
1545}
1546
b6a7d936 1547$msgcode['setPortVLAN']['ERR1'] = 156;
46f92ff7
DO
1548// This handler's context is pre-built, but not authorized. It is assumed, that the
1549// handler will take existing context and before each commit check authorization
1550// on the base chain plus necessary context added.
1551function setPortVLAN ()
1552{
1553 assertUIntArg ('portcount', __FUNCTION__);
1554 $data = getSwitchVLANs ($_REQUEST['object_id']);
1555 if ($data === NULL)
135080d8 1556 return buildRedirectURL (__FUNCTION__, 'ERR1');
46f92ff7
DO
1557 list ($vlanlist, $portlist) = $data;
1558 // Here we just build up 1 set command for the gateway with all of the ports
1559 // included. The gateway is expected to filter unnecessary changes silently
1560 // and to provide a list of responses with either error or success message
1561 // for each of the rest.
1562 $nports = $_REQUEST['portcount'];
1563 $prefix = 'set ';
db55cf54 1564 $log = emptyLog();
46f92ff7
DO
1565 $setcmd = '';
1566 for ($i = 0; $i < $nports; $i++)
1567 if
1568 (
1569 !isset ($_REQUEST['portname_' . $i]) ||
1570 !isset ($_REQUEST['vlanid_' . $i]) ||
1571 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1572 )
24dcb9d8 1573 $log['m'][] = array ('c' => 158, 'a' => array ($i));
46f92ff7
DO
1574 elseif
1575 (
1576 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1577 $portlist[$i]['vlaind'] == 'TRUNK'
1578 )
1579 continue;
1580 else
1581 {
1582 $portname = $_REQUEST['portname_' . $i];
1583 $oldvlanid = $portlist[$i]['vlanid'];
1584 $newvlanid = $_REQUEST['vlanid_' . $i];
1585 // Finish the security context and evaluate it.
1586 $annex = array();
1587 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1588 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1589 if (!permitted (NULL, NULL, NULL, $annex))
1590 {
24dcb9d8 1591 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
46f92ff7
DO
1592 continue;
1593 }
1594 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1595 $prefix = ';';
1596 }
1597 // Feed the gateway and interpret its (non)response.
1598 if ($setcmd != '')
f0ff4930 1599 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
46f92ff7 1600 else
24dcb9d8 1601 $log['m'][] = array ('c' => 201);
46f92ff7
DO
1602 return buildWideRedirectURL ($log);
1603}
1604
2987fc1f
DO
1605function submitSLBConfig ()
1606{
1607 assertUIntArg ('object_id', __FUNCTION__);
1608 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
ff0df5c3 1609 $msglog = gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
2987fc1f
DO
1610 return buildWideRedirectURL ($msglog);
1611}
1612
b6a7d936
DO
1613$msgcode['addRow']['OK'] = 74;
1614$msgcode['addRow']['ERR'] = 100;
345fd640
AD
1615function addRow ()
1616{
1617 assertStringArg ('name', __FUNCTION__);
1618
10bac82a 1619 if (commitAddRow ($_REQUEST['name']) === TRUE)
135080d8 1620 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1621 else
135080d8 1622 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1623}
1624
b6a7d936
DO
1625$msgcode['updateRow']['OK'] = 75;
1626$msgcode['updateRow']['ERR'] = 100;
345fd640
AD
1627function updateRow ()
1628{
1629 assertUIntArg ('row_id', __FUNCTION__);
1630 assertStringArg ('name', __FUNCTION__);
1631
10bac82a 1632 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
135080d8 1633 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
345fd640 1634 else
135080d8 1635 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
345fd640
AD
1636}
1637
b6a7d936
DO
1638$msgcode['deleteRow']['OK'] = 77;
1639$msgcode['deleteRow']['ERR'] = 100;
9f14a7ef
DY
1640function deleteRow ()
1641{
1642 assertUIntArg ('row_id', __FUNCTION__);
7be51fea 1643 $rowinfo = getRackRowInfo ($_REQUEST['row_id']);
9f14a7ef
DY
1644
1645 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
7be51fea 1646 return buildRedirectURL (__FUNCTION__, 'OK', array ($rowinfo['name']));
9f14a7ef 1647 else
7be51fea 1648 return buildRedirectURL (__FUNCTION__, 'ERR', array ($rowinfo['name']));
9f14a7ef
DY
1649}
1650
b6a7d936
DO
1651$msgcode['addRack']['OK'] = 65;
1652$msgcode['addRack']['ERR1'] = 171;
1653$msgcode['addRack']['ERR2'] = 172;
f19c75d6
DO
1654function addRack ()
1655{
1656 assertUIntArg ('row_id', __FUNCTION__);
1657 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1658 if (isset ($_REQUEST['got_data']))
1659 {
1660 assertStringArg ('rack_name', __FUNCTION__);
1661 assertUIntArg ('rack_height1', __FUNCTION__);
1662 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1663
1664 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
135080d8 1665 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
f19c75d6 1666 else
135080d8 1667 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
f19c75d6
DO
1668 }
1669 elseif (isset ($_REQUEST['got_mdata']))
1670 {
1671 assertUIntArg ('rack_height2', __FUNCTION__);
1672 assertStringArg ('rack_names', __FUNCTION__, TRUE);
db55cf54 1673 $log = emptyLog();
f19c75d6
DO
1674 // copy-and-paste from renderAddMultipleObjectsForm()
1675 $names1 = explode ('\n', $_REQUEST['rack_names']);
1676 $names2 = array();
1677 foreach ($names1 as $line)
1678 {
1679 $parts = explode ('\r', $line);
1680 reset ($parts);
59a83bd8 1681 if (!strlen ($parts[0]))
f19c75d6
DO
1682 continue;
1683 else
1684 $names2[] = rtrim ($parts[0]);
1685 }
b3029d5c 1686 global $msgcode;
f19c75d6
DO
1687 foreach ($names2 as $cname)
1688 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
b3029d5c 1689 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
f19c75d6 1690 else
b3029d5c 1691 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
f19c75d6
DO
1692 return buildWideRedirectURL ($log);
1693 }
1694 else
135080d8 1695 return buildRedirectURL (__FUNCTION__, 'ERR2');
f19c75d6
DO
1696}
1697
b6a7d936
DO
1698$msgcode['deleteRack']['OK'] = 79;
1699$msgcode['deleteRack']['ERR'] = 100;
1700$msgcode['deleteRack']['ERR1'] = 206;
c8187437
DY
1701function deleteRack ()
1702{
1703 assertUIntArg ('rack_id', __FUNCTION__);
61a1d996
DO
1704 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1705 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1706 amplifyCell ($rackData);
1707 if (count ($rackData['mountedObjects']))
c8187437 1708 return buildRedirectURL (__FUNCTION__, 'ERR1');
61a1d996
DO
1709 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1710 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1711 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
c8187437
DY
1712}
1713
b6a7d936
DO
1714$msgcode['updateRack']['OK'] = 68;
1715$msgcode['updateRack']['ERR'] = 177;
7056988c
DO
1716function updateRack ()
1717{
7056988c
DO
1718 assertUIntArg ('rack_id', __FUNCTION__);
1719 assertUIntArg ('rack_row_id', __FUNCTION__);
1720 assertUIntArg ('rack_height', __FUNCTION__);
1721 assertStringArg ('rack_name', __FUNCTION__);
1722 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
7056988c 1723
7056988c 1724 resetThumbCache ($_REQUEST['rack_id']);
0c2b7c4a 1725 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
135080d8 1726 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
0c2b7c4a 1727 else
135080d8 1728 return buildRedirectURL (__FUNCTION__, 'ERR');
7056988c
DO
1729}
1730
61a1d996 1731$msgcode['updateRackDesign']['ERR'] = 100;
4fbb5a00
DY
1732function updateRackDesign ()
1733{
1734 assertUIntArg ('rack_id', __FUNCTION__);
61a1d996
DO
1735 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1736 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1737 amplifyCell ($rackData);
4fbb5a00
DY
1738 applyRackDesignMask($rackData);
1739 markupObjectProblems ($rackData);
1740 $response = processGridForm ($rackData, 'A', 'F');
51690ad4 1741 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1742}
1743
61a1d996 1744$msgcode['updateRackProblems']['ERR'] = 100;
4fbb5a00
DY
1745function updateRackProblems ()
1746{
1747 assertUIntArg ('rack_id', __FUNCTION__);
61a1d996
DO
1748 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1749 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1750 amplifyCell ($rackData);
4fbb5a00
DY
1751 applyRackProblemMask($rackData);
1752 markupObjectProblems ($rackData);
1753 $response = processGridForm ($rackData, 'F', 'U');
51690ad4 1754 return buildWideRedirectURL (array($response));
4fbb5a00
DY
1755}
1756
7056988c
DO
1757function querySNMPData ()
1758{
1759 assertUIntArg ('object_id', __FUNCTION__);
1760 assertStringArg ('community', __FUNCTION__);
83fdf85f 1761 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
7056988c
DO
1762}
1763
b6a7d936
DO
1764$msgcode['addFileWithoutLink']['OK'] = 69;
1765$msgcode['addFileWithoutLink']['ERR'] = 100;
e1ae3fb4
AD
1766// File-related functions
1767function addFileWithoutLink ()
1768{
1769 assertStringArg ('comment', __FUNCTION__, TRUE);
1770
1771 // Make sure the file can be uploaded
1772 if (get_cfg_var('file_uploads') != 1)
135080d8 1773 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
e1ae3fb4
AD
1774
1775 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1776 global $sic;
1777 // commitAddFile() uses prepared statements
1778 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
f857f71f
DO
1779 if (isset ($_REQUEST['taglist']))
1780 produceTagsForLastRecord ('file', $_REQUEST['taglist']);
e1ae3fb4
AD
1781
1782 if ($error != '')
135080d8 1783 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1784
135080d8 1785 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1786}
1787
b6a7d936
DO
1788$msgcode['addFileToEntity']['OK'] = 69;
1789$msgcode['addFileToEntity']['ERR1'] = 187;
1790$msgcode['addFileToEntity']['ERR2'] = 181;
1791$msgcode['addFileToEntity']['ERR3'] = 182;
e1ae3fb4
AD
1792function addFileToEntity ()
1793{
9a61c175
DO
1794 global $page, $pageno, $etype_by_pageno;
1795 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1796 return buildRedirectURL (__FUNCTION__, 'ERR1', array (__FUNCTION__));
1797 $realm = $etype_by_pageno[$pageno];
1798 $bypass = $page[$pageno]['bypass'];
1799 assertUIntArg ($bypass, __FUNCTION__);
1800 $entity_id = $_REQUEST[$bypass];
e1ae3fb4 1801 assertStringArg ('comment', __FUNCTION__, TRUE);
e1ae3fb4
AD
1802
1803 // Make sure the file can be uploaded
1804 if (get_cfg_var('file_uploads') != 1)
9a61c175 1805 return buildRedirectURL (__FUNCTION__, 'ERR2');
e1ae3fb4
AD
1806
1807 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
4bb95650
DO
1808 global $sic;
1809 // commitAddFile() uses prepared statements
1810 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
e1ae3fb4 1811 if ($error != '')
9a61c175 1812 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1813
9a61c175 1814 $error = commitLinkFile (lastInsertID(), $realm, $entity_id);
e1ae3fb4 1815 if ($error != '')
9a61c175 1816 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
e1ae3fb4 1817
135080d8 1818 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
e1ae3fb4
AD
1819}
1820
b6a7d936
DO
1821$msgcode['linkFileToEntity']['OK'] = 71;
1822$msgcode['linkFileToEntity']['ERR1'] = 178;
1823$msgcode['linkFileToEntity']['ERR2'] = 100;
e1ae3fb4
AD
1824function linkFileToEntity ()
1825{
e1ae3fb4 1826 assertUIntArg ('file_id', __FUNCTION__);
482c7f35 1827 global $page, $pageno, $etype_by_pageno;
121496b6
DO
1828 $entity_type = $etype_by_pageno[$pageno];
1829 $bypass_name = $page[$pageno]['bypass'];
1830 assertUIntArg ($bypass_name, __FUNCTION__);
1831
d3b5008b 1832 $fi = spotEntity ('file', $_REQUEST['file_id']);
121496b6 1833 if ($fi === NULL)
135080d8 1834 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
121496b6 1835 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
e1ae3fb4 1836 if ($error != '')
135080d8 1837 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
e1ae3fb4 1838
135080d8 1839 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
e1ae3fb4
AD
1840}
1841
b6a7d936
DO
1842$msgcode['replaceFile']['OK'] = 70;
1843$msgcode['replaceFile']['ERR1'] = 181;
1844$msgcode['replaceFile']['ERR2'] = 207;
1845$msgcode['replaceFile']['ERR3'] = 182;
fd1fb420
AD
1846function replaceFile ()
1847{
db55cf54 1848 global $sic;
fd1fb420
AD
1849 assertUIntArg ('file_id', __FUNCTION__);
1850
1851 // Make sure the file can be uploaded
1852 if (get_cfg_var('file_uploads') != 1)
db55cf54 1853 return buildRedirectURL (__FUNCTION__, 'ERR1');
d3b5008b 1854 $shortInfo = spotEntity ('file', $sic['file_id']);
fd1fb420
AD
1855
1856 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
db55cf54
DO
1857 if ($fp === FALSE)
1858 return buildRedirectURL (__FUNCTION__, 'ERR2');
f8874cdb 1859 $error = commitReplaceFile ($sic['file_id'], $fp);
fd1fb420 1860 if ($error != '')
db55cf54 1861 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
fd1fb420 1862
db55cf54 1863 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
fd1fb420
AD
1864}
1865
b6a7d936
DO
1866$msgcode['updateFile']['OK'] = 70;
1867$msgcode['updateFile']['ERR'] = 100;
e1ae3fb4
AD
1868function updateFile ()
1869{
1870 assertUIntArg ('file_id', __FUNCTION__);
9db67d95
DO
1871 assertStringArg ('file_name', __FUNCTION__);
1872 assertStringArg ('file_type', __FUNCTION__);
1873 assertStringArg ('file_comment', __FUNCTION__, TRUE);
f3d274bf
DO
1874 // prepared statement params below
1875 global $sic;
1876 $error = commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']);
e1ae3fb4 1877 if ($error != '')
135080d8 1878 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1879
135080d8 1880 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
e1ae3fb4
AD
1881}
1882
b6a7d936
DO
1883$msgcode['unlinkFile']['OK'] = 72;
1884$msgcode['unlinkFile']['ERR'] = 182;
e1ae3fb4
AD
1885function unlinkFile ()
1886{
1887 assertUIntArg ('link_id', __FUNCTION__);
1888 $error = commitUnlinkFile ($_REQUEST['link_id']);
1889
1890 if ($error != '')
135080d8 1891 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1892
8bc5d1e4 1893 return buildRedirectURL (__FUNCTION__, 'OK');
e1ae3fb4
AD
1894}
1895
b6a7d936
DO
1896$msgcode['deleteFile']['OK'] = 73;
1897$msgcode['deleteFile']['ERR'] = 100;
e1ae3fb4
AD
1898function deleteFile ()
1899{
1900 assertUIntArg ('file_id', __FUNCTION__);
d3b5008b 1901 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
e1ae3fb4
AD
1902 $error = commitDeleteFile ($_REQUEST['file_id']);
1903
1904 if ($error != '')
135080d8 1905 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
e1ae3fb4 1906
f8874cdb 1907 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
e1ae3fb4
AD
1908}
1909
b6a7d936
DO
1910$msgcode['updateFileText']['OK'] = 78;
1911$msgcode['updateFileText']['ERR1'] = 179;
1912$msgcode['updateFileText']['ERR2'] = 180;
8628ae44
DO
1913function updateFileText ()
1914{
1915 assertUIntArg ('file_id', __FUNCTION__);
70cb9b56 1916 assertStringArg ('mtime_copy', __FUNCTION__);
8628ae44 1917 assertStringArg ('file_text', __FUNCTION__, TRUE); // it's Ok to save empty
d3b5008b 1918 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
70cb9b56
DO
1919 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1920 return buildRedirectURL (__FUNCTION__, 'ERR1');
f8874cdb
DO
1921 global $sic;
1922 $error = commitReplaceFile ($sic['file_id'], $sic['file_text']);
8628ae44 1923 if ($error == '')
f8874cdb 1924 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
70cb9b56 1925 return buildRedirectURL (__FUNCTION__, 'ERR2');
8628ae44
DO
1926}
1927
2dfa1b73
DO
1928$msgcode['addPortInterfaceCompat']['OK'] = 48;
1929$msgcode['addPortInterfaceCompat']['ERR'] = 110;
1930function addPortInterfaceCompat ()
1931{
1932 assertUIntArg ('iif_id', __FUNCTION__);
1933 assertUIntArg ('oif_id', __FUNCTION__);
1934 if (commitSupplementPIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
1935 return buildRedirectURL (__FUNCTION__, 'OK');
1936 return buildRedirectURL (__FUNCTION__, 'ERR');
1937}
1938
1939$msgcode['delPortInterfaceCompat']['OK'] = 49;
1940$msgcode['delPortInterfaceCompat']['ERR'] = 111;
1941function delPortInterfaceCompat ()
1942{
1943 assertUIntArg ('iif_id', __FUNCTION__);
1944 assertUIntArg ('oif_id', __FUNCTION__);
1945 if (commitReducePIC ($_REQUEST['iif_id'], $_REQUEST['oif_id']))
1946 return buildRedirectURL (__FUNCTION__, 'OK');
1947 return buildRedirectURL (__FUNCTION__, 'ERR');
1948}
1949
2400d7ec
DO
1950$ifcompatpack = array
1951(
1952 '1000cwdm80' => array (1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216),
1953 '1000dwdm80' => array // ITU channels 20~61
1954 (
1955 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226,
1956 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236,
1957 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246,
1958 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256,
1959 1257, 1258
1960 ),
1961 '10000dwdm80' => array // same channels for 10GE
1962 (
1963 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268,
1964 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278,
1965 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288,
1966 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1298,
1967 1299, 1300
1968 ),
1969);
1970
3d9ac3d4
DO
1971$msgcode['addPortInterfaceCompatPack']['OK'] = 44;
1972$msgcode['addPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
1973function addPortInterfaceCompatPack ()
1974{
3d9ac3d4
DO
1975 assertStringArg ('standard', __FUNCTION__);
1976 assertUIntArg ('iif_id', __FUNCTION__);
1977 global $ifcompatpack;
1978 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
1979 return buildRedirectURL (__FUNCTION__, 'ERR');
1980 $ngood = $nbad = 0;
1981 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
1982 if (commitSupplementPIC ($_REQUEST['iif_id'], $oif_id))
1983 $ngood++;
1984 else
1985 $nbad++;
1986 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
1987}
1988
3d9ac3d4
DO
1989$msgcode['delPortInterfaceCompatPack']['OK'] = 44;
1990$msgcode['delPortInterfaceCompatPack']['ERR'] = 123;
2400d7ec
DO
1991function delPortInterfaceCompatPack ()
1992{
3d9ac3d4
DO
1993 assertStringArg ('standard', __FUNCTION__);
1994 assertUIntArg ('iif_id', __FUNCTION__);
1995 global $ifcompatpack;
1996 if (!array_key_exists ($_REQUEST['standard'], $ifcompatpack) or !array_key_exists ($_REQUEST['iif_id'], getPortIIFOptions()))
1997 return buildRedirectURL (__FUNCTION__, 'ERR');
1998 $ngood = $nbad = 0;
1999 foreach ($ifcompatpack[$_REQUEST['standard']] as $oif_id)
2000 if (commitReducePIC ($_REQUEST['iif_id'], $oif_id))
2001 $ngood++;
2002 else
2003 $nbad++;
2004 return buildRedirectURL (__FUNCTION__, 'OK', array ($nbad, $ngood));
2400d7ec
DO
2005}
2006
e673ee24 2007?>