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 //////////////////////////////////////// tag types
70 $known_actions []= "update-tag-type";
71 // expects: tag_type_id & name & description & category & min_role_id
72 $known_actions []= "add-tag-type";
73 // expects: tag_type_id & tagname & description & category & min_role_id
74 $known_actions []= "delete-tag-types";
75 // expects: tag_type_ids
77 //////////////////////////////////////// tags
78 $known_actions []= "set-tag-on-node";
79 // expects: node_id tagname value
80 $known_actions []= "set-tag-on-interface";
81 // expects: interface_id tagname value
82 $known_actions []= "delete-node-tags";
83 // expects: node_id & node_tag_ids
84 $known_actions []= "delete-interface-tags";
85 // expects: interface_id & interface_tag_ids
87 ////////////////////////////////////////////////////////////
88 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
89 'broadcast', 'netmask', 'dns1', 'dns2',
90 'hostname', 'mac', 'bwlimit' );
92 //////////////////////////////
93 // sometimes we don't set 'action', but use the submit button name instead
94 // so if 'action' not set, see if $_POST has one of the actions as a key
96 $action=$_POST['action'];
98 foreach ($known_actions as $known_action)
99 if ($_POST[$known_action]) {
100 $action=$known_action;
104 //uncomment for debugging incoming data
107 $person_id = $_POST['person_id']; // usually needed
110 drupal_set_message ("actions.php: action not set or not in known_actions");
111 plc_debug('POST',$_POST);
117 case 'add-person-to-site': {
118 $site_id = $_POST['site_id'];
119 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
120 plc_redirect (l_person($person_id));
123 case 'remove-person-from-sites': {
124 $site_ids = $_POST['site_ids'];
126 drupal_set_message("action=$action - No site selected");
129 foreach ( $site_ids as $site_id ) {
130 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
132 plc_redirect (l_person($person_id));
135 case 'remove-roles-from-person' : {
136 $role_ids=$_POST['role_ids'];
138 drupal_set_message("action=$action - No role selected");
141 foreach( $role_ids as $role_id) {
142 $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) );
144 plc_redirect (l_person($person_id));
147 case 'add-role-to-person' : {
148 $role_id=$_POST['role_id'];
149 $api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) );
150 plc_redirect (l_person($person_id));
153 case 'enable-person' : {
154 $fields = array( "enabled"=>true );
155 $api->UpdatePerson( intval( $person_id ), $fields );
156 plc_redirect (l_person($person_id));
159 case 'disable-person' : {
160 $fields = array( "enabled"=>false );
161 $api->UpdatePerson( intval( $person_id ), $fields );
162 plc_redirect (l_person($person_id));
165 case 'become-person' : {
166 $plc->BecomePerson (intval($person_id));
167 plc_redirect (l_persons());
170 case 'delete-person' : {
171 $api->DeletePerson( intval( $person_id ) );
172 plc_redirect (l_persons());
175 case 'delete-keys' : {
176 $key_ids=$_POST['key_ids'];
178 drupal_set_message("action=$action - No key selected");
183 foreach( $key_ids as $key_id ) {
184 if ($api->DeleteKey( intval( $key_id )) != 1)
190 drupal_set_message ("Deleted $counter key(s)");
192 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
193 plc_redirect(l_person($person_id));
197 case 'upload-key' : {
198 if ( ! isset( $_FILES['key'] ) ) {
199 drupal_set_message ("action=$action, no key file set");
203 $key_file= $_FILES['key']['tmp_name'];
205 plc_error("Please select a valid SSH key file to upload");
208 $fp = fopen( $key_file, "r" );
211 plc_error("Unable to open key file $key_file");
214 // opened the key file, read the one line of contents
215 // The POST operation always creates a file even if the filename
216 // the user specified was garbage. If there was some problem
217 // with the source file, we'll get a zero length read here.
218 $key = fread($fp, filesize($key_file));
221 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
224 drupal_set_message ("New key added");
226 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
228 plc_redirect(l_person($person_id));
231 case 'update-person': {
232 $person_id=$_POST['person_id'];
233 // attempt to update this person
234 $first_name= $_POST['first_name'];
235 $last_name= $_POST['last_name'];
236 $title= $_POST['title'];
237 $email= $_POST['email'];
238 $phone= $_POST['phone'];
240 $bio= str_replace("\r", "", $_POST['bio']);
241 $password1= $_POST['password1'];
242 $password2= $_POST['password2'];
244 if( $password1 != $password2 ) {
245 drupal_set_error ("The passwords do not match");
246 plc_redirect(l_person($person_id));
250 $fields['first_name']= $first_name;
251 $fields['last_name']= $last_name;
252 $fields['title']= $title;
253 $fields['email']= $email;
254 $fields['phone']= $phone;
255 $fields['url']= $url;
256 $fields['bio']= $bio;
258 if ( $password1 != "" )
259 $fields['password']= $password1;
261 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
262 drupal_set_message("$first_name $last_name updated");
264 drupal_set_error ("Could not update person $person_id" . $api->error());
266 plc_redirect(l_person($person_id));
270 //////////////////////////////////////////////////////////// nodes
271 case 'node-boot-state': {
272 $node_id=intval($_POST['node_id']);
273 $boot_state=$_POST['boot_state'];
274 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
276 drupal_set_message("boot state updated");
277 plc_redirect (l_node($node_id));
279 drupal_set_error("Could not set boot_state '$boot_state'");
284 case 'delete-node': {
285 $node_id=intval($_POST['node_id']);
286 $result=$api->DeleteNode( intval( $node_id ) );
288 drupal_set_message("Node $node_id deleted");
289 plc_redirect (l_nodes());
291 drupal_set_error ("Could not delete node $node_id");
296 case 'update-node': {
297 $hostname= $_POST['hostname'];
298 $model= $_POST['model'];
300 $fields= array( "hostname"=>$hostname, "model"=>$model );
301 $api->UpdateNode( intval( $node_id ), $fields );
302 $error= $api->error();
304 if( empty( $error ) ) {
305 drupal_set_message("Update node $hostname");
306 plc_redirect(l_node($node_id));
308 drupal_set_error($error);
313 //////////////////////////////////////////////////////////// interfaces
314 case 'delete-interfaces' : {
315 $interface_ids=$_POST['interface_ids'];
316 if ( ! $interface_ids) {
317 drupal_set_message("action=$action - No interface selected");
322 foreach( $interface_ids as $interface_id ) {
323 if ($api->DeleteInterface( intval( $interface_id )) != 1)
329 drupal_set_message ("Deleted $counter interface(s)");
331 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
332 plc_redirect(l_node($_POST['node_id']));
335 case 'add-interface': {
336 $node_id=$_POST['node_id'];
337 foreach ($interface_details as $field) {
338 $interface[$field]= $_POST[$field];
339 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
340 $interface[$field]= intval( $interface[$field] );
343 $result=$api->AddInterface( intval( $node_id ), $interface );
345 drupal_set_message ("Interface $result added into node $node_id");
347 drupal_set_error ("Could not create interface");
348 plc_redirect (l_node($node_id));
351 case 'update-interface': {
352 $interface_id=$_POST['interface_id'];
353 foreach ($interface_details as $field) {
354 $interface[$field]= $_POST[$field];
355 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
356 $interface[$field]= intval( $interface[$field] );
359 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
361 drupal_set_message ("Interface $interface_id updated");
363 drupal_set_error ("Could not update interface");
364 plc_redirect (l_interface($interface_id));
367 //////////////////////////////////////////////////////////// sites
368 case 'delete-site': {
369 $site_id = intval($_POST['site_id']);
370 if ($api->DeleteSite($site_id) ==1)
371 drupal_set_message ("Site $site_id deleted");
373 drupal_set_error("Failed to delete site $site_id");
374 plc_redirect (l_sites());
378 case 'expire-all-slices-in-site': {
380 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
383 //// old code from sites/expire.php
384 $sites = $api->GetSites( array( intval( $site_id )));
386 // xxx why not 'now?'
387 $expiration= strtotime( $_POST['expires'] );
388 // loop through all slices for site
389 foreach ($site['slice_ids'] as $slice_id) {
390 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
392 // update site to not allow slice creation or renewal
393 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
394 plc_redirect (l_site($site_id));
398 case 'update-site': {
399 $site_id=intval($_POST['site_id']);
400 $name= $_POST['name'];
401 $abbreviated_name= $_POST['abbreviated_name'];
403 $latitude= floatval($_POST['latitude']);
404 $longitude= floatval($_POST['longitude']);
405 //$max_slivers= $_POST['max_slivers'];
407 $fields= array( "name" => $name,
408 "abbreviated_name" => $abbreviated_name,
410 "latitude" => floatval( $latitude ),
411 "longitude" => floatval( $longitude ));
413 if ($_POST['login_base'])
414 $fields['login_base'] = $_POST['login_base'];
415 if ($_POST['max_slices'])
416 $fields['max_slices'] = intval($_POST['max_slices']);
418 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
420 drupal_set_message("Site $name updated");
422 drupal_set_error ("Could not update site $site_id");
424 plc_redirect(l_site($site_id));
428 //////////////////////////////////////////////////////////// tag types
430 case 'update-tag-type': {
432 $tag_type_id= intval( $_POST['tag_type_id'] );
433 $tagname = $_POST['tagname'];
434 $min_role_id= intval( $_POST['min_role_id'] );
435 $description= $_POST['description'];
436 $category= $_POST['category'];
438 // make tag_type_fields dict
439 $tag_type_fields= array( "min_role_id" => $min_role_id,
440 "tagname" => $tagname,
441 "description" => $description,
442 "category" => $category,
445 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
446 drupal_set_message ("Tag type $tagname updated");
448 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
449 plc_redirect(l_tag($tag_type_id));
452 case 'add-tag-type': {
454 $tagname = $_POST['tagname'];
455 $min_role_id= intval( $_POST['min_role_id'] );
456 $description= $_POST['description'];
457 $category= $_POST['category'];
459 // make tag_type_fields dict
460 $tag_type_fields= array( "min_role_id" => $min_role_id,
461 "tagname" => $tagname,
462 "description" => $description,
463 "category" => $category,
467 $tag_type_id=$api->AddTagType( $tag_type_fields );
468 if ($tag_type_id > 0)
469 drupal_set_message ("tag type $tag_type_id created");
471 drupal_set_error ("Could not create tag type $tagname");
472 plc_redirect( l_tags());
475 case 'delete-tag-types': {
476 $tag_type_ids = $_POST['tag_type_ids'];
477 if ( ! $tag_type_ids) {
478 drupal_set_message("action=$action - No tag selected");
483 foreach ($tag_type_ids as $tag_type_id)
484 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
489 drupal_set_message ("Deleted $counter tag(s)");
491 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
492 plc_redirect (l_tags());
496 //////////////////////////////////////// tags
497 case 'set-tag-on-node':
498 case 'set-tag-on-interface': {
501 if ($action == 'set-tag-on-node') $node_mode=true;
504 $node_id = intval($_POST['node_id']);
506 $interface_id=intval($_POST['interface_id']);
507 $tag_type_id = intval($_POST['tag_type_id']);
508 $value = $_POST['value'];
510 $tag_types=$api->GetTagTypes(array($tag_type_id));
511 if (count ($tag_types) != 1) {
512 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
515 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
517 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
518 if ( count ($tags) == 1) {
521 $tag_id=$tag['node_tag_id'];
522 $result=$api->UpdateNodeTag($tag_id,$value);
524 $tag_id=$tag['interface_tag_id'];
525 $result=$api->UpdateInterfaceTag($tag_id,$value);
528 drupal_set_message ("Updated tag, new value = $value");
530 drupal_set_error ("Could not update tag");
533 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
535 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
537 drupal_set_message ("Created tag, new value = $value");
539 drupal_set_error ("Could not create tag");
544 plc_redirect (l_node($node_id));
546 plc_redirect (l_interface($interface_id));
549 case 'delete-node-tags' :
550 case 'delete-interface-tags' : {
553 if ($action == 'delete-node-tags') $node_mode=true;
556 $tag_ids=$_POST['node_tag_ids'];
558 $tag_ids=$_POST['interface_tag_ids'];
561 drupal_set_message("action=$action - No tag selected");
566 foreach( $tag_ids as $tag_id ) {
568 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
570 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
577 drupal_set_message ("Deleted $counter tag(s)");
579 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
581 plc_redirect(l_node($_POST['node_id']));
583 plc_redirect(l_interface($_POST['interface_id']));
587 ////////////////////////////////////////
590 plc_debug('GET',$_GET);
591 plc_debug('POST',$_POST);
592 plc_debug('FILES',$_FILES);
597 plc_error ("Unknown action $action in actions.php");