package IIfe7ed66bde3aa68899b7883eab4be165::mod_CPanel; use strict; use base 'JinPluginBaseExtScript'; use Data::Dumper; use LWP::UserAgent; use HTTP::Headers; sub init { my $this = shift()->SUPER::init(@_); return $this->msg('error.missing_config') if grep { ! $this->{$_} } qw/api_url domain plan login password/; my $response = $this->callAPI('listpkgs'); return $this->msg('error.connect', $response->message) if $response->message !~ /OK/; my %packages = map { $_ =~ s/%20/ /g; $_ => 1 } map {m!<name>(.+)</name>!sgi} $response->content =~ m!<package>(.+?)</package>!sgi; return $this->msg('error.invalid_package', $this->{plan}) unless $packages{$this->{plan}}; return; } sub addUser { my $this = shift; my $user = shift; my $username = $this->getUsernameForUser($user); my %accts = $this->listaccts; my %options; my $function; if($accts{$username}) { return unless $accts{$username}{suspended}; $function = "unsuspendacct"; %options = (user => $username); } else { $function = "createacct"; %options = ( username => $username, password => $user->{passwd}, plan => $this->{plan}, domain => "$user->{id}.$this->{domain}", ip => "n", cgi => "y", frontpage => "n", contactemail => $user->{email}, cpmod => "x3", useregns => 0, reseller => 0 ); $options{quota} = $this->{quota} if $this->{quota}; } my $response = $this->callAPI($function, %options); return; } sub updateUser { my $this = shift; my $user = shift; return; } sub deleteUser { my $this = shift; my $user = shift; $this->suspendUser($user); return; } sub suspendUser { my $this = shift; my $user = shift; my $username = $this->getUsernameForUser($user); my %options = ( user => $username, reason => "Delavo", ); my $response = $this->callAPI('suspendacct', %options); return; } sub getUsernameForUser { my $this = shift; my $user = shift; return "u$user->{id}"; } sub listaccts { my $this = shift; my %options = (searchtype => "package"); my $response = $this->callAPI('listaccts', %options); my @accounts = $response->content =~ m!<acct>(.+?)</acct>!sgi; my %accts; for my $a (@accounts) { my ($user) = $a =~ m|<user>(.+)</user>|sig; ($accts{$user}{suspended}) = $a =~ m|<suspended>(.+)</suspended>|sig; ($accts{$user}{domain}) = $a =~ m|<domain>(.+)</domain>|sig; ($accts{$user}{plan}) = $a =~ m|<plan>(.+)</plan>|sig; $accts{$user}{plan} =~ s/%20/ /g; } return %accts; } sub callAPI { my $this = shift; my ($function, %options) = @_; my $apiURL = $this->{api_url} . ($this->{api_url} =~ m!/$! ? "" :"/") . $function; $apiURL .= "?" . join('&', map { "$_=$options{$_}" } keys %options) if %options; my $headers = HTTP::Headers->new; $headers->authorization_basic($this->{login}, $this->{password}); my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; $ua->default_headers($headers); my $response = $ua->post($apiURL); warn "cPanel/WHM API URL: " . $apiURL; warn "OPTIONS: " . Dumper(%options); warn "RESPONSE: " . $response->message; warn "RESPONSE: " . $response->content; return $response; } 1;