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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow visitors to create a user account with the username being either an 
                      3: # institutional log-in ID (institutional authentication required - localauth
                      4: #  or kerberos) or an e-mail address.
                      5: #
1.40.2.1! raeburn     6: # $Id: createaccount.pm,v 1.40 2010/08/16 14:23:50 raeburn Exp $
1.1       raeburn     7: #
                      8: # Copyright Michigan State University Board of Trustees
                      9: #
                     10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     11: #
                     12: # LON-CAPA is free software; you can redistribute it and/or modify
                     13: # it under the terms of the GNU General Public License as published by
                     14: # the Free Software Foundation; either version 2 of the License, or
                     15: # (at your option) any later version.
                     16: #
                     17: # LON-CAPA is distributed in the hope that it will be useful,
                     18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     20: # GNU General Public License for more details.
                     21: #
                     22: # You should have received a copy of the GNU General Public License
                     23: # along with LON-CAPA; if not, write to the Free Software
                     24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     25: #
                     26: # /home/httpd/html/adm/gpl.txt
                     27: #
                     28: # http://www.lon-capa.org/
                     29: #
                     30: #
                     31: package Apache::createaccount;
                     32: 
                     33: use strict;
                     34: use Apache::Constants qw(:common);
                     35: use Apache::lonacc;
                     36: use Apache::lonnet;
                     37: use Apache::loncommon;
1.12      raeburn    38: use Apache::lonhtmlcommon;
1.1       raeburn    39: use Apache::lonlocal;
1.3       raeburn    40: use Apache::lonauth;
1.1       raeburn    41: use Apache::resetpw;
                     42: use Authen::Captcha;
                     43: use DynaLoader; # for Crypt::DES version
                     44: use Crypt::DES;
1.3       raeburn    45: use LONCAPA qw(:DEFAULT :match);
1.8       raeburn    46: use HTML::Entities;
1.1       raeburn    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:     }
1.22      raeburn    55: 
1.5       raeburn    56:     my $domain;
1.3       raeburn    57: 
1.5       raeburn    58:     my $sso_username = $r->subprocess_env->get('REDIRECT_SSOUserUnknown');
                     59:     my $sso_domain = $r->subprocess_env->get('REDIRECT_SSOUserDomain');
                     60: 
1.27      raeburn    61:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token','courseid']);
                     62:     &Apache::lonacc::get_posted_cgi($r);
                     63:     &Apache::lonlocal::get_language_handle($r);
                     64: 
1.5       raeburn    65:     if ($sso_username ne '' && $sso_domain ne '') {
                     66:         $domain = $sso_domain; 
1.27      raeburn    67:     } else {
1.5       raeburn    68:         $domain = &Apache::lonnet::default_login_domain();
1.27      raeburn    69:         if (defined($env{'form.courseid'})) {
                     70:             if (&validate_course($env{'form.courseid'})) {
                     71:                 if ($env{'form.courseid'} =~ /^($match_domain)_($match_courseid)$/) {
                     72:                     $domain = $1; 
                     73:                 }
                     74:             }
                     75:         }
1.5       raeburn    76:     }
1.1       raeburn    77:     my $domdesc = &Apache::lonnet::domain($domain,'description');
                     78:     my $contact_name = &mt('LON-CAPA helpdesk');
1.15      raeburn    79:     my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
                     80:     my $contacts =
                     81:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
                     82:                                                  $domain,$origmail);
                     83:     my ($contact_email) = split(',',$contacts);
1.1       raeburn    84:     my $lonhost = $r->dir_config('lonHostID');
                     85:     my $include = $r->dir_config('lonIncludes');
1.4       raeburn    86:     my $start_page;
1.3       raeburn    87: 
                     88:     my $handle = &Apache::lonnet::check_for_valid_session($r);
1.30      raeburn    89:     if (($handle ne '') && ($handle !~ /^publicuser_\d+$/)) {
1.4       raeburn    90:         $start_page =
1.3       raeburn    91:             &Apache::loncommon::start_page('Already logged in');
                     92:         my $end_page =
                     93:             &Apache::loncommon::end_page();
                     94:         $r->print($start_page."\n".'<h2>'.&mt('You are already logged in').'</h2>'.
1.33      hauer      95:                   '<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].','<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>').
1.6       bisitz     96:                   '</p><p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'.$end_page);
1.20      raeburn    97:         return OK;
                     98:     }
                     99: 
1.22      raeburn   100:     my ($js,$courseid,$title);
1.20      raeburn   101:     if (defined($env{'form.courseid'})) {
                    102:         $courseid = &validate_course($env{'form.courseid'});
1.3       raeburn   103:     }
1.22      raeburn   104:     if ($courseid ne '') {
                    105:         $js = &catreturn_js();
                    106:         $title = 'Self-enroll in a LON-CAPA course';
                    107:     } else {
                    108:         $title = 'Create a user account in LON-CAPA';
                    109:     }
1.20      raeburn   110:     if ($env{'form.phase'} eq 'selfenroll_login') {
1.22      raeburn   111:         $title = 'Self-enroll in a LON-CAPA course';
1.4       raeburn   112:         if ($env{'form.udom'} ne '') {
                    113:             $domain = $env{'form.udom'};
                    114:         }
1.35      raeburn   115: 
                    116:         my %domconfig = 
                    117:             &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
                    118:         my ($cancreate,$statustocreate) = 
                    119:             &get_creation_controls($domain,$domconfig{'usercreation'});
                    120: 
1.20      raeburn   121:         my ($result,$output) =
                    122:             &username_validation($r,$env{'form.uname'},$domain,$domdesc,
                    123:                                  $contact_name,$contact_email,$courseid,
1.35      raeburn   124:                                  $lonhost,$statustocreate);
1.20      raeburn   125:         if ($result eq 'existingaccount') {
                    126:             $r->print($output);
1.22      raeburn   127:             &print_footer($r);
1.20      raeburn   128:             return OK;
                    129:         } else {
                    130:             $start_page = 
1.40.2.1! raeburn   131:                 &Apache::loncommon::start_page($title,$js,
        !           132:                                                {'no_inline_link'   => 1,});
1.22      raeburn   133:             &print_header($r,$start_page,$courseid);
                    134:             $r->print($output);
                    135:             &print_footer($r);    
1.20      raeburn   136:             return OK;
                    137:         }
1.4       raeburn   138:     }
1.20      raeburn   139:     $start_page =
1.40.2.1! raeburn   140:         &Apache::loncommon::start_page($title,$js,
        !           141:                                        {'no_inline_link'   => 1,});
1.35      raeburn   142:     my %domconfig = 
                    143:         &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
                    144:     my ($cancreate,$statustocreate) = &get_creation_controls($domain,$domconfig{'usercreation'});
                    145:     if (@{$cancreate} == 0) {
1.22      raeburn   146:         &print_header($r,$start_page,$courseid);
1.14      raeburn   147:         my $output = '<h3>'.&mt('Account creation unavailable').'</h3>'.
                    148:                      '<span class="LC_warning">'.
1.16      raeburn   149:                      &mt('Creation of a new user account using an e-mail address or an institutional log-in ID as username is not permitted at this institution ([_1]).',$domdesc).'</span><br /><br />';
1.3       raeburn   150:         $r->print($output);
1.22      raeburn   151:         &print_footer($r);
1.3       raeburn   152:         return OK;
                    153:     }
                    154: 
1.5       raeburn   155:     if ($sso_username ne '') {
1.22      raeburn   156:         &print_header($r,$start_page,$courseid);
1.18      raeburn   157:         my ($msg,$sso_logout);
                    158:         $sso_logout = &sso_logout_frag($r,$domain);
1.35      raeburn   159:         if (grep(/^sso$/,@{$cancreate})) {
1.14      raeburn   160:             $msg = '<h3>'.&mt('Account creation').'</h3>'.
1.17      raeburn   161:                    &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 at this institution.").'<br />';
                    162: 
1.18      raeburn   163:             $msg .= &username_check($sso_username,$domain,$domdesc,$courseid, 
1.35      raeburn   164:                                     $lonhost,$contact_email,$contact_name,
                    165:                                     $sso_logout,$statustocreate);
1.5       raeburn   166:         } else {
1.17      raeburn   167:             $msg = '<h3>'.&mt('Account creation unavailable').'</h3>'.
                    168:                    '<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 at this institution, and you are not permitted to create one.").'</span><br /><br />'.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email).'<hr />'.
1.18      raeburn   169:                    $sso_logout;
1.5       raeburn   170:         }
1.17      raeburn   171:         $r->print($msg);
1.22      raeburn   172:         &print_footer($r);
1.5       raeburn   173:         return OK;
                    174:     }
                    175: 
