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

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.