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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow visitors to create a user account with the username being either an 
1.58      raeburn     3: # institutional log-in ID (institutional authentication required - localauth,
                      4: # kerberos, or SSO) or an e-mail address. Requests to use an e-mail address as
                      5: # username may be processed automatically, or may be queued for approval.
1.1       raeburn     6: #
1.72.4.7! raeburn     7: # $Id: createaccount.pm,v 1.72.4.6 2021/01/04 04:42:13 raeburn Exp $
1.1       raeburn     8: #
                      9: # Copyright Michigan State University Board of Trustees
                     10: #
                     11: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                     12: #
                     13: # LON-CAPA is free software; you can redistribute it and/or modify
                     14: # it under the terms of the GNU General Public License as published by
                     15: # the Free Software Foundation; either version 2 of the License, or
                     16: # (at your option) any later version.
                     17: #
                     18: # LON-CAPA is distributed in the hope that it will be useful,
                     19: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     20: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     21: # GNU General Public License for more details.
                     22: #
                     23: # You should have received a copy of the GNU General Public License
                     24: # along with LON-CAPA; if not, write to the Free Software
                     25: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     26: #
                     27: # /home/httpd/html/adm/gpl.txt
                     28: #
                     29: # http://www.lon-capa.org/
                     30: #
                     31: #
                     32: package Apache::createaccount;
                     33: 
                     34: use strict;
                     35: use Apache::Constants qw(:common);
                     36: use Apache::lonacc;
                     37: use Apache::lonnet;
                     38: use Apache::loncommon;
1.12      raeburn    39: use Apache::lonhtmlcommon;
1.72.4.3  raeburn    40: use Apache::lonuserutils;
1.1       raeburn    41: use Apache::lonlocal;
1.3       raeburn    42: use Apache::lonauth;
1.1       raeburn    43: use Apache::resetpw;
                     44: use DynaLoader; # for Crypt::DES version
                     45: use Crypt::DES;
1.3       raeburn    46: use LONCAPA qw(:DEFAULT :match);
1.8       raeburn    47: use HTML::Entities;
1.1       raeburn    48: 
                     49: sub handler {
                     50:     my $r = shift;
                     51:     &Apache::loncommon::content_type($r,'text/html');
                     52:     $r->send_http_header;
                     53:     if ($r->header_only) {
                     54:         return OK;
                     55:     }
1.22      raeburn    56: 
1.5       raeburn    57:     my $domain;
1.3       raeburn    58: 
1.57      raeburn    59:     my $sso_username = $r->subprocess_env->get('SSOUserUnknown');
                     60:     my $sso_domain = $r->subprocess_env->get('SSOUserDomain');
1.5       raeburn    61: 
1.56      raeburn    62:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.61      raeburn    63:                                             ['token','courseid','domain','type']);
1.27      raeburn    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.56      raeburn    70:         ($domain, undef) = Apache::lonnet::is_course($env{'form.courseid'});
                     71:         unless ($domain) {
                     72:             if ($env{'form.phase'} =~ /^username_(activation|validation)$/) {
                     73:                 if (($env{'form.udom'} =~ /^$match_domain$/) &&
                     74:                     (&Apache::lonnet::domain($env{'form.udom'}) ne '')) {
                     75:                     $domain = $env{'form.udom'};
                     76:                 } else {
                     77:                     $domain = &Apache::lonnet::default_login_domain();
                     78:                 }
                     79:             } elsif (($env{'form.phase'} eq '') &&
                     80:                      ($env{'form.domain'} =~ /^$match_domain$/) &&
                     81:                      (&Apache::lonnet::domain($env{'form.domain'}) ne '')) {
                     82:                 $domain = $env{'form.domain'};
                     83:             } else {
                     84:                 $domain = &Apache::lonnet::default_login_domain();
                     85:             }
                     86:         }
1.5       raeburn    87:     }
1.1       raeburn    88:     my $domdesc = &Apache::lonnet::domain($domain,'description');
                     89:     my $contact_name = &mt('LON-CAPA helpdesk');
1.15      raeburn    90:     my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
                     91:     my $contacts =
                     92:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
                     93:                                                  $domain,$origmail);
                     94:     my ($contact_email) = split(',',$contacts);
1.1       raeburn    95:     my $lonhost = $r->dir_config('lonHostID');
                     96:     my $include = $r->dir_config('lonIncludes');
1.4       raeburn    97:     my $start_page;
1.3       raeburn    98: 
                     99:     my $handle = &Apache::lonnet::check_for_valid_session($r);
1.30      raeburn   100:     if (($handle ne '') && ($handle !~ /^publicuser_\d+$/)) {
1.4       raeburn   101:         $start_page =
1.3       raeburn   102:             &Apache::loncommon::start_page('Already logged in');
                    103:         my $end_page =
                    104:             &Apache::loncommon::end_page();
                    105:         $r->print($start_page."\n".'<h2>'.&mt('You are already logged in').'</h2>'.
1.58      raeburn   106:                   '<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',
                    107:                             '<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>').
1.6       bisitz    108:                   '</p><p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'.$end_page);
1.20      raeburn   109:         return OK;
                    110:     }
                    111: 
1.22      raeburn   112:     my ($js,$courseid,$title);
1.48      droeschl  113:     $courseid = Apache::lonnet::is_course($env{'form.courseid'});
1.22      raeburn   114:     if ($courseid ne '') {
                    115:         $js = &catreturn_js();
                    116:         $title = 'Self-enroll in a LON-CAPA course';
                    117:     } else {
                    118:         $title = 'Create a user account in LON-CAPA';
                    119:     }
1.20      raeburn   120:     if ($env{'form.phase'} eq 'selfenroll_login') {
1.22      raeburn   121:         $title = 'Self-enroll in a LON-CAPA course';
1.4       raeburn   122:         if ($env{'form.udom'} ne '') {
                    123:             $domain = $env{'form.udom'};
                    124:         }
1.35      raeburn   125: 
                    126:         my %domconfig = 
                    127:             &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
1.72.4.2  raeburn   128:         my ($cancreate,$statustocreate) = 
1.35      raeburn   129:             &get_creation_controls($domain,$domconfig{'usercreation'});
                    130: 
1.20      raeburn   131:         my ($result,$output) =
                    132:             &username_validation($r,$env{'form.uname'},$domain,$domdesc,
                    133:                                  $contact_name,$contact_email,$courseid,
1.35      raeburn   134:                                  $lonhost,$statustocreate);
1.58      raeburn   135:         if ($result eq 'redirect') {
                    136:             $r->internal_redirect('/adm/switchserver');
                    137:             return OK;
                    138:         } elsif ($result eq 'existingaccount') {
1.20      raeburn   139:             $r->print($output);
1.22      raeburn   140:             &print_footer($r);
1.20      raeburn   141:             return OK;
                    142:         } else {
1.51      raeburn   143:             $start_page = &Apache::loncommon::start_page($title,$js); 
1.22      raeburn   144:             &print_header($r,$start_page,$courseid);
                    145:             $r->print($output);
                    146:             &print_footer($r);    
1.20      raeburn   147:             return OK;
                    148:         }
1.4       raeburn   149:     }
1.3       raeburn   150: 
1.72.4.2  raeburn   151:     my %domconfig =
1.35      raeburn   152:         &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
1.72.4.2  raeburn   153:     my ($cancreate,$statustocreate,$statusforemail,$emailusername,
                    154:         $emailoptions,$verification,$emaildomain,$types,$usertypes,$othertitle) =
1.58      raeburn   155:         &get_creation_controls($domain,$domconfig{'usercreation'});
1.72.4.2  raeburn   156:     my ($additems,$pagetitle);
                    157:     if (ref($cancreate) eq 'ARRAY') {
                    158:         unless (($env{'form.token'}) || ($sso_username ne '') || ($env{'form.phase'}) ||
                    159:                 ($env{'form.create_with_email'})) {
                    160:             if ((grep(/^email$/,@{$cancreate})) && (ref($statusforemail) eq 'ARRAY')) {
                    161:                 my $usertype = &get_usertype($domain);
                    162:                 if ((($usertype eq '') || (!grep(/^\Q$usertype\E$/,@{$statusforemail}))) && 
                    163:                     (@{$statusforemail} > 0)) {
                    164:                     $js .= &setelements_js($statusforemail,$types,$usertypes,$othertitle);
                    165:                     $additems = {'add_entries' => { 'onload' => "setElements();"} };
                    166:                     if ((@{$cancreate} == 1) && (@{$statusforemail} > 0)) {
                    167:                         $pagetitle = 'Select affiliation';
                    168:                     }
                    169:                 } else {
                    170:                     $js .= &username_js();
                    171:                 }
                    172:             }
                    173:         }
                    174:     }
                    175:     $start_page = &Apache::loncommon::start_page($title,$js,$additems);
1.35      raeburn   176:     if (@{$cancreate} == 0) {
1.72.4.2  raeburn   177:         &print_header($r,$start_page,$courseid,$pagetitle);
1.14      raeburn   178:         my $output = '<h3>'.&mt('Account creation unavailable').'</h3>'.
                    179:                      '<span class="LC_warning">'.
1.72.4.2  raeburn   180:                      &mt('Creation of a new user account using an institutional log-in ID or e-mail verification is not permitted for: [_1].',$domdesc).
1.58      raeburn   181:                      '</span><br /><br />';
1.3       raeburn   182:         $r->print($output);
1.22      raeburn   183:         &print_footer($r);
1.3       raeburn   184:         return OK;
                    185:     }
                    186: 
1.5       raeburn   187:     if ($sso_username ne '') {
1.22      raeburn   188:         &print_header($r,$start_page,$courseid);
1.18      raeburn   189:         my ($msg,$sso_logout);
                    190:         $sso_logout = &sso_logout_frag($r,$domain);
1.35      raeburn   191:         if (grep(/^sso$/,@{$cancreate})) {
1.14      raeburn   192:             $msg = '<h3>'.&mt('Account creation').'</h3>'.
1.17      raeburn   193:                    &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 />';
1.65      raeburn   194:             my $shibenv;
                    195:             if (($r->dir_config('lonOtherAuthen') eq 'yes') && 
                    196:                 ($r->dir_config('lonOtherAuthenType') eq 'Shibboleth')) {
                    197:                 if (ref($domconfig{'usercreation'}) eq 'HASH') {
                    198:                     if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
                    199:                         if (ref($domconfig{'usercreation'}{'cancreate'}{'shibenv'}) eq 'HASH') {
                    200:                             my @possfields = ('firstname','middlename','lastname','generation',
                    201:                                               'permanentemail','id');
                    202:                             $shibenv= {};
                    203:                             foreach my $key (keys(%{$domconfig{'usercreation'}{'cancreate'}{'shibenv'}})) {
                    204:                                 if ($key eq 'inststatus') {
                    205:                                     if (ref($usertypes) eq 'HASH') {
                    206:                                         if ($domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key} ne '') {
                    207:                                             if (exists($usertypes->{$domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key}})) {
                    208:                                                 $shibenv->{$key} = $domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key};
                    209:                                              }
                    210:                                         }
                    211:                                     }
                    212:                                 } elsif (grep(/^\Q$key\E/,@possfields)) {
                    213:                                     if ($domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key} ne '') {
                    214:                                         $shibenv->{$key} = $domconfig{'usercreation'}{'cancreate'}{'shibenv'}{$key};
                    215:                                     }
                    216:                                 }
                    217:                             }
                    218:                         }
                    219:                     }
                    220:                 }
                    221:             }
1.18      raeburn   222:             $msg .= &username_check($sso_username,$domain,$domdesc,$courseid, 
1.35      raeburn   223:                                     $lonhost,$contact_email,$contact_name,
1.65      raeburn   224:                                     $sso_logout,$statustocreate,$shibenv);
1.5       raeburn   225:         } else {
1.17      raeburn   226:             $msg = '<h3>'.&mt('Account creation unavailable').'</h3>'.
                    227:                    '<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   228:                    $sso_logout;
1.5       raeburn   229:         }
1.17      raeburn   230:         $r->print($msg);
1.22      raeburn   231:         &print_footer($r);
1.5       raeburn   232:         return OK;
                    233:     }
                    234: 
1.58      raeburn   235:     my ($output,$nostart,$noend,$redirect);
1.3       raeburn   236:     my $token = $env{'form.token'};
                    237:     if ($token) {
1.58      raeburn   238:         ($output,$nostart,$noend,$redirect) = 
1.3       raeburn   239:             &process_mailtoken($r,$token,$contact_name,$contact_email,$domain,
1.58      raeburn   240:                                $domdesc,$lonhost,$include,$start_page,$cancreate,
1.72.4.2  raeburn   241:                                $domconfig{'usercreation'},$types);
1.58      raeburn   242:         if ($redirect) {
                    243:             $r->internal_redirect('/adm/switchserver');
                    244:             return OK;
                    245:         } elsif ($nostart) {
1.3       raeburn   246:             if ($noend) {
                    247:                 return OK;
                    248:             } else {
                    249:                 $r->print($output);
1.22      raeburn   250:                 &print_footer($r);
1.3       raeburn   251:                 return OK;
                    252:             }
                    253:         } else {
1.22      raeburn   254:             &print_header($r,$start_page,$courseid);
1.3       raeburn   255:             $r->print($output);
1.22      raeburn   256:             &print_footer($r);
1.3       raeburn   257:             return OK;
                    258:         }
                    259:     }
1.72.4.2  raeburn   260:     my ($usernameset,$condition,$excluded,$hascustom);
                    261:     if ((grep(/^email$/,@{$cancreate})) && (($env{'form.create_with_email'}) ||
                    262:                                             ((!$token) && ($env{'form.phase'} eq '')))) {
                    263:         my $usertype = &get_usertype($domain);
                    264:         if ($usertype eq '') {
                    265:             $usertype = 'default';
                    266:         }
                    267:         if (ref($verification) eq 'HASH') {
                    268:             if ($verification->{$usertype} =~ /^(free|first)$/) {
                    269:                 $usernameset = $verification->{$usertype};
                    270:             }
                    271:         }
                    272:         if (ref($emailoptions) eq 'HASH') {
                    273:             if ($emailoptions->{$usertype} =~ /^(inst|noninst)$/) {
                    274:                 my $chosen = $1;
                    275:                 if (ref($emaildomain) eq 'HASH') {
                    276:                     if (ref($emaildomain->{$usertype}) eq 'HASH') {
                    277:                         if ($chosen eq 'inst') {
                    278:                             $condition = $emaildomain->{$usertype}->{$chosen};
                    279:                         } else {
                    280:                             $excluded = $emaildomain->{$usertype}->{$chosen};
                    281:                         }
                    282:                     }
                    283:                 }
                    284:             } elsif ($emailoptions->{$usertype} eq 'custom') {
                    285:                 $hascustom = 1;
                    286:             }
                    287:         }
                    288:     }
