| 1 | package CGI::Portal::Controls::changepw; | ||
| 2 | # Copyright (c) 2008 Alexander David P. All rights reserved. | ||
| 3 | # | ||
| 4 | # Update users passw | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | use Digest::MD5 qw(md5_hex); | ||
| 9 | use CGI::Portal::Scripts::changepw; | ||
| 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 | my $passw; | ||
| 19 | |||
| 20 | 1; | ||
| 21 | |||
| 22 | sub launch { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | # Authenticate | ||
| 26 | $self->authenticate_user(); | ||
| 27 | if ($self->{'user'}){ | ||
| 28 | |||
| 29 | # Get users password hash | ||
| 30 | my $users = $self->{'rdb'}->exec("select $self->{'conf'}{'user_passw_field'} from $self->{'conf'}{'user_table'} where $self->{'conf'}{'user_user_field'}=" . $self->{'rdb'}->escape($self->{'user'}) . " limit 1")->fetch; | ||
| 31 | $passw = $users->[0]; | ||
| 32 | |||
| 33 | # Validate | ||
| 34 | unless ($self->input_error("pass_new","cpass_new","passw") || $self->chpss_error()){ | ||
| 35 | |||
| 36 | # Hash new passw | ||
| 37 | my $enc_passw = md5_hex($self->{'in'}{'pass_new'}); | ||
| 38 | |||
| 39 | # Update | ||
| 40 | $self->{'rdb'}->exec("update $self->{'conf'}{'user_table'} set $self->{'conf'}{'user_passw_field'}=\'$enc_passw\' where $self->{'conf'}{'user_user_field'}=" . $self->{'rdb'}->escape($self->{'user'})); | ||
| 41 | $self->{'tmpl_vars'}{'result'} = "Password updated!"; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | # Redirect | ||
| 46 | $self->CGI::Portal::Scripts::changepw::launch(); | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | # Validate | ||
| 51 | sub input_error { | ||
| 52 | my ($self, @requireds) = @_; | ||
| 53 | my $input_error = 0; | ||
| 54 | |||
| 55 | # Loop thru requireds | ||
| 56 | foreach my $required (@requireds) { | ||
| 57 | if (!$self->{'in'}{$required}){ | ||
| 58 | $self->{'tmpl_vars'}{"${required}_msg"} = "Field is required"; | ||
| 59 | $input_error = 1; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | if ($input_error) { | ||
| 64 | $self->{'tmpl_vars'}{'result'} = "Missing fields, no changes made."; | ||
| 65 | } | ||
| 66 | |||
| 67 | return $input_error; | ||
| 68 | } | ||
| 69 | |||
| 70 | # Validate | ||
| 71 | sub chpss_error { | ||
| 72 | my ($self) = @_; | ||
| 73 | my $input_error = 0; | ||
| 74 | |||
| 75 | # Compare password hashes | ||
| 76 | if ($self->{'in'}{'passw'} && md5_hex($self->{'in'}{'passw'}) ne $passw) { | ||
| 77 | $self->{'tmpl_vars'}{'passw_msg'} = "Incorrect Password"; | ||
| 78 | $input_error = 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | # Passwords must have 4 chars | ||
| 82 | if ($self->{'in'}{'pass_new'} && $self->{'in'}{'pass_new'} !~ /..../i) { | ||
| 83 | $self->{'tmpl_vars'}{'pass_new_msg'} = "Passwords must consist of at least 4 characters"; | ||
| 84 | $input_error = 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Compare confirm password | ||
| 88 | if ($self->{'in'}{'cpass_new'} && $self->{'in'}{'pass_new'} ne $self->{'in'}{'cpass_new'}) { | ||
| 89 | $self->{'tmpl_vars'}{'cpass_new_msg'} = "Please reenter and confirm password"; | ||
| 90 | $input_error = 1; | ||
| 91 | } | ||
| 92 | return $input_error; | ||
| 93 | } |