ckp
[plewww.git] / planetlab / 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 $known_actions []= "add-person-to-site";
22 //      expects:        person_id & site_id
23 $known_actions []= "remove-person-from-sites";
24 //      expects:        person_id & site_ids
25 $known_actions []= "remove-roles-from-person";
26 //      expects:        person_id & role_ids
27 $known_actions []= "add-role-to-person";
28 //      expects:        role_person_id & id
29 $known_actions []= "enable-person";
30 //      expects:        person_id
31 $known_actions []= "disable-person";
32 //      expects:        person_id
33 $known_actions []= "become-person";
34 //      expects:        person_id
35 $known_actions []= "delete-person";
36 //      expects:        person_id
37 $known_actions []= "delete-keys";
38 //      expects:        key_ids & person_id (for redirecting to the person's page)
39 $known_actions []= "upload-key";
40 //      expects:        person_id & $_FILES['key']
41 $known_actions []= "update-tag-type";
42 //      expects:        tag_type_id & name & description & category & min_role_id  
43 $known_actions []= "add-tag-type";
44 //      expects:        tag_type_id & name & description & category & min_role_id  
45 $known_actions []= "delete-site";       
46 //      expects:        site_id
47 $known_actions []= "expire-all-slices-in-site";
48 //      expects:        slice_ids
49
50 //////////////////////////////
51 // sometimes we don't set 'action', but use the submit button name instead
52 // so if 'action' not set, see if $_POST has one of the actions as a key
53 if ($_POST['action']) 
54   $action=$_POST['action'];
55 else 
56   foreach ($known_actions as $known_action) 
57     if ($_POST[$known_action]) {
58       $action=$known_action;
59       break;
60     }
61
62 //debug
63 //$action='debug';
64
65 $person_id = $_POST['person_id'];       // usually needed
66
67 if ( ! $action ) {
68   drupal_set_message ("actions.php: action not set");
69   plc_debug('POST',$_POST);
70   return;
71  }
72
73 switch ($action) {
74
75  case 'add-person-to-site': {
76    $site_id = $_POST['site_id'];
77    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
78    header( "location: " . l_person($person_id));
79    exit();
80  }
81
82  case 'remove-person-from-sites': {
83    $site_ids = $_POST['site_ids'];
84    if ( ! $site_ids) {
85      drupal_set_message("action=$action - No site selected");
86      return;
87    }
88    foreach ( $site_ids as $site_id ) {
89      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
90    }
91    header( "location: " . l_person($person_id));
92    exit();
93  }
94
95  case 'remove-roles-from-person' : {
96    $role_ids=$_POST['role_ids'];
97    if ( ! $role_ids) {
98      drupal_set_message("action=$action - No role selected");
99      return;
100    }
101    foreach( $role_ids as $role_id)  {
102      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
103    }
104    header( "location: " . l_person($person_id));
105    exit();
106  }
107      
108  case 'add-role-to-person' : {
109    $role_id=$_POST['role_id'];
110    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
111    header( "location: " . l_person($person_id));
112    exit();
113  }
114
115  case 'enable-person' : {
116    $fields = array( "enabled"=>true );
117    $api->UpdatePerson( intval( $person_id ), $fields );
118    header( "location: " . l_person($person_id));
119    exit();
120  }
121
122  case 'disable-person' : {
123    $fields = array( "enabled"=>false );
124    $api->UpdatePerson( intval( $person_id ), $fields );
125    header( "location: " . l_person($person_id));
126    exit();
127  }
128
129  case 'become-person' : {
130    $plc->BecomePerson (intval($person_id));
131    header ("location: " . l_persons());
132    exit();
133  }
134
135  case 'delete-person' : {
136   $api->DeletePerson( intval( $person_id ) );
137   header( "location: " . l_persons() );
138   exit();
139  }
140
141  case 'delete-keys' : {
142    $key_ids=$_POST['key_ids'];
143    if ( ! $key_ids) {
144      drupal_set_message("action=$action - No key selected");
145      return;
146    }
147    foreach( $key_ids as $key_id ) {
148      $api->DeleteKey( intval( $key_id ) );
149    }
150    header( "location: " . l_person($person_id));
151    exit();
152  }
153
154  case 'upload-key' : {
155    if ( ! isset( $_FILES['key'] ) ) {
156      drupal_set_message ("action=$action, no key file set");
157      return;
158    }
159    
160    $key_file= $_FILES['key']['tmp_name'];
161    if ( ! $key_file ) {
162      plc_error("Please select a valid SSH key file to upload");
163      return;
164    } 
165    $fp = fopen( $key_file, "r" );
166    $key = "";
167    if( ! $fp ) {
168      plc_error("Unable to open key file $key_file");
169      return;
170    }
171    // opened the key file, read the one line of contents
172    // The POST operation always creates a file even if the filename
173    // the user specified was garbage.  If there was some problem
174    // with the source file, we'll get a zero length read here.
175    $key = fread($fp, filesize($key_file));
176    fclose($fp);
177    
178    $key_id= $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
179    
180    if ( ! $key_id ) {
181      $error=  $api->error();
182      plc_error("$error");
183      plc_error("Please verify your SSH  file content");
184      return;
185    }
186    header( "location: " . l_person($person_id));
187    exit();
188  }
189
190  case 'update-tag-type': {
191   // get post vars 
192    $tag_type_id= intval( $_POST['tag_type_id'] );
193    $name = $_POST['name'];
194    $min_role_id= intval( $_POST['min_role_id'] );
195    $description= $_POST['description'];  
196    $category= $_POST['category'];  
197   
198    // make tag_type_fields dict
199    $tag_type_fields= array( "min_role_id" => $min_role_id, 
200                             "tagname" => $name, 
201                             "description" => $description,
202                             "category" => $category,
203                             );
204
205    // Update it!
206    $api->UpdateTagType( $tag_type_id, $tag_type_fields );
207    
208    header( "location: " . l_tag($tag_type_id));
209    exit();
210  }
211
212  case 'add-tag-type': {
213   // get post vars 
214    $name = $_POST['name'];
215    $min_role_id= intval( $_POST['min_role_id'] );
216    $description= $_POST['description'];  
217    $category= $_POST['category'];  
218   
219    // make tag_type_fields dict
220    $tag_type_fields= array( "min_role_id" => $min_role_id, 
221                             "tagname" => $name, 
222                             "description" => $description,
223                             "category" => $category,
224                             );
225
226   // Add it!
227    $id=$api->AddTagType( $tag_type_fields );
228    drupal_set_message ("tag type $id created");
229   
230    header( "location: " . l_tag($id));
231    exit();
232  }
233
234  case 'delete-site': {
235    $site_id = intval($_POST['site_id']);
236    if ($api->DeleteSite($site_id) ==1) 
237      drupal_set_message ("Site $site_id deleted");
238    else
239      drupal_set_error("Failed to delete site $site_id");
240    header ("location: " . l_sites());
241    exit();
242  }
243
244  case 'expire-all-slices-in-site': {
245    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
246    return;
247
248    //// old code from sites/expire.php
249    $sites = $api->GetSites( array( intval( $site_id )));
250    $site=$sites[0];
251    // xxx why not 'now?'
252    $expiration= strtotime( $_POST['expires'] );
253    // loop through all slices for site
254    foreach ($site['slice_ids'] as $slice_id) {
255      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
256    }
257    // update site to not allow slice creation or renewal
258    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
259    header ("location: " . l_site($site_id));
260    exit(0);
261  }
262
263  case 'debug': {
264    plc_debug('GET',$_GET);
265    plc_debug('POST',$_POST);
266    plc_debug('FILES',$_FILES);
267    return;
268  }
269
270  default: {
271    plc_error ("Unknown action $action in actions.php");
272    return;
273  }
274
275  }
276
277 ?>