Subversion Repositories VORC

Rev

Rev 206 | Details | Compare with Previous | Last modification | View Log | RSS feed

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