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

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

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