lib/API/DirectAdmin.pm

filter: Statementstmt 75.3% 104 / 138 Branchbran 46.1% 24 / 52 Conditioncond 51.7% 15 / 29 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 89.4% 17 / 19 Podpod 0.0% 0 / 11 totaltotal 61.7% 163 / 264 CC 43 SCAR 23.8
CC
43
Cov
65%
mk_query_to_server
42.0
parse_answer
30.0
mk_full_query_string
9.0
CRAP 10.8
« blib/lib/API/DirectAdmin/DNS.pm ↑ Summary lib/API/DirectAdmin/Mysql.pm »
1 package API::DirectAdmin;
2  
3 1 ▶use Modern::Perl '2010';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = 0.09;
11 1 our $DEBUG = '';
12 1 our $FAKE_ANSWER = '';
13  
14 # for init subclasses
15 1 init_components(
16 domain => 'Domain',
17 mysql => 'Mysql',
18 user => 'User',
19 dns => 'DNS',
20 ip => 'Ip',
21 );
22  
23 # init
24 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod undocumented
25 1 my $class = shift;
26 1 ▶ $class = ref ($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
27
28 1 my $self = {
29 auth_user => '',
30 auth_passwd => '',
31 host => '',
32 ip => '',
33 debug => $DEBUG,
34 allow_https => 1,
35 fake_answer => $FAKE_ANSWER,
36 (@_)
37 };
38  
39 1 ▶ confess "Required auth_user!" unless $self->{auth_user};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auth_user"} 0 1 1
40 1 ▶ confess "Required auth_passwd!" unless $self->{auth_passwd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auth_passwd"} 0 1 1
41 1 ▶ confess "Required host!" unless $self->{host};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"host"} 0 1 1
42  
43 1 return bless $self, $class;
44 }
45  
46 # initialize components
47 1 ▶sub init_components {
Subroutine init_components: called CC=2 SCAR=7.0
Pod undocumented
48 1 my ( %c ) = @_;
49 1 my $caller = caller;
50  
51 1 for my $alias ( keys %c ) {
52  
53 5 my $item = $c{$alias};
54  
55 13 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
56 13 my( $self ) = @_;
57 13 ▶ $self->{"_$alias"} ||= $self->load_component($item);
Condition66%2/3
$$self{"_$alias"} ||= $self->load_component($item)
l!l&&r!l&&!r
940
MC/DC0%0/2
$$self{"_$alias"} ||= $self->load_component($item)
$$self{"_$alias"} $self->load_component($item)
Truth table66%2/3
A ||= B
A= $$self{"_$alias"}B= $self->load_component($item)
ABresult
1X1
011
000
58 13 ▶ return $self->{"_$alias"} || confess "Not implemented!";
Condition50%1/2
$$self{"_$alias"} || confess('Not implemented!')
l!l
130
MC/DC0%0/1
$$self{"_$alias"} || confess('Not implemented!')
$$self{"_$alias"}
Truth table50%1/2
A
A= $$self{"_$alias"}
Aresult
11
00
59 5 };
60
61 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62
63 5 *{"$caller\::$alias"} = $sub;
64 }
65 }
66  
67 # loads component package and creates object
68 4 ▶sub load_component {
Subroutine load_component: called CC=2 SCAR=7.0
Pod undocumented
69 4 my ( $self, $item ) = @_;
70  
71 4 my $pkg = ref($self) . '::' . $item;
72  
73 4 my $module = "$pkg.pm";
74 4 $module =~ s/::/\//g;
75  
76 4 local $@;
77 4 eval { require $module };
78 4 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
79 0 confess "Failed to load $pkg: $@";
80 }
81  
82 4 return $pkg->new(directadmin => $self);
83  
84 }
85  
86 # Filter hash
87 # STATIC(HASHREF: hash, ARRREF: allowed_keys)
88 # RETURN: hashref only with allowed keys
89 17 ▶sub filter_hash {
Subroutine filter_hash: called CC=7 SCAR=20.5
Pod undocumented
90 17 my ($self, $hash, $allowed_keys) = @_;
91
92 17 ▶ return {} unless defined $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash 3 14 17
93
94 14 ▶ confess "Wrong params" unless ref $hash eq 'HASH' && ref $allowed_keys eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" and ref $allowed_keys eq "ARRAY" 0 14 14
Condition33%1/3
ref $hash eq "HASH" and ref $allowed_keys eq "ARRAY"
!ll&&!rl&&r
0014
MC/DC0%0/2
ref $hash eq "HASH" and ref $allowed_keys eq "ARRAY"
ref $hash eq "HASH" ref $allowed_keys eq "ARRAY"
Truth table33%1/3
A and B
A= ref $hash eq "HASH"B= ref $allowed_keys eq "ARRAY"
ABresult
0X0
100
111
95  
96 14 my $new_hash = { };
97  
98 14 foreach my $allowed_key (@$allowed_keys) {
99 120 ▶ if (exists $hash->{$allowed_key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $hash->{$allowed_key}) { } 42 78 120
elsif (exists $$hash{lc $allowed_key}) { } 0 78 78
100 42 $new_hash->{$allowed_key} = $hash->{$allowed_key};
101 }
102 elsif (exists $hash->{lc $allowed_key}) {
103 0 $new_hash->{$allowed_key} = $hash->{lc $allowed_key};
104 };
105 }
106  
107 14 return $new_hash;
108 }
109  
110 # all params derived from get_auth_hash
111 13 ▶sub query {
Subroutine query: called CC=7 SCAR=19.7
Pod undocumented
112 13 my ( $self, %params ) = @_;
113  
114 13 my $command = delete $params{command};
115 13 ▶ my $fields = $params{allowed_fields} || '';
Condition100%2/2
$params{'allowed_fields'} || ''
l!l
103
MC/DC100%1/1
$params{'allowed_fields'} || ''
$params{'allowed_fields'}
Truth table100%2/2
A
A= $params{'allowed_fields'}
Aresult
11
00
116  
117 13 my $allowed_fields;
118 13 ▶ warn 'query_abstract ' . Dumper( \%params ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 13 13
119  
120 13 ▶ confess "Empty command" unless $command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 13 13
121  
122 13 $fields = "host port auth_user auth_passwd method allow_https command $fields";
123 13 @$allowed_fields = split /\s+/, $fields;
124  
125 13 my $params = $self->filter_hash( $params{params}, $allowed_fields );
126  
127 13 my $query_string = $self->mk_full_query_string( {
128 command => $command,
129 %$params,
130 } );
131  
132 13 ▶ carp Dumper $query_string if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 13 13
133
134 my $server_answer = $self->process_query(
135 13 ▶ method => $params{method} || 'GET',
Condition100%2/2
$params{'method'} || 'GET'
l!l
85
MC/DC100%1/1
$params{'method'} || 'GET'
$params{'method'}
Truth table100%2/2
A
A= $params{'method'}
Aresult
11
00
136 query_string => $query_string,
137 params => $params,
138 );
139
140 13 ▶ carp Dumper $server_answer if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 13 13
141  
142 13 return $server_answer;
143 }
144  
145 # Kill slashes at start / end string
146 # STATIC(STRING:input_string)
147 17 ▶sub kill_start_end_slashes {
Subroutine kill_start_end_slashes: called CC=2 SCAR=6.9
Pod undocumented
148 17 my ($self ) = @_;
149  
150 17 for ( $self->{host} ) {
151 17 s/^\/+//sgi;
152 17 s/\/+$//sgi;
153 }
154  
155 17 return 1;
156 }
157  
158 # Make full query string
159 # STATIC(HASHREF: params)
160 # params:
161 # host*
162 # port*
163 # param1
164 # param2
165 # ...
166 17 ▶sub mk_full_query_string {
Subroutine mk_full_query_string: called CC=8 SCAR=22.0
Pod undocumented
167 17 my ( $self, $params ) = @_;
168  
169 confess "Wrong params: " . Dumper( $params ) unless ref $params eq 'HASH'
170 && scalar keys %$params
171 && $self->{host}
172 17 ▶ && $params->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" and scalar keys %$params and $self->{"host"} and $params->{"command"} 0 17 17
Condition37%3/8
ref $params eq "HASH" and scalar keys %$params and $self->{"host"} and $params->{"command"}
!ll&&!rl&&r
0017
ref $params eq "HASH" and scalar keys %$params and $self->{"host"}
!ll&&!rl&&r
0017
ref $params eq "HASH" and scalar keys %$params
!ll
017
MC/DC0%0/3
ref $params eq "HASH" and scalar keys %$params and $self->{"host"} and $params->{"command"}
ref $params eq "HASH" $self->{"host"} $params->{"command"}
Truth table25%1/4
A and B and C
A= ref $params eq "HASH"B= $self->{"host"}C= $params->{"command"}
ABCresult
0XX0
10X0
1100
1111
173  
174 17 ▶ my $allow_https = defined $params->{allow_https} ? $params->{allow_https} : $self->{allow_https};
Branch: 1 branch
BranchTrueFalseTotal
defined $params->{'allow_https'} ? : 2 15 17
175 17 delete $params->{allow_https};
176
177 17 my $host = $self->{host};
178 17 ▶ my $port = $self->{port} || 2222;
Condition50%1/2
$self->{'port'} || 2222
l!l
017
MC/DC0%0/1
$self->{'port'} || 2222
$self->{'port'}
Truth table50%1/2
A
A= $self->{'port'}
Aresult
11
00
179 17 my $command = delete $params->{command};
180 17 my $auth_user = $self->{auth_user};
181 17 my $auth_passwd = $self->{auth_passwd};
182  
183 17 $self->kill_start_end_slashes();
184  
185 17 ▶ my $query_path = ( $allow_https ? 'https' : 'http' ) . "://$auth_user:$auth_passwd\@$host:$port/$command?";
Branch: 1 branch
BranchTrueFalseTotal
$allow_https ? : 15 2 17
186 17 return $query_path . $self->mk_query_string($params);
187 }
188  
189 # Make query string
190 # STATIC(HASHREF: params)
191 24 ▶sub mk_query_string {
Subroutine mk_query_string: called CC=3 SCAR=11.0
Pod undocumented
192 24 my ($self, $params) = @_;
193  
194 24 ▶ return '' unless ref $params eq 'HASH' && scalar keys %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params eq "HASH" and scalar keys %$params 10 14 24
Condition100%2/2
ref $params eq "HASH" and scalar keys %$params
!ll
321
MC/DC100%1/1
ref $params eq "HASH" and scalar keys %$params
ref $params eq "HASH"
Truth table100%2/2
A
A= ref $params eq "HASH"
Aresult
00
11
195  
196 14 my %params = %$params;
197  
198 14 my $result = join '&', map { "$_=$params{$_}" } sort keys %params;
199  
200 14 return $result;
201 }
202  
203 # Get + deparse
204 # STATIC(STRING: query_string)
205 13 ▶sub process_query {
Subroutine process_query: called CC=4 SCAR=14.3
Pod undocumented
206 13 my ( $self, %params ) = @_;
207  
208 13 my $query_string = $params{query_string};
209 13 my $method = $params{method};
210  
211 13 ▶ confess "Empty query string" unless $query_string;
Branch: 1 branch
BranchTrueFalseTotal
unless $query_string 0 13 13
212  
213 13 ▶ my $answer = $self->{fake_answer} ? $self->{fake_answer} : $self->mk_query_to_server( $method, $query_string, $params{params} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'fake_answer'} ? : 13 0 13
214 13 ▶ carp $answer if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 13 13
215  
216 13 return $answer;
217 }
218  
219 # Make request to server and get answer
220 # STATIC (STRING: query_string)
221 0 ▶sub mk_query_to_server {
Subroutine mk_query_to_server: not called CC=6 SCAR=37.4
Pod undocumented
222 0 my ( $self, $method, $url, $params ) = @_;
223
224 0 ▶ unless ( $method ~~ [ qw( POST GET ) ] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method ~~ ["POST", "GET"]) 0 0 0
225 0 confess "Unknown request method: '$method'";
226 }
227  
228 0 ▶ confess "URL is empty" unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
229  
230 0 my $content;
231 0 my $ua = LWP::UserAgent->new;
232 0 my $request = HTTP::Request->new( $method, $url );
233
234 0 ▶ if ( $method eq 'GET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'GET') { } 0 0 0
235 0 my $response = $ua->request( $request );
236 0 $content = $response->content;
237 }
238 else { # Temporary URL for making request
239 0 my $temp_uri = URI->new('http:');
240 0 $temp_uri->query_form( $params );
241 0 $request->content( $temp_uri->query );
242 0 $request->content_type('application/x-www-form-urlencoded');
243 0 my $response = $ua->request($request);
244 0 $content = $response->content;
245 }
246
247 0 ▶ warn "Answer: " . $content if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
248
249 0 ▶ return $content if $params->{noparse};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"noparse"} 0 0 0
250 0 return $self->parse_answer($content);
251 }
252  
253 # Parse answer
254 0 ▶sub parse_answer {
Subroutine parse_answer: not called CC=5 SCAR=34.0
Pod undocumented
255 0 my ($self, $response) = @_;
256  
257 0 ▶ return '' unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
258
259 0 my %answer;
260 0 $response =~ s/&#60br&#62|&#\d+//ig; # Some trash from answer
261 0 $response =~ s/\n+/\n/ig;
262 0 my @params = split /&/, $response;
263
264 0 foreach my $param ( @params ) {
265 0 my ($key, $value) = split /=/, $param;
266 0 ▶ if ( $key =~ /(.*)\[\]/ ) { # lists
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(.*)\[\]/) { } 0 0 0
267 0 push @{ $answer{$1} }, $value;
268 }
269 else {
270 0 $answer{$key} = $value;
271 }
272 }
273  
274 0 ▶ return \%answer || '';
Condition0%0/2
\%answer || ''
l!l
00
MC/DC0%0/1
\%answer || ''
\%answer
Truth table0%0/2
A
A= \%answer
Aresult
11
00
275 }
276  
277 1 1;
278  
279 __END__
« blib/lib/API/DirectAdmin/DNS.pm ↑ Summary lib/API/DirectAdmin/Mysql.pm »