Annotation of loncom/interface/resetpw.pm, revision 1.38

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow access to password changing via a token sent to user's e-mail. 
                      3: #
1.38    ! raeburn     4: # $Id: resetpw.pm,v 1.37 2016/01/27 00:24:09 raeburn Exp $
1.14      bisitz      5: #
1.1       raeburn     6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: #
1.21      raeburn    29: 
                     30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
1.22      raeburn    34: Apache::resetpw: reset user password.
1.21      raeburn    35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: Handles resetting of forgotten passwords.
                     39: 
                     40: This is part of the LearningOnline Network with CAPA project
                     41: described at http://www.lon-capa.org.
                     42:  
                     43: =head1 OVERVIEW
                     44: 
                     45: A user with an e-mail address associated with his/her LON-CAPA username
1.22      raeburn    46: can reset a forgotten password, using a link sent to the e-mail address
1.21      raeburn    47: if the authentication type for the account is "internal".
                     48: 
                     49: =cut
                     50: 
1.1       raeburn    51: package Apache::resetpw;
                     52: 
                     53: use strict;
                     54: use Apache::Constants qw(:common);
                     55: use Apache::lonacc;
                     56: use Apache::lonnet;
                     57: use Apache::loncommon;
                     58: use Apache::lonlocal;
                     59: use LONCAPA;
1.38    ! raeburn    60: use HTML::Entities;
1.1       raeburn    61: 
                     62: sub handler {
                     63:     my $r = shift;
                     64:     &Apache::loncommon::content_type($r,'text/html');
                     65:     $r->send_http_header;
                     66:     if ($r->header_only) {
                     67:         return OK;
                     68:     }
                     69:     my $contact_name = &mt('LON-CAPA helpdesk');
1.27      raeburn    70:     my $origmail =  $r->dir_config('lonSupportEMail');
1.1       raeburn    71:     my $server = $r->dir_config('lonHostID');
1.19      raeburn    72:     my $defdom = &Apache::lonnet::default_login_domain();
1.27      raeburn    73:     my $contacts =
                     74:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
                     75:                                                  $defdom,$origmail);
                     76:     my ($contact_email) = split(',',$contacts);
1.26      raeburn    77:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                     78:     my $lonidsdir=$r->dir_config('lonIDsDir');
                     79:     if ($handle ne '') {
                     80:         if ($handle=~/^publicuser\_/) {
                     81:             unlink($r->dir_config('lonIDsDir')."/$handle.id");
                     82:         } else {
                     83:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                     84:         }
                     85:     }
1.1       raeburn    86:     &Apache::lonacc::get_posted_cgi($r);
                     87:     &Apache::lonlocal::get_language_handle($r);
                     88:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
                     89:     
                     90:     my @emailtypes = ('permanentemail','critnotification','notification');
1.38    ! raeburn    91:     my $uname = $env{'form.uname'};
        !            92:     $uname =~ s/^\s+|\s+$//g;
        !            93:     $uname = &LONCAPA::clean_username($uname);
        !            94:     my $udom = &LONCAPA::clean_domain($env{'form.udom'});
        !            95:     my ($domdesc,$otherinst);
        !            96:     if ($udom) {
        !            97:         $domdesc = &Apache::lonnet::domain($udom,'description');
        !            98:         if ($domdesc) {
        !            99:             my %servers = &Apache::lonnet::internet_dom_servers($udom);
        !           100:             unless (exists($servers{$server})) {
        !           101:                 $otherinst = 1;  
        !           102:             }
        !           103:         }
        !           104:     }
1.1       raeburn   105:     my $token = $env{'form.token'};
1.26      raeburn   106:     my $brcrum = [];
                    107:     if ($token) {
                    108:         push (@{$brcrum},
                    109:             {href => '/adm/resetpw',
                    110:              text => 'Update Password'});
                    111:     } else {
                    112:         push (@{$brcrum},
                    113:             {href => '/adm/resetpw',
                    114:              text => 'Account Information'});
                    115:         if ($uname && $udom) {
                    116:             push (@{$brcrum},
                    117:                 {href => '/adm/resetpw',
                    118:                  text => 'Result'});
                    119:         }
                    120:     }
