96d49f00c7bb84ddf9603faa330acbbfe157ca98
[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   'notes'   => <<'END'
59 Send an HTTP or HTTPS GET or POST to the specified URL.  For HTTPS support,
60 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
61 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
62 is required.
63 END
64 );
65
66 sub rebless { shift; }
67
68 sub _export_insert {
69   my $self = shift;
70   $self->_export_command('insert', @_);
71 }
72
73 sub _export_delete {
74   my $self = shift;
75   $self->_export_command('delete', @_);
76 }
77
78 sub _export_suspend {
79   my $self = shift;
80   $self->_export_command('suspend', @_);
81 }
82
83 sub _export_unsuspend {
84   my $self = shift;
85   $self->_export_command('unsuspend', @_);
86 }
87
88 sub _export_command {
89   my( $self, $action, $svc_x ) = ( shift, shift, shift );
90
91   return unless $self->option("${action}_data");
92
93   my $cust_main = $svc_x->table eq 'cust_main'
94                     ? $svc_x
95                     : $svc_x->cust_svc->cust_pkg->cust_main;
96
97   $self->http_queue( $svc_x->svcnum,
98     $self->option('method'),
99     $self->option('url'),
100     $self->option('success_regexp'),
101     map {
102       /^\s*(\S+)\s+(.*)$/ or /()()/;
103       my( $field, $value_expression ) = ( $1, $2 );
104       my $value = eval $value_expression;
105       die $@ if $@;
106       ( $field, $value );
107     } split(/\n/, $self->option("${action}_data") )
108   );
109
110 }
111
112 sub _export_replace {
113   my( $self, $new, $old ) = ( shift, shift, shift );
114
115   return unless $self->option('replace_data');
116
117   my $new_cust_main = $new->table eq 'cust_main'
118                         ? $new
119                         : $new->cust_svc->cust_pkg->cust_main;
120   my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
121
122   $self->http_queue( $new->svcnum,
123     $self->option('method'),
124     $self->option('url'),
125     $self->option('success_regexp'),
126     map {
127       /^\s*(\S+)\s+(.*)$/ or /()()/;
128       my( $field, $value_expression ) = ( $1, $2 );
129       my $value = eval $value_expression;
130       die $@ if $@;
131       ( $field, $value );
132     } split(/\n/, $self->option('replace_data') )
133   );
134
135 }
136
137 sub http_queue {
138   my($self, $svcnum) = (shift, shift);
139   my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
140   $queue->svcnum($svcnum) if $svcnum;
141   $queue->insert( @_ );
142 }
143
144 sub http {
145   my($method, $url, $success_regexp, @data) = @_;
146
147   $method = lc($method);
148
149   eval "use LWP::UserAgent;";
150   die "using LWP::UserAgent: $@" if $@;
151   eval "use HTTP::Request::Common;";
152   die "using HTTP::Request::Common: $@" if $@;
153
154   my $ua = LWP::UserAgent->new;
155
156   #my $response = $ua->$method(
157   #  $url, \%data,
158   #  'Content-Type'=>'application/x-www-form-urlencoded'
159   #);
160   my $req = HTTP::Request::Common::POST( $url, \@data );
161   my $response = $ua->request($req);
162
163   die $response->error_as_HTML if $response->is_error;
164
165   if(length($success_regexp) > 1) {
166     my $response_content = $response->content;
167     die $response_content unless $response_content =~ /$success_regexp/;
168   }
169
170 }
171
172 1;
173