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 []= "update-slice";
71 // expects: slice_id, name, description, url
72 $known_actions []= "renew-slice";
73 // expects: slice_id & expires
74 $known_actions []= 'remove-persons-from-slice';
75 // expects: slice_id & person_ids
76 $known_actions []= 'add-persons-in-slice';
77 // expects: slice_id & person_ids
78 $known_actions []= 'remove-nodes-from-slice';
79 // expects: slice_id & node_ids
80 $known_actions []= 'add-nodes-in-slice';
81 // expects: slice_id & node_ids
83 //////////////////////////////////////// tag types
84 $known_actions []= "update-tag-type";
85 // expects: tag_type_id & name & description & category & min_role_id
86 $known_actions []= "add-tag-type";
87 // expects: tag_type_id & tagname & description & category & min_role_id
88 $known_actions []= "delete-tag-types";
89 // expects: tag_type_ids
91 //////////////////////////////////////// tags
92 $known_actions []= "set-tag-on-node";
93 // expects: node_id tagname value
94 $known_actions []= "set-tag-on-interface";
95 // expects: interface_id tagname value
96 $known_actions []= "delete-node-tags";
97 // expects: node_id & node_tag_ids
98 $known_actions []= "delete-interface-tags";
99 // expects: interface_id & interface_tag_ids
101 ////////////////////////////////////////////////////////////
102 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
103 'broadcast', 'netmask', 'dns1', 'dns2',
104 'hostname', 'mac', 'bwlimit' );
106 //////////////////////////////
107 // sometimes we don't set 'action', but use the submit button name instead
108 // so if 'action' not set, see if $_POST has one of the actions as a key
109 if ($_POST['action'])
110 $action=$_POST['action'];
112 foreach ($known_actions as $known_action)
113 if ($_POST[$known_action]) {
114 $action=$known_action;
118 //uncomment for debugging incoming data
121 $person_id = $_POST['person_id']; // usually needed
124 drupal_set_message ("actions.php: action not set or not in known_actions");
125 plc_debug('POST',$_POST);
131 case 'add-person-to-site': {
132 $site_id = $_POST['site_id'];
133 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
134 plc_redirect (l_person($person_id));
137 case 'remove-person-from-sites': {
138 $site_ids = $_POST['site_ids'];
140 drupal_set_message("action=$action - No site selected");
143 foreach ( $site_ids as $site_id ) {
144 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
146 plc_redirect (l_person($person_id));
149 case 'remove-roles-from-person' : {
150 $role_ids=$_POST['role_ids'];
152 drupal_set_message("action=$action - No role selected");
155 foreach( $role_ids as $role_id) {
156 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
158 plc_redirect (l_person($person_id));
161 case 'add-role-to-person' : {
162 $role_id=$_POST['role_id'];
163 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
164 plc_redirect (l_person($person_id));
167 case 'enable-person' : {
168 $fields = array( "enabled"=>true );
169 $api->UpdatePerson( intval( $person_id ), $fields );
170 plc_redirect (l_person($person_id));
173 case 'disable-person' : {
174 $fields = array( "enabled"=>false );
175 $api->UpdatePerson( intval( $person_id ), $fields );
176 plc_redirect (l_person($person_id));
179 case 'become-person' : {
180 $plc->BecomePerson (intval($person_id));
181 plc_redirect (l_persons());
184 case 'delete-person' : {
185 $api->DeletePerson( intval( $person_id ) );
186 plc_redirect (l_persons());
189 case 'delete-keys' : {
190 $key_ids=$_POST['key_ids'];
192 drupal_set_message("action=$action - No key selected");
197 foreach( $key_ids as $key_id ) {
198 if ($api->DeleteKey( intval( $key_id )) != 1)
204 drupal_set_message ("Deleted $counter key(s)");
206 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
207 plc_redirect(l_person($person_id));
211 case 'upload-key' : {
212 if ( ! isset( $_FILES['key'] ) ) {
213 drupal_set_message ("action=$action, no key file set");
217 $key_file= $_FILES['key']['tmp_name'];
219 plc_error("Please select a valid SSH key file to upload");
222 $fp = fopen( $key_file, "r" );
225 plc_error("Unable to open key file $key_file");
228 // opened the key file, read the one line of contents
229 // The POST operation always creates a file even if the filename
230 // the user specified was garbage. If there was some problem
231 // with the source file, we'll get a zero length read here.
232 $key = fread($fp, filesize($key_file));
235 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
238 drupal_set_message ("New key added");
240 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
242 plc_redirect(l_person($person_id));
245 case 'update-person': {
246 $person_id=$_POST['person_id'];
247 // attempt to update this person
248 $first_name= $_POST['first_name'];
249 $last_name= $_POST['last_name'];
250 $title= $_POST['title'];
251 $email= $_POST['email'];
252 $phone= $_POST['phone'];
254 $bio= str_replace("\r", "", $_POST['bio']);
255 $password1= $_POST['password1'];
256 $password2= $_POST['password2'];
258 if( $password1 != $password2 ) {
259 drupal_set_error ("The passwords do not match");
260 plc_redirect(l_person($person_id));
264 $fields['first_name']= $first_name;
265 $fields['last_name']= $last_name;
266 $fields['title']= $title;
267 $fields['email']= $email;
268 $fields['phone']= $phone;
269 $fields['url']= $url;
270 $fields['bio']= $bio;
272 if ( $password1 != "" )
273 $fields['password']= $password1;
275 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
276 drupal_set_message("$first_name $last_name updated");
278 drupal_set_error ("Could not update person $person_id" . $api->error());
280 plc_redirect(l_person($person_id));
284 //////////////////////////////////////////////////////////// nodes
285 case 'node-boot-state': {
286 $node_id=intval($_POST['node_id']);
287 $boot_state=$_POST['boot_state'];
288 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
290 drupal_set_message("boot state updated");
291 plc_redirect (l_node($node_id));
293 drupal_set_error("Could not set boot_state '$boot_state'");
298 case 'delete-node': {
299 $node_id=intval($_POST['node_id']);
300 $result=$api->DeleteNode( intval( $node_id ) );
302 drupal_set_message("Node $node_id deleted");
303 plc_redirect (l_nodes());
305 drupal_set_error ("Could not delete node $node_id");
310 case 'update-node': {
311 $hostname= $_POST['hostname'];
312 $model= $_POST['model'];
314 $fields= array( "hostname"=>$hostname, "model"=>$model );
315 $api->UpdateNode( intval( $node_id ), $fields );
316 $error= $api->error();
318 if( empty( $error ) ) {
319 drupal_set_message("Update node $hostname");
320 plc_redirect(l_node($node_id));
322 drupal_set_error($error);
327 //////////////////////////////////////////////////////////// interfaces
328 case 'delete-interfaces' : {
329 $interface_ids=$_POST['interface_ids'];
330 if ( ! $interface_ids) {
331 drupal_set_message("action=$action - No interface selected");
336 foreach( $interface_ids as $interface_id ) {
337 if ($api->DeleteInterface( intval( $interface_id )) != 1)
343 drupal_set_message ("Deleted $counter interface(s)");
345 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
346 plc_redirect(l_node($_POST['node_id']));
349 case 'add-interface': {
350 $node_id=$_POST['node_id'];
351 foreach ($interface_details as $field) {
352 $interface[$field]= $_POST[$field];
353 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
354 $interface[$field]= intval( $interface[$field] );
357 $result=$api->AddInterface( intval( $node_id ), $interface );
359 drupal_set_message ("Interface $result added into node $node_id");
361 drupal_set_error ("Could not create interface");
362 plc_redirect (l_node($node_id));
365 case 'update-interface': {
366 $interface_id=$_POST['interface_id'];
367 foreach ($interface_details as $field) {
368 $interface[$field]= $_POST[$field];
369 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
370 $interface[$field]= intval( $interface[$field] );
373 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
375 drupal_set_message ("Interface $interface_id updated");
377 drupal_set_error ("Could not update interface");
378 plc_redirect (l_interface($interface_id));
381 //////////////////////////////////////////////////////////// sites
382 case 'delete-site': {
383 $site_id = intval($_POST['site_id']);
384 if ($api->DeleteSite($site_id) ==1)
385 drupal_set_message ("Site $site_id deleted");
387 drupal_set_error("Failed to delete site $site_id");
388 plc_redirect (l_sites());
392 case 'expire-all-slices-in-site': {
394 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
397 //// old code from sites/expire.php
398 $sites = $api->GetSites( array( intval( $site_id )));
400 // xxx why not 'now?'
401 $expiration= strtotime( $_POST['expires'] );
402 // loop through all slices for site
403 foreach ($site['slice_ids'] as $slice_id) {
404 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
406 // update site to not allow slice creation or renewal
407 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
408 plc_redirect (l_site($site_id));
412 case 'update-site': {
413 $site_id=intval($_POST['site_id']);
414 $name= $_POST['name'];
415 $abbreviated_name= $_POST['abbreviated_name'];
417 $latitude= floatval($_POST['latitude']);
418 $longitude= floatval($_POST['longitude']);
419 //$max_slivers= $_POST['max_slivers'];
421 $fields= array( "name" => $name,
422 "abbreviated_name" => $abbreviated_name,
424 "latitude" => floatval( $latitude ),
425 "longitude" => floatval( $longitude ));
427 if ($_POST['login_base'])
428 $fields['login_base'] = $_POST['login_base'];
429 if ($_POST['max_slices'])
430 $fields['max_slices'] = intval($_POST['max_slices']);
432 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
434 drupal_set_message("Site $name updated");
436 drupal_set_error ("Could not update site $site_id");
438 plc_redirect(l_site($site_id));
442 //////////////////////////////////////////////////////////// slices
443 case 'update-slice': {
444 $slice_id = $_POST['slice_id'];
445 $name = $_POST['name'];
446 $description= $_POST['description'];
449 $fields= array( "description"=>$description, "url"=>$url );
450 $api->UpdateSlice( intval( $slice_id ), $fields );
451 $error= $api->error();
453 if( empty( $error ) ) {
454 drupal_set_message("Update slice $name");
455 plc_redirect(l_slice($slice_id));
457 drupal_set_error($error);
462 case 'renew-slice': {
463 $slice_id = intval ($_POST['slice_id']);
464 $expires = intval ($_POST['expires']);
470 $MAX_FUTURE=$WEEKS*$WEEK;
471 if ( ($expires-$now) > $MAX_FUTURE) {
472 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
473 plc_redirect(l_slice($slice_id));
475 plc_debug('slice_id',$slice_id);
476 plc_debug('expires',$expires);
477 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
478 drupal_set_message("Slice renewed");
480 drupal_set_error("Could not update slice $slice_id");
481 plc_redirect(l_slice($slice_id));
485 case 'remove-persons-from-slice': {
486 $slice_id = intval ($_POST['slice_id']);
487 $person_ids = $_POST['person_ids'];
491 foreach( $person_ids as $person_id ) {
492 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
498 drupal_set_message ("Deleted $counter person(s)");
500 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
501 plc_redirect(l_slice($slice_id) . " &show_persons=true");
505 case 'add-persons-in-slice': {
506 $slice_id = intval ($_POST['slice_id']);
507 $person_ids = $_POST['person_ids'];
511 foreach ($person_ids as $person_id) {
512 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
518 drupal_set_message ("Added $counter person(s)");
520 drupal_set_error ("Could not add all selected persons, only $counter were added");
521 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
525 case 'remove-nodes-from-slice': {
526 $slice_id = intval ($_POST['slice_id']);
527 $node_ids = array_map("intval",$_POST['node_ids']);
528 $count=count($node_ids);
530 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
531 drupal_set_message ("Removed $count node(s)");
533 drupal_set_error ("Could not remove selected nodes");
534 plc_redirect(l_slice($slice_id) . " &show_nodes=true");
538 case 'add-nodes-in-slice': {
539 $slice_id = intval ($_POST['slice_id']);
540 $node_ids = array_map("intval",$_POST['node_ids']);
541 $count=count($node_ids);
542 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
543 drupal_set_message ("Added $count node(s)");
545 drupal_set_error ("Could not add all selected nodes");
546 plc_redirect(l_slice($slice_id) . "&show_nodes=true" );
550 //////////////////////////////////////////////////////////// tag types
552 case 'update-tag-type': {
554 $tag_type_id= intval( $_POST['tag_type_id'] );
555 $tagname = $_POST['tagname'];
556 $min_role_id= intval( $_POST['min_role_id'] );
557 $description= $_POST['description'];
558 $category= $_POST['category'];
560 // make tag_type_fields dict
561 $tag_type_fields= array( "min_role_id" => $min_role_id,
562 "tagname" => $tagname,
563 "description" => $description,
564 "category" => $category,
567 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
568 drupal_set_message ("Tag type $tagname updated");
570 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
571 plc_redirect(l_tag($tag_type_id));
574 case 'add-tag-type': {
576 $tagname = $_POST['tagname'];
577 $min_role_id= intval( $_POST['min_role_id'] );
578 $description= $_POST['description'];
579 $category= $_POST['category'];
581 // make tag_type_fields dict
582 $tag_type_fields= array( "min_role_id" => $min_role_id,
583 "tagname" => $tagname,
584 "description" => $description,
585 "category" => $category,
589 $tag_type_id=$api->AddTagType( $tag_type_fields );
590 if ($tag_type_id > 0)
591 drupal_set_message ("tag type $tag_type_id created");
593 drupal_set_error ("Could not create tag type $tagname");
594 plc_redirect( l_tags());
597 case 'delete-tag-types': {
598 $tag_type_ids = $_POST['tag_type_ids'];
599 if ( ! $tag_type_ids) {
600 drupal_set_message("action=$action - No tag selected");
605 foreach ($tag_type_ids as $tag_type_id)
606 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
611 drupal_set_message ("Deleted $counter tag(s)");
613 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
614 plc_redirect (l_tags());
618 //////////////////////////////////////// tags
619 case 'set-tag-on-node':
620 case 'set-tag-on-interface': {
623 if ($action == 'set-tag-on-node') $node_mode=true;
626 $node_id = intval($_POST['node_id']);
628 $interface_id=intval($_POST['interface_id']);
629 $tag_type_id = intval($_POST['tag_type_id']);
630 $value = $_POST['value'];
632 $tag_types=$api->GetTagTypes(array($tag_type_id));
633 if (count ($tag_types) != 1) {
634 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
637 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
639 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
640 if ( count ($tags) == 1) {
643 $tag_id=$tag['node_tag_id'];
644 $result=$api->UpdateNodeTag($tag_id,$value);
646 $tag_id=$tag['interface_tag_id'];
647 $result=$api->UpdateInterfaceTag($tag_id,$value);
650 drupal_set_message ("Updated tag, new value = $value");
652 drupal_set_error ("Could not update tag");
655 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
657 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
659 drupal_set_message ("Created tag, new value = $value");
661 drupal_set_error ("Could not create tag");
666 plc_redirect (l_node($node_id));
668 plc_redirect (l_interface($interface_id));
671 case 'delete-node-tags' :
672 case 'delete-interface-tags' : {
675 if ($action == 'delete-node-tags') $node_mode=true;
678 $tag_ids=$_POST['node_tag_ids'];
680 $tag_ids=$_POST['interface_tag_ids'];
683 drupal_set_message("action=$action - No tag selected");
688 foreach( $tag_ids as $tag_id ) {
690 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
692 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
699 drupal_set_message ("Deleted $counter tag(s)");
701 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
703 plc_redirect(l_node($_POST['node_id']));
705 plc_redirect(l_interface($_POST['interface_id']));
709 ////////////////////////////////////////
712 plc_debug('GET',$_GET);
713 plc_debug('POST',$_POST);
714 plc_debug('FILES',$_FILES);
719 plc_error ("Unknown action $action in actions.php");