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