#!/usr/bin/perl -w ##################################### # There are two database tables this application uses. One is # for riders, the other for drivers. I chose to put them in two # tables because they talk about different things. The only overlap # is in the use of name and email. # # Passengers know in which car they are, cars know how many passengers # they have, but not who they are. # # Copyright 2001 Ben Hartshorne. All rights reserved. # Version 2.6 # $Id: skipool.cgi,v 1.10 2002/10/20 21:04:37 ben Exp $ ##################################### #use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; use CGI::Pretty; use DBI; use Mail::Sendmail; use Date::Manip; use Digest::SHA1; use ski_db; my $DEBUGGING = 0; my $DATE = `date`; my $posted = new CGI; my $action; print $posted->header; print $posted->start_html( -title=>'SkiPool', -author=>'ben-skicode@hartshorne.net', -meta=>{'generator'=>'emacs'}, -BGCOLOR=>'FFFFFF'); if($DEBUGGING){ foreach $key ($posted->param()) { print $key . " => " . $posted->param("$key") . "
\n"; } } $action = $posted->param('action'); print $posted->h1($action) if $DEBUGGING; SWITCH: { if($action eq 'display_items'){ &display_items($posted);last SWITCH; } if($action eq 'sign_up_for_car'){ &sign_up_for_car($posted);last SWITCH; } if($action eq 'delete_from_car'){ &delete_from_car($posted);last SWITCH; } if($action eq 'delete_from_car2'){ &delete_from_car2($posted);last SWITCH; } if($action eq 'sign_up_Submit'){ &sign_up_Submit($posted);last SWITCH; } if($action eq 'register_car_submit'){®ister_car_submit($posted);last SWITCH; } if($action eq 'register_car_verified'){®ister_car_verified($posted);last SWITCH; } &actionNotRecognized($posted); } print $posted->hr; # print $posted->dump; print qq{\n
Ben Hartshorne, skipool\@green.hartshorne.netThese are the cars going up and coming back from Tahoe. Click on one of the ";
print " cars below to join.
";
print "If you're already listed in one of the cars, and you need to cancel, click ";
print " on your name and it will remove you from that car.
";
print "If none of these cars meet your schedule, ";
print " request a ride.
"; print "
| Name | Time Up | Time Down | Origin | Destination | Equip | Add to car | Delete | Update |
| $rider | $rider_up | $rider_down | "); print("$origin | $destination | "); print(""); if($skis eq 'on'){ print "Skis "; } if($board eq 'on'){ print "Board"; } print " "; print " | "; print(qq{Take me with you! | }); print(qq{(D) | }); print(qq{(U) | }); print "
\n"; print "
bound vars for fetched car
\n" if $DEBUGGING; while ($car_sth->fetch()) { if($gray eq "#CCCCCC"){ $gray = "#AAFFAA"; }else{ $gray = "#CCCCCC"; } $etd = &Date::Manip::UnixDate($etd, "%a %b %e, %I:%M %p"); print <| Driver: $driver (update your itinerary) | |||||||||||||
| Driving Up: $etd |
|
||||||||||||
Passengers
| \n";#done printing passengers
print "Notes";
print "
| ";
print "||||||||||||
bound vars for fetched car
\n" if $DEBUGGING; while ($car_sth->fetch()) { if($gray eq "#CCFFFF"){ $gray = "#CCCCCC"; }else{ $gray = "#CCFFFF"; } $etd = &Date::Manip::UnixDate($etd, "%a %b %e, %I:%M %p"); print <| Driver: $driver (update your itinerary) | |||||||||||||
| Driving Down: $etd |
|
||||||||||||
Passengers
| \n"; #done printing passengers
print "Notes";
print "
| ";
print "||||||||||||
You have asked to join " . $driver . "'s car for the trip " . $up_or_down . ". He/she has $num_spots open spots left, \n"; print "and is leaving on " . &Date::Manip::UnixDate($etd, "%a %b %e, at %I:%M %p."); print "Your driver can be reached at $phone_number or $email. You can also reach everyone in your car at tahoecar-" . $car_id . "\@skipool.hartshorne.net. Make sure to call your driver to confirm.
\n"; print $posted->start_form( -method=>'POST', -action=>'skipool.cgi'); $posted->param('action', 'sign_up_Submit'); print $posted->hidden('action'); $posted->param('car_id', $car_id); print $posted->hidden('car_id'); print "
| \n";
print "
|
\n"; print "It may take a little time, but please press the submit button only once.\n"; print $posted->submit(-name=>"Carpool!"); print $posted->end_form; &Ski::DB::return_handle($dbh); } sub sign_up_Submit { my $posted = shift; my ($dbh, $car_sth, $car_sql, $pass_sth, $pass_sql); my ($car_open_spots, $driver, $driver_phone_number, $driver_email, $pass_exist); my $car_id = $posted->param('car_id'); my $up_or_down = $posted->param('trip'); my $rider = $posted->param('rider'); my $phone_number = $posted->param('phone_number'); my $email = $posted->param('email'); my $password = $posted->param('password'); my ($encrypted_pass, $database_pass); my $pass_notes = $posted->param('pass_notes'); my $car_change_mail = $posted->param('car_change_mail'); my $new_car_mail = $posted->param('new_car_mail'); my $blue_moon_mail = $posted->param('blue_moon_mail'); my %mail; #to send email to the driver $dbh = &Ski::DB::get_handle(); $car_sql = "SELECT open_spots_" . $up_or_down . ", driver, phone_number, email FROM cars WHERE car_id = $car_id"; $car_sth = $dbh->prepare($car_sql); $car_sth->execute(); $car_sth->bind_columns(undef, \$car_open_spots, \$driver, \$driver_phone_number, \$driver_email); $car_sth->fetch(); if($car_open_spots < 1){ print $posted->h2("I'm Sorry."); print "
The car you chose has fewer seats than you said you need. \n"; print "I don't believe in crowded cars, you'll have to go back and choose\n"; print "another car. Sorry!
\n"; return; } $pass_sql = "SELECT rider FROM passengers WHERE car_id_" . $up_or_down . " = $car_id"; $pass_sth = $dbh->prepare($pass_sql); $pass_sth->execute(); $pass_sth->bind_columns(undef, \$pass_exist); ############### check and see if they already are in the car, and warn them. while ($pass_sth->fetch()){ if($pass_exist eq $rider){ #make this test better print "
You're already in the car, but I'm adding you a second time. If this isn't\n"; print "what you want, go to the main page and delete the second occurence of you \n"; print "from the car.
"; print "pass_exist = $pass_exist, rider = $rider
" if $DEBUGGING; last; } } print "Tests passed ok, entering rider in database
\n" if $DEBUGGING; print "car open spots = $car_open_spots before resetting
\n" if $DEBUGGING; $car_open_spots -= 1; print "car open spots = $car_open_spots after resetting
\n" if $DEBUGGING; $encrypted_pass = Digest::SHA1::sha1_base64($password); $pass_sql = "INSERT INTO passengers (rider, car_id_" . $up_or_down . ", phone_number, email, password, car_change_mail, new_car_mail, pass_notes) VALUES (" . $dbh->quote($rider) . ", " . $dbh->quote($car_id) . ", " . $dbh->quote($phone_number) . ", " . $dbh->quote($email) . ", " . $dbh->quote($encrypted_pass) . ", " . $dbh->quote($car_change_mail) . ", " . $dbh->quote($new_car_mail) . ", " . # $dbh->quote($blue_moon_mail) . ", " . $dbh->quote($pass_notes) . ")"; $car_sql = "UPDATE cars SET open_spots_" . $up_or_down . " = $car_open_spots WHERE car_id = $car_id"; $car_sth = $dbh->prepare($car_sql); $pass_sth = $dbh->prepare($pass_sql); print "about to enter passengers
\n" if $DEBUGGING; print "pass_sql = >>> $pass_sql <<<
\n" if $DEBUGGING; print "car_sql = >>> $car_sql <<<
\n" if $DEBUGGING; $pass_sth->execute(); #this order is actually important, cuz if the pass insert fails, $car_sth->execute(); # you don't want to decrement the car's open_spots number. ########### send driver an email letting them know they have another passenger %mail = ( To => "$driver <$driver_email>", From => "SkipoolYou have been registered with $driver. Remember to call them at $driver_phone_number\n"; print "to confirm. "; if (sendmail(%mail)){ print "I have sent a message to $driver announcing you as a passenger, but you should talk\n"; print "to him or her directly anyways.
\n"; }else{ print "I tried to send $driver an email announcing you as a passenger, but something went wrong.\n"; print "Would you take care of that for me? You can reach $driver at $driver_email. Thanks!\n"; } %mail = ( To => "$rider <$email>", From => "SkipoolIf you want to contact everyone in this car, you can send mail to tahoecar-$car_id\@skipool.hartshorne.net.
\n"; print "See you up there!
\n"; #### subscribe to newcar if they asked us to if($new_car_mail eq "on"){ %mail = ( To => "newcar-request\@hartshorne.net", From => "$email", Subject => "subscribe" ); if(sendmail(%mail)){ print "I have sent a subscription request to newcar\@hartshorne.net, the mailing list that announces when new cars sign up. You should get a confirmation in the mail soon. You must reply to this confirmation before your subscription will be activated. Visit http://skipool.hartshorne.net/cgi-bin/mailman/listinfo/newcar for more information about the list (or to unsubscribe).
"; }else{ print "I tried to send mail to newcar-request\@hartshorne.net to subscribe you to the new car announcement mailing list, but I failed. You're going to have to do it yourself. Please go to http://skipool.hartshorne.net/cgi-bin/mailman/listinfo/newcar and subscribe. Thanks!
"; } } print "You can verify you were added or Go Back Home
"; &Ski::DB::return_handle($dbh); } # takes a dbh and car_id # returns a string of the passengers, formatted for text sub get_formatted_rider_list(){ my $dbh = shift; my ($sth, $sql); my $car_id = shift; my $up_or_down = shift; my ($passenger, $pass_email, $pass_phone); my $rider_list; $sql = "SELECT rider, email, phone_number FROM passengers WHERE car_id_" . $up_or_down . " = $car_id"; $sth = $dbh->prepare($sql); $sth->execute(); $sth->bind_columns(undef, \$passenger, \$pass_email, \$pass_phone); while($sth->fetch){ $rider_list .= "\t$passenger ($pass_phone) <$pass_email>\n"; } return $rider_list; } sub delete_from_car { my $posted = shift; my $car_id = $posted->param('car_id'); my $pass_id = $posted->param('pass_id'); my $up_or_down = $posted->param('trip'); print $posted->h1("Are You Sure?"); print "Are you sure you want to remove yourself from this car? If not, press the back button now. click 'continue.' if you are sure.
"; print $posted->start_form( -method=>'POST', -action=>'skipool.cgi'); $posted->param('action', 'delete_from_car2'); print $posted->hidden('action'); $posted->param('car_id', $car_id); print $posted->hidden('car_id'); $posted->param('pass_id', $pass_id); print $posted->hidden('pass_id'); $posted->param('trip', $up_or_down); print $posted->hidden('trip'); print "Please give me a password: \n"; print $posted->password_field( -name=>'password', -size=>40); print "
It may take a little time, but please press the continue button only once."; print $posted->submit('button', 'Continue'); print $posted->end_form; } sub delete_from_car2 { my $posted = shift; my $car_id = $posted->param('car_id'); my $pass_id = $posted->param('pass_id'); my $up_or_down = $posted->param('trip'); my $password = $posted->param('password'); my ($sql, $sth, $dbh); my ($encrypted_pass, $passenger_database_pass, $driver_database_pass); my $open_spots; my ($passenger, $passenger_email, $driver, $driver_email, $driver_phone_number); my %mail; my ($demail_retval, $pemail_retval); $dbh = &Ski::DB::get_handle(); $sql = "SELECT open_spots_" . $up_or_down . ", driver, email, password, phone_number FROM cars WHERE car_id = $car_id"; print "
My sql statement is \"$sql\"
" if $DEBUGGING; $sth = $dbh->prepare($sql); $sth->execute(); $sth->bind_columns(undef, \$open_spots, \$driver, \$driver_email, \$driver_database_pass, \$driver_phone_number); $sth->fetch(); $sql = "SELECT rider, email, password FROM passengers WHERE pass_id = $pass_id"; $sth = $dbh->prepare($sql); $sth->execute(); $sth->bind_columns(undef, \$passenger, \$passenger_email, \$passenger_database_pass); $sth->fetch(); $open_spots++; ### verify password $encrypted_pass = &Digest::SHA1::sha1_base64($password); if(($encrypted_pass ne $passenger_database_pass) and ($encrypted_pass ne $driver_database_pass)){ #password failed. print "I'm sorry, but the password you entered does not belong to either the driver or the passenger. Passengers can only remove themselves, and drivers can only remove passengers in their own car.
"; print "Forgot your password?\n"; return; } $sql = "UPDATE cars SET open_spots_" . $up_or_down . " = $open_spots WHERE car_id = $car_id"; $sth = $dbh->prepare($sql); $sth->execute(); $sql = "UPDATE passengers SET car_id_" . $up_or_down . " = '' WHERE pass_id = $pass_id"; $sth = $dbh->prepare($sql); $sth->execute(); print $posted->h2("Removed"); print "$passenger has been removed from " . $driver . "'s car for the trip " . $up_or_down . ".\n
"; # send an email to driver to tell them %mail = ( To => "$driver <$driver_email>", From => "Skipool\n"; }else{ print "I tried to send $driver an email saying that you have decided not to ride with him or \n"; print "her, but I failed. Would you take care of that for me? $driver can be reached at \n"; print "$driver_email or $driver_phone_number.\n
"; } %mail = ( To => "$passenger <$passenger_email>", From => "Skipool\n"; }else{ print "I have failed to send $passenger an email confirming this removal. Sorry!
\n"; } print "
I'm sorry you won't be joining us this time, but thanks for playing!
\n"; print ""; &Ski::DB::return_handle($dbh); } sub register_car_submit { my $posted = shift; my ($car_id, $driver, $phone_number, $email, $password, $car_type, $open_spots, $rtd, $rtr, $ski_rack, $fourwd, $chains, $stops, $music, $notes, $origin, $destination); my ($new_pass, $encrypted_pass, $database_pass); my ($dbh, $sql, $sth); my $form_ok = 'true'; $car_id = $posted->param('car_id'); $driver = $posted->param('driver'); $phone_number = $posted->param('phone_number'); print "snagged phone: is >> $phone_number <<Name: $driver Description: $car_type You plan on leaving the bay area: $rtd
Phone: $phone_number
Email: $email
Password: xxxxxxx
END_DRIVE_CONF_A
if($new_pass){
print(qq{Your old and new passwords are accepted; when you click submit, your password will be changed.
});
}
print <Your Car:
Passengers: $open_spots
Ski Rack: $ski_rack
4WD: $fourwd
Chains: $chains
Stops: $stops
Music: $music
Notes: $notes
Origin: $origin
Destination: $destination
Timing:
You plan on coming back: $rtr