Subversion Repositories VORC

Rev

Rev 63 | Rev 150 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package WebDB;
  use strict;
  use DBI;
  my $host_name = "192.168.1.5";
  my $db_name = "rollerco_vorc";
  my $dsn = "DBI:mysql:host=$host_name;database=$db_name";
  $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  {
    return connect_with_option_file() 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  {
          $dsn .= ";mysql_read_default_file=$ENV{HOME}/.my.cnf";
          
          return (DBI->connect ($dsn, undef, undef, {PrintError => 0, RaiseError => 0}) or 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