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