Annotation of loncom/interface/lonpreferences.pm, revision 1.33

1.1       www         1: # The LearningOnline Network
                      2: # Preferences
                      3: #
1.33    ! www         4: # $Id: lonpreferences.pm,v 1.32 2003/11/07 23:22:21 www Exp $
1.2       albertel    5: #
                      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: #
1.1       www        28: # (Internal Server Error Handler
                     29: #
                     30: # (Login Screen
                     31: # 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14,
                     32: # 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9 Gerd Kortemeyer)
                     33: #
                     34: # 3/1/1 Gerd Kortemeyer)
                     35: #
                     36: # 3/1 Gerd Kortemeyer
                     37: #
1.3       matthew    38: # 2/13/02 2/14 2/15 Matthew Hall
                     39: #
                     40: # This package uses the "londes.js" javascript code. 
                     41: #
                     42: # TODOs that have to be completed:
                     43: #    interface with lonnet to change the password
                     44:  
1.1       www        45: package Apache::lonpreferences;
                     46: 
                     47: use strict;
                     48: use Apache::Constants qw(:common);
1.3       matthew    49: use Apache::File;
                     50: use Crypt::DES;
                     51: use DynaLoader; # for Crypt::DES version
1.4       matthew    52: use Apache::loncommon();
1.23      matthew    53: use Apache::lonhtmlcommon();
1.32      www        54: use Apache::lonlocal;
1.3       matthew    55: 
                     56: #
                     57: # Write lonnet::passwd to do the call below.
                     58: # Use:
                     59: #   my $answer=reply("encrypt:passwd:$udom:$uname:$upass",$tryserver);
                     60: #
                     61: ##################################################
                     62: #          password associated functions         #
                     63: ##################################################
                     64: sub des_keys {
1.4       matthew    65:     # Make a new key for DES encryption.
                     66:     # Each key has two parts which are returned seperately.
                     67:     # Please note:  Each key must be passed through the &hex function
                     68:     # before it is output to the web browser.  The hex versions cannot
                     69:     # be used to decrypt.
1.3       matthew    70:     my @hexstr=('0','1','2','3','4','5','6','7',
                     71:                 '8','9','a','b','c','d','e','f');
                     72:     my $lkey='';
                     73:     for (0..7) {
                     74:         $lkey.=$hexstr[rand(15)];
                     75:     }
                     76:     my $ukey='';
                     77:     for (0..7) {
                     78:         $ukey.=$hexstr[rand(15)];
                     79:     }
                     80:     return ($lkey,$ukey);
                     81: }
                     82: 
                     83: sub des_decrypt {
                     84:     my ($key,$cyphertext) = @_;
                     85:     my $keybin=pack("H16",$key);
                     86:     my $cypher;
                     87:     if ($Crypt::DES::VERSION>=2.03) {
                     88:         $cypher=new Crypt::DES $keybin;
                     89:     } else {
                     90:         $cypher=new DES $keybin;
                     91:     }
                     92:     my $plaintext=
                     93: 	$cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,0,16))));
                     94:     $plaintext.=
                     95: 	$cypher->decrypt(unpack("a8",pack("H16",substr($cyphertext,16,16))));
1.4       matthew    96:     $plaintext=substr($plaintext,1,ord(substr($plaintext,0,1)) );
1.3       matthew    97:     return $plaintext;
                     98: }
                     99: 
1.4       matthew   100: ################################################################
                    101: #                       Handler subroutines                    #
                    102: ################################################################
1.9       matthew   103: 
                    104: ################################################################
