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

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

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