-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookspider.cgi
executable file
·379 lines (317 loc) · 9.87 KB
/
bookspider.cgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/perl -T
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;
use MIME::Lite;
use Net::SMTP::SSL;
use File::Basename;
use POSIX qw(dup2);
my $q = CGI->new;
$q->charset('utf-8');
# Set this to the bookspider.test.sh script for a local development environment.
my $command = "/opt/scripts/bookspider/bookspider.pl";
#$command = "/Users/benhaw01/desktop/BookSpider/bookspider.test.sh"; # override for development on macbook
my $outdir = "/tmp";
my $default_email = "username\@kindle.com"; # Comment out if you don't want a default address.
# Set this to a local directory if you want to develop locally.
my $assetprefix = "http://bookspider.swimfrog.com";
#$assetprefix = "/bookspider/assets"; # override for development on macbook
my $from = 'gmail_account_username@gmail.com';
my $password = 'gmail_account_password_goes_here';
# Path untainting
$ENV{"PATH"} = "";
# Autoflush (why wouldn't you?)
local $| = 1;
# "mode" cgi parameter + untainting
my $cgi_mode = $q->param('mode');
my $mode = "init";
$mode = $1 if ($cgi_mode =~ /([a-z]+)/);
# "plugin" cgi parameter + untainting
my $cgi_plugin = $q->param('plugin');
my $plugin = "";
$plugin = $1 if ($cgi_plugin =~ /([a-z0-9_]+)/);
# "verbose" cgi parameter + untainting
my $cgi_verbose = $q->param('verbose');
my $verbose = "";
$verbose = "-v" if ($cgi_verbose =~ /([01]+)/);
# "id" cgi parameter + untainting
my $cgi_id = $q->param('id');
my $id = 0;
$id = $1 if ($cgi_id =~ /([0-9A-Za-z]+)/);
# "shortname" cgi parameter + untainting
my $cgi_shortname = $q->param('shortname');
my $shortname = "";
$shortname = $1 if ($cgi_shortname =~ /([0-9A-Za-z_]+)/);
# "email" cgi parameter + untainting
my $cgi_email = $q->param('email');
my $email = "";
$email = $1 if ($cgi_email =~ /([0-9A-Za-z_\-\.\@]+)/);
print <<EOF;
Content-type: text/html;charset=UTF-8\n
<HTML>\n
<HEAD>\n
<link rel="stylesheet" type="text/css" href="$assetprefix/stylesheet.css" id="thecss">
EOF
print '<link rel="stylesheet" type="text/css" href="'.$assetprefix.'/mobile.css" id="mobilecss"\n' if ismobile($ENV{HTTP_USER_AGENT});
print STDERR "client (useragent=".$ENV{HTTP_USER_AGENT}." mobile=".ismobile($ENV{HTTP_USER_AGENT})."\n";
print <<EOF;
<script src="$assetprefix/jquery-1.7.2.min.js"></script>
<script src="$assetprefix/jquery.watermark.js"></script>
</HEAD>\n
<BODY>\n
EOF
# Nice for debugging CGI state.
#print '<div id="debug"><p><font size=1 color="gray">'.Dumper($q).'</font></p></div>';
sub htmlize {
my $buffer = $_;
print Dumper($buffer);
$buffer =~ s/\n/<br>\n/g; # Add literal line breaks
#TODO Add link detection?
return $buffer;
}
sub in_array {
my ($arr,$search_for) = @_;
my %items = map {$_ => 1} @$arr; # create a hash out of the array values
return (exists($items{$search_for}))?1:0;
}
sub ismobile {
$useragent=lc(@_);
$is_mobile = 0;
if($useragent =~ m/(android|up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|kindle)/i) {
$is_mobile=1;
}
if((index($ENV{HTTP_ACCEPT},'application/vnd.wap.xhtml+xml')>0) || ($ENV{HTTP_X_WAP_PROFILE} || $ENV{HTTP_PROFILE})) {
$is_mobile=1;
}
$mobile_ua = lc(substr $ENV{HTTP_USER_AGENT},0,4);
@mobile_agents = ('w3c ','acs-','alav','alca','amoi','andr','audi','avan','benq','bird','blac','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','newt','noki','oper','palm','pana','pant','phil','play','port','prox','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','wapr','webc','winw','winw','xda','xda-',);
if(in_array(\@mobile_agents,$mobile_ua)) {
$is_mobile=1;
}
if ($ENV{ALL_HTTP}) {
if (index(lc($ENV{ALL_HTTP}),'OperaMini')>0) {
$is_mobile=1;
}
}
if (index(lc($ENV{HTTP_USER_AGENT}),'windows')>0) {
$is_mobile=0;
}
return $is_mobile;
}
sub send_file_mail {
my $to = shift;
my $subject = shift;
my $filename = shift;
my $from = 'username';
my $password = 'password';
open(FILE, "<$filename") || die("Couldn't open file to mail: $!");
my $msg = MIME::Lite->new(
From =>$from,
To =>$to,
#CC =>'cc@gmail.com',
#Reply-To => 'cc@gmail.com',
Subject =>$subject,
Type =>'text/plain',
Data =>"Book delivery.",
);
$msg->attach(
Type => "text/plain",
Path => $filename,
Filename => basename($filename),
Disposition => "attachment",
Encoding => "base64",
);
print htmlize($msg->as_string) if $verbose;
my $smtp;
if (not $smtp = Net::SMTP::SSL->new('smtp.gmail.com',
Port => 465,
Debug => 0,
)) {
die "Could not connect to server\n";
}
$smtp->auth($from, $password)
|| die "Authentication failed!\n";
$smtp->mail($from . "\n");
my @recepients = split(/,/, $to);
foreach my $recp (@recepients) {
$smtp->to($recp . "\n");
}
$smtp->data();
$smtp->datasend($msg->as_string . "\n");
$smtp->dataend();
$smtp->quit;
return 1;
}
sub fork_child {
my ($child_process_code) = @_;
my $pid = fork;
die "Can't fork: $!\n" if !defined $pid;
return $pid if $pid != 0;
# Now in child process
$child_process_code->();
exit;
}
if ($mode eq "init") {
print '
<form method="POST">
<div id="logo">
<center>
<img id="logo" src="'.$assetprefix.'/logo.png">
<p>What can BookSpider grab for you today?</p>
</center>
</div>
<div class="section" id="section_one">
<div id="number_one" class="number">
<img id="number_one_img" src="'.$assetprefix.'/number_one.png">
<p class="value">Pick a plugin:</p>
</div>
<div id="plugin_name_div">
<p class="value">
<select name="plugin" id="plugin">';
open (COMMAND, "$command --listplugins 2>&1 |") || die("Couldn't list plugins: $!");
while (<COMMAND>) {
chomp;
print "<option name=\"$_\">$_</option>\n";
}
close(COMMAND);
print '
</select>
</p>
</div>
</div>
<div class="section" id="section_two">
<div id="number_two" class="number">
<img id="number_two_img" src="'.$assetprefix.'/number_two.png">
<p class="value">A few details:</p>
</div>
<div id="id_div">
<p class="value"><input type="text" id="id" name="id"></p>
</div>
<div id="shortname_div">
<p class="value"><input type="text" id="shortname" name="shortname"></p>
</div>
<div id="email_div">
<p class="value"><input type="text" value="'.$default_email.'" id="email" name="email"></p>
</div>
</div>
<div class="section" id="section_three">
<div id="number_three" class="number">
<img id="number_three_img" src="'.$assetprefix.'/number_three.png">
<p class="value">Go!</p>
</div>
<div class="submit" id="submit_div">
<input type="hidden" name="mode" value="run">
<input type="submit" name="submit" value="submit">
</div>
<br>
<div id="verbose_div">
<p class="value"><input type="checkbox" name="verbose" value="1">Verbose?</p>
</div>
</div>
</form>
';
} elsif ($mode eq "run") {
print '
<div class="header">
<img id="logo_small" src="'.$assetprefix.'/logo_small.png">
<p>Now downloading '.$shortname.' using plugin '.$plugin.'...</p>
</div>
<div class="scriptoutput">
';
unless (($plugin) && ($id) && ($shortname)) {
print "invalid parameters specified";
die("invalid parameters specified");
}
my $runcommand = "$command $verbose -p $plugin -f $outdir/$shortname.txt $id";
#print "<br>About to run: ".$runcommand."\n";
# open(COMMAND, "$runcommand 2>&1 |") || die ("Could not execute $runcommand: $!\n");
#
# while (<COMMAND>) {
# chomp;
# next if ($_ =~ m/Wide character in print/);
# $_ .="<br>\n";
# print;
# }
#
# close(COMMAND);
my @execcmd = split(" ", $runcommand);
pipe my ($readable, $writable)
or die "Can't create pipe: $!\n";
my $pid = fork_child(sub {
dup2(fileno $writable, 1);
dup2(1, 2); # Redirect stderr to stdout
exec @execcmd or die "Can't exec $runcommand: $!\n";
});
close $writable;
while (<$readable>) {
#chomp;
next if (m/Wide character in print/);
#$_ .="<br>\n";
#print "Gonna htmlize $_\n";
#htmlize;
print;
}
waitpid $pid, 0;
#print "Here is the preview:<br>\n";
#print '<div id="preview_div"><font size=1>';
#open (OUTPUT, "<$outdir/$shortname.txt") || die("Couldn't open output file: $!\n");
#while (<OUTPUT>) {
# print;
#}
#print '</font></div>';
print '
</div>
';
print '
<form method="POST">
<div id="hidden_div">
<input type="hidden" value="$shortname">
</div>
<div id="hidden_div">
<p>Please review the output. OK to send the output to '.$email.'?</p>
</div>
<div id="submit_div">
<input type="hidden" name="mode" value="send">
<input type="hidden" name="shortname" value="'.$shortname.'">
<input type="hidden" name="email" value="'.$email.'">
<input type="submit" value="confirm">
</div>
</form>
';
} elsif ($mode eq "send") {
print '
<form method="POST">
<div class="header">
<img id="logo_small" src="'.$assetprefix.'/logo_small.png">
<p>Now sending '.$shortname.' to '.$email.'...</p>
</div>
<div class="scriptoutput">
';
die "Invalid input parameters\n" if ((!$email) || (!$shortname) );
my $succ = send_file_mail($email, $shortname, "$outdir/$shortname.txt");
if ($succ) {
print "Mail to $email sent successfully. ($outdir/$shortname.txt)\n";
} else {
print "Error sending mail to $email. ($outdir/$shortname.txt)\n";
}
print '
</div>
<div id="submit_div">
<input type="hidden" name="mode" value="init">
<input type="submit" value="Start Over">
</div>
</form>
';
}
print <<EOF;
<div id="footer">
<center><p><a href="mailto:benh\@swimfrog.com">benh\@swimfrog.com</a></p></center>
</div>
<script type="text/javascript">
\$("#id").watermark("The book ID");
\$("#shortname").watermark("The book title");
\$("#email").watermark("Your email address");
</script>
</BODY>\n
</HTML>\n
EOF