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