Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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   'no_machine' => 1,
47   'notes'   => <<'END'
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>
51 is required.
52 END
53 );
54
55 sub rebless { shift; }
56
57 sub _export_insert {
58   my $self = shift;
59   $self->_export_command('insert', @_);
60 }
61
62 sub _export_delete {
63   my $self = shift;
64   $self->_export_command('delete', @_);
65 }
66
67 sub _export_command {
68   my( $self, $action, $svc_x ) = ( shift, shift, shift );
69
70   return unless $self->option("${action}_data");
71
72   my $cust_main = $svc_x->table eq 'cust_main'
73                     ? $svc_x
74                     : $svc_x->cust_svc->cust_pkg->cust_main;
75
76   $self->http_queue( $svc_x->svcnum,
77     $self->option('method'),
78     $self->option('url'),
79     $self->option('success_regexp'),
80     map {
81       /^\s*(\S+)\s+(.*)$/ or /()()/;
82       my( $field, $value_expression ) = ( $1, $2 );
83       my $value = eval $value_expression;
84       die $@ if $@;
85       ( $field, $value );
86     } split(/\n/, $self->option("${action}_data") )
87   );
88
89 }
90
91 sub _export_replace {
92   my( $self, $new, $old ) = ( shift, shift, shift );
93
94   return unless $self->option('replace_data');
95
96   my $new_cust_main = $new->table eq 'cust_main'
97                         ? $new
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
100
101   $self->http_queue( $new->svcnum,
102     $self->option('method'),
103     $self->option('url'),
104     $self->option('success_regexp'),
105     map {
106       /^\s*(\S+)\s+(.*)$/ or /()()/;
107       my( $field, $value_expression ) = ( $1, $2 );
108       my $value = eval $value_expression;
109       die $@ if $@;
110       ( $field, $value );
111     } split(/\n/, $self->option('replace_data') )
112   );
113
114 }
115
116 sub http_queue {
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( @_ );
121 }
122
123 sub http {
124   my($method, $url, $success_regexp, @data) = @_;
125
126   $method = lc($method);
127
128   eval "use LWP::UserAgent;";
129   die "using LWP::UserAgent: $@" if $@;
130   eval "use HTTP::Request::Common;";
131   die "using HTTP::Request::Common: $@" if $@;
132
133   my $ua = LWP::UserAgent->new;
134
135   #my $response = $ua->$method(
136   #  $url, \%data,
137   #  'Content-Type'=>'application/x-www-form-urlencoded'
138   #);
139   my $req = HTTP::Request::Common::POST( $url, \@data );
140   my $response = $ua->request($req);
141
142   die $response->error_as_HTML if $response->is_error;
143
144   if(length($success_regexp) > 1) {
145     my $response_content = $response->content;
146     die $response_content unless $response_content =~ /$success_regexp/;
147   }
148
149 }
150
151 1;
152