Annotation of loncom/interface/createaccount.pm, revision 1.2

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow visitors to create a user account with the username being either an 
                      3: # institutional log-in ID (institutional authentication required - localauth
                      4: #  or kerberos) or an e-mail address.
                      5: #
1.2     ! raeburn     6: # $Id: createaccount.pm,v 1.1 2008/02/24 23:23:39 raeburn Exp $
1.1       raeburn     7: #
                      8: # Copyright Michigan State University Board of Trustees
                      9: #
                     10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     11: #
                     12: # LON-CAPA is free software; you can redistribute it and/or modify
                     13: # it under the terms of the GNU General Public License as published by
                     14: # the Free Software Foundation; either version 2 of the License, or
                     15: # (at your option) any later version.
                     16: #
                     17: # LON-CAPA is distributed in the hope that it will be useful,
                     18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     20: # GNU General Public License for more details.
                     21: #
                     22: # You should have received a copy of the GNU General Public License
                     23: # along with LON-CAPA; if not, write to the Free Software
                     24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     25: #
                     26: # /home/httpd/html/adm/gpl.txt
                     27: #
                     28: # http://www.lon-capa.org/
                     29: #
                     30: #
                     31: package Apache::createaccount;
                     32: 
                     33: use strict;
                     34: use Apache::Constants qw(:common);
                     35: use Apache::lonacc;
                     36: use Apache::lonnet;
                     37: use Apache::loncommon;
                     38: use Apache::lonlocal;
                     39: use Apache::resetpw;
                     40: use Authen::Captcha;
                     41: use DynaLoader; # for Crypt::DES version
                     42: use Crypt::DES;
                     43: use LONCAPA;
                     44: 
                     45: sub handler {
                     46:     my $r = shift;
                     47:     &Apache::loncommon::content_type($r,'text/html');
                     48:     $r->send_http_header;
                     49:     if ($r->header_only) {
                     50:         return OK;
                     51:     }
                     52:     my $domain = &Apache::lonnet::default_login_domain();
                     53:     my $domdesc = &Apache::lonnet::domain($domain,'description');
                     54:     my $start_page =
                     55:         &Apache::loncommon::start_page('Create a user account in LON-CAPA','',
                     56:                                            {
                     57:                                              'no_inline_link'   => 1,});
                     58:     $r->print($start_page);
                     59:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                     60:     &Apache::lonhtmlcommon::add_breadcrumb
                     61:     ({href=>"/adm/createuser",
                     62:       text=>"New username"});
                     63:     my $contact_name = &mt('LON-CAPA helpdesk');
                     64:     my $contact_email =  $r->dir_config('lonSupportEMail');
                     65:     my $lonhost = $r->dir_config('lonHostID');
                     66:     my $include = $r->dir_config('lonIncludes');
                     67:     &Apache::lonacc::get_posted_cgi($r);
                     68:     &Apache::lonlocal::get_language_handle($r);
                     69:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
                     70:     my $token = $env{'form.token'};
                     71:     my $output;
                     72:     my $cancreate;
                     73:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
                     74:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                     75:         if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') { 
1.2     ! raeburn    76:             if ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') {
        !            77:                 $cancreate = $domconfig{'usercreation'}{'cancreate'}{'selfcreate'};
1.1       raeburn    78:             }
                     79:         }
                     80:     }
                     81:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account'));
                     82:     if (!$cancreate) {
                     83:         $output = &mt('Creation of a new user account using an e-mail address as username or a loginID from your institution is not permitted in the domain: [_1] ([_2])',$domain,$domdesc);
                     84:     } elsif ($token) {  
                     85:         $output = &process_mailtoken($r,$token,$contact_name,$contact_email,$domain,
                     86:                                      $domdesc,$lonhost,$include);
                     87:     } elsif ($env{'form.create_with_email'}) {
                     88:         $output = &process_email_request($env{'form.useremail'},$domain,$domdesc,
                     89:                                          $contact_name,$contact_email,$cancreate,
                     90:                                          $lonhost,$domconfig{'usercreation'});
                     91:     } elsif ($env{'form.phase'} eq 'username_validation') {
                     92:         $output = &username_validation($env{'form.uname'},$domain,$domdesc,
                     93:                                        $contact_name,$contact_email);
                     94:     } elsif ($env{'form.phase'} eq 'username_activation') {
                     95:         (my $result,$output) = &username_activation($env{'form.uname'},
                     96:                                                     $domain,$domdesc);
                     97:     } else {
                     98:         my $now=time;
                     99:         if ($cancreate eq 'any' || $cancreate eq 'login') {
                    100:             my $jsh=Apache::File->new($include."/londes.js");
                    101:             $r->print(<$jsh>);
                    102:             $r->print(&javascript_setforms($now));
                    103:         }
                    104:         $output = &print_username_form($domain,$domdesc,$cancreate,$now,$lonhost); 
                    105:     }
                    106:     $r->print($output);
                    107:     $r->print(&Apache::loncommon::end_page());
                    108:     return OK;
                    109: }
                    110: 
                    111: sub javascript_setforms {
                    112:     my ($now) =  @_;
                    113:     my $js = <<ENDSCRIPT;
                    114:  <script language="JavaScript">
                    115:     function send() {
                    116:         this.document.server.elements.uname.value = this.document.client.elements.uname.value;
                    117:         uextkey=this.document.client.elements.uextkey.value;
                    118:         lextkey=this.document.client.elements.lextkey.value;
                    119:         initkeys();
                    120: 
                    121:         this.document.server.elements.upass.value
                    122:             = crypted(this.document.client.elements.upass$now.value);
                    123: 
                    124:         this.document.client.elements.uname.value='';
                    125:         this.document.client.elements.upass$now.value='';
                    126: 
                    127:         this.document.server.submit();
                    128:         return false;
                    129:     }
                    130:  </script>
                    131: ENDSCRIPT
                    132:     return $js;
                    133: }
                    134: 
                    135: sub javascript_checkpass {
                    136:     my ($now) = @_;
                    137:     my $nopass = &mt('You must enter a password');
                    138:     my $mismatchpass = &mt('The passwords you entered did not match.').'\\n'.
                    139:                        &mt('Please try again.'); 
                    140:     my $js = <<"ENDSCRIPT";
                    141: <script type="text/javascript" language="JavaScript">
                    142:     function checkpass() {
                    143:         var upass = this.document.client.elements.upass$now.value;
                    144:         var upasscheck = this.document.client.elements.upasscheck$now.value;
                    145:         if (upass == '') {
                    146:             alert("$nopass");
                    147:             return;
                    148:         }
                    149:         if (upass == upasscheck) {
                    150:             this.document.client.elements.upasscheck$now.value='';
                    151:             send();
                    152:             return;
                    153:         } else {
                    154:             alert("$mismatchpass");
                    155:             return;
                    156:         } 
                    157:     }
                    158: </script>
                    159: ENDSCRIPT
                    160:     return $js;
                    161: }
                    162: 
                    163: sub print_username_form {
                    164:     my ($domain,$domdesc,$cancreate,$now,$lonhost) = @_;
                    165:     my %lt = &Apache::lonlocal::texthash(
                    166:                                          unam => 'username',
                    167:                                          udom => 'domain',
                    168:                                          uemail => 'Email address in LON-CAPA',
                    169:                                          proc => 'Proceed');
                    170:     my $output;
                    171:     if ($cancreate eq 'any' || $cancreate eq 'login') {
                    172:         my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    173:         if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
                    174:             $output = '<div class="LC_left_float"><h3>'.&mt('Create account with a username provided by your institution').'</h3>';
                    175:             $output .= &mt('If you already have a Log-in ID at your institution, you may be able to use it[_1] for LON-CAPA.','<br />').'&nbsp;'.&mt('Type in your Log-in ID and password to find out.').'<br /><br />';
                    176:             my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    177:             my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
                    178:             my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
                    179:                                                $lonhost);
                    180:             $output .= &serverform($logtoken,$lonhost);
                    181:             my $unameform = '<input type="text" name="uname" size="10" value="" />';
                    182:             my $upassform = '<input type="password" name="upass'.$now.'" size="10" />';
                    183:             my $submit_text = &mt('Create LON-CAPA account');
                    184:             $output .= '<form name="client" method="post" />'."\n". 
                    185:                        &Apache::lonhtmlcommon::start_pick_box()."\n".
                    186:                        &Apache::lonhtmlcommon::row_title(&mt('Log-in ID'),
                    187:                                                         'LC_pick_box_title')."\n".
                    188:                        $unameform."\n".
                    189:                        &Apache::lonhtmlcommon::row_closure(1)."\n".
                    190:                        &Apache::lonhtmlcommon::row_title(&mt('Password'),
                    191:                                                         'LC_pick_box_title')."\n".
                    192:                        $upassform."\n".'<br /><br />'."\n".
                    193:                        '<input type="button" name="username_validation" value="'.
                    194:                        $submit_text.'" onclick="javascript:send()" />'."\n". 
                    195:                        &Apache::lonhtmlcommon::row_closure(1)."\n".
                    196:                        &Apache::lonhtmlcommon::end_pick_box().'<br /><br />'."\n".
                    197:                        '<input type="hidden" name="lextkey" value="'.$lextkey.'">'."\n".
                    198:                        '<input type="hidden" name="uextkey" value="'.$uextkey.'">'."\n".
                    199:                        '</form></div>';
                    200:         }
                    201:     }
                    202:     if (($cancreate eq 'any') || ($cancreate eq 'email')) {
                    203:         $output .= '<div class="LC_left_float"><h3>'.&mt('Create account with an e-mail address as your username').'</h3>';
                    204:         if ($cancreate eq 'any') {
                    205:             $output .= &mt('Provide your e-mail address to request a LON-CAPA account if you do not have [_1] a log-in ID at your institution.','<br />').'<br /><br />';
                    206:         } elsif ($cancreate eq 'unofficial') {
                    207:             $output .= '<br />';
                    208:         }
                    209:         my $emailform = '<input type="text" name="useremail" size="25" value="" />';
                    210:         my $captchaform = &create_captcha();
                    211:         my $submit_text = &mt('Request LON-CAPA account');
                    212:         $output .=  '<form name="createaccount" method="post" onsubmit="validate_email();" >'.
                    213:                     &Apache::lonhtmlcommon::start_pick_box()."\n".
                    214:                     &Apache::lonhtmlcommon::row_title(&mt('E-mail address'),
                    215:                                                      'LC_pick_box_title')."\n".
                    216:                     $emailform."\n".
                    217:                     &Apache::lonhtmlcommon::row_closure(1).
                    218:                     &Apache::lonhtmlcommon::row_title(&mt('Validation'),
                    219:                                                      'LC_pick_box_title')."\n".
                    220:                     $captchaform."\n".'<br /><br />'.
                    221:                     '<input type="submit" name="create_with_email" value="'.
                    222:                     $submit_text.'" />'.
                    223:                     &Apache::lonhtmlcommon::row_closure(1).
                    224:                     &Apache::lonhtmlcommon::end_pick_box().'<br /><br /></form>'.
                    225:                     '</div>';
                    226:     }
                    227:     if ($output eq '') {
                    228:         $output = &mt('Creation of a new user account using either an e-mail address or institutional log-in ID as your username is not permitted in the domain: [_1] ([_2])',$domain,$domdesc);
                    229:     } else {
                    230:         $output .= '<div class="LC_clear_float_footer"></div>';
                    231:     }
                    232:     return $output;
                    233: }
                    234: 
                    235: sub process_email_request {
                    236:     my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
                    237:         $server,$settings) = @_;
                    238:     my $useremail = $env{'form.useremail'};
                    239:     my $output;
                    240:     if ($cancreate ne 'any' && $cancreate ne 'email') {
                    241:         $output = &invalid_state('noemails',$domdesc,
                    242:                                  $contact_name,$contact_email);
                    243:         return $output;
                    244:     } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    245:         $output = &invalid_state('baduseremail',$domdesc,
                    246:                                  $contact_name,$contact_email);
                    247:         return $output;
                    248:     } else {
                    249:         my $uhome = &Apache::lonnet::homeserver($useremail,$domain);
                    250:         if ($uhome ne 'no_host') {
                    251:             $output = &invalid_state('existinguser',$domdesc,
                    252:                                      $contact_name,$contact_email);
                    253:             return $output;
                    254:         } else {
                    255:             my $code = $env{'form.code'};
                    256:             my $md5sum = $env{'form.crypt'};
                    257:             my %captcha_params = &captcha_settings();
                    258:             my $captcha = Authen::Captcha->new(
                    259:                               output_folder => $captcha_params{'output_dir'},
                    260:                               data_folder   => $captcha_params{'db_dir'},
                    261:                              );
                    262:             my $captcha_chk = $captcha->check_code($code,$md5sum);
                    263:             my %captcha_hash = (
                    264:                               0       => 'Code not checked (file error)',
                    265:                               -1      => 'Failed: code expired',
                    266:                               -2      => 'Failed: invalid code (not in database)',
                    267:                               -3      => 'Failed: invalid code (code does not match crypt)',
                    268:                                );
                    269:             if ($captcha_chk != 1) {
                    270:                 $output = &invalid_state('captcha',$domdesc,$contact_name,
                    271:                                          $contact_email,$captcha_hash{$captcha_chk});
                    272:                 return $output;
                    273:             }
                    274:             my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
                    275:             my $uhome=&Apache::lonnet::homeserver($useremail,$domain);
                    276:             if ($uhome eq 'no_host') {
                    277:                 my $checkhash;
                    278:                 my $checks = { 'username' => 1 };
                    279:                 $checkhash->{$useremail.':'.$domain} = { 'newuser' => 1, };
                    280:                 &Apache::loncommon::user_rule_check($checkhash,$checks,
                    281:                       \%alerts,\%rulematch,\%inst_results,\%curr_rules,
                    282:                       \%got_rules);
                    283:                 if (ref($alerts{'useremail'}) eq 'HASH') {
                    284:                     if (ref($alerts{'useremail'}{$domain}) eq 'HASH') {
                    285:                         if ($alerts{'username'}{$domain}{$useremail}) {
                    286:                             $output = &invalid_state('userrules',$domdesc,
                    287:                                                      $contact_name,$contact_email);
                    288:                             return $output;
                    289:                         }
                    290:                     }
                    291:                 }
                    292:                 my $format_msg = 
                    293:                     &guest_format_check($useremail,$domain,$cancreate,
                    294:                                         $settings);
                    295:                 if ($format_msg) {
                    296:                     $output = &invalid_state('userformat',$domdesc,$contact_name,
                    297:                                              $contact_email,$format_msg);
                    298:                     return $output;
                    299:                 }
                    300:             }
                    301:         }
                    302:     }
                    303:     $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
                    304:                           $contact_email);
                    305:     return $output;
                    306: }
                    307: 
                    308: sub send_token {
                    309:     my ($domain,$email,$server,$domdesc,$contact_name,$contact_email) = @_;
                    310:     my $msg = &mt('Thank you for your request to create a new LON-CAPA account.').'<br /><br />';
                    311:     my $now = time;
                    312:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    313:                 'time'       => $now,
                    314:                 'domain'     => $domain,
                    315:                 'username'   => $email);
                    316:     my $token = &Apache::lonnet::tmpput(\%info,$server);
                    317:     if ($token !~ /^error/ && $token ne 'no_such_host') {
                    318:         my $esc_token = &escape($token);
                    319:         my $mailmsg = &mt('A request was submitted on [_1] for creation of a LON-CAPA account in the [_2] domain.',localtime(time),$domdesc).' '.
                    320:              &mt('To complete this process please open a web browser and enter the following ".
                    321:              "URL in the address/location box: ').&Apache::lonnet::absolute_url()."/adm/createaccount?token=$esc_token";
                    322:         my $result = &Apache::resetpw::send_mail($domdesc,$email,$mailmsg,$contact_name,
                    323:                                                  $contact_email);
                    324:         if ($result eq 'ok') {
                    325:             $msg .= &mt('A message has been sent to the e-mail address you provided.').'<br />'.&mt('The message includes the web address for the link you will use to complete the account creation process.').'<br />'.&mt("The link included in the message will be valid for the next [_1]two[_2] hours.",'<b>','</b>');
                    326:         } else {
                    327:             $msg .= &mt('An error occurred when sending a message to the e-mail address you provided. Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
                    328:         }
                    329:     } else {
                    330:         $msg .= &mt('An error occurred creating a token required for the account creation process. Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
                    331:     }
                    332:     return $msg;
                    333: }
                    334: 
                    335: sub process_mailtoken {
                    336:     my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,$include) = @_;
                    337:     my $msg;
                    338:     my %data = &Apache::lonnet::tmpget($token);
                    339:     my $now = time;
                    340:     if (keys(%data) == 0) {
                    341:         $msg = &mt('Sorry, the URL you provided to complete creation of a new LON-CAPA account was invalid.  Either the token included in the URL has been deleted or the URL you provided was invalid. Please submit a <a href="/adm/createaccount">new request</a> for account creation and follow the link to the new URL included in the e-mail that will be sent to you.');
                    342:         return $msg;
                    343:     }
                    344:     if (($data{'time'} =~ /^\d+$/) &&
                    345:         ($data{'domain'} ne '') &&
                    346:         ($data{'username'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
                    347:         my $reqtime = localtime($data{'time'});
                    348:         if ($now - $data{'time'} < 7200) {
                    349:             if ($env{'form.phase'} eq 'createaccount') {
                    350:                 my ($result,$output) = &create_account($r,$domain,$lonhost,$token,
                    351:                                                        $data{'username'},$domdesc);
                    352:                 if ($result eq 'ok') {
                    353:                     $msg = $output; 
                    354:                     my $delete = &Apache::lonnet::tmpdel($token);
                    355:                     my $now = localtime(time);
                    356:                     my $mailmsg = &mt('A LON-CAPA account in the [_1] domain has been created [_2] from IP address: [_3].  If you did not perform this action or authorize it, please contact the [_4] ([_5]).',$domdesc,$now,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n";
                    357:                     my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'},
                    358:                                                                  $mailmsg,$contact_name,
                    359:                                                                  $contact_email);
                    360:                     if ($mailresult eq 'ok') {
                    361:                         $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'});
                    362:                     } else {
                    363:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
                    364:                     }
                    365:                     $msg .= '<br /><br />'.&mt('<a href="/adm/login">Go to the login page</a>.');
                    366:                 } else {
                    367:                     $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account').'<br />'.$output.&mt('Please contact the [_1] - (<a href="mailto:[_2]">[_2]</a>) for assistance.',$contact_name,$contact_email);
                    368:                 }
                    369:             } else {
                    370:                 $r->print(&mt('Please provide user information and a password for your new account.').'<br />'.&mt('Your password, which must contain at least seven characters, will be sent to the LON-CAPA server in an encrypted form.').'<br />');
                    371:                 &print_dataentry_form($r,$domain,$lonhost,$include,$token,$now,$data{'username'});
                    372: 
                    373:             }
                    374:         } else {
                    375:             $msg = &mt('Sorry, the token generated when you requested creation of an account has expired. Please submit a <a href="/adm/createaccount">new request</a>, and follow the link to the web page included in the new e-mail that will be sent to you, to allow you to create the account.');
                    376:         }
                    377:     } else {
                    378:         $msg .= &mt('Sorry, the URL generated when you requested creation of an accountcontained incomplete information. Please submit a <a href="/adm/createaccount">new request</a> for creation of an account, and use the new URL that will be sent to your e-mail address to complete the process.');
                    379:     }
                    380:     return $msg;
                    381: }
                    382: 
                    383: sub print_dataentry_form {
                    384:     my ($r,$domain,$lonhost,$include,$mailtoken,$now,$username) = @_;
                    385:     my ($error,$output);
                    386:     if (open(my $jsh,"<$include/londes.js")) {
                    387:         while(my $line = <$jsh>) {
                    388:             $r->print($line);
                    389:         }
                    390:         close($jsh);
                    391:         $r->print(&javascript_setforms($now)."\n".&javascript_checkpass($now));
                    392:         my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    393:         my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
                    394:         my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
                    395:                                            $lonhost);
                    396:         my @userinfo = ('firstname','middlename','lastname','generation','id',
                    397:                         'permanentemail');
                    398:         my %lt=&Apache::lonlocal::texthash(
                    399:                     'pd'             => "Personal Data",
                    400:                     'firstname'      => "First Name",
                    401:                     'middlename'     => "Middle Name",
                    402:                     'lastname'       => "Last Name",
                    403:                     'generation'     => "Generation",
                    404:                     'permanentemail' => "Permanent e-mail address",
                    405:                     'id'             => "ID/Student Number",
                    406:                     'lg'             => "Login Data"
                    407:         );
                    408:         my %textboxsize = (
                    409:                            firstname      => '15',
                    410:                            middlename     => '15',
                    411:                            lastname       => '15',
                    412:                            generation     => '5',
                    413:                            id             => '15',
                    414:                           );
                    415:         my $genhelp=&Apache::loncommon::help_open_topic('Generation');
                    416:         $output = '<div class="LC_left_float"><h3>'.$lt{'pd'}.'</h3>'.
                    417:                   '<form name="server" method="post" target="_top">'.
                    418:                   &Apache::lonhtmlcommon::start_pick_box();
                    419:         foreach my $item (@userinfo) {
                    420:             my $rowtitle = $lt{$item};
                    421:             if ($item eq 'generation') {
                    422:                 $rowtitle = $genhelp.$rowtitle;
                    423:             }
                    424:             $output .= &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
                    425:             if ($item eq 'permanentemail') {
                    426:                 $output .= $username;
                    427:             } else {
                    428:                 $output .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                    429:             }
                    430:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                    431:         }
                    432:         $output .= &Apache::lonhtmlcommon::end_pick_box();
                    433:         $output .= <<"ENDSERVERFORM";
                    434:    <input type="hidden" name="logtoken" value="$logtoken" />
                    435:    <input type="hidden" name="token" value="$mailtoken" />
                    436:    <input type="hidden" name="serverid" value="$lonhost" />
                    437:    <input type="hidden" name="uname" value="" />
                    438:    <input type="hidden" name="upass" value="" />
                    439:    <input type="hidden" name="phase" value="createaccount" />
                    440:   </form></div>
                    441: ENDSERVERFORM
                    442:         my $upassone = '<input type="password" name="upass'.$now.'" size="10" />';
                    443:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="10" />';
                    444:         my $submit_text = &mt('Create LON-CAPA account');
                    445:         $output .= '<div class="LC_left_float"><h3>'.$lt{'lg'}.'</h3>'."\n".
                    446:                    '<form name="client" method="post" />'."\n".
                    447:                    &Apache::lonhtmlcommon::start_pick_box()."\n".
                    448:                    &Apache::lonhtmlcommon::row_title(&mt('Username'),
                    449:                                                     'LC_pick_box_title')."\n".
                    450:                    $username."\n".
                    451:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    452:                    &Apache::lonhtmlcommon::row_title(&mt('Password'),
                    453:                                                     'LC_pick_box_title')."\n".
                    454:                    $upassone."\n".
                    455:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    456:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password'),
                    457:                                                 'LC_pick_box_title')."\n".
                    458:                    $upasstwo."\n".
                    459:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    460:                    &Apache::lonhtmlcommon::end_pick_box()."\n".
                    461:                    '<input type="hidden" name="uname" value="'.$username.'">'."\n".
                    462:                    '<input type="hidden" name="lextkey" value="'.$lextkey.'">'."\n".
                    463:                    '<input type="hidden" name="uextkey" value="'.$uextkey.'">'."\n".
                    464:                    '</form></div>'."\n".
                    465:                    '<div class="LC_clear_float_footer"><br /><br />'."\n".
                    466:                    '<form name="buttonform">'."\n".
                    467:                    '<input type="button" name="createaccount" value="'.
                    468:                    $submit_text.'" onclick="javascript:checkpass();" /></form></div>';
                    469:         $r->print($output);
                    470:     } else {
                    471:         $error = &mt('Could not load javascript file [_1]','londes.js');
                    472:         $r->print($error);
                    473:     }
                    474:     return;
                    475: }
                    476: 
                    477: sub create_account {
                    478:     my ($r,$domain,$lonhost,$logtoken,$username,$domdesc) = @_;
                    479:     my ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    480:                                                           $env{'form.serverid'}); 
                    481:     # Error messages
                    482:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
                    483:     my $end       = '</span><br /><br />';
                    484:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                    485:                     &mt('Return to previous page').'</a>'.
                    486:                     &Apache::loncommon::end_page();
                    487:     if ($retrieved eq 'ok') {
                    488:         if ($env{'form.cid'} ne '') {
                    489:             my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
                    490:             if ($result eq 'fail') {
                    491:                 $output = $error.&mt('Invalid ID format').$end.
                    492:                           $userchkmsg.$rtnlink;
                    493:                 return ('fail',$output);
                    494:             }
                    495:         }
                    496:     } else {
                    497:         return ('fail',$error.$output.$end.$rtnlink);
                    498:     }
                    499:     # Call modifyuser
                    500:     my $result = 
                    501:         &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
                    502:                                     'internal',$upass,$env{'form.cfirstname'},
                    503:                                     $env{'form.cmiddlename'},$env{'form.clastname'},
                    504:                                     $env{'form.cgeneration'},undef,undef,$username);
                    505:     $output = &mt('Generating user').': '.$result;
                    506:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
                    507:     $output .= '<br />'.&mt('Home server').': '.$uhome.' '.
                    508:               &Apache::lonnet::hostname($uhome).'<br /><br />';
                    509:     return ('ok',$output);
                    510: }
                    511: 
                    512: sub username_validation {
                    513:     my ($username,$domain,$domdesc,$contact_name,$contact_email) = @_;
                    514:     my ($retrieved,$output,$upass);
                    515: 
                    516:     $username= &LONCAPA::clean_username($username);
                    517:     $domain = &LONCAPA::clean_domain($domain);
                    518:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
                    519: 
                    520:     if ($uhome ne 'no_host') {
                    521:         $output = &invalid_state('existinguser',$domdesc,
                    522:                                  $contact_name,$contact_email);
                    523:         return $output;
                    524:     }
                    525:     ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    526:                                                        $env{'form.serverid'});
                    527:     if ($retrieved eq 'ok') {
                    528:         my $primlibserv = &Apache::lonnet::domain($domain,'primary');
                    529:         my $authok;
                    530:         my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    531:         if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
                    532:             my $checkdefauth = 1;
                    533:             $authok = 
                    534:                 &Apache::lonnet::reply("encrypt:auth:$domain:$username:$upass:$checkdefauth",$primlibserv);
                    535:         } else {
                    536:             $authok = 'non_authorized';
                    537:         }
                    538:         if ($authok eq 'authorized') {
                    539:             my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
                    540:             $newuser = 1;
                    541:             my $checkhash;
                    542:             my $checks = { 'username' => 1 };
                    543:             $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser };
                    544:             &Apache::loncommon::user_rule_check($checkhash,$checks,
                    545:                 \%alerts,\%rulematch,\%inst_results,\%curr_rules,\%got_rules);
                    546:             if (ref($alerts{'username'}) eq 'HASH') {
                    547:                 if (ref($alerts{'username'}{$domain}) eq 'HASH') {
                    548:                     if ($alerts{'username'}{$domain}{$username}) {
                    549:                         my $userchkmsg;
                    550:                         if (ref($curr_rules{$domain}) eq 'HASH') {
                    551:                             $userchkmsg =
                    552:                                 &Apache::loncommon::instrule_disallow_msg('username',
                    553:                                                                           $domdesc,1).
                    554:                             &Apache::loncommon::user_rule_formats($domain,
                    555:                                 $domdesc,$curr_rules{$domain}{'username'},
                    556:                                 'username');
                    557:                         }
                    558:                         return $userchkmsg; 
                    559:                     }
                    560:                 }
                    561:             }
                    562:             my $submit_text = &mt('Create LON-CAPA account');
                    563:             $output =
                    564:                 '<form method="post">'.
                    565:                 &Apache::loncreateuser::personal_data_display($username,$domain,1,
                    566:                                        undef,$inst_results{$username.':'.$domain}).
                    567:                 '<br /><br /><input type="hidden" name="uname" value="'.$username.'" />'.
                    568:                 '<input type="hidden" name="phase" value="username_activation" />'.
                    569:                 '<input type="submit" name="newaccount" value="'.
                    570:                 $submit_text.'" /></form>';
                    571:         } else {
                    572:             $output = &mt('Not authenticated').' '.&mt('Please check the username and password'); 
                    573:         }
                    574:     }
                    575:     return $output;
                    576: }
                    577: 
                    578: sub username_activation {
                    579:     my ($username,$domain,$domdesc) = @_;
                    580:     my $output;
                    581:     my $error     = '<span class="LC_error">'.&mt('Error').': ';
                    582:     my $end       = '</span><br /><br />';
                    583:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                    584:                     &mt('Return to previous page').'</a>'.
                    585:                     &Apache::loncommon::end_page();
                    586:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    587:     if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
                    588:         if ($env{'form.cid'} ne '') {
                    589:             my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
                    590:             if ($result eq 'fail') {
                    591:                 $output = $error.&mt('Invalid ID format').$end.
                    592:                           $userchkmsg.$rtnlink;
                    593:                 return ('fail',$output);
                    594:             }
                    595:         }
                    596:         # Call modifyuser
                    597:         my $result =
                    598:             &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
                    599:                           $domdefaults{'auth_def'},
                    600:                           $domdefaults{'auth_arg_def'},$env{'form.cfirstname'},
                    601:                           $env{'form.cmiddlename'},$env{'form.clastname'},
                    602:                           $env{'form.cgeneration'},undef,undef,
                    603:                           $env{'form.cpermanentemail'});
                    604:         $output = &mt('Generating user').': '.$result;
                    605:         my $uhome = &Apache::lonnet::homeserver($username,$domain);
                    606:         $output .= '<br />'.&mt('Home server').': '.$uhome.' '.
                    607:                    &Apache::lonnet::hostname($uhome).'<br /><br />';
                    608:         return ('ok',$output);
                    609:     } else {
                    610:         $output = &mt("User account creation is not available for the current default authentication type.\n");
                    611:         return('fail',$output);
                    612:     }
                    613: }
                    614: 
                    615: sub check_id {
                    616:     my ($username,$domain,$domdesc) = @_;
                    617:     # Check ID format
                    618:     my (%alerts,%rulematch,%inst_results,%curr_rules,%checkhash);
                    619:     my %checks = ('id' => 1);
                    620:     %{$checkhash{$username.':'.$domain}} = (
                    621:                                             'newuser' => 1,
                    622:                                             'id' => $env{'form.cid'},
                    623:                                            );
                    624:     &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                    625:                                         \%rulematch,\%inst_results,\%curr_rules);
                    626:     if (ref($alerts{'id'}) eq 'HASH') {
                    627:         if (ref($alerts{'id'}{$domain}) eq 'HASH') {
                    628:             if ($alerts{'id'}{$domain}{$env{'form.cid'}}) {
                    629:                 my $userchkmsg;
                    630:                 if (ref($curr_rules{$domain}) eq 'HASH') {
                    631:                     $userchkmsg  =
                    632:                         &Apache::loncommon::instrule_disallow_msg('id',
                    633:                                                            $domdesc,1).
                    634:                         &Apache::loncommon::user_rule_formats($domain,
                    635:                               $domdesc,$curr_rules{$domain}{'id'},'id');
                    636:                 }
                    637:                 return ('fail',$userchkmsg);
                    638:             }
                    639:         }
                    640:     }
                    641:     return; 
                    642: }
                    643: 
                    644: sub invalid_state {
                    645:     my ($error,$domdesc,$contact_name,$contact_email,$msgtext) = @_;
                    646:     my $msg;
                    647:     if ($error eq 'baduseremail') {
                    648:         $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    649:     } elsif ($error eq 'existinguser') {
                    650:         $msg = &mt('The e-mail address you provided is already in use as a username in this LON-CAPA domain.');
                    651:     } elsif ($error eq 'userrules') {
                    652:         $msg = &mt('Username rules for this LON-CAPA domain do not allow the e-mail address you provided to be used as a username.');
                    653:     } elsif ($error eq 'userformat') {
                    654:         $msg = &mt('The e-mail address you provided may not be used as a username in this LON-CAPA domain.');
                    655:     } elsif ($error eq 'captcha') {
                    656:         $msg = &mt('Validation of the code your entered failed.');
                    657:     } elsif ($error eq 'noemails') {
                    658:         $msg = &mt('Creation of a new user account using an e-mail address as username is not permitted in this LON-CAPA domain.');
                    659:     }
                    660:     if ($msgtext) {
                    661:         $msg .= '<br />'.$msgtext;
                    662:     }
                    663:     if ($contact_email ne '') {
                    664:         my $escuri = &HTML::Entities::encode('/adm/createaccount','&<>"');
                    665:         $msg .= '<br />'.&mt(' You may wish to contact the <a href="/adm/helpdesk?origurl=[_1]">LON-CAPA helpdesk</a> for the [_2] domain.',$escuri,$domdesc);
                    666:     } else {
                    667:         $msg .= '<br />'.&mt(' You may wish to send an e-mail to the server administrator: [_1] for the [_2] domain.',$Apache::lonnet::perlvar{'AdminEmail'},$domdesc);
                    668:     }
                    669:     return $msg;
                    670: }
                    671: 
                    672: sub create_captcha {
                    673:     my ($output_dir,$db_dir) = @_;
                    674:     my %captcha_params = &captcha_settings();
                    675:     my $captcha = Authen::Captcha->new(
                    676:                                        output_folder => $captcha_params{'output_dir'},
                    677:                                        data_folder   => $captcha_params{'db_dir'},
                    678:                                       );
                    679:     my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
                    680:     my $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
                    681:                  &mt('Type in the letters/numbers shown below').'&nbsp;'.
                    682:                  '<input type="text" size="5" name="code" value="" /><br />'.
                    683:                  '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png">';
                    684:     return $output;
                    685: }
                    686: 
                    687: sub captcha_settings {
                    688:     my %captcha_params = ( 
                    689:                            output_dir     => "/home/httpd/html/captcha",
                    690:                            www_output_dir => "/captcha",
                    691:                            db_dir         => "/home/www/captchadb",
                    692:                            numchars       => '5',
                    693:                          );
                    694:     return %captcha_params;
                    695: }
                    696: 
                    697: sub getkeys {
                    698:     my ($lkey,$ukey) = @_;
                    699:     my $lextkey=hex($lkey);
                    700:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
                    701: 
                    702:     my $uextkey=hex($ukey);
                    703:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
                    704:     return ($lextkey,$uextkey);
                    705: }
                    706: 
                    707: sub serverform {
                    708:     my ($logtoken,$lonhost,$mailtoken) = @_;
                    709:     my $output .= <<ENDSERVERFORM;
                    710:   <form name="server" method="post" target="_top">
                    711:    <input type="hidden" name="logtoken" value="$logtoken" />
                    712:    <input type="hidden" name="token" value="$mailtoken" />
                    713:    <input type="hidden" name="serverid" value="$lonhost" />
                    714:    <input type="hidden" name="uname" value="" />
                    715:    <input type="hidden" name="upass" value="" />
                    716:    <input type="hidden" name="phase" value="username_validation" />
                    717:   </form>
                    718: ENDSERVERFORM
                    719:     return $output;
                    720: }
                    721: 
                    722: sub process_credentials {
                    723:     my ($logtoken,$lonhost) = @_;
                    724:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
                    725:     my ($retrieved,$output,$upass);
                    726:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
                    727:         $output = &mt('Information needed to retrieve your log-in information is missing, inaccessible or expired.').'<br />'.&mt('You may need to reload the previous page to obtain a new token.');
                    728:         return ($retrieved,$output,$upass); 
                    729:     } else {
                    730:         my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$lonhost);
                    731:         if ($reply eq 'ok') {
                    732:             $retrieved = 'ok';
                    733:         } else {
                    734:             $output = &mt('Session could not be opened.');
                    735:         }
                    736:     }
                    737:     my ($key,$caller)=split(/&/,$tmpinfo);
                    738:     if ($caller eq 'createaccount') {
                    739:         $upass = &Apache::lonpreferences::des_decrypt($key,$env{'form.upass'});
                    740:     } else {
                    741:         $output = &mt('Unable to retrieve your log-in information - unexpected context');
                    742:     }
                    743:     return ($retrieved,$output,$upass);
                    744: }
                    745: 
                    746: sub guest_format_check {
                    747:     my ($useremail,$domain,$cancreate,$settings) = @_;
                    748:     my ($login,$format_match,$format_msg,@user_rules);
                    749:     if (ref($settings) eq 'HASH') {
                    750:         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
                    751:             push(@user_rules,@{$settings->{'email_rule'}});
                    752:         }
                    753:     }
                    754:     if (@user_rules > 0) {
                    755:         my %rule_check = 
                    756:             &Apache::lonnet::inst_rulecheck($domain,$useremail,undef,
1.2     ! raeburn   757:                                             'selfcreate',\@user_rules);
1.1       raeburn   758:         if (keys(%rule_check) > 0) {
                    759:             foreach my $item (keys(%rule_check)) {
                    760:                 if ($rule_check{$item}) {
                    761:                     $format_match = 1;   
                    762:                     last;
                    763:                 }
                    764:             }
                    765:         }
                    766:     }
                    767:     if ($format_match) {
                    768:         ($login) = ($useremail =~ /^([^\@]+)\@/);
                    769:         $format_msg = '<br />'.&mt("Your e-mail address uses the same internet domain as your institution's LON-CAPA service.").'<br />'.&mt('Creation of a LON-CAPA account with this type of e-mail address as username is not permitted.').'<br />';
                    770:         if ($cancreate eq 'any' || $cancreate eq 'login') {
                    771:             $format_msg .= &mt('You should request creation of a LON-CAPA account for a Log-in ID of "[_1]" at your institution instead.',$login).'<br />'; 
                    772:         }
                    773:     }
                    774:     return $format_msg;
                    775: }
                    776: 
                    777: 1;

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>