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