1.3       raeburn   289:     if ($env{'form.phase'} eq 'username_activation') {
                    290:         (my $result,$output,$nostart) = 
                    291:             &username_activation($r,$env{'form.uname'},$domain,$domdesc,
1.51      raeburn   292:                                  $courseid);
1.58      raeburn   293:         if ($result eq 'redirect') {
                    294:             $r->internal_redirect('/adm/switchserver');
                    295:             return OK; 
                    296:         } elsif ($result eq 'ok') {
1.3       raeburn   297:             if ($nostart) {
                    298:                 return OK;
                    299:             }
                    300:         }
1.22      raeburn   301:         &print_header($r,$start_page,$courseid);
1.3       raeburn   302:         $r->print($output);
1.22      raeburn   303:         &print_footer($r);
1.3       raeburn   304:         return OK;
1.19      raeburn   305:     } elsif ($env{'form.phase'} eq 'username_validation') { 
                    306:         (my $result,$output) = 
                    307:             &username_validation($r,$env{'form.uname'},$domain,$domdesc,
                    308:                                  $contact_name,$contact_email,$courseid,
1.35      raeburn   309:                                  $lonhost,$statustocreate);
1.19      raeburn   310:         if ($result eq 'existingaccount') {
                    311:             $r->print($output);
1.22      raeburn   312:             &print_footer($r);
1.19      raeburn   313:             return OK;
                    314:         } else {
1.22      raeburn   315:             &print_header($r,$start_page,$courseid);
1.19      raeburn   316:         }
                    317:     } elsif ($env{'form.create_with_email'}) {
1.22      raeburn   318:         &print_header($r,$start_page,$courseid);
1.61      raeburn   319:         my $usertype = &get_usertype($domain);
1.72.4.2  raeburn   320:         if ($usertype eq '') {
                    321:             $usertype = 'default';
                    322:         }
1.58      raeburn   323:         $output = &process_email_request($env{'form.uname'},$domain,$domdesc,
1.35      raeburn   324:                                          $contact_name,$contact_email,$cancreate,
1.3       raeburn   325:                                          $lonhost,$domconfig{'usercreation'},
1.72.4.2  raeburn   326:                                          $emailusername,$courseid,$usertype,
                    327:                                          $usernameset,$condition,$excluded,$hascustom);
1.3       raeburn   328:     } elsif (!$token) {
1.72.4.2  raeburn   329:         &print_header($r,$start_page,$courseid,$pagetitle);
1.1       raeburn   330:         my $now=time;
1.68      raeburn   331:         if ((grep(/^login$/,@{$cancreate})) && (!grep(/^email$/,@{$cancreate}))) {
1.72.4.1  raeburn   332:             if (open(my $jsh,"<","$include/londes.js")) {
1.58      raeburn   333:                 while(my $line = <$jsh>) {
                    334:                     $r->print($line);
                    335:                 }
                    336:                 close($jsh);
                    337:                 $r->print(&javascript_setforms($now));
                    338:             }
1.1       raeburn   339:         }
1.64      raeburn   340:         if (grep(/^email$/,@{$cancreate})) {
1.72.4.2  raeburn   341:             $r->print(&javascript_validmail($condition));
1.12      raeburn   342:         }
1.61      raeburn   343:         my $usertype = &get_usertype($domain);
1.58      raeburn   344:         $output = &print_username_form($r,$domain,$domdesc,$cancreate,$now,$lonhost,
1.72.4.2  raeburn   345:                                        $include,$courseid,$emailusername,
                    346:                                        $statusforemail,$usernameset,$condition,
                    347:                                        $excluded,$usertype,$types,$usertypes,$othertitle);
1.1       raeburn   348:     }
                    349:     $r->print($output);
1.22      raeburn   350:     &print_footer($r);
1.1       raeburn   351:     return OK;
                    352: }
                    353: 
1.3       raeburn   354: sub print_header {
1.72.4.2  raeburn   355:     my ($r,$start_page,$courseid,$pagetitle) = @_;
1.3       raeburn   356:     $r->print($start_page);
                    357:     &Apache::lonhtmlcommon::clear_breadcrumbs();
1.72.4.2  raeburn   358:     my $url = '/adm/createaccount';
                    359:     if ($pagetitle eq '') {
                    360:         $pagetitle = 'New username';
                    361:     }
1.22      raeburn   362:     if ($courseid ne '') {
                    363:         my %coursehash = &Apache::lonnet::coursedescription($courseid);
                    364:         &selfenroll_crumbs($r,$courseid,$coursehash{'description'});
                    365:     }
1.72.4.2  raeburn   366:     if ($env{'form.reportedtype'}) {
                    367:         &Apache::lonhtmlcommon::add_breadcrumb
                    368:         ({href=>$url,
                    369:           text=>"Select affiliation"});
                    370:     }
1.3       raeburn   371:     &Apache::lonhtmlcommon::add_breadcrumb
1.72.4.2  raeburn   372:     ({href=>$url,
                    373:       text=>$pagetitle});
1.3       raeburn   374:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Create account'));
                    375:     return;
                    376: }
                    377: 
1.22      raeburn   378: sub print_footer {
                    379:     my ($r) = @_;
                    380:     if ($env{'form.courseid'} ne '') {
                    381:         $r->print('<form name="backupcrumbs" method="post" action="">'.
                    382:                   &Apache::lonhtmlcommon::echo_form_input(['backto','logtoken',
                    383:                       'token','serverid','uname','upass','phase','create_with_email',
1.72      raeburn   384:                       'code','crypt','cfirstname','clastname','g-recaptcha-response',
1.59      raeburn   385:                       'recaptcha_challenge_field','recaptcha_response_field',
1.23      raeburn   386:                       'cmiddlename','cgeneration','cpermanentemail','cid']).
1.22      raeburn   387:                   '</form>');
                    388:     }
                    389:     $r->print(&Apache::loncommon::end_page());
                    390: }
                    391: 
1.61      raeburn   392: sub get_usertype {
                    393:     my ($domain) = @_;
1.72.4.2  raeburn   394:     my $usertype;
1.61      raeburn   395:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($domain);
                    396:     if (ref($types) eq 'ARRAY') {
                    397:         push(@{$types},'default');
                    398:         my $posstype = $env{'form.type'};
                    399:         $posstype =~ s/^\s+|\s$//g;
                    400:         if (grep(/^\Q$posstype\E$/,@{$types})) {
                    401:             $usertype = $posstype;
                    402:         }
                    403:     }
                    404:     return $usertype;
                    405: }
                    406: 
1.22      raeburn   407: sub selfenroll_crumbs {
                    408:     my ($r,$courseid,$desc) = @_;
                    409:     &Apache::lonhtmlcommon::add_breadcrumb
                    410:          ({href=>"javascript:ToCatalog('backupcrumbs','')",
1.37      bisitz    411:            text=>"Course/Community Catalog"});
1.22      raeburn   412:     if ($env{'form.coursenum'} ne '') {
                    413:         &Apache::lonhtmlcommon::add_breadcrumb
                    414:           ({href=>"javascript:ToCatalog('backupcrumbs','details')",
                    415:             text=>"Course details"});
                    416:     }
                    417:     my $last_crumb;
                    418:     if ($desc ne '') {
1.62      raeburn   419:         $last_crumb = &mt("Self-enroll in [_1]","'$desc'");
1.22      raeburn   420:     } else {
                    421:         $last_crumb = &mt('Self-enroll');
                    422:     }
                    423:     &Apache::lonhtmlcommon::add_breadcrumb
                    424:                    ({href=>"javascript:ToSelfenroll('backupcrumbs')",
                    425:                      text=>$last_crumb,
                    426:                      no_mt=>"1"});
                    427:     return;
                    428: }
                    429: 
1.1       raeburn   430: sub javascript_setforms {
1.72.4.2  raeburn   431:     my ($now,$emailusername,$captcha,$usertype,$recaptchaversion,$usernameset,$condition,$excluded) =  @_;
1.61      raeburn   432:     my ($setuserinfo,@required,$requiredchk);
1.58      raeburn   433:     if (ref($emailusername) eq 'HASH') {
1.61      raeburn   434:         if (ref($emailusername->{$usertype}) eq 'HASH') {  
                    435:             foreach my $key (sort(keys(%{$emailusername->{$usertype}}))) {
                    436:                 if ($emailusername->{$usertype}{$key} eq 'required') {
                    437:                     push(@required,$key); 
                    438:                 }
                    439:                 $setuserinfo .= '                    server.elements.'.$key.'.value=client.elements.'.$key.'.value;'."\n";
                    440:             }
1.72.4.7! raeburn   441:             if ($usertype ne '') {
        !           442:                 $setuserinfo .= '                    server.elements.type.value=client.elements.type.value;'."\n";
        !           443:             }
1.58      raeburn   444:         }
1.59      raeburn   445:         if ($captcha eq 'original') {
1.61      raeburn   446:             $setuserinfo .= '                    server.elements.code.value=client.elements.code.value;'."\n".
                    447:                             '                    server.elements.crypt.value=client.elements.crypt.value;'."\n";
1.59      raeburn   448:         } elsif ($captcha eq 'recaptcha') {
1.72      raeburn   449:             if ($recaptchaversion ne '2') {
                    450:                 $setuserinfo .=
1.61      raeburn   451:                 '                    server.elements.recaptcha_challenge_field.value=client.elements.recaptcha_challenge_field.value;'."\n".
                    452:                 '                    server.elements.recaptcha_response_field.value=client.elements.recaptcha_response_field.value;'."\n";
1.72      raeburn   453:             }
1.59      raeburn   454:         }
1.72.4.2  raeburn   455:         if ($usernameset eq 'free') {
                    456:             $setuserinfo .=
                    457:                 '                    server.elements.username.value=client.elements.username.value;'."\n";
                    458:         }
1.58      raeburn   459:     }
1.61      raeburn   460:     if (@required) {
                    461:         my $missprompt = &mt('One or more required fields are currently blank.');
1.70      damieng   462:         &js_escape(\$missprompt);
1.61      raeburn   463:         my $reqstr = join("','",@required);
                    464:         $requiredchk = <<"ENDCHK";
                    465:                 var requiredfields = new Array('$reqstr');
                    466:                 missing = 0; 
                    467:                 for (var i=0; i<requiredfields.length; i++) {
                    468:                     try {
                    469:                         eval("client.elements."+requiredfields[i]+".value");
                    470:                     }
                    471:                     catch(err) {
                    472:                         continue;
                    473:                     }
                    474:                     if (eval("client.elements."+requiredfields[i]+".value") == '') {
                    475:                         missing ++;
                    476:                     }
                    477:                 }
                    478:                 if (missing > 0) {
                    479:                     alert("$missprompt");
                    480:                     return false;
                    481:                 }
                    482: 
                    483: ENDCHK
                    484:     }
1.1       raeburn   485:     my $js = <<ENDSCRIPT;
1.58      raeburn   486: <script type="text/javascript">
                    487: // <![CDATA[
                    488:     function send(one,two,context) {
                    489:         var server;
                    490:         var client;
                    491:         if (document.forms[one]) {
                    492:             server = document.forms[one];
                    493:             if (document.forms[two]) {
                    494:                 client = document.forms[two];
1.61      raeburn   495: $requiredchk
1.58      raeburn   496:                 server.elements.uname.value = client.elements.uname.value;
                    497:                 server.elements.udom.value = client.elements.udom.value;
1.1       raeburn   498: 
1.58      raeburn   499:                 uextkey=client.elements.uextkey.value;
                    500:                 lextkey=client.elements.lextkey.value;
                    501:                 initkeys();
                    502:                 server.elements.upass.value
1.71      raeburn   503:                     = getCrypted(client.elements.upass$now.value);
1.58      raeburn   504:                 client.elements.uname.value='';
                    505:                 client.elements.upass$now.value='';
1.60      raeburn   506:                 if (context == 'email') {
1.61      raeburn   507: $setuserinfo
1.60      raeburn   508:                     client.elements.upasscheck$now.value='';
                    509:                 }
1.58      raeburn   510:                 server.submit();
                    511:             }
                    512:         }
1.1       raeburn   513:         return false;
                    514:     }
1.71      raeburn   515: 
1.58      raeburn   516: // ]]>
                    517: </script>
1.1       raeburn   518: ENDSCRIPT
1.72      raeburn   519:     if (($captcha eq 'recaptcha') && ($recaptchaversion eq '2')) {
                    520:         $js .= "\n".'<script src="https://www.google.com/recaptcha/api.js"></script>'."\n";
                    521:     }
1.1       raeburn   522:     return $js;
                    523: }
                    524: 
                    525: sub javascript_checkpass {
1.72.4.3  raeburn   526:     my ($now,$context,$domain) = @_;
1.7       bisitz    527:     my $nopass = &mt('You must enter a password.');
1.70      damieng   528:     my $mismatchpass = &mt('The passwords you entered did not match.')."\n".
1.1       raeburn   529:                        &mt('Please try again.'); 
1.72.4.3  raeburn   530:     my ($numrules,$intargjs) =
                    531:         &Apache::lonuserutils::passwd_validation_js('upass',$domain);
1.70      damieng   532:     &js_escape(\$nopass);
                    533:     &js_escape(\$mismatchpass);
1.1       raeburn   534:     my $js = <<"ENDSCRIPT";
1.58      raeburn   535: <script type="text/javascript">
                    536: // <![CDATA[
                    537:     function checkpass(one,two) {
                    538:         var client;
                    539:         if (document.forms[two]) {
                    540:             client = document.forms[two]; 
                    541:             var upass = client.elements.upass$now.value;
                    542:             var upasscheck = client.elements.upasscheck$now.value;
                    543:             if (upass == '') {
                    544:                 alert("$nopass");
                    545:                 return false;
                    546:             }
                    547:             if (upass == upasscheck) {
1.72.4.3  raeburn   548:                 var numrules = $numrules;
                    549:                 if (numrules > 0) {
                    550: $intargjs
                    551:                 }
1.58      raeburn   552:                 client.elements.upasscheck$now.value='';
                    553:                 if (validate_email(client)) {
                    554:                     send(one,two,'$context');
                    555:                 } 
                    556:                 return false;
                    557:             } else {
                    558:                 alert("$mismatchpass");
                    559:                 return false;
                    560:             }
1.32      raeburn   561:         }
1.58      raeburn   562:         return false; 
1.1       raeburn   563:     }
1.58      raeburn   564: // ]]>
1.1       raeburn   565: </script>
                    566: ENDSCRIPT
                    567:     return $js;
                    568: }
                    569: 