1.33      bisitz    121:     my $args = {bread_crumbs => $brcrum};
1.38    ! raeburn   122:     my $js;
        !           123:     unless ($token || $otherinst || ($uname && $udom)) {
        !           124:         my (@intdoms,@instdoms);
        !           125:         my $internet_names = &Apache::lonnet::get_internet_names($server);
        !           126:         if (ref($internet_names) eq 'ARRAY') {
        !           127:             @intdoms = @{$internet_names};
        !           128:         }
        !           129:         if (@intdoms) {
        !           130:             my %iphost = &Apache::lonnet::get_iphost();
        !           131:             foreach my $ip (keys(%iphost)) {
        !           132:                 if (ref($iphost{$ip}) eq 'ARRAY') {
        !           133:                     foreach my $id (@{$iphost{$ip}}) {
        !           134:                         my $location = &Apache::lonnet::internet_dom($id);
        !           135:                         if ($location) {
        !           136:                             if (grep(/^\Q$location\E$/,@intdoms)) {
        !           137:                                 my $dom = &Apache::lonnet::host_domain($id);
        !           138:                                 unless (grep(/^\Q$dom\E/,@instdoms)) {
        !           139:                                     push(@instdoms,$dom);
        !           140:                                 }
        !           141:                             }
        !           142:                         }
        !           143:                     }
        !           144:                 }
        !           145:             }
        !           146:         }
        !           147:         my $instdomstr;
        !           148:         if (@instdoms > 0) {
        !           149:             $instdomstr = "'".join("','",@instdoms)."'";
        !           150:         }
        !           151:         my %js_lt = &Apache::lonlocal::texthash(
        !           152:             thdo  => 'The domain you have selected is for another institution.',
        !           153:             yowi  => 'You will be switched to the Forgot Password utility at that institution.',
        !           154:             unam  => 'You must enter a username.',
        !           155:             mail  => 'You must enter an e-mail address.'
        !           156:         );
        !           157:         &js_escape(\%js_lt);
        !           158:         $js = <<"END";
        !           159: <script type="text/javascript">
        !           160: // <![CDATA[
        !           161: function verifyDomain(caller,form) {
        !           162:     var redirect = 1; 
        !           163:     var instdoms = new Array($instdomstr);
        !           164:     if (instdoms.length > 0) {
        !           165:         for (var i=0; i<instdoms.length; i++) {
        !           166:             if (caller.options[caller.selectedIndex].value == instdoms[i]) {
        !           167:                 redirect = 0;
        !           168:                 break;
        !           169:             }
        !           170:         }
        !           171:     }
        !           172:     if (redirect == 1) {
        !           173:         if (confirm('$js_lt{thdo}\\n$js_lt{yowi}')) {
        !           174:             form.submit();
        !           175:         }
        !           176:     }
        !           177:     return;
        !           178: }
        !           179: 
        !           180: function validInfo() {
        !           181:     if (document.forgotpw.uname.value == '') {
        !           182:         alert("$js_lt{'unam'}");
        !           183:         return false;
        !           184:     }
        !           185:     if (document.forgotpw.useremail.value == '') {
        !           186:         alert("$js_lt{'mail'}");
        !           187:         return false;
        !           188:     }
        !           189:     return true;
        !           190: }
        !           191: // ]]>
        !           192: </script>
        !           193: END
        !           194:     }
        !           195:     my $header = &Apache::loncommon::start_page('Reset password',$js,$args).
        !           196:                  '<h2>'.&mt('Reset forgotten LON-CAPA password').'</h2>';
