| 1 | package CGI::Portal::Controls::register; | ||
| 2 | # Copyright (c) 2008 Alexander David P. All rights reserved. | ||
| 3 | # | ||
| 4 | # Insert user info | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | use Digest::MD5 qw(md5_hex); | ||
| 9 | use CGI::Portal::Scripts::logon; | ||
| 10 | use CGI::Portal::Scripts::register; | ||
| 11 | use CGI::Portal::Scripts; | ||
| 12 | |||
| 13 | use vars qw(@ISA $VERSION); | ||
| 14 | |||
| 15 | $VERSION = "0.12"; | ||
| 16 | |||
| 17 | @ISA = qw(CGI::Portal::Scripts); | ||
| 18 | |||
| 19 | 1; | ||
| 20 | |||
| 21 | sub launch { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | # Validate | ||
| 25 | unless ($self->input_error("user","password","cpassw","email") || $self->register_error()){ | ||
| 26 | |||
| 27 | # Get the current index | ||
| 28 | my $cc = $self->{'rdb'}->exec("select $self->{'conf'}{'user_index_field'} from $self->{'conf'}{'user_table'} order by $self->{'conf'}{'user_index_field'} desc limit 1")->fetch; | ||
| 29 | my $c = $cc->[0]+1; | ||
| 30 | |||
| 31 | # Hash the passw | ||
| 32 | my $enc_passw = md5_hex($self->{'in'}{'password'}); | ||
| 33 | |||
| 34 | # Collect values for SQL | ||
| 35 | my @additional_values; | ||
| 36 | foreach my $f (@{$self->{'conf'}{'user_additional'}}) { | ||
| 37 | push(@additional_values, $self->{'in'}{$f}); | ||
| 38 | } | ||
| 39 | |||
| 40 | # Escape values | ||
| 41 | my $values = $self->{'rdb'}->escape($c,$self->{'in'}{'user'},$enc_passw,@additional_values); | ||
| 42 | |||
| 43 | # Join fields for SQL | ||
| 44 | my $fields = join(',', @{$self->{'conf'}{'user_additional'}}); | ||
| 45 | |||
| 46 | # Insert | ||
| 47 | $self->{'rdb'}->exec("insert into $self->{'conf'}{'user_table'} ($self->{'conf'}{'user_index_field'},$self->{'conf'}{'user_user_field'},$self->{'conf'}{'user_passw_field'},$fields) values ($values)"); | ||
| 48 | |||
| 49 | # Assign user to object | ||
| 50 | $self->{'user'} = $self->{'in'}{'user'}; | ||
| 51 | |||
| 52 | # Redirect | ||
| 53 | $self->CGI::Portal::Scripts::logon::launch(); | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Redirect | ||
| 58 | $self->CGI::Portal::Scripts::register::launch(); | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | # Validate | ||
| 63 | sub input_error { | ||
| 64 | my ($self, @requireds) = @_; | ||
| 65 | my $input_error = 0; | ||
| 66 | |||
| 67 | # Loop thru requireds | ||
| 68 | foreach my $required (@requireds) { | ||
| 69 | if (!$self->{'in'}{$required}){ | ||
| 70 | $self->{'tmpl_vars'}{"${required}_msg"} = "Field is required"; | ||
| 71 | $input_error = 1; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | if ($input_error) { | ||
| 76 | $self->{'tmpl_vars'}{'result'} = "Missing fields, no changes made."; | ||
| 77 | } | ||
| 78 | |||
| 79 | return $input_error; | ||
| 80 | } | ||
| 81 | |||
| 82 | # Validate | ||
| 83 | sub register_error { | ||
| 84 | my ($self) = @_; | ||
| 85 | my $input_error = 0; | ||
| 86 | |||
| 87 | # See if user name is available | ||
| 88 | my $r = $self->{'rdb'}->exec("select $self->{'conf'}{'user_index_field'} from $self->{'conf'}{'user_table'} where $self->{'conf'}{'user_user_field'} like " . $self->{'rdb'}->escape($self->{'in'}{'user'}) . " limit 1")->fetch; | ||
| 89 | if ($r->[0]) { | ||
| 90 | $self->{'tmpl_vars'}{'user_msg'} = "User name $self->{'in'}{'user'} is not available"; | ||
| 91 | $input_error = 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | # User name requirements | ||
| 95 | if ($self->{'in'}{'user'} && $self->{'in'}{'user'} =~ /[^\w ]/i) { | ||
| 96 | $self->{'tmpl_vars'}{'user_msg'} = "User names must consist of letters or numbers"; | ||
| 97 | $input_error = 1; | ||
| 98 | } | ||
| 99 | if ($self->{'in'}{'user'} && $self->{'in'}{'user'} =~ / /i) { | ||
| 100 | $self->{'tmpl_vars'}{'user_msg'} = "User names cannot contain spaces"; | ||
| 101 | $input_error = 1; | ||
| 102 | } | ||
| 103 | if ($self->{'in'}{'user'} && $self->{'in'}{'user'} =~ /................/i) { | ||
| 104 | $self->{'tmpl_vars'}{'user_msg'} = "User names must consist of less than 16 characters"; | ||
| 105 | $input_error = 1; | ||
| 106 | } | ||
| 107 | |||
| 108 | # Password requirements | ||
| 109 | if ($self->{'in'}{'password'} && $self->{'in'}{'password'} !~ /..../i) { | ||
| 110 | $self->{'tmpl_vars'}{'password_msg'} = "Passwords must consist of at least 4 characters"; | ||
| 111 | $input_error = 1; | ||
| 112 | } | ||
| 113 | if ($self->{'in'}{'cpassw'} && $self->{'in'}{'password'} ne $self->{'in'}{'cpassw'}) { | ||
| 114 | $self->{'tmpl_vars'}{'cpassw_msg'} = "Please reenter and confirm password"; | ||
| 115 | $input_error = 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | return $input_error; | ||
| 119 | } |