| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::Antigate::V1; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 1229 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 90 |  | 
| 4 | 2 |  |  | 2 |  | 11 | use parent 'WebService::Antigate'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 43 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | my %MESSAGES = ( | 
| 7 |  |  |  |  |  |  | 'ERROR_KEY_DOES_NOT_EXIST'       => 'wrong service key used', | 
| 8 |  |  |  |  |  |  | 'ERROR_WRONG_USER_KEY'           => 'wrong service key used', | 
| 9 |  |  |  |  |  |  | 'ERROR_NO_SLOT_AVAILABLE'        => 'all recognizers are busy, try later', | 
| 10 |  |  |  |  |  |  | 'ERROR_ZERO_CAPTCHA_FILESIZE'    => 'uploaded captcha size is zero', | 
| 11 |  |  |  |  |  |  | 'ERROR_TOO_BIG_CAPTCHA_FILESIZE' => 'uploaded captcha size is grater than 90 Kb', | 
| 12 |  |  |  |  |  |  | 'ERROR_WRONG_FILE_EXTENSION'     => 'wrong extension of the uploaded captcha, allowed extensions are gif, jpg, png', | 
| 13 |  |  |  |  |  |  | 'ERROR_IP_NOT_ALLOWED'           => 'this ip not allowed to use this account', | 
| 14 |  |  |  |  |  |  | 'ERROR_WRONG_ID_FORMAT'          => 'captcha id should be number', | 
| 15 |  |  |  |  |  |  | 'ERROR_NO_SUCH_CAPCHA_ID'        => 'no such captcha id in the database', | 
| 16 |  |  |  |  |  |  | 'ERROR_URL_METHOD_FORBIDDEN'     => 'this upload method is already not supported', | 
| 17 |  |  |  |  |  |  | 'ERROR_IMAGE_IS_NOT_PNG'         => 'captcha is not correct png file', | 
| 18 |  |  |  |  |  |  | 'ERROR_IMAGE_IS_NOT_JPEG'        => 'captcha is not correct jpeg file', | 
| 19 |  |  |  |  |  |  | 'ERROR_IMAGE_IS_NOT_GIF'         => 'captcha is not correct gif file', | 
| 20 |  |  |  |  |  |  | 'ERROR_ZERO_BALANCE'             => 'you have a zero balance', | 
| 21 |  |  |  |  |  |  | 'CAPCHA_NOT_READY'               => 'captcha is not recognized yet', | 
| 22 |  |  |  |  |  |  | 'OK_REPORT_RECORDED'             => 'your abuse recorded', | 
| 23 |  |  |  |  |  |  | 'ERROR_CAPTCHA_UNSOLVABLE'       => 'captcha can\'t be recognized', | 
| 24 |  |  |  |  |  |  | 'ERROR_BAD_DUPLICATES'           => 'captcha duplicates limit reached' | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub try_upload { | 
| 28 | 2 |  |  | 2 | 1 | 6 | my ($self, %opts) = @_; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | Carp::croak "Specified captcha file doesn't exist" | 
| 31 | 2 | 50 | 66 |  |  | 28 | if defined($opts{file}) && ! -e $opts{file}; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 2 |  |  |  |  | 5 | my $file; | 
| 34 |  |  |  |  |  |  | my $response = $self->{ua}->post | 
| 35 |  |  |  |  |  |  | ( | 
| 36 |  |  |  |  |  |  | "$self->{scheme}://$self->{subdomain}$self->{domain}/in.php", | 
| 37 |  |  |  |  |  |  | defined $opts{file} || defined $opts{content} | 
| 38 |  |  |  |  |  |  | ? ( | 
| 39 |  |  |  |  |  |  | Content_Type => "form-data", | 
| 40 |  |  |  |  |  |  | Content    => | 
| 41 |  |  |  |  |  |  | [ | 
| 42 |  |  |  |  |  |  | key    => $self->{key}, | 
| 43 |  |  |  |  |  |  | method => 'post', | 
| 44 |  |  |  |  |  |  | file   => | 
| 45 |  |  |  |  |  |  | [ | 
| 46 |  |  |  |  |  |  | defined($opts{file}) ? | 
| 47 |  |  |  |  |  |  | ( | 
| 48 |  |  |  |  |  |  | $file = delete $opts{file}, | 
| 49 |  |  |  |  |  |  | defined($opts{name}) ? | 
| 50 |  |  |  |  |  |  | delete $opts{name} | 
| 51 |  |  |  |  |  |  | : | 
| 52 |  |  |  |  |  |  | $file !~ /\..{1,5}$/ ? # filename without extension | 
| 53 |  |  |  |  |  |  | $self->_name_by_file_signature($file) | 
| 54 |  |  |  |  |  |  | : | 
| 55 |  |  |  |  |  |  | undef | 
| 56 |  |  |  |  |  |  | ) | 
| 57 |  |  |  |  |  |  | : | 
| 58 |  |  |  |  |  |  | ( | 
| 59 |  |  |  |  |  |  | undef, | 
| 60 |  |  |  |  |  |  | defined($opts{name}) ? | 
| 61 |  |  |  |  |  |  | delete $opts{name} | 
| 62 |  |  |  |  |  |  | : | 
| 63 |  |  |  |  |  |  | $self->_name_by_signature($opts{content}), | 
| 64 |  |  |  |  |  |  | Content => delete $opts{content} | 
| 65 |  |  |  |  |  |  | ) | 
| 66 |  |  |  |  |  |  | ], | 
| 67 |  |  |  |  |  |  | %opts | 
| 68 |  |  |  |  |  |  | ] | 
| 69 |  |  |  |  |  |  | ) | 
| 70 | 2 | 50 | 66 |  |  | 46 | : { key => $self->{key}, %opts } | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | ); | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 2 | 50 |  |  |  | 105400 | unless($response->is_success) { | 
| 74 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 75 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 76 | 0 |  |  |  |  | 0 | return undef; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 2 |  |  |  |  | 28 | my $captcha_id; | 
| 80 | 2 | 50 |  |  |  | 13 | unless(($captcha_id) = $response->content =~ /OK\|(\d+)/) { | 
| 81 | 0 |  |  |  |  | 0 | $self->{errno}  = $response->content; | 
| 82 | 0 |  |  |  |  | 0 | $self->{errstr} = $MESSAGES{ $self->{errno} }; | 
| 83 | 0 |  |  |  |  | 0 | return undef; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 2 |  |  |  |  | 78 | return $self->{last_captcha_id} = $captcha_id; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub try_recognize { | 
| 90 | 2 |  |  | 2 | 1 | 19 | my ($self, $id) = @_; | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 2 | 50 |  |  |  | 22 | Carp::croak "Captcha id should be specified" unless defined $id; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 2 |  |  |  |  | 57 | my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=get&id=$id"); | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 2 | 50 |  |  |  | 11708 | unless($response->is_success) { | 
| 97 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 98 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 99 | 0 |  |  |  |  | 0 | return undef; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 2 |  |  |  |  | 28 | my $captcha_text; | 
| 103 | 2 | 100 |  |  |  | 11 | unless(($captcha_text) = $response->content =~ /OK\|(.+)/) { | 
| 104 | 1 |  |  |  |  | 22 | $self->{errno}  = $response->content; | 
| 105 | 1 |  |  |  |  | 20 | $self->{errstr} = $MESSAGES{ $self->{errno} }; | 
| 106 | 1 |  |  |  |  | 25 | return undef; | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 1 |  |  |  |  | 43 | return $captcha_text; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub abuse { | 
| 113 | 2 |  |  | 2 | 1 | 9 | my ($self, $id) = @_; | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 2 | 50 |  |  |  | 8 | Carp::croak "Captcha id should be specified" unless defined $id; | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 2 |  |  |  |  | 16 | my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=reportbad&id=$id"); | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 2 | 50 |  |  |  | 7676 | unless($response->is_success) { | 
| 120 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 121 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 122 | 0 |  |  |  |  | 0 | return undef; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 2 | 100 |  |  |  | 26 | unless($response->content eq 'OK_REPORT_RECORDED') { | 
| 126 | 1 |  |  |  |  | 14 | $self->{errno}  = $response->content; | 
| 127 | 1 |  |  |  |  | 13 | $self->{errstr} = $MESSAGES{ $self->{errno} }; | 
| 128 | 1 |  |  |  |  | 11 | return undef; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 1 |  |  |  |  | 25 | return 1; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | sub balance { | 
| 136 | 2 |  |  | 2 | 1 | 9 | my $self = shift; | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 2 |  |  |  |  | 15 | my $response = $self->{ua}->get("$self->{scheme}://$self->{subdomain}$self->{domain}/res.php?key=$self->{key}&action=getbalance"); | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 2 | 50 |  |  |  | 7956 | unless($response->is_success) { | 
| 141 | 0 |  |  |  |  | 0 | $self->{errno}  = 'HTTP_ERROR'; | 
| 142 | 0 |  |  |  |  | 0 | $self->{errstr} = $response->status_line; | 
| 143 | 0 |  |  |  |  | 0 | return undef; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 2 | 100 |  |  |  | 24 | if($response->content =~ /^ERROR_/) { | 
| 147 | 1 |  |  |  |  | 18 | $self->{errno}  = $response->content; | 
| 148 | 1 |  |  |  |  | 15 | $self->{errstr} = $MESSAGES{ $self->{errno} }; | 
| 149 | 1 |  |  |  |  | 11 | return undef; | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 1 |  |  |  |  | 17 | return $response->content; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | 1; | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | __END__ |