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

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

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