tweaks, fixes, slice user management OK
[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
53 //////////////////////////////////////// interfaces
54 $known_actions []= "delete-interfaces"; 
55 //      expects:        interface_ids
56 $known_actions []="add-interface";
57 //      expects:        node_id & interface details
58 $known_actions []="update-interface";
59 //      expects:        interface_id & interface details
60
61 //////////////////////////////////////// sites
62 $known_actions []= "delete-site";       
63 //      expects:        site_id
64 $known_actions []= "expire-all-slices-in-site";
65 //      expects:        slice_ids
66 $known_actions []= "update-site";
67 //      expects:        site_id & name abbreviated_name url latitude longitude [login_base max_slices]
68
69 //////////////////////////////////////// slices
70 $known_actions []= "update-slice";      
71 //      expects:        slice_id, name, description, url
72 $known_actions []= "renew-slice";
73 //      expects:        slice_id & expires
74 $known_actions []= 'remove-persons-from-slice';
75 //      expects:        slice_id & person_ids
76 $known_actions []= 'add-persons-in-slice';
77 //      expects:        slice_id & person_ids
78
79 //////////////////////////////////////// tag types
80 $known_actions []= "update-tag-type";
81 //      expects:        tag_type_id & name & description & category & min_role_id  
82 $known_actions []= "add-tag-type";
83 //      expects:        tag_type_id & tagname & description & category & min_role_id  
84 $known_actions []= "delete-tag-types";
85 //      expects:        tag_type_ids
86
87 //////////////////////////////////////// tags
88 $known_actions []= "set-tag-on-node";
89 //      expects:        node_id tagname value
90 $known_actions []= "set-tag-on-interface";
91 //      expects:        interface_id tagname value
92 $known_actions []= "delete-node-tags";
93 //      expects:        node_id & node_tag_ids
94 $known_actions []= "delete-interface-tags";
95 //      expects:        interface_id & interface_tag_ids
96
97 ////////////////////////////////////////////////////////////
98 $interface_details= array ('method','type', 'ip', 'gateway', 'network', 
99                            'broadcast', 'netmask', 'dns1', 'dns2', 
100                            'hostname', 'mac', 'bwlimit' );
101
102 //////////////////////////////
103 // sometimes we don't set 'action', but use the submit button name instead
104 // so if 'action' not set, see if $_POST has one of the actions as a key
105 if ($_POST['action']) 
106   $action=$_POST['action'];
107 else 
108   foreach ($known_actions as $known_action) 
109     if ($_POST[$known_action]) {
110       $action=$known_action;
111       break;
112     }
113
114 //uncomment for debugging incoming data
115 //$action='debug';
116
117 $person_id = $_POST['person_id'];       // usually needed
118
119 if ( ! $action ) {
120   drupal_set_message ("actions.php: action not set or not in known_actions");
121   plc_debug('POST',$_POST);
122   return;
123  }
124
125 switch ($action) {
126
127  case 'add-person-to-site': {
128    $site_id = $_POST['site_id'];
129    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
130    plc_redirect (l_person($person_id));
131  }
132
133  case 'remove-person-from-sites': {
134    $site_ids = $_POST['site_ids'];
135    if ( ! $site_ids) {
136      drupal_set_message("action=$action - No site selected");
137      return;
138    }
139    foreach ( $site_ids as $site_id ) {
140      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
141    }
142    plc_redirect (l_person($person_id));
143  }
144
145  case 'remove-roles-from-person' : {
146    $role_ids=$_POST['role_ids'];
147    if ( ! $role_ids) {
148      drupal_set_message("action=$action - No role selected");
149      return;
150    }
151    foreach( $role_ids as $role_id)  {
152      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
153    }
154    plc_redirect (l_person($person_id));
155  }
156      
157  case 'add-role-to-person' : {
158    $role_id=$_POST['role_id'];
159    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
160    plc_redirect (l_person($person_id));
161  }
162
163  case 'enable-person' : {
164    $fields = array( "enabled"=>true );
165    $api->UpdatePerson( intval( $person_id ), $fields );
166    plc_redirect (l_person($person_id));
167  }
168
169  case 'disable-person' : {
170    $fields = array( "enabled"=>false );
171    $api->UpdatePerson( intval( $person_id ), $fields );
172    plc_redirect (l_person($person_id));
173  }
174
175  case 'become-person' : {
176    $plc->BecomePerson (intval($person_id));
177    plc_redirect (l_persons());
178  }
179
180  case 'delete-person' : {
181   $api->DeletePerson( intval( $person_id ) );
182    plc_redirect (l_persons());
183  }
184
185  case 'delete-keys' : {
186    $key_ids=$_POST['key_ids'];
187    if ( ! $key_ids) {
188      drupal_set_message("action=$action - No key selected");
189      return;
190    }
191    $success=true;
192    $counter=0;
193    foreach( $key_ids as $key_id ) {
194      if ($api->DeleteKey( intval( $key_id )) != 1) 
195        $success=false;
196      else
197        $counter++;
198    }
199    if ($success) 
200      drupal_set_message ("Deleted $counter key(s)");
201    else
202      drupal_set_error ("Could not delete all selected keys, only $counter were removed");
203    plc_redirect(l_person($person_id));
204  }
205
206
207  case 'upload-key' : {
208    if ( ! isset( $_FILES['key'] ) ) {
209      drupal_set_message ("action=$action, no key file set");
210      return;
211    }
212    
213    $key_file= $_FILES['key']['tmp_name'];
214    if ( ! $key_file ) {
215      plc_error("Please select a valid SSH key file to upload");
216      return;
217    } 
218    $fp = fopen( $key_file, "r" );
219    $key = "";
220    if( ! $fp ) {
221      plc_error("Unable to open key file $key_file");
222      return;
223    }
224    // opened the key file, read the one line of contents
225    // The POST operation always creates a file even if the filename
226    // the user specified was garbage.  If there was some problem
227    // with the source file, we'll get a zero length read here.
228    $key = fread($fp, filesize($key_file));
229    fclose($fp);
230    
231    $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
232    
233    if ( $key_id == 1) 
234      drupal_set_message ("New key added");
235    else
236      drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
237    
238    plc_redirect(l_person($person_id));
239  }
240
241  case 'update-person': {
242    $person_id=$_POST['person_id'];
243    // attempt to update this person
244    $first_name= $_POST['first_name'];
245    $last_name= $_POST['last_name'];
246    $title= $_POST['title'];
247    $email= $_POST['email'];
248    $phone= $_POST['phone'];
249    $url= $_POST['url'];
250    $bio= str_replace("\r", "", $_POST['bio']);
251    $password1= $_POST['password1'];
252    $password2= $_POST['password2'];
253
254    if( $password1 != $password2 ) {
255      drupal_set_error ("The passwords do not match");
256      plc_redirect(l_person($person_id));
257   }
258
259    $fields= array();
260    $fields['first_name']= $first_name;
261    $fields['last_name']= $last_name;
262    $fields['title']= $title;
263    $fields['email']= $email;
264    $fields['phone']= $phone;
265    $fields['url']= $url;
266    $fields['bio']= $bio;
267                 
268    if ( $password1 != "" )
269      $fields['password']= $password1;
270     
271     if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
272       drupal_set_message("$first_name $last_name updated");
273     else 
274       drupal_set_error ("Could not update person $person_id" . $api->error());
275
276     plc_redirect(l_person($person_id));
277     break;
278   }
279
280 //////////////////////////////////////////////////////////// nodes
281  case 'node-boot-state': {
282    $node_id=intval($_POST['node_id']);
283    $boot_state=$_POST['boot_state'];
284    $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
285    if ($result==1) {
286      drupal_set_message("boot state updated");
287      plc_redirect (l_node($node_id));
288    } else {
289      drupal_set_error("Could not set boot_state '$boot_state'");
290    }
291    break;
292  }
293
294  case 'delete-node': {
295    $node_id=intval($_POST['node_id']);
296    $result=$api->DeleteNode( intval( $node_id ) );
297    if ($api==1) {
298      drupal_set_message("Node $node_id deleted");
299      plc_redirect (l_nodes());
300    } else {
301      drupal_set_error ("Could not delete node $node_id");
302    }
303    break;
304  }
305
306  case 'update-node': {
307    $hostname= $_POST['hostname'];
308    $model= $_POST['model'];
309
310    $fields= array( "hostname"=>$hostname, "model"=>$model );
311    $api->UpdateNode( intval( $node_id ), $fields );
312    $error= $api->error();
313
314    if( empty( $error ) ) {
315      drupal_set_message("Update node $hostname");
316      plc_redirect(l_node($node_id));
317    } else {
318      drupal_set_error($error);
319    }
320    break;
321  }
322
323 //////////////////////////////////////////////////////////// interfaces
324  case 'delete-interfaces' : {
325    $interface_ids=$_POST['interface_ids'];
326    if ( ! $interface_ids) {
327      drupal_set_message("action=$action - No interface selected");
328      return;
329    }
330    $success=true;
331    $counter=0;
332    foreach( $interface_ids as $interface_id ) {
333      if ($api->DeleteInterface( intval( $interface_id )) != 1) 
334        $success=false;
335      else
336        $counter++;
337    }
338    if ($success) 
339      drupal_set_message ("Deleted $counter interface(s)");
340    else
341      drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
342    plc_redirect(l_node($_POST['node_id']));
343  }
344
345  case 'add-interface': {
346    $node_id=$_POST['node_id'];
347    foreach ($interface_details as $field) {
348      $interface[$field]= $_POST[$field];
349      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
350        $interface[$field]= intval( $interface[$field] );
351      }
352    }
353    $result=$api->AddInterface( intval( $node_id ), $interface );
354    if ($result >0 ) 
355      drupal_set_message ("Interface $result added into node $node_id");
356    else
357      drupal_set_error ("Could not create interface");
358    plc_redirect (l_node($node_id));
359  }
360    
361  case 'update-interface': {
362    $interface_id=$_POST['interface_id'];
363    foreach ($interface_details as $field) {
364      $interface[$field]= $_POST[$field];
365      if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
366        $interface[$field]= intval( $interface[$field] );
367      }
368    }
369    $result=$api->UpdateInterface( intval( $interface_id ), $interface );
370    if ($result == 1 ) 
371      drupal_set_message ("Interface $interface_id updated");
372    else
373      drupal_set_error ("Could not update interface");
374    plc_redirect (l_interface($interface_id));
375  }
376    
377 //////////////////////////////////////////////////////////// sites
378  case 'delete-site': {
379    $site_id = intval($_POST['site_id']);
380    if ($api->DeleteSite($site_id) ==1) 
381      drupal_set_message ("Site $site_id deleted");
382    else
383      drupal_set_error("Failed to delete site $site_id");
384    plc_redirect (l_sites());
385    break;
386  }
387
388  case 'expire-all-slices-in-site': {
389    // xxx todo
390    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
391    return;
392
393    //// old code from sites/expire.php
394    $sites = $api->GetSites( array( intval( $site_id )));
395    $site=$sites[0];
396    // xxx why not 'now?'
397    $expiration= strtotime( $_POST['expires'] );
398    // loop through all slices for site
399    foreach ($site['slice_ids'] as $slice_id) {
400      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
401    }
402    // update site to not allow slice creation or renewal
403    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
404    plc_redirect (l_site($site_id));
405    break;
406  }
407
408  case 'update-site': {
409    $site_id=intval($_POST['site_id']);
410    $name= $_POST['name'];
411    $abbreviated_name= $_POST['abbreviated_name'];
412    $url= $_POST['url'];
413    $latitude= floatval($_POST['latitude']);
414    $longitude= floatval($_POST['longitude']);
415    //$max_slivers= $_POST['max_slivers'];
416    
417    $fields= array( "name" => $name, 
418                    "abbreviated_name" => $abbreviated_name, 
419                    "url" => $url, 
420                    "latitude" => floatval( $latitude ), 
421                    "longitude" => floatval( $longitude ));
422
423    if ($_POST['login_base']) 
424      $fields['login_base'] = $_POST['login_base'];
425    if ($_POST['max_slices']) 
426      $fields['max_slices'] = intval($_POST['max_slices']);
427    
428    $retcod=$api->UpdateSite( intval( $site_id ), $fields );
429    if ($retcod == 1) 
430      drupal_set_message("Site $name updated");
431    else 
432      drupal_set_error ("Could not update site $site_id");
433      
434    plc_redirect(l_site($site_id));
435    break;
436  }
437
438 //////////////////////////////////////////////////////////// slices
439  case 'update-slice': {
440    $slice_id = $_POST['slice_id'];
441    $name = $_POST['name'];
442    $description= $_POST['description'];
443    $url= $_POST['url'];
444
445    $fields= array( "description"=>$description, "url"=>$url );
446    $api->UpdateSlice( intval( $slice_id ), $fields );
447    $error= $api->error();
448
449    if( empty( $error ) ) {
450      drupal_set_message("Update slice $name");
451      plc_redirect(l_slice($slice_id));
452    } else {
453      drupal_set_error($error);
454    }
455    break;
456  }
457
458  case 'renew-slice': {
459    $slice_id = intval ($_POST['slice_id']);     
460    $expires = intval ($_POST['expires']);
461    // 8 weeks from now
462    // xxx
463    $now=mktime();
464    $WEEK=7*24*3600;
465    $WEEKS=8;
466    $MAX_FUTURE=$WEEKS*$WEEK;
467    if ( ($expires-$now) > $MAX_FUTURE) {
468      drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
469      plc_redirect(l_slice($slice_id));
470    }
471    plc_debug('slice_id',$slice_id);
472    plc_debug('expires',$expires);
473    if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
474      drupal_set_message("Slice renewed");
475    else
476      drupal_set_error("Could not update slice $slice_id");
477    plc_redirect(l_slice($slice_id));
478    break;
479  }
480
481  case 'remove-persons-from-slice': {
482    $slice_id = intval ($_POST['slice_id']);     
483    $person_ids = $_POST['person_ids'];
484    
485    $success=true;
486    $counter=0;
487    foreach( $person_ids as $person_id ) {
488      if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1) 
489        $success=false;
490      else
491        $counter++;
492    }
493    if ($success) 
494      drupal_set_message ("Deleted $counter person(s)");
495    else
496      drupal_set_error ("Could not delete all selected persons, only $counter were removed");
497    plc_redirect(l_slice($slice_id));
498    break;
499  }
500
501  case 'add-persons-in-slice': {
502    $slice_id = intval ($_POST['slice_id']);     
503    $person_ids = $_POST['person_ids'];
504    
505    $success=true;
506    $counter=0;
507    foreach( $person_ids as $person_id ) {
508      if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1) 
509        $success=false;
510      else
511        $counter++;
512    }
513    if ($success) 
514      drupal_set_message ("Added $counter person(s)");
515    else
516      drupal_set_error ("Could not add all selected persons, only $counter were added");
517    plc_redirect(l_slice($slice_id));
518    break;
519  }
520
521 //////////////////////////////////////////////////////////// tag types
522
523  case 'update-tag-type': {
524   // get post vars 
525    $tag_type_id= intval( $_POST['tag_type_id'] );
526    $tagname = $_POST['tagname'];
527    $min_role_id= intval( $_POST['min_role_id'] );
528    $description= $_POST['description'];  
529    $category= $_POST['category'];  
530   
531    // make tag_type_fields dict
532    $tag_type_fields= array( "min_role_id" => $min_role_id, 
533                             "tagname" => $tagname, 
534                             "description" => $description,
535                             "category" => $category,
536                             );
537
538    if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1) 
539      drupal_set_message ("Tag type $tagname updated");
540    else 
541      drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
542    plc_redirect(l_tag($tag_type_id));
543  }
544
545  case 'add-tag-type': {
546   // get post vars 
547    $tagname = $_POST['tagname'];
548    $min_role_id= intval( $_POST['min_role_id'] );
549    $description= $_POST['description'];  
550    $category= $_POST['category'];  
551   
552    // make tag_type_fields dict
553    $tag_type_fields= array( "min_role_id" => $min_role_id, 
554                             "tagname" => $tagname, 
555                             "description" => $description,
556                             "category" => $category,
557                             );
558
559   // Add it!
560    $tag_type_id=$api->AddTagType( $tag_type_fields );
561    if ($tag_type_id > 0) 
562      drupal_set_message ("tag type $tag_type_id created");
563    else
564      drupal_set_error ("Could not create tag type $tagname");
565    plc_redirect( l_tags());
566  }
567
568  case 'delete-tag-types': {
569    $tag_type_ids = $_POST['tag_type_ids'];
570    if ( ! $tag_type_ids) {
571      drupal_set_message("action=$action - No tag selected");
572      return;
573    }
574    $success=true;
575    $counter=0;
576    foreach ($tag_type_ids as $tag_type_id) 
577      if ($api->DeleteTagType(intval($tag_type_id)) != 1) 
578        $success=false;
579      else
580        $counter++;
581    if ($success) 
582      drupal_set_message ("Deleted $counter tag(s)");
583    else
584      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
585    plc_redirect (l_tags());
586    break;
587  }
588
589 //////////////////////////////////////// tags   
590  case 'set-tag-on-node': 
591  case 'set-tag-on-interface': {
592    
593    $node_mode = false;
594    if ($action == 'set-tag-on-node') $node_mode=true;
595
596    if ($node_mode)
597      $node_id = intval($_POST['node_id']);
598    else 
599      $interface_id=intval($_POST['interface_id']);
600    $tag_type_id = intval($_POST['tag_type_id']);
601    $value = $_POST['value'];
602
603    $tag_types=$api->GetTagTypes(array($tag_type_id));
604    if (count ($tag_types) != 1) {
605      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
606    } else {
607      if ($node_mode) 
608        $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
609      else
610        $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
611      if ( count ($tags) == 1) {
612        $tag=$tags[0];
613        if ($node_mode) {
614          $tag_id=$tag['node_tag_id'];
615          $result=$api->UpdateNodeTag($tag_id,$value);
616        } else {
617          $tag_id=$tag['interface_tag_id'];
618          $result=$api->UpdateInterfaceTag($tag_id,$value);
619        }
620        if ($result == 1) 
621          drupal_set_message ("Updated tag, new value = $value");
622        else
623          drupal_set_error ("Could not update tag");
624      } else {
625        if ($node_mode)
626          $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
627        else
628          $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
629        if ($tag_id) 
630          drupal_set_message ("Created tag, new value = $value");
631        else
632          drupal_set_error ("Could not create tag");
633      }
634    }
635    
636    if ($node_mode)
637      plc_redirect (l_node($node_id));
638    else
639      plc_redirect (l_interface($interface_id));
640  }
641
642  case 'delete-node-tags' : 
643  case 'delete-interface-tags' : {
644
645    $node_mode = false;
646    if ($action == 'delete-node-tags') $node_mode=true;
647
648    if ($node_mode)
649      $tag_ids=$_POST['node_tag_ids'];
650    else
651      $tag_ids=$_POST['interface_tag_ids'];
652
653    if ( ! $tag_ids) {
654      drupal_set_message("action=$action - No tag selected");
655      return;
656    }
657    $success=true;
658    $counter=0;
659    foreach( $tag_ids as $tag_id ) {
660      if ($node_mode)
661        $retcod = $api->DeleteNodeTag( intval( $tag_id ));
662      else
663        $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
664      if ($retcod != 1) 
665        $success=false;
666      else
667        $counter++;
668    }
669    if ($success) 
670      drupal_set_message ("Deleted $counter tag(s)");
671    else
672      drupal_set_error ("Could not delete all selected tags, only $counter were removed");
673    if ($node_mode)
674      plc_redirect(l_node($_POST['node_id']));
675    else
676      plc_redirect(l_interface($_POST['interface_id']));
677  }
678
679
680 ////////////////////////////////////////
681
682  case 'debug': {
683    plc_debug('GET',$_GET);
684    plc_debug('POST',$_POST);
685    plc_debug('FILES',$_FILES);
686    return;
687  }
688
689  default: {
690    plc_error ("Unknown action $action in actions.php");
691    return;
692  }
693
694  }
695
696 ?>