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

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

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