1: # The LearningOnline Network with CAPA
2: # Handler to rename files, etc, in construction space
3: #
4: # This file responds to the various buttons and events
5: # in the top frame of the construction space directory.
6: # Each event is processed in two phases. The first phase
7: # presents a page that describes the proposed action to the user
8: # and requests confirmation. The second phase commits the action
9: # and displays a page showing the results of the action.
10: #
11: #
12: # $Id: loncfile.pm,v 1.128 2024/05/13 13:55:50 raeburn Exp $
13: #
14: # Copyright Michigan State University Board of Trustees
15: #
16: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
17: #
18: # LON-CAPA is free software; you can redistribute it and/or modify
19: # it under the terms of the GNU General Public License as published by
20: # the Free Software Foundation; either version 2 of the License, or
21: # (at your option) any later version.
22: #
23: # LON-CAPA is distributed in the hope that it will be useful,
24: # but WITHOUT ANY WARRANTY; without even the implied warranty of
25: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26: # GNU General Public License for more details.
27: #
28: # You should have received a copy of the GNU General Public License
29: # along with LON-CAPA; if not, write to the Free Software
30: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31: #
32: # /home/httpd/html/adm/gpl.txt
33: #
34: # http://www.lon-capa.org/
35: #
36: =pod
37:
38: =head1 NAME
39:
40: Apache::loncfile - Authoring space file management.
41:
42: =head1 SYNOPSIS
43:
44: Content handler for buttons on the top frame of the construction space
45: directory.
46:
47: =head1 INTRODUCTION
48:
49: loncfile is invoked when buttons in the top frame of the construction
50: space directory listing are clicked. All operations proceed in two phases.
51: The first phase describes to the user exactly what will be done. If the user
52: confirms the operation, the second phase commits the operation and indicates
53: completion. When the user dismisses the output of phase2, they are returned to
54: an "appropriate" directory listing in general.
55:
56: This is part of the LearningOnline Network with CAPA project
57: described at http://www.lon-capa.org.
58:
59: =head2 Subroutines
60:
61: =cut
62:
63: package Apache::loncfile;
64:
65: use strict;
66: use Apache::File;
67: use File::Basename;
68: use File::Copy;
69: use HTML::Entities();
70: use Apache::Constants qw(:common :http :methods);
71: use Apache::lonnet;
72: use Apache::loncommon();
73: use Apache::lonhtmlcommon;
74: use Apache::lonlocal;
75: use LONCAPA qw(:DEFAULT :match);
76:
77: my $DEBUG=0;
78: my $r; # Needs to be global for some stuff RF.
79:
80: =pod
81:
82: =item Debug($request, $message)
83:
84: If debugging is enabled puts out a debugging message determined by the
85: caller. The debug message goes to the Apache error log file. Debugging
86: is enabled by setting the module global DEBUG variable to nonzero (TRUE).
87:
88: Parameters:
89:
90: =over 4
91:
92: =item $request - The current request operation.
93:
94: =item $message - The message to put in the log file.
95:
96: =back
97:
98: Returns:
99: nothing.
100:
101: =cut
102:
103: sub Debug {
104: # Put out the indicated message but only if DEBUG is true.
105: if ($DEBUG) {
106: my ($r,$message) = @_;
107: $r->log_reason($message);
108: }
109: }
110:
111: sub done {
112: my ($destfn) = @_;
113: return
114: '<p>'
115: .&Apache::lonhtmlcommon::confirm_success(&mt("Done"))
116: .'<br /><a href="'.&url($destfn).'">'.&mt("Continue").'</a>'
117: .'<script type="text/javascript">'
118: .'location.href="'.&url($destfn,'js').'";'
119: .'</script>'
120: .'</p>';
121: }
122:
123: =pod
124:
125: =item URLToPath($url)
126:
127: Convert a URL to a file system path.
128:
129: In order to manipulate the construction space objects, it is necessary
130: to access url identified objects a filespace objects. This function
131: translates a construction space URL to a file system path.
132: Parameters:
133:
134: =over 4
135:
136: =item Url - string [in] The url to convert.
137:
138: =back
139:
140: Returns:
141:
142: =over 4
143:
144: =item The corresponding file system path.
145:
146: =back
147:
148: Global References
149:
150: =over 4
151:
152: =item $r - Request object [in] Referenced in the &Debug calls.
153:
154: =back
155:
156: =cut
157:
158: sub URLToPath {
159: my $Url = shift;
160: &Debug($r, "UrlToPath got: $Url");
161: $Url=~ s{^https?\://[^/]+}{};
162: $Url=~ s{//+}{/}g;
163: $Url=~ s{^/}{};
164: $Url=$Apache::lonnet::perlvar{'lonDocRoot'}."/$Url";
165: &Debug($r, "Returning $Url \n");
166: return $Url;
167: }
168:
169: sub url {
170: my ($fn,$context) = @_;
171: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
172: $fn=~ s/^\Q$londocroot\E//;
173: $fn=~s{/\./}{/}g;
174: if ($context eq 'js') {
175: &js_escape(\$fn);
176: } else {
177: $fn=&HTML::Entities::encode($fn,'\'<>"&');
178: }
179: return $fn;
180: }
181:
182: sub display {
183: my $fn=shift;
184: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
185: $fn=~s/^\Q$londocroot\E//;
186: $fn=~s{/\./}{/}g;
187: return '<span class="LC_filename">'.$fn.'</span>';
188: }
189:
190:
191: # see if the file is
192: # a) published (return 0 if not)
193: # b) if, so obsolete (return 0 if not)
194:
195: sub obsolete_unpub {
196: my ($user,$domain,$construct)=@_;
197: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
198: my $published=$construct;
199: $published=~s{^\Q$londocroot/priv/\E}{$londocroot/res/};
200: if (-e $published) {
201: if (&Apache::lonnet::metadata($published,'obsolete')) {
202: return 1;
203: }
204: return 0;
205: } else {
206: return 1;
207: }
208: }
209:
210: # see if directory is empty
211: # ignores any .meta, .save, .bak, and .log files created for a previously
212: # published file, which has since been marked obsolete and deleted.
213: # ignores a .DS_Store file put there when viewing directory via webDAV on MacOS.
214: sub empty_directory {
215: my ($dirname,$phase) = @_;
216: if (opendir DIR, $dirname) {
217: my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and ..
218: if (@files) {
219: my @orphans = grep(/\.(meta|save|log|bak|DS_Store)$/,@files);
220: if (scalar(@files) - scalar(@orphans) > 0) {
221: return 0;
222: } else {
223: if (($phase eq 'Delete2') && (@orphans > 0)) {
224: foreach my $file (@orphans) {
225: if ($file =~ /\.(meta|save|log|bak)$/) {
226: unlink($dirname.$file);
227: }
228: }
229: }
230: }
231: }
232: closedir(DIR);
233: return 1;
234: }
235: return 0;
236: }
237:
238: =pod
239:
240: =item exists($user, $domain, $file)
241:
242: Determine if a resource filename has been published or exists
243: in the construction space.
244:
245: Parameters:
246:
247: =over 4
248:
249: =item $user - string [in] - Name of the user for which to check.
250:
251: =item $domain - string [in] - Name of the domain in which the resource
252: might have been published.
253:
254: =item $file - string [in] - Name of the file.
255:
256: =item $creating - string [in] - optional, type of object being created,
257: either 'directory' or 'file'. Defaults to
258: 'file' if unspecified.
259:
260: =back
261:
262: Returns:
263:
264: =over 4
265:
266: =item string - Either undef, 'warning' or 'error' depending on the
267: type of problem
268:
269: =item string - Either where the resource exists as an html string that can
270: be embedded in a dialog or an empty string if the resource
271: does not exist.
272:
273: =back
274:
275: =cut
276:
277: sub exists {
278: my ($user, $domain, $construct, $creating) = @_;
279: $creating ||= 'file';
280:
281: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
282: my $published=$construct;
283: $published=~s{^\Q$londocroot/priv/\E}{$londocroot/res/};
284: my ($type,$result);
285: if ( -d $construct ) {
286: return ('error','<p class="LC_error">'.&mt('Error: destination for operation is an existing directory.').'</p>');
287:
288: }
289:
290: if ( -e $published) {
291: if ( -e $construct ) {
292: $type = 'warning';
293: $result.='<p class="LC_warning">'.&mt('Warning: target file exists, and has been published!').'</p>';
294: } else {
295: my $published_type = (-d $published) ? 'directory' : 'file';
296:
297: if ($published_type eq $creating) {
298: $type = 'warning';
299: $result.='<p class="LC_warning">'.&mt("Warning: a published $published_type of this name exists.").'</p>';
300: } else {
301: $type = 'error';
302: $result.='<p class="LC_error">'.&mt("Error: a published $published_type of this name exists.").'</p>';
303: }
304: }
305: } elsif ( -e $construct) {
306: $type = 'warning';
307: $result.='<p class="LC_warning">'.&mt('Warning: target file exists!').'</p>';
308: }
309:
310: return ($type,$result);
311: }
312:
313: =pod
314:
315: =item checksuffix($old, $new)
316:
317: Determine if a resource filename suffix (the stuff after the .) would change
318: as a result of this operation.
319:
320: Parameters:
321:
322: =over 4
323:
324: =item $old = string [in] Previous filename.
325:
326: =item $new = string [in] Resultant filename.
327:
328: =back
329:
330: Returns:
331:
332: =over 4
333:
334: =item Empty string if everything worked.
335:
336: =item String containing an error message if there was a problem.
337:
338: =back
339:
340: =cut
341:
342: sub checksuffix {
343: my ($old,$new) = @_;
344: my $result;
345: my $oldsuffix;
346: my $newsuffix;
347: if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
348: if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
349: if (lc($oldsuffix) ne lc($newsuffix)) {
350: $result.=
351: '<p class="LC_warning">'.&mt('Warning: change of MIME type!').'></p>';
352: }
353: return $result;
354: }
355:
356: sub cleanDest {
357: my ($dest,$subdir,$fn,$uname,$udom)=@_;
358: #remove bad characters
359: my $foundbad=0;
360: my $warnings;
361: my $error='';
362: if ($subdir && $dest =~/\./) {
363: $foundbad=1;
364: $dest=~s/\.//g;
365: }
366: $dest =~ s/(\s+$|^\s+)//g;
367: if ($dest=~/[\#\?&%\":]/) {
368: $foundbad=1;
369: $dest=~s/[\#\?&%\":]//g;
370: }
371: if ($dest=~m|/|) {
372: my ($newpath)=($dest=~m|(.*)/|);
373: ($newpath,$error)=&relativeDest($fn,$newpath,$uname,$udom);
374: if (! -d "$newpath") {
375: $warnings = '<p class="LC_warning">'
376: .&mt("You have requested to create file in directory [_1] which doesn't exist. The requested directory path has been removed from the requested filename."
377: ,&display($newpath))
378: .'</p>';
379: $dest=~s|.*/||;
380: }
381: }
382: if ($dest =~ /\.(\d+)\.(\w+)$/) {
383: $warnings .= '<p class="LC_warning">'
384: .&mt('Bad filename [_1]',&display($dest))
385: .'<br />'
386: .&mt('[_1](name).(number).(extension)[_2] not allowed.','<tt>','</tt>')
387: .'<br />'
388: .&mt('Removing the [_1].number.[_2] from requested filename.','<tt>','</tt>')
389: .'</p>';
390: $dest =~ s/\.(\d+)(\.\w+)$/$2/;
391: }
392: if ($foundbad) {
393: $warnings .= '<p class="LC_warning">'
394: .&mt('Invalid characters in requested name have been removed.')
395: .'</p>';
396: }
397: return ($dest,$error,$warnings);
398: }
399:
400: sub relativeDest {
401: my ($fn,$newfilename,$uname,$udom)=@_;
402: my $error = '';
403: if ($newfilename=~/^\//) {
404: # absolute, simply add path
405: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
406: $newfilename="$londocroot/res/$udom/$uname/";
407: } else {
408: my $dir=$fn;
409: $dir=~s{/[^/]+$}{};
410: $newfilename=$dir.'/'.$newfilename;
411: }
412: $newfilename=~s{//+}{/}g; # remove duplicate /
413: while ($newfilename=~m{/\.\./}) {
414: $newfilename=~ s{/[^/]+/\.\./}{/}g; #remove dir/..
415: }
416: my ($authorname,$authordom)=&Apache::lonnet::constructaccess($newfilename);
417: unless (($authorname) && ($authordom)) {
418: my $otherdir = &display($newfilename);
419: $error = &mt('Access denied to [_1]',$otherdir);
420: }
421: return ($newfilename,$error);
422: }
423:
424: =pod
425:
426: =item CloseForm1($request, $user, $file)
427:
428: Close of a form on the successful completion of phase 1 processing
429:
430: Parameters:
431:
432: =over 4
433:
434: =item $request - Apache Request Object [in] - Apache server request object.
435:
436: =item $cancelurl - the url to go to on cancel.
437:
438: =back
439:
440: =cut
441:
442: sub CloseForm1 {
443: my ($request, $fn) = @_;
444: $request->print('<input type="submit" value="'.&mt('Continue').'" /></form>');
445: $request->print(' <form action="'.&url($fn).'" method="post">'.
446: '<input type="submit" value="'.&mt('Cancel').'" /></form>');
447: }
448:
449:
450: =pod
451:
452: =item CloseForm2($request, $user, $directory)
453:
454: Successfully close off the phase 2 form.
455:
456: Parameters:
457:
458: =over 4
459:
460: =item $request - Apache Request object [in] - The request that is being
461: executed.
462:
463: =item $user - string [in] - Name of the user that is initiating the
464: request.
465:
466: =item $directory - string [in] - Directory in which the operation is
467: being done relative to the top level construction space
468: directory.
469:
470: =back
471:
472: =cut
473:
474: sub CloseForm2 {
475: my ($request, $user, $fn) = @_;
476: $request->print(&done($fn));
477: }
478:
479: =pod
480:
481: =item Rename1($request, $filename, $user, $domain, $dir)
482:
483: Perform phase 1 processing of the file rename operation.
484:
485: Parameters:
486:
487: =over 4
488:
489: =item $request - Apache Request Object [in] The request object for the
490: current request.
491:
492: =item $filename - The filename relative to construction space.
493:
494: =item $user - Name of the user making the request.
495:
496: =item $domain - User login domain.
497:
498: =item $dir - Directory specification of the path to the file.
499:
500: =back
501:
502: Side effects:
503:
504: =over 4
505:
506: =item A new form is displayed prompting for confirmation. The newfilename
507: hidden field of this form is loaded with
508: new filename relative to the current directory ($dir).
509:
510: =back
511:
512: =cut
513:
514: sub Rename1 {
515: my ($request, $user, $domain, $fn, $newfilename, $style) = @_;
516:
517: if(-e $fn) {
518: if($newfilename) {
519: # is dest a dir
520: if ($style eq 'move') {
521: if (-d $newfilename) {
522: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
523: }
524: }
525: if ($newfilename =~ m|/[^\.]+$|) {
526: #no extension add on original extension
527: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
528: $newfilename.='.'.$1;
529: }
530: }
531: $request->print(&checksuffix($fn, $newfilename));
532: #renaming a dir, delete the trailing /
533: #remove second to last element for current dir
534: if (-d $fn) {
535: $newfilename=~/\.(\w+)$/;
536: if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
537: $request->print('<p><span class="LC_error">'.
538: &mt('Cannot change MIME type of a directory.').
539: '</span>'.
540: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>');
541: return;
542: }
543: $newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
544: }
545: $newfilename=~s://+:/:g; # remove duplicate /
546: while ($newfilename=~m:/\.\./:) {
547: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
548: }
549: my ($type, $return)=&exists($user, $domain, $newfilename);
550: $request->print($return);
551: if ($type eq 'error') {
552: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
553: return;
554: }
555: unless (&obsolete_unpub($user,$domain,$fn)) {
556: $request->print('<p><span class="LC_error">'
557: .&mt('Cannot rename or move non-obsolete published file.')
558: .'</span><br />'
559: .'<a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
560: );
561: return;
562: }
563: my $action;
564: if ($style eq 'rename') {
565: $action='Rename';
566: } else {
567: $action='Move';
568: }
569: $request->print('<input type="hidden" name="newfilename" value="'
570: .$newfilename.'" />'
571: .'<p>'
572: .&mt($action.' [_1] to [_2]?',
573: &display($fn),
574: &display($newfilename))
575: .'</p>'
576: );
577: &CloseForm1($request, $fn);
578: } else {
579: $request->print('<p class="LC_error">'.&mt('No new filename specified.').'</p></form>');
580: return;
581: }
582: } else {
583: $request->print('<p class="LC_error">'
584: .&mt('No such file: [_1]',
585: &display($fn))
586: .'</p></form>'
587: );
588: return;
589: }
590:
591: }
592:
593: =pod
594:
595: =item Delete1
596:
597: Performs phase 1 processing of the delete operation. In phase one
598: we just check to be sure the file exists.
599:
600: Parameters:
601:
602: =over 4
603:
604: =item $request - Apache Request Object [in] request object for the current
605: request.
606:
607: =item $user - string [in] Name of the user initiating the request.
608:
609: =item $domain - string [in] Domain the initiating user is logged in as
610:
611: =item $filename - string [in] Source filename.
612:
613: =back
614:
615: =cut
616:
617: sub Delete1 {
618: my ($request, $user, $domain, $fn) = @_;
619:
620: if( -e $fn) {
621: $request->print('<input type="hidden" name="newfilename" value="'.
622: $fn.'" />');
623: if (-d $fn) {
624: unless (&empty_directory($fn,'Delete1')) {
625: $request->print('<p>'
626: .'<span class="LC_error">'
627: .&mt('Only empty directories may be deleted.')
628: .'</span><br />'
629: .&mt('You must delete the contents of the directory first.')
630: .'</p>'
631: .'<p><a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
632: );
633: return;
634: }
635: } else {
636: unless (&obsolete_unpub($user,$domain,$fn)) {
637: $request->print('<p><span class="LC_error">'
638: .&mt('Cannot delete non-obsolete published file.')
639: .'</span><br />'
640: .'<a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
641: );
642: return;
643: }
644: }
645: $request->print('<p>'
646: .&mt('Delete [_1]?',
647: &display($fn))
648: .'</p>'
649: );
650: &CloseForm1($request, $fn);
651: } else {
652: $request->print('<p class="LC_error">'
653: .&mt('No such file: [_1]',
654: &display($fn))
655: .'</p></form>'
656: );
657: }
658: }
659:
660: =pod
661:
662: =item Copy1($request, $user, $domain, $filename, $newfilename)
663:
664: Performs phase 1 processing of the construction space copy command.
665: Ensure that the source file exists. Ensure that a destination exists,
666: also warn if the destination already exists.
667:
668: Parameters:
669:
670: =over 4
671:
672: =item $request - Apache Request Object [in] request object for the current
673: request.
674:
675: =item $user - string [in] Name of the user initiating the request.
676:
677: =item $domain - string [in] Domain the initiating user is logged in as
678:
679: =item $fn - string [in] Source filename.
680:
681: =item $newfilename-string [in] Destination filename.
682:
683: =back
684:
685: =cut
686:
687: sub Copy1 {
688: my ($request, $user, $domain, $fn, $newfilename) = @_;
689:
690: if(-e $fn) {
691: # is dest a dir
692: if (-d $newfilename) {
693: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
694: }
695: if ($newfilename =~ m|/[^\.]+$|) {
696: #no extension add on original extension
697: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) { $newfilename.='.'.$1; }
698: }
699: $newfilename=~s://+:/:g; # remove duplicate /
700: while ($newfilename=~m:/\.\./:) {
701: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
702: }
703: $request->print(&checksuffix($fn,$newfilename));
704: my ($type,$return)=&exists($user, $domain, $newfilename);
705: $request->print($return);
706: if ($type eq 'error') {
707: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a></form>');
708: return;
709: }
710: # Check if there is enough space.
711: my @fileinfo = stat($fn);
712: my ($dir,$fname) = ($fn =~ m{^(.+/)([^/]+)$});
713: my $filesize = $fileinfo[7];
714: $filesize = int($filesize/1000); #expressed in kb
715: my $output = &Apache::loncommon::excess_filesize_warning($user,$domain,'author',
716: $fname,$filesize,'copy');
717: if ($output) {
718: $request->print($output.'<br /><a href="'.&url($dir).'">'.&mt('Cancel').'</a></form>');
719: return;
720: }
721: $request->print(
722: '<input type="hidden" name="newfilename"'
723: .' value="'.$newfilename.'" />'
724: .'<p>'
725: .&mt('Copy [_1] to [_2]?',
726: &display($fn),
727: &display($newfilename))
728: .'</p>'
729: );
730: &CloseForm1($request, $fn);
731: } else {
732: $request->print('<p class="LC_error">'
733: .&mt('No such file: [_1]',
734: &display($fn))
735: .'</p></form>'
736: );
737: }
738: }
739:
740: =pod
741:
742: =item NewDir1
743:
744: Does all phase 1 processing of directory creation:
745: Ensures that the user provides a new directory name,
746: and that the directory does not already exist.
747:
748: Parameters:
749:
750: =over 4
751:
752: =item $request - Apache Request Object [in] - Server request object for the
753: current url.
754:
755: =item $username - Name of the user that is requesting the directory creation.
756:
757: =item $domain - Domain user is in
758:
759: =item $fn - source file.
760:
761: =item $newdir - Name of the directory to be created; path relative to the
762: top level of construction space.
763: =back
764:
765: Side Effects:
766:
767: =over 4
768:
769: =item A new form is displayed. Clicking on the confirmation button
770: causes the newdir operation to transition into phase 2. The hidden field
771: "newfilename" is set with the construction space path to the new directory.
772:
773:
774: =back
775:
776: =cut
777:
778:
779: sub NewDir1 {
780: my ($request, $username, $domain, $fn, $newfilename, $mode) = @_;
781:
782: my ($type, $result)=&exists($username,$domain,$newfilename,'directory');
783: $request->print($result);
784: if ($type eq 'error') {
785: $request->print('</form>');
786: } else {
787: if (($mode eq 'testbank') || ($mode eq 'imsimport')) {
788: $request->print('<input type="hidden" name="callingmode" value="'.$mode.'" />'."\n".
789: '<input type="hidden" name="inhibitmenu" value="yes" />');
790: }
791: $request->print('<input type="hidden" name="newfilename" value="'
792: .$newfilename.'" />'
793: .'<p>'
794: .&mt('Make new directory [_1]?',
795: &display($newfilename))
796: .'</p>'
797: );
798: &CloseForm1($request, $fn);
799: }
800: }
801:
802:
803: sub Decompress1 {
804: my ($request, $user, $domain, $fn) = @_;
805: if( -e $fn) {
806: $request->print('<input type="hidden" name="newfilename" value="'.$fn.'" />');
807: $request->print('<p>'
808: .&mt('Decompress [_1]?',
809: &display($fn))
810: .'</p>'
811: );
812: &CloseForm1($request, $fn);
813: } else {
814: $request->print('<p class="LC_error">'
815: .&mt('No such file: [_1]',
816: &display($fn))
817: .'</p></form>'
818: );
819: }
820: }
821:
822: sub Archive1 {
823: my ($request,$fn) = @_;
824: my @posstypes = qw(problem library sty sequence page task rights meta xml html xhtml htm xhtm css js tex txt gif jpg jpeg png svg other);
825: my (%location_of,%default,$compstyle);
826: foreach my $program ('tar','gzip','bzip2','xz','zip') {
827: foreach my $dir ('/bin/','/usr/bin/','/usr/local/bin/','/sbin/',
828: '/usr/sbin/') {
829: if (-x $dir.$program) {
830: $location_of{$program} = $dir.$program;
831: last;
832: }
833: }
834: }
835: my (%defaults,$cancompress,$canarchive);
836: if (exists($location_of{'tar'})) {
837: $default{'tar'} = ' checked="checked"';
838: $canarchive = 1;
839: $compstyle = 'block';
840: } elsif (exists($location_of{'zip'})) {
841: $default{'zip'} = ' checked="checked"';
842: $canarchive = 1;
843: $compstyle = 'none';
844: }
845: foreach my $compress ('gzip','bzip2','xz') {
846: if (exists($location_of{$compress})) {
847: $default{$compress} = ' checked="checked"';
848: $cancompress = 1;
849: last;
850: }
851: }
852: if (!$canarchive) {
853: $request->print('<p class="LC_error">'.
854: &mt('This LON-CAPA instance does not seem to have either tar or zip installed.').'</p>'.
855: '<span class="LC_warning">'.
856: &mt('At least one of the two is needed in order to be able to create an archive file for: [_1].',
857: &display($fn)).
858: '</span></form>');
859: } elsif (-e $fn) {
860: $request->print(&Apache::lonhtmlcommon::start_pick_box().
861: &Apache::lonhtmlcommon::row_title(&mt('Directory')).
862: &display($fn).
863: &Apache::lonhtmlcommon::row_closure().
864: &Apache::lonhtmlcommon::row_title(&mt('Options').
865: &Apache::loncommon::help_open_topic('Archiving_Directory_Options')).
866: '<fieldset><legend>'.&mt('Recurse').'</legend>'.
867: '<span class="LC_nobreak"><label><input type="checkbox" name="recurse" /> '.
868: &mt('include subdirectories').'</label></span>'.
869: '</fieldset>'.
870: '<fieldset><legend>'.&mt('File types (extensions) to include').(' 'x2).
871: '<span style="text-decoration:line-through">'.(' 'x5).'</span>'.(' 'x2).
872: '<input type="button" name="checkall" value="'.&mt('check all').
873: '" style="height:20px;" onclick="checkAll(document.phaseone.filetype);" />'.
874: (' 'x2).
875: '<input type="button" name="uncheckall" value="'.&mt('uncheck all').
876: '" style="height:20px;" onclick="uncheckAll(document.phaseone.filetype);" /></legend>'.
877: '<table>');
878: my $rem;
879: my $numinrow = 6;
880: for (my $i=0; $i<@posstypes; $i++) {
881: my $rem = $i%($numinrow);
882: if ($rem == 0) {
883: if ($i > 0) {
884: $request->print('</tr>'."\n");
885: }
886: $request->print('<tr>'."\n");
887: }
888: $request->print('<td class="LC_left_item">'.
889: '<span class="LC_nobreak"><label>'.
890: '<input type="checkbox" name="filetype" '.
891: 'value="'.$posstypes[$i].'" /> '.
892: $posstypes[$i].'</label></span></td>'."\n");
893: }
894: $rem = scalar(@posstypes)%($numinrow);
895: my $colsleft;
896: if ($rem) {
897: $colsleft = $numinrow - $rem;
898: }
899: if ($colsleft > 1 ) {
900: $request->print('<td colspan="'.$colsleft.'" class="LC_left_item">'.
901: ' </td>'."\n");
902: } elsif ($colsleft == 1) {
903: $request->print('<td class="LC_left_item"> </td>'."\n");
904: }
905: $request->print('</tr></table>'."\n".
906: '</fieldset>'.
907: '<fieldset><legend>'.&mt('Archive file format').'</legend>');
908: foreach my $possfmt ('tar','zip') {
909: if (exists($location_of{$possfmt})) {
910: $request->print('<span class="LC_nobreak">'.
911: '<label><input type="radio" name="format" value="'.$possfmt.'"'.
912: $default{$possfmt}.' onclick="toggleCompression(this.form);" /> '.
913: $possfmt.'</label></span> ');
914: }
915: }
916: $request->print('</fieldset>'."\n".
917: '<fieldset style="display:'.$compstyle.'" id="tar_compression">'.
918: '<legend>'.&mt('Compression to apply to tar file').'</legend>'.
919: '<span class="LC_nobreak">');
920: if ($cancompress) {
921: foreach my $compress ('gzip','bzip2','xz') {
922: if (exists($location_of{$compress})) {
923: $request->print('<label><input type="radio" name="compress" value="'.$compress.'"'.
924: $default{$compress}.' />'.$compress.'</label> ');
925: }
926: }
927: } else {
928: $request->print('<span class="LC_warning">'.
929: &mt('This LON-CAPA instance does not seem to have gzip, bzip2 or xz installed.').
930: '<br />'.&mt('No compression will be used.').'</span>');
931: }
932: $request->print('</fieldset>'.
933: &Apache::lonhtmlcommon::row_closure(1).
934: &Apache::lonhtmlcommon::end_pick_box()
935: );
936: &CloseForm1($request, $fn);
937: } else {
938: $request->print('<p class="LC_error">'
939: .&mt('No such directory: [_1]',
940: &display($fn))
941: .'</p></form>'
942: );
943: }
944: }
945:
946: =pod
947:
948: =item NewFile1
949:
950: Does all phase 1 processing of file creation:
951: Ensures that the user provides a new filename, adds proper extension
952: if needed and that the file does not already exist, if it is a html,
953: problem, page, or sequence, it then creates a form link to hand the
954: actual creation off to the proper handler.
955:
956: Parameters:
957:
958: =over 4
959:
960: =item $request - Apache Request Object [in] - Server request object for the
961: current url.
962:
963: =item $username - Name of the user that is requesting the directory creation.
964:
965: =item $domain - Name of the domain of the user
966:
967: =item $fn - Source filename
968:
969: =item $newfilename
970: - Name of the file to be created; no path information
971:
972: =item $warnings - Information about changes to filename made by cleanDest().
973:
974: =back
975:
976: Side Effects:
977:
978: =over 4
979:
980: =item 2 new forms are displayed. Clicking on the confirmation button
981: causes the browser to attempt to load the specfied URL, allowing the
982: proper handler to take care of file creation. There is also a Cancel
983: button which returns you to the directory listing you came from
984:
985: =back
986:
987: =cut
988:
989: sub NewFile1 {
990: my ($request, $user, $domain, $fn, $newfilename, $warnings) = @_;
991: return if (&filename_check($newfilename,$warnings) ne 'ok');
992:
993: if ($env{'form.action'} =~ /new(.+)file/) {
994: my $extension=$1;
995: if ($newfilename !~ /\Q.$extension\E$/) {
996: if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) {
997: #already has an extension strip it and add in expected one
998: $newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|;
999: }
1000: $newfilename.=".$extension";
1001: }
1002: }
1003: my ($type, $result)=&exists($user,$domain,$newfilename);
1004: if ($type eq 'error') {
1005: $request->print($warnings.$result);
1006: $request->print('</form>');
1007: } else {
1008: my $extension;
1009:
1010: if ($newfilename =~ m{[^/.]+\.([^/.]+)$}) {
1011: $extension = $1;
1012: }
1013:
1014: my @okexts = qw(xml html xhtml htm xhtm problem page sequence rights sty task library js css txt);
1015: if (($extension eq '') || (!grep(/^\Q$extension\E/,@okexts))) {
1016: my $validexts = '.'.join(', .',@okexts);
1017: $request->print($warnings.$result);
1018: $request->print('<p class="LC_warning">'.
1019: &mt('Invalid filename: ').&display($newfilename).'</p><p>'.
1020: &mt('The name of the new file needs to end with an appropriate file extension to indicate the type of file to create.').'<br />'.
1021: &mt('The following are valid extensions: [_1].',$validexts).
1022: '</p></form><p>'.
1023: '<form name="fileaction" action="/adm/cfile" method="post">'.
1024: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
1025: '<input type="hidden" name="action" value="newfile" />'.
1026: '<span class ="LC_nobreak">'.&mt('Enter a filename: ').'<input type="text" name="newfilename" value="Type Name Here" onfocus="if (this.value == '."'Type Name Here') this.value=''".'" /> <input type="submit" value="Go" />'.
1027: '</span></form></p>'.
1028: '<p><form action="'.&url($fn).
1029: '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></form></p>');
1030: } elsif (($type ne 'warning') && ($warnings eq '') && ($result eq '')) {
1031: my $query = "";
1032: $query .= "?mode=" . $env{'form.mode'} unless (!exists($env{'form.mode'}) || !length($env{'form.mode'}));
1033: $request->print('
1034: <script type="text/javascript">
1035: window.location = "'.&url($newfilename,'js'). $query .'";
1036: </script>');
1037: } else {
1038: $request->print($warnings.$result);
1039: $request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
1040: $request->print('</form>');
1041: $request->print('<form action="'.&url($newfilename).
1042: '" method="post"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
1043: $request->print('<form action="'.&url($fn).
1044: '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1045: }
1046: }
1047: return;
1048: }
1049:
1050: sub filename_check {
1051: my ($newfilename) = @_;
1052: ##Informs User (name).(number).(extension) not allowed
1053: if($newfilename =~ /\.(\d+)\.(\w+)$/){
1054: $r->print('<span class="LC_error">'.$newfilename.
1055: ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
1056: ' '.&mt('Not Allowed').'</span>');
1057: return;
1058: }
1059: if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
1060: $r->print('<span class="LC_error">'.$newfilename.
1061: ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
1062: ' '.&mt('Not Allowed').'</span>');
1063: return;
1064: }
1065: return 'ok';
1066: }
1067:
1068: =pod
1069:
1070: =item phaseone($r, $fn, $uname, $udom)
1071:
1072: Peforms phase one processing of the request. In phase one, error messages
1073: are returned if the request cannot be performed (e.g. attempts to manipulate
1074: files that are nonexistent). If the operation can be performed, what is
1075: about to be done will be presented to the user for confirmation. If the
1076: user confirms the request, then phase two is executed, the action
1077: performed and reported to the user.
1078:
1079: Parameters:
1080:
1081: =over 4
1082:
1083: =item $r - request object [in] - The Apache request being executed.
1084:
1085: =item $fn = string [in] - The filename being manipulated by the
1086: request.
1087:
1088: =item $uname - string [in] Name of user logged in and doing this action.
1089:
1090: =item $udom - string [in] Domain name under which the user logged in.
1091:
1092: =back
1093:
1094: =cut
1095:
1096: sub phaseone {
1097: my ($r,$fn,$uname,$udom)=@_;
1098:
1099: my $doingdir=0;
1100: if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
1101: my ($newfilename,$error,$warnings) =
1102: &cleanDest($env{'form.newfilename'},$doingdir,$fn,$uname,$udom);
1103: unless ($error) {
1104: ($newfilename,$error)=&relativeDest($fn,$newfilename,$uname,$udom);
1105: }
1106: if ($error) {
1107: my $dirlist;
1108: if ($fn=~m{^(.*/)[^/]+$}) {
1109: $dirlist=$1;
1110: } else {
1111: $dirlist=$fn;
1112: }
1113: if ($warnings) {
1114: $r->print($warnings);
1115: }
1116: $r->print('<div class="LC_error">'.$error.'</div>'.
1117: '<p><a href="'.&url($dirlist).'">'.&mt('Return to Directory').
1118: '</a></p>');
1119: return;
1120: }
1121: $r->print('<form action="/adm/cfile" method="post" name="phaseone">'.
1122: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
1123: '<input type="hidden" name="phase" value="two" />'.
1124: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
1125:
1126: if ($env{'form.action'} eq 'newfile' ||
1127: $env{'form.action'} eq 'newhtmlfile' ||
1128: $env{'form.action'} eq 'newproblemfile' ||
1129: $env{'form.action'} eq 'newpagefile' ||
1130: $env{'form.action'} eq 'newsequencefile' ||
1131: $env{'form.action'} eq 'newrightsfile' ||
1132: $env{'form.action'} eq 'newstyfile' ||
1133: $env{'form.action'} eq 'newtaskfile' ||
1134: $env{'form.action'} eq 'newlibraryfile' ||
1135: $env{'form.action'} eq 'Select Action') {
1136: my $empty=&mt('Type Name Here');
1137: if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
1138: &NewFile1($r, $uname, $udom, $fn, $newfilename, $warnings);
1139: } else {
1140: if ($warnings) {
1141: $r->print($warnings);
1142: }
1143: $r->print('<p class="LC_error">'
1144: .&mt('No new filename specified.')
1145: .'</p></form>'
1146: );
1147: }
1148: } else {
1149: if ($warnings) {
1150: $r->print($warnings);
1151: }
1152: if ($env{'form.action'} eq 'rename') {
1153: &Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
1154: } elsif ($env{'form.action'} eq 'move') {
1155: &Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
1156: } elsif ($env{'form.action'} eq 'delete') {
1157: &Delete1($r, $uname, $udom, $fn);
1158: } elsif ($env{'form.action'} eq 'decompress') {
1159: &Decompress1($r, $uname, $udom, $fn);
1160: } elsif ($env{'form.action'} eq 'archive') {
1161: &Archive1($r,$fn);
1162: } elsif ($env{'form.action'} eq 'copy') {
1163: if ($newfilename) {
1164: &Copy1($r, $uname, $udom, $fn, $newfilename);
1165: } else {
1166: $r->print('<p class="LC_error">'
1167: .&mt('No new filename specified.')
1168: .'</p></form>'
1169: );
1170: }
1171: } elsif ($env{'form.action'} eq 'newdir') {
1172: my $mode = '';
1173: if (exists($env{'form.callingmode'}) ) {
1174: $mode = $env{'form.callingmode'};
1175: }
1176: &NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
1177: }
1178: }
1179: }
1180:
1181: =pod
1182:
1183: =item Rename2($request, $user, $directory, $oldfile, $newfile)
1184:
1185: Performs phase 2 processing of a rename reequest. This is where the
1186: actual rename is performed.
1187:
1188: Parameters
1189:
1190: =over 4
1191:
1192: =item $request - Apache request object [in] The request being processed.
1193:
1194: =item $user - string [in] The name of the user initiating the request.
1195:
1196: =item $directory - string [in] The name of the directory relative to the
1197: construction space top level of the renamed file.
1198:
1199: =item $oldfile - Name of the file.
1200:
1201: =item $newfile - Name of the new file.
1202:
1203: =back
1204:
1205: Returns:
1206:
1207: =over 4
1208:
1209: =item 1 Success.
1210:
1211: =item 0 Failure.
1212:
1213: =cut
1214:
1215: sub Rename2 {
1216:
1217: my ($request, $user, $directory, $oldfile, $newfile) = @_;
1218:
1219: &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
1220: " new file ".$newfile."\n");
1221: &Debug($request, "Target is: ".$directory.'/'.
1222: $newfile);
1223: if (-e $oldfile) {
1224:
1225: my $oRN=$oldfile;
1226: my $nRN=$newfile;
1227: unless (rename($oldfile,$newfile)) {
1228: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1229: return 0;
1230: }
1231: ## If old name.(extension) exits, move under new name.
1232: ## If it doesn't exist and a new.(extension) exists
1233: ## delete it (only concern when renaming over files)
1234: my $tmp1=$oRN.'.meta';
1235: my $tmp2=$nRN.'.meta';
1236: if(-e $tmp1){
1237: unless(rename($tmp1,$tmp2)){ }
1238: } elsif(-e $tmp2){
1239: unlink $tmp2;
1240: }
1241: $tmp1=$oRN.'.save';
1242: $tmp2=$nRN.'.save';
1243: if(-e $tmp1){
1244: unless(rename($tmp1,$tmp2)){ }
1245: } elsif(-e $tmp2){
1246: unlink $tmp2;
1247: }
1248: $tmp1=$oRN.'.log';
1249: $tmp2=$nRN.'.log';
1250: if(-e $tmp1){
1251: unless(rename($tmp1,$tmp2)){ }
1252: } elsif(-e $tmp2){
1253: unlink $tmp2;
1254: }
1255: $tmp1=$oRN.'.bak';
1256: $tmp2=$nRN.'.bak';
1257: if(-e $tmp1){
1258: unless(rename($tmp1,$tmp2)){ }
1259: } elsif(-e $tmp2){
1260: unlink $tmp2;
1261: }
1262: } else {
1263: $request->print(
1264: '<p class="LC_error">'
1265: .&mt('No such file: [_1]',
1266: &display($oldfile))
1267: .'</p></form>'
1268: );
1269: return 0;
1270: }
1271: return 1;
1272: }
1273:
1274: =pod
1275:
1276: =item Delete2($request, $user, $filename)
1277:
1278: Performs phase two of a delete. The user has confirmed that they want
1279: to delete the selected file. The file is deleted and the results of the
1280: delete attempt are indicated.
1281:
1282: Parameters:
1283:
1284: =over 4
1285:
1286: =item $request - Apache Request object [in] the request object for the current
1287: delete operation.
1288:
1289: =item $user - string [in] The name of the user initiating the delete
1290: request.
1291:
1292: =item $filename - string [in] The name of the file, relative to construction
1293: space, to delete.
1294:
1295: =back
1296:
1297: Returns:
1298: 1 - success.
1299: 0 - Failure.
1300:
1301: =cut
1302:
1303: sub Delete2 {
1304: my ($request, $user, $filename) = @_;
1305: if (-d $filename) {
1306: unless (&empty_directory($filename,'Delete2')) {
1307: $request->print('<span class="LC_error">'.&mt('Error: Directory Non Empty').'</span>');
1308: return 0;
1309: } else {
1310: if(-e $filename) {
1311: unless(rmdir($filename)) {
1312: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1313: return 0;
1314: }
1315: } else {
1316: $request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1317: return 0;
1318: }
1319: }
1320: } else {
1321: if(-e $filename) {
1322: unless(unlink($filename)) {
1323: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1324: return 0;
1325: }
1326: } else {
1327: $request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1328: return 0;
1329: }
1330: }
1331: return 1;
1332: }
1333:
1334: =pod
1335:
1336: =item Copy2($request, $username, $dir, $oldfile, $newfile)
1337:
1338: Performs phase 2 of a copy. The file is copied and the status
1339: of that copy is reported back to the user.
1340:
1341: =over 4
1342:
1343: =item $request - Apache request object [in]; the apache request currently
1344: being executed.
1345:
1346: =item $username - string [in] Name of the user who is requesting the copy.
1347:
1348: =item $dir - string [in] Directory path relative to the construction space
1349: of the destination file.
1350:
1351: =item $oldfile - string [in] Name of the source file.
1352:
1353: =item $newfile - string [in] Name of the destination file.
1354:
1355:
1356: =back
1357:
1358: Returns 0 failure, and 1 successs.
1359:
1360: =cut
1361:
1362: sub Copy2 {
1363: my ($request, $username, $dir, $oldfile, $newfile) = @_;
1364: &Debug($request ,"Will try to copy $oldfile to $newfile");
1365: if(-e $oldfile) {
1366: if ($oldfile eq $newfile) {
1367: $request->print('<span class="LC_error">'.&mt('Warning').': '.&mt('Name of new file is the same as name of old file').' - '.&mt('no action taken').'.</span>');
1368: return 1;
1369: }
1370: unless (copy($oldfile, $newfile)) {
1371: $request->print('<span class="LC_error">'.&mt('copy Error').': '.$!.'</span>');
1372: return 0;
1373: } elsif (!chmod(0660, $newfile)) {
1374: $request->print('<span class="LC_error">'.&mt('chmod error').': '.$!.'</span>');
1375: return 0;
1376: } elsif (-e $oldfile.'.meta' &&
1377: !copy($oldfile.'.meta', $newfile.'.meta') &&
1378: !chmod(0660, $newfile.'.meta')) {
1379: $request->print('<span class="LC_error">'.&mt('copy metadata error').
1380: ': '.$!.'</span>');
1381: return 0;
1382: } else {
1383: return 1;
1384: }
1385: } else {
1386: $request->print('<p class="LC_error">'.&mt('No such file').'</p>');
1387: return 0;
1388: }
1389: return 1;
1390: }
1391:
1392: =pod
1393:
1394: =item NewDir2($request, $user, $newdirectory)
1395:
1396: Performs phase 2 processing of directory creation. This involves creating the directory and
1397: reporting the results of that creation to the user.
1398:
1399: Parameters:
1400: =over 4
1401:
1402: =item $request - Apache request object [in]. Object representing the current HTTP request.
1403:
1404: =item $user - string [in] The name of the user that is initiating the request.
1405:
1406: =item $newdirectory - string [in] The full path of the directory being created.
1407:
1408: =back
1409:
1410: Returns 0 - failure 1 - success.
1411:
1412: =cut
1413:
1414: sub NewDir2 {
1415: my ($request, $user, $newdirectory) = @_;
1416:
1417: unless(mkdir($newdirectory, 02770)) {
1418: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1419: return 0;
1420: }
1421: unless(chmod(02770, ($newdirectory))) {
1422: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1423: return 0;
1424: }
1425: return 1;
1426: }
1427:
1428: sub decompress2 {
1429: my ($r, $user, $dir, $file) = @_;
1430: &Apache::lonnet::appenv({'cgi.file' => $file});
1431: &Apache::lonnet::appenv({'cgi.dir' => $dir});
1432: my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
1433: $r->print($result);
1434: &Apache::lonnet::delenv('cgi.file');
1435: &Apache::lonnet::delenv('cgi.dir');
1436: return 1;
1437: }
1438:
1439: sub Archive2 {
1440: my ($r,$name,$udom,$fn,$identifier) = @_;
1441: my %options = (
1442: dir => $fn,
1443: );
1444: my @filetypes = qw(problem library sty sequence page task rights meta xml html xhtml htm xhtm css js tex txt gif jpg jpeg png svg other);
1445: my (@include,%oktypes);
1446: map { $oktypes{$_} = 1; } @filetypes;
1447: my @posstypes = &Apache::loncommon::get_env_multiple('form.filetype');
1448: foreach my $type (@posstypes) {
1449: if ($oktypes{$type}) {
1450: push(@include,$type);
1451: }
1452: }
1453: if (scalar(@include) == scalar(@filetypes)) {
1454: $options{'types'} = 'all';
1455: } else {
1456: $options{'types'} = join(',',@include);
1457: }
1458: if (exists($env{'form.recurse'})) {
1459: $options{'recurse'} = 1;
1460: }
1461: if (exists($env{'form.encrypt'})) {
1462: if ($env{'form.enckey'} ne '') {
1463: $options{'encrypt'} = $env{'form.enckey'};
1464: }
1465: }
1466: $options{'format'} = 'tar';
1467: $options{'compress'} = 'gzip';
1468: if ((exists($env{'form.format'})) && $env{'form.format'} =~ /^zip$/i) {
1469: $options{'format'} = 'zip';
1470: delete($options{'compress'});
1471: } elsif ((exists($env{'form.compress'})) && ($env{'form.compress'} =~ /^(xz|bzip2)$/i)) {
1472: $options{'compress'} = lc($env{'form.compress'});
1473: }
1474: my $key = 'cgi.'.$identifier.'.archive';
1475: my $storestring = &Apache::lonnet::freeze_escape(\%options);
1476: &Apache::lonnet::appenv({$key => $storestring});
1477: return 1;
1478: }
1479:
1480: =pod
1481:
1482: =item phasetwo($r, $fn, $uname, $udom,$identifier)
1483:
1484: Controls the phase 2 processing of file management
1485: requests for construction space. In phase one, the user
1486: was asked to confirm the operation. In phase 2, the operation
1487: is performed and the result is shown.
1488:
1489: The strategy is to break out the processing into specific action processors
1490: named action2 where action is the requested action and the 2 denotes
1491: phase 2 processing.
1492:
1493: Parameters:
1494:
1495: =over 4
1496:
1497: =item $r - Apache Request object [in] The request object for this httpd
1498: transaction.
1499:
1500: =item $fn - string [in] A filename indicating the object that is being
1501: manipulated.
1502:
1503: =item $uname - string [in] The name of the user initiating the file management
1504: request.
1505:
1506: =item $udom - string [in] The login domain of the user initiating the
1507: file management request.
1508: =back
1509:
1510: =cut
1511:
1512: sub phasetwo {
1513: my ($r,$fn,$uname,$udom,$identifier)=@_;
1514:
1515: &Debug($r, "loncfile - Entering phase 2 for $fn");
1516:
1517: # Break down the file into its component pieces.
1518:
1519: my $dir; # Directory path
1520: my $main; # Filename.
1521: my $suffix; # Extension.
1522: if ($fn=~m:(.*)/([^/]+):) {
1523: $dir=$1; # Directory path
1524: $main=$2; # Filename.
1525: }
1526: if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1527: $suffix=$1; #This is the actually filename extension if it exists
1528: $main=~s/\.\w+$//; #strip the extension
1529: }
1530: my $dest; #
1531: my $dest_dir; # On success this is where we'll go.
1532: my $disp_newname; #
1533: my $dest_newname; #
1534: &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1535: &Debug($r," newfilename = ".$env{'form.newfilename'});
1536:
1537: my $conspace=$fn;
1538:
1539: &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1540:
1541: &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1542:
1543: # Select the appropriate processing sub.
1544: if ($env{'form.action'} eq 'decompress') {
1545: $main .= '.'.$suffix;
1546: if(!&decompress2($r, $uname, $dir, $main)) {
1547: return ;
1548: }
1549: $dest = $dir."/.";
1550: } elsif ($env{'form.action'} eq 'archive') {
1551: &Archive2($r,$uname,$udom,$fn,$identifier);
1552: return;
1553: } elsif ($env{'form.action'} eq 'rename' ||
1554: $env{'form.action'} eq 'move') {
1555: if($env{'form.newfilename'}) {
1556: if (!defined($dir)) {
1557: $fn=~m:^(.*)/:;
1558: $dir=$1;
1559: }
1560: if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1561: return;
1562: }
1563: $dest = $dir."/";
1564: $dest_newname = $env{'form.newfilename'};
1565: $env{'form.newfilename'} =~ /.+(\/.+$)/;
1566: $disp_newname = $1;
1567: $disp_newname =~ s/\///;
1568: }
1569: } elsif ($env{'form.action'} eq 'delete') {
1570: if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1571: return ;
1572: }
1573: # Once a resource is deleted, we just list the directory that
1574: # previously held it.
1575: #
1576: $dest = $dir."/."; # Parent dir.
1577: } elsif ($env{'form.action'} eq 'copy') {
1578: if($env{'form.newfilename'}) {
1579: if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1580: return ;
1581: }
1582: $dest = $env{'form.newfilename'};
1583: } else {
1584: $r->print('<p class="LC_error">'.&mt('No New filename specified').'</p></form>');
1585: return;
1586: }
1587:
1588: } elsif ($env{'form.action'} eq 'newdir') {
1589: my $newdir= $env{'form.newfilename'};
1590: if(!&NewDir2($r, $uname, $newdir)) {
1591: return;
1592: }
1593: $dest = $newdir."/";
1594: }
1595: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1596: $r->print(
1597: '<p>'
1598: .&Apache::lonhtmlcommon::confirm_success(&mt('Done'))
1599: .'<br /><a href="javascript:self.close()">'.&mt('Continue').'</a>'
1600: .'</p>'
1601: );
1602: } else {
1603: if ($env{'form.action'} eq 'rename') {
1604: $r->print(
1605: '<p>'.&Apache::lonhtmlcommon::confirm_success(&mt('Done')).'</p>'
1606: .&Apache::lonhtmlcommon::actionbox(
1607: ['<a href="'.&url($dest).'">'.&mt('Return to Directory').'</a>',
1608: '<a href="'.&url($dest_newname).'">'.$disp_newname.'</a>']));
1609: } else {
1610: $r->print(&done($dest));
1611: }
1612: }
1613: }
1614:
1615: sub handler {
1616:
1617: $r=shift;
1618:
1619: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename','mode']);
1620:
1621: &Debug($r, "loncfile.pm - handler entered");
1622: &Debug($r, " filename: ".$env{'form.filename'});
1623: &Debug($r, " newfilename: ".$env{'form.newfilename'});
1624: #
1625: # Determine the root filename
1626: # This could come in as "filename", which actually is a URL, or
1627: # as "qualifiedfilename", which is indeed a real filename in filesystem
1628: #
1629: my $fn;
1630:
1631: if ($env{'form.filename'}) {
1632: &Debug($r, "test: $env{'form.filename'}");
1633: $fn=&unescape($env{'form.filename'});
1634: $fn=&URLToPath($fn);
1635: } elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {
1636: #Just hijack the script only the first time around to inject the
1637: #correct information for further processing
1638: $fn=&unescape($env{'form.decompress'});
1639: $fn=&URLToPath($fn);
1640: $env{'form.action'}="decompress";
1641: } elsif ($env{'form.qualifiedfilename'}) {
1642: $fn=$env{'form.qualifiedfilename'};
1643: } else {
1644: &Debug($r, "loncfile::handler - no form.filename");
1645: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1646: ' unspecified filename for cfile', $r->filename);
1647: return HTTP_NOT_FOUND;
1648: }
1649:
1650: unless ($fn) {
1651: &Debug($r, "loncfile::handler - doctored url is empty");
1652: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1653: ' trying to cfile non-existing file', $r->filename);
1654: return HTTP_NOT_FOUND;
1655: }
1656:
1657: # ----------------------------------------------------------- Start page output
1658:
1659: my ($uname,$udom) = &Apache::lonnet::constructaccess($fn);
1660: &Debug($r,
1661: "loncfile::handler constructaccess uname = $uname domain = $udom");
1662: if (($uname eq '') || ($udom eq '')) {
1663: $r->log_reason($uname.' at '.$udom.
1664: ' trying to manipulate file '.$env{'form.filename'}.
1665: ' ('.$fn.') - not authorized',
1666: $r->filename);
1667: return HTTP_NOT_ACCEPTABLE;
1668: }
1669:
1670:
1671: &Apache::loncommon::content_type($r,'text/html');
1672: $r->send_http_header;
1673:
1674: my ($js,$identifier);
1675: my $args = {};
1676:
1677: if (($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') &&
1678: (($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport'))) {
1679: my $newdirname = $env{'form.newfilename'};
1680: &js_escape(\$newdirname);
1681: $js = <<"ENDJS";
1682: <script type="text/javascript">
1683: // <![CDATA[
1684: function writeDone() {
1685: window.focus();
1686: opener.document.info.newdir.value = "$newdirname";
1687: setTimeout("self.close()",10000);
1688: }
1689: // ]]>
1690: </script>
1691: ENDJS
1692: $args->{'add_entries'} = { onload => "writeDone()" };
1693: } elsif (($env{'form.action'} eq 'archive') &&
1694: ($env{'environment.authorarchive'})) {
1695: if ($env{'form.phase'} eq 'two') {
1696: $identifier = &Apache::loncommon::get_cgi_id();
1697: $args->{'redirect'} = [0,"/cgi-bin/archive.pl?$identifier"];
1698: } else {
1699: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1700: $js = <<"ENDJS";
1701: <script type="text/javascript">
1702: // <![CDATA[
1703: function toggleCompression(form) {
1704: if (document.getElementById('tar_compression')) {
1705: if (form.format.length > 1) {
1706: for (var i=0; i<form.format.length; i++) {
1707: if (form.format[i].checked) {
1708: if (form.format[i].value == 'zip') {
1709: document.getElementById('tar_compression').style.display = 'none';
1710: } else if (form.format[i].value == 'tar') {
1711: document.getElementById('tar_compression').style.display = 'block';
1712: }
1713: break;
1714: }
1715: }
1716: }
1717: }
1718: return;
1719: }
1720:
1721: function resetForm() {
1722: if (document.phaseone.filetype.length) {
1723: for (var i=0; i<document.phaseone.filetype.length; i++) {
1724: document.phaseone.filetype[i].checked = false;
1725: }
1726: }
1727: if (document.getElementById('tar_compression')) {
1728: if (document.phaseone.format.length) {
1729: document.getElementById('tar_compression').style.display = 'block';
1730: for (var i=0; i<document.phaseone.format.length; i++) {
1731: if (document.phaseone.format[i].value == 'tar') {
1732: document.phaseone.format[i].checked = true;
1733: } else {
1734: document.phaseone.format[i].checked = false;
1735: }
1736: }
1737: }
1738: if (document.phaseone.compress.length) {
1739: for (var i=0; i<document.phaseone.compress.length; i++) {
1740: if (document.phaseone.compress[i].value == 'gzip') {
1741: document.phaseone.compress[i].checked = true;
1742: } else {
1743: document.phaseone.compress[i].checked = false;
1744: }
1745: }
1746: }
1747: }
1748: document.phaseone.recurse.checked = false;
1749: }
1750:
1751: $check_uncheck_js
1752:
1753: // ]]>
1754: </script>
1755:
1756: ENDJS
1757: $args->{'add_entries'} = { onload => "resetForm()" };
1758: }
1759: }
1760: my $londocroot = $r->dir_config('lonDocRoot');
1761: my $trailfile = $fn;
1762: $trailfile =~ s{^/(priv/)}{$londocroot/$1};
1763:
1764: # Breadcrumbs
1765: my $crsauthor;
1766: my $text = 'Authoring Space';
1767: my $title = 'Authoring Space File Operation',
1768: my $href = &Apache::loncommon::authorspace(&url($fn));
1769: if ($env{'request.course.id'}) {
1770: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1771: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1772: if ($href eq "/priv/$cdom/$cnum/") {
1773: $text = 'Course Authoring Space';
1774: $title = 'Course Authoring Space File Operation',
1775: $crsauthor = 1;
1776: }
1777: }
1778: &Apache::lonhtmlcommon::clear_breadcrumbs();
1779: &Apache::lonhtmlcommon::add_breadcrumb({
1780: 'text' => $text,
1781: 'href' => $href,
1782: });
1783: &Apache::lonhtmlcommon::add_breadcrumb({
1784: 'text' => 'File Operation',
1785: 'title' => $title,
1786: 'href' => '',
1787: });
1788:
1789: $r->print(&Apache::loncommon::start_page($title,$js,$args)
1790: .&Apache::lonhtmlcommon::breadcrumbs()
1791: .&Apache::loncommon::head_subbox(
1792: &Apache::loncommon::CSTR_pageheader($trailfile))
1793: );
1794:
1795: unless ($env{'form.action'} eq 'archive') {
1796: $r->print('<p>'.&mt('Location').': '.&display($fn).'</p>');
1797: }
1798:
1799: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1800: unless ($crsauthor) {
1801: $r->print('<p class="LC_info">'
1802: .&mt('Co-Author [_1]',$uname.':'.$udom)
1803: .'</p>'
1804: );
1805: }
1806: }
1807:
1808:
1809: &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
1810: my %action = &Apache::lonlocal::texthash(
1811: 'delete' => 'Delete',
1812: 'rename' => 'Rename',
1813: 'move' => 'Move',
1814: 'newdir' => 'New Directory',
1815: 'decompress' => 'Decompress',
1816: 'archive' => 'Export directory to archive file',
1817: 'copy' => 'Copy',
1818: 'newfile' => 'New Resource',
1819: 'newhtmlfile' => 'New Resource',
1820: 'newproblemfile' => 'New Resource',
1821: 'newpagefile' => 'New Resource',
1822: 'newsequencefile' => 'New Resource',
1823: 'newrightsfile' => 'New Resource',
1824: 'newstyfile' => 'New Resource',
1825: 'newtaskfile' => 'New Resource',
1826: 'newlibraryfile' => 'New Resource',
1827: 'Select Action' => 'New Resource',
1828: );
1829: if ($action{$env{'form.action'}}) {
1830: if ($crsauthor) {
1831: my @disallowed = qw(page sequence rights library);
1832: my $newtype;
1833: if ($env{'form.action'} =~ /^new(\w+)file$/) {
1834: $newtype = $1;
1835: } elsif ($env{'form.action'} eq 'newfile') {
1836: ($newtype) = ($env{'form.newfilename'} =~ m{\.([^/.]+)$});
1837: $newtype = lc($newtype);
1838: }
1839: if (($newtype ne '') &&
1840: (grep(/^\Q$newtype\E$/,@disallowed))) {
1841: $r->print('<p class="LC_error">'
1842: .&mt('Creation of a new file of type: [_1] is not permitted in Course Authoring Space',$newtype)
1843: .'</p>'
1844: .&Apache::loncommon::end_page()
1845: );
1846: return OK;
1847: }
1848: if ($env{'form.action'} eq 'archive') {
1849: $r->print('<p>'.&mt('Location').': '.&display($fn).'</p>'."\n".
1850: '<p class="LC_error">'.
1851: &mt('Export to an archive file is not permitted in Course Authoring Space').
1852: '</p>'."\n".
1853: &Apache::loncommon::end_page());
1854: return OK;
1855: }
1856: } elsif ($env{'form.action'} eq 'archive') {
1857: unless ($env{'environment.authorarchive'}) {
1858: $r->print('<p>'.&mt('Location').': '.&display($fn).'</p>'."\n".
1859: '<p class="LC_error">'.
1860: &mt('You do not have permission to export to an archive file in this Authoring Space').
1861: '</p>'."\n".
1862: &Apache::loncommon::end_page());
1863: return OK;
1864: }
1865: }
1866: $r->print('<h2>'.$action{$env{'form.action'}}.'</h2>');
1867: } else {
1868: $r->print('<p class="LC_error">'
1869: .&mt('Unknown Action: [_1]',$env{'form.action'})
1870: .'</p>'
1871: .&Apache::loncommon::end_page()
1872: );
1873: return OK;
1874: }
1875:
1876: if ($env{'form.phase'} eq 'two') {
1877: &Debug($r, "loncfile::handler entering phase2");
1878: &phasetwo($r,$fn,$uname,$udom,$identifier);
1879: } else {
1880: &Debug($r, "loncfile::handler entering phase1");
1881: &phaseone($r,$fn,$uname,$udom);
1882: }
1883:
1884: $r->print(&Apache::loncommon::end_page());
1885: return OK;
1886: }
1887:
1888: 1;
1889: __END__
1890:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>