This commit was manufactured by cvs2svn to create tag 'freeside_2_1_1'.
[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 ;
37
38 %info = (
39   'svc'     => 'svc_domain',
40   'desc'    => 'Send an HTTP or HTTPS GET or POST request',
41   'options' => \%options,
42   'notes'   => <<'END'
43 Send an HTTP or HTTPS GET or POST to the specified URL.  For HTTPS support,
44 <a href="http://search.cpan.org/dist/Crypt-SSLeay">Crypt::SSLeay</a>
45 or <a href="http://search.cpan.org/dist/IO-Socket-SSL">IO::Socket::SSL</a>
46 is required.
47 END
48 );
49
50 sub rebless { shift; }
51
52 sub _export_insert {
53   my $self = shift;
54   $self->_export_command('insert', @_);
55 }
56
57 sub _export_delete {
58   my $self = shift;
59   $self->_export_command('delete', @_);
60 }
61
62 sub _export_command {
63   my( $self, $action, $svc_x ) = ( shift, shift, shift );
64
65   return unless $self->option("${action}_data");
66
67   my $cust_main = $svc_x->table eq 'cust_main'
68                     ? $svc_x
69                     : $svc_x->cust_svc->cust_pkg->cust_main;
70
71   $self->http_queue( $svc_x->svcnum,
72     $self->option('method'),
73     $self->option('url'),
74     map {
75       /^\s*(\S+)\s+(.*)$/ or /()()/;
76       my( $field, $value_expression ) = ( $1, $2 );
77       my $value = eval $value_expression;
78       die $@ if $@;
79       ( $field, $value );
80     } split(/\n/, $self->option("${action}_data") )
81   );
82
83 }
84
85 sub _export_replace {
86   my( $self, $new, $old ) = ( shift, shift, shift );
87
88   return unless $self->option('replace_data');
89
90   my $new_cust_main = $new->table eq 'cust_main'
91                         ? $new
92                         : $new->cust_svc->cust_pkg->cust_main;
93   my $cust_main = $new_cust_main; #so folks can use $new_cust_main or $cust_main
94
95   $self->http_queue( $new->svcnum,
96     $self->option('method'),
97     $self->option('url'),
98     map {
99       /^\s*(\S+)\s+(.*)$/ or /()()/;
100       my( $field, $value_expression ) = ( $1, $2 );
101       my $value = eval $value_expression;
102       die $@ if $@;
103       ( $field, $value );
104     } split(/\n/, $self->option('replace_data') )
105   );
106
107 }
108
109 sub http_queue {
110   my($self, $svcnum) = (shift, shift);
111   my $queue = new FS::queue { 'job' => "FS::part_export::http::http" };
112   $queue->svcnum($svcnum) if $svcnum;
113   $queue->insert( @_ );
114 }
115
116 sub http {
117   my($method, $url, @data) = @_;
118
119   $method = lc($method);
120
121   eval "use LWP::UserAgent;";
122   die "using LWP::UserAgent: $@" if $@;
123   eval "use HTTP::Request::Common;";
124   die "using HTTP::Request::Common: $@" if $@;
125
126   my $ua = LWP::UserAgent->new;
127
128   #my $response = $ua->$method(
129   #  $url, \%data,
130   #  'Content-Type'=>'application/x-www-form-urlencoded'
131   #);
132   my $req = HTTP::Request::Common::POST( $url, \@data );
133   my $response = $ua->request($req);
134
135   die $response->error_as_HTML if $response->is_error;
136
137 }
138
139 1;
140