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
79 //////////////////////////////////////// tag types
80 $known_actions []= "update-tag-type";
81 // expects: tag_type_id & name & description & category & min_role_id
82 $known_actions []= "add-tag-type";
83 // expects: tag_type_id & tagname & description & category & min_role_id
84 $known_actions []= "delete-tag-types";
85 // expects: tag_type_ids
87 //////////////////////////////////////// tags
88 $known_actions []= "set-tag-on-node";
89 // expects: node_id tagname value
90 $known_actions []= "set-tag-on-interface";
91 // expects: interface_id tagname value
92 $known_actions []= "delete-node-tags";
93 // expects: node_id & node_tag_ids
94 $known_actions []= "delete-interface-tags";
95 // expects: interface_id & interface_tag_ids
97 ////////////////////////////////////////////////////////////
98 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
99 'broadcast', 'netmask', 'dns1', 'dns2',
100 'hostname', 'mac', 'bwlimit' );
102 //////////////////////////////
103 // sometimes we don't set 'action', but use the submit button name instead
104 // so if 'action' not set, see if $_POST has one of the actions as a key
105 if ($_POST['action'])
106 $action=$_POST['action'];
108 foreach ($known_actions as $known_action)
109 if ($_POST[$known_action]) {
110 $action=$known_action;
114 //uncomment for debugging incoming data
117 $person_id = $_POST['person_id']; // usually needed
120 drupal_set_message ("actions.php: action not set or not in known_actions");
121 plc_debug('POST',$_POST);
127 case 'add-person-to-site': {
128 $site_id = $_POST['site_id'];
129 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
130 plc_redirect (l_person($person_id));
133 case 'remove-person-from-sites': {
134 $site_ids = $_POST['site_ids'];
136 drupal_set_message("action=$action - No site selected");
139 foreach ( $site_ids as $site_id ) {
140 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
142 plc_redirect (l_person($person_id));
145 case 'remove-roles-from-person' : {
146 $role_ids=$_POST['role_ids'];
148 drupal_set_message("action=$action - No role selected");
151 foreach( $role_ids as $role_id) {
152 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
154 plc_redirect (l_person($person_id));
157 case 'add-role-to-person' : {
158 $role_id=$_POST['role_id'];
159 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
160 plc_redirect (l_person($person_id));
163 case 'enable-person' : {
164 $fields = array( "enabled"=>true );
165 $api->UpdatePerson( intval( $person_id ), $fields );
166 plc_redirect (l_person($person_id));
169 case 'disable-person' : {
170 $fields = array( "enabled"=>false );
171 $api->UpdatePerson( intval( $person_id ), $fields );
172 plc_redirect (l_person($person_id));
175 case 'become-person' : {
176 $plc->BecomePerson (intval($person_id));
177 plc_redirect (l_persons());
180 case 'delete-person' : {
181 $api->DeletePerson( intval( $person_id ) );
182 plc_redirect (l_persons());
185 case 'delete-keys' : {
186 $key_ids=$_POST['key_ids'];
188 drupal_set_message("action=$action - No key selected");
193 foreach( $key_ids as $key_id ) {
194 if ($api->DeleteKey( intval( $key_id )) != 1)
200 drupal_set_message ("Deleted $counter key(s)");
202 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
203 plc_redirect(l_person($person_id));
207 case 'upload-key' : {
208 if ( ! isset( $_FILES['key'] ) ) {
209 drupal_set_message ("action=$action, no key file set");
213 $key_file= $_FILES['key']['tmp_name'];
215 plc_error("Please select a valid SSH key file to upload");
218 $fp = fopen( $key_file, "r" );
221 plc_error("Unable to open key file $key_file");
224 // opened the key file, read the one line of contents
225 // The POST operation always creates a file even if the filename
226 // the user specified was garbage. If there was some problem
227 // with the source file, we'll get a zero length read here.
228 $key = fread($fp, filesize($key_file));
231 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
234 drupal_set_message ("New key added");
236 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
238 plc_redirect(l_person($person_id));
241 case 'update-person': {
242 $person_id=$_POST['person_id'];
243 // attempt to update this person
244 $first_name= $_POST['first_name'];
245 $last_name= $_POST['last_name'];
246 $title= $_POST['title'];
247 $email= $_POST['email'];
248 $phone= $_POST['phone'];
250 $bio= str_replace("\r", "", $_POST['bio']);
251 $password1= $_POST['password1'];
252 $password2= $_POST['password2'];
254 if( $password1 != $password2 ) {
255 drupal_set_error ("The passwords do not match");
256 plc_redirect(l_person($person_id));
260 $fields['first_name']= $first_name;
261 $fields['last_name']= $last_name;
262 $fields['title']= $title;
263 $fields['email']= $email;
264 $fields['phone']= $phone;
265 $fields['url']= $url;
266 $fields['bio']= $bio;
268 if ( $password1 != "" )
269 $fields['password']= $password1;
271 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
272 drupal_set_message("$first_name $last_name updated");
274 drupal_set_error ("Could not update person $person_id" . $api->error());
276 plc_redirect(l_person($person_id));
280 //////////////////////////////////////////////////////////// nodes
281 case 'node-boot-state': {
282 $node_id=intval($_POST['node_id']);
283 $boot_state=$_POST['boot_state'];
284 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
286 drupal_set_message("boot state updated");
287 plc_redirect (l_node($node_id));
289 drupal_set_error("Could not set boot_state '$boot_state'");
294 case 'delete-node': {
295 $node_id=intval($_POST['node_id']);
296 $result=$api->DeleteNode( intval( $node_id ) );
298 drupal_set_message("Node $node_id deleted");
299 plc_redirect (l_nodes());
301 drupal_set_error ("Could not delete node $node_id");
306 case 'update-node': {
307 $hostname= $_POST['hostname'];
308 $model= $_POST['model'];
310 $fields= array( "hostname"=>$hostname, "model"=>$model );
311 $api->UpdateNode( intval( $node_id ), $fields );
312 $error= $api->error();
314 if( empty( $error ) ) {
315 drupal_set_message("Update node $hostname");
316 plc_redirect(l_node($node_id));
318 drupal_set_error($error);
323 //////////////////////////////////////////////////////////// interfaces
324 case 'delete-interfaces' : {
325 $interface_ids=$_POST['interface_ids'];
326 if ( ! $interface_ids) {
327 drupal_set_message("action=$action - No interface selected");
332 foreach( $interface_ids as $interface_id ) {
333 if ($api->DeleteInterface( intval( $interface_id )) != 1)
339 drupal_set_message ("Deleted $counter interface(s)");
341 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
342 plc_redirect(l_node($_POST['node_id']));
345 case 'add-interface': {
346 $node_id=$_POST['node_id'];
347 foreach ($interface_details as $field) {
348 $interface[$field]= $_POST[$field];
349 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
350 $interface[$field]= intval( $interface[$field] );
353 $result=$api->AddInterface( intval( $node_id ), $interface );
355 drupal_set_message ("Interface $result added into node $node_id");
357 drupal_set_error ("Could not create interface");
358 plc_redirect (l_node($node_id));
361 case 'update-interface': {
362 $interface_id=$_POST['interface_id'];
363 foreach ($interface_details as $field) {
364 $interface[$field]= $_POST[$field];
365 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
366 $interface[$field]= intval( $interface[$field] );
369 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
371 drupal_set_message ("Interface $interface_id updated");
373 drupal_set_error ("Could not update interface");
374 plc_redirect (l_interface($interface_id));
377 //////////////////////////////////////////////////////////// sites
378 case 'delete-site': {
379 $site_id = intval($_POST['site_id']);
380 if ($api->DeleteSite($site_id) ==1)
381 drupal_set_message ("Site $site_id deleted");
383 drupal_set_error("Failed to delete site $site_id");
384 plc_redirect (l_sites());
388 case 'expire-all-slices-in-site': {
390 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
393 //// old code from sites/expire.php
394 $sites = $api->GetSites( array( intval( $site_id )));
396 // xxx why not 'now?'
397 $expiration= strtotime( $_POST['expires'] );
398 // loop through all slices for site
399 foreach ($site['slice_ids'] as $slice_id) {
400 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
402 // update site to not allow slice creation or renewal
403 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
404 plc_redirect (l_site($site_id));
408 case 'update-site': {
409 $site_id=intval($_POST['site_id']);
410 $name= $_POST['name'];
411 $abbreviated_name= $_POST['abbreviated_name'];
413 $latitude= floatval($_POST['latitude']);
414 $longitude= floatval($_POST['longitude']);
415 //$max_slivers= $_POST['max_slivers'];
417 $fields= array( "name" => $name,
418 "abbreviated_name" => $abbreviated_name,
420 "latitude" => floatval( $latitude ),
421 "longitude" => floatval( $longitude ));
423 if ($_POST['login_base'])
424 $fields['login_base'] = $_POST['login_base'];
425 if ($_POST['max_slices'])
426 $fields['max_slices'] = intval($_POST['max_slices']);
428 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
430 drupal_set_message("Site $name updated");
432 drupal_set_error ("Could not update site $site_id");
434 plc_redirect(l_site($site_id));
438 //////////////////////////////////////////////////////////// slices
439 case 'update-slice': {
440 $slice_id = $_POST['slice_id'];
441 $name = $_POST['name'];
442 $description= $_POST['description'];
445 $fields= array( "description"=>$description, "url"=>$url );
446 $api->UpdateSlice( intval( $slice_id ), $fields );
447 $error= $api->error();
449 if( empty( $error ) ) {
450 drupal_set_message("Update slice $name");
451 plc_redirect(l_slice($slice_id));
453 drupal_set_error($error);
458 case 'renew-slice': {
459 $slice_id = intval ($_POST['slice_id']);
460 $expires = intval ($_POST['expires']);
466 $MAX_FUTURE=$WEEKS*$WEEK;
467 if ( ($expires-$now) > $MAX_FUTURE) {
468 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
469 plc_redirect(l_slice($slice_id));
471 plc_debug('slice_id',$slice_id);
472 plc_debug('expires',$expires);
473 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
474 drupal_set_message("Slice renewed");
476 drupal_set_error("Could not update slice $slice_id");
477 plc_redirect(l_slice($slice_id));
481 case 'remove-persons-from-slice': {
482 $slice_id = intval ($_POST['slice_id']);
483 $person_ids = $_POST['person_ids'];
487 foreach( $person_ids as $person_id ) {
488 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
494 drupal_set_message ("Deleted $counter person(s)");
496 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
497 plc_redirect(l_slice($slice_id) . " &show_users=true");
501 case 'add-persons-in-slice': {
502 $slice_id = intval ($_POST['slice_id']);
503 $person_ids = $_POST['person_ids'];
507 foreach ($person_ids as $person_id) {
508 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
514 drupal_set_message ("Added $counter person(s)");
516 drupal_set_error ("Could not add all selected persons, only $counter were added");
517 plc_redirect(l_slice($slice_id) . "&show_users=true" );
521 //////////////////////////////////////////////////////////// tag types
523 case 'update-tag-type': {
525 $tag_type_id= intval( $_POST['tag_type_id'] );
526 $tagname = $_POST['tagname'];
527 $min_role_id= intval( $_POST['min_role_id'] );
528 $description= $_POST['description'];
529 $category= $_POST['category'];
531 // make tag_type_fields dict
532 $tag_type_fields= array( "min_role_id" => $min_role_id,
533 "tagname" => $tagname,
534 "description" => $description,
535 "category" => $category,
538 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
539 drupal_set_message ("Tag type $tagname updated");
541 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
542 plc_redirect(l_tag($tag_type_id));
545 case 'add-tag-type': {
547 $tagname = $_POST['tagname'];
548 $min_role_id= intval( $_POST['min_role_id'] );
549 $description= $_POST['description'];
550 $category= $_POST['category'];
552 // make tag_type_fields dict
553 $tag_type_fields= array( "min_role_id" => $min_role_id,
554 "tagname" => $tagname,
555 "description" => $description,
556 "category" => $category,
560 $tag_type_id=$api->AddTagType( $tag_type_fields );
561 if ($tag_type_id > 0)
562 drupal_set_message ("tag type $tag_type_id created");
564 drupal_set_error ("Could not create tag type $tagname");
565 plc_redirect( l_tags());
568 case 'delete-tag-types': {
569 $tag_type_ids = $_POST['tag_type_ids'];
570 if ( ! $tag_type_ids) {
571 drupal_set_message("action=$action - No tag selected");
576 foreach ($tag_type_ids as $tag_type_id)
577 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
582 drupal_set_message ("Deleted $counter tag(s)");
584 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
585 plc_redirect (l_tags());
589 //////////////////////////////////////// tags
590 case 'set-tag-on-node':
591 case 'set-tag-on-interface': {
594 if ($action == 'set-tag-on-node') $node_mode=true;
597 $node_id = intval($_POST['node_id']);
599 $interface_id=intval($_POST['interface_id']);
600 $tag_type_id = intval($_POST['tag_type_id']);
601 $value = $_POST['value'];
603 $tag_types=$api->GetTagTypes(array($tag_type_id));
604 if (count ($tag_types) != 1) {
605 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
608 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
610 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
611 if ( count ($tags) == 1) {
614 $tag_id=$tag['node_tag_id'];
615 $result=$api->UpdateNodeTag($tag_id,$value);
617 $tag_id=$tag['interface_tag_id'];
618 $result=$api->UpdateInterfaceTag($tag_id,$value);
621 drupal_set_message ("Updated tag, new value = $value");
623 drupal_set_error ("Could not update tag");
626 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
628 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
630 drupal_set_message ("Created tag, new value = $value");
632 drupal_set_error ("Could not create tag");
637 plc_redirect (l_node($node_id));
639 plc_redirect (l_interface($interface_id));
642 case 'delete-node-tags' :
643 case 'delete-interface-tags' : {
646 if ($action == 'delete-node-tags') $node_mode=true;
649 $tag_ids=$_POST['node_tag_ids'];
651 $tag_ids=$_POST['interface_tag_ids'];
654 drupal_set_message("action=$action - No tag selected");
659 foreach( $tag_ids as $tag_id ) {
661 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
663 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
670 drupal_set_message ("Deleted $counter tag(s)");
672 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
674 plc_redirect(l_node($_POST['node_id']));
676 plc_redirect(l_interface($_POST['interface_id']));
680 ////////////////////////////////////////
683 plc_debug('GET',$_GET);
684 plc_debug('POST',$_POST);
685 plc_debug('FILES',$_FILES);
690 plc_error ("Unknown action $action in actions.php");