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

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.10    ! raeburn     6: # $Id: createaccount.pm,v 1.9 2008/07/04 19:32:56 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;
1.3       raeburn    39: use Apache::lonauth;
1.1       raeburn    40: use Apache::resetpw;
                     41: use Authen::Captcha;
                     42: use DynaLoader; # for Crypt::DES version
                     43: use Crypt::DES;
1.3       raeburn    44: use LONCAPA qw(:DEFAULT :match);
1.8       raeburn    45: use HTML::Entities;
1.1       raeburn    46: 
                     47: sub handler {
                     48:     my $r = shift;
                     49:     &Apache::loncommon::content_type($r,'text/html');
                     50:     $r->send_http_header;
                     51:     if ($r->header_only) {
                     52:         return OK;
                     53:     }
1.5       raeburn    54:     
                     55:     my $domain;
1.3       raeburn    56: 
1.5       raeburn    57:     my $sso_username = $r->subprocess_env->get('REDIRECT_SSOUserUnknown');
                     58:     my $sso_domain = $r->subprocess_env->get('REDIRECT_SSOUserDomain');
                     59: 
                     60:     if ($sso_username ne '' && $sso_domain ne '') {
                     61:         $domain = $sso_domain; 
                     62:     } else { 
                     63:         $domain = &Apache::lonnet::default_login_domain();
                     64:     }
1.1       raeburn    65:     my $domdesc = &Apache::lonnet::domain($domain,'description');
                     66:     my $contact_name = &mt('LON-CAPA helpdesk');
                     67:     my $contact_email =  $r->dir_config('lonSupportEMail');
                     68:     my $lonhost = $r->dir_config('lonHostID');
                     69:     my $include = $r->dir_config('lonIncludes');
1.4       raeburn    70:     my $start_page;
1.3       raeburn    71: 
                     72:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token','courseid']);
1.1       raeburn    73:     &Apache::lonacc::get_posted_cgi($r);
                     74:     &Apache::lonlocal::get_language_handle($r);
1.3       raeburn    75: 
                     76:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                     77:     if ($handle ne '') {
1.4       raeburn    78:         $start_page =
1.3       raeburn    79:             &Apache::loncommon::start_page('Already logged in');
                     80:         my $end_page =
                     81:             &Apache::loncommon::end_page();
                     82:         $r->print($start_page."\n".'<h2>'.&mt('You are already logged in').'</h2>'.
                     83:                   '<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]logout[_4].','<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>').
1.6       bisitz     84:                   '</p><p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'.$end_page);
1.3       raeburn    85:        return OK;
                     86:     }
1.4       raeburn    87:     $start_page =
                     88:         &Apache::loncommon::start_page('Create a user account in LON-CAPA','',
                     89:                                        {'no_inline_link'   => 1,});
                     90:     if ($env{'form.phase'} eq 'username_activation') {
                     91:         if ($env{'form.udom'} ne '') {
                     92:             $domain = $env{'form.udom'};
                     93:         }
                     94:     }
1.5       raeburn    95:     my @cancreate;
1.1       raeburn    96:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
                     97:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
1.3       raeburn    98:         if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
1.5       raeburn    99:             if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                    100:                 @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
                    101:             } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
                    102:                      ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
                    103:                 @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
1.1       raeburn   104:             }
                    105:         }
                    106:     }
1.3       raeburn   107: 
1.5       raeburn   108:     if (@cancreate == 0) {
1.3       raeburn   109:         &print_header($r,$start_page);
1.7       bisitz    110:         my $output = &mt('Creation of a new user account using an e-mail address or an institutional log-in ID as username is not permitted in the domain: [_1] ([_2]).',$domain,$domdesc);
1.3       raeburn   111:         $r->print($output);
                    112:         $r->print(&Apache::loncommon::end_page());
                    113:         return OK;
                    114:     }
                    115: 
1.5       raeburn   116:     my $courseid;
                    117:     if (defined($env{'form.courseid'})) {
                    118:         $courseid = &validate_course($env{'form.courseid'});
                    119:     }
                    120: 
                    121:     if ($sso_username ne '') {
                    122:         &print_header($r,$start_page);
                    123:         my ($output,$msg);
                    124:         if (grep(/^sso$/,@cancreate)) {
                    125:             $msg = &mt("Although your username and password were authenticated by your institution's Single Sign On system, you do not currently have a LON-CAPA account in this domain.");
1.8       raeburn   126:             ($output, my $checkfail) = &username_check($sso_username,$domain,
                    127:                                                        $domdesc,$courseid,
                    128:                                                        $lonhost,$contact_email); 
                    129:             if ($checkfail eq 'username') {
1.5       raeburn   130:                 $msg .= &mt('A LON-CAPA account may not be created with the username you use.');
1.8       raeburn   131:             } elsif ($checkfail eq 'authtoken') {
                    132:                 $msg .= &mt('Error creating token.');
1.5       raeburn   133:             } else {
                    134:                 $msg .= &mt('To create one, use the table below to provide information about yourself (if appropriate), then click the "Create LON-CAPA account" button.');
                    135:             }
                    136:         } else {
                    137:             $msg = &mt("Although your username and password were authenticated by your institution's Single Sign On system, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.");
                    138:         }
                    139:         $r->print('<h4>'.$msg.'</h4>'.$output);
                    140:         $r->print(&Apache::loncommon::end_page());
                    141:         return OK;
                    142:     }
                    143: 
