--- loncom/publisher/londiff.pm	2001/05/03 11:04:07	1.4
+++ loncom/publisher/londiff.pm	2011/11/07 15:18:13	1.28.2.1
@@ -1,154 +1,269 @@
 # The LearningOnline Network with CAPA
 # Handler to show differences between file versions
 #
-# (Handler to retrieve an old version of a file
+# $Id: londiff.pm,v 1.28.2.1 2011/11/07 15:18:13 raeburn Exp $
 #
-# (Publication Handler
-# 
-# (TeX Content Handler
+# Copyright Michigan State University Board of Trustees
 #
-# 05/29/00,05/30,10/11 Gerd Kortemeyer)
+# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
 #
-# 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
-# 03/23 Guy Albertelli
-# 03/24,03/29 Gerd Kortemeyer)
+# LON-CAPA is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
 #
-# 03/31,04/03 Gerd Kortemeyer)
+# LON-CAPA is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
 #
-# 05/02/01 Gerd Kortemeyer
+# You should have received a copy of the GNU General Public License
+# along with LON-CAPA; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+#
+# /home/httpd/html/adm/gpl.txt
+#
+# http://www.lon-capa.org/
+#
+###
+
+
+
 
 package Apache::londiff;
 
 use strict;
 use Apache::File;
 use File::Copy;
+use File::Compare;
 use Algorithm::Diff qw(diff);
 use Apache::Constants qw(:common :http :methods);
+use Apache::loncacc();
+use Apache::lonnet;
+use Apache::loncommon();
+use Apache::lonretrieve();
+use Apache::lonlocal;
+use LONCAPA();
+
+sub get_split_file {
+    my ($fn,$style)=@_;
+    my $f1;
+    my @f1;
+    if ($style eq 'local') {
+	if (-e $fn) {
+	    my $fh=Apache::File->new($fn);
+	    my $line;	
+	    while($line=<$fh>) {
+		$f1.=$line;
+	    }
+	}
+    } elsif ($style eq 'remote') {
+	$f1=&Apache::lonnet::getfile($fn);
+    }
+    @f1=split(/\r\n|\r|\n/,$f1);
+    return @f1;
+}
 
+sub are_different_files {
+    my ($fileone,$filetwo)=@_;
+    return &compare($fileone,$filetwo);
+}
 
 sub handler {
 
-  my $r=shift;
-
+    my $r=shift;
 # Get query string for limited number of parameters
 
-    map {
-       my ($name, $value) = split(/=/,$_);
-       $value =~ tr/+/ /;
-       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
-       if (($name eq 'filename') || ($name eq 'versiontwo') || 
-           ($name eq 'versionone')) {
-           unless ($ENV{'form.'.$name}) {
-              $ENV{'form.'.$name}=$value;
-	   }
-       }
-    } (split(/&/,$ENV{'QUERY_STRING'}));
+    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
+					    ['filename','versiontwo',
+					     'versionone','filetwo']);
+
+# Check permissions
+    my $allowed=0;
+    my $cuname=$env{'user.name'};
+    my $cudom=$env{'user.domain'};
+
+    if ($env{'form.filename'}=~ m{^/res/}) {
+        if (&Apache::lonnet::allowed('bre',$env{'form.filename'})) {
+            if ($env{'request.course.id'}) {
+                if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
+                    $allowed = 1;
+                }
+            } else {
+                $allowed = 1;
+            }
+        } elsif (&Apache::lonnet::allowed('bro',$env{'form.filename'})) {
+            $allowed = 1;
+        }
+        if ($allowed) {
+            ($cudom,$cuname,$env{'form.filename'})=
+                ($env{'form.filename'}=~m{^/res/($LONCAPA::domain_re)/($LONCAPA::username_re)(/.*)$});
+
+            if (($env{'form.versionone'} eq 'priv') || ($env{'form.versiontwo'} eq 'priv')) {
+                my ($cstrname,$cstrdom) =
+                    &Apache::loncacc::constructaccess("/priv/$cuname".$env{'form.filename'},$cudom);
+                unless (($cstrname eq $cuname) && ($cstrdom eq $cudom)) {
+                    $allowed = 0;
+                }
+            }
+        }
+    } else {
+        ($cuname,$cudom)=
+            &Apache::loncacc::constructaccess($env{'form.filename'},
+                                              $r->dir_config('lonDefDomain'));
+        if ($cuname ne '' && $cudom ne '') {
+            $allowed = 1;
+        } else {
+            $r->log_reason($env{'user.name'}.':'.$env{'user.domain'}.
+                           ' trying to get diffs file '.$env{'form.filename'}.
+                           '  - not authorized',
+                           $r->filename);
+        }
+    }
+    unless ($allowed) {
+        return HTTP_NOT_ACCEPTABLE;
+    }
 
 # Get the files
 
