fixed roles management in the person page
[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
55 //////////////////////////////////////// interfaces
56 $known_actions []= "delete-interfaces"; 
57 //      expects:        interface_ids
58 $known_actions []="add-interface";
59 //      expects:        node_id & interface details
60 $known_actions []="new-interface";
61 //      expects:        node_id 
62 $known_actions []="update-interface";
63 //      expects:        interface_id & interface details
64
65 //////////////////////////////////////// sites
66 $known_actions []= "delete-site";       
67 //      expects:        site_id
68 $known_actions []= "expire-all-slices-in-site";
69 //      expects:        slice_ids
70 $known_actions []= "update-site";
71 //      expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
72
73 //////////////////////////////////////// slices
74 $known_actions []= "delete-slice";
75 //      expects:        slice_id
76 $known_actions []= "update-slice";      
77 //      expects:        slice_id, name, description, url
78 $known_actions []= "renew-slice";
79 //      expects:        slice_id & expires
80 $known_actions []= 'remove-persons-from-slice';
81 //      expects:        slice_id & person_ids
82 $known_actions []= 'add-persons-in-slice';
83 //      expects:        slice_id & person_ids
84 $known_actions []= 'remove-nodes-from-slice';
85 //      expects:        slice_id & node_ids
86 $known_actions []= 'add-nodes-in-slice';
87 //      expects:        slice_id & node_ids
88 $known_actions []= 'delete-slice-tags';
89 //      expects:        slice_tag_id
90 $known_actions []= 'add-slice-tag';
91 //      expects:        slice_id & tag_type_id & node_id & nodegroup_id
92
93 //////////////////////////////////////// tag types
94 $known_actions []= "update-tag-type";
95 //      expects:        tag_type_id & name & description & category & min_role_id  
96 $known_actions []= "add-tag-type";
97 //      expects:        tag_type_id & tagname & description & category & min_role_id  
98 $known_actions []= "delete-tag-types";
99 //      expects:        tag_type_ids
100
101 //////////////////////////////////////// tags
102 $known_actions []= "set-tag-on-node";
103 //      expects:        node_id tagname value
104 $known_actions []= "set-tag-on-interface";
105 //      expects:        interface_id tagname value
106 $known_actions []= "delete-node-tags";
107 //      expects:        node_id & node_tag_ids
108 $known_actions []= "delete-interface-tags";
109 //      expects:        interface_id & interface_tag_ids
110
111 //////////////////////////////////////// nodegroups
112 $known_actions []= "update-nodegroup";
113 //      expects nodegroup_id groupname value
114 $known_actions []= "add-nodegroup";
115 //      expects groupname, tag_type_id, value
116 $known_actions []= 'delete-nodegroups';
117 //      expects nodegroup_ids
118
119 ////////////////////////////////////////////////////////////
120 $interface_details= array ('method','type', 'ip', 'gateway', 'network', 
121                            'broadcast', 'netmask', 'dns1', 'dns2', 
122                            'hostname', 'mac', 'bwlimit' );
123
124 //////////////////////////////
125 // sometimes we don't set 'action', but use the submit button name instead
126 // so if 'action' not set, see if $_POST has one of the actions as a key
127 if ($_POST['action']) 
128   $action=$_POST['action'];
129 else 
130   foreach ($known_actions as $known_action) 
131     if ($_POST[$known_action]) {
132       $action=$known_action;
133       break;
134     }
135
136 //uncomment for debugging incoming data
137 //$action='debug';
138
139 $person_id = $_POST['person_id'];       // usually needed
140
141 if ( ! $action ) {
142   drupal_set_message ("actions.php: action not set or not in known_actions");
143   plc_debug('POST',$_POST);
144   return;
145  }
146
147 switch ($action) {
148
149  case 'add-person-to-site': {
150    $site_id = $_POST['site_id'];
151    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
152    plc_redirect (l_person($person_id));
153  }
154
155  case 'remove-person-from-sites': {
156    $site_ids = $_POST['site_ids'];
157    if ( ! $site_ids) {
158      drupal_set_message("action=$action - No site selected");
159      return;
160    }
161    foreach ( $site_ids as $site_id ) {
162      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
163    }
164    plc_redirect (l_person($person_id));
165  }
166
167  case 'remove-roles-from-person' : {
168    $role_ids=$_POST['role_ids'];
169    if ( ! $role_ids) {
170      drupal_set_error("You have not selected role(s) to remove");
171    } else {
172      foreach( $role_ids as $role_id)  
173        if ( $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) ) != 1 ) 
174          drupal_set_error ("Could not remove role $role_id from person $person_id");
175    }
176    plc_redirect (l_person_roles($person_id));
177  }
178      
179  case 'add-role-to-person' : {
180    $role_id=$_POST['role_id'];
181    if ( ! $role_id) {
182      drupal_set_error ("You have not selected a role to add");
183    } else if ($api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) ) != 1) {
184      drupal_set_error("Could not add role $role_id to person $person_id");
185    }
186    plc_redirect (l_person_roles($person_id));
187  }
188
189  case 'enable-person' : {
190    $fields = array( "enabled"=>true );
191    $api->UpdatePerson( intval( $person_id ), $fields );
192    plc_redirect (l_person($person_id));
193  }
194
195  case 'disable-person' : {
196    $fields = array( "enabled"=>false );
197    $api->UpdatePerson( intval( $person_id ), $fields );
198    plc_redirect (l_person($person_id));
199  }
200
201  case 'become-person' : {
202    $plc->BecomePerson (intval($person_id));
203    plc_redirect (l_person(intval($person_id)));
204  }
205
206  case 'delete-person' : {
207   $api->DeletePerson( intval( $person_id ) );
208    plc_redirect (l_persons());
209  }
210
211  case 'delete-keys' : {
212    $key_ids=$_POST['key_ids'];
213    if ( ! $key_ids) {
214      drupal_set_message("action=$action - No key selected");
215      return;
216    }
217    $success=true;
218    $counter=0;
219    foreach( $key_ids as $key_id ) {
220      if ($api->DeleteKey( intval( $key_id )) != 1) 
221        $success=false;
222      else
223        $counter++;
224    }
225    if ($success) 
226      drupal_set_message ("Deleted $counter key(s)");
227    else
228      drupal_set_error ("Could not delete all selected keys, only $counter were removed");
229    plc_redirect(l_person($person_id));
230  }
231
232
233  case 'upload-key' : {
234    if ( ! isset( $_FILES['key'] ) ) {
235      drupal_set_message ("action=$action, no key file set");
236      return;
237    }
238    
239    $key_file= $_FILES['key']['tmp_name'];
240    if ( ! $key_file ) {
241      plc_error("Please select a valid SSH key file to upload");
242      return;
243    } 
244    $fp = fopen( $key_file, "r" );
245    $key = "";
246    if( ! $fp ) {
247      plc_error("Unable to open key file $key_file");
248      return;
249    }
250    // opened the key file, read the one line of contents
251    // The POST operation always creates a file even if the filename
252    // the user specified was garbage.  If there was some problem
253    // with the source file, we'll get a zero length read here.
254    $key = fread($fp, filesize($key_file));
255    fclose($fp);
256    
257    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
258    
259    if ( $key_id >= 1) 
260      drupal_set_message ("New key added");
261    else
262      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
263    
264    plc_redirect(l_person($person_id));
265  }
266
267  case 'update-person': {
268    $person_id=$_POST['person_id'];
269    // attempt to update this person
270    $first_name= $_POST['first_name'];
271    $last_name= $_POST['last_name'];
272    $title= $_POST['title'];
273    $email= $_POST['email'];
274    $phone= $_POST['phone'];
275    $url= $_POST['url'];
276    $bio= str_replace("\r", "", $_POST['bio']);
277    $password1= $_POST['password1'];
278    $password2= $_POST['password2'];
279
280    if( $password1 != $password2 ) {
281      drupal_set_error ("The passwords do not match");
282      plc_redirect(l_person($person_id));
283   }
284
285    $fields= array();
286    $fields['first_name']= $first_name;
287    $fields['last_name']= $last_name;
288    $fields['title']= $title;
289    $fields['email']= $email;
290    $fields['phone']= $phone;
291    $fields['url']= $url;
292    $fields['bio']= $bio;
293                 
294    if ( $password1 != "" )
295      $fields['password']= $password1;
296     
297     if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
298       drupal_set_message("$first_name $last_name updated");
299     else 
300       drupal_set_error ("Could not update person $person_id" . $api->error());
301
302     plc_redirect(l_person($person_id));
303     break;
304   }
305
306 //////////////////////////////////////////////////////////// nodes
307  case 'node-boot-state': {
308    $node_id=intval($_POST['node_id']);
309    $boot_state=$_POST['boot_state'];
310    $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
311    if ($result==1) {
312      drupal_set_message("boot state updated");
313      plc_redirect (l_node($node_id));
314    } else {
315      drupal_set_error("Could not set boot_state '$boot_state'");
316    }
317    break;
318  }
319
320  case 'delete-node': {
321    $node_id=intval($_POST['node_id']);
322    $result=$api->DeleteNode( intval( $node_id ) );
323    if ($api==1) {
324      drupal_set_message("Node $node_id deleted");
325      plc_redirect (l_nodes());
326    } else {
327      drupal_set_error ("Could not delete node $node_id");
328    }
329    break;
330  }
331
332  case 'update-node': {
333    $node_id=intval($_POST['node_id']);
334    $hostname= $_POST['hostname'];
335    $model= $_POST['model'];
336
337    $fields= array( "hostname"=>$hostname, "model"=>$model );
338    $api->UpdateNode( $node_id, $fields );
339    $error= $api->error();
340
341    if( empty( $error ) ) {
342      drupal_set_message("Update node $hostname");
343      plc_redirect(l_node($node_id));
344    } else {
345      drupal_set_error($error);
346    }
347    break;
348  }
349
350  // this code will ensure that at most one PCU gets attached to the node
351  case 'attach-pcu': {
352    $node_id=intval($_POST['node_id']);
353    $pcu_id=intval($_POST['pcu_id']);
354    $port=intval($_POST['port']);
355    // always start with deleting former PCUs
356    $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
357    $former_pcu_ids = $nodes[0]['pcu_ids'];
358    if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
359        if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1) 
360          drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
361        else 
362          drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
363      }
364    // re-attach only if provided pcu_id >=0
365    if ($pcu_id >= 0) {
366      if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
367        drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
368      else
369        drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
370    } else {
371      drupal_set_message ('Detached node from all PCUs');
372    }
373    
374    plc_redirect(l_node($node_id));
375    break;
376  }
377    
378
379 //////////////////////////////////////////////////////////// interfaces
380  case 'delete-interfaces' : {
381    $interface_ids=$_POST['interface_ids'];
382    if ( ! $interface_ids) {
383      drupal_set_message("action=$action - No interface selected");
384      return;
385    }
386    $success=true;
387    $counter=0;
388    foreach( $interface_ids as $interface_id ) {
389      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
390        $success=false;
391      else
392        $counter++;
393    }
394    if ($success) 
395      drupal_set_message ("Deleted $counter interface(s)");
396    else
397      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
398    plc_redirect(l_node($_POST['node_id']));
399  }
400  case 'new-interface': {
401    plc_redirect(l_interface_add($_POST['node_id']));
402  }
403  case 'add-interface': {
404    $node_id=$_POST['node_id'];
405    foreach ($interface_details as $field) {
406      $interface[$field]= $_POST[$field];
407      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
408        $interface[$field]= intval( $interface[$field] );
409      }
410    }
411    $interface_id =$api->AddInterface( intval( $node_id ), $interface );
412    if ($interface_id >0 ) {
413      $api->begin();
414          $api->AddInterfaceTag($interface_id,"alias",strval($interface_id));
415          $api->AddInterfaceTag($interface_id,"ifname","eth0");
416      list($id1, $id2) = $api->commit();
417      if ( $id1 > 0 && $id2 > 0 ) {
418          drupal_set_message ("Interface $interface_id added into node $node_id");
419      } else {
420          drupal_set_error ("Could not add interface tags to interface $interface_id");
421      }
422    } else {
423      drupal_set_error ("Could not create interface");
424    }
425    plc_redirect (l_node($node_id));
426  }
427    
428  case 'update-interface': {
429    $interface_id=$_POST['interface_id'];
430    foreach ($interface_details as $field) {
431      $interface[$field]= $_POST[$field];
432      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
433        if ( intval($interface[$field]) != 0 ) {
434            $interface[$field]= intval( $interface[$field]);
435        } elseif ($field=='bwlimit' ) {
436            $interface[$field] = NULL;
437        }
438      }
439    }
440    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
441    if ($result == 1 ) 
442      drupal_set_message ("Interface $interface_id updated");
443    else
444      drupal_set_error ("Could not update interface");
445    plc_redirect (l_interface($interface_id));
446  }
447    
448 //////////////////////////////////////////////////////////// sites
449  case 'delete-site': {
450    $site_id = intval($_POST['site_id']);
451    if ($api->DeleteSite($site_id) ==1) 
452      drupal_set_message ("Site $site_id deleted");
453    else
454      drupal_set_error("Failed to delete site $site_id");
455    plc_redirect (l_sites());
456    break;
457  }
458
459  case 'expire-all-slices-in-site': {
460    // xxx todo
461    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
462    return;
463
464    //// old code from sites/expire.php
465    $sites = $api->GetSites( array( intval( $site_id )));
466    $site=$sites[0];
467    // xxx why not 'now?'
468    $expiration= strtotime( $_POST['expires'] );
469    // loop through all slices for site
470    foreach ($site['slice_ids'] as $slice_id) {
471      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
472    }
473    // update site to not allow slice creation or renewal
474    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
475    plc_redirect (l_site($site_id));
476    break;
477  }
478
479  case 'update-site': {
480    $site_id=intval($_POST['site_id']);
481    $name= $_POST['name'];
482    $abbreviated_name= $_POST['abbreviated_name'];
483    $url= $_POST['url'];
484    $latitude= floatval($_POST['latitude']);
485    $longitude= floatval($_POST['longitude']);
486    //$max_slivers= $_POST['max_slivers'];
487    
488    $fields= array( "name" => $name, 
489                    "abbreviated_name" => $abbreviated_name, 
490                    "url" => $url, 
491                    "latitude" => floatval( $latitude ), 
492                    "longitude" => floatval( $longitude ));
493
494    if ($_POST['login_base']) 
495      $fields['login_base'] = $_POST['login_base'];
496    if (isset($_POST['max_slices']))
497      $fields['max_slices'] = intval($_POST['max_slices']);
498    if (isset($_POST['enabled'])) {
499      $fields['enabled'] = (bool)$_POST['enabled'];
500    }
501    
502    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
503    if ($retcod == 1) 
504      drupal_set_message("Site $name updated");
505    else 
506      drupal_set_error ("Could not update site $site_id");
507      
508    plc_redirect(l_site($site_id));
509    break;
510  }
511
512 //////////////////////////////////////////////////////////// slices
513  case 'delete-slice': {
514    $slice_id = $_POST['slice_id'];
515    if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
516      drupal_set_message("Slice $slice_id deleted");
517      plc_redirect(l_slices());
518    } else {
519      drupal_set_error("Could not delete slice $slice_id " . $api->error());
520    }
521    break;
522  }
523      
524  case 'update-slice': {
525    $slice_id = $_POST['slice_id'];
526    $name = $_POST['name'];
527    $description= $_POST['description'];
528    $url= $_POST['url'];
529
530    $fields= array( "description"=>$description, "url"=>$url );
531    $api->UpdateSlice( intval( $slice_id ), $fields );
532    $error= $api->error();
533
534    if( empty( $error ) ) {
535      drupal_set_message("Update slice $name");
536      plc_redirect(l_slice($slice_id));
537    } else {
538      drupal_set_error($error);
539    }
540    break;
541  }
542
543  case 'renew-slice': {
544    $slice_id = intval ($_POST['slice_id']);     
545    $expires = intval ($_POST['expires']);
546    // 8 weeks from now
547    // xxx
548    $now=mktime();
549    $WEEK=7*24*3600;
550    $WEEKS=8;
551    $MAX_FUTURE=$WEEKS*$WEEK;
552    if ( ($expires-$now) > $MAX_FUTURE) {
553      drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
554      plc_redirect(l_slice($slice_id));
555    }
556    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
557      drupal_set_message("Slice renewed");
558    else
559      drupal_set_error("Could not update slice $slice_id");
560    plc_redirect(l_slice($slice_id));
561    break;
562  }
563
564  case 'remove-persons-from-slice': {
565    $slice_id = intval ($_POST['slice_id']);     
566    $person_ids = $_POST['person_ids'];
567    
568    $slice_name = "";
569    $tmp_slices = $api->GetSlices($slice_id, array("name"));
570    if (count($tmp_slices) > 0) {
571        $tmp_slice = $tmp_slices[0];
572        $slice_name = $tmp_slice["name"];
573    }
574    $notify_subject = "Removed from slice: " . $slice_name;
575    $notify_body = sprintf("You have been removed from the slice %s.
576
577 Our support team will be glad to answer any question that you might have.
578 ",$slice_name);
579    $notify_person_ids = array();
580    
581    $success=true;
582    $counter=0;
583    foreach( $person_ids as $person_id ) {
584      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
585        $success=false;
586      else {
587          array_push($notify_person_ids, intval($person_id));
588        $counter++;
589      }
590    }
591    if ($success) {
592      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
593      drupal_set_message ("Deleted $counter person(s)");
594    }
595    else
596      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
597    plc_redirect(l_slice($slice_id) . " &show_persons=true");
598    break;
599  }
600
601  case 'add-persons-in-slice': {
602    $slice_id = intval ($_POST['slice_id']);     
603    $person_ids = $_POST['person_ids'];
604
605    $slice_name = "";
606    $tmp_slices = $api->GetSlices($slice_id, array("name"));
607    if (count($tmp_slices) > 0) {
608      $tmp_slice = $tmp_slices[0];
609      $slice_name = $tmp_slice["name"];
610    }
611    $notify_subject = "Added to slice: " . $slice_name;
612    $notify_body = sprintf("You have been added to the slice %s as a user.
613
614 You can go to your slice page following the link below:
615 https://%s:%d/db/slices/index.php?id=%d
616
617 Our support team will be glad to answer any question that you might have.
618 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
619    $notify_person_ids = array();
620    
621    $success=true;
622    $counter=0;
623    foreach ($person_ids as $person_id) {
624      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
625        $success=false;
626      else {
627        array_push($notify_person_ids, intval($person_id));
628        $counter++;
629      }
630    }
631    if ($success) {
632      $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
633      drupal_set_message ("Added $counter person(s)");
634    }
635    else
636      drupal_set_error ("Could not add all selected persons, only $counter were added");
637    plc_redirect(l_slice($slice_id) . "&show_persons=true" );
638    break;
639  }
640
641  case 'remove-nodes-from-slice': {
642    $slice_id = intval ($_POST['slice_id']);     
643    $node_ids = array_map("intval",$_POST['node_ids']);
644    $count=count($node_ids);
645    
646    if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1) 
647      drupal_set_message ("Removed $count node(s)");
648    else
649      drupal_set_error ("Could not remove selected nodes");
650    plc_redirect(l_slice_nodes($slice_id));
651    break;
652  }
653
654  case 'add-nodes-in-slice': {
655    $slice_id = intval ($_POST['slice_id']);     
656    $node_ids = array_map("intval",$_POST['node_ids']);
657    $count=count($node_ids);
658    if ($api->AddSliceToNodes($slice_id,$node_ids) == 1) 
659      drupal_set_message ("Added $count node(s)");
660    else
661      drupal_set_error ("Could not add all selected nodes");
662    plc_redirect(l_slice_nodes($slice_id));
663    break;
664  }
665
666  case 'delete-slice-tags': {
667    $slice_id = intval($_POST['slice_id']);
668    $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
669    $count = 0;
670    $success = true;
671    foreach($slice_tag_ids as $slice_tag_id) {
672      if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
673      else {
674        drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
675        $success = false;
676      }
677    }
678    if ($success)
679      drupal_set_message ("Deleted $count slice tag(s)");
680    plc_redirect(l_slice($slice_id) . "&show_tags=true" );
681    break;
682  }
683   
684  case 'add-slice-tag': {
685    $slice_id = intval($_POST['slice_id']);
686    $tag_type_id = intval($_POST['tag_type_id']);
687    $value = $_POST['value'];
688    $node_id = intval($_POST['node_id']);
689    $nodegroup_id = intval($_POST['nodegroup_id']);
690   
691    $result = null;
692    if ($node_id) {
693      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
694    } elseif ($nodegroup_id) {
695      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
696    } else {
697      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
698    }
699    if ($result)
700      drupal_set_message ("Added slice tag.");
701    else 
702        drupal_set_error("Could not add slice tag");
703    if ($_POST['sliver_action'])
704        plc_redirect(l_sliver($node_id,$slice_id));
705    else
706        plc_redirect(l_slice($slice_id) . "&show_tags=true" );
707    break;
708  }
709
710 //////////////////////////////////////////////////////////// tag types
711
712  case 'update-tag-type': {
713   // get post vars 
714    $tag_type_id= intval( $_POST['tag_type_id'] );
715    $tagname = $_POST['tagname'];
716    $min_role_id= intval( $_POST['min_role_id'] );
717    $description= $_POST['description'];  
718    $category= $_POST['category'];
719   
720    // make tag_type_fields dict
721    $tag_type_fields= array( "min_role_id" => $min_role_id, 
722                             "tagname" => $tagname, 
723                             "description" => $description,
724                             "category" => $category,
725                             );
726
727    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
728      drupal_set_message ("Tag type $tagname updated");
729    else 
730      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
731    plc_redirect(l_tag($tag_type_id));
732    break;
733  }
734
735  case 'add-tag-type': {
736   // get post vars 
737    $tagname = $_POST['tagname'];
738    $min_role_id= intval( $_POST['min_role_id'] );
739    $description= $_POST['description'];  
740    $category= $_POST['category'];  
741   
742    // make tag_type_fields dict
743    $tag_type_fields= array( "min_role_id" => $min_role_id, 
744                             "tagname" => $tagname, 
745                             "description" => $description,
746                             "category" => $category,
747                             );
748
749   // Add it!
750    $tag_type_id=$api->AddTagType( $tag_type_fields );
751    if ($tag_type_id > 0) 
752      drupal_set_message ("tag type $tag_type_id created");
753    else
754      drupal_set_error ("Could not create tag type $tagname");
755    plc_redirect( l_tags());
756    break;
757  }
758
759  case 'delete-tag-types': {
760    $tag_type_ids = $_POST['tag_type_ids'];
761    if ( ! $tag_type_ids) {
762      drupal_set_message("action=$action - No tag selected");
763      return;
764    }
765    $success=true;
766    $counter=0;
767    foreach ($tag_type_ids as $tag_type_id) 
768      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
769        $success=false;
770      else
771        $counter++;
772    if ($success) 
773      drupal_set_message ("Deleted $counter tag(s)");
774    else
775      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
776    plc_redirect (l_tags());
777    break;
778  }
779
780 //////////////////////////////////////// tags   
781  case 'set-tag-on-node': 
782  case 'set-tag-on-interface': {
783    
784    $node_mode = false;
785    if ($action == 'set-tag-on-node') $node_mode=true;
786
787    if ($node_mode)
788      $node_id = intval($_POST['node_id']);
789    else 
790      $interface_id=intval($_POST['interface_id']);
791    $tag_type_id = intval($_POST['tag_type_id']);
792    $value = $_POST['value'];
793
794    $tag_types=$api->GetTagTypes(array($tag_type_id));
795    if (count ($tag_types) != 1) {
796      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
797    } else {
798      if ($node_mode) 
799        $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
800      else
801        $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
802      if ( count ($tags) == 1) {
803        $tag=$tags[0];
804        if ($node_mode) {
805          $tag_id=$tag['node_tag_id'];
806          $result=$api->UpdateNodeTag($tag_id,$value);
807        } else {
808          $tag_id=$tag['interface_tag_id'];
809          $result=$api->UpdateInterfaceTag($tag_id,$value);
810        }
811        if ($result == 1) 
812          drupal_set_message ("Updated tag, new value = $value");
813        else
814          drupal_set_error ("Could not update tag");
815      } else {
816        if ($node_mode)
817          $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
818        else
819          $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
820        if ($tag_id) 
821          drupal_set_message ("Created tag, new value = $value");
822        else
823          drupal_set_error ("Could not create tag");
824      }
825    }
826    
827    if ($node_mode)
828      plc_redirect (l_node($node_id));
829    else
830      plc_redirect (l_interface($interface_id));
831  }
832
833  case 'delete-node-tags' : 
834  case 'delete-interface-tags' : {
835
836    $node_mode = false;
837    if ($action == 'delete-node-tags') $node_mode=true;
838
839    if ($node_mode)
840      $tag_ids=$_POST['node_tag_ids'];
841    else
842      $tag_ids=$_POST['interface_tag_ids'];
843
844    if ( ! $tag_ids) {
845      drupal_set_message("action=$action - No tag selected");
846      return;
847    }
848    $success=true;
849    $counter=0;
850    foreach( $tag_ids as $tag_id ) {
851      if ($node_mode)
852        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
853      else
854        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
855      if ($retcod != 1) 
856        $success=false;
857      else
858        $counter++;
859    }
860    if ($success) 
861      drupal_set_message ("Deleted $counter tag(s)");
862    else
863      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
864    if ($node_mode)
865      plc_redirect(l_node($_POST['node_id']));
866    else
867      plc_redirect(l_interface($_POST['interface_id']));
868  }
869
870 //////////////////////////////////////// nodegroups
871  case 'update-nodegroup': {
872    $nodegroup_id = $_POST['nodegroup_id'];
873    $groupname = $_POST['groupname'];
874    $value = $_POST['value'];
875
876    $fields=array();
877    $fields['groupname']=$groupname;
878    $fields['value']=$value;
879    if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1) 
880      drupal_set_message ('Nodegroup updated');
881    else 
882      drupal_set_error ("Could not update nodegroup $groupname");
883    
884    plc_redirect(l_nodegroup($nodegroup_id));
885
886  }
887
888  case 'add-nodegroup': {
889    $groupname=$_POST['groupname'];
890    if ( ! $groupname ) {
891      drupal_set_error ('Empty groupname');
892      plc_redirect (l_nodegroups());
893    }
894    $tag_type_id=intval($_POST['tag_type_id']);
895    if ( ! $tag_type_id ) {
896      drupal_set_error ('You must select a tag in the dropdown list');
897      plc_redirect (l_nodegroups());
898    }
899    $value=$_POST['value'];
900    if ( ! $value ) {
901      drupal_set_message ("Empty value.. let's see ..");
902    }
903    if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0) 
904      drupal_set_message ("Nodegroup $groupname created");
905    else
906      drupal_set_error ("Could not create nodegroup $groupname");
907
908    plc_redirect (l_nodegroups());
909    break;
910  }
911
912  case 'delete-nodegroups': {
913    $nodegroup_ids=$_POST['nodegroup_ids'];
914    if ( ! $nodegroup_ids ) {
915      drupal_set_message("action=delete-nodegroups - No group selected");
916      plc_redirect(l_nodegroups());
917    }
918    $success=true;
919    $counter=0;
920    foreach ($nodegroup_ids as $nodegroup_id) 
921      if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) 
922        $success=false;
923      else
924        $counter++;
925    if ($success) 
926      drupal_set_message ("Deleted $counter group(s)");
927    else
928      drupal_set_error ("Could not delete all selected groups, only $counter were removed");
929    plc_redirect (l_nodegroups());
930    break;
931  }
932
933 ////////////////////////////////////////
934
935  case 'debug': {
936    plc_debug('GET',$_GET);
937    plc_debug('POST',$_POST);
938    plc_debug('FILES',$_FILES);
939    return;
940  }
941
942  default: {
943    plc_error ("Unknown action $action in actions.php");
944    return;
945  }
946
947  }
948
949 ?>