Annotation of loncom/auth/restrictedaccess.pm, revision 1.5

1.1       raeburn     1: # The LearningOnline Network
                      2: # Passphrase Entry and Validation for Portfolio files 
                      3: #
                      4: # Copyright Michigan State University Board of Trustees
                      5: #
                      6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      7: #
                      8: # LON-CAPA is free software; you can redistribute it and/or modify
                      9: # it under the terms of the GNU General Public License as published by
                     10: # the Free Software Foundation; either version 2 of the License, or
                     11: # (at your option) any later version.
                     12: #
                     13: # LON-CAPA is distributed in the hope that it will be useful,
                     14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     16: # GNU General Public License for more details.
                     17: #
                     18: # You should have received a copy of the GNU General Public License
                     19: # along with LON-CAPA; if not, write to the Free Software
                     20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     21: #
                     22: # /home/httpd/html/adm/gpl.txt
                     23: #
                     24: # http://www.lon-capa.org/
                     25: #
                     26: 
                     27: package Apache::restrictedaccess;
                     28: 
                     29: use strict;
                     30: use lib '/home/httpd/lib/perl/';
                     31: use Apache::Constants qw(:common :http REDIRECT);
                     32: use CGI::Cookie();
                     33: use Apache::lonnet;
                     34: use Apache::loncommon();
                     35: use Apache::lonauth();
                     36: use Apache::lonlocal;
                     37: use Apache::lonacc;
                     38: use Fcntl qw(:flock);
                     39: use LONCAPA;
                     40: 
                     41: sub handler {
                     42:     my $r = shift;
                     43: 
                     44:     my $origurl = &unescape($env{'form.origurl'});
1.2       albertel   45:     if (!defined($origurl)) {
                     46: 	$origurl = $r->uri;
                     47:     }
1.5     ! albertel   48:     my $msg='';
1.1       raeburn    49:     if (exists($env{'form.pass1'})) {
                     50:         my ($result,$end) = &check_pass($r,$origurl);
                     51:         if ($result eq 'ok') {
1.5     ! albertel   52: 	    &Apache::lonnet::allowuploaded('/adm/restrictedaccess',
        !            53: 					   $origurl);
1.2       albertel   54: 	    $env{'request.state'} = "published";
                     55: 	    $env{'request.filename'} = $origurl;
                     56: 	    $r->header_out(Location => 'http://'.$ENV{'HTTP_HOST'}.$origurl);
                     57: 	    return REDIRECT;
1.1       raeburn    58:         } else {
1.5     ! albertel   59: 	    $msg = 'Invalid passphrase';
        !            60: 	}
1.1       raeburn    61:     }
1.5     ! albertel   62: 
        !            63:     &Apache::loncommon::content_type($r,'text/html');
        !            64:     $r->send_http_header;
        !            65:     return OK if $r->header_only;
        !            66: 
        !            67:     $r->print(&Apache::loncommon::start_page('Passphrase protected file'));
        !            68:     &print_entryform($r,$origurl,$msg);
        !            69: 
1.1       raeburn    70:     return OK;
                     71: }
                     72: 
1.5     ! albertel   73: sub setup_handler {
        !            74:     my ($r) = @_;
        !            75:     $r->set_handlers('PerlHandler'=> 
        !            76: 		     \&Apache::restrictedaccess::handler);
        !            77:     $r->content_type('perl-script');		    
        !            78: }
        !            79: 
1.1       raeburn    80: sub print_entryform {
                     81:     my ($r,$origurl,$msg) = @_;
                     82: 
                     83:     $r->print('<script type="text/javascript">
                     84: function verify() {
                     85:     if (document.passform.pass1.value == "") {
                     86:         alert("You must enter a passphrase");
                     87:         return;
                     88:     }
                     89:     document.passform.submit();
                     90: } 
                     91: </script>');
1.5     ! albertel   92:     if ($msg ne '') {
        !            93: 	$r->print('<span class="LC_error">'.$msg.'</span>');
        !            94:     }
1.1       raeburn    95:     $r->print('<div align="center"><form name="passform" method="post" '.
                     96:               'action="/adm/restrictedaccess">');
                     97:     $r->print('<br /><br /><br />');
                     98:     $r->print(&Apache::loncommon::start_data_table());
                     99:     $r->print(&Apache::loncommon::start_data_table_row());     
                    100:     $r->print('<td><nobr>'.&mt('Passphrase: ').'</nobr></td>'.
1.5     ! albertel  101:               '<td><input type="password" size="20" name="pass1" /></td>');
1.1       raeburn   102:     $r->print(&Apache::loncommon::end_data_table_row());
                    103:     $r->print(&Apache::loncommon::start_data_table_row());
                    104:     $r->print('<td align="center" colspan="2"><br />'.
                    105:               '<input type="button" name="sendpass" value="'.
                    106:               &mt('Submit passphrase').'" onClick="verify()" /></td>');
                    107:     $r->print(&Apache::loncommon::end_data_table_row());
                    108:     $r->print(&Apache::loncommon::end_data_table());
                    109:     $r->print('<input type="hidden" name="origurl" value="'.
                    110:               &escape($origurl).'" /></form></div>');
                    111:     $r->print(&Apache::loncommon::end_page());
                    112: }
                    113: 
                    114: sub check_pass {
                    115:     my ($r,$origurl) = @_;
1.3       albertel  116:     my (undef,$udom,$unum,$file_name,$group) = 
1.4       albertel  117: 	&Apache::lonnet::parse_portfolio_url($origurl);
1.3       albertel  118: 
1.1       raeburn   119:     my $curr_perms = &Apache::lonnet::get_portfile_permissions($udom,$unum);
                    120:     my %acc_controls = &Apache::lonnet::get_access_controls($curr_perms,
                    121:                                                             $group,$file_name);
                    122:     my $access_hash = $acc_controls{$file_name};
1.3       albertel  123: 
                    124:     my ($result,$end);
1.1       raeburn   125:     foreach my $key (sort(keys(%{$access_hash}))) {
                    126:         if ($key =~ /^[^:]+:guest_(\d+)/) {
                    127:             $end = $1;
1.2       albertel  128:             if ($env{'form.pass1'} eq $access_hash->{$key}{'password'}) {
1.1       raeburn   129:                 $result = 'ok';
                    130:             } else {
                    131:                 $result = 'fail';
                    132:             }
                    133:             last;
                    134:         }
                    135:     }
                    136:     return ($result,$end);
                    137: }
                    138: 
                    139: 1;

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