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
84 $known_actions []= 'delete-slice-tags';
85 // expects: slice_tag_id
86 $known_actions []= 'add-slice-tag';
87 // expects: slice_id & tag_type_id & node_id & nodegroup_id
89 //////////////////////////////////////// tag types
90 $known_actions []= "update-tag-type";
91 // expects: tag_type_id & name & description & category & min_role_id
92 $known_actions []= "add-tag-type";
93 // expects: tag_type_id & tagname & description & category & min_role_id
94 $known_actions []= "delete-tag-types";
95 // expects: tag_type_ids
97 //////////////////////////////////////// tags
98 $known_actions []= "set-tag-on-node";
99 // expects: node_id tagname value
100 $known_actions []= "set-tag-on-interface";
101 // expects: interface_id tagname value
102 $known_actions []= "delete-node-tags";
103 // expects: node_id & node_tag_ids
104 $known_actions []= "delete-interface-tags";
105 // expects: interface_id & interface_tag_ids
107 ////////////////////////////////////////////////////////////
108 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
109 'broadcast', 'netmask', 'dns1', 'dns2',
110 'hostname', 'mac', 'bwlimit' );
112 //////////////////////////////
113 // sometimes we don't set 'action', but use the submit button name instead
114 // so if 'action' not set, see if $_POST has one of the actions as a key
115 if ($_POST['action'])
116 $action=$_POST['action'];
118 foreach ($known_actions as $known_action)
119 if ($_POST[$known_action]) {
120 $action=$known_action;
124 //uncomment for debugging incoming data
127 $person_id = $_POST['person_id']; // usually needed
130 drupal_set_message ("actions.php: action not set or not in known_actions");
131 plc_debug('POST',$_POST);
137 case 'add-person-to-site': {
138 $site_id = $_POST['site_id'];
139 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
140 plc_redirect (l_person($person_id));
143 case 'remove-person-from-sites': {
144 $site_ids = $_POST['site_ids'];
146 drupal_set_message("action=$action - No site selected");
149 foreach ( $site_ids as $site_id ) {
150 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
152 plc_redirect (l_person($person_id));
155 case 'remove-roles-from-person' : {
156 $role_ids=$_POST['role_ids'];
158 drupal_set_message("action=$action - No role selected");
161 foreach( $role_ids as $role_id) {
162 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
164 plc_redirect (l_person($person_id));
167 case 'add-role-to-person' : {
168 $role_id=$_POST['role_id'];
169 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
170 plc_redirect (l_person($person_id));
173 case 'enable-person' : {
174 $fields = array( "enabled"=>true );
175 $api->UpdatePerson( intval( $person_id ), $fields );
176 plc_redirect (l_person($person_id));
179 case 'disable-person' : {
180 $fields = array( "enabled"=>false );
181 $api->UpdatePerson( intval( $person_id ), $fields );
182 plc_redirect (l_person($person_id));
185 case 'become-person' : {
186 $plc->BecomePerson (intval($person_id));
187 plc_redirect (l_persons());
190 case 'delete-person' : {
191 $api->DeletePerson( intval( $person_id ) );
192 plc_redirect (l_persons());
195 case 'delete-keys' : {
196 $key_ids=$_POST['key_ids'];
198 drupal_set_message("action=$action - No key selected");
203 foreach( $key_ids as $key_id ) {
204 if ($api->DeleteKey( intval( $key_id )) != 1)
210 drupal_set_message ("Deleted $counter key(s)");
212 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
213 plc_redirect(l_person($person_id));
217 case 'upload-key' : {
218 if ( ! isset( $_FILES['key'] ) ) {
219 drupal_set_message ("action=$action, no key file set");
223 $key_file= $_FILES['key']['tmp_name'];
225 plc_error("Please select a valid SSH key file to upload");
228 $fp = fopen( $key_file, "r" );
231 plc_error("Unable to open key file $key_file");
234 // opened the key file, read the one line of contents
235 // The POST operation always creates a file even if the filename
236 // the user specified was garbage. If there was some problem
237 // with the source file, we'll get a zero length read here.
238 $key = fread($fp, filesize($key_file));
241 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
244 drupal_set_message ("New key added");
246 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
248 plc_redirect(l_person($person_id));
251 case 'update-person': {
252 $person_id=$_POST['person_id'];
253 // attempt to update this person
254 $first_name= $_POST['first_name'];
255 $last_name= $_POST['last_name'];
256 $title= $_POST['title'];
257 $email= $_POST['email'];
258 $phone= $_POST['phone'];
260 $bio= str_replace("\r", "", $_POST['bio']);
261 $password1= $_POST['password1'];
262 $password2= $_POST['password2'];
264 if( $password1 != $password2 ) {
265 drupal_set_error ("The passwords do not match");
266 plc_redirect(l_person($person_id));
270 $fields['first_name']= $first_name;
271 $fields['last_name']= $last_name;
272 $fields['title']= $title;
273 $fields['email']= $email;
274 $fields['phone']= $phone;
275 $fields['url']= $url;
276 $fields['bio']= $bio;
278 if ( $password1 != "" )
279 $fields['password']= $password1;
281 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
282 drupal_set_message("$first_name $last_name updated");
284 drupal_set_error ("Could not update person $person_id" . $api->error());
286 plc_redirect(l_person($person_id));
290 //////////////////////////////////////////////////////////// nodes
291 case 'node-boot-state': {
292 $node_id=intval($_POST['node_id']);
293 $boot_state=$_POST['boot_state'];
294 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
296 drupal_set_message("boot state updated");
297 plc_redirect (l_node($node_id));
299 drupal_set_error("Could not set boot_state '$boot_state'");
304 case 'delete-node': {
305 $node_id=intval($_POST['node_id']);
306 $result=$api->DeleteNode( intval( $node_id ) );
308 drupal_set_message("Node $node_id deleted");
309 plc_redirect (l_nodes());
311 drupal_set_error ("Could not delete node $node_id");
316 case 'update-node': {
317 $hostname= $_POST['hostname'];
318 $model= $_POST['model'];
320 $fields= array( "hostname"=>$hostname, "model"=>$model );
321 $api->UpdateNode( intval( $node_id ), $fields );
322 $error= $api->error();
324 if( empty( $error ) ) {
325 drupal_set_message("Update node $hostname");
326 plc_redirect(l_node($node_id));
328 drupal_set_error($error);
333 //////////////////////////////////////////////////////////// interfaces
334 case 'delete-interfaces' : {
335 $interface_ids=$_POST['interface_ids'];
336 if ( ! $interface_ids) {
337 drupal_set_message("action=$action - No interface selected");
342 foreach( $interface_ids as $interface_id ) {
343 if ($api->DeleteInterface( intval( $interface_id )) != 1)
349 drupal_set_message ("Deleted $counter interface(s)");
351 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
352 plc_redirect(l_node($_POST['node_id']));
355 case 'add-interface': {
356 $node_id=$_POST['node_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->AddInterface( intval( $node_id ), $interface );
365 drupal_set_message ("Interface $result added into node $node_id");
367 drupal_set_error ("Could not create interface");
368 plc_redirect (l_node($node_id));
371 case 'update-interface': {
372 $interface_id=$_POST['interface_id'];
373 foreach ($interface_details as $field) {
374 $interface[$field]= $_POST[$field];
375 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
376 $interface[$field]= intval( $interface[$field] );
379 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
381 drupal_set_message ("Interface $interface_id updated");
383 drupal_set_error ("Could not update interface");
384 plc_redirect (l_interface($interface_id));
387 //////////////////////////////////////////////////////////// sites
388 case 'delete-site': {
389 $site_id = intval($_POST['site_id']);
390 if ($api->DeleteSite($site_id) ==1)
391 drupal_set_message ("Site $site_id deleted");
393 drupal_set_error("Failed to delete site $site_id");
394 plc_redirect (l_sites());
398 case 'expire-all-slices-in-site': {
400 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
403 //// old code from sites/expire.php
404 $sites = $api->GetSites( array( intval( $site_id )));
406 // xxx why not 'now?'
407 $expiration= strtotime( $_POST['expires'] );
408 // loop through all slices for site
409 foreach ($site['slice_ids'] as $slice_id) {
410 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
412 // update site to not allow slice creation or renewal
413 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
414 plc_redirect (l_site($site_id));
418 case 'update-site': {
419 $site_id=intval($_POST['site_id']);
420 $name= $_POST['name'];
421 $abbreviated_name= $_POST['abbreviated_name'];
423 $latitude= floatval($_POST['latitude']);
424 $longitude= floatval($_POST['longitude']);
425 //$max_slivers= $_POST['max_slivers'];
427 $fields= array( "name" => $name,
428 "abbreviated_name" => $abbreviated_name,
430 "latitude" => floatval( $latitude ),
431 "longitude" => floatval( $longitude ));
433 if ($_POST['login_base'])
434 $fields['login_base'] = $_POST['login_base'];
435 if ($_POST['max_slices'])
436 $fields['max_slices'] = intval($_POST['max_slices']);
438 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
440 drupal_set_message("Site $name updated");
442 drupal_set_error ("Could not update site $site_id");
444 plc_redirect(l_site($site_id));
448 //////////////////////////////////////////////////////////// slices
449 case 'delete-slice': {
450 $slice_id = $_POST['slice_id'];
451 if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
452 drupal_set_message("Slice $slice_id deleted");
453 plc_redirect(l_slices());
455 drupal_set_error("Could not delete slice $slice_id " . $api->error());
460 case 'update-slice': {
461 $slice_id = $_POST['slice_id'];
462 $name = $_POST['name'];
463 $description= $_POST['description'];
466 $fields= array( "description"=>$description, "url"=>$url );
467 $api->UpdateSlice( intval( $slice_id ), $fields );
468 $error= $api->error();
470 if( empty( $error ) ) {
471 drupal_set_message("Update slice $name");
472 plc_redirect(l_slice($slice_id));
474 drupal_set_error($error);
479 case 'renew-slice': {
480 $slice_id = intval ($_POST['slice_id']);
481 $expires = intval ($_POST['expires']);
487 $MAX_FUTURE=$WEEKS*$WEEK;
488 if ( ($expires-$now) > $MAX_FUTURE) {
489 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
490 plc_redirect(l_slice($slice_id));
492 plc_debug('slice_id',$slice_id);
493 plc_debug('expires',$expires);
494 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
495 drupal_set_message("Slice renewed");
497 drupal_set_error("Could not update slice $slice_id");
498 plc_redirect(l_slice($slice_id));
502 case 'remove-persons-from-slice': {
503 $slice_id = intval ($_POST['slice_id']);
504 $person_ids = $_POST['person_ids'];
508 foreach( $person_ids as $person_id ) {
509 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
515 drupal_set_message ("Deleted $counter person(s)");
517 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
518 plc_redirect(l_slice($slice_id) . " &show_persons=true");
522 case 'add-persons-in-slice': {
523 $slice_id = intval ($_POST['slice_id']);
524 $person_ids = $_POST['person_ids'];
528 foreach ($person_ids as $person_id) {
529 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
535 drupal_set_message ("Added $counter person(s)");
537 drupal_set_error ("Could not add all selected persons, only $counter were added");
538 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
542 case 'remove-nodes-from-slice': {
543 $slice_id = intval ($_POST['slice_id']);
544 $node_ids = array_map("intval",$_POST['node_ids']);
545 $count=count($node_ids);
547 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
548 drupal_set_message ("Removed $count node(s)");
550 drupal_set_error ("Could not remove selected nodes");
551 plc_redirect(l_slice($slice_id) . " &show_nodes=true");
555 case 'add-nodes-in-slice': {
556 $slice_id = intval ($_POST['slice_id']);
557 $node_ids = array_map("intval",$_POST['node_ids']);
558 $count=count($node_ids);
559 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
560 drupal_set_message ("Added $count node(s)");
562 drupal_set_error ("Could not add all selected nodes");
563 plc_redirect(l_slice($slice_id) . "&show_nodes=true" );
567 case 'delete-slice-tags': {
568 $slice_id = intval($_POST['slice_id']);
569 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
572 foreach($slice_tag_ids as $slice_tag_id) {
573 if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
575 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
580 drupal_set_message ("Deleted $count slice tag(s)");
581 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
585 case 'add-slice-tag': {
586 $slice_id = intval($_POST['slice_id']);
587 $tag_type_id = intval($_POST['tag_type_id']);
588 $value = $_POST['value'];
589 $node_id = intval($_POST['node_id']);
590 $nodegroup_id = intval($_POST['nodegroup_id']);
594 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
595 } elseif ($nodegroup_id) {
596 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
598 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
601 drupal_set_message ("Added slice tag.");
603 drupal_set_error("Could not add slice tag");
604 if ($_POST['sliver_action'])
605 plc_redirect(l_sliver($node_id,$slice_id));
607 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
611 //////////////////////////////////////////////////////////// tag types
613 case 'update-tag-type': {
615 $tag_type_id= intval( $_POST['tag_type_id'] );
616 $tagname = $_POST['tagname'];
617 $min_role_id= intval( $_POST['min_role_id'] );
618 $description= $_POST['description'];
619 $category= $_POST['category'];
621 // make tag_type_fields dict
622 $tag_type_fields= array( "min_role_id" => $min_role_id,
623 "tagname" => $tagname,
624 "description" => $description,
625 "category" => $category,
628 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
629 drupal_set_message ("Tag type $tagname updated");
631 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
632 plc_redirect(l_tag($tag_type_id));
635 case 'add-tag-type': {
637 $tagname = $_POST['tagname'];
638 $min_role_id= intval( $_POST['min_role_id'] );
639 $description= $_POST['description'];
640 $category= $_POST['category'];
642 // make tag_type_fields dict
643 $tag_type_fields= array( "min_role_id" => $min_role_id,
644 "tagname" => $tagname,
645 "description" => $description,
646 "category" => $category,
650 $tag_type_id=$api->AddTagType( $tag_type_fields );
651 if ($tag_type_id > 0)
652 drupal_set_message ("tag type $tag_type_id created");
654 drupal_set_error ("Could not create tag type $tagname");
655 plc_redirect( l_tags());
658 case 'delete-tag-types': {
659 $tag_type_ids = $_POST['tag_type_ids'];
660 if ( ! $tag_type_ids) {
661 drupal_set_message("action=$action - No tag selected");
666 foreach ($tag_type_ids as $tag_type_id)
667 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
672 drupal_set_message ("Deleted $counter tag(s)");
674 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
675 plc_redirect (l_tags());
679 //////////////////////////////////////// tags
680 case 'set-tag-on-node':
681 case 'set-tag-on-interface': {
684 if ($action == 'set-tag-on-node') $node_mode=true;
687 $node_id = intval($_POST['node_id']);
689 $interface_id=intval($_POST['interface_id']);
690 $tag_type_id = intval($_POST['tag_type_id']);
691 $value = $_POST['value'];
693 $tag_types=$api->GetTagTypes(array($tag_type_id));
694 if (count ($tag_types) != 1) {
695 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
698 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
700 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
701 if ( count ($tags) == 1) {
704 $tag_id=$tag['node_tag_id'];
705 $result=$api->UpdateNodeTag($tag_id,$value);
707 $tag_id=$tag['interface_tag_id'];
708 $result=$api->UpdateInterfaceTag($tag_id,$value);
711 drupal_set_message ("Updated tag, new value = $value");
713 drupal_set_error ("Could not update tag");
716 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
718 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
720 drupal_set_message ("Created tag, new value = $value");
722 drupal_set_error ("Could not create tag");
727 plc_redirect (l_node($node_id));
729 plc_redirect (l_interface($interface_id));
732 case 'delete-node-tags' :
733 case 'delete-interface-tags' : {
736 if ($action == 'delete-node-tags') $node_mode=true;
739 $tag_ids=$_POST['node_tag_ids'];
741 $tag_ids=$_POST['interface_tag_ids'];
744 drupal_set_message("action=$action - No tag selected");
749 foreach( $tag_ids as $tag_id ) {
751 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
753 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
760 drupal_set_message ("Deleted $counter tag(s)");
762 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
764 plc_redirect(l_node($_POST['node_id']));
766 plc_redirect(l_interface($_POST['interface_id']));
770 ////////////////////////////////////////
773 plc_debug('GET',$_GET);
774 plc_debug('POST',$_POST);
775 plc_debug('FILES',$_FILES);
780 plc_error ("Unknown action $action in actions.php");