1.1       raeburn   197:     my $output;
                    198:     if ($token) {
                    199:         $output = &reset_passwd($r,$token,$contact_name,$contact_email);
1.38    ! raeburn   200:     } elsif ($udom) {
        !           201:         if (!$domdesc) {
        !           202:             $output = &invalid_state('baddomain',$domdesc,
        !           203:                                      $contact_name,$contact_email); 
        !           204:         } elsif ($otherinst) {
        !           205:             ($header,$output) = &homeserver_redirect($uname,$udom,$domdesc,$brcrum);
        !           206:         } elsif ($uname) {
        !           207:             my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
        !           208:             if ($authtype =~ /^internal/) {
        !           209:                 my $useremail = $env{'form.useremail'};
        !           210:                 my ($blocked,$blocktext) =
        !           211:                     &Apache::loncommon::blocking_status('passwd',$uname,$udom);
        !           212:                 if ($blocked) {
        !           213:                     $output = '<p class="LC_warning">'.$blocktext.'</p>'
        !           214:                               .&display_actions($contact_email,$domdesc);
        !           215:                 } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
1.37      raeburn   216:                     $output = &invalid_state('baduseremail',$domdesc,
                    217:                                              $contact_name,$contact_email);
1.38    ! raeburn   218:                 } else {
        !           219:                     my %userinfo = 
        !           220: 	                &Apache::lonnet::get('environment',\@emailtypes,
        !           221: 					     $udom,$uname);
        !           222:                     my @allemails;
        !           223:                     foreach my $type (@emailtypes) {
        !           224:                         my $email = $userinfo{$type};
        !           225:                         my @items;
        !           226:                         if ($email =~ /,/) {
        !           227:                             @items = split(',',$userinfo{$type});
        !           228:                         } else {
        !           229:                             @items = ($email);
        !           230:                         }
        !           231:                         foreach my $item (@items) {
        !           232:                             if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
        !           233:                                 unless(grep(/^\Q$item\E$/,@allemails)) { 
        !           234:                                     push(@allemails,$item);
        !           235:                                 }
1.18      raeburn   236:                             }
                    237:                         }
1.3       raeburn   238:                     }
1.38    ! raeburn   239:                     if (@allemails > 0) {
        !           240:                         if (grep(/^\Q$useremail\E$/,@allemails)) {
        !           241:                             $output = &send_token($uname,$udom,$useremail,$server,
        !           242:                                                   $domdesc,$contact_name,
        !           243:                                                   $contact_email);
        !           244:                         } else {
        !           245:                             $output = &invalid_state('mismatch',$domdesc,
        !           246:                                                      $contact_name,
        !           247:                                                      $contact_email);
        !           248:                         }
1.3       raeburn   249:                     } else {
1.38    ! raeburn   250:                         $output = &invalid_state('missing',$domdesc,
        !           251:                                                  $contact_name,$contact_email);
1.3       raeburn   252:                     }
1.1       raeburn   253:                 }
1.38    ! raeburn   254:             } elsif ($authtype =~ /^(krb|unix|local)/) { 
        !           255:                 $output = &invalid_state('authentication',$domdesc,
        !           256:                                          $contact_name,$contact_email);
        !           257:             } else {
        !           258:                 $output = &invalid_state('invalid',$domdesc,
        !           259:                                          $contact_name,$contact_email);
1.1       raeburn   260:             }
                    261:         } else {
1.38    ! raeburn   262:             $output = &get_uname($defdom);
1.1       raeburn   263:         }
                    264:     } else {
                    265:         $output = &get_uname($defdom);
                    266:     }
