blob: 6b9a46e9daa457cd2dea135780212309dd624b71 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package Net::Plesk::Response;
use strict;
use XML::Simple;
use XML::XPath;
use XML::XPath::XMLParser;
=head1 NAME
Net::Plesk::Response - Plesk response object
=head1 SYNOPSIS
my $response = $plesk->some_method( $and, $args );
if ( $response->is_success ) {
my $id = $response->id;
#...
} else {
my $error = $response->error; #error code
my $errortext = $response->errortext; #error message
#...
}
=head1 DESCRIPTION
The "Net::Plesk::Response" class represents Plesk responses.
=cut
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {};
bless($self, $class);
my $encoding = ''; #default
my $data = shift;
if ($data =~ /^\<\?xml version=\"1.0\"(\s+encoding="([\w\-]*)")?\?\>(.*)$/s){
$encoding = $2; #don't actually do anything with this yet
$data = $3;
}else{
$data =~ s/[^\w\s]/ /g; # yes, we lose stuff
$data = '<?xml version="1.0"?>' .
'<packet version="' . $self->{'version'} . '">' .
"<system><status>error</status><errcode>500</errcode>" .
"<errtext>Malformed Plesk response:" . $data . "</errtext>".
"</system></packet>";
}
my $xp = XML::XPath->new(xml => $data);
my $nodeset = $xp->find('//result');
foreach my $node ($nodeset->get_nodelist) {
push @{$self->{'results'}}, XML::XPath::XMLParser::as_string($node);
}
$nodeset = $xp->find('//system');
foreach my $node ($nodeset->get_nodelist) {
my $parsed = XML::XPath::XMLParser::as_string($node);
$parsed =~ s/\<(\/?)system\>/<$1result>/ig;
push @{$self->{'results'}}, $parsed;
}
$self;
}
sub is_success {
my $self = shift;
my $status = 1;
foreach my $result (@{$self->{'results'}}) {
$status = (XMLin($result)->{'status'} eq 'ok');
last unless $status;
}
$status;
}
sub error {
my $self = shift;
my @errcode;
foreach my $result (@{$self->{'results'}}) {
my $errcode = XMLin($result)->{'errcode'};
push @errcode, $errcode if $errcode;
}
return wantarray ? @errcode : $errcode[0];
}
sub errortext {
my $self = shift;
my @errtext;
foreach my $result (@{$self->{'results'}}) {
my $errtext = XMLin($result)->{'errtext'};
push @errtext, $errtext if $errtext;
}
return wantarray ? @errtext : $errtext[0];
}
sub id {
my $self = shift;
my @id;
foreach my $result (@{$self->{'results'}}) {
my $id = XMLin($result)->{'id'};
push @id, $id if $id;
}
return wantarray ? @id : $id[0];
}
=head1 BUGS
Needs better documentation.
=head1 SEE ALSO
L<Net::Plesk>,
=head1 AUTHOR
Jeff Finucane E<lt>jeff@cmh.netE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2006 Jeff Finucane
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|