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 []= "delete-slice";
72 $known_actions []= "update-slice";
73 // expects: slice_id, name, description, url
74 $known_actions []= "renew-slice";
75 // expects: slice_id & expires
76 $known_actions []= 'remove-persons-from-slice';
77 // expects: slice_id & person_ids
78 $known_actions []= 'add-persons-in-slice';
79 // expects: slice_id & person_ids
80 $known_actions []= 'remove-nodes-from-slice';
81 // expects: slice_id & node_ids
82 $known_actions []= 'add-nodes-in-slice';
83 // expects: slice_id & node_ids
85 //////////////////////////////////////// tag types
86 $known_actions []= "update-tag-type";
87 // expects: tag_type_id & name & description & category & min_role_id
88 $known_actions []= "add-tag-type";
89 // expects: tag_type_id & tagname & description & category & min_role_id
90 $known_actions []= "delete-tag-types";
91 // expects: tag_type_ids
93 //////////////////////////////////////// tags
94 $known_actions []= "set-tag-on-node";
95 // expects: node_id tagname value
96 $known_actions []= "set-tag-on-interface";
97 // expects: interface_id tagname value
98 $known_actions []= "delete-node-tags";
99 // expects: node_id & node_tag_ids
100 $known_actions []= "delete-interface-tags";
101 // expects: interface_id & interface_tag_ids
103 ////////////////////////////////////////////////////////////
104 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
105 'broadcast', 'netmask', 'dns1', 'dns2',
106 'hostname', 'mac', 'bwlimit' );
108 //////////////////////////////
109 // sometimes we don't set 'action', but use the submit button name instead
110 // so if 'action' not set, see if $_POST has one of the actions as a key
111 if ($_POST['action'])
112 $action=$_POST['action'];
114 foreach ($known_actions as $known_action)
115 if ($_POST[$known_action]) {
116 $action=$known_action;
120 //uncomment for debugging incoming data
123 $person_id = $_POST['person_id']; // usually needed
126 drupal_set_message ("actions.php: action not set or not in known_actions");
127 plc_debug('POST',$_POST);
133 case 'add-person-to-site': {
134 $site_id = $_POST['site_id'];
135 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
136 plc_redirect (l_person($person_id));
139 case 'remove-person-from-sites': {
140 $site_ids = $_POST['site_ids'];
142 drupal_set_message("action=$action - No site selected");
145 foreach ( $site_ids as $site_id ) {
146 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
148 plc_redirect (l_person($person_id));
151 case 'remove-roles-from-person' : {
152 $role_ids=$_POST['role_ids'];
154 drupal_set_message("action=$action - No role selected");
157 foreach( $role_ids as $role_id) {
158 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
160 plc_redirect (l_person($person_id));
163 case 'add-role-to-person' : {
164 $role_id=$_POST['role_id'];
165 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
166 plc_redirect (l_person($person_id));
169 case 'enable-person' : {
170 $fields = array( "enabled"=>true );
171 $api->UpdatePerson( intval( $person_id ), $fields );
172 plc_redirect (l_person($person_id));
175 case 'disable-person' : {
176 $fields = array( "enabled"=>false );
177 $api->UpdatePerson( intval( $person_id ), $fields );
178 plc_redirect (l_person($person_id));
181 case 'become-person' : {
182 $plc->BecomePerson (intval($person_id));
183 plc_redirect (l_persons());
186 case 'delete-person' : {
187 $api->DeletePerson( intval( $person_id ) );
188 plc_redirect (l_persons());
191 case 'delete-keys' : {
192 $key_ids=$_POST['key_ids'];
194 drupal_set_message("action=$action - No key selected");
199 foreach( $key_ids as $key_id ) {
200 if ($api->DeleteKey( intval( $key_id )) != 1)
206 drupal_set_message ("Deleted $counter key(s)");
208 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
209 plc_redirect(l_person($person_id));
213 case 'upload-key' : {
214 if ( ! isset( $_FILES['key'] ) ) {
215 drupal_set_message ("action=$action, no key file set");
219 $key_file= $_FILES['key']['tmp_name'];
221 plc_error("Please select a valid SSH key file to upload");
224 $fp = fopen( $key_file, "r" );
227 plc_error("Unable to open key file $key_file");
230 // opened the key file, read the one line of contents
231 // The POST operation always creates a file even if the filename
232 // the user specified was garbage. If there was some problem
233 // with the source file, we'll get a zero length read here.
234 $key = fread($fp, filesize($key_file));
237 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
240 drupal_set_message ("New key added");
242 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
244 plc_redirect(l_person($person_id));
247 case 'update-person': {
248 $person_id=$_POST['person_id'];
249 // attempt to update this person
250 $first_name= $_POST['first_name'];
251 $last_name= $_POST['last_name'];
252 $title= $_POST['title'];
253 $email= $_POST['email'];
254 $phone= $_POST['phone'];
256 $bio= str_replace("\r", "", $_POST['bio']);
257 $password1= $_POST['password1'];
258 $password2= $_POST['password2'];
260 if( $password1 != $password2 ) {
261 drupal_set_error ("The passwords do not match");
262 plc_redirect(l_person($person_id));
266 $fields['first_name']= $first_name;
267 $fields['last_name']= $last_name;
268 $fields['title']= $title;
269 $fields['email']= $email;
270 $fields['phone']= $phone;
271 $fields['url']= $url;
272 $fields['bio']= $bio;
274 if ( $password1 != "" )
275 $fields['password']= $password1;
277 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
278 drupal_set_message("$first_name $last_name updated");
280 drupal_set_error ("Could not update person $person_id" . $api->error());
282 plc_redirect(l_person($person_id));
286 //////////////////////////////////////////////////////////// nodes
287 case 'node-boot-state': {
288 $node_id=intval($_POST['node_id']);
289 $boot_state=$_POST['boot_state'];
290 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
292 drupal_set_message("boot state updated");
293 plc_redirect (l_node($node_id));
295 drupal_set_error("Could not set boot_state '$boot_state'");
300 case 'delete-node': {
301 $node_id=intval($_POST['node_id']);
302 $result=$api->DeleteNode( intval( $node_id ) );
304 drupal_set_message("Node $node_id deleted");
305 plc_redirect (l_nodes());
307 drupal_set_error ("Could not delete node $node_id");
312 case 'update-node': {
313 $hostname= $_POST['hostname'];
314 $model= $_POST['model'];
316 $fields= array( "hostname"=>$hostname, "model"=>$model );
317 $api->UpdateNode( intval( $node_id ), $fields );
318 $error= $api->error();
320 if( empty( $error ) ) {
321 drupal_set_message("Update node $hostname");
322 plc_redirect(l_node($node_id));
324 drupal_set_error($error);
329 //////////////////////////////////////////////////////////// interfaces
330 case 'delete-interfaces' : {
331 $interface_ids=$_POST['interface_ids'];
332 if ( ! $interface_ids) {
333 drupal_set_message("action=$action - No interface selected");
338 foreach( $interface_ids as $interface_id ) {
339 if ($api->DeleteInterface( intval( $interface_id )) != 1)
345 drupal_set_message ("Deleted $counter interface(s)");
347 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
348 plc_redirect(l_node($_POST['node_id']));
351 case 'add-interface': {
352 $node_id=$_POST['node_id'];
353 foreach ($interface_details as $field) {
354 $interface[$field]= $_POST[$field];
355 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
356 $interface[$field]= intval( $interface[$field] );
359 $result=$api->AddInterface( intval( $node_id ), $interface );
361 drupal_set_message ("Interface $result added into node $node_id");
363 drupal_set_error ("Could not create interface");
364 plc_redirect (l_node($node_id));
367 case 'update-interface': {
368 $interface_id=$_POST['interface_id'];
369 foreach ($interface_details as $field) {
370 $interface[$field]= $_POST[$field];
371 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
372 $interface[$field]= intval( $interface[$field] );
375 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
377 drupal_set_message ("Interface $interface_id updated");
379 drupal_set_error ("Could not update interface");
380 plc_redirect (l_interface($interface_id));
383 //////////////////////////////////////////////////////////// sites
384 case 'delete-site': {
385 $site_id = intval($_POST['site_id']);
386 if ($api->DeleteSite($site_id) ==1)
387 drupal_set_message ("Site $site_id deleted");
389 drupal_set_error("Failed to delete site $site_id");
390 plc_redirect (l_sites());
394 case 'expire-all-slices-in-site': {
396 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
399 //// old code from sites/expire.php
400 $sites = $api->GetSites( array( intval( $site_id )));
402 // xxx why not 'now?'
403 $expiration= strtotime( $_POST['expires'] );
404 // loop through all slices for site
405 foreach ($site['slice_ids'] as $slice_id) {
406 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
408 // update site to not allow slice creation or renewal
409 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
410 plc_redirect (l_site($site_id));
414 case 'update-site': {
415 $site_id=intval($_POST['site_id']);
416 $name= $_POST['name'];
417 $abbreviated_name= $_POST['abbreviated_name'];
419 $latitude= floatval($_POST['latitude']);
420 $longitude= floatval($_POST['longitude']);
421 //$max_slivers= $_POST['max_slivers'];
423 $fields= array( "name" => $name,
424 "abbreviated_name" => $abbreviated_name,
426 "latitude" => floatval( $latitude ),
427 "longitude" => floatval( $longitude ));
429 if ($_POST['login_base'])
430 $fields['login_base'] = $_POST['login_base'];
431 if ($_POST['max_slices'])
432 $fields['max_slices'] = intval($_POST['max_slices']);
434 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
436 drupal_set_message("Site $name updated");
438 drupal_set_error ("Could not update site $site_id");
440 plc_redirect(l_site($site_id));
444 //////////////////////////////////////////////////////////// slices
445 case 'delete-slice': {
446 $slice_id = $_POST['slice_id'];
447 $api->DeleteSlice( intval( $slice_id ) );
448 $error= $api->error();
449 if( empty( $error ) ) {
450 drupal_set_message("Deleted slice $slice_id");
452 drupal_set_error($error);
457 case 'update-slice': {
458 $slice_id = $_POST['slice_id'];
459 $name = $_POST['name'];
460 $description= $_POST['description'];
463 $fields= array( "description"=>$description, "url"=>$url );
464 $api->UpdateSlice( intval( $slice_id ), $fields );
465 $error= $api->error();
467 if( empty( $error ) ) {
468 drupal_set_message("Update slice $name");
469 plc_redirect(l_slice($slice_id));
471 drupal_set_error($error);
476 case 'renew-slice': {
477 $slice_id = intval ($_POST['slice_id']);
478 $expires = intval ($_POST['expires']);
484 $MAX_FUTURE=$WEEKS*$WEEK;
485 if ( ($expires-$now) > $MAX_FUTURE) {
486 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
487 plc_redirect(l_slice($slice_id));
489 plc_debug('slice_id',$slice_id);
490 plc_debug('expires',$expires);
491 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
492 drupal_set_message("Slice renewed");
494 drupal_set_error("Could not update slice $slice_id");
495 plc_redirect(l_slice($slice_id));
499 case 'remove-persons-from-slice': {
500 $slice_id = intval ($_POST['slice_id']);
501 $person_ids = $_POST['person_ids'];
505 foreach( $person_ids as $person_id ) {
506 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
512 drupal_set_message ("Deleted $counter person(s)");
514 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
515 plc_redirect(l_slice($slice_id) . " &show_persons=true");
519 case 'add-persons-in-slice': {
520 $slice_id = intval ($_POST['slice_id']);
521 $person_ids = $_POST['person_ids'];
525 foreach ($person_ids as $person_id) {
526 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
532 drupal_set_message ("Added $counter person(s)");
534 drupal_set_error ("Could not add all selected persons, only $counter were added");
535 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
539 case 'remove-nodes-from-slice': {
540 $slice_id = intval ($_POST['slice_id']);
541 $node_ids = array_map("intval",$_POST['node_ids']);
542 $count=count($node_ids);
544 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
545 drupal_set_message ("Removed $count node(s)");
547 drupal_set_error ("Could not remove selected nodes");
548 plc_redirect(l_slice($slice_id) . " &show_nodes=true");
552 case 'add-nodes-in-slice': {
553 $slice_id = intval ($_POST['slice_id']);
554 $node_ids = array_map("intval",$_POST['node_ids']);
555 $count=count($node_ids);
556 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
557 drupal_set_message ("Added $count node(s)");
559 drupal_set_error ("Could not add all selected nodes");
560 plc_redirect(l_slice($slice_id) . "&show_nodes=true" );
564 //////////////////////////////////////////////////////////// tag types
566 case 'update-tag-type': {
568 $tag_type_id= intval( $_POST['tag_type_id'] );
569 $tagname = $_POST['tagname'];
570 $min_role_id= intval( $_POST['min_role_id'] );
571 $description= $_POST['description'];
572 $category= $_POST['category'];
574 // make tag_type_fields dict
575 $tag_type_fields= array( "min_role_id" => $min_role_id,
576 "tagname" => $tagname,
577 "description" => $description,
578 "category" => $category,
581 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
582 drupal_set_message ("Tag type $tagname updated");
584 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
585 plc_redirect(l_tag($tag_type_id));
588 case 'add-tag-type': {
590 $tagname = $_POST['tagname'];
591 $min_role_id= intval( $_POST['min_role_id'] );
592 $description= $_POST['description'];
593 $category= $_POST['category'];
595 // make tag_type_fields dict
596 $tag_type_fields= array( "min_role_id" => $min_role_id,
597 "tagname" => $tagname,
598 "description" => $description,
599 "category" => $category,
603 $tag_type_id=$api->AddTagType( $tag_type_fields );
604 if ($tag_type_id > 0)
605 drupal_set_message ("tag type $tag_type_id created");
607 drupal_set_error ("Could not create tag type $tagname");
608 plc_redirect( l_tags());
611 case 'delete-tag-types': {
612 $tag_type_ids = $_POST['tag_type_ids'];
613 if ( ! $tag_type_ids) {
614 drupal_set_message("action=$action - No tag selected");
619 foreach ($tag_type_ids as $tag_type_id)
620 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
625 drupal_set_message ("Deleted $counter tag(s)");
627 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
628 plc_redirect (l_tags());
632 //////////////////////////////////////// tags
633 case 'set-tag-on-node':
634 case 'set-tag-on-interface': {
637 if ($action == 'set-tag-on-node') $node_mode=true;
640 $node_id = intval($_POST['node_id']);
642 $interface_id=intval($_POST['interface_id']);
643 $tag_type_id = intval($_POST['tag_type_id']);
644 $value = $_POST['value'];
646 $tag_types=$api->GetTagTypes(array($tag_type_id));
647 if (count ($tag_types) != 1) {
648 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
651 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
653 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
654 if ( count ($tags) == 1) {
657 $tag_id=$tag['node_tag_id'];
658 $result=$api->UpdateNodeTag($tag_id,$value);
660 $tag_id=$tag['interface_tag_id'];
661 $result=$api->UpdateInterfaceTag($tag_id,$value);
664 drupal_set_message ("Updated tag, new value = $value");
666 drupal_set_error ("Could not update tag");
669 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
671 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
673 drupal_set_message ("Created tag, new value = $value");
675 drupal_set_error ("Could not create tag");
680 plc_redirect (l_node($node_id));
682 plc_redirect (l_interface($interface_id));
685 case 'delete-node-tags' :
686 case 'delete-interface-tags' : {
689 if ($action == 'delete-node-tags') $node_mode=true;
692 $tag_ids=$_POST['node_tag_ids'];
694 $tag_ids=$_POST['interface_tag_ids'];
697 drupal_set_message("action=$action - No tag selected");
702 foreach( $tag_ids as $tag_id ) {
704 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
706 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
713 drupal_set_message ("Deleted $counter tag(s)");
715 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
717 plc_redirect(l_node($_POST['node_id']));
719 plc_redirect(l_interface($_POST['interface_id']));
723 ////////////////////////////////////////
726 plc_debug('GET',$_GET);
727 plc_debug('POST',$_POST);
728 plc_debug('FILES',$_FILES);
733 plc_error ("Unknown action $action in actions.php");