File:  [LON-CAPA] / loncom / interface / createaccount.pm
Revision 1.14: download - view: text, annotated - select for diffs
Wed Jul 9 17:30:59 2008 UTC (15 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Adding some header lines and wrapping some messages in <span>s for LC_warning or LC_error.

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

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