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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Allow access to password changing via a token sent to user's e-mail. 
                      3: #
1.40    ! raeburn     4: # $Id: resetpw.pm,v 1.39 2016/10/30 01:45:31 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) {
1.40    ! raeburn    99:             $otherinst = 1;
        !           100:             my @ids=&Apache::lonnet::current_machine_ids();
1.38      raeburn   101:             my %servers = &Apache::lonnet::internet_dom_servers($udom);
1.40    ! raeburn   102:             foreach my $server (keys(%servers)) {
        !           103:                 if (grep(/^\Q$server\E$/,@ids)) {
        !           104:                     $otherinst = 0;
        !           105:                     last;
        !           106:                 }
1.38      raeburn   107:             }
                    108:         }
                    109:     }
1.1       raeburn   110:     my $token = $env{'form.token'};
1.26      raeburn   111:     my $brcrum = [];
                    112:     if ($token) {
                    113:         push (@{$brcrum},
                    114:             {href => '/adm/resetpw',
                    115:              text => 'Update Password'});
                    116:     } else {
                    117:         push (@{$brcrum},
                    118:             {href => '/adm/resetpw',
                    119:              text => 'Account Information'});
                    120:         if ($uname && $udom) {
                    121:             push (@{$brcrum},
                    122:                 {href => '/adm/resetpw',
                    123:                  text => 'Result'});
                    124:         }
                    125:     }
1.33      bisitz    126:     my $args = {bread_crumbs => $brcrum};
1.38      raeburn   127:     my $js;
                    128:     unless ($token || $otherinst || ($uname && $udom)) {
                    129:         my (@intdoms,@instdoms);
                    130:         my $internet_names = &Apache::lonnet::get_internet_names($server);
                    131:         if (ref($internet_names) eq 'ARRAY') {
                    132:             @intdoms = @{$internet_names};
                    133:         }
                    134:         if (@intdoms) {
                    135:             my %iphost = &Apache::lonnet::get_iphost();
                    136:             foreach my $ip (keys(%iphost)) {
                    137:                 if (ref($iphost{$ip}) eq 'ARRAY') {
                    138:                     foreach my $id (@{$iphost{$ip}}) {
                    139:                         my $location = &Apache::lonnet::internet_dom($id);
                    140:                         if ($location) {
                    141:                             if (grep(/^\Q$location\E$/,@intdoms)) {
                    142:                                 my $dom = &Apache::lonnet::host_domain($id);
                    143:                                 unless (grep(/^\Q$dom\E/,@instdoms)) {
                    144:                                     push(@instdoms,$dom);
                    145:                                 }
                    146:                             }
                    147:                         }
                    148:                     }
                    149:                 }
                    150:             }
                    151:         }
                    152:         my $instdomstr;
                    153:         if (@instdoms > 0) {
                    154:             $instdomstr = "'".join("','",@instdoms)."'";
                    155:         }
                    156:         my %js_lt = &Apache::lonlocal::texthash(
                    157:             thdo  => 'The domain you have selected is for another institution.',
                    158:             yowi  => 'You will be switched to the Forgot Password utility at that institution.',
                    159:             unam  => 'You must enter a username.',
                    160:             mail  => 'You must enter an e-mail address.'
                    161:         );
                    162:         &js_escape(\%js_lt);
                    163:         $js = <<"END";
                    164: <script type="text/javascript">
                    165: // <![CDATA[
                    166: function verifyDomain(caller,form) {
                    167:     var redirect = 1; 
                    168:     var instdoms = new Array($instdomstr);
                    169:     if (instdoms.length > 0) {
                    170:         for (var i=0; i<instdoms.length; i++) {
                    171:             if (caller.options[caller.selectedIndex].value == instdoms[i]) {
                    172:                 redirect = 0;
                    173:                 break;
                    174:             }
                    175:         }
                    176:     }
                    177:     if (redirect == 1) {
                    178:         if (confirm('$js_lt{thdo}\\n$js_lt{yowi}')) {
                    179:             form.submit();
                    180:         }
                    181:     }
                    182:     return;
                    183: }
                    184: 
                    185: function validInfo() {
                    186:     if (document.forgotpw.uname.value == '') {
                    187:         alert("$js_lt{'unam'}");
                    188:         return false;
                    189:     }
                    190:     if (document.forgotpw.useremail.value == '') {
                    191:         alert("$js_lt{'mail'}");
                    192:         return false;
                    193:     }
                    194:     return true;
                    195: }
                    196: // ]]>
                    197: </script>
                    198: END
                    199:     }
                    200:     my $header = &Apache::loncommon::start_page('Reset password',$js,$args).
                    201:                  '<h2>'.&mt('Reset forgotten LON-CAPA password').'</h2>';
