7f13eda20672412a07092af06eda039fd5989292
[racktables] / inc / ophandlers.php
1 <?php
2 /*
3 *
4 * This file is a library of operation handlers for RackTables.
5 *
6 */
7
8 $msgcode = array();
9
10 function buildWideRedirectURL ($log, $nextpage = NULL, $nexttab = NULL, $moreArgs = array())
11 {
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']];
20
21 if (count($moreArgs)>0)
22 {
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 }
35 }
36
37 $_SESSION['log'] = $log;
38 return $url;
39 }
40
41 function buildRedirectURL ($callfunc, $status, $args = array(), $nextpage = NULL, $nexttab = NULL)
42 {
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);
49 }
50
51 $msgcode['addPortForwarding']['OK'] = 2;
52 $msgcode['addPortForwarding']['ERR'] = 100;
53 function addPortForwarding ()
54 {
55 assertUIntArg ('object_id', __FUNCTION__);
56 assertIPv4Arg ('localip', __FUNCTION__);
57 assertIPv4Arg ('remoteip', __FUNCTION__);
58 assertUIntArg ('localport', __FUNCTION__);
59 assertStringArg ('proto', __FUNCTION__);
60 assertStringArg ('description', __FUNCTION__, TRUE);
61 $remoteport = isset ($_REQUEST['remoteport']) ? $_REQUEST['remoteport'] : '';
62 if (!strlen ($remoteport))
63 $remoteport = $_REQUEST['localport'];
64
65 $error = newPortForwarding
66 (
67 $_REQUEST['object_id'],
68 $_REQUEST['localip'],
69 $_REQUEST['localport'],
70 $_REQUEST['remoteip'],
71 $remoteport,
72 $_REQUEST['proto'],
73 $_REQUEST['description']
74 );
75
76 if ($error == '')
77 return buildRedirectURL (__FUNCTION__, 'OK');
78 else
79 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
80 }
81
82 $msgcode['delPortForwarding']['OK'] = 3;
83 $msgcode['delPortForwarding']['ERR'] = 100;
84 function delPortForwarding ()
85 {
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__);
92
93 $error = deletePortForwarding
94 (
95 $_REQUEST['object_id'],
96 $_REQUEST['localip'],
97 $_REQUEST['localport'],
98 $_REQUEST['remoteip'],
99 $_REQUEST['remoteport'],
100 $_REQUEST['proto']
101 );
102 if ($error == '')
103 return buildRedirectURL (__FUNCTION__, 'OK');
104 else
105 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
106 }
107
108 $msgcode['updPortForwarding']['OK'] = 4;
109 $msgcode['updPortForwarding']['ERR'] = 100;
110 function updPortForwarding ()
111 {
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__);
119
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 );
130 if ($error == '')
131 return buildRedirectURL (__FUNCTION__, 'OK');
132 else
133 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
134 }
135
136 $msgcode['addPortForObject']['OK'] = 5;
137 $msgcode['addPortForObject']['ERR1'] = 101;
138 $msgcode['addPortForObject']['ERR2'] = 100;
139 function addPortForObject ()
140 {
141 assertUIntArg ('object_id', __FUNCTION__);
142 assertStringArg ('port_name', __FUNCTION__, TRUE);
143 if (!strlen ($_REQUEST['port_name']))
144 return buildRedirectURL (__FUNCTION__, 'ERR1');
145 $error = commitAddPort ($_REQUEST['object_id'], $_REQUEST['port_name'], $_REQUEST['port_type_id'], $_REQUEST['port_label'], $_REQUEST['port_l2address']);
146 if ($error != '')
147 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
148 else
149 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
150 }
151
152 $msgcode['editPortForObject']['OK'] = 6;
153 $msgcode['editPortForObject']['ERR1'] = 101;
154 $msgcode['editPortForObject']['ERR2'] = 100;
155 function editPortForObject ()
156 {
157 assertUIntArg ('object_id', __FUNCTION__);
158 assertUIntArg ('port_id', __FUNCTION__);
159 assertUIntArg ('port_type_id', __FUNCTION__);
160 // tolerate empty value now to produce custom informative message later
161 assertStringArg ('name', __FUNCTION__, TRUE);
162 if (!strlen ($_REQUEST['name']))
163 return buildRedirectURL (__FUNCTION__, 'ERR1');
164
165 if (isset ($_REQUEST['reservation_comment']) and strlen ($_REQUEST['reservation_comment']))
166 $port_rc = '"' . $_REQUEST['reservation_comment'] . '"';
167 else
168 $port_rc = 'NULL';
169 $error = commitUpdatePort ($_REQUEST['object_id'], $_REQUEST['port_id'], $_REQUEST['name'], $_REQUEST['port_type_id'], $_REQUEST['label'], $_REQUEST['l2address'], $port_rc);
170 if ($error != '')
171 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
172 else
173 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
174 }
175
176 $msgcode['delPortFromObject']['OK'] = 7;
177 $msgcode['delPortFromObject']['ERR'] = 100;
178 function delPortFromObject ()
179 {
180 assertUIntArg ('port_id', __FUNCTION__);
181 $error = delObjectPort ($_REQUEST['port_id']);
182
183 if ($error != '')
184 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
185 else
186 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name']));
187 }
188
189 $msgcode['linkPortForObject']['OK'] = 8;
190 $msgcode['linkPortForObject']['ERR'] = 100;
191 function linkPortForObject ()
192 {
193 assertUIntArg ('port_id', __FUNCTION__);
194 assertUIntArg ('remote_port_id', __FUNCTION__);
195 assertStringArg ('port_name', __FUNCTION__, TRUE);
196 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
197 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
198
199 $error = linkPorts ($_REQUEST['port_id'], $_REQUEST['remote_port_id']);
200 if ($error != '')
201 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
202 else
203 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
204 }
205
206 $msgcode['unlinkPortForObject']['OK'] = 9;
207 $msgcode['unlinkPortForObject']['ERR'] = 100;
208 function unlinkPortForObject ()
209 {
210 assertUIntArg ('port_id', __FUNCTION__);
211 assertStringArg ('port_name', __FUNCTION__, TRUE);
212 assertStringArg ('remote_port_name', __FUNCTION__, TRUE);
213 assertStringArg ('remote_object_name', __FUNCTION__, TRUE);
214
215 $error = unlinkPort ($_REQUEST['port_id']);
216 if ($error != '')
217 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
218 else
219 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['port_name'], $_REQUEST['remote_port_name'], $_REQUEST['remote_object_name']));
220 }
221
222 $msgcode['addMultiPorts']['OK'] = 10;
223 $msgcode['addMultiPorts']['ERR'] = 123;
224 function addMultiPorts ()
225 {
226 assertStringArg ('format', __FUNCTION__);
227 assertStringArg ('input', __FUNCTION__);
228 assertUIntArg ('port_type', __FUNCTION__);
229 assertUIntArg ('object_id', __FUNCTION__);
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);
240 if (!strlen ($parts[0]))
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 /*
263 How Async Lines are Numbered in Cisco 3600 Series Routers
264 http://www.cisco.com/en/US/products/hw/routers/ps274/products_tech_note09186a00801ca70b.shtml
265
266 Understanding 16- and 32-Port Async Network Modules
267 http://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;
294 case 'ssv1':
295 $words = explode (' ', $line);
296 if (!strlen ($words[0]) or !strlen ($words[1]))
297 continue;
298 $ports[] = array
299 (
300 'name' => $words[0],
301 'l2address' => $words[1],
302 'label' => ''
303 );
304 break;
305 default:
306 return buildRedirectURL (__FUNCTION__, 'ERR');
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 {
325 $result = commitUpdatePort ($object_id, $port_id, $port['name'], $port_type, $port['label'], $port['l2address']);
326 if ($result == '')
327 $updated_count++;
328 else
329 $error_count++;
330 }
331 }
332 return buildRedirectURL (__FUNCTION__, 'OK', array ($added_count, $updated_count, $error_count));
333 }
334
335 $msgcode['updIPv4Allocation']['OK'] = 12;
336 $msgcode['updIPv4Allocation']['ERR'] = 100;
337 function updIPv4Allocation ()
338 {
339 assertIPv4Arg ('ip', __FUNCTION__);
340 assertUIntArg ('object_id', __FUNCTION__);
341 assertStringArg ('bond_name', __FUNCTION__, TRUE);
342 assertStringArg ('bond_type', __FUNCTION__);
343
344 $error = updateBond ($_REQUEST['ip'], $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
345 if ($error != '')
346 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
347 else
348 return buildRedirectURL (__FUNCTION__, 'OK');
349 }
350
351 $msgcode['delIPv4Allocation']['OK'] = 14;
352 $msgcode['delIPv4Allocation']['ERR'] = 100;
353 function delIPv4Allocation ()
354 {
355 assertIPv4Arg ('ip', __FUNCTION__);
356 assertUIntArg ('object_id', __FUNCTION__);
357
358 $error = unbindIpFromObject ($_REQUEST['ip'], $_REQUEST['object_id']);
359 if ($error != '')
360 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
361 else
362 return buildRedirectURL (__FUNCTION__, 'OK');
363 }
364
365 $msgcode['addIPv4Allocation']['OK'] = 13;
366 $msgcode['addIPv4Allocation']['ERR1'] = 170;
367 $msgcode['addIPv4Allocation']['ERR2'] = 100;
368 function addIPv4Allocation ()
369 {
370 assertIPv4Arg ('ip', __FUNCTION__);
371 assertUIntArg ('object_id', __FUNCTION__);
372 assertStringArg ('bond_name', __FUNCTION__, TRUE);
373 assertStringArg ('bond_type', __FUNCTION__);
374
375 // Strip masklen.
376 $ip = ereg_replace ('/[[:digit:]]+$', '', $_REQUEST['ip']);
377 if (getConfigVar ('IPV4_JAYWALK') != 'yes' and NULL === getIPv4AddressNetworkId ($ip))
378 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($ip));
379
380 $error = bindIpToObject ($ip, $_REQUEST['object_id'], $_REQUEST['bond_name'], $_REQUEST['bond_type']);
381 if ($error != '')
382 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
383 $address = getIPv4Address ($ip);
384 if ($address['reserved'] == 'yes' or strlen ($address['name']))
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 }
393 return buildRedirectURL (__FUNCTION__, 'OK');
394 }
395
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;
402 function addIPv4Prefix ()
403 {
404 assertStringArg ('range', __FUNCTION__);
405 assertStringArg ('name', __FUNCTION__, TRUE);
406
407 $is_bcast = isset ($_REQUEST['is_bcast']) ? $_REQUEST['is_bcast'] : 'off';
408 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
409 $error = createIPv4Prefix ($_REQUEST['range'], $_REQUEST['name'], $is_bcast == 'on', $taglist);
410 if ($error != '')
411 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
412 else
413 return buildRedirectURL (__FUNCTION__, 'OK');
414 }
415
416 $msgcode['delIPv4Prefix']['OK'] = 24;
417 $msgcode['delIPv4Prefix']['ERR'] = 100;
418 function delIPv4Prefix ()
419 {
420 assertUIntArg ('id', __FUNCTION__);
421 $error = destroyIPv4Prefix ($_REQUEST['id']);
422 if ($error != '')
423 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
424 else
425 return buildRedirectURL (__FUNCTION__, 'OK');
426 }
427
428 $msgcode['updIPv4Prefix']['OK'] = 25;
429 $msgcode['updIPv4Prefix']['ERR'] = 100;
430 function updIPv4Prefix ()
431 {
432 assertUIntArg ('id', __FUNCTION__);
433 assertStringArg ('name', __FUNCTION__, TRUE);
434 assertStringArg ('comment', __FUNCTION__, TRUE);
435 global $sic;
436 if (strlen ($error = updateIPv4Network_real ($sic['id'], $sic['name'], $sic['comment'])))
437 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
438 else
439 return buildRedirectURL (__FUNCTION__, 'OK');
440 }
441
442 $msgcode['editAddress']['OK'] = 27;
443 $msgcode['editAddress']['ERR'] = 100;
444 function editAddress ()
445 {
446 assertIPv4Arg ('ip', __FUNCTION__);
447 assertStringArg ('name', __FUNCTION__, TRUE);
448
449 if (isset ($_REQUEST['reserved']))
450 $reserved = $_REQUEST['reserved'];
451 else
452 $reserved = 'off';
453 $error = updateAddress ($_REQUEST['ip'], $_REQUEST['name'], $reserved == 'on' ? 'yes' : 'no');
454 if ($error != '')
455 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
456 else
457 return buildRedirectURL (__FUNCTION__, 'OK');
458 }
459
460 $msgcode['createUser']['OK'] = 40;
461 $msgcode['createUser']['ERR'] = 102;
462 function createUser ()
463 {
464 assertStringArg ('username', __FUNCTION__);
465 assertStringArg ('realname', __FUNCTION__, TRUE);
466 assertStringArg ('password', __FUNCTION__);
467 $username = $_REQUEST['username'];
468 $password = sha1 ($_REQUEST['password']);
469 $result = commitCreateUserAccount ($username, $_REQUEST['realname'], $password);
470 if ($result == TRUE)
471 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
472 else
473 return buildRedirectURL (__FUNCTION__, 'ERR', array ($username));
474 }
475
476 $msgcode['updateUser']['OK'] = 39;
477 $msgcode['updateUser']['ERR1'] = 103;
478 $msgcode['updateUser']['ERR1'] = 104;
479 function updateUser ()
480 {
481 assertUIntArg ('user_id', __FUNCTION__);
482 assertStringArg ('username', __FUNCTION__);
483 assertStringArg ('realname', __FUNCTION__, TRUE);
484 assertStringArg ('password', __FUNCTION__);
485 $username = $_REQUEST['username'];
486 $new_password = $_REQUEST['password'];
487 if (NULL == ($userinfo = spotEntity ('user', $_REQUEST['user_id'])))
488 return buildRedirectURL (__FUNCTION__, 'ERR1');
489 // Update user password only if provided password is not the same as current password hash.
490 if ($new_password != $userinfo['user_password_hash'])
491 $new_password = sha1 ($new_password);
492 $result = commitUpdateUserAccount ($_REQUEST['user_id'], $username, $_REQUEST['realname'], $new_password);
493 if ($result == TRUE)
494 return buildRedirectURL (__FUNCTION__, 'OK', array ($username));
495 else
496 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($username));
497 }
498
499 $msgcode['savePortMap']['OK'] = 44;
500 $msgcode['savePortMap']['ERR'] = 108;
501 // This function find differences in users's submit and PortCompat table
502 // and modifies database accordingly.
503 function savePortMap ()
504 {
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:
538 showError ('Internal error: oldCompatTable is invalid', __FUNCTION__);
539 break;
540 }
541 if ($error_count == 0)
542 return buildRedirectURL (__FUNCTION__, 'OK', array ($error_count, $success_count));
543 else
544 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error_count, $success_count));
545 }
546
547 $msgcode['updateDictionary']['OK'] = 51;
548 $msgcode['updateDictionary']['ERR'] = 109;
549 function updateDictionary ()
550 {
551 assertUIntArg ('chapter_no', __FUNCTION__);
552 assertUIntArg ('dict_key', __FUNCTION__);
553 assertStringArg ('dict_value', __FUNCTION__);
554 if (commitUpdateDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key'], $_REQUEST['dict_value']) === TRUE)
555 return buildRedirectURL (__FUNCTION__, 'OK');
556 else
557 return buildRedirectURL (__FUNCTION__, 'ERR');
558 }
559
560 $msgcode['supplementDictionary']['OK'] = 52;
561 $msgcode['supplementDictionary']['ERR'] = 110;
562 function supplementDictionary ()
563 {
564 assertUIntArg ('chapter_no', __FUNCTION__);
565 assertStringArg ('dict_value', __FUNCTION__);
566 if (commitSupplementDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_value']) === TRUE)
567 return buildRedirectURL (__FUNCTION__, 'OK');
568 else
569 return buildRedirectURL (__FUNCTION__, 'ERR');
570 }
571
572 $msgcode['reduceDictionary']['OK'] = 50;
573 $msgcode['reduceDictionary']['ERR'] = 111;
574 function reduceDictionary ()
575 {
576 assertUIntArg ('chapter_no', __FUNCTION__);
577 assertUIntArg ('dict_key', __FUNCTION__);
578 if (commitReduceDictionary ($_REQUEST['chapter_no'], $_REQUEST['dict_key']) === TRUE)
579 return buildRedirectURL (__FUNCTION__, 'OK');
580 else
581 return buildRedirectURL (__FUNCTION__, 'ERR');
582 }
583
584 $msgcode['addChapter']['OK'] = 55;
585 $msgcode['addChapter']['ERR'] = 112;
586 function addChapter ()
587 {
588 assertStringArg ('chapter_name', __FUNCTION__);
589 if (commitAddChapter ($_REQUEST['chapter_name']) === TRUE)
590 return buildRedirectURL (__FUNCTION__, 'OK');
591 else
592 return buildRedirectURL (__FUNCTION__, 'ERR');
593 }
594
595 $msgcode['updateChapter']['OK'] = 54;
596 $msgcode['updateChapter']['ERR'] = 113;
597 function updateChapter ()
598 {
599 assertUIntArg ('chapter_no', __FUNCTION__);
600 assertStringArg ('chapter_name', __FUNCTION__);
601 if (commitUpdateChapter ($_REQUEST['chapter_no'], $_REQUEST['chapter_name']) === TRUE)
602 return buildRedirectURL (__FUNCTION__, 'OK');
603 else
604 return buildRedirectURL (__FUNCTION__, 'ERR');
605 }
606
607 $msgcode['delChapter']['OK'] = 53;
608 $msgcode['delChapter']['ERR'] = 114;
609 function delChapter ()
610 {
611 assertUIntArg ('chapter_no', __FUNCTION__);
612 if (commitDeleteChapter ($_REQUEST['chapter_no']))
613 return buildRedirectURL (__FUNCTION__, 'OK');
614 else
615 return buildRedirectURL (__FUNCTION__, 'ERR');
616 }
617
618 $msgcode['changeAttribute']['OK'] = 46;
619 $msgcode['changeAttribute']['ERR'] = 115;
620 function changeAttribute ()
621 {
622 assertUIntArg ('attr_id', __FUNCTION__);
623 assertStringArg ('attr_name', __FUNCTION__);
624 if (commitUpdateAttribute ($_REQUEST['attr_id'], $_REQUEST['attr_name']))
625 return buildRedirectURL (__FUNCTION__, 'OK');
626 else
627 return buildRedirectURL (__FUNCTION__, 'ERR');
628 }
629
630 $msgcode['createAttribute']['OK'] = 45;
631 $msgcode['createAttribute']['ERR'] = 116;
632 function createAttribute ()
633 {
634 assertStringArg ('attr_name', __FUNCTION__);
635 assertStringArg ('attr_type', __FUNCTION__);
636 if (commitAddAttribute ($_REQUEST['attr_name'], $_REQUEST['attr_type']))
637 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['attr_name']));
638 else
639 return buildRedirectURL (__FUNCTION__, 'ERR');
640 }
641
642 $msgcode['deleteAttribute']['OK'] = 47;
643 $msgcode['deleteAttribute']['ERR'] = 117;
644 function deleteAttribute ()
645 {
646 assertUIntArg ('attr_id', __FUNCTION__);
647 if (commitDeleteAttribute ($_REQUEST['attr_id']))
648 return buildRedirectURL (__FUNCTION__, 'OK');
649 else
650 return buildRedirectURL (__FUNCTION__, 'ERR');
651 }
652
653 $msgcode['supplementAttrMap']['OK'] = 48;
654 $msgcode['supplementAttrMap']['ERR'] = 118;
655 function supplementAttrMap ()
656 {
657 assertUIntArg ('attr_id', __FUNCTION__);
658 assertUIntArg ('objtype_id', __FUNCTION__);
659 assertUIntArg ('chapter_no', __FUNCTION__);
660 if (commitSupplementAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id'], $_REQUEST['chapter_no']) === TRUE)
661 return buildRedirectURL (__FUNCTION__, 'OK');
662 else
663 return buildRedirectURL (__FUNCTION__, 'ERR');
664 }
665
666 $msgcode['reduceAttrMap']['OK'] = 49;
667 $msgcode['reduceAttrMap']['ERR'] = 119;
668 function reduceAttrMap ()
669 {
670 assertUIntArg ('attr_id', __FUNCTION__);
671 assertUIntArg ('objtype_id', __FUNCTION__);
672 if (commitReduceAttrMap ($_REQUEST['attr_id'], $_REQUEST['objtype_id']) === TRUE)
673 return buildRedirectURL (__FUNCTION__, 'OK');
674 else
675 return buildRedirectURL (__FUNCTION__, 'ERR');
676 }
677
678 $msgcode['clearSticker']['OK'] = 15;
679 $msgcode['clearSticker']['ERR'] = 120;
680 function clearSticker ()
681 {
682 assertUIntArg ('attr_id', __FUNCTION__);
683 assertUIntArg ('object_id', __FUNCTION__);
684 if (commitResetAttrValue ($_REQUEST['object_id'], $_REQUEST['attr_id']) === TRUE)
685 return buildRedirectURL (__FUNCTION__, 'OK');
686 else
687 return buildRedirectURL (__FUNCTION__, 'ERR');
688 }
689
690 $msgcode['updateObjectAllocation']['OK'] = 63;
691 function updateObjectAllocation ()
692 {
693 assertUIntArg ('object_id', __FUNCTION__);
694
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)
708 {
709 if (!isset ($workingRacksData[$cand_id]))
710 {
711 $rackData = spotEntity ('rack', $cand_id);
712 if ($rackData == NULL)
713 {
714 showError ('rack not found', __FUNCTION__);
715 return;
716 }
717 amplifyCell ($rackData);
718 $workingRacksData[$cand_id] = $rackData;
719 }
720 }
721
722 foreach ($workingRacksData as &$rd)
723 applyObjectMountMask ($rd, $object_id);
724
725 $oldMolecule = getMoleculeForObject ($object_id);
726 $changecnt = 0;
727 $log = array();
728 foreach ($workingRacksData as $rack_id => $rackData)
729 {
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;
740 }
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);
761 }
762
763 $msgcode['updateObject']['OK'] = 16;
764 $msgcode['updateObject']['ERR'] = 121;
765 function updateObject ()
766 {
767 assertUIntArg ('num_attrs', __FUNCTION__, TRUE);
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)
789 return buildRedirectURL (__FUNCTION__, 'ERR');
790
791 // Update optional attributes
792 $oldvalues = getAttrValues ($_REQUEST['object_id']);
793 $result = array();
794 $num_attrs = isset ($_REQUEST['num_attrs']) ? $_REQUEST['num_attrs'] : 0;
795 for ($i = 0; $i < $num_attrs; $i++)
796 {
797 assertUIntArg ("${i}_attr_id", __FUNCTION__);
798 $attr_id = $_REQUEST["${i}_attr_id"];
799
800 // Field is empty, delete attribute and move on.
801 if (!strlen ($_REQUEST["${i}_value"]))
802 {
803 commitResetAttrValue ($_REQUEST['object_id'], $attr_id);
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.
809 assertStringArg ("${i}_value", __FUNCTION__);
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:
822 showError ('Internal structure error', __FUNCTION__);
823 die;
824 }
825 if ($value === $oldvalue) // ('' == 0), but ('' !== 0)
826 continue;
827
828 // Note if the queries succeed or not, it determines which page they see.
829 $result[] = commitUpdateAttrValue ($_REQUEST['object_id'], $attr_id, $value);
830 }
831 if (in_array (FALSE, $result))
832 return buildRedirectURL (__FUNCTION__, 'ERR');
833
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
838 return buildRedirectURL (__FUNCTION__, 'OK');
839 }
840
841
842 function addMultipleObjects()
843 {
844 $log = emptyLog();
845 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
846 $max = getConfigVar ('MASSCOUNT');
847 for ($i = 0; $i < $max; $i++)
848 {
849 if (!isset ($_REQUEST["${i}_object_type_id"]))
850 {
851 $log = mergeLogs ($log, oneLiner (184, array ($i + 1)));
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);
859 $name = $_REQUEST["${i}_object_name"];
860
861 // It's better to skip silently, than to print a notice.
862 if ($_REQUEST["${i}_object_type_id"] == 0)
863 continue;
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)
873 $log = mergeLogs ($log, oneLiner (80, array ($name)));
874 else
875 $log = mergeLogs ($log, oneLiner (185, array ($name)));
876 }
877 return buildWideRedirectURL ($log);
878 }
879
880 function addLotOfObjects()
881 {
882 $log = emptyLog();
883 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
884 assertUIntArg ('global_type_id', __FUNCTION__, TRUE);
885 assertStringArg ('namelist', __FUNCTION__, TRUE);
886 $global_type_id = $_REQUEST['global_type_id'];
887 if ($global_type_id == 0 or !strlen ($_REQUEST['namelist']))
888 $log = mergeLogs ($log, oneLiner (186));
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);
898 if (!strlen ($parts[0]))
899 continue;
900 else
901 $names2[] = rtrim ($parts[0]);
902 }
903 foreach ($names2 as $name)
904 if (commitAddObject ($name, '', '', $global_type_id, '', $taglist) === TRUE)
905 $log = mergeLogs ($log, oneLiner (80, array ($name)));
906 else
907 $log = mergeLogs ($log, oneLiner (185, array ($name)));
908 }
909 return buildWideRedirectURL ($log);
910 }
911
912 $msgcode['deleteObject']['OK'] = 76;
913 $msgcode['deleteObject']['ERR'] = 100;
914 function deleteObject ()
915 {
916 assertUIntArg ('object_id', __FUNCTION__);
917 if (NULL === ($oinfo = spotEntity ('object', $_REQUEST['object_id'])))
918 return buildRedirectURL (__FUNCTION__, 'ERR', array ('object not found'));
919
920 $racklist = getResidentRacksData ($_REQUEST['object_id'], FALSE);
921 $error = commitDeleteObject ($_REQUEST['object_id']);
922 foreach ($racklist as $rack_id)
923 resetThumbCache ($rack_id);
924
925 if ($error != '')
926 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
927
928 return buildRedirectURL (__FUNCTION__, 'OK', array ($oinfo['dname']));
929 }
930
931 $msgcode['useupPort']['OK'] = 11;
932 $msgcode['useupPort']['ERR'] = 124;
933 function useupPort ()
934 {
935 assertUIntArg ('port_id', __FUNCTION__);
936 if (commitUseupPort ($_REQUEST['port_id']) === TRUE)
937 return buildRedirectURL (__FUNCTION__, 'OK');
938 else
939 return buildRedirectURL (__FUNCTION__, 'ERR');
940 }
941
942 $msgcode['updateUI']['OK'] = 56;
943 $msgcode['updateUI']['ERR'] = 125;
944 function updateUI ()
945 {
946 assertUIntArg ('num_vars', __FUNCTION__);
947
948 for ($i = 0; $i < $_REQUEST['num_vars']; $i++)
949 {
950 assertStringArg ("${i}_varname", __FUNCTION__);
951 assertStringArg ("${i}_varvalue", __FUNCTION__, TRUE);
952 $varname = $_REQUEST["${i}_varname"];
953 $varvalue = $_REQUEST["${i}_varvalue"];
954
955 // If form value = value in DB, don't bother updating DB
956 if ($varvalue === getConfigVar ($varname))
957 continue;
958
959 // Note if the queries succeed or not, it determines which page they see.
960 $error = setConfigVar ($varname, $varvalue, TRUE);
961 if (strlen ($error))
962 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
963 }
964 return buildRedirectURL (__FUNCTION__, 'OK');
965 }
966
967 $msgcode['resetUIConfig']['OK'] = 57;
968 function resetUIConfig()
969 {
970 setConfigVar ('default_port_type','24');
971 setConfigVar ('MASSCOUNT','15');
972 setConfigVar ('MAXSELSIZE','30');
973 setConfigVar ('ROW_SCALE','2');
974 setConfigVar ('PORTS_PER_ROW','12');
975 setConfigVar ('IPV4_ADDRS_PER_PAGE','256');
976 setConfigVar ('DEFAULT_RACK_HEIGHT','42');
977 setConfigVar ('DEFAULT_SLB_VS_PORT','');
978 setConfigVar ('DEFAULT_SLB_RS_PORT','');
979 setConfigVar ('DETECT_URLS','no');
980 setConfigVar ('RACK_PRESELECT_THRESHOLD','1');
981 setConfigVar ('DEFAULT_IPV4_RS_INSERVICE','no');
982 setConfigVar ('AUTOPORTS_CONFIG','4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm');
983 setConfigVar ('SHOW_EXPLICIT_TAGS','yes');
984 setConfigVar ('SHOW_IMPLICIT_TAGS','yes');
985 setConfigVar ('SHOW_AUTOMATIC_TAGS','no');
986 setConfigVar ('DEFAULT_OBJECT_TYPE','4');
987 setConfigVar ('IPV4_AUTO_RELEASE','1');
988 setConfigVar ('SHOW_LAST_TAB', 'no');
989 setConfigVar ('EXT_IPV4_VIEW', 'yes');
990 setConfigVar ('TREE_THRESHOLD', '25');
991 setConfigVar ('IPV4_JAYWALK', 'no');
992 setConfigVar ('ADDNEW_AT_TOP', 'yes');
993 setConfigVar ('IPV4_TREE_SHOW_USAGE', 'yes');
994 setConfigVar ('PREVIEW_TEXT_MAXCHARS', '10240');
995 setConfigVar ('PREVIEW_TEXT_ROWS', '25');
996 setConfigVar ('PREVIEW_TEXT_COLS', '80');
997 setConfigVar ('PREVIEW_IMAGE_MAXPXS', '320');
998 setConfigVar ('VENDOR_SIEVE', '');
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}');
1004 setConfigVar ('RACKS_PER_ROW','12');
1005 setConfigVar ('FILTER_PREDICATE_SIEVE','');
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');
1011 setConfigVar ('DEFAULT_SNMP_COMMUNITY','public');
1012 setConfigVar ('IPV4_ENABLE_KNIGHT','yes');
1013 setConfigVar ('TAGS_TOPLIST_SIZE','50');
1014 setConfigVar ('TAGS_QUICKLIST_SIZE','20');
1015 setConfigVar ('TAGS_QUICKLIST_THRESHOLD','50');
1016 return buildRedirectURL (__FUNCTION__, 'OK');
1017 }
1018
1019 $msgcode['addRealServer']['OK'] = 34;
1020 $msgcode['addRealServer']['ERR'] = 126;
1021 // Add single record.
1022 function addRealServer ()
1023 {
1024 assertUIntArg ('pool_id', __FUNCTION__);
1025 assertIPv4Arg ('remoteip', __FUNCTION__);
1026 assertStringArg ('rsport', __FUNCTION__, TRUE);
1027 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1028 if (!addRStoRSPool (
1029 $_REQUEST['pool_id'],
1030 $_REQUEST['remoteip'],
1031 $_REQUEST['rsport'],
1032 getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'),
1033 $_REQUEST['rsconfig']
1034 ))
1035 return buildRedirectURL (__FUNCTION__, 'ERR');
1036 else
1037 return buildRedirectURL (__FUNCTION__, 'OK');
1038 }
1039
1040 $msgcode['addRealServers']['OK'] = 37;
1041 $msgcode['addRealServers']['ERR1'] = 131;
1042 $msgcode['addRealServers']['ERR2'] = 127;
1043 // Parse textarea submitted and try adding a real server for each line.
1044 function addRealServers ()
1045 {
1046 assertUIntArg ('pool_id', __FUNCTION__);
1047 assertStringArg ('format', __FUNCTION__);
1048 assertStringArg ('rawtext', __FUNCTION__);
1049 $rawtext = str_replace ('\r', '', $_REQUEST['rawtext']);
1050 $ngood = $nbad = 0;
1051 $rsconfig = '';
1052 // Keep in mind, that the text will have HTML entities (namely '>') escaped.
1053 foreach (explode ('\n', $rawtext) as $line)
1054 {
1055 if (!strlen ($line))
1056 continue;
1057 $match = array ();
1058 switch ($_REQUEST['format'])
1059 {
1060 case 'ipvs_2': // address and port only
1061 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) /', $line, $match))
1062 continue;
1063 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1064 $ngood++;
1065 else
1066 $nbad++;
1067 break;
1068 case 'ipvs_3': // address, port and weight
1069 if (!preg_match ('/^ -&gt; ([0-9\.]+):([0-9]+) +[a-zA-Z]+ +([0-9]+) /', $line, $match))
1070 continue;
1071 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), 'weight ' . $match[3]))
1072 $ngood++;
1073 else
1074 $nbad++;
1075 break;
1076 case 'ssv_2': // IP address and port
1077 if (!preg_match ('/^([0-9\.]+) ([0-9]+)$/', $line, $match))
1078 continue;
1079 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], $match[2], getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1080 $ngood++;
1081 else
1082 $nbad++;
1083 break;
1084 case 'ssv_1': // IP address
1085 if (!preg_match ('/^([0-9\.]+)$/', $line, $match))
1086 continue;
1087 if (addRStoRSPool ($_REQUEST['pool_id'], $match[1], 0, getConfigVar ('DEFAULT_IPV4_RS_INSERVICE'), ''))
1088 $ngood++;
1089 else
1090 $nbad++;
1091 break;
1092 default:
1093 return buildRedirectURL (__FUNCTION__, 'ERR1');
1094 break;
1095 }
1096 }
1097 if ($nbad == 0 and $ngood > 0)
1098 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1099 else
1100 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($ngood, $nbad));
1101 }
1102
1103 $msgcode['addVService']['OK'] = 28;
1104 $msgcode['addVService']['ERR1'] = 132;
1105 $msgcode['addVService']['ERR2'] = 100;
1106 function addVService ()
1107 {
1108 assertIPv4Arg ('vip', __FUNCTION__);
1109 assertUIntArg ('vport', __FUNCTION__);
1110 assertStringArg ('proto', __FUNCTION__);
1111 if ($_REQUEST['proto'] != 'TCP' and $_REQUEST['proto'] != 'UDP')
1112 return buildRedirectURL (__FUNCTION__, 'ERR1');
1113 assertStringArg ('name', __FUNCTION__, TRUE);
1114 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1115 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1116 $error = commitCreateVS
1117 (
1118 $_REQUEST['vip'],
1119 $_REQUEST['vport'],
1120 $_REQUEST['proto'],
1121 $_REQUEST['name'],
1122 $_REQUEST['vsconfig'],
1123 $_REQUEST['rsconfig'],
1124 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1125 );
1126 if ($error != '')
1127 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error));
1128 else
1129 return buildRedirectURL (__FUNCTION__, 'OK');
1130 }
1131
1132 $msgcode['deleteRealServer']['OK'] = 35;
1133 $msgcode['deleteRealServer']['ERR'] = 128;
1134 function deleteRealServer ()
1135 {
1136 assertUIntArg ('id', __FUNCTION__);
1137 if (!commitDeleteRS ($_REQUEST['id']))
1138 return buildRedirectURL (__FUNCTION__, 'ERR');
1139 else
1140 return buildRedirectURL (__FUNCTION__, 'OK');
1141 }
1142
1143 $msgcode['deleteLoadBalancer']['OK'] = 19;
1144 $msgcode['deleteLoadBalancer']['ERR'] = 129;
1145 function deleteLoadBalancer ()
1146 {
1147 assertUIntArg ('object_id', __FUNCTION__);
1148 assertUIntArg ('pool_id', __FUNCTION__);
1149 assertUIntArg ('vs_id', __FUNCTION__);
1150 if (!commitDeleteLB (
1151 $_REQUEST['object_id'],
1152 $_REQUEST['pool_id'],
1153 $_REQUEST['vs_id']
1154 ))
1155 return buildRedirectURL (__FUNCTION__, 'ERR');
1156 else
1157 return buildRedirectURL (__FUNCTION__, 'OK');
1158 }
1159
1160 $msgcode['deleteVService']['OK'] = 29;
1161 $msgcode['deleteVService']['ERR'] = 130;
1162 function deleteVService ()
1163 {
1164 assertUIntArg ('vs_id', __FUNCTION__);
1165 if (!commitDeleteVS ($_REQUEST['vs_id']))
1166 return buildRedirectURL (__FUNCTION__, 'ERR');
1167 else
1168 return buildRedirectURL (__FUNCTION__, 'OK');
1169 }
1170
1171 $msgcode['updateRealServer']['OK'] = 36;
1172 $msgcode['updateRealServer']['ERR'] = 133;
1173 function updateRealServer ()
1174 {
1175 assertUIntArg ('rs_id', __FUNCTION__);
1176 assertIPv4Arg ('rsip', __FUNCTION__);
1177 assertStringArg ('rsport', __FUNCTION__, TRUE);
1178 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1179 if (!commitUpdateRS (
1180 $_REQUEST['rs_id'],
1181 $_REQUEST['rsip'],
1182 $_REQUEST['rsport'],
1183 $_REQUEST['rsconfig']
1184 ))
1185 return buildRedirectURL (__FUNCTION__, 'ERR');
1186 else
1187 return buildRedirectURL (__FUNCTION__, 'OK');
1188 }
1189
1190 $msgcode['updateLoadBalancer']['OK'] = 20;
1191 $msgcode['updateLoadBalancer']['ERR'] = 134;
1192 function updateLoadBalancer ()
1193 {
1194 assertUIntArg ('object_id', __FUNCTION__);
1195 assertUIntArg ('pool_id', __FUNCTION__);
1196 assertUIntArg ('vs_id', __FUNCTION__);
1197 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1198 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1199 if (!commitUpdateLB (
1200 $_REQUEST['object_id'],
1201 $_REQUEST['pool_id'],
1202 $_REQUEST['vs_id'],
1203 $_REQUEST['vsconfig'],
1204 $_REQUEST['rsconfig']
1205 ))
1206 return buildRedirectURL (__FUNCTION__, 'ERR');
1207 else
1208 return buildRedirectURL (__FUNCTION__, 'OK');
1209 }
1210
1211 $msgcode['updateVService']['OK'] = 30;
1212 $msgcode['updateVService']['ERR'] = 135;
1213 function updateVService ()
1214 {
1215 assertUIntArg ('vs_id', __FUNCTION__);
1216 assertIPv4Arg ('vip', __FUNCTION__);
1217 assertUIntArg ('vport', __FUNCTION__);
1218 assertStringArg ('proto', __FUNCTION__);
1219 assertStringArg ('name', __FUNCTION__, TRUE);
1220 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1221 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1222 if (!commitUpdateVS (
1223 $_REQUEST['vs_id'],
1224 $_REQUEST['vip'],
1225 $_REQUEST['vport'],
1226 $_REQUEST['proto'],
1227 $_REQUEST['name'],
1228 $_REQUEST['vsconfig'],
1229 $_REQUEST['rsconfig']
1230 ))
1231 return buildRedirectURL (__FUNCTION__, 'ERR');
1232 else
1233 return buildRedirectURL (__FUNCTION__, 'OK');
1234 }
1235
1236 $msgcode['addLoadBalancer']['OK'] = 18;
1237 $msgcode['addLoadBalancer']['ERR'] = 137;
1238 function addLoadBalancer ()
1239 {
1240 assertUIntArg ('pool_id', __FUNCTION__);
1241 assertUIntArg ('object_id', __FUNCTION__);
1242 assertUIntArg ('vs_id', __FUNCTION__);
1243 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1244 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1245 if (!addLBtoRSPool (
1246 $_REQUEST['pool_id'],
1247 $_REQUEST['object_id'],
1248 $_REQUEST['vs_id'],
1249 $_REQUEST['vsconfig'],
1250 $_REQUEST['rsconfig']
1251 ))
1252 return buildRedirectURL (__FUNCTION__, 'ERR');
1253 else
1254 return buildRedirectURL (__FUNCTION__, 'OK');
1255 }
1256
1257 $msgcode['addRSPool']['OK'] = 31;
1258 $msgcode['addRSPool']['ERR'] = 100;
1259 function addRSPool ()
1260 {
1261 assertStringArg ('name', __FUNCTION__, TRUE);
1262 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1263 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1264 $error = commitCreateRSPool
1265 (
1266 $_REQUEST['name'],
1267 $_REQUEST['vsconfig'],
1268 $_REQUEST['rsconfig'],
1269 isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array()
1270 );
1271 if ($error != '')
1272 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1273 else
1274 return buildRedirectURL (__FUNCTION__, 'OK');
1275 }
1276
1277 $msgcode['deleteRSPool']['OK'] = 32;
1278 $msgcode['deleteRSPool']['ERR'] = 138;
1279 function deleteRSPool ()
1280 {
1281 assertUIntArg ('pool_id', __FUNCTION__);
1282 if (!commitDeleteRSPool ($_REQUEST['pool_id']))
1283 return buildRedirectURL (__FUNCTION__, 'ERR');
1284 else
1285 return buildRedirectURL (__FUNCTION__, 'OK');
1286 }
1287
1288 $msgcode['updateRSPool']['OK'] = 33;
1289 $msgcode['updateRSPool']['ERR'] = 139;
1290 function updateRSPool ()
1291 {
1292 assertUIntArg ('pool_id', __FUNCTION__);
1293 assertStringArg ('name', __FUNCTION__, TRUE);
1294 assertStringArg ('vsconfig', __FUNCTION__, TRUE);
1295 assertStringArg ('rsconfig', __FUNCTION__, TRUE);
1296 if (!commitUpdateRSPool ($_REQUEST['pool_id'], $_REQUEST['name'], $_REQUEST['vsconfig'], $_REQUEST['rsconfig']))
1297 return buildRedirectURL (__FUNCTION__, 'ERR');
1298 else
1299 return buildRedirectURL (__FUNCTION__, 'OK');
1300 }
1301
1302 $msgcode['updateRSInService']['OK'] = 38;
1303 $msgcode['updateRSInService']['ERR'] = 140;
1304 function updateRSInService ()
1305 {
1306 assertUIntArg ('rscount', __FUNCTION__);
1307 $pool_id = $_REQUEST['pool_id'];
1308 $orig = spotEntity ('ipv4rspool', $pool_id);
1309 amplifyCell ($orig);
1310 $nbad = $ngood = 0;
1311 for ($i = 1; $i <= $_REQUEST['rscount']; $i++)
1312 {
1313 $rs_id = $_REQUEST["rsid_${i}"];
1314 if (isset ($_REQUEST["inservice_${i}"]) and $_REQUEST["inservice_${i}"] == 'on')
1315 $newval = 'yes';
1316 else
1317 $newval = 'no';
1318 if ($newval != $orig['rslist'][$rs_id]['inservice'])
1319 {
1320 if (commitSetInService ($rs_id, $newval))
1321 $ngood++;
1322 else
1323 $nbad++;
1324 }
1325 }
1326 if (!$nbad)
1327 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1328 else
1329 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1330 }
1331
1332 $msgcode['importPTRData']['OK'] = 26;
1333 $msgcode['importPTRData']['ERR'] = 141;
1334 // FIXME: check, that each submitted address belongs to the prefix we
1335 // are operating on.
1336 function importPTRData ()
1337 {
1338 assertUIntArg ('addrcount', __FUNCTION__);
1339 $nbad = $ngood = 0;
1340 for ($i = 0; $i < $_REQUEST['addrcount']; $i++)
1341 {
1342 $inputname = "import_${i}";
1343 if (!isset ($_REQUEST[$inputname]) or $_REQUEST[$inputname] != 'on')
1344 continue;
1345 assertIPv4Arg ("addr_${i}", __FUNCTION__);
1346 assertStringArg ("descr_${i}", __FUNCTION__, TRUE);
1347 assertStringArg ("rsvd_${i}", __FUNCTION__);
1348 // Non-existent addresses will not have this argument set in request.
1349 $rsvd = 'no';
1350 if ($_REQUEST["rsvd_${i}"] == 'yes')
1351 $rsvd = 'yes';
1352 if (updateAddress ($_REQUEST["addr_${i}"], $_REQUEST["descr_${i}"], $rsvd) == '')
1353 $ngood++;
1354 else
1355 $nbad++;
1356 }
1357 if (!$nbad)
1358 return buildRedirectURL (__FUNCTION__, 'OK', array ($ngood));
1359 else
1360 return buildRedirectURL (__FUNCTION__, 'ERR', array ($nbad, $ngood));
1361 }
1362
1363 $msgcode['generateAutoPorts']['OK'] = 21;
1364 $msgcode['generateAutoPorts']['ERR'] = 142;
1365 function generateAutoPorts ()
1366 {
1367 global $pageno;
1368 assertUIntArg ('object_id', __FUNCTION__);
1369 $info = spotEntity ('object', $_REQUEST['object_id']);
1370 // Navigate away in case of success, stay at the place otherwise.
1371 if (executeAutoPorts ($_REQUEST['object_id'], $info['objtype_id']))
1372 return buildRedirectURL (__FUNCTION__, 'OK', array(), $pageno, 'ports');
1373 else
1374 return buildRedirectURL (__FUNCTION__, 'ERR');
1375 }
1376
1377 $msgcode['saveEntityTags']['OK'] = 22;
1378 $msgcode['saveEntityTags']['ERR1'] = 143;
1379 $msgcode['saveEntityTags']['ERR2'] = 187;
1380 // Filter out implicit tags before storing the new tag set.
1381 function saveEntityTags ()
1382 {
1383 global $page, $pageno, $etype_by_pageno;
1384 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1385 return buildRedirectURL (__FUNCTION__, 'ERR2', array (__FUNCTION__));
1386 $realm = $etype_by_pageno[$pageno];
1387 $bypass = $page[$pageno]['bypass'];
1388 assertUIntArg ($bypass, __FUNCTION__);
1389 $entity_id = $_REQUEST[$bypass];
1390 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1391 // Build a chain from the submitted data, minimize it,
1392 // then wipe existing records and store the new set instead.
1393 destroyTagsForEntity ($realm, $entity_id);
1394 // TODO: these actions are very close to what rebuildTagChainForEntity() does,
1395 // so why not use it?
1396 $newchain = getExplicitTagsOnly (buildTagChainFromIds ($taglist));
1397 $n_succeeds = $n_errors = 0;
1398 foreach ($newchain as $taginfo)
1399 if (addTagForEntity ($realm, $entity_id, $taginfo['id']))
1400 $n_succeeds++;
1401 else
1402 $n_errors++;
1403 if ($n_errors)
1404 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($n_succeeds, $n_errors));
1405 else
1406 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_succeeds));
1407 }
1408
1409 $msgcode['destroyTag']['OK'] = 58;
1410 $msgcode['destroyTag']['ERR1'] = 183;
1411 $msgcode['destroyTag']['ERR2'] = 144;
1412 function destroyTag ()
1413 {
1414 assertUIntArg ('tag_id', __FUNCTION__);
1415 global $taglist;
1416 if (!isset ($taglist[$_REQUEST['tag_id']]))
1417 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['tag_id']));
1418 if (($ret = commitDestroyTag ($_REQUEST['tag_id'])) == '')
1419 return buildRedirectURL (__FUNCTION__, 'OK', array ($taglist[$_REQUEST['tag_id']]['tag']));
1420 else
1421 return buildRedirectURL (__FUNCTION__, 'ERR2');
1422 }
1423
1424 $msgcode['createTag']['OK'] = 59;
1425 $msgcode['createTag']['ERR1'] = 145;
1426 $msgcode['createTag']['ERR3'] = 147;
1427 function createTag ()
1428 {
1429 assertStringArg ('tag_name', __FUNCTION__);
1430 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1431 $tagname = trim ($_REQUEST['tag_name']);
1432 if (!validTagName ($tagname))
1433 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
1434 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1435 $parent_id = 'NULL';
1436 if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
1437 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
1438 else
1439 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($tagname, $ret));
1440 }
1441
1442 $msgcode['updateTag']['OK'] = 60;
1443 $msgcode['updateTag']['ERR1'] = 145;
1444 $msgcode['updateTag']['ERR2'] = 148;
1445 function updateTag ()
1446 {
1447 assertUIntArg ('tag_id', __FUNCTION__);
1448 assertUIntArg ('parent_id', __FUNCTION__, TRUE);
1449 assertStringArg ('tag_name', __FUNCTION__);
1450 $tagname = trim ($_REQUEST['tag_name']);
1451 if (!validTagName ($tagname))
1452 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($tagname));
1453 if (($parent_id = $_REQUEST['parent_id']) <= 0)
1454 $parent_id = 'NULL';
1455 if (($ret = commitUpdateTag ($_REQUEST['tag_id'], $tagname, $parent_id)) == '')
1456 return buildRedirectURL (__FUNCTION__, 'OK', array ($tagname));
1457 // Use old name in the message, cause update failed.
1458 global $taglist;
1459 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($taglist[$_REQUEST['tag_id']]['tag'], $ret));
1460 }
1461
1462 $msgcode['rollTags']['OK'] = 67;
1463 $msgcode['rollTags']['ERR'] = 149;
1464 function rollTags ()
1465 {
1466 assertUIntArg ('row_id', __FUNCTION__);
1467 assertStringArg ('sum', __FUNCTION__, TRUE);
1468 assertUIntArg ('realsum', __FUNCTION__);
1469 if ($_REQUEST['sum'] != $_REQUEST['realsum'])
1470 return buildRedirectURL (__FUNCTION__, 'ERR');
1471 // Even if the user requested an empty tag list, don't bail out, but process existing
1472 // tag chains with "zero" extra. This will make sure, that the stuff processed will
1473 // have its chains refined to "normal" form.
1474 $extratags = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1475 $n_ok = 0;
1476 // Minimizing the extra chain early, so that tag rebuilder doesn't have to
1477 // filter out the same tag again and again. It will have own noise to cancel.
1478 $extrachain = getExplicitTagsOnly (buildTagChainFromIds ($extratags));
1479 foreach (listCells ('rack', $_REQUEST['row_id']) as $rack)
1480 {
1481 if (rebuildTagChainForEntity ('rack', $rack['id'], $extrachain))
1482 $n_ok++;
1483 amplifyCell ($rack);
1484 foreach ($rack['mountedObjects'] as $object_id)
1485 if (rebuildTagChainForEntity ('object', $object_id, $extrachain))
1486 $n_ok++;
1487 }
1488 return buildRedirectURL (__FUNCTION__, 'OK', array ($n_ok));
1489 }
1490
1491 $msgcode['changeMyPassword']['OK'] = 61;
1492 $msgcode['changeMyPassword']['ERR1'] = 150;
1493 $msgcode['changeMyPassword']['ERR2'] = 151;
1494 $msgcode['changeMyPassword']['ERR3'] = 152;
1495 $msgcode['changeMyPassword']['ERR4'] = 153;
1496 function changeMyPassword ()
1497 {
1498 global $remote_username, $user_auth_src;
1499 if ($user_auth_src != 'database')
1500 return buildRedirectURL (__FUNCTION__, 'ERR1');
1501 assertStringArg ('oldpassword', __FUNCTION__);
1502 assertStringArg ('newpassword1', __FUNCTION__);
1503 assertStringArg ('newpassword2', __FUNCTION__);
1504 $remote_userid = getUserIDByUsername ($remote_username);
1505 $userinfo = spotEntity ('user', $remote_userid);
1506 if ($userinfo['user_password_hash'] != sha1 ($_REQUEST['oldpassword']))
1507 return buildRedirectURL (__FUNCTION__, 'ERR2');
1508 if ($_REQUEST['newpassword1'] != $_REQUEST['newpassword2'])
1509 return buildRedirectURL (__FUNCTION__, 'ERR3');
1510 if (commitUpdateUserAccount ($remote_userid, $userinfo['user_name'], $userinfo['user_realname'], sha1 ($_REQUEST['newpassword1'])))
1511 return buildRedirectURL (__FUNCTION__, 'OK');
1512 else
1513 return buildRedirectURL (__FUNCTION__, 'ERR4');
1514 }
1515
1516 $msgcode['saveRackCode']['OK'] = 43;
1517 $msgcode['saveRackCode']['ERR1'] = 154;
1518 $msgcode['saveRackCode']['ERR2'] = 155;
1519 function saveRackCode ()
1520 {
1521 assertStringArg ('rackcode');
1522 // For the test to succeed, unescape LFs, strip CRs.
1523 $newcode = str_replace ('\r', '', str_replace ('\n', "\n", $_REQUEST['rackcode']));
1524 $parseTree = getRackCode ($newcode);
1525 if ($parseTree['result'] != 'ACK')
1526 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($parseTree['load']));
1527 saveScript ('RackCodeCache', '');
1528 if (saveScript ('RackCode', $newcode))
1529 return buildRedirectURL (__FUNCTION__, 'OK');
1530 else
1531 return buildRedirectURL (__FUNCTION__, 'ERR2');
1532 }
1533
1534 $msgcode['setPortVLAN']['ERR1'] = 156;
1535 // This handler's context is pre-built, but not authorized. It is assumed, that the
1536 // handler will take existing context and before each commit check authorization
1537 // on the base chain plus necessary context added.
1538 function setPortVLAN ()
1539 {
1540 assertUIntArg ('portcount', __FUNCTION__);
1541 $data = getSwitchVLANs ($_REQUEST['object_id']);
1542 if ($data === NULL)
1543 return buildRedirectURL (__FUNCTION__, 'ERR1');
1544 list ($vlanlist, $portlist) = $data;
1545 // Here we just build up 1 set command for the gateway with all of the ports
1546 // included. The gateway is expected to filter unnecessary changes silently
1547 // and to provide a list of responses with either error or success message
1548 // for each of the rest.
1549 $nports = $_REQUEST['portcount'];
1550 $prefix = 'set ';
1551 $log = emptyLog();
1552 $setcmd = '';
1553 for ($i = 0; $i < $nports; $i++)
1554 if
1555 (
1556 !isset ($_REQUEST['portname_' . $i]) ||
1557 !isset ($_REQUEST['vlanid_' . $i]) ||
1558 $_REQUEST['portname_' . $i] != $portlist[$i]['portname']
1559 )
1560 $log['m'][] = array ('c' => 158, 'a' => array ($i));
1561 elseif
1562 (
1563 $_REQUEST['vlanid_' . $i] == $portlist[$i]['vlanid'] ||
1564 $portlist[$i]['vlaind'] == 'TRUNK'
1565 )
1566 continue;
1567 else
1568 {
1569 $portname = $_REQUEST['portname_' . $i];
1570 $oldvlanid = $portlist[$i]['vlanid'];
1571 $newvlanid = $_REQUEST['vlanid_' . $i];
1572 // Finish the security context and evaluate it.
1573 $annex = array();
1574 $annex[] = array ('tag' => '$fromvlan_' . $oldvlanid);
1575 $annex[] = array ('tag' => '$tovlan_' . $newvlanid);
1576 if (!permitted (NULL, NULL, NULL, $annex))
1577 {
1578 $log['m'][] = array ('c' => 159, 'a' => array ($portname, $oldvlanid, $newvlanid));
1579 continue;
1580 }
1581 $setcmd .= $prefix . $portname . '=' . $newvlanid;
1582 $prefix = ';';
1583 }
1584 // Feed the gateway and interpret its (non)response.
1585 if ($setcmd != '')
1586 $log['m'] = array_merge ($log['m'], setSwitchVLANs ($_REQUEST['object_id'], $setcmd));
1587 else
1588 $log['m'][] = array ('c' => 201);
1589 return buildWideRedirectURL ($log);
1590 }
1591
1592 function submitSLBConfig ()
1593 {
1594 assertUIntArg ('object_id', __FUNCTION__);
1595 $newconfig = buildLVSConfig ($_REQUEST['object_id']);
1596 $msglog = gwSendFileToObject ($_REQUEST['object_id'], 'slbconfig', html_entity_decode ($newconfig, ENT_QUOTES, 'UTF-8'));
1597 return buildWideRedirectURL ($msglog);
1598 }
1599
1600 $msgcode['addRow']['OK'] = 74;
1601 $msgcode['addRow']['ERR'] = 100;
1602 function addRow ()
1603 {
1604 assertStringArg ('name', __FUNCTION__);
1605
1606 if (commitAddRow ($_REQUEST['name']) === TRUE)
1607 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1608 else
1609 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
1610 }
1611
1612 $msgcode['updateRow']['OK'] = 75;
1613 $msgcode['updateRow']['ERR'] = 100;
1614 function updateRow ()
1615 {
1616 assertUIntArg ('row_id', __FUNCTION__);
1617 assertStringArg ('name', __FUNCTION__);
1618
1619 if (TRUE === commitUpdateRow ($_REQUEST['row_id'], $_REQUEST['name']))
1620 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1621 else
1622 return buildRedirectURL (__FUNCTION__, 'ERR', array ($_REQUEST['name']));
1623 }
1624
1625 $msgcode['deleteRow']['OK'] = 77;
1626 $msgcode['deleteRow']['ERR'] = 100;
1627 function deleteRow ()
1628 {
1629 assertUIntArg ('row_id', __FUNCTION__);
1630 $rowinfo = getRackRowInfo ($_REQUEST['row_id']);
1631
1632 if (TRUE === commitDeleteRow ($_REQUEST['row_id']))
1633 return buildRedirectURL (__FUNCTION__, 'OK', array ($rowinfo['name']));
1634 else
1635 return buildRedirectURL (__FUNCTION__, 'ERR', array ($rowinfo['name']));
1636 }
1637
1638 $msgcode['addRack']['OK'] = 65;
1639 $msgcode['addRack']['ERR1'] = 171;
1640 $msgcode['addRack']['ERR2'] = 172;
1641 function addRack ()
1642 {
1643 assertUIntArg ('row_id', __FUNCTION__);
1644 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
1645 if (isset ($_REQUEST['got_data']))
1646 {
1647 assertStringArg ('rack_name', __FUNCTION__);
1648 assertUIntArg ('rack_height1', __FUNCTION__);
1649 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1650
1651 if (commitAddRack ($_REQUEST['rack_name'], $_REQUEST['rack_height1'], $_REQUEST['row_id'], $_REQUEST['rack_comment'], $taglist) === TRUE)
1652 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
1653 else
1654 return buildRedirectURL (__FUNCTION__, 'ERR1', array ($_REQUEST['rack_name']));
1655 }
1656 elseif (isset ($_REQUEST['got_mdata']))
1657 {
1658 assertUIntArg ('rack_height2', __FUNCTION__);
1659 assertStringArg ('rack_names', __FUNCTION__, TRUE);
1660 $log = emptyLog();
1661 // copy-and-paste from renderAddMultipleObjectsForm()
1662 $names1 = explode ('\n', $_REQUEST['rack_names']);
1663 $names2 = array();
1664 foreach ($names1 as $line)
1665 {
1666 $parts = explode ('\r', $line);
1667 reset ($parts);
1668 if (!strlen ($parts[0]))
1669 continue;
1670 else
1671 $names2[] = rtrim ($parts[0]);
1672 }
1673 global $msgcode;
1674 foreach ($names2 as $cname)
1675 if (commitAddRack ($cname, $_REQUEST['rack_height2'], $_REQUEST['row_id'], '', $taglist) === TRUE)
1676 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['OK'], 'a' => array ($cname));
1677 else
1678 $log['m'][] = array ('c' => $msgcode[__FUNCTION__]['ERR1'], 'a' => array ($cname));
1679 return buildWideRedirectURL ($log);
1680 }
1681 else
1682 return buildRedirectURL (__FUNCTION__, 'ERR2');
1683 }
1684
1685 $msgcode['deleteRack']['OK'] = 79;
1686 $msgcode['deleteRack']['ERR'] = 100;
1687 $msgcode['deleteRack']['ERR1'] = 206;
1688 function deleteRack ()
1689 {
1690 assertUIntArg ('rack_id', __FUNCTION__);
1691 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1692 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1693 amplifyCell ($rackData);
1694 if (count ($rackData['mountedObjects']))
1695 return buildRedirectURL (__FUNCTION__, 'ERR1');
1696 if (TRUE !== commitDeleteRack ($_REQUEST['rack_id']))
1697 return buildRedirectURL (__FUNCTION__, 'ERR', array(), 'rackspace', 'default');
1698 return buildRedirectURL (__FUNCTION__, 'OK', array ($rackData['name']), 'rackspace', 'default');
1699 }
1700
1701 $msgcode['updateRack']['OK'] = 68;
1702 $msgcode['updateRack']['ERR'] = 177;
1703 function updateRack ()
1704 {
1705 assertUIntArg ('rack_id', __FUNCTION__);
1706 assertUIntArg ('rack_row_id', __FUNCTION__);
1707 assertUIntArg ('rack_height', __FUNCTION__);
1708 assertStringArg ('rack_name', __FUNCTION__);
1709 assertStringArg ('rack_comment', __FUNCTION__, TRUE);
1710
1711 resetThumbCache ($_REQUEST['rack_id']);
1712 if (TRUE === commitUpdateRack ($_REQUEST['rack_id'], $_REQUEST['rack_name'], $_REQUEST['rack_height'], $_REQUEST['rack_row_id'], $_REQUEST['rack_comment']))
1713 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['rack_name']));
1714 else
1715 return buildRedirectURL (__FUNCTION__, 'ERR');
1716 }
1717
1718 $msgcode['updateRackDesign']['ERR'] = 100;
1719 function updateRackDesign ()
1720 {
1721 assertUIntArg ('rack_id', __FUNCTION__);
1722 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1723 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1724 amplifyCell ($rackData);
1725 applyRackDesignMask($rackData);
1726 markupObjectProblems ($rackData);
1727 $response = processGridForm ($rackData, 'A', 'F');
1728 return buildWideRedirectURL (array($response));
1729 }
1730
1731 $msgcode['updateRackProblems']['ERR'] = 100;
1732 function updateRackProblems ()
1733 {
1734 assertUIntArg ('rack_id', __FUNCTION__);
1735 if (NULL == ($rackData = spotEntity ('rack', $_REQUEST['rack_id'])))
1736 return buildRedirectURL (__FUNCTION__, 'ERR', array ('Rack not found'), 'rackspace', 'default');
1737 amplifyCell ($rackData);
1738 applyRackProblemMask($rackData);
1739 markupObjectProblems ($rackData);
1740 $response = processGridForm ($rackData, 'F', 'U');
1741 return buildWideRedirectURL (array($response));
1742 }
1743
1744 function querySNMPData ()
1745 {
1746 assertUIntArg ('object_id', __FUNCTION__);
1747 assertStringArg ('community', __FUNCTION__);
1748 return buildWideRedirectURL (doSNMPmining ($_REQUEST['object_id'], $_REQUEST['community']));
1749 }
1750
1751 $msgcode['addFileWithoutLink']['OK'] = 69;
1752 $msgcode['addFileWithoutLink']['ERR'] = 100;
1753 // File-related functions
1754 function addFileWithoutLink ()
1755 {
1756 assertStringArg ('comment', __FUNCTION__, TRUE);
1757
1758 // Make sure the file can be uploaded
1759 if (get_cfg_var('file_uploads') != 1)
1760 return buildRedirectURL (__FUNCTION__, 'ERR', array ("file uploads not allowed, change 'file_uploads' parameter in php.ini"));
1761
1762 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1763 global $sic;
1764 // commitAddFile() uses prepared statements
1765 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
1766
1767 if ($error != '')
1768 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1769
1770 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
1771 }
1772
1773 $msgcode['addFileToEntity']['OK'] = 69;
1774 $msgcode['addFileToEntity']['ERR1'] = 187;
1775 $msgcode['addFileToEntity']['ERR2'] = 181;
1776 $msgcode['addFileToEntity']['ERR3'] = 182;
1777 function addFileToEntity ()
1778 {
1779 global $page, $pageno, $etype_by_pageno;
1780 if (!isset ($etype_by_pageno[$pageno]) or !isset ($page[$pageno]['bypass']))
1781 return buildRedirectURL (__FUNCTION__, 'ERR1', array (__FUNCTION__));
1782 $realm = $etype_by_pageno[$pageno];
1783 $bypass = $page[$pageno]['bypass'];
1784 assertUIntArg ($bypass, __FUNCTION__);
1785 $entity_id = $_REQUEST[$bypass];
1786 assertStringArg ('comment', __FUNCTION__, TRUE);
1787
1788 // Make sure the file can be uploaded
1789 if (get_cfg_var('file_uploads') != 1)
1790 return buildRedirectURL (__FUNCTION__, 'ERR2');
1791
1792 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1793 global $sic;
1794 // commitAddFile() uses prepared statements
1795 $error = commitAddFile ($_FILES['file']['name'], $_FILES['file']['type'], $_FILES['file']['size'], $fp, $sic['comment']);
1796 if ($error != '')
1797 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
1798
1799 $error = commitLinkFile (lastInsertID(), $realm, $entity_id);
1800 if ($error != '')
1801 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
1802
1803 return buildRedirectURL (__FUNCTION__, 'OK', array ($_FILES['file']['name']));
1804 }
1805
1806 $msgcode['linkFileToEntity']['OK'] = 71;
1807 $msgcode['linkFileToEntity']['ERR1'] = 178;
1808 $msgcode['linkFileToEntity']['ERR2'] = 100;
1809 function linkFileToEntity ()
1810 {
1811 assertUIntArg ('file_id', __FUNCTION__);
1812 global $page, $pageno, $etype_by_pageno;
1813 $entity_type = $etype_by_pageno[$pageno];
1814 $bypass_name = $page[$pageno]['bypass'];
1815 assertUIntArg ($bypass_name, __FUNCTION__);
1816
1817 $fi = spotEntity ('file', $_REQUEST['file_id']);
1818 if ($fi === NULL)
1819 return buildRedirectURL (__FUNCTION__, 'ERR1'); // file not found
1820 $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
1821 if ($error != '')
1822 return buildRedirectURL (__FUNCTION__, 'ERR2', array ($error)); // linking failed
1823
1824 return buildRedirectURL (__FUNCTION__, 'OK', array ($fi['name']));
1825 }
1826
1827 $msgcode['replaceFile']['OK'] = 70;
1828 $msgcode['replaceFile']['ERR1'] = 181;
1829 $msgcode['replaceFile']['ERR2'] = 207;
1830 $msgcode['replaceFile']['ERR3'] = 182;
1831 function replaceFile ()
1832 {
1833 global $sic;
1834 assertUIntArg ('file_id', __FUNCTION__);
1835
1836 // Make sure the file can be uploaded
1837 if (get_cfg_var('file_uploads') != 1)
1838 return buildRedirectURL (__FUNCTION__, 'ERR1');
1839 $shortInfo = spotEntity ('file', $sic['file_id']);
1840
1841 $fp = fopen($_FILES['file']['tmp_name'], 'rb');
1842 if ($fp === FALSE)
1843 return buildRedirectURL (__FUNCTION__, 'ERR2');
1844 $error = commitReplaceFile ($sic['file_id'], $fp);
1845 if ($error != '')
1846 return buildRedirectURL (__FUNCTION__, 'ERR3', array ($error));
1847
1848 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
1849 }
1850
1851 $msgcode['updateFile']['OK'] = 70;
1852 $msgcode['updateFile']['ERR'] = 100;
1853 function updateFile ()
1854 {
1855 assertUIntArg ('file_id', __FUNCTION__);
1856 assertStringArg ('file_name', __FUNCTION__);
1857 assertStringArg ('file_type', __FUNCTION__);
1858 assertStringArg ('file_comment', __FUNCTION__, TRUE);
1859 // prepared statement params below
1860 global $sic;
1861 $error = commitUpdateFile ($sic['file_id'], $sic['file_name'], $sic['file_type'], $sic['file_comment']);
1862 if ($error != '')
1863 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1864
1865 return buildRedirectURL (__FUNCTION__, 'OK', array ($_REQUEST['name']));
1866 }
1867
1868 $msgcode['unlinkFile']['OK'] = 72;
1869 $msgcode['unlinkFile']['ERR'] = 182;
1870 function unlinkFile ()
1871 {
1872 assertUIntArg ('link_id', __FUNCTION__);
1873 $error = commitUnlinkFile ($_REQUEST['link_id']);
1874
1875 if ($error != '')
1876 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1877
1878 return buildRedirectURL (__FUNCTION__, 'OK');
1879 }
1880
1881 $msgcode['deleteFile']['OK'] = 73;
1882 $msgcode['deleteFile']['ERR'] = 100;
1883 function deleteFile ()
1884 {
1885 assertUIntArg ('file_id', __FUNCTION__);
1886 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
1887 $error = commitDeleteFile ($_REQUEST['file_id']);
1888
1889 if ($error != '')
1890 return buildRedirectURL (__FUNCTION__, 'ERR', array ($error));
1891
1892 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
1893 }
1894
1895 $msgcode['updateFileText']['OK'] = 78;
1896 $msgcode['updateFileText']['ERR1'] = 179;
1897 $msgcode['updateFileText']['ERR2'] = 180;
1898 function updateFileText ()
1899 {
1900 assertUIntArg ('file_id', __FUNCTION__);
1901 assertStringArg ('mtime_copy', __FUNCTION__);
1902 assertStringArg ('file_text', __FUNCTION__, TRUE); // it's Ok to save empty
1903 $shortInfo = spotEntity ('file', $_REQUEST['file_id']);
1904 if ($shortInfo['mtime'] != $_REQUEST['mtime_copy'])
1905 return buildRedirectURL (__FUNCTION__, 'ERR1');
1906 global $sic;
1907 $error = commitReplaceFile ($sic['file_id'], $sic['file_text']);
1908 if ($error == '')
1909 return buildRedirectURL (__FUNCTION__, 'OK', array (htmlspecialchars ($shortInfo['name'])));
1910 return buildRedirectURL (__FUNCTION__, 'ERR2');
1911 }
1912
1913 ?>