1: # The LearningOnline Network with CAPA
2: # Handler to retrieve an old version of a file
3: #
4: # $Id: lonretrieve.pm,v 1.42 2010/12/14 16:26:04 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: =head1 NAME
32:
33: Apache::lonretrieve - retrieves an old version of a file
34:
35: =head1 SYNOPSIS
36:
37: Invoked by /etc/httpd/conf/srm.conf:
38:
39: <Location /adm/retrieve>
40: PerlAccessHandler Apache::lonacc
41: SetHandler perl-script
42: PerlHandler Apache::lonretrieve
43: ErrorDocument 403 /adm/login
44: ErrorDocument 404 /adm/notfound.html
45: ErrorDocument 406 /adm/unauthorized.html
46: ErrorDocument 500 /adm/errorhandler
47: </Location>
48:
49: =head1 INTRODUCTION
50:
51: This module retrieves an old published version of a file.
52:
53: This is part of the LearningOnline Network with CAPA project
54: described at http://www.lon-capa.org.
55:
56: =head1 HANDLER SUBROUTINE
57:
58: This routine is called by Apache and mod_perl.
59:
60: =over 4
61:
62: =item *
63:
64: Get query string for limited number of parameters
65:
66: =item *
67:
68: Start page output
69:
70: =item *
71:
72: print phase relevant output
73:
74: =item *
75:
76: (phase one is to select version; phase two retrieves version)
77:
78: =back
79:
80: =head1 OTHER SUBROUTINES
81:
82: =over 4
83:
84: =item *
85:
86: phaseone() : Interface for selecting previous version.
87:
88: =item *
89:
90: phasetwo() : Interface for presenting specified version.
91:
92: =back
93:
94: =cut
95:
96: package Apache::lonretrieve;
97:
98: use strict;
99: use Apache::File;
100: use File::Copy;
101: use Apache::Constants qw(:common :http :methods);
102: use Apache::loncacc;
103: use Apache::loncommon();
104: use Apache::lonlocal;
105: use Apache::lonnet;
106: use LONCAPA();
107:
108: # ------------------------------------ Interface for selecting previous version
109: sub phaseone {
110: my ($r,$fn,$uname,$udom)=@_;
111: my $docroot=$r->dir_config('lonDocRoot');
112:
113: my $urldir='/res/'.$udom.'/'.$uname.$fn;
114: $urldir=~s/\/[^\/]+$/\//;
115:
116: my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
117: my $resdir=$resfn;
118: $resdir=~s/\/[^\/]+$/\//;
119:
120: my ($main,$suffix,$is_meta) = &get_file_info($fn);
121:
122: if (-e $resfn) {
123: $r->print('<form action="/adm/retrieve" method="post">'.
124: '<input type="hidden" name="filename" value="/~'.$uname.$fn.'" />'.
125: '<input type="hidden" name="phase" value="two" />'.
126: &Apache::loncommon::start_data_table().
127: &Apache::loncommon::start_data_table_header_row().
128: '<th>'.&mt('Select').'</th>'.
129: '<th>'.&mt('Version').'</th>'.
130: '<th>'.&mt('Published on ...').'</th>');
131: if (!$is_meta) {
132: $r->print('<th>'.&mt('Metadata').'</th>');
133: }
134: if ($is_meta
135: || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
136: $r->print('<th>'.&mt('Diffs').'</th>');
137: }
138: $r->print(&Apache::loncommon::end_data_table_header_row());
139:
140: opendir(DIR,$resdir);
141: my @files = grep(/^\Q$main\E\.(\d+)\.\Q$suffix\E$/,readdir(DIR));
142: @files = sort {
143: my ($aver) = ($a=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/);
144: my ($bver) = ($b=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/);
145: return $aver <=> $bver;
146: } (@files);
147: closedir(DIR);
148:
149: foreach my $filename (@files) {
150: if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
151: my $version=$1;
152: my $rmtime=&Apache::lonnet::metadata($resdir.'/'.$filename,'lastrevisiondate');
153: $r->print(&Apache::loncommon::start_data_table_row().
154: '<td><input type="radio" name="version" value="'.
155: $version.'" /></td><td>'.&mt('Previously published version').' '.$version.'</td>'.
156: '<td>'.&Apache::lonlocal::locallocaltime($rmtime).'</td>');
157:
158: if (!$is_meta) {
159: $r->print('<td><a href="'.$urldir.$filename.'.meta" target="cat">'.
160: &mt('Metadata Version').' '.$version.'</a></td>');
161: }
162: if ($is_meta
163: || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
164: $r->print(
165: '<td><a target="cat" href="/adm/diff?filename=/~'.
166: $uname.$fn.
167: '&versiontwo=priv&versionone='.$version.
168: '">'.&mt('Diffs with Version').' '.$version.
169: '</a></td>');
170: }
171: $r->print(&Apache::loncommon::end_data_table_row());
172: }
173: }
174: closedir(DIR);
175: my $rmtime=&Apache::lonnet::metadata($resfn,'lastrevisiondate');
176: $r->print(&Apache::loncommon::start_data_table_row().
177: '<td><input type="radio" name="version" value="new" /></td>'.
178: '<td><b>'.&mt('Currently published version').'</b></td>'.
179: '<td>'.&Apache::lonlocal::locallocaltime($rmtime).'</td>'
180: );
181: if (!$is_meta) {
182: $r->print('<td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target="cat">'.
183: &mt('Metadata current version').'</a></td>');
184: }
185: if ($is_meta
186: || &Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
187: $r->print(
188: '<td><a target="cat" href="/adm/diff?filename=/~'.
189: $uname.$fn.
190: '&versiontwo=priv'.
191: '">'.&mt('Diffs with current Version').'</a></td>');
192: }
193: $r->print(&Apache::loncommon::end_data_table_row().
194: &Apache::loncommon::end_data_table().
195: '<p>'.'<span class="LC_warning">'.
196: &mt('Retrieval of an old version will overwrite the file currently in construction space.').'</span></p>');
197: if (!$is_meta) {
198: $r->print('<p>'.'<span class="LC_warning">'.
199: &mt('This will only retrieve the resource. If you want to retrieve the metadata, you will need to do that separately.').
200: '</span></p>');
201: }
202: $r->print('<input type="submit" value="'.&mt('Retrieve selected Version').'" /></form>');
203: } else {
204: $r->print('<p class="LC_warning">'.&mt('No previous versions published.').'</p>');
205: }
206:
207: my $dir = &Apache::loncommon::authorspace()
208: .&File::Basename::dirname($fn)
209: .'/';
210: $r->print(&Apache::lonhtmlcommon::start_funclist()
211: .&Apache::lonhtmlcommon::add_item_funclist(
212: '<a href="/priv/'.$uname.$fn.'">'
213: .&mt('Back to Resource')
214: .'</a>')
215: .&Apache::lonhtmlcommon::add_item_funclist(
216: '<a href="'.$dir.'">'
217: .&mt('Back to Directory')
218: .'</a>')
219: .&Apache::lonhtmlcommon::end_funclist()
220: );
221: }
222:
223: # ---------------------------------- Interface for presenting specified version
224: sub phasetwo {
225: my ($r,$fn,$uname,$udom)=@_;
226: if ($env{'form.version'}) {
227: my $version=$env{'form.version'};
228: if ($version eq 'new') {
229: $r->print('<h3>'.&mt('Retrieving current (most recent) version').'</h3>');
230: } else {
231: $r->print('<h3>'.&mt('Retrieving old version').' '.$version.'</h3>');
232: }
233: my ($main,$suffix,$is_meta) = &get_file_info($fn);
234:
235: my $logfile;
236: my $ctarget='/home/'.$uname.'/public_html'.$fn;
237: my $vfn=$fn;
238: if ($version ne 'new') {
239: $vfn=~s/\.(\Q$suffix\E)$/\.$version\.$1/;
240: }
241:
242: my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
243:
244: my $logname = $ctarget;
245: if ($is_meta) { $logname =~ s/\.meta$//; }
246: $logname = $ctarget.'.log';
247: unless ($logfile=Apache::File->new('>>'.$logname)) {
248: $r->print('<span class="LC_error">'
249: .&mt('No write permission to user directory, FAIL')
250: .'</span>');
251: }
252: print $logfile
253: "\n\n================= Retrieve ".localtime()." ================\n".
254: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
255: $r->print('<p>'.&mt('Copying file').': ');
256: if (copy($csource,$ctarget)) {
257: $r->print('<span class="LC_success">'
258: .&mt('ok')
259: .'</span>');
260: print $logfile "Copied sucessfully.\n\n";
261: } else {
262: my $error=$!;
263: $r->print('<span class="LC_error">'
264: .&mt('Copy failed: [_1]',$error)
265: .'</span>');
266: print $logfile "Copy failed: $error\n\n";
267: }
268: $r->print('</p>'
269: .'<p><a href="/priv/'.$uname.$fn.'">'
270: .&mt('Back to Resource')
271: .'</a></p>');
272: } else {
273: $r->print('<p class="LC_info">'.&mt('Please pick a version to retrieve:').'</p>');
274: &phaseone($r,$fn,$uname,$udom);
275: }
276: }
277:
278: sub get_file_info {
279: my ($fn) = @_;
280: my ($main,$suffix) = ($fn=~/\/([^\/]+)\.(\w+)$/);
281: my $is_meta=0;
282: if ($suffix eq 'meta') {
283: $is_meta = 1;
284: ($main,$suffix) = ($main=~/(.+)\.(\w+)$/);
285: $suffix .= '.meta';
286: }
287: return ($main,$suffix,$is_meta);
288: }
289:
290: # ---------------------------------------------------------------- Main Handler
291: sub handler {
292:
293: my $r=shift;
294:
295: my $fn;
296:
297:
298: # Get query string for limited number of parameters
299:
300: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
301: ['filename']);
302:
303: if ($env{'form.filename'}) {
304: $fn=$env{'form.filename'};
305: $fn=~s/^https?\:\/\/[^\/]+//;
306: } else {
307: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
308: ' unspecified filename for retrieval', $r->filename);
309: return HTTP_NOT_FOUND;
310: }
311:
312: unless ($fn) {
313: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
314: ' trying to retrieve non-existing file', $r->filename);
315: return HTTP_NOT_FOUND;
316: }
317:
318: # ----------------------------------------------------------- Start page output
319: my $uname;
320: my $udom;
321:
322: ($uname,$udom)=
323: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
324: unless (($uname) && ($udom)) {
325: $r->log_reason($uname.' at '.$udom.
326: ' trying to publish file '.$env{'form.filename'}.
327: ' ('.$fn.') - not authorized',
328: $r->filename);
329: return HTTP_NOT_ACCEPTABLE;
330: }
331:
332: $fn=~s{/~($LONCAPA::username_re)}{};
333:
334: &Apache::loncommon::content_type($r,'text/html');
335: $r->send_http_header;
336:
337: # Breadcrumbs
338: &Apache::lonhtmlcommon::clear_breadcrumbs();
339: &Apache::lonhtmlcommon::add_breadcrumb({
340: 'text' => 'Construction Space',
341: 'href' => &Apache::loncommon::authorspace(),
342: });
343: &Apache::lonhtmlcommon::add_breadcrumb({
344: 'text' => 'Retrieve previous version',
345: 'href' => '',
346: });
347:
348: $r->print(&Apache::loncommon::start_page('Retrieve Published Resources')
349: .&Apache::lonhtmlcommon::breadcrumbs()
350: .&Apache::loncommon::head_subbox(
351: &Apache::loncommon::CSTR_pageheader()) # FIXME crumbs broken?
352: );
353:
354: $r->print('<p>'
355: .&mt('Retrieve previous versions of [_1]'
356: ,'<span class="LC_filename">'.$fn.'</span>')
357: .'</p>');
358:
359: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
360: $r->print('<p><span class="LC_info">'
361: .&mt('Co-Author [_1]'
362: ,&Apache::loncommon::plainname($uname,$udom)
363: .' ('.$uname.':'.$udom.')')
364: .'</span></p>');
365: }
366:
367:
368: if ($env{'form.phase'} eq 'two') {
369: &phasetwo($r,$fn,$uname,$udom);
370: } else {
371: &phaseone($r,$fn,$uname,$udom);
372: }
373:
374: $r->print(&Apache::loncommon::end_page());
375: return OK;
376: }
377:
378: 1;
379: __END__
380:
381:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>