1.12      raeburn   570: sub javascript_validmail {
1.72.4.2  raeburn   571:     my ($condition) = @_;
1.70      damieng   572:     my %js_lt = &Apache::lonlocal::texthash (
1.12      raeburn   573:                email => 'The e-mail address you entered',
                    574:                notv  => 'is not a valid e-mail address',
1.72.4.4  raeburn   575:                avae  => 'A valid e-mail address is not formed when the value you entered is combined with the required domain',
1.12      raeburn   576:     );
                    577:     my $output =  "\n".'<script type="text/javascript">'."\n".
1.58      raeburn   578:                   '// <![CDATA['."\n".
1.12      raeburn   579:                   &Apache::lonhtmlcommon::javascript_valid_email()."\n";
1.70      damieng   580:     &js_escape(\%js_lt);
1.12      raeburn   581:     $output .= <<"ENDSCRIPT";
1.58      raeburn   582: function validate_email(client) {
                    583:     field = client.uname;
1.72.4.2  raeburn   584:     var condition = '$condition';
                    585:     if (validmail(field,condition) == false) {
                    586:         if ((condition != undefined) && (condition != '')) {
1.72.4.4  raeburn   587:             alert("$js_lt{'avae'}: "+condition);
1.72.4.2  raeburn   588:         } else {
                    589:             alert("$js_lt{'email'}: "+field.value+" $js_lt{'notv'}.");
                    590:         }
1.12      raeburn   591:         return false;
                    592:     }
                    593:     return true;
                    594: }
                    595: ENDSCRIPT
1.58      raeburn   596:     $output .= "\n".'// ]]>'."\n".'</script>'."\n";
1.12      raeburn   597:     return $output;
                    598: }
                    599: 
1.1       raeburn   600: sub print_username_form {
1.68      raeburn   601:     my ($r,$domain,$domdesc,$cancreate,$now,$lonhost,$include,$courseid,$emailusername,
1.72.4.2  raeburn   602:         $statusforemail,$usernameset,$condition,$excluded,$usertype,$types,$usertypes,
                    603:         $othertitle) = @_;
1.59      raeburn   604:     my %lt = &Apache::lonlocal::texthash (
                    605:                                          crac => 'Create account with a username provided by this institution',
                    606:                                          clca => 'Create LON-CAPA account',
                    607:                                          type => 'Type in your log-in ID and password to find out.',
                    608:                                          plse => 'Please provide a password for your new account.',
                    609:                                          info => 'Please provide user information and a password for your new account.',
                    610:                                          yopw => 'Your password will be encrypted when sent (and stored).',
1.72.4.2  raeburn   611:                                          crae => 'Create account using e-mail address verification',
1.59      raeburn   612:                                          );
1.1       raeburn   613:     my $output;
1.5       raeburn   614:     if (ref($cancreate) eq 'ARRAY') {
                    615:         if (grep(/^login$/,@{$cancreate})) {
                    616:             my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    617:             if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
1.59      raeburn   618:                 $output = '<div class="LC_left_float"><h3>'.$lt{'crac'}.'</h3>';
                    619:                 $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 />').
1.58      raeburn   620:                            '<br /><br />'.
1.59      raeburn   621:                            $lt{'type'}.
1.58      raeburn   622:                            '<br /><br />';
1.59      raeburn   623:                 $output .= &login_box($now,$lonhost,$courseid,$lt{'clca'},
1.22      raeburn   624:                                       $domain,'createaccount').'</div>';
1.5       raeburn   625:             }
                    626:         }
1.64      raeburn   627:         if (grep(/^email$/,@{$cancreate})) {
1.72.4.2  raeburn   628:             $output .= '<div class="LC_left_float"><h3>'.$lt{'crae'}.'</h3>';
                    629:             if ($usertype ne '') {
                    630:                 if ((ref($statusforemail) eq 'ARRAY') && (@{$statusforemail} > 0)) {
                    631:                     unless (grep(/^\Q$usertype\E$/,@{$statusforemail})) {
                    632:                         undef($usertype);
                    633:                     }
                    634:                 } elsif ($usertype ne 'default') {
                    635:                     undef($usertype);
                    636:                 }
                    637:             }
                    638:             if (($usertype eq '') && (ref($statusforemail) eq 'ARRAY') && 
                    639:                 (@{$statusforemail} > 0) && (ref($types) eq 'ARRAY') && (@{$types} > 0)) {
                    640:                 my @posstypes = @{$types};
                    641:                 unless (grep(/^default$/,@posstypes)) {
                    642:                     push(@posstypes,'default');
                    643:                 }
                    644:                 $output .= '<p>'.&mt('Choose your affiliation at [_1]',$domdesc).'</p>'."\n".
                    645:                            '<form name="reportstatus" id="LC_reportstatus" action="/adm/createaccount" method="post" '.
                    646:                            'onsubmit="return checkVerification();"><p>';
                    647:                 foreach my $type (@posstypes) {
                    648:                     my $name;
                    649:                     if ($type eq 'default') {
                    650:                         $name = $othertitle;
                    651:                     } else {
                    652:                         $name = $type;
                    653:                         if (ref($usertypes) eq 'HASH') {
                    654:                             if (exists($usertypes->{$type})) {
                    655:                                 $name = $usertypes->{$type};
                    656:                             }
                    657:                         }
                    658:                     }
                    659:                     my $checked;
                    660:                     if ($env{'form.type'} eq $type) {
                    661:                         $checked = ' checked="checked"';
                    662:                     }
                    663:                     $output .= '<label><input type="radio" name="type" value="'.$type.'"'.$checked.' />'.
                    664:                                $name.'</label>'.('&nbsp;'x2);
                    665:                 }
                    666:                 if ($env{'form.courseid'} =~ /^$match_domain\_$match_courseid$/) {
                    667:                     $output .= "\n".'<input type="hidden" name="courseid" value="'.$env{'form.courseid'}.'" />';
                    668:                 }
                    669:                 $output .= '</p>'."\n".'<p><input type="submit" name="reportedtype" value="'.&mt('Submit').'" /></p></form>';
1.52      raeburn   670:             } else {
1.72.4.2  raeburn   671:                 my ($captchaform,$error,$captcha,$recaptchaversion) = 
                    672:                     &Apache::loncommon::captcha_display('usercreation',$lonhost);
                    673:                 if ($error) {
                    674:                     my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
                    675:                     if ($courseid ne '') {
                    676:                         $helpdesk .= '&courseid='.$courseid;
                    677:                     }
                    678:                     $output .= '<span class="LC_error">'.
                    679:                                &mt('An error occurred generating the validation code[_1] required for use of an e-mail address to request a LON-CAPA account.','<br />').
                    680:                                '</span><br /><br />'.
                    681:                                &mt('[_1]Contact the helpdesk[_2] or [_3]reload[_2] the page and try again.',
                    682:                                    '<a href="'.$helpdesk.'">','</a>','<a href="javascript:window.location.reload()">');
1.28      raeburn   683:                 } else {
1.72.4.2  raeburn   684:                     if (grep(/^login$/,@{$cancreate})) {
                    685:                         $output .= &mt('If you do not have a log-in ID at your institution, [_1]provide your e-mail address to request a LON-CAPA account.','<br />').'<br /><br />'.
                    686:                                    $lt{'plse'}.'<br />'.
                    687:                                    $lt{'yopw'}.'<br />';
                    688:                     } else {
                    689:                         my $prompt = $lt{'plse'};
                    690:                         if (ref($emailusername) eq 'HASH') {
                    691:                             if (ref($emailusername->{$usertype}) eq 'HASH') {
                    692:                                 if (keys(%{$emailusername->{$usertype}}) > 0) {
                    693:                                     $prompt = $lt{'info'};
                    694:                                 }
1.59      raeburn   695:                             }
                    696:                         }
1.72.4.2  raeburn   697:                         $output .= $prompt.'<br />'.
                    698:                                    $lt{'yopw'}.'<br />';
                    699:                     }
                    700:                     if ($usertype eq '') {
                    701:                         $usertype = 'default';
                    702:                     } elsif (ref($usertypes) eq 'HASH') {
                    703:                         my $usertitle;
                    704:                         if ($usertype eq 'default') {
                    705:                             $usertitle = $othertitle;
                    706:                         } elsif (exists($usertypes->{$usertype})) {
                    707:                             $usertitle = $usertypes->{$usertype};
                    708:                         }
                    709:                         if ($usertitle ne '') {
                    710:                             $output .= &mt('Self-reported affiliation: [_1]',
                    711:                                            '<span style="font-style: italic;">'.$usertitle.'</span>').
                    712:                                        '<br />';
                    713:                         }
1.59      raeburn   714:                     }
1.72.4.2  raeburn   715:                     $output .= &print_dataentry_form($r,$domain,$lonhost,$include,$now,$captchaform,
                    716:                                                      $courseid,$emailusername,$captcha,$usertype,
                    717:                                                      $recaptchaversion,$usernameset,$condition,$excluded);
1.28      raeburn   718:                 }
1.5       raeburn   719:             }
1.28      raeburn   720:             $output .= '</div>';
1.3       raeburn   721:         }
1.1       raeburn   722:     }
                    723:     if ($output eq '') {
1.72.4.2  raeburn   724:         $output = &mt('Creation of a new LON-CAPA user account using an institutional log-in ID or verification by e-mail is not permitted at [_1].',$domdesc);
1.1       raeburn   725:     } else {
                    726:         $output .= '<div class="LC_clear_float_footer"></div>';
                    727:     }
                    728:     return $output;
                    729: }
                    730: 
1.20      raeburn   731: sub login_box {
                    732:     my ($now,$lonhost,$courseid,$submit_text,$domain,$context) = @_;
                    733:     my $output;
                    734:     my %titles = &Apache::lonlocal::texthash(
                    735:                                               createaccount => 'Log-in ID',
                    736:                                               selfenroll    => 'Username',
                    737:                                             );
1.58      raeburn   738:     my ($lkey,$ukey) = &Apache::loncommon::des_keys();
1.20      raeburn   739:     my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
1.41      raeburn   740:     my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount:createaccount',
1.20      raeburn   741:                                        $lonhost);
                    742:     $output = &serverform($logtoken,$lonhost,undef,$courseid,$context);
1.59      raeburn   743:     my $unameform = '<input type="text" name="uname" size="20" value="" autocomplete="off" />';
                    744:     my $upassform = '<input type="password" name="upass'.$now.'" size="20" autocomplete="off" />';
1.58      raeburn   745:     $output .= '<form name="client" method="post" action="" onsubmit="return(send('."'server','client'".'));">'."\n".
1.32      raeburn   746:                &Apache::lonhtmlcommon::start_pick_box()."\n".
                    747:                &Apache::lonhtmlcommon::row_title($titles{$context},
1.31      bisitz    748:                                                  'LC_pick_box_title')."\n".
                    749:                $unameform."\n".
                    750:                &Apache::lonhtmlcommon::row_closure(1)."\n".
                    751:                &Apache::lonhtmlcommon::row_title(&mt('Password'),
                    752:                                                 'LC_pick_box_title')."\n".
                    753:                $upassform;
1.20      raeburn   754:     if ($context eq 'selfenroll') {
1.32      raeburn   755:         my $udomform = '<input type="text" name="udom" size="10" value="'.
1.20      raeburn   756:                         $domain.'" />';
1.31      bisitz    757:         $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                    758:                    &Apache::lonhtmlcommon::row_title(&mt('Domain'),
1.20      raeburn   759:                                                      'LC_pick_box_title')."\n".
1.31      bisitz    760:                    $udomform."\n";
1.32      raeburn   761:     } else {
                    762:         $output .= '<input type="hidden" name="udom" value="'.$domain.'" />';
1.20      raeburn   763:     }
1.32      raeburn   764:     $output .= &Apache::lonhtmlcommon::row_closure(1).
1.31      bisitz    765:                &Apache::lonhtmlcommon::row_title().
1.32      raeburn   766:                '<br /><input type="submit" name="username_validation" value="'.
                    767:                $submit_text.'" />'."\n";
                    768:     if ($context eq 'selfenroll') {
                    769:         $output .= '<br /><br /><table width="100%"><tr><td align="right">'.
                    770:                    '<span class="LC_fontsize_medium">'.
                    771:                    '<a href="/adm/resetpw">'.&mt('Forgot password?').'</a>'.
                    772:                    '</span></td></tr></table>'."\n";
                    773:     }
                    774:     $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                    775:                &Apache::lonhtmlcommon::end_pick_box().'<br />'."\n";
1.34      bisitz    776:     $output .= '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
                    777:                '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
1.22      raeburn   778:                '</form>';
1.20      raeburn   779:     return $output;
                    780: }
                    781: 
1.1       raeburn   782: sub process_email_request {
                    783:     my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
1.72.4.2  raeburn   784:         $server,$settings,$emailusername,$courseid,$usertype,$usernameset,
                    785:         $condition,$excluded,$hascustom) = @_;
                    786:     my ($output,$uname);