1.38    ! raeburn   267:     $r->print($header.$output);
1.1       raeburn   268:     $r->print(&Apache::loncommon::end_page());
                    269:     return OK;
                    270: }
                    271: 
                    272: sub get_uname {
                    273:     my ($defdom) = @_;
                    274:     my %lt = &Apache::lonlocal::texthash(
1.32      bisitz    275:                                          unam => 'LON-CAPA username',
                    276:                                          udom => 'LON-CAPA domain',
1.12      schafran  277:                                          uemail => 'E-mail address in LON-CAPA',
1.1       raeburn   278:                                          proc => 'Proceed');
                    279: 
1.23      bisitz    280:     my $msg = &mt('If you use the same account for other campus services besides LON-CAPA, (e.g., e-mail, course registration, etc.), a separate centrally managed mechanism likely exists to reset a password. However, if your account is used for just LON-CAPA access you will probably be able to reset a password from this page.');
1.8       bisitz    281:     $msg .= '<br /><br />'.&mt('Three conditions must be met:')
                    282:            .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
                    283:            .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
                    284:            .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
                    285:            .'</ul>';
1.38    ! raeburn   286:     my $mobileargs;
        !           287:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
        !           288:         &Apache::loncommon::decode_user_agent();
        !           289:     if ($clientmobile) {
        !           290:         $mobileargs = 'autocapitalize="off" autocorrect="off" ';
        !           291:     }
        !           292:     my $onchange = 'javascript:verifyDomain(this,this.form);';
        !           293:     $msg .= '<form name="forgotpw" method="post" action="/adm/resetpw" onsubmit="return validInfo();">'.
1.26      raeburn   294:             &Apache::lonhtmlcommon::start_pick_box(). 
1.32      bisitz    295:             &Apache::lonhtmlcommon::row_title($lt{'unam'}).
1.38    ! raeburn   296:             '<input type="text" name="uname" size="20" '.$mobileargs.'/>'.
1.26      raeburn   297:             &Apache::lonhtmlcommon::row_closure(1).
1.32      bisitz    298:             &Apache::lonhtmlcommon::row_title($lt{'udom'}).
1.38    ! raeburn   299:             &Apache::loncommon::select_dom_form($defdom,'udom',undef,undef,$onchange).
1.26      raeburn   300:             &Apache::lonhtmlcommon::row_closure(1).
                    301:             &Apache::lonhtmlcommon::row_title($lt{'uemail'}).
1.38    ! raeburn   302:             '<input type="text" name="useremail" size="30" '.$mobileargs.'/>'.
1.26      raeburn   303:             &Apache::lonhtmlcommon::end_pick_box().
                    304:             '<br /><br /><input type="submit" name="resetter" value="'.$lt{'proc'}.'" /></form>';
1.1       raeburn   305:     return $msg;
                    306: }
                    307: 
                    308: sub send_token {
                    309:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
                    310:         $contact_email) = @_;
1.29      bisitz    311:     my $msg =
                    312:         '<p class="LC_info">'
                    313:        .&mt('Thank you for your request to reset the password for your LON-CAPA account.')
                    314:        .'</p>';
1.1       raeburn   315: 
                    316:     my $now = time;
                    317:     my $temppasswd = &create_passwd();
1.2       albertel  318:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    319: 		'time'       => $now,
                    320: 		'domain'     => $udom,
                    321: 		'username'   => $uname,
                    322: 		'email'      => $email,
                    323: 		'temppasswd' => $temppasswd);
1.1       raeburn   324: 
1.3       raeburn   325:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
                    326:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  327:         my $esc_token = &escape($token);
1.15      raeburn   328:         my $showtime = &Apache::lonlocal::locallocaltime(time);
                    329:         my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.26      raeburn   330:         my $mailmsg = &mt('A request was submitted on [_1] for reset of the password for your LON-CAPA account.',$showtime)." \n".&mt('To complete this process please open a web browser and enter the following URL in the address/location box: [_1]',"\n\n".$reseturl);
