File:  [LON-CAPA] / loncom / publisher / londiff.pm
Revision 1.31: download - view: text, annotated - select for diffs
Sun Oct 30 00:30:19 2011 UTC (12 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- permissions checking.

    1: # The LearningOnline Network with CAPA
    2: # Handler to show differences between file versions
    3: #
    4: # $Id: londiff.pm,v 1.31 2011/10/30 00:30:19 raeburn Exp $
    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: #
   28: ###
   29: 
   30: 
   31: 
   32: 
   33: package Apache::londiff;
   34: 
   35: use strict;
   36: use Apache::File;
   37: use File::Copy;
   38: use File::Compare;
   39: use Algorithm::Diff qw(diff);
   40: use Apache::Constants qw(:common :http :methods);
   41: use Apache::loncacc();
   42: use Apache::lonnet;
   43: use Apache::loncommon();
   44: use Apache::lonretrieve();
   45: use Apache::lonlocal;
   46: use LONCAPA();
   47: 
   48: sub get_split_file {
   49:     my ($fn,$style)=@_;
   50:     my $f1;
   51:     my @f1;
   52:     if ($style eq 'local') {
   53: 	if (-e $fn) {
   54: 	    my $fh=Apache::File->new($fn);
   55: 	    my $line;	
   56: 	    while($line=<$fh>) {
   57: 		$f1.=$line;
   58: 	    }
   59: 	}
   60:     } elsif ($style eq 'remote') {
   61: 	$f1=&Apache::lonnet::getfile($fn);
   62:     }
   63:     @f1=split(/\r\n|\r|\n/,$f1);
   64:     return @f1;
   65: }
   66: 
   67: sub are_different_files {
   68:     my ($fileone,$filetwo)=@_;
   69:     return &compare($fileone,$filetwo);
   70: }
   71: 
   72: sub handler {
   73: 
   74:     my $r=shift;
   75: # Get query string for limited number of parameters
   76: 
   77:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   78: 					    ['filename','versiontwo',
   79: 					     'versionone','filetwo']);
   80: 
   81: # Check permissions
   82:     my $allowed=0;
   83:     my $cuname=$env{'user.name'};
   84:     my $cudom=$env{'user.domain'};
   85: 
   86:     if ($env{'form.filename'}=~/^\/res\//) {
   87:         if (&Apache::lonnet::allowed('bre',$env{'form.filename'})) {
   88:             if ($env{'request.course.id'}) {
   89:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
   90:                     $allowed = 1;
   91:                 }
   92:             }
   93:         } elsif (&Apache::lonnet::allowed('bro',$env{'form.filename'})) {
   94:             $allowed = 1; 
   95:         }
   96:         if ($allowed) {
   97:             ($cudom,$cuname,$env{'form.filename'})=
   98:                 ($env{'form.filename'}=~m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)(/.*)$});        
   99:         }
  100:     } else {
  101: 	($cuname,$cudom)=
  102: 	    &Apache::loncacc::constructaccess($env{'form.filename'},
  103: 					      $r->dir_config('lonDefDomain'));
  104:         if ($cuname ne '' && $cudom ne '') {
  105:             $allowed = 1;
  106:         } else {
  107: 	    $r->log_reason($env{'user.name'}.':'.$env{'user.domain'}.
  108: 			   ' trying to get diffs file '.$env{'form.filename'}.
  109: 			   '  - not authorized', 
  110: 			   $r->filename); 
  111: 	}
  112:     }
  113:     unless ($allowed) {
  114:         return HTTP_NOT_ACCEPTABLE;
  115:     }
  116: 
  117: # Get the files
  118:   
  119:     my $efn=$env{'form.filename'};
  120: 
  121:     $efn=~s{^/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
  122: 
  123:     my @f1=();
  124:     my @f2=();
  125: 
  126:     &Apache::loncommon::content_type($r,'text/html');
  127:     $r->send_http_header;
  128: 
  129:     $r->print(&Apache::loncommon::start_page('Resource Differences',undef,
  130:                                              {'no_nav_bar'  => 1, }));
  131:   
  132:     $r->print(($env{'form.filetwo'}?'':&mt('Compare versions of')).
  133: 	      ' <span class="LC_filename">'.$efn.'</span>');
  134:    
  135:     if (($cuname ne $env{'user.name'}) || ($cudom ne $env{'user.domain'})) {
  136:         $r->print('<p><span class="LC_info">'
  137:                  .&mt('Co-Author [_1]'
  138:                      ,&Apache::loncommon::plainname($cuname,$cudom)
  139:                      .' ('.$cuname.':'.$cudom.')')
  140:                 .'</span></p>'
  141:         );
  142:     }
  143: 
  144: 
  145:     if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq 'ssi'
  146: 	|| $efn =~ /\.meta$/) {
  147: 	$r->print('<p><span class="LC_diff_removed">');
  148: 	if ($env{'form.versionone'} eq 'priv') {
  149: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn;
  150: 	    @f1=&get_split_file($fn,'local');
  151: 	    $r->print('<b>'.&mt('Construction Space Version').'</b>');
  152: 	} else {
  153: 	    my $fn=$r->dir_config('lonDocRoot')."/res/$cudom/$cuname";
  154: 	    if ($env{'form.versionone'}) {
  155: 		my ($main,$suffix,$is_meta)=
  156: 		    &Apache::lonretrieve::get_file_info($efn);
  157: 		
  158: 		$fn.=($efn =~m|(.*/)[^/]+|)[0];
  159: 		# add on to $fn the path information in $efn
  160: 		$fn.=$main.'.'.$env{'form.versionone'}.'.'.$suffix;
  161: 		$r->print('<b>'.&mt('Version').' '.$env{'form.versionone'}.'</b>');
  162: 	    } else {
  163: 		$fn.=$efn;
  164: 		$r->print('<b>'.&mt('Current Version').'</b>');
  165: 	    }
  166: 	    @f1=&get_split_file($fn,'remote');
  167: 	}
  168: 	
  169: 	$r->print('</span><br />'.&mt('versus').'<br /><span class="LC_diff_added">');
  170: 
  171: 	if ($env{'form.filetwo'}) {
  172: 	    my $efn2=$env{'form.filetwo'};
  173: 	    $efn2=~s{^/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
  174: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn2;
  175: 	    @f2=&get_split_file($fn,'local');
  176: 	    $r->print('<tt>'.$efn2.'</tt>');
  177: 	} elsif ($env{'form.versiontwo'} eq 'priv') {
  178: 	    my $fn=$r->dir_config('lonDocRoot')."/priv/$cudom/$cuname".$efn;
  179: 	    @f2=&get_split_file($fn,'local');
  180: 	    $r->print('<b>'.&mt('Construction Space Version').'</b>');
  181: 	} else {
  182: 	    my $fn=
  183: 		'/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
  184: 	    if ($env{'form.versiontwo'}) {
  185: 		my ($main,$suffix,$is_meta)=
  186: 		    &Apache::lonretrieve::get_file_info($efn);
  187: 		# add on to $fn the path information in $efn
  188: 		$fn.=($efn =~m|(.*/)[^/]+|)[0];
  189: 		$fn.=$main.'.'.$env{'form.versiontwo'}.'.'.$suffix;
  190: 		$r->print('<b>'.&mt('Version').' '.$env{'form.versiontwo'}.'</b>');
  191: 	    } else {
  192: 		$fn.=$efn;
  193: 		$r->print('<b>'.&mt('Current Version').'</b>');
  194: 	    }
  195: 	    @f2=&get_split_file($fn,'remote');
  196: 	}
  197: 	$r->print('</span></p>');
  198: # Run diff
  199: 
  200: 	my $diffs = diff(\@f1, \@f2);
  201: 
  202:         if (@$diffs) {
  203:             # Start page output
  204:             my $chunk;
  205:             my $line;
  206:             $r->print('<pre>');
  207:             foreach $chunk (@$diffs) {
  208:                 foreach $line (@$chunk) {
  209:                     my ($sign, $lineno, $text) = @$line;
  210:                     $text=&HTML::Entities::encode($text,'<>&"');
  211:                     $lineno=substr($lineno.'        ',0,7);
  212:                     $r->print('<span class="'.(($sign eq '+')?'LC_diff_added'
  213:                                                              :'LC_diff_removed').'">'.
  214:                               $sign.' '.$lineno.' '.$text."</span>\n");
  215:                 }
  216:                 $r->print("</pre><hr /><pre>\n");
  217:             }
  218:             $r->print('</pre>');
  219:         } else {
  220:             $r->print('<p class="LC_info">'.&mt('No differences found').'</p>');
  221:         }
  222:     } else {
  223: 	$r->print('<h1><span class="LC_warning">'.&mt('Binary File').'</span></h1>');
  224:     }
  225:     $r->print('<center><a href="javascript:window.close();">'.&mt('Close This Window').'</a></center>');
  226:     $r->print(&Apache::loncommon::end_page()); 
  227:     return OK;  
  228: }
  229: 
  230: 
  231: 1;
  232: __END__
  233: 
  234: 
  235: =pod
  236: 
  237: =head1 NAME
  238: 
  239: Apache::londiff
  240: 
  241: =head1 SYNOPSIS
  242: 
  243: Handler to show difference between two files.
  244: 
  245: This is part of the LearningOnline Network with CAPA project
  246: described at http://www.lon-capa.org.
  247: 
  248: =head1 Subroutines
  249: 
  250: =over
  251: 
  252: =item get_split_file()
  253: 
  254: =item are_different_files()
  255: 
  256: =item handler()
  257: 
  258: =cut

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