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)
54 $known_actions []= "reboot-node-with-pcu";
57 //////////////////////////////////////// interfaces
58 $known_actions []= "delete-interfaces";
59 // expects: interface_ids
60 $known_actions []="add-interface";
61 // expects: node_id & interface details
62 $known_actions []="new-interface";
64 $known_actions []="update-interface";
65 // expects: interface_id & interface details
67 //////////////////////////////////////// sites
68 $known_actions []= "delete-site";
70 $known_actions []= "expire-all-slices-in-site";
72 $known_actions []= "update-site";
73 // expects: site_id & name abbreviated_name url latitude longitude [login_base max_slices]
75 //////////////////////////////////////// slices
76 $known_actions []= "delete-slice";
78 $known_actions []= "update-slice";
79 // expects: slice_id, name, description, url
80 $known_actions []= "renew-slice";
81 // expects: slice_id & expires
82 $known_actions []= 'remove-persons-from-slice';
83 // expects: slice_id & person_ids
84 $known_actions []= 'add-persons-in-slice';
85 // expects: slice_id & person_ids
86 $known_actions []= 'remove-nodes-from-slice';
87 // expects: slice_id & node_ids
88 $known_actions []= 'add-nodes-in-slice';
89 // expects: slice_id & node_ids
90 $known_actions []= 'update-initscripts';
91 // expects: slice_id & name & previous-initscript & previous-initscript-code
92 // & initscript & initscript-code
93 $known_actions []= 'delete-slice-tags';
94 // expects: slice_tag_id
95 $known_actions []= 'add-slice-tag';
96 // expects: slice_id & tag_type_id & node_id & nodegroup_id
98 //////////////////////////////////////// tag types
99 $known_actions []= "update-tag-type";
100 // expects: tag_type_id & name & description & category & min_role_id
101 $known_actions []= "add-tag-type";
102 // expects: tag_type_id & tagname & description & category & min_role_id
103 $known_actions []= "delete-tag-types";
104 // expects: tag_type_ids
105 $known_actions []= "remove-roles-from-tag-type";
106 // expects: tag_type_id & role_ids
107 $known_actions []= "add-role-to-tag-type";
108 // expects: tag_type_id_id & id
110 //////////////////////////////////////// tags
111 $known_actions []= "set-tag-on-node";
112 // expects: node_id tagname value
113 $known_actions []= "set-tag-on-interface";
114 // expects: interface_id tagname value
115 $known_actions []= "delete-node-tags";
116 // expects: node_id & node_tag_ids
117 $known_actions []= "delete-interface-tags";
118 // expects: interface_id & interface_tag_ids
120 //////////////////////////////////////// nodegroups
121 $known_actions []= "update-nodegroup";
122 // expects nodegroup_id groupname value
123 $known_actions []= "add-nodegroup";
124 // expects groupname, tag_type_id, value
125 $known_actions []= 'delete-nodegroups';
126 // expects nodegroup_ids
128 //////////////////////////////////////// leases
129 $known_actions []= "manage-leases";
130 // expects as 'actions' a list of 'action' of the form
131 // either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
132 // or [ 'delete-leases', lease_id ]
134 ////////////////////////////////////////////////////////////
135 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
136 'broadcast', 'netmask', 'dns1', 'dns2',
137 'hostname', 'mac', 'bwlimit' );
139 //////////////////////////////
140 // sometimes we don't set 'action', but use the submit button name instead
141 // so if 'action' not set, see if $_POST has one of the actions as a key
142 if ($_POST['action'])
143 $action=$_POST['action'];
145 foreach ($known_actions as $known_action)
146 if ($_POST[$known_action]) {
147 $action=$known_action;
151 //uncomment for debugging incoming data
154 $person_id = $_POST['person_id']; // usually needed
157 drupal_set_message ("actions.php: action not set or not in known_actions");
158 plc_debug('POST',$_POST);
164 case 'add-person-to-site': {
165 $site_id = $_POST['site_id'];
166 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
167 plc_redirect (l_person($person_id));
170 case 'remove-person-from-sites': {
171 $site_ids = $_POST['site_ids'];
173 drupal_set_message("action=$action - No site selected");
176 foreach ( $site_ids as $site_id ) {
177 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
179 plc_redirect (l_person($person_id));
182 case 'remove-roles-from-person' : {
183 $role_ids=$_POST['role_ids'];
185 drupal_set_error("You have not selected role(s) to remove");
187 foreach( $role_ids as $role_id)
188 if ( $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) ) != 1 )
189 drupal_set_error ("Could not remove role $role_id from person $person_id");
191 plc_redirect (l_person_roles($person_id));
194 case 'add-role-to-person' : {
195 $role_id=$_POST['role_id'];
197 drupal_set_error ("You have not selected a role to add");
198 } else if ($api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) ) != 1) {
199 drupal_set_error("Could not add role $role_id to person $person_id");
201 plc_redirect (l_person_roles($person_id));
204 case 'enable-person' : {
205 $fields = array( "enabled"=>true );
206 $api->UpdatePerson( intval( $person_id ), $fields );
207 plc_redirect (l_person($person_id));
210 case 'disable-person' : {
211 $fields = array( "enabled"=>false );
212 $api->UpdatePerson( intval( $person_id ), $fields );
213 plc_redirect (l_person($person_id));
216 case 'become-person' : {
217 $plc->BecomePerson (intval($person_id));
218 plc_redirect (l_person(intval($person_id)));
221 case 'delete-person' : {
222 $api->DeletePerson( intval( $person_id ) );
223 plc_redirect (l_persons());
226 case 'delete-keys' : {
227 $key_ids=$_POST['key_ids'];
229 drupal_set_message("action=$action - No key selected");
234 foreach( $key_ids as $key_id ) {
235 if ($api->DeleteKey( intval( $key_id )) != 1)
241 drupal_set_message ("Deleted $counter key(s)");
243 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
244 plc_redirect(l_person($person_id));
248 case 'upload-key' : {
249 if ( ! isset( $_FILES['key'] ) ) {
250 drupal_set_message ("action=$action, no key file set");
254 $key_file= $_FILES['key']['tmp_name'];
256 plc_error("Please select a valid SSH key file to upload");
259 $fp = fopen( $key_file, "r" );
262 plc_error("Unable to open key file $key_file");
265 // opened the key file, read the one line of contents
266 // The POST operation always creates a file even if the filename
267 // the user specified was garbage. If there was some problem
268 // with the source file, we'll get a zero length read here.
269 $key = fread($fp, filesize($key_file));
272 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
275 drupal_set_message ("New key added");
277 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
279 plc_redirect(l_person($person_id));
282 case 'update-person': {
283 $person_id=$_POST['person_id'];
284 // attempt to update this person
285 $first_name= $_POST['first_name'];
286 $last_name= $_POST['last_name'];
287 $title= $_POST['title'];
288 $email= $_POST['email'];
289 $phone= $_POST['phone'];
291 $bio= str_replace("\r", "", $_POST['bio']);
292 $password1= $_POST['password1'];
293 $password2= $_POST['password2'];
295 if( $password1 != $password2 ) {
296 drupal_set_error ("The passwords do not match");
297 plc_redirect(l_person($person_id));
301 $fields['first_name']= $first_name;
302 $fields['last_name']= $last_name;
303 $fields['title']= $title;
304 $fields['email']= $email;
305 $fields['phone']= $phone;
306 $fields['url']= $url;
307 $fields['bio']= $bio;
309 if ( $password1 != "" )
310 $fields['password']= $password1;
312 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
313 drupal_set_message("$first_name $last_name updated");
315 drupal_set_error ("Could not update person $person_id" . $api->error());
317 plc_redirect(l_person($person_id));
321 //////////////////////////////////////////////////////////// nodes
322 case 'node-boot-state': {
323 $node_id=intval($_POST['node_id']);
324 $boot_state=$_POST['boot_state'];
325 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
327 drupal_set_message("boot state updated");
328 plc_redirect (l_node($node_id));
330 drupal_set_error("Could not set boot_state '$boot_state'");
335 case 'delete-node': {
336 $node_id=intval($_POST['node_id']);
337 $result=$api->DeleteNode( intval( $node_id ) );
339 drupal_set_message("Node $node_id deleted");
340 plc_redirect (l_nodes());
342 drupal_set_error ("Could not delete node $node_id");
347 case 'update-node': {
348 $node_id=intval($_POST['node_id']);
349 $hostname= $_POST['hostname'];
350 $model= $_POST['model'];
352 $fields= array( "hostname"=>$hostname, "model"=>$model );
353 $api->UpdateNode( $node_id, $fields );
354 $error= $api->error();
356 if( empty( $error ) ) {
357 drupal_set_message("Update node $hostname");
358 plc_redirect(l_node($node_id));
360 drupal_set_error($error);
365 // this code will ensure that at most one PCU gets attached to the node
367 $node_id=intval($_POST['node_id']);
368 $pcu_id=intval($_POST['pcu_id']);
369 $port=intval($_POST['port']);
370 // always start with deleting former PCUs
371 $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
372 $former_pcu_ids = $nodes[0]['pcu_ids'];
373 if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
374 if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1)
375 drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
377 drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
379 // re-attach only if provided pcu_id >=0
381 if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
382 drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
384 drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
386 drupal_set_message ('Detached node from all PCUs');
389 plc_redirect(l_node($node_id));
393 case 'reboot-node-with-pcu': {
394 $node_id=intval($_POST['node_id']);
395 $hostname= $_POST['hostname'];
396 $test = $_POST['test'];
397 settype($test, "boolean");
399 $ret = $api->RebootNodeWithPCU( $node_id, $test );
400 $error= $api->error();
402 if( empty( $error ) ) {
403 drupal_set_message("Reboot node $hostname: $ret");
404 plc_redirect(l_node($node_id));
406 drupal_set_error($error);
412 //////////////////////////////////////////////////////////// interfaces
413 case 'delete-interfaces' : {
414 $interface_ids=$_POST['interface_ids'];
415 if ( ! $interface_ids) {
416 drupal_set_message("action=$action - No interface selected");
421 foreach( $interface_ids as $interface_id ) {
422 if ($api->DeleteInterface( intval( $interface_id )) != 1)
428 drupal_set_message ("Deleted $counter interface(s)");
430 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
431 plc_redirect(l_node($_POST['node_id']));
434 case 'new-interface': {
435 plc_redirect(l_interface_add($_POST['node_id']));
438 case 'add-interface': {
439 $node_id=intval($_POST['node_id']);
440 foreach ($interface_details as $field) {
441 $interface[$field]= $_POST[$field];
442 // these must be integers
443 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
444 if ( empty ($interface[$field]) )
445 unset ($interface[$field]);
447 $interface[$field]= intval( $interface[$field] );
450 $interface_id =$api->AddInterface( $node_id , $interface );
451 if ($interface_id <= 0 ) {
452 drupal_set_error ("Could not create interface");
453 drupal_set_error ($api->error());
455 $ip=$interface['ip'];
456 drupal_set_message ("Interface $ip added into node $node_id");
457 if ($_POST['is-virtual']) {
458 $ifname=$_POST['ifname'];
459 if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0)
460 drupal_set_error ("Could not set tag 'ifname'=$ifname");
462 drupal_set_message ("Set tag 'ifname'=$ifname");
463 $alias=$_POST['alias'];
464 // deafult to interface_id
465 if ( ! $alias ) $alias=strval($interface_id);
466 if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0)
467 drupal_set_error ("Could not set tag 'alias'=$alias");
469 drupal_set_message ("Set tag 'alias'=$alias");
472 plc_redirect (l_node_interfaces($node_id));
475 case 'update-interface': {
476 $interface_id=$_POST['interface_id'];
477 foreach ($interface_details as $field) {
478 $interface[$field]= $_POST[$field];
479 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
480 if ( intval($interface[$field]) != 0 ) {
481 $interface[$field]= intval( $interface[$field]);
482 } elseif ($field=='bwlimit' ) {
483 $interface[$field] = NULL;
487 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
489 drupal_set_message ("Interface $interface_id updated");
491 drupal_set_error ("Could not update interface: " . $api->error());
492 plc_redirect (l_interface($interface_id));
495 //////////////////////////////////////////////////////////// sites
496 case 'delete-site': {
497 $site_id = intval($_POST['site_id']);
498 if ($api->DeleteSite($site_id) ==1)
499 drupal_set_message ("Site $site_id deleted");
501 drupal_set_error("Failed to delete site $site_id");
502 plc_redirect (l_sites());
506 case 'expire-all-slices-in-site': {
508 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
511 //// old code from sites/expire.php
512 $sites = $api->GetSites( array( intval( $site_id )));
514 // xxx why not 'now?'
515 $expiration= strtotime( $_POST['expires'] );
516 // loop through all slices for site
517 foreach ($site['slice_ids'] as $slice_id) {
518 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
520 // update site to not allow slice creation or renewal
521 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
522 plc_redirect (l_site($site_id));
526 case 'update-site': {
527 $site_id=intval($_POST['site_id']);
528 $name= $_POST['name'];
529 $abbreviated_name= $_POST['abbreviated_name'];
531 $latitude= floatval($_POST['latitude']);
532 $longitude= floatval($_POST['longitude']);
533 //$max_slivers= $_POST['max_slivers'];
535 $fields= array( "name" => $name,
536 "abbreviated_name" => $abbreviated_name,
538 "latitude" => floatval( $latitude ),
539 "longitude" => floatval( $longitude ));
541 if ($_POST['login_base'])
542 $fields['login_base'] = $_POST['login_base'];
543 if (isset($_POST['max_slices']))
544 $fields['max_slices'] = intval($_POST['max_slices']);
545 if (isset($_POST['enabled'])) {
546 $fields['enabled'] = (bool)$_POST['enabled'];
549 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
551 drupal_set_message("Site $name updated");
553 drupal_set_error ("Could not update site $site_id");
555 plc_redirect(l_site($site_id));
559 //////////////////////////////////////////////////////////// slices
560 case 'delete-slice': {
561 $slice_id = $_POST['slice_id'];
562 if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
563 drupal_set_message("Slice $slice_id deleted");
564 plc_redirect(l_slices());
566 drupal_set_error("Could not delete slice $slice_id " . $api->error());
571 case 'update-slice': {
572 $slice_id = $_POST['slice_id'];
573 $name = $_POST['name'];
574 $description= $_POST['description'];
577 $fields= array( "description"=>$description, "url"=>$url );
578 $api->UpdateSlice( intval( $slice_id ), $fields );
579 $error= $api->error();
581 if( empty( $error ) ) {
582 drupal_set_message("Update slice $name");
583 plc_redirect(l_slice($slice_id));
585 drupal_set_error($error);
590 case 'renew-slice': {
591 $slice_id = intval ($_POST['slice_id']);
592 $expires = intval ($_POST['expires']);
598 $MAX_FUTURE=$WEEKS*$WEEK;
599 if ( ($expires-$now) > $MAX_FUTURE) {
600 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
601 plc_redirect(l_slice($slice_id));
603 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
604 drupal_set_message("Slice renewed");
606 drupal_set_error("Could not update slice $slice_id");
607 plc_redirect(l_slice($slice_id));
611 case 'remove-persons-from-slice': {
612 $slice_id = intval ($_POST['slice_id']);
613 $person_ids = $_POST['person_ids'];
616 $tmp_slices = $api->GetSlices($slice_id, array("name"));
617 if (count($tmp_slices) > 0) {
618 $tmp_slice = $tmp_slices[0];
619 $slice_name = $tmp_slice["name"];
621 $notify_subject = "Removed from slice: " . $slice_name;
622 $notify_body = sprintf("You have been removed from the slice %s.
624 Our support team will be glad to answer any question that you might have.
626 $notify_person_ids = array();
630 foreach( $person_ids as $person_id ) {
631 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
634 array_push($notify_person_ids, intval($person_id));
639 $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
640 drupal_set_message ("Deleted $counter person(s)");
643 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
644 plc_redirect(l_slice($slice_id) . " &show_persons=true");
648 case 'add-persons-in-slice': {
649 $slice_id = intval ($_POST['slice_id']);
650 $person_ids = $_POST['person_ids'];
653 $tmp_slices = $api->GetSlices($slice_id, array("name"));
654 if (count($tmp_slices) > 0) {
655 $tmp_slice = $tmp_slices[0];
656 $slice_name = $tmp_slice["name"];
658 $notify_subject = "Added to slice: " . $slice_name;
659 $notify_body = sprintf("You have been added to the slice %s as a user.
661 You can go to your slice page following the link below:
662 https://%s:%d/db/slices/index.php?id=%d
664 Our support team will be glad to answer any question that you might have.
665 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
666 $notify_person_ids = array();
670 foreach ($person_ids as $person_id) {
671 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
674 array_push($notify_person_ids, intval($person_id));
679 $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
680 drupal_set_message ("Added $counter person(s)");
683 drupal_set_error ("Could not add all selected persons, only $counter were added");
684 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
688 case 'remove-nodes-from-slice': {
689 $slice_id = intval ($_POST['slice_id']);
690 $node_ids = array_map("intval",$_POST['node_ids']);
691 $count=count($node_ids);
693 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
694 drupal_set_message ("Removed $count node(s)");
696 drupal_set_error ("Could not remove selected nodes");
697 plc_redirect(l_slice_nodes($slice_id));
701 case 'add-nodes-in-slice': {
702 $slice_id = intval ($_POST['slice_id']);
703 $node_ids = array_map("intval",$_POST['node_ids']);
704 $count=count($node_ids);
705 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
706 drupal_set_message ("Added $count node(s)");
708 drupal_set_error ("Could not add all selected nodes");
709 plc_redirect(l_slice_nodes($slice_id));
713 case 'update-initscripts': {
714 // expects: slice_id & name & previous-initscript & previous-initscript-code
715 // & initscript & initscript-code
716 $slice_id = intval ($_POST['slice_id']);
717 $previous_initscript=$_POST['previous-initscript'];
718 $initscript=$_POST['initscript'];
719 $previous_initscript_code=html_entity_decode($_POST['previous-initscript-code']);
720 $initscript_code=$_POST['initscript-code'];
723 if (strcmp($initscript,$previous_initscript) != 0) {
724 $newvalue=$api->SetSliceInitscript($slice_id,$initscript);
725 $status = (strcmp($newvalue,$initscript)==0) ? "OK" : "failed";
726 if (! $initscript) drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
727 else drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
731 // somehow some \r chars make it here; just ignore them
732 $previous_initscript_code=str_replace("\r","",$previous_initscript_code);
733 // plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
735 $initscript_code=str_replace("\r","",$initscript_code);
736 // make sure the script ends with a single \n
737 $initscript_code=trim($initscript_code);
738 if (!empty($initscript_code) && $initscript_code[strlen($initscript_code)-1] != "\n")
739 $initscript_code.="\n";
740 // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
742 if (strcmp($initscript_code,$previous_initscript_code) != 0) {
743 $newvalue=$api->SetSliceInitscriptCode($slice_id,$initscript_code);
744 // plc_debug_txt('newvalue',$newvalue);
745 $status=(strcmp($newvalue,$initscript_code)==0) ? "OK" : "failed";
746 if (! $initscript_code) drupal_set_message("Removed initscript code " . $status);
747 else drupal_set_message("Installed new initscript code " . $status);
750 if (!$changes) drupal_set_message("No changes required in initscript");
751 plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1" );
756 case 'delete-slice-tags': {
757 $slice_id = intval($_POST['slice_id']);
758 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
761 foreach($slice_tag_ids as $slice_tag_id) {
762 if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
764 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
769 drupal_set_message ("Deleted $count slice tag(s)");
770 plc_redirect(l_slice($slice_id) . "&show_tags=1" );
774 case 'add-slice-tag': {
775 $slice_id = intval($_POST['slice_id']);
776 $tag_type_id = intval($_POST['tag_type_id']);
777 $value = $_POST['value'];
778 $node_id = intval($_POST['node_id']);
779 $nodegroup_id = intval($_POST['nodegroup_id']);
783 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
784 } elseif ($nodegroup_id) {
785 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
787 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
790 drupal_set_message ("Added slice tag.");
792 drupal_set_error("Could not add slice tag");
793 if ($_POST['sliver_action'])
794 plc_redirect(l_sliver($node_id,$slice_id));
796 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
800 //////////////////////////////////////////////////////////// tag types
802 case 'update-tag-type': {
804 $tag_type_id= intval( $_POST['tag_type_id'] );
805 $tagname = $_POST['tagname'];
806 $min_role_id= intval( $_POST['min_role_id'] );
807 $description= $_POST['description'];
808 $category= $_POST['category'];
810 // make tag_type_fields dict
811 $tag_type_fields= array( "min_role_id" => $min_role_id,
812 "tagname" => $tagname,
813 "description" => $description,
814 "category" => $category,
817 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
818 drupal_set_message ("Tag type $tagname updated");
820 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
821 plc_redirect(l_tag($tag_type_id));
825 case 'add-tag-type': {
827 $tagname = $_POST['tagname'];
828 $min_role_id= intval( $_POST['min_role_id'] );
829 $description= $_POST['description'];
830 $category= $_POST['category'];
832 // make tag_type_fields dict
833 $tag_type_fields= array( "min_role_id" => $min_role_id,
834 "tagname" => $tagname,
835 "description" => $description,
836 "category" => $category,
840 $tag_type_id=$api->AddTagType( $tag_type_fields );
841 if ($tag_type_id > 0)
842 drupal_set_message ("tag type $tag_type_id created");
844 drupal_set_error ("Could not create tag type $tagname");
845 plc_redirect( l_tags());
849 case 'delete-tag-types': {
850 $tag_type_ids = $_POST['tag_type_ids'];
851 if ( ! $tag_type_ids) {
852 drupal_set_message("action=$action - No tag selected");
857 foreach ($tag_type_ids as $tag_type_id)
858 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
863 drupal_set_message ("Deleted $counter tag(s)");
865 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
866 plc_redirect (l_tags());
870 case 'remove-roles-from-tag-type' : {
871 $tag_type_id=$_POST['tag_type_id'];
872 $role_ids=$_POST['role_ids'];
874 drupal_set_error("You have not selected role(s) to remove");
876 foreach( $role_ids as $role_id)
877 if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 )
878 drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id");
880 plc_redirect (l_tag_roles($tag_type_id));
883 case 'add-role-to-tag-type' : {
884 $tag_type_id=$_POST['tag_type_id'];
885 $role_id=$_POST['role_id'];
887 drupal_set_error ("You have not selected a role to add");
888 } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) {
889 drupal_set_error("Could not add role $role_id to tag $tag_type_id");
891 plc_redirect (l_tag_roles($tag_type_id));
894 //////////////////////////////////////// tags
895 case 'set-tag-on-node':
896 case 'set-tag-on-interface': {
899 if ($action == 'set-tag-on-node') $node_mode=true;
902 $node_id = intval($_POST['node_id']);
904 $interface_id=intval($_POST['interface_id']);
905 $tag_type_id = intval($_POST['tag_type_id']);
906 $value = $_POST['value'];
908 $tag_types=$api->GetTagTypes(array($tag_type_id));
909 if (count ($tag_types) != 1) {
910 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
913 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
915 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
916 if ( count ($tags) == 1) {
919 $tag_id=$tag['node_tag_id'];
920 $result=$api->UpdateNodeTag($tag_id,$value);
922 $tag_id=$tag['interface_tag_id'];
923 $result=$api->UpdateInterfaceTag($tag_id,$value);
926 drupal_set_message ("Updated tag, new value = $value");
928 drupal_set_error ("Could not update tag");
931 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
933 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
935 drupal_set_message ("Created tag, new value = $value");
937 drupal_set_error ("Could not create tag");
942 plc_redirect (l_node_tags($node_id));
944 plc_redirect (l_interface_tags($interface_id));
947 case 'delete-node-tags' :
948 case 'delete-interface-tags' : {
951 if ($action == 'delete-node-tags') $node_mode=true;
954 $tag_ids=$_POST['node_tag_ids'];
956 $tag_ids=$_POST['interface_tag_ids'];
959 drupal_set_message("action=$action - No tag selected");
964 foreach( $tag_ids as $tag_id ) {
966 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
968 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
975 drupal_set_message ("Deleted $counter tag(s)");
977 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
979 plc_redirect(l_node_tags($_POST['node_id']));
981 plc_redirect(l_interface_tags($_POST['interface_id']));
984 //////////////////////////////////////// nodegroups
985 case 'update-nodegroup': {
986 $nodegroup_id = $_POST['nodegroup_id'];
987 $groupname = $_POST['groupname'];
988 $value = $_POST['value'];
991 $fields['groupname']=$groupname;
992 $fields['value']=$value;
993 if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1)
994 drupal_set_message ('Nodegroup updated');
996 drupal_set_error ("Could not update nodegroup $groupname");
998 plc_redirect(l_nodegroup($nodegroup_id));
1002 case 'add-nodegroup': {
1003 $groupname=$_POST['groupname'];
1004 if ( ! $groupname ) {
1005 drupal_set_error ('Empty groupname');
1006 plc_redirect (l_nodegroups());
1008 $tag_type_id=intval($_POST['tag_type_id']);
1009 if ( ! $tag_type_id ) {
1010 drupal_set_error ('You must select a tag in the dropdown list');
1011 plc_redirect (l_nodegroups());
1013 $value=$_POST['value'];
1015 drupal_set_message ("Empty value.. let's see ..");
1017 if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0)
1018 drupal_set_message ("Nodegroup $groupname created");
1020 drupal_set_error ("Could not create nodegroup $groupname");
1022 plc_redirect (l_nodegroups());
1026 case 'delete-nodegroups': {
1027 $nodegroup_ids=$_POST['nodegroup_ids'];
1028 if ( ! $nodegroup_ids ) {
1029 drupal_set_message("action=delete-nodegroups - No group selected");
1030 plc_redirect(l_nodegroups());
1034 foreach ($nodegroup_ids as $nodegroup_id)
1035 if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1)
1040 drupal_set_message ("Deleted $counter group(s)");
1042 drupal_set_error ("Could not delete all selected groups, only $counter were removed");
1043 plc_redirect (l_nodegroups());
1047 //////////////////////////////////////// leases
1048 case 'manage-leases': {
1049 $actions=json_decode($_POST['actions']);
1055 foreach ($actions as $action) {
1056 if ($action[0] == 'add-leases') {
1057 $nodenames=$action[1];
1058 $add_requested += count($nodenames);
1059 $slicename=$action[2];
1060 $t_from=intval($action[3]);
1061 $t_until=intval($action[4]);
1062 $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
1063 // update number of added leases
1064 $ids=$hash['new_ids'];
1065 $add_done += count($ids);
1066 // update global errors array
1067 foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
1068 } else if ($action[0]=='delete-leases') {
1069 $lease_id=intval($action[1]);
1070 $del_requested += 1;
1071 if ($api->DeleteLeases(array($lease_id)) == 1) {
1074 $errors []= "Could not delete lease " . $lease_id;
1077 $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1081 if (count($errors)==0) {
1082 echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1084 foreach ($errors as $error) echo($error. "\n");
1085 echo("Leases updated only partially (" .
1086 $add_done . "/" . $add_requested . " added and " .
1087 $del_done . "/" . $del_requested . " deleted)");
1093 ////////////////////////////////////////
1096 plc_debug('GET',$_GET);
1097 plc_debug('POST',$_POST);
1098 plc_debug('FILES',$_FILES);
1103 plc_error ("Unknown action $action in actions.php");