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'];
397 $ret = $api->RebootNodeWithPCU( $node_id );
398 $error= $api->error();
400 if( empty( $error ) ) {
401 drupal_set_message("Reboot node $hostname: $ret");
402 plc_redirect(l_node($node_id));
404 drupal_set_error($error);
410 //////////////////////////////////////////////////////////// interfaces
411 case 'delete-interfaces' : {
412 $interface_ids=$_POST['interface_ids'];
413 if ( ! $interface_ids) {
414 drupal_set_message("action=$action - No interface selected");
419 foreach( $interface_ids as $interface_id ) {
420 if ($api->DeleteInterface( intval( $interface_id )) != 1)
426 drupal_set_message ("Deleted $counter interface(s)");
428 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
429 plc_redirect(l_node($_POST['node_id']));
432 case 'new-interface': {
433 plc_redirect(l_interface_add($_POST['node_id']));
436 case 'add-interface': {
437 $node_id=intval($_POST['node_id']);
438 foreach ($interface_details as $field) {
439 $interface[$field]= $_POST[$field];
440 // these must be integers
441 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
442 if ( empty ($interface[$field]) )
443 unset ($interface[$field]);
445 $interface[$field]= intval( $interface[$field] );
448 $interface_id =$api->AddInterface( $node_id , $interface );
449 if ($interface_id <= 0 ) {
450 drupal_set_error ("Could not create interface");
451 drupal_set_error ($api->error());
453 $ip=$interface['ip'];
454 drupal_set_message ("Interface $ip added into node $node_id");
455 if ($_POST['is-virtual']) {
456 $ifname=$_POST['ifname'];
457 if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0)
458 drupal_set_error ("Could not set tag 'ifname'=$ifname");
460 drupal_set_message ("Set tag 'ifname'=$ifname");
461 $alias=$_POST['alias'];
462 // deafult to interface_id
463 if ( ! $alias ) $alias=strval($interface_id);
464 if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0)
465 drupal_set_error ("Could not set tag 'alias'=$alias");
467 drupal_set_message ("Set tag 'alias'=$alias");
470 plc_redirect (l_node_interfaces($node_id));
473 case 'update-interface': {
474 $interface_id=$_POST['interface_id'];
475 foreach ($interface_details as $field) {
476 $interface[$field]= $_POST[$field];
477 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
478 if ( intval($interface[$field]) != 0 ) {
479 $interface[$field]= intval( $interface[$field]);
480 } elseif ($field=='bwlimit' ) {
481 $interface[$field] = NULL;
485 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
487 drupal_set_message ("Interface $interface_id updated");
489 drupal_set_error ("Could not update interface: " . $api->error());
490 plc_redirect (l_interface($interface_id));
493 //////////////////////////////////////////////////////////// sites
494 case 'delete-site': {
495 $site_id = intval($_POST['site_id']);
496 if ($api->DeleteSite($site_id) ==1)
497 drupal_set_message ("Site $site_id deleted");
499 drupal_set_error("Failed to delete site $site_id");
500 plc_redirect (l_sites());
504 case 'expire-all-slices-in-site': {
506 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
509 //// old code from sites/expire.php
510 $sites = $api->GetSites( array( intval( $site_id )));
512 // xxx why not 'now?'
513 $expiration= strtotime( $_POST['expires'] );
514 // loop through all slices for site
515 foreach ($site['slice_ids'] as $slice_id) {
516 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
518 // update site to not allow slice creation or renewal
519 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
520 plc_redirect (l_site($site_id));
524 case 'update-site': {
525 $site_id=intval($_POST['site_id']);
526 $name= $_POST['name'];
527 $abbreviated_name= $_POST['abbreviated_name'];
529 $latitude= floatval($_POST['latitude']);
530 $longitude= floatval($_POST['longitude']);
531 //$max_slivers= $_POST['max_slivers'];
533 $fields= array( "name" => $name,
534 "abbreviated_name" => $abbreviated_name,
536 "latitude" => floatval( $latitude ),
537 "longitude" => floatval( $longitude ));
539 if ($_POST['login_base'])
540 $fields['login_base'] = $_POST['login_base'];
541 if (isset($_POST['max_slices']))
542 $fields['max_slices'] = intval($_POST['max_slices']);
543 if (isset($_POST['enabled'])) {
544 $fields['enabled'] = (bool)$_POST['enabled'];
547 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
549 drupal_set_message("Site $name updated");
551 drupal_set_error ("Could not update site $site_id");
553 plc_redirect(l_site($site_id));
557 //////////////////////////////////////////////////////////// slices
558 case 'delete-slice': {
559 $slice_id = $_POST['slice_id'];
560 if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
561 drupal_set_message("Slice $slice_id deleted");
562 plc_redirect(l_slices());
564 drupal_set_error("Could not delete slice $slice_id " . $api->error());
569 case 'update-slice': {
570 $slice_id = $_POST['slice_id'];
571 $name = $_POST['name'];
572 $description= $_POST['description'];
575 $fields= array( "description"=>$description, "url"=>$url );
576 $api->UpdateSlice( intval( $slice_id ), $fields );
577 $error= $api->error();
579 if( empty( $error ) ) {
580 drupal_set_message("Update slice $name");
581 plc_redirect(l_slice($slice_id));
583 drupal_set_error($error);
588 case 'renew-slice': {
589 $slice_id = intval ($_POST['slice_id']);
590 $expires = intval ($_POST['expires']);
596 $MAX_FUTURE=$WEEKS*$WEEK;
597 if ( ($expires-$now) > $MAX_FUTURE) {
598 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
599 plc_redirect(l_slice($slice_id));
601 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
602 drupal_set_message("Slice renewed");
604 drupal_set_error("Could not update slice $slice_id");
605 plc_redirect(l_slice($slice_id));
609 case 'remove-persons-from-slice': {
610 $slice_id = intval ($_POST['slice_id']);
611 $person_ids = $_POST['person_ids'];
614 $tmp_slices = $api->GetSlices($slice_id, array("name"));
615 if (count($tmp_slices) > 0) {
616 $tmp_slice = $tmp_slices[0];
617 $slice_name = $tmp_slice["name"];
619 $notify_subject = "Removed from slice: " . $slice_name;
620 $notify_body = sprintf("You have been removed from the slice %s.
622 Our support team will be glad to answer any question that you might have.
624 $notify_person_ids = array();
628 foreach( $person_ids as $person_id ) {
629 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
632 array_push($notify_person_ids, intval($person_id));
637 $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
638 drupal_set_message ("Deleted $counter person(s)");
641 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
642 plc_redirect(l_slice($slice_id) . " &show_persons=true");
646 case 'add-persons-in-slice': {
647 $slice_id = intval ($_POST['slice_id']);
648 $person_ids = $_POST['person_ids'];
651 $tmp_slices = $api->GetSlices($slice_id, array("name"));
652 if (count($tmp_slices) > 0) {
653 $tmp_slice = $tmp_slices[0];
654 $slice_name = $tmp_slice["name"];
656 $notify_subject = "Added to slice: " . $slice_name;
657 $notify_body = sprintf("You have been added to the slice %s as a user.
659 You can go to your slice page following the link below:
660 https://%s:%d/db/slices/index.php?id=%d
662 Our support team will be glad to answer any question that you might have.
663 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
664 $notify_person_ids = array();
668 foreach ($person_ids as $person_id) {
669 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
672 array_push($notify_person_ids, intval($person_id));
677 $api->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
678 drupal_set_message ("Added $counter person(s)");
681 drupal_set_error ("Could not add all selected persons, only $counter were added");
682 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
686 case 'remove-nodes-from-slice': {
687 $slice_id = intval ($_POST['slice_id']);
688 $node_ids = array_map("intval",$_POST['node_ids']);
689 $count=count($node_ids);
691 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
692 drupal_set_message ("Removed $count node(s)");
694 drupal_set_error ("Could not remove selected nodes");
695 plc_redirect(l_slice_nodes($slice_id));
699 case 'add-nodes-in-slice': {
700 $slice_id = intval ($_POST['slice_id']);
701 $node_ids = array_map("intval",$_POST['node_ids']);
702 $count=count($node_ids);
703 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
704 drupal_set_message ("Added $count node(s)");
706 drupal_set_error ("Could not add all selected nodes");
707 plc_redirect(l_slice_nodes($slice_id));
711 case 'update-initscripts': {
712 // expects: slice_id & name & previous-initscript & previous-initscript-code
713 // & initscript & initscript-code
714 $slice_id = intval ($_POST['slice_id']);
715 $previous_initscript=$_POST['previous-initscript'];
716 $initscript=$_POST['initscript'];
717 $previous_initscript_code=html_entity_decode($_POST['previous-initscript-code']);
718 $initscript_code=$_POST['initscript-code'];
721 if (strcmp($initscript,$previous_initscript) != 0) {
722 $newvalue=$api->SetSliceInitscript($slice_id,$initscript);
723 $status = (strcmp($newvalue,$initscript)==0) ? "OK" : "failed";
724 if (! $initscript) drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
725 else drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
729 // somehow some \r chars make it here; just ignore them
730 $previous_initscript_code=str_replace("\r","",$previous_initscript_code);
731 // plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
733 $initscript_code=str_replace("\r","",$initscript_code);
734 // make sure the script ends with a single \n
735 $initscript_code=trim($initscript_code);
736 if (!empty($initscript_code) && $initscript_code[strlen($initscript_code)-1] != "\n")
737 $initscript_code.="\n";
738 // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
740 if (strcmp($initscript_code,$previous_initscript_code) != 0) {
741 $newvalue=$api->SetSliceInitscriptCode($slice_id,$initscript_code);
742 // plc_debug_txt('newvalue',$newvalue);
743 $status=(strcmp($newvalue,$initscript_code)==0) ? "OK" : "failed";
744 if (! $initscript_code) drupal_set_message("Removed initscript code " . $status);
745 else drupal_set_message("Installed new initscript code " . $status);
748 if (!$changes) drupal_set_message("No changes required in initscript");
749 plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1" );
754 case 'delete-slice-tags': {
755 $slice_id = intval($_POST['slice_id']);
756 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
759 foreach($slice_tag_ids as $slice_tag_id) {
760 if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
762 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
767 drupal_set_message ("Deleted $count slice tag(s)");
768 plc_redirect(l_slice($slice_id) . "&show_tags=1" );
772 case 'add-slice-tag': {
773 $slice_id = intval($_POST['slice_id']);
774 $tag_type_id = intval($_POST['tag_type_id']);
775 $value = $_POST['value'];
776 $node_id = intval($_POST['node_id']);
777 $nodegroup_id = intval($_POST['nodegroup_id']);
781 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
782 } elseif ($nodegroup_id) {
783 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
785 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
788 drupal_set_message ("Added slice tag.");
790 drupal_set_error("Could not add slice tag");
791 if ($_POST['sliver_action'])
792 plc_redirect(l_sliver($node_id,$slice_id));
794 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
798 //////////////////////////////////////////////////////////// tag types
800 case 'update-tag-type': {
802 $tag_type_id= intval( $_POST['tag_type_id'] );
803 $tagname = $_POST['tagname'];
804 $min_role_id= intval( $_POST['min_role_id'] );
805 $description= $_POST['description'];
806 $category= $_POST['category'];
808 // make tag_type_fields dict
809 $tag_type_fields= array( "min_role_id" => $min_role_id,
810 "tagname" => $tagname,
811 "description" => $description,
812 "category" => $category,
815 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
816 drupal_set_message ("Tag type $tagname updated");
818 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
819 plc_redirect(l_tag($tag_type_id));
823 case 'add-tag-type': {
825 $tagname = $_POST['tagname'];
826 $min_role_id= intval( $_POST['min_role_id'] );
827 $description= $_POST['description'];
828 $category= $_POST['category'];
830 // make tag_type_fields dict
831 $tag_type_fields= array( "min_role_id" => $min_role_id,
832 "tagname" => $tagname,
833 "description" => $description,
834 "category" => $category,
838 $tag_type_id=$api->AddTagType( $tag_type_fields );
839 if ($tag_type_id > 0)
840 drupal_set_message ("tag type $tag_type_id created");
842 drupal_set_error ("Could not create tag type $tagname");
843 plc_redirect( l_tags());
847 case 'delete-tag-types': {
848 $tag_type_ids = $_POST['tag_type_ids'];
849 if ( ! $tag_type_ids) {
850 drupal_set_message("action=$action - No tag selected");
855 foreach ($tag_type_ids as $tag_type_id)
856 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
861 drupal_set_message ("Deleted $counter tag(s)");
863 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
864 plc_redirect (l_tags());
868 case 'remove-roles-from-tag-type' : {
869 $tag_type_id=$_POST['tag_type_id'];
870 $role_ids=$_POST['role_ids'];
872 drupal_set_error("You have not selected role(s) to remove");
874 foreach( $role_ids as $role_id)
875 if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 )
876 drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id");
878 plc_redirect (l_tag_roles($tag_type_id));
881 case 'add-role-to-tag-type' : {
882 $tag_type_id=$_POST['tag_type_id'];
883 $role_id=$_POST['role_id'];
885 drupal_set_error ("You have not selected a role to add");
886 } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) {
887 drupal_set_error("Could not add role $role_id to tag $tag_type_id");
889 plc_redirect (l_tag_roles($tag_type_id));
892 //////////////////////////////////////// tags
893 case 'set-tag-on-node':
894 case 'set-tag-on-interface': {
897 if ($action == 'set-tag-on-node') $node_mode=true;
900 $node_id = intval($_POST['node_id']);
902 $interface_id=intval($_POST['interface_id']);
903 $tag_type_id = intval($_POST['tag_type_id']);
904 $value = $_POST['value'];
906 $tag_types=$api->GetTagTypes(array($tag_type_id));
907 if (count ($tag_types) != 1) {
908 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
911 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
913 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
914 if ( count ($tags) == 1) {
917 $tag_id=$tag['node_tag_id'];
918 $result=$api->UpdateNodeTag($tag_id,$value);
920 $tag_id=$tag['interface_tag_id'];
921 $result=$api->UpdateInterfaceTag($tag_id,$value);
924 drupal_set_message ("Updated tag, new value = $value");
926 drupal_set_error ("Could not update tag");
929 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
931 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
933 drupal_set_message ("Created tag, new value = $value");
935 drupal_set_error ("Could not create tag");
940 plc_redirect (l_node_tags($node_id));
942 plc_redirect (l_interface_tags($interface_id));
945 case 'delete-node-tags' :
946 case 'delete-interface-tags' : {
949 if ($action == 'delete-node-tags') $node_mode=true;
952 $tag_ids=$_POST['node_tag_ids'];
954 $tag_ids=$_POST['interface_tag_ids'];
957 drupal_set_message("action=$action - No tag selected");
962 foreach( $tag_ids as $tag_id ) {
964 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
966 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
973 drupal_set_message ("Deleted $counter tag(s)");
975 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
977 plc_redirect(l_node_tags($_POST['node_id']));
979 plc_redirect(l_interface_tags($_POST['interface_id']));
982 //////////////////////////////////////// nodegroups
983 case 'update-nodegroup': {
984 $nodegroup_id = $_POST['nodegroup_id'];
985 $groupname = $_POST['groupname'];
986 $value = $_POST['value'];
989 $fields['groupname']=$groupname;
990 $fields['value']=$value;
991 if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1)
992 drupal_set_message ('Nodegroup updated');
994 drupal_set_error ("Could not update nodegroup $groupname");
996 plc_redirect(l_nodegroup($nodegroup_id));
1000 case 'add-nodegroup': {
1001 $groupname=$_POST['groupname'];
1002 if ( ! $groupname ) {
1003 drupal_set_error ('Empty groupname');
1004 plc_redirect (l_nodegroups());
1006 $tag_type_id=intval($_POST['tag_type_id']);
1007 if ( ! $tag_type_id ) {
1008 drupal_set_error ('You must select a tag in the dropdown list');
1009 plc_redirect (l_nodegroups());
1011 $value=$_POST['value'];
1013 drupal_set_message ("Empty value.. let's see ..");
1015 if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0)
1016 drupal_set_message ("Nodegroup $groupname created");
1018 drupal_set_error ("Could not create nodegroup $groupname");
1020 plc_redirect (l_nodegroups());
1024 case 'delete-nodegroups': {
1025 $nodegroup_ids=$_POST['nodegroup_ids'];
1026 if ( ! $nodegroup_ids ) {
1027 drupal_set_message("action=delete-nodegroups - No group selected");
1028 plc_redirect(l_nodegroups());
1032 foreach ($nodegroup_ids as $nodegroup_id)
1033 if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1)
1038 drupal_set_message ("Deleted $counter group(s)");
1040 drupal_set_error ("Could not delete all selected groups, only $counter were removed");
1041 plc_redirect (l_nodegroups());
1045 //////////////////////////////////////// leases
1046 case 'manage-leases': {
1047 $actions=json_decode($_POST['actions']);
1053 foreach ($actions as $action) {
1054 if ($action[0] == 'add-leases') {
1055 $nodenames=$action[1];
1056 $add_requested += count($nodenames);
1057 $slicename=$action[2];
1058 $t_from=intval($action[3]);
1059 $t_until=intval($action[4]);
1060 $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
1061 // update number of added leases
1062 $ids=$hash['new_ids'];
1063 $add_done += count($ids);
1064 // update global errors array
1065 foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
1066 } else if ($action[0]=='delete-leases') {
1067 $lease_id=intval($action[1]);
1068 $del_requested += 1;
1069 if ($api->DeleteLeases(array($lease_id)) == 1) {
1072 $errors []= "Could not delete lease " . $lease_id;
1075 $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1079 if (count($errors)==0) {
1080 echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1082 foreach ($errors as $error) echo($error. "\n");
1083 echo("Leases updated only partially (" .
1084 $add_done . "/" . $add_requested . " added and " .
1085 $del_done . "/" . $del_requested . " deleted)");
1091 ////////////////////////////////////////
1094 plc_debug('GET',$_GET);
1095 plc_debug('POST',$_POST);
1096 plc_debug('FILES',$_FILES);
1101 plc_error ("Unknown action $action in actions.php");