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