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,
63 Send an HTTP or HTTPS GET or POST to the specified URL. For HTTPS support,
64 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
65 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
70 sub rebless { shift; }
74 $self->_export_command('insert', @_);
79 $self->_export_command('delete', @_);
84 $self->_export_command('suspend', @_);
87 sub _export_unsuspend {
89 $self->_export_command('unsuspend', @_);
93 my( $self, $action, $svc_x ) = ( shift, shift, shift );
95 return unless $self->option("${action}_data");
97 my $cust_main = $svc_x->cust_main or return;
99 $self->http_queue( $svc_x->svcnum,
100 ( $self->option('ssl_no_verify') ? 'ssl_no_verify' : '' ),
101 $self->option('method'),
102 $self->option('url'),
103 $self->option('success_regexp'),
105 /^\s*(\S+)\s+(.*)$/ or /()()/;
106 my( $field, $value_expression ) = ( $1, $2 );
107 my $value = eval $value_expression;
110 } split(/\n/, $self->option("${action}_data") )
115 sub _export_replace {
116 my( $self, $new, $old ) = ( shift, shift, shift );
118 return unless $self->option('replace_data');
120 my $new_cust_main = $new->cust_main or return;
121 my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
123 $self->http_queue( $new->svcnum,
124 ( $self->option('ssl_no_verify') ? 'ssl_no_verify' : '' ),
125 $self->option('method'),
126 $self->option('url'),
127 $self->option('success_regexp'),
129 /^\s*(\S+)\s+(.*)$/ or /()()/;
130 my( $field, $value_expression ) = ( $1, $2 );
131 my $value = eval $value_expression;
134 } split(/\n/, $self->option('replace_data') )
140 my($self, $svcnum) = (shift, shift);
141 my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
142 $queue->svcnum($svcnum) if $svcnum;
143 $queue->insert( @_ );
147 my $ssl_no_verify = ( $_[0] eq 'ssl_no_verify' || $_[0] eq '' ) ? shift : '';
148 my($method, $url, $success_regexp, @data) = @_;
150 $method = lc($method);
152 eval "use LWP::UserAgent;";
153 die "using LWP::UserAgent: $@" if $@;
154 eval "use HTTP::Request::Common;";
155 die "using HTTP::Request::Common: $@" if $@;
158 push @lwp_opts, 'ssl_opts'=>{ 'verify_hostname'=>0 } if $ssl_no_verify;
159 my $ua = LWP::UserAgent->new(@lwp_opts);
161 #my $response = $ua->$method(
163 # 'Content-Type'=>'application/x-www-form-urlencoded'
165 my $req = HTTP::Request::Common::POST( $url, \@data );
166 my $response = $ua->request($req);
168 die $response->error_as_HTML if $response->is_error;
170 if(length($success_regexp) > 1) {
171 my $response_content = $response->content;
172 die $response_content unless $response_content =~ /$success_regexp/;