1.4       raeburn   144:     my ($output,$nostart,$noend);
1.3       raeburn   145:     my $token = $env{'form.token'};
                    146:     if ($token) {
                    147:         ($output,$nostart,$noend) = 
                    148:             &process_mailtoken($r,$token,$contact_name,$contact_email,$domain,
1.4       raeburn   149:                                $domdesc,$lonhost,$include,$start_page);
1.3       raeburn   150:         if ($nostart) {
                    151:             if ($noend) {
                    152:                 return OK;
                    153:             } else {
                    154:                 $r->print($output);
                    155:                 $r->print(&Apache::loncommon::end_page());
                    156:                 return OK;
                    157:             }
                    158:         } else {
                    159:             &print_header($r,$start_page);
                    160:             $r->print($output);
                    161:             $r->print(&Apache::loncommon::end_page());
                    162:             return OK;
                    163:         }
                    164:     }
                    165: 
                    166:     if ($env{'form.phase'} eq 'username_activation') {
                    167:         (my $result,$output,$nostart) = 
                    168:             &username_activation($r,$env{'form.uname'},$domain,$domdesc,
                    169:                                  $lonhost,$courseid);
                    170:         if ($result eq 'ok') {
                    171:             if ($nostart) {
                    172:                 return OK;
                    173:             }
                    174:         }
                    175:         &print_header($r,$start_page);
                    176:         $r->print($output);
                    177:         $r->print(&Apache::loncommon::end_page());
                    178:         return OK;
                    179:     }
                    180: 
                    181:     &print_header($r,$start_page);
                    182:     if ($env{'form.create_with_email'}) {
1.1       raeburn   183:         $output = &process_email_request($env{'form.useremail'},$domain,$domdesc,
1.5       raeburn   184:                                          $contact_name,$contact_email,\@cancreate,
1.3       raeburn   185:                                          $lonhost,$domconfig{'usercreation'},
                    186:                                          $courseid);
1.1       raeburn   187:     } elsif ($env{'form.phase'} eq 'username_validation') {
                    188:         $output = &username_validation($env{'form.uname'},$domain,$domdesc,
1.8       raeburn   189:                                        $contact_name,$contact_email,$courseid,
                    190:                                        $lonhost);
1.3       raeburn   191:     } elsif (!$token) {
1.1       raeburn   192:         my $now=time;
1.5       raeburn   193:         if (grep(/^login$/,@cancreate)) {
1.1       raeburn   194:             my $jsh=Apache::File->new($include."/londes.js");
                    195:             $r->print(<$jsh>);
                    196:             $r->print(&javascript_setforms($now));
                    197:         }
1.5       raeburn   198:         $output = &print_username_form($domain,$domdesc,\@cancreate,$now,$lonhost,
1.3       raeburn   199:                                        $courseid); 
1.1       raeburn   200:     }
                    201:     $r->print($output);
                    202:     $r->print(&Apache::loncommon::end_page());
                    203:     return OK;
                    204: }
                    205: 
1.3       raeburn   206: sub print_header {
                    207:     my ($r,$start_page) = @_;
                    208:     $r->print($start_page);
                    209:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    210:     &Apache::lonhtmlcommon::add_breadcrumb
                    211:     ({href=>"/adm/createuser",
                    212:       text=>"New username"});
                    213:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account'));
                    214:     return;
                    215: }
                    216: 
                    217: sub validate_course {
                    218:     my ($courseid) = @_;
                    219:     my ($cdom,$cnum) = ($courseid =~ /^($match_domain)_($match_courseid)$/);
                    220:     if (($cdom ne '') && ($cnum ne '')) {
                    221:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
                    222:             return ($courseid);
                    223:         }
                    224:     }
                    225:     return;
                    226: }
                    227: 
