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

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.16! (raeburn    6:: # $Id: createaccount.pm,v 1.40.2.5.2.15 2012/05/01 13:58:27 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') ||
1.40.2.5.2.15  (raeburn  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.',
1.40.2.5.2.16! (raeburn  418::         aftr   => 'After completing the activation process you will be directed to the course.',
        !           419::         ther   => 'There you will have access to a "self test" that will help you assess your readiness for the course.',
        !           420::         ywil   => 'You will also have access to introductory material and to the course schedule and policies.',
        !           421::         yous   => 'Your user id for the course is your email address.',
1.40.2.5.2.5  raeburn   422:     );
1.1       raeburn   423:     my $output;
1.5       raeburn   424:     if (ref($cancreate) eq 'ARRAY') {
                    425:         if (grep(/^login$/,@{$cancreate})) {
                    426:             my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    427:             if ((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
1.40.2.5.2.5  raeburn   428:                 $output = '<div class="LC_left_float"><h3>'.$lt{'crea'}.'</h3>';
                    429:                 my $submit_text = $lt{'crlc'};
                    430:                 $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 />').
                    431:                            '<br /><br />'.$lt{'type'}.'<br /><br />';
1.20      raeburn   432:                 $output .= &login_box($now,$lonhost,$courseid,$submit_text,
1.22      raeburn   433:                                       $domain,'createaccount').'</div>';
1.5       raeburn   434:             }
                    435:         }
                    436:         if (grep(/^email$/,@{$cancreate})) {
1.40.2.5.2.2  raeburn   437:             $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   438:                         '<p class="LC_info">'.
                    439:                         &mt('You are about to sign-up for the [_1]Mechanics Online[_2] course.','<em>','</em>').'<br />'.
                    440:                         $lt{'plse'}.'</p><ul>'.
                    441:                         '<li>'.$lt{'inst'}.'</li>'.
1.40.2.5.2.16! (raeburn  442::                         '<li>'.$lt{'aftr'}.'<br />'.$lt{'ther'}.'<br />'.
        !           443::                                $lt{'ywil'}.'</li>'.
1.40.2.5.2.15  (raeburn  444::                         '<li>'.$lt{'yous'}.'</li></ul>';
1.40.2.5.2.6  raeburn   445:             my $captchaform = &create_recaptcha($pubkey);
1.28      raeburn   446:             if ($captchaform) {
1.40.2.5.2.2  raeburn   447:                 my $submit_text = &mt('Sign-up');
1.28      raeburn   448:                 my $emailform = '<input type="text" name="useremail" size="25" value="" />';
                    449:                 if (grep(/^login$/,@{$cancreate})) {
1.40.2.5.2.5  raeburn   450:                     $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   451:                 } else {
                    452:                     $output .= '<br />';
                    453:                 }
1.40.2.5  raeburn   454:                 $output .=  '<form name="createaccount" method="post" onsubmit="return validate_email()" action="/adm/createaccount">'.
1.28      raeburn   455:                             &Apache::lonhtmlcommon::start_pick_box()."\n".
                    456:                             &Apache::lonhtmlcommon::row_title(&mt('E-mail address'),
                    457:                                                              'LC_pick_box_title')."\n".
                    458:                             $emailform."\n".
                    459:                             &Apache::lonhtmlcommon::row_closure(1).
                    460:                             &Apache::lonhtmlcommon::row_title(&mt('Validation'),
                    461:                                                              'LC_pick_box_title')."\n".
1.40.2.5.2.1  raeburn   462:                             $captchaform."\n".
                    463:                             &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   464:                 if ($courseid ne '') {
                    465:                     $output .= '<input type="hidden" name="courseid" value="'.$courseid.'"/>'."\n"; 
                    466:                 }
1.32      raeburn   467:                 $output .=  &Apache::lonhtmlcommon::row_closure(1).
                    468:                             &Apache::lonhtmlcommon::row_title().'<br />'.
                    469:                             '<input type="submit" name="create_with_email" value="'. 
1.28      raeburn   470:                             $submit_text.'" />'.
                    471:                             &Apache::lonhtmlcommon::row_closure(1).
                    472:                             &Apache::lonhtmlcommon::end_pick_box().'<br /><br />';
                    473:                 if ($courseid ne '') {
                    474:                     $output .= &Apache::lonhtmlcommon::echo_form_input(['courseid']);
                    475:                 }
                    476:                 $output .= '</form>';
1.5       raeburn   477:             } else {
1.28      raeburn   478:                 my $helpdesk = '/adm/helpdesk?origurl=%2fadm%2fcreateaccount';
                    479:                 if ($courseid ne '') {
                    480:                     $helpdesk .= '&courseid='.$courseid;
                    481:                 }
                    482:                 $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   483:             }
1.28      raeburn   484:             $output .= '</div>';
1.3       raeburn   485:         }
1.1       raeburn   486:     }
                    487:     if ($output eq '') {
1.16      raeburn   488:         $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   489:     } else {
                    490:         $output .= '<div class="LC_clear_float_footer"></div>';
                    491:     }
                    492:     return $output;
                    493: }
                    494: 
1.20      raeburn   495: sub login_box {
                    496:     my ($now,$lonhost,$courseid,$submit_text,$domain,$context) = @_;
                    497:     my $output;
                    498:     my %titles = &Apache::lonlocal::texthash(
                    499:                                               createaccount => 'Log-in ID',
                    500:                                               selfenroll    => 'Username',
                    501:                                             );
                    502:     my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    503:     my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
1.40.2.2  raeburn   504:     my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount:createaccount',
1.20      raeburn   505:                                        $lonhost);
                    506:     $output = &serverform($logtoken,$lonhost,undef,$courseid,$context);
1.32      raeburn   507:     my $unameform = '<input type="text" name="uname" size="20" value="" />';
                    508:     my $upassform = '<input type="password" name="upass'.$now.'" size="20" />';
1.40.2.5  raeburn   509:     $output .= '<form name="client" method="post" action="" onsubmit="return(send());">'."\n".
1.40.2.5.2.1  raeburn   510:                '<input type="hidden" name="udom" value="'.$domain.'" />'.
1.32      raeburn   511:                &Apache::lonhtmlcommon::start_pick_box()."\n".
                    512:                &Apache::lonhtmlcommon::row_title($titles{$context},
1.31      bisitz    513:                                                  'LC_pick_box_title')."\n".
                    514:                $unameform."\n".
                    515:                &Apache::lonhtmlcommon::row_closure(1)."\n".
                    516:                &Apache::lonhtmlcommon::row_title(&mt('Password'),
                    517:                                                 'LC_pick_box_title')."\n".
1.40.2.5.2.1  raeburn   518:                $upassform.
                    519:                &Apache::lonhtmlcommon::row_closure(1).
1.31      bisitz    520:                &Apache::lonhtmlcommon::row_title().
1.32      raeburn   521:                '<br /><input type="submit" name="username_validation" value="'.
                    522:                $submit_text.'" />'."\n";
                    523:     if ($context eq 'selfenroll') {
                    524:         $output .= '<br /><br /><table width="100%"><tr><td align="right">'.
                    525:                    '<span class="LC_fontsize_medium">'.
                    526:                    '<a href="/adm/resetpw">'.&mt('Forgot password?').'</a>'.
                    527:                    '</span></td></tr></table>'."\n";
                    528:     }
                    529:     $output .= &Apache::lonhtmlcommon::row_closure(1)."\n".
                    530:                &Apache::lonhtmlcommon::end_pick_box().'<br />'."\n";
1.34      bisitz    531:     $output .= '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
                    532:                '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
1.22      raeburn   533:                '</form>';
1.20      raeburn   534:     return $output;
                    535: }
                    536: 
1.1       raeburn   537: sub process_email_request {
                    538:     my ($useremail,$domain,$domdesc,$contact_name,$contact_email,$cancreate,
1.40.2.5.2.6  raeburn   539:         $server,$settings,$courseid,$privkey) = @_;
1.40.2.5.2.1  raeburn   540:     $useremail = lc($env{'form.useremail'});
1.1       raeburn   541:     my $output;
1.5       raeburn   542:     if (ref($cancreate) eq 'ARRAY') {
                    543:         if (!grep(/^email$/,@{$cancreate})) {
                    544:             $output = &invalid_state('noemails',$domdesc,
                    545:                                      $contact_name,$contact_email);
                    546:             return $output;
                    547:         } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    548:             $output = &invalid_state('baduseremail',$domdesc,
1.1       raeburn   549:                                      $contact_name,$contact_email);
                    550:             return $output;
                    551:         } else {
1.5       raeburn   552:             my $uhome = &Apache::lonnet::homeserver($useremail,$domain);
                    553:             if ($uhome ne 'no_host') {
                    554:                 $output = &invalid_state('existinguser',$domdesc,
1.40.2.5.2.1  raeburn   555:                                          $contact_name,$contact_email,'',$useremail);
1.1       raeburn   556:                 return $output;
1.5       raeburn   557:             } else {
1.40.2.5.2.1  raeburn   558:                  my $captcha = Captcha::reCAPTCHA->new;
                    559:                  my $captcha_result =
                    560:                      $captcha->check_answer(
1.40.2.5.2.6  raeburn   561:                                             $privkey,
1.40.2.5.2.1  raeburn   562:                                             $ENV{'REMOTE_ADDR'},
                    563:                                             $env{'form.recaptcha_challenge_field'},
                    564:                                             $env{'form.recaptcha_response_field'},
                    565:                                            );
1.40.2.5.2.3  raeburn   566:                 # PRIVATE key from https://www.google.com/recaptcha
1.40.2.5.2.1  raeburn   567:                 if (!$captcha_result->{is_valid}) {
1.5       raeburn   568:                     $output = &invalid_state('captcha',$domdesc,$contact_name,
1.40.2.5.2.1  raeburn   569:                                              $contact_email);
                    570: 
1.5       raeburn   571:                     return $output;
                    572:                 }
                    573:                 my $uhome=&Apache::lonnet::homeserver($useremail,$domain);
                    574:                 if ($uhome eq 'no_host') {
1.23      raeburn   575:                     my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts);
                    576:                     &call_rulecheck($useremail,$domain,\%alerts,\%rulematch,
1.40      raeburn   577:                                     \%inst_results,\%curr_rules,\%got_rules,'username');
1.23      raeburn   578:                     if (ref($alerts{'username'}) eq 'HASH') {
                    579:                         if (ref($alerts{'username'}{$domain}) eq 'HASH') {
1.5       raeburn   580:                             if ($alerts{'username'}{$domain}{$useremail}) {
                    581:                                 $output = &invalid_state('userrules',$domdesc,
                    582:                                                          $contact_name,$contact_email);
                    583:                                 return $output;
                    584:                             }
1.1       raeburn   585:                         }
                    586:                     }
1.5       raeburn   587:                     my $format_msg = 
                    588:                         &guest_format_check($useremail,$domain,$cancreate,
                    589:                                             $settings);
                    590:                     if ($format_msg) {
                    591:                         $output = &invalid_state('userformat',$domdesc,$contact_name,
                    592:                                                  $contact_email,$format_msg);
                    593:                         return $output;
                    594:                     }
1.1       raeburn   595:                 }
                    596:             }
                    597:         }
1.5       raeburn   598:         $output = &send_token($domain,$useremail,$server,$domdesc,$contact_name,
                    599:                           $contact_email,$courseid);
1.1       raeburn   600:     }
                    601:     return $output;
                    602: }
                    603: 
1.23      raeburn   604: sub call_rulecheck {
                    605:     my ($uname,$udom,$alerts,$rulematch,$inst_results,$curr_rules,
                    606:         $got_rules,$tocheck) = @_;
                    607:     my ($checkhash,$checks);
                    608:     $checkhash->{$uname.':'.$udom} = { 'newuser' => 1, };
                    609:     if ($tocheck eq 'username') {
                    610:         $checks = { 'username' => 1 };
                    611:     }
                    612:     &Apache::loncommon::user_rule_check($checkhash,$checks,
                    613:            $alerts,$rulematch,$inst_results,$curr_rules,
                    614:            $got_rules);
                    615:     return;
                    616: }
                    617: 
1.1       raeburn   618: sub send_token {
1.3       raeburn   619:     my ($domain,$email,$server,$domdesc,$contact_name,$contact_email,$courseid) = @_;
1.14      raeburn   620:     my $msg = '<h3>'.&mt('Account creation status').'</h3>'.
1.40.2.5.2.10  (raeburn  621::               &mt('Thank you for your request to create a new Mechanics Online account.').
1.14      raeburn   622:               '<br /><br />';
1.1       raeburn   623:     my $now = time;
                    624:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    625:                 'time'       => $now,
                    626:                 'domain'     => $domain,
1.3       raeburn   627:                 'username'   => $email,
                    628:                 'courseid'   => $courseid);
1.40.2.2  raeburn   629:     my $token = &Apache::lonnet::tmpput(\%info,$server,'createaccount');
1.1       raeburn   630:     if ($token !~ /^error/ && $token ne 'no_such_host') {
                    631:         my $esc_token = &escape($token);
1.40.2.5.2.9  raeburn   632:         my $mailsubj = &mt('Your Mechanics Online Course Account');
                    633:         my $msgfrom  = &mt('Mechanics Online Course Staff');
                    634:         my $mailmsg =  &mt('Greetings.')."\n\n".
                    635:                        &mt('Thank you for your interest in our Online Mechanics Course.').
                    636:                        ' '.
                    637:                        &mt('In order to complete the registration process, please open the following link in your web browser:').
                    638:                        "\n\n".
1.40.2.5.2.10  (raeburn  639::                        &Apache::lonnet::absolute_url().'/adm/createaccount?token='.$esc_token.
1.40.2.5.2.9  raeburn   640:                        "\n\n".
                    641:                        &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.').
                    642:                        ' '.
1.40.2.5.2.11  (raeburn  643::                        &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   644:                        "\n\n".
                    645:                        &mt('Best Regards,').
                    646:                        "\n\n".
                    647:                        &mt('RELATE Group')."\n";
                    648: 
                    649:         my $result = &Apache::resetpw::send_mail('MIT',$email,$mailmsg,
                    650:                                                  $msgfrom,$contact_email,
                    651:                                                  $mailsubj);
1.1       raeburn   652:         if ($result eq 'ok') {
1.40.2.5.2.2  raeburn   653:             $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   654:         } else {
1.14      raeburn   655:             $msg .= '<span class="LC_error">'.
                    656:                     &mt('An error occurred when sending a message to the e-mail address you provided.').'</span><br />'.
                    657:                     ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   658:         }
                    659:     } else {
1.14      raeburn   660:         $msg .= '<span class="LC_error">'.
                    661:                 &mt('An error occurred creating a token required for the account creation process.').'</span><br />'.
                    662:                 ' '.&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   663:     }
                    664:     return $msg;
                    665: }
                    666: 
                    667: sub process_mailtoken {
1.3       raeburn   668:     my ($r,$token,$contact_name,$contact_email,$domain,$domdesc,$lonhost,
                    669:         $include,$start_page) = @_;
                    670:     my ($msg,$nostart,$noend);
1.1       raeburn   671:     my %data = &Apache::lonnet::tmpget($token);
                    672:     my $now = time;
                    673:     if (keys(%data) == 0) {
1.9       raeburn   674:         $msg = &mt('Sorry, the URL you provided to complete creation of a new LON-CAPA account was invalid.')
                    675:                .' '.&mt('Either the token included in the URL has been deleted or the URL you provided was invalid.')
                    676:                .' '.&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   677:         return $msg;
                    678:     }
                    679:     if (($data{'time'} =~ /^\d+$/) &&
                    680:         ($data{'domain'} ne '') &&
                    681:         ($data{'username'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/)) {
                    682:         if ($now - $data{'time'} < 7200) {
                    683:             if ($env{'form.phase'} eq 'createaccount') {
1.3       raeburn   684:                 my ($result,$output) = &create_account($r,$domain,$lonhost,
1.1       raeburn   685:                                                        $data{'username'},$domdesc);
                    686:                 if ($result eq 'ok') {
                    687:                     $msg = $output; 
1.17      raeburn   688:                     my $shownow = &Apache::lonlocal::locallocaltime($now);
1.40.2.5.2.1  raeburn   689:                     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   690:                     my $mailresult = &Apache::resetpw::send_mail($domdesc,$data{'email'},
                    691:                                                                  $mailmsg,$contact_name,
                    692:                                                                  $contact_email);
                    693:                     if ($mailresult eq 'ok') {
                    694:                         $msg .= &mt('An e-mail confirming creation of your new LON-CAPA account has been sent to [_1].',$data{'username'});
                    695:                     } else {
                    696:                         $msg .= &mt('An error occurred when sending e-mail to [_1] confirming creation of your LON-CAPA account.',$data{'username'});
                    697:                     }
1.3       raeburn   698:                     my %form = &start_session($r,$data{'username'},$domain, 
                    699:                                               $lonhost,$data{'courseid'},
                    700:                                               $token);
                    701:                     $nostart = 1;
                    702:                     $noend = 1;
1.1       raeburn   703:                 } else {
1.7       bisitz    704:                     $msg .= &mt('A problem occurred when attempting to create your new LON-CAPA account.')
                    705:                            .'<br />'.$output
                    706: #                           .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>');
                    707:                            .&mt('Please contact the [_1] ([_2]) for assistance.',$contact_name,$contact_email);
1.1       raeburn   708:                 }
1.3       raeburn   709:                 my $delete = &Apache::lonnet::tmpdel($token);
1.1       raeburn   710:             } else {
1.3       raeburn   711:                 $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 />';
                    712:                 $msg .= &print_dataentry_form($r,$domain,$lonhost,$include,$token,$now,$data{'username'},$start_page);
                    713:                 $nostart = 1;
1.1       raeburn   714:             }
                    715:         } else {
1.7       bisitz    716:             $msg = &mt('Sorry, the token generated when you requested creation of an account has expired.')
                    717:                   .' '.&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   718:             }
1.1       raeburn   719:     } else {
1.7       bisitz    720:         $msg .= &mt('Sorry, the URL generated when you requested creation of an account contained incomplete information.')
                    721:                .' '.&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   722:     }
1.3       raeburn   723:     return ($msg,$nostart,$noend);
                    724: }
                    725: 
                    726: sub start_session {
                    727:     my ($r,$username,$domain,$lonhost,$courseid,$token) = @_;
                    728:     my %form = (
                    729:                 uname => $username,
                    730:                 udom  => $domain,
                    731:                );
                    732:     my $firsturl = '/adm/roles';
                    733:     if (defined($courseid)) {
                    734:         $courseid = &validate_course($courseid);
                    735:         if ($courseid ne '') {
                    736:             $form{'courseid'} = $courseid;
1.22      raeburn   737:             $firsturl = '/adm/selfenroll?courseid='.$courseid;
1.3       raeburn   738:         }
                    739:     }
                    740:     if ($r->dir_config('lonBalancer') eq 'yes') {
                    741:         &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
                    742:                                   $lonhost,'noredirect',undef,\%form);
1.19      raeburn   743:         if ($token ne '') { 
                    744:             my $delete = &Apache::lonnet::tmpdel($token);
                    745:         }
1.3       raeburn   746:         $r->internal_redirect('/adm/switchserver');
                    747:     } else {
                    748:         &Apache::lonauth::success($r,$form{'uname'},$form{'udom'},
                    749:                                   $lonhost,$firsturl,undef,\%form);
                    750:     }
                    751:     return %form;
1.1       raeburn   752: }
                    753: 
1.3       raeburn   754: 
1.1       raeburn   755: sub print_dataentry_form {
1.3       raeburn   756:     my ($r,$domain,$lonhost,$include,$mailtoken,$now,$username,$start_page) = @_;
1.1       raeburn   757:     my ($error,$output);
1.3       raeburn   758:     &print_header($r,$start_page);
1.1       raeburn   759:     if (open(my $jsh,"<$include/londes.js")) {
                    760:         while(my $line = <$jsh>) {
                    761:             $r->print($line);
                    762:         }
                    763:         close($jsh);
1.3       raeburn   764:         $output .= &javascript_setforms($now)."\n".&javascript_checkpass($now);
1.1       raeburn   765:         my ($lkey,$ukey) = &Apache::lonpreferences::des_keys();
                    766:         my ($lextkey,$uextkey) = &getkeys($lkey,$ukey);
1.40.2.2  raeburn   767:         my $logtoken=Apache::lonnet::reply('tmpput:'.$ukey.$lkey.'&createaccount:createaccount',
1.1       raeburn   768:                                            $lonhost);
1.32      raeburn   769:         my $formtag = '<form name="server" method="post" target="_top" action="/adm/createaccount">';
                    770:         my ($datatable,$rowcount) =
                    771:             &Apache::loncreateuser::personal_data_display($username,$domain,
                    772:                                                           'email','selfcreate');
                    773:         if ($rowcount) {
                    774:             $output .= '<div class="LC_left_float">'.$formtag.$datatable;
                    775:         } else {
                    776:             $output .= $formtag;
1.1       raeburn   777:         }
                    778:         $output .= <<"ENDSERVERFORM";
                    779:    <input type="hidden" name="logtoken" value="$logtoken" />
                    780:    <input type="hidden" name="token" value="$mailtoken" />
                    781:    <input type="hidden" name="serverid" value="$lonhost" />
                    782:    <input type="hidden" name="uname" value="" />
                    783:    <input type="hidden" name="upass" value="" />
1.32      raeburn   784:    <input type="hidden" name="udom" value="" />
1.1       raeburn   785:    <input type="hidden" name="phase" value="createaccount" />
1.32      raeburn   786:   </form>
1.1       raeburn   787: ENDSERVERFORM
1.32      raeburn   788:         if ($rowcount) {
                    789:             $output .= '</div>'.
                    790:                        '<div class="LC_left_float">';
                    791:         }
1.1       raeburn   792:         my $upassone = '<input type="password" name="upass'.$now.'" size="10" />';
                    793:         my $upasstwo = '<input type="password" name="upasscheck'.$now.'" size="10" />';
                    794:         my $submit_text = &mt('Create LON-CAPA account');
1.32      raeburn   795:         $output .= '<h3>'.&mt('Login Data').'</h3>'."\n".
1.40.2.5  raeburn   796:                    '<form name="client" method="post" action="" '.
1.32      raeburn   797:                    'onsubmit="return checkpass();">'."\n".
1.1       raeburn   798:                    &Apache::lonhtmlcommon::start_pick_box()."\n".
                    799:                    &Apache::lonhtmlcommon::row_title(&mt('Username'),
1.32      raeburn   800:                                                      'LC_pick_box_title',
                    801:                                                      'LC_oddrow_value')."\n".
1.1       raeburn   802:                    $username."\n".
                    803:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    804:                    &Apache::lonhtmlcommon::row_title(&mt('Password'),
1.32      raeburn   805:                                                     'LC_pick_box_title',
                    806:                                                     'LC_oddrow_value')."\n".
1.1       raeburn   807:                    $upassone."\n".
                    808:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    809:                    &Apache::lonhtmlcommon::row_title(&mt('Confirm password'),
1.32      raeburn   810:                                                      'LC_pick_box_title',
                    811:                                                      'LC_oddrow_value')."\n".
                    812:                    $upasstwo.
                    813:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    814:                    &Apache::lonhtmlcommon::row_title()."\n".
                    815:                    '<br /><input type="submit" name="createaccount" value="'.
                    816:                    $submit_text.'" />'.
1.1       raeburn   817:                    &Apache::lonhtmlcommon::row_closure(1)."\n".
                    818:                    &Apache::lonhtmlcommon::end_pick_box()."\n".
1.32      raeburn   819:                    '<input type="hidden" name="uname" value="'.$username.'" />'."\n".
                    820:                    '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
                    821:                    '<input type="hidden" name="lextkey" value="'.$lextkey.'" />'."\n".
                    822:                    '<input type="hidden" name="uextkey" value="'.$uextkey.'" />'."\n".
                    823:                    '</form>';
                    824:         if ($rowcount) {
                    825:             $output .= '</div>'."\n".
                    826:                        '<div class="LC_clear_float_footer"></div>'."\n";
                    827:         }
1.1       raeburn   828:     } else {
1.7       bisitz    829:         $output = &mt('Could not load javascript file [_1]','<tt>londes.js</tt>');
1.1       raeburn   830:     }
1.3       raeburn   831:     return $output;
1.1       raeburn   832: }
                    833: 
1.35      raeburn   834: sub get_creation_controls {
                    835:     my ($domain,$usercreation) = @_;
                    836:     my (@cancreate,@statustocreate);
                    837:     if (ref($usercreation) eq 'HASH') {
                    838:         if (ref($usercreation->{'cancreate'}) eq 'HASH') {
                    839:             if (ref($usercreation->{'cancreate'}{'statustocreate'}) eq 'ARRAY') {
                    840:                 @statustocreate = @{$usercreation->{'cancreate'}{'statustocreate'}};
1.40.2.4  raeburn   841:                 if (@statustocreate == 0) {
                    842:                     my ($othertitle,$usertypes,$types) =
                    843:                         &Apache::loncommon::sorted_inst_types($domain);
                    844:                     if (ref($types) eq 'ARRAY') {
                    845:                         if (@{$types} == 0) {
                    846:                             @statustocreate = ('default');
                    847:                         }
                    848:                     } else {
                    849:                         @statustocreate = ('default');
                    850:                     }
                    851:                 }
1.35      raeburn   852:             } else {
                    853:                 @statustocreate = ('default');
                    854:                 my ($othertitle,$usertypes,$types) =
                    855:                     &Apache::loncommon::sorted_inst_types($domain);
                    856:                 if (ref($types) eq 'ARRAY') {
                    857:                     push(@statustocreate,@{$types});
                    858:                 }
                    859:             }
                    860:             if (ref($usercreation->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                    861:                 @cancreate = @{$usercreation->{'cancreate'}{'selfcreate'}};
                    862:             } elsif (($usercreation->{'cancreate'}{'selfcreate'} ne 'none') &&
                    863:                      ($usercreation->{'cancreate'}{'selfcreate'} ne '')) {
                    864:                 @cancreate = ($usercreation->{'cancreate'}{'selfcreate'});
                    865:             }
                    866:         }
                    867:     }
                    868:     return (\@cancreate,\@statustocreate);
                    869: }
                    870: 
1.1       raeburn   871: sub create_account {
1.3       raeburn   872:     my ($r,$domain,$lonhost,$username,$domdesc) = @_;
1.1       raeburn   873:     my ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    874:                                                           $env{'form.serverid'}); 
                    875:     # Error messages
1.7       bisitz    876:     my $error     = '<span class="LC_error">'.&mt('Error:').' ';
1.1       raeburn   877:     my $end       = '</span><br /><br />';
                    878:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                    879:                     &mt('Return to previous page').'</a>'.
                    880:                     &Apache::loncommon::end_page();
                    881:     if ($retrieved eq 'ok') {
1.22      raeburn   882:         if ($env{'form.courseid'} ne '') {
1.1       raeburn   883:             my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
                    884:             if ($result eq 'fail') {
                    885:                 $output = $error.&mt('Invalid ID format').$end.
                    886:                           $userchkmsg.$rtnlink;
                    887:                 return ('fail',$output);
                    888:             }
                    889:         }
                    890:     } else {
                    891:         return ('fail',$error.$output.$end.$rtnlink);
                    892:     }
                    893:     # Call modifyuser
                    894:     my $result = 
                    895:         &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
                    896:                                     'internal',$upass,$env{'form.cfirstname'},
                    897:                                     $env{'form.cmiddlename'},$env{'form.clastname'},
                    898:                                     $env{'form.cgeneration'},undef,undef,$username);
1.7       bisitz    899:     $output = &mt('Generating user: [_1]',$result);
1.1       raeburn   900:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
1.7       bisitz    901:     $output .= '<br />'.&mt('Home server: [_1]',$uhome).' '.
1.1       raeburn   902:               &Apache::lonnet::hostname($uhome).'<br /><br />';
                    903:     return ('ok',$output);
                    904: }
                    905: 
                    906: sub username_validation {
1.19      raeburn   907:     my ($r,$username,$domain,$domdesc,$contact_name,$contact_email,$courseid,
1.35      raeburn   908:         $lonhost,$statustocreate) = @_;
1.1       raeburn   909:     my ($retrieved,$output,$upass);
                    910: 
                    911:     $username= &LONCAPA::clean_username($username);
                    912:     $domain = &LONCAPA::clean_domain($domain);
                    913:     my $uhome = &Apache::lonnet::homeserver($username,$domain);
                    914: 
                    915:     ($retrieved,$output,$upass) = &process_credentials($env{'form.logtoken'},
                    916:                                                        $env{'form.serverid'});
1.19      raeburn   917:     if ($retrieved ne 'ok') {
                    918:         return ('fail',$output);
                    919:     }
                    920:     if ($uhome ne 'no_host') {
                    921:         my $result = &Apache::lonnet::authenticate($username,$upass,$domain);
                    922:         if ($result ne 'no_host') { 
                    923:             my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
                    924:             $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.');
                    925:             return ('existingaccount',$output);
                    926:         } else {
1.22      raeburn   927:             $output = &login_failure_msg($courseid);
1.19      raeburn   928:         }
                    929:     } else {
1.1       raeburn   930:         my $primlibserv = &Apache::lonnet::domain($domain,'primary');
                    931:         my $authok;
                    932:         my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
                    933:         if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth')) {
                    934:             my $checkdefauth = 1;
                    935:             $authok = 
                    936:                 &Apache::lonnet::reply("encrypt:auth:$domain:$username:$upass:$checkdefauth",$primlibserv);
                    937:         } else {
                    938:             $authok = 'non_authorized';
                    939:         }
                    940:         if ($authok eq 'authorized') {
1.18      raeburn   941:             $output = &username_check($username,$domain,$domdesc,$courseid,$lonhost,
1.35      raeburn   942:                                       $contact_email,$contact_name,undef,
                    943:                                       $statustocreate);
1.4       raeburn   944:         } else {
1.22      raeburn   945:             $output = &login_failure_msg($courseid);
1.4       raeburn   946:         }
                    947:     }
1.19      raeburn   948:     return ('ok',$output);
1.4       raeburn   949: }
                    950: 
1.22      raeburn   951: sub login_failure_msg {
                    952:     my ($courseid) = @_;
                    953:     my $url;
                    954:     if ($courseid ne '') {
                    955:         $url = "/adm/selfenroll?courseid=".$courseid;
                    956:     } else {
                    957:         $url = "/adm/createaccount";
                    958:     }
                    959:     my $output = '<h4>'.&mt('Authentication failed').'</h4><div class="LC_warning">'.
                    960:                  &mt('Username and/or password could not be authenticated.').
                    961:                  '</div>'.
                    962:                  &mt('Please check the username and password.').'<br /><br />';
                    963:                  '<a href="'.$url.'">'.&mt('Try again').'</a>';
                    964:     return $output;
                    965: }
                    966: 
1.4       raeburn   967: sub username_check {
1.35      raeburn   968:     my ($username,$domain,$domdesc,$courseid,$lonhost,$contact_email,
                    969:         $contact_name,$sso_logout,$statustocreate) = @_;
1.23      raeburn   970:     my (%rulematch,%inst_results,$checkfail,$rowcount,$editable,$output,$msg,
1.18      raeburn   971:         %alerts,%curr_rules,%got_rules);
1.23      raeburn   972:     &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1.40      raeburn   973:                     \%inst_results,\%curr_rules,\%got_rules,'username');
1.4       raeburn   974:     if (ref($alerts{'username'}) eq 'HASH') {
                    975:         if (ref($alerts{'username'}{$domain}) eq 'HASH') {
                    976:             if ($alerts{'username'}{$domain}{$username}) {
                    977:                 if (ref($curr_rules{$domain}) eq 'HASH') {
1.18      raeburn   978:                     $output =
1.17      raeburn   979:                         &Apache::loncommon::instrule_disallow_msg('username',$domdesc,1,
                    980:                                                                   'selfcreate').
1.4       raeburn   981:                         &Apache::loncommon::user_rule_formats($domain,$domdesc,
                    982:                                 $curr_rules{$domain}{'username'},'username');
1.1       raeburn   983:                 }
1.18      raeburn   984:                 $checkfail = 'username';
1.1       raeburn   985:             }
1.4       raeburn   986:         }
                    987:     }
1.18      raeburn   988:     if (!$checkfail) {
1.35      raeburn   989:         if (ref($statustocreate) eq 'ARRAY') {
                    990:             $checkfail = 'inststatus';
                    991:             if (ref($inst_results{$username.':'.$domain}{inststatus}) eq 'ARRAY') {
                    992:                 foreach my $inststatus (@{$inst_results{$username.':'.$domain}{inststatus}}) {
                    993:                     if (grep(/^\Q$inststatus\E$/,@{$statustocreate})) {
                    994:                         undef($checkfail);
                    995:                         last;
                    996:                     }
                    997:                 }
                    998:             } elsif (grep(/^default$/,@{$statustocreate})) {
                    999:                 undef($checkfail);
                   1000:             }
                   1001:         }
                   1002:     }
                   1003:     if (!$checkfail) {
1.18      raeburn  1004:         $output = '<form method="post" action="/adm/createaccount">';
                   1005:         (my $datatable,$rowcount,$editable) = 
                   1006:             &Apache::loncreateuser::personal_data_display($username,$domain,1,'selfcreate',
                   1007:                                                          $inst_results{$username.':'.$domain});
                   1008:         if ($rowcount > 0) {
                   1009:             $output .= $datatable;
                   1010:         }
                   1011:         $output .=  '<br /><br /><input type="hidden" name="uname" value="'.$username.'" />'."\n".
                   1012:                     '<input type="hidden" name="udom" value="'.$domain.'" />'."\n".
                   1013:                     '<input type="hidden" name="phase" value="username_activation" />';
                   1014:         my $now = time;
                   1015:         my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                   1016:                     'time'       => $now,
                   1017:                     'domain'     => $domain,
                   1018:                     'username'   => $username);
1.40.2.2  raeburn  1019:         my $authtoken = &Apache::lonnet::tmpput(\%info,$lonhost,'createaccount');
1.18      raeburn  1020:         if ($authtoken !~ /^error/ && $authtoken ne 'no_such_host') {
                   1021:             $output .= '<input type="hidden" name="authtoken" value="'.&HTML::Entities::encode($authtoken,'&<>"').'" />';
                   1022:         } else {
                   1023:             $output = &mt('An error occurred when storing a token').'<br />'.
                   1024:                       &mt('You will not be able to proceed to the next stage of account creation').
                   1025:                       &linkto_email_help($contact_email,$domdesc);
                   1026:             $checkfail = 'authtoken';
                   1027:         }
                   1028:     }
                   1029:     if ($checkfail) { 
1.40.2.4  raeburn  1030:         $msg = '<br /><h4>'.&mt('Account creation unavailable').'</h4>';
1.18      raeburn  1031:         if ($checkfail eq 'username') {
                   1032:             $msg .= '<span class="LC_warning">'.
                   1033:                      &mt('A LON-CAPA account may not be created with the username you use.').
                   1034:                      '</span><br /><br />'.$output;
                   1035:         } elsif ($checkfail eq 'authtoken') {
                   1036:             $msg .= '<span class="LC_error">'.&mt('Error creating token.').'</span>'.
                   1037:                     '<br />'.$output;
1.35      raeburn  1038:         } elsif ($checkfail eq 'inststatus') {
                   1039:             $msg .= '<span class="LC_warning">'.
                   1040:                      &mt('You are not permitted to create a LON-CAPA account.').
                   1041:                      '</span><br /><br />'.$output;
1.18      raeburn  1042:         }
                   1043:         $msg .= &mt('Please contact the [_1] ([_2]) for assistance.',
                   1044:                 $contact_name,$contact_email).'<br /><hr />'.
                   1045:                 $sso_logout;
                   1046:         &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  1047:     } else {
1.18      raeburn  1048:         if ($courseid ne '') {
                   1049:             $output .= '<input type="hidden" name="courseid" value="'.$courseid.'" />';
                   1050:         }
                   1051:         $output .= '<input type="submit" name="newaccount" value="'.
                   1052:                    &mt('Create LON-CAPA account').'" /></form>';
                   1053:         if ($rowcount) {
                   1054:             if ($editable) {
1.22      raeburn  1055:                 if ($courseid ne '') { 
1.40.2.4  raeburn  1056:                     $msg = '<br /><h4>'.&mt('User information').'</h4>';
1.22      raeburn  1057:                 }
                   1058:                 $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  1059:             } else {
1.22      raeburn  1060:                  if ($courseid ne '') {
                   1061:                      $msg = '<h4>'.&mt('Review user information').'</h4>';
                   1062:                  }
                   1063:                  $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  1064:             }
                   1065:         } else {
1.22      raeburn  1066:             if ($courseid ne '') {
                   1067:                 $msg = '<h4>'.&mt('Confirmation').'</h4>';
                   1068:             }
                   1069:             $msg .= &mt('Confirm that you wish to create an account.');
1.18      raeburn  1070:         }
                   1071:         $msg .= $output;
                   1072:     }
                   1073:     return $msg;