1.28      www       105: #         Language Change Subroutines                          #
                    106: ################################################################
                    107: sub languagechanger {
                    108:     my $r = shift;
                    109:     my $user       = $ENV{'user.name'};
                    110:     my $domain     = $ENV{'user.domain'};
                    111:     my %userenv = &Apache::lonnet::get
1.32      www       112:         ('environment',['languages']);
1.29      www       113:     my $language=$userenv{'languages'};
1.32      www       114: 
1.28      www       115:     my $bodytag=&Apache::loncommon::bodytag(
                    116:               'Change Your Language Preferences');
1.33    ! www       117:     my $pref=&mt('Preferred language');
        !           118:     my %langchoices=('' => 'No language preference');
        !           119:     foreach (&Apache::loncommon::languageids()) {
        !           120: 	if (&Apache::loncommon::supportedlanguagecode($_)) {
        !           121: 	    $langchoices{&Apache::loncommon::supportedlanguagecode($_)}
        !           122: 	               = &Apache::loncommon::plainlanguagedescription($_);
        !           123: 	}
        !           124:     }
        !           125:     my $selectionbox=&Apache::loncommon::select_form($language,'language',
        !           126: 						     %langchoices);
1.28      www       127:     $r->print(<<ENDLSCREEN);
                    128: <html>
                    129: $bodytag
                    130: 
                    131: <form name="server" action="/adm/preferences" method="post">
                    132: <input type="hidden" name="action" value="verify_and_change_languages" />
1.33    ! www       133: <br />$pref: $selectionbox
1.28      www       134: ENDLSCREEN
1.32      www       135:     $r->print('<br /><input type="submit" value="'.&mt('Change').'" /></form></body></html>');
1.28      www       136: }
                    137: 
                    138: 
                    139: sub verify_and_change_languages {
                    140:     my $r = shift;
                    141:     my $user       = $ENV{'user.name'};
                    142:     my $domain     = $ENV{'user.domain'};
                    143: # Screenname
                    144:     my $newlanguage  = $ENV{'form.language'};
                    145:     $newlanguage=~s/[^\-\w]//g;
                    146:     my $message='';
                    147:     if ($newlanguage) {
1.29      www       148:         &Apache::lonnet::put('environment',{'languages' => $newlanguage});
                    149:         &Apache::lonnet::appenv('environment.languages' => $newlanguage);
                    150:         $message='Set new preferred languages to '.$newlanguage;
1.28      www       151:     } else {
1.29      www       152:         &Apache::lonnet::del('environment',['languages']);
                    153:         &Apache::lonnet::delenv('environment\.languages');
1.28      www       154:         $message='Reset preferred language';
                    155:     }
                    156:     my $bodytag=&Apache::loncommon::bodytag(
                    157:                     'Change Your Language Preferences');
                    158:     $r->print(<<ENDVCSCREEN);
                    159: <html>
                    160: $bodytag
                    161: </p>
                    162: $message
                    163: </body></html>
                    164: ENDVCSCREEN
                    165: }
                    166: 
                    167: 
                    168: ################################################################
1.9       matthew   169: #         Anonymous Discussion Name Change Subroutines         #
                    170: ################################################################
1.5       www       171: sub screennamechanger {
                    172:     my $r = shift;
                    173:     my $user       = $ENV{'user.name'};
                    174:     my $domain     = $ENV{'user.domain'};
1.14      www       175:     my %userenv = &Apache::lonnet::get
                    176:         ('environment',['screenname','nickname']);
1.6       www       177:     my $screenname=$userenv{'screenname'};
1.14      www       178:     my $nickname=$userenv{'nickname'};
1.10      www       179:     my $bodytag=&Apache::loncommon::bodytag(
1.14      www       180:               'Change Your Nickname and Anonymous Screen Name');
1.5       www       181:     $r->print(<<ENDSCREEN);
                    182: <html>
1.10      www       183: $bodytag
                    184: 
1.6       www       185: <form name="server" action="/adm/preferences" method="post">
                    186: <input type="hidden" name="action" value="verify_and_change_screenname" />
1.14      www       187: <br />New screenname (shown if you post anonymously):
1.6       www       188: <input type="text" size="20" value="$screenname" name="screenname" />
1.14      www       189: <br />New nickname (shown if you post non-anonymously):
                    190: <input type="text" size="20" value="$nickname" name="nickname" />
1.6       www       191: <input type="submit" value="Change" />
                    192: </form>
1.5       www       193: </body>
                    194: </html>
                    195: ENDSCREEN
                    196: }