1.5       raeburn   787:     if (ref($cancreate) eq 'ARRAY') {
1.64      raeburn   788:         if (!grep(/^email$/,@{$cancreate})) {
1.5       raeburn   789:             $output = &invalid_state('noemails',$domdesc,
                    790:                                      $contact_name,$contact_email);
                    791:             return $output;
1.72.4.2  raeburn   792:         } elsif ((($condition ne '') && ($useremail !~ /^[^\@]+$/)) ||
                    793:                  (($condition eq '') && ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/))) {
1.5       raeburn   794:             $output = &invalid_state('baduseremail',$domdesc,
1.1       raeburn   795:                                      $contact_name,$contact_email);
                    796:             return $output;
                    797:         } else {
1.58      raeburn   798:             $useremail =~ s/^\s+|\s+$//g;
1.72.4.2  raeburn   799:             my $possuname;
                    800:             if ($condition ne '') {
                    801:                 if ($usernameset eq 'first') {
                    802:                     $possuname = $useremail;
                    803:                 }
                    804:                 $useremail .= $condition;
                    805:             } elsif ($excluded ne '') {
                    806:                 if ($useremail =~ /^[^\@]+\Q$excluded\E$/) {
                    807:                     $output = &invalid_state('userrules',$domdesc,
                    808:                                      $contact_name,$contact_email);
                    809:                     return $output;
                    810:                 } 
                    811:             }
                    812:             if (($usernameset eq 'free') && ($env{'form.username'} ne '')) {
                    813:                 $possuname = $env{'form.username'};
                    814:             } elsif (($usernameset eq 'first') && ($condition eq '')) {
                    815:                 if ($condition eq '') {
                    816:                     ($possuname) = ($useremail =~ /^([^\@]+)\@/);
                    817:                 }
                    818:             }
                    819:             if ($possuname ne '') {
                    820:                 $possuname  =~ s/^\s+|\s+$//g;
                    821:                 if ($possuname ne '') {
                    822:                     $uname=&LONCAPA::clean_username($possuname);
                    823:                     if ($uname ne $possuname) {
                    824:                         $output = &invalid_state('badusername',$domdesc,
                    825:                                                  $contact_name,$contact_email);
                    826:                         return $output;
                    827:                     }
                    828:                 }
                    829:             }
                    830:             if ($possuname eq '') {
                    831:                 $uname=&LONCAPA::clean_username($useremail);
                    832:                 if ($useremail ne $uname) {
                    833:                     $output = &invalid_state('badusername',$domdesc,
                    834:                                              $contact_name,$contact_email);
                    835:                     return $output;
                    836:                 }
1.58      raeburn   837:             }
1.72.4.2  raeburn   838:             my $uhome = &Apache::lonnet::homeserver($uname,$domain);
1.5       raeburn   839:             if ($uhome ne 'no_host') {
                    840:                 $output = &invalid_state('existinguser',$domdesc,
                    841:                                          $contact_name,$contact_email);
1.1       raeburn   842:                 return $output;
1.5       raeburn   843:             } else {
1.52      raeburn   844:                 my ($captcha_chk,$captcha_error) = &Apache::loncommon::captcha_response('usercreation',$server);
1.5       raeburn   845:                 if ($captcha_chk != 1) {
1.72.4.3  raeburn   846:                     $output = '<span class="LC_warning">'.
                    847:                               &mt('Validation of the code you entered failed.').'</span>'.
                    848:                               '<br />'.$captcha_error."\n".'<br /><p>'.
                    849:                                &mt('[_1]Return[_2] to the previous page to try again.',
                    850:                                    '<a href="javascript:document.retryemail.submit();">','</a>')."\n".
                    851:                               '<form name="retryemail" action="/adm/createaccount" method="post" />'.
                    852:                               '<input type="hidden" name="domain" value="'.$domain.'" />'."\n";
                    853:                     if ($env{'form.courseid'} =~ /^$match_domain\_$match_courseid$/) {
                    854:                         $output .= '<input type="hidden" name="courseid" value="'.$env{'form.courseid'}.'" />'."\n";
                    855:                     }
                    856:                     if ($env{'form.type'}) {
                    857:                         my $usertype = &get_usertype($domain);
                    858:                         if ($usertype ne '') {
                    859:                             $output .= '<input type="hidden" name="type" value="'.$usertype.'" />'."\n".
                    860:                                        '<input type="hidden" name="reportedtype" value="'.&mt('Submit').'" />'."\n";
                    861:                         }
                    862:                     }
                    863:                     $output .= '</form></p>';
1.5       raeburn   864:                     return $output;
                    865:                 }
1.58      raeburn   866:                 my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
1.72.4.2  raeburn   867:                 &call_rulecheck($uname,$domain,\%alerts,\%rulematch,
1.58      raeburn   868:                                 \%inst_results,\%curr_rules,\%got_rules,'username');
                    869:                 if (ref($alerts{'username'}) eq 'HASH') {
                    870:                     if (ref($alerts{'username'}{$domain}) eq 'HASH') {
1.72.4.2  raeburn   871:                         if ($alerts{'username'}{$domain}{$uname}) {
1.58      raeburn   872:                             $output = &invalid_state('userrules',$domdesc,
                    873:                                                      $contact_name,$contact_email);
                    874:                             return $output;
1.1       raeburn   875:                         }
                    876:                     }
1.58      raeburn   877:                 }
1.72.4.2  raeburn   878:                 if ($hascustom) {
                    879:                     my $format_msg = 
                    880:                         &guest_format_check($useremail,$domain,$cancreate,
                    881:                                             $settings,$usertype);
                    882:                     if ($format_msg) {
                    883:                         $output = &invalid_state('userformat',$domdesc,$contact_name,
                    884:                                                  $contact_email,$format_msg);
                    885:                         return $output;
                    886:                     }
1.1       raeburn   887:                 }
                    888:             }
                    889:         }
1.5       raeburn   890:         $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
1.72.4.2  raeburn   891:                               $contact_email,$courseid,$emailusername,$usertype,
                    892:                               $uname);
1.1       raeburn   893:     }
                    894:     return $output;
                    895: }
                    896: 
1.23      raeburn   897: sub call_rulecheck {
                    898:     my ($uname,$udom,$alerts,$rulematch,$inst_results,$curr_rules,
                    899:         $got_rules,$tocheck) = @_;
                    900:     my ($checkhash,$checks);
                    901:     $checkhash->{$uname.':'.$udom} = { 'newuser' => 1, };
                    902:     if ($tocheck eq 'username') {
                    903:         $checks = { 'username' => 1 };
                    904:     }
                    905:     &Apache::loncommon::user_rule_check($checkhash,$checks,
                    906:            $alerts,$rulematch,$inst_results,$curr_rules,
                    907:            $got_rules);
                    908:     return;
                    909: }
                    910: 
1.1       raeburn   911: sub send_token {
1.61      raeburn   912:     my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid,$emailusername,
1.72.4.2  raeburn   913:         $usertype,$uname) = @_;
1.14      raeburn   914:     my $msg = '<h3>'.&mt('Account creation status').'</h3>'.
                    915:               &mt('Thank you for your request to create a new LON-CAPA account.').
                    916:               '<br /><br />';
1.1       raeburn   917:     my $now = time;
1.58      raeburn   918:     $env{'form.logtoken'} =~ s/(`)//g;
                    919:     if ($env{'form.logtoken'}) {
                    920:         my $logtoken = $env{'form.logtoken'};
1.72.4.3  raeburn   921:         my $earlyout;
1.58      raeburn   922:         my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$server);
                    923:         if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
                    924:             $msg = &mt('Information needed to process your request is missing, inaccessible or expired.')
1.72.4.3  raeburn   925:                   .'<br /><p>'.&mt('[_1]Return[_2] to the previous page to try again.',
                    926:                                    '<a href="javascript:document.retryemail.submit();">','</a>');
                    927:             $earlyout = 1;
1.58      raeburn   928:         } else {
                    929:             my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$server);
                    930:             unless ($reply eq 'ok') {
                    931:                 $msg .= &mt('Request could not be processed.');
                    932:             }
                    933:         }
1.72.4.3  raeburn   934: # Check if the password entered by the user satisfies domain's requirements
                    935:         my %passwdconf = &Apache::lonnet::get_passwdconf($domain);
                    936:         my ($min,$max,@chars);
                    937:         $min = $Apache::lonnet::passwdmin;
                    938:         if (ref($passwdconf{'chars'}) eq 'ARRAY') {
                    939:             if ($passwdconf{'min'} =~ /^\d+$/) {
                    940:                 if ($passwdconf{'min'} > $min) {
                    941:                     $min = $passwdconf{'min'};
                    942:                 }
                    943:             }
                    944:             if ($passwdconf{'max'} =~ /^\d+$/) {
                    945:                 $max = $passwdconf{'max'};
                    946:             }
                    947:             @chars = @{$passwdconf{'chars'}};
                    948:         }
                    949:         my $encpass = $env{'form.upass'};
                    950:         if ($encpass eq '') {
                    951:             $msg = &mt('Password retrieved was blank.').
                    952:                    '<br /><p>'.&mt('[_1]Return[_2] to the previous page to try again.',
                    953:                                    '<a href="javascript:document.retryemail.submit();">','</a>');
                    954:             $earlyout = 1;
                    955:         } else {
                    956: # Split the logtoken to retrieve the DES key and decrypt the encypted password
                    957:             my ($key,$caller)=split(/&/,$tmpinfo);
                    958:             if ($caller eq 'createaccount') {
                    959:                 my $plainpass = &Apache::loncommon::des_decrypt($key,$encpass);
                    960:                 if (($min > 0) || ($max ne '') || (@chars > 0)) {
                    961:                     my $warning = &Apache::loncommon::check_passwd_rules($domain,$plainpass);
                    962:                     if ($warning) {
                    963:                         $msg = $warning.
                    964:                                '<p>'.&mt('[_1]Return[_2] to the previous page to try again.',
                    965:                                          '<a href="javascript:document.retryemail.submit();">','</a>');
                    966:                         $earlyout = 1;
                    967:                     }
                    968:                 }
                    969:             }
                    970:         }
                    971:         if ($earlyout) {
                    972:             $msg .= '<form name="retryemail" action="/adm/createaccount" method="post" />'.
                    973:                     '<input type="hidden" name="domain" value="'.$domain.'" />'."\n";
                    974:             if ($env{'form.courseid'} =~ /^$match_domain\_$match_courseid$/) {
                    975:                 $msg .= '<input type="hidden" name="courseid" value="'.$env{'form.courseid'}.'" />'."\n";
                    976:             }
                    977:             if ($env{'form.type'}) {
                    978:                 my $usertype = &get_usertype($domain);
                    979:                 if ($usertype ne '') {
                    980:                     $msg .= '<input type="hidden" name="type" value="'.$usertype.'" />'.
                    981:                             '<input type="hidden" name="reportedtype" value="'.&mt('Submit').'" />'."\n";
                    982:                 }
                    983:             }
                    984:             $msg .= '</form></p>';
                    985:             return $msg;
                    986:         }
1.72.4.6  raeburn   987:         my $ip = &Apache::lonnet::get_requestor_ip();
                    988:         my %info = ('ip'         => $ip,
1.58      raeburn   989:                     'time'       => $now,
                    990:                     'domain'     => $domain,
                    991:                     'username'   => $email,
                    992:                     'courseid'   => $courseid,
                    993:                     'upass'      => $env{'form.upass'},
                    994:                     'serverid'   => $env{'form.serverid'},
                    995:                     'tmpinfo'    => $tmpinfo);
1.72.4.2  raeburn   996:         if ($uname ne '') {
                    997:             $info{'username'} = $uname;
                    998:             $info{'email'} = $email;
                    999:         }
1.58      raeburn  1000:         if (ref($emailusername) eq 'HASH') {
1.61      raeburn  1001:             if (ref($emailusername->{$usertype}) eq 'HASH') {
                   1002:                 foreach my $item (keys(%{$emailusername->{$usertype}})) {
                   1003:                     $info{$item} = $env{'form.'.$item};
                   1004:                     $info{$item} =~ s/(`)//g;
                   1005:                 }
1.58      raeburn  1006:             }
                   1007:         }
1.72.4.2  raeburn  1008:         if ($usertype ne '') {
                   1009:            $info{'usertype'} = $usertype;
                   1010:         }
1.58      raeburn  1011:         my $token = &Apache::lonnet::tmpput(\%info,$server,'createaccount');
                   1012:         if ($token !~ /^error/ && $token ne 'no_such_host') {
                   1013:             my $esc_token = &escape($token);
                   1014:             my $showtime = localtime(time);
                   1015:             my $mailmsg = &mt('A request was submitted on [_1] for creation of a LON-CAPA account at the following institution: [_2].',$showtime,$domdesc).' '.
                   1016:                           &mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',
                   1017:                           &Apache::lonnet::absolute_url().'/adm/createaccount?token='.$esc_token);
                   1018:             my $result = &Apache::resetpw::send_mail($domdesc,$email,$mailmsg,$contact_name,
                   1019:                                                      $contact_email);
                   1020:             if ($result eq 'ok') {
                   1021:                 $msg .= &mt('A message has been sent to the e-mail address you provided.').'<br />'.
                   1022:                         &mt('The message includes the web address for the link you will use to complete the account creation process.').'<br />'.
                   1023:                         &mt("The link included in the message will be valid for the next [_1]two[_2] hours.",'<b>','</b>');
                   1024:             } else {
                   1025:                 $msg .= '<span class="LC_error">'.
                   1026:                         &mt('An error occurred when sending a message to the e-mail address you provided.').'</span><br />'.
                   1027:                         ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
                   1028:             }
1.1       raeburn  1029:         } else {
1.14      raeburn  1030:             $msg .= '<span class="LC_error">'.
1.58      raeburn  1031:                     &mt('An error occurred creating a token required for the account creation process.').'</span><br />'.
1.14      raeburn  1032:                     ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn  1033:         }
                   1034:     } else {
1.58      raeburn  1035:         $msg .=  $msg = &mt('Information needed to process your request is missing, inaccessible or expired.')
                   1036:                 .'<br />'.&mt('Return to the previous page to try again.');
                   1037: 
1.1       raeburn  1038:     }
                   1039:     return $msg;
                   1040: }
                   1041: 
                   1042: sub process_mailtoken {
1.3       raeburn  1043:     my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,
1.72.4.2  raeburn  1044:         $include,$start_page,$cancreate,$settings,$types) = @_;
1.58      raeburn  1045:     my ($msg,$nostart,$noend,$redirect);
1.1       raeburn  1046:     my %data = &Apache::lonnet::tmpget($token);
                   1047:     my $now = time;
                   1048:     if (keys(%data) == 0) {
1.9       raeburn  1049:         $msg = &mt('Sorry, the URL you provided to complete creation of a new LON-CAPA account was invalid.')
                   1050:                .' '.&mt('Either the token included in the URL has been deleted or the URL you provided was invalid.')
1.58      raeburn  1051:                .' '.&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.',
                   1052:                         '<a href="/adm/createaccount">','</a>');
