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
53 //////////////////////////////////////// interfaces
54 $known_actions []= "delete-interfaces";
55 // expects: interface_ids
56 $known_actions []="add-interface";
57 // expects: node_id & interface details
58 $known_actions []="update-interface";
59 // expects: interface_id & interface details
61 //////////////////////////////////////// sites
62 $known_actions []= "delete-site";
64 $known_actions []= "expire-all-slices-in-site";
66 $known_actions []= "update-site";
67 // expects: site_id & name abbreviated_name url latitude longitude [login_base max_slices]
69 //////////////////////////////////////// slices
70 $known_actions []= "renew-slice";
71 // expects: slice_id & expires
73 //////////////////////////////////////// tag types
74 $known_actions []= "update-tag-type";
75 // expects: tag_type_id & name & description & category & min_role_id
76 $known_actions []= "add-tag-type";
77 // expects: tag_type_id & tagname & description & category & min_role_id
78 $known_actions []= "delete-tag-types";
79 // expects: tag_type_ids
81 //////////////////////////////////////// tags
82 $known_actions []= "set-tag-on-node";
83 // expects: node_id tagname value
84 $known_actions []= "set-tag-on-interface";
85 // expects: interface_id tagname value
86 $known_actions []= "delete-node-tags";
87 // expects: node_id & node_tag_ids
88 $known_actions []= "delete-interface-tags";
89 // expects: interface_id & interface_tag_ids
91 ////////////////////////////////////////////////////////////
92 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
93 'broadcast', 'netmask', 'dns1', 'dns2',
94 'hostname', 'mac', 'bwlimit' );
96 //////////////////////////////
97 // sometimes we don't set 'action', but use the submit button name instead
98 // so if 'action' not set, see if $_POST has one of the actions as a key
100 $action=$_POST['action'];
102 foreach ($known_actions as $known_action)
103 if ($_POST[$known_action]) {
104 $action=$known_action;
108 //uncomment for debugging incoming data
111 $person_id = $_POST['person_id']; // usually needed
114 drupal_set_message ("actions.php: action not set or not in known_actions");
115 plc_debug('POST',$_POST);
121 case 'add-person-to-site': {
122 $site_id = $_POST['site_id'];
123 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
124 plc_redirect (l_person($person_id));
127 case 'remove-person-from-sites': {
128 $site_ids = $_POST['site_ids'];
130 drupal_set_message("action=$action - No site selected");
133 foreach ( $site_ids as $site_id ) {
134 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
136 plc_redirect (l_person($person_id));
139 case 'remove-roles-from-person' : {
140 $role_ids=$_POST['role_ids'];
142 drupal_set_message("action=$action - No role selected");
145 foreach( $role_ids as $role_id) {
146 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
148 plc_redirect (l_person($person_id));
151 case 'add-role-to-person' : {
152 $role_id=$_POST['role_id'];
153 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
154 plc_redirect (l_person($person_id));
157 case 'enable-person' : {
158 $fields = array( "enabled"=>true );
159 $api->UpdatePerson( intval( $person_id ), $fields );
160 plc_redirect (l_person($person_id));
163 case 'disable-person' : {
164 $fields = array( "enabled"=>false );
165 $api->UpdatePerson( intval( $person_id ), $fields );
166 plc_redirect (l_person($person_id));
169 case 'become-person' : {
170 $plc->BecomePerson (intval($person_id));
171 plc_redirect (l_persons());
174 case 'delete-person' : {
175 $api->DeletePerson( intval( $person_id ) );
176 plc_redirect (l_persons());
179 case 'delete-keys' : {
180 $key_ids=$_POST['key_ids'];
182 drupal_set_message("action=$action - No key selected");
187 foreach( $key_ids as $key_id ) {
188 if ($api->DeleteKey( intval( $key_id )) != 1)
194 drupal_set_message ("Deleted $counter key(s)");
196 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
197 plc_redirect(l_person($person_id));
201 case 'upload-key' : {
202 if ( ! isset( $_FILES['key'] ) ) {
203 drupal_set_message ("action=$action, no key file set");
207 $key_file= $_FILES['key']['tmp_name'];
209 plc_error("Please select a valid SSH key file to upload");
212 $fp = fopen( $key_file, "r" );
215 plc_error("Unable to open key file $key_file");
218 // opened the key file, read the one line of contents
219 // The POST operation always creates a file even if the filename
220 // the user specified was garbage. If there was some problem
221 // with the source file, we'll get a zero length read here.
222 $key = fread($fp, filesize($key_file));
225 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
228 drupal_set_message ("New key added");
230 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
232 plc_redirect(l_person($person_id));
235 case 'update-person': {
236 $person_id=$_POST['person_id'];
237 // attempt to update this person
238 $first_name= $_POST['first_name'];
239 $last_name= $_POST['last_name'];
240 $title= $_POST['title'];
241 $email= $_POST['email'];
242 $phone= $_POST['phone'];
244 $bio= str_replace("\r", "", $_POST['bio']);
245 $password1= $_POST['password1'];
246 $password2= $_POST['password2'];
248 if( $password1 != $password2 ) {
249 drupal_set_error ("The passwords do not match");
250 plc_redirect(l_person($person_id));
254 $fields['first_name']= $first_name;
255 $fields['last_name']= $last_name;
256 $fields['title']= $title;
257 $fields['email']= $email;
258 $fields['phone']= $phone;
259 $fields['url']= $url;
260 $fields['bio']= $bio;
262 if ( $password1 != "" )
263 $fields['password']= $password1;
265 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
266 drupal_set_message("$first_name $last_name updated");
268 drupal_set_error ("Could not update person $person_id" . $api->error());
270 plc_redirect(l_person($person_id));
274 //////////////////////////////////////////////////////////// nodes
275 case 'node-boot-state': {
276 $node_id=intval($_POST['node_id']);
277 $boot_state=$_POST['boot_state'];
278 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
280 drupal_set_message("boot state updated");
281 plc_redirect (l_node($node_id));
283 drupal_set_error("Could not set boot_state '$boot_state'");
288 case 'delete-node': {
289 $node_id=intval($_POST['node_id']);
290 $result=$api->DeleteNode( intval( $node_id ) );
292 drupal_set_message("Node $node_id deleted");
293 plc_redirect (l_nodes());
295 drupal_set_error ("Could not delete node $node_id");
300 case 'update-node': {
301 $hostname= $_POST['hostname'];
302 $model= $_POST['model'];
304 $fields= array( "hostname"=>$hostname, "model"=>$model );
305 $api->UpdateNode( intval( $node_id ), $fields );
306 $error= $api->error();
308 if( empty( $error ) ) {
309 drupal_set_message("Update node $hostname");
310 plc_redirect(l_node($node_id));
312 drupal_set_error($error);
317 //////////////////////////////////////////////////////////// interfaces
318 case 'delete-interfaces' : {
319 $interface_ids=$_POST['interface_ids'];
320 if ( ! $interface_ids) {
321 drupal_set_message("action=$action - No interface selected");
326 foreach( $interface_ids as $interface_id ) {
327 if ($api->DeleteInterface( intval( $interface_id )) != 1)
333 drupal_set_message ("Deleted $counter interface(s)");
335 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
336 plc_redirect(l_node($_POST['node_id']));
339 case 'add-interface': {
340 $node_id=$_POST['node_id'];
341 foreach ($interface_details as $field) {
342 $interface[$field]= $_POST[$field];
343 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
344 $interface[$field]= intval( $interface[$field] );
347 $result=$api->AddInterface( intval( $node_id ), $interface );
349 drupal_set_message ("Interface $result added into node $node_id");
351 drupal_set_error ("Could not create interface");
352 plc_redirect (l_node($node_id));
355 case 'update-interface': {
356 $interface_id=$_POST['interface_id'];
357 foreach ($interface_details as $field) {
358 $interface[$field]= $_POST[$field];
359 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
360 $interface[$field]= intval( $interface[$field] );
363 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
365 drupal_set_message ("Interface $interface_id updated");
367 drupal_set_error ("Could not update interface");
368 plc_redirect (l_interface($interface_id));
371 //////////////////////////////////////////////////////////// sites
372 case 'delete-site': {
373 $site_id = intval($_POST['site_id']);
374 if ($api->DeleteSite($site_id) ==1)
375 drupal_set_message ("Site $site_id deleted");
377 drupal_set_error("Failed to delete site $site_id");
378 plc_redirect (l_sites());
382 case 'expire-all-slices-in-site': {
384 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
387 //// old code from sites/expire.php
388 $sites = $api->GetSites( array( intval( $site_id )));
390 // xxx why not 'now?'
391 $expiration= strtotime( $_POST['expires'] );
392 // loop through all slices for site
393 foreach ($site['slice_ids'] as $slice_id) {
394 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
396 // update site to not allow slice creation or renewal
397 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
398 plc_redirect (l_site($site_id));
402 case 'update-site': {
403 $site_id=intval($_POST['site_id']);
404 $name= $_POST['name'];
405 $abbreviated_name= $_POST['abbreviated_name'];
407 $latitude= floatval($_POST['latitude']);
408 $longitude= floatval($_POST['longitude']);
409 //$max_slivers= $_POST['max_slivers'];
411 $fields= array( "name" => $name,
412 "abbreviated_name" => $abbreviated_name,
414 "latitude" => floatval( $latitude ),
415 "longitude" => floatval( $longitude ));
417 if ($_POST['login_base'])
418 $fields['login_base'] = $_POST['login_base'];
419 if ($_POST['max_slices'])
420 $fields['max_slices'] = intval($_POST['max_slices']);
422 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
424 drupal_set_message("Site $name updated");
426 drupal_set_error ("Could not update site $site_id");
428 plc_redirect(l_site($site_id));
432 //////////////////////////////////////////////////////////// slices
433 case 'renew_slice': {
434 $slice_id = intval ($_POST['slice_id']);
435 $expires = intval ($_POST['expires']);
440 $MAX_FUTURE=$WEEKS*7*24*3600;
441 if ( ($expires-$now) > $MAX_FUTURE) {
442 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS from now");
443 plc_redirect(l_slice($slice_id));
445 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
446 drupal_set_message("Slice renewed");
448 drupal_set_error("Could not update slice $slice_id");
449 plc_redirect(l_slice($slice_id));
454 //////////////////////////////////////////////////////////// tag types
456 case 'update-tag-type': {
458 $tag_type_id= intval( $_POST['tag_type_id'] );
459 $tagname = $_POST['tagname'];
460 $min_role_id= intval( $_POST['min_role_id'] );
461 $description= $_POST['description'];
462 $category= $_POST['category'];
464 // make tag_type_fields dict
465 $tag_type_fields= array( "min_role_id" => $min_role_id,
466 "tagname" => $tagname,
467 "description" => $description,
468 "category" => $category,
471 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
472 drupal_set_message ("Tag type $tagname updated");
474 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
475 plc_redirect(l_tag($tag_type_id));
478 case 'add-tag-type': {
480 $tagname = $_POST['tagname'];
481 $min_role_id= intval( $_POST['min_role_id'] );
482 $description= $_POST['description'];
483 $category= $_POST['category'];
485 // make tag_type_fields dict
486 $tag_type_fields= array( "min_role_id" => $min_role_id,
487 "tagname" => $tagname,
488 "description" => $description,
489 "category" => $category,
493 $tag_type_id=$api->AddTagType( $tag_type_fields );
494 if ($tag_type_id > 0)
495 drupal_set_message ("tag type $tag_type_id created");
497 drupal_set_error ("Could not create tag type $tagname");
498 plc_redirect( l_tags());
501 case 'delete-tag-types': {
502 $tag_type_ids = $_POST['tag_type_ids'];
503 if ( ! $tag_type_ids) {
504 drupal_set_message("action=$action - No tag selected");
509 foreach ($tag_type_ids as $tag_type_id)
510 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
515 drupal_set_message ("Deleted $counter tag(s)");
517 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
518 plc_redirect (l_tags());
522 //////////////////////////////////////// tags
523 case 'set-tag-on-node':
524 case 'set-tag-on-interface': {
527 if ($action == 'set-tag-on-node') $node_mode=true;
530 $node_id = intval($_POST['node_id']);
532 $interface_id=intval($_POST['interface_id']);
533 $tag_type_id = intval($_POST['tag_type_id']);
534 $value = $_POST['value'];
536 $tag_types=$api->GetTagTypes(array($tag_type_id));
537 if (count ($tag_types) != 1) {
538 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
541 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
543 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
544 if ( count ($tags) == 1) {
547 $tag_id=$tag['node_tag_id'];
548 $result=$api->UpdateNodeTag($tag_id,$value);
550 $tag_id=$tag['interface_tag_id'];
551 $result=$api->UpdateInterfaceTag($tag_id,$value);
554 drupal_set_message ("Updated tag, new value = $value");
556 drupal_set_error ("Could not update tag");
559 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
561 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
563 drupal_set_message ("Created tag, new value = $value");
565 drupal_set_error ("Could not create tag");
570 plc_redirect (l_node($node_id));
572 plc_redirect (l_interface($interface_id));
575 case 'delete-node-tags' :
576 case 'delete-interface-tags' : {
579 if ($action == 'delete-node-tags') $node_mode=true;
582 $tag_ids=$_POST['node_tag_ids'];
584 $tag_ids=$_POST['interface_tag_ids'];
587 drupal_set_message("action=$action - No tag selected");
592 foreach( $tag_ids as $tag_id ) {
594 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
596 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
603 drupal_set_message ("Deleted $counter tag(s)");
605 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
607 plc_redirect(l_node($_POST['node_id']));
609 plc_redirect(l_interface($_POST['interface_id']));
613 ////////////////////////////////////////
616 plc_debug('GET',$_GET);
617 plc_debug('POST',$_POST);
618 plc_debug('FILES',$_FILES);
623 plc_error ("Unknown action $action in actions.php");