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