1.1       raeburn  1074: }
                   1075: 
                   1076: sub username_activation {
1.3       raeburn  1077:     my ($r,$username,$domain,$domdesc,$lonhost,$courseid) = @_;
1.1       raeburn  1078:     my $output;
1.7       bisitz   1079:     my $error     = '<span class="LC_error">'.&mt('Error:').' ';
1.1       raeburn  1080:     my $end       = '</span><br /><br />';
                   1081:     my $rtnlink   = '<a href="javascript:history.back();" />'.
                   1082:                     &mt('Return to previous page').'</a>'.
                   1083:                     &Apache::loncommon::end_page();
                   1084:     my %domdefaults = &Apache::lonnet::get_domain_defaults($domain);
1.8       raeburn  1085:     my %data = &Apache::lonnet::tmpget($env{'form.authtoken'});
                   1086:     my $now = time;
                   1087:     my $earlyout;
                   1088:     my $timeout = 300;
                   1089:     if (keys(%data) == 0) {
                   1090:         $output = &mt('Sorry, your authentication has expired.');
                   1091:         $earlyout = 'fail';
                   1092:     }
                   1093:     if (($data{'time'} !~ /^\d+$/) ||
                   1094:         ($data{'domain'} ne $domain) || 
                   1095:         ($data{'username'} ne $username)) {
                   1096:         $earlyout = 'fail';
                   1097:         $output = &mt('The credentials you provided could not be verified.');   
                   1098:     } elsif ($now - $data{'time'} > $timeout) {
                   1099:         $earlyout = 'fail';
                   1100:         $output = &mt('Sorry, your authentication has expired.');
                   1101:     }
                   1102:     if ($earlyout ne '') {
                   1103:         $output .= '<br />'.&mt('Please [_1]start again[_2].','<a href="/adm/createaccount">','</a>');
                   1104:         return($earlyout,$output);
                   1105:     }
1.3       raeburn  1106:     if ((($domdefaults{'auth_def'} =~/^krb(4|5)$/) && 
                   1107:          ($domdefaults{'auth_arg_def'} ne '')) || 
                   1108:         ($domdefaults{'auth_def'} eq 'localauth')) {
1.22      raeburn  1109:         if ($env{'form.courseid'} ne '') {
1.1       raeburn  1110:             my ($result,$userchkmsg) = &check_id($username,$domain,$domdesc);
                   1111:             if ($result eq 'fail') {
                   1112:                 $output = $error.&mt('Invalid ID format').$end.
                   1113:                           $userchkmsg.$rtnlink;
                   1114:                 return ('fail',$output);
                   1115:             }
                   1116:         }
                   1117:         # Call modifyuser
1.23      raeburn  1118:         my (%rulematch,%inst_results,%curr_rules,%got_rules,%alerts,%info);
                   1119:         &call_rulecheck($username,$domain,\%alerts,\%rulematch,
1.40      raeburn  1120:                         \%inst_results,\%curr_rules,\%got_rules);
1.23      raeburn  1121:         my @userinfo = ('firstname','middlename','lastname','generation',
                   1122:                         'permanentemail','id');
                   1123:         my %canmodify = 
                   1124:             &Apache::loncreateuser::selfcreate_canmodify('selfcreate',$domain,
                   1125:                                                          \@userinfo,\%inst_results);
                   1126:         foreach my $item (@userinfo) {
                   1127:             if ($canmodify{$item}) {
                   1128:                 $info{$item} = $env{'form.c'.$item};
                   1129:             } else {
                   1130:                 $info{$item} = $inst_results{$username.':'.$domain}{$item}; 
                   1131:             }
                   1132:         }
                   1133:         if (ref($inst_results{$username.':'.$domain}{'inststatus'}) eq 'ARRAY') {
                   1134:             my @inststatuses = @{$inst_results{$username.':'.$domain}{'inststatus'}};
                   1135:             $info{'inststatus'} = join(':',map { &escape($_); } @inststatuses);
                   1136:         }
1.1       raeburn  1137:         my $result =
1.23      raeburn  1138:             &Apache::lonnet::modifyuser($domain,$username,$env{'form.cid'},
1.1       raeburn  1139:                           $domdefaults{'auth_def'},
1.23      raeburn  1140:                           $domdefaults{'auth_arg_def'},$info{'firstname'},
                   1141:                           $info{'middlename'},$info{'lastname'},
                   1142:                           $info{'generation'},undef,undef,
                   1143:                           $info{'permanentemail'},$info{'inststatus'});
1.3       raeburn  1144:         if ($result eq 'ok') {
1.8       raeburn  1145:             my $delete = &Apache::lonnet::tmpdel($env{'form.authtoken'});
1.3       raeburn  1146:             $output = &mt('A LON-CAPA account has been created for username: [_1] in domain: [_2].',$username,$domain);
                   1147:             my %form = &start_session($r,$username,$domain,$lonhost,$courseid);
                   1148:             my $nostart = 1;
                   1149:             return ('ok',$output,$nostart);
                   1150:         } else {
                   1151:             $output = &mt('Account creation failed for username: [_1] in domain: [_2].',$username,$domain).'<br /><span class="LC_error">'.&mt('Error: [_1]',$result).'</span>';
                   1152:             return ('fail',$output);
                   1153:         }
1.1       raeburn  1154:     } else {
1.7       bisitz   1155:         $output = &mt('User account creation is not available for the current default authentication type.')."\n";
1.1       raeburn  1156:         return('fail',$output);
                   1157:     }
                   1158: }
                   1159: 
                   1160: sub check_id {
                   1161:     my ($username,$domain,$domdesc) = @_;
                   1162:     # Check ID format
                   1163:     my (%alerts,%rulematch,%inst_results,%curr_rules,%checkhash);
                   1164:     my %checks = ('id' => 1);
                   1165:     %{$checkhash{$username.':'.$domain}} = (
                   1166:                                             'newuser' => 1,
                   1167:                                             'id' => $env{'form.cid'},
                   1168:                                            );
                   1169:     &Apache::loncommon::user_rule_check(\%checkhash,\%checks,\%alerts,
                   1170:                                         \%rulematch,\%inst_results,\%curr_rules);
                   1171:     if (ref($alerts{'id'}) eq 'HASH') {
                   1172:         if (ref($alerts{'id'}{$domain}) eq 'HASH') {
                   1173:             if ($alerts{'id'}{$domain}{$env{'form.cid'}}) {
                   1174:                 my $userchkmsg;
                   1175:                 if (ref($curr_rules{$domain}) eq 'HASH') {
                   1176:                     $userchkmsg  =
                   1177:                         &Apache::loncommon::instrule_disallow_msg('id',
                   1178:                                                            $domdesc,1).
                   1179:                         &Apache::loncommon::user_rule_formats($domain,
                   1180:                               $domdesc,$curr_rules{$domain}{'id'},'id');
                   1181:                 }
                   1182:                 return ('fail',$userchkmsg);
                   1183:             }
                   1184:         }
                   1185:     }
                   1186:     return; 
                   1187: }
                   1188: 
                   1189: sub invalid_state {
1.40.2.5.2.1  raeburn  1190:     my ($error,$domdesc,$contact_name,$contact_email,$msgtext,$useremail) = @_;
1.14      raeburn  1191:     my $msg = '<h3>'.&mt('Account creation unavailable').'</h3><span class="LC_error">';
1.1       raeburn  1192:     if ($error eq 'baduseremail') {
1.40.2.3  raeburn  1193:         $msg .= &mt('The e-mail address you provided does not appear to be a valid address.');
1.1       raeburn  1194:     } elsif ($error eq 'existinguser') {
1.40.2.5.2.1  raeburn  1195:         my $uname = &HTML::Entities::encode($useremail); 
                   1196:         $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>'.
                   1197:                 '<li>'.&mt('Return to the [_1]log-in page[_2] and enter your password.','<a href="/adm/login?username='.$uname.'">','</a>').'</li>'.
                   1198:                 '<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>').
                   1199:                 '</li></ul>';
1.1       raeburn  1200:     } elsif ($error eq 'userrules') {
1.40.2.3  raeburn  1201:         $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  1202:     } elsif ($error eq 'userformat') {
1.40.2.3  raeburn  1203:         $msg .= &mt('The e-mail address you provided may not be used as a username at this LON-CAPA institution.');
1.1       raeburn  1204:     } elsif ($error eq 'captcha') {
1.40.2.3  raeburn  1205:         $msg .= &mt('Validation of the code you entered failed.');
1.1       raeburn  1206:     } elsif ($error eq 'noemails') {
1.40.2.3  raeburn  1207:         $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  1208:     }
1.14      raeburn  1209:     $msg .= '</span>';
1.1       raeburn  1210:     if ($msgtext) {
                   1211:         $msg .= '<br />'.$msgtext;
                   1212:     }