1.4       raeburn   176:     my ($output,$nostart,$noend);
1.3       raeburn   177:     my $token = $env{'form.token'};
                    178:     if ($token) {
                    179:         ($output,$nostart,$noend) = 
                    180:             &process_mailtoken($r,$token,$contact_name,$contact_email,$domain,
1.4       raeburn   181:                                $domdesc,$lonhost,$include,$start_page);
1.3       raeburn   182:         if ($nostart) {
                    183:             if ($noend) {
                    184:                 return OK;
                    185:             } else {
                    186:                 $r->print($output);
1.22      raeburn   187:                 &print_footer($r);
1.3       raeburn   188:                 return OK;
                    189:             }
                    190:         } else {
1.22      raeburn   191:             &print_header($r,$start_page,$courseid);
1.3       raeburn   192:             $r->print($output);
1.22      raeburn   193:             &print_footer($r);
1.3       raeburn   194:             return OK;
                    195:         }
                    196:     }
                    197: 
                    198:     if ($env{'form.phase'} eq 'username_activation') {
                    199:         (my $result,$output,$nostart) = 
                    200:             &username_activation($r,$env{'form.uname'},$domain,$domdesc,
                    201:                                  $lonhost,$courseid);
                    202:         if ($result eq 'ok') {
                    203:             if ($nostart) {
                    204:                 return OK;
                    205:             }
                    206:         }
1.22      raeburn   207:         &print_header($r,$start_page,$courseid);
1.3       raeburn   208:         $r->print($output);
1.22      raeburn   209:         &print_footer($r);
1.3       raeburn   210:         return OK;
1.19      raeburn   211:     } elsif ($env{'form.phase'} eq 'username_validation') { 
                    212:         (my $result,$output) = 
                    213:             &username_validation($r,$env{'form.uname'},$domain,$domdesc,
                    214:                                  $contact_name,$contact_email,$courseid,
1.35      raeburn   215:                                  $lonhost,$statustocreate);
1.19      raeburn   216:         if ($result eq 'existingaccount') {
                    217:             $r->print($output);
1.22      raeburn   218:             &print_footer($r);
1.19      raeburn   219:             return OK;
                    220:         } else {
1.22      raeburn   221:             &print_header($r,$start_page,$courseid);
1.19      raeburn   222:         }
                    223:     } elsif ($env{'form.create_with_email'}) {
1.22      raeburn   224:         &print_header($r,$start_page,$courseid);
1.1       raeburn   225:         $output = &process_email_request($env{'form.useremail'},$domain,$domdesc,
1.35      raeburn   226:                                          $contact_name,$contact_email,$cancreate,
1.3       raeburn   227:                                          $lonhost,$domconfig{'usercreation'},
                    228:                                          $courseid);
                    229:     } elsif (!$token) {
1.22      raeburn   230:         &print_header($r,$start_page,$courseid);
1.1       raeburn   231:         my $now=time;
1.35      raeburn   232:         if (grep(/^login$/,@{$cancreate})) {
1.1       raeburn   233:             my $jsh=Apache::File->new($include."/londes.js");
                    234:             $r->print(<$jsh>);
                    235:             $r->print(&javascript_setforms($now));
                    236:         }
1.35      raeburn   237:         if (grep(/^email$/,@{$cancreate})) {
1.12      raeburn   238:             $r->print(&javascript_validmail());
                    239:         }
1.35      raeburn   240:         $output = &print_username_form($domain,$domdesc,$cancreate,$now,$lonhost,
1.22      raeburn   241:                                        $courseid);
1.1       raeburn   242:     }
                    243:     $r->print($output);
1.22      raeburn   244:     &print_footer($r);
1.1       raeburn   245:     return OK;
                    246: }
                    247: 
1.3       raeburn   248: sub print_header {
1.22      raeburn   249:     my ($r,$start_page,$courseid) = @_;
1.3       raeburn   250:     $r->print($start_page);
                    251:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.22      raeburn   252:     if ($courseid ne '') {
                    253:         my %coursehash = &Apache::lonnet::coursedescription($courseid);
                    254:         &selfenroll_crumbs($r,$courseid,$coursehash{'description'});
                    255:     }
1.3       raeburn   256:     &Apache::lonhtmlcommon::add_breadcrumb
                    257:     ({href=>"/adm/createuser",
                    258:       text=>"New username"});
                    259:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account'));
                    260:     return;
                    261: }
                    262: 
1.22      raeburn   263: sub print_footer {
                    264:     my ($r) = @_;
                    265:     if ($env{'form.courseid'} ne '') {
                    266:         $r->print('<form name="backupcrumbs" method="post" action="">'.
                    267:                   &Apache::lonhtmlcommon::echo_form_input(['backto','logtoken',
                    268:                       'token','serverid','uname','upass','phase','create_with_email',
                    269:                       'code','useremail','crypt','cfirstname','clastname',
1.23      raeburn   270:                       'cmiddlename','cgeneration','cpermanentemail','cid']).
1.22      raeburn   271:                   '</form>');
                    272:     }
                    273:     $r->print(&Apache::loncommon::end_page());
                    274: }
                    275: 
                    276: sub selfenroll_crumbs {
                    277:     my ($r,$courseid,$desc) = @_;
                    278:     &Apache::lonhtmlcommon::add_breadcrumb
                    279:          ({href=>"javascript:ToCatalog('backupcrumbs','')",
1.37      bisitz    280:            text=>"Course/Community Catalog"});
1.22      raeburn   281:     if ($env{'form.coursenum'} ne '') {
                    282:         &Apache::lonhtmlcommon::add_breadcrumb
                    283:           ({href=>"javascript:ToCatalog('backupcrumbs','details')",
                    284:             text=>"Course details"});
                    285:     }
                    286:     my $last_crumb;
                    287:     if ($desc ne '') {
                    288:         $last_crumb = &mt('Self-enroll in [_1]','<span class="LC_cusr_emph">'.$desc.'</span>');
                    289:     } else {
                    290:         $last_crumb = &mt('Self-enroll');
                    291:     }
                    292:     &Apache::lonhtmlcommon::add_breadcrumb
                    293:                    ({href=>"javascript:ToSelfenroll('backupcrumbs')",
                    294:                      text=>$last_crumb,
                    295:                      no_mt=>"1"});
                    296:     return;
                    297: }
                    298: 
1.3       raeburn   299: sub validate_course {
                    300:     my ($courseid) = @_;
                    301:     my ($cdom,$cnum) = ($courseid =~ /^($match_domain)_($match_courseid)$/);
                    302:     if (($cdom ne '') && ($cnum ne '')) {
                    303:         if (&Apache::lonnet::is_course($cdom,$cnum)) {
                    304:             return ($courseid);
                    305:         }
                    306:     }
                    307:     return;
                    308: }
                    309: 
1.1       raeburn   310: sub javascript_setforms {
                    311:     my ($now) =  @_;
                    312:     my $js = <<ENDSCRIPT;
1.32      raeburn   313:  <script type="text/javascript" language="JavaScript">
1.1       raeburn   314:     function send() {
                    315:         this.document.server.elements.uname.value = this.document.client.elements.uname.value;
1.32      raeburn   316:         this.document.server.elements.udom.value = this.document.client.elements.udom.value;
1.1       raeburn   317:         uextkey=this.document.client.elements.uextkey.value;
                    318:         lextkey=this.document.client.elements.lextkey.value;
                    319:         initkeys();
                    320: 
                    321:         this.document.server.elements.upass.value
                    322:             = crypted(this.document.client.elements.upass$now.value);
                    323: 
                    324:         this.document.client.elements.uname.value='';
                    325:         this.document.client.elements.upass$now.value='';
                    326: 
                    327:         this.document.server.submit();
                    328:         return false;
                    329:     }
                    330:  </script>
                    331: ENDSCRIPT
                    332:     return $js;
                    333: }
                    334: 
                    335: sub javascript_checkpass {
                    336:     my ($now) = @_;
1.7       bisitz    337:     my $nopass = &mt('You must enter a password.');
1.1       raeburn   338:     my $mismatchpass = &mt('The passwords you entered did not match.').'\\n'.
                    339:                        &mt('Please try again.'); 
                    340:     my $js = <<"ENDSCRIPT";
                    341: <script type="text/javascript" language="JavaScript">
                    342:     function checkpass() {
                    343:         var upass = this.document.client.elements.upass$now.value;
                    344:         var upasscheck = this.document.client.elements.upasscheck$now.value;
                    345:         if (upass == '') {
                    346:             alert("$nopass");
1.32      raeburn   347:             return false;
1.1       raeburn   348:         }
                    349:         if (upass == upasscheck) {
                    350:             this.document.client.elements.upasscheck$now.value='';
                    351:             send();
1.32      raeburn   352:             return false;
1.1       raeburn   353:         } else {
                    354:             alert("$mismatchpass");
1.32      raeburn   355:             return false;
                    356:         }
1.1       raeburn   357:     }
                    358: </script>
                    359: ENDSCRIPT
                    360:     return $js;
                    361: }
                    362: 
