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

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

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