1.1       raeburn   202:     my $output;
                    203:     if ($token) {
1.39      raeburn   204:         $r->print($header);
                    205:         &reset_passwd($r,$token,$contact_name,$contact_email);
                    206:         $r->print(&Apache::loncommon::end_page());
                    207:         return OK;
1.38      raeburn   208:     } elsif ($udom) {
                    209:         if (!$domdesc) {
                    210:             $output = &invalid_state('baddomain',$domdesc,
                    211:                                      $contact_name,$contact_email); 
                    212:         } elsif ($otherinst) {
                    213:             ($header,$output) = &homeserver_redirect($uname,$udom,$domdesc,$brcrum);
                    214:         } elsif ($uname) {
                    215:             my $authtype = &Apache::lonnet::queryauthenticate($uname,$udom);
                    216:             if ($authtype =~ /^internal/) {
                    217:                 my $useremail = $env{'form.useremail'};
                    218:                 my ($blocked,$blocktext) =
                    219:                     &Apache::loncommon::blocking_status('passwd',$uname,$udom);
                    220:                 if ($blocked) {
                    221:                     $output = '<p class="LC_warning">'.$blocktext.'</p>'
                    222:                               .&display_actions($contact_email,$domdesc);
                    223:                 } elsif ($useremail !~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
1.37      raeburn   224:                     $output = &invalid_state('baduseremail',$domdesc,
                    225:                                              $contact_name,$contact_email);
1.38      raeburn   226:                 } else {
                    227:                     my %userinfo = 
                    228: 	                &Apache::lonnet::get('environment',\@emailtypes,
                    229: 					     $udom,$uname);
                    230:                     my @allemails;
                    231:                     foreach my $type (@emailtypes) {
                    232:                         my $email = $userinfo{$type};
                    233:                         my @items;
                    234:                         if ($email =~ /,/) {
                    235:                             @items = split(',',$userinfo{$type});
                    236:                         } else {
                    237:                             @items = ($email);
                    238:                         }
                    239:                         foreach my $item (@items) {
                    240:                             if ($item =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) {
                    241:                                 unless(grep(/^\Q$item\E$/,@allemails)) { 
                    242:                                     push(@allemails,$item);
                    243:                                 }
1.18      raeburn   244:                             }
                    245:                         }
1.3       raeburn   246:                     }
1.38      raeburn   247:                     if (@allemails > 0) {
                    248:                         if (grep(/^\Q$useremail\E$/,@allemails)) {
                    249:                             $output = &send_token($uname,$udom,$useremail,$server,
                    250:                                                   $domdesc,$contact_name,
                    251:                                                   $contact_email);
                    252:                         } else {
                    253:                             $output = &invalid_state('mismatch',$domdesc,
                    254:                                                      $contact_name,
                    255:                                                      $contact_email);
                    256:                         }
1.3       raeburn   257:                     } else {
1.38      raeburn   258:                         $output = &invalid_state('missing',$domdesc,
                    259:                                                  $contact_name,$contact_email);
1.3       raeburn   260:                     }
1.1       raeburn   261:                 }
1.38      raeburn   262:             } elsif ($authtype =~ /^(krb|unix|local)/) { 
                    263:                 $output = &invalid_state('authentication',$domdesc,
                    264:                                          $contact_name,$contact_email);
                    265:             } else {
                    266:                 $output = &invalid_state('invalid',$domdesc,
                    267:                                          $contact_name,$contact_email);
1.1       raeburn   268:             }
                    269:         } else {
1.38      raeburn   270:             $output = &get_uname($defdom);
1.1       raeburn   271:         }
                    272:     } else {
                    273:         $output = &get_uname($defdom);
                    274:     }
