Annotation of loncom/publisher/londiff.pm, revision 1.28.2.1

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

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