ba6010493bb723631bacc1ffe70bf2ff0c78d9e9
[plewww.git] / planetlab / common / actions.php
1 <?php
2
3 // Require login
4 require_once 'plc_login.php';
5
6 // Get session and API handles
7 require_once 'plc_session.php';
8 global $plc, $api;
9
10 //print header
11 require_once 'plc_drupal.php';
12
13 // Common functions
14 require_once 'plc_functions.php';
15
16 $known_actions=array();
17 ////////////////////////////////////////////////////////////
18 // interface :
19 // (*) use POST 
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";
31 //      expects:        person_id
32 $known_actions []= "disable-person";
33 //      expects:        person_id
34 $known_actions []= "become-person";
35 //      expects:        person_id
36 $known_actions []= "delete-person";
37 //      expects:        person_id
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]
44
45 //////////////////////////////////////// nodes
46 $known_actions []= "node-boot-state";   
47 //      expects:        node_id boot_state
48 $known_actions []= "delete-node";       
49 //      expects:        node_id
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";
55 //      expects:        node_id
56
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";
63 //      expects:        node_id 
64 $known_actions []="update-interface";
65 //      expects:        interface_id & interface details
66
67 //////////////////////////////////////// sites
68 $known_actions []= "delete-site";       
69 //      expects:        site_id
70 $known_actions []= "expire-all-slices-in-site";
71 //      expects:        slice_ids
72 $known_actions []= "update-site";
73 //      expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
74
75 //////////////////////////////////////// slices
76 $known_actions []= "delete-slice";
77 //      expects:        slice_id
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
94
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
106
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
116
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
124
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 ]
130
131 ////////////////////////////////////////////////////////////
132 $interface_details= array ('method','type', 'ip', 'gateway', 'network', 
133                            'broadcast', 'netmask', 'dns1', 'dns2', 
134                            'hostname', 'mac', 'bwlimit' );
135
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'];
141 else 
142   foreach ($known_actions as $known_action) 
143     if ($_POST[$known_action]) {
144       $action=$known_action;
145       break;
146     }
147
148 //uncomment for debugging incoming data
149 //$action='debug';
150
151 $person_id = $_POST['person_id'];       // usually needed
152
153 if ( ! $action ) {
154   drupal_set_message ("actions.php: action not set or not in known_actions");
155   plc_debug('POST',$_POST);
156   return;
157  }
158
159 switch ($action) {
160
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));
165  }
166
167  case 'remove-person-from-sites': {
168    $site_ids = $_POST['site_ids'];
169    if ( ! $site_ids) {
170      drupal_set_message("action=$action - No site selected");
171      return;
172    }
173    foreach ( $site_ids as $site_id ) {
174      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
175    }
176    plc_redirect (l_person($person_id));
177  }
178
179  case 'remove-roles-from-person' : {
180    $role_ids=$_POST['role_ids'];
181    if ( ! $role_ids) {
182      drupal_set_error("You have not selected role(s) to remove");
183    } else {
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");
187    }
188    plc_redirect (l_person_roles($person_id));
189  }
190      
191  case 'add-role-to-person' : {
192    $role_id=$_POST['role_id'];
193    if ( ! $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");
197    }
198    plc_redirect (l_person_roles($person_id));
199  }
200
201  case 'enable-person' : {
202    $fields = array( "enabled"=>true );
203    $api->UpdatePerson( intval( $person_id ), $fields );
204    plc_redirect (l_person($person_id));
205  }
206
207  case 'disable-person' : {
208    $fields = array( "enabled"=>false );
209    $api->UpdatePerson( intval( $person_id ), $fields );
210    plc_redirect (l_person($person_id));
211  }
212
213  case 'become-person' : {
214    $plc->BecomePerson (intval($person_id));
215    plc_redirect (l_person(intval($person_id)));
216  }
217
218  case 'delete-person' : {
219   $api->DeletePerson( intval( $person_id ) );
220    plc_redirect (l_persons());
221  }
222
223  case 'delete-keys' : {
224    $key_ids=$_POST['key_ids'];
225    if ( ! $key_ids) {
226      drupal_set_message("action=$action - No key selected");
227      return;
228    }
229    $success=true;
230    $counter=0;
231    foreach( $key_ids as $key_id ) {
232      if ($api->DeleteKey( intval( $key_id )) != 1) 
233        $success=false;
234      else
235        $counter++;
236    }
237    if ($success) 
238      drupal_set_message ("Deleted $counter key(s)");
239    else
240      drupal_set_error ("Could not delete all selected keys, only $counter were removed");
241    plc_redirect(l_person($person_id));
242  }
243
244
245  case 'upload-key' : {
246    if ( ! isset( $_FILES['key'] ) ) {
247      drupal_set_message ("action=$action, no key file set");
248      return;
249    }
250    
251    $key_file= $_FILES['key']['tmp_name'];
252    if ( ! $key_file ) {
253      plc_error("Please select a valid SSH key file to upload");
254      return;
255    } 
256    $fp = fopen( $key_file, "r" );
257    $key = "";
258    if( ! $fp ) {
259      plc_error("Unable to open key file $key_file");
260      return;
261    }
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));
267    fclose($fp);
268    
269    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
270    
271    if ( $key_id >= 1) 
272      drupal_set_message ("New key added");
273    else
274      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
275    
276    plc_redirect(l_person($person_id));
277  }
278
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'];
287    $url= $_POST['url'];
288    $bio= str_replace("\r", "", $_POST['bio']);
289    $password1= $_POST['password1'];
290    $password2= $_POST['password2'];
291
292    if( $password1 != $password2 ) {
293      drupal_set_error ("The passwords do not match");
294      plc_redirect(l_person($person_id));
295   }
296
297    $fields= array();
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;
305                 
306    if ( $password1 != "" )
307      $fields['password']= $password1;
308     
309     if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
310       drupal_set_message("$first_name $last_name updated");
311     else 
312       drupal_set_error ("Could not update person $person_id" . $api->error());
313
314     plc_redirect(l_person($person_id));
315     break;
316   }
317
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 ) );
323    if ($result==1) {
324      drupal_set_message("boot state updated");
325      plc_redirect (l_node($node_id));
326    } else {
327      drupal_set_error("Could not set boot_state '$boot_state'");
328    }
329    break;
330  }
331
332  case 'delete-node': {
333    $node_id=intval($_POST['node_id']);
334    $result=$api->DeleteNode( intval( $node_id ) );
335    if ($api==1) {
336      drupal_set_message("Node $node_id deleted");
337      plc_redirect (l_nodes());
338    } else {
339      drupal_set_error ("Could not delete node $node_id");
340    }
341    break;
342  }
343
344  case 'update-node': {
345    $node_id=intval($_POST['node_id']);
346    $hostname= $_POST['hostname'];
347    $model= $_POST['model'];
348
349    $fields= array( "hostname"=>$hostname, "model"=>$model );
350    $api->UpdateNode( $node_id, $fields );
351    $error= $api->error();
352
353    if( empty( $error ) ) {
354      drupal_set_message("Update node $hostname");
355      plc_redirect(l_node($node_id));
356    } else {
357      drupal_set_error($error);
358    }
359    break;
360  }
361
362  // this code will ensure that at most one PCU gets attached to the node
363  case 'attach-pcu': {
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);
373        else 
374          drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
375      }
376    // re-attach only if provided pcu_id >=0
377    if ($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);
380      else
381        drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
382    } else {
383      drupal_set_message ('Detached node from all PCUs');
384    }
385    
386    plc_redirect(l_node($node_id));
387    break;
388  }
389
390  case 'reboot-node-with-pcu': {
391    $node_id=intval($_POST['node_id']);
392    $hostname= $_POST['hostname'];
393
394    $ret = $api->RebootNodeWithPCU( $node_id );
395    $error= $api->error();
396
397    if( empty( $error ) ) {
398      drupal_set_message("Reboot node $hostname: $ret");
399      plc_redirect(l_node($node_id));
400    } else {
401      drupal_set_error($error);
402    }
403    break;
404  }
405    
406
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");
412      return;
413    }
414    $success=true;
415    $counter=0;
416    foreach( $interface_ids as $interface_id ) {
417      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
418        $success=false;
419      else
420        $counter++;
421    }
422    if ($success) 
423      drupal_set_message ("Deleted $counter interface(s)");
424    else
425      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
426    plc_redirect(l_node($_POST['node_id']));
427  }
428
429  case 'new-interface': {
430    plc_redirect(l_interface_add($_POST['node_id']));
431  }
432
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]);
441        else 
442          $interface[$field]= intval( $interface[$field] );
443      }
444    }
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());
449    } else {
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");
456        else 
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");
463        else 
464          drupal_set_message ("Set tag 'alias'=$alias");
465      }
466    }
467    plc_redirect (l_node_interfaces($node_id));
468  }
469    
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;
479        }
480      }
481    }
482    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
483    if ($result == 1 ) 
484      drupal_set_message ("Interface $interface_id updated");
485    else
486      drupal_set_error ("Could not update interface: " . $api->error());
487    plc_redirect (l_interface($interface_id));
488  }
489    
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");
495    else
496      drupal_set_error("Failed to delete site $site_id");
497    plc_redirect (l_sites());
498    break;
499  }
500
501  case 'expire-all-slices-in-site': {
502    // xxx todo
503    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
504    return;
505
506    //// old code from sites/expire.php
507    $sites = $api->GetSites( array( intval( $site_id )));
508    $site=$sites[0];
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 ) );
514    }
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));
518    break;
519  }
520
521  case 'update-site': {
522    $site_id=intval($_POST['site_id']);
523    $name= $_POST['name'];
524    $abbreviated_name= $_POST['abbreviated_name'];
525    $url= $_POST['url'];
526    $latitude= floatval($_POST['latitude']);
527    $longitude= floatval($_POST['longitude']);
528    //$max_slivers= $_POST['max_slivers'];
529    
530    $fields= array( "name" => $name, 
531                    "abbreviated_name" => $abbreviated_name, 
532                    "url" => $url, 
533                    "latitude" => floatval( $latitude ), 
534                    "longitude" => floatval( $longitude ));
535
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'];
542    }
543    
544    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
545    if ($retcod == 1) 
546      drupal_set_message("Site $name updated");
547    else 
548      drupal_set_error ("Could not update site $site_id");
549      
550    plc_redirect(l_site($site_id));
551    break;
552  }
553
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());
560    } else {
561      drupal_set_error("Could not delete slice $slice_id " . $api->error());
562    }
563    break;
564  }
565      
566  case 'update-slice': {
567    $slice_id = $_POST['slice_id'];
568    $name = $_POST['name'];
569    $description= $_POST['description'];
570    $url= $_POST['url'];
571
572    $fields= array( "description"=>$description, "url"=>$url );
573    $api->UpdateSlice( intval( $slice_id ), $fields );
574    $error= $api->error();
575
576    if( empty( $error ) ) {
577      drupal_set_message("Update slice $name");
578      plc_redirect(l_slice($slice_id));
579    } else {
580      drupal_set_error($error);
581    }
582    break;
583  }
584
585  case 'renew-slice': {
586    $slice_id = intval ($_POST['slice_id']);     
587    $expires = intval ($_POST['expires']);
588    // 8 weeks from now
589    // xxx
590    $now=mktime();
591    $WEEK=7*24*3600;
592    $WEEKS=8;
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));
597    }
598    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
599      drupal_set_message("Slice renewed");
600    else
601      drupal_set_error("Could not update slice $slice_id");
602    plc_redirect(l_slice($slice_id));
603    break;
604  }
605
606  case 'remove-persons-from-slice': {
607    $slice_id = intval ($_POST['slice_id']);     
608    $person_ids = $_POST['person_ids'];
609    
610    $slice_name = "";
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"];
615    }
616    $notify_subject = "Removed from slice: " . $slice_name;
617    $notify_body = sprintf("You have been removed from the slice %s.
618
619 Our support team will be glad to answer any question that you might have.
620 ",$slice_name);
621    $notify_person_ids = array();
622    
623    $success=true;
624    $counter=0;
625    foreach( $person_ids as $person_id ) {
626      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
627        $success=false;
628      else {
629          array_push($notify_person_ids, intval($person_id));
630        $counter++;
631      }
632    }
633    if ($success) {
634      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
635      drupal_set_message ("Deleted $counter person(s)");
636    }
637    else
638      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
639    plc_redirect(l_slice($slice_id) . " &show_persons=true");
640    break;
641  }
642
643  case 'add-persons-in-slice': {
644    $slice_id = intval ($_POST['slice_id']);     
645    $person_ids = $_POST['person_ids'];
646
647    $slice_name = "";
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"];
652    }
653    $notify_subject = "Added to slice: " . $slice_name;
654    $notify_body = sprintf("You have been added to the slice %s as a user.
655
656 You can go to your slice page following the link below:
657 https://%s:%d/db/slices/index.php?id=%d
658
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();
662    
663    $success=true;
664    $counter=0;
665    foreach ($person_ids as $person_id) {
666      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
667        $success=false;
668      else {
669        array_push($notify_person_ids, intval($person_id));
670        $counter++;
671      }
672    }
673    if ($success) {
674      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
675      drupal_set_message ("Added $counter person(s)");
676    }
677    else
678      drupal_set_error ("Could not add all selected persons, only $counter were added");
679    plc_redirect(l_slice($slice_id) . "&show_persons=true" );
680    break;
681  }
682
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);
687    
688    if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1) 
689      drupal_set_message ("Removed $count node(s)");
690    else
691      drupal_set_error ("Could not remove selected nodes");
692    plc_redirect(l_slice_nodes($slice_id));
693    break;
694  }
695
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)");
702    else
703      drupal_set_error ("Could not add all selected nodes");
704    plc_redirect(l_slice_nodes($slice_id));
705    break;
706  }
707
708  case 'delete-slice-tags': {
709    $slice_id = intval($_POST['slice_id']);
710    $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
711    $count = 0;
712    $success = true;
713    foreach($slice_tag_ids as $slice_tag_id) {
714      if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
715      else {
716        drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
717        $success = false;
718      }
719    }
720    if ($success)
721      drupal_set_message ("Deleted $count slice tag(s)");
722    plc_redirect(l_slice($slice_id) . "&show_tags=true" );
723    break;
724  }
725   
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']);
732   
733    $result = null;
734    if ($node_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);
738    } else {
739      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
740    }
741    if ($result)
742      drupal_set_message ("Added slice tag.");
743    else 
744        drupal_set_error("Could not add slice tag");
745    if ($_POST['sliver_action'])
746        plc_redirect(l_sliver($node_id,$slice_id));
747    else
748        plc_redirect(l_slice($slice_id) . "&show_tags=true" );
749    break;
750  }
751
752 //////////////////////////////////////////////////////////// tag types
753
754  case 'update-tag-type': {
755   // get post vars 
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'];
761   
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,
767                             );
768
769    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
770      drupal_set_message ("Tag type $tagname updated");
771    else 
772      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
773    plc_redirect(l_tag($tag_type_id));
774    break;
775  }
776
777  case 'add-tag-type': {
778   // get post vars 
779    $tagname = $_POST['tagname'];
780    $min_role_id= intval( $_POST['min_role_id'] );
781    $description= $_POST['description'];  
782    $category= $_POST['category'];  
783   
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,
789                             );
790
791   // Add it!
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");
795    else
796      drupal_set_error ("Could not create tag type $tagname");
797    plc_redirect( l_tags());
798    break;
799  }
800
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");
805      return;
806    }
807    $success=true;
808    $counter=0;
809    foreach ($tag_type_ids as $tag_type_id) 
810      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
811        $success=false;
812      else
813        $counter++;
814    if ($success) 
815      drupal_set_message ("Deleted $counter tag(s)");
816    else
817      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
818    plc_redirect (l_tags());
819    break;
820  }
821
822  case 'remove-roles-from-tag-type' : {
823    $tag_type_id=$_POST['tag_type_id'];
824    $role_ids=$_POST['role_ids'];
825    if ( ! $role_ids) {
826      drupal_set_error("You have not selected role(s) to remove");
827    } else {
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");
831    }
832    plc_redirect (l_tag_roles($tag_type_id));
833  }
834      
835  case 'add-role-to-tag-type' : {
836    $tag_type_id=$_POST['tag_type_id'];
837    $role_id=$_POST['role_id'];
838    if ( ! $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");
842    }
843    plc_redirect (l_tag_roles($tag_type_id));
844  }
845
846 //////////////////////////////////////// tags   
847  case 'set-tag-on-node': 
848  case 'set-tag-on-interface': {
849    
850    $node_mode = false;
851    if ($action == 'set-tag-on-node') $node_mode=true;
852
853    if ($node_mode)
854      $node_id = intval($_POST['node_id']);
855    else 
856      $interface_id=intval($_POST['interface_id']);
857    $tag_type_id = intval($_POST['tag_type_id']);
858    $value = $_POST['value'];
859
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.");
863    } else {
864      if ($node_mode) 
865        $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
866      else
867        $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
868      if ( count ($tags) == 1) {
869        $tag=$tags[0];
870        if ($node_mode) {
871          $tag_id=$tag['node_tag_id'];
872          $result=$api->UpdateNodeTag($tag_id,$value);
873        } else {
874          $tag_id=$tag['interface_tag_id'];
875          $result=$api->UpdateInterfaceTag($tag_id,$value);
876        }
877        if ($result == 1) 
878          drupal_set_message ("Updated tag, new value = $value");
879        else
880          drupal_set_error ("Could not update tag");
881      } else {
882        if ($node_mode)
883          $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
884        else
885          $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
886        if ($tag_id) 
887          drupal_set_message ("Created tag, new value = $value");
888        else
889          drupal_set_error ("Could not create tag");
890      }
891    }
892    
893    if ($node_mode)
894      plc_redirect (l_node_tags($node_id));
895    else
896      plc_redirect (l_interface_tags($interface_id));
897  }
898
899  case 'delete-node-tags' : 
900  case 'delete-interface-tags' : {
901
902    $node_mode = false;
903    if ($action == 'delete-node-tags') $node_mode=true;
904
905    if ($node_mode)
906      $tag_ids=$_POST['node_tag_ids'];
907    else
908      $tag_ids=$_POST['interface_tag_ids'];
909
910    if ( ! $tag_ids) {
911      drupal_set_message("action=$action - No tag selected");
912      return;
913    }
914    $success=true;
915    $counter=0;
916    foreach( $tag_ids as $tag_id ) {
917      if ($node_mode)
918        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
919      else
920        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
921      if ($retcod != 1) 
922        $success=false;
923      else
924        $counter++;
925    }
926    if ($success) 
927      drupal_set_message ("Deleted $counter tag(s)");
928    else
929      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
930    if ($node_mode)
931      plc_redirect(l_node_tags($_POST['node_id']));
932    else
933      plc_redirect(l_interface_tags($_POST['interface_id']));
934  }
935
936 //////////////////////////////////////// nodegroups
937  case 'update-nodegroup': {
938    $nodegroup_id = $_POST['nodegroup_id'];
939    $groupname = $_POST['groupname'];
940    $value = $_POST['value'];
941
942    $fields=array();
943    $fields['groupname']=$groupname;
944    $fields['value']=$value;
945    if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1) 
946      drupal_set_message ('Nodegroup updated');
947    else 
948      drupal_set_error ("Could not update nodegroup $groupname");
949    
950    plc_redirect(l_nodegroup($nodegroup_id));
951
952  }
953
954  case 'add-nodegroup': {
955    $groupname=$_POST['groupname'];
956    if ( ! $groupname ) {
957      drupal_set_error ('Empty groupname');
958      plc_redirect (l_nodegroups());
959    }
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());
964    }
965    $value=$_POST['value'];
966    if ( ! $value ) {
967      drupal_set_message ("Empty value.. let's see ..");
968    }
969    if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0) 
970      drupal_set_message ("Nodegroup $groupname created");
971    else
972      drupal_set_error ("Could not create nodegroup $groupname");
973
974    plc_redirect (l_nodegroups());
975    break;
976  }
977
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());
983    }
984    $success=true;
985    $counter=0;
986    foreach ($nodegroup_ids as $nodegroup_id) 
987      if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) 
988        $success=false;
989      else
990        $counter++;
991    if ($success) 
992      drupal_set_message ("Deleted $counter group(s)");
993    else
994      drupal_set_error ("Could not delete all selected groups, only $counter were removed");
995    plc_redirect (l_nodegroups());
996    break;
997  }
998
999 //////////////////////////////////////// leases
1000  case 'manage-leases': {
1001    $actions=json_decode($_POST['actions']);
1002    $add_requested=0;
1003    $add_done=0;
1004    $del_requested=0;
1005    $del_done=0;
1006    $errors=array();
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) {
1024          $del_done += 1;
1025        } else {
1026          $errors []= "Could not delete lease " . $lease_id;
1027        }
1028      } else {
1029        $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1030      }
1031    }
1032    
1033    if (count($errors)==0) {
1034      echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1035    } else {
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)");
1040    }     
1041
1042    break;
1043  }
1044
1045 ////////////////////////////////////////
1046
1047  case 'debug': {
1048    plc_debug('GET',$_GET);
1049    plc_debug('POST',$_POST);
1050    plc_debug('FILES',$_FILES);
1051    return;
1052  }
1053
1054  default: {
1055    plc_error ("Unknown action $action in actions.php");
1056    return;
1057  }
1058
1059  }
1060
1061 ?>