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