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

1.1       raeburn     1: # The LearningOnline Network
                      2: # Passphrase Entry and Validation for Portfolio files 
                      3: #
1.10    ! bisitz      4: # $Id: restrictedaccess.pm,v 1.9 2013/06/02 02:20:33 raeburn Exp $
1.8       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: 
                     29: package Apache::restrictedaccess;
                     30: 
                     31: use strict;
                     32: use lib '/home/httpd/lib/perl/';
                     33: use Apache::Constants qw(:common :http REDIRECT);
                     34: use CGI::Cookie();
                     35: use Apache::lonnet;
                     36: use Apache::loncommon();
                     37: use Apache::lonauth();
                     38: use Apache::lonlocal;
                     39: use Apache::lonacc;
                     40: use Fcntl qw(:flock);
                     41: use LONCAPA;
                     42: 
                     43: sub handler {
                     44:     my $r = shift;
                     45: 
                     46:     my $origurl = &unescape($env{'form.origurl'});
1.2       albertel   47:     if (!defined($origurl)) {
                     48: 	$origurl = $r->uri;
                     49:     }
1.5       albertel   50:     my $msg='';
1.1       raeburn    51:     if (exists($env{'form.pass1'})) {
                     52:         my ($result,$end) = &check_pass($r,$origurl);
                     53:         if ($result eq 'ok') {
1.5       albertel   54: 	    &Apache::lonnet::allowuploaded('/adm/restrictedaccess',
                     55: 					   $origurl);
1.2       albertel   56: 	    $env{'request.state'} = "published";
                     57: 	    $env{'request.filename'} = $origurl;
1.9       raeburn    58:             my $newurl = &Apache::lonnet::absolute_url($ENV{'HTTP_HOST'}).$origurl;
                     59: 	    $r->header_out(Location => $newurl);
1.2       albertel   60: 	    return REDIRECT;
1.1       raeburn    61:         } else {
1.5       albertel   62: 	    $msg = 'Invalid passphrase';
                     63: 	}
1.1       raeburn    64:     }
1.5       albertel   65: 
                     66:     &Apache::loncommon::content_type($r,'text/html');
                     67:     $r->send_http_header;
                     68:     return OK if $r->header_only;
                     69: 
                     70:     $r->print(&Apache::loncommon::start_page('Passphrase protected file'));
                     71:     &print_entryform($r,$origurl,$msg);
                     72: 
1.1       raeburn    73:     return OK;
                     74: }
                     75: 
1.5       albertel   76: sub setup_handler {
                     77:     my ($r) = @_;
                     78:     $r->set_handlers('PerlHandler'=> 
1.6       raeburn    79: 		     [\&Apache::restrictedaccess::handler]);
                     80:     $r->handler('perl-script');		    
1.5       albertel   81: }
                     82: 
1.1       raeburn    83: sub print_entryform {
                     84:     my ($r,$origurl,$msg) = @_;
                     85: 
                     86:     $r->print('<script type="text/javascript">
                     87: function verify() {
                     88:     if (document.passform.pass1.value == "") {
                     89:         alert("You must enter a passphrase");
                     90:         return;
                     91:     }
                     92:     document.passform.submit();
                     93: } 
                     94: </script>');
1.5       albertel   95:     if ($msg ne '') {
                     96: 	$r->print('<span class="LC_error">'.$msg.'</span>');
                     97:     }
1.1       raeburn    98:     $r->print('<div align="center"><form name="passform" method="post" '.
                     99:               'action="/adm/restrictedaccess">');
                    100:     $r->print('<br /><br /><br />');
                    101:     $r->print(&Apache::loncommon::start_data_table());
                    102:     $r->print(&Apache::loncommon::start_data_table_row());     
1.7       bisitz    103:     $r->print('<td><span class="LC_nobreak">'.&mt('Passphrase: ').'</span></td>'.
1.5       albertel  104:               '<td><input type="password" size="20" name="pass1" /></td>');
1.1       raeburn   105:     $r->print(&Apache::loncommon::end_data_table_row());
                    106:     $r->print(&Apache::loncommon::start_data_table_row());
                    107:     $r->print('<td align="center" colspan="2"><br />'.
                    108:               '<input type="button" name="sendpass" value="'.
1.10    ! bisitz    109:               &mt('Submit passphrase').'" onclick="verify()" /></td>');
1.1       raeburn   110:     $r->print(&Apache::loncommon::end_data_table_row());
                    111:     $r->print(&Apache::loncommon::end_data_table());
                    112:     $r->print('<input type="hidden" name="origurl" value="'.
                    113:               &escape($origurl).'" /></form></div>');
                    114:     $r->print(&Apache::loncommon::end_page());
                    115: }
                    116: 
                    117: sub check_pass {
                    118:     my ($r,$origurl) = @_;
1.3       albertel  119:     my (undef,$udom,$unum,$file_name,$group) = 
1.4       albertel  120: 	&Apache::lonnet::parse_portfolio_url($origurl);
1.3       albertel  121: 
1.1       raeburn   122:     my $curr_perms = &Apache::lonnet::get_portfile_permissions($udom,$unum);
                    123:     my %acc_controls = &Apache::lonnet::get_access_controls($curr_perms,
                    124:                                                             $group,$file_name);
                    125:     my $access_hash = $acc_controls{$file_name};
1.3       albertel  126: 
                    127:     my ($result,$end);
1.1       raeburn   128:     foreach my $key (sort(keys(%{$access_hash}))) {
                    129:         if ($key =~ /^[^:]+:guest_(\d+)/) {
                    130:             $end = $1;
1.2       albertel  131:             if ($env{'form.pass1'} eq $access_hash->{$key}{'password'}) {
1.1       raeburn   132:                 $result = 'ok';
                    133:             } else {
                    134:                 $result = 'fail';
                    135:             }
                    136:             last;
                    137:         }
                    138:     }
                    139:     return ($result,$end);
                    140: }
                    141: 
                    142: 1;

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