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