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

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

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