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

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

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