-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.cgi
executable file
·525 lines (414 loc) · 11.7 KB
/
install.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/perl
# Parts of this install script are copyrighted by Perlfect Solutions, http://www.perlfect.com
use strict;
use DBI;
my $VERSION = "0.1";
print "
BookExchange $VERSION Setup Utility
NOTE: You must have
access to an SQL database
access to the DBI driver for the database
a created database for the bookexchange
Without these pieces, the bookexchange can't run! Please take special note to
the third piece, the only unautomatic part of this installation: you need to create
the database (but not its tables) before installation.
If you don't know if have
these, please ask your system administrator. (Also ask for a user name and
password for connecting to the database, and that the user can create tables.)
";
exit if (input("Do you want to continue? [y/n]", "y", ["y","n"]) eq "n");
print "
BookExchange
Copyright 1999 Brian Deitte (except as noted below)
Original Installer: Copyright 1999 Perlfect Solutions
All Images: Copyright 1999 David Selden
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
";
exit if (input("Do you want to continue? [y/n]", "y", ["y","n"]) eq "n");
my $os = input("What operating system is your site running on? [unix/nt]", "", ["unix", "nt"]);
my $perl;
if($os eq 'unix')
{
$perl = `which perl`;
chomp $perl;
$perl='/usr/bin/perl' unless ($perl);
$perl = input("Where is perl? [$perl]", "$perl");
print "Ok, I will configure the scripts to run with $perl\n";
my @perlfiles;
my @sfile = &get_files('web_perl');
foreach (@sfile)
{
set_perl_path("web_perl/$_", $perl);
}
@sfile = &get_files('admin_perl');
foreach (@sfile)
{
set_perl_path("admin_perl/$_", $perl);
}
@sfile = &get_files('lib_perl');
foreach (@sfile)
{
set_perl_path("lib_perl/$_", $perl);
}
print "\n";
}
my ($instdir, $insturl, $cgidir, $cgiurl, $sendmail, $adminemail,
$dbdriver, $dbname, $dbuser, $dbpassword);
do
{
$instdir = input("
Where would you like the bookexchange to be installed?
(e.g. /home/httpd/html/bookexchange)
If the folder does not exist, it will be created.");
$insturl = input("
What is the URL of the above directory?
(e.g. http://www.yoursite.edu/bookexchange)");
$cgidir = input("
Where would you like the cgi files to be installed?
(e.g. /home/httpd/cgi-bin/bookexchange)
If the folder does not exist, it will be created.");
$cgiurl = input("
What is the URL of the above directory?
(e.g.http://www.yoursite.edu/cgi-bin/bookexchange)");
$sendmail = input("
Where is sendmail (or the equivalent) located?
(e.g. /usr/lib/sendmail");
$adminemail = input("
What is the email users should use if they need help with the bookexchange?");
$dbdriver = input("
What kind of database are you using? (e.g. mysql)");
$dbname = input("
What is the database named? (e.g. bookexchange)
Remember, this install script will not work if the
database has not already been created.");
$dbuser = input("
What name will be used to connect to the database?");
$dbpassword = input("
What is the password for this name?");
}
while (input("End of custom information. Is the information you provided ok? [y/n]", "y", ["y","n"]) eq "n");
foreach ($instdir, $insturl, $cgidir, $cgiurl)
{
$_ =~ s/[\/:\\]$//;
}
print "
I will now set up the database...
";
(my $dbh = DBI->connect("DBI:$dbdriver:database=$dbname;", $dbuser, $dbpassword))
or die "Couldn't connect to the database by DBI: $DBI::errstr";
my $drh = DBI->install_driver($dbdriver)
or die "Couldn't install the database driver: $DBI::errstr";
my $sth = $dbh->prepare("
create table books (
item_id integer AUTO_INCREMENT PRIMARY KEY,
email varchar (60),
title varchar(100),
author varchar(100),
dept varchar(100),
class varchar(100),
isbn varchar(20),
condition varchar(50),
other varchar(255),
price varchar(10),
date date
)
");
(defined $sth) or die "Couldn't create table: $DBI::errstr";
($sth->execute) or die "Couldn't create table: $DBI::errstr";
$sth->finish;
$sth = $dbh->prepare("
create table users (
ID integer AUTO_INCREMENT PRIMARY KEY,
email varchar(60),
password varchar(50),
books varchar(255)
)
");
(defined $sth) or die "Couldn't create table: $DBI::errstr";
($sth->execute) or die "Couldn't create table: $DBI::errstr";
$sth->finish;
my $rc = $dbh->disconnect;
($rc) or die "Couldn't disconnect from database: $rc->errstr";
print "
I will now install the files...
";
install_dir($instdir, "0755");
install_dir_files('html/', "$instdir/", "0644");
html_install($instdir, $cgiurl);
install_dir($cgidir, "0755");
install_dir_files('web_perl/', "$cgidir/", "0755");
install_dir_files('lib_perl/', "$cgidir/", "0755");
my $admindir = $instdir . "/admin";
install_dir($admindir, "0700");
install_dir_files('admin_perl/', "$admindir/", "0700");
my $imagedir = $instdir . "/images";
install_dir($imagedir, "0755");
install_dir_files("images/", "$imagedir/", "0644");
my $datadir = $instdir . "/data";
install_dir($datadir, "0755");
install_dir_files("data/", "$datadir/", "0666");
my $configdir = $instdir . "/config";
install_dir($configdir, "0750");
print "Configuring BookExchange...\n";
open(CONF, ">$cgidir/conf.pl")
or die "\n** Could not open $cgidir/conf.pl: $!\n";
print CONF "
# Configuration for the BookExchange $VERSION
#The system running under
\$OS = '$os';
#The url of the site
\$BASE_URL = '$insturl';
#The url of the cgis
\$CGI_URL = '$cgiurl';
#The base directory
\$BASE_DIR = '$instdir';
#The directory of the cgi files
\$CGI_DIR = '$cgidir';
#The directory of the data
\$DATA_DIR = '$datadir';
#The location of sendmail or equivalent
\$SENDMAIL = '$sendmail';
#the contact-email
\$ADMIN_EMAIL = '$adminemail';
\$DB_DRIVER = '$dbdriver';
\$DB_USER = '$dbuser';
\$DB_NAME = '$dbname';
\$DB_PASSWORD = '$dbpassword';
1;
";
close CONF;
unless(chmod(oct("0755"), "$cgidir/conf.pl"))
{
print "**Cannot set permissions!\n**Aborting.\n";
exit(1);
}
print "
BookExchange $VERSION has been installed on your system.
Note: the files in
$datadir
have been set up as world-readable and world-writable for unix systems.
This isn't a good idea, however. Try to change it so that it only needs
to be in the web-browsing 'group'.
";
press_enter();
system('clear') if($os eq 'unix');
print "
The exchange divides books into different areas. The names (and number)
of the areas is up to you.
You can set up the areas now by entering one area per line. Just enter a blank
line when you're done. Otherwise, you should edit the area file by yourself:
its located at $datadir/areas.dat
";
if (input("Would you like to set up the areas now? [y/n]", "y", ["y","n"]) eq "y")
{
print "Enter one area per line, and a blank line when you're done.\n\n";
my $areas = "Nothing Selected\n";
my $line = <>;
chomp $line;
while (defined $line && $line ne '')
{
$areas .= "$line\n";
$line = <>;
chomp $line;
}
my $areafile = "$datadir/areas.dat";
open(CONF, ">$areafile")
or die "\n** Could not open $areafile: $!\n";
print CONF $areas;
close CONF;
unless(chmod(oct("0644"), "$areafile"))
{
print "**Cannot set permissions!\n**Aborting.\n";
exit(1);
}
print "Area setup complete! \n";
}
print "
Thanks for choosing the BookExchange for your site.
For more information on this program, please see
http://cs.uiowa.edu/~bdeitte/bookexchange
";
sub html_install
{
my $inst_dir = shift;
my $cgi_url = shift;
my @allfiles = get_files($inst_dir);
my @htmlfiles = grep /[\.html]$/, @allfiles;
my($file, $temp, $text);
foreach $file (@htmlfiles)
{
$file = "$inst_dir/$file";
open (HTML, $file) or (die "Couldn't open $file: $!");
$text = '';
while (<HTML>)
{
$temp = $_;
$temp =~ s/CGI_URL/$cgiurl/;
$temp =~ s/BASE_URL/$insturl/;
$text .= $temp;
}
close HTML;
open (NEWH, ">$file");
print NEWH $text;
close NEWH;
(chmod(oct("0644"), "$file")) or die "Couldn't set permissions on $file.";
}
}
sub install_dir
{
my ($dir, $permissions) = @_;
print "Setting up $dir\n";
if(-e $dir)
{
if(-d $dir)
{
print "**Directory already exists.\n";
}
else
{
print "**$dir is not a directory.\n";
print "**I'd better not touch it then\n**Aborting!\n";
exit(1);
}
}
else
{
unless(mkdir($dir, oct($permissions)))
{
print "**Cannot create $dir\n";
print "**Aborting!\n";
exit(1);
}
}
unless(chmod(oct($permissions), $dir))
{
print "**Cannot set permissions!\n**Aborting.\n";
exit(1);
}
}
sub install_file
{
my ($source, $destination, $permissions, $keepold) = @_;
my $uninstall;
if(-e $destination)
{
print "**$destination already exists.\n";
if($keepold)
{
print "**Preserving existing $destination\n";
return;
}
if(rename("$destination", "$destination.bak"))
{
print "**I kept a backup in $destination.bak\n";
}
else
{
print "**Cannot backup the file\n**Aborting.\n";
exit(1);
}
}
if ($os eq 'unix')
{
if(system("cp $source $destination"))
{
print "**Cannot copy $source to $destination\n**Aborting.";
exit(1);
}
}
elsif ($os eq 'nt')
{
$source =~ s/\//\\/g;
$destination =~ s/\//\\/g;
if(system("copy $source $destination"))
{
print "**Cannot copy $source to $destination\n**Aborting.";
exit(1);
}
}
unless(chmod(oct($permissions), $destination))
{
print "**Cannot set permissions!\n**Aborting.\n";
exit(1);
}
}
sub install_dir_files {
my ($source, $destination, $permissions, $keepold) = @_;
print "Installing files in $source to $destination\n";
my @files = get_files($source);
foreach (@files) {
install_file($source . $_, $destination . $_, $permissions, $keepold);
}
}
sub get_files {
my $dir = shift;
if (! opendir(THISDIR, $dir))
{
print "Can't open the directory $dir\n**Aborting";
exit(1);
}
# gets all files in directory (other than '.', '..')
my @shortfiles = grep !/^\.\.?$/, readdir THISDIR;
closedir THISDIR;
return @shortfiles;
}
sub set_perl_path
{
my ($file, $path) = @_;
print "Setting perl path in $file.\n";
my $source = "#!$path\n";
open(SRC, "$file") or die "Cannot open $file: $!\n";
my $dump = <SRC>;
while(<SRC>)
{
$source .= $_;
}
close SRC;
open(DST, ">$file") or die "Cannot open $file: $!\n";
print DST $source;
close DST;
}
sub input
{
my ($question, $default, $options) = @_;
my $answer = '';
while ($answer eq "")
{
print "$question\n";
$answer = <>;
chomp $answer;
print "\n";
$answer = $default unless $answer;
}
unless(!defined $options or scalar(@$options)==0 or member($options, $answer))
{
print "Invalid option. Please select one of: ";
print join(",", @$options)."\n";
return input($question, $default, $options);
}
return $answer;
}
sub press_enter
{
print "-- press enter to continue --";
my $blah = <>;
}
sub member
{
my ($ary, $el) = @_;
foreach (@$ary)
{
return 1 if($_ eq $el);
}
return 0;
}