1: # The LearningOnline Network with CAPA
2: # Authoring Space Directory Lister
3: #
4: # $Id: lonpubdir.pm,v 1.160.2.5.2.3 2024/02/28 04:58:02 raeburn 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: package Apache::lonpubdir;
31:
32: use strict;
33: use Apache::File;
34: use File::Copy;
35: use Apache::Constants qw(:common :http :methods);
36: use Apache::loncommon();
37: use Apache::lonhtmlcommon();
38: use Apache::londiff();
39: use Apache::lonlocal;
40: use Apache::lonmsg;
41: use Apache::lonmenu;
42: use Apache::lonnet;
43: use LONCAPA qw(:DEFAULT :match);
44:
45: sub handler {
46:
47: my $r=shift;
48:
49: # Validate access to the construction space and get username:domain.
50:
51: my ($uname,$udom)=&Apache::lonnet::constructaccess($r->uri);
52: unless (($uname) && ($udom)) {
53: return HTTP_NOT_ACCEPTABLE;
54: }
55:
56: # ----------------------------------------------------------- Start page output
57:
58: my $fn=$r->filename;
59: $fn=~s/\/$//;
60: my $thisdisfn=$fn;
61:
62: my $docroot=$r->dir_config('lonDocRoot'); # Apache londocument root.
63: if ($thisdisfn eq "$docroot/priv/$udom") {
64: if ((-d "/home/$uname/public_html/") && (!-e "$docroot/priv/$udom/$uname")) {
65: my ($version) = ($r->dir_config('lonVersion') =~ /^\'?(\d+\.\d+)\./);
66: &Apache::loncommon::content_type($r,'text/html');
67: $r->send_http_header;
68:
69: &Apache::lonhtmlcommon::clear_breadcrumbs();
70: $r->print(&Apache::loncommon::start_page('Authoring Space').
71: '<div class="LC_error">'.
72: '<br /><p>'.
73: &mt('Your Authoring Space is currently in the location used by LON-CAPA version 2.10 and older, but your domain is using a newer LON-CAPA version ([_1]).',$version).'</p>'.
74: '<p>'.
75: &mt('Please ask your Domain Coordinator to move your Authoring Space to the new location.').
76: '</p>'.
77: '</div>'.
78: &Apache::loncommon::end_page());
79: return OK;
80: }
81: }
82: $thisdisfn=~s/^\Q$docroot\E\/priv//;
83:
84: my $resdir=$docroot.'/res'.$thisdisfn; # Resource directory
85: my $targetdir='/res'.$thisdisfn; # Publication target directory.
86: my $linkdir='/priv'.$thisdisfn; # Full URL name of constr space.
87:
88: my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
89:
90: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
91: my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,
92: "$londocroot/priv/$udom/$uname"); # expressed in kB
93: my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,
94: 'author'); # expressed in MB
95:
96: # Put out the start of page.
97: &startpage($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota);
98:
99: if (!-d $fn) {
100: if (-e $fn) {
101: $r->print('<p class="LC_info">'.&mt('Requested item is a file not a directory.').'</p>');
102: } else {
103: $r->print('<p class="LC_info">'.&mt('The requested subdirectory does not exist.').'</p>');
104: }
105: $r->print(&Apache::loncommon::end_page());
106: return OK;
107: }
108: my @files;
109: if (opendir(DIR,$fn)) {
110: @files = grep(!/^\.+$/,readdir(DIR));
111: closedir(DIR);
112: } else {
113: $r->print('<p class="LC_error">'.&mt('Could not open directory.').'</p>');
114: $r->print(&Apache::loncommon::end_page());
115: return OK;
116: }
117:
118: # Put out actions for directory, browse/upload + new file page.
119: &dircontrols($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota);
120: &resourceactions($r,$uname,$udom,$thisdisfn); # Put out form used for printing/deletion etc.
121:
122: my $numdir = 0;
123: my $numres = 0;
124:
125: if ((@files == 0) && ($thisdisfn =~ m{^/$match_domain/$match_username})) {
126: if ($thisdisfn =~ m{^/$match_domain/$match_username$}) {
127: $r->print('<p class="LC_info">'.&mt('This Authoring Space is currently empty.').'</p>');
128: } else {
129: $r->print('<p class="LC_info">'.&mt('This subdirectory is currently empty.').'</p>');
130: }
131: $r->print(&Apache::loncommon::end_page());
132: return OK;
133: }
134:
135: # Retrieving value for "sortby" and "sortorder" from QUERY_STRING
136: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
137: ['sortby','sortorder']);
138:
139: # Sort by name as default, not reversed
140: if (! exists($env{'form.sortby'})) { $env{'form.sortby'} = 'filename' }
141: if (! exists($env{'form.sortorder'})) { $env{'form.sortorder'} = '' }
142: my $sortby = $env{'form.sortby'};
143: my $sortorder = $env{'form.sortorder'};
144:
145: # Order in which columns are displayed from left to right
146: my @order = ('filetype','actions','filename','title',
147: 'pubstatus','cmtime','size');
148:
149: # Up and down arrows to indicate sort order
150: my @arrows = (' ▲',' ▼','');
151:
152: # Default sort order and column title
153: my %columns = (
154: filetype => {
155: order => 'ascending',
156: text => &mt('Type'),
157: },
158: actions => {
159: # Not sortable
160: text => &mt('Actions'),
161: },
162: filename => {
163: order => 'ascending',
164: text => &mt('Name'),
165: },
166: title => {
167: order => 'ascending',
168: text => &mt('Title'),
169: },
170: pubstatus => {
171: order => 'ascending',
172: text => &mt('Status'),
173: colspan => '2',
174: },
175: cmtime => {
176: order => 'descending',
177: text => &mt('Last Modified'),
178: },
179: size => {
180: order => 'ascending',
181: text => &mt('Size').' (kB)',
182: },
183: );
184:
185: # Print column headers
186: my $output = '';
187: foreach my $key (@order) {
188: my $idx;
189: # Append an up or down arrow to sorted column
190: if ($sortby eq $key) {
191: $idx = ($columns{$key}{order} eq 'ascending') ? 0:1;
192: if ($sortorder eq 'rev') { $idx ++; }
193: $idx = $idx%2;
194: } else { $idx = 2; } # No arrow if column is not sorted
195: $output .= (($columns{$key}{order}) ?
196: '<th'.($columns{$key}{colspan} ? ' colspan="'.$columns{$key}{colspan}.'"' : '')
197: .'><a href="'.$linkdir.'/?sortby='.$key.'&sortorder='
198: .((($sortby eq $key) && ($sortorder ne 'rev')) ? 'rev' : '').'">'
199: .$columns{$key}{text}.$arrows[$idx].'</a></th>' :
200: '<th>'.$columns{$key}{text}.'</th>');
201: }
202: $r->print(&Apache::loncommon::start_data_table()
203: .&Apache::loncommon::start_data_table_header_row() . $output
204: .&Apache::loncommon::end_data_table_header_row()
205: );
206:
207: my $dirptr=16384; # Mask indicating a directory in stat.cmode.
208: my $filehash = {};
209: foreach my $filename (@files) {
210: # Skip .DS_Store, .DAV and hidden files
211: my ($extension) = ($filename=~/\.(\w+)$/);
212: next if (($filename eq '.DS_Store')
213: || ($filename eq '.DAV')
214: || (&Apache::loncommon::fileembstyle($extension) eq 'hdn')
215: || ($filename =~ /^\._/));
216:
217: my ($cmode,$csize,$cmtime)=(stat($fn.'/'.$filename))[2,7,9];
218: my $linkfilename = &HTML::Entities::encode('/priv'.$thisdisfn.'/'.$filename,'<>&"');
219: # Identify type of file according to icon used
220: my ($filetype) = (&Apache::loncommon::icon($filename) =~ m{/(\w+).gif$});
221: my $cstr_dir = $r->dir_config('lonDocRoot').'/priv'.$thisdisfn;
222: my $meta_same = &isMetaSame($cstr_dir, $resdir, $filename);
223:
224: # Store size, title, and status for files but not directories
225: my $size = (!($cmode&$dirptr)) ? $csize/1024. : 0;
226: my ($status, $pubstatus, $title, $fulltitle);
227: if (!($cmode&$dirptr)) {
228: ($status, $pubstatus) = &getStatus($resdir, $targetdir, $cstr_dir,
229: $filename, $linkfilename, $cmtime, $meta_same);
230: ($fulltitle, $title) = &getTitle($resdir, $targetdir, $filename,
231: $linkfilename, $meta_same, \%bombs);
232: } else {
233: ($status, $pubstatus) = ('','');
234: ($fulltitle, $title) = ('','');
235: }
236:
237: # This hash will allow sorting
238: $filehash->{ $filename } = {
239: "cmtime" => $cmtime,
240: "size" => $size,
241: "cmode" => $cmode,
242: "filetype" => $filetype,
243: "title" => $title,
244: "fulltitle" => $fulltitle,
245: "status" => $status,
246: "pubstatus" => $pubstatus,
247: "linkfilename" => $linkfilename,
248: }
249: }
250:
251: my @sorted_files;
252: # Sorting by something other than "Name". Name is the secondary key.
253: if ($sortby =~ m{cmtime|size}) { # Numeric fields
254: # First check if order should be reversed
255: if ($sortorder eq "rev") {
256: @sorted_files = sort {
257: $filehash->{$a}->{$sortby} <=> $filehash->{$b}->{$sortby}
258: or
259: uc($a) cmp uc($b)
260: } (keys(%{$filehash}));
261: } else {
262: @sorted_files = sort {
263: $filehash->{$b}->{$sortby} <=> $filehash->{$a}->{$sortby}
264: or
265: uc($a) cmp uc($b)
266: } (keys(%{$filehash}));
267: }
268: } elsif ($sortby =~ m{filetype|title|status}) { # String fields
269: if ($sortorder eq "rev") {
270: @sorted_files = sort {
271: $filehash->{$b}->{$sortby} cmp $filehash->{$a}->{$sortby}
272: or
273: uc($a) cmp uc($b)
274: } (keys(%{$filehash}));
275: } else {
276: @sorted_files = sort {
277: $filehash->{$a}->{$sortby} cmp $filehash->{$b}->{$sortby}
278: or
279: uc($a) cmp uc($b)
280: } (keys(%{$filehash}));
281: }
282:
283: # Sort by "Name" is the default
284: } else {
285: if ($sortorder eq "rev") {
286: @sorted_files = sort {uc($b) cmp uc($a)} (keys(%{$filehash}));
287: } else {
288: @sorted_files = sort {uc($a) cmp uc($b)} (keys(%{$filehash}));
289: }
290: }
291:
292: # Print the sorted resources
293: my %editors = &Apache::loncommon::permitted_editors();
294: foreach my $filename (@sorted_files) {
295: if ($filehash->{$filename}->{"cmode"}&$dirptr) { # Directories
296: &putdirectory($r, $thisdisfn, $linkdir, $filename,
297: $filehash->{$filename}->{"cmtime"},
298: $targetdir, \%bombs, \$numdir);
299: } else { # Files
300: &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir,
301: $targetdir, $linkdir, $filehash->{$filename}->{"cmtime"},
302: $filehash->{$filename}->{"size"}, \$numres,
303: $filehash->{$filename}->{"linkfilename"},
304: $filehash->{$filename}->{"fulltitle"},
305: $filehash->{$filename}->{"status"},
306: $filehash->{$filename}->{"pubstatus"},
307: \%editors);
308: }
309: }
310:
311: $r->print( &Apache::loncommon::end_data_table()
312: .&Apache::loncommon::end_page() );
313:
314: return OK;
315: }
316:
317:
318:
319: # Output the header of the page. This includes:
320: # - The HTML header
321: # - The H1/H3 stuff which includes the directory.
322: #
323: # startpage($r, $uame, $udom, $thisdisfn, $current_disk_usage, $disk_quota);
324: # $r - The apache request object.
325: # $uname - User name.
326: # $udom - Domain name the user is logged in under.
327: # $thisdisfn - Displayable version of the filename.
328: # $current_disk_usage - User's current disk usage (in kB).
329: # $disk_quota - Disk quota for user's authoring space (in MB).
330: # $crstype - Course type, if this is for "course author"
331:
332: sub startpage {
333: my ($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota) = @_;
334: &Apache::loncommon::content_type($r,'text/html');
335: $r->send_http_header;
336:
337: my $formaction='/priv'.$thisdisfn.'/';
338: $formaction=~s|/+|/|g;
339: &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
340:
341: &Apache::lonhtmlcommon::clear_breadcrumbs();
342: &Apache::lonhtmlcommon::add_breadcrumb({
343: 'text' => 'Authoring Space',
344: 'href' => &Apache::loncommon::authorspace($formaction),
345: });
346: # breadcrumbs (and tools) will be created
347: # in start_page->bodytag->innerregister
348:
349: $env{'request.noversionuri'}=$formaction;
350: $r->print(&Apache::loncommon::start_page('Authoring Space'));
351:
352: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
353: my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
354: my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author'); #expressed in MB
355: $disk_quota = 1024 * $disk_quota; # convert from MB to kB
356:
357: $r->print(&Apache::loncommon::head_subbox(
358: '<div style="float:right;padding-top:0;margin-top;0">'
359: .&Apache::lonhtmlcommon::display_usage($current_disk_usage,
360: $disk_quota,'authoring')
361: .'</div>'
362: .&Apache::loncommon::CSTR_pageheader()));
363:
364: my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
365: my $doctitle = 'LON-CAPA '.&mt('Authoring Space');
366: my $newname = &mt('New Name');
367: my $pubdirscript=(<<ENDPUBDIRSCRIPT);
368: <script type="text/javascript">
369: top.document.title = '$esc_thisdisfn/ - $doctitle';
370: // Store directory location for menu bar to find
371:
372: parent.lastknownpriv='/priv$esc_thisdisfn/';
373:
374: // Confirmation dialogues
375:
376: function currdiract(theform) {
377: if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
378: document.publishdir.filename.value = theform.filename.value;
379: document.publishdir.submit();
380: }
381: if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editmeta') {
382: top.location=theform.filename.value+'default.meta'
383: }
384: if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
385: document.printdir.postdata.value=theform.filename.value
386: document.printdir.submit();
387: }
388: if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
389: var delform = document.delresource
390: delform.filename.value = theform.filename.value
391: delform.submit()
392: }
393: }
394:
395: function checkUpload(theform) {
396: if (theform.file == '') {
397: alert("Please use 'Browse..' to choose a file first, before uploading")
398: return
399: }
400: theform.submit()
401: }
402:
403: function SetPubDir(theform,printForm) {
404: if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
405: top.location = theform.openname.value
406: return
407: }
408: if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
409: theform.submit();
410: }
411: if (theform.diraction.options[theform.diraction.selectedIndex].value == "editmeta") {
412: top.location=theform.filename.value+'default.meta'
413: }
414: if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
415: theform.action = '/adm/printout'
416: theform.postdata.value = theform.filename.value
417: theform.submit()
418: }
419: if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
420: var delform = document.delresource
421: delform.filename.value = theform.filename.value
422: delform.submit()
423: }
424: return
425: }
426: function SetResChoice(theform) {
427: var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
428: if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
429: changename(theform,activity)
430: }
431: if (activity == 'publish') {
432: var pubform = document.pubresource
433: pubform.filename.value = theform.filename.value
434: pubform.submit()
435: }
436: if (activity == 'delete') {
437: var delform = document.delresource
438: delform.filename.value = theform.filename.value
439: delform.submit()
440: }
441: if (activity == 'obsolete') {
442: var pubform = document.pubresource
443: pubform.filename.value = theform.filename.value
444: pubform.makeobsolete.value=1;
445: pubform.submit()
446: }
447: if (activity == 'print') {
448: document.printresource.postdata.value = theform.filename.value
449: document.printresource.submit()
450: }
451: if (activity == 'retrieve') {
452: document.retrieveres.filename.value = theform.filename.value
453: document.retrieveres.submit()
454: }
455: if (activity == 'cleanup') {
456: document.cleanup.filename.value = theform.filename.value
457: document.cleanup.submit()
458: }
459: return
460: }
461: function changename(theform,activity) {
462: var oldname=theform.dispfilename.value;
463: var newname=prompt('$newname',oldname);
464: if (newname == "" || !newname || newname == oldname) {
465: return
466: }
467: document.moveresource.newfilename.value = newname
468: document.moveresource.filename.value = theform.filename.value
469: document.moveresource.action.value = activity
470: document.moveresource.submit();
471: }
472: </script>
473: ENDPUBDIRSCRIPT
474: $r->print($pubdirscript);
475: }
476:
477: sub dircontrols {
478: my ($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota) = @_;
479: my %lt=&Apache::lonlocal::texthash(
480: cnpd => 'Cannot publish directory',
481: cnrd => 'Cannot retrieve directory',
482: mcdi => 'Must create new subdirectory inside a directory',
483: pubr => 'Publish this Resource',
484: pubd => 'Publish this Directory',
485: dedr => 'Delete Directory',
486: rtrv => 'Retrieve Old Version',
487: list => 'List Directory',
488: uplo => 'Upload file',
489: dele => 'Delete',
490: edit => 'Edit Metadata',
491: sela => 'Select Action',
492: nfil => 'New file',
493: nhtm => 'New HTML file',
494: nprb => 'New problem',
495: npag => 'New assembled page',
496: nseq => 'New assembled sequence',
497: ncrf => 'New custom rights file',
498: nsty => 'New style file',
499: nlib => 'New library file',
500: nbt => 'New bridgetask file',
501: nsub => 'New subdirectory',
502: renm => 'Rename current file to',
503: move => 'Move current file to',
504: copy => 'Copy current file to',
505: type => 'Type Name Here',
506: go => 'Go',
507: prnt => 'Print contents of directory',
508: crea => 'Create a new directory or LON-CAPA document',
509: acti => 'Actions for current directory',
510: updc => 'Upload a new document',
511: pick => 'Please select an action to perform using the new filename',
512: );
513: my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
514: # Calculate free space in bytes.
515: # $disk_quota is in MB and $current_disk_usage is in kB
516: my $free_space = 1024 * ((1024 * $disk_quota) - $current_disk_usage);
517: $r->print(<<END);
518: <div class="LC_columnSection">
519: <div>
520: <form name="curractions" method="post" action="">
521: <fieldset>
522: <legend>$lt{'acti'}</legend>
523: <select name="dirtask" onchange="currdiract(this.form)">
524: <option>$lt{'sela'}</option>
525: <option value="publish">$lt{'pubd'}</option>
526: <option value="editmeta">$lt{'edit'}</option>
527: <option value="printdir">$lt{'prnt'}</option>
528: <option value="delete">$lt{'dedr'}</option>
529: </select>
530: <input type="hidden" name="filename" value="/priv$thisdisfn/" />
531: </fieldset>
532: </form>
533: <form name="publishdir" method="post" action="/adm/publish">
534: <input type="hidden" name="pubrec" value="" />
535: <input type="hidden" name="filename" value="" />
536: </form>
537: <form name="printdir" method="post" action="/adm/printout">
538: <input type="hidden" name="postdata" value="" />
539: </form>
540: </div>
541:
542: <div style="padding-bottom: 2px">
543: <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload">
544: <fieldset>
545: <legend>$lt{'updc'}</legend>
546: <input type="hidden" name="filename" value="/priv$thisdisfn/" />
547: <input type="file" name="upfile" class="LC_flUpload" size="20" />
548: <input type="hidden" id="LC_free_space" value="$free_space" />
549: <input type="button" value="$lt{'uplo'}" onclick="checkUpload(this.form)" />
550: </fieldset>
551: </form>
552: </div>
553:
554: <div>
555: <form name="fileaction" method="post" action="/adm/cfile">
556: <fieldset>
557: <legend>$lt{'crea'}</legend>
558: <span class="LC_nobreak">
559: <input type="hidden" name="filename" value="/priv$thisdisfn/" />
560: <script type="text/javascript">
561: function validate_go() {
562: var selected = document.fileaction.action.selectedIndex;
563: if (selected == 0) {
564: alert('$lt{'pick'}');
565: } else {
566: document.fileaction.submit();
567: }
568: }
569: </script>
570: <select name="action">
571: <option value="none">$lt{'sela'}</option>
572: <option value="newfile">$lt{'nfil'}:</option>
573: <option value="newhtmlfile">$lt{'nhtm'}:</option>
574: <option value="newproblemfile">$lt{'nprb'}:</option>
575: <option value="newpagefile">$lt{'npag'}:</option>
576: <option value="newsequencefile">$lt{'nseq'}:</option>
577: <option value="newrightsfile">$lt{'ncrf'}:</option>
578: <option value="newstyfile">$lt{'nsty'}:</option>
579: <option value="newtaskfile">$lt{'nbt'}:</option>
580: <option value="newlibraryfile">$lt{'nlib'}:</option>
581: <option value="newdir">$lt{'nsub'}:</option>
582: </select> <input type="text" name="newfilename" placeholder="$lt{'type'}" value="" onfocus="if (this.value == is.empty()) this.value=''" /> <input type="button" value="Go" onclick="validate_go();" />
583: <br />
584: <span>Quickactions:
585: <input type="hidden" name="mode"/>
586: <a href="javascript:void(0)" onclick="javascript:validate_action('blank')">
587: <img src="/adm/lonIcons/unknown.gif" title="Create blank problem file"></a>
588: <a href="javascript:void(0)" onclick="javascript:validate_action('problemtempl')">
589: <img src="/adm/lonIcons/problem.gif" title="Create new problem from template"></a>
590: <a href="javascript:void(0)" onclick="javascript:validate_action('blankhtml')">
591: <img src="/adm/lonIcons/html.gif" title="Create new blank HTML file"></a>
592: <a href="javascript:void(0)" onclick="javascript:validate_action('folder')">
593: <img src="/adm/lonIcons/navmap.folder.closed.gif" title="Create new subdirectory"></a>
594: </span>
595: <script type="text/javascript">
596: function validate_action(action){
597:
598: if (document.getElementsByName(\'newfilename\')[0].value != \'\'){
599: if (action == "blank") {
600: document.fileaction.action.value=\'newproblemfile\';
601: document.fileaction.mode.value=\'blank\';
602: } else if (action == "problemtempl") {
603: document.fileaction.action.value=\'newproblemfile\';
604: validate_go();
605: } else if (action == "blankhtml") {
606: document.fileaction.action.value=\'newhtmlfile\';
607: validate_go();
608: } else if (action == "folder") {
609: document.fileaction.action.value=\'newdir\';
610: document.fileaction.mode.value=\'folder\';
611: }
612: fileaction.submit();
613: } else {
614: alert(\'Please specify file name.\');
615: // TODO: ask for filename? if so, do some refactoring
616:
617: }
618: }
619: </script>
620: </span>
621: </fieldset>
622: </form>
623: </div>
624: </div>
625: END
626: }
627:
628: sub resourceactions {
629: my ($r,$uname,$udom,$thisdisfn) = @_;
630: $r->print(<<END);
631: <form name="moveresource" action="/adm/cfile" method="post">
632: <input type="hidden" name="filename" value="" />
633: <input type="hidden" name="newfilename" value="" />
634: <input type="hidden" name="action" value="" />
635: </form>
636: <form name="delresource" action="/adm/cfile" method="post">
637: <input type="hidden" name="filename" value="" />
638: <input type="hidden" name="action" value="delete" />
639: </form>
640: <form name="pubresource" action="/adm/publish" method="post">
641: <input type="hidden" name="filename" value="" />
642: <input type="hidden" name="makeobsolete" value="0" />
643: </form>
644: <form name="printresource" action="/adm/printout" method="post">
645: <input type="hidden" name="postdata" value="" />
646: </form>
647: <form name="retrieveres" action="/adm/retrieve" method="post">
648: <input type="hidden" name="filename" value="" />
649: </form>
650: <form name="cleanup" action="/adm/cleanup" method="post">
651: <input type="hidden" name="filename" value="" />
652: </form>
653: END
654: }
655:
656: #
657: # Get the title string or "[untitled]" if the file has no title metadata:
658: # Without the latter substitution, it's impossible to examine metadata for
659: # untitled resources. Resources may be legitimately untitled, to prevent
660: # searches from locating them.
661: #
662: # $str = getTitleString($fullname);
663: # $fullname - Fully qualified filename to check.
664: #
665: sub getTitleString {
666: my $fullname = shift;
667: my $title = &Apache::lonnet::metadata($fullname, 'title');
668:
669: unless ($title) {
670: $title = "[".&mt('untitled')."]";
671: }
672: return $title;
673: }
674:
675: sub getCopyRightString {
676: my $fullname = shift;
677: return &Apache::lonnet::metadata($fullname, 'copyright');
678: }
679:
680: sub getSourceRightString {
681: my $fullname = shift;
682: return &Apache::lonnet::metadata($fullname, 'sourceavail');
683: }
684: #
685: # Put out a directory table row:
686: # putdirectory(r, base, here, dirname, modtime, targetdir, bombs, numdir)
687: # r - Apache request object.
688: # reqfile - File in request.
689: # here - Where we are in directory tree.
690: # dirname - Name of directory special file.
691: # modtime - Encoded modification time.
692: # targetdir - Publication target directory.
693: # bombs - Reference to hash of URLs with runtime error messages.
694: # numdir - Reference to scalar used to track number of sub-directories
695: # in directory (used in form name for each "actions" dropdown).
696: #
697: sub putdirectory {
698: my ($r, $reqfile, $here, $dirname, $modtime, $targetdir, $bombs, $numdir) = @_;
699:
700: # construct the display filename: the directory name unless ..:
701:
702: my $actionitem;
703:
704: my $disfilename = $dirname;
705: # Don't display directory itself, and there is no way up from root directory
706: unless ((($dirname eq '..') && ($reqfile=~/^\/[^\/]+\/[^\/]+$/)) || ($dirname eq '.')) {
707: my $kaputt=0;
708: if (ref($bombs) eq 'HASH') {
709: foreach my $key (keys(%{$bombs})) {
710: my $currentdir = &Apache::lonnet::declutter("$targetdir/$disfilename");
711: if (($key) =~ m{^\Q$currentdir\E/}) { $kaputt=1; last; }
712: }
713: }
714: #
715: # Get the metadata from that directory's default.meta to display titles
716: #
717: %Apache::lonpublisher::metadatafields=();
718: %Apache::lonpublisher::metadatakeys=();
719: &Apache::lonpublisher::metaeval(
720: &Apache::lonnet::getfile($r->dir_config('lonDocRoot').$here.'/'.$dirname.'/default.meta')
721: );
722: if ($dirname eq '..') {
723: $actionitem = &mt('Go to ...');
724: $disfilename = '<i>'.&mt('Parent Directory').'</i>';
725: } else {
726: $actionitem =
727: '<form name="dirselect_'.$$numdir.
728: '" action="/adm/publish">'.
729: '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
730: '<option selected="selected">'.&mt('Select action').'</option>'.
731: '<option value="open">'.&mt('Open').'</option>'.
732: '<option value="publish">'.&mt('Publish').'</option>'.
733: '<option value="editmeta">'.&mt('Edit Metadata').'</option>'.
734: '<option value="printdir">'.&mt('Print directory').'</option>'.
735: '<option value="delete">'.&mt('Delete directory').'</option>'.
736: '</select>'.
737: '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" />'.
738: '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
739: '<input type="hidden" name="postdata" value="" />'.
740: '</form>';
741: $$numdir ++;
742: }
743: $r->print('<tr class="LC_browser_folder">'.
744: '<td><img src="'.
745: $Apache::lonnet::perlvar{'lonIconsURL'}.'/navmap.folder.closed.gif" alt="folder" /></td>'.
746: '<td>'.$actionitem.'</td>'.
747: '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/">'.
748: $disfilename.'</a></span></td>'.
749: '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($targetdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
750: if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
751: $r->print(' <i>'.
752: $Apache::lonpublisher::metadatafields{'subject'}.
753: '</i> ');
754: }
755: $r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
756: '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
757: '<td></td>'.
758: "</tr>\n");
759: }
760: return;
761: }
762:
763: sub getTitle {
764: my ($resdir, $targetdir, $filename, $linkfilename, $meta_same, $bombs) = @_;
765: my $title='';
766: my $titleString = &getTitleString($targetdir.'/'.$filename);
767: if (-e $resdir.'/'.$filename) {
768: $title = '<a href="'.$targetdir.'/'.$filename.
769: '.meta" target="cat">'.$titleString.'</a>';
770: if (!$meta_same) {
771: $title = &mt('Metadata Modified').'<br />'.$title.
772: '<br />'.
773: &Apache::loncommon::modal_link(
774: '/adm/diff?filename='.$linkfilename.'.meta'.'&versiontwo=priv',
775: &mt('Metadata Diffs'),600,500);
776: $title.="\n".'<br />'.
777: &Apache::loncommon::modal_link(
778: '/adm/retrieve?filename='.$linkfilename.'.meta&inhibitmenu=yes&add_modal=yes',
779: &mt('Retrieve Metadata'),600,500);
780: }
781: }
782: # Allow editing metadata of published and unpublished resources
783: $title .= "\n".'<br />' if ($title);
784: $title .= '<a href="'.$linkfilename.'.meta">'.
785: ($$bombs{&Apache::lonnet::declutter($targetdir.'/'.$filename)}?
786: '<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':
787: &mt('Edit Metadata')).
788: '</a>';
789:
790: return ($title, $titleString);
791: }
792:
793:
794: sub isMetaSame {
795: my ($cstr_dir, $resdir, $filename) = @_;
796: my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
797: my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
798: return (&Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
799: $cstr_dir.'/'.$filename.'.meta') && $meta_rmtime < $meta_cmtime)
800: ? 0 : 1;
801: }
802:
803:
804: sub getStatus {
805: my ($resdir, $targetdir, $cstr_dir, $filename,
806: $linkfilename, $cmtime, $meta_same) = @_;
807: my $pubstatus = 'unpublished';
808: my $status = &mt('Unpublished');
809:
810: if (-e $resdir.'/'.$filename) {
811: my $same = 0;
812: if ((stat($resdir.'/'.$filename))[9] >= $cmtime) {
813: $same = 1;
814: } else {
815: if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
816: $cstr_dir.'/'.$filename)) {
817: $same = 0;
818: } else {
819: $same = 1;
820: }
821: }
822:
823: my $rights_status =
824: &mt(&getCopyRightString($targetdir.'/'.$filename)).', ';
825:
826: my %lt_SourceRight = &Apache::lonlocal::texthash(
827: 'open' => 'Source: open',
828: 'closed' => 'Source: closed',
829: );
830: $rights_status .=
831: $lt_SourceRight{&getSourceRightString($targetdir.'/'.$filename)};
832:
833: if ($same) {
834: if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
835: $pubstatus = 'obsolete';
836: $status=&mt('Obsolete');
837: } else {
838: if (!$meta_same) {
839: $pubstatus = 'metamodified';
840: } else {
841: $pubstatus = 'published';
842: }
843: $status=&mt('Published').
844: '<br />'. $rights_status;
845: }
846: } else {
847: $pubstatus = 'modified';
848: $status=&mt('Modified').
849: '<br />'. $rights_status;
850: if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
851: $status.='<br />'.
852: &Apache::loncommon::modal_link(
853: '/adm/diff?filename='.$linkfilename.'&versiontwo=priv',
854: &mt('Diffs'),600,500);
855: }
856: }
857:
858: $status.="\n".'<br />'.
859: &Apache::loncommon::modal_link(
860: '/adm/retrieve?filename='.$linkfilename.'&inhibitmenu=yes&add_modal=yes',&mt('Retrieve'),600,500);
861: }
862:
863: return ($status, $pubstatus);
864: }
865:
866:
867: #
868: # Put a table row for a file resource.
869: #
870: sub putresource {
871: my ($r, $udom, $uname, $filename, $thisdisfn, $resdir, $targetdir,
872: $linkdir, $cmtime, $size, $numres, $linkfilename, $title,
873: $status, $pubstatus, $editors) = @_;
874: &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
875:
876: my $editlink='';
877: my $editlink2='';
878: if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty|txt|css|js)$/) {
879: $editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&problemmode=edit">'.&mt('Edit').'</a>)';
880: }
881: if ($filename=~/$LONCAPA::assess_re/) {
882: if ($editors->{'xml'}) {
883: $editlink=' (<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&problemmode=editxml">'.&mt('EditXML').'</a>)';
884: }
885: if ($editors->{'edit'}) {
886: $editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&problemmode=edit">'.&mt('Edit').'</a>)';
887: }
888: }
889: if ($filename=~/\.(xml|html|htm|xhtml|xhtm)$/ || $filename=~/$LONCAPA::assess_re/) {
890: if (($editors->{'daxe'}) &&
891: ($env{'browser.type'} ne 'explorer' || $env{'browser.version'} > 9)) {
892: my $daxeurl = '/daxepage'.$linkdir.'/'.$filename;
893: $editlink .= ' (<a href="'.$daxeurl.'">Daxe</a>)';
894: }
895: }
896: if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
897: $editlink.=' (<a href="/adm/cleanup?filename='.$linkfilename.'">'.&mt('Clean Up').')</a>';
898: }
899: if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
900: $editlink=' (<a href="/adm/cfile?decompress='.$linkfilename.'">'.&mt('Decompress').'</a>)';
901: }
902: my $publish_button = (-e $resdir.'/'.$filename) ? &mt('Re-publish') : &mt('Publish');
903: my $pub_select = '';
904: &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
905: $r->print(&Apache::loncommon::start_data_table_row().
906: '<td>'.($filename=~/[\#\~]$/?' ':
907: '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
908: '<td>'.$pub_select.'</td>'.
909: '<td><span class="LC_filename">'.
910: '<a href="'.$linkdir.'/'.$filename.'">'.
911: $filename.'</a></span>'.$editlink2.$editlink.
912: '</td>'.
913: '<td>'.$title.'</td>'.
914: '<td class="LC_browser_file_'.$pubstatus.'"> </td>'. # Display publication status
915: '<td>'.$status.'</td>'.
916: '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
917: '<td>'.sprintf("%.1f",$size).'</td>'.
918: &Apache::loncommon::end_data_table_row()
919: );
920: return;
921: }
922:
923: sub create_pubselect {
924: my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
925: $$pub_select = '
926: <form name="resselect_'.$$numres.'" action="">
927: <select name="reschoice" onchange="SetResChoice(this.form)">
928: <option>'.&mt('Select action').'</option>'.
929: '<option value="copy">'.&mt('Copy').'</option>';
930: if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
931: $$pub_select .=
932: '<option value="rename">'.&mt('Rename').'</option>'.
933: '<option value="move">'.&mt('Move').'</option>'.
934: '<option value="delete">'.&mt('Delete').'</option>';
935: } else {
936: $$pub_select .= '
937: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
938: }
939: # check for versions
940: my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
941: if ($versions > 0) {
942: $$pub_select .='
943: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
944: }
945: $$pub_select .= '
946: <option value="publish">'.$publish_button.'</option>'.
947: '<option value="cleanup">'.&mt('Clean up').'</option>'.
948: '<option value="print">'.&mt('Print').'</option>'.
949: '</select>
950: <input type="hidden" name="filename" value="/priv'.
951: &HTML::Entities::encode($thisdisfn.'/'.$filename,'<>&"').'" />
952: <input type="hidden" name="dispfilename" value="'.
953: &HTML::Entities::encode($filename).'" /></form>';
954: $$numres ++;
955: }
956:
957: sub check_for_versions {
958: my ($r,$fn,$udom,$uname) = @_;
959: my $versions = 0;
960: my $docroot=$r->dir_config('lonDocRoot');
961: my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
962: my $resdir=$resfn;
963: $resdir=~s/\/[^\/]+$/\//;
964: $fn=~/\/([^\/]+)\.(\w+)$/;
965: my $main=$1;
966: my $suffix=$2;
967: opendir(DIR,$resdir);
968: while (my $filename=readdir(DIR)) {
969: if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
970: $versions ++;
971: }
972: }
973: closedir(DIR);
974: return $versions;
975: }
976:
977: 1;
978: __END__
979:
980:
981: =head1 NAME
982:
983: Apache::lonpubdir - Authoring space directory lister
984:
985: =head1 SYNOPSIS
986:
987: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
988:
989: <LocationMatch "^/+priv.*/$">
990: PerlAccessHandler Apache::loncacc
991: SetHandler perl-script
992: PerlHandler Apache::lonpubdir
993: ErrorDocument 403 /adm/login
994: ErrorDocument 404 /adm/notfound.html
995: ErrorDocument 406 /adm/unauthorized.html
996: ErrorDocument 500 /adm/errorhandler
997: </LocationMatch>
998:
999: <Location /adm/pubdir>
1000: PerlAccessHandler Apache::lonacc
1001: SetHandler perl-script
1002: PerlHandler Apache::lonpubdir
1003: ErrorDocument 403 /adm/login
1004: ErrorDocument 404 /adm/notfound.html
1005: ErrorDocument 406 /adm/unauthorized.html
1006: ErrorDocument 500 /adm/errorhandler
1007: </Location>
1008:
1009: =head1 INTRODUCTION
1010:
1011: This module publishes a directory of files.
1012:
1013: This is part of the LearningOnline Network with CAPA project
1014: described at http://www.lon-capa.org.
1015:
1016: =head1 HANDLER SUBROUTINE
1017:
1018: This routine is called by Apache and mod_perl.
1019:
1020: =over 4
1021:
1022: =item *
1023:
1024: read in information
1025:
1026: =item *
1027:
1028: start page output
1029:
1030: =item *
1031:
1032: run through list of files and attempt to publish unhidden files
1033:
1034: =back
1035:
1036: =head1 SUBROUTINES:
1037:
1038: =over
1039:
1040: =item startpage($r, $uame, $udom, $thisdisfn)
1041:
1042: Output the header of the page. This includes:
1043: - The HTML header
1044: - The H1/H3 stuff which includes the directory.
1045:
1046: startpage($r, $uame, $udom, $thisdisfn);
1047: $r - The apache request object.
1048: $uname - User name.
1049: $udom - Domain name the user is logged in under.
1050: $thisdisfn - Displayable version of the filename.
1051:
1052: =item getTitleString($fullname)
1053:
1054: Get the title string or "[untitled]" if the file has no title metadata:
1055: Without the latter substitution, it's impossible to examine metadata for
1056: untitled resources. Resources may be legitimately untitled, to prevent
1057: searches from locating them.
1058:
1059: $str = getTitleString($fullname);
1060: $fullname - Fully qualified filename to check.
1061:
1062: =item putdirectory($r, $base, $here, $dirname, $modtime, $targetdir, $bombs,
1063: $numdir)
1064:
1065: Put out a directory table row:
1066:
1067: $r - Apache request object.
1068: $reqfile - File in request.
1069: $here - Where we are in directory tree.
1070: $dirname - Name of directory special file.
1071: $modtime - Encoded modification time.
1072: targetdir - Publication target directory.
1073: bombs - Reference to hash of URLs with runtime error messages.
1074: numdir - Reference to scalar used to track number of sub-directories
1075: in directory (used in form name for each "actions" dropdown).
1076:
1077: =back
1078:
1079: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>