-  my @f1=();
-  my @f2=();
+    my $efn=$env{'form.filename'};
 
-  $r->content_type('text/html');
-  $r->send_http_header;
+    $efn=~s{/\~($LONCAPA::username_re)}{}g;
 
-  $r->print('<html><head><title>LON-CAPA Construction Diffs</title></head>');
+    my @f1=();
+    my @f2=();
 
-  $r->print('<body bgcolor="#FFFFFF">');
+    &Apache::loncommon::content_type($r,'text/html');
+    $r->send_http_header;
 
+    $r->print(&Apache::loncommon::start_page('Resource Differences',undef,
+                                             {'no_nav_bar'  => 1, }));
   
-  $r->print('<h1>Compare versions of <tt>'.$ENV{'form.filename'}.'</tt></h1>');
+    $r->print(($env{'form.filetwo'}?'':&mt('Compare versions of')).
+	      ' <span class="LC_filename">'.$efn.'</span>');
+   
+    if (($cuname ne $env{'user.name'}) || ($cudom ne $env{'user.domain'})) {
+        $r->print('<p><span class="LC_info">'
+                 .&mt('Co-Author [_1]'
+                     ,&Apache::loncommon::plainname($cuname,$cudom)
+                     .' ('.$cuname.':'.$cudom.')')
+                .'</span></p>'
+        );
+    }
 
