RT# 83450 - fixed rateplan export
[freeside.git] / FS / FS / part_export / www_plesk.pm
1 package FS::part_export::www_plesk;
2
3 use vars qw(@ISA %info);
4 use Tie::IxHash;
5 use FS::part_export;
6
7 @ISA = qw(FS::part_export);
8
9 tie my %options, 'Tie::IxHash',
10   'URL'       => { label=>'URL' },
11   'login'     => { label=>'Login' },
12   'password'  => { label=>'Password' },
13   'template'  => { label=>'Domain Template' },
14   'web'       => { label=>'Host Website',
15                     type=>'checkbox'          },
16   'debug'     => { label=>'Enable debugging',
17                     type=>'checkbox'          },
18 ;
19
20 %info = (
21   'svc'        => 'svc_www',
22   'desc'       => 'Real-time export to Plesk managed hosting service',
23   'options'    => \%options,
24   'no_machine' => 1,
25   'notes'      => <<'END'
26 Real-time export to
27 <a href="http://www.swsoft.com/">Plesk</a> managed server.
28 Requires installation of
29 <a href="http://search.cpan.org/dist/Net-Plesk">Net::Plesk</a>
30 from CPAN and proper <a href="http://www.freeside.biz/mediawiki/index.php/Freeside:1.7:Documentation:Administration:www_plesk.pm">configuration</a>.
31 END
32 );
33
34 sub rebless { shift; }
35
36 # experiment: want the status of these right away (don't want account to
37 # create or whatever and then get error in the queue from dup username or
38 # something), so no queueing
39
40 sub _export_insert {
41   my( $self, $www ) = ( shift, shift );
42
43   eval "use Net::Plesk;";
44   return $@ if $@;
45
46   my $plesk = new Net::Plesk (
47     'POST'              => $self->option('URL'),
48     ':HTTP_AUTH_LOGIN'  => $self->option('login'),
49     ':HTTP_AUTH_PASSWD' => $self->option('password'),
50   );
51
52   my $gcresp = $plesk->client_get( $www->svc_acct->username );
53   return $gcresp->errortext
54     unless $gcresp->is_success;
55
56   unless ($gcresp->id) {
57     my $cust_main = $www->cust_svc->cust_pkg->cust_main;
58     $gcresp = $plesk->client_add( $cust_main->name,
59                                   $www->svc_acct->username,
60                                   $www->svc_acct->_password,
61                                   $cust_main->daytime,
62                                   $cust_main->fax,
63                                   $cust_main->invoicing_list->[0],
64                                   $cust_main->address1 . $cust_main->address2,
65                                   $cust_main->city,
66                                   $cust_main->state,
67                                   $cust_main->zip,
68                                   $cust_main->country,
69                                 );
70     return $gcresp->errortext
71       unless $gcresp->is_success;
72   }
73
74   $plesk->client_ippool_add_ip ( $gcresp->id,
75                                  $www->domain_record->recdata,
76                                 );
77
78   if ($self->option('web')) {
79     $self->_plesk_command( 'domain_add', 
80                            $www->domain_record->svc_domain->domain,
81                            $gcresp->id,
82                            $www->domain_record->recdata,
83                            $self->option('template')?$self->option('template'):'',
84                            $www->svc_acct->username,
85                            $www->svc_acct->_password,
86                          );
87   }else{
88     $self->_plesk_command( 'domain_add', 
89                            $www->domain_record->svc_domain->domain,
90                            $gcresp->id,
91                            $www->domain_record->recdata,
92                            $self->option('template')?$self->option('template'):'',
93                          );
94   }
95 }
96
97 sub _plesk_command {
98   my( $self, $method, @args ) = @_;
99
100   eval "use Net::Plesk;";
101   return $@ if $@;
102   
103   local($Net::Plesk::DEBUG) = 1
104     if $self->option('debug');
105
106   my $plesk = new Net::Plesk (
107     'POST'              => $self->option('URL'),
108     ':HTTP_AUTH_LOGIN'  => $self->option('login'),
109     ':HTTP_AUTH_PASSWD' => $self->option('password'),
110   );
111
112   my $response = $plesk->$method(@args);
113   return $response->errortext unless $response->is_success;
114   '';
115
116 }
117
118 sub _export_replace {
119   my( $self, $new, $old ) = (shift, shift, shift);
120
121   return "can't change domain with Plesk"
122     if $old->domain_record->svc_domain->domain ne
123        $new->domain_record->svc_domain->domain;
124
125   return "can't change client with Plesk"
126     if $old->svc_acct->username ne
127        $new->svc_acct->username;
128
129   return '';
130
131 }
132
133 sub _export_delete {
134   my( $self, $www ) = ( shift, shift );
135   $self->_plesk_command( 'domain_del', $www->domain_record->svc_domain->domain);
136 }
137
138 1;
139