1.12      raeburn   363: sub javascript_validmail {
                    364:     my %lt = &Apache::lonlocal::texthash (
                    365:                email => 'The e-mail address you entered',
                    366:                notv  => 'is not a valid e-mail address',
                    367:     );
                    368:     my $output =  "\n".'<script type="text/javascript">'."\n".
                    369:                   &Apache::lonhtmlcommon::javascript_valid_email()."\n";
                    370:     $output .= <<"ENDSCRIPT";
                    371: function validate_email() {
                    372:     field = document.createaccount.useremail;
                    373:     if (validmail(field) == false) {
                    374:         alert("$lt{'email'}: "+field.value+" $lt{'notv'}.");
                    375:         return false;
                    376:     }
                    377:     return true;
                    378: }
                    379: ENDSCRIPT
                    380:     $output .= "\n".'</script>'."\n";
                    381:     return $output;
                    382: }
                    383: 
1.1       raeburn   384: sub print_username_form {
1.3       raeburn   385:     my ($domain,$domdesc,$cancreate,$now,$lonhost,$courseid) = @_;
1.1       raeburn   386:     my %lt = &Apache::lonlocal::texthash(
                    387:                                          unam => 'username',
                    388:                                          udom => 'domain',
1.26      schafran  389:                                          uemail => 'E-mail address in LON-CAPA',
1.1       raeburn   390:                                          proc => 'Proceed');
                    391:     my $output;
1.5       raeburn   392:     if (ref($cancreate) eq 'ARRAY') {
                    393:         if (grep(/^login$/,@{$cancreate})) {
                    394:             my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    395:             if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
1.16      raeburn   396:                 $output = '<div class="LC_left_float"><h3>'.&mt('Create account with a username provided by this institution').'</h3>';
1.20      raeburn   397:                 my $submit_text = &mt('Create LON-CAPA account');
1.16      raeburn   398:                 $output .= &mt('If you already have a log-in ID at this institution,[_1] you may be able to use it for LON-CAPA.','<br />').'<br /><br />'.&mt('Type in your log-in ID and password to find out.').'<br /><br />';
1.20      raeburn   399:                 $output .= &login_box($now,$lonhost,$courseid,$submit_text,
1.22      raeburn   400:                                       $domain,'createaccount').'</div>';
1.5       raeburn   401:             }
                    402:         }
                    403:         if (grep(/^email$/,@{$cancreate})) {
                    404:             $output .= '<div class="LC_left_float"><h3>'.&mt('Create account with an e-mail address as your username').'</h3>';
1.28      raeburn   405:             my $captchaform = &create_captcha();
                    406:             if ($captchaform) {
                    407:                 my $submit_text = &mt('Request LON-CAPA account');
                    408:                 my $emailform = '<input type="text" name="useremail" size="25" value="" />';
                    409:                 if (grep(/^login$/,@{$cancreate})) {
                    410:                     $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 />';
                    411:                 } else {
                    412:                     $output .= '<br />';
                    413:                 }
                    414:                 $output .=  '<form name="createaccount" method="post" onSubmit="return validate_email()" action="/adm/createaccount">'.
                    415:                             &Apache::lonhtmlcommon::start_pick_box()."\n".
                    416:                             &Apache::lonhtmlcommon::row_title(&mt('E-mail address'),
                    417:                                                              'LC_pick_box_title')."\n".
                    418:                             $emailform."\n".
                    419:                             &Apache::lonhtmlcommon::row_closure(1).
                    420:                             &Apache::lonhtmlcommon::row_title(&mt('Validation'),
                    421:                                                              'LC_pick_box_title')."\n".
                    422:                             $captchaform."\n".'<br /><br />';
                    423:                 if ($courseid ne '') {
                    424:                     $output .= '<input type="hidden" name="courseid" value="'.$courseid.'"/>'."\n"; 
                    425:                 }
1.32      raeburn   426:                 $output .=  &Apache::lonhtmlcommon::row_closure(1).
                    427:                             &Apache::lonhtmlcommon::row_title().'<br />'.
                    428:                             '<input type="submit" name="create_with_email" value="'. 
1.28      raeburn   429:                             $submit_text.'" />'.
                    430:                             &Apache::lonhtmlcommon::row_closure(1).
                    431:                             &Apache::lonhtmlcommon::end_pick_box().'<br /><br />';
                    432:                 if ($courseid ne '') {
                    433:                     $output .= &Apache::lonhtmlcommon::echo_form_input(['courseid']);
                    434:                 }
                    435:                 $output .= '</form>';
1.5       raeburn   436:             } else {
1.28      raeburn   437:                 my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
                    438:                 if ($courseid ne '') {
                    439:                     $helpdesk .= '&courseid='.$courseid;
                    440:                 }
                    441:                 $output .= '<span class="LC_error">'.&mt('An error occurred generating the validation code[_1] required for an e-mail address to be used as username.','<br />').'</span><br /><br />'.&mt('[_1]Contact the helpdesk[_2] or [_3]reload[_2] the page and try again.','<a href="'.$helpdesk.'">','</a>','<a href="javascript:window.location.reload()">');
1.5       raeburn   442:             }
1.28      raeburn   443:             $output .= '</div>';
1.3       raeburn   444:         }
1.1       raeburn   445:     }
                    446:     if ($output eq '') {
1.16      raeburn   447:         $output = &mt('Creation of a new LON-CAPA user account using an e-mail address or an institutional log-in ID as your username is not permitted at [_1].',$domdesc);
1.1       raeburn   448:     } else {
                    449:         $output .= '<div class="LC_clear_float_footer"></div>';
                    450:     }
                    451:     return $output;
                    452: }
                    453: 
1.20      raeburn   454: sub login_box {
                    455:     my ($now,$lonhost,$courseid,$submit_text,$domain,$context) = @_;
                    456:     my $output;
                    457:     my %titles = &Apache::lonlocal::texthash(
                    458:                                               createaccount => 'Log-in ID',
                    459:                                               selfenroll    => 'Username',
                    460:                                             );
                    461:     my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    462:     my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
                    463:     my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
                    464:                                        $lonhost);
                    465:     $output = &serverform($logtoken,$lonhost,undef,$courseid,$context);
1.32      raeburn   466:     my $unameform = '<input type="text" name="uname" size="20" value="" />';
                    467:     my $upassform = '<input type="password" name="upass'.$now.'" size="20" />';
                    468:     $output .= '<form name="client" method="post" onsubmit="return(send());">'."\n".
                    469:                &Apache::lonhtmlcommon::start_pick_box()."\n".
                    470:                &Apache::lonhtmlcommon::row_title($titles{$context},
1.31      bisitz    471:                                                  'LC_pick_box_title')."\n".
                    472:                $unameform."\n".
                    473:                &Apache::lonhtmlcommon::row_closure(1)."\n".
                    474:                &Apache::lonhtmlcommon::row_title(&mt('Password'),
                    475:                                                 'LC_pick_box_title')."\n".
                    476:                $upassform;
1.20      raeburn   477:     if ($context eq 'selfenroll') {
1.32      raeburn   478:         my $udomform = '<input type="text" name="udom" size="10" value="'.
1.20      raeburn   479:                         $domain.'" />';
1.31      bisitz    480:         $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                    481:                    &Apache::lonhtmlcommon::row_title(&mt('Domain'),
1.20      raeburn   482:                                                      'LC_pick_box_title')."\n".
1.31      bisitz    483:                    $udomform."\n";
1.32      raeburn   484:     } else {
                    485:         $output .= '<input type="hidden" name="udom" value="'.$domain.'" />';
1.20      raeburn   486:     }
1.32      raeburn   487:     $output .= &Apache::lonhtmlcommon::row_closure(1).
1.31      bisitz    488:                &Apache::lonhtmlcommon::row_title().
1.32      raeburn   489:                '<br /><input type="submit" name="username_validation" value="'.
                    490:                $submit_text.'" />'."\n";
                    491:     if ($context eq 'selfenroll') {
                    492:         $output .= '<br /><br /><table width="100%"><tr><td align="right">'.
                    493:                    '<span class="LC_fontsize_medium">'.
                    494:                    '<a href="/adm/resetpw">'.&mt('Forgot password?').'</a>'.
                    495:                    '</span></td></tr></table>'."\n";
                    496:     }
                    497:     $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                    498:                &Apache::lonhtmlcommon::end_pick_box().'<br />'."\n";
1.34      bisitz    499:     $output .= '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
                    500:                '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
1.22      raeburn   501:                '</form>';
1.20      raeburn   502:     return $output;
                    503: }
                    504: 