1.6       www       197: 
                    198: sub verify_and_change_screenname {
                    199:     my $r = shift;
                    200:     my $user       = $ENV{'user.name'};
                    201:     my $domain     = $ENV{'user.domain'};
1.14      www       202: # Screenname
1.6       www       203:     my $newscreen  = $ENV{'form.screenname'};
1.14      www       204:     $newscreen=~s/[^ \w]//g;
1.6       www       205:     my $message='';
                    206:     if ($newscreen) {
1.7       www       207:         &Apache::lonnet::put('environment',{'screenname' => $newscreen});
                    208:         &Apache::lonnet::appenv('environment.screenname' => $newscreen);
1.6       www       209:         $message='Set new screenname to '.$newscreen;
                    210:     } else {
                    211:         &Apache::lonnet::del('environment',['screenname']);
1.7       www       212:         &Apache::lonnet::delenv('environment\.screenname');
1.6       www       213:         $message='Reset screenname';
                    214:     }
1.14      www       215: # Nickname
                    216:     $message.='<br />';
1.17      matthew   217:     $newscreen  = $ENV{'form.nickname'};
1.14      www       218:     $newscreen=~s/[^ \w]//g;
                    219:     if ($newscreen) {
                    220:         &Apache::lonnet::put('environment',{'nickname' => $newscreen});
                    221:         &Apache::lonnet::appenv('environment.nickname' => $newscreen);
                    222:         $message.='Set new nickname to '.$newscreen;
                    223:     } else {
                    224:         &Apache::lonnet::del('environment',['nickname']);
                    225:         &Apache::lonnet::delenv('environment\.nickname');
                    226:         $message.='Reset nickname';
                    227:     }
                    228: 
1.10      www       229:     my $bodytag=&Apache::loncommon::bodytag(
1.14      www       230:                     'Change Your Nickname and Anonymous Screen Name');
1.6       www       231:     $r->print(<<ENDVCSCREEN);
                    232: <html>
1.10      www       233: $bodytag
1.6       www       234: </p>
                    235: $message
                    236: </body></html>
                    237: ENDVCSCREEN
1.20      www       238: }
                    239: 
                    240: ################################################################
                    241: #         Message Forward                                      #
                    242: ################################################################
                    243: 
                    244: sub msgforwardchanger {
                    245:     my $r = shift;
                    246:     my $user       = $ENV{'user.name'};
                    247:     my $domain     = $ENV{'user.domain'};
1.26      www       248:     my %userenv = &Apache::lonnet::get('environment',['msgforward','notification','critnotification']);
1.20      www       249:     my $msgforward=$userenv{'msgforward'};
                    250:     my $notification=$userenv{'notification'};
                    251:     my $critnotification=$userenv{'critnotification'};
                    252:     my $bodytag=&Apache::loncommon::bodytag(
                    253:                     'Change Your Message Forwarding and Notification');
1.25      bowersj2  254:     my $forwardingHelp = Apache::loncommon::help_open_topic("Prefs_Forwarding",
                    255: 							    "What are forwarding ".
                    256: 							    "and notification ".
                    257: 							    "addresses");
1.27      bowersj2  258:     my $criticalMessageHelp = Apache::loncommon::help_open_topic("Course_Critical_Message",
                    259: 								 "What are critical messages");
                    260: 
1.20      www       261:     $r->print(<<ENDMSG);
                    262: <html>
                    263: $bodytag
1.25      bowersj2  264: $forwardingHelp <br />
1.20      www       265: <form name="server" action="/adm/preferences" method="post">
                    266: <input type="hidden" name="action" value="verify_and_change_msgforward" />
                    267: New Forwarding Address(es) (<tt>user:domain,user:domain,...</tt>):
                    268: <input type="text" size="40" value="$msgforward" name="msgforward" /><hr />
                    269: New Message Notification Email Address(es) (<tt>joe\@doe.com,jane\@doe.edu,...</tt>):
                    270: <input type="text" size="40" value="$notification" name="notification" /><hr />
                    271: New Critical Message Notification Email Address(es) (<tt>joe\@doe.com,jane\@doe.edu,...</tt>):
1.27      bowersj2  272: <input type="text" size="40" value="$critnotification" name="critnotification" />$criticalMessageHelp<hr />
1.20      www       273: <input type="submit" value="Change" />
                    274: </form>
                    275: </body>
                    276: </html>
                    277: ENDMSG
                    278: }
                    279: 
                    280: sub verify_and_change_msgforward {
                    281:     my $r = shift;
                    282:     my $user       = $ENV{'user.name'};
                    283:     my $domain     = $ENV{'user.domain'};
                    284:     my $newscreen  = '';
                    285:     my $message='';
                    286:     foreach (split(/\,/,$ENV{'form.msgforward'})) {
                    287: 	my ($msuser,$msdomain)=split(/[\@\:]/,$_);
                    288:         $msuser=~s/\W//g;
                    289:         $msdomain=~s/\W//g;
                    290:         if (($msuser) && ($msdomain)) {
                    291: 	    if (&Apache::lonnet::homeserver($msuser,$msdomain) ne 'no_host') {
                    292:                $newscreen.=$msuser.':'.$msdomain.',';
                    293: 	   } else {
                    294:                $message.='No such user: '.$msuser.':'.$msdomain.'<br>';
                    295:            }
                    296:         }
                    297:     }
                    298:     $newscreen=~s/\,$//;
                    299:     if ($newscreen) {
                    300:         &Apache::lonnet::put('environment',{'msgforward' => $newscreen});
                    301:         &Apache::lonnet::appenv('environment.msgforward' => $newscreen);
                    302:         $message.='Set new message forwarding to '.$newscreen.'<br />';
                    303:     } else {
                    304:         &Apache::lonnet::del('environment',['msgforward']);
                    305:         &Apache::lonnet::delenv('environment\.msgforward');
                    306:         $message.='Reset message forwarding<br />';
                    307:     }
                    308:     my $notification=$ENV{'form.notification'};
                    309:     $notification=~s/\s//gs;
                    310:     if ($notification) {
                    311:         &Apache::lonnet::put('environment',{'notification' => $notification});
                    312:         &Apache::lonnet::appenv('environment.notification' => $notification);
                    313:         $message.='Set message notification address to '.$notification.'<br />';
                    314:     } else {
                    315:         &Apache::lonnet::del('environment',['notification']);
                    316:         &Apache::lonnet::delenv('environment\.notification');
                    317:         $message.='Reset message notification<br />';
                    318:     }
                    319:     my $critnotification=$ENV{'form.critnotification'};
                    320:     $critnotification=~s/\s//gs;
                    321:     if ($critnotification) {
                    322:         &Apache::lonnet::put('environment',{'critnotification' => $critnotification});
                    323:         &Apache::lonnet::appenv('environment.critnotification' => $critnotification);
                    324:         $message.='Set critical message notification address to '.$critnotification;
                    325:     } else {
                    326:         &Apache::lonnet::del('environment',['critnotification']);
                    327:         &Apache::lonnet::delenv('environment\.critnotification');
                    328:         $message.='Reset critical message notification<br />';
                    329:     }
                    330:     my $bodytag=&Apache::loncommon::bodytag(
                    331:                            'Change Your Message Forwarding and Notifications');
                    332:     $r->print(<<ENDVCMSG);
                    333: <html>
                    334: $bodytag
                    335: </p>
                    336: $message
                    337: </body></html>
                    338: ENDVCMSG
1.6       www       339: }
                    340: 
