#################### main pod documentation begin ################### =head1 NAME Zymonic::Decryptor::Message::FileFetch - Zymonic Decryptor Message. =head1 SYNOPSIS This Message uses Zymonic::FileFetch to do remote file handling. =head1 DESCRIPTION This Message calls down to Zymonic::FileFetch to handle files in a remote location. It is used when sensitive data to do with the connection (e.g. password) are stored in the decryptor. =head1 USAGE The message should be: { messagetype => 'FileFetch', action => 'fetch'/'put', host => ..., port => ..., username => ..., password => ..., protocol => ..., file_owner => ..., # if set will chown any files to this user # if fetch remote_remote => 'true'/'', # deleted remote files once retrieved? retrieval_glob => '', # glob to retrieve # if put files => { file_name => contents }, # files to put remote_directory => '', # where to put the files } The response will be: { error => ... } or ( if fetch ) { files => { file_name => contents }, # files receieved unique_local_directory, # the directory the files are in on server } or ( if put ) { success => 'true'/'' } =head1 BUGS None we're aware of... =head1 SUPPORT As in the license, Zymonic is provided without warranty or support unless purchased separately, however... If you email zymonic-support@zednax.com your issue will be noted and may receive a response. For security issues, please contact zymonic-security@zednax.com and someone will respond within 8 working hours. =head1 AUTHOR Alex Masidlover et al. CPAN ID: MODAUTHOR Zednax Limited alex.masidlover@zednax.com http://www.zednax.com =head1 COPYRIGHT This program is free software licensed under the... Zymonic Public License 1.0 The full text of the license can be found in the LICENSE file included with this module. Other licenses may be acceptable if including parts of Zymonic in larger projects, please contact Zednax for details. =head1 SEE ALSO Zymonic::PaymentGateway perl(1). =cut #################### main pod documentation end ################### package Zymonic::Decryptor::Message::FileFetch; use strict; use warnings; BEGIN { use Exporter (); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = '0.01'; @ISA = qw(Exporter); #Give a hoot don't pollute, do not export more than needed by default @EXPORT = qw(); @EXPORT_OK = qw(); %EXPORT_TAGS = (); } use base 'Zymonic::Decryptor::Message'; use Zymonic::FileFetch; use File::Basename; use File::Slurp qw(read_file write_file); use Zymonic::Utils qw(debug rethrow_exception); #################### subroutine header begin #################### =head2 respond Usage : my $response = $mh->repsond($in) Purpose : This is the response handler method for decryptor messages. Returns : a response hashref Argument : a message hashref Throws : nothing Comment : See Also : =cut #################### subroutine header end #################### sub respond { my $self = shift; my $in = shift; # check for required params map { return { error => "Missing FileFetch Parameter: $_" } unless $in->{$_}; } qw(action protocol host); $self->{decryptor_server}->connection_log("FileFetch $in->{action} Requested"); my $ff = Zymonic::FileFetch->new( parent => $self, config => $self->{decryptor_server}->{config}, DB => $self->{decryptor_server}->{DB}, # set values map { $_ => $in->{$_} } grep { $in->{$_} } qw(protocol host port username password remove_remote remote_directory file_owner) ); if ( $in->{action} eq 'fetch' ) { return { error => "Missing FileFetch get Parameter: retrieval_glob" } unless $in->{retrieval_glob}; my @files = (); my $result = {}; eval { @files = $ff->fetch( $in->{retrieval_glob} ); 1; } or do { my $exception = $@; if ( ref($exception) && $exception->isa('Zymonic::Exception::FileFetch') ) { $result->{error} = $self->ll_exception($exception); } else { $ff->cleanup(); rethrow_exception($exception); } }; unless ( $result->{error} ) { my $files_parsed = {}; foreach my $file (@files) { my $file_name = fileparse($file); my $file_contents = read_file($file); $files_parsed->{$file_name} = $file_contents; } debug( 'Found files: ' . join( ', ', @files ) . '; Parsed: ' . join( ', ', keys %{$files_parsed} ) ); $result->{files} = $files_parsed; } # clean the existing local files $ff->cleanup(); # return just the file contents return $result; } elsif ( $in->{action} eq 'put' ) { return { error => 'Missing FileFetch get Parameter: files' } unless ref( $in->{files} ) eq 'HASH' && keys %{ $in->{files} }; # write files locally for sending map { write_file( "$ff->{unique_local_directory}/$_", $in->{files}->{$_} ) } keys %{ $in->{files} }; my $result = {}; eval { $ff->put( [ map { "$ff->{unique_local_directory}/$_" } keys %{ $in->{files} } ] ); 1; } or do { my $exception = $@; if ( ref($exception) && $exception->isa('Zymonic::Exception::FileFetch') ) { $result->{error} = $self->ll_exception($exception); } else { $ff->cleanup(); rethrow_exception($exception); } }; # clean up $ff->cleanup(); unless ( $result->{error} ) { $result->{success} = 'true'; } return $result; } else { return { error => "Unknown FileFetch action: $in->{action}" }; } return { error => "Unable to complete FileFetch action: $in->{action}" }; } 1;