1.38      raeburn   275:     $r->print($header.$output);
1.1       raeburn   276:     $r->print(&Apache::loncommon::end_page());
                    277:     return OK;
                    278: }
                    279: 
                    280: sub get_uname {
                    281:     my ($defdom) = @_;
                    282:     my %lt = &Apache::lonlocal::texthash(
1.32      bisitz    283:                                          unam => 'LON-CAPA username',
                    284:                                          udom => 'LON-CAPA domain',
1.12      schafran  285:                                          uemail => 'E-mail address in LON-CAPA',
1.1       raeburn   286:                                          proc => 'Proceed');
                    287: 
1.23      bisitz    288:     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    289:     $msg .= '<br /><br />'.&mt('Three conditions must be met:')
                    290:            .'<ul><li>'.&mt('An e-mail address must have previously been associated with your LON-CAPA username.').'</li>'
                    291:            .'<li>'.&mt('You must be able to access e-mail sent to that address.').'</li>'
                    292:            .'<li>'.&mt('Your LON-CAPA account must be of a type for which LON-CAPA can reset a password.')
                    293:            .'</ul>';
1.38      raeburn   294:     my $mobileargs;
                    295:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
                    296:         &Apache::loncommon::decode_user_agent();
                    297:     if ($clientmobile) {
                    298:         $mobileargs = 'autocapitalize="off" autocorrect="off" ';
                    299:     }
                    300:     my $onchange = 'javascript:verifyDomain(this,this.form);';
                    301:     $msg .= '<form name="forgotpw" method="post" action="/adm/resetpw" onsubmit="return validInfo();">'.
1.26      raeburn   302:             &Apache::lonhtmlcommon::start_pick_box(). 
1.32      bisitz    303:             &Apache::lonhtmlcommon::row_title($lt{'unam'}).
1.38      raeburn   304:             '<input type="text" name="uname" size="20" '.$mobileargs.'/>'.
1.26      raeburn   305:             &Apache::lonhtmlcommon::row_closure(1).
1.32      bisitz    306:             &Apache::lonhtmlcommon::row_title($lt{'udom'}).
1.38      raeburn   307:             &Apache::loncommon::select_dom_form($defdom,'udom',undef,undef,$onchange).
1.26      raeburn   308:             &Apache::lonhtmlcommon::row_closure(1).
                    309:             &Apache::lonhtmlcommon::row_title($lt{'uemail'}).
1.38      raeburn   310:             '<input type="text" name="useremail" size="30" '.$mobileargs.'/>'.
1.26      raeburn   311:             &Apache::lonhtmlcommon::end_pick_box().
                    312:             '<br /><br /><input type="submit" name="resetter" value="'.$lt{'proc'}.'" /></form>';
1.1       raeburn   313:     return $msg;
                    314: }
                    315: 
                    316: sub send_token {
                    317:     my ($uname,$udom,$email,$server,$domdesc,$contact_name,
                    318:         $contact_email) = @_;
1.29      bisitz    319:     my $msg =
                    320:         '<p class="LC_info">'
                    321:        .&mt('Thank you for your request to reset the password for your LON-CAPA account.')
                    322:        .'</p>';
1.1       raeburn   323: 
                    324:     my $now = time;
                    325:     my $temppasswd = &create_passwd();
1.2       albertel  326:     my %info = ('ip'         => $ENV{'REMOTE_ADDR'},
                    327: 		'time'       => $now,
                    328: 		'domain'     => $udom,
                    329: 		'username'   => $uname,
                    330: 		'email'      => $email,
                    331: 		'temppasswd' => $temppasswd);
1.1       raeburn   332: 
1.3       raeburn   333:     my $token = &Apache::lonnet::tmpput(\%info,$server,'resetpw');
                    334:     if ($token !~ /^error/ && $token ne 'no_such_host') {
1.2       albertel  335:         my $esc_token = &escape($token);
1.15      raeburn   336:         my $showtime = &Apache::lonlocal::locallocaltime(time);
                    337:         my $reseturl = &Apache::lonnet::absolute_url().'/adm/resetpw?token='.$esc_token;
1.26      raeburn   338:         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   339:         my $result = &send_mail($domdesc,$email,$mailmsg,$contact_name,
                    340:                                 $contact_email);
                    341:         if ($result eq 'ok') {
1.34      bisitz    342:             $msg .=
                    343:                 &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.')
                    344:                .'<br /><br />'
                    345:                .&mt('The link included in the message will be valid for the next [_1]two[_2] hours.','<b>','</b>');
1.1       raeburn   346:         } else {
1.28      bisitz    347:             $msg .=
1.29      bisitz    348:                 '<p class="LC_error">'
                    349:                .&mt('An error occurred when sending a message to the e-mail address'
                    350:                    .' associated with your LON-CAPA account.')
                    351:                .'</p>'
                    352:                .&display_actions($contact_email,$domdesc);
1.1       raeburn   353:         }
                    354:     } else {
1.28      bisitz    355:         $msg .=
1.29      bisitz    356:             '<p class="LC_error">'
                    357:            .&mt('An error occurred creating a token required for the'
                    358:                .' password reset process.')
                    359:            .'</p>'
                    360:            .&display_actions($contact_email,$domdesc);
1.1       raeburn   361:     }
                    362:     return $msg;
                    363: }
                    364: 
                    365: sub send_mail {
                    366:     my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_;
                    367:     my $outcome;
                    368:     my $requestmail = "To: $email\n".
                    369:                       "From: $contact_name <$contact_email>\n".
                    370:                       "Subject: ".&mt('Your LON-CAPA account')."\n".
1.25      bisitz    371:                       "Content-type: text/plain\;charset=UTF-8\n".
1.1       raeburn   372:                       "\n\n".$mailmsg."\n\n".
                    373:                       &mt('[_1] LON-CAPA support team',$domdesc)."\n".
                    374:                       "$contact_email\n";
                    375:     if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) {
                    376:         print MAIL $requestmail;
                    377:         close(MAIL);
                    378:         $outcome = 'ok';
                    379:     } else {
                    380:         $outcome = 'fail';
                    381:     }
                    382:     return $outcome;
                    383: }
                    384: 
                    385: sub invalid_state {
                    386:     my ($error,$domdesc,$contact_name,$contact_email) = @_;
                    387:     my $msg;
                    388:     if ($error eq 'invalid') {
1.29      bisitz    389:         $msg =
                    390:             '<p class="LC_warning">'
                    391:             .&mt('The username you provided was not verified as a valid username'
                    392:                 .' in the LON-CAPA system for the [_1] domain.','<i>'.$domdesc.'</i>')
                    393:               .'</p>';
                    394:         $msg .= &display_actions($contact_email,$domdesc);
1.1       raeburn   395:     } else {
1.3       raeburn   396:         if ($error eq 'baduseremail') {
                    397:             $msg = &mt('The e-mail address you provided does not appear to be a valid address.');
                    398:         } elsif ($error eq 'mismatch') {
                    399:             $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.');  
                    400:         } elsif ($error eq 'missing') {
1.1       raeburn   401:             $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.');
                    402:         } elsif ($error eq 'authentication') {
                    403:             $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.');
1.38      raeburn   404:         } elsif ($error eq 'baddomain') {
                    405:             $msg = &mt('The domain you provided was not verified as a valid domain in the LON-CAPA system.');
1.1       raeburn   406:         }
1.29      bisitz    407:         $msg = '<p class="LC_warning">'.$msg.'</p>'
                    408:               .&display_actions($contact_email,$domdesc);
1.1       raeburn   409:     }
                    410:     return $msg;
                    411: }
                    412: 
