| 1 | package CGI::Portal::Controls::emailpw; | ||
| 2 | # Copyright (c) 2008 Alexander David P. All rights reserved. | ||
| 3 | # | ||
| 4 | # Generate a random passw, update and email to user | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | use Digest::MD5 qw(md5_hex); | ||
| 9 | use CGI::Portal::Scripts::emailpw; | ||
| 10 | use CGI::Portal::Scripts; | ||
| 11 | |||
| 12 | use vars qw(@ISA $VERSION); | ||
| 13 | |||
| 14 | $VERSION = "0.12"; | ||
| 15 | |||
| 16 | @ISA = qw(CGI::Portal::Scripts); | ||
| 17 | |||
| 18 | 1; | ||
| 19 | |||
| 20 | sub launch { | ||
| 21 | my $self = shift; | ||
| 22 | |||
| 23 | # Require usr | ||
| 24 | if ($self->{'in'}{'usr'}){ | ||
| 25 | |||
| 26 | # Get users email | ||
| 27 | my $r = $self->{'rdb'}->exec("select $self->{'conf'}{'user_additional'}[0],$self->{'conf'}{'user_user_field'} from $self->{'conf'}{'user_table'} where $self->{'conf'}{'user_user_field'} like " . $self->{'rdb'}->escape($self->{'in'}{'usr'}) . " limit 1")->fetch; | ||
| 28 | |||
| 29 | # Validate email | ||
| 30 | if ($r->[0] =~ /.*@.*\./){ | ||
| 31 | |||
| 32 | # Generate a passw | ||
| 33 | my $pw = substr(md5_hex(rand(64)), 1, 9); | ||
| 34 | |||
| 35 | # Hash the passw | ||
| 36 | my $enc_pw = md5_hex($pw); | ||
| 37 | |||
| 38 | # Update | ||
| 39 | $self->{'rdb'}->exec("update $self->{'conf'}{'user_table'} set $self->{'conf'}{'user_passw_field'}=\'$enc_pw\' where $self->{'conf'}{'user_user_field'}=" . $self->{'rdb'}->escape($r->[1])); | ||
| 40 | |||
| 41 | # Email passw to user | ||
| 42 | mailit($r->[0],$self->{'conf'}{'admin_email'},"Logon Info ","Please use $pw to log on, and choose a new password at your convenience."); | ||
| 43 | $self->{'tmpl_vars'}{'result'} = "A temporary password has been emailed to you."; | ||
| 44 | } | ||
| 45 | elsif (! $r->[0] ){ | ||
| 46 | |||
| 47 | # No email no user | ||
| 48 | $self->{'tmpl_vars'}{'result'} = "Unknown User"; | ||
| 49 | |||
| 50 | }else{ | ||
| 51 | $self->{'tmpl_vars'}{'result'} = "Invalid email on record, please contact us."; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | # Redirect | ||
| 56 | $self->CGI::Portal::Scripts::emailpw::launch(); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | # Emailing | ||
| 61 | sub mailit { | ||
| 62 | my $recipient = shift; | ||
| 63 | my $sender = shift; | ||
| 64 | my $subject = shift; | ||
| 65 | my $message = shift; | ||
| 66 | open(MAIL, "|/usr/lib/sendmail -t"); | ||
| 67 | print MAIL "To: $recipient\n"; | ||
| 68 | print MAIL "From: $sender\n"; | ||
| 69 | print MAIL "Subject: $subject\n\n"; | ||
| 70 | print MAIL "$message"; | ||
| 71 | close (MAIL); | ||
| 72 | } |