Annotation of loncom/publisher/lonretrieve.pm, revision 1.12
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to retrieve an old version of a file
3: #
4: # (Publication Handler
5: #
6: # (TeX Content Handler
7: #
8: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
9: #
10: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
11: # 03/23 Guy Albertelli
12: # 03/24,03/29 Gerd Kortemeyer)
13: #
1.11 www 14: # 03/31,04/03,05/02,05/09 Gerd Kortemeyer
1.1 www 15:
16: package Apache::lonretrieve;
17:
18: use strict;
19: use Apache::File;
20: use File::Copy;
21: use Apache::Constants qw(:common :http :methods);
1.10 www 22: use Apache::loncacc;
1.1 www 23:
1.2 www 24: sub phaseone {
25: my ($r,$fn,$uname,$udom)=@_;
26: my $docroot=$r->dir_config('lonDocRoot');
27:
1.3 www 28: my $urldir='/res/'.$udom.'/'.$uname.$fn;
29: $urldir=~s/\/[^\/]+$/\//;
30:
31: my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
32: my $resdir=$resfn;
1.2 www 33: $resdir=~s/\/[^\/]+$/\//;
34:
1.6 www 35: $fn=~/\/([^\/]+)\.(\w+)$/;
1.2 www 36: my $main=$1;
37: my $suffix=$2;
1.6 www 38:
39: if (-e $resfn) {
1.3 www 40: $r->print('<form action=/adm/retrieve method=post>'.
1.12 ! www 41: '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
1.3 www 42: '<input type=hidden name=phase value=two>'.
43: '<table border=2><tr><th>Select</th><th>Version</th>'.
44: '<th>Became this version on ...</th>'.
45: '<th>Metadata</th></tr>');
1.2 www 46: my $filename;
47: opendir(DIR,$resdir);
48: while ($filename=readdir(DIR)) {
49: if ($filename=~/^$main\.(\d+)\.$suffix$/) {
1.3 www 50: my $version=$1;
51: my ($rdev,$rino,$rmode,$rnlink,
52: $ruid,$rgid,$rrdev,$rsize,
53: $ratime,$rmtime,$rctime,
54: $rblksize,$rblocks)=stat($resdir.'/'.$filename);
55: $r->print('<tr><td><input type=radio name=version value="'.
56: $version.'"></td><th>'.$version.'</th><td>'.
57: localtime($rmtime).'</td><td>'.
58: '<a href="'.$urldir.$filename.'.meta" target=cat>'.
1.9 www 59: 'Metadata Version '.$version.'</a>');
1.8 www 60: if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
61: $r->print(
1.11 www 62: ' <a target=cat href="/adm/diff?filename=/~'.
63: $uname.$fn.
1.8 www 64: '&versionone=priv&versiontwo='.$version.
65: '">Diffs with Version '.$version.'</a>');
66: }
67: $r->print('</a></td></tr>');
1.2 www 68: }
69: }
70: closedir(DIR);
1.3 www 71: my ($rdev,$rino,$rmode,$rnlink,
72: $ruid,$rgid,$rrdev,$rsize,
73: $ratime,$rmtime,$rctime,
74: $rblksize,$rblocks)=stat($resfn);
75: $r->print('<tr><td><input type=radio name=version value="new"></td>'.
76: '<th>Current</th><td>'.localtime($rmtime).
77: '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
1.9 www 78: 'Metadata current version</a>');
79: if (&Apache::lonnet::fileembstyle($suffix) eq 'ssi') {
80: $r->print(
1.11 www 81: ' <a target=cat href="/adm/diff?filename=/~'.
82: $uname.$fn.
1.9 www 83: '&versionone=priv'.
84: '">Diffs with current Version</a>');
85: }
86: $r->print('</td></tr></table><p>'.
1.4 www 87: '<font size=+1 color=red>Retrieval of an old version will '.
88: 'overwrite the file currently in construction space</font><p>'.
1.3 www 89: '<input type=submit value="Retrieve version"></form>');
1.6 www 90: } else {
91: $r->print('<h3>No previous versions published.</h3>');
92: }
1.2 www 93: }
1.1 www 94:
1.4 www 95: sub phasetwo {
96: my ($r,$fn,$uname,$udom)=@_;
97: if ($ENV{'form.version'}) {
98: my $version=$ENV{'form.version'};
99: if ($version eq 'new') {
100: $r->print('<h3>Retrieving current (most recent) version</h3>');
101: } else {
102: $r->print('<h3>Retrieving old version '.$version.'</h3>');
103: }
104: my $logfile;
105: my $ctarget='/home/'.$uname.'/public_html'.$fn;
1.5 www 106: my $vfn=$fn;
107: if ($version ne 'new') {
108: $vfn=~s/\.(\w+)$/\.$version\.$1/;
109: }
110: my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
1.4 www 111: unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
112: $r->print(
113: '<font color=red>No write permission to user directory, FAIL</font>');
114: }
115: print $logfile
116: "\n\n================= Retrieve ".localtime()." ================\n".
1.5 www 117: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
118: $r->print('<p>Copying file: ');
119: if (copy($csource,$ctarget)) {
120: $r->print('ok<p>');
121: print $logfile "Copied sucessfully.\n\n";
122: } else {
123: my $error=$!;
124: $r->print('fail, '.$error.'<p>');
125: print $logfile "Copy failed: $error\n\n";
126: }
127: $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
128: '">Back to '.$fn.'</a></font>');
1.4 www 129: } else {
130: $r->print(
131: '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
132: &phaseone($r,$fn,$uname,$udom);
133: }
134: }
135:
1.1 www 136: sub handler {
137:
138: my $r=shift;
139:
140: my $fn;
141:
142: if ($ENV{'form.filename'}) {
143: $fn=$ENV{'form.filename'};
1.10 www 144: $fn=~s/^http\:\/\/[^\/]+//;
1.1 www 145: } else {
1.2 www 146: $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
147: ' unspecified filename for retrieval', $r->filename);
148: return HTTP_NOT_FOUND;
1.1 www 149: }
150:
151: unless ($fn) {
152: $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2 www 153: ' trying to retrieve non-existing file', $r->filename);
1.1 www 154: return HTTP_NOT_FOUND;
155: }
156:
157: # ----------------------------------------------------------- Start page output
1.10 www 158: my $uname;
159: my $udom;
1.1 www 160:
1.10 www 161: unless (($uname,$udom)=
162: &Apache::loncacc::constructaccess(
163: $fn,$r->dir_config('lonDefDomain'))) {
164: $r->log_reason($uname.' at '.$udom.
165: ' trying to publish file '.$ENV{'form.filename'}.
166: ' ('.$fn.') - not authorized',
167: $r->filename);
168: return HTTP_NOT_ACCEPTABLE;
169: }
170:
171: $fn=~s/\/\~(\w+)//;
1.1 www 172:
173: $r->content_type('text/html');
174: $r->send_http_header;
175:
176: $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
177:
178: $r->print(
179: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
180:
181:
1.2 www 182: $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
1.10 www 183:
184: if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
185: $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
186: '</font></h3>');
187: }
188:
1.1 www 189:
1.2 www 190: if ($ENV{'form.phase'} eq 'two') {
1.4 www 191: &phasetwo($r,$fn,$uname,$udom);
1.2 www 192: } else {
193: &phaseone($r,$fn,$uname,$udom);
194: }
1.1 www 195:
196: $r->print('</body></html>');
197: return OK;
198: }
1.7 www 199:
200: 1;
201: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>