1.1       raeburn   228: sub javascript_setforms {
                    229:     my ($now) =  @_;
                    230:     my $js = <<ENDSCRIPT;
                    231:  <script language="JavaScript">
                    232:     function send() {
                    233:         this.document.server.elements.uname.value = this.document.client.elements.uname.value;
                    234:         uextkey=this.document.client.elements.uextkey.value;
                    235:         lextkey=this.document.client.elements.lextkey.value;
                    236:         initkeys();
                    237: 
                    238:         this.document.server.elements.upass.value
                    239:             = crypted(this.document.client.elements.upass$now.value);
                    240: 
                    241:         this.document.client.elements.uname.value='';
                    242:         this.document.client.elements.upass$now.value='';
                    243: 
                    244:         this.document.server.submit();
                    245:         return false;
                    246:     }
                    247:  </script>
                    248: ENDSCRIPT
                    249:     return $js;
                    250: }
                    251: 
                    252: sub javascript_checkpass {
                    253:     my ($now) = @_;
1.7       bisitz    254:     my $nopass = &mt('You must enter a password.');
1.1       raeburn   255:     my $mismatchpass = &mt('The passwords you entered did not match.').'\\n'.
                    256:                        &mt('Please try again.'); 
                    257:     my $js = <<"ENDSCRIPT";
                    258: <script type="text/javascript" language="JavaScript">
                    259:     function checkpass() {
                    260:         var upass = this.document.client.elements.upass$now.value;
                    261:         var upasscheck = this.document.client.elements.upasscheck$now.value;
                    262:         if (upass == '') {
                    263:             alert("$nopass");
                    264:             return;
                    265:         }
                    266:         if (upass == upasscheck) {
                    267:             this.document.client.elements.upasscheck$now.value='';
                    268:             send();
                    269:             return;
                    270:         } else {
                    271:             alert("$mismatchpass");
                    272:             return;
                    273:         } 
                    274:     }
                    275: </script>
                    276: ENDSCRIPT
                    277:     return $js;
                    278: }
                    279: 
                    280: sub print_username_form {
1.3       raeburn   281:     my ($domain,$domdesc,$cancreate,$now,$lonhost,$courseid) = @_;
1.1       raeburn   282:     my %lt = &Apache::lonlocal::texthash(
                    283:                                          unam => 'username',
                    284:                                          udom => 'domain',
                    285:                                          uemail => 'Email address in LON-CAPA',
                    286:                                          proc => 'Proceed');
                    287:     my $output;
1.5       raeburn   288:     if (ref($cancreate) eq 'ARRAY') {
                    289:         if (grep(/^login$/,@{$cancreate})) {
                    290:             my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    291:             if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
                    292:                 $output = '<div class="LC_left_float"><h3>'.&mt('Create account with a username provided by your institution').'</h3>';
1.10    ! raeburn   293:                 $output .= &mt('If you already have a log-in ID at your institution,[_1] you may be able to use it for LON-CAPA.','<br />').'<br /><br />'.&mt('Type in your log-in ID and password to find out.').'<br /><br />';
1.5       raeburn   294:                 my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    295:                 my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
                    296:                 my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
                    297:                                                    $lonhost);
                    298:                 $output .= &serverform($logtoken,$lonhost,undef,$courseid);
                    299:                 my $unameform = '<input type="text" name="uname" size="10" value="" />';
                    300:                 my $upassform = '<input type="password" name="upass'.$now.'" size="10" />';
                    301:                 my $submit_text = &mt('Create LON-CAPA account');
                    302:                 $output .= '<form name="client" method="post" action="/adm/createaccount">'."\n". 
                    303:                            &Apache::lonhtmlcommon::start_pick_box()."\n".
                    304:                            &Apache::lonhtmlcommon::row_title(&mt('Log-in ID'),
1.1       raeburn   305:                                                         'LC_pick_box_title')."\n".
1.5       raeburn   306:                            $unameform."\n".
                    307:                            &Apache::lonhtmlcommon::row_closure(1)."\n".
                    308:                            &Apache::lonhtmlcommon::row_title(&mt('Password'),
1.1       raeburn   309:                                                         'LC_pick_box_title')."\n".
1.5       raeburn   310:                            $upassform."\n".'<br /><br />'."\n".
                    311:                            '<input type="button" name="username_validation" value="'.
                    312:                            $submit_text.'" onclick="javascript:send()" />'."\n". 
                    313:                            &Apache::lonhtmlcommon::row_closure(1)."\n".
                    314:                            &Apache::lonhtmlcommon::end_pick_box().'<br /><br />'."\n".
                    315:                            '<input type="hidden" name="lextkey" value="'.$lextkey.'">'."\n".
                    316:                            '<input type="hidden" name="uextkey" value="'.$uextkey.'">'."\n".
                    317:                            '</form></div>';
                    318:             }
                    319:         }
                    320:         if (grep(/^email$/,@{$cancreate})) {
                    321:             $output .= '<div class="LC_left_float"><h3>'.&mt('Create account with an e-mail address as your username').'</h3>';
                    322:             if (grep(/^login$/,@{$cancreate})) {
1.9       raeburn   323:                 $output .= &mt('Provide your e-mail address to request a LON-CAPA account,[_1] if you do not have a log-in ID at your institution.','<br />').'<br /><br />';
1.5       raeburn   324:             } else {
                    325:                 $output .= '<br />';
                    326:             }
                    327:             my $emailform = '<input type="text" name="useremail" size="25" value="" />';
                    328:             my $captchaform = &create_captcha();
                    329:             my $submit_text = &mt('Request LON-CAPA account');
                    330:             $output .=  '<form name="createaccount" method="post" onsubmit="validate_email();" action="/adm/createaccount">'.
                    331:                         &Apache::lonhtmlcommon::start_pick_box()."\n".
                    332:                         &Apache::lonhtmlcommon::row_title(&mt('E-mail address'),
                    333:                                                          'LC_pick_box_title')."\n".
                    334:                         $emailform."\n".
                    335:                         &Apache::lonhtmlcommon::row_closure(1).
                    336:                         &Apache::lonhtmlcommon::row_title(&mt('Validation'),
                    337:                                                          'LC_pick_box_title')."\n".
                    338:                         $captchaform."\n".'<br /><br />';
                    339:             if ($courseid ne '') {
                    340:                 $output .= '<input type="hidden" name="courseid" value="'.$courseid.'"/>'."\n"; 
                    341:             }
                    342:             $output .= '<input type="submit" name="create_with_email" value="'. 
                    343:                         $submit_text.'" />'.
                    344:                         &Apache::lonhtmlcommon::row_closure(1).
                    345:                         &Apache::lonhtmlcommon::end_pick_box().'<br /><br /></form>'.
                    346:                         '</div>';
1.3       raeburn   347:         }
1.1       raeburn   348:     }
                    349:     if ($output eq '') {
1.7       bisitz    350:         $output = &mt('Creation of a new user account using an e-mail address or an institutional log-in ID as your username is not permitted in the domain: [_1] ([_2])',$domain,$domdesc);
1.1       raeburn   351:     } else {
                    352:         $output .= '<div class="LC_clear_float_footer"></div>';
                    353:     }
                    354:     return $output;
                    355: }
                    356: 
                    357: sub process_email_request {
                    358:     my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
1.3       raeburn   359:         $server,$settings,$courseid) = @_;
1.1       raeburn   360:     my $useremail = $env{'form.useremail'};
                    361:     my $output;
1.5       raeburn   362:     if (ref($cancreate) eq 'ARRAY') {
                    363:         if (!grep(/^email$/,@{$cancreate})) {
                    364:             $output = &invalid_state('noemails',$domdesc,
                    365:                                      $contact_name,$contact_email);
                    366:             return $output;
                    367:         } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    368:             $output = &invalid_state('baduseremail',$domdesc,
1.1       raeburn   369:                                      $contact_name,$contact_email);
                    370:             return $output;
                    371:         } else {
1.5       raeburn   372:             my $uhome = &Apache::lonnet::homeserver($useremail,$domain);
                    373:             if ($uhome ne 'no_host') {
                    374:                 $output = &invalid_state('existinguser',$domdesc,
                    375:                                          $contact_name,$contact_email);
1.1       raeburn   376:                 return $output;
1.5       raeburn   377:             } else {
                    378:                 my $code = $env{'form.code'};
                    379:                 my $md5sum = $env{'form.crypt'};
                    380:                 my %captcha_params = &captcha_settings();
                    381:                 my $captcha = Authen::Captcha->new(
                    382:                                   output_folder => $captcha_params{'output_dir'},
                    383:                                   data_folder   => $captcha_params{'db_dir'},
                    384:                                  );
                    385:                 my $captcha_chk = $captcha->check_code($code,$md5sum);
                    386:                 my %captcha_hash = (
                    387:                                   0       => 'Code not checked (file error)',
                    388:                                   -1      => 'Failed: code expired',
                    389:                                   -2      => 'Failed: invalid code (not in database)',
                    390:                                   -3      => 'Failed: invalid code (code does not match crypt)',
                    391:                                    );
                    392:                 if ($captcha_chk != 1) {
                    393:                     $output = &invalid_state('captcha',$domdesc,$contact_name,
                    394:                                              $contact_email,$captcha_hash{$captcha_chk});
                    395:                     return $output;
                    396:                 }
                    397:                 my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
                    398:                 my $uhome=&Apache::lonnet::homeserver($useremail,$domain);
                    399:                 if ($uhome eq 'no_host') {
                    400:                     my $checkhash;
                    401:                     my $checks = { 'username' => 1 };
                    402:                     $checkhash->{$useremail.':'.$domain} = { 'newuser' => 1, };
                    403:                     &Apache::loncommon::user_rule_check($checkhash,$checks,
                    404:                           \%alerts,\%rulematch,\%inst_results,\%curr_rules,
                    405:                           \%got_rules);
                    406:                     if (ref($alerts{'useremail'}) eq 'HASH') {
                    407:                         if (ref($alerts{'useremail'}{$domain}) eq 'HASH') {
                    408:                             if ($alerts{'username'}{$domain}{$useremail}) {
                    409:                                 $output = &invalid_state('userrules',$domdesc,
                    410:                                                          $contact_name,$contact_email);
                    411:                                 return $output;
                    412:                             }
1.1       raeburn   413:                         }
                    414:                     }
1.5       raeburn   415:                     my $format_msg = 
                    416:                         &guest_format_check($useremail,$domain,$cancreate,
                    417:                                             $settings);
                    418:                     if ($format_msg) {
                    419:                         $output = &invalid_state('userformat',$domdesc,$contact_name,
                    420:                                                  $contact_email,$format_msg);
                    421:                         return $output;
                    422:                     }
1.1       raeburn   423:                 }
                    424:             }
                    425:         }