1.12      www       341: ################################################################
1.19      www       342: #         Colors                                               #
1.12      www       343: ################################################################
                    344: 
1.19      www       345: sub colorschanger {
1.12      www       346:     my $r = shift;
                    347:     my $bodytag=&Apache::loncommon::bodytag(
1.21      www       348:                     'Change Color Scheme for Current Role Type','',
                    349:                     'onUnload="pclose();"');
1.19      www       350: # figure out colors
                    351:     my $function='student';
                    352:     if ($ENV{'request.role'}=~/^(cc|in|ta|ep)/) {
                    353: 	$function='coordinator';
                    354:     }
                    355:     if ($ENV{'request.role'}=~/^(su|dc|ad|li)/) {
                    356: 	$function='admin';
                    357:     }
                    358:     if (($ENV{'request.role'}=~/^(au|ca)/) ||
                    359: 	($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
                    360: 	$function='author';
                    361:     }
                    362:     my $domain=&Apache::loncommon::determinedomain();
                    363:     my %colortypes=('pgbg'  => 'Page Background',
                    364:                     'tabbg' => 'Header Background',
                    365:                     'sidebg'=> 'Header Border',
                    366:                     'font'  => 'Font',
                    367:                     'link'  => 'Un-Visited Link',
                    368:                     'vlink' => 'Visited Link',
                    369:                     'alink' => 'Active Link');
                    370:     my $chtable='';
1.22      matthew   371:     foreach my $item (sort(keys(%colortypes))) {
1.19      www       372:        my $curcol=&Apache::loncommon::designparm($function.'.'.$item,$domain);
                    373:        $chtable.='<tr><td>'.$colortypes{$item}.'</td><td bgcolor="'.$curcol.
                    374:         '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td><input name="'.$item.
1.21      www       375:         '" size="10" value="'.$curcol.
                    376: '" /></td><td><a href="javascript:pjump('."'color_custom','".$colortypes{$item}.
1.19      www       377: "','".$curcol."','"
1.21      www       378: 	    .$item."','parmform.pres','psub'".');">Select</a></td></tr>';
1.19      www       379:     }
1.23      matthew   380:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
1.19      www       381:     $r->print(<<ENDCOL);
1.12      www       382: <html>
1.19      www       383: <script>
                    384: 
                    385:     function pclose() {
                    386:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms",
                    387:                  "height=350,width=350,scrollbars=no,menubar=no");
                    388:         parmwin.close();
                    389:     }
                    390: 
1.23      matthew   391:     $pjump_def
1.19      www       392: 
                    393:     function psub() {
                    394:         pclose();
                    395:         if (document.parmform.pres_marker.value!='') {
1.21      www       396:             if (document.parmform.pres_type.value!='') {
                    397:                 eval('document.server.'+
                    398:                      document.parmform.pres_marker.value+
1.19      www       399: 		     '.value=document.parmform.pres_value.value;');
1.21      www       400: 	    }
1.19      www       401:         } else {
                    402:             document.parmform.pres_value.value='';
                    403:             document.parmform.pres_marker.value='';
                    404:         }
                    405:     }
                    406: 
                    407: 
                    408: </script>
1.12      www       409: $bodytag
1.21      www       410: <form name="parmform">
                    411: <input type="hidden" name="pres_marker" />
                    412: <input type="hidden" name="pres_type" />
                    413: <input type="hidden" name="pres_value" />
                    414: </form>
1.12      www       415: <form name="server" action="/adm/preferences" method="post">
1.19      www       416: <input type="hidden" name="action" value="verify_and_change_colors" />
                    417: <table border="2">
                    418: $chtable
                    419: </table>
1.21      www       420: <input type="submit" value="Change Custom Colors" />
                    421: <input type="submit" name="resetall" value="Reset All Colors to Default" />
1.12      www       422: </form>
                    423: </body>
                    424: </html>
1.19      www       425: ENDCOL
1.12      www       426: }
                    427: 