- if (&Apache::lonnet::fileembstyle(($ENV{'form.filename'}=~/\.(\w+)$/)) eq
-      'ssi') {
-  if ($ENV{'form.versionone'} eq 'priv') {
-      my $fn='/home/'.$ENV{'user.name'}.'/public_html/'.$ENV{'form.filename'};
-      if (-e $fn) {
-	  my $fh=Apache::File->new($fn);
-          my $line;
-          while($line=<$fh>) {
-             chomp($line);
-             $f1[$#f1+1]=$line;
-	 }
-      }
-      $r->print('<h3>Construction Space Version</h3>');
-  } else {
-      my $fn=
-       '/home/httpd/html//res/'.$ENV{'user.domain'}.'/'.$ENV{'user.name'}.'/';
-      if ($ENV{'form.versionone'}) {
-         my ($main,$suffix)=($ENV{'form.filename'}=~/^(.+)\.(\w+)$/);
-         $fn.=$main.'.'.$ENV{'form.versionone'}.'.'.$suffix;
-	 $r->print('<h3>Version '.$ENV{'form.versionone'}.'</h3>');
-      } else {
-         $fn.=$ENV{'form.filename'};
-	 $r->print('<h3>Current Version</h3>');
-      }
-      @f1=split(/\n/,&Apache::lonnet::getfile($fn));      
-  }
-
-  $r->print('versus');
-
-  if ($ENV{'form.versiontwo'} eq 'priv') {
-      my $fn='/home/'.$ENV{'user.name'}.'/public_html/'.$ENV{'form.filename'};
-      if (-e $fn) {
-	  my $fh=Apache::File->new($fn);
-          my $line;
-          while($line=<$fh>) {
-             chomp($line);
-             $f2[$#f2+1]=$line;
-	 }
-      }
-      $r->print('<h3>Construction Space Version</h3>');
-  } else {
-      my $fn=
-       '/home/httpd/html/res/'.$ENV{'user.domain'}.'/'.$ENV{'user.name'}.'/';
-      if ($ENV{'form.versiontwo'}) {
-         my ($main,$suffix)=($ENV{'form.filename'}=~/^(.+)\.(\w+)$/);
-         $fn.=$main.'.'.$ENV{'form.versiontwo'}.'.'.$suffix;
-	 $r->print('<h3>Version '.$ENV{'form.versiontwo'}.'</h3>');
-      } else {
-         $fn.=$ENV{'form.filename'};
-	 $r->print('<h3>Current Version</h3>');
-      }
-      @f2=split(/\n/,&Apache::lonnet::getfile($fn));      
-  }
 
+    if (&Apache::loncommon::fileembstyle(($efn=~/\.(\w+)$/)) eq 'ssi'
+	|| $efn =~ /\.meta$/) {
+	$r->print('<p><span class="LC_diff_removed">');
+	if ($env{'form.versionone'} eq 'priv') {
+	    my $fn='/home/'.$cuname.'/public_html/'.$efn;
+	    @f1=&get_split_file($fn,'local');
+	    $r->print('<b>'.&mt('Construction Space Version').'</b>');
+	} else {
+	    my $fn=
+		'/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
+	    if ($env{'form.versionone'}) {
+		my ($main,$suffix,$is_meta)=
+		    &Apache::lonretrieve::get_file_info($efn);
+		
+		$fn.=($efn =~m|(.*/)[^/]+|)[0];
+		# add on to $fn the path information in $efn
+		$fn.=$main.'.'.$env{'form.versionone'}.'.'.$suffix;
+		$r->print('<b>'.&mt('Version').' '.$env{'form.versionone'}.'</b>');
+	    } else {
+		$fn.=$efn;
+		$r->print('<b>'.&mt('Current Version').'</b>');
+	    }
+	    @f1=&get_split_file($fn,'remote');
+	}
+	
+	$r->print('</span><br />'.&mt('versus').'<br /><span class="LC_diff_added">');
+
+	if ($env{'form.filetwo'}) {
+	    my $efn2=$env{'form.filetwo'};
+	    $efn2=~s{/\~($LONCAPA::username_re)}{}g;
+	    my $fn='/home/'.$cuname.'/public_html/'.$efn2;
+	    @f2=&get_split_file($fn,'local');
+	    $r->print('<tt>'.$efn2.'</tt>');
+	} elsif ($env{'form.versiontwo'} eq 'priv') {
+	    my $fn='/home/'.$cuname.'/public_html/'.$efn;
+	    @f2=&get_split_file($fn,'local');
+	    $r->print('<b>'.&mt('Construction Space Version').'</b>');
+	} else {
+	    my $fn=
+		'/home/httpd/html/res/'.$cudom.'/'.$cuname.'/';
+	    if ($env{'form.versiontwo'}) {
+		my ($main,$suffix,$is_meta)=
+		    &Apache::lonretrieve::get_file_info($efn);
+		# add on to $fn the path information in $efn
+		$fn.=($efn =~m|(.*/)[^/]+|)[0];
+		$fn.=$main.'.'.$env{'form.versiontwo'}.'.'.$suffix;
+		$r->print('<b>'.&mt('Version').' '.$env{'form.versiontwo'}.'</b>');
+	    } else {
+		$fn.=$efn;
+		$r->print('<b>'.&mt('Current Version').'</b>');
+	    }
+	    @f2=&get_split_file($fn,'remote');
+	}
+	$r->print('</span></p>');
 # Run diff
 
-  my $diffs = diff(\@f1, \@f2);
+	my $diffs = diff(\@f1, \@f2);
 
-# Start page output
+        if (@$diffs) {
+            # Start page output
+            my $chunk;
+            my $line;
+            $r->print('<pre>');
+            foreach $chunk (@$diffs) {
+                foreach $line (@$chunk) {
+                    my ($sign, $lineno, $text) = @$line;
+                    $text=&HTML::Entities::encode($text,'<>&"');
+                    $lineno=substr($lineno.'        ',0,7);
+                    $r->print('<span class="'.(($sign eq '+')?'LC_diff_added'
+                                                             :'LC_diff_removed').'">'.
+                              $sign.' '.$lineno.' '.$text."</span>\n");
+                }
+                $r->print("</pre><hr /><pre>\n");
+            }
+            $r->print('</pre>');
+        } else {
+            $r->print('<p class="LC_info">'.&mt('No differences found').'</p>');
+        }
+    } else {
+	$r->print('<h1><span class="LC_warning">'.&mt('Binary File').'</span></h1>');
+    }
+    $r->print('<center><a href="javascript:window.close();">'.&mt('Close This Window').'</a></center>');
+    $r->print(&Apache::loncommon::end_page()); 
+    return OK;  
+}
 
-  my $chunk;
-  my $line;
 
-  $r->print('<pre>');
+1;
+__END__
 
-  foreach $chunk (@$diffs) {
 
-    foreach $line (@$chunk) {
-      my ($sign, $lineno, $text) = @$line;
-      $text=~s/\</\&lt\;/g;
-      $text=~s/\>/\&gt\;/g;
-      $lineno=substr($lineno.'        ',0,7);
-      $r->print('<font color='.(($sign eq '+')?'green':'red').'>'.
-                $sign.' '.$lineno.' '.$text."</font>\n");
-    }
-    $r->print("<hr>\n");
-  }
-  $r->print('</pre>');
+=pod
 
-} else {
-    $r->print('<h1><font color=red>Binary File</font></h1>');
-}
-  $r->print('</body></html>');
-  return OK;  
-}
+=head1 NAME
 
+Apache::londiff
 
-1;
-__END__
+=head1 SYNOPSIS
+
+Handler to show difference between two files.
+
+This is part of the LearningOnline Network with CAPA project
+described at http://www.lon-capa.org.
+
+=head1 Subroutines
+
+=over
 
+=item get_split_file()
 
+=item are_different_files()
 
+=item handler()
 
+=cut