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 => 'Success Regexp',
43 'svc' => 'svc_domain',
44 'desc' => 'Send an HTTP or HTTPS GET or POST request',
45 'options' => \%options,
47 Send an HTTP or HTTPS GET or POST to the specified URL. For HTTPS support,
48 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
49 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
54 sub rebless { shift; }
58 $self->_export_command('insert', @_);
63 $self->_export_command('delete', @_);
67 my( $self, $action, $svc_x ) = ( shift, shift, shift );
69 return unless $self->option("${action}_data");
71 my $cust_main = $svc_x->table eq 'cust_main'
73 : $svc_x->cust_svc->cust_pkg->cust_main;
75 $self->http_queue( $svc_x->svcnum,
76 $self->option('method'),
78 $self->option('success_regexp'),
80 /^\s*(\S+)\s+(.*)$/ or /()()/;
81 my( $field, $value_expression ) = ( $1, $2 );
82 my $value = eval $value_expression;
85 } split(/\n/, $self->option("${action}_data") )
91 my( $self, $new, $old ) = ( shift, shift, shift );
93 return unless $self->option('replace_data');
95 my $new_cust_main = $new->table eq 'cust_main'
97 : $new->cust_svc->cust_pkg->cust_main;
98 my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
100 $self->http_queue( $new->svcnum,
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('replace_data') )
116 my($self, $svcnum) = (shift, shift);
117 my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
118 $queue->svcnum($svcnum) if $svcnum;
119 $queue->insert( @_ );
123 my($method, $url, $success_regexp, @data) = @_;
125 $method = lc($method);
127 eval "use LWP::UserAgent;";
128 die "using LWP::UserAgent: $@" if $@;
129 eval "use HTTP::Request::Common;";
130 die "using HTTP::Request::Common: $@" if $@;
132 my $ua = LWP::UserAgent->new;
134 #my $response = $ua->$method(
136 # 'Content-Type'=>'application/x-www-form-urlencoded'
138 my $req = HTTP::Request::Common::POST( $url, \@data );
139 my $response = $ua->request($req);
141 die $response->error_as_HTML if $response->is_error;
143 if(length($success_regexp) > 1) {
144 my $response_content = $response->content;
145 die $response_content unless $response_content =~ /$success_regexp/;