1 package FS::part_export::http;
3 use base qw( FS::part_export );
4 use vars qw( %options %info );
7 use HTTP::Request::Common qw( POST );
10 tie %options, 'Tie::IxHash',
11 'method' => { label =>'Method',
13 #options =>[qw(POST GET)],
16 'url' => { label => 'URL', default => 'http://', },
17 'ssl_no_verify' => { label => 'Skip SSL certificate validation',
21 label => 'Insert data',
24 'DomainName $svc_x->domain',
25 'Email ( grep { $_ !~ /^(POST|FAX)$/ } $svc_x->cust_svc->cust_pkg->cust_main->invoicing_list)[0]',
27 'reseller $svc_x->cust_svc->cust_pkg->part_pkg->pkg =~ /reseller/i',
31 label => 'Delete data',
37 label => 'Replace data',
43 label => 'Suspend data',
49 label => 'Unsuspend data',
55 label => 'Success Regexp',
61 'svc' => 'svc_domain',
62 'desc' => 'Send an HTTP or HTTPS GET or POST request',
63 'options' => \%options,
66 Send an HTTP or HTTPS GET or POST to the specified URL. For HTTPS support,
67 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
68 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
73 sub rebless { shift; }
77 $self->_export_command('insert', @_);
82 $self->_export_command('delete', @_);
87 $self->_export_command('suspend', @_);
90 sub _export_unsuspend {
92 $self->_export_command('unsuspend', @_);
96 my( $self, $action, $svc_x ) = ( shift, shift, shift );
98 return unless $self->option("${action}_data");
100 my $cust_main = $svc_x->cust_main or return;
102 $self->http_queue( $svc_x->svcnum,
103 ( $self->option('ssl_no_verify') ? 'ssl_no_verify' : '' ),
104 $self->option('method'),
105 $self->option('url'),
106 $self->option('success_regexp'),
108 /^\s*(\S+)\s+(.*)$/ or /()()/;
109 my( $field, $value_expression ) = ( $1, $2 );
110 my $value = eval $value_expression;
113 } split(/\n/, $self->option("${action}_data") )
118 sub _export_replace {
119 my( $self, $new, $old ) = ( shift, shift, shift );
121 return unless $self->option('replace_data');
123 my $new_cust_main = $new->cust_main or return;
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);
156 push @lwp_opts, 'ssl_opts' => {
157 verify_hostname => 0,
158 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
161 my $ua = LWP::UserAgent->new(@lwp_opts);
163 #my $response = $ua->$method(
165 # 'Content-Type'=>'application/x-www-form-urlencoded'
167 my $req = HTTP::Request::Common::POST( $url, \@data );
168 my $response = $ua->request($req);
170 die $response->error_as_HTML if $response->is_error;
172 if(length($success_regexp) > 1) {
173 my $response_content = $response->content;
174 die $response_content unless $response_content =~ /$success_regexp/;