4 require_once 'plc_login.php';
6 // Get session and API handles
7 require_once 'plc_session.php';
8 require_once 'plc_api.php';
9 global $plc, $api, $adm;
12 require_once 'plc_drupal.php';
15 require_once 'plc_functions.php';
17 $known_actions = array();
18 ////////////////////////////////////////////////////////////
21 // (*) set 'action' to one of the following
22 //////////////////////////////////////// persons
23 $known_actions[] = "add-person-to-site";
24 // expects: person_id & site_id
25 $known_actions[] = "remove-person-from-sites";
26 // expects: person_id & site_ids
27 $known_actions[] = "remove-roles-from-person";
28 // expects: person_id & role_ids
29 $known_actions[] = "add-role-to-person";
30 // expects: role_person_id & id
31 $known_actions[] = "enable-person";
33 $known_actions[] = "disable-person";
35 $known_actions[] = "become-person";
37 $known_actions[] = "delete-person";
39 $known_actions[] = "delete-keys";
40 // expects: key_ids & person_id (for redirecting to the person's page)
41 $known_actions[] = "upload-key";
42 // expects: person_id & $_FILES['key']
43 $known_actions[] = "update-person";
44 // expects: person_id & first_name last_name title email phone url bio + [password1 password2]
46 //////////////////////////////////////// nodes
47 $known_actions[] = "node-boot-state";
48 // expects: node_id boot_state
49 $known_actions[] = "delete-node";
51 $known_actions[] = "update-node";
52 // expects: node_id, hostname, model, node_type
53 $known_actions[] = "attach-pcu";
54 // expects: node_id, pcu_id, port (pcu_id <0 means detach)
55 $known_actions[] = "reboot-node-with-pcu";
58 //////////////////////////////////////// interfaces
59 $known_actions[] = "delete-interfaces";
60 // expects: interface_ids
61 $known_actions[] = "add-interface";
62 // expects: node_id & interface details
63 $known_actions[] = "new-interface";
65 $known_actions[] = "update-interface";
66 // expects: interface_id & interface details
68 //////////////////////////////////////// sites
69 $known_actions[] = "delete-site";
71 $known_actions[] = "expire-all-slices-in-site";
73 $known_actions[] = "update-site";
74 // expects: site_id & name abbreviated_name url latitude longitude [login_base max_slices]
76 //////////////////////////////////////// slices
77 $known_actions[] = "delete-slice";
79 $known_actions[] = "update-slice";
80 // expects: slice_id, name, description, url
81 $known_actions[] = "renew-slice";
82 // expects: slice_id & expires
83 $known_actions[] = 'remove-persons-from-slice';
84 // expects: slice_id & person_ids
85 $known_actions[] = 'add-persons-in-slice';
86 // expects: slice_id & person_ids
87 $known_actions[] = 'remove-nodes-from-slice';
88 // expects: slice_id & node_ids
89 $known_actions[] = 'add-nodes-in-slice';
90 // expects: slice_id & node_ids
91 $known_actions[] = 'update-initscripts';
92 // expects: slice_id & name & previous-initscript & previous-initscript-code
93 // & initscript & initscript-code
94 $known_actions[] = 'delete-slice-tags';
95 // expects: slice_tag_id
96 $known_actions[] = 'add-slice-tag';
97 // expects: slice_id & tag_type_id & node_id & nodegroup_id
99 //////////////////////////////////////// tag types
100 $known_actions[] = "update-tag-type";
101 // expects: tag_type_id & name & description & category & min_role_id
102 $known_actions[] = "add-tag-type";
103 // expects: tag_type_id & tagname & description & category & min_role_id
104 $known_actions[] = "delete-tag-types";
105 // expects: tag_type_ids
106 $known_actions[] = "remove-roles-from-tag-type";
107 // expects: tag_type_id & role_ids
108 $known_actions[] = "add-role-to-tag-type";
109 // expects: tag_type_id_id & id
111 //////////////////////////////////////// tags
112 $known_actions[] = "set-tag-on-node";
113 // expects: node_id tagname value
114 $known_actions[] = "set-tag-on-site";
115 // expects: site_id tagname value
116 $known_actions[] = "set-tag-on-person";
117 // expects: person_id tagname value
118 $known_actions[] = "set-tag-on-interface";
119 // expects: interface_id tagname value
120 $known_actions[] = "delete-node-tags";
121 // expects: node_id & node_tag_ids
122 $known_actions[] = "delete-site-tags";
123 // expects: site_id & site_tag_ids
124 $known_actions[] = "delete-person-tags";
125 // expects: person_id & person_tag_ids
126 $known_actions[] = "delete-interface-tags";
127 // expects: interface_id & interface_tag_ids
129 //////////////////////////////////////// nodegroups
130 $known_actions[] = "update-nodegroup";
131 // expects nodegroup_id groupname value
132 $known_actions[] = "add-nodegroup";
133 // expects groupname, tag_type_id, value
134 $known_actions[] = 'delete-nodegroups';
135 // expects nodegroup_ids
137 //////////////////////////////////////// leases
138 $known_actions[] = "manage-leases";
139 // expects as 'actions' a list of 'action' of the form
140 // either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
141 // or [ 'delete-leases', lease_id ]
143 ////////////////////////////////////////////////////////////
144 $interface_details = array('method', 'type', 'ip', 'gateway', 'network',
145 'broadcast', 'netmask', 'dns1', 'dns2',
146 'hostname', 'mac', 'bwlimit');
148 //////////////////////////////
149 // sometimes we don't set 'action', but use the submit button name instead
150 // so if 'action' not set, see if $_POST has one of the actions as a key
151 if ($_POST['action']) {
152 $action = $_POST['action'];
154 foreach ($known_actions as $known_action) {
155 if ($_POST[$known_action]) {
156 $action = $known_action;
162 //uncomment for debugging incoming data
165 $person_id = $_POST['person_id']; // usually needed
168 drupal_set_message("actions.php: action not set or not in known_actions");
169 plc_debug('POST', $_POST);
175 case 'add-person-to-site':{
176 $site_id = $_POST['site_id'];
177 $api->AddPersonToSite(intval($person_id), intval($site_id));
178 plc_redirect(l_person($person_id));
181 case 'remove-person-from-sites':{
182 $site_ids = $_POST['site_ids'];
184 drupal_set_message("action=$action - No site selected");
187 foreach ($site_ids as $site_id) {
188 $api->DeletePersonFromSite(intval($person_id), intval($site_id));
190 plc_redirect(l_person($person_id));
193 case 'remove-roles-from-person':{
194 $role_ids = $_POST['role_ids'];
196 drupal_set_error("You have not selected role(s) to remove");
198 foreach ($role_ids as $role_id) {
199 if ($api->DeleteRoleFromPerson(intval($role_id), intval($person_id)) != 1) {
200 drupal_set_error("Could not remove role $role_id from person $person_id");
205 plc_redirect(l_person_roles($person_id));
208 case 'add-role-to-person':{
209 $role_id = $_POST['role_id'];
211 drupal_set_error("You have not selected a role to add");
212 } else if ($api->AddRoleToPerson(intval($role_id), intval($person_id)) != 1) {
213 drupal_set_error("Could not add role $role_id to person $person_id");
215 plc_redirect(l_person_roles($person_id));
218 case 'enable-person':{
219 $fields = array("enabled" => true);
220 $api->UpdatePerson(intval($person_id), $fields);
221 plc_redirect(l_person($person_id));
224 case 'disable-person':{
225 $fields = array("enabled" => false);
226 $api->UpdatePerson(intval($person_id), $fields);
227 plc_redirect(l_person($person_id));
230 case 'become-person':{
231 $plc->BecomePerson(intval($person_id));
232 plc_redirect(l_person(intval($person_id)));
235 case 'delete-person':{
236 $api->DeletePerson(intval($person_id));
237 plc_redirect(l_persons());
241 $key_ids = $_POST['key_ids'];
243 drupal_set_message("action=$action - No key selected");
248 foreach ($key_ids as $key_id) {
249 if ($api->DeleteKey(intval($key_id)) != 1) {
257 drupal_set_message("Deleted $counter key(s)");
259 drupal_set_error("Could not delete all selected keys, only $counter were removed");
262 plc_redirect(l_person($person_id));
266 if (!isset($_FILES['key'])) {
267 drupal_set_message("action=$action, no key file set");
271 $key_file = $_FILES['key']['tmp_name'];
273 plc_error("Please select a valid SSH key file to upload");
276 $fp = fopen($key_file, "r");
279 plc_error("Unable to open key file $key_file");
282 // opened the key file, read the one line of contents
283 // The POST operation always creates a file even if the filename
284 // the user specified was garbage. If there was some problem
285 // with the source file, we'll get a zero length read here.
286 $key = fread($fp, filesize($key_file));
289 $key_id = $api->AddPersonKey(intval($person_id), array("key_type" => 'ssh', "key" => $key));
292 drupal_set_message("New key added");
294 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
297 plc_redirect(l_person($person_id));
300 case 'update-person':{
301 $person_id = $_POST['person_id'];
302 // attempt to update this person
303 $first_name = $_POST['first_name'];
304 $last_name = $_POST['last_name'];
305 $title = $_POST['title'];
306 $email = $_POST['email'];
307 $phone = $_POST['phone'];
308 $url = $_POST['url'];
309 $bio = str_replace("\r", "", $_POST['bio']);
310 $password1 = $_POST['password1'];
311 $password2 = $_POST['password2'];
313 if ($password1 != $password2) {
314 drupal_set_error("The passwords do not match");
315 plc_redirect(l_person($person_id));
319 $fields['first_name'] = $first_name;
320 $fields['last_name'] = $last_name;
321 $fields['title'] = $title;
322 $fields['email'] = $email;
323 $fields['phone'] = $phone;
324 $fields['url'] = $url;
325 $fields['bio'] = $bio;
327 if ($password1 != "") {
328 $fields['password'] = $password1;
331 if ($api->UpdatePerson(intval($person_id), $fields) == 1) {
332 drupal_set_message("$first_name $last_name updated");
334 drupal_set_error("Could not update person $person_id" . $api->error());
337 plc_redirect(l_person($person_id));
341 //////////////////////////////////////////////////////////// nodes
342 case 'node-boot-state':{
343 $node_id = intval($_POST['node_id']);
344 $boot_state = $_POST['boot_state'];
345 $result = $api->UpdateNode($node_id, array("boot_state" => $boot_state));
347 drupal_set_message("boot state updated");
348 plc_redirect(l_node($node_id));
350 drupal_set_error("Could not set boot_state '$boot_state'");
356 $node_id = intval($_POST['node_id']);
357 $result = $api->DeleteNode(intval($node_id));
359 drupal_set_message("Node $node_id deleted");
360 plc_redirect(l_nodes());
362 drupal_set_error("Could not delete node $node_id");
368 $node_id = intval($_POST['node_id']);
369 $hostname = $_POST['hostname'];
370 $model = $_POST['model'];
371 $node_type = $_POST['node_type'];
373 $fields = array("hostname" => $hostname, "model" => $model, "node_type" => $node_type);
374 $api->UpdateNode($node_id, $fields);
375 $error = $api->error();
378 drupal_set_message("Update node $hostname");
379 plc_redirect(l_node($node_id));
381 drupal_set_error($error);
386 // this code will ensure that at most one PCU gets attached to the node
388 $node_id = intval($_POST['node_id']);
389 $pcu_id = intval($_POST['pcu_id']);
390 $port = intval($_POST['port']);
391 // always start with deleting former PCUs
392 $nodes = $api->GetNodes(array($node_id), array('pcu_ids'));
393 $former_pcu_ids = $nodes[0]['pcu_ids'];
394 if ($former_pcu_ids) {
395 foreach ($former_pcu_ids as $former_pcu_id) {
396 if ($api->DeleteNodeFromPCU($node_id, $former_pcu_id) == 1) {
397 drupal_set_message('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
399 drupal_set_error('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
405 // re-attach only if provided pcu_id >=0
407 if ($api->AddNodeToPCU($node_id, $pcu_id, $port) == 1) {
408 drupal_set_message('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
410 drupal_set_error('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
414 drupal_set_message('Detached node from all PCUs');
417 plc_redirect(l_node($node_id));
421 case 'reboot-node-with-pcu':{
422 $node_id = intval($_POST['node_id']);
423 $hostname = $_POST['hostname'];
424 $test = $_POST['test'];
425 settype($test, "boolean");
427 $ret = $api->RebootNodeWithPCU($node_id, $test);
428 $error = $api->error();
431 drupal_set_message("Reboot node $hostname: $ret");
432 plc_redirect(l_node($node_id));
434 drupal_set_error($error);
439 //////////////////////////////////////////////////////////// interfaces
440 case 'delete-interfaces':{
441 $interface_ids = $_POST['interface_ids'];
442 if (!$interface_ids) {
443 drupal_set_message("action=$action - No interface selected");
448 foreach ($interface_ids as $interface_id) {
449 if ($api->DeleteInterface(intval($interface_id)) != 1) {
457 drupal_set_message("Deleted $counter interface(s)");
459 drupal_set_error("Could not delete all selected interfaces, only $counter were removed");
462 plc_redirect(l_node($_POST['node_id']));
465 case 'new-interface':{
466 plc_redirect(l_interface_add($_POST['node_id']));
469 case 'add-interface':{
470 $node_id = intval($_POST['node_id']);
471 foreach ($interface_details as $field) {
472 $interface[$field] = $_POST[$field];
473 // these must be integers
474 if (in_array($field, array('bwlimit', 'node_id'))) {
475 if (empty($interface[$field])) {
476 unset($interface[$field]);
478 $interface[$field] = intval($interface[$field]);
483 $interface_id = $api->AddInterface($node_id, $interface);
484 if ($interface_id <= 0) {
485 drupal_set_error("Could not create interface");
486 drupal_set_error($api->error());
488 $ip = $interface['ip'];
489 drupal_set_message("Interface $ip added into node $node_id");
490 if ($_POST['is-virtual']) {
491 $ifname = $_POST['ifname'];
492 if ($api->AddInterfaceTag($interface_id, "ifname", $ifname) <= 0) {
493 drupal_set_error("Could not set tag 'ifname'=$ifname");
495 drupal_set_message("Set tag 'ifname'=$ifname");
498 $alias = $_POST['alias'];
499 // deafult to interface_id
501 $alias = strval($interface_id);
504 if ($api->AddInterfaceTag($interface_id, "alias", $alias) <= 0) {
505 drupal_set_error("Could not set tag 'alias'=$alias");
507 drupal_set_message("Set tag 'alias'=$alias");
512 plc_redirect(l_node_interfaces($node_id));
515 case 'update-interface':{
516 $interface_id = $_POST['interface_id'];
517 foreach ($interface_details as $field) {
518 $interface[$field] = $_POST[$field];
519 if (in_array($field, array('bwlimit', 'node_id'))) {
520 if (intval($interface[$field]) != 0) {
521 $interface[$field] = intval($interface[$field]);
522 } elseif ($field == 'bwlimit') {
523 $interface[$field] = null;
527 $result = $api->UpdateInterface(intval($interface_id), $interface);
529 drupal_set_message("Interface $interface_id updated");
531 drupal_set_error("Could not update interface: " . $api->error());
534 plc_redirect(l_interface($interface_id));
537 //////////////////////////////////////////////////////////// sites
539 $site_id = intval($_POST['site_id']);
540 if ($api->DeleteSite($site_id) == 1) {
541 drupal_set_message("Site $site_id deleted");
543 drupal_set_error("Failed to delete site $site_id");
546 plc_redirect(l_sites());
550 case 'expire-all-slices-in-site':{
552 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
555 //// old code from sites/expire.php
556 $sites = $api->GetSites(array(intval($site_id)));
558 // xxx why not 'now?'
559 $expiration = strtotime($_POST['expires']);
560 // loop through all slices for site
561 foreach ($site['slice_ids'] as $slice_id) {
562 $api->UpdateSlice($slice_id, array("expires" => $expiration));
564 // update site to not allow slice creation or renewal
565 $api->UpdateSite($site_id, array("max_slices" => 0));
566 plc_redirect(l_site($site_id));
571 $site_id = intval($_POST['site_id']);
572 $name = $_POST['name'];
573 $abbreviated_name = $_POST['abbreviated_name'];
574 $url = $_POST['url'];
575 $latitude = floatval($_POST['latitude']);
576 $longitude = floatval($_POST['longitude']);
577 //$max_slivers= $_POST['max_slivers'];
579 $fields = array("name" => $name,
580 "abbreviated_name" => $abbreviated_name,
582 "latitude" => floatval($latitude),
583 "longitude" => floatval($longitude));
585 if ($_POST['login_base']) {
586 $fields['login_base'] = $_POST['login_base'];
589 if (isset($_POST['max_slices'])) {
590 $fields['max_slices'] = intval($_POST['max_slices']);
593 if (isset($_POST['enabled'])) {
594 $fields['enabled'] = (bool) $_POST['enabled'];
597 $retcod = $api->UpdateSite(intval($site_id), $fields);
599 drupal_set_message("Site $name updated");
601 drupal_set_error("Could not update site $site_id");
604 plc_redirect(l_site($site_id));
608 //////////////////////////////////////////////////////////// slices
609 case 'delete-slice':{
610 $slice_id = $_POST['slice_id'];
611 if ($api->DeleteSlice(intval($slice_id)) == 1) {
612 drupal_set_message("Slice $slice_id deleted");
613 plc_redirect(l_slices());
615 drupal_set_error("Could not delete slice $slice_id " . $api->error());
620 case 'update-slice':{
621 $slice_id = $_POST['slice_id'];
622 $name = $_POST['name'];
623 $description = $_POST['description'];
624 $url = $_POST['url'];
626 $fields = array("description" => $description, "url" => $url);
627 $api->UpdateSlice(intval($slice_id), $fields);
628 $error = $api->error();
631 drupal_set_message("Update slice $name");
632 plc_redirect(l_slice($slice_id));
634 drupal_set_error($error);
639 case 'renew-slice': {
640 $slice_id = intval($_POST['slice_id']);
641 $expires = intval($_POST['expires']);
645 $WEEK = 7 * 24 * 3600;
647 $MAX_FUTURE = $WEEKS * $WEEK;
648 if (($expires - $now) > $MAX_FUTURE) {
649 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
650 plc_redirect(l_slice($slice_id));
652 if ($api->UpdateSlice($slice_id, array('expires' => $expires)) == 1) {
653 drupal_set_message("Slice renewed");
655 drupal_set_error("Could not update slice $slice_id");
658 plc_redirect(l_slice($slice_id));
662 case 'remove-persons-from-slice':{
663 $slice_id = intval($_POST['slice_id']);
664 $person_ids = $_POST['person_ids'];
665 $return_url = $_POST['return_url'];
668 $return_url = l_slice($slice_id) . "&show_persons=true";
672 $tmp_slices = $api->GetSlices($slice_id, array("name"));
673 if (count($tmp_slices) > 0) {
674 $tmp_slice = $tmp_slices[0];
675 $slice_name = $tmp_slice["name"];
677 $notify_subject = "Removed from slice: " . $slice_name;
678 $notify_body = sprintf("You have been removed from the slice %s.
680 Our support team will be glad to answer any question that you might have.
682 $notify_person_ids = array();
686 foreach ($person_ids as $person_id) {
687 if ($api->DeletePersonFromSlice(intval($person_id), $slice_id) != 1) {
690 array_push($notify_person_ids, intval($person_id));
695 $adm->NotifyPersons($notify_person_ids, $notify_subject, $notify_body);
696 drupal_set_message("Deleted $counter person(s)");
698 drupal_set_error("Could not delete all selected persons, only $counter were removed");
701 plc_redirect($return_url);
705 case 'add-persons-in-slice':{
706 $slice_id = intval($_POST['slice_id']);
707 $person_ids = $_POST['person_ids'];
708 $return_url = $_POST['return_url'];
711 $return_url = l_slice($slice_id) . "&show_persons=true";
715 $tmp_slices = $api->GetSlices($slice_id, array("name"));
716 if (count($tmp_slices) > 0) {
717 $tmp_slice = $tmp_slices[0];
718 $slice_name = $tmp_slice["name"];
720 $notify_subject = "Added to slice: " . $slice_name;
721 $notify_body = sprintf("You have been added to the slice %s as a user.
723 You can go to your slice page following the link below:
724 https://%s:%d/db/slices/index.php?id=%d
726 Our support team will be glad to answer any question that you might have.
727 ", $slice_name, PLC_WWW_HOST, PLC_WWW_SSL_PORT, $slice_id);
728 $notify_person_ids = array();
732 foreach ($person_ids as $person_id) {
733 if ($api->AddPersonToSlice(intval($person_id), $slice_id) != 1) {
736 array_push($notify_person_ids, intval($person_id));
741 $adm->NotifyPersons($notify_person_ids, $notify_subject, $notify_body);
742 drupal_set_message("Added $counter person(s)");
744 drupal_set_error("Could not add all selected persons, only $counter were added");
747 plc_redirect($return_url);
751 case 'remove-nodes-from-slice':{
752 $slice_id = intval($_POST['slice_id']);
753 $node_ids = array_map("intval", $_POST['node_ids']);
754 $count = count($node_ids);
756 if ($api->DeleteSliceFromNodes($slice_id, $node_ids) == 1) {
757 drupal_set_message("Removed $count node(s)");
759 drupal_set_error("Could not remove selected nodes");
762 plc_redirect(l_slice_nodes($slice_id));
766 case 'add-nodes-in-slice':{
767 $slice_id = intval($_POST['slice_id']);
768 $node_ids = array_map("intval", $_POST['node_ids']);
769 $count = count($node_ids);
770 if ($api->AddSliceToNodes($slice_id, $node_ids) == 1) {
771 drupal_set_message("Added $count node(s)");
773 drupal_set_error("Could not add all selected nodes");
776 plc_redirect(l_slice_nodes($slice_id));
780 case 'update-initscripts':{
781 // expects: slice_id & name & previous-initscript & previous-initscript-code
782 // & initscript & initscript-code
783 $slice_id = intval($_POST['slice_id']);
784 $previous_initscript = $_POST['previous-initscript'];
785 $initscript = $_POST['initscript'];
786 $previous_initscript_code = html_entity_decode($_POST['previous-initscript-code']);
787 $initscript_code = $_POST['initscript-code'];
790 if (strcmp($initscript, $previous_initscript) != 0) {
791 $newvalue = $api->SetSliceInitscript($slice_id, $initscript);
792 $status = (strcmp($newvalue, $initscript) == 0) ? "OK" : "failed";
794 drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
796 drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
802 // somehow some \r chars make it here; just ignore them
803 $previous_initscript_code = str_replace("\r", "", $previous_initscript_code);
804 // plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
806 $initscript_code = str_replace("\r", "", $initscript_code);
807 // make sure the script ends with a single \n
808 $initscript_code = trim($initscript_code);
809 if (!empty($initscript_code) && $initscript_code[strlen($initscript_code) - 1] != "\n") {
810 $initscript_code .= "\n";
813 // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
815 if (strcmp($initscript_code, $previous_initscript_code) != 0) {
816 $newvalue = $api->SetSliceInitscriptCode($slice_id, $initscript_code);
817 // plc_debug_txt('newvalue',$newvalue);
818 $status = (strcmp($newvalue, $initscript_code) == 0) ? "OK" : "failed";
819 if (!$initscript_code) {
820 drupal_set_message("Removed initscript code " . $status);
822 drupal_set_message("Installed new initscript code " . $status);
828 drupal_set_message("No changes required in initscript");
831 plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1");
835 case 'delete-slice-tags':{
836 $slice_id = intval($_POST['slice_id']);
837 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
840 foreach ($slice_tag_ids as $slice_tag_id) {
841 if ($api->DeleteSliceTag($slice_tag_id)) {
844 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
849 drupal_set_message("Deleted $count slice tag(s)");
852 plc_redirect(l_slice($slice_id) . "&show_tags=1");
856 case 'add-slice-tag':{
857 $slice_id = intval($_POST['slice_id']);
858 $tag_type_id = intval($_POST['tag_type_id']);
859 $value = $_POST['value'];
860 $node_id = intval($_POST['node_id']);
861 $nodegroup_id = intval($_POST['nodegroup_id']);
865 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
866 } elseif ($nodegroup_id) {
867 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
869 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
872 drupal_set_message("Added slice tag.");
874 drupal_set_error("Could not add slice tag");
877 if ($_POST['sliver_action']) {
878 plc_redirect(l_sliver($node_id, $slice_id));
880 plc_redirect(l_slice($slice_id) . "&show_tags=true");
886 //////////////////////////////////////////////////////////// tag types
888 case 'update-tag-type':{
890 $tag_type_id = intval($_POST['tag_type_id']);
891 $tagname = $_POST['tagname'];
892 $min_role_id = intval($_POST['min_role_id']);
893 $description = $_POST['description'];
894 $category = $_POST['category'];
896 // make tag_type_fields dict
897 $tag_type_fields = array("min_role_id" => $min_role_id,
898 "tagname" => $tagname,
899 "description" => $description,
900 "category" => $category,
903 if ($api->UpdateTagType($tag_type_id, $tag_type_fields) == 1) {
904 drupal_set_message("Tag type $tagname updated");
906 drupal_set_error("Could not update tag type $tag_type_id\n" . $api->error());
909 plc_redirect(l_tag($tag_type_id));
913 case 'add-tag-type':{
915 $tagname = $_POST['tagname'];
916 $min_role_id = intval($_POST['min_role_id']);
917 $description = $_POST['description'];
918 $category = $_POST['category'];
920 // make tag_type_fields dict
921 $tag_type_fields = array("min_role_id" => $min_role_id,
922 "tagname" => $tagname,
923 "description" => $description,
924 "category" => $category,
928 $tag_type_id = $api->AddTagType($tag_type_fields);
929 if ($tag_type_id > 0) {
930 drupal_set_message("tag type $tag_type_id created");
932 drupal_set_error("Could not create tag type $tagname");
935 plc_redirect(l_tags());
939 case 'delete-tag-types':{
940 $tag_type_ids = $_POST['tag_type_ids'];
941 if (!$tag_type_ids) {
942 drupal_set_message("action=$action - No tag selected");
947 foreach ($tag_type_ids as $tag_type_id) {
948 if ($api->DeleteTagType(intval($tag_type_id)) != 1) {
956 drupal_set_message("Deleted $counter tag(s)");
958 drupal_set_error("Could not delete all selected tags, only $counter were removed");
961 plc_redirect(l_tags());
965 case 'remove-roles-from-tag-type':{
966 $tag_type_id = $_POST['tag_type_id'];
967 $role_ids = $_POST['role_ids'];
969 drupal_set_error("You have not selected role(s) to remove");
971 foreach ($role_ids as $role_id) {
972 if ($api->DeleteRoleFromTagType(intval($role_id), intval($tag_type_id)) != 1) {
973 drupal_set_error("Could not remove role $role_id from tag type $tag_type_id");
978 plc_redirect(l_tag_roles($tag_type_id));
981 case 'add-role-to-tag-type':{
982 $tag_type_id = $_POST['tag_type_id'];
983 $role_id = $_POST['role_id'];
985 drupal_set_error("You have not selected a role to add");
986 } else if ($api->AddRoleToTagType(intval($role_id), intval($tag_type_id)) != 1) {
987 drupal_set_error("Could not add role $role_id to tag $tag_type_id");
989 plc_redirect(l_tag_roles($tag_type_id));
992 //////////////////////////////////////// tags
993 case 'set-tag-on-node':
994 case 'set-tag-on-site':
995 case 'set-tag-on-person':
996 case 'set-tag-on-interface':
1000 if ($action == 'set-tag-on-node') {
1004 if ($action == 'set-tag-on-site') {
1008 if ($action == 'set-tag-on-person') {
1012 if ($action == 'set-tag-on-interface') {
1013 $mode = 'interface';
1016 if ($mode == 'node') {
1017 $id = intval($_POST['node_id']);
1018 } else if ($mode == 'site') {
1019 $id = intval($_POST['site_id']);
1020 } else if ($mode == 'person') {
1021 $id = intval($_POST['person_id']);
1023 $id = intval($_POST['interface_id']);
1026 $tag_type_id = intval($_POST['tag_type_id']);
1027 $value = $_POST['value'];
1029 $tag_types = $api->GetTagTypes(array($tag_type_id));
1030 if (count($tag_types) != 1) {
1031 drupal_set_error("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
1033 if ($mode == 'node') {
1034 $tags = $api->GetNodeTags(array('node_id' => $id, 'tag_type_id' => $tag_type_id));
1035 } else if ($mode == 'site') {
1036 $tags = $api->GetSiteTags(array('site_id' => $id, 'tag_type_id' => $tag_type_id));
1037 } else if ($mode == 'person') {
1038 $tags = $api->GetPersonTags(array('person_id' => $id, 'tag_type_id' => $tag_type_id));
1040 $tags = $api->GetInterfaceTags(array('interface_id' => $id, 'tag_type_id' => $tag_type_id));
1043 // already has a tag set
1044 if (count($tags) == 1) {
1046 if ($mode == 'node') {
1047 $result = $api->UpdateNodeTag($tag['node_tag_id'], $value);
1048 } else if ($mode == 'site') {
1049 $result = $api->UpdateSiteTag($tag['site_tag_id'], $value);
1050 } else if ($mode == 'person') {
1051 $result = $api->UpdatePersonTag($tag['person_tag_id'], $value);
1053 $result = $api->UpdateInterfaceTag($tag['interface_tag_id'], $value);
1057 drupal_set_message("Updated tag, new value = $value");
1059 drupal_set_error("Could not update tag");
1062 // no such tag set yet on that object
1064 if ($mode == 'node') {
1065 $tag_id = $api->AddNodeTag($id, $tag_type_id, $value);
1066 } else if ($mode == 'site') {
1067 $tag_id = $api->AddSiteTag($id, $tag_type_id, $value);
1068 } else if ($mode == 'person') {
1069 $tag_id = $api->AddPersonTag($id, $tag_type_id, $value);
1071 $tag_id = $api->AddInterfaceTag($id, $tag_type_id, $value);
1075 drupal_set_message("Created tag, new value = $value");
1077 drupal_set_error("Could not create tag");
1083 if ($mode == 'node') {
1084 plc_redirect(l_node_tags($id));
1085 } else if ($mode == 'site') {
1086 plc_redirect(l_site_tags($id));
1087 } else if ($mode == 'person') {
1088 plc_redirect(l_person_tags($id));
1090 plc_redirect(l_interface_tags($id));
1095 case 'delete-node-tags':
1096 case 'delete-site-tags':
1097 case 'delete-person-tags':
1098 case 'delete-interface-tags':{
1101 if ($action == 'delete-node-tags') {
1105 if ($action == 'delete-site-tags') {
1109 if ($action == 'delete-person-tags') {
1113 if ($action == 'delete-interface-tags') {
1114 $mode = 'interface';
1117 if ($mode == 'node') {
1118 $id = $_POST['node_id'];
1119 $tag_ids = $_POST['node_tag_ids'];
1120 } else if ($mode == 'site') {
1121 $id = $_POST['site_id'];
1122 $tag_ids = $_POST['site_tag_ids'];
1123 } else if ($mode == 'person') {
1124 $id = $_POST['person_id'];
1125 $tag_ids = $_POST['person_tag_ids'];
1127 $id = $_POST['interface_id'];
1128 $tag_ids = $_POST['interface_tag_ids'];
1132 drupal_set_message("action=$action - No tag selected");
1137 foreach ($tag_ids as $tag_id) {
1138 if ($mode == 'node') {
1139 $retcod = $api->DeleteNodeTag(intval($tag_id));
1140 } else if ($mode == 'site') {
1141 $retcod = $api->DeleteSiteTag(intval($tag_id));
1142 } else if ($mode == 'person') {
1143 $retcod = $api->DeletePersonTag(intval($tag_id));
1145 $retcod = $api->DeleteInterfaceTag(intval($tag_id));
1156 drupal_set_message("Deleted $counter tag(s)");
1158 drupal_set_error("Could not delete all selected tags, only $counter were removed");
1161 if ($mode == 'node') {
1162 plc_redirect(l_node_tags($id));
1163 } else if ($mode == 'site') {
1164 plc_redirect(l_site_tags($id));
1165 } else if ($mode == 'person') {
1166 plc_redirect(l_person_tags($id));
1168 plc_redirect(l_interface_tags($id));
1173 //////////////////////////////////////// nodegroups
1174 case 'update-nodegroup':{
1175 $nodegroup_id = $_POST['nodegroup_id'];
1176 $groupname = $_POST['groupname'];
1177 $value = $_POST['value'];
1180 $fields['groupname'] = $groupname;
1181 $fields['value'] = $value;
1182 if ($api->UpdateNodeGroup($nodegroup_id, $fields) == 1) {
1183 drupal_set_message('Nodegroup updated');
1185 drupal_set_error("Could not update nodegroup $groupname");
1188 plc_redirect(l_nodegroup($nodegroup_id));
1192 case 'add-nodegroup':{
1193 $groupname = $_POST['groupname'];
1195 drupal_set_error('Empty groupname');
1196 plc_redirect(l_nodegroups());
1198 $tag_type_id = intval($_POST['tag_type_id']);
1199 if (!$tag_type_id) {
1200 drupal_set_error('You must select a tag in the dropdown list');
1201 plc_redirect(l_nodegroups());
1203 $value = $_POST['value'];
1205 drupal_set_message("Empty value.. let's see ..");
1207 if ($api->AddNodeGroup($groupname, $tag_type_id, $value) > 0) {
1208 drupal_set_message("Nodegroup $groupname created");
1210 drupal_set_error("Could not create nodegroup $groupname");
1213 plc_redirect(l_nodegroups());
1217 case 'delete-nodegroups':{
1218 $nodegroup_ids = $_POST['nodegroup_ids'];
1219 if (!$nodegroup_ids) {
1220 drupal_set_message("action=delete-nodegroups - No group selected");
1221 plc_redirect(l_nodegroups());
1225 foreach ($nodegroup_ids as $nodegroup_id) {
1226 if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) {
1234 drupal_set_message("Deleted $counter group(s)");
1236 drupal_set_error("Could not delete all selected groups, only $counter were removed");
1239 plc_redirect(l_nodegroups());
1243 //////////////////////////////////////// leases
1244 case 'manage-leases':{
1245 $actions = json_decode($_POST['actions']);
1251 foreach ($actions as $action) {
1252 if ($action[0] == 'add-leases') {
1253 $nodenames = $action[1];
1254 $add_requested += count($nodenames);
1255 $slicename = $action[2];
1256 $t_from = intval($action[3]);
1257 $t_until = intval($action[4]);
1258 $hash = $api->AddLeases($nodenames, $slicename, $t_from, $t_until);
1259 // update number of added leases
1260 $ids = $hash['new_ids'];
1261 $add_done += count($ids);
1262 // update global errors array
1263 foreach ($api_errors = $hash['errors'] as $error) {
1267 } else if ($action[0] == 'delete-leases') {
1268 $lease_id = intval($action[1]);
1269 $del_requested += 1;
1270 if ($api->DeleteLeases(array($lease_id)) == 1) {
1273 $errors[] = "Could not delete lease " . $lease_id;
1276 $errors[] = "in actions.php, manage-leases, wrong action " . $action[0];
1280 if (count($errors) == 0) {
1281 echo ("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1283 foreach ($errors as $error) {
1284 echo ($error . "\n");
1287 echo ("Leases updated only partially (" .
1288 $add_done . "/" . $add_requested . " added and " .
1289 $del_done . "/" . $del_requested . " deleted)");
1295 ////////////////////////////////////////
1298 plc_debug('GET', $_GET);
1299 plc_debug('POST', $_POST);
1300 plc_debug('FILES', $_FILES);
1305 plc_error("Unknown action $action in actions.php");