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