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-site";
115 // expects: site_id tagname value
116 $known_actions []= "set-tag-on-person";
117 // expects: person_id tagname value
118 $known_actions []= "set-tag-on-interface";
119 // expects: interface_id tagname value
120 $known_actions []= "delete-node-tags";
121 // expects: node_id & node_tag_ids
122 $known_actions []= "delete-site-tags";
123 // expects: site_id & site_tag_ids
124 $known_actions []= "delete-person-tags";
125 // expects: person_id & person_tag_ids
126 $known_actions []= "delete-interface-tags";
127 // expects: interface_id & interface_tag_ids
129 //////////////////////////////////////// nodegroups
130 $known_actions []= "update-nodegroup";
131 // expects nodegroup_id groupname value
132 $known_actions []= "add-nodegroup";
133 // expects groupname, tag_type_id, value
134 $known_actions []= 'delete-nodegroups';
135 // expects nodegroup_ids
137 //////////////////////////////////////// leases
138 $known_actions []= "manage-leases";
139 // expects as 'actions' a list of 'action' of the form
140 // either [ 'add-leases', [nodenames], slicename, t_from, t_until ]
141 // or [ 'delete-leases', lease_id ]
143 ////////////////////////////////////////////////////////////
144 $interface_details= array ('method','type', 'ip', 'gateway', 'network',
145 'broadcast', 'netmask', 'dns1', 'dns2',
146 'hostname', 'mac', 'bwlimit' );
148 //////////////////////////////
149 // sometimes we don't set 'action', but use the submit button name instead
150 // so if 'action' not set, see if $_POST has one of the actions as a key
151 if ($_POST['action'])
152 $action=$_POST['action'];
154 foreach ($known_actions as $known_action)
155 if ($_POST[$known_action]) {
156 $action=$known_action;
160 //uncomment for debugging incoming data
163 $person_id = $_POST['person_id']; // usually needed
166 drupal_set_message ("actions.php: action not set or not in known_actions");
167 plc_debug('POST',$_POST);
173 case 'add-person-to-site': {
174 $site_id = $_POST['site_id'];
175 $api->AddPersonToSite( intval( $person_id ), intval( $site_id ) );
176 plc_redirect (l_person($person_id));
179 case 'remove-person-from-sites': {
180 $site_ids = $_POST['site_ids'];
182 drupal_set_message("action=$action - No site selected");
185 foreach ( $site_ids as $site_id ) {
186 $api->DeletePersonFromSite( intval( $person_id ), intval( $site_id ) );
188 plc_redirect (l_person($person_id));
191 case 'remove-roles-from-person' : {
192 $role_ids=$_POST['role_ids'];
194 drupal_set_error("You have not selected role(s) to remove");
196 foreach( $role_ids as $role_id)
197 if ( $api->DeleteRoleFromPerson( intval( $role_id ), intval( $person_id ) ) != 1 )
198 drupal_set_error ("Could not remove role $role_id from person $person_id");
200 plc_redirect (l_person_roles($person_id));
203 case 'add-role-to-person' : {
204 $role_id=$_POST['role_id'];
206 drupal_set_error ("You have not selected a role to add");
207 } else if ($api->AddRoleToPerson( intval( $role_id ), intval( $person_id ) ) != 1) {
208 drupal_set_error("Could not add role $role_id to person $person_id");
210 plc_redirect (l_person_roles($person_id));
213 case 'enable-person' : {
214 $fields = array( "enabled"=>true );
215 $api->UpdatePerson( intval( $person_id ), $fields );
216 plc_redirect (l_person($person_id));
219 case 'disable-person' : {
220 $fields = array( "enabled"=>false );
221 $api->UpdatePerson( intval( $person_id ), $fields );
222 plc_redirect (l_person($person_id));
225 case 'become-person' : {
226 $plc->BecomePerson (intval($person_id));
227 plc_redirect (l_person(intval($person_id)));
230 case 'delete-person' : {
231 $api->DeletePerson( intval( $person_id ) );
232 plc_redirect (l_persons());
235 case 'delete-keys' : {
236 $key_ids=$_POST['key_ids'];
238 drupal_set_message("action=$action - No key selected");
243 foreach( $key_ids as $key_id ) {
244 if ($api->DeleteKey( intval( $key_id )) != 1)
250 drupal_set_message ("Deleted $counter key(s)");
252 drupal_set_error ("Could not delete all selected keys, only $counter were removed");
253 plc_redirect(l_person($person_id));
257 case 'upload-key' : {
258 if ( ! isset( $_FILES['key'] ) ) {
259 drupal_set_message ("action=$action, no key file set");
263 $key_file= $_FILES['key']['tmp_name'];
265 plc_error("Please select a valid SSH key file to upload");
268 $fp = fopen( $key_file, "r" );
271 plc_error("Unable to open key file $key_file");
274 // opened the key file, read the one line of contents
275 // The POST operation always creates a file even if the filename
276 // the user specified was garbage. If there was some problem
277 // with the source file, we'll get a zero length read here.
278 $key = fread($fp, filesize($key_file));
281 $key_id = $api->AddPersonKey( intval( $person_id ), array( "key_type"=> 'ssh', "key"=> $key ) );
284 drupal_set_message ("New key added");
286 drupal_set_error("Could not add key, please verify your SSH file content\n" . $api->error());
288 plc_redirect(l_person($person_id));
291 case 'update-person': {
292 $person_id=$_POST['person_id'];
293 // attempt to update this person
294 $first_name= $_POST['first_name'];
295 $last_name= $_POST['last_name'];
296 $title= $_POST['title'];
297 $email= $_POST['email'];
298 $phone= $_POST['phone'];
300 $bio= str_replace("\r", "", $_POST['bio']);
301 $password1= $_POST['password1'];
302 $password2= $_POST['password2'];
304 if( $password1 != $password2 ) {
305 drupal_set_error ("The passwords do not match");
306 plc_redirect(l_person($person_id));
310 $fields['first_name']= $first_name;
311 $fields['last_name']= $last_name;
312 $fields['title']= $title;
313 $fields['email']= $email;
314 $fields['phone']= $phone;
315 $fields['url']= $url;
316 $fields['bio']= $bio;
318 if ( $password1 != "" )
319 $fields['password']= $password1;
321 if ( $api->UpdatePerson( intval( $person_id ), $fields) == 1 )
322 drupal_set_message("$first_name $last_name updated");
324 drupal_set_error ("Could not update person $person_id" . $api->error());
326 plc_redirect(l_person($person_id));
330 //////////////////////////////////////////////////////////// nodes
331 case 'node-boot-state': {
332 $node_id=intval($_POST['node_id']);
333 $boot_state=$_POST['boot_state'];
334 $result=$api->UpdateNode( $node_id, array( "boot_state" => $boot_state ) );
336 drupal_set_message("boot state updated");
337 plc_redirect (l_node($node_id));
339 drupal_set_error("Could not set boot_state '$boot_state'");
344 case 'delete-node': {
345 $node_id=intval($_POST['node_id']);
346 $result=$api->DeleteNode( intval( $node_id ) );
348 drupal_set_message("Node $node_id deleted");
349 plc_redirect (l_nodes());
351 drupal_set_error ("Could not delete node $node_id");
356 case 'update-node': {
357 $node_id=intval($_POST['node_id']);
358 $hostname= $_POST['hostname'];
359 $model= $_POST['model'];
360 $node_type= $_POST['node_type'];
362 $fields= array( "hostname"=>$hostname, "model"=>$model, "node_type"=>$node_type );
363 $api->UpdateNode( $node_id, $fields );
364 $error= $api->error();
366 if( empty( $error ) ) {
367 drupal_set_message("Update node $hostname");
368 plc_redirect(l_node($node_id));
370 drupal_set_error($error);
375 // this code will ensure that at most one PCU gets attached to the node
377 $node_id=intval($_POST['node_id']);
378 $pcu_id=intval($_POST['pcu_id']);
379 $port=intval($_POST['port']);
380 // always start with deleting former PCUs
381 $nodes = $api->GetNodes(array($node_id),array('pcu_ids'));
382 $former_pcu_ids = $nodes[0]['pcu_ids'];
383 if ($former_pcu_ids) foreach ($former_pcu_ids as $former_pcu_id) {
384 if ($api->DeleteNodeFromPCU($node_id,$former_pcu_id) == 1)
385 drupal_set_message ('Detached node ' . $node_id . ' from PCU ' . $pcu_id);
387 drupal_set_error ('Could not detach node ' . $node_id . ' from PCU ' . $pcu_id);
389 // re-attach only if provided pcu_id >=0
391 if ($api->AddNodeToPCU($node_id,$pcu_id,$port) == 1)
392 drupal_set_message ('Attached node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
394 drupal_set_error ('Failed to attach node ' . $node_id . ' to PCU ' . $pcu_id . ' on port ' . $port);
396 drupal_set_message ('Detached node from all PCUs');
399 plc_redirect(l_node($node_id));
403 case 'reboot-node-with-pcu': {
404 $node_id=intval($_POST['node_id']);
405 $hostname= $_POST['hostname'];
406 $test = $_POST['test'];
407 settype($test, "boolean");
409 $ret = $api->RebootNodeWithPCU( $node_id, $test );
410 $error= $api->error();
412 if( empty( $error ) ) {
413 drupal_set_message("Reboot node $hostname: $ret");
414 plc_redirect(l_node($node_id));
416 drupal_set_error($error);
422 //////////////////////////////////////////////////////////// interfaces
423 case 'delete-interfaces' : {
424 $interface_ids=$_POST['interface_ids'];
425 if ( ! $interface_ids) {
426 drupal_set_message("action=$action - No interface selected");
431 foreach( $interface_ids as $interface_id ) {
432 if ($api->DeleteInterface( intval( $interface_id )) != 1)
438 drupal_set_message ("Deleted $counter interface(s)");
440 drupal_set_error ("Could not delete all selected interfaces, only $counter were removed");
441 plc_redirect(l_node($_POST['node_id']));
444 case 'new-interface': {
445 plc_redirect(l_interface_add($_POST['node_id']));
448 case 'add-interface': {
449 $node_id=intval($_POST['node_id']);
450 foreach ($interface_details as $field) {
451 $interface[$field]= $_POST[$field];
452 // these must be integers
453 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
454 if ( empty ($interface[$field]) )
455 unset ($interface[$field]);
457 $interface[$field]= intval( $interface[$field] );
460 $interface_id =$api->AddInterface( $node_id , $interface );
461 if ($interface_id <= 0 ) {
462 drupal_set_error ("Could not create interface");
463 drupal_set_error ($api->error());
465 $ip=$interface['ip'];
466 drupal_set_message ("Interface $ip added into node $node_id");
467 if ($_POST['is-virtual']) {
468 $ifname=$_POST['ifname'];
469 if ($api->AddInterfaceTag($interface_id,"ifname",$ifname) <= 0)
470 drupal_set_error ("Could not set tag 'ifname'=$ifname");
472 drupal_set_message ("Set tag 'ifname'=$ifname");
473 $alias=$_POST['alias'];
474 // deafult to interface_id
475 if ( ! $alias ) $alias=strval($interface_id);
476 if ($api->AddInterfaceTag($interface_id,"alias",$alias) <= 0)
477 drupal_set_error ("Could not set tag 'alias'=$alias");
479 drupal_set_message ("Set tag 'alias'=$alias");
482 plc_redirect (l_node_interfaces($node_id));
485 case 'update-interface': {
486 $interface_id=$_POST['interface_id'];
487 foreach ($interface_details as $field) {
488 $interface[$field]= $_POST[$field];
489 if( in_array( $field, array( 'bwlimit', 'node_id' ) ) ) {
490 if ( intval($interface[$field]) != 0 ) {
491 $interface[$field]= intval( $interface[$field]);
492 } elseif ($field=='bwlimit' ) {
493 $interface[$field] = NULL;
497 $result=$api->UpdateInterface( intval( $interface_id ), $interface );
499 drupal_set_message ("Interface $interface_id updated");
501 drupal_set_error ("Could not update interface: " . $api->error());
502 plc_redirect (l_interface($interface_id));
505 //////////////////////////////////////////////////////////// sites
506 case 'delete-site': {
507 $site_id = intval($_POST['site_id']);
508 if ($api->DeleteSite($site_id) ==1)
509 drupal_set_message ("Site $site_id deleted");
511 drupal_set_error("Failed to delete site $site_id");
512 plc_redirect (l_sites());
516 case 'expire-all-slices-in-site': {
518 drupal_set_message("action $action not implemented in actions.php -- need tweaks and test");
521 //// old code from sites/expire.php
522 $sites = $api->GetSites( array( intval( $site_id )));
524 // xxx why not 'now?'
525 $expiration= strtotime( $_POST['expires'] );
526 // loop through all slices for site
527 foreach ($site['slice_ids'] as $slice_id) {
528 $api->UpdateSlice( $slice_id, array( "expires" => $expiration ) );
530 // update site to not allow slice creation or renewal
531 $api->UpdateSite( $site_id, array( "max_slices" => 0 )) ;
532 plc_redirect (l_site($site_id));
536 case 'update-site': {
537 $site_id=intval($_POST['site_id']);
538 $name= $_POST['name'];
539 $abbreviated_name= $_POST['abbreviated_name'];
541 $latitude= floatval($_POST['latitude']);
542 $longitude= floatval($_POST['longitude']);
543 //$max_slivers= $_POST['max_slivers'];
545 $fields= array( "name" => $name,
546 "abbreviated_name" => $abbreviated_name,
548 "latitude" => floatval( $latitude ),
549 "longitude" => floatval( $longitude ));
551 if ($_POST['login_base'])
552 $fields['login_base'] = $_POST['login_base'];
553 if (isset($_POST['max_slices']))
554 $fields['max_slices'] = intval($_POST['max_slices']);
555 if (isset($_POST['enabled'])) {
556 $fields['enabled'] = (bool)$_POST['enabled'];
559 $retcod=$api->UpdateSite( intval( $site_id ), $fields );
561 drupal_set_message("Site $name updated");
563 drupal_set_error ("Could not update site $site_id");
565 plc_redirect(l_site($site_id));
569 //////////////////////////////////////////////////////////// slices
570 case 'delete-slice': {
571 $slice_id = $_POST['slice_id'];
572 if ($api->DeleteSlice( intval( $slice_id )) == 1 ) {
573 drupal_set_message("Slice $slice_id deleted");
574 plc_redirect(l_slices());
576 drupal_set_error("Could not delete slice $slice_id " . $api->error());
581 case 'update-slice': {
582 $slice_id = $_POST['slice_id'];
583 $name = $_POST['name'];
584 $description= $_POST['description'];
587 $fields= array( "description"=>$description, "url"=>$url );
588 $api->UpdateSlice( intval( $slice_id ), $fields );
589 $error= $api->error();
591 if( empty( $error ) ) {
592 drupal_set_message("Update slice $name");
593 plc_redirect(l_slice($slice_id));
595 drupal_set_error($error);
600 case 'renew-slice': {
601 $slice_id = intval ($_POST['slice_id']);
602 $expires = intval ($_POST['expires']);
608 $MAX_FUTURE=$WEEKS*$WEEK;
609 if ( ($expires-$now) > $MAX_FUTURE) {
610 drupal_set_error("Cannot renew slice that far in the future, max is $WEEKS weeks from now");
611 plc_redirect(l_slice($slice_id));
613 if ($api->UpdateSlice ($slice_id, array('expires'=>$expires)) == 1)
614 drupal_set_message("Slice renewed");
616 drupal_set_error("Could not update slice $slice_id");
617 plc_redirect(l_slice($slice_id));
621 case 'remove-persons-from-slice': {
622 $slice_id = intval ($_POST['slice_id']);
623 $person_ids = $_POST['person_ids'];
624 $return_url = $_POST['return_url'];
627 $return_url = l_slice($slice_id) . "&show_persons=true" ;
631 $tmp_slices = $api->GetSlices($slice_id, array("name"));
632 if (count($tmp_slices) > 0) {
633 $tmp_slice = $tmp_slices[0];
634 $slice_name = $tmp_slice["name"];
636 $notify_subject = "Removed from slice: " . $slice_name;
637 $notify_body = sprintf("You have been removed from the slice %s.
639 Our support team will be glad to answer any question that you might have.
641 $notify_person_ids = array();
645 foreach( $person_ids as $person_id ) {
646 if ($api->DeletePersonFromSlice(intval($person_id),$slice_id) != 1)
649 array_push($notify_person_ids, intval($person_id));
654 $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
655 drupal_set_message ("Deleted $counter person(s)");
658 drupal_set_error ("Could not delete all selected persons, only $counter were removed");
659 plc_redirect($return_url);
663 case 'add-persons-in-slice': {
664 $slice_id = intval ($_POST['slice_id']);
665 $person_ids = $_POST['person_ids'];
666 $return_url = $_POST['return_url'];
669 $return_url = l_slice($slice_id) . "&show_persons=true" ;
673 $tmp_slices = $api->GetSlices($slice_id, array("name"));
674 if (count($tmp_slices) > 0) {
675 $tmp_slice = $tmp_slices[0];
676 $slice_name = $tmp_slice["name"];
678 $notify_subject = "Added to slice: " . $slice_name;
679 $notify_body = sprintf("You have been added to the slice %s as a user.
681 You can go to your slice page following the link below:
682 https://%s:%d/db/slices/index.php?id=%d
684 Our support team will be glad to answer any question that you might have.
685 ",$slice_name,PLC_WWW_HOST,PLC_WWW_SSL_PORT,$slice_id);
686 $notify_person_ids = array();
690 foreach ($person_ids as $person_id) {
691 if ($api->AddPersonToSlice(intval($person_id),$slice_id) != 1)
694 array_push($notify_person_ids, intval($person_id));
699 $adm->NotifyPersons($notify_person_ids,$notify_subject,$notify_body);
700 drupal_set_message ("Added $counter person(s)");
703 drupal_set_error ("Could not add all selected persons, only $counter were added");
704 plc_redirect($return_url);
708 case 'remove-nodes-from-slice': {
709 $slice_id = intval ($_POST['slice_id']);
710 $node_ids = array_map("intval",$_POST['node_ids']);
711 $count=count($node_ids);
713 if ($api->DeleteSliceFromNodes($slice_id,$node_ids) == 1)
714 drupal_set_message ("Removed $count node(s)");
716 drupal_set_error ("Could not remove selected nodes");
717 plc_redirect(l_slice_nodes($slice_id));
721 case 'add-nodes-in-slice': {
722 $slice_id = intval ($_POST['slice_id']);
723 $node_ids = array_map("intval",$_POST['node_ids']);
724 $count=count($node_ids);
725 if ($api->AddSliceToNodes($slice_id,$node_ids) == 1)
726 drupal_set_message ("Added $count node(s)");
728 drupal_set_error ("Could not add all selected nodes");
729 plc_redirect(l_slice_nodes($slice_id));
733 case 'update-initscripts': {
734 // expects: slice_id & name & previous-initscript & previous-initscript-code
735 // & initscript & initscript-code
736 $slice_id = intval ($_POST['slice_id']);
737 $previous_initscript=$_POST['previous-initscript'];
738 $initscript=$_POST['initscript'];
739 $previous_initscript_code=html_entity_decode($_POST['previous-initscript-code']);
740 $initscript_code=$_POST['initscript-code'];
743 if (strcmp($initscript,$previous_initscript) != 0) {
744 $newvalue=$api->SetSliceInitscript($slice_id,$initscript);
745 $status = (strcmp($newvalue,$initscript)==0) ? "OK" : "failed";
746 if (! $initscript) drupal_set_message("Removed shared initscript '" . $previous_initscript . "' " . $status);
747 else drupal_set_message("Replaced shared initscript with '" . $initscript . "' " . $status);
751 // somehow some \r chars make it here; just ignore them
752 $previous_initscript_code=str_replace("\r","",$previous_initscript_code);
753 // plc_debug_txt('previous initscript_code after cr',$previous_initscript_code);
755 $initscript_code=str_replace("\r","",$initscript_code);
756 // make sure the script ends with a single \n
757 $initscript_code=trim($initscript_code);
758 if (!empty($initscript_code) && $initscript_code[strlen($initscript_code)-1] != "\n")
759 $initscript_code.="\n";
760 // plc_debug_txt('initscript_code after cr & nl/eof',$initscript_code);
762 if (strcmp($initscript_code,$previous_initscript_code) != 0) {
763 $newvalue=$api->SetSliceInitscriptCode($slice_id,$initscript_code);
764 // plc_debug_txt('newvalue',$newvalue);
765 $status=(strcmp($newvalue,$initscript_code)==0) ? "OK" : "failed";
766 if (! $initscript_code) drupal_set_message("Removed initscript code " . $status);
767 else drupal_set_message("Installed new initscript code " . $status);
770 if (!$changes) drupal_set_message("No changes required in initscript");
771 plc_redirect(l_slice($slice_id) . "&show_details=0&show_initscripts=1" );
776 case 'delete-slice-tags': {
777 $slice_id = intval($_POST['slice_id']);
778 $slice_tag_ids = array_map("intval", $_POST['slice_tag_ids']);
781 foreach($slice_tag_ids as $slice_tag_id) {
782 if ($api->DeleteSliceTag($slice_tag_id)) $count += 1;
784 drupal_set_error("Could not delete slice tag: slice_tag_id = $slice_tag_id");
789 drupal_set_message ("Deleted $count slice tag(s)");
790 plc_redirect(l_slice($slice_id) . "&show_tags=1" );
794 case 'add-slice-tag': {
795 $slice_id = intval($_POST['slice_id']);
796 $tag_type_id = intval($_POST['tag_type_id']);
797 $value = $_POST['value'];
798 $node_id = intval($_POST['node_id']);
799 $nodegroup_id = intval($_POST['nodegroup_id']);
803 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, $node_id);
804 } elseif ($nodegroup_id) {
805 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value, null, $nodegroup_id);
807 $result = $api->AddSliceTag($slice_id, $tag_type_id, $value);
810 drupal_set_message ("Added slice tag.");
812 drupal_set_error("Could not add slice tag");
813 if ($_POST['sliver_action'])
814 plc_redirect(l_sliver($node_id,$slice_id));
816 plc_redirect(l_slice($slice_id) . "&show_tags=true" );
820 //////////////////////////////////////////////////////////// tag types
822 case 'update-tag-type': {
824 $tag_type_id= intval( $_POST['tag_type_id'] );
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,
837 if ($api->UpdateTagType( $tag_type_id, $tag_type_fields ) == 1)
838 drupal_set_message ("Tag type $tagname updated");
840 drupal_set_error ("Could not update tag type $tag_type_id\n".$api->error());
841 plc_redirect(l_tag($tag_type_id));
845 case 'add-tag-type': {
847 $tagname = $_POST['tagname'];
848 $min_role_id= intval( $_POST['min_role_id'] );
849 $description= $_POST['description'];
850 $category= $_POST['category'];
852 // make tag_type_fields dict
853 $tag_type_fields= array( "min_role_id" => $min_role_id,
854 "tagname" => $tagname,
855 "description" => $description,
856 "category" => $category,
860 $tag_type_id=$api->AddTagType( $tag_type_fields );
861 if ($tag_type_id > 0)
862 drupal_set_message ("tag type $tag_type_id created");
864 drupal_set_error ("Could not create tag type $tagname");
865 plc_redirect( l_tags());
869 case 'delete-tag-types': {
870 $tag_type_ids = $_POST['tag_type_ids'];
871 if ( ! $tag_type_ids) {
872 drupal_set_message("action=$action - No tag selected");
877 foreach ($tag_type_ids as $tag_type_id)
878 if ($api->DeleteTagType(intval($tag_type_id)) != 1)
883 drupal_set_message ("Deleted $counter tag(s)");
885 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
886 plc_redirect (l_tags());
890 case 'remove-roles-from-tag-type' : {
891 $tag_type_id=$_POST['tag_type_id'];
892 $role_ids=$_POST['role_ids'];
894 drupal_set_error("You have not selected role(s) to remove");
896 foreach( $role_ids as $role_id)
897 if ( $api->DeleteRoleFromTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1 )
898 drupal_set_error ("Could not remove role $role_id from tag type $tag_type_id");
900 plc_redirect (l_tag_roles($tag_type_id));
903 case 'add-role-to-tag-type' : {
904 $tag_type_id=$_POST['tag_type_id'];
905 $role_id=$_POST['role_id'];
907 drupal_set_error ("You have not selected a role to add");
908 } else if ($api->AddRoleToTagType( intval( $role_id ), intval( $tag_type_id ) ) != 1) {
909 drupal_set_error("Could not add role $role_id to tag $tag_type_id");
911 plc_redirect (l_tag_roles($tag_type_id));
914 //////////////////////////////////////// tags
915 case 'set-tag-on-node':
916 case 'set-tag-on-site':
917 case 'set-tag-on-person':
918 case 'set-tag-on-interface':
922 if ($action == 'set-tag-on-node') $mode='node';
923 if ($action == 'set-tag-on-site') $mode='site';
924 if ($action == 'set-tag-on-person') $mode='person';
925 if ($action == 'set-tag-on-interface') $mode='interface';
928 $id = intval($_POST['node_id']);
929 else if ($mode=='site')
930 $id = intval($_POST['site_id']);
931 else if ($mode=='person')
932 $id = intval($_POST['person_id']);
934 $id = intval($_POST['interface_id']);
935 $tag_type_id = intval($_POST['tag_type_id']);
936 $value = $_POST['value'];
938 $tag_types=$api->GetTagTypes(array($tag_type_id));
939 if (count ($tag_types) != 1) {
940 drupal_set_error ("Could not locate tag_type_id $tag_type_id </br> Tag not set.");
943 $tags = $api->GetNodeTags (array('node_id'=>$id, 'tag_type_id'=> $tag_type_id));
944 else if ($mode=='site')
945 $tags = $api->GetSiteTags (array('site_id'=>$id, 'tag_type_id'=> $tag_type_id));
946 else if ($mode=='person')
947 $tags = $api->GetPersonTags (array('person_id'=>$id, 'tag_type_id'=> $tag_type_id));
949 $tags = $api->GetInterfaceTags (array('interface_id'=>$id, 'tag_type_id'=> $tag_type_id));
951 // already has a tag set
952 if ( count ($tags) == 1) {
955 $result=$api->UpdateNodeTag($tag['node_tag_id'],$value);
956 else if ($mode=='site')
957 $result=$api->UpdateSiteTag($tag['site_tag_id'],$value);
958 else if ($mode=='person')
959 $result=$api->UpdatePersonTag($tag['person_tag_id'],$value);
961 $result=$api->UpdateInterfaceTag($tag['interface_tag_id'],$value);
964 drupal_set_message ("Updated tag, new value = $value");
966 drupal_set_error ("Could not update tag");
968 // no such tag set yet on that object
971 $tag_id = $api->AddNodeTag($id,$tag_type_id,$value);
972 else if ($mode=='site')
973 $tag_id = $api->AddSiteTag($id,$tag_type_id,$value);
974 else if ($mode=='person')
975 $tag_id = $api->AddPersonTag($id,$tag_type_id,$value);
977 $tag_id = $api->AddInterfaceTag($id,$tag_type_id,$value);
979 drupal_set_message ("Created tag, new value = $value");
981 drupal_set_error ("Could not create tag");
986 plc_redirect (l_node_tags($id));
987 else if ($mode=='site')
988 plc_redirect (l_site_tags($id));
989 else if ($mode=='person')
990 plc_redirect (l_person_tags($id));
992 plc_redirect (l_interface_tags($id));
995 case 'delete-node-tags':
996 case 'delete-site-tags':
997 case 'delete-person-tags':
998 case 'delete-interface-tags': {
1001 if ($action == 'delete-node-tags') $mode='node';
1002 if ($action == 'delete-site-tags') $mode='site';
1003 if ($action == 'delete-person-tags') $mode='person';
1004 if ($action == 'delete-interface-tags') $mode='interface';
1006 if ($mode=='node') {
1007 $id=$_POST['node_id'];
1008 $tag_ids=$_POST['node_tag_ids'];
1009 } else if ($mode=='site') {
1010 $id=$_POST['site_id'];
1011 $tag_ids=$_POST['site_tag_ids'];
1012 } else if ($mode=='person') {
1013 $id=$_POST['person_id'];
1014 $tag_ids=$_POST['person_tag_ids'];
1016 $id=$_POST['interface_id'];
1017 $tag_ids=$_POST['interface_tag_ids'];
1021 drupal_set_message("action=$action - No tag selected");
1026 foreach( $tag_ids as $tag_id ) {
1028 $retcod = $api->DeleteNodeTag( intval( $tag_id ));
1029 else if ($mode=='site')
1030 $retcod = $api->DeleteSiteTag( intval( $tag_id ));
1031 else if ($mode=='person')
1032 $retcod = $api->DeletePersonTag( intval( $tag_id ));
1034 $retcod = $api->DeleteInterfaceTag( intval( $tag_id ));
1041 drupal_set_message ("Deleted $counter tag(s)");
1043 drupal_set_error ("Could not delete all selected tags, only $counter were removed");
1046 plc_redirect (l_node_tags($id));
1047 else if ($mode=='site')
1048 plc_redirect (l_site_tags($id));
1049 else if ($mode=='person')
1050 plc_redirect (l_person_tags($id));
1052 plc_redirect (l_interface_tags($id));
1056 //////////////////////////////////////// nodegroups
1057 case 'update-nodegroup': {
1058 $nodegroup_id = $_POST['nodegroup_id'];
1059 $groupname = $_POST['groupname'];
1060 $value = $_POST['value'];
1063 $fields['groupname']=$groupname;
1064 $fields['value']=$value;
1065 if ( $api->UpdateNodeGroup($nodegroup_id,$fields) == 1)
1066 drupal_set_message ('Nodegroup updated');
1068 drupal_set_error ("Could not update nodegroup $groupname");
1070 plc_redirect(l_nodegroup($nodegroup_id));
1074 case 'add-nodegroup': {
1075 $groupname=$_POST['groupname'];
1076 if ( ! $groupname ) {
1077 drupal_set_error ('Empty groupname');
1078 plc_redirect (l_nodegroups());
1080 $tag_type_id=intval($_POST['tag_type_id']);
1081 if ( ! $tag_type_id ) {
1082 drupal_set_error ('You must select a tag in the dropdown list');
1083 plc_redirect (l_nodegroups());
1085 $value=$_POST['value'];
1087 drupal_set_message ("Empty value.. let's see ..");
1089 if ( $api->AddNodeGroup ($groupname,$tag_type_id,$value) > 0)
1090 drupal_set_message ("Nodegroup $groupname created");
1092 drupal_set_error ("Could not create nodegroup $groupname");
1094 plc_redirect (l_nodegroups());
1098 case 'delete-nodegroups': {
1099 $nodegroup_ids=$_POST['nodegroup_ids'];
1100 if ( ! $nodegroup_ids ) {
1101 drupal_set_message("action=delete-nodegroups - No group selected");
1102 plc_redirect(l_nodegroups());
1106 foreach ($nodegroup_ids as $nodegroup_id)
1107 if ($api->DeleteNodeGroup(intval($nodegroup_id)) != 1)
1112 drupal_set_message ("Deleted $counter group(s)");
1114 drupal_set_error ("Could not delete all selected groups, only $counter were removed");
1115 plc_redirect (l_nodegroups());
1119 //////////////////////////////////////// leases
1120 case 'manage-leases': {
1121 $actions=json_decode($_POST['actions']);
1127 foreach ($actions as $action) {
1128 if ($action[0] == 'add-leases') {
1129 $nodenames=$action[1];
1130 $add_requested += count($nodenames);
1131 $slicename=$action[2];
1132 $t_from=intval($action[3]);
1133 $t_until=intval($action[4]);
1134 $hash = $api->AddLeases($nodenames,$slicename,$t_from,$t_until);
1135 // update number of added leases
1136 $ids=$hash['new_ids'];
1137 $add_done += count($ids);
1138 // update global errors array
1139 foreach ($api_errors=$hash['errors'] as $error) $errors[]=$error;
1140 } else if ($action[0]=='delete-leases') {
1141 $lease_id=intval($action[1]);
1142 $del_requested += 1;
1143 if ($api->DeleteLeases(array($lease_id)) == 1) {
1146 $errors []= "Could not delete lease " . $lease_id;
1149 $errors []= "in actions.php, manage-leases, wrong action ". $action[0];
1153 if (count($errors)==0) {
1154 echo("All leases updated (" . $add_done . " added and " . $del_done . " deleted)");
1156 foreach ($errors as $error) echo($error. "\n");
1157 echo("Leases updated only partially (" .
1158 $add_done . "/" . $add_requested . " added and " .
1159 $del_done . "/" . $del_requested . " deleted)");
1165 ////////////////////////////////////////
1168 plc_debug('GET',$_GET);
1169 plc_debug('POST',$_POST);
1170 plc_debug('FILES',$_FILES);
1175 plc_error ("Unknown action $action in actions.php");