1.1       raeburn   505: sub process_email_request {
                    506:     my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
1.3       raeburn   507:         $server,$settings,$courseid) = @_;
1.21      raeburn   508:     $useremail = $env{'form.useremail'};
1.1       raeburn   509:     my $output;
1.5       raeburn   510:     if (ref($cancreate) eq 'ARRAY') {
                    511:         if (!grep(/^email$/,@{$cancreate})) {
                    512:             $output = &invalid_state('noemails',$domdesc,
                    513:                                      $contact_name,$contact_email);
                    514:             return $output;
                    515:         } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    516:             $output = &invalid_state('baduseremail',$domdesc,
1.1       raeburn   517:                                      $contact_name,$contact_email);
                    518:             return $output;
                    519:         } else {
1.5       raeburn   520:             my $uhome = &Apache::lonnet::homeserver($useremail,$domain);
                    521:             if ($uhome ne 'no_host') {
                    522:                 $output = &invalid_state('existinguser',$domdesc,
                    523:                                          $contact_name,$contact_email);
1.1       raeburn   524:                 return $output;
1.5       raeburn   525:             } else {
                    526:                 my $code = $env{'form.code'};
                    527:                 my $md5sum = $env{'form.crypt'};
                    528:                 my %captcha_params = &captcha_settings();
                    529:                 my $captcha = Authen::Captcha->new(
                    530:                                   output_folder => $captcha_params{'output_dir'},
                    531:                                   data_folder   => $captcha_params{'db_dir'},
                    532:                                  );
                    533:                 my $captcha_chk = $captcha->check_code($code,$md5sum);
                    534:                 my %captcha_hash = (
                    535:                                   0       => 'Code not checked (file error)',
                    536:                                   -1      => 'Failed: code expired',
                    537:                                   -2      => 'Failed: invalid code (not in database)',
                    538:                                   -3      => 'Failed: invalid code (code does not match crypt)',
                    539:                                    );
                    540:                 if ($captcha_chk != 1) {
                    541:                     $output = &invalid_state('captcha',$domdesc,$contact_name,
                    542:                                              $contact_email,$captcha_hash{$captcha_chk});
                    543:                     return $output;
                    544:                 }
                    545:                 my $uhome=&Apache::lonnet::homeserver($useremail,$domain);
                    546:                 if ($uhome eq 'no_host') {
1.23      raeburn   547:                     my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
                    548:                     &call_rulecheck($useremail,$domain,\%alerts,\%rulematch,
1.40      raeburn   549:                                     \%inst_results,\%curr_rules,\%got_rules,'username');
1.23      raeburn   550:                     if (ref($alerts{'username'}) eq 'HASH') {
                    551:                         if (ref($alerts{'username'}{$domain}) eq 'HASH') {
1.5       raeburn   552:                             if ($alerts{'username'}{$domain}{$useremail}) {
                    553:                                 $output = &invalid_state('userrules',$domdesc,
                    554:                                                          $contact_name,$contact_email);
                    555:                                 return $output;
                    556:                             }
1.1       raeburn   557:                         }
                    558:                     }
1.5       raeburn   559:                     my $format_msg = 
                    560:                         &guest_format_check($useremail,$domain,$cancreate,
                    561:                                             $settings);
                    562:                     if ($format_msg) {
                    563:                         $output = &invalid_state('userformat',$domdesc,$contact_name,
                    564:                                                  $contact_email,$format_msg);
                    565:                         return $output;
                    566:                     }
1.1       raeburn   567:                 }
                    568:             }
                    569:         }
1.5       raeburn   570:         $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
                    571:                           $contact_email,$courseid);
1.1       raeburn   572:     }
                    573:     return $output;
                    574: }
                    575: 
1.23      raeburn   576: sub call_rulecheck {
                    577:     my ($uname,$udom,$alerts,$rulematch,$inst_results,$curr_rules,
                    578:         $got_rules,$tocheck) = @_;
                    579:     my ($checkhash,$checks);
                    580:     $checkhash->{$uname.':'.$udom} = { 'newuser' => 1, };
                    581:     if ($tocheck eq 'username') {
                    582:         $checks = { 'username' => 1 };
                    583:     }
                    584:     &Apache::loncommon::user_rule_check($checkhash,$checks,
                    585:            $alerts,$rulematch,$inst_results,$curr_rules,
                    586:            $got_rules);
                    587:     return;
                    588: }
                    589: 
1.1       raeburn   590: sub send_token {
1.3       raeburn   591:     my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid) = @_;
1.14      raeburn   592:     my $msg = '<h3>'.&mt('Account creation status').'</h3>'.
                    593:               &mt('Thank you for your request to create a new LON-CAPA account.').
                    594:               '<br /><br />';
1.1       raeburn   595:     my $now = time;
                    596:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    597:                 'time'       => $now,
                    598:                 'domain'     => $domain,
1.3       raeburn   599:                 'username'   => $email,
                    600:                 'courseid'   => $courseid);
1.1       raeburn   601:     my $token = &Apache::lonnet::tmpput(\%info,$server);
                    602:     if ($token !~ /^error/ && $token ne 'no_such_host') {
                    603:         my $esc_token = &escape($token);
1.28      raeburn   604:         my $showtime = localtime(time);
                    605:         my $mailmsg = &mt('A request was submitted on [_1] for creation of a LON-CAPA account at the following institution: [_2].',$showtime,$domdesc).' '.
                    606:              &mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',
                    607:                  &Apache::lonnet::absolute_url().'/adm/createaccount?token='.$esc_token);
1.1       raeburn   608:         my $result = &Apache::resetpw::send_mail($domdesc,$email,$mailmsg,$contact_name,
                    609:                                                  $contact_email);
                    610:         if ($result eq 'ok') {
                    611:             $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>');
                    612:         } else {
1.14      raeburn   613:             $msg .= '<span class="LC_error">'.
                    614:                     &mt('An error occurred when sending a message to the e-mail address you provided.').'</span><br />'.
                    615:                     ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   616:         }
                    617:     } else {
1.14      raeburn   618:         $msg .= '<span class="LC_error">'.
                    619:                 &mt('An error occurred creating a token required for the account creation process.').'</span><br />'.
                    620:                 ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   621:     }
                    622:     return $msg;
                    623: }
                    624: 
                    625: sub process_mailtoken {
1.3       raeburn   626:     my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,
                    627:         $include,$start_page) = @_;
                    628:     my ($msg,$nostart,$noend);
1.1       raeburn   629:     my %data = &Apache::lonnet::tmpget($token);
                    630:     my $now = time;
                    631:     if (keys(%data) == 0) {
1.9       raeburn   632:         $msg = &mt('Sorry, the URL you provided to complete creation of a new LON-CAPA account was invalid.')
                    633:                .' '.&mt('Either the token included in the URL has been deleted or the URL you provided was invalid.')
                    634:                .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link page included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.1       raeburn   635:         return $msg;
                    636:     }
                    637:     if (($data{'time'} =~ /^\d+$/) &&
                    638:         ($data{'domain'} ne '') &&
                    639:         ($data{'username'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
                    640:         if ($now - $data{'time'} < 7200) {
                    641:             if ($env{'form.phase'} eq 'createaccount') {
1.3       raeburn   642:                 my ($result,$output) = &create_account($r,$domain,$lonhost,
1.1       raeburn   643:                                                        $data{'username'},$domdesc);
                    644:                 if ($result eq 'ok') {
                    645:                     $msg = $output; 
1.17      raeburn   646:                     my $shownow = &Apache::lonlocal::locallocaltime($now);
1.38      bisitz    647:                     my $mailmsg = &mt('A LON-CAPA account for the institution: [_1] has been created [_2] from IP address: [_3]. If you did not perform this action or authorize it, please contact the [_4] ([_5]).',$domdesc,$shownow,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n";
1.1       raeburn   648:                     my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'},
                    649:                                                                  $mailmsg,$contact_name,
                    650:                                                                  $contact_email);
                    651:                     if ($mailresult eq 'ok') {
                    652:                         $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'});
                    653:                     } else {
                    654:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
                    655:                     }
1.3       raeburn   656:                     my %form = &start_session($r,$data{'username'},$domain, 
                    657:                                               $lonhost,$data{'courseid'},
                    658:                                               $token);
                    659:                     $nostart = 1;
                    660:                     $noend = 1;
1.1       raeburn   661:                 } else {
1.7       bisitz    662:                     $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.')
                    663:                            .'<br />'.$output
                    664: #                           .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
                    665:                            .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   666:                 }
1.3       raeburn   667:                 my $delete = &Apache::lonnet::tmpdel($token);
1.1       raeburn   668:             } else {
1.3       raeburn   669:                 $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 />';
                    670:                 $msg .= &print_dataentry_form($r,$domain,$lonhost,$include,$token,$now,$data{'username'},$start_page);
                    671:                 $nostart = 1;
1.1       raeburn   672:             }
                    673:         } else {
1.7       bisitz    674:             $msg = &mt('Sorry, the token generated when you requested creation of an account has expired.')
                    675:                   .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.4       raeburn   676:             }
1.1       raeburn   677:     } else {
1.7       bisitz    678:         $msg .= &mt('Sorry, the URL generated when you requested creation of an account contained incomplete information.')
                    679:                .' '.&mt('Please submit a [_1]new request[_2] for account creation and follow the new link included in the e-mail that will be sent to you.','<a href="/adm/createaccount">','</a>');
1.1       raeburn   680:     }
1.3       raeburn   681:     return ($msg,$nostart,$noend);
                    682: }
                    683: 
                    684: sub start_session {
                    685:     my ($r,$username,$domain,$lonhost,$courseid,$token) = @_;
                    686:     my %form = (
                    687:                 uname => $username,
                    688:                 udom  => $domain,
                    689:                );
                    690:     my $firsturl = '/adm/roles';
                    691:     if (defined($courseid)) {
                    692:         $courseid = &validate_course($courseid);
                    693:         if ($courseid ne '') {
                    694:             $form{'courseid'} = $courseid;
1.22      raeburn   695:             $firsturl = '/adm/selfenroll?courseid='.$courseid;
1.3       raeburn   696:         }
                    697:     }
                    698:     if ($r->dir_config('lonBalancer') eq 'yes') {
                    699:         &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
                    700:                                   $lonhost,'noredirect',undef,\%form);
1.19      raeburn   701:         if ($token ne '') { 
                    702:             my $delete = &Apache::lonnet::tmpdel($token);
                    703:         }
1.3       raeburn   704:         $r->internal_redirect('/adm/switchserver');
                    705:     } else {
                    706:         &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
                    707:                                   $lonhost,$firsturl,undef,\%form);
                    708:     }
                    709:     return %form;
