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://', },
15 label => 'Insert data',
18 'DomainName $svc_x->domain',
19 'Email ( grep { $_ !~ /^(POST|FAX)$/ } $svc_x->cust_svc->cust_pkg->cust_main->invoicing_list)[0]',
21 'reseller $svc_x->cust_svc->cust_pkg->part_pkg->pkg =~ /reseller/i',
25 label => 'Delete data',
31 label => 'Replace data',
37 label => 'Suspend data',
43 label => 'Unsuspend data',
49 label => 'Success Regexp',
55 'svc' => 'svc_domain',
56 'desc' => 'Send an HTTP or HTTPS GET or POST request',
57 'options' => \%options,
60 Send an HTTP or HTTPS GET or POST to the specified URL. For HTTPS support,
61 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
62 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
67 sub rebless { shift; }
71 $self->_export_command('insert', @_);
76 $self->_export_command('delete', @_);
81 $self->_export_command('suspend', @_);
84 sub _export_unsuspend {
86 $self->_export_command('unsuspend', @_);
90 my( $self, $action, $svc_x ) = ( shift, shift, shift );
92 return unless $self->option("${action}_data");
94 my $cust_main = $svc_x->table eq 'cust_main'
96 : $svc_x->cust_svc->cust_pkg->cust_main;
98 $self->http_queue( $svc_x->svcnum,
99 $self->option('method'),
100 $self->option('url'),
101 $self->option('success_regexp'),
103 /^\s*(\S+)\s+(.*)$/ or /()()/;
104 my( $field, $value_expression ) = ( $1, $2 );
105 my $value = eval $value_expression;
108 } split(/\n/, $self->option("${action}_data") )
113 sub _export_replace {
114 my( $self, $new, $old ) = ( shift, shift, shift );
116 return unless $self->option('replace_data');
118 my $new_cust_main = $new->table eq 'cust_main'
120 : $new->cust_svc->cust_pkg->cust_main;
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('method'),
125 $self->option('url'),
126 $self->option('success_regexp'),
128 /^\s*(\S+)\s+(.*)$/ or /()()/;
129 my( $field, $value_expression ) = ( $1, $2 );
130 my $value = eval $value_expression;
133 } split(/\n/, $self->option('replace_data') )
139 my($self, $svcnum) = (shift, shift);
140 my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
141 $queue->svcnum($svcnum) if $svcnum;
142 $queue->insert( @_ );
146 my($method, $url, $success_regexp, @data) = @_;
148 $method = lc($method);
150 eval "use LWP::UserAgent;";
151 die "using LWP::UserAgent: $@" if $@;
152 eval "use HTTP::Request::Common;";
153 die "using HTTP::Request::Common: $@" if $@;
155 my $ua = LWP::UserAgent->new;
157 #my $response = $ua->$method(
159 # 'Content-Type'=>'application/x-www-form-urlencoded'
161 my $req = HTTP::Request::Common::POST( $url, \@data );
162 my $response = $ua->request($req);
164 die $response->error_as_HTML if $response->is_error;
166 if(length($success_regexp) > 1) {
167 my $response_content = $response->content;
168 die $response_content unless $response_content =~ /$success_regexp/;