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