1.1       raeburn   331:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    332:                                 $contact_email);
                    333:         if ($result eq 'ok') {
1.34      bisitz    334:             $msg .=
                    335:                 &mt('An e-mail sent to the e-mail address associated with your LON-CAPA account includes the web address for the link you should use to complete the reset process.')
                    336:                .'<br /><br />'
                    337:                .&mt('The link included in the message will be valid for the next [_1]two[_2] hours.','<b>','</b>');
1.1       raeburn   338:         } else {
1.28      bisitz    339:             $msg .=
1.29      bisitz    340:                 '<p class="LC_error">'
                    341:                .&mt('An error occurred when sending a message to the e-mail address'
                    342:                    .' associated with your LON-CAPA account.')
                    343:                .'</p>'
                    344:                .&display_actions($contact_email,$domdesc);
1.1       raeburn   345:         }
                    346:     } else {
1.28      bisitz    347:         $msg .=
1.29      bisitz    348:             '<p class="LC_error">'
                    349:            .&mt('An error occurred creating a token required for the'
                    350:                .' password reset process.')
                    351:            .'</p>'
                    352:            .&display_actions($contact_email,$domdesc);
1.1       raeburn   353:     }
                    354:     return $msg;
                    355: }
                    356: 
                    357: sub send_mail {
                    358:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
                    359:     my $outcome;
                    360:     my $requestmail = "To: $email\n".
                    361:                       "From: $contact_name <$contact_email>\n".
                    362:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
1.25      bisitz    363:                       "Content-type: text/plain\;charset=UTF-8\n".
1.1       raeburn   364:                       "\n\n".$mailmsg."\n\n".
                    365:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
                    366:                       "$contact_email\n";
                    367:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                    368:         print MAIL $requestmail;
                    369:         close(MAIL);
                    370:         $outcome = 'ok';
                    371:     } else {
                    372:         $outcome = 'fail';
                    373:     }
                    374:     return $outcome;
                    375: }
                    376: 
                    377: sub invalid_state {
                    378:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
                    379:     my $msg;
                    380:     if ($error eq 'invalid') {
1.29      bisitz    381:         $msg =
                    382:             '<p class="LC_warning">'
                    383:             .&mt('The username you provided was not verified as a valid username'
                    384:                 .' in the LON-CAPA system for the [_1] domain.','<i>'.$domdesc.'</i>')
                    385:               .'</p>';
                    386:         $msg .= &display_actions($contact_email,$domdesc);
1.1       raeburn   387:     } else {
1.3       raeburn   388:         if ($error eq 'baduseremail') {
                    389:             $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    390:         } elsif ($error eq 'mismatch') {
                    391:             $msg = &mt('The e-mail address you provided does not match the address recorded in the LON-CAPA system for the username and domain you provided.');  
                    392:         } elsif ($error eq 'missing') {
1.1       raeburn   393:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
                    394:         } elsif ($error eq 'authentication') {
                    395:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
1.38    ! raeburn   396:         } elsif ($error eq 'baddomain') {
        !           397:             $msg = &mt('The domain you provided was not verified as a valid domain in the LON-CAPA system.');
1.1       raeburn   398:         }
1.29      bisitz    399:         $msg = '<p class="LC_warning">'.$msg.'</p>'
                    400:               .&display_actions($contact_email,$domdesc);
1.1       raeburn   401:     }
                    402:     return $msg;
                    403: }
                    404: 
1.38    ! raeburn   405: sub homeserver_redirect {
        !           406:     my ($uname,$udom,$domdesc,$brcrum) = @_;
        !           407:     my $uhome = &Apache::lonnet::homeserver();
        !           408:     if ($uhome eq 'no_host') {
        !           409:         $uhome = &Apache::lonnet::domain($udom,'primary');
        !           410:     }
        !           411:     my $protocol = $Apache::lonnet::protocol{$uhome};
        !           412:     $protocol = 'http' if ($protocol ne 'https');
        !           413:     my $url = $protocol.'://'.&Apache::lonnet::hostname($uhome).'/adm/resetpw';
        !           414:     # Breadcrumbs
        !           415:     my $start_page = &Apache::loncommon::start_page('Switching Server',undef,
        !           416:                                                     {'redirect' => [0,$url],
        !           417:                                                      'bread_crumbs' => $brcrum,});
        !           418:     my $output = '<p>'.&mt('This LON-CAPA server belongs to a different domain.').' '.
        !           419:                        &mt('You are being switched to your domain ([_1]), to use the "Forgot Password" tool.',$domdesc).
        !           420:                  '</p>';
        !           421:     return ($start_page,$output);
        !           422: }
        !           423: 
