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