4 require_once 'plc_login.php';
6 // Get session and API handles
7 require_once 'plc_session.php';
11 require_once 'plc_drupal.php';
14 require_once 'plc_functions.php';
16 $known_actions=array();
17 ////////////////////////////////////////////////////////////
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";
32 $known_actions []= "disable-person";
34 $known_actions []= "become-person";
36 $known_actions []= "delete-person";
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]
45 //////////////////////////////////////// nodes
46 $known_actions []= "node-boot-state";
47 // expects: node_id boot_state
48 $known_actions []= "delete-node";
50 $known_actions []= "update-node";
51 // expects: node_id, hostname, model
52 //////////////////////////////////////// sites
53 $known_actions []= "delete-site";
55 $known_actions []= "expire-all-slices-in-site";
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
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
70 $action=$_POST['action'];
72 foreach ($known_actions as $known_action)
73 if ($_POST[$known_action]) {
74 $action=$known_action;
81 $person_id = $_POST['person_id']; // usually needed
84 drupal_set_message ("actions.php: action not set");
85 plc_debug('POST',$_POST);
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));
97 case 'remove-person-from-sites': {
98 $site_ids = $_POST['site_ids'];
100 drupal_set_message("action=$action - No site selected");
103 foreach ( $site_ids as $site_id ) {
104 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
106 plc_redirect (l_person($person_id));
109 case 'remove-roles-from-person' : {
110 $role_ids=$_POST['role_ids'];
112 drupal_set_message("action=$action - No role selected");
115 foreach( $role_ids as $role_id) {
116 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
118 plc_redirect (l_person($person_id));
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));
127 case 'enable-person' : {
128 $fields = array( "enabled"=>true );
129 $api->UpdatePerson( intval( $person_id ), $fields );
130 plc_redirect (l_person($person_id));
133 case 'disable-person' : {
134 $fields = array( "enabled"=>false );
135 $api->UpdatePerson( intval( $person_id ), $fields );
136 plc_redirect (l_person($person_id));
139 case 'become-person' : {
140 $plc->BecomePerson (intval($person_id));
141 plc_redirect (l_persons());
144 case 'delete-person' : {
145 $api->DeletePerson( intval( $person_id ) );
146 plc_redirect (l_persons());
149 case 'delete-keys' : {
150 $key_ids=$_POST['key_ids'];
152 drupal_set_message("action=$action - No key selected");
155 foreach( $key_ids as $key_id ) {
156 $api->DeleteKey( intval( $key_id ) );
158 plc_redirect(l_person($person_id));
162 case 'upload-key' : {
163 if ( ! isset( $_FILES['key'] ) ) {
164 drupal_set_message ("action=$action, no key file set");
168 $key_file= $_FILES['key']['tmp_name'];
170 plc_error("Please select a valid SSH key file to upload");
173 $fp = fopen( $key_file, "r" );
176 plc_error("Unable to open key file $key_file");
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));
186 $key_id= $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
189 $error= $api->error();
191 plc_error("Please verify your SSH file content");
194 plc_redirect(l_person($person_id));
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'];
206 $bio= str_replace("\r", "", $_POST['bio']);
207 $password1= $_POST['password1'];
208 $password2= $_POST['password2'];
210 if( $password1 != $password2 ) {
211 drupal_set_error ("The passwords do not match");
212 plc_redirect(l_person($person_id));
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;
224 if( $password1 != "" )
225 $update_vals['password']= $password1;
227 $rc= $api->UpdatePerson( intval( $person_id ), $update_vals);
230 drupal_set_message("$first_name $last_name updated");
232 drupal_set_error ("Could not update person $person_id" . $api->error());
234 plc_redirect(l_person($person_id));
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 ) );
244 drupal_set_message("boot state updated");
245 plc_redirect (l_node($node_id));
247 drupal_set_error("Could not set boot_state '$boot_state'");
252 case 'delete-node': {
253 $node_id=intval($_POST['node_id']);
254 $result=$api->DeleteNode( intval( $node_id ) );
256 drupal_set_message("Node $node_id deleted");
257 plc_redirect (l_nodes());
259 drupal_set_error ("Could not delete node $node_id");
264 case 'update-node': {
265 $hostname= $_POST['hostname'];
266 $model= $_POST['model'];
268 $fields= array( "hostname"=>$hostname, "model"=>$model );
269 $api->UpdateNode( intval( $node_id ), $fields );
270 $error= $api->error();
272 if( empty( $error ) ) {
273 drupal_set_message("Update node $hostname");
274 plc_redirect(l_node($node_id));
276 drupal_set_error($error);
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");
287 drupal_set_error("Failed to delete site $site_id");
288 plc_redirect (l_sites());
291 case 'expire-all-slices-in-site': {
293 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
296 //// old code from sites/expire.php
297 $sites = $api->GetSites( array( intval( $site_id )));
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 ) );
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));
310 //////////////////////////////////////////////////////////// tags
312 case 'update-tag-type': {
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'];
320 // make tag_type_fields dict
321 $tag_type_fields= array( "min_role_id" => $min_role_id,
323 "description" => $description,
324 "category" => $category,
328 $api->UpdateTagType( $tag_type_id, $tag_type_fields );
330 plc_redirect(l_tag($tag_type_id));
333 case 'add-tag-type': {
335 $name = $_POST['name'];
336 $min_role_id= intval( $_POST['min_role_id'] );
337 $description= $_POST['description'];
338 $category= $_POST['category'];
340 // make tag_type_fields dict
341 $tag_type_fields= array( "min_role_id" => $min_role_id,
343 "description" => $description,
344 "category" => $category,
348 $id=$api->AddTagType( $tag_type_fields );
349 drupal_set_message ("tag type $id created");
351 plc_redirect( l_tag($id));
354 case 'set-tag-on-node': {
356 $node_id = intval($_POST['node_id']);
357 $tag_type_id = intval($_POST['tag_type_id']);
358 $value = $_POST['value'];
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.");
364 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
365 if ( count ($tags) == 1) {
367 $tag_id=$tag['node_tag_id'];
368 $result=$api->UpdateNodeTag($tag_id,$value);
370 drupal_set_message ("Updated tag, new value = $value");
372 drupal_set_error ("Could not update tag");
374 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
376 drupal_set_message ("Created tag, new value = $value");
378 drupal_set_error ("Could not create tag");
382 plc_redirect (l_node($node_id));
385 ////////////////////////////////////////
388 plc_debug('GET',$_GET);
389 plc_debug('POST',$_POST);
390 plc_debug('FILES',$_FILES);
395 plc_error ("Unknown action $action in actions.php");