File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.26: download - view: text, annotated - select for diffs
Wed Jan 5 17:38:27 2005 UTC (19 years, 5 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #1356: less odd display.

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.26 2005/01/05 17:38:27 www 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: package Apache::lonretrieve;
   32: 
   33: use strict;
   34: use Apache::File;
   35: use File::Copy;
   36: use Apache::Constants qw(:common :http :methods);
   37: use Apache::loncacc;
   38: use Apache::loncommon();
   39: use Apache::lonlocal;
   40: 
   41: # ------------------------------------ Interface for selecting previous version
   42: sub phaseone {
   43:     my ($r,$fn,$uname,$udom)=@_;
   44:     my $docroot=$r->dir_config('lonDocRoot');
   45: 
   46:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   47:     $urldir=~s/\/[^\/]+$/\//;
   48: 
   49:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   50:     my $resdir=$resfn;
   51:     $resdir=~s/\/[^\/]+$/\//;
   52: 
   53:     $fn=~/\/([^\/]+)\.(\w+)$/;
   54:     my $main=$1;
   55:     my $suffix=$2;
   56: 
   57:     if (-e $resfn) {  
   58:     $r->print('<form action=/adm/retrieve method=post>'.
   59: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
   60:               '<input type=hidden name=phase value=two>'.
   61:               '<table border=2><tr><th>'.&mt('Select').'</th><th>'.
   62: 	      &mt('Version').'</th>'.
   63:               '<th>'.&mt('Published on ...').'</th>'.
   64:               '<th>'.&mt('Metadata').'</th></tr>');
   65:     my $filename;
   66:     opendir(DIR,$resdir);
   67:     while ($filename=readdir(DIR)) {
   68:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
   69: 	   my $version=$1;
   70:            my $rmtime=&Apache::lonnet::metadata($resdir.'/'.$filename,'lastrevisiondate');
   71:            $r->print('<tr><td><input type=radio name=version value="'.
   72:                      $version.'"></td><td>'.&mt('Previously published version').' '.$version.'</td><td>'.
   73:                      localtime($rmtime).'</td><td>'.
   74:                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.
   75:                      &mt('Metadata Version').' '.$version.'</a>');
   76:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
   77:                $r->print(
   78:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
   79:                         $uname.$fn.
   80:                         '&versiontwo=priv&versionone='.$version.
   81:                         '">'.&mt('Diffs with Version').' '.$version.'</a>');
   82:            }
   83:            $r->print('</a></td></tr>');
   84:         }
   85:     }
   86:     closedir(DIR);
   87:     my $rmtime=&Apache::lonnet::metadata($resfn,'lastrevisiondate');
   88:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
   89:               '<th>'.&mt('Currently public version').'</th><td>'.localtime($rmtime).
   90:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
   91:               &mt('Metadata current version').'</a>');           
   92:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
   93:                $r->print(
   94:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
   95:                         $uname.$fn.
   96:                         '&versiontwo=priv'.
   97:                         '">'.&mt('Diffs with current Version').'</a>');
   98:            }
   99:            $r->print('</td></tr></table><p>'.
  100:            '<font size=+1 color=red>'.
  101: &mt('Retrieval of an old version will overwrite the file currently in construction space').'</font><p>'.
  102:            '<input type=submit value="'.&mt('Retrieve version').'"></form>');
  103: } else {
  104:     $r->print('<h3>'.&mt('No previous versions published.').'</h3>');
  105: }
  106:     $r->print('<p><a href="/priv/'.$uname.$fn.'">'.&mt('Back to').' '.$fn.
  107: 	      '</a></p>'); 
  108: }
  109: 
  110: # ---------------------------------- Interface for presenting specified version
  111: sub phasetwo {
  112:     my ($r,$fn,$uname,$udom)=@_;
  113:     if ($ENV{'form.version'}) {
  114:         my $version=$ENV{'form.version'};
  115: 	if ($version eq 'new') {
  116: 	    $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
  117:         } else {
  118:             $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
  119:         }
  120:         my $logfile;
  121:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  122:         my $vfn=$fn;
  123:         if ($version ne 'new') {
  124: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
  125:         }
  126:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  127:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
  128: 	  $r->print(
  129:          '<font color=red>'.&mt('No write permission to user directory, FAIL').'</font>');
  130:         }
  131:         print $logfile 
  132: "\n\n================= Retrieve ".localtime()." ================\n".
  133: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  134:         $r->print('<p>'.&mt('Copying file').': ');
  135:         if (copy($csource,$ctarget)) {
  136: 	    $r->print('ok<p>');
  137:             print $logfile "Copied sucessfully.\n\n";
  138:         } else {
  139:             my $error=$!;
  140: 	    $r->print('fail, '.$error.'<p>');
  141:             print $logfile "Copy failed: $error\n\n";
  142:         }
  143:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  144:                   '">'.&mt('Back to').' '.$fn.'</a></font>'); 
  145:     } else {
  146:        $r->print(
  147:    '<font size=+1 color=red>'.&mt('Please pick a version to retrieve').'</font><p>');
  148:        &phaseone($r,$fn,$uname,$udom);
  149:     }
  150: }
  151: 
  152: # ---------------------------------------------------------------- Main Handler
  153: sub handler {
  154: 
  155:   my $r=shift;
  156: 
  157:   my $fn;
  158: 
  159: 
  160: # Get query string for limited number of parameters
  161: 
  162:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  163: 					  ['filename']);
  164: 
  165:   if ($ENV{'form.filename'}) {
  166:       $fn=$ENV{'form.filename'};
  167:       $fn=~s/^http\:\/\/[^\/]+//;
  168:   } else {
  169:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  170:          ' unspecified filename for retrieval', $r->filename); 
  171:      return HTTP_NOT_FOUND;
  172:   }
  173: 
  174:   unless ($fn) { 
  175:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  176:          ' trying to retrieve non-existing file', $r->filename); 
  177:      return HTTP_NOT_FOUND;
  178:   } 
  179: 
  180: # ----------------------------------------------------------- Start page output
  181:   my $uname;
  182:   my $udom;
  183: 
  184:   ($uname,$udom)=
  185:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  186:   unless (($uname) && ($udom)) {
  187:      $r->log_reason($uname.' at '.$udom.
  188:          ' trying to publish file '.$ENV{'form.filename'}.
  189:          ' ('.$fn.') - not authorized', 
  190:          $r->filename); 
  191:      return HTTP_NOT_ACCEPTABLE;
  192:   }
  193: 
  194:   $fn=~s/\/\~(\w+)//;
  195: 
  196:   &Apache::loncommon::content_type($r,'text/html');
  197:   $r->send_http_header;
  198: 
  199:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  200: 
  201:   $r->print(&Apache::loncommon::bodytag('Retrieve Published Resources'));
  202: 
  203:   
  204:   $r->print('<h1>'.&mt('Retrieve previous versions of').' <tt>'.$fn.'</tt></h1>');
  205:   
  206:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  207:           $r->print('<h3><font color=red>'.&mt('Co-Author').': '.$uname.
  208: 		    &mt(' at ').$udom.
  209:                '</font></h3>');
  210:   }
  211: 
  212: 
  213:   if ($ENV{'form.phase'} eq 'two') {
  214:       &phasetwo($r,$fn,$uname,$udom);
  215:   } else {
  216:       &phaseone($r,$fn,$uname,$udom);
  217:   }
  218: 
  219:   $r->print('</body></html>');
  220:   return OK;  
  221: }
  222: 
  223: 1;
  224: __END__
  225: 
  226: =head1 NAME
  227: 
  228: Apache::lonretrieve - retrieves an old version of a file
  229: 
  230: =head1 SYNOPSIS
  231: 
  232: Invoked by /etc/httpd/conf/srm.conf:
  233: 
  234:  <Location /adm/retrieve>
  235:  PerlAccessHandler       Apache::lonacc
  236:  SetHandler perl-script
  237:  PerlHandler Apache::lonretrieve
  238:  ErrorDocument     403 /adm/login
  239:  ErrorDocument     404 /adm/notfound.html
  240:  ErrorDocument     406 /adm/unauthorized.html
  241:  ErrorDocument	  500 /adm/errorhandler
  242:  </Location>
  243: 
  244: =head1 INTRODUCTION
  245: 
  246: This module retrieves an old published version of a file.
  247: 
  248: This is part of the LearningOnline Network with CAPA project
  249: described at http://www.lon-capa.org.
  250: 
  251: =head1 HANDLER SUBROUTINE
  252: 
  253: This routine is called by Apache and mod_perl.
  254: 
  255: =over 4
  256: 
  257: =item *
  258: 
  259: Get query string for limited number of parameters
  260: 
  261: =item *
  262: 
  263: Start page output
  264: 
  265: =item *
  266: 
  267: print phase relevant output
  268: 
  269: =item *
  270: 
  271: (phase one is to select version; phase two retrieves version)
  272: 
  273: =back
  274: 
  275: =head1 OTHER SUBROUTINES
  276: 
  277: =over 4
  278: 
  279: =item *
  280: 
  281: phaseone() : Interface for selecting previous version.
  282: 
  283: =item *
  284: 
  285: phasetwo() : Interface for presenting specified version.
  286: 
  287: =back
  288: 
  289: =cut

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