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

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

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