1.1       raeburn   424: sub reset_passwd {
                    425:     my ($r,$token,$contact_name,$contact_email) = @_;
                    426:     my $msg;
                    427:     my %data = &Apache::lonnet::tmpget($token);
                    428:     my $now = time;
                    429:     if (keys(%data) == 0) {
1.17      bisitz    430:         $msg = &mt('Sorry, the URL you provided to complete the reset of your password was invalid. Either the token included in the URL has been deleted or the URL you provided was invalid. Please submit a [_1]new request[_2] for a password reset, and follow the link to the new URL included in the e-mail that will be sent to you, to allow you to enter a new password.'
                    431:                   ,'<a href="/adm/resetpw">','</a>');
1.1       raeburn   432:         return $msg;
                    433:     }
                    434:     if (($data{'time'} =~ /^\d+$/) && 
                    435:         ($data{'username'} ne '') && 
                    436:         ($data{'domain'} ne '') && 
1.3       raeburn   437:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   438:         ($data{'temppasswd'} =~/^\w+$/)) {
1.9       raeburn   439:         my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.37      raeburn   440:         my ($blocked,$blocktext) =
                    441:             &Apache::loncommon::blocking_status('passwd',$data{'username'},$data{'domain'});
                    442:         if ($blocked) {
                    443:             $msg = '<p class="LC_warning">'.$blocktext.'</p>';
                    444:             return $msg;
                    445:         } elsif ($now - $data{'time'} < 7200) {
1.1       raeburn   446:             if ($env{'form.action'} eq 'verify_and_change_pass') {
1.22      raeburn   447:                 unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'}) && ($env{'form.email'} eq $data{'email'})) {
                    448:                     $msg = &generic_failure_msg($contact_name,$contact_email);
                    449:                     return $msg;
                    450:                 }
1.1       raeburn   451:                 my $change_failed = 
1.2       albertel  452: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1       raeburn   453:                 if (!$change_failed) {
                    454:                     my $delete = &Apache::lonnet::tmpdel($token);
1.9       raeburn   455:                     my $now = &Apache::lonlocal::locallocaltime(time);
1.1       raeburn   456:                     my $domdesc = 
1.6       albertel  457: 			&Apache::lonnet::domain($data{'domain'},'description');
1.1       raeburn   458:                     my $mailmsg = &mt('The password for your LON-CAPA account in the [_1] domain was changed [_2] from IP address: [_3].  If you did not perform this change or authorize it, please contact the [_4] ([_5]).',$domdesc,$now,$ENV{'REMOTE_ADDR'},$contact_name,$contact_email)."\n";
                    459:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    460:                                             $contact_name,$contact_email);
1.33      bisitz    461:                     my $confirm_msg;
1.1       raeburn   462:                     if ($result eq 'ok') {
1.33      bisitz    463:                         $confirm_msg =
                    464:                             &Apache::lonhtmlcommon::confirm_success(
                    465:                                 &mt('An e-mail confirming setting of the password'
                    466:                                    .' for your LON-CAPA account has been sent to [_1].'
                    467:                                     ,'<span class="LC_filename">'.$data{'email'}.'</span>'));
1.1       raeburn   468:                     } else {
1.33      bisitz    469:                         $confirm_msg =
                    470:                             &Apache::lonhtmlcommon::confirm_success(
                    471:                                 &mt('An error occurred when sending e-mail to [_1]'
                    472:                                    .' confirming setting of your new password.'
                    473:                                     ,'<span class="LC_filename">'.$data{'email'}.'</span>'),1);
1.1       raeburn   474:                     }
1.33      bisitz    475:                     $msg .=
                    476:                         &Apache::loncommon::confirmwrapper($confirm_msg)
                    477:                        .&Apache::lonhtmlcommon::actionbox([
                    478:                             '<a href="/adm/login">'.&mt('Go to the login page').'</a>']);
1.20      raeburn   479:                 } elsif ($change_failed eq 'invalid_client') {
                    480:                     my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
                    481:                     if ($homeserver eq 'no_host') {
                    482:                         $msg .= &generic_failure_msg($contact_name,$contact_email);
                    483:                     } else {
                    484:                         my $protocol = $Apache::lonnet::protocol{$homeserver};
                    485:                         $protocol = 'http' if ($protocol ne 'https');
                    486:                         my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
                    487:                                   '/adm/resetpw';
                    488:                         my ($opentag,$closetag);
                    489:                         if ($url) {
                    490:                            $opentag = '<a href="'.$url.'">';
                    491:                            $closetag = '</a>';
                    492:                         }
1.28      bisitz    493:                         $msg .=
                    494:                             '<p class="LC_warning">'
                    495:                            .&mt('A problem occurred when attempting to reset'
                    496:                                .' the password for your account.'
                    497:                                .' Please try again from your [_1]home server[_2].'
                    498:                                 ,$opentag,$closetag)
                    499:                            .'</p>';
1.20      raeburn   500:                     }
1.1       raeburn   501:                 } else {
1.20      raeburn   502:                     $msg .= &generic_failure_msg($contact_name,$contact_email);
1.1       raeburn   503:                 }
                    504:             } else {
1.13      schafran  505:                 $r->print(&mt('The token included in an e-mail sent to you [_1] has been verified, so you may now proceed to reset the password for your LON-CAPA account.',$reqtime).'<br /><br />');
1.1       raeburn   506:                 $r->print(&mt('Please enter the username and domain of the LON-CAPA account, and the associated e-mail address, for which you are setting a password. The new password must contain at least 7 characters.').' '.&mt('Your new password will be sent to the LON-CAPA server in an encrypted form.').'<br />');
                    507:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
                    508:             }
                    509:         } else {
1.28      bisitz    510:             $msg =
                    511:                 '<p class="LC_warning">'
                    512:                .&mt('Sorry, the token generated when you requested a password reset has expired. Please submit a [_1]new request[_2], and follow the link to the web page included in the new e-mail that will be sent to you, to allow you to enter a new password.'
                    513:                     ,'<a href="/adm/resetpw">','</a>')
                    514:                .'</p>';
1.1       raeburn   515:         }
                    516:     } else {
1.28      bisitz    517:         $msg .=
                    518:             '<p class="LC_warning">'
                    519:            .&mt('Sorry, the URL generated when you requested reset of your password contained incomplete information. Please submit a [_1]new request[_2] for a password reset, and use the new URL that will be sent to your e-mail account to complete the process.'
                    520:                 ,'<a href="/adm/resetpw">','</a>')
                    521:            .'</p>';
1.1       raeburn   522:     }
                    523:     return $msg;
                    524: }
                    525: 
