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