4 require_once 'plc_login.php';
6 // Get session and API handles
7 require_once 'plc_session.php';
11 require_once 'plc_drupal.php';
14 require_once 'plc_functions.php';
16 $known_actions=array();
17 ////////////////////////////////////////////////////////////
20 // (*) set 'action' to one of the following
21 //////////////////////////////////////// persons
22 $known_actions []= "add-person-to-site";
23 // expects: person_id & site_id
24 $known_actions []= "remove-person-from-sites";
25 // expects: person_id & site_ids
26 $known_actions []= "remove-roles-from-person";
27 // expects: person_id & role_ids
28 $known_actions []= "add-role-to-person";
29 // expects: role_person_id & id
30 $known_actions []= "enable-person";
32 $known_actions []= "disable-person";
34 $known_actions []= "become-person";
36 $known_actions []= "delete-person";
38 $known_actions []= "delete-keys";
39 // expects: key_ids & person_id (for redirecting to the person's page)
40 $known_actions []= "upload-key";
41 // expects: person_id & $_FILES['key']
42 $known_actions []= "update-person";
43 // expects: person_id & first_name last_name title email phone url bio + [password1 password2]
45 //////////////////////////////////////// nodes
46 $known_actions []= "node-boot-state";
47 // expects: node_id boot_state
48 $known_actions []= "delete-node";
50 $known_actions []= "update-node";
51 // expects: node_id, hostname, model
52 $known_actions []= "attach-pcu";
53 // expects: node_id, pcu_id, port (pcu_id <0 means detach)
54 $known_actions []= "reboot-node-with-pcu";
57 //////////////////////////////////////// interfaces
58 $known_actions []= "delete-interfaces";
59 // expects: interface_ids
60 $known_actions []="add-interface";
61 // expects: node_id & interface details
62 $known_actions []="new-interface";
64 $known_actions []="update-interface";
65 // expects: interface_id & interface details
67 //////////////////////////////////////// sites
68 $known_actions []= "delete-site";
70 $known_actions []= "expire-all-slices-in-site";
72 $known_actions []= "update-site";
73 // expects: site_id & name abbreviated_name url latitude longitude [login_base max_slices]
75 //////////////////////////////////////// slices
76 $known_actions []= "delete-slice";
78 $known_actions []= "update-slice";
79 // expects: slice_id, name, description, url
80 $known_actions []= "renew-slice";
81 // expects: slice_id & expires
82 $known_actions []= 'remove-persons-from-slice';
83 // expects: slice_id & person_ids
84 $known_actions []= 'add-persons-in-slice';
85 // expects: slice_id & person_ids
86 $known_actions []= 'remove-nodes-from-slice';
87 // expects: slice_id & node_ids
88 $known_actions []= 'add-nodes-in-slice';
89 // expects: slice_id & node_ids
90 $known_actions []= 'delete-slice-tags';
91 // expects: slice_tag_id
92 $known_actions []= 'add-slice-tag';
93 // expects: slice_id & tag_type_id & node_id & nodegroup_id
95 //////////////////////////////////////// tag types
96 $known_actions []= "update-tag-type";
97 // expects: tag_type_id & name & description & category & min_role_id
98 $known_actions []= "add-tag-type";
99 // expects: tag_type_id & tagname & description & category & min_role_id
100 $known_actions []= "delete-tag-types";
101 // expects: tag_type_ids
102 $known_actions []= "remove-roles-from-tag-type";
103 // expects: tag_type_id & role_ids
104 $known_actions []= "add-role-to-tag-type";
105 // expects: tag_type_id_id & id
107 //////////////////////////////////////// tags
108 $known_actions []= "set-tag-on-node";
109 // expects: node_id tagname value
110 $known_actions []= "set-tag-on-interface";
111 // expects: interface_id tagname value
112 $known_actions []= "delete-node-tags";
113 // expects: node_id & node_tag_ids
114 $known_actions []= "delete-interface-tags";
115 // expects: interface_id & interface_tag_ids
117 //////////////////////////////////////// nodegroups
118 $known_actions []= "update-nodegroup";
119 // expects nodegroup_id groupname value
120 $known_actions []= "add-nodegroup";
121 // expects groupname, tag_type_id, value
122 $known_actions []= 'delete-nodegroups';
123 // expects nodegroup_ids
125 //////////////////////////////////////// leases
126 $known_actions []= "manage-leases";
127 // expects as 'actions' a list of 'action' of the form
128 // either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
129 // or [ 'delete-leases', lease_id ]
131 ////////////////////////////////////////////////////////////
132 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
133 'broadcast', 'netmask', 'dns1', 'dns2',
134 'hostname', 'mac', 'bwlimit' );
136 //////////////////////////////
137 // sometimes we don't set 'action', but use the submit button name instead
138 // so if 'action' not set, see if $_POST has one of the actions as a key
139 if ($_POST['action'])
140 $action=$_POST['action'];
142 foreach ($known_actions as $known_action)
143 if ($_POST[$known_action]) {
144 $action=$known_action;
148 //uncomment for debugging incoming data
151 $person_id = $_POST['person_id']; // usually needed
154 drupal_set_message ("actions.php: action not set or not in known_actions");
155 plc_debug('POST',$_POST);
161 case 'add-person-to-site': {
162 $site_id = $_POST['site_id'];
163 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
164 plc_redirect (l_person($person_id));
167 case 'remove-person-from-sites': {
168 $site_ids = $_POST['site_ids'];
170 drupal_set_message("action=$action - No site selected");
173 foreach ( $site_ids as $site_id ) {
174 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
176 plc_redirect (l_person($person_id));
179 case 'remove-roles-from-person' : {
180 $role_ids=$_POST['role_ids'];
182 drupal_set_error("You have not selected role(s) to remove");
184 foreach( $role_ids as $role_id)
185 if ( $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) ) != 1 )
186 drupal_set_error ("Could not remove role $role_id from person $person_id");
188 plc_redirect (l_person_roles($person_id));
191 case 'add-role-to-person' : {
192 $role_id=$_POST['role_id'];
194 drupal_set_error ("You have not selected a role to add");
195 } else if ($api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) ) != 1) {
196 drupal_set_error("Could not add role $role_id to person $person_id");
198 plc_redirect (l_person_roles($person_id));
201 case 'enable-person' : {
202 $fields = array( "enabled"=>true );
203 $api->UpdatePerson( intval( $person_id ), $fields );
204 plc_redirect (l_person($person_id));
207 case 'disable-person' : {
208 $fields = array( "enabled"=>false );
209 $api->UpdatePerson( intval( $person_id ), $fields );
210 plc_redirect (l_person($person_id));
213 case 'become-person' : {
214 $plc->BecomePerson (intval($person_id));
215 plc_redirect (l_person(intval($person_id)));
218 case 'delete-person' : {
219 $api->DeletePerson( intval( $person_id ) );
220 plc_redirect (l_persons());
223 case 'delete-keys' : {
224 $key_ids=$_POST['key_ids'];
226 drupal_set_message("action=$action - No key selected");
231 foreach( $key_ids as $key_id ) {
232 if ($api->DeleteKey( intval( $key_id )) != 1)
238 drupal_set_message ("Deleted $counter key(s)");
240 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
241 plc_redirect(l_person($person_id));
245 case 'upload-key' : {
246 if ( ! isset( $_FILES['key'] ) ) {
247 drupal_set_message ("action=$action, no key file set");
251 $key_file= $_FILES['key']['tmp_name'];
253 plc_error("Please select a valid SSH key file to upload");
256 $fp = fopen( $key_file, "r" );
259 plc_error("Unable to open key file $key_file");
262 // opened the key file, read the one line of contents
263 // The POST operation always creates a file even if the filename
264 // the user specified was garbage. If there was some problem
265 // with the source file, we'll get a zero length read here.
266 $key = fread($fp, filesize($key_file));
269 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
272 drupal_set_message ("New key added");
274 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
276 plc_redirect(l_person($person_id));
279 case 'update-person': {
280 $person_id=$_POST['person_id'];
281 // attempt to update this person
282 $first_name= $_POST['first_name'];
283 $last_name= $_POST['last_name'];
284 $title= $_POST['title'];
285 $email= $_POST['email'];
286 $phone= $_POST['phone'];
288 $bio= str_replace("\r", "", $_POST['bio']);
289 $password1= $_POST['password1'];
290 $password2= $_POST['password2'];
292 if( $password1 != $password2 ) {
293 drupal_set_error ("The passwords do not match");
294 plc_redirect(l_person($person_id));
298 $fields['first_name']= $first_name;
299 $fields['last_name']= $last_name;
300 $fields['title']= $title;
301 $fields['email']= $email;
302 $fields['phone']= $phone;
303 $fields['url']= $url;
304 $fields['bio']= $bio;
306 if ( $password1 != "" )
307 $fields['password']= $password1;
309 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
310 drupal_set_message("$first_name $last_name updated");
312 drupal_set_error ("Could not update person $person_id" . $api->error());
314 plc_redirect(l_person($person_id));
318 //////////////////////////////////////////////////////////// nodes
319 case 'node-boot-state': {
320 $node_id=intval($_POST['node_id']);
321 $boot_state=$_POST['boot_state'];
322 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
324 drupal_set_message("boot state updated");
325 plc_redirect (l_node($node_id));
327 drupal_set_error("Could not set boot_state '$boot_state'");
332 case 'delete-node': {
333 $node_id=intval($_POST['node_id']);
334 $result=$api->DeleteNode( intval( $node_id ) );
336 drupal_set_message("Node $node_id deleted");
337 plc_redirect (l_nodes());
339 drupal_set_error ("Could not delete node $node_id");
344 case 'update-node': {
345 $node_id=intval($_POST['node_id']);
346 $hostname= $_POST['hostname'];
347 $model= $_POST['model'];
349 $fields= array( "hostname"=>$hostname, "model"=>$model );
350 $api->UpdateNode( $node_id, $fields );
351 $error= $api->error();
353 if( empty( $error ) ) {
354 drupal_set_message("Update node $hostname");
355 plc_redirect(l_node($node_id));
357 drupal_set_error($error);
362 // this code will ensure that at most one PCU gets attached to the node
364 $node_id=intval($_POST['node_id']);
365 $pcu_id=intval($_POST['pcu_id']);
366 $port=intval($_POST['port']);
367 // always start with deleting former PCUs
368 $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
369 $former_pcu_ids = $nodes[0]['pcu_ids'];
370 if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
371 if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1)
372 drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
374 drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
376 // re-attach only if provided pcu_id >=0
378 if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
379 drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
381 drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
383 drupal_set_message ('Detached node from all PCUs');
386 plc_redirect(l_node($node_id));
390 case 'reboot-node-with-pcu': {
391 $node_id=intval($_POST['node_id']);
392 $hostname= $_POST['hostname'];
394 $ret = $api->RebootNodeWithPCU( $node_id );
395 $error= $api->error();
397 if( empty( $error ) ) {
398 drupal_set_message("Reboot node $hostname: $ret");
399 plc_redirect(l_node($node_id));
401 drupal_set_error($error);
407 //////////////////////////////////////////////////////////// interfaces
408 case 'delete-interfaces' : {
409 $interface_ids=$_POST['interface_ids'];
410 if ( ! $interface_ids) {
411 drupal_set_message("action=$action - No interface selected");
416 foreach( $interface_ids as $interface_id ) {
417 if ($api->DeleteInterface( intval( $interface_id )) != 1)
423 drupal_set_message ("Deleted $counter interface(s)");
425 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
426 plc_redirect(l_node($_POST['node_id']));
429 case 'new-interface': {
430 plc_redirect(l_interface_add($_POST['node_id']));
433 case 'add-interface': {
434 $node_id=intval($_POST['node_id']);
435 foreach ($interface_details as $field) {
436 $interface[$field]= $_POST[$field];
437 // these must be integers
438 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
439 if ( empty ($interface[$field]) )
440 unset ($interface[$field]);
442 $interface[$field]= intval( $interface[$field] );
445 $interface_id =$api->AddInterface( $node_id , $interface );
446 if ($interface_id <= 0 ) {
447 drupal_set_error ("Could not create interface");
448 drupal_set_error ($api->error());
450 $ip=$interface['ip'];
451 drupal_set_message ("Interface $ip added into node $node_id");
452 if ($_POST['is-virtual']) {
453 $ifname=$_POST['ifname'];
454 if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0)
455 drupal_set_error ("Could not set tag 'ifname'=$ifname");
457 drupal_set_message ("Set tag 'ifname'=$ifname");
458 $alias=$_POST['alias'];
459 // deafult to interface_id
460 if ( ! $alias ) $alias=strval($interface_id);
461 if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0)
462 drupal_set_error ("Could not set tag 'alias'=$alias");
464 drupal_set_message ("Set tag 'alias'=$alias");
467 plc_redirect (l_node_interfaces($node_id));
470 case 'update-interface': {
471 $interface_id=$_POST['interface_id'];
472 foreach ($interface_details as $field) {
473 $interface[$field]= $_POST[$field];
474 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
475 if ( intval($interface[$field]) != 0 ) {
476 $interface[$field]= intval( $interface[$field]);
477 } elseif ($field=='bwlimit' ) {
478 $interface[$field] = NULL;
482 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
484 drupal_set_message ("Interface $interface_id updated");
486 drupal_set_error ("Could not update interface: " . $api->error());
487 plc_redirect (l_interface($interface_id));
490 //////////////////////////////////////////////////////////// sites
491 case 'delete-site': {
492 $site_id = intval($_POST['site_id']);
493 if ($api->DeleteSite($site_id) ==1)
494 drupal_set_message ("Site $site_id deleted");
496 drupal_set_error("Failed to delete site $site_id");
497 plc_redirect (l_sites());
501 case 'expire-all-slices-in-site': {
503 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
506 //// old code from sites/expire.php
507 $sites = $api->GetSites( array( intval( $site_id )));
509 // xxx why not 'now?'
510 $expiration= strtotime( $_POST['expires'] );
511 // loop through all slices for site
512 foreach ($site['slice_ids'] as $slice_id) {
513 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
515 // update site to not allow slice creation or renewal
516 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
517 plc_redirect (l_site($site_id));
521 case 'update-site': {
522 $site_id=intval($_POST['site_id']);
523 $name= $_POST['name'];
524 $abbreviated_name= $_POST['abbreviated_name'];
526 $latitude= floatval($_POST['latitude']);
527 $longitude= floatval($_POST['longitude']);
528 //$max_slivers= $_POST['max_slivers'];
530 $fields= array( "name" => $name,
531 "abbreviated_name" => $abbreviated_name,
533 "latitude" => floatval( $latitude ),
534 "longitude" => floatval( $longitude ));
536 if ($_POST['login_base'])
537 $fields['login_base'] = $_POST['login_base'];
538 if (isset($_POST['max_slices']))
539 $fields['max_slices'] = intval($_POST['max_slices']);
540 if (isset($_POST['enabled'])) {
541 $fields['enabled'] = (bool)$_POST['enabled'];
544 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
546 drupal_set_message("Site $name updated");
548 drupal_set_error ("Could not update site $site_id");
550 plc_redirect(l_site($site_id));
554 //////////////////////////////////////////////////////////// slices
555 case 'delete-slice': {
556 $slice_id = $_POST['slice_id'];
557 if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
558 drupal_set_message("Slice $slice_id deleted");
559 plc_redirect(l_slices());
561 drupal_set_error("Could not delete slice $slice_id " . $api->error());
566 case 'update-slice': {
567 $slice_id = $_POST['slice_id'];
568 $name = $_POST['name'];
569 $description= $_POST['description'];
572 $fields= array( "description"=>$description, "url"=>$url );
573 $api->UpdateSlice( intval( $slice_id ), $fields );
574 $error= $api->error();
576 if( empty( $error ) ) {
577 drupal_set_message("Update slice $name");
578 plc_redirect(l_slice($slice_id));
580 drupal_set_error($error);
585 case 'renew-slice': {
586 $slice_id = intval ($_POST['slice_id']);
587 $expires = intval ($_POST['expires']);
593 $MAX_FUTURE=$WEEKS*$WEEK;
594 if ( ($expires-$now) > $MAX_FUTURE) {
595 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
596 plc_redirect(l_slice($slice_id));
598 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
599 drupal_set_message("Slice renewed");
601 drupal_set_error("Could not update slice $slice_id");
602 plc_redirect(l_slice($slice_id));
606 case 'remove-persons-from-slice': {
607 $slice_id = intval ($_POST['slice_id']);
608 $person_ids = $_POST['person_ids'];
611 $tmp_slices = $api->GetSlices($slice_id, array("name"));
612 if (count($tmp_slices) > 0) {
613 $tmp_slice = $tmp_slices[0];
614 $slice_name = $tmp_slice["name"];
616 $notify_subject = "Removed from slice: " . $slice_name;
617 $notify_body = sprintf("You have been removed from the slice %s.
619 Our support team will be glad to answer any question that you might have.
621 $notify_person_ids = array();
625 foreach( $person_ids as $person_id ) {
626 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
629 array_push($notify_person_ids, intval($person_id));
634 $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
635 drupal_set_message ("Deleted $counter person(s)");
638 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
639 plc_redirect(l_slice($slice_id) . " &show_persons=true");
643 case 'add-persons-in-slice': {
644 $slice_id = intval ($_POST['slice_id']);
645 $person_ids = $_POST['person_ids'];
648 $tmp_slices = $api->GetSlices($slice_id, array("name"));
649 if (count($tmp_slices) > 0) {
650 $tmp_slice = $tmp_slices[0];
651 $slice_name = $tmp_slice["name"];
653 $notify_subject = "Added to slice: " . $slice_name;
654 $notify_body = sprintf("You have been added to the slice %s as a user.
656 You can go to your slice page following the link below:
657 https://%s:%d/db/slices/index.php?id=%d
659 Our support team will be glad to answer any question that you might have.
660 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
661 $notify_person_ids = array();
665 foreach ($person_ids as $person_id) {
666 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
669 array_push($notify_person_ids, intval($person_id));
674 $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
675 drupal_set_message ("Added $counter person(s)");
678 drupal_set_error ("Could not add all selected persons, only $counter were added");
679 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
683 case 'remove-nodes-from-slice': {
684 $slice_id = intval ($_POST['slice_id']);
685 $node_ids = array_map("intval",$_POST['node_ids']);
686 $count=count($node_ids);
688 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
689 drupal_set_message ("Removed $count node(s)");
691 drupal_set_error ("Could not remove selected nodes");
692 plc_redirect(l_slice_nodes($slice_id));
696 case 'add-nodes-in-slice': {
697 $slice_id = intval ($_POST['slice_id']);
698 $node_ids = array_map("intval",$_POST['node_ids']);
699 $count=count($node_ids);
700 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
701 drupal_set_message ("Added $count node(s)");
703 drupal_set_error ("Could not add all selected nodes");
704 plc_redirect(l_slice_nodes($slice_id));
708 case 'delete-slice-tags': {
709 $slice_id = intval($_POST['slice_id']);
710 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
713 foreach($slice_tag_ids as $slice_tag_id) {
714 if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
716 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
721 drupal_set_message ("Deleted $count slice tag(s)");
722 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
726 case 'add-slice-tag': {
727 $slice_id = intval($_POST['slice_id']);
728 $tag_type_id = intval($_POST['tag_type_id']);
729 $value = $_POST['value'];
730 $node_id = intval($_POST['node_id']);
731 $nodegroup_id = intval($_POST['nodegroup_id']);
735 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
736 } elseif ($nodegroup_id) {
737 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
739 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
742 drupal_set_message ("Added slice tag.");
744 drupal_set_error("Could not add slice tag");
745 if ($_POST['sliver_action'])
746 plc_redirect(l_sliver($node_id,$slice_id));
748 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
752 //////////////////////////////////////////////////////////// tag types
754 case 'update-tag-type': {
756 $tag_type_id= intval( $_POST['tag_type_id'] );
757 $tagname = $_POST['tagname'];
758 $min_role_id= intval( $_POST['min_role_id'] );
759 $description= $_POST['description'];
760 $category= $_POST['category'];
762 // make tag_type_fields dict
763 $tag_type_fields= array( "min_role_id" => $min_role_id,
764 "tagname" => $tagname,
765 "description" => $description,
766 "category" => $category,
769 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
770 drupal_set_message ("Tag type $tagname updated");
772 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
773 plc_redirect(l_tag($tag_type_id));
777 case 'add-tag-type': {
779 $tagname = $_POST['tagname'];
780 $min_role_id= intval( $_POST['min_role_id'] );
781 $description= $_POST['description'];
782 $category= $_POST['category'];
784 // make tag_type_fields dict
785 $tag_type_fields= array( "min_role_id" => $min_role_id,
786 "tagname" => $tagname,
787 "description" => $description,
788 "category" => $category,
792 $tag_type_id=$api->AddTagType( $tag_type_fields );
793 if ($tag_type_id > 0)
794 drupal_set_message ("tag type $tag_type_id created");
796 drupal_set_error ("Could not create tag type $tagname");
797 plc_redirect( l_tags());
801 case 'delete-tag-types': {
802 $tag_type_ids = $_POST['tag_type_ids'];
803 if ( ! $tag_type_ids) {
804 drupal_set_message("action=$action - No tag selected");
809 foreach ($tag_type_ids as $tag_type_id)
810 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
815 drupal_set_message ("Deleted $counter tag(s)");
817 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
818 plc_redirect (l_tags());
822 case 'remove-roles-from-tag-type' : {
823 $tag_type_id=$_POST['tag_type_id'];
824 $role_ids=$_POST['role_ids'];
826 drupal_set_error("You have not selected role(s) to remove");
828 foreach( $role_ids as $role_id)
829 if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 )
830 drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id");
832 plc_redirect (l_tag_roles($tag_type_id));
835 case 'add-role-to-tag-type' : {
836 $tag_type_id=$_POST['tag_type_id'];
837 $role_id=$_POST['role_id'];
839 drupal_set_error ("You have not selected a role to add");
840 } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) {
841 drupal_set_error("Could not add role $role_id to tag $tag_type_id");
843 plc_redirect (l_tag_roles($tag_type_id));
846 //////////////////////////////////////// tags
847 case 'set-tag-on-node':
848 case 'set-tag-on-interface': {
851 if ($action == 'set-tag-on-node') $node_mode=true;
854 $node_id = intval($_POST['node_id']);
856 $interface_id=intval($_POST['interface_id']);
857 $tag_type_id = intval($_POST['tag_type_id']);
858 $value = $_POST['value'];
860 $tag_types=$api->GetTagTypes(array($tag_type_id));
861 if (count ($tag_types) != 1) {
862 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
865 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
867 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
868 if ( count ($tags) == 1) {
871 $tag_id=$tag['node_tag_id'];
872 $result=$api->UpdateNodeTag($tag_id,$value);
874 $tag_id=$tag['interface_tag_id'];
875 $result=$api->UpdateInterfaceTag($tag_id,$value);
878 drupal_set_message ("Updated tag, new value = $value");
880 drupal_set_error ("Could not update tag");
883 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
885 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
887 drupal_set_message ("Created tag, new value = $value");
889 drupal_set_error ("Could not create tag");
894 plc_redirect (l_node_tags($node_id));
896 plc_redirect (l_interface_tags($interface_id));
899 case 'delete-node-tags' :
900 case 'delete-interface-tags' : {
903 if ($action == 'delete-node-tags') $node_mode=true;
906 $tag_ids=$_POST['node_tag_ids'];
908 $tag_ids=$_POST['interface_tag_ids'];
911 drupal_set_message("action=$action - No tag selected");
916 foreach( $tag_ids as $tag_id ) {
918 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
920 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
927 drupal_set_message ("Deleted $counter tag(s)");
929 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
931 plc_redirect(l_node_tags($_POST['node_id']));
933 plc_redirect(l_interface_tags($_POST['interface_id']));
936 //////////////////////////////////////// nodegroups
937 case 'update-nodegroup': {
938 $nodegroup_id = $_POST['nodegroup_id'];
939 $groupname = $_POST['groupname'];
940 $value = $_POST['value'];
943 $fields['groupname']=$groupname;
944 $fields['value']=$value;
945 if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1)
946 drupal_set_message ('Nodegroup updated');
948 drupal_set_error ("Could not update nodegroup $groupname");
950 plc_redirect(l_nodegroup($nodegroup_id));
954 case 'add-nodegroup': {
955 $groupname=$_POST['groupname'];
956 if ( ! $groupname ) {
957 drupal_set_error ('Empty groupname');
958 plc_redirect (l_nodegroups());
960 $tag_type_id=intval($_POST['tag_type_id']);
961 if ( ! $tag_type_id ) {
962 drupal_set_error ('You must select a tag in the dropdown list');
963 plc_redirect (l_nodegroups());
965 $value=$_POST['value'];
967 drupal_set_message ("Empty value.. let's see ..");
969 if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0)
970 drupal_set_message ("Nodegroup $groupname created");
972 drupal_set_error ("Could not create nodegroup $groupname");
974 plc_redirect (l_nodegroups());
978 case 'delete-nodegroups': {
979 $nodegroup_ids=$_POST['nodegroup_ids'];
980 if ( ! $nodegroup_ids ) {
981 drupal_set_message("action=delete-nodegroups - No group selected");
982 plc_redirect(l_nodegroups());
986 foreach ($nodegroup_ids as $nodegroup_id)
987 if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1)
992 drupal_set_message ("Deleted $counter group(s)");
994 drupal_set_error ("Could not delete all selected groups, only $counter were removed");
995 plc_redirect (l_nodegroups());
999 //////////////////////////////////////// leases
1000 case 'manage-leases': {
1001 $actions=json_decode($_POST['actions']);
1007 foreach ($actions as $action) {
1008 if ($action[0] == 'add-leases') {
1009 $nodenames=$action[1];
1010 $add_requested += count($nodenames);
1011 $slicename=$action[2];
1012 $t_from=intval($action[3]);
1013 $t_until=intval($action[4]);
1014 $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
1015 // update number of added leases
1016 $ids=$hash['new_ids'];
1017 $add_done += count($ids);
1018 // update global errors array
1019 foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
1020 } else if ($action[0]=='delete-leases') {
1021 $lease_id=intval($action[1]);
1022 $del_requested += 1;
1023 if ($api->DeleteLeases(array($lease_id)) == 1) {
1026 $errors []= "Could not delete lease " . $lease_id;
1029 $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1033 if (count($errors)==0) {
1034 echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1036 foreach ($errors as $error) echo($error. "\n");
1037 echo("Leases updated only partially (" .
1038 $add_done . "/" . $add_requested . " added and " .
1039 $del_done . "/" . $del_requested . " deleted)");
1045 ////////////////////////////////////////
1048 plc_debug('GET',$_GET);
1049 plc_debug('POST',$_POST);
1050 plc_debug('FILES',$_FILES);
1055 plc_error ("Unknown action $action in actions.php");