This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / FS / FS / part_export / http.pm
1 package FS::part_export::http;
2
3 use base qw( FS::part_export );
4 use vars qw( %options %info );
5 use Tie::IxHash;
6
7 tie %options, 'Tie::IxHash',
8   'method' => { label   =>'Method',
9                 type    =>'select',
10                 #options =>[qw(POST GET)],
11                 options =>[qw(POST)],
12                 default =>'POST' },
13   'url'    => { label   => 'URL', default => 'http://', },
14   'insert_data' => {
15     label   => 'Insert data',
16     type    => 'textarea',
17     default => join("\n",
18       'DomainName $svc_x->domain',
19       'Email ( grep { $_ !~ /^(POST|FAX)$/ } $svc_x->cust_svc->cust_pkg->cust_main->invoicing_list)[0]',
20       'test 1',
21       'reseller $svc_x->cust_svc->cust_pkg->part_pkg->pkg =~ /reseller/i',
22     ),
23   },
24   'delete_data' => {
25     label   => 'Delete data',
26     type    => 'textarea',
27     default => join("\n",
28     ),
29   },
30   'replace_data' => {
31     label   => 'Replace data',
32     type    => 'textarea',
33     default => join("\n",
34     ),
35   },
36   'success_regexp' => {
37     label  => 'Success Regexp',
38     default => '',
39   },
40 ;
41
42 %info = (
43   'svc'     => 'svc_domain',
44   'desc'    => 'Send an HTTP or HTTPS GET or POST request',
45   'options' => \%options,
46   'notes'   => <<'END'
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>
50 is required.
51 END
52 );
53
54 sub rebless { shift; }
55
56 sub _export_insert {
57   my $self = shift;
58   $self->_export_command('insert', @_);
59 }
60
61 sub _export_delete {
62   my $self = shift;
63   $self->_export_command('delete', @_);
64 }
65
66 sub _export_command {
67   my( $self, $action, $svc_x ) = ( shift, shift, shift );
68
69   return unless $self->option("${action}_data");
70
71   my $cust_main = $svc_x->table eq 'cust_main'
72                     ? $svc_x
73                     : $svc_x->cust_svc->cust_pkg->cust_main;
74
75   $self->http_queue( $svc_x->svcnum,
76     $self->option('method'),
77     $self->option('url'),
78     $self->option('success_regexp'),
79     map {
80       /^\s*(\S+)\s+(.*)$/ or /()()/;
81       my( $field, $value_expression ) = ( $1, $2 );
82       my $value = eval $value_expression;
83       die $@ if $@;
84       ( $field, $value );
85     } split(/\n/, $self->option("${action}_data") )
86   );
87
88 }
89
90 sub _export_replace {
91   my( $self, $new, $old ) = ( shift, shift, shift );
92
93   return unless $self->option('replace_data');
94
95   my $new_cust_main = $new->table eq 'cust_main'
96                         ? $new
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
99
100   $self->http_queue( $new->svcnum,
101     $self->option('method'),
102     $self->option('url'),
103     $self->option('success_regexp'),
104     map {
105       /^\s*(\S+)\s+(.*)$/ or /()()/;
106       my( $field, $value_expression ) = ( $1, $2 );
107       my $value = eval $value_expression;
108       die $@ if $@;
109       ( $field, $value );
110     } split(/\n/, $self->option('replace_data') )
111   );
112
113 }
114
115 sub http_queue {
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( @_ );
120 }
121
122 sub http {
123   my($method, $url, $success_regexp, @data) = @_;
124
125   $method = lc($method);
126
127   eval "use LWP::UserAgent;";
128   die "using LWP::UserAgent: $@" if $@;
129   eval "use HTTP::Request::Common;";
130   die "using HTTP::Request::Common: $@" if $@;
131
132   my $ua = LWP::UserAgent->new;
133
134   #my $response = $ua->$method(
135   #  $url, \%data,
136   #  'Content-Type'=>'application/x-www-form-urlencoded'
137   #);
138   my $req = HTTP::Request::Common::POST( $url, \@data );
139   my $response = $ua->request($req);
140
141   die $response->error_as_HTML if $response->is_error;
142
143   if(length($success_regexp) > 1) {
144     my $response_content = $response->content;
145     die $response_content unless $response_content =~ /$success_regexp/;
146   }
147
148 }
149
150 1;
151