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 []= "renew-slice";
71 // expects: slice_id & expires
72 $known_actions []= 'remove-persons-from-slice';
73 // expects: slice_id & person_ids
74 $known_actions []= 'add-persons-in-slice';
75 // expects: slice_id & person_ids
77 //////////////////////////////////////// tag types
78 $known_actions []= "update-tag-type";
79 // expects: tag_type_id & name & description & category & min_role_id
80 $known_actions []= "add-tag-type";
81 // expects: tag_type_id & tagname & description & category & min_role_id
82 $known_actions []= "delete-tag-types";
83 // expects: tag_type_ids
85 //////////////////////////////////////// tags
86 $known_actions []= "set-tag-on-node";
87 // expects: node_id tagname value
88 $known_actions []= "set-tag-on-interface";
89 // expects: interface_id tagname value
90 $known_actions []= "delete-node-tags";
91 // expects: node_id & node_tag_ids
92 $known_actions []= "delete-interface-tags";
93 // expects: interface_id & interface_tag_ids
95 ////////////////////////////////////////////////////////////
96 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
97 'broadcast', 'netmask', 'dns1', 'dns2',
98 'hostname', 'mac', 'bwlimit' );
100 //////////////////////////////
101 // sometimes we don't set 'action', but use the submit button name instead
102 // so if 'action' not set, see if $_POST has one of the actions as a key
103 if ($_POST['action'])
104 $action=$_POST['action'];
106 foreach ($known_actions as $known_action)
107 if ($_POST[$known_action]) {
108 $action=$known_action;
112 //uncomment for debugging incoming data
115 $person_id = $_POST['person_id']; // usually needed
118 drupal_set_message ("actions.php: action not set or not in known_actions");
119 plc_debug('POST',$_POST);
125 case 'add-person-to-site': {
126 $site_id = $_POST['site_id'];
127 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
128 plc_redirect (l_person($person_id));
131 case 'remove-person-from-sites': {
132 $site_ids = $_POST['site_ids'];
134 drupal_set_message("action=$action - No site selected");
137 foreach ( $site_ids as $site_id ) {
138 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
140 plc_redirect (l_person($person_id));
143 case 'remove-roles-from-person' : {
144 $role_ids=$_POST['role_ids'];
146 drupal_set_message("action=$action - No role selected");
149 foreach( $role_ids as $role_id) {
150 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
152 plc_redirect (l_person($person_id));
155 case 'add-role-to-person' : {
156 $role_id=$_POST['role_id'];
157 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
158 plc_redirect (l_person($person_id));
161 case 'enable-person' : {
162 $fields = array( "enabled"=>true );
163 $api->UpdatePerson( intval( $person_id ), $fields );
164 plc_redirect (l_person($person_id));
167 case 'disable-person' : {
168 $fields = array( "enabled"=>false );
169 $api->UpdatePerson( intval( $person_id ), $fields );
170 plc_redirect (l_person($person_id));
173 case 'become-person' : {
174 $plc->BecomePerson (intval($person_id));
175 plc_redirect (l_persons());
178 case 'delete-person' : {
179 $api->DeletePerson( intval( $person_id ) );
180 plc_redirect (l_persons());
183 case 'delete-keys' : {
184 $key_ids=$_POST['key_ids'];
186 drupal_set_message("action=$action - No key selected");
191 foreach( $key_ids as $key_id ) {
192 if ($api->DeleteKey( intval( $key_id )) != 1)
198 drupal_set_message ("Deleted $counter key(s)");
200 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
201 plc_redirect(l_person($person_id));
205 case 'upload-key' : {
206 if ( ! isset( $_FILES['key'] ) ) {
207 drupal_set_message ("action=$action, no key file set");
211 $key_file= $_FILES['key']['tmp_name'];
213 plc_error("Please select a valid SSH key file to upload");
216 $fp = fopen( $key_file, "r" );
219 plc_error("Unable to open key file $key_file");
222 // opened the key file, read the one line of contents
223 // The POST operation always creates a file even if the filename
224 // the user specified was garbage. If there was some problem
225 // with the source file, we'll get a zero length read here.
226 $key = fread($fp, filesize($key_file));
229 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
232 drupal_set_message ("New key added");
234 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
236 plc_redirect(l_person($person_id));
239 case 'update-person': {
240 $person_id=$_POST['person_id'];
241 // attempt to update this person
242 $first_name= $_POST['first_name'];
243 $last_name= $_POST['last_name'];
244 $title= $_POST['title'];
245 $email= $_POST['email'];
246 $phone= $_POST['phone'];
248 $bio= str_replace("\r", "", $_POST['bio']);
249 $password1= $_POST['password1'];
250 $password2= $_POST['password2'];
252 if( $password1 != $password2 ) {
253 drupal_set_error ("The passwords do not match");
254 plc_redirect(l_person($person_id));
258 $fields['first_name']= $first_name;
259 $fields['last_name']= $last_name;
260 $fields['title']= $title;
261 $fields['email']= $email;
262 $fields['phone']= $phone;
263 $fields['url']= $url;
264 $fields['bio']= $bio;
266 if ( $password1 != "" )
267 $fields['password']= $password1;
269 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
270 drupal_set_message("$first_name $last_name updated");
272 drupal_set_error ("Could not update person $person_id" . $api->error());
274 plc_redirect(l_person($person_id));
278 //////////////////////////////////////////////////////////// nodes
279 case 'node-boot-state': {
280 $node_id=intval($_POST['node_id']);
281 $boot_state=$_POST['boot_state'];
282 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
284 drupal_set_message("boot state updated");
285 plc_redirect (l_node($node_id));
287 drupal_set_error("Could not set boot_state '$boot_state'");
292 case 'delete-node': {
293 $node_id=intval($_POST['node_id']);
294 $result=$api->DeleteNode( intval( $node_id ) );
296 drupal_set_message("Node $node_id deleted");
297 plc_redirect (l_nodes());
299 drupal_set_error ("Could not delete node $node_id");
304 case 'update-node': {
305 $hostname= $_POST['hostname'];
306 $model= $_POST['model'];
308 $fields= array( "hostname"=>$hostname, "model"=>$model );
309 $api->UpdateNode( intval( $node_id ), $fields );
310 $error= $api->error();
312 if( empty( $error ) ) {
313 drupal_set_message("Update node $hostname");
314 plc_redirect(l_node($node_id));
316 drupal_set_error($error);
321 //////////////////////////////////////////////////////////// interfaces
322 case 'delete-interfaces' : {
323 $interface_ids=$_POST['interface_ids'];
324 if ( ! $interface_ids) {
325 drupal_set_message("action=$action - No interface selected");
330 foreach( $interface_ids as $interface_id ) {
331 if ($api->DeleteInterface( intval( $interface_id )) != 1)
337 drupal_set_message ("Deleted $counter interface(s)");
339 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
340 plc_redirect(l_node($_POST['node_id']));
343 case 'add-interface': {
344 $node_id=$_POST['node_id'];
345 foreach ($interface_details as $field) {
346 $interface[$field]= $_POST[$field];
347 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
348 $interface[$field]= intval( $interface[$field] );
351 $result=$api->AddInterface( intval( $node_id ), $interface );
353 drupal_set_message ("Interface $result added into node $node_id");
355 drupal_set_error ("Could not create interface");
356 plc_redirect (l_node($node_id));
359 case 'update-interface': {
360 $interface_id=$_POST['interface_id'];
361 foreach ($interface_details as $field) {
362 $interface[$field]= $_POST[$field];
363 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
364 $interface[$field]= intval( $interface[$field] );
367 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
369 drupal_set_message ("Interface $interface_id updated");
371 drupal_set_error ("Could not update interface");
372 plc_redirect (l_interface($interface_id));
375 //////////////////////////////////////////////////////////// sites
376 case 'delete-site': {
377 $site_id = intval($_POST['site_id']);
378 if ($api->DeleteSite($site_id) ==1)
379 drupal_set_message ("Site $site_id deleted");
381 drupal_set_error("Failed to delete site $site_id");
382 plc_redirect (l_sites());
386 case 'expire-all-slices-in-site': {
388 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
391 //// old code from sites/expire.php
392 $sites = $api->GetSites( array( intval( $site_id )));
394 // xxx why not 'now?'
395 $expiration= strtotime( $_POST['expires'] );
396 // loop through all slices for site
397 foreach ($site['slice_ids'] as $slice_id) {
398 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
400 // update site to not allow slice creation or renewal
401 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
402 plc_redirect (l_site($site_id));
406 case 'update-site': {
407 $site_id=intval($_POST['site_id']);
408 $name= $_POST['name'];
409 $abbreviated_name= $_POST['abbreviated_name'];
411 $latitude= floatval($_POST['latitude']);
412 $longitude= floatval($_POST['longitude']);
413 //$max_slivers= $_POST['max_slivers'];
415 $fields= array( "name" => $name,
416 "abbreviated_name" => $abbreviated_name,
418 "latitude" => floatval( $latitude ),
419 "longitude" => floatval( $longitude ));
421 if ($_POST['login_base'])
422 $fields['login_base'] = $_POST['login_base'];
423 if ($_POST['max_slices'])
424 $fields['max_slices'] = intval($_POST['max_slices']);
426 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
428 drupal_set_message("Site $name updated");
430 drupal_set_error ("Could not update site $site_id");
432 plc_redirect(l_site($site_id));
436 //////////////////////////////////////////////////////////// slices
437 case 'renew-slice': {
438 $slice_id = intval ($_POST['slice_id']);
439 $expires = intval ($_POST['expires']);
445 $MAX_FUTURE=$WEEKS*$WEEK;
446 if ( ($expires-$now) > $MAX_FUTURE) {
447 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
448 plc_redirect(l_slice($slice_id));
450 plc_debug('slice_id',$slice_id);
451 plc_debug('expires',$expires);
452 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
453 drupal_set_message("Slice renewed");
455 drupal_set_error("Could not update slice $slice_id");
456 plc_redirect(l_slice($slice_id));
460 case 'remove-persons-from-slice': {
461 $slice_id = intval ($_POST['slice_id']);
462 $person_ids = $_POST['person_ids'];
466 foreach( $person_ids as $person_id ) {
467 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
473 drupal_set_message ("Deleted $counter person(s)");
475 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
476 plc_redirect(l_slice($slice_id));
480 case 'add-persons-in-slice': {
481 $slice_id = intval ($_POST['slice_id']);
482 $person_ids = $_POST['person_ids'];
486 foreach( $person_ids as $person_id ) {
487 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
493 drupal_set_message ("Added $counter person(s)");
495 drupal_set_error ("Could not add all selected persons, only $counter were added");
496 plc_redirect(l_slice($slice_id));
500 //////////////////////////////////////////////////////////// tag types
502 case 'update-tag-type': {
504 $tag_type_id= intval( $_POST['tag_type_id'] );
505 $tagname = $_POST['tagname'];
506 $min_role_id= intval( $_POST['min_role_id'] );
507 $description= $_POST['description'];
508 $category= $_POST['category'];
510 // make tag_type_fields dict
511 $tag_type_fields= array( "min_role_id" => $min_role_id,
512 "tagname" => $tagname,
513 "description" => $description,
514 "category" => $category,
517 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
518 drupal_set_message ("Tag type $tagname updated");
520 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
521 plc_redirect(l_tag($tag_type_id));
524 case 'add-tag-type': {
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,
539 $tag_type_id=$api->AddTagType( $tag_type_fields );
540 if ($tag_type_id > 0)
541 drupal_set_message ("tag type $tag_type_id created");
543 drupal_set_error ("Could not create tag type $tagname");
544 plc_redirect( l_tags());
547 case 'delete-tag-types': {
548 $tag_type_ids = $_POST['tag_type_ids'];
549 if ( ! $tag_type_ids) {
550 drupal_set_message("action=$action - No tag selected");
555 foreach ($tag_type_ids as $tag_type_id)
556 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
561 drupal_set_message ("Deleted $counter tag(s)");
563 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
564 plc_redirect (l_tags());
568 //////////////////////////////////////// tags
569 case 'set-tag-on-node':
570 case 'set-tag-on-interface': {
573 if ($action == 'set-tag-on-node') $node_mode=true;
576 $node_id = intval($_POST['node_id']);
578 $interface_id=intval($_POST['interface_id']);
579 $tag_type_id = intval($_POST['tag_type_id']);
580 $value = $_POST['value'];
582 $tag_types=$api->GetTagTypes(array($tag_type_id));
583 if (count ($tag_types) != 1) {
584 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
587 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
589 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
590 if ( count ($tags) == 1) {
593 $tag_id=$tag['node_tag_id'];
594 $result=$api->UpdateNodeTag($tag_id,$value);
596 $tag_id=$tag['interface_tag_id'];
597 $result=$api->UpdateInterfaceTag($tag_id,$value);
600 drupal_set_message ("Updated tag, new value = $value");
602 drupal_set_error ("Could not update tag");
605 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
607 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
609 drupal_set_message ("Created tag, new value = $value");
611 drupal_set_error ("Could not create tag");
616 plc_redirect (l_node($node_id));
618 plc_redirect (l_interface($interface_id));
621 case 'delete-node-tags' :
622 case 'delete-interface-tags' : {
625 if ($action == 'delete-node-tags') $node_mode=true;
628 $tag_ids=$_POST['node_tag_ids'];
630 $tag_ids=$_POST['interface_tag_ids'];
633 drupal_set_message("action=$action - No tag selected");
638 foreach( $tag_ids as $tag_id ) {
640 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
642 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
649 drupal_set_message ("Deleted $counter tag(s)");
651 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
653 plc_redirect(l_node($_POST['node_id']));
655 plc_redirect(l_interface($_POST['interface_id']));
659 ////////////////////////////////////////
662 plc_debug('GET',$_GET);
663 plc_debug('POST',$_POST);
664 plc_debug('FILES',$_FILES);
669 plc_error ("Unknown action $action in actions.php");