| 2 |
- |
1 |
package WebDB;
|
|
|
2 |
use strict;
|
|
|
3 |
use DBI;
|
|
|
4 |
use List::Util 'shuffle';
|
|
|
5 |
|
|
|
6 |
my $host_name = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "localhost" : "192.168.1.5";
|
|
|
7 |
$ENV{HOME} = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco" : "/tmp";
|
|
|
8 |
# $ENV{HOME} = "/tmp" unless $ENV{HOME};
|
|
|
9 |
|
|
|
10 |
sub SessionDSN {
|
|
|
11 |
return "DBI:mysql:wftdi_peeps;host=$host_name;mysql_read_default_file=/tmp/.my.cnf";
|
|
|
12 |
}
|
|
|
13 |
|
|
|
14 |
# Connect to MySQL server, using hardwired name and password
|
|
|
15 |
sub connect {
|
|
|
16 |
my $DB = shift // "peeps";
|
|
|
17 |
my $dbuser = shift // undef;
|
|
|
18 |
|
|
|
19 |
my $db_name = "wftdi_".$DB;
|
|
|
20 |
my $dsn = "DBI:mysql:host=$host_name;database=$db_name";
|
|
|
21 |
|
|
|
22 |
return connect_with_option_file($dsn, $dbuser) unless !(-f $ENV{HOME}.'/.my.cnf');
|
|
|
23 |
|
|
|
24 |
warn "using hardcoded password (not $ENV{HOME}/.my.cnf)...";
|
|
|
25 |
return (DBI->connect ($dsn, "root", "*********", {PrintError => 0, RaiseError => 0}) or db_error ($DBI::errstr));
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
# Connect to MySQL server, using name and password from the current
|
|
|
29 |
# user's ~/.my.cnf option file. The mysql_read_default_file option,
|
|
|
30 |
# when added to the DSN, specifies which option file to read.
|
|
|
31 |
sub connect_with_option_file {
|
|
|
32 |
my $dsn = shift;
|
|
|
33 |
my $dbuser = shift // undef;
|
|
|
34 |
|
|
|
35 |
$dsn .= ";mysql_read_default_file=$ENV{HOME}/.my.cnf";
|
|
|
36 |
|
|
|
37 |
if ($dbuser eq "OG") {
|
|
|
38 |
my $connection = DBI->connect ($dsn, undef, undef, {PrintError => 0, RaiseError => 0});
|
|
|
39 |
return $connection unless !$connection;
|
|
|
40 |
} elsif ($dbuser =~ /^[12345]$/) {
|
|
|
41 |
my $connection = DBI->connect ($dsn.".$_", undef, undef, {PrintError => 0, RaiseError => 0});
|
|
|
42 |
return $connection unless !$connection;
|
|
|
43 |
} elsif (defined $dbuser) {
|
|
|
44 |
db_error ("Invalid User ID provided to WebDB::connect ($dbuser)");
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
for (shuffle qw(1 2 3 4 5)) {
|
|
|
48 |
# warn "Trying connection $_";
|
|
|
49 |
my $connection = DBI->connect ($dsn.".$_", undef, undef, {PrintError => 0, RaiseError => 0});
|
|
|
50 |
return $connection unless !$connection;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
db_error ($DBI::errstr);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
# Quash any leading or trailing whitespace
|
|
|
57 |
sub trim {
|
|
|
58 |
my $str = shift;
|
|
|
59 |
|
|
|
60 |
return "" if !defined $str;
|
|
|
61 |
$str =~ s/^\s+//;
|
|
|
62 |
$str =~ s/\s+$//;
|
|
|
63 |
return ($str);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
sub db_error {
|
|
|
68 |
my $ERR = shift // "something bad happened";
|
|
|
69 |
|
|
|
70 |
use Carp qw/cluck/;
|
|
|
71 |
cluck $ERR;
|
|
|
72 |
|
|
|
73 |
use HTML::Tiny;
|
|
|
74 |
my $h = HTML::Tiny->new( mode => 'html' );
|
|
|
75 |
$ERR = $ERR ? $h->p ( {class=>"hint"}, $ERR) : "";
|
|
|
76 |
use CGI qw/header/;
|
|
|
77 |
print header ();
|
|
|
78 |
print $h->html ([ $h->head ( [ $h->title ( "PEEPS - Database Error" ), $h->link ( { rel=>"stylesheet", href=>"/style.css" }) ] ),
|
|
|
79 |
[ $h->div ({ class=>"sp0", style=>"max-width:700px" }, [
|
|
|
80 |
$h->div ({ class=>"spLeft" }, $h->a ({ href=>"/schedule/" }, $h->img ({ src=>"/logo.jpg", width=>"75", height=>"75" }))),
|
|
|
81 |
$h->div ({ class=>"spRight" }, $h->h1 ("PEEPS Database Error"))
|
|
|
82 |
]),
|
|
|
83 |
$h->div ({ class=>"error" }, [ $h->br, $h->br, "If you're seeing this, there was an error connecting to the database:", $h->br, $ERR, "Apologies." ])
|
|
|
84 |
]
|
|
|
85 |
]);
|
|
|
86 |
|
|
|
87 |
exit;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
1; # return true
|