1.5       raeburn   426:         $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
                    427:                           $contact_email,$courseid);
1.1       raeburn   428:     }
                    429:     return $output;
                    430: }
                    431: 
                    432: sub send_token {
1.3       raeburn   433:     my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid) = @_;
1.1       raeburn   434:     my $msg = &mt('Thank you for your request to create a new LON-CAPA account.').'<br /><br />';
                    435:     my $now = time;
                    436:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    437:                 'time'       => $now,
                    438:                 'domain'     => $domain,
1.3       raeburn   439:                 'username'   => $email,
                    440:                 'courseid'   => $courseid);
1.1       raeburn   441:     my $token = &Apache::lonnet::tmpput(\%info,$server);
                    442:     if ($token !~ /^error/ && $token ne 'no_such_host') {
                    443:         my $esc_token = &escape($token);
                    444:         my $mailmsg = &mt('A request was submitted on [_1] for creation of a LON-CAPA account in the [_2] domain.',localtime(time),$domdesc).' '.
1.7       bisitz    445:              &mt('To complete this process please open a web browser and enter the following'
                    446:                 .' URL in the address/location box: [_1]'
1.9       raeburn   447:                 ,&Apache::lonnet::absolute_url().'/adm/createaccount?token='.$esc_token);
1.1       raeburn   448:         my $result = &Apache::resetpw::send_mail($domdesc,$email,$mailmsg,$contact_name,
                    449:                                                  $contact_email);
                    450:         if ($result eq 'ok') {
                    451:             $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>');
                    452:         } else {
1.7       bisitz    453:             $msg .= &mt('An error occurred when sending a message to the e-mail address you provided.')
                    454:                    .' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   455:         }
                    456:     } else {
1.7       bisitz    457:         $msg .= &mt('An error occurred creating a token required for the account creation process.')
                    458:                .' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   459:     }
                    460:     return $msg;
                    461: }
                    462: 
                    463: sub process_mailtoken {
1.3       raeburn   464:     my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,
                    465:         $include,$start_page) = @_;
                    466:     my ($msg,$nostart,$noend);
1.1       raeburn   467:     my %data = &Apache::lonnet::tmpget($token);
                    468:     my $now = time;
                    469:     if (keys(%data) == 0) {
1.9       raeburn   470:         $msg = &mt('Sorry, the URL you provided to complete creation of a new LON-CAPA account was invalid.')
                    471:                .' '.&mt('Either the token included in the URL has been deleted or the URL you provided was invalid.')
                    472:                .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link page included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.1       raeburn   473:         return $msg;
                    474:     }
                    475:     if (($data{'time'} =~ /^\d+$/) &&
                    476:         ($data{'domain'} ne '') &&
                    477:         ($data{'username'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
                    478:         if ($now - $data{'time'} < 7200) {
                    479:             if ($env{'form.phase'} eq 'createaccount') {
1.3       raeburn   480:                 my ($result,$output) = &create_account($r,$domain,$lonhost,
1.1       raeburn   481:                                                        $data{'username'},$domdesc);
                    482:                 if ($result eq 'ok') {
                    483:                     $msg = $output; 
1.4       raeburn   484:                     my $shownow = localtime($now);
                    485:                     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,$shownow,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n";
1.1       raeburn   486:                     my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'},
                    487:                                                                  $mailmsg,$contact_name,
                    488:                                                                  $contact_email);
                    489:                     if ($mailresult eq 'ok') {
                    490:                         $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'});
                    491:                     } else {
                    492:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
                    493:                     }
1.3       raeburn   494:                     my %form = &start_session($r,$data{'username'},$domain, 
                    495:                                               $lonhost,$data{'courseid'},
                    496:                                               $token);
                    497:                     $nostart = 1;
                    498:                     $noend = 1;
1.1       raeburn   499:                 } else {
1.7       bisitz    500:                     $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.')
                    501:                            .'<br />'.$output
                    502: #                           .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
                    503:                            .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   504:                 }
1.3       raeburn   505:                 my $delete = &Apache::lonnet::tmpdel($token);
1.1       raeburn   506:             } else {
1.3       raeburn   507:                 $msg .= &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 />';
                    508:                 $msg .= &print_dataentry_form($r,$domain,$lonhost,$include,$token,$now,$data{'username'},$start_page);
                    509:                 $nostart = 1;
1.1       raeburn   510:             }
                    511:         } else {
1.7       bisitz    512:             $msg = &mt('Sorry, the token generated when you requested creation of an account has expired.')
                    513:                   .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.4       raeburn   514:             }
1.1       raeburn   515:     } else {
1.7       bisitz    516:         $msg .= &mt('Sorry, the URL generated when you requested creation of an account contained incomplete information.')
                    517:                .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.1       raeburn   518:     }
1.3       raeburn   519:     return ($msg,$nostart,$noend);
                    520: }
                    521: 
                    522: sub start_session {
                    523:     my ($r,$username,$domain,$lonhost,$courseid,$token) = @_;
                    524:     my %form = (
                    525:                 uname => $username,
                    526:                 udom  => $domain,
                    527:                );
                    528:     my $firsturl = '/adm/roles';
                    529:     if (defined($courseid)) {
                    530:         $courseid = &validate_course($courseid);
                    531:         if ($courseid ne '') {
                    532:             $form{'courseid'} = $courseid;
                    533:             $firsturl = '/adm/selfenroll?cid='.$courseid;
                    534:         }
                    535:     }
                    536:     if ($r->dir_config('lonBalancer') eq 'yes') {
                    537:         &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
                    538:                                   $lonhost,'noredirect',undef,\%form);
                    539:         my $delete = &Apache::lonnet::tmpdel($token);
                    540:         $r->internal_redirect('/adm/switchserver');
                    541:     } else {
                    542:         &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
                    543:                                   $lonhost,$firsturl,undef,\%form);
                    544:     }
                    545:     return %form;
