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

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

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