File:  [LON-CAPA] / loncom / interface / createaccount.pm
Revision 1.12: download - view: text, annotated - select for diffs
Tue Jul 8 17:47:57 2008 UTC (15 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- client side javascript validation of e-mail address format for e-mail being proposed as username.

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

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