1.1       raeburn   710: }
                    711: 
1.3       raeburn   712: 
1.1       raeburn   713: sub print_dataentry_form {
1.3       raeburn   714:     my ($r,$domain,$lonhost,$include,$mailtoken,$now,$username,$start_page) = @_;
1.1       raeburn   715:     my ($error,$output);
1.3       raeburn   716:     &print_header($r,$start_page);
1.1       raeburn   717:     if (open(my $jsh,"<$include/londes.js")) {
                    718:         while(my $line = <$jsh>) {
                    719:             $r->print($line);
                    720:         }
                    721:         close($jsh);
1.3       raeburn   722:         $output .= &javascript_setforms($now)."\n".&javascript_checkpass($now);
1.1       raeburn   723:         my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    724:         my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
                    725:         my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount',
                    726:                                            $lonhost);
1.32      raeburn   727:         my $formtag = '<form name="server" method="post" target="_top" action="/adm/createaccount">';
                    728:         my ($datatable,$rowcount) =
                    729:             &Apache::loncreateuser::personal_data_display($username,$domain,
                    730:                                                           'email','selfcreate');
                    731:         if ($rowcount) {
                    732:             $output .= '<div class="LC_left_float">'.$formtag.$datatable;
                    733:         } else {
                    734:             $output .= $formtag;
1.1       raeburn   735:         }
                    736:         $output .= <<"ENDSERVERFORM";
                    737:    <input type="hidden" name="logtoken" value="$logtoken" />
                    738:    <input type="hidden" name="token" value="$mailtoken" />
                    739:    <input type="hidden" name="serverid" value="$lonhost" />
                    740:    <input type="hidden" name="uname" value="" />
                    741:    <input type="hidden" name="upass" value="" />
1.32      raeburn   742:    <input type="hidden" name="udom" value="" />
1.1       raeburn   743:    <input type="hidden" name="phase" value="createaccount" />
1.32      raeburn   744:   </form>
1.1       raeburn   745: ENDSERVERFORM
1.32      raeburn   746:         if ($rowcount) {
                    747:             $output .= '</div>'.
                    748:                        '<div class="LC_left_float">';
                    749:         }
1.1       raeburn   750:         my $upassone = '<input type="password" name="upass'.$now.'" size="10" />';
                    751:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="10" />';
                    752:         my $submit_text = &mt('Create LON-CAPA account');
1.32      raeburn   753:         $output .= '<h3>'.&mt('Login Data').'</h3>'."\n".
                    754:                    '<form name="client" method="post" '.
                    755:                    'onsubmit="return checkpass();">'."\n".
1.1       raeburn   756:                    &Apache::lonhtmlcommon::start_pick_box()."\n".
                    757:                    &Apache::lonhtmlcommon::row_title(&mt('Username'),
1.32      raeburn   758:                                                      'LC_pick_box_title',
                    759:                                                      'LC_oddrow_value')."\n".
1.1       raeburn   760:                    $username."\n".
                    761:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    762:                    &Apache::lonhtmlcommon::row_title(&mt('Password'),
1.32      raeburn   763:                                                     'LC_pick_box_title',
                    764:                                                     'LC_oddrow_value')."\n".
1.1       raeburn   765:                    $upassone."\n".
                    766:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    767:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password'),
1.32      raeburn   768:                                                      'LC_pick_box_title',
                    769:                                                      'LC_oddrow_value')."\n".
                    770:                    $upasstwo.
                    771:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    772:                    &Apache::lonhtmlcommon::row_title()."\n".
                    773:                    '<br /><input type="submit" name="createaccount" value="'.
                    774:                    $submit_text.'" />'.
1.1       raeburn   775:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    776:                    &Apache::lonhtmlcommon::end_pick_box()."\n".
1.32      raeburn   777:                    '<input type="hidden" name="uname" value="'.$username.'" />'."\n".
                    778:                    '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
                    779:                    '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
                    780:                    '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
                    781:                    '</form>';
                    782:         if ($rowcount) {
                    783:             $output .= '</div>'."\n".
                    784:                        '<div class="LC_clear_float_footer"></div>'."\n";
                    785:         }
1.1       raeburn   786:     } else {
1.7       bisitz    787:         $output = &mt('Could not load javascript file [_1]','<tt>londes.js</tt>');
1.1       raeburn   788:     }
1.3       raeburn   789:     return $output;
1.1       raeburn   790: }
                    791: 
1.35      raeburn   792: sub get_creation_controls {
                    793:     my ($domain,$usercreation) = @_;
                    794:     my (@cancreate,@statustocreate);
                    795:     if (ref($usercreation) eq 'HASH') {
                    796:         if (ref($usercreation->{'cancreate'}) eq 'HASH') {
                    797:             if (ref($usercreation->{'cancreate'}{'statustocreate'}) eq 'ARRAY') {
                    798:                 @statustocreate = @{$usercreation->{'cancreate'}{'statustocreate'}};
                    799:             } else {
                    800:                 @statustocreate = ('default');
                    801:                 my ($othertitle,$usertypes,$types) =
                    802:                     &Apache::loncommon::sorted_inst_types($domain);
                    803:                 if (ref($types) eq 'ARRAY') {
                    804:                     push(@statustocreate,@{$types});
                    805:                 }
                    806:             }
                    807:             if (ref($usercreation->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                    808:                 @cancreate = @{$usercreation->{'cancreate'}{'selfcreate'}};
                    809:             } elsif (($usercreation->{'cancreate'}{'selfcreate'} ne 'none') &&
                    810:                      ($usercreation->{'cancreate'}{'selfcreate'} ne '')) {
                    811:                 @cancreate = ($usercreation->{'cancreate'}{'selfcreate'});
                    812:             }
                    813:         }
                    814:     }
                    815:     return (\@cancreate,\@statustocreate);
                    816: }
                    817: 
1.1       raeburn   818: sub create_account {
1.3       raeburn   819:     my ($r,$domain,$lonhost,$username,$domdesc) = @_;
1.1       raeburn   820:     my ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    821:                                                           $env{'form.serverid'}); 
                    822:     # Error messages
1.7       bisitz    823:     my $error     = '<span class="LC_error">'.&mt('Error:').' ';
1.1       raeburn   824:     my $end       = '</span><br /><br />';
                    825:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                    826:                     &mt('Return to previous page').'</a>'.
                    827:                     &Apache::loncommon::end_page();
                    828:     if ($retrieved eq 'ok') {
1.22      raeburn   829:         if ($env{'form.courseid'} ne '') {
1.1       raeburn   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:     } else {
                    838:         return ('fail',$error.$output.$end.$rtnlink);
                    839:     }
                    840:     # Call modifyuser
                    841:     my $result = 
                    842:         &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
                    843:                                     'internal',$upass,$env{'form.cfirstname'},
                    844:                                     $env{'form.cmiddlename'},$env{'form.clastname'},
                    845:                                     $env{'form.cgeneration'},undef,undef,$username);
1.7       bisitz    846:     $output = &mt('Generating user: [_1]',$result);
1.1       raeburn   847:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
1.7       bisitz    848:     $output .= '<br />'.&mt('Home server: [_1]',$uhome).' '.
1.1       raeburn   849:               &Apache::lonnet::hostname($uhome).'<br /><br />';
                    850:     return ('ok',$output);
                    851: }
                    852: 
                    853: sub username_validation {
1.19      raeburn   854:     my ($r,$username,$domain,$domdesc,$contact_name,$contact_email,$courseid,
1.35      raeburn   855:         $lonhost,$statustocreate) = @_;
1.1       raeburn   856:     my ($retrieved,$output,$upass);
                    857: 
                    858:     $username= &LONCAPA::clean_username($username);
                    859:     $domain = &LONCAPA::clean_domain($domain);
                    860:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
                    861: 
                    862:     ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    863:                                                        $env{'form.serverid'});
