4 require_once 'plc_login.php';
6 // Get session and API handles
7 require_once 'plc_session.php';
8 require_once 'plc_api.php';
9 global $plc, $api, $adm;
12 require_once 'plc_drupal.php';
15 require_once 'plc_functions.php';
17 $known_actions=array();
18 ////////////////////////////////////////////////////////////
21 // (*) set 'action' to one of the following
22 //////////////////////////////////////// persons
23 $known_actions []= "add-person-to-site";
24 // expects: person_id & site_id
25 $known_actions []= "remove-person-from-sites";
26 // expects: person_id & site_ids
27 $known_actions []= "remove-roles-from-person";
28 // expects: person_id & role_ids
29 $known_actions []= "add-role-to-person";
30 // expects: role_person_id & id
31 $known_actions []= "enable-person";
33 $known_actions []= "disable-person";
35 $known_actions []= "become-person";
37 $known_actions []= "delete-person";
39 $known_actions []= "delete-keys";
40 // expects: key_ids & person_id (for redirecting to the person's page)
41 $known_actions []= "upload-key";
42 // expects: person_id & $_FILES['key']
43 $known_actions []= "update-person";
44 // expects: person_id & first_name last_name title email phone url bio + [password1 password2]
46 //////////////////////////////////////// nodes
47 $known_actions []= "node-boot-state";
48 // expects: node_id boot_state
49 $known_actions []= "delete-node";
51 $known_actions []= "update-node";
52 // expects: node_id, hostname, model, node_type
53 $known_actions []= "attach-pcu";
54 // expects: node_id, pcu_id, port (pcu_id <0 means detach)
55 $known_actions []= "reboot-node-with-pcu";
58 //////////////////////////////////////// interfaces
59 $known_actions []= "delete-interfaces";
60 // expects: interface_ids
61 $known_actions []="add-interface";
62 // expects: node_id & interface details
63 $known_actions []="new-interface";
65 $known_actions []="update-interface";
66 // expects: interface_id & interface details
68 //////////////////////////////////////// sites
69 $known_actions []= "delete-site";
71 $known_actions []= "expire-all-slices-in-site";
73 $known_actions []= "update-site";
74 // expects: site_id & name abbreviated_name url latitude longitude [login_base max_slices]
76 //////////////////////////////////////// slices
77 $known_actions []= "delete-slice";
79 $known_actions []= "update-slice";
80 // expects: slice_id, name, description, url
81 $known_actions []= "renew-slice";
82 // expects: slice_id & expires
83 $known_actions []= 'remove-persons-from-slice';
84 // expects: slice_id & person_ids
85 $known_actions []= 'add-persons-in-slice';
86 // expects: slice_id & person_ids
87 $known_actions []= 'remove-nodes-from-slice';
88 // expects: slice_id & node_ids
89 $known_actions []= 'add-nodes-in-slice';
90 // expects: slice_id & node_ids
91 $known_actions []= 'update-initscripts';
92 // expects: slice_id & name & previous-initscript & previous-initscript-code
93 // & initscript & initscript-code
94 $known_actions []= 'delete-slice-tags';
95 // expects: slice_tag_id
96 $known_actions []= 'add-slice-tag';
97 // expects: slice_id & tag_type_id & node_id & nodegroup_id
99 //////////////////////////////////////// tag types
100 $known_actions []= "update-tag-type";
101 // expects: tag_type_id & name & description & category & min_role_id
102 $known_actions []= "add-tag-type";
103 // expects: tag_type_id & tagname & description & category & min_role_id
104 $known_actions []= "delete-tag-types";
105 // expects: tag_type_ids
106 $known_actions []= "remove-roles-from-tag-type";
107 // expects: tag_type_id & role_ids
108 $known_actions []= "add-role-to-tag-type";
109 // expects: tag_type_id_id & id
111 //////////////////////////////////////// tags
112 $known_actions []= "set-tag-on-node";
113 // expects: node_id tagname value
114 $known_actions []= "set-tag-on-interface";
115 // expects: interface_id tagname value
116 $known_actions []= "delete-node-tags";
117 // expects: node_id & node_tag_ids
118 $known_actions []= "delete-interface-tags";
119 // expects: interface_id & interface_tag_ids
121 //////////////////////////////////////// nodegroups
122 $known_actions []= "update-nodegroup";
123 // expects nodegroup_id groupname value
124 $known_actions []= "add-nodegroup";
125 // expects groupname, tag_type_id, value
126 $known_actions []= 'delete-nodegroups';
127 // expects nodegroup_ids
129 //////////////////////////////////////// leases
130 $known_actions []= "manage-leases";
131 // expects as 'actions' a list of 'action' of the form
132 // either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
133 // or [ 'delete-leases', lease_id ]
135 ////////////////////////////////////////////////////////////
136 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
137 'broadcast', 'netmask', 'dns1', 'dns2',
138 'hostname', 'mac', 'bwlimit' );
140 //////////////////////////////
141 // sometimes we don't set 'action', but use the submit button name instead
142 // so if 'action' not set, see if $_POST has one of the actions as a key
143 if ($_POST['action'])
144 $action=$_POST['action'];
146 foreach ($known_actions as $known_action)
147 if ($_POST[$known_action]) {
148 $action=$known_action;
152 //uncomment for debugging incoming data
155 $person_id = $_POST['person_id']; // usually needed
158 drupal_set_message ("actions.php: action not set or not in known_actions");
159 plc_debug('POST',$_POST);
165 case 'add-person-to-site': {
166 $site_id = $_POST['site_id'];
167 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
168 plc_redirect (l_person($person_id));
171 case 'remove-person-from-sites': {
172 $site_ids = $_POST['site_ids'];
174 drupal_set_message("action=$action - No site selected");
177 foreach ( $site_ids as $site_id ) {
178 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
180 plc_redirect (l_person($person_id));
183 case 'remove-roles-from-person' : {
184 $role_ids=$_POST['role_ids'];
186 drupal_set_error("You have not selected role(s) to remove");
188 foreach( $role_ids as $role_id)
189 if ( $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) ) != 1 )
190 drupal_set_error ("Could not remove role $role_id from person $person_id");
192 plc_redirect (l_person_roles($person_id));
195 case 'add-role-to-person' : {
196 $role_id=$_POST['role_id'];
198 drupal_set_error ("You have not selected a role to add");
199 } else if ($api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) ) != 1) {
200 drupal_set_error("Could not add role $role_id to person $person_id");
202 plc_redirect (l_person_roles($person_id));
205 case 'enable-person' : {
206 $fields = array( "enabled"=>true );
207 $api->UpdatePerson( intval( $person_id ), $fields );
208 plc_redirect (l_person($person_id));
211 case 'disable-person' : {
212 $fields = array( "enabled"=>false );
213 $api->UpdatePerson( intval( $person_id ), $fields );
214 plc_redirect (l_person($person_id));
217 case 'become-person' : {
218 $plc->BecomePerson (intval($person_id));
219 plc_redirect (l_person(intval($person_id)));
222 case 'delete-person' : {
223 $api->DeletePerson( intval( $person_id ) );
224 plc_redirect (l_persons());
227 case 'delete-keys' : {
228 $key_ids=$_POST['key_ids'];
230 drupal_set_message("action=$action - No key selected");
235 foreach( $key_ids as $key_id ) {
236 if ($api->DeleteKey( intval( $key_id )) != 1)
242 drupal_set_message ("Deleted $counter key(s)");
244 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
245 plc_redirect(l_person($person_id));
249 case 'upload-key' : {
250 if ( ! isset( $_FILES['key'] ) ) {
251 drupal_set_message ("action=$action, no key file set");
255 $key_file= $_FILES['key']['tmp_name'];
257 plc_error("Please select a valid SSH key file to upload");
260 $fp = fopen( $key_file, "r" );
263 plc_error("Unable to open key file $key_file");
266 // opened the key file, read the one line of contents
267 // The POST operation always creates a file even if the filename
268 // the user specified was garbage. If there was some problem
269 // with the source file, we'll get a zero length read here.
270 $key = fread($fp, filesize($key_file));
273 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
276 drupal_set_message ("New key added");
278 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
280 plc_redirect(l_person($person_id));
283 case 'update-person': {
284 $person_id=$_POST['person_id'];
285 // attempt to update this person
286 $first_name= $_POST['first_name'];
287 $last_name= $_POST['last_name'];
288 $title= $_POST['title'];
289 $email= $_POST['email'];
290 $phone= $_POST['phone'];
292 $bio= str_replace("\r", "", $_POST['bio']);
293 $password1= $_POST['password1'];
294 $password2= $_POST['password2'];
296 if( $password1 != $password2 ) {
297 drupal_set_error ("The passwords do not match");
298 plc_redirect(l_person($person_id));
302 $fields['first_name']= $first_name;
303 $fields['last_name']= $last_name;
304 $fields['title']= $title;
305 $fields['email']= $email;
306 $fields['phone']= $phone;
307 $fields['url']= $url;
308 $fields['bio']= $bio;
310 if ( $password1 != "" )
311 $fields['password']= $password1;
313 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
314 drupal_set_message("$first_name $last_name updated");
316 drupal_set_error ("Could not update person $person_id" . $api->error());
318 plc_redirect(l_person($person_id));
322 //////////////////////////////////////////////////////////// nodes
323 case 'node-boot-state': {
324 $node_id=intval($_POST['node_id']);
325 $boot_state=$_POST['boot_state'];
326 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
328 drupal_set_message("boot state updated");
329 plc_redirect (l_node($node_id));
331 drupal_set_error("Could not set boot_state '$boot_state'");
336 case 'delete-node': {
337 $node_id=intval($_POST['node_id']);
338 $result=$api->DeleteNode( intval( $node_id ) );
340 drupal_set_message("Node $node_id deleted");
341 plc_redirect (l_nodes());
343 drupal_set_error ("Could not delete node $node_id");
348 case 'update-node': {
349 $node_id=intval($_POST['node_id']);
350 $hostname= $_POST['hostname'];
351 $model= $_POST['model'];
352 $node_type= $_POST['node_type'];
354 $fields= array( "hostname"=>$hostname, "model"=>$model, "node_type"=>$node_type );
355 $api->UpdateNode( $node_id, $fields );
356 $error= $api->error();
358 if( empty( $error ) ) {
359 drupal_set_message("Update node $hostname");
360 plc_redirect(l_node($node_id));
362 drupal_set_error($error);
367 // this code will ensure that at most one PCU gets attached to the node
369 $node_id=intval($_POST['node_id']);
370 $pcu_id=intval($_POST['pcu_id']);
371 $port=intval($_POST['port']);
372 // always start with deleting former PCUs
373 $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
374 $former_pcu_ids = $nodes[0]['pcu_ids'];
375 if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
376 if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1)
377 drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
379 drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
381 // re-attach only if provided pcu_id >=0
383 if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
384 drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
386 drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
388 drupal_set_message ('Detached node from all PCUs');
391 plc_redirect(l_node($node_id));
395 case 'reboot-node-with-pcu': {
396 $node_id=intval($_POST['node_id']);
397 $hostname= $_POST['hostname'];
398 $test = $_POST['test'];
399 settype($test, "boolean");
401 $ret = $api->RebootNodeWithPCU( $node_id, $test );
402 $error= $api->error();
404 if( empty( $error ) ) {
405 drupal_set_message("Reboot node $hostname: $ret");
406 plc_redirect(l_node($node_id));
408 drupal_set_error($error);
414 //////////////////////////////////////////////////////////// interfaces
415 case 'delete-interfaces' : {
416 $interface_ids=$_POST['interface_ids'];
417 if ( ! $interface_ids) {
418 drupal_set_message("action=$action - No interface selected");
423 foreach( $interface_ids as $interface_id ) {
424 if ($api->DeleteInterface( intval( $interface_id )) != 1)
430 drupal_set_message ("Deleted $counter interface(s)");
432 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
433 plc_redirect(l_node($_POST['node_id']));
436 case 'new-interface': {
437 plc_redirect(l_interface_add($_POST['node_id']));
440 case 'add-interface': {
441 $node_id=intval($_POST['node_id']);
442 foreach ($interface_details as $field) {
443 $interface[$field]= $_POST[$field];
444 // these must be integers
445 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
446 if ( empty ($interface[$field]) )
447 unset ($interface[$field]);
449 $interface[$field]= intval( $interface[$field] );
452 $interface_id =$api->AddInterface( $node_id , $interface );
453 if ($interface_id <= 0 ) {
454 drupal_set_error ("Could not create interface");
455 drupal_set_error ($api->error());
457 $ip=$interface['ip'];
458 drupal_set_message ("Interface $ip added into node $node_id");
459 if ($_POST['is-virtual']) {
460 $ifname=$_POST['ifname'];
461 if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0)
462 drupal_set_error ("Could not set tag 'ifname'=$ifname");
464 drupal_set_message ("Set tag 'ifname'=$ifname");
465 $alias=$_POST['alias'];
466 // deafult to interface_id
467 if ( ! $alias ) $alias=strval($interface_id);
468 if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0)
469 drupal_set_error ("Could not set tag 'alias'=$alias");
471 drupal_set_message ("Set tag 'alias'=$alias");
474 plc_redirect (l_node_interfaces($node_id));
477 case 'update-interface': {
478 $interface_id=$_POST['interface_id'];
479 foreach ($interface_details as $field) {
480 $interface[$field]= $_POST[$field];
481 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
482 if ( intval($interface[$field]) != 0 ) {
483 $interface[$field]= intval( $interface[$field]);
484 } elseif ($field=='bwlimit' ) {
485 $interface[$field] = NULL;
489 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
491 drupal_set_message ("Interface $interface_id updated");
493 drupal_set_error ("Could not update interface: " . $api->error());
494 plc_redirect (l_interface($interface_id));
497 //////////////////////////////////////////////////////////// sites
498 case 'delete-site': {
499 $site_id = intval($_POST['site_id']);
500 if ($api->DeleteSite($site_id) ==1)
501 drupal_set_message ("Site $site_id deleted");
503 drupal_set_error("Failed to delete site $site_id");
504 plc_redirect (l_sites());
508 case 'expire-all-slices-in-site': {
510 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
513 //// old code from sites/expire.php
514 $sites = $api->GetSites( array( intval( $site_id )));
516 // xxx why not 'now?'
517 $expiration= strtotime( $_POST['expires'] );
518 // loop through all slices for site
519 foreach ($site['slice_ids'] as $slice_id) {
520 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
522 // update site to not allow slice creation or renewal
523 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
524 plc_redirect (l_site($site_id));
528 case 'update-site': {
529 $site_id=intval($_POST['site_id']);
530 $name= $_POST['name'];
531 $abbreviated_name= $_POST['abbreviated_name'];
533 $latitude= floatval($_POST['latitude']);
534 $longitude= floatval($_POST['longitude']);
535 //$max_slivers= $_POST['max_slivers'];
537 $fields= array( "name" => $name,
538 "abbreviated_name" => $abbreviated_name,
540 "latitude" => floatval( $latitude ),
541 "longitude" => floatval( $longitude ));
543 if ($_POST['login_base'])
544 $fields['login_base'] = $_POST['login_base'];
545 if (isset($_POST['max_slices']))
546 $fields['max_slices'] = intval($_POST['max_slices']);
547 if (isset($_POST['enabled'])) {
548 $fields['enabled'] = (bool)$_POST['enabled'];
551 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
553 drupal_set_message("Site $name updated");
555 drupal_set_error ("Could not update site $site_id");
557 plc_redirect(l_site($site_id));
561 //////////////////////////////////////////////////////////// slices
562 case 'delete-slice': {
563 $slice_id = $_POST['slice_id'];
564 if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
565 drupal_set_message("Slice $slice_id deleted");
566 plc_redirect(l_slices());
568 drupal_set_error("Could not delete slice $slice_id " . $api->error());
573 case 'update-slice': {
574 $slice_id = $_POST['slice_id'];
575 $name = $_POST['name'];
576 $description= $_POST['description'];
579 $fields= array( "description"=>$description, "url"=>$url );
580 $api->UpdateSlice( intval( $slice_id ), $fields );
581 $error= $api->error();
583 if( empty( $error ) ) {
584 drupal_set_message("Update slice $name");
585 plc_redirect(l_slice($slice_id));
587 drupal_set_error($error);
592 case 'renew-slice': {
593 $slice_id = intval ($_POST['slice_id']);
594 $expires = intval ($_POST['expires']);
600 $MAX_FUTURE=$WEEKS*$WEEK;
601 if ( ($expires-$now) > $MAX_FUTURE) {
602 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
603 plc_redirect(l_slice($slice_id));
605 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
606 drupal_set_message("Slice renewed");
608 drupal_set_error("Could not update slice $slice_id");
609 plc_redirect(l_slice($slice_id));
613 case 'remove-persons-from-slice': {
614 $slice_id = intval ($_POST['slice_id']);
615 $person_ids = $_POST['person_ids'];
618 $tmp_slices = $api->GetSlices($slice_id, array("name"));
619 if (count($tmp_slices) > 0) {
620 $tmp_slice = $tmp_slices[0];
621 $slice_name = $tmp_slice["name"];
623 $notify_subject = "Removed from slice: " . $slice_name;
624 $notify_body = sprintf("You have been removed from the slice %s.
626 Our support team will be glad to answer any question that you might have.
628 $notify_person_ids = array();
632 foreach( $person_ids as $person_id ) {
633 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
636 array_push($notify_person_ids, intval($person_id));
641 $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
642 drupal_set_message ("Deleted $counter person(s)");
645 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
646 plc_redirect(l_slice($slice_id) . " &show_persons=true");
650 case 'add-persons-in-slice': {
651 $slice_id = intval ($_POST['slice_id']);
652 $person_ids = $_POST['person_ids'];
655 $tmp_slices = $api->GetSlices($slice_id, array("name"));
656 if (count($tmp_slices) > 0) {
657 $tmp_slice = $tmp_slices[0];
658 $slice_name = $tmp_slice["name"];
660 $notify_subject = "Added to slice: " . $slice_name;
661 $notify_body = sprintf("You have been added to the slice %s as a user.
663 You can go to your slice page following the link below:
664 https://%s:%d/db/slices/index.php?id=%d
666 Our support team will be glad to answer any question that you might have.
667 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
668 $notify_person_ids = array();
672 foreach ($person_ids as $person_id) {
673 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
676 array_push($notify_person_ids, intval($person_id));
681 $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
682 drupal_set_message ("Added $counter person(s)");
685 drupal_set_error ("Could not add all selected persons, only $counter were added");
686 plc_redirect(l_slice($slice_id) . "&show_persons=true" );
690 case 'remove-nodes-from-slice': {
691 $slice_id = intval ($_POST['slice_id']);
692 $node_ids = array_map("intval",$_POST['node_ids']);
693 $count=count($node_ids);
695 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
696 drupal_set_message ("Removed $count node(s)");
698 drupal_set_error ("Could not remove selected nodes");
699 plc_redirect(l_slice_nodes($slice_id));
703 case 'add-nodes-in-slice': {
704 $slice_id = intval ($_POST['slice_id']);
705 $node_ids = array_map("intval",$_POST['node_ids']);
706 $count=count($node_ids);
707 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
708 drupal_set_message ("Added $count node(s)");
710 drupal_set_error ("Could not add all selected nodes");
711 plc_redirect(l_slice_nodes($slice_id));
715 case 'update-initscripts': {
716 // expects: slice_id & name & previous-initscript & previous-initscript-code
717 // & initscript & initscript-code
718 $slice_id = intval ($_POST['slice_id']);
719 $previous_initscript=$_POST['previous-initscript'];
720 $initscript=$_POST['initscript'];
721 $previous_initscript_code=html_entity_decode($_POST['previous-initscript-code']);
722 $initscript_code=$_POST['initscript-code'];
725 if (strcmp($initscript,$previous_initscript) != 0) {
726 $newvalue=$api->SetSliceInitscript($slice_id,$initscript);
727 $status = (strcmp($newvalue,$initscript)==0) ? "OK" : "failed";
728 if (! $initscript) drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
729 else drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
733 // somehow some \r chars make it here; just ignore them
734 $previous_initscript_code=str_replace("\r","",$previous_initscript_code);
735 // plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
737 $initscript_code=str_replace("\r","",$initscript_code);
738 // make sure the script ends with a single \n
739 $initscript_code=trim($initscript_code);
740 if (!empty($initscript_code) && $initscript_code[strlen($initscript_code)-1] != "\n")
741 $initscript_code.="\n";
742 // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
744 if (strcmp($initscript_code,$previous_initscript_code) != 0) {
745 $newvalue=$api->SetSliceInitscriptCode($slice_id,$initscript_code);
746 // plc_debug_txt('newvalue',$newvalue);
747 $status=(strcmp($newvalue,$initscript_code)==0) ? "OK" : "failed";
748 if (! $initscript_code) drupal_set_message("Removed initscript code " . $status);
749 else drupal_set_message("Installed new initscript code " . $status);
752 if (!$changes) drupal_set_message("No changes required in initscript");
753 plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1" );
758 case 'delete-slice-tags': {
759 $slice_id = intval($_POST['slice_id']);
760 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
763 foreach($slice_tag_ids as $slice_tag_id) {
764 if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
766 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
771 drupal_set_message ("Deleted $count slice tag(s)");
772 plc_redirect(l_slice($slice_id) . "&show_tags=1" );
776 case 'add-slice-tag': {
777 $slice_id = intval($_POST['slice_id']);
778 $tag_type_id = intval($_POST['tag_type_id']);
779 $value = $_POST['value'];
780 $node_id = intval($_POST['node_id']);
781 $nodegroup_id = intval($_POST['nodegroup_id']);
785 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
786 } elseif ($nodegroup_id) {
787 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
789 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
792 drupal_set_message ("Added slice tag.");
794 drupal_set_error("Could not add slice tag");
795 if ($_POST['sliver_action'])
796 plc_redirect(l_sliver($node_id,$slice_id));
798 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
802 //////////////////////////////////////////////////////////// tag types
804 case 'update-tag-type': {
806 $tag_type_id= intval( $_POST['tag_type_id'] );
807 $tagname = $_POST['tagname'];
808 $min_role_id= intval( $_POST['min_role_id'] );
809 $description= $_POST['description'];
810 $category= $_POST['category'];
812 // make tag_type_fields dict
813 $tag_type_fields= array( "min_role_id" => $min_role_id,
814 "tagname" => $tagname,
815 "description" => $description,
816 "category" => $category,
819 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
820 drupal_set_message ("Tag type $tagname updated");
822 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
823 plc_redirect(l_tag($tag_type_id));
827 case 'add-tag-type': {
829 $tagname = $_POST['tagname'];
830 $min_role_id= intval( $_POST['min_role_id'] );
831 $description= $_POST['description'];
832 $category= $_POST['category'];
834 // make tag_type_fields dict
835 $tag_type_fields= array( "min_role_id" => $min_role_id,
836 "tagname" => $tagname,
837 "description" => $description,
838 "category" => $category,
842 $tag_type_id=$api->AddTagType( $tag_type_fields );
843 if ($tag_type_id > 0)
844 drupal_set_message ("tag type $tag_type_id created");
846 drupal_set_error ("Could not create tag type $tagname");
847 plc_redirect( l_tags());
851 case 'delete-tag-types': {
852 $tag_type_ids = $_POST['tag_type_ids'];
853 if ( ! $tag_type_ids) {
854 drupal_set_message("action=$action - No tag selected");
859 foreach ($tag_type_ids as $tag_type_id)
860 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
865 drupal_set_message ("Deleted $counter tag(s)");
867 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
868 plc_redirect (l_tags());
872 case 'remove-roles-from-tag-type' : {
873 $tag_type_id=$_POST['tag_type_id'];
874 $role_ids=$_POST['role_ids'];
876 drupal_set_error("You have not selected role(s) to remove");
878 foreach( $role_ids as $role_id)
879 if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 )
880 drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id");
882 plc_redirect (l_tag_roles($tag_type_id));
885 case 'add-role-to-tag-type' : {
886 $tag_type_id=$_POST['tag_type_id'];
887 $role_id=$_POST['role_id'];
889 drupal_set_error ("You have not selected a role to add");
890 } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) {
891 drupal_set_error("Could not add role $role_id to tag $tag_type_id");
893 plc_redirect (l_tag_roles($tag_type_id));
896 //////////////////////////////////////// tags
897 case 'set-tag-on-node':
898 case 'set-tag-on-interface': {
901 if ($action == 'set-tag-on-node') $node_mode=true;
904 $node_id = intval($_POST['node_id']);
906 $interface_id=intval($_POST['interface_id']);
907 $tag_type_id = intval($_POST['tag_type_id']);
908 $value = $_POST['value'];
910 $tag_types=$api->GetTagTypes(array($tag_type_id));
911 if (count ($tag_types) != 1) {
912 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
915 $tags = $api->GetNodeTags (array('node_id'=>$node_id, 'tag_type_id'=> $tag_type_id));
917 $tags = $api->GetInterfaceTags (array('interface_id'=>$interface_id, 'tag_type_id'=> $tag_type_id));
918 if ( count ($tags) == 1) {
921 $tag_id=$tag['node_tag_id'];
922 $result=$api->UpdateNodeTag($tag_id,$value);
924 $tag_id=$tag['interface_tag_id'];
925 $result=$api->UpdateInterfaceTag($tag_id,$value);
928 drupal_set_message ("Updated tag, new value = $value");
930 drupal_set_error ("Could not update tag");
933 $tag_id = $api->AddNodeTag($node_id,$tag_type_id,$value);
935 $tag_id = $api->AddInterfaceTag($interface_id,$tag_type_id,$value);
937 drupal_set_message ("Created tag, new value = $value");
939 drupal_set_error ("Could not create tag");
944 plc_redirect (l_node_tags($node_id));
946 plc_redirect (l_interface_tags($interface_id));
949 case 'delete-node-tags' :
950 case 'delete-interface-tags' : {
953 if ($action == 'delete-node-tags') $node_mode=true;
956 $tag_ids=$_POST['node_tag_ids'];
958 $tag_ids=$_POST['interface_tag_ids'];
961 drupal_set_message("action=$action - No tag selected");
966 foreach( $tag_ids as $tag_id ) {
968 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
970 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
977 drupal_set_message ("Deleted $counter tag(s)");
979 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
981 plc_redirect(l_node_tags($_POST['node_id']));
983 plc_redirect(l_interface_tags($_POST['interface_id']));
986 //////////////////////////////////////// nodegroups
987 case 'update-nodegroup': {
988 $nodegroup_id = $_POST['nodegroup_id'];
989 $groupname = $_POST['groupname'];
990 $value = $_POST['value'];
993 $fields['groupname']=$groupname;
994 $fields['value']=$value;
995 if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1)
996 drupal_set_message ('Nodegroup updated');
998 drupal_set_error ("Could not update nodegroup $groupname");
1000 plc_redirect(l_nodegroup($nodegroup_id));
1004 case 'add-nodegroup': {
1005 $groupname=$_POST['groupname'];
1006 if ( ! $groupname ) {
1007 drupal_set_error ('Empty groupname');
1008 plc_redirect (l_nodegroups());
1010 $tag_type_id=intval($_POST['tag_type_id']);
1011 if ( ! $tag_type_id ) {
1012 drupal_set_error ('You must select a tag in the dropdown list');
1013 plc_redirect (l_nodegroups());
1015 $value=$_POST['value'];
1017 drupal_set_message ("Empty value.. let's see ..");
1019 if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0)
1020 drupal_set_message ("Nodegroup $groupname created");
1022 drupal_set_error ("Could not create nodegroup $groupname");
1024 plc_redirect (l_nodegroups());
1028 case 'delete-nodegroups': {
1029 $nodegroup_ids=$_POST['nodegroup_ids'];
1030 if ( ! $nodegroup_ids ) {
1031 drupal_set_message("action=delete-nodegroups - No group selected");
1032 plc_redirect(l_nodegroups());
1036 foreach ($nodegroup_ids as $nodegroup_id)
1037 if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1)
1042 drupal_set_message ("Deleted $counter group(s)");
1044 drupal_set_error ("Could not delete all selected groups, only $counter were removed");
1045 plc_redirect (l_nodegroups());
1049 //////////////////////////////////////// leases
1050 case 'manage-leases': {
1051 $actions=json_decode($_POST['actions']);
1057 foreach ($actions as $action) {
1058 if ($action[0] == 'add-leases') {
1059 $nodenames=$action[1];
1060 $add_requested += count($nodenames);
1061 $slicename=$action[2];
1062 $t_from=intval($action[3]);
1063 $t_until=intval($action[4]);
1064 $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
1065 // update number of added leases
1066 $ids=$hash['new_ids'];
1067 $add_done += count($ids);
1068 // update global errors array
1069 foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
1070 } else if ($action[0]=='delete-leases') {
1071 $lease_id=intval($action[1]);
1072 $del_requested += 1;
1073 if ($api->DeleteLeases(array($lease_id)) == 1) {
1076 $errors []= "Could not delete lease " . $lease_id;
1079 $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1083 if (count($errors)==0) {
1084 echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1086 foreach ($errors as $error) echo($error. "\n");
1087 echo("Leases updated only partially (" .
1088 $add_done . "/" . $add_requested . " added and " .
1089 $del_done . "/" . $del_requested . " deleted)");
1095 ////////////////////////////////////////
1098 plc_debug('GET',$_GET);
1099 plc_debug('POST',$_POST);
1100 plc_debug('FILES',$_FILES);
1105 plc_error ("Unknown action $action in actions.php");