details can be updated inline - old forms still to be cleaned up
[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 //////////////////////////////////////// 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 //////////////////////////////////////// sites
53 $known_actions []= "delete-site";       
54 //      expects:        site_id
55 $known_actions []= "expire-all-slices-in-site";
56 //      expects:        slice_ids
57
58 //////////////////////////////////////// tags
59 $known_actions []= "update-tag-type";
60 //      expects:        tag_type_id & name & description & category & min_role_id  
61 $known_actions []= "add-tag-type";
62 //      expects:        tag_type_id & name & description & category & min_role_id  
63 $known_actions []= "set-tag-on-node";
64 //      expects:        node_id tagname value
65
66 //////////////////////////////
67 // sometimes we don't set 'action', but use the submit button name instead
68 // so if 'action' not set, see if $_POST has one of the actions as a key
69 if ($_POST['action']) 
70   $action=$_POST['action'];
71 else 
72   foreach ($known_actions as $known_action) 
73     if ($_POST[$known_action]) {
74       $action=$known_action;
75       break;
76     }
77
78 //debug
79 //$action='debug';
80
81 $person_id = $_POST['person_id'];       // usually needed
82
83 if ( ! $action ) {
84   drupal_set_message ("actions.php: action not set");
85   plc_debug('POST',$_POST);
86   return;
87  }
88
89 switch ($action) {
90
91  case 'add-person-to-site': {
92    $site_id = $_POST['site_id'];
93    $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
94    plc_redirect (l_person($person_id));
95  }
96
97  case 'remove-person-from-sites': {
98    $site_ids = $_POST['site_ids'];
99    if ( ! $site_ids) {
100      drupal_set_message("action=$action - No site selected");
101      return;
102    }
103    foreach ( $site_ids as $site_id ) {
104      $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
105    }
106    plc_redirect (l_person($person_id));
107  }
108
109  case 'remove-roles-from-person' : {
110    $role_ids=$_POST['role_ids'];
111    if ( ! $role_ids) {
112      drupal_set_message("action=$action - No role selected");
113      return;
114    }
115    foreach( $role_ids as $role_id)  {
116      $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
117    }
118    plc_redirect (l_person($person_id));
119  }
120      
121  case 'add-role-to-person' : {
122    $role_id=$_POST['role_id'];
123    $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
124    plc_redirect (l_person($person_id));
125  }
126
127  case 'enable-person' : {
128    $fields = array( "enabled"=>true );
129    $api->UpdatePerson( intval( $person_id ), $fields );
130    plc_redirect (l_person($person_id));
131  }
132
133  case 'disable-person' : {
134    $fields = array( "enabled"=>false );
135    $api->UpdatePerson( intval( $person_id ), $fields );
136    plc_redirect (l_person($person_id));
137  }
138
139  case 'become-person' : {
140    $plc->BecomePerson (intval($person_id));
141    plc_redirect (l_persons());
142  }
143
144  case 'delete-person' : {
145   $api->DeletePerson( intval( $person_id ) );
146    plc_redirect (l_persons());
147  }
148
149  case 'delete-keys' : {
150    $key_ids=$_POST['key_ids'];
151    if ( ! $key_ids) {
152      drupal_set_message("action=$action - No key selected");
153      return;
154    }
155    foreach( $key_ids as $key_id ) {
156      $api->DeleteKey( intval( $key_id ) );
157    }
158    plc_redirect(l_person($person_id));
159  }
160
161
162  case 'upload-key' : {
163    if ( ! isset( $_FILES['key'] ) ) {
164      drupal_set_message ("action=$action, no key file set");
165      return;
166    }
167    
168    $key_file= $_FILES['key']['tmp_name'];
169    if ( ! $key_file ) {
170      plc_error("Please select a valid SSH key file to upload");
171      return;
172    } 
173    $fp = fopen( $key_file, "r" );
174    $key = "";
175    if( ! $fp ) {
176      plc_error("Unable to open key file $key_file");
177      return;
178    }
179    // opened the key file, read the one line of contents
180    // The POST operation always creates a file even if the filename
181    // the user specified was garbage.  If there was some problem
182    // with the source file, we'll get a zero length read here.
183    $key = fread($fp, filesize($key_file));
184    fclose($fp);
185    
186    $key_id= $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
187    
188    if ( ! $key_id ) {
189      $error=  $api->error();
190      plc_error("$error");
191      plc_error("Please verify your SSH  file content");
192      return;
193    }
194    plc_redirect(l_person($person_id));
195  }
196
197  case 'update-person': {
198    $person_id=$_POST['person_id'];
199    // attempt to update this person
200    $first_name= $_POST['first_name'];
201    $last_name= $_POST['last_name'];
202    $title= $_POST['title'];
203    $email= $_POST['email'];
204    $phone= $_POST['phone'];
205    $url= $_POST['url'];
206    $bio= str_replace("\r", "", $_POST['bio']);
207    $password1= $_POST['password1'];
208    $password2= $_POST['password2'];
209
210    if( $password1 != $password2 ) {
211      drupal_set_error ("The passwords do not match");
212      plc_redirect(l_person($person_id));
213   }
214
215    $update_vals= array();
216    $update_vals['first_name']= $first_name;
217    $update_vals['last_name']= $last_name;
218    $update_vals['title']= $title;
219    $update_vals['email']= $email;
220    $update_vals['phone']= $phone;
221    $update_vals['url']= $url;
222    $update_vals['bio']= $bio;
223                 
224    if( $password1 != "" )
225      $update_vals['password']= $password1;
226     
227     $rc= $api->UpdatePerson( intval( $person_id ), $update_vals);
228     
229     if ( $rc == 1 ) {
230       drupal_set_message("$first_name $last_name updated");
231     } else {
232       drupal_set_error ("Could not update person $person_id" . $api->error());
233     }
234     plc_redirect(l_person($person_id));
235     break;
236   }
237
238 //////////////////////////////////////////////////////////// nodes
239  case 'node-boot-state': {
240    $node_id=intval($_POST['node_id']);
241    $boot_state=$_POST['boot_state'];
242    $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
243    if ($result==1) {
244      drupal_set_message("boot state updated");
245      plc_redirect (l_node($node_id));
246    } else {
247      drupal_set_error("Could not set boot_state '$boot_state'");
248    }
249    break;
250  }
251
252  case 'delete-node': {
253    $node_id=intval($_POST['node_id']);
254    $result=$api->DeleteNode( intval( $node_id ) );
255    if ($api==1) {
256      drupal_set_message("Node $node_id deleted");
257      plc_redirect (l_nodes());
258    } else {
259      drupal_set_error ("Could not delete node $node_id");
260    }
261    break;
262  }
263
264  case 'update-node': {
265    $hostname= $_POST['hostname'];
266    $model= $_POST['model'];
267
268    $fields= array( "hostname"=>$hostname, "model"=>$model );
269    $api->UpdateNode( intval( $node_id ), $fields );
270    $error= $api->error();
271
272    if( empty( $error ) ) {
273      drupal_set_message("Update node $hostname");
274      plc_redirect(l_node($node_id));
275    } else {
276      drupal_set_error($error);
277    }
278    break;
279  }
280
281 //////////////////////////////////////////////////////////// sites
282  case 'delete-site': {
283    $site_id = intval($_POST['site_id']);
284    if ($api->DeleteSite($site_id) ==1) 
285      drupal_set_message ("Site $site_id deleted");
286    else
287      drupal_set_error("Failed to delete site $site_id");
288    plc_redirect (l_sites());
289  }
290
291  case 'expire-all-slices-in-site': {
292    // xxx todo
293    drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
294    return;
295
296    //// old code from sites/expire.php
297    $sites = $api->GetSites( array( intval( $site_id )));
298    $site=$sites[0];
299    // xxx why not 'now?'
300    $expiration= strtotime( $_POST['expires'] );
301    // loop through all slices for site
302    foreach ($site['slice_ids'] as $slice_id) {
303      $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
304    }
305    // update site to not allow slice creation or renewal
306    $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
307    plc_redirect (l_site($site_id));
308  }
309
310 //////////////////////////////////////////////////////////// tags
311
312  case 'update-tag-type': {
313   // get post vars 
314    $tag_type_id= intval( $_POST['tag_type_id'] );
315    $name = $_POST['name'];
316    $min_role_id= intval( $_POST['min_role_id'] );
317    $description= $_POST['description'];  
318    $category= $_POST['category'];  
319   
320    // make tag_type_fields dict
321    $tag_type_fields= array( "min_role_id" => $min_role_id, 
322                             "tagname" => $name, 
323                             "description" => $description,
324                             "category" => $category,
325                             );
326
327    // Update it!
328    $api->UpdateTagType( $tag_type_id, $tag_type_fields );
329    
330    plc_redirect(l_tag($tag_type_id));
331  }
332
333  case 'add-tag-type': {
334   // get post vars 
335    $name = $_POST['name'];
336    $min_role_id= intval( $_POST['min_role_id'] );
337    $description= $_POST['description'];  
338    $category= $_POST['category'];  
339   
340    // make tag_type_fields dict
341    $tag_type_fields= array( "min_role_id" => $min_role_id, 
342                             "tagname" => $name, 
343                             "description" => $description,
344                             "category" => $category,
345                             );
346
347   // Add it!
348    $id=$api->AddTagType( $tag_type_fields );
349    drupal_set_message ("tag type $id created");
350   
351    plc_redirect( l_tag($id));
352  }
353
354  case 'set-tag-on-node': {
355
356    $node_id = intval($_POST['node_id']);
357    $tag_type_id = intval($_POST['tag_type_id']);
358    $value = $_POST['value'];
359
360    $tag_types=$api->GetTagTypes(array($tag_type_id));
361    if (count ($tag_types) != 1) {
362      drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
363    } else {
364      $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
365      if ( count ($tags) == 1) {
366        $tag=$tags[0];
367        $tag_id=$tag['node_tag_id'];
368        $result=$api->UpdateNodeTag($tag_id,$value);
369        if ($result == 1) 
370          drupal_set_message ("Updated tag, new value = $value");
371        else
372          drupal_set_error ("Could not update tag");
373      } else {
374        $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
375        if ($tag_id) 
376          drupal_set_message ("Created tag, new value = $value");
377        else
378          drupal_set_error ("Could not create tag");
379      }
380    }
381    
382    plc_redirect (l_node($node_id));
383  }
384
385 ////////////////////////////////////////
386
387  case 'debug': {
388    plc_debug('GET',$_GET);
389    plc_debug('POST',$_POST);
390    plc_debug('FILES',$_FILES);
391    return;
392  }
393
394  default: {
395    plc_error ("Unknown action $action in actions.php");
396    return;
397  }
398
399  }
400
401 ?>