1.19      raeburn   864:     if ($retrieved ne 'ok') {
                    865:         return ('fail',$output);
                    866:     }
                    867:     if ($uhome ne 'no_host') {
                    868:         my $result = &Apache::lonnet::authenticate($username,$upass,$domain);
                    869:         if ($result ne 'no_host') { 
                    870:             my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
                    871:             $output = '<br /><br />'.&mt('A LON-CAPA account already exists for username [_1] at this institution ([_2]).','<tt>'.$username.'</tt>',$domdesc).'<br />'.&mt('The password entered was also correct so you have been logged in.');
                    872:             return ('existingaccount',$output);
                    873:         } else {
1.22      raeburn   874:             $output = &login_failure_msg($courseid);
1.19      raeburn   875:         }
                    876:     } else {
1.1       raeburn   877:         my $primlibserv = &Apache::lonnet::domain($domain,'primary');
                    878:         my $authok;
                    879:         my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    880:         if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
                    881:             my $checkdefauth = 1;
                    882:             $authok = 
                    883:                 &Apache::lonnet::reply("encrypt:auth:$domain:$username:$upass:$checkdefauth",$primlibserv);
                    884:         } else {
                    885:             $authok = 'non_authorized';
                    886:         }
                    887:         if ($authok eq 'authorized') {
1.18      raeburn   888:             $output = &username_check($username,$domain,$domdesc,$courseid,$lonhost,
1.35      raeburn   889:                                       $contact_email,$contact_name,undef,
                    890:                                       $statustocreate);
1.4       raeburn   891:         } else {
1.22      raeburn   892:             $output = &login_failure_msg($courseid);
1.4       raeburn   893:         }
                    894:     }
1.19      raeburn   895:     return ('ok',$output);
1.4       raeburn   896: }
                    897: 
1.22      raeburn   898: sub login_failure_msg {
                    899:     my ($courseid) = @_;
                    900:     my $url;
                    901:     if ($courseid ne '') {
                    902:         $url = "/adm/selfenroll?courseid=".$courseid;
                    903:     } else {
                    904:         $url = "/adm/createaccount";
                    905:     }
                    906:     my $output = '<h4>'.&mt('Authentication failed').'</h4><div class="LC_warning">'.
                    907:                  &mt('Username and/or password could not be authenticated.').
                    908:                  '</div>'.
                    909:                  &mt('Please check the username and password.').'<br /><br />';
                    910:                  '<a href="'.$url.'">'.&mt('Try again').'</a>';
                    911:     return $output;
                    912: }
                    913: 
1.4       raeburn   914: sub username_check {
1.35      raeburn   915:     my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email,
                    916:         $contact_name,$sso_logout,$statustocreate) = @_;
1.23      raeburn   917:     my (%rulematch,%inst_results,$checkfail,$rowcount,$editable,$output,$msg,
1.18      raeburn   918:         %alerts,%curr_rules,%got_rules);
1.23      raeburn   919:     &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1.40      raeburn   920:                     \%inst_results,\%curr_rules,\%got_rules,'username');
1.4       raeburn   921:     if (ref($alerts{'username'}) eq 'HASH') {
                    922:         if (ref($alerts{'username'}{$domain}) eq 'HASH') {
                    923:             if ($alerts{'username'}{$domain}{$username}) {
                    924:                 if (ref($curr_rules{$domain}) eq 'HASH') {
1.18      raeburn   925:                     $output =
1.17      raeburn   926:                         &Apache::loncommon::instrule_disallow_msg('username',$domdesc,1,
                    927:                                                                   'selfcreate').
1.4       raeburn   928:                         &Apache::loncommon::user_rule_formats($domain,$domdesc,
                    929:                                 $curr_rules{$domain}{'username'},'username');
1.1       raeburn   930:                 }
1.18      raeburn   931:                 $checkfail = 'username';
1.1       raeburn   932:             }
1.4       raeburn   933:         }
                    934:     }
1.18      raeburn   935:     if (!$checkfail) {
1.35      raeburn   936:         if (ref($statustocreate) eq 'ARRAY') {
                    937:             $checkfail = 'inststatus';
                    938:             if (ref($inst_results{$username.':'.$domain}{inststatus}) eq 'ARRAY') {
                    939:                 foreach my $inststatus (@{$inst_results{$username.':'.$domain}{inststatus}}) {
                    940:                     if (grep(/^\Q$inststatus\E$/,@{$statustocreate})) {
                    941:                         undef($checkfail);
                    942:                         last;
                    943:                     }
                    944:                 }
                    945:             } elsif (grep(/^default$/,@{$statustocreate})) {
                    946:                 undef($checkfail);
                    947:             }
                    948:         }
                    949:     }
                    950:     if (!$checkfail) {
1.18      raeburn   951:         $output = '<form method="post" action="/adm/createaccount">';
                    952:         (my $datatable,$rowcount,$editable) = 
                    953:             &Apache::loncreateuser::personal_data_display($username,$domain,1,'selfcreate',
                    954:                                                          $inst_results{$username.':'.$domain});
                    955:         if ($rowcount > 0) {
                    956:             $output .= $datatable;
                    957:         }
                    958:         $output .=  '<br /><br /><input type="hidden" name="uname" value="'.$username.'" />'."\n".
                    959:                     '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
                    960:                     '<input type="hidden" name="phase" value="username_activation" />';
                    961:         my $now = time;
                    962:         my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    963:                     'time'       => $now,
                    964:                     'domain'     => $domain,
                    965:                     'username'   => $username);
                    966:         my $authtoken = &Apache::lonnet::tmpput(\%info,$lonhost);
                    967:         if ($authtoken !~ /^error/ && $authtoken ne 'no_such_host') {
                    968:             $output .= '<input type="hidden" name="authtoken" value="'.&HTML::Entities::encode($authtoken,'&<>"').'" />';
                    969:         } else {
                    970:             $output = &mt('An error occurred when storing a token').'<br />'.
                    971:                       &mt('You will not be able to proceed to the next stage of account creation').
                    972:                       &linkto_email_help($contact_email,$domdesc);
                    973:             $checkfail = 'authtoken';
                    974:         }
                    975:     }
                    976:     if ($checkfail) { 
                    977:         $msg = '<h4>'.&mt('Account creation unavailable').'</h4>';
                    978:         if ($checkfail eq 'username') {
                    979:             $msg .= '<span class="LC_warning">'.
                    980:                      &mt('A LON-CAPA account may not be created with the username you use.').
                    981:                      '</span><br /><br />'.$output;
                    982:         } elsif ($checkfail eq 'authtoken') {
                    983:             $msg .= '<span class="LC_error">'.&mt('Error creating token.').'</span>'.
                    984:                     '<br />'.$output;
1.35      raeburn   985:         } elsif ($checkfail eq 'inststatus') {
                    986:             $msg .= '<span class="LC_warning">'.
                    987:                      &mt('You are not permitted to create a LON-CAPA account.').
                    988:                      '</span><br /><br />'.$output;
1.18      raeburn   989:         }
                    990:         $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',
                    991:                 $contact_name,$contact_email).'<br /><hr />'.
                    992:                 $sso_logout;
                    993:         &Apache::lonnet::logthis("ERROR: failure type of '$checkfail' when performing username check to create account for authenticated user: $username, in domain $domain");
1.8       raeburn   994:     } else {
1.18      raeburn   995:         if ($courseid ne '') {
                    996:             $output .= '<input type="hidden" name="courseid" value="'.$courseid.'" />';
                    997:         }
                    998:         $output .= '<input type="submit" name="newaccount" value="'.
                    999:                    &mt('Create LON-CAPA account').'" /></form>';
                   1000:         if ($rowcount) {
                   1001:             if ($editable) {
1.22      raeburn  1002:                 if ($courseid ne '') { 
                   1003:                     $msg = '<h4>'.&mt('User information').'</h4>';
                   1004:                 }
                   1005:                 $msg .= &mt('To create one, use the table below to provide information about yourself, then click the [_1]Create LON-CAPA account[_2] button.','<span class="LC_cusr_emph">','</span>').'<br />';
1.18      raeburn  1006:             } else {
1.22      raeburn  1007:                  if ($courseid ne '') {
                   1008:                      $msg = '<h4>'.&mt('Review user information').'</h4>';
                   1009:                  }
                   1010:                  $msg .= &mt('A user account will be created with information displayed in the table below, when you click the [_1]Create LON-CAPA account[_2] button.','<span class="LC_cusr_emph">','</span>').'<br />';
1.18      raeburn  1011:             }
                   1012:         } else {
1.22      raeburn  1013:             if ($courseid ne '') {
                   1014:                 $msg = '<h4>'.&mt('Confirmation').'</h4>';
                   1015:             }
                   1016:             $msg .= &mt('Confirm that you wish to create an account.');
1.18      raeburn  1017:         }
                   1018:         $msg .= $output;
                   1019:     }
                   1020:     return $msg;