1.1       raeburn   546: }
                    547: 
1.3       raeburn   548: 
1.1       raeburn   549: sub print_dataentry_form {
1.3       raeburn   550:     my ($r,$domain,$lonhost,$include,$mailtoken,$now,$username,$start_page) = @_;
1.1       raeburn   551:     my ($error,$output);
1.3       raeburn   552:     &print_header($r,$start_page);
1.1       raeburn   553:     if (open(my $jsh,"<$include/londes.js")) {
                    554:         while(my $line = <$jsh>) {
                    555:             $r->print($line);
                    556:         }
                    557:         close($jsh);
1.3       raeburn   558:         $output .= &javascript_setforms($now)."\n".&javascript_checkpass($now);
1.1       raeburn   559:         my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    560:         my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
                    561:         my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
                    562:                                            $lonhost);
                    563:         my @userinfo = ('firstname','middlename','lastname','generation','id',
                    564:                         'permanentemail');
                    565:         my %lt=&Apache::lonlocal::texthash(
                    566:                     'pd'             => "Personal Data",
                    567:                     'firstname'      => "First Name",
                    568:                     'middlename'     => "Middle Name",
                    569:                     'lastname'       => "Last Name",
                    570:                     'generation'     => "Generation",
                    571:                     'permanentemail' => "Permanent e-mail address",
                    572:                     'id'             => "ID/Student Number",
                    573:                     'lg'             => "Login Data"
                    574:         );
                    575:         my %textboxsize = (
                    576:                            firstname      => '15',
                    577:                            middlename     => '15',
                    578:                            lastname       => '15',
                    579:                            generation     => '5',
                    580:                            id             => '15',
                    581:                           );
                    582:         my $genhelp=&Apache::loncommon::help_open_topic('Generation');
1.3       raeburn   583:         $output .= '<div class="LC_left_float"><h3>'.$lt{'pd'}.'</h3>'.
                    584:                   '<form name="server" method="post" target="_top" action="/adm/createaccount">'.
1.1       raeburn   585:                   &Apache::lonhtmlcommon::start_pick_box();
                    586:         foreach my $item (@userinfo) {
                    587:             my $rowtitle = $lt{$item};
                    588:             if ($item eq 'generation') {
                    589:                 $rowtitle = $genhelp.$rowtitle;
                    590:             }
                    591:             $output .= &Apache::lonhtmlcommon::row_title($rowtitle,undef,'LC_oddrow_value')."\n";
                    592:             if ($item eq 'permanentemail') {
                    593:                 $output .= $username;
                    594:             } else {
                    595:                 $output .= '<input type="text" name="c'.$item.'" size="'.$textboxsize{$item}.'" value="" />';
                    596:             }
                    597:             $output .= &Apache::lonhtmlcommon::row_closure(1);
                    598:         }
                    599:         $output .= &Apache::lonhtmlcommon::end_pick_box();
                    600:         $output .= <<"ENDSERVERFORM";
                    601:    <input type="hidden" name="logtoken" value="$logtoken" />
                    602:    <input type="hidden" name="token" value="$mailtoken" />
                    603:    <input type="hidden" name="serverid" value="$lonhost" />
                    604:    <input type="hidden" name="uname" value="" />
                    605:    <input type="hidden" name="upass" value="" />
                    606:    <input type="hidden" name="phase" value="createaccount" />
                    607:   </form></div>
                    608: ENDSERVERFORM
                    609:         my $upassone = '<input type="password" name="upass'.$now.'" size="10" />';
                    610:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="10" />';
                    611:         my $submit_text = &mt('Create LON-CAPA account');
                    612:         $output .= '<div class="LC_left_float"><h3>'.$lt{'lg'}.'</h3>'."\n".
                    613:                    '<form name="client" method="post" />'."\n".
                    614:                    &Apache::lonhtmlcommon::start_pick_box()."\n".
                    615:                    &Apache::lonhtmlcommon::row_title(&mt('Username'),
                    616:                                                     'LC_pick_box_title')."\n".
                    617:                    $username."\n".
                    618:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    619:                    &Apache::lonhtmlcommon::row_title(&mt('Password'),
                    620:                                                     'LC_pick_box_title')."\n".
                    621:                    $upassone."\n".
                    622:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    623:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password'),
                    624:                                                 'LC_pick_box_title')."\n".
                    625:                    $upasstwo."\n".
                    626:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    627:                    &Apache::lonhtmlcommon::end_pick_box()."\n".
                    628:                    '<input type="hidden" name="uname" value="'.$username.'">'."\n".
                    629:                    '<input type="hidden" name="lextkey" value="'.$lextkey.'">'."\n".
                    630:                    '<input type="hidden" name="uextkey" value="'.$uextkey.'">'."\n".
                    631:                    '</form></div>'."\n".
                    632:                    '<div class="LC_clear_float_footer"><br /><br />'."\n".
                    633:                    '<form name="buttonform">'."\n".
                    634:                    '<input type="button" name="createaccount" value="'.
                    635:                    $submit_text.'" onclick="javascript:checkpass();" /></form></div>';
                    636:     } else {
1.7       bisitz    637:         $output = &mt('Could not load javascript file [_1]','<tt>londes.js</tt>');
1.1       raeburn   638:     }
1.3       raeburn   639:     return $output;
1.1       raeburn   640: }
                    641: 
                    642: sub create_account {
1.3       raeburn   643:     my ($r,$domain,$lonhost,$username,$domdesc) = @_;
1.1       raeburn   644:     my ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    645:                                                           $env{'form.serverid'}); 
                    646:     # Error messages