1.38      raeburn   413: sub homeserver_redirect {
                    414:     my ($uname,$udom,$domdesc,$brcrum) = @_;
                    415:     my $uhome = &Apache::lonnet::homeserver();
                    416:     if ($uhome eq 'no_host') {
                    417:         $uhome = &Apache::lonnet::domain($udom,'primary');
                    418:     }
                    419:     my $protocol = $Apache::lonnet::protocol{$uhome};
                    420:     $protocol = 'http' if ($protocol ne 'https');
                    421:     my $url = $protocol.'://'.&Apache::lonnet::hostname($uhome).'/adm/resetpw';
                    422:     # Breadcrumbs
                    423:     my $start_page = &Apache::loncommon::start_page('Switching Server',undef,
                    424:                                                     {'redirect' => [0,$url],
                    425:                                                      'bread_crumbs' => $brcrum,});
                    426:     my $output = '<p>'.&mt('This LON-CAPA server belongs to a different domain.').' '.
                    427:                        &mt('You are being switched to your domain ([_1]), to use the "Forgot Password" tool.',$domdesc).
                    428:                  '</p>';
                    429:     return ($start_page,$output);
                    430: }
                    431: 
1.1       raeburn   432: sub reset_passwd {
                    433:     my ($r,$token,$contact_name,$contact_email) = @_;
                    434:     my %data = &Apache::lonnet::tmpget($token);
                    435:     my $now = time;
                    436:     if (keys(%data) == 0) {
1.39      raeburn   437:         $r->print(&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.'
                    438:                   ,'<a href="/adm/resetpw">','</a>')
                    439:         );
                    440:         return;
1.1       raeburn   441:     }
                    442:     if (($data{'time'} =~ /^\d+$/) && 
                    443:         ($data{'username'} ne '') && 
                    444:         ($data{'domain'} ne '') && 
1.3       raeburn   445:         ($data{'email'}  =~ /^[^\@]+\@[^\@]+\.[^\@\.]+$/) && 
1.1       raeburn   446:         ($data{'temppasswd'} =~/^\w+$/)) {
1.9       raeburn   447:         my $reqtime = &Apache::lonlocal::locallocaltime($data{'time'});
1.37      raeburn   448:         my ($blocked,$blocktext) =
                    449:             &Apache::loncommon::blocking_status('passwd',$data{'username'},$data{'domain'});
                    450:         if ($blocked) {
1.39      raeburn   451:             $r->print('<p class="LC_warning">'.$blocktext.'</p>');
                    452:             return;
1.37      raeburn   453:         } elsif ($now - $data{'time'} < 7200) {
1.1       raeburn   454:             if ($env{'form.action'} eq 'verify_and_change_pass') {
1.22      raeburn   455:                 unless (($env{'form.uname'} eq $data{'username'}) && ($env{'form.udom'} eq $data{'domain'}) && ($env{'form.email'} eq $data{'email'})) {
1.39      raeburn   456:                     $r->print(&generic_failure_msg($contact_name,$contact_email));
                    457:                     return;
1.22      raeburn   458:                 }
1.1       raeburn   459:                 my $change_failed = 
1.2       albertel  460: 		    &Apache::lonpreferences::verify_and_change_password($r,'reset_by_email',$token);
1.1       raeburn   461:                 if (!$change_failed) {
                    462:                     my $delete = &Apache::lonnet::tmpdel($token);
1.9       raeburn   463:                     my $now = &Apache::lonlocal::locallocaltime(time);
1.1       raeburn   464:                     my $domdesc = 
1.6       albertel  465: 			&Apache::lonnet::domain($data{'domain'},'description');
1.1       raeburn   466:                     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";
                    467:                     my $result = &send_mail($domdesc,$data{'email'},$mailmsg,
                    468:                                             $contact_name,$contact_email);
1.33      bisitz    469:                     my $confirm_msg;
1.1       raeburn   470:                     if ($result eq 'ok') {
1.33      bisitz    471:                         $confirm_msg =
                    472:                             &Apache::lonhtmlcommon::confirm_success(
                    473:                                 &mt('An e-mail confirming setting of the password'
                    474:                                    .' for your LON-CAPA account has been sent to [_1].'
                    475:                                     ,'<span class="LC_filename">'.$data{'email'}.'</span>'));
1.1       raeburn   476:                     } else {
1.33      bisitz    477:                         $confirm_msg =
                    478:                             &Apache::lonhtmlcommon::confirm_success(
                    479:                                 &mt('An error occurred when sending e-mail to [_1]'
                    480:                                    .' confirming setting of your new password.'
                    481:                                     ,'<span class="LC_filename">'.$data{'email'}.'</span>'),1);
1.1       raeburn   482:                     }
1.39      raeburn   483:                     $r->print(
1.33      bisitz    484:                         &Apache::loncommon::confirmwrapper($confirm_msg)
                    485:                        .&Apache::lonhtmlcommon::actionbox([
1.39      raeburn   486:                             '<a href="/adm/login">'.&mt('Go to the login page').'</a>'])
                    487:                     );
1.20      raeburn   488:                 } elsif ($change_failed eq 'invalid_client') {
                    489:                     my $homeserver = &Apache::lonnet::homeserver($data{'username'},$data{'domain'});
                    490:                     if ($homeserver eq 'no_host') {
1.39      raeburn   491:                         $r->print(&generic_failure_msg($contact_name,$contact_email));
1.20      raeburn   492:                     } else {
                    493:                         my $protocol = $Apache::lonnet::protocol{$homeserver};
                    494:                         $protocol = 'http' if ($protocol ne 'https');
                    495:                         my $url = $protocol.'://'.&Apache::lonnet::hostname($homeserver).
                    496:                                   '/adm/resetpw';
                    497:                         my ($opentag,$closetag);
                    498:                         if ($url) {
                    499:                            $opentag = '<a href="'.$url.'">';
                    500:                            $closetag = '</a>';
                    501:                         }
1.39      raeburn   502:                         $r->print(
1.28      bisitz    503:                             '<p class="LC_warning">'
                    504:                            .&mt('A problem occurred when attempting to reset'
                    505:                                .' the password for your account.'
                    506:                                .' Please try again from your [_1]home server[_2].'
                    507:                                 ,$opentag,$closetag)
1.39      raeburn   508:                            .'</p>'
                    509:                         );
1.20      raeburn   510:                     }
1.1       raeburn   511:                 } else {
1.39      raeburn   512:                     $r->print(&generic_failure_msg($contact_name,$contact_email));
1.1       raeburn   513:                 }
                    514:             } else {
1.13      schafran  515:                 $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   516:                 $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 />');
                    517:                 &Apache::lonpreferences::passwordchanger($r,'','reset_by_email',$token);
                    518:             }
                    519:         } else {
1.39      raeburn   520:             $r->print(
1.28      bisitz    521:                 '<p class="LC_warning">'
                    522:                .&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.'
                    523:                     ,'<a href="/adm/resetpw">','</a>')
1.39      raeburn   524:                .'</p>'
                    525:             );
1.1       raeburn   526:         }
                    527:     } else {
1.39      raeburn   528:         $r->print(
1.28      bisitz    529:             '<p class="LC_warning">'
                    530:            .&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.'
                    531:                 ,'<a href="/adm/resetpw">','</a>')
1.39      raeburn   532:            .'</p>'
                    533:         );