1.20      raeburn   526: sub generic_failure_msg {
                    527:     my ($contact_name,$contact_email) = @_;
1.28      bisitz    528:     return
1.29      bisitz    529:         '<p class="LC_error">'
                    530:        .&mt('A problem occurred when attempting to reset the password for your account.')
                    531:        .'<br />'
1.36      raeburn   532:        .&mt('Please contact the [_1] ([_2]) for assistance.',
1.28      bisitz    533:               $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>')
                    534:        .'</p>';
1.20      raeburn   535: }
                    536: 
1.1       raeburn   537: sub create_passwd {
                    538:     my $passwd = '';
1.2       albertel  539:     my @letts = ("a".."z");
1.1       raeburn   540:     for (my $i=0; $i<8; $i++) {
1.2       albertel  541:         my $lettnum = int(rand(2));
1.1       raeburn   542:         my $item = '';
                    543:         if ($lettnum) {
1.2       albertel  544:             $item = $letts[int(rand(26))];
                    545:             my $uppercase = int(rand(2));
1.1       raeburn   546:             if ($uppercase) {
                    547:                 $item =~ tr/a-z/A-Z/;
                    548:             }
                    549:         } else {
1.2       albertel  550:             $item = int(rand(10));
1.1       raeburn   551:         }
                    552:         $passwd .= $item;
                    553:     }
                    554:     return ($passwd);
                    555: }
                    556: 
1.29      bisitz    557: sub display_actions {
                    558:     my ($contact_email, $domdesc) = @_;
                    559:     my @msg = (&mt('[_1]Go back[_2] and try again',
                    560:                    '<a href="javascript:history.go(-1)">','</a>'));
                    561:     my $msg2 = '';
                    562:     if ($contact_email ne '') {
1.30      raeburn   563:         my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
                    564:         push(@msg, &mt('Contact the [_1]LON-CAPA helpdesk[_2] for the institution: [_3]',
                    565:                        '<a href="/adm/helpdesk?origurl='.$escuri.'">',
                    566:                        '</a>','<i>'.$domdesc.'</i>'));
                    567:     } else {
                    568:         $msg2 =
                    569:             '<p>'
                    570:            .&mt('You may wish to send an e-mail to the'
                    571:            .' server administrator: [_1] for the [_2] domain.',
1.31      raeburn   572:                 '<i>'.$Apache::lonnet::perlvar{'AdmEMail'}.'</i>',
1.30      raeburn   573:                 '<i>'.$domdesc.'</i>')
                    574:            .'</p>';
                    575:     }
1.29      bisitz    576: 
                    577:     return &Apache::lonhtmlcommon::actionbox(\@msg).$msg2;
                    578: 
                    579: }
                    580: 
1.1       raeburn   581: 1;

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