| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Pixelletter; | 
| 2 | 1 |  |  | 1 |  | 25358 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 3 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 4 | 1 |  |  | 1 |  | 1352 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 64850 |  | 
|  | 1 |  |  |  |  | 30 |  | 
| 5 | 1 |  |  | 1 |  | 1130 | use File::Util; | 
|  | 1 |  |  |  |  | 21196 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 6 | 1 |  |  | 1 |  | 7588 | use Locale::Country; | 
|  | 1 |  |  |  |  | 30027 |  | 
|  | 1 |  |  |  |  | 79 |  | 
| 7 | 1 |  |  | 1 |  | 18 | use 5.010000; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.2'; | 
| 9 | 1 |  |  | 1 |  | 4 | use constant ALLOWED_FILE_EXTENSIONS => qw/pdf doc xls ppt rtf wpd psd odt ods odp odg/; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 62 |  | 
| 10 | 1 |  |  | 1 |  | 4 | use constant MAX_FILE_SIZE => 8388607; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1058 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new | 
| 13 |  |  |  |  |  |  | { | 
| 14 | 0 |  |  | 0 | 1 |  | my ($class, %args) = @_; | 
| 15 | 0 |  |  |  |  |  | my $self = {}; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # Some Defaults | 
| 18 | 0 |  |  |  |  |  | $self->{url} = 'http://www.pixelletter.de/xml/index.php'; | 
| 19 | 0 |  |  |  |  |  | $self->{test_mode} = 'false'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 0 |  |  |  |  |  | foreach my $arg (keys %args) | 
| 22 |  |  |  |  |  |  | { | 
| 23 | 0 |  |  |  |  |  | $self->{$arg} = $args{$arg}; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # Check for required parameters | 
| 27 | 0 |  |  |  |  |  | foreach( qw/username password/ ) | 
| 28 |  |  |  |  |  |  | { | 
| 29 | 0 | 0 |  |  |  |  | unless( $self->{$_} ) | 
| 30 |  |  |  |  |  |  | { | 
| 31 | 0 |  |  |  |  |  | die( "Required parameter $_ not defined\n" ); | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | # The user agent can also be passed (if you want to recycle...), but usually | 
| 36 |  |  |  |  |  |  | # it will be defined new here | 
| 37 | 0 | 0 |  |  |  |  | if( ! $self->{user_agent} ) | 
| 38 |  |  |  |  |  |  | { | 
| 39 | 0 |  |  |  |  |  | my $ua = LWP::UserAgent->new; | 
| 40 | 0 |  |  |  |  |  | $self->{user_agent} = $ua; | 
| 41 |  |  |  |  |  |  | } | 
| 42 | 0 |  |  |  |  |  | bless($self); | 
| 43 | 0 |  |  |  |  |  | return($self); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub addFile | 
| 47 |  |  |  |  |  |  | { | 
| 48 | 0 |  |  | 0 | 1 |  | my( $self, $file ) = @_; | 
| 49 | 0 |  |  |  |  |  | my $f = File::Util->new(); | 
| 50 | 0 |  |  |  |  |  | my @files; | 
| 51 | 0 | 0 |  |  |  |  | if( $self->{files} ) | 
| 52 |  |  |  |  |  |  | { | 
| 53 | 0 |  |  |  |  |  | @files = @{ $self->{files} }; | 
|  | 0 |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # Does the file exist | 
| 57 | 0 | 0 |  |  |  |  | if( ! -f $file ) | 
| 58 |  |  |  |  |  |  | { | 
| 59 | 0 |  |  |  |  |  | die( "File $file does not exist\n" ); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Is it one of the "allowed" extensions (a rudementary test to make sure user is | 
| 63 |  |  |  |  |  |  | # not trying to send something which pixelletter does not understand | 
| 64 | 0 |  |  |  |  |  | my $allowed = undef; | 
| 65 | 0 |  |  |  |  |  | my $extension = ( $file =~ m/.*\.(.*?)$/ )[0]; | 
| 66 | 0 |  |  |  |  |  | foreach( ALLOWED_FILE_EXTENSIONS ) | 
| 67 |  |  |  |  |  |  | { | 
| 68 | 0 | 0 |  |  |  |  | if( $_ eq $extension ) | 
| 69 |  |  |  |  |  |  | { | 
| 70 | 0 |  |  |  |  |  | $allowed = 1; | 
| 71 | 0 |  |  |  |  |  | last; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | } | 
| 74 | 0 | 0 |  |  |  |  | if( ! $allowed ) | 
| 75 |  |  |  |  |  |  | { | 
| 76 | 0 |  |  |  |  |  | die( "$extension is not an allowed file type.  Allowed file extensions are: " . join( ', ', ALLOWED_FILE_EXTENSIONS ) . "\n" ); | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # Make sure file is not too big (pixelletter only accepts up to 8MB | 
| 80 | 0 | 0 |  |  |  |  | if( $f->size( $file ) > MAX_FILE_SIZE ) | 
| 81 |  |  |  |  |  |  | { | 
| 82 | 0 |  |  |  |  |  | die( sprintf( "Cannot process %s because pixelletter only allowes files up to %u bytes but yours is %u bytes\n", | 
| 83 |  |  |  |  |  |  | $file, | 
| 84 |  |  |  |  |  |  | MAX_FILE_SIZE, | 
| 85 |  |  |  |  |  |  | $f->size( $file ), | 
| 86 |  |  |  |  |  |  | ) ); | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # Add the file to the form | 
| 90 | 0 |  |  |  |  |  | push( @files, [$file] ); | 
| 91 | 0 |  |  |  |  |  | $self->{files} = \@files; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub files | 
| 95 |  |  |  |  |  |  | { | 
| 96 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 97 | 0 |  |  |  |  |  | return $self->{files}; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub filecount | 
| 101 |  |  |  |  |  |  | { | 
| 102 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 103 | 0 | 0 |  |  |  |  | if( $self->{files} ) | 
| 104 |  |  |  |  |  |  | { | 
| 105 | 0 |  |  |  |  |  | return( scalar( @{ $self->{files} } ) ); | 
|  | 0 |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | } | 
| 107 | 0 |  |  |  |  |  | return 0; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub sendFax | 
| 111 |  |  |  |  |  |  | { | 
| 112 | 0 |  |  | 0 | 1 |  | my( $self, $fax_number ) = @_; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 0 | 0 | 0 |  |  |  | if( ! $fax_number || $fax_number !~ m/^\+[0-9]{2,} [0-9]{1,} [0-9]+$/ ) | 
| 115 |  |  |  |  |  |  | { | 
| 116 | 0 |  |  |  |  |  | die( "Not a valid fax number\n". | 
| 117 |  |  |  |  |  |  | "Pixelletter only accepts fax numbers formated like this example:\n+49 89 12345678\n". | 
| 118 |  |  |  |  |  |  | "You must have a '+' before the country code, and a space either side\n". | 
| 119 |  |  |  |  |  |  | "of the area code\n" ); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 | 0 |  |  |  |  | if( $self->filecount() < 1 ) | 
| 123 |  |  |  |  |  |  | { | 
| 124 | 0 |  |  |  |  |  | die( "No files to send...\n" ); | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  |  | my $xml = qq! | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | $self->{username} | 
| 131 |  |  |  |  |  |  | $self->{password} | 
| 132 |  |  |  |  |  |  | ja | 
| 133 |  |  |  |  |  |  | ja | 
| 134 |  |  |  |  |  |  | $self->{test_mode} | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | upload | 
| 139 |  |  |  |  |  |  | 2 | 
| 140 |  |  |  |  |  |  | $fax_number | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | !; | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 0 |  |  |  |  |  | $self->_submitForm( $xml ); | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | sub sendPost | 
| 150 |  |  |  |  |  |  | { | 
| 151 | 0 |  |  | 0 | 1 |  | my( $self, $post_center, $dest_country ) = @_; | 
| 152 |  |  |  |  |  |  |  | 
| 153 | 0 | 0 | 0 |  |  |  | if( ! $post_center || $post_center !~ m/^\d$/ || $post_center < 1 || $post_center > 3 ) | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 154 |  |  |  |  |  |  | { | 
| 155 | 0 |  |  |  |  |  | die( "Not a valid post center id.  Valid are 1 to 3.\n" ); | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | # Check the destination country is a known ISO abbreviation | 
| 159 | 0 | 0 | 0 |  |  |  | if( ! $dest_country || $dest_country !~ m/^\w{2}$/ || ! code2country( $dest_country ) ) | 
|  |  |  | 0 |  |  |  |  | 
| 160 |  |  |  |  |  |  | { | 
| 161 | 0 |  |  |  |  |  | die( "Not a valid destination country code.  Please use 2 character ISO3166 codes.\n" ); | 
| 162 |  |  |  |  |  |  | } | 
| 163 | 0 |  |  |  |  |  | $dest_country = uc( $dest_country ); | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 0 | 0 |  |  |  |  | if( $self->filecount() < 1 ) | 
| 166 |  |  |  |  |  |  | { | 
| 167 | 0 |  |  |  |  |  | die( "No files to send...\n" ); | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  |  | 
| 170 | 0 |  |  |  |  |  | my $xml = qq! | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | $self->{username} | 
| 174 |  |  |  |  |  |  | $self->{password} | 
| 175 |  |  |  |  |  |  | ja | 
| 176 |  |  |  |  |  |  | ja | 
| 177 |  |  |  |  |  |  | $self->{test_mode} | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | upload | 
| 182 |  |  |  |  |  |  | $post_center | 
| 183 |  |  |  |  |  |  | $dest_country | 
| 184 |  |  |  |  |  |  | 1 | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | !; | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 0 |  |  |  |  |  | $self->_submitForm( $xml ); | 
| 191 |  |  |  |  |  |  | } | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | sub _submitForm | 
| 195 |  |  |  |  |  |  | { | 
| 196 | 0 |  |  | 0 |  |  | my( $self, $xml ) = @_; | 
| 197 | 0 |  |  |  |  |  | my %form = ( 'xml' => $xml ); | 
| 198 | 0 |  |  |  |  |  | my $file_idx = 0; | 
| 199 | 0 |  |  |  |  |  | foreach( @{ $self->files() } ) | 
|  | 0 |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | { | 
| 201 | 0 |  |  |  |  |  | $form{'uploadfile'.$file_idx} = $_; | 
| 202 | 0 |  |  |  |  |  | $file_idx++; | 
| 203 |  |  |  |  |  |  | } | 
| 204 |  |  |  |  |  |  |  | 
| 205 | 0 |  |  |  |  |  | my $response = $self->{user_agent}->post( $self->{url}, Content_Type => 'multipart/form-data', Content => \%form ); | 
| 206 | 0 | 0 |  |  |  |  | unless ($response->is_success) | 
| 207 |  |  |  |  |  |  | { | 
| 208 | 0 |  |  |  |  |  | die( "Error connecting to server: " . $response->status_line . "\n" ); | 
| 209 |  |  |  |  |  |  | } | 
| 210 |  |  |  |  |  |  |  | 
| 211 | 0 |  |  |  |  |  | my $response_xml = $response->content; | 
| 212 | 0 | 0 |  |  |  |  | if( $response_xml =~ m/result code\=\"(\d*)\".*\(.*?)\<\/msg\>/s ) | 
| 213 |  |  |  |  |  |  | { | 
| 214 | 0 | 0 |  |  |  |  | if( $1 == 100 ) | 
| 215 |  |  |  |  |  |  | { | 
| 216 | 0 |  |  |  |  |  | return( $1, $2 ); | 
| 217 |  |  |  |  |  |  | } | 
| 218 |  |  |  |  |  |  | else | 
| 219 |  |  |  |  |  |  | { | 
| 220 | 0 |  |  |  |  |  | die( "Send failed ($1): $2\n" ); | 
| 221 |  |  |  |  |  |  | } | 
| 222 |  |  |  |  |  |  | } | 
| 223 | 0 |  |  |  |  |  | die( "Send failed:\n$response_xml\n" ); | 
| 224 |  |  |  |  |  |  | } | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | __END__ |