1.7       bisitz    647:     my $error     = '<span class="LC_error">'.&mt('Error:').' ';
1.1       raeburn   648:     my $end       = '</span><br /><br />';
                    649:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                    650:                     &mt('Return to previous page').'</a>'.
                    651:                     &Apache::loncommon::end_page();
                    652:     if ($retrieved eq 'ok') {
                    653:         if ($env{'form.cid'} ne '') {
                    654:             my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
                    655:             if ($result eq 'fail') {
                    656:                 $output = $error.&mt('Invalid ID format').$end.
                    657:                           $userchkmsg.$rtnlink;
                    658:                 return ('fail',$output);
                    659:             }
                    660:         }
                    661:     } else {
                    662:         return ('fail',$error.$output.$end.$rtnlink);
                    663:     }
                    664:     # Call modifyuser
                    665:     my $result = 
                    666:         &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
                    667:                                     'internal',$upass,$env{'form.cfirstname'},
                    668:                                     $env{'form.cmiddlename'},$env{'form.clastname'},
                    669:                                     $env{'form.cgeneration'},undef,undef,$username);
1.7       bisitz    670:     $output = &mt('Generating user: [_1]',$result);
1.1       raeburn   671:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
1.7       bisitz    672:     $output .= '<br />'.&mt('Home server: [_1]',$uhome).' '.
1.1       raeburn   673:               &Apache::lonnet::hostname($uhome).'<br /><br />';
                    674:     return ('ok',$output);
                    675: }
                    676: 
                    677: sub username_validation {
1.8       raeburn   678:     my ($username,$domain,$domdesc,$contact_name,$contact_email,$courseid,$lonhost) = @_;
1.1       raeburn   679:     my ($retrieved,$output,$upass);
                    680: 
                    681:     $username= &LONCAPA::clean_username($username);
                    682:     $domain = &LONCAPA::clean_domain($domain);
                    683:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
                    684: 
                    685:     if ($uhome ne 'no_host') {
                    686:         $output = &invalid_state('existinguser',$domdesc,
                    687:                                  $contact_name,$contact_email);
                    688:         return $output;
                    689:     }
                    690:     ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    691:                                                        $env{'form.serverid'});
                    692:     if ($retrieved eq 'ok') {
                    693:         my $primlibserv = &Apache::lonnet::domain($domain,'primary');
                    694:         my $authok;
                    695:         my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    696:         if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
                    697:             my $checkdefauth = 1;
                    698:             $authok = 
                    699:                 &Apache::lonnet::reply("encrypt:auth:$domain:$username:$upass:$checkdefauth",$primlibserv);
                    700:         } else {
                    701:             $authok = 'non_authorized';
                    702:         }
                    703:         if ($authok eq 'authorized') {
1.8       raeburn   704:             ($output,undef) = &username_check($username,$domain,$domdesc,
                    705:                                               $courseid,$lonhost,$contact_email); 
1.4       raeburn   706:         } else {
1.6       bisitz    707:             $output = '<div class="LC_warning">'
                    708:                      .&mt('Username and/or password could not be authenticated.')
                    709:                      .'</div>'
                    710:                      .&mt('Please check the username and password.'); 
1.4       raeburn   711:         }
                    712:     }
                    713:     return $output;
                    714: }
                    715: 
                    716: sub username_check {
1.8       raeburn   717:     my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email) = @_;
1.4       raeburn   718:     my (%rulematch,%inst_results,$newuser,%alerts,%curr_rules,%got_rules);
                    719:     $newuser = 1;
                    720:     my $checkhash;
                    721:     my $checks = { 'username' => 1 };
                    722:     $checkhash->{$username.':'.$domain} = { 'newuser' => $newuser };
                    723:     &Apache::loncommon::user_rule_check($checkhash,$checks,\%alerts,\%rulematch,
                    724:                                         \%inst_results,\%curr_rules,\%got_rules);
                    725:     if (ref($alerts{'username'}) eq 'HASH') {
                    726:         if (ref($alerts{'username'}{$domain}) eq 'HASH') {
                    727:             if ($alerts{'username'}{$domain}{$username}) {
1.5       raeburn   728:                 my ($userchkmsg,$checkfail);
1.4       raeburn   729:                 if (ref($curr_rules{$domain}) eq 'HASH') {
                    730:                     $userchkmsg =
                    731:                         &Apache::loncommon::instrule_disallow_msg('username',$domdesc,1).
                    732:                         &Apache::loncommon::user_rule_formats($domain,$domdesc,
                    733:                                 $curr_rules{$domain}{'username'},'username');
1.5       raeburn   734:                     if ($userchkmsg) {
1.8       raeburn   735:                         $checkfail = 'username';
1.5       raeburn   736:                     }
1.1       raeburn   737:                 }
1.5       raeburn   738:                 return ($userchkmsg,$checkfail);
1.1       raeburn   739:             }
1.4       raeburn   740:         }
                    741:     }
                    742:     my $submit_text = &mt('Create LON-CAPA account');
                    743:     my $output = '<form method="post" action="/adm/createaccount">'.
                    744:                  &Apache::loncreateuser::personal_data_display($username,$domain,1,
                    745:                                     undef,$inst_results{$username.':'.$domain}).
                    746:                 '<br /><br /><input type="hidden" name="uname" value="'.$username.'" />'."\n".
                    747:                 '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
1.3       raeburn   748:                 '<input type="hidden" name="phase" value="username_activation" />';
1.8       raeburn   749:     my $now = time;
                    750:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    751:                 'time'       => $now,
                    752:                 'domain'     => $domain,
                    753:                 'username'   => $username);
                    754:     my $authtoken = &Apache::lonnet::tmpput(\%info,$lonhost);
                    755:     if ($authtoken !~ /^error/ && $authtoken ne 'no_such_host') {
                    756:         $output .= '<input type="hidden" name="authtoken" value="'.&HTML::Entities::encode($authtoken,'&<>"').'" />';
                    757:     } else {
                    758:         $output = &mt('An error occurred when storing a token').'<br />'.
                    759:                   &mt('You will not be able to proceed to the next stage of account creation').
                    760:                   &linkto_email_help($contact_email,$domdesc);
                    761:         return($output,'authtoken');
                    762:     }