1.40.2.3  raeburn  1213:     $msg .= &linkto_email_help($contact_email,$domdesc,$error);
1.8       raeburn  1214:     return $msg;
                   1215: }
                   1216: 
                   1217: sub linkto_email_help {
1.40.2.3  raeburn  1218:     my ($contact_email,$domdesc,$error) = @_;
1.8       raeburn  1219:     my $msg;
1.40.2.3  raeburn  1220:     my $href = '/adm/helpdesk';
1.1       raeburn  1221:     if ($contact_email ne '') {
                   1222:         my $escuri = &HTML::Entities::encode('/adm/createaccount','&<>"');
1.40.2.3  raeburn  1223:         $href .= '?origurl='.$escuri;
                   1224:         if ($error eq 'existinguser') {
                   1225:             my $escemail = &HTML::Entities::encode($env{'form.useremail'});
                   1226:             $href .= '&useremail='.$escemail.'&useraccount='.$escemail;
                   1227:         }
                   1228:         $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  1229:     } else {
1.17      raeburn  1230:         $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  1231:     }
                   1232:     return $msg;
                   1233: }
                   1234: 
1.40.2.5.2.1  raeburn  1235: sub create_recaptcha {
1.40.2.5.2.6  raeburn  1236:     my ($pubkey) = @_;
1.40.2.5.2.1  raeburn  1237:     my $captcha = Captcha::reCAPTCHA->new;
1.40.2.5.2.3  raeburn  1238:     # generate PUBLICKEY from https://www.google.com/recaptcha
1.40.2.5.2.1  raeburn  1239:     return $captcha->get_options_setter({theme => 'white'})."\n".
1.40.2.5.2.6  raeburn  1240:            $captcha->get_html($pubkey);
1.1       raeburn  1241: }
                   1242: 
                   1243: sub getkeys {
                   1244:     my ($lkey,$ukey) = @_;
                   1245:     my $lextkey=hex($lkey);
                   1246:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
                   1247: 
                   1248:     my $uextkey=hex($ukey);
                   1249:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
                   1250:     return ($lextkey,$uextkey);
                   1251: }
                   1252: 
                   1253: sub serverform {
1.20      raeburn  1254:     my ($logtoken,$lonhost,$mailtoken,$courseid,$context) = @_;
1.22      raeburn  1255:     my $phase = 'username_validation';
                   1256:     my $catalog_elements;
1.20      raeburn  1257:     if ($context eq 'selfenroll') {
                   1258:         $phase = 'selfenroll_login';
                   1259:     }
1.22      raeburn  1260:     if ($courseid ne '') {
                   1261:         $catalog_elements = &Apache::lonhtmlcommon::echo_form_input(['courseid','phase']);
                   1262:     } 
                   1263:     my $output = <<ENDSERVERFORM;
1.20      raeburn  1264:   <form name="server" method="post" action="/adm/createaccount">
1.1       raeburn  1265:    <input type="hidden" name="logtoken" value="$logtoken" />
                   1266:    <input type="hidden" name="token" value="$mailtoken" />
                   1267:    <input type="hidden" name="serverid" value="$lonhost" />
                   1268:    <input type="hidden" name="uname" value="" />
                   1269:    <input type="hidden" name="upass" value="" />
1.32      raeburn  1270:    <input type="hidden" name="udom" value="" />
1.20      raeburn  1271:    <input type="hidden" name="phase" value="$phase" />
1.3       raeburn  1272:    <input type="hidden" name="courseid" value="$courseid" />
1.22      raeburn  1273:    $catalog_elements
1.1       raeburn  1274:   </form>
                   1275: ENDSERVERFORM
                   1276:     return $output;
                   1277: }
                   1278: 
                   1279: sub process_credentials {
                   1280:     my ($logtoken,$lonhost) = @_;
                   1281:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
                   1282:     my ($retrieved,$output,$upass);
                   1283:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.7       bisitz   1284:         $output = &mt('Information needed to verify your login information is missing, inaccessible or expired.')
                   1285:                  .'<br />'.&mt('You may need to reload the previous page to obtain a new token.');
1.1       raeburn  1286:         return ($retrieved,$output,$upass); 
                   1287:     } else {
                   1288:         my $reply = &Apache::lonnet::reply('tmpdel:'.$logtoken,$lonhost);
                   1289:         if ($reply eq 'ok') {
                   1290:             $retrieved = 'ok';
                   1291:         } else {
                   1292:             $output = &mt('Session could not be opened.');
                   1293:         }
                   1294:     }
                   1295:     my ($key,$caller)=split(/&/,$tmpinfo);
                   1296:     if ($caller eq 'createaccount') {
                   1297:         $upass = &Apache::lonpreferences::des_decrypt($key,$env{'form.upass'});
                   1298:     } else {
                   1299:         $output = &mt('Unable to retrieve your log-in information - unexpected context');
                   1300:     }
                   1301:     return ($retrieved,$output,$upass);
                   1302: }
                   1303: 
                   1304: sub guest_format_check {
                   1305:     my ($useremail,$domain,$cancreate,$settings) = @_;
                   1306:     my ($login,$format_match,$format_msg,@user_rules);
                   1307:     if (ref($settings) eq 'HASH') {
                   1308:         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
                   1309:             push(@user_rules,@{$settings->{'email_rule'}});
                   1310:         }
                   1311:     }
                   1312:     if (@user_rules > 0) {
                   1313:         my %rule_check = 
                   1314:             &Apache::lonnet::inst_rulecheck($domain,$useremail,undef,
1.2       raeburn  1315:                                             'selfcreate',\@user_rules);
1.1       raeburn  1316:         if (keys(%rule_check) > 0) {
                   1317:             foreach my $item (keys(%rule_check)) {
                   1318:                 if ($rule_check{$item}) {
                   1319:                     $format_match = 1;   
                   1320:                     last;
                   1321:                 }
                   1322:             }
                   1323:         }
                   1324:     }
                   1325:     if ($format_match) {
                   1326:         ($login) = ($useremail =~ /^([^\@]+)\@/);
                   1327:         $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  1328:         if (ref($cancreate) eq 'ARRAY') {
                   1329:             if (grep(/^login$/,@{$cancreate})) {
1.7       bisitz   1330:                 $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  1331:             }
1.1       raeburn  1332:         }
                   1333:     }
                   1334:     return $format_msg;
                   1335: }
                   1336: 