1.1       raeburn  1053:         return $msg;
                   1054:     }
                   1055:     if (($data{'time'} =~ /^\d+$/) &&
                   1056:         ($data{'domain'} ne '') &&
1.72.4.2  raeburn  1057:         ((($data{'email'} =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && ($data{'username'} =~ /^$match_username$/)) ||
                   1058:           ($data{'username'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/))) {
1.1       raeburn  1059:         if ($now - $data{'time'} < 7200) {
1.58      raeburn  1060: # Check if request should be queued.
                   1061:             if (ref($cancreate) eq 'ARRAY') {
1.72.4.2  raeburn  1062:                 my ($disposition,$usertype);
1.58      raeburn  1063:                 if (grep(/^email$/,@{$cancreate})) {
1.72.4.2  raeburn  1064:                     if (exists($data{'usertype'})) {
                   1065:                         $usertype = $data{'usertype'};
                   1066:                         my @posstypes;
                   1067:                         if (ref($types) eq 'ARRAY') {
                   1068:                             @posstypes = @{$types};
                   1069:                             if (@posstypes) {
                   1070:                                 unless (grep(/^default$/,@posstypes)) {
                   1071:                                     push(@posstypes,'default');
                   1072:                                 }
                   1073:                             }
                   1074:                             if (grep(/\Q$usertype\E/,@posstypes)) {
                   1075:                                 unless ($usertype eq 'default') {
                   1076:                                     $data{'inststatus'} = $usertype;
                   1077:                                 }
                   1078:                             } else {
                   1079:                                 $disposition = 'approval';
                   1080:                             }
                   1081:                         }
                   1082:                         delete($data{'usertype'});
                   1083:                     }
1.64      raeburn  1084:                     if (ref($settings) eq 'HASH') {
                   1085:                         if (ref($settings->{'cancreate'}) eq 'HASH') {
                   1086:                             if (ref($settings->{'cancreate'}{'selfcreateprocessing'}) eq 'HASH') {
1.72.4.2  raeburn  1087:                                 if ($usertype ne '') {
                   1088:                                     $disposition = $settings->{'cancreate'}{'selfcreateprocessing'}{$usertype};
                   1089:                                     unless ($disposition =~ /^(approval|automatic)$/) {
                   1090:                                         $disposition = 'approval';
                   1091:                                     }
                   1092:                                 }
1.64      raeburn  1093:                             }
1.58      raeburn  1094:                         }
1.64      raeburn  1095:                     }
                   1096:                     if ($disposition eq 'approval') {
                   1097:                         $msg = &store_request($domain,$data{'username'},'approval',\%data,$settings);
                   1098:                         my $delete = &Apache::lonnet::tmpdel($token);
1.1       raeburn  1099:                     } else {
1.64      raeburn  1100:                         my ($result,$output,$uhome) = 
                   1101:                             &create_account($r,$domain,$domdesc,\%data);
                   1102:                         if ($result eq 'ok') {
                   1103:                             $msg = $output;
1.72.4.6  raeburn  1104:                             my $ip = &Apache::lonnet::get_requestor_ip();
1.64      raeburn  1105:                             my $shownow = &Apache::lonlocal::locallocaltime($now);
1.72.4.6  raeburn  1106:                             my $mailmsg = &mt('A LON-CAPA account for the institution: [_1] has been created [_2] from IP address: [_3]. If you did not perform this action or authorize it, please contact the [_4] ([_5]).',$domdesc,$shownow,$ip,$contact_name,$contact_email)."\n";
1.64      raeburn  1107:                             my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'},
                   1108:                                                                         $mailmsg,$contact_name,
                   1109:                                                                         $contact_email);
                   1110:                             if ($mailresult eq 'ok') {
                   1111:                                 $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'});
                   1112:                             } else {
                   1113:                                 $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
                   1114:                             }
                   1115:                             $redirect = &start_session($r,$data{'username'},$domain,$uhome,
                   1116:                                                        $data{'courseid'},$token);
                   1117:                             $nostart = 1;
                   1118:                             $noend = 1;
                   1119:                         } else {
                   1120:                             $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.')
                   1121:                                    .'<br />'.$output;
                   1122:                             if (($contact_name ne '') && ($contact_email ne '')) {
                   1123:                                 $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
                   1124:                             }
1.58      raeburn  1125:                         }
1.64      raeburn  1126:                         my $delete = &Apache::lonnet::tmpdel($token);
1.1       raeburn  1127:                     }
                   1128:                 } else {
1.58      raeburn  1129:                     $msg = &invalid_state('noemails',$domdesc,$contact_name,$contact_email);
1.1       raeburn  1130:                 }
                   1131:             } else {
1.58      raeburn  1132:                 $msg = &invalid_state('noemails',$domdesc,$contact_name,$contact_email);
1.1       raeburn  1133:             }
                   1134:         } else {
1.7       bisitz   1135:             $msg = &mt('Sorry, the token generated when you requested creation of an account has expired.')
                   1136:                   .' '.&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  1137:             }
1.1       raeburn  1138:     } else {
1.7       bisitz   1139:         $msg .= &mt('Sorry, the URL generated when you requested creation of an account contained incomplete information.')
                   1140:                .' '.&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  1141:     }
1.58      raeburn  1142:     return ($msg,$nostart,$noend,$redirect);
1.3       raeburn  1143: }
                   1144: 
                   1145: sub start_session {
1.51      raeburn  1146:     my ($r,$username,$domain,$uhome,$courseid,$token) = @_;
1.69      raeburn  1147:     my ($is_balancer) = &Apache::lonnet::check_loadbalancing($username,$domain);
                   1148:     if ($is_balancer) {
1.51      raeburn  1149:         Apache::lonauth::success($r, $username, $domain, $uhome,
1.48      droeschl 1150:             'noredirect', undef, {});
                   1151: 
                   1152:         Apache::lonnet::tmpdel($token) if $token;
                   1153: 
1.58      raeburn  1154:         return 'redirect';
1.3       raeburn  1155:     } else {
1.48      droeschl 1156:         $courseid = Apache::lonnet::is_course($courseid); 
                   1157: 
1.51      raeburn  1158:         Apache::lonauth::success($r, $username, $domain, $uhome,
1.48      droeschl 1159:             ($courseid ? "/adm/selfenroll?courseid=$courseid" : '/adm/roles'),
                   1160:             undef, {}); 
1.3       raeburn  1161:     }
1.48      droeschl 1162:     return;
1.1       raeburn  1163: }
                   1164: 
1.50      www      1165: #
                   1166: # The screen that the user gets to create his or her account
                   1167: # Desired username, desired password, etc
                   1168: # Stores token to store DES-key and stage during creation session
                   1169: #
1.1       raeburn  1170: sub print_dataentry_form {
1.68      raeburn  1171:     my ($r,$domain,$lonhost,$include,$now,$captchaform,$courseid,$emailusername,$captcha,
1.72.4.2  raeburn  1172:         $usertype,$recaptchaversion,$usernameset,$condition,$excluded) = @_;
1.1       raeburn  1173:     my ($error,$output);
1.72.4.1  raeburn  1174:     if (open(my $jsh,"<","$include/londes.js")) {
1.68      raeburn  1175:         while(my $line = <$jsh>) {
                   1176:             $r->print($line);
                   1177:         }
                   1178:         close($jsh);
1.72.4.2  raeburn  1179:         $output = &javascript_setforms($now,$emailusername,$captcha,$usertype,$recaptchaversion,
                   1180:                                        $usernameset,$condition,$excluded).
1.72.4.3  raeburn  1181:                   "\n".&javascript_checkpass($now,'email',$domain);
1.58      raeburn  1182:         my ($lkey,$ukey) = &Apache::loncommon::des_keys();
1.1       raeburn  1183:         my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
1.41      raeburn  1184:         my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount:createaccount',
1.1       raeburn  1185:                                            $lonhost);
1.72.4.7! raeburn  1186:         my $showsubmit = 1;
        !          1187:         my $serverform =
1.58      raeburn  1188:             '<form name="createaccount" method="post" target="_top" action="/adm/createaccount">';
                   1189:         if ($courseid ne '') {
1.72.4.7! raeburn  1190:             $serverform .= '<input type="hidden" name="courseid" value="'.$courseid.'"/>'."\n";
1.58      raeburn  1191:         }
                   1192:         if (ref($emailusername) eq 'HASH') {
1.61      raeburn  1193:             if (ref($emailusername->{$usertype}) eq 'HASH') {
                   1194:                 foreach my $field (sort(keys(%{$emailusername->{$usertype}}))) {
1.72.4.7! raeburn  1195:                     $serverform .= '<input type="hidden" name="'.$field.'" value="" />'."\n";
1.61      raeburn  1196:                 }
1.58      raeburn  1197:             }
1.1       raeburn  1198:         }
1.59      raeburn  1199:         if ($captcha eq 'original') {
1.72.4.7! raeburn  1200:             $serverform .= '
1.59      raeburn  1201:    <input type="hidden" name="crypt" value="" />
                   1202:    <input type="hidden" name="code" value="" />
                   1203: ';
                   1204:         } elsif ($captcha eq 'recaptcha') {
1.72      raeburn  1205:             if ($recaptchaversion eq '2') {
1.72.4.7! raeburn  1206:                 $serverform .= &Apache::lonhtmlcommon::start_pick_box().
        !          1207:                                &Apache::lonhtmlcommon::row_title(&mt('Validation').'<b>*</b>',
        !          1208:                                                                  'LC_pick_box_title',
        !          1209:                                                                  'LC_oddrow_value')."\n".
        !          1210:                                                                  $captchaform.
        !          1211:                                &Apache::lonhtmlcommon::row_closure(1)."\n".
        !          1212:                                &Apache::lonhtmlcommon::row_title()."\n".
        !          1213:                                '<br /><input type="button" name="createaccount" value="'.
        !          1214:                                &mt('Create account').'" onclick="checkpass('."'createaccount','newemail'".')" />'.
        !          1215:                                &Apache::lonhtmlcommon::row_closure(1)."\n".
        !          1216:                                &Apache::lonhtmlcommon::end_pick_box();
1.72      raeburn  1217:                 undef($captchaform);
1.72.4.7! raeburn  1218:                 undef($showsubmit);
1.72      raeburn  1219:             } else {
1.72.4.7! raeburn  1220:                 $serverform .= '
1.59      raeburn  1221:    <input type="hidden" name="recaptcha_challenge_field" value="" />
                   1222:    <input type="hidden" name="recaptcha_response_field" value="" />
                   1223: ';
1.72      raeburn  1224:             }
1.59      raeburn  1225:         }
1.72.4.2  raeburn  1226:         if ($usertype ne '') {
1.72.4.7! raeburn  1227:             $serverform .= '<input type="hidden" name="type" value="'.
        !          1228:                            &HTML::Entities::encode($usertype,'\'<>"&').'" />'."\n";
1.72.4.2  raeburn  1229:         }
                   1230:         if ($usernameset eq 'free') {
1.72.4.7! raeburn  1231:             $serverform .= '<input type="hidden" name="username" value="" />'."\n"; 
1.72.4.2  raeburn  1232:         }
1.72.4.7! raeburn  1233:         $serverform .= <<"ENDSERVERFORM";
1.1       raeburn  1234:    <input type="hidden" name="logtoken" value="$logtoken" />
                   1235:    <input type="hidden" name="serverid" value="$lonhost" />
                   1236:    <input type="hidden" name="uname" value="" />
                   1237:    <input type="hidden" name="upass" value="" />
1.32      raeburn  1238:    <input type="hidden" name="udom" value="" />
1.1       raeburn  1239:    <input type="hidden" name="phase" value="createaccount" />
1.58      raeburn  1240:    <input type="hidden" name="create_with_email" value="1" />
1.32      raeburn  1241:   </form>
1.1       raeburn  1242: ENDSERVERFORM
1.58      raeburn  1243:         my $beginclientform = '<form name="newemail" method="post" action="" '.
                   1244:                               'onsubmit="return checkpass('."'createaccount','newemail'".');">'."\n";
1.72.4.7! raeburn  1245:         my $endclientform;
        !          1246:         unless ($showsubmit) {
        !          1247:             if ($usertype ne '') {
        !          1248:                 $endclientform = '<input type="hidden" name="type" value="'.
        !          1249:                                  &HTML::Entities::encode($usertype,'\'<>"&').'" />'."\n";
        !          1250:             }
        !          1251:         }
        !          1252:         $endclientform .= '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
        !          1253:                           '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
        !          1254:                           '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
        !          1255:                           '</form>'."\n";
1.58      raeburn  1256:         my ($datatable,$rowcount) =
                   1257:             &Apache::loncreateuser::personal_data_display('',$domain,'email','selfcreate',
                   1258:                                                           '','',$now,$captchaform,
1.72.4.2  raeburn  1259:                                                           $emailusername,$usertype,
1.72.4.7! raeburn  1260:                                                           $usernameset,$condition,$excluded,
        !          1261:                                                           $showsubmit);
1.32      raeburn  1262:         if ($rowcount) {
1.72.4.7! raeburn  1263:             $output .= '<div class="LC_left_float">'.$beginclientform.$datatable.$endclientform.'</div>'."\n".
        !          1264:                        '<div class="LC_clear_float_footer"></div>'."\n";
1.58      raeburn  1265:         } else {
                   1266:             $output .= $beginclientform.$endclientform;
1.32      raeburn  1267:         }
1.72.4.7! raeburn  1268:         $output .= $serverform.
        !          1269:                    '<p class="LC_info">'.
        !          1270:                    &mt('Fields marked [_1]*[_2] are required.','<b>','</b>').
        !          1271:                    '</p>';
1.1       raeburn  1272:     } else {
1.7       bisitz   1273:         $output = &mt('Could not load javascript file [_1]','<tt>londes.js</tt>');
1.1       raeburn  1274:     }
1.3       raeburn  1275:     return $output;
1.1       raeburn  1276: }
                   1277: 
1.50      www      1278: #
                   1279: # Retrieve rules for generating accounts from domain configuration
                   1280: # Can the user make a new account or just self-enroll?
                   1281: 
1.35      raeburn  1282: sub get_creation_controls {
                   1283:     my ($domain,$usercreation) = @_;
1.72.4.2  raeburn  1284:     my (@cancreate,@statustocreate,@statusforemail,$emailusername,$processing,
                   1285:         $emailoptions,$verification,$emaildomain,$othertitle,$usertypes,$types);
1.35      raeburn  1286:     if (ref($usercreation) eq 'HASH') {
                   1287:         if (ref($usercreation->{'cancreate'}) eq 'HASH') {
1.72.4.2  raeburn  1288:             ($othertitle,$usertypes,$types) =
                   1289:                 &Apache::loncommon::sorted_inst_types($domain);
1.35      raeburn  1290:             if (ref($usercreation->{'cancreate'}{'statustocreate'}) eq 'ARRAY') {
                   1291:                 @statustocreate = @{$usercreation->{'cancreate'}{'statustocreate'}};
1.47      raeburn  1292:                 if (@statustocreate == 0) {
                   1293:                     if (ref($types) eq 'ARRAY') {
                   1294:                         if (@{$types} == 0) {
                   1295:                             @statustocreate = ('default');
                   1296:                         }
                   1297:                     } else {
                   1298:                         @statustocreate = ('default');
                   1299:                     }
                   1300:                 }
1.35      raeburn  1301:             } else {
                   1302:                 @statustocreate = ('default');
                   1303:                 if (ref($types) eq 'ARRAY') {
                   1304:                     push(@statustocreate,@{$types});
                   1305:                 }
                   1306:             }
                   1307:             if (ref($usercreation->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                   1308:                 @cancreate = @{$usercreation->{'cancreate'}{'selfcreate'}};
                   1309:             } elsif (($usercreation->{'cancreate'}{'selfcreate'} ne 'none') &&
                   1310:                      ($usercreation->{'cancreate'}{'selfcreate'} ne '')) {
                   1311:                 @cancreate = ($usercreation->{'cancreate'}{'selfcreate'});
                   1312:             }
1.72.4.2  raeburn  1313:             if (grep(/^email$/,@cancreate)) {
                   1314:                 if (ref($usercreation->{'cancreate'}{'selfcreateprocessing'}) eq 'HASH') {
                   1315:                     $processing = $usercreation->{'cancreate'}{'selfcreateprocessing'};
                   1316:                 }
                   1317:                 if (ref($usercreation->{'cancreate'}{'emailoptions'}) eq 'HASH') {
                   1318:                     $emailoptions = $usercreation->{'cancreate'}{'emailoptions'};
                   1319:                 }
                   1320:                 if (ref($usercreation->{'cancreate'}{'emailverified'}) eq 'HASH') {
                   1321:                     $verification = $usercreation->{'cancreate'}{'emailverified'};
                   1322:                 }
                   1323:                 if (ref($usercreation->{'cancreate'}{'emaildomain'}) eq 'HASH') {
                   1324:                     $emaildomain = $usercreation->{'cancreate'}{'emaildomain'};
                   1325:                 }
                   1326:                 if (ref($processing)) {
                   1327:                     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                   1328:                     my @emailtypes;
                   1329:                     if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
                   1330:                         @statusforemail = @{$domdefaults{'inststatusguest'}};
                   1331:                         unless (@statusforemail) {
                   1332:                             my @okcreate;
                   1333:                             foreach my $poss (@cancreate) {
                   1334:                                 unless ($poss eq 'email') {
                   1335:                                      push(@okcreate,$poss);
                   1336:                                 }
                   1337:                             }
                   1338:                             @cancreate = @okcreate;
                   1339:                         }
                   1340:                     }
                   1341:                 }  
                   1342:             }
1.58      raeburn  1343:             if (ref($usercreation->{'cancreate'}{'emailusername'}) eq 'HASH') {
                   1344:                 $emailusername = $usercreation->{'cancreate'}{'emailusername'};
1.59      raeburn  1345:             } else {
1.67      raeburn  1346:                 $emailusername = {
                   1347:                                     default =>  {
                   1348:                                                    'lastname' => '1',
                   1349:                                                    'firstname' => 1,
                   1350:                                                 },
                   1351:                                  };
1.58      raeburn  1352:             }
1.35      raeburn  1353:         }
                   1354:     }
1.72.4.2  raeburn  1355:     return (\@cancreate,\@statustocreate,\@statusforemail,$emailusername,
                   1356:             $emailoptions,$verification,$emaildomain,$types,$usertypes,$othertitle);
1.35      raeburn  1357: }
                   1358: 