1.19      www       428: sub verify_and_change_colors {
1.12      www       429:     my $r = shift;
1.19      www       430: # figure out colors
                    431:     my $function='student';
                    432:     if ($ENV{'request.role'}=~/^(cc|in|ta|ep)/) {
                    433: 	$function='coordinator';
                    434:     }
                    435:     if ($ENV{'request.role'}=~/^(su|dc|ad|li)/) {
                    436: 	$function='admin';
                    437:     }
                    438:     if (($ENV{'request.role'}=~/^(au|ca)/) ||
                    439: 	($ENV{'REQUEST_URI'}=~/^(\/priv|\~)/)) {
                    440: 	$function='author';
                    441:     }
                    442:     my $domain=&Apache::loncommon::determinedomain();
                    443:     my %colortypes=('pgbg'  => 'Page Background',
                    444:                     'tabbg' => 'Header Background',
                    445:                     'sidebg'=> 'Header Border',
                    446:                     'font'  => 'Font',
                    447:                     'link'  => 'Un-Visited Link',
                    448:                     'vlink' => 'Visited Link',
                    449:                     'alink' => 'Active Link');
                    450: 
1.12      www       451:     my $message='';
1.21      www       452:     foreach my $item (keys %colortypes) {
                    453:         my $color=$ENV{'form.'.$item};
                    454:         my $entry='color.'.$function.'.'.$item;
                    455: 	if (($color=~/^\#[0-9A-Fa-f]{6}$/) && (!$ENV{'form.resetall'})) {
                    456: 	    &Apache::lonnet::put('environment',{$entry => $color});
                    457: 	    &Apache::lonnet::appenv('environment.'.$entry => $color);
                    458: 	    $message.='Set '.$colortypes{$item}.' to '.$color.'<br />';
                    459: 	} else {
                    460: 	    &Apache::lonnet::del('environment',[$entry]);
                    461: 	    &Apache::lonnet::delenv('environment\.'.$entry);
                    462: 	    $message.='Reset '.$colortypes{$item}.'<br />';
                    463: 	}
                    464:     }
1.12      www       465:     my $bodytag=&Apache::loncommon::bodytag(
1.19      www       466:                            'Change Color Scheme for Current Role Type');
                    467:     $r->print(<<ENDVCCOL);
1.12      www       468: <html>
                    469: $bodytag
                    470: </p>
                    471: $message
1.21      www       472: <form name="client" action="/adm/preferences" method="post">
                    473: <input type="hidden" name="action" value="changecolors" />
                    474: <input type="submit" value="Revise color scheme again" />
                    475: </form>
1.12      www       476: </body></html>
1.19      www       477: ENDVCCOL
1.12      www       478: }
                    479: 
1.4       matthew   480: ######################################################
                    481: #            password handler subroutines            #
                    482: ######################################################
1.3       matthew   483: sub passwordchanger {
1.4       matthew   484:     # This function is a bit of a mess....
1.3       matthew   485:     # Passwords are encrypted using londes.js (DES encryption)
                    486:     my $r = shift;
1.4       matthew   487:     my $errormessage = shift;
                    488:     $errormessage = ($errormessage || '');
1.3       matthew   489:     my $user       = $ENV{'user.name'};
                    490:     my $domain     = $ENV{'user.domain'};
                    491:     my $homeserver = $ENV{'user.home'};
                    492:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
                    493:     # Check for authentication types that allow changing of the password.
                    494:     return if ($currentauth !~ /^(unix|internal):/);
                    495:     #
                    496:     # Generate keys
                    497:     my ($lkey_cpass ,$ukey_cpass ) = &des_keys();
                    498:     my ($lkey_npass1,$ukey_npass1) = &des_keys();
                    499:     my ($lkey_npass2,$ukey_npass2) = &des_keys();
1.4       matthew   500:     # Store the keys in the log files
1.3       matthew   501:     my $lonhost = $r->dir_config('lonHostID');
                    502:     my $logtoken=Apache::lonnet::reply('tmpput:'
                    503: 				       .$ukey_cpass  . $lkey_cpass .'&'
                    504: 				       .$ukey_npass1 . $lkey_npass1.'&'
                    505: 				       .$ukey_npass2 . $lkey_npass2,
                    506: 				       $lonhost);
1.4       matthew   507:     # Hexify the keys for output as javascript variables
1.3       matthew   508:     $ukey_cpass = hex($ukey_cpass);
                    509:     $lkey_cpass = hex($lkey_cpass);
                    510:     $ukey_npass1= hex($ukey_npass1);
                    511:     $lkey_npass1= hex($lkey_npass1);
                    512:     $ukey_npass2= hex($ukey_npass2);
                    513:     $lkey_npass2= hex($lkey_npass2);
                    514:     # Output javascript to deal with passwords
1.4       matthew   515:     # Output DES javascript
1.9       matthew   516:     $r->print("<html><head>");
1.3       matthew   517:     {
                    518: 	my $include = $r->dir_config('lonIncludes');
                    519: 	my $jsh=Apache::File->new($include."/londes.js");
                    520: 	$r->print(<$jsh>);
                    521:     }
1.10      www       522:     my $bodytag=&Apache::loncommon::bodytag('Change Password','',
                    523:                                          'onLoad="init();"');
1.3       matthew   524:     $r->print(<<ENDFORM);
1.9       matthew   525: </head>
1.10      www       526: $bodytag
1.1       www       527: 
1.3       matthew   528: <script language="JavaScript">
                    529: 
                    530:     function send() {
                    531:         uextkey=this.document.client.elements.ukey_cpass.value;
                    532:         lextkey=this.document.client.elements.lkey_cpass.value;
                    533:         initkeys();
                    534: 
                    535:         this.document.server.elements.currentpass.value
                    536:             =crypted(this.document.client.elements.currentpass.value);
                    537: 
                    538:         uextkey=this.document.client.elements.ukey_npass1.value;
                    539:         lextkey=this.document.client.elements.lkey_npass1.value;
                    540:         initkeys();
                    541:         this.document.server.elements.newpass_1.value
                    542:             =crypted(this.document.client.elements.newpass_1.value);
                    543: 
                    544:         uextkey=this.document.client.elements.ukey_npass2.value;
                    545:         lextkey=this.document.client.elements.lkey_npass2.value;
                    546:         initkeys();
                    547:         this.document.server.elements.newpass_2.value
                    548:             =crypted(this.document.client.elements.newpass_2.value);
                    549: 
                    550:         this.document.server.submit();
                    551:     }
                    552: 
                    553: </script>
1.4       matthew   554: $errormessage
1.10      www       555: 
1.3       matthew   556: <p>
                    557: <!-- We seperate the forms into 'server' and 'client' in order to
                    558:      ensure that unencrypted passwords will not be sent out by a
                    559:      crappy browser -->
                    560: 
                    561: <form name="server" action="/adm/preferences" method="post">
                    562: <input type="hidden" name="logtoken"    value="$logtoken" />
                    563: <input type="hidden" name="action"      value="verify_and_change_pass" />
                    564: <input type="hidden" name="currentpass" value="" />
1.4       matthew   565: <input type="hidden" name="newpass_1"   value="" />
                    566: <input type="hidden" name="newpass_2"   value="" />
1.3       matthew   567: </form>
                    568: 
                    569: <form name="client" >
                    570: <table>
1.4       matthew   571: <tr><td align="right"> Current password:                      </td>
                    572:     <td><input type="password" name="currentpass" size="10"/> </td></tr>
                    573: <tr><td align="right"> New password:                          </td>
                    574:     <td><input type="password" name="newpass_1" size="10"  /> </td></tr>
                    575: <tr><td align="right"> Confirm password:                      </td>
                    576:     <td><input type="password" name="newpass_2" size="10"  /> </td></tr>
1.3       matthew   577: <tr><td colspan="2" align="center">
                    578:     <input type="button" value="Change Password" onClick="send();">
                    579: </table>
1.4       matthew   580: <input type="hidden" name="ukey_cpass"  value="$ukey_cpass" />
                    581: <input type="hidden" name="lkey_cpass"  value="$lkey_cpass" />
1.3       matthew   582: <input type="hidden" name="ukey_npass1" value="$ukey_npass1" />
                    583: <input type="hidden" name="lkey_npass1" value="$lkey_npass1" />
                    584: <input type="hidden" name="ukey_npass2" value="$ukey_npass2" />
                    585: <input type="hidden" name="lkey_npass2" value="$lkey_npass2" />
                    586: </form>
                    587: </p>
                    588: ENDFORM
                    589:     #
                    590:     return;
                    591: }
                    592: 
                    593: sub verify_and_change_password {
                    594:     my $r = shift;
                    595:     my $user       = $ENV{'user.name'};
                    596:     my $domain     = $ENV{'user.domain'};
                    597:     my $homeserver = $ENV{'user.home'};
                    598:     my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
1.4       matthew   599:     # Check for authentication types that allow changing of the password.
                    600:     return if ($currentauth !~ /^(unix|internal):/);
1.3       matthew   601:     #
1.4       matthew   602:     $r->print(<<ENDHEADER);
                    603: <html>
                    604: <head>
                    605: <title>LON-CAPA Preferences:  Change password for $user</title>
                    606: </head>
                    607: ENDHEADER
1.3       matthew   608:     #
                    609:     my $currentpass = $ENV{'form.currentpass'}; 
                    610:     my $newpass1    = $ENV{'form.newpass_1'}; 
                    611:     my $newpass2    = $ENV{'form.newpass_2'};
                    612:     my $logtoken    = $ENV{'form.logtoken'};
                    613:     # Check for empty data 
1.4       matthew   614:     unless (defined($currentpass) && 
                    615: 	    defined($newpass1)    && 
                    616: 	    defined($newpass2)    ){
                    617: 	&passwordchanger($r,"<p>\n<font color='#ff0000'>ERROR</font>".
                    618: 			 "Password data was blank.\n</p>");
1.3       matthew   619: 	return;
                    620:     }
1.16      albertel  621:     # Get the keys
                    622:     my $lonhost = $r->dir_config('lonHostID');
1.3       matthew   623:     my $tmpinfo = Apache::lonnet::reply('tmpget:'.$logtoken,$lonhost);
                    624:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.4       matthew   625:         # I do not a have a better idea about how to handle this
1.3       matthew   626: 	$r->print(<<ENDERROR);
                    627: <p>
                    628: <font color="#ff0000">ERROR:</font> Unable to retrieve stored token for
1.4       matthew   629: password decryption.  Please log out and try again.
1.3       matthew   630: </p>
                    631: ENDERROR
1.4       matthew   632:         # Probably should log an error here
1.3       matthew   633:         return;
                    634:     }
                    635:     my ($ckey,$n1key,$n2key)=split(/&/,$tmpinfo);
1.4       matthew   636:     # 
1.17      matthew   637:     $currentpass = &des_decrypt($ckey ,$currentpass);
                    638:     $newpass1    = &des_decrypt($n1key,$newpass1);
                    639:     $newpass2    = &des_decrypt($n2key,$newpass2);
1.4       matthew   640:     # 
1.3       matthew   641:     if ($newpass1 ne $newpass2) {
1.4       matthew   642: 	&passwordchanger($r,
                    643: 			 '<font color="#ff0000">ERROR:</font>'.
                    644: 			 'The new passwords you entered do not match.  '.
                    645: 			 'Please try again.');
                    646: 	return;
                    647:     }
                    648:     if (length($newpass1) < 7) {
                    649: 	&passwordchanger($r,
                    650: 			 '<font color="#ff0000">ERROR:</font>'.
                    651: 			 'Passwords must be a minimum of 7 characters long.  '.
                    652: 			 'Please try again.');
1.3       matthew   653: 	return;
                    654:     }
1.4       matthew   655:     #
                    656:     # Check for bad characters
                    657:     my $badpassword = 0;
                    658:     foreach (split(//,$newpass1)) {
                    659: 	$badpassword = 1 if ((ord($_)<32)||(ord($_)>126));
                    660:     }
                    661:     if ($badpassword) {
                    662: 	# I can't figure out how to enter bad characters on my browser.
                    663: 	&passwordchanger($r,<<ENDERROR);
                    664: <font color="#ff0000">ERROR:</font>
                    665: The password you entered contained illegal characters.<br />
                    666: Valid characters are: space and <br />
                    667: <pre>
                    668: !&quot;\#$%&amp;\'()*+,-./0123456789:;&lt;=&gt;?\@
                    669: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}~
                    670: </pre>
                    671: ENDERROR
                    672:     }
                    673:     # 
                    674:     # Change the password (finally)
                    675:     my $result = &Apache::lonnet::changepass
                    676: 	($user,$domain,$currentpass,$newpass1,$homeserver);
                    677:     # Inform the user the password has (not?) been changed
                    678:     if ($result =~ /^ok$/) {
                    679: 	$r->print(<<"ENDTEXT");
1.9       matthew   680: <h2>The password for $user was successfully changed</h2>
1.4       matthew   681: ENDTEXT
                    682:     } else {
                    683: 	# error error: run in circles, scream and shout
                    684:         $r->print(<<ENDERROR);
1.9       matthew   685: <h2><font color="#ff0000">The password for $user was not changed</font></h2>
1.8       matthew   686: Please make sure your old password was entered correctly.
1.4       matthew   687: ENDERROR
                    688:     }
                    689:     return;
1.3       matthew   690: }
                    691: 
