aeeb59bfac4af5491a0cd2afdaf7944c09755f33
[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   'suspend_data' => {
37     label   => 'Suspend data',
38     type    => 'textarea',
39     default => join("\n",
40     ),
41   },
42   'unsuspend_data' => {
43     label   => 'Unsuspend data',
44     type    => 'textarea',
45     default => join("\n",
46     ),
47   },
48   'success_regexp' => {
49     label  => 'Success Regexp',
50     default => '',
51   },
52 ;
53
54 %info = (
55   'svc'     => 'svc_domain',
56   'desc'    => 'Send an HTTP or HTTPS GET or POST request',
57   'options' => \%options,
58   'no_machine' => 1,
59   'notes'   => <<'END'
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>
63 is required.
64 END
65 );
66
67 sub rebless { shift; }
68
69 sub _export_insert {
70   my $self = shift;
71   $self->_export_command('insert', @_);
72 }
73
74 sub _export_delete {
75   my $self = shift;
76   $self->_export_command('delete', @_);
77 }
78
79 sub _export_suspend {
80   my $self = shift;
81   $self->_export_command('suspend', @_);
82 }
83
84 sub _export_unsuspend {
85   my $self = shift;
86   $self->_export_command('unsuspend', @_);
87 }
88
89 sub _export_command {
90   my( $self, $action, $svc_x ) = ( shift, shift, shift );
91
92   return unless $self->option("${action}_data");
93
94   my $cust_main = $svc_x->cust_main or return;
95
96   $self->http_queue( $svc_x->svcnum,
97     $self->option('method'),
98     $self->option('url'),
99     $self->option('success_regexp'),
100     map {
101       /^\s*(\S+)\s+(.*)$/ or /()()/;
102       my( $field, $value_expression ) = ( $1, $2 );
103       my $value = eval $value_expression;
104       die $@ if $@;
105       ( $field, $value );
106     } split(/\n/, $self->option("${action}_data") )
107   );
108
109 }
110
111 sub _export_replace {
112   my( $self, $new, $old ) = ( shift, shift, shift );
113
114   return unless $self->option('replace_data');
115
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
118
119   $self->http_queue( $new->svcnum,
120     $self->option('method'),
121     $self->option('url'),
122     $self->option('success_regexp'),
123     map {
124       /^\s*(\S+)\s+(.*)$/ or /()()/;
125       my( $field, $value_expression ) = ( $1, $2 );
126       my $value = eval $value_expression;
127       die $@ if $@;
128       ( $field, $value );
129     } split(/\n/, $self->option('replace_data') )
130   );
131
132 }
133
134 sub http_queue {
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( @_ );
139 }
140
141 sub http {
142   my($method, $url, $success_regexp, @data) = @_;
143
144   $method = lc($method);
145
146   eval "use LWP::UserAgent;";
147   die "using LWP::UserAgent: $@" if $@;
148   eval "use HTTP::Request::Common;";
149   die "using HTTP::Request::Common: $@" if $@;
150
151   my $ua = LWP::UserAgent->new;
152
153   #my $response = $ua->$method(
154   #  $url, \%data,
155   #  'Content-Type'=>'application/x-www-form-urlencoded'
156   #);
157   my $req = HTTP::Request::Common::POST( $url, \@data );
158   my $response = $ua->request($req);
159
160   die $response->error_as_HTML if $response->is_error;
161
162   if(length($success_regexp) > 1) {
163     my $response_content = $response->content;
164     die $response_content unless $response_content =~ /$success_regexp/;
165   }
166
167 }
168
169 1;
170