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 $known_actions []= "attach-pcu";
53 // expects: node_id, pcu_id, port (pcu_id <0 means detach)
55 //////////////////////////////////////// interfaces
56 $known_actions []= "delete-interfaces";
57 // expects: interface_ids
58 $known_actions []="add-interface";
59 // expects: node_id & interface details
60 $known_actions []="update-interface";
61 // expects: interface_id & interface details
63 //////////////////////////////////////// sites
64 $known_actions []= "delete-site";
66 $known_actions []= "expire-all-slices-in-site";
68 $known_actions []= "update-site";
69 // expects: site_id & name abbreviated_name url latitude longitude [login_base max_slices]
71 //////////////////////////////////////// slices
72 $known_actions []= "delete-slice";
74 $known_actions []= "update-slice";
75 // expects: slice_id, name, description, url
76 $known_actions []= "renew-slice";
77 // expects: slice_id & expires
78 $known_actions []= 'remove-persons-from-slice';
79 // expects: slice_id & person_ids
80 $known_actions []= 'add-persons-in-slice';
81 // expects: slice_id & person_ids
82 $known_actions []= 'remove-nodes-from-slice';
83 // expects: slice_id & node_ids
84 $known_actions []= 'add-nodes-in-slice';
85 // expects: slice_id & node_ids
86 $known_actions []= 'delete-slice-tags';
87 // expects: slice_tag_id
88 $known_actions []= 'add-slice-tag';
89 // expects: slice_id & tag_type_id & node_id & nodegroup_id
91 //////////////////////////////////////// tag types
92 $known_actions []= "update-tag-type";
93 // expects: tag_type_id & name & description & category & min_role_id
94 $known_actions []= "add-tag-type";
95 // expects: tag_type_id & tagname & description & category & min_role_id
96 $known_actions []= "delete-tag-types";
97 // expects: tag_type_ids
99 //////////////////////////////////////// tags
100 $known_actions []= "set-tag-on-node";
101 // expects: node_id tagname value
102 $known_actions []= "set-tag-on-interface";
103 // expects: interface_id tagname value
104 $known_actions []= "delete-node-tags";
105 // expects: node_id & node_tag_ids
106 $known_actions []= "delete-interface-tags";
107 // expects: interface_id & interface_tag_ids
109 ////////////////////////////////////////////////////////////
110 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
111 'broadcast', 'netmask', 'dns1', 'dns2',
112 'hostname', 'mac', 'bwlimit' );
114 //////////////////////////////
115 // sometimes we don't set 'action', but use the submit button name instead
116 // so if 'action' not set, see if $_POST has one of the actions as a key
117 if ($_POST['action'])
118 $action=$_POST['action'];
120 foreach ($known_actions as $known_action)
121 if ($_POST[$known_action]) {
122 $action=$known_action;
126 //uncomment for debugging incoming data
129 $person_id = $_POST['person_id']; // usually needed
132 drupal_set_message ("actions.php: action not set or not in known_actions");
133 plc_debug('POST',$_POST);
139 case 'add-person-to-site': {
140 $site_id = $_POST['site_id'];
141 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
142 plc_redirect (l_person($person_id));
145 case 'remove-person-from-sites': {
146 $site_ids = $_POST['site_ids'];
148 drupal_set_message("action=$action - No site selected");
151 foreach ( $site_ids as $site_id ) {
152 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
154 plc_redirect (l_person($person_id));
157 case 'remove-roles-from-person' : {
158 $role_ids=$_POST['role_ids'];
160 drupal_set_message("action=$action - No role selected");
163 foreach( $role_ids as $role_id) {
164 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
166 plc_redirect (l_person($person_id));
169 case 'add-role-to-person' : {
170 $role_id=$_POST['role_id'];
171 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
172 plc_redirect (l_person($person_id));
175 case 'enable-person' : {
176 $fields = array( "enabled"=>true );
177 $api->UpdatePerson( intval( $person_id ), $fields );
178 plc_redirect (l_person($person_id));
181 case 'disable-person' : {
182 $fields = array( "enabled"=>false );
183 $api->UpdatePerson( intval( $person_id ), $fields );
184 plc_redirect (l_person($person_id));
187 case 'become-person' : {
188 $plc->BecomePerson (intval($person_id));
189 plc_redirect (l_person(intval($person_id)));
192 case 'delete-person' : {
193 $api->DeletePerson( intval( $person_id ) );
194 plc_redirect (l_persons());
197 case 'delete-keys' : {
198 $key_ids=$_POST['key_ids'];
200 drupal_set_message("action=$action - No key selected");
205 foreach( $key_ids as $key_id ) {
206 if ($api->DeleteKey( intval( $key_id )) != 1)
212 drupal_set_message ("Deleted $counter key(s)");
214 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
215 plc_redirect(l_person($person_id));
219 case 'upload-key' : {
220 if ( ! isset( $_FILES['key'] ) ) {
221 drupal_set_message ("action=$action, no key file set");
225 $key_file= $_FILES['key']['tmp_name'];
227 plc_error("Please select a valid SSH key file to upload");
230 $fp = fopen( $key_file, "r" );
233 plc_error("Unable to open key file $key_file");
236 // opened the key file, read the one line of contents
237 // The POST operation always creates a file even if the filename
238 // the user specified was garbage. If there was some problem
239 // with the source file, we'll get a zero length read here.
240 $key = fread($fp, filesize($key_file));
243 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
246 drupal_set_message ("New key added");
248 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
250 plc_redirect(l_person($person_id));
253 case 'update-person': {
254 $person_id=$_POST['person_id'];
255 // attempt to update this person
256 $first_name= $_POST['first_name'];
257 $last_name= $_POST['last_name'];
258 $title= $_POST['title'];
259 $email= $_POST['email'];
260 $phone= $_POST['phone'];
262 $bio= str_replace("\r", "", $_POST['bio']);
263 $password1= $_POST['password1'];
264 $password2= $_POST['password2'];
266 if( $password1 != $password2 ) {
267 drupal_set_error ("The passwords do not match");
268 plc_redirect(l_person($person_id));
272 $fields['first_name']= $first_name;
273 $fields['last_name']= $last_name;
274 $fields['title']= $title;
275 $fields['email']= $email;
276 $fields['phone']= $phone;
277 $fields['url']= $url;
278 $fields['bio']= $bio;
280 if ( $password1 != "" )
281 $fields['password']= $password1;
283 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
284 drupal_set_message("$first_name $last_name updated");
286 drupal_set_error ("Could not update person $person_id" . $api->error());
288 plc_redirect(l_person($person_id));
292 //////////////////////////////////////////////////////////// nodes
293 case 'node-boot-state': {
294 $node_id=intval($_POST['node_id']);
295 $boot_state=$_POST['boot_state'];
296 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
298 drupal_set_message("boot state updated");
299 plc_redirect (l_node($node_id));
301 drupal_set_error("Could not set boot_state '$boot_state'");
306 case 'delete-node': {
307 $node_id=intval($_POST['node_id']);
308 $result=$api->DeleteNode( intval( $node_id ) );
310 drupal_set_message("Node $node_id deleted");
311 plc_redirect (l_nodes());
313 drupal_set_error ("Could not delete node $node_id");
318 case 'update-node': {
319 $node_id=intval($_POST['node_id']);
320 $hostname= $_POST['hostname'];
321 $model= $_POST['model'];
323 $fields= array( "hostname"=>$hostname, "model"=>$model );
324 $api->UpdateNode( $node_id, $fields );
325 $error= $api->error();
327 if( empty( $error ) ) {
328 drupal_set_message("Update node $hostname");
329 plc_redirect(l_node($node_id));
331 drupal_set_error($error);
336 // this code will ensure that at most one PCU gets attached to the node
338 $node_id=intval($_POST['node_id']);
339 $pcu_id=intval($_POST['pcu_id']);
340 $port=intval($_POST['port']);
341 // always start with deleting former PCUs
342 $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
343 $former_pcu_ids = $nodes[0]['pcu_ids'];
344 if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
345 if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1)
346 drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
348 drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
350 // re-attach only if provided pcu_id >=0
352 if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
353 drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
355 drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
357 drupal_set_message ('Detached node from all PCUs');
360 plc_redirect(l_node($node_id));
365 //////////////////////////////////////////////////////////// interfaces
366 case 'delete-interfaces' : {
367 $interface_ids=$_POST['interface_ids'];
368 if ( ! $interface_ids) {
369 drupal_set_message("action=$action - No interface selected");
374 foreach( $interface_ids as $interface_id ) {
375 if ($api->DeleteInterface( intval( $interface_id )) != 1)
381 drupal_set_message ("Deleted $counter interface(s)");
383 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
384 plc_redirect(l_node($_POST['node_id']));
387 case 'add-interface': {
388 $node_id=$_POST['node_id'];
389 foreach ($interface_details as $field) {
390 $interface[$field]= $_POST[$field];
391 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
392 $interface[$field]= intval( $interface[$field] );
395 $result=$api->AddInterface( intval( $node_id ), $interface );
397 drupal_set_message ("Interface $result added into node $node_id");
399 drupal_set_error ("Could not create interface");
400 plc_redirect (l_node($node_id));
403 case 'update-interface': {
404 $interface_id=$_POST['interface_id'];
405 foreach ($interface_details as $field) {
406 $interface[$field]= $_POST[$field];
407 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
408 $interface[$field]= intval( $interface[$field] );
411 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
413 drupal_set_message ("Interface $interface_id updated");
415 drupal_set_error ("Could not update interface");
416 plc_redirect (l_interface($interface_id));
419 //////////////////////////////////////////////////////////// sites
420 case 'delete-site': {
421 $site_id = intval($_POST['site_id']);
422 if ($api->DeleteSite($site_id) ==1)
423 drupal_set_message ("Site $site_id deleted");
425 drupal_set_error("Failed to delete site $site_id");
426 plc_redirect (l_sites());
430 case 'expire-all-slices-in-site': {
432 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
435 //// old code from sites/expire.php
436 $sites = $api->GetSites( array( intval( $site_id )));
438 // xxx why not 'now?'
439 $expiration= strtotime( $_POST['expires'] );
440 // loop through all slices for site
441 foreach ($site['slice_ids'] as $slice_id) {
442 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
444 // update site to not allow slice creation or renewal
445 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
446 plc_redirect (l_site($site_id));
450 case 'update-site': {
451 $site_id=intval($_POST['site_id']);
452 $name= $_POST['name'];
453 $abbreviated_name= $_POST['abbreviated_name'];
455 $latitude= floatval($_POST['latitude']);
456 $longitude= floatval($_POST['longitude']);
457 //$max_slivers= $_POST['max_slivers'];
459 $fields= array( "name" => $name,
460 "abbreviated_name" => $abbreviated_name,
462 "latitude" => floatval( $latitude ),
463 "longitude" => floatval( $longitude ));
465 if ($_POST['login_base'])
466 $fields['login_base'] = $_POST['login_base'];
467 if ($_POST['max_slices'])
468 $fields['max_slices'] = intval($_POST['max_slices']);
469 if (isset($_POST['enabled'])) {
470 $fields['enabled'] = (bool)$_POST['enabled'];
473 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
475 drupal_set_message("Site $name updated");
477 drupal_set_error ("Could not update site $site_id");
479 plc_redirect(l_site($site_id));
483 //////////////////////////////////////////////////////////// slices
484 case 'delete-slice': {
485 $slice_id = $_POST['slice_id'];
486 if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
487 drupal_set_message("Slice $slice_id deleted");
488 plc_redirect(l_slices());
490 drupal_set_error("Could not delete slice $slice_id " . $api->error());
495 case 'update-slice': {
496 $slice_id = $_POST['slice_id'];
497 $name = $_POST['name'];
498 $description= $_POST['description'];
501 $fields= array( "description"=>$description, "url"=>$url );
502 $api->UpdateSlice( intval( $slice_id ), $fields );
503 $error= $api->error();
505 if( empty( $error ) ) {
506 drupal_set_message("Update slice $name");
507 plc_redirect(l_slice($slice_id));
509 drupal_set_error($error);
514 case 'renew-slice': {
515 $slice_id = intval ($_POST['slice_id']);
516 $expires = intval ($_POST['expires']);
522 $MAX_FUTURE=$WEEKS*$WEEK;
523 if ( ($expires-$now) > $MAX_FUTURE) {
524 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
525 plc_redirect(l_slice($slice_id));
527 plc_debug('slice_id',$slice_id);
528 plc_debug('expires',$expires);
529 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
530 drupal_set_message("Slice renewed");
532 drupal_set_error("Could not update slice $slice_id");
533 plc_redirect(l_slice($slice_id));
537 case 'remove-persons-from-slice': {
538 $slice_id = intval ($_POST['slice_id']);
539 $person_ids = $_POST['person_ids'];
543 foreach( $person_ids as $person_id ) {
544 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
550 drupal_set_message ("Deleted $counter person(s)");
552 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
553 plc_redirect(l_slice($slice_id) . " &show_persons=true");
557 case 'add-persons-in-slice': {
558 $slice_id = intval ($_POST['slice_id']);
559 $person_ids = $_POST['person_ids'];
563 foreach ($person_ids as $person_id) {
564 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
570 drupal_set_message ("Added $counter person(s)");
572 drupal_set_error ("Could not add all selected persons, only $counter were added");
573 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
577 case 'remove-nodes-from-slice': {
578 $slice_id = intval ($_POST['slice_id']);
579 $node_ids = array_map("intval",$_POST['node_ids']);
580 $count=count($node_ids);
582 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
583 drupal_set_message ("Removed $count node(s)");
585 drupal_set_error ("Could not remove selected nodes");
586 plc_redirect(l_slice($slice_id) . " &show_nodes=true");
590 case 'add-nodes-in-slice': {
591 $slice_id = intval ($_POST['slice_id']);
592 $node_ids = array_map("intval",$_POST['node_ids']);
593 $count=count($node_ids);
594 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
595 drupal_set_message ("Added $count node(s)");
597 drupal_set_error ("Could not add all selected nodes");
598 plc_redirect(l_slice($slice_id) . "&show_nodes=true" );
602 case 'delete-slice-tags': {
603 $slice_id = intval($_POST['slice_id']);
604 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
607 foreach($slice_tag_ids as $slice_tag_id) {
608 if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
610 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
615 drupal_set_message ("Deleted $count slice tag(s)");
616 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
620 case 'add-slice-tag': {
621 $slice_id = intval($_POST['slice_id']);
622 $tag_type_id = intval($_POST['tag_type_id']);
623 $value = $_POST['value'];
624 $node_id = intval($_POST['node_id']);
625 $nodegroup_id = intval($_POST['nodegroup_id']);
629 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
630 } elseif ($nodegroup_id) {
631 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
633 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
636 drupal_set_message ("Added slice tag.");
638 drupal_set_error("Could not add slice tag");
639 if ($_POST['sliver_action'])
640 plc_redirect(l_sliver($node_id,$slice_id));
642 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
646 //////////////////////////////////////////////////////////// tag types
648 case 'update-tag-type': {
650 $tag_type_id= intval( $_POST['tag_type_id'] );
651 $tagname = $_POST['tagname'];
652 $min_role_id= intval( $_POST['min_role_id'] );
653 $description= $_POST['description'];
654 $category= $_POST['category'];
656 // make tag_type_fields dict
657 $tag_type_fields= array( "min_role_id" => $min_role_id,
658 "tagname" => $tagname,
659 "description" => $description,
660 "category" => $category,
663 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
664 drupal_set_message ("Tag type $tagname updated");
666 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
667 plc_redirect(l_tag($tag_type_id));
670 case 'add-tag-type': {
672 $tagname = $_POST['tagname'];
673 $min_role_id= intval( $_POST['min_role_id'] );
674 $description= $_POST['description'];
675 $category= $_POST['category'];
677 // make tag_type_fields dict
678 $tag_type_fields= array( "min_role_id" => $min_role_id,
679 "tagname" => $tagname,
680 "description" => $description,
681 "category" => $category,
685 $tag_type_id=$api->AddTagType( $tag_type_fields );
686 if ($tag_type_id > 0)
687 drupal_set_message ("tag type $tag_type_id created");
689 drupal_set_error ("Could not create tag type $tagname");
690 plc_redirect( l_tags());
693 case 'delete-tag-types': {
694 $tag_type_ids = $_POST['tag_type_ids'];
695 if ( ! $tag_type_ids) {
696 drupal_set_message("action=$action - No tag selected");
701 foreach ($tag_type_ids as $tag_type_id)
702 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
707 drupal_set_message ("Deleted $counter tag(s)");
709 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
710 plc_redirect (l_tags());
714 //////////////////////////////////////// tags
715 case 'set-tag-on-node':
716 case 'set-tag-on-interface': {
719 if ($action == 'set-tag-on-node') $node_mode=true;
722 $node_id = intval($_POST['node_id']);
724 $interface_id=intval($_POST['interface_id']);
725 $tag_type_id = intval($_POST['tag_type_id']);
726 $value = $_POST['value'];
728 $tag_types=$api->GetTagTypes(array($tag_type_id));
729 if (count ($tag_types) != 1) {
730 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
733 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
735 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
736 if ( count ($tags) == 1) {
739 $tag_id=$tag['node_tag_id'];
740 $result=$api->UpdateNodeTag($tag_id,$value);
742 $tag_id=$tag['interface_tag_id'];
743 $result=$api->UpdateInterfaceTag($tag_id,$value);
746 drupal_set_message ("Updated tag, new value = $value");
748 drupal_set_error ("Could not update tag");
751 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
753 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
755 drupal_set_message ("Created tag, new value = $value");
757 drupal_set_error ("Could not create tag");
762 plc_redirect (l_node($node_id));
764 plc_redirect (l_interface($interface_id));
767 case 'delete-node-tags' :
768 case 'delete-interface-tags' : {
771 if ($action == 'delete-node-tags') $node_mode=true;
774 $tag_ids=$_POST['node_tag_ids'];
776 $tag_ids=$_POST['interface_tag_ids'];
779 drupal_set_message("action=$action - No tag selected");
784 foreach( $tag_ids as $tag_id ) {
786 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
788 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
795 drupal_set_message ("Deleted $counter tag(s)");
797 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
799 plc_redirect(l_node($_POST['node_id']));
801 plc_redirect(l_interface($_POST['interface_id']));
805 ////////////////////////////////////////
808 plc_debug('GET',$_GET);
809 plc_debug('POST',$_POST);
810 plc_debug('FILES',$_FILES);
815 plc_error ("Unknown action $action in actions.php");