add tags sections to sites and persons
[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 require_once 'plc_api.php';
9 global $plc, $api, $adm;
10
11 //print header
12 require_once 'plc_drupal.php';
13
14 // Common functions
15 require_once 'plc_functions.php';
16
17 $known_actions=array();
18 ////////////////////////////////////////////////////////////
19 // interface :
20 // (*) use POST 
21 // (*) set 'action' to one of the following
22 //////////////////////////////////////// persons
23 $known_actions []= "add-person-to-site";
24 //      expects:        person_id & site_id
25 $known_actions []= "remove-person-from-sites";
26 //      expects:        person_id & site_ids
27 $known_actions []= "remove-roles-from-person";
28 //      expects:        person_id & role_ids
29 $known_actions []= "add-role-to-person";
30 //      expects:        role_person_id & id
31 $known_actions []= "enable-person";
32 //      expects:        person_id
33 $known_actions []= "disable-person";
34 //      expects:        person_id
35 $known_actions []= "become-person";
36 //      expects:        person_id
37 $known_actions []= "delete-person";
38 //      expects:        person_id
39 $known_actions []= "delete-keys";
40 //      expects:        key_ids & person_id (for redirecting to the person's page)
41 $known_actions []= "upload-key";
42 //      expects:        person_id & $_FILES['key']
43 $known_actions []= "update-person";
44 //      expects:        person_id & first_name last_name title email phone url bio + [password1 password2]
45
46 //////////////////////////////////////// nodes
47 $known_actions []= "node-boot-state";   
48 //      expects:        node_id boot_state
49 $known_actions []= "delete-node";       
50 //      expects:        node_id
51 $known_actions []= "update-node";       
52 //      expects:        node_id, hostname, model, node_type
53 $known_actions []= "attach-pcu";
54 //      expects:        node_id, pcu_id, port (pcu_id <0 means detach)
55 $known_actions []= "reboot-node-with-pcu";
56 //      expects:        node_id
57
58 //////////////////////////////////////// interfaces
59 $known_actions []= "delete-interfaces"; 
60 //      expects:        interface_ids
61 $known_actions []="add-interface";
62 //      expects:        node_id & interface details
63 $known_actions []="new-interface";
64 //      expects:        node_id 
65 $known_actions []="update-interface";
66 //      expects:        interface_id & interface details
67
68 //////////////////////////////////////// sites
69 $known_actions []= "delete-site";       
70 //      expects:        site_id
71 $known_actions []= "expire-all-slices-in-site";
72 //      expects:        slice_ids
73 $known_actions []= "update-site";
74 //      expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
75
76 //////////////////////////////////////// slices
77 $known_actions []= "delete-slice";
78 //      expects:        slice_id
79 $known_actions []= "update-slice";      
80 //      expects:        slice_id, name, description, url
81 $known_actions []= "renew-slice";
82 //      expects:        slice_id & expires
83 $known_actions []= 'remove-persons-from-slice';
84 //      expects:        slice_id & person_ids
85 $known_actions []= 'add-persons-in-slice';
86 //      expects:        slice_id & person_ids
87 $known_actions []= 'remove-nodes-from-slice';
88 //      expects:        slice_id & node_ids
89 $known_actions []= 'add-nodes-in-slice';
90 //      expects:        slice_id & node_ids
91 $known_actions []= 'update-initscripts';
92 //      expects:        slice_id & name & previous-initscript & previous-initscript-code 
93 //                      & initscript & initscript-code
94 $known_actions []= 'delete-slice-tags';
95 //      expects:        slice_tag_id
96 $known_actions []= 'add-slice-tag';
97 //      expects:        slice_id & tag_type_id & node_id & nodegroup_id
98
99 //////////////////////////////////////// tag types
100 $known_actions []= "update-tag-type";
101 //      expects:        tag_type_id & name & description & category & min_role_id  
102 $known_actions []= "add-tag-type";
103 //      expects:        tag_type_id & tagname & description & category & min_role_id  
104 $known_actions []= "delete-tag-types";
105 //      expects:        tag_type_ids
106 $known_actions []= "remove-roles-from-tag-type";
107 //      expects:        tag_type_id & role_ids
108 $known_actions []= "add-role-to-tag-type";
109 //      expects:        tag_type_id_id & id
110
111 //////////////////////////////////////// tags
112 $known_actions []= "set-tag-on-node";
113 //      expects:        node_id tagname value
114 $known_actions []= "set-tag-on-site";
115 //      expects:        site_id tagname value
116 $known_actions []= "set-tag-on-person";
117 //      expects:        person_id tagname value
118 $known_actions []= "set-tag-on-interface";
119 //      expects:        interface_id tagname value
120 $known_actions []= "delete-node-tags";
121 //      expects:        node_id & node_tag_ids
122 $known_actions []= "delete-site-tags";
123 //      expects:        site_id & site_tag_ids
124 $known_actions []= "delete-person-tags";
125 //      expects:        person_id & person_tag_ids
126 $known_actions []= "delete-interface-tags";
127 //      expects:        interface_id & interface_tag_ids
128
129 //////////////////////////////////////// nodegroups
130 $known_actions []= "update-nodegroup";
131 //      expects nodegroup_id groupname value
132 $known_actions []= "add-nodegroup";
133 //      expects groupname, tag_type_id, value
134 $known_actions []= 'delete-nodegroups';
135 //      expects nodegroup_ids
136
137 //////////////////////////////////////// leases
138 $known_actions []= "manage-leases";
139 //      expects as 'actions' a list of 'action' of the form
140 //      either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
141 //      or     [ 'delete-leases', lease_id ]
142
143 ////////////////////////////////////////////////////////////
144 $interface_details= array ('method','type', 'ip', 'gateway', 'network', 
145                            'broadcast', 'netmask', 'dns1', 'dns2', 
146                            'hostname', 'mac', 'bwlimit' );
147
148 //////////////////////////////
149 // sometimes we don't set 'action', but use the submit button name instead
150 // so if 'action' not set, see if $_POST has one of the actions as a key
151 if ($_POST['action']) 
152   $action=$_POST['action'];
153 else 
154   foreach ($known_actions as $known_action) 
155     if ($_POST[$known_action]) {
156       $action=$known_action;
157       break;
158     }
159
160 //uncomment for debugging incoming data
161 //$action='debug';
162
163 $person_id = $_POST['person_id'];       // usually needed
164
165 if ( ! $action ) {
166   drupal_set_message ("actions.php: action not set or not in known_actions");
167   plc_debug('POST',$_POST);
168   return;
169  }
170
171 switch ($action) {
172
173  case 'add-person-to-site': {
174    $site_id = $_POST['site_id'];
175    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
176    plc_redirect (l_person($person_id));
177  }
178
179  case 'remove-person-from-sites': {
180    $site_ids = $_POST['site_ids'];
181    if ( ! $site_ids) {
182      drupal_set_message("action=$action - No site selected");
183      return;
184    }
185    foreach ( $site_ids as $site_id ) {
186      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
187    }
188    plc_redirect (l_person($person_id));
189  }
190
191  case 'remove-roles-from-person' : {
192    $role_ids=$_POST['role_ids'];
193    if ( ! $role_ids) {
194      drupal_set_error("You have not selected role(s) to remove");
195    } else {
196      foreach( $role_ids as $role_id)  
197        if ( $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) ) != 1 ) 
198          drupal_set_error ("Could not remove role $role_id from person $person_id");
199    }
200    plc_redirect (l_person_roles($person_id));
201  }
202      
203  case 'add-role-to-person' : {
204    $role_id=$_POST['role_id'];
205    if ( ! $role_id) {
206      drupal_set_error ("You have not selected a role to add");
207    } else if ($api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) ) != 1) {
208      drupal_set_error("Could not add role $role_id to person $person_id");
209    }
210    plc_redirect (l_person_roles($person_id));
211  }
212
213  case 'enable-person' : {
214    $fields = array( "enabled"=>true );
215    $api->UpdatePerson( intval( $person_id ), $fields );
216    plc_redirect (l_person($person_id));
217  }
218
219  case 'disable-person' : {
220    $fields = array( "enabled"=>false );
221    $api->UpdatePerson( intval( $person_id ), $fields );
222    plc_redirect (l_person($person_id));
223  }
224
225  case 'become-person' : {
226    $plc->BecomePerson (intval($person_id));
227    plc_redirect (l_person(intval($person_id)));
228  }
229
230  case 'delete-person' : {
231   $api->DeletePerson( intval( $person_id ) );
232    plc_redirect (l_persons());
233  }
234
235  case 'delete-keys' : {
236    $key_ids=$_POST['key_ids'];
237    if ( ! $key_ids) {
238      drupal_set_message("action=$action - No key selected");
239      return;
240    }
241    $success=true;
242    $counter=0;
243    foreach( $key_ids as $key_id ) {
244      if ($api->DeleteKey( intval( $key_id )) != 1) 
245        $success=false;
246      else
247        $counter++;
248    }
249    if ($success) 
250      drupal_set_message ("Deleted $counter key(s)");
251    else
252      drupal_set_error ("Could not delete all selected keys, only $counter were removed");
253    plc_redirect(l_person($person_id));
254  }
255
256
257  case 'upload-key' : {
258    if ( ! isset( $_FILES['key'] ) ) {
259      drupal_set_message ("action=$action, no key file set");
260      return;
261    }
262    
263    $key_file= $_FILES['key']['tmp_name'];
264    if ( ! $key_file ) {
265      plc_error("Please select a valid SSH key file to upload");
266      return;
267    } 
268    $fp = fopen( $key_file, "r" );
269    $key = "";
270    if( ! $fp ) {
271      plc_error("Unable to open key file $key_file");
272      return;
273    }
274    // opened the key file, read the one line of contents
275    // The POST operation always creates a file even if the filename
276    // the user specified was garbage.  If there was some problem
277    // with the source file, we'll get a zero length read here.
278    $key = fread($fp, filesize($key_file));
279    fclose($fp);
280    
281    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
282    
283    if ( $key_id >= 1) 
284      drupal_set_message ("New key added");
285    else
286      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
287    
288    plc_redirect(l_person($person_id));
289  }
290
291  case 'update-person': {
292    $person_id=$_POST['person_id'];
293    // attempt to update this person
294    $first_name= $_POST['first_name'];
295    $last_name= $_POST['last_name'];
296    $title= $_POST['title'];
297    $email= $_POST['email'];
298    $phone= $_POST['phone'];
299    $url= $_POST['url'];
300    $bio= str_replace("\r", "", $_POST['bio']);
301    $password1= $_POST['password1'];
302    $password2= $_POST['password2'];
303
304    if( $password1 != $password2 ) {
305      drupal_set_error ("The passwords do not match");
306      plc_redirect(l_person($person_id));
307   }
308
309    $fields= array();
310    $fields['first_name']= $first_name;
311    $fields['last_name']= $last_name;
312    $fields['title']= $title;
313    $fields['email']= $email;
314    $fields['phone']= $phone;
315    $fields['url']= $url;
316    $fields['bio']= $bio;
317                 
318    if ( $password1 != "" )
319      $fields['password']= $password1;
320     
321     if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
322       drupal_set_message("$first_name $last_name updated");
323     else 
324       drupal_set_error ("Could not update person $person_id" . $api->error());
325
326     plc_redirect(l_person($person_id));
327     break;
328   }
329
330 //////////////////////////////////////////////////////////// nodes
331  case 'node-boot-state': {
332    $node_id=intval($_POST['node_id']);
333    $boot_state=$_POST['boot_state'];
334    $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
335    if ($result==1) {
336      drupal_set_message("boot state updated");
337      plc_redirect (l_node($node_id));
338    } else {
339      drupal_set_error("Could not set boot_state '$boot_state'");
340    }
341    break;
342  }
343
344  case 'delete-node': {
345    $node_id=intval($_POST['node_id']);
346    $result=$api->DeleteNode( intval( $node_id ) );
347    if ($api==1) {
348      drupal_set_message("Node $node_id deleted");
349      plc_redirect (l_nodes());
350    } else {
351      drupal_set_error ("Could not delete node $node_id");
352    }
353    break;
354  }
355
356  case 'update-node': {
357    $node_id=intval($_POST['node_id']);
358    $hostname= $_POST['hostname'];
359    $model= $_POST['model'];
360    $node_type= $_POST['node_type'];
361
362    $fields= array( "hostname"=>$hostname, "model"=>$model, "node_type"=>$node_type );
363    $api->UpdateNode( $node_id, $fields );
364    $error= $api->error();
365
366    if( empty( $error ) ) {
367      drupal_set_message("Update node $hostname");
368      plc_redirect(l_node($node_id));
369    } else {
370      drupal_set_error($error);
371    }
372    break;
373  }
374
375  // this code will ensure that at most one PCU gets attached to the node
376  case 'attach-pcu': {
377    $node_id=intval($_POST['node_id']);
378    $pcu_id=intval($_POST['pcu_id']);
379    $port=intval($_POST['port']);
380    // always start with deleting former PCUs
381    $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
382    $former_pcu_ids = $nodes[0]['pcu_ids'];
383    if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
384        if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1) 
385          drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
386        else 
387          drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
388      }
389    // re-attach only if provided pcu_id >=0
390    if ($pcu_id >= 0) {
391      if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
392        drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
393      else
394        drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
395    } else {
396      drupal_set_message ('Detached node from all PCUs');
397    }
398    
399    plc_redirect(l_node($node_id));
400    break;
401  }
402
403  case 'reboot-node-with-pcu': {
404    $node_id=intval($_POST['node_id']);
405    $hostname= $_POST['hostname'];
406    $test = $_POST['test'];
407    settype($test, "boolean");
408
409    $ret = $api->RebootNodeWithPCU( $node_id, $test );
410    $error= $api->error();
411
412    if( empty( $error ) ) {
413      drupal_set_message("Reboot node $hostname: $ret");
414      plc_redirect(l_node($node_id));
415    } else {
416      drupal_set_error($error);
417    }
418    break;
419  }
420    
421
422 //////////////////////////////////////////////////////////// interfaces
423  case 'delete-interfaces' : {
424    $interface_ids=$_POST['interface_ids'];
425    if ( ! $interface_ids) {
426      drupal_set_message("action=$action - No interface selected");
427      return;
428    }
429    $success=true;
430    $counter=0;
431    foreach( $interface_ids as $interface_id ) {
432      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
433        $success=false;
434      else
435        $counter++;
436    }
437    if ($success) 
438      drupal_set_message ("Deleted $counter interface(s)");
439    else
440      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
441    plc_redirect(l_node($_POST['node_id']));
442  }
443
444  case 'new-interface': {
445    plc_redirect(l_interface_add($_POST['node_id']));
446  }
447
448  case 'add-interface': {
449    $node_id=intval($_POST['node_id']);
450    foreach ($interface_details as $field) {
451      $interface[$field]= $_POST[$field];
452      // these must be integers
453      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
454        if ( empty ($interface[$field]) ) 
455          unset ($interface[$field]);
456        else 
457          $interface[$field]= intval( $interface[$field] );
458      }
459    }
460    $interface_id =$api->AddInterface( $node_id , $interface );
461    if ($interface_id <= 0 ) {
462      drupal_set_error ("Could not create interface");
463      drupal_set_error ($api->error());
464    } else {
465      $ip=$interface['ip'];
466      drupal_set_message ("Interface $ip added into node $node_id");
467      if ($_POST['is-virtual']) {
468        $ifname=$_POST['ifname'];
469        if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0) 
470          drupal_set_error ("Could not set tag 'ifname'=$ifname");
471        else 
472          drupal_set_message ("Set tag 'ifname'=$ifname");
473        $alias=$_POST['alias'];
474        // deafult to interface_id
475        if ( ! $alias ) $alias=strval($interface_id);
476        if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0) 
477          drupal_set_error ("Could not set tag 'alias'=$alias");
478        else 
479          drupal_set_message ("Set tag 'alias'=$alias");
480      }
481    }
482    plc_redirect (l_node_interfaces($node_id));
483  }
484    
485  case 'update-interface': {
486    $interface_id=$_POST['interface_id'];
487    foreach ($interface_details as $field) {
488      $interface[$field]= $_POST[$field];
489      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
490        if ( intval($interface[$field]) != 0 ) {
491            $interface[$field]= intval( $interface[$field]);
492        } elseif ($field=='bwlimit' ) {
493            $interface[$field] = NULL;
494        }
495      }
496    }
497    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
498    if ($result == 1 ) 
499      drupal_set_message ("Interface $interface_id updated");
500    else
501      drupal_set_error ("Could not update interface: " . $api->error());
502    plc_redirect (l_interface($interface_id));
503  }
504    
505 //////////////////////////////////////////////////////////// sites
506  case 'delete-site': {
507    $site_id = intval($_POST['site_id']);
508    if ($api->DeleteSite($site_id) ==1) 
509      drupal_set_message ("Site $site_id deleted");
510    else
511      drupal_set_error("Failed to delete site $site_id");
512    plc_redirect (l_sites());
513    break;
514  }
515
516  case 'expire-all-slices-in-site': {
517    // xxx todo
518    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
519    return;
520
521    //// old code from sites/expire.php
522    $sites = $api->GetSites( array( intval( $site_id )));
523    $site=$sites[0];
524    // xxx why not 'now?'
525    $expiration= strtotime( $_POST['expires'] );
526    // loop through all slices for site
527    foreach ($site['slice_ids'] as $slice_id) {
528      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
529    }
530    // update site to not allow slice creation or renewal
531    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
532    plc_redirect (l_site($site_id));
533    break;
534  }
535
536  case 'update-site': {
537    $site_id=intval($_POST['site_id']);
538    $name= $_POST['name'];
539    $abbreviated_name= $_POST['abbreviated_name'];
540    $url= $_POST['url'];
541    $latitude= floatval($_POST['latitude']);
542    $longitude= floatval($_POST['longitude']);
543    //$max_slivers= $_POST['max_slivers'];
544    
545    $fields= array( "name" => $name, 
546                    "abbreviated_name" => $abbreviated_name, 
547                    "url" => $url, 
548                    "latitude" => floatval( $latitude ), 
549                    "longitude" => floatval( $longitude ));
550
551    if ($_POST['login_base']) 
552      $fields['login_base'] = $_POST['login_base'];
553    if (isset($_POST['max_slices']))
554      $fields['max_slices'] = intval($_POST['max_slices']);
555    if (isset($_POST['enabled'])) {
556      $fields['enabled'] = (bool)$_POST['enabled'];
557    }
558    
559    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
560    if ($retcod == 1) 
561      drupal_set_message("Site $name updated");
562    else 
563      drupal_set_error ("Could not update site $site_id");
564      
565    plc_redirect(l_site($site_id));
566    break;
567  }
568
569 //////////////////////////////////////////////////////////// slices
570  case 'delete-slice': {
571    $slice_id = $_POST['slice_id'];
572    if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
573      drupal_set_message("Slice $slice_id deleted");
574      plc_redirect(l_slices());
575    } else {
576      drupal_set_error("Could not delete slice $slice_id " . $api->error());
577    }
578    break;
579  }
580      
581  case 'update-slice': {
582    $slice_id = $_POST['slice_id'];
583    $name = $_POST['name'];
584    $description= $_POST['description'];
585    $url= $_POST['url'];
586
587    $fields= array( "description"=>$description, "url"=>$url );
588    $api->UpdateSlice( intval( $slice_id ), $fields );
589    $error= $api->error();
590
591    if( empty( $error ) ) {
592      drupal_set_message("Update slice $name");
593      plc_redirect(l_slice($slice_id));
594    } else {
595      drupal_set_error($error);
596    }
597    break;
598  }
599
600  case 'renew-slice': {
601    $slice_id = intval ($_POST['slice_id']);     
602    $expires = intval ($_POST['expires']);
603    // 8 weeks from now
604    // xxx
605    $now=mktime();
606    $WEEK=7*24*3600;
607    $WEEKS=8;
608    $MAX_FUTURE=$WEEKS*$WEEK;
609    if ( ($expires-$now) > $MAX_FUTURE) {
610      drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
611      plc_redirect(l_slice($slice_id));
612    }
613    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
614      drupal_set_message("Slice renewed");
615    else
616      drupal_set_error("Could not update slice $slice_id");
617    plc_redirect(l_slice($slice_id));
618    break;
619  }
620
621  case 'remove-persons-from-slice': {
622    $slice_id = intval ($_POST['slice_id']);     
623    $person_ids = $_POST['person_ids'];
624    
625    $slice_name = "";
626    $tmp_slices = $api->GetSlices($slice_id, array("name"));
627    if (count($tmp_slices) > 0) {
628        $tmp_slice = $tmp_slices[0];
629        $slice_name = $tmp_slice["name"];
630    }
631    $notify_subject = "Removed from slice: " . $slice_name;
632    $notify_body = sprintf("You have been removed from the slice %s.
633
634 Our support team will be glad to answer any question that you might have.
635 ",$slice_name);
636    $notify_person_ids = array();
637    
638    $success=true;
639    $counter=0;
640    foreach( $person_ids as $person_id ) {
641      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
642        $success=false;
643      else {
644          array_push($notify_person_ids, intval($person_id));
645        $counter++;
646      }
647    }
648    if ($success) {
649      $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
650      drupal_set_message ("Deleted $counter person(s)");
651    }
652    else
653      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
654    plc_redirect(l_slice($slice_id) . " &show_persons=true");
655    break;
656  }
657
658  case 'add-persons-in-slice': {
659    $slice_id = intval ($_POST['slice_id']);     
660    $person_ids = $_POST['person_ids'];
661
662    $slice_name = "";
663    $tmp_slices = $api->GetSlices($slice_id, array("name"));
664    if (count($tmp_slices) > 0) {
665      $tmp_slice = $tmp_slices[0];
666      $slice_name = $tmp_slice["name"];
667    }
668    $notify_subject = "Added to slice: " . $slice_name;
669    $notify_body = sprintf("You have been added to the slice %s as a user.
670
671 You can go to your slice page following the link below:
672 https://%s:%d/db/slices/index.php?id=%d
673
674 Our support team will be glad to answer any question that you might have.
675 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
676    $notify_person_ids = array();
677    
678    $success=true;
679    $counter=0;
680    foreach ($person_ids as $person_id) {
681      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
682        $success=false;
683      else {
684        array_push($notify_person_ids, intval($person_id));
685        $counter++;
686      }
687    }
688    if ($success) {
689      $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
690      drupal_set_message ("Added $counter person(s)");
691    }
692    else
693      drupal_set_error ("Could not add all selected persons, only $counter were added");
694    plc_redirect(l_slice($slice_id) . "&show_persons=true" );
695    break;
696  }
697
698  case 'remove-nodes-from-slice': {
699    $slice_id = intval ($_POST['slice_id']);     
700    $node_ids = array_map("intval",$_POST['node_ids']);
701    $count=count($node_ids);
702    
703    if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1) 
704      drupal_set_message ("Removed $count node(s)");
705    else
706      drupal_set_error ("Could not remove selected nodes");
707    plc_redirect(l_slice_nodes($slice_id));
708    break;
709  }
710
711  case 'add-nodes-in-slice': {
712    $slice_id = intval ($_POST['slice_id']);     
713    $node_ids = array_map("intval",$_POST['node_ids']);
714    $count=count($node_ids);
715    if ($api->AddSliceToNodes($slice_id,$node_ids) == 1) 
716      drupal_set_message ("Added $count node(s)");
717    else
718      drupal_set_error ("Could not add all selected nodes");
719    plc_redirect(l_slice_nodes($slice_id));
720    break;
721  }
722
723  case 'update-initscripts': {
724 //      expects:        slice_id & name & previous-initscript & previous-initscript-code 
725 //                      & initscript & initscript-code
726    $slice_id = intval ($_POST['slice_id']);     
727    $previous_initscript=$_POST['previous-initscript'];
728    $initscript=$_POST['initscript'];
729    $previous_initscript_code=html_entity_decode($_POST['previous-initscript-code']);
730    $initscript_code=$_POST['initscript-code'];
731
732    $changes=FALSE;
733    if (strcmp($initscript,$previous_initscript) != 0) {
734      $newvalue=$api->SetSliceInitscript($slice_id,$initscript);
735      $status = (strcmp($newvalue,$initscript)==0) ? "OK" : "failed";
736      if (! $initscript)         drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
737      else                       drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
738      $changes=TRUE;
739    }
740
741    // somehow some \r chars make it here; just ignore them
742    $previous_initscript_code=str_replace("\r","",$previous_initscript_code);
743    //   plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
744
745    $initscript_code=str_replace("\r","",$initscript_code);
746    // make sure the script ends with a single \n 
747    $initscript_code=trim($initscript_code);
748    if (!empty($initscript_code) && $initscript_code[strlen($initscript_code)-1] != "\n")
749      $initscript_code.="\n";
750    // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
751
752    if (strcmp($initscript_code,$previous_initscript_code) != 0) {
753      $newvalue=$api->SetSliceInitscriptCode($slice_id,$initscript_code);
754      // plc_debug_txt('newvalue',$newvalue);
755      $status=(strcmp($newvalue,$initscript_code)==0) ? "OK" : "failed";
756      if (! $initscript_code)    drupal_set_message("Removed initscript code " . $status);
757      else                       drupal_set_message("Installed new initscript code " . $status);
758      $changes=TRUE;
759    }
760    if (!$changes) drupal_set_message("No changes required in initscript");
761    plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1" );
762    break;
763  }
764
765
766  case 'delete-slice-tags': {
767    $slice_id = intval($_POST['slice_id']);
768    $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
769    $count = 0;
770    $success = true;
771    foreach($slice_tag_ids as $slice_tag_id) {
772      if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
773      else {
774        drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
775        $success = false;
776      }
777    }
778    if ($success)
779      drupal_set_message ("Deleted $count slice tag(s)");
780    plc_redirect(l_slice($slice_id) . "&show_tags=1" );
781    break;
782  }
783   
784  case 'add-slice-tag': {
785    $slice_id = intval($_POST['slice_id']);
786    $tag_type_id = intval($_POST['tag_type_id']);
787    $value = $_POST['value'];
788    $node_id = intval($_POST['node_id']);
789    $nodegroup_id = intval($_POST['nodegroup_id']);
790   
791    $result = null;
792    if ($node_id) {
793      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
794    } elseif ($nodegroup_id) {
795      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
796    } else {
797      $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
798    }
799    if ($result)
800      drupal_set_message ("Added slice tag.");
801    else 
802        drupal_set_error("Could not add slice tag");
803    if ($_POST['sliver_action'])
804        plc_redirect(l_sliver($node_id,$slice_id));
805    else
806        plc_redirect(l_slice($slice_id) . "&show_tags=true" );
807    break;
808  }
809
810 //////////////////////////////////////////////////////////// tag types
811
812  case 'update-tag-type': {
813   // get post vars 
814    $tag_type_id= intval( $_POST['tag_type_id'] );
815    $tagname = $_POST['tagname'];
816    $min_role_id= intval( $_POST['min_role_id'] );
817    $description= $_POST['description'];  
818    $category= $_POST['category'];
819   
820    // make tag_type_fields dict
821    $tag_type_fields= array( "min_role_id" => $min_role_id, 
822                             "tagname" => $tagname, 
823                             "description" => $description,
824                             "category" => $category,
825                             );
826
827    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
828      drupal_set_message ("Tag type $tagname updated");
829    else 
830      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
831    plc_redirect(l_tag($tag_type_id));
832    break;
833  }
834
835  case 'add-tag-type': {
836   // get post vars 
837    $tagname = $_POST['tagname'];
838    $min_role_id= intval( $_POST['min_role_id'] );
839    $description= $_POST['description'];  
840    $category= $_POST['category'];  
841   
842    // make tag_type_fields dict
843    $tag_type_fields= array( "min_role_id" => $min_role_id, 
844                             "tagname" => $tagname, 
845                             "description" => $description,
846                             "category" => $category,
847                             );
848
849   // Add it!
850    $tag_type_id=$api->AddTagType( $tag_type_fields );
851    if ($tag_type_id > 0) 
852      drupal_set_message ("tag type $tag_type_id created");
853    else
854      drupal_set_error ("Could not create tag type $tagname");
855    plc_redirect( l_tags());
856    break;
857  }
858
859  case 'delete-tag-types': {
860    $tag_type_ids = $_POST['tag_type_ids'];
861    if ( ! $tag_type_ids) {
862      drupal_set_message("action=$action - No tag selected");
863      return;
864    }
865    $success=true;
866    $counter=0;
867    foreach ($tag_type_ids as $tag_type_id) 
868      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
869        $success=false;
870      else
871        $counter++;
872    if ($success) 
873      drupal_set_message ("Deleted $counter tag(s)");
874    else
875      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
876    plc_redirect (l_tags());
877    break;
878  }
879
880  case 'remove-roles-from-tag-type' : {
881    $tag_type_id=$_POST['tag_type_id'];
882    $role_ids=$_POST['role_ids'];
883    if ( ! $role_ids) {
884      drupal_set_error("You have not selected role(s) to remove");
885    } else {
886      foreach( $role_ids as $role_id)  
887        if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 ) 
888          drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id");
889    }
890    plc_redirect (l_tag_roles($tag_type_id));
891  }
892      
893  case 'add-role-to-tag-type' : {
894    $tag_type_id=$_POST['tag_type_id'];
895    $role_id=$_POST['role_id'];
896    if ( ! $role_id) {
897      drupal_set_error ("You have not selected a role to add");
898    } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) {
899      drupal_set_error("Could not add role $role_id to tag $tag_type_id");
900    }
901    plc_redirect (l_tag_roles($tag_type_id));
902  }
903
904 //////////////////////////////////////// tags   
905  case 'set-tag-on-node': 
906  case 'set-tag-on-site': 
907  case 'set-tag-on-person': 
908  case 'set-tag-on-interface': 
909    {
910    
911    $mode = NULL;
912    if ($action == 'set-tag-on-node') $mode='node';
913    if ($action == 'set-tag-on-site') $mode='site';
914    if ($action == 'set-tag-on-person') $mode='person';
915    if ($action == 'set-tag-on-interface') $mode='interface';
916
917    if ($mode=='node') 
918      $id = intval($_POST['node_id']);
919    else if ($mode=='site')
920      $id = intval($_POST['site_id']);
921    else if ($mode=='person')
922      $id = intval($_POST['person_id']);
923    else 
924      $id = intval($_POST['interface_id']);
925    $tag_type_id = intval($_POST['tag_type_id']);
926    $value = $_POST['value'];
927
928    $tag_types=$api->GetTagTypes(array($tag_type_id));
929    if (count ($tag_types) != 1) {
930      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
931    } else {
932      if ($mode=='node') 
933        $tags = $api->GetNodeTags (array('node_id'=>$id, 'tag_type_id'=> $tag_type_id));
934      else if ($mode=='site')
935        $tags = $api->GetSiteTags (array('site_id'=>$id, 'tag_type_id'=> $tag_type_id));
936      else if ($mode=='person')
937        $tags = $api->GetPersonTags (array('person_id'=>$id, 'tag_type_id'=> $tag_type_id));
938      else
939        $tags = $api->GetInterfaceTags (array('interface_id'=>$id, 'tag_type_id'=> $tag_type_id));
940
941      // already has a tag set
942      if ( count ($tags) == 1) {
943        $tag=$tags[0];
944        if ($mode=='node') 
945          $result=$api->UpdateNodeTag($tag['node_tag_id'],$value);
946        else if ($mode=='site') 
947          $result=$api->UpdateSiteTag($tag['site_tag_id'],$value);
948        else if ($mode=='person') 
949          $result=$api->UpdatePersonTag($tag['person_tag_id'],$value);
950        else 
951          $result=$api->UpdateInterfaceTag($tag['interface_tag_id'],$value);
952
953        if ($result == 1) 
954          drupal_set_message ("Updated tag, new value = $value");
955        else
956          drupal_set_error ("Could not update tag");
957
958      // no such tag set yet on that object
959      } else {
960        if ($mode=='node')
961          $tag_id = $api->AddNodeTag($id,$tag_type_id,$value);
962        else if ($mode=='site')
963          $tag_id = $api->AddSiteTag($id,$tag_type_id,$value);
964        else if ($mode=='person')
965          $tag_id = $api->AddPersonTag($id,$tag_type_id,$value);
966        else
967          $tag_id = $api->AddInterfaceTag($id,$tag_type_id,$value);
968        if ($tag_id) 
969          drupal_set_message ("Created tag, new value = $value");
970        else
971          drupal_set_error ("Could not create tag");
972      }
973    }
974    
975    if ($mode=='node')
976      plc_redirect (l_node_tags($id));
977    else if ($mode=='site')
978      plc_redirect (l_site_tags($id));
979    else if ($mode=='person')
980      plc_redirect (l_person_tags($id));
981    else
982      plc_redirect (l_interface_tags($id));
983  }
984
985  case 'delete-node-tags': 
986  case 'delete-site-tags':
987  case 'delete-person-tags':
988  case 'delete-interface-tags': {
989
990    $mode = NULL;
991    if ($action == 'delete-node-tags') $mode='node';
992    if ($action == 'delete-site-tags') $mode='site';
993    if ($action == 'delete-person-tags') $mode='person';
994    if ($action == 'delete-interface-tags') $mode='interface';
995
996    if ($mode=='node') {
997      $id=$_POST['node_id'];
998      $tag_ids=$_POST['node_tag_ids'];
999    } else if ($mode=='site') {
1000      $id=$_POST['site_id'];
1001      $tag_ids=$_POST['site_tag_ids'];
1002    } else if ($mode=='person') {
1003      $id=$_POST['person_id'];
1004      $tag_ids=$_POST['person_tag_ids'];
1005    } else {
1006      $id=$_POST['interface_id'];
1007      $tag_ids=$_POST['interface_tag_ids'];
1008    }
1009
1010    if ( ! $tag_ids) {
1011      drupal_set_message("action=$action - No tag selected");
1012      return;
1013    }
1014    $success=true;
1015    $counter=0;
1016    foreach( $tag_ids as $tag_id ) {
1017      if ($mode=='node') 
1018        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
1019      else if ($mode=='site')
1020        $retcod = $api->DeleteSiteTag( intval( $tag_id ));
1021      else if ($mode=='person')
1022        $retcod = $api->DeletePersonTag( intval( $tag_id ));
1023      else
1024        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
1025      if ($retcod != 1) 
1026        $success=false;
1027      else
1028        $counter++;
1029    }
1030    if ($success) 
1031      drupal_set_message ("Deleted $counter tag(s)");
1032    else
1033      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
1034
1035    if ($mode=='node')
1036      plc_redirect (l_node_tags($id));
1037    else if ($mode=='site')
1038      plc_redirect (l_site_tags($id));
1039    else if ($mode=='person')
1040      plc_redirect (l_person_tags($id));
1041    else
1042      plc_redirect (l_interface_tags($id));
1043
1044  }
1045
1046 //////////////////////////////////////// nodegroups
1047  case 'update-nodegroup': {
1048    $nodegroup_id = $_POST['nodegroup_id'];
1049    $groupname = $_POST['groupname'];
1050    $value = $_POST['value'];
1051
1052    $fields=array();
1053    $fields['groupname']=$groupname;
1054    $fields['value']=$value;
1055    if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1) 
1056      drupal_set_message ('Nodegroup updated');
1057    else 
1058      drupal_set_error ("Could not update nodegroup $groupname");
1059    
1060    plc_redirect(l_nodegroup($nodegroup_id));
1061
1062  }
1063
1064  case 'add-nodegroup': {
1065    $groupname=$_POST['groupname'];
1066    if ( ! $groupname ) {
1067      drupal_set_error ('Empty groupname');
1068      plc_redirect (l_nodegroups());
1069    }
1070    $tag_type_id=intval($_POST['tag_type_id']);
1071    if ( ! $tag_type_id ) {
1072      drupal_set_error ('You must select a tag in the dropdown list');
1073      plc_redirect (l_nodegroups());
1074    }
1075    $value=$_POST['value'];
1076    if ( ! $value ) {
1077      drupal_set_message ("Empty value.. let's see ..");
1078    }
1079    if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0) 
1080      drupal_set_message ("Nodegroup $groupname created");
1081    else
1082      drupal_set_error ("Could not create nodegroup $groupname");
1083
1084    plc_redirect (l_nodegroups());
1085    break;
1086  }
1087
1088  case 'delete-nodegroups': {
1089    $nodegroup_ids=$_POST['nodegroup_ids'];
1090    if ( ! $nodegroup_ids ) {
1091      drupal_set_message("action=delete-nodegroups - No group selected");
1092      plc_redirect(l_nodegroups());
1093    }
1094    $success=true;
1095    $counter=0;
1096    foreach ($nodegroup_ids as $nodegroup_id) 
1097      if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1) 
1098        $success=false;
1099      else
1100        $counter++;
1101    if ($success) 
1102      drupal_set_message ("Deleted $counter group(s)");
1103    else
1104      drupal_set_error ("Could not delete all selected groups, only $counter were removed");
1105    plc_redirect (l_nodegroups());
1106    break;
1107  }
1108
1109 //////////////////////////////////////// leases
1110  case 'manage-leases': {
1111    $actions=json_decode($_POST['actions']);
1112    $add_requested=0;
1113    $add_done=0;
1114    $del_requested=0;
1115    $del_done=0;
1116    $errors=array();
1117    foreach ($actions as $action) {
1118      if ($action[0] == 'add-leases') {
1119        $nodenames=$action[1];
1120        $add_requested += count($nodenames);
1121        $slicename=$action[2];
1122        $t_from=intval($action[3]);
1123        $t_until=intval($action[4]);
1124        $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
1125        // update number of added leases
1126        $ids=$hash['new_ids'];
1127        $add_done += count($ids);
1128        // update global errors array
1129        foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
1130      } else if ($action[0]=='delete-leases') {
1131        $lease_id=intval($action[1]);
1132        $del_requested += 1;
1133        if ($api->DeleteLeases(array($lease_id)) == 1) {
1134          $del_done += 1;
1135        } else {
1136          $errors []= "Could not delete lease " . $lease_id;
1137        }
1138      } else {
1139        $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1140      }
1141    }
1142    
1143    if (count($errors)==0) {
1144      echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1145    } else {
1146      foreach ($errors as $error) echo($error. "\n");
1147      echo("Leases updated only partially (" . 
1148           $add_done . "/" . $add_requested . " added and " . 
1149           $del_done . "/" . $del_requested . " deleted)");
1150    }     
1151
1152    break;
1153  }
1154
1155 ////////////////////////////////////////
1156
1157  case 'debug': {
1158    plc_debug('GET',$_GET);
1159    plc_debug('POST',$_POST);
1160    plc_debug('FILES',$_FILES);
1161    return;
1162  }
1163
1164  default: {
1165    plc_error ("Unknown action $action in actions.php");
1166    return;
1167  }
1168
1169  }
1170
1171 ?>