1 package FS::part_export::http;
3 use base qw( FS::part_export );
4 use vars qw( %options %info );
7 tie %options, 'Tie::IxHash',
8 'method' => { label =>'Method',
10 #options =>[qw(POST GET)],
13 'url' => { label => 'URL', default => 'http://', },
14 'ssl_no_verify' => { label => 'Skip SSL certificate validation',
18 label => 'Insert data',
21 'DomainName $svc_x->domain',
22 'Email ( grep { $_ !~ /^(POST|FAX)$/ } $svc_x->cust_svc->cust_pkg->cust_main->invoicing_list)[0]',
24 'reseller $svc_x->cust_svc->cust_pkg->part_pkg->pkg =~ /reseller/i',
28 label => 'Delete data',
34 label => 'Replace data',
40 label => 'Suspend data',
46 label => 'Unsuspend data',
52 label => 'Success Regexp',
58 'svc' => 'svc_domain',
59 'desc' => 'Send an HTTP or HTTPS GET or POST request',
60 'options' => \%options,
62 Send an HTTP or HTTPS GET or POST to the specified URL. For HTTPS support,
63 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
64 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
69 sub rebless { shift; }
73 $self->_export_command('insert', @_);
78 $self->_export_command('delete', @_);
83 $self->_export_command('suspend', @_);
86 sub _export_unsuspend {
88 $self->_export_command('unsuspend', @_);
92 my( $self, $action, $svc_x ) = ( shift, shift, shift );
94 return unless $self->option("${action}_data");
96 my $cust_main = $svc_x->table eq 'cust_main'
98 : $svc_x->cust_svc->cust_pkg->cust_main;
100 $self->http_queue( $svc_x->svcnum,
101 ( $self->option('ssl_no_verify') ? 'ssl_no_verify' : '' ),
102 $self->option('method'),
103 $self->option('url'),
104 $self->option('success_regexp'),
106 /^\s*(\S+)\s+(.*)$/ or /()()/;
107 my( $field, $value_expression ) = ( $1, $2 );
108 my $value = eval $value_expression;
111 } split(/\n/, $self->option("${action}_data") )
116 sub _export_replace {
117 my( $self, $new, $old ) = ( shift, shift, shift );
119 return unless $self->option('replace_data');
121 my $new_cust_main = $new->table eq 'cust_main'
123 : $new->cust_svc->cust_pkg->cust_main;
124 my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
126 $self->http_queue( $new->svcnum,
127 ( $self->option('ssl_no_verify') ? 'ssl_no_verify' : '' ),
128 $self->option('method'),
129 $self->option('url'),
130 $self->option('success_regexp'),
132 /^\s*(\S+)\s+(.*)$/ or /()()/;
133 my( $field, $value_expression ) = ( $1, $2 );
134 my $value = eval $value_expression;
137 } split(/\n/, $self->option('replace_data') )
143 my($self, $svcnum) = (shift, shift);
144 my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
145 $queue->svcnum($svcnum) if $svcnum;
146 $queue->insert( @_ );
150 my $ssl_no_verify = ( $_[0] eq 'ssl_no_verify' || $_[0] eq '' ) ? shift : '';
151 my($method, $url, $success_regexp, @data) = @_;
153 $method = lc($method);
155 eval "use LWP::UserAgent;";
156 die "using LWP::UserAgent: $@" if $@;
157 eval "use HTTP::Request::Common;";
158 die "using HTTP::Request::Common: $@" if $@;
161 push @lwp_opts, 'ssl_opts'=>{ 'verify_hostname'=>0 } if $ssl_no_verify;
162 my $ua = LWP::UserAgent->new(@lwp_opts);
164 #my $response = $ua->$method(
166 # 'Content-Type'=>'application/x-www-form-urlencoded'
168 my $req = HTTP::Request::Common::POST( $url, \@data );
169 my $response = $ua->request($req);
171 die $response->error_as_HTML if $response->is_error;
173 if(length($success_regexp) > 1) {
174 my $response_content = $response->content;
175 die $response_content unless $response_content =~ /$success_regexp/;