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,
48 Send an HTTP or HTTPS GET or POST to the specified URL. For HTTPS support,
49 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
50 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
55 sub rebless { shift; }
59 $self->_export_command('insert', @_);
64 $self->_export_command('delete', @_);
68 my( $self, $action, $svc_x ) = ( shift, shift, shift );
70 return unless $self->option("${action}_data");
72 my $cust_main = $svc_x->table eq 'cust_main'
74 : $svc_x->cust_svc->cust_pkg->cust_main;
76 $self->http_queue( $svc_x->svcnum,
77 $self->option('method'),
79 $self->option('success_regexp'),
81 /^\s*(\S+)\s+(.*)$/ or /()()/;
82 my( $field, $value_expression ) = ( $1, $2 );
83 my $value = eval $value_expression;
86 } split(/\n/, $self->option("${action}_data") )
92 my( $self, $new, $old ) = ( shift, shift, shift );
94 return unless $self->option('replace_data');
96 my $new_cust_main = $new->table eq 'cust_main'
98 : $new->cust_svc->cust_pkg->cust_main;
99 my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
101 $self->http_queue( $new->svcnum,
102 $self->option('method'),
103 $self->option('url'),
104 $self->option('success_regexp'),
106 /^\s*(\S+)\s+(.*)$/ or /()()/;
107 my( $field, $value_expression ) = ( $1, $2 );
108 my $value = eval $value_expression;
111 } split(/\n/, $self->option('replace_data') )
117 my($self, $svcnum) = (shift, shift);
118 my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
119 $queue->svcnum($svcnum) if $svcnum;
120 $queue->insert( @_ );
124 my($method, $url, $success_regexp, @data) = @_;
126 $method = lc($method);
128 eval "use LWP::UserAgent;";
129 die "using LWP::UserAgent: $@" if $@;
130 eval "use HTTP::Request::Common;";
131 die "using HTTP::Request::Common: $@" if $@;
133 my $ua = LWP::UserAgent->new;
135 #my $response = $ua->$method(
137 # 'Content-Type'=>'application/x-www-form-urlencoded'
139 my $req = HTTP::Request::Common::POST( $url, \@data );
140 my $response = $ua->request($req);
142 die $response->error_as_HTML if $response->is_error;
144 if(length($success_regexp) > 1) {
145 my $response_content = $response->content;
146 die $response_content unless $response_content =~ /$success_regexp/;