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