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->cust_main or return;
96 $self->http_queue( $svc_x->svcnum,
97 $self->option('method'),
99 $self->option('success_regexp'),
101 /^\s*(\S+)\s+(.*)$/ or /()()/;
102 my( $field, $value_expression ) = ( $1, $2 );
103 my $value = eval $value_expression;
106 } split(/\n/, $self->option("${action}_data") )
111 sub _export_replace {
112 my( $self, $new, $old ) = ( shift, shift, shift );
114 return unless $self->option('replace_data');
116 my $new_cust_main = $new->cust_main or return;
117 my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
119 $self->http_queue( $new->svcnum,
120 $self->option('method'),
121 $self->option('url'),
122 $self->option('success_regexp'),
124 /^\s*(\S+)\s+(.*)$/ or /()()/;
125 my( $field, $value_expression ) = ( $1, $2 );
126 my $value = eval $value_expression;
129 } split(/\n/, $self->option('replace_data') )
135 my($self, $svcnum) = (shift, shift);
136 my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
137 $queue->svcnum($svcnum) if $svcnum;
138 $queue->insert( @_ );
142 my($method, $url, $success_regexp, @data) = @_;
144 $method = lc($method);
146 eval "use LWP::UserAgent;";
147 die "using LWP::UserAgent: $@" if $@;
148 eval "use HTTP::Request::Common;";
149 die "using HTTP::Request::Common: $@" if $@;
151 my $ua = LWP::UserAgent->new;
153 #my $response = $ua->$method(
155 # 'Content-Type'=>'application/x-www-form-urlencoded'
157 my $req = HTTP::Request::Common::POST( $url, \@data );
158 my $response = $ua->request($req);
160 die $response->error_as_HTML if $response->is_error;
162 if(length($success_regexp) > 1) {
163 my $response_content = $response->content;
164 die $response_content unless $response_content =~ /$success_regexp/;