1.4       raeburn   763:     if ($courseid ne '') {
                    764:         $output .= '<input type="hidden" name="courseid" value="'.$courseid.'" />';
1.1       raeburn   765:     }
1.4       raeburn   766:     $output .= '<input type="submit" name="newaccount" value="'.
                    767:                $submit_text.'" /></form>';
1.5       raeburn   768:     return ($output,'');
1.1       raeburn   769: }
                    770: 
                    771: sub username_activation {
1.3       raeburn   772:     my ($r,$username,$domain,$domdesc,$lonhost,$courseid) = @_;
1.1       raeburn   773:     my $output;
1.7       bisitz    774:     my $error     = '<span class="LC_error">'.&mt('Error:').' ';
1.1       raeburn   775:     my $end       = '</span><br /><br />';
                    776:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                    777:                     &mt('Return to previous page').'</a>'.
                    778:                     &Apache::loncommon::end_page();
                    779:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1.8       raeburn   780:     my %data = &Apache::lonnet::tmpget($env{'form.authtoken'});
                    781:     my $now = time;
                    782:     my $earlyout;
                    783:     my $timeout = 300;
                    784:     if (keys(%data) == 0) {
                    785:         $output = &mt('Sorry, your authentication has expired.');
                    786:         $earlyout = 'fail';
                    787:     }
                    788:     if (($data{'time'} !~ /^\d+$/) ||
                    789:         ($data{'domain'} ne $domain) || 
                    790:         ($data{'username'} ne $username)) {
                    791:         $earlyout = 'fail';
                    792:         $output = &mt('The credentials you provided could not be verified.');   
                    793:     } elsif ($now - $data{'time'} > $timeout) {
                    794:         $earlyout = 'fail';
                    795:         $output = &mt('Sorry, your authentication has expired.');
                    796:     }
                    797:     if ($earlyout ne '') {
                    798:         $output .= '<br />'.&mt('Please [_1]start again[_2].','<a href="/adm/createaccount">','</a>');
                    799:         return($earlyout,$output);
                    800:     }
1.3       raeburn   801:     if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && 
                    802:          ($domdefaults{'auth_arg_def'} ne '')) || 
                    803:         ($domdefaults{'auth_def'} eq 'localauth')) {
1.1       raeburn   804:         if ($env{'form.cid'} ne '') {
                    805:             my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
                    806:             if ($result eq 'fail') {
                    807:                 $output = $error.&mt('Invalid ID format').$end.
                    808:                           $userchkmsg.$rtnlink;
                    809:                 return ('fail',$output);
                    810:             }
                    811:         }
                    812:         # Call modifyuser
                    813:         my $result =
                    814:             &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
                    815:                           $domdefaults{'auth_def'},
                    816:                           $domdefaults{'auth_arg_def'},$env{'form.cfirstname'},
                    817:                           $env{'form.cmiddlename'},$env{'form.clastname'},
                    818:                           $env{'form.cgeneration'},undef,undef,
                    819:                           $env{'form.cpermanentemail'});
1.3       raeburn   820:         if ($result eq 'ok') {
1.8       raeburn   821:             my $delete = &Apache::lonnet::tmpdel($env{'form.authtoken'});
1.3       raeburn   822:             $output = &mt('A LON-CAPA account has been created for username: [_1] in domain: [_2].',$username,$domain);
                    823:             my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
                    824:             my $nostart = 1;
                    825:             return ('ok',$output,$nostart);
                    826:         } else {
                    827:             $output = &mt('Account creation failed for username: [_1] in domain: [_2].',$username,$domain).'<br /><span class="LC_error">'.&mt('Error: [_1]',$result).'</span>';
                    828:             return ('fail',$output);
                    829:         }