1.17      raeburn  1337: sub sso_logout_frag {
                   1338:     my ($r,$domain) = @_;
                   1339:     my $endsessionmsg;
                   1340:     if (defined($r->dir_config('lonSSOUserLogoutMessageFile_'.$domain))) {
                   1341:         my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile_'.$domain);
                   1342:         if (-e $msgfile) {
                   1343:             open(my $fh,"<$msgfile");
                   1344:             $endsessionmsg = join('',<$fh>);
                   1345:             close($fh);
                   1346:         }
                   1347:     } elsif (defined($r->dir_config('lonSSOUserLogoutMessageFile'))) {
                   1348:         my $msgfile = $r->dir_config('lonSSOUserLogoutMessageFile');
                   1349:         if (-e $msgfile) {     
                   1350:             open(my $fh,"<$msgfile");
                   1351:             $endsessionmsg = join('',<$fh>);
                   1352:             close($fh);
                   1353:         }
                   1354:     }
                   1355:     return $endsessionmsg;
                   1356: }
                   1357: 
1.22      raeburn  1358: sub catreturn_js {
                   1359:     return  <<"ENDSCRIPT";
                   1360: <script type="text/javascript">
                   1361: 
                   1362: function ToSelfenroll(formname) {
                   1363:     var formidx = getFormByName(formname);
                   1364:     if (formidx > -1) {
                   1365:         document.forms[formidx].action = '/adm/selfenroll';
                   1366:         numidx = getIndexByName(formidx,'phase');
                   1367:         if (numidx > -1) {
                   1368:             document.forms[formidx].elements[numidx].value = '';   
                   1369:         }
                   1370:         numidx = getIndexByName(formidx,'context');
                   1371:         if (numidx > -1) {
                   1372:             document.forms[formidx].elements[numidx].value = '';
                   1373:         }
                   1374:     }
                   1375:     document.forms[formidx].submit();
                   1376: }
                   1377: 
                   1378: function ToCatalog(formname,caller) {
                   1379:     var formidx = getFormByName(formname);
                   1380:     if (formidx > -1) {
                   1381:         document.forms[formidx].action = '/adm/coursecatalog';
                   1382:         numidx = getIndexByName(formidx,'coursenum');
                   1383:         if (numidx > -1) {
                   1384:             if (caller != 'details') {
                   1385:                 document.forms[formidx].elements[numidx].value = '';
                   1386:             }
                   1387:         }
                   1388:     }
                   1389:     document.forms[formidx].submit();
                   1390: }
                   1391: 
                   1392: function getIndexByName(formidx,item) {
                   1393:     for (var i=0;i<document.forms[formidx].elements.length;i++) {
                   1394:         if (document.forms[formidx].elements[i].name == item) {
                   1395:             return i;
                   1396:         }
                   1397:     }
                   1398:     return -1;
                   1399: }
                   1400: 
                   1401: function getFormByName(item) {
                   1402:     for (var i=0; i<document.forms.length; i++) {
                   1403:         if (document.forms[i].name == item) {
                   1404:             return i;
                   1405:         }
                   1406:     }
                   1407:     return -1;
                   1408: }
                   1409: 
                   1410: </script>
                   1411: ENDSCRIPT
                   1412: 
                   1413: }
                   1414: 
1.1       raeburn  1415: 1;

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