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

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

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