1.1       raeburn  1021: }
                   1022: 
                   1023: sub username_activation {
1.3       raeburn  1024:     my ($r,$username,$domain,$domdesc,$lonhost,$courseid) = @_;
1.1       raeburn  1025:     my $output;
1.7       bisitz   1026:     my $error     = '<span class="LC_error">'.&mt('Error:').' ';
1.1       raeburn  1027:     my $end       = '</span><br /><br />';
                   1028:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                   1029:                     &mt('Return to previous page').'</a>'.
                   1030:                     &Apache::loncommon::end_page();
                   1031:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1.8       raeburn  1032:     my %data = &Apache::lonnet::tmpget($env{'form.authtoken'});
                   1033:     my $now = time;
                   1034:     my $earlyout;
                   1035:     my $timeout = 300;
                   1036:     if (keys(%data) == 0) {
                   1037:         $output = &mt('Sorry, your authentication has expired.');
                   1038:         $earlyout = 'fail';
                   1039:     }
                   1040:     if (($data{'time'} !~ /^\d+$/) ||
                   1041:         ($data{'domain'} ne $domain) || 
                   1042:         ($data{'username'} ne $username)) {
                   1043:         $earlyout = 'fail';
                   1044:         $output = &mt('The credentials you provided could not be verified.');   
                   1045:     } elsif ($now - $data{'time'} > $timeout) {
                   1046:         $earlyout = 'fail';
                   1047:         $output = &mt('Sorry, your authentication has expired.');
                   1048:     }
                   1049:     if ($earlyout ne '') {
                   1050:         $output .= '<br />'.&mt('Please [_1]start again[_2].','<a href="/adm/createaccount">','</a>');
                   1051:         return($earlyout,$output);
                   1052:     }
1.3       raeburn  1053:     if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && 
                   1054:          ($domdefaults{'auth_arg_def'} ne '')) || 
                   1055:         ($domdefaults{'auth_def'} eq 'localauth')) {
1.22      raeburn  1056:         if ($env{'form.courseid'} ne '') {
1.1       raeburn  1057:             my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
                   1058:             if ($result eq 'fail') {
                   1059:                 $output = $error.&mt('Invalid ID format').$end.
                   1060:                           $userchkmsg.$rtnlink;
                   1061:                 return ('fail',$output);
                   1062:             }
                   1063:         }
                   1064:         # Call modifyuser
1.23      raeburn  1065:         my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts,%info);
                   1066:         &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1.40      raeburn  1067:                         \%inst_results,\%curr_rules,\%got_rules);
1.23      raeburn  1068:         my @userinfo = ('firstname','middlename','lastname','generation',
                   1069:                         'permanentemail','id');
                   1070:         my %canmodify = 
                   1071:             &Apache::loncreateuser::selfcreate_canmodify('selfcreate',$domain,
                   1072:                                                          \@userinfo,\%inst_results);
                   1073:         foreach my $item (@userinfo) {
                   1074:             if ($canmodify{$item}) {
                   1075:                 $info{$item} = $env{'form.c'.$item};
                   1076:             } else {
                   1077:                 $info{$item} = $inst_results{$username.':'.$domain}{$item}; 
                   1078:             }
                   1079:         }
                   1080:         if (ref($inst_results{$username.':'.$domain}{'inststatus'}) eq 'ARRAY') {
                   1081:             my @inststatuses = @{$inst_results{$username.':'.$domain}{'inststatus'}};
                   1082:             $info{'inststatus'} = join(':',map { &escape($_); } @inststatuses);
                   1083:         }
1.1       raeburn  1084:         my $result =
1.23      raeburn  1085:             &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
1.1       raeburn  1086:                           $domdefaults{'auth_def'},
1.23      raeburn  1087:                           $domdefaults{'auth_arg_def'},$info{'firstname'},
                   1088:                           $info{'middlename'},$info{'lastname'},
                   1089:                           $info{'generation'},undef,undef,
                   1090:                           $info{'permanentemail'},$info{'inststatus'});
1.3       raeburn  1091:         if ($result eq 'ok') {
1.8       raeburn  1092:             my $delete = &Apache::lonnet::tmpdel($env{'form.authtoken'});
1.3       raeburn  1093:             $output = &mt('A LON-CAPA account has been created for username: [_1] in domain: [_2].',$username,$domain);
                   1094:             my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
                   1095:             my $nostart = 1;
                   1096:             return ('ok',$output,$nostart);
                   1097:         } else {
                   1098:             $output = &mt('Account creation failed for username: [_1] in domain: [_2].',$username,$domain).'<br /><span class="LC_error">'.&mt('Error: [_1]',$result).'</span>';
                   1099:             return ('fail',$output);
                   1100:         }