1.1       raeburn  1359: sub create_account {
1.58      raeburn  1360:     my ($r,$domain,$domdesc,$dataref) = @_;
                   1361:     my $error    = '<span class="LC_error">'.&mt('Error:').' ';
                   1362:     my $end      = '</span><br /><br />';
                   1363:     my $rtnlink  = '<a href="javascript:history.back();">'.
1.1       raeburn  1364:                     &mt('Return to previous page').'</a>'.
                   1365:                     &Apache::loncommon::end_page();
1.58      raeburn  1366:     my $output;
                   1367:     if (ref($dataref) eq 'HASH') {
                   1368:         my ($username,$encpass,$serverid,$courseid,$id,$firstname,$middlename,$lastname,
1.72.4.2  raeburn  1369:             $generation,$inststatus,$permanentemail);
1.58      raeburn  1370:         $username   = $dataref->{'username'};
                   1371:         $encpass    = $dataref->{'upass'};
                   1372:         $serverid   = $dataref->{'serverid'};
                   1373:         $courseid   = $dataref->{'courseid'};
                   1374:         $id         = $dataref->{'id'};
                   1375:         $firstname  = $dataref->{'firstname'};
                   1376:         $middlename = $dataref->{'middlename'};
                   1377:         $lastname   = $dataref->{'lastname'};
                   1378:         $generation = $dataref->{'generation'};
1.63      raeburn  1379:         $inststatus = $dataref->{'inststatus'};
                   1380: 
1.72.4.2  raeburn  1381:         if ($dataref->{'email'} ne '') {
                   1382:             $permanentemail = $dataref->{'email'};
                   1383:         } else {
                   1384:             $permanentemail = $dataref->{'username'};
                   1385:         }
1.58      raeburn  1386:         my $currhome = &Apache::lonnet::homeserver($username,$domain);
                   1387:         unless ($currhome eq 'no_host') {
                   1388:             $output = &mt('User account requested for username: [_1] in domain: [_2] already exists.',$username,$domain);
                   1389:             return ('fail',$error.$output.$end.$rtnlink);
                   1390:         }
                   1391: 
                   1392: # Split the logtoken to retrieve the DES key and decrypt the encypted password
                   1393: 
                   1394:         my ($key,$caller)=split(/&/,$dataref->{'tmpinfo'});
                   1395:         if ($caller eq 'createaccount') {
1.72.4.2  raeburn  1396:             my $upass;
                   1397:             if ($encpass eq '') {
                   1398:                 $output = &mt('Password retrieved was blank.');
                   1399:                 return ('fail',$error.$output.$end.$rtnlink);
                   1400:             } else {
                   1401:                 $upass = &Apache::loncommon::des_decrypt($key,$encpass);
                   1402:             }
1.58      raeburn  1403: 
                   1404: # See if we are allowed to use the proposed student/employee ID,
                   1405: # as per domain rules; if not, student/employee will be left blank.
                   1406: 
                   1407:             if ($id ne '') {
                   1408:                 my ($result,$userchkmsg) = &check_id($username,$domain,$id,$domdesc,'email');
                   1409:                 if ($result eq 'fail') {
                   1410:                     $output = $error.&mt('Invalid ID format').$end.
                   1411:                               $userchkmsg;
                   1412:                     undef($id);
                   1413:                 }
1.1       raeburn  1414:             }
1.58      raeburn  1415: 
                   1416: # Create an internally authenticated account with password $upass
                   1417: # if the user account does not already exist.
                   1418: # Assign student/employee id, first name, last name, etc.
                   1419: 
                   1420:             my $result =
                   1421:                 &Apache::lonnet::modifyuser($domain,$username,$id,
                   1422:                                             'internal',$upass,$firstname,$middlename,
1.72.4.2  raeburn  1423:                                             $lastname,$generation,undef,undef,$permanentemail);
1.58      raeburn  1424:             $output = &mt('Generating user: [_1]',$result);
                   1425: 
                   1426: # Now that the user account exists, retrieve the homeserver, and include it in the output.
                   1427: 
                   1428:             my $uhome = &Apache::lonnet::homeserver($username,$domain);
1.61      raeburn  1429:             unless (($inststatus eq 'default') || ($inststatus eq '')) {
                   1430:                 &Apache::lonnet::put('environment',{inststatus => $inststatus},$domain,$username);
                   1431:             }
1.72.4.5  raeburn  1432:             $output .= '<br />'.&mt('Home Server').": $uhome ".
1.58      raeburn  1433:                        &Apache::lonnet::hostname($uhome).'<br /><br />';
                   1434:             return ('ok',$output,$uhome);
                   1435:         } else {
                   1436:             $output = &mt('Unable to retrieve your account creation information - unexpected context');
                   1437:             undef($encpass);
                   1438:             return ('fail',$error.$output.$end.$rtnlink);
1.1       raeburn  1439:         }
                   1440:     } else {
1.58      raeburn  1441:         $output = &mt('Unable to retrieve information for your account request.');
1.1       raeburn  1442:         return ('fail',$error.$output.$end.$rtnlink);
                   1443:     }
                   1444: }
                   1445: 
                   1446: sub username_validation {
1.19      raeburn  1447:     my ($r,$username,$domain,$domdesc,$contact_name,$contact_email,$courseid,
1.35      raeburn  1448:         $lonhost,$statustocreate) = @_;
1.58      raeburn  1449: # $r: request object
1.49      www      1450: # $username,$domain: for the user who needs to be validated
                   1451: # $domdesc: full name of the domain (for error messages)
1.58      raeburn  1452: # $contact_name, $contact_email: name and email for user assistance (for error messages in &username_check)
                   1453: # $courseid: ID of the course if user came to username_validation via self-enroll link,
                   1454: #             passed to start_session()
                   1455: # $lonhost: LON-CAPA lonHostID
1.49      www      1456: # $statustocreate: -> inststatus in username_check ('faculty', 'staff', 'student', ...)
                   1457:  
1.58      raeburn  1458: #
                   1459: # Sanitize incoming username and domain
                   1460: #
1.1       raeburn  1461:     $username= &LONCAPA::clean_username($username);
                   1462:     $domain = &LONCAPA::clean_domain($domain);
1.58      raeburn  1463: 
                   1464: #
                   1465: # Check if LON-CAPA account already exists for $username:$domain
                   1466: #
1.1       raeburn  1467:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
                   1468: 
1.58      raeburn  1469:     my $output;
                   1470: 
                   1471: # Retrieve DES key from server using logtoken
                   1472:  
                   1473:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$env{'form.logtoken'},$env{'form.serverid'});
                   1474:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
                   1475:         $output = &mt('Information needed to verify your login information is missing, inaccessible or expired.')
                   1476:                  .'<br />'.&mt('You may need to reload the previous page to obtain a new token.');
                   1477:         return ('fail',$output);
                   1478:     } else {
                   1479:         my $reply = &Apache::lonnet::reply('tmpdel:'.$env{'form.logtoken'},$env{'form.serverid'});
                   1480:         unless ($reply eq 'ok') {
                   1481:             $output = &mt('Session could not be opened.');
                   1482:             return ('fail',$output); 
                   1483:         }
                   1484:     }
                   1485: 
                   1486: # Split the logtoken to retrieve the DES key and decrypt the encypted password
                   1487: 
                   1488:     my ($key,$caller)=split(/&/,$tmpinfo);
                   1489:     my $upass;
                   1490:     if ($caller eq 'createaccount') {
                   1491:         $upass = &Apache::loncommon::des_decrypt($key,$env{'form.upass'});
                   1492:     } else {
                   1493:         $output = &mt('Unable to retrieve your log-in information - unexpected context');
1.19      raeburn  1494:         return ('fail',$output);
                   1495:     }
                   1496:     if ($uhome ne 'no_host') {
                   1497:         my $result = &Apache::lonnet::authenticate($username,$upass,$domain);
                   1498:         if ($result ne 'no_host') { 
1.58      raeburn  1499:             my $redirect = &start_session($r,$username,$domain,$uhome,$courseid);
                   1500:             if ($redirect) {
                   1501:                 return ($redirect);
                   1502:             }
                   1503:             $output = '<br /><br />'.
                   1504:                       &mt('A LON-CAPA account already exists for username [_1] at this institution ([_2]).',
                   1505:                           '<tt>'.$username.'</tt>',$domdesc).'<br />'.
                   1506:                       &mt('The password entered was also correct so you have been logged in.');
1.19      raeburn  1507:             return ('existingaccount',$output);
                   1508:         } else {
1.22      raeburn  1509:             $output = &login_failure_msg($courseid);
1.19      raeburn  1510:         }
                   1511:     } else {
1.1       raeburn  1512:         my $primlibserv = &Apache::lonnet::domain($domain,'primary');
                   1513:         my $authok;
                   1514:         my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1.58      raeburn  1515:         if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) || 
                   1516:              ($domdefaults{'auth_def'} eq 'localauth')) {
1.1       raeburn  1517:             my $checkdefauth = 1;
                   1518:             $authok = 
                   1519:                 &Apache::lonnet::reply("encrypt:auth:$domain:$username:$upass:$checkdefauth",$primlibserv);
                   1520:         } else {
                   1521:             $authok = 'non_authorized';
                   1522:         }
                   1523:         if ($authok eq 'authorized') {
1.18      raeburn  1524:             $output = &username_check($username,$domain,$domdesc,$courseid,$lonhost,
1.35      raeburn  1525:                                       $contact_email,$contact_name,undef,
                   1526:                                       $statustocreate);
1.4       raeburn  1527:         } else {
1.22      raeburn  1528:             $output = &login_failure_msg($courseid);
1.4       raeburn  1529:         }
                   1530:     }
1.19      raeburn  1531:     return ('ok',$output);
1.4       raeburn  1532: }
                   1533: 
1.22      raeburn  1534: sub login_failure_msg {
                   1535:     my ($courseid) = @_;
                   1536:     my $url;
                   1537:     if ($courseid ne '') {
                   1538:         $url = "/adm/selfenroll?courseid=".$courseid;
                   1539:     } else {
                   1540:         $url = "/adm/createaccount";
                   1541:     }
                   1542:     my $output = '<h4>'.&mt('Authentication failed').'</h4><div class="LC_warning">'.
                   1543:                  &mt('Username and/or password could not be authenticated.').
                   1544:                  '</div>'.
                   1545:                  &mt('Please check the username and password.').'<br /><br />';
                   1546:                  '<a href="'.$url.'">'.&mt('Try again').'</a>';
                   1547:     return $output;
                   1548: }
                   1549: 
1.4       raeburn  1550: sub username_check {
1.35      raeburn  1551:     my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email,
1.65      raeburn  1552:         $contact_name,$sso_logout,$statustocreate,$shibenv) = @_;
1.23      raeburn  1553:     my (%rulematch,%inst_results,$checkfail,$rowcount,$editable,$output,$msg,
1.18      raeburn  1554:         %alerts,%curr_rules,%got_rules);
1.23      raeburn  1555:     &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1.40      raeburn  1556:                     \%inst_results,\%curr_rules,\%got_rules,'username');
1.4       raeburn  1557:     if (ref($alerts{'username'}) eq 'HASH') {
                   1558:         if (ref($alerts{'username'}{$domain}) eq 'HASH') {
                   1559:             if ($alerts{'username'}{$domain}{$username}) {
                   1560:                 if (ref($curr_rules{$domain}) eq 'HASH') {
1.18      raeburn  1561:                     $output =
1.17      raeburn  1562:                         &Apache::loncommon::instrule_disallow_msg('username',$domdesc,1,
                   1563:                                                                   'selfcreate').
1.4       raeburn  1564:                         &Apache::loncommon::user_rule_formats($domain,$domdesc,
                   1565:                                 $curr_rules{$domain}{'username'},'username');
1.1       raeburn  1566:                 }
1.18      raeburn  1567:                 $checkfail = 'username';
1.1       raeburn  1568:             }
1.4       raeburn  1569:         }
                   1570:     }
