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