1.1       raeburn  1101:     } else {
1.7       bisitz   1102:         $output = &mt('User account creation is not available for the current default authentication type.')."\n";
1.1       raeburn  1103:         return('fail',$output);
                   1104:     }
                   1105: }
                   1106: 
                   1107: sub check_id {
                   1108:     my ($username,$domain,$domdesc) = @_;
                   1109:     # Check ID format
                   1110:     my (%alerts,%rulematch,%inst_results,%curr_rules,%checkhash);
                   1111:     my %checks = ('id' => 1);
                   1112:     %{$checkhash{$username.':'.$domain}} = (
                   1113:                                             'newuser' => 1,
                   1114:                                             'id' => $env{'form.cid'},
                   1115:                                            );
                   1116:     &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   1117:                                         \%rulematch,\%inst_results,\%curr_rules);
                   1118:     if (ref($alerts{'id'}) eq 'HASH') {
                   1119:         if (ref($alerts{'id'}{$domain}) eq 'HASH') {
                   1120:             if ($alerts{'id'}{$domain}{$env{'form.cid'}}) {
                   1121:                 my $userchkmsg;
                   1122:                 if (ref($curr_rules{$domain}) eq 'HASH') {
                   1123:                     $userchkmsg  =
                   1124:                         &Apache::loncommon::instrule_disallow_msg('id',
                   1125:                                                            $domdesc,1).
                   1126:                         &Apache::loncommon::user_rule_formats($domain,
                   1127:                               $domdesc,$curr_rules{$domain}{'id'},'id');
                   1128:                 }
                   1129:                 return ('fail',$userchkmsg);
                   1130:             }
                   1131:         }
                   1132:     }
                   1133:     return; 
                   1134: }
                   1135: 
                   1136: sub invalid_state {
                   1137:     my ($error,$domdesc,$contact_name,$contact_email,$msgtext) = @_;
1.14      raeburn  1138:     my $msg = '<h3>'.&mt('Account creation unavailable').'</h3><span class="LC_error">';
1.1       raeburn  1139:     if ($error eq 'baduseremail') {
                   1140:         $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                   1141:     } elsif ($error eq 'existinguser') {
1.16      raeburn  1142:         $msg = &mt('The e-mail address you provided is already in use as a username in LON-CAPA at this institution.');
1.1       raeburn  1143:     } elsif ($error eq 'userrules') {
1.25      bisitz   1144:         $msg = &mt('Username rules at this institution do not allow the e-mail address you provided to be used as a username.');
1.1       raeburn  1145:     } elsif ($error eq 'userformat') {
1.16      raeburn  1146:         $msg = &mt('The e-mail address you provided may not be used as a username at this LON-CAPA institution.');
1.1       raeburn  1147:     } elsif ($error eq 'captcha') {
                   1148:         $msg = &mt('Validation of the code your entered failed.');
                   1149:     } elsif ($error eq 'noemails') {
1.16      raeburn  1150:         $msg = &mt('Creation of a new user account using an e-mail address as username is not permitted at this LON-CAPA institution.');
1.1       raeburn  1151:     }
1.14      raeburn  1152:     $msg .= '</span>';
1.1       raeburn  1153:     if ($msgtext) {
                   1154:         $msg .= '<br />'.$msgtext;
                   1155:     }
1.8       raeburn  1156:     $msg .= &linkto_email_help($contact_email,$domdesc);
                   1157:     return $msg;
                   1158: }
                   1159: 
                   1160: sub linkto_email_help {
                   1161:     my ($contact_email,$domdesc) = @_;
                   1162:     my $msg;
1.1       raeburn  1163:     if ($contact_email ne '') {
                   1164:         my $escuri = &HTML::Entities::encode('/adm/createaccount','&<>"');
1.17      raeburn  1165:         $msg .= '<br />'.&mt('You may wish to contact the [_1]LON-CAPA helpdesk[_2] for [_3].','<a href="/adm/helpdesk?origurl='.$escuri.'">','</a>',$domdesc).'<br />';
1.1       raeburn  1166:     } else {
1.17      raeburn  1167:         $msg .= '<br />'.&mt('You may wish to send an e-mail to the server administrator: [_1] for [_2].',$Apache::lonnet::perlvar{'AdminEmail'},$domdesc).'<br />';
1.1       raeburn  1168:     }
                   1169:     return $msg;
                   1170: }
                   1171: 
                   1172: sub create_captcha {
                   1173:     my ($output_dir,$db_dir) = @_;
                   1174:     my %captcha_params = &captcha_settings();
1.28      raeburn  1175:     my ($output,$maxtries,$tries) = ('',10,0);
                   1176:     while ($tries < $maxtries) {
                   1177:         $tries ++;
                   1178:         my $captcha = Authen::Captcha->new (
                   1179:                                            output_folder => $captcha_params{'output_dir'},
                   1180:                                            data_folder   => $captcha_params{'db_dir'},
                   1181:                                           );
                   1182:         my $md5sum = $captcha->generate_code($captcha_params{'numchars'});
                   1183: 
                   1184:         if (-e $Apache::lonnet::perlvar{'lonCaptchaDir'}.'/'.$md5sum.'.png') {
                   1185:             $output = '<input type="hidden" name="crypt" value="'.$md5sum.'" />'."\n".
                   1186:                       &mt('Type in the letters/numbers shown below').'&nbsp;'.
                   1187:                      '<input type="text" size="5" name="code" value="" /><br />'.
1.36      bisitz   1188:                      '<img src="'.$captcha_params{'www_output_dir'}.'/'.$md5sum.'.png" />';
1.28      raeburn  1189:             last;
                   1190:         }
                   1191:     }
1.1       raeburn  1192:     return $output;
                   1193: }
                   1194: 
                   1195: sub captcha_settings {
                   1196:     my %captcha_params = ( 
1.13      raeburn  1197:                            output_dir     => $Apache::lonnet::perlvar{'lonCaptchaDir'},
                   1198:                            www_output_dir => "/captchaspool",
                   1199:                            db_dir         => $Apache::lonnet::perlvar{'lonCaptchaDb'},
1.1       raeburn  1200:                            numchars       => '5',
                   1201:                          );
                   1202:     return %captcha_params;
                   1203: }
                   1204: 
                   1205: sub getkeys {
                   1206:     my ($lkey,$ukey) = @_;
                   1207:     my $lextkey=hex($lkey);
                   1208:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
                   1209: 
                   1210:     my $uextkey=hex($ukey);
                   1211:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
                   1212:     return ($lextkey,$uextkey);
                   1213: }
                   1214: 
                   1215: sub serverform {
1.20      raeburn  1216:     my ($logtoken,$lonhost,$mailtoken,$courseid,$context) = @_;
1.22      raeburn  1217:     my $phase = 'username_validation';
                   1218:     my $catalog_elements;
1.20      raeburn  1219:     if ($context eq 'selfenroll') {
                   1220:         $phase = 'selfenroll_login';
                   1221:     }
1.22      raeburn  1222:     if ($courseid ne '') {
                   1223:         $catalog_elements = &Apache::lonhtmlcommon::echo_form_input(['courseid','phase']);
                   1224:     } 
                   1225:     my $output = <<ENDSERVERFORM;
1.20      raeburn  1226:   <form name="server" method="post" action="/adm/createaccount">
1.1       raeburn  1227:    <input type="hidden" name="logtoken" value="$logtoken" />
                   1228:    <input type="hidden" name="token" value="$mailtoken" />
                   1229:    <input type="hidden" name="serverid" value="$lonhost" />
                   1230:    <input type="hidden" name="uname" value="" />
                   1231:    <input type="hidden" name="upass" value="" />
1.32      raeburn  1232:    <input type="hidden" name="udom" value="" />
1.20      raeburn  1233:    <input type="hidden" name="phase" value="$phase" />
1.3       raeburn  1234:    <input type="hidden" name="courseid" value="$courseid" />
1.22      raeburn  1235:    $catalog_elements
1.1       raeburn  1236:   </form>
                   1237: ENDSERVERFORM
                   1238:     return $output;
                   1239: }
                   1240: 
                   1241: sub process_credentials {
                   1242:     my ($logtoken,$lonhost) = @_;
                   1243:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
                   1244:     my ($retrieved,$output,$upass);
                   1245:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.7       bisitz   1246:         $output = &mt('Information needed to verify your login information is missing, inaccessible or expired.')
                   1247:                  .'<br />'.&mt('You may need to reload the previous page to obtain a new token.');
1.1       raeburn  1248:         return ($retrieved,$output,$upass); 
                   1249:     } else {
                   1250:         my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$lonhost);
                   1251:         if ($reply eq 'ok') {
                   1252:             $retrieved = 'ok';
                   1253:         } else {
                   1254:             $output = &mt('Session could not be opened.');
                   1255:         }
                   1256:     }
                   1257:     my ($key,$caller)=split(/&/,$tmpinfo);
                   1258:     if ($caller eq 'createaccount') {
                   1259:         $upass = &Apache::lonpreferences::des_decrypt($key,$env{'form.upass'});
                   1260:     } else {
                   1261:         $output = &mt('Unable to retrieve your log-in information - unexpected context');
                   1262:     }
                   1263:     return ($retrieved,$output,$upass);
                   1264: }
                   1265: 
                   1266: sub guest_format_check {
                   1267:     my ($useremail,$domain,$cancreate,$settings) = @_;
                   1268:     my ($login,$format_match,$format_msg,@user_rules);
                   1269:     if (ref($settings) eq 'HASH') {
                   1270:         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
                   1271:             push(@user_rules,@{$settings->{'email_rule'}});
                   1272:         }
                   1273:     }
                   1274:     if (@user_rules > 0) {
                   1275:         my %rule_check = 
                   1276:             &Apache::lonnet::inst_rulecheck($domain,$useremail,undef,
1.2       raeburn  1277:                                             'selfcreate',\@user_rules);
1.1       raeburn  1278:         if (keys(%rule_check) > 0) {
                   1279:             foreach my $item (keys(%rule_check)) {
                   1280:                 if ($rule_check{$item}) {
                   1281:                     $format_match = 1;   
                   1282:                     last;
                   1283:                 }
                   1284:             }
                   1285:         }
                   1286:     }
                   1287:     if ($format_match) {
                   1288:         ($login) = ($useremail =~ /^([^\@]+)\@/);
                   1289:         $format_msg = '<br />'.&mt("Your e-mail address uses the same internet domain as your institution's LON-CAPA service.").'<br />'.&mt('Creation of a LON-CAPA account with this type of e-mail address as username is not permitted.').'<br />';
1.5       raeburn  1290:         if (ref($cancreate) eq 'ARRAY') {
                   1291:             if (grep(/^login$/,@{$cancreate})) {
1.7       bisitz   1292:                 $format_msg .= &mt('You should request creation of a LON-CAPA account for a log-in ID of "[_1]" at your institution instead.',$login).'<br />'; 
1.5       raeburn  1293:             }
1.1       raeburn  1294:         }
                   1295:     }
                   1296:     return $format_msg;
                   1297: }
                   1298: 
1.17      raeburn  1299: sub sso_logout_frag {
                   1300:     my ($r,$domain) = @_;
                   1301:     my $endsessionmsg;
                   1302:     if (defined($r->dir_config('lonSSOUserLogoutMessageFile_'.$domain))) {
                   1303:         my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile_'.$domain);
                   1304:         if (-e $msgfile) {
                   1305:             open(my $fh,"<$msgfile");
                   1306:             $endsessionmsg = join('',<$fh>);
                   1307:             close($fh);
                   1308:         }
                   1309:     } elsif (defined($r->dir_config('lonSSOUserLogoutMessageFile'))) {
                   1310:         my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile');
                   1311:         if (-e $msgfile) {     
                   1312:             open(my $fh,"<$msgfile");
                   1313:             $endsessionmsg = join('',<$fh>);
                   1314:             close($fh);
                   1315:         }
                   1316:     }
                   1317:     return $endsessionmsg;
                   1318: }
                   1319: 
1.22      raeburn  1320: sub catreturn_js {
                   1321:     return  <<"ENDSCRIPT";
                   1322: <script type="text/javascript">
                   1323: 
                   1324: function ToSelfenroll(formname) {
                   1325:     var formidx = getFormByName(formname);
                   1326:     if (formidx > -1) {
                   1327:         document.forms[formidx].action = '/adm/selfenroll';
                   1328:         numidx = getIndexByName(formidx,'phase');
                   1329:         if (numidx > -1) {
                   1330:             document.forms[formidx].elements[numidx].value = '';   
                   1331:         }
                   1332:         numidx = getIndexByName(formidx,'context');
                   1333:         if (numidx > -1) {
                   1334:             document.forms[formidx].elements[numidx].value = '';
                   1335:         }
                   1336:     }
                   1337:     document.forms[formidx].submit();
                   1338: }
                   1339: 
                   1340: function ToCatalog(formname,caller) {
                   1341:     var formidx = getFormByName(formname);
                   1342:     if (formidx > -1) {
                   1343:         document.forms[formidx].action = '/adm/coursecatalog';
                   1344:         numidx = getIndexByName(formidx,'coursenum');
                   1345:         if (numidx > -1) {
                   1346:             if (caller != 'details') {
                   1347:                 document.forms[formidx].elements[numidx].value = '';
                   1348:             }
                   1349:         }
                   1350:     }
                   1351:     document.forms[formidx].submit();
                   1352: }
                   1353: 
                   1354: function getIndexByName(formidx,item) {
                   1355:     for (var i=0;i<document.forms[formidx].elements.length;i++) {
                   1356:         if (document.forms[formidx].elements[i].name == item) {
                   1357:             return i;
                   1358:         }
                   1359:     }
                   1360:     return -1;
                   1361: }
                   1362: 
                   1363: function getFormByName(item) {
                   1364:     for (var i=0; i<document.forms.length; i++) {
                   1365:         if (document.forms[i].name == item) {
                   1366:             return i;
                   1367:         }
                   1368:     }
                   1369:     return -1;
                   1370: }
                   1371: 
                   1372: </script>
                   1373: ENDSCRIPT
                   1374: 
                   1375: }
                   1376: 
1.1       raeburn  1377: 1;

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