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