1.1       raeburn   534:     }
1.39      raeburn   535:     return;
1.1       raeburn   536: }
                    537: 
1.20      raeburn   538: sub generic_failure_msg {
                    539:     my ($contact_name,$contact_email) = @_;
1.28      bisitz    540:     return
1.29      bisitz    541:         '<p class="LC_error">'
                    542:        .&mt('A problem occurred when attempting to reset the password for your account.')
                    543:        .'<br />'
1.36      raeburn   544:        .&mt('Please contact the [_1] ([_2]) for assistance.',
1.28      bisitz    545:               $contact_name,'<a href="mailto:'.$contact_email.'">'.$contact_email.'</a>')
                    546:        .'</p>';
1.20      raeburn   547: }
                    548: 
1.1       raeburn   549: sub create_passwd {
                    550:     my $passwd = '';
1.2       albertel  551:     my @letts = ("a".."z");
1.1       raeburn   552:     for (my $i=0; $i<8; $i++) {
1.2       albertel  553:         my $lettnum = int(rand(2));
1.1       raeburn   554:         my $item = '';
                    555:         if ($lettnum) {
1.2       albertel  556:             $item = $letts[int(rand(26))];
                    557:             my $uppercase = int(rand(2));
1.1       raeburn   558:             if ($uppercase) {
                    559:                 $item =~ tr/a-z/A-Z/;
                    560:             }
                    561:         } else {
1.2       albertel  562:             $item = int(rand(10));
1.1       raeburn   563:         }
                    564:         $passwd .= $item;
                    565:     }
                    566:     return ($passwd);
                    567: }
                    568: 
