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