Subversion Repositories PEEPS

Rev

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

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