1.4       matthew   692: ######################################################
                    693: #            other handler subroutines               #
                    694: ######################################################
                    695: 
1.3       matthew   696: ################################################################
                    697: #                          Main handler                        #
                    698: ################################################################
1.1       www       699: sub handler {
                    700:     my $r = shift;
1.3       matthew   701:     my $user = $ENV{'user.name'};
                    702:     my $domain = $ENV{'user.domain'};
1.31      www       703:     &Apache::loncommon::content_type($r,'text/html');
1.4       matthew   704:     # Some pages contain DES keys and should not be cached.
                    705:     &Apache::loncommon::no_cache($r);
1.1       www       706:     $r->send_http_header;
                    707:     return OK if $r->header_only;
1.9       matthew   708:     #
1.3       matthew   709:     if ($ENV{'form.action'} eq 'changepass') {
                    710: 	&passwordchanger($r);
                    711:     } elsif ($ENV{'form.action'} eq 'verify_and_change_pass') {
                    712: 	&verify_and_change_password($r);
1.5       www       713:     } elsif ($ENV{'form.action'} eq 'changescreenname') {
                    714:         &screennamechanger($r);
1.6       www       715:     } elsif ($ENV{'form.action'} eq 'verify_and_change_screenname') {
                    716:         &verify_and_change_screenname($r);
1.12      www       717:     } elsif ($ENV{'form.action'} eq 'changemsgforward') {
                    718:         &msgforwardchanger($r);
                    719:     } elsif ($ENV{'form.action'} eq 'verify_and_change_msgforward') {
                    720:         &verify_and_change_msgforward($r);
1.19      www       721:     } elsif ($ENV{'form.action'} eq 'changecolors') {
                    722:         &colorschanger($r);
                    723:     } elsif ($ENV{'form.action'} eq 'verify_and_change_colors') {
                    724:         &verify_and_change_colors($r);
1.28      www       725:     } elsif ($ENV{'form.action'} eq 'changelanguages') {
                    726:         &languagechanger($r);
                    727:     } elsif ($ENV{'form.action'} eq 'verify_and_change_languages') {
                    728:         &verify_and_change_languages($r);
1.15      albertel  729:     } elsif ($ENV{'form.action'} eq 'debugtoggle') {
1.24      www       730: 	if (($ENV{'user.name'} eq 'albertel' ) ||
                    731:             ($ENV{'user.name'} eq 'kortemey' ) ||
                    732:             ($ENV{'user.name'} eq 'korte')) {
1.15      albertel  733: 	    if ($ENV{'user.debug'}) {
                    734: 		&Apache::lonnet::delenv('user\.debug');
                    735: 	    } else {
                    736: 		&Apache::lonnet::appenv('user.debug' => 1);
                    737: 	    }
                    738: 	}
1.3       matthew   739:     } else {
                    740: 	$r->print(<<ENDHEADER);
1.1       www       741: <html>
                    742: <head>
1.4       matthew   743: <title>LON-CAPA Preferences</title>
1.1       www       744: </head>
1.3       matthew   745: ENDHEADER
1.10      www       746:         $r->print(&Apache::loncommon::bodytag('Change Your Preferences'));
1.3       matthew   747: 	# Determine current authentication method
                    748: 	my $currentauth=&Apache::lonnet::queryauthenticate($user,$domain);
                    749: 	if ($currentauth =~ /^(unix|internal):/) {
1.4       matthew   750: 	    $r->print(<<ENDPASSWORDFORM);
                    751: <form name="client" action="/adm/preferences" method="post">
1.14      www       752: <input type="hidden" name="action" value="changepass" />
                    753: <input type="submit" value="Change password" />
1.4       matthew   754: </form>
                    755: ENDPASSWORDFORM
1.13      www       756:         }
1.5       www       757: # Change screen name
                    758: 	    $r->print(<<ENDSCREENNAMEFORM);
                    759: <form name="client" action="/adm/preferences" method="post">
1.14      www       760: <input type="hidden" name="action" value="changescreenname" />
                    761: <input type="submit" 
                    762: value="Change nickname and anonymous discussion screen name" />
1.5       www       763: </form>
                    764: ENDSCREENNAMEFORM
1.12      www       765: 	    $r->print(<<ENDMSGFORWARDFORM);
                    766: <form name="client" action="/adm/preferences" method="post">
1.14      www       767: <input type="hidden" name="action" value="changemsgforward" />
1.18      www       768: <input type="submit" value="Change message forwarding and notification addresses" />
1.12      www       769: </form>
                    770: ENDMSGFORWARDFORM
1.11      www       771: # The "about me" page
1.15      albertel  772: 	my $aboutmeaction=
                    773: 	    '/adm/'.$ENV{'user.domain'}.'/'.$ENV{'user.name'}.'/aboutme';
                    774: 	$r->print(<<ENDABOUTME);
1.11      www       775: <form name="client" action="$aboutmeaction" method="post">
1.14      www       776: <input type="hidden" name="action" value="changescreenname" />
1.19      www       777: <input type="submit" value="Edit the 'About Me' personal information screen" />
1.11      www       778: </form>
                    779: ENDABOUTME
1.19      www       780: 	    $r->print(<<ENDCOLORFORM);
                    781: <form name="client" action="/adm/preferences" method="post">
                    782: <input type="hidden" name="action" value="changecolors" />
                    783: <input type="submit" value="Change color scheme" />
                    784: </form>
                    785: ENDCOLORFORM
1.28      www       786: 
                    787: 	    $r->print(<<ENDLANGUAGES);
                    788: <form name="client" action="/adm/preferences" method="post">
                    789: <input type="hidden" name="action" value="changelanguages" />
                    790: <input type="submit" value="Change language preferences" />
                    791: </form>
                    792: ENDLANGUAGES
1.19      www       793: 
1.24      www       794: 	if (($ENV{'user.name'} eq 'albertel' ) ||
                    795:             ($ENV{'user.name'} eq 'kortemey' ) ||
                    796:             ($ENV{'user.name'} eq 'korte')) {
1.15      albertel  797: 	    $r->print(<<ENDDEBUG);
                    798: <form name="client" action="/adm/preferences" method="post">
                    799: <input type="hidden" name="action" value="debugtoggle" />
                    800: <input type="submit" value="Toggle Debug" />
                    801: Current Debug status is -$ENV{'user.debug'}-.
                    802: </form>
                    803: ENDDEBUG
                    804: 	}
                    805: 	# Other preference setting code should be added here
1.3       matthew   806:     }
                    807:     $r->print(<<ENDFOOTER);
1.1       www       808: </body>
                    809: </html>
1.3       matthew   810: ENDFOOTER
1.1       www       811:     return OK;
1.13      www       812: }
1.1       www       813: 
                    814: 1;
                    815: __END__

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