Annotation of loncom/publisher/lonpubdir.pm, revision 1.31
1.1 www 1: # The LearningOnline Network with CAPA
1.16 albertel 2: # (Publication Handler
3: #
1.31 ! albertel 4: # $Id: lonpubdir.pm,v 1.30 2003/02/03 18:03:53 harris41 Exp $
1.16 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/
1.1 www 27: #
28: #
29: # (TeX Content Handler
30: #
1.17 harris41 31: # YEAR=2000
1.1 www 32: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
33: #
34: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
1.17 harris41 35: # YEAR=2001
1.1 www 36: # 03/23 Guy Albertelli
37: # 03/24,03/29 Gerd Kortemeyer)
1.17 harris41 38: # 03/31,04/03,05/09,06/23,08/18,08/20 Gerd Kortemeyer
1.19 www 39: # 12/28 Gerd Kortemeyer
1.1 www 40: #
1.17 harris41 41: ###
1.1 www 42:
43: package Apache::lonpubdir;
44:
45: use strict;
46: use Apache::File;
47: use File::Copy;
48: use Apache::Constants qw(:common :http :methods);
1.6 www 49: use Apache::loncacc;
1.17 harris41 50: use Apache::loncommon();
1.1 www 51:
52: sub handler {
53:
54: my $r=shift;
55:
56: my $fn;
57:
1.23 foxr 58:
59:
60: $fn = getEffectiveUrl($r);
61:
62: # Validate access to the construction space and get username@domain.
1.6 www 63:
64: my $uname;
65: my $udom;
66:
1.9 www 67: ($uname,$udom)=
1.6 www 68: &Apache::loncacc::constructaccess(
1.9 www 69: $fn,$r->dir_config('lonDefDomain'));
70: unless (($uname) && ($udom)) {
1.6 www 71: $r->log_reason($uname.' at '.$udom.
72: ' trying to publish file '.$ENV{'form.filename'}.
73: ' ('.$fn.') - not authorized',
74: $r->filename);
75: return HTTP_NOT_ACCEPTABLE;
76: }
1.23 foxr 77:
78: # Remove trailing / from direcgtory name.
79:
1.3 www 80: $fn=~s/\/$//;
1.1 www 81:
82: unless ($fn) {
83: $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1.2 www 84: ' trying to list empty directory', $r->filename);
1.1 www 85: return HTTP_NOT_FOUND;
86: }
87:
88: # ----------------------------------------------------------- Start page output
89:
1.23 foxr 90: my $thisdisfn=$fn;
91: $thisdisfn=~s/^\/home\/$uname\/public_html//; # subdirectory part of
92: # construction space.
93: my $docroot=$r->dir_config('lonDocRoot'); # Apache londocument root.
1.1 www 94:
1.23 foxr 95: my $resdir=$docroot.'/res/'.$udom.'/'.$uname.$thisdisfn; # Resource directory
96: my $targetdir=$udom.'/'.$uname.$thisdisfn; # Publiction target directory.
1.25 www 97: my $linkdir='/priv/'.$uname.$thisdisfn; # Full URL name of constr space.
1.1 www 98:
99:
100:
1.26 www 101: &startpage($r, $uname, $udom, $thisdisfn); # Put out the start of page.
1.6 www 102:
1.23 foxr 103: # Start off the diretory table.
1.1 www 104:
1.2 www 105: $r->print('<table border=2>'.
1.23 foxr 106: '<tr><th>Actions</th><th>Name</th><th>Title</th>'.
107: '<th>Status</th><th>Last Modified</th></tr>');
1.1 www 108:
1.2 www 109: my $filename;
1.23 foxr 110: my $dirptr=16384; # Mask indicating a directory in stat.cmode.
1.1 www 111:
1.2 www 112: opendir(DIR,$fn);
1.10 albertel 113: my @files=sort(readdir(DIR));
1.11 albertel 114: foreach my $filename (@files) {
1.2 www 115: my ($cdev,$cino,$cmode,$cnlink,
116: $cuid,$cgid,$crdev,$csize,
117: $catime,$cmtime,$cctime,
118: $cblksize,$cblocks)=stat($fn.'/'.$filename);
1.12 www 119:
1.10 albertel 120: my $extension='';
121: if ($filename=~/\.(\w+)$/) { $extension=$1; }
1.15 matthew 122: if ($cmode&$dirptr) {
1.21 foxr 123: putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime);
1.17 harris41 124: } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
1.22 foxr 125: putresource($r, $uname, $filename, $thisdisfn, $resdir,
126: $targetdir, $linkdir, $cmtime);
1.14 albertel 127: } else {
1.15 matthew 128: # "hidden" extension and not a directory, so hide it away.
1.2 www 129: }
130: }
131: closedir(DIR);
132:
133: $r->print('</table></body></html>');
1.1 www 134: return OK;
135: }
1.21 foxr 136: #
1.23 foxr 137: # Gets the effective URL of the request and returns it:
138: # $effn = getEffectiveUrl($r);
139: # $r - The Apache Request object.
140: sub getEffectiveUrl {
141: my $r = shift;
142: my $fn;
143:
144: if ($ENV{'form.filename'}) { # If a form filename is defined.
145: $fn=$ENV{'form.filename'};
146: #
147: # Replace the ~username of the URL with /home/username/public_html
148: # so that we don't have to worry about ~ expansion internally.
149: #
150: $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
151:
152: # Remove trailing / strings (?)
153:
154: $fn=~s/\/[^\/]+$//;
1.24 albertel 155: } else {
156: # If no form is defined, use request filename.
157: $fn = $r->filename();
158: my $lonDocRoot=$r->dir_config('lonDocRoot');
159: if ( $fn =~ /$lonDocRoot/ ) {
160: #internal authentication, needs fixup.
161: $fn = $r->uri(); # non users do not get the full path request
162: # through SCRIPT_FILENAME
163: $fn=~s|^/~(\w+)|/home/$1/public_html|;
164: }
1.23 foxr 165: }
166: return $fn;
167: }
168: #
169: # Output the header of the page. This includes:
170: # - The HTML header
171: # - The H1/H3 stuff which includes the directory.
172: #
173: # startpage($r, $uame, $udom, $thisdisfn);
174: # $r - The apache request object.
175: # $uname - User name.
176: # $udom - Domain name the user is logged in under.
177: # $thisdisfn - Displayable version of the filename.
1.26 www 178:
1.23 foxr 179: sub startpage {
180: my ($r, $uname, $udom, $thisdisfn) = @_;
181:
182: $r->content_type('text/html');
183: $r->send_http_header;
184:
185: $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
186:
1.26 www 187: $r->print(&Apache::loncommon::bodytag(undef,undef,undef,1));
1.29 www 188: my $pubdirscript=(<<ENDPUBDIRSCRIPT);
189: <script>
190: function pubdir(theform) {
191: if (confirm('Publish complete directory?')) {
192: theform.submit();
193: }
194: }
195: function pubrecdir(theform) {
196: if (confirm('Publish directory and all subdirectories?')) {
197: theform.pubrec.value='1';
198: theform.submit();
199: }
200: }
201: </script>
202: ENDPUBDIRSCRIPT
203:
1.23 foxr 204: $r->print('<h1>Construction Space Directory <tt>'.
1.31 ! albertel 205: $thisdisfn.'/</tt></h1>'.
! 206: '<script type="text/javascript">top.document.title = \''.
! 207: $thisdisfn.'/ - LON-CAPA Construction Space\';</script>'.
! 208: $pubdirscript.
1.26 www 209: '<form method="post" action="/adm/publish" target="_parent">'.
210: '<input type="hidden" name="filename" value="/~'.
211: $uname.$thisdisfn.'/" />'.
1.29 www 212: '<input type="button" onClick="pubdir(this.form);" value="Publish Directory" />'.
213: '<input type="hidden" name="pubrec" value="" />'.
214: '<input type="button" onClick="pubrecdir(this.form);" value="Publish Directory and Sub Directories" />'.
1.28 www 215: '<input type="button" onClick="window.location='."'/~".
216: $uname.$thisdisfn."/default.meta'".'" value="Directory Catalog Information" /></form>');
1.23 foxr 217:
218: if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
219: $r->print('<h3>Co-Author: '.$uname.' at '.$udom.
220: '</h3>');
221: }
222: }
223:
224: #
225: # Get the title string or "[untitled]" if the file has no title metadata:
226: # Without the latter substitution, it's impossible to examine metadata for
227: # untitled resources. Resources may be legitimately untitled, to prevent
228: # searches from locating them.
229: #
230: # $str = getTitleString($fullname);
231: # $fullname - Fully qualified filename to check.
232: #
233: sub getTitleString {
234: my $fullname = shift;
235: my $title = &Apache::lonnet::metadata($fullname, 'title');
236:
237: unless ($title) {
238: $title = "[untitled]";
239: }
240: return $title;
241: }
242:
243:
244: #
1.21 foxr 245: # Put out a directory table row:
246: # putdirectory(r, base, here, dirname, modtime)
247: # r - Apache request object.
248: # reqfile - File in request.
249: # here - Where we are in directory tree.
250: # dirname - Name of directory special file.
251: # modtime - Encoded modification time.
252: #
253: sub putdirectory {
254: my ($r, $reqfile, $here, $dirname, $modtime) = @_;
255:
256: # construct the display filename: the directory name unless ..:
257:
258: my $disfilename = $dirname;
259: if ($dirname eq '..') {
260: $disfilename = '<i>Parent Directory</i>';
261: }
262: unless (( ($dirname eq '..') && ($reqfile eq '')) ||
263: ($dirname eq '.')) {
1.25 www 264: $r->print('<tr bgcolor="#CCCCFF">'.
265: '<td>Go to ...</td>'.
266: '<td><a href="'.$here.'/'.$dirname.'/" target="_top">'.
1.21 foxr 267: $disfilename.'</a></td>'.
268: '<td> </td>'.
269: '<td> </td>'.
270: '<td>'.localtime($modtime).'</td>'.
1.25 www 271: "</tr>\n");
1.21 foxr 272: }
273: return OK;
274: }
1.22 foxr 275: #
276: # Put a table row for a file resource.
277: #
278: sub putresource {
279: my ($r, $uname, $filename, $thisdisfn,
280: $resdir, $targetdir, $linkdir,
281: $cmtime) = @_;
282:
283: my $status='Unpublished';
1.25 www 284: my $bgcolor='#FFCCCC';
1.22 foxr 285: my $title=' ';
286: if (-e $resdir.'/'.$filename) {
287: my ($rdev,$rino,$rmode,$rnlink,
288: $ruid,$rgid,$rrdev,$rsize,
289: $ratime,$rmtime,$rctime,
290: $rblksize,$rblocks)=stat($resdir.'/'.$filename);
291: if ($rmtime>=$cmtime) {
292: $status='Published';
1.25 www 293: $bgcolor='#CCFFCC';
1.23 foxr 294: $title='<a href="/res/'.$targetdir.'/'.$filename.
295: '.meta" target=cat>'.
296: getTitleString($targetdir.'/'.$filename, 'title').'</a>';
1.22 foxr 297: } else {
298: $status='Modified';
1.25 www 299: $bgcolor='#FFFFCC';
1.22 foxr 300: $title='<a href="/res/'.$targetdir.'/'.$filename.'.meta" target=cat>'.
1.25 www 301: getTitleString($targetdir.'/'.$filename,'title').'</a>';
1.22 foxr 302: if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
303: $status.='<br><a href="/adm/diff?filename=/~'.$uname.
304: $thisdisfn.'/'.$filename.
1.27 albertel 305: '&versiontwo=priv" target=cat>Diffs</a>';
1.22 foxr 306: }
307: }
308: $status.='<br><a href="/adm/retrieve?filename=/~'.$uname.
309: $thisdisfn.'/'.$filename.'" target=cat>Retrieve</a>';
310: }
1.25 www 311: $r->print('<tr bgcolor="'.$bgcolor.'">'.
1.22 foxr 312: '<td><a target="_parent" href="/adm/publish?filename=/~'.
313: $uname.$thisdisfn.'/'.$filename.'">'.'Publish</a>'.
314: '</td>'.
315: '<td>'.
1.25 www 316: '<a href="'.$linkdir.'/'.$filename.'" target="_top">'.
317: $filename.'</a>'.
1.22 foxr 318: '</td>'.
319: '<td>'.$title.'</td>'.
320: '<td>'.$status.'</td>'.
321: '<td>'.localtime($cmtime).'</td>'.
1.25 www 322: "</tr>\n");
1.22 foxr 323: return OK;
1.23 foxr 324: }
325: #
326: # Categorize files in the directory.
327: # For each file in a list of files in a file directory,
328: # the file categorized as one of:
329: # - directory
330: # - sequence
331: # - problem
332: # - Other resource.
333: #
334: # For each file the modification date is determined as well.
335: # Returned is a list of sublists:
336: # (directories, sequences, problems, other)
337: # each of the sublists contains entries of the following form (sorted by
338: # filename):
339: # (filename, typecode, lastmodtime)
340: #
341: # $list = CategorizeFiles($location, $files)
342: # $location - Directory in which the files live (relative to our
343: # execution.
344: # $files - list of files.
345: #
346: sub CategorizeFiles {
347: my $location = shift;
348: my $files = shift;
1.22 foxr 349: }
350:
1.4 www 351: 1;
352: __END__
1.17 harris41 353:
354: =head1 NAME
355:
356: Apache::lonpubdir - Publication Handler for Directories
357:
358: =head1 SYNOPSIS
359:
360: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
361:
1.18 harris41 362: <LocationMatch "^/\~.*/$">
363: PerlAccessHandler Apache::loncacc
364: SetHandler perl-script
365: PerlHandler Apache::lonpubdir
366: ErrorDocument 403 /adm/login
367: ErrorDocument 404 /adm/notfound.html
368: ErrorDocument 406 /adm/unauthorized.html
369: ErrorDocument 500 /adm/errorhandler
370: </LocationMatch>
371:
372: <Location /adm/pubdir>
373: PerlAccessHandler Apache::lonacc
374: SetHandler perl-script
375: PerlHandler Apache::lonpubdir
376: ErrorDocument 403 /adm/login
377: ErrorDocument 404 /adm/notfound.html
378: ErrorDocument 406 /adm/unauthorized.html
379: ErrorDocument 500 /adm/errorhandler
380: </Location>
1.17 harris41 381:
382: =head1 INTRODUCTION
383:
1.18 harris41 384: This module publishes a directory of files.
1.17 harris41 385:
386: This is part of the LearningOnline Network with CAPA project
387: described at http://www.lon-capa.org.
388:
389: =head1 HANDLER SUBROUTINE
390:
391: This routine is called by Apache and mod_perl.
392:
393: =over 4
394:
395: =item *
396:
397: read in information
398:
399: =item *
400:
401: start page output
402:
403: =item *
404:
405: run through list of files and attempt to publish unhidden files
406:
407: =back
408:
409: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>