1.18      raeburn  1571:     if (!$checkfail) {
1.35      raeburn  1572:         if (ref($statustocreate) eq 'ARRAY') {
                   1573:             $checkfail = 'inststatus';
                   1574:             if (ref($inst_results{$username.':'.$domain}{inststatus}) eq 'ARRAY') {
                   1575:                 foreach my $inststatus (@{$inst_results{$username.':'.$domain}{inststatus}}) {
                   1576:                     if (grep(/^\Q$inststatus\E$/,@{$statustocreate})) {
                   1577:                         undef($checkfail);
                   1578:                         last;
                   1579:                     }
                   1580:                 }
                   1581:             } elsif (grep(/^default$/,@{$statustocreate})) {
                   1582:                 undef($checkfail);
                   1583:             }
                   1584:         }
                   1585:     }
                   1586:     if (!$checkfail) {
1.18      raeburn  1587:         $output = '<form method="post" action="/adm/createaccount">';
1.65      raeburn  1588:         if (ref($shibenv) eq 'HASH') {
                   1589:             foreach my $key (keys(%{$shibenv})) {
1.66      raeburn  1590:                 if ($ENV{$shibenv->{$key}} ne '') {
                   1591:                     $inst_results{$username.':'.$domain}{$key} = $ENV{$shibenv->{$key}};
                   1592:                 }
1.65      raeburn  1593:             }
                   1594:         }
1.18      raeburn  1595:         (my $datatable,$rowcount,$editable) = 
                   1596:             &Apache::loncreateuser::personal_data_display($username,$domain,1,'selfcreate',
                   1597:                                                          $inst_results{$username.':'.$domain});
                   1598:         if ($rowcount > 0) {
                   1599:             $output .= $datatable;
                   1600:         }
                   1601:         $output .=  '<br /><br /><input type="hidden" name="uname" value="'.$username.'" />'."\n".
                   1602:                     '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
                   1603:                     '<input type="hidden" name="phase" value="username_activation" />';
                   1604:         my $now = time;
1.72.4.6  raeburn  1605:         my $ip = &Apache::lonnet::get_requestor_ip();
                   1606:         my %info = ('ip'         => $ip,
1.18      raeburn  1607:                     'time'       => $now,
                   1608:                     'domain'     => $domain,
                   1609:                     'username'   => $username);
1.41      raeburn  1610:         my $authtoken = &Apache::lonnet::tmpput(\%info,$lonhost,'createaccount');
1.18      raeburn  1611:         if ($authtoken !~ /^error/ && $authtoken ne 'no_such_host') {
                   1612:             $output .= '<input type="hidden" name="authtoken" value="'.&HTML::Entities::encode($authtoken,'&<>"').'" />';
                   1613:         } else {
                   1614:             $output = &mt('An error occurred when storing a token').'<br />'.
                   1615:                       &mt('You will not be able to proceed to the next stage of account creation').
                   1616:                       &linkto_email_help($contact_email,$domdesc);
                   1617:             $checkfail = 'authtoken';
                   1618:         }
                   1619:     }
                   1620:     if ($checkfail) { 
1.47      raeburn  1621:         $msg = '<br /><h4>'.&mt('Account creation unavailable').'</h4>';
1.18      raeburn  1622:         if ($checkfail eq 'username') {
                   1623:             $msg .= '<span class="LC_warning">'.
                   1624:                      &mt('A LON-CAPA account may not be created with the username you use.').
                   1625:                      '</span><br /><br />'.$output;
                   1626:         } elsif ($checkfail eq 'authtoken') {
                   1627:             $msg .= '<span class="LC_error">'.&mt('Error creating token.').'</span>'.
                   1628:                     '<br />'.$output;
1.35      raeburn  1629:         } elsif ($checkfail eq 'inststatus') {
                   1630:             $msg .= '<span class="LC_warning">'.
                   1631:                      &mt('You are not permitted to create a LON-CAPA account.').
                   1632:                      '</span><br /><br />'.$output;
1.18      raeburn  1633:         }
                   1634:         $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',
                   1635:                 $contact_name,$contact_email).'<br /><hr />'.
                   1636:                 $sso_logout;
                   1637:         &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  1638:     } else {
1.18      raeburn  1639:         if ($courseid ne '') {
                   1640:             $output .= '<input type="hidden" name="courseid" value="'.$courseid.'" />';
                   1641:         }
                   1642:         $output .= '<input type="submit" name="newaccount" value="'.
                   1643:                    &mt('Create LON-CAPA account').'" /></form>';
                   1644:         if ($rowcount) {
                   1645:             if ($editable) {
1.22      raeburn  1646:                 if ($courseid ne '') { 
1.47      raeburn  1647:                     $msg = '<br /><h4>'.&mt('User information').'</h4>';
1.22      raeburn  1648:                 }
                   1649:                 $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  1650:             } else {
1.22      raeburn  1651:                  if ($courseid ne '') {
                   1652:                      $msg = '<h4>'.&mt('Review user information').'</h4>';
                   1653:                  }
                   1654:                  $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  1655:             }
                   1656:         } else {
1.22      raeburn  1657:             if ($courseid ne '') {
                   1658:                 $msg = '<h4>'.&mt('Confirmation').'</h4>';
                   1659:             }
                   1660:             $msg .= &mt('Confirm that you wish to create an account.');
1.18      raeburn  1661:         }
                   1662:         $msg .= $output;
                   1663:     }
                   1664:     return $msg;
1.1       raeburn  1665: }
                   1666: 
                   1667: sub username_activation {
1.51      raeburn  1668:     my ($r,$username,$domain,$domdesc,$courseid) = @_;
1.1       raeburn  1669:     my $output;
1.7       bisitz   1670:     my $error     = '<span class="LC_error">'.&mt('Error:').' ';
1.1       raeburn  1671:     my $end       = '</span><br /><br />';
1.54      bisitz   1672:     my $rtnlink   = '<a href="javascript:history.back();">'.
1.1       raeburn  1673:                     &mt('Return to previous page').'</a>'.
                   1674:                     &Apache::loncommon::end_page();
                   1675:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1.8       raeburn  1676:     my %data = &Apache::lonnet::tmpget($env{'form.authtoken'});
                   1677:     my $now = time;
                   1678:     my $earlyout;
                   1679:     my $timeout = 300;
                   1680:     if (keys(%data) == 0) {
                   1681:         $output = &mt('Sorry, your authentication has expired.');
                   1682:         $earlyout = 'fail';
                   1683:     }
                   1684:     if (($data{'time'} !~ /^\d+$/) ||
                   1685:         ($data{'domain'} ne $domain) || 
                   1686:         ($data{'username'} ne $username)) {
                   1687:         $earlyout = 'fail';
                   1688:         $output = &mt('The credentials you provided could not be verified.');   
                   1689:     } elsif ($now - $data{'time'} > $timeout) {
                   1690:         $earlyout = 'fail';
                   1691:         $output = &mt('Sorry, your authentication has expired.');
                   1692:     }
                   1693:     if ($earlyout ne '') {
1.56      raeburn  1694:         my $link = '/adm/createaccount';
                   1695:         if (&Apache::lonnet::domain($domain) ne '') {
                   1696:             $link .= "?domain=$domain"; 
                   1697:         }
                   1698:         $output .= '<br />'.&mt('Please [_1]start again[_2].',
                   1699:                                 '<a href="'.$link.'">','</a>');
1.8       raeburn  1700:         return($earlyout,$output);
                   1701:     }
1.3       raeburn  1702:     if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && 
                   1703:          ($domdefaults{'auth_arg_def'} ne '')) || 
                   1704:         ($domdefaults{'auth_def'} eq 'localauth')) {
1.22      raeburn  1705:         if ($env{'form.courseid'} ne '') {
1.58      raeburn  1706:             my $id = $env{'form.cid'}; 
                   1707:             my ($result,$userchkmsg) = &check_id($username,$domain,$id,$domdesc,'institutional');
1.1       raeburn  1708:             if ($result eq 'fail') {
                   1709:                 $output = $error.&mt('Invalid ID format').$end.
                   1710:                           $userchkmsg.$rtnlink;
                   1711:                 return ('fail',$output);
                   1712:             }
                   1713:         }
                   1714:         # Call modifyuser
1.23      raeburn  1715:         my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts,%info);
                   1716:         &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1.40      raeburn  1717:                         \%inst_results,\%curr_rules,\%got_rules);
1.23      raeburn  1718:         my @userinfo = ('firstname','middlename','lastname','generation',
                   1719:                         'permanentemail','id');
                   1720:         my %canmodify = 
                   1721:             &Apache::loncreateuser::selfcreate_canmodify('selfcreate',$domain,
                   1722:                                                          \@userinfo,\%inst_results);
                   1723:         foreach my $item (@userinfo) {
                   1724:             if ($canmodify{$item}) {
                   1725:                 $info{$item} = $env{'form.c'.$item};
                   1726:             } else {
                   1727:                 $info{$item} = $inst_results{$username.':'.$domain}{$item}; 
                   1728:             }
                   1729:         }
                   1730:         if (ref($inst_results{$username.':'.$domain}{'inststatus'}) eq 'ARRAY') {
                   1731:             my @inststatuses = @{$inst_results{$username.':'.$domain}{'inststatus'}};
                   1732:             $info{'inststatus'} = join(':',map { &escape($_); } @inststatuses);
                   1733:         }
1.1       raeburn  1734:         my $result =
1.23      raeburn  1735:             &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
1.1       raeburn  1736:                           $domdefaults{'auth_def'},
1.23      raeburn  1737:                           $domdefaults{'auth_arg_def'},$info{'firstname'},
                   1738:                           $info{'middlename'},$info{'lastname'},
                   1739:                           $info{'generation'},undef,undef,
                   1740:                           $info{'permanentemail'},$info{'inststatus'});
1.3       raeburn  1741:         if ($result eq 'ok') {
1.8       raeburn  1742:             my $delete = &Apache::lonnet::tmpdel($env{'form.authtoken'});
1.3       raeburn  1743:             $output = &mt('A LON-CAPA account has been created for username: [_1] in domain: [_2].',$username,$domain);
1.51      raeburn  1744:             my $uhome=&Apache::lonnet::homeserver($username,$domain,'true');
1.3       raeburn  1745:             my $nostart = 1;
1.58      raeburn  1746:             my $response = 'ok';
                   1747:             my $redirect = &start_session($r,$username,$domain,$uhome,$courseid);
                   1748:             if ($redirect) {
                   1749:                 $response = $redirect;
                   1750:             }
                   1751:             return ($response,$output,$nostart);
1.3       raeburn  1752:         } else {
                   1753:             $output = &mt('Account creation failed for username: [_1] in domain: [_2].',$username,$domain).'<br /><span class="LC_error">'.&mt('Error: [_1]',$result).'</span>';
                   1754:             return ('fail',$output);
                   1755:         }
1.1       raeburn  1756:     } else {
1.7       bisitz   1757:         $output = &mt('User account creation is not available for the current default authentication type.')."\n";
1.1       raeburn  1758:         return('fail',$output);
                   1759:     }
                   1760: }
                   1761: 
                   1762: sub check_id {
1.58      raeburn  1763:     my ($username,$domain,$id,$domdesc,$usernametype) = @_;
                   1764:     # Check student/employee ID format
                   1765:     # Is proposed student/employee ID acceptable according to domain's rules.  
1.50      www      1766:     # $domdesc is just used for user error messages
1.1       raeburn  1767:     my (%alerts,%rulematch,%inst_results,%curr_rules,%checkhash);
                   1768:     my %checks = ('id' => 1);
                   1769:     %{$checkhash{$username.':'.$domain}} = (
                   1770:                                             'newuser' => 1,
1.58      raeburn  1771:                                             'id' => $id,
1.1       raeburn  1772:                                            );
                   1773:     &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   1774:                                         \%rulematch,\%inst_results,\%curr_rules);
                   1775:     if (ref($alerts{'id'}) eq 'HASH') {
                   1776:         if (ref($alerts{'id'}{$domain}) eq 'HASH') {
                   1777:             if ($alerts{'id'}{$domain}{$env{'form.cid'}}) {
                   1778:                 my $userchkmsg;
                   1779:                 if (ref($curr_rules{$domain}) eq 'HASH') {
1.58      raeburn  1780:                     if ($usernametype eq 'email') {
                   1781:                         $userchkmsg = &mt('A student/employee ID has not been set because the value suggested matched the format used for institutional users in the domain, and you are using an e-mail address as username, not an institutional username.');
                   1782:                     } else {
                   1783:                         $userchkmsg =
                   1784:                             &Apache::loncommon::instrule_disallow_msg('id',
                   1785:                                                                       $domdesc,1).
                   1786:                             &Apache::loncommon::user_rule_formats($domain,
                   1787:                                 $domdesc,$curr_rules{$domain}{'id'},'id');
                   1788:                     }
1.1       raeburn  1789:                 }
                   1790:                 return ('fail',$userchkmsg);
                   1791:             }
                   1792:         }
                   1793:     }
                   1794:     return; 
                   1795: }
                   1796: 
                   1797: sub invalid_state {
                   1798:     my ($error,$domdesc,$contact_name,$contact_email,$msgtext) = @_;
1.14      raeburn  1799:     my $msg = '<h3>'.&mt('Account creation unavailable').'</h3><span class="LC_error">';
1.1       raeburn  1800:     if ($error eq 'baduseremail') {
1.42      raeburn  1801:         $msg .= &mt('The e-mail address you provided does not appear to be a valid address.');
1.58      raeburn  1802:     } elsif ($error eq 'badusername') {
                   1803:         $msg .= &mt('The e-mail address you provided contains characters which prevent its use as a username in LON-CAPA.');
1.1       raeburn  1804:     } elsif ($error eq 'existinguser') {
1.42      raeburn  1805:         $msg .= &mt('The e-mail address you provided is already in use as a username in LON-CAPA at this institution.');
1.1       raeburn  1806:     } elsif ($error eq 'userrules') {
1.42      raeburn  1807:         $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  1808:     } elsif ($error eq 'userformat') {
1.42      raeburn  1809:         $msg .= &mt('The e-mail address you provided may not be used as a username at this LON-CAPA institution.');
1.1       raeburn  1810:     } elsif ($error eq 'noemails') {
1.42      raeburn  1811:         $msg .= &mt('Creation of a new user account using an e-mail address as username is not permitted at this LON-CAPA institution.');
1.72.4.2  raeburn  1812:     } elsif ($error eq 'emailfail') {
                   1813:         $msg .= &mt('Creation of a new user account with verification by e-mail is not permitted with the e-mail address you provided');
1.1       raeburn  1814:     }
1.14      raeburn  1815:     $msg .= '</span>';
1.1       raeburn  1816:     if ($msgtext) {
                   1817:         $msg .= '<br />'.$msgtext;
                   1818:     }
1.44      raeburn  1819:     $msg .= &linkto_email_help($contact_email,$domdesc,$error);
1.8       raeburn  1820:     return $msg;
                   1821: }
                   1822: 
                   1823: sub linkto_email_help {
1.44      raeburn  1824:     my ($contact_email,$domdesc,$error) = @_;
1.8       raeburn  1825:     my $msg;
1.44      raeburn  1826:     my $href = '/adm/helpdesk';
1.1       raeburn  1827:     if ($contact_email ne '') {
                   1828:         my $escuri = &HTML::Entities::encode('/adm/createaccount','&<>"');
1.44      raeburn  1829:         $href .= '?origurl='.$escuri;
                   1830:         if ($error eq 'existinguser') {
                   1831:             my $escemail = &HTML::Entities::encode($env{'form.useremail'});
                   1832:             $href .= '&useremail='.$escemail.'&useraccount='.$escemail;
                   1833:         }
                   1834:         $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  1835:     } else {
1.55      raeburn  1836:         $msg .= '<br />'.&mt('You may wish to send an e-mail to the server administrator: [_1] for [_2].',$Apache::lonnet::perlvar{'AdmEMail'},$domdesc).'<br />';
1.1       raeburn  1837:     }
                   1838:     return $msg;
                   1839: }
                   1840: 
                   1841: sub getkeys {
                   1842:     my ($lkey,$ukey) = @_;
                   1843:     my $lextkey=hex($lkey);
                   1844:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
                   1845: 
                   1846:     my $uextkey=hex($ukey);
                   1847:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
                   1848:     return ($lextkey,$uextkey);
                   1849: }
                   1850: 
                   1851: sub serverform {
1.20      raeburn  1852:     my ($logtoken,$lonhost,$mailtoken,$courseid,$context) = @_;
1.22      raeburn  1853:     my $phase = 'username_validation';
                   1854:     my $catalog_elements;
1.20      raeburn  1855:     if ($context eq 'selfenroll') {
                   1856:         $phase = 'selfenroll_login';
                   1857:     }
1.22      raeburn  1858:     if ($courseid ne '') {
                   1859:         $catalog_elements = &Apache::lonhtmlcommon::echo_form_input(['courseid','phase']);
                   1860:     } 
                   1861:     my $output = <<ENDSERVERFORM;
1.20      raeburn  1862:   <form name="server" method="post" action="/adm/createaccount">
1.1       raeburn  1863:    <input type="hidden" name="logtoken" value="$logtoken" />
                   1864:    <input type="hidden" name="token" value="$mailtoken" />
                   1865:    <input type="hidden" name="serverid" value="$lonhost" />
                   1866:    <input type="hidden" name="uname" value="" />
                   1867:    <input type="hidden" name="upass" value="" />
1.32      raeburn  1868:    <input type="hidden" name="udom" value="" />
1.20      raeburn  1869:    <input type="hidden" name="phase" value="$phase" />
1.3       raeburn  1870:    <input type="hidden" name="courseid" value="$courseid" />
1.22      raeburn  1871:    $catalog_elements
1.1       raeburn  1872:   </form>
                   1873: ENDSERVERFORM
                   1874:     return $output;
                   1875: }
                   1876: 
