Subversion Repositories VORC

Rev

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

package WebDB;
  use strict;
  use DBI;
  use List::Util 'shuffle';
  
  my $host_name = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "localhost" : "192.168.1.5";
  $ENV{HOME} = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco" : "/tmp";
#  $ENV{HOME} = "/tmp" unless $ENV{HOME};
  
  # Connect to MySQL server, using hardwired name and password
  sub connect {
                my $DB = shift // "vorc";
                my $dbuser = shift // undef;
                
          my $db_name = "rollerco_".$DB;
        my $dsn = "DBI:mysql:host=$host_name;database=$db_name";
                
    return connect_with_option_file($dsn, $dbuser) unless !(-f $ENV{HOME}.'/.my.cnf');
    
    warn "using hardcoded password (not $ENV{HOME}/.my.cnf)...";
          return (DBI->connect ($dsn, "root", "*********", {PrintError => 0, RaiseError => 0}) or db_error ($DBI::errstr));
  }
  
  # Connect to MySQL server, using name and password from the current
  # user's ~/.my.cnf option file. The mysql_read_default_file option,
  # when added to the DSN, specifies which option file to read.
  sub connect_with_option_file  {
        my $dsn = shift;
        my $dbuser = shift // undef;
        
          $dsn .= ";mysql_read_default_file=$ENV{HOME}/.my.cnf";
          
          if ($dbuser eq "OG") {
                my $connection = DBI->connect ($dsn, undef, undef, {PrintError => 0, RaiseError => 0});
                return $connection unless !$connection;     
          } elsif ($dbuser =~ /^[12345]$/) {
                my $connection = DBI->connect ($dsn.".$_", undef, undef, {PrintError => 0, RaiseError => 0});
                return $connection unless !$connection;     
          } elsif (defined $dbuser) {
            db_error ("Invalid User ID provided to WebDB::connect ($dbuser)");
          }
          
          for (shuffle qw(1 2 3 4 5)) {
            # warn "Trying connection $_";
                my $connection = DBI->connect ($dsn.".$_", undef, undef, {PrintError => 0, RaiseError => 0});
                return $connection unless !$connection;
          }
          
          db_error ($DBI::errstr);
  }
  
  # Quash any leading or trailing whitespace
  sub trim {
    my $str = shift;

        return "" if !defined $str;
        $str =~ s/^\s+//;
        $str =~ s/\s+$//;
        return ($str);
  }


sub db_error {
  my $ERR = shift // "something bad happened";
  
  use Carp qw/cluck/;
  cluck $ERR;
  
  use HTML::Tiny;
  my $h = HTML::Tiny->new( mode => 'html' );
  $ERR = $ERR ? $h->p ( {class=>"hint"}, $ERR) : "";
  use CGI qw/header/;
  print header ();
  print $h->html ([ $h->head ( [ $h->title ( "vORC - Database Error" ), $h->link ( { rel=>"stylesheet", href=>"/style.css" }) ] ),
          [ $h->div ({ class=>"sp0", style=>"max-width:700px" }, [
                                       $h->div ({ class=>"spLeft"  },  $h->a ({ href=>"/schedule/" }, $h->img ({ src=>"/logo.jpg", width=>"75", height=>"75" }))),
                                       $h->div ({ class=>"spRight" },  $h->h1 ("vORC Database Error"))
                                       ]),
            $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." ])
          ]
        ]);
        
  exit;
}

1; # return true