1.1       raeburn   830:     } else {
1.7       bisitz    831:         $output = &mt('User account creation is not available for the current default authentication type.')."\n";
1.1       raeburn   832:         return('fail',$output);
                    833:     }
                    834: }
                    835: 
                    836: sub check_id {
                    837:     my ($username,$domain,$domdesc) = @_;
                    838:     # Check ID format
                    839:     my (%alerts,%rulematch,%inst_results,%curr_rules,%checkhash);
                    840:     my %checks = ('id' => 1);
                    841:     %{$checkhash{$username.':'.$domain}} = (
                    842:                                             'newuser' => 1,
                    843:                                             'id' => $env{'form.cid'},
                    844:                                            );
                    845:     &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                    846:                                         \%rulematch,\%inst_results,\%curr_rules);
                    847:     if (ref($alerts{'id'}) eq 'HASH') {
                    848:         if (ref($alerts{'id'}{$domain}) eq 'HASH') {
                    849:             if ($alerts{'id'}{$domain}{$env{'form.cid'}}) {
                    850:                 my $userchkmsg;
                    851:                 if (ref($curr_rules{$domain}) eq 'HASH') {
                    852:                     $userchkmsg  =
                    853:                         &Apache::loncommon::instrule_disallow_msg('id',
                    854:                                                            $domdesc,1).
                    855:                         &Apache::loncommon::user_rule_formats($domain,
                    856:                               $domdesc,$curr_rules{$domain}{'id'},'id');
                    857:                 }
                    858:                 return ('fail',$userchkmsg);
                    859:             }
                    860:         }
                    861:     }
                    862:     return; 
                    863: }
                    864: 
                    865: sub invalid_state {
                    866:     my ($error,$domdesc,$contact_name,$contact_email,$msgtext) = @_;
                    867:     my $msg;
                    868:     if ($error eq 'baduseremail') {
                    869:         $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    870:     } elsif ($error eq 'existinguser') {
                    871:         $msg = &mt('The e-mail address you provided is already in use as a username in this LON-CAPA domain.');
                    872:     } elsif ($error eq 'userrules') {
                    873:         $msg = &mt('Username rules for this LON-CAPA domain do not allow the e-mail address you provided to be used as a username.');
                    874:     } elsif ($error eq 'userformat') {
                    875:         $msg = &mt('The e-mail address you provided may not be used as a username in this LON-CAPA domain.');
                    876:     } elsif ($error eq 'captcha') {
                    877:         $msg = &mt('Validation of the code your entered failed.');
                    878:     } elsif ($error eq 'noemails') {
                    879:         $msg = &mt('Creation of a new user account using an e-mail address as username is not permitted in this LON-CAPA domain.');
                    880:     }
                    881:     if ($msgtext) {
                    882:         $msg .= '<br />'.$msgtext;
                    883:     }
1.8       raeburn   884:     $msg .= &linkto_email_help($contact_email,$domdesc);
                    885:     return $msg;
                    886: }
                    887: 
                    888: sub linkto_email_help {
                    889:     my ($contact_email,$domdesc) = @_;
                    890:     my $msg;
1.1       raeburn   891:     if ($contact_email ne '') {
                    892:         my $escuri = &HTML::Entities::encode('/adm/createaccount','&<>"');
1.7       bisitz    893:         $msg .= '<br />'.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for the [_3] domain.','<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc);
1.1       raeburn   894:     } else {
1.7       bisitz    895:         $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);
1.1       raeburn   896:     }
                    897:     return $msg;
                    898: }
                    899: 
                    900: sub create_captcha {
                    901:     my ($output_dir,$db_dir) = @_;
                    902:     my %captcha_params = &captcha_settings();
                    903:     my $captcha = Authen::Captcha->new(
                    904:                                        output_folder => $captcha_params{'output_dir'},
                    905:                                        data_folder   => $captcha_params{'db_dir'},
                    906:                                       );
                    907:     my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
                    908:     my $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
                    909:                  &mt('Type in the letters/numbers shown below').'&nbsp;'.
                    910:                  '<input type="text" size="5" name="code" value="" /><br />'.
                    911:                  '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png">';
                    912:     return $output;
                    913: }
                    914: 
                    915: sub captcha_settings {
                    916:     my %captcha_params = ( 
                    917:                            output_dir     => "/home/httpd/html/captcha",
                    918:                            www_output_dir => "/captcha",
                    919:                            db_dir         => "/home/www/captchadb",
                    920:                            numchars       => '5',
                    921:                          );
                    922:     return %captcha_params;
                    923: }
                    924: 
                    925: sub getkeys {
                    926:     my ($lkey,$ukey) = @_;
                    927:     my $lextkey=hex($lkey);
                    928:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
                    929: 
                    930:     my $uextkey=hex($ukey);
                    931:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
                    932:     return ($lextkey,$uextkey);
                    933: }
                    934: 
                    935: sub serverform {
1.3       raeburn   936:     my ($logtoken,$lonhost,$mailtoken,$courseid) = @_;
1.1       raeburn   937:     my $output .= <<ENDSERVERFORM;
                    938:   <form name="server" method="post" target="_top">
                    939:    <input type="hidden" name="logtoken" value="$logtoken" />
                    940:    <input type="hidden" name="token" value="$mailtoken" />
                    941:    <input type="hidden" name="serverid" value="$lonhost" />
                    942:    <input type="hidden" name="uname" value="" />
                    943:    <input type="hidden" name="upass" value="" />
                    944:    <input type="hidden" name="phase" value="username_validation" />
1.3       raeburn   945:    <input type="hidden" name="courseid" value="$courseid" />
1.1       raeburn   946:   </form>
                    947: ENDSERVERFORM
                    948:     return $output;
                    949: }
                    950: 
                    951: sub process_credentials {
                    952:     my ($logtoken,$lonhost) = @_;
                    953:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
                    954:     my ($retrieved,$output,$upass);
                    955:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.7       bisitz    956:         $output = &mt('Information needed to verify your login information is missing, inaccessible or expired.')
                    957:                  .'<br />'.&mt('You may need to reload the previous page to obtain a new token.');
1.1       raeburn   958:         return ($retrieved,$output,$upass); 
                    959:     } else {
                    960:         my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$lonhost);
                    961:         if ($reply eq 'ok') {
                    962:             $retrieved = 'ok';
                    963:         } else {
                    964:             $output = &mt('Session could not be opened.');
                    965:         }
                    966:     }
                    967:     my ($key,$caller)=split(/&/,$tmpinfo);
                    968:     if ($caller eq 'createaccount') {
                    969:         $upass = &Apache::lonpreferences::des_decrypt($key,$env{'form.upass'});
                    970:     } else {
                    971:         $output = &mt('Unable to retrieve your log-in information - unexpected context');
                    972:     }
                    973:     return ($retrieved,$output,$upass);
                    974: }
                    975: 
                    976: sub guest_format_check {
                    977:     my ($useremail,$domain,$cancreate,$settings) = @_;
                    978:     my ($login,$format_match,$format_msg,@user_rules);
                    979:     if (ref($settings) eq 'HASH') {
                    980:         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
                    981:             push(@user_rules,@{$settings->{'email_rule'}});
                    982:         }
                    983:     }
                    984:     if (@user_rules > 0) {
                    985:         my %rule_check = 
                    986:             &Apache::lonnet::inst_rulecheck($domain,$useremail,undef,
1.2       raeburn   987:                                             'selfcreate',\@user_rules);
1.1       raeburn   988:         if (keys(%rule_check) > 0) {
                    989:             foreach my $item (keys(%rule_check)) {
                    990:                 if ($rule_check{$item}) {
                    991:                     $format_match = 1;   
                    992:                     last;
                    993:                 }
                    994:             }
                    995:         }
                    996:     }
                    997:     if ($format_match) {
                    998:         ($login) = ($useremail =~ /^([^\@]+)\@/);
                    999:         $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 />';
1.5       raeburn  1000:         if (ref($cancreate) eq 'ARRAY') {
                   1001:             if (grep(/^login$/,@{$cancreate})) {
1.7       bisitz   1002:                 $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 />'; 
1.5       raeburn  1003:             }
1.1       raeburn  1004:         }
                   1005:     }
                   1006:     return $format_msg;
                   1007: }
                   1008: 
                   1009: 1;

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