1.58      raeburn  1877: sub store_request {
                   1878:     my ($dom,$username,$val,$dataref,$settings) = @_;
                   1879:     my $output;
                   1880:     my $domconfiguser = &Apache::lonnet::get_domainconfiguser($dom);
                   1881:     my $key = &escape($username);
                   1882:     my $now = time();
                   1883:     if (&Apache::lonnet::put('usernamequeue', { $key.'_'.$val => $now },
                   1884:                              $dom,$domconfiguser) eq 'ok') {
                   1885:         if (ref($dataref) eq 'HASH') {
                   1886:             my $logtoken = $dataref->{'tmpinfo'};
                   1887:             my $serverid = $dataref->{'serverid'}; 
                   1888:             if ($logtoken && $serverid) {
                   1889:                 my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$serverid);
                   1890:                 unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
                   1891:                     my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$serverid);
                   1892:                     if ($reply eq 'ok') {
                   1893:                         my ($key,$caller)=split(/&/,$tmpinfo);
                   1894:                         $dataref->{'key'} = $key;
                   1895:                         undef($dataref->{'tmpinfo'});
                   1896:                         undef($dataref->{'serverid'});
                   1897:                     }
                   1898:                 }
                   1899:             }
                   1900:         }
                   1901:         my %userrequest = ( $username => $dataref );
                   1902:         $userrequest{$username}{timestamp} = $now;
                   1903:         $userrequest{$username}{status} = $val;
                   1904:         my $notifylist;
                   1905:         if (ref($settings) eq 'HASH') {
                   1906:             if (ref($settings->{'cancreate'}) eq 'HASH') {
                   1907:                 if (ref($settings->{'cancreate'}{'notify'}) eq 'HASH') {
                   1908:                     my $notifylist = $settings->{'cancreate'}{'notify'}{'approval'};
                   1909:                     if ($notifylist) {
                   1910:                         my $sender = $domconfiguser.':'.$dom;
                   1911:                         my $domdesc = &Apache::lonnet::domain($dom,'description');
                   1912:                         my $fullname;
                   1913:                         if (ref($dataref) eq 'HASH') {
                   1914:                             if ($dataref->{'firstname'}) {
                   1915:                                 $fullname = $dataref->{'firstname'};
                   1916:                             }
                   1917:                             if ($dataref->{'lastname'}) {
                   1918:                                 $fullname .= ' '.$dataref->{'lastname'};
                   1919:                             }
                   1920:                             $fullname =~ s/^\s+|\s+$//g; 
                   1921:                         }
                   1922:                         &Apache::loncoursequeueadmin::send_selfserve_notification($notifylist,
                   1923:                                                      "$fullname ($username)",
                   1924:                                                      undef,$domdesc,$now,'usernamereq',$sender);
                   1925:                     }
                   1926:                 }
                   1927:             }
1.1       raeburn  1928:         }
1.58      raeburn  1929:         my $userresult =
                   1930:             &Apache::lonnet::put('nohist_requestedusernames',\%userrequest,$dom,$domconfiguser);
                   1931:         $output = '<p class="LC_info">'.
                   1932:                   &mt('Your request for a LON-CAPA account has been submitted for approval.').
                   1933:                   '</p>'.
                   1934:                   '<p class="LC_info">'.
                   1935:                   &mt('An e-mail will be sent to [_1] when your request has been reviewed by an administrator and action has been taken.',$username).
                   1936:                   '</p>';
1.1       raeburn  1937:     } else {
1.58      raeburn  1938:         $output = '<span class="LC_error">'.
                   1939:                   &mt('An error occurred when attempting to save your request for a LON-CAPA account.');
                   1940:                   '</span>';
1.1       raeburn  1941:     }
1.58      raeburn  1942:     return $output;
1.1       raeburn  1943: }
                   1944: 
                   1945: sub guest_format_check {
1.72.4.2  raeburn  1946:     my ($useremail,$domain,$cancreate,$settings,$usertype) = @_;
1.1       raeburn  1947:     my ($login,$format_match,$format_msg,@user_rules);
                   1948:     if (ref($settings) eq 'HASH') {
                   1949:         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
                   1950:             push(@user_rules,@{$settings->{'email_rule'}});
1.72.4.2  raeburn  1951:         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
                   1952:             if (ref($settings->{'email_rule'}->{$usertype}) eq 'ARRAY') {
                   1953:                 push(@user_rules,@{$settings->{'email_rule'}->{$usertype}});
                   1954:             }
1.1       raeburn  1955:         }
                   1956:     }
                   1957:     if (@user_rules > 0) {
                   1958:         my %rule_check = 
                   1959:             &Apache::lonnet::inst_rulecheck($domain,$useremail,undef,
1.2       raeburn  1960:                                             'selfcreate',\@user_rules);
1.1       raeburn  1961:         if (keys(%rule_check) > 0) {
                   1962:             foreach my $item (keys(%rule_check)) {
                   1963:                 if ($rule_check{$item}) {
                   1964:                     $format_match = 1;   
                   1965:                     last;
                   1966:                 }
                   1967:             }
                   1968:         }
                   1969:     }
                   1970:     if ($format_match) {
                   1971:         ($login) = ($useremail =~ /^([^\@]+)\@/);
1.58      raeburn  1972:         $format_msg = '<br />'.
                   1973:                       &mt("Your e-mail address uses the same internet domain as your institution's LON-CAPA service.").'<br />'.
                   1974:                       &mt('Creation of a LON-CAPA account with this type of e-mail address as username is not permitted.').'<br />';
1.5       raeburn  1975:         if (ref($cancreate) eq 'ARRAY') {
                   1976:             if (grep(/^login$/,@{$cancreate})) {
1.7       bisitz   1977:                 $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  1978:             }
1.1       raeburn  1979:         }
                   1980:     }
                   1981:     return $format_msg;
                   1982: }
                   1983: 
1.17      raeburn  1984: sub sso_logout_frag {
                   1985:     my ($r,$domain) = @_;
                   1986:     my $endsessionmsg;
                   1987:     if (defined($r->dir_config('lonSSOUserLogoutMessageFile_'.$domain))) {
                   1988:         my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile_'.$domain);
                   1989:         if (-e $msgfile) {
1.72.4.1  raeburn  1990:             open(my $fh,"<",$msgfile);
1.17      raeburn  1991:             $endsessionmsg = join('',<$fh>);
                   1992:             close($fh);
                   1993:         }
                   1994:     } elsif (defined($r->dir_config('lonSSOUserLogoutMessageFile'))) {
                   1995:         my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile');
                   1996:         if (-e $msgfile) {     
1.72.4.1  raeburn  1997:             open(my $fh,"<",$msgfile);
1.17      raeburn  1998:             $endsessionmsg = join('',<$fh>);
                   1999:             close($fh);
                   2000:         }
                   2001:     }
                   2002:     return $endsessionmsg;
                   2003: }
                   2004: 
1.22      raeburn  2005: sub catreturn_js {
                   2006:     return  <<"ENDSCRIPT";
                   2007: <script type="text/javascript">
1.62      raeburn  2008: // <![CDATA[
1.22      raeburn  2009: function ToSelfenroll(formname) {
                   2010:     var formidx = getFormByName(formname);
                   2011:     if (formidx > -1) {
                   2012:         document.forms[formidx].action = '/adm/selfenroll';
                   2013:         numidx = getIndexByName(formidx,'phase');
                   2014:         if (numidx > -1) {
                   2015:             document.forms[formidx].elements[numidx].value = '';   
                   2016:         }
                   2017:         numidx = getIndexByName(formidx,'context');
                   2018:         if (numidx > -1) {
                   2019:             document.forms[formidx].elements[numidx].value = '';
                   2020:         }
                   2021:     }
                   2022:     document.forms[formidx].submit();
                   2023: }
                   2024: 
                   2025: function ToCatalog(formname,caller) {
                   2026:     var formidx = getFormByName(formname);
                   2027:     if (formidx > -1) {
                   2028:         document.forms[formidx].action = '/adm/coursecatalog';
                   2029:         numidx = getIndexByName(formidx,'coursenum');
                   2030:         if (numidx > -1) {
                   2031:             if (caller != 'details') {
                   2032:                 document.forms[formidx].elements[numidx].value = '';
                   2033:             }
                   2034:         }
                   2035:     }
                   2036:     document.forms[formidx].submit();
                   2037: }
                   2038: 
                   2039: function getIndexByName(formidx,item) {
                   2040:     for (var i=0;i<document.forms[formidx].elements.length;i++) {
                   2041:         if (document.forms[formidx].elements[i].name == item) {
                   2042:             return i;
                   2043:         }
                   2044:     }
                   2045:     return -1;
                   2046: }
                   2047: 
                   2048: function getFormByName(item) {
                   2049:     for (var i=0; i<document.forms.length; i++) {
                   2050:         if (document.forms[i].name == item) {
                   2051:             return i;
                   2052:         }
                   2053:     }
                   2054:     return -1;
                   2055: }
1.62      raeburn  2056: // ]]>
1.22      raeburn  2057: </script>
                   2058: ENDSCRIPT
                   2059: 
                   2060: }
                   2061: 
1.72.4.2  raeburn  2062: sub setelements_js {
                   2063:     my ($statusforemail,$types,$usertypes,$othertitle) = @_;
                   2064:     my ($posstypes,$posstypesnames,$availabletypes);
                   2065:     if ((ref($statusforemail) eq 'ARRAY') && (ref($types) eq 'ARRAY') && 
                   2066:         (ref($usertypes) eq 'HASH')) {
                   2067:         $posstypes = join("','",@{$types},'default');
                   2068:         $posstypesnames = join("','",(map {$usertypes->{$_};} @{$types}),$othertitle);
                   2069:         $availabletypes = join("','", @{$statusforemail});
                   2070:     }
                   2071:     return  <<"ENDSCRIPT";
                   2072: <script type="text/javascript">
                   2073: // <![CDATA[
                   2074: 
                   2075: function setElements() {
                   2076:     if (document.getElementById('LC_reportstatus')) {
                   2077:         var reportnum = document.reportstatus.type.length;
                   2078:         if ((reportnum != 'undefined') && (typeof(document.reportstatus.type) != 'undefined')) {
                   2079:             for (var i=0; i<reportnum; i++) {
                   2080:                 document.reportstatus.type[i].checked = false;
                   2081:             }
                   2082:         }
                   2083:     }
                   2084: }
                   2085: 
                   2086: function checkVerification() {
                   2087:     var curr;
                   2088:     var cancreate = false;
                   2089:     var reportnum = document.reportstatus.type.length;
                   2090:     if ((reportnum == 'undefined') && (typeof(document.reportstatus.type) != 'undefined')) {
                   2091:         curr = document.reportstatus.type.value; 
                   2092:     } else if (document.reportstatus.type.length) {
                   2093:         for (var i=0; i<document.reportstatus.type.length; i++) {
                   2094:             if (document.reportstatus.type[i].checked) {
                   2095:                 curr = document.reportstatus.type[i].value;
                   2096:                 break;
                   2097:             }
                   2098:         }
                   2099:     }
                   2100:     var types = Array('$posstypes');
                   2101:     var names = Array('$posstypesnames');
                   2102:     var available = Array('$availabletypes');
                   2103:     if (available.length) {
                   2104:         for (var i=0; i<available.length; i++) {
                   2105:             if (curr == available[i]) {
                   2106:                 cancreate = true;   
                   2107:                 break;
                   2108:             }
                   2109:         }
                   2110:     }
                   2111:     if (types.length > 0) {
                   2112:         for (var j=0; j<types.length; j++) {
                   2113:             if (curr == types[j]) {
                   2114:                 if (!cancreate) {
                   2115:                     alert('Creation of an account via verification by e-mail unavailable for user type: "'+names[j]+'"');
                   2116:                     setElements();
                   2117:                 }
                   2118:                 break;
                   2119:             }
                   2120:         }
                   2121:     }
                   2122:     if (cancreate) {
                   2123:         return true;
                   2124:     } else {
                   2125:         return false;
                   2126:     }
                   2127: }
                   2128: 
                   2129: // ]]>
                   2130: </script>
                   2131: ENDSCRIPT
                   2132: 
                   2133: }
                   2134: 
                   2135: sub username_js {
                   2136:     return  <<"ENDSCRIPT";
                   2137: <script type="text/javascript">
                   2138: // <![CDATA[
                   2139: 
                   2140: function toggleUsernameDisp(caller,divid) {
                   2141:     if (document.getElementById(divid)) {
                   2142:         if (caller.checked) {
                   2143:             if (caller.value == '1') {
                   2144:                 document.getElementById(divid).style.display = 'none';
                   2145:             } else {
                   2146:                 document.getElementById(divid).style.display = 'inline';
                   2147:             }
                   2148:         }
                   2149:     }
                   2150: }
                   2151: // ]]>
                   2152: </script>
                   2153: ENDSCRIPT
                   2154: 
                   2155: }
                   2156: 
1.1       raeburn  2157: 1;

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