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