1.29      bisitz    569: sub display_actions {
                    570:     my ($contact_email, $domdesc) = @_;
                    571:     my @msg = (&mt('[_1]Go back[_2] and try again',
                    572:                    '<a href="javascript:history.go(-1)">','</a>'));
                    573:     my $msg2 = '';
                    574:     if ($contact_email ne '') {
1.30      raeburn   575:         my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"');
                    576:         push(@msg, &mt('Contact the [_1]LON-CAPA helpdesk[_2] for the institution: [_3]',
                    577:                        '<a href="/adm/helpdesk?origurl='.$escuri.'">',
                    578:                        '</a>','<i>'.$domdesc.'</i>'));
                    579:     } else {
                    580:         $msg2 =
                    581:             '<p>'
                    582:            .&mt('You may wish to send an e-mail to the'
                    583:            .' server administrator: [_1] for the [_2] domain.',
1.31      raeburn   584:                 '<i>'.$Apache::lonnet::perlvar{'AdmEMail'}.'</i>',
1.30      raeburn   585:                 '<i>'.$domdesc.'</i>')
                    586:            .'</p>';
                    587:     }
1.29      bisitz    588: 
                    589:     return &Apache::lonhtmlcommon::actionbox(\@msg).$msg2;
                    590: 
                    591: }
                    592: 
1.1       raeburn   593: 1;

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