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.131 2024/09/26 22:43:36 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,%defaults);
826: my ($compstyle,$canarchive,$cancompress,$numformat,$numcompress,$defext) =
827: &archive_tools(\%location_of,\%defaults);
828: if (!$canarchive) {
829: $request->print('<p class="LC_error">'.
830: &mt('This LON-CAPA instance does not seem to have either tar or zip installed.').'</p>'."\n".
831: '<span class="LC_warning">'.
832: &mt('At least one of the two is needed in order to be able to create an archive file for: [_1].',
833: &display($fn))."\n".
834: '</span></form>');
835: } elsif (-e $fn) {
836: $request->print('<input type="hidden" name="adload" value="" />'."\n".
837: &Apache::lonhtmlcommon::start_pick_box().
838: &Apache::lonhtmlcommon::row_title(&mt('Directory')).
839: &display($fn).
840: &Apache::lonhtmlcommon::row_closure().
841: &Apache::lonhtmlcommon::row_title(&mt('Options').
842: &Apache::loncommon::help_open_topic('Archiving_Directory_Options')).
843: '<fieldset><legend>'.&mt('Recurse').'</legend>'.
844: '<span class="LC_nobreak"><label><input type="checkbox" name="recurse" /> '.
845: &mt('include subdirectories').'</label></span>'.
846: '</fieldset>'.
847: '<fieldset><legend>'.&mt('File types (extensions) to include').(' 'x2).
848: '<span style="text-decoration:line-through">'.(' 'x5).'</span>'.(' 'x2).
849: '<input type="button" name="checkall" value="'.&mt('check all').
850: '" style="height:20px;" onclick="checkAll(document.phaseone.filetype);" />'.
851: (' 'x2).
852: '<input type="button" name="uncheckall" value="'.&mt('uncheck all').
853: '" style="height:20px;" onclick="uncheckAll(document.phaseone.filetype);" /></legend>'.
854: '<table>');
855: my $rem;
856: my $numinrow = 6;
857: for (my $i=0; $i<@posstypes; $i++) {
858: my $rem = $i%($numinrow);
859: if ($rem == 0) {
860: if ($i > 0) {
861: $request->print('</tr>'."\n");
862: }
863: $request->print('<tr>'."\n");
864: }
865: $request->print('<td class="LC_left_item">'.
866: '<span class="LC_nobreak"><label>'.
867: '<input type="checkbox" name="filetype" '.
868: 'value="'.$posstypes[$i].'" /> '.
869: $posstypes[$i].'</label></span></td>'."\n");
870: }
871: $rem = scalar(@posstypes)%($numinrow);
872: my $colsleft;
873: if ($rem) {
874: $colsleft = $numinrow - $rem;
875: }
876: if ($colsleft > 1 ) {
877: $request->print('<td colspan="'.$colsleft.'" class="LC_left_item">'.
878: ' </td>'."\n");
879: } elsif ($colsleft == 1) {
880: $request->print('<td class="LC_left_item"> </td>'."\n");
881: }
882: $request->print('</tr></table>'."\n".
883: '</fieldset>'.
884: '<fieldset><legend>'.&mt('Archive file format').'</legend>');
885: foreach my $possfmt ('tar','zip') {
886: if (exists($location_of{$possfmt})) {
887: $request->print('<span class="LC_nobreak">'.
888: '<label><input type="radio" name="format" value="'.$possfmt.'"'.
889: $defaults{$possfmt}.' onclick="toggleCompression(this.form);" /> '.
890: $possfmt.'</label></span> ');
891: }
892: }
893: $request->print('</fieldset>'."\n".
894: '<fieldset style="display:'.$compstyle.'" id="tar_compression">'.
895: '<legend>'.&mt('Compression to apply to tar file').'</legend>'.
896: '<span class="LC_nobreak">');
897: if ($cancompress) {
898: foreach my $compress ('gzip','bzip2','xz') {
899: if (exists($location_of{$compress})) {
900: $request->print('<label><input type="radio" name="compress" value="'.$compress.'"'.
901: $defaults{$compress}.' onclick="setArchiveExt(this.form);" />'.
902: $compress.'</label> ');
903: }
904: }
905: } else {
906: $request->print('<span class="LC_warning">'.
907: &mt('This LON-CAPA instance does not seem to have gzip, bzip2 or xz installed.').
908: '<br />'.&mt('No compression will be used.').'</span>');
909: }
910: $request->print('</fieldset>'."\n".
911: '<fieldset style="display:none" id="archive_saveas">'.
912: '<legend>'.&mt('Filename to download').'</legend>'.
913: '<table style="border-spacing:0"><tr><td style="padding:0;">'.&mt('Name').'<br />'."\n".
914: '<input type="text" name="archivefname" value="" size="8" /></td><td style="padding:0;">'.
915: &mt('Extension').'<br />'."\n".
916: '<input type="text" name="archiveext" id="archiveext" value="" size="4" readonly="readonly" />'.
917: '</td></tr></table></fieldset>'."\n".
918: &Apache::lonhtmlcommon::row_closure(1).
919: &Apache::lonhtmlcommon::end_pick_box().'<br />'."\n"
920: );
921: &CloseForm1($request, $fn);
922: } else {
923: $request->print('<p class="LC_error">'
924: .&mt('No such directory: [_1]',
925: &display($fn))
926: .'</p></form>'
927: );
928: }
929: return;
930: }
931:
932: sub archive_tools {
933: my ($location_of,$defaults) = @_;
934: my ($compstyle,$canarchive,$cancompress,$numformat,$numcompress,$defext);
935: ($numformat,$numcompress) = (0,0);
936: if ((ref($location_of) eq 'HASH') && (ref($defaults) eq 'HASH')) {
937: foreach my $program ('tar','gzip','bzip2','xz','zip') {
938: foreach my $dir ('/bin/','/usr/bin/','/usr/local/bin/','/sbin/',
939: '/usr/sbin/') {
940: if (-x $dir.$program) {
941: $location_of->{$program} = $dir.$program;
942: last;
943: }
944: }
945: }
946: foreach my $format ('tar','zip') {
947: if (exists($location_of->{$format})) {
948: unless ($canarchive) {
949: $defext = $format;
950: $defaults->{$format} = ' checked="checked"';
951: if ($format eq 'tar') {
952: $compstyle = 'block';
953: } else {
954: $compstyle = 'none';
955: }
956: }
957: $canarchive = 1;
958: $numformat ++;
959: }
960: }
961: foreach my $compress ('gzip','bzip2','xz') {
962: if (exists($location_of->{$compress})) {
963: $numcompress ++;
964: unless ($cancompress) {
965: if ($defext eq 'tar') {
966: if ($compress eq 'gzip') {
967: $defext .= '.gz';
968: } elsif ($compress eq 'bzip2') {
969: $defext .= '.bz2';
970: } else {
971: $defext .= ".$compress";
972: }
973: }
974: $defaults->{$compress} = ' checked="checked"';
975: $cancompress = 1;
976: }
977: }
978: }
979: }
980: if (wantarray) {
981: return ($compstyle,$canarchive,$cancompress,$numformat,$numcompress,$defext);
982: } else {
983: return $defext;
984: }
985: }
986:
987: sub archive_in_progress {
988: my ($earlyout,$idnum);
989: if ($env{'cgi.author.archive'} =~ /^(\d+)_\d+_\d+$/) {
990: my $timestamp = $1;
991: $idnum = $env{'cgi.author.archive'};
992: if (exists($env{'cgi.'.$idnum.'.archive'})) {
993: my $hashref = &Apache::lonnet::thaw_unescape($env{'cgi.'.$idnum.'.archive'});
994: my $lonprtdir = $Apache::lonnet::perlvar{'lonPrtDir'};
995: if (-e $lonprtdir.'/'.$env{'user.name'}.'_'.$env{'user.domain'}.'_archive_'.$idnum.'.txt') {
996: $earlyout = $timestamp;
997: } elsif (ref($hashref) eq 'HASH') {
998: my $suffix = $hashref->{'extension'};
999: if (-e $lonprtdir.'/'.$env{'user.name'}.'_'.$env{'user.domain'}.'_archive_'.$idnum.$suffix) {
1000: $earlyout = $timestamp;
1001: }
1002: }
1003: unless ($earlyout) {
1004: &Apache::lonnet::delenv('cgi.'.$idnum.'.archive');
1005: &Apache::lonnet::delenv('cgi.author.archive');
1006: }
1007: } else {
1008: &Apache::lonnet::delenv('cgi.author.archive');
1009: }
1010: }
1011: return ($earlyout,$idnum);
1012: }
1013:
1014: sub cancel_archive_form {
1015: my ($r,$title,$fname,$earlyout,$idnum) = @_;
1016: $r->print('<h2>'.$title.'</h2>'."\n".
1017: '<form action="/adm/cfile" method="post" onsubmit="return confirmation(this);">'."\n".
1018: '<input type="hidden" name="filename" value="'.$fname.'" />'."\n".
1019: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n".
1020: '<p>'.&mt('Each author may only have one archive request in process at a time.')."\n".'<ul>'.
1021: '<li>'.&mt('An incomplete archive request was begun: [_1].',
1022: &Apache::lonlocal::locallocaltime($earlyout)).
1023: '</li>'."\n".
1024: '<li>'.&mt('An archive request is considered complete when the archive file has been successfully downloaded.').'</li>'."\n".
1025: '<li>'.
1026: &mt('To submit a new archive request, either wait for the existing request (e.g., in another tab/window) to complete, or remove it.').'</li>'."\n".
1027: '</ul></p>'."\n".
1028: '<p><span class="LC_nobreak">'.&mt('Remove existing archive request?').' '."\n".
1029: '<label><input type="radio" name="remove_archive_request" value="'.$idnum.'" />'.&mt('Yes').'</label>'.
1030: (' 'x2)."\n".
1031: '<label><input type="radio" name="remove_archive_request" value="" checked="checked" />'.&mt('No').'</label></span></p>'."\n".
1032: '<br />');
1033: }
1034:
1035: =pod
1036:
1037: =item NewFile1
1038:
1039: Does all phase 1 processing of file creation:
1040: Ensures that the user provides a new filename, adds proper extension
1041: if needed and that the file does not already exist, if it is a html,
1042: problem, page, or sequence, it then creates a form link to hand the
1043: actual creation off to the proper handler.
1044:
1045: Parameters:
1046:
1047: =over 4
1048:
1049: =item $request - Apache Request Object [in] - Server request object for the
1050: current url.
1051:
1052: =item $username - Name of the user that is requesting the directory creation.
1053:
1054: =item $domain - Name of the domain of the user
1055:
1056: =item $fn - Source filename
1057:
1058: =item $newfilename
1059: - Name of the file to be created; no path information
1060:
1061: =item $warnings - Information about changes to filename made by cleanDest().
1062:
1063: =back
1064:
1065: Side Effects:
1066:
1067: =over 4
1068:
1069: =item 2 new forms are displayed. Clicking on the confirmation button
1070: causes the browser to attempt to load the specfied URL, allowing the
1071: proper handler to take care of file creation. There is also a Cancel
1072: button which returns you to the directory listing you came from
1073:
1074: =back
1075:
1076: =cut
1077:
1078: sub NewFile1 {
1079: my ($request, $user, $domain, $fn, $newfilename, $warnings) = @_;
1080: return if (&filename_check($newfilename,$warnings) ne 'ok');
1081:
1082: if ($env{'form.action'} =~ /new(.+)file/) {
1083: my $extension=$1;
1084: if ($newfilename !~ /\Q.$extension\E$/) {
1085: if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) {
1086: #already has an extension strip it and add in expected one
1087: $newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|;
1088: }
1089: $newfilename.=".$extension";
1090: }
1091: }
1092: my ($type, $result)=&exists($user,$domain,$newfilename);
1093: if ($type eq 'error') {
1094: $request->print($warnings.$result);
1095: $request->print('</form>');
1096: } else {
1097: my $extension;
1098:
1099: if ($newfilename =~ m{[^/.]+\.([^/.]+)$}) {
1100: $extension = $1;
1101: }
1102:
1103: my @okexts = qw(xml html xhtml htm xhtm problem page sequence rights sty task library js css txt);
1104: if (($extension eq '') || (!grep(/^\Q$extension\E/,@okexts))) {
1105: my $validexts = '.'.join(', .',@okexts);
1106: $request->print($warnings.$result);
1107: $request->print('<p class="LC_warning">'.
1108: &mt('Invalid filename: ').&display($newfilename).'</p><p>'.
1109: &mt('The name of the new file needs to end with an appropriate file extension to indicate the type of file to create.').'<br />'.
1110: &mt('The following are valid extensions: [_1].',$validexts).
1111: '</p></form><p>'.
1112: '<form name="fileaction" action="/adm/cfile" method="post">'.
1113: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
1114: '<input type="hidden" name="action" value="newfile" />'.
1115: '<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" />'.
1116: '</span></form></p>'.
1117: '<p><form action="'.&url($fn).
1118: '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></form></p>');
1119: } elsif (($type ne 'warning') && ($warnings eq '') && ($result eq '')) {
1120: my $query = "";
1121: $query .= "?mode=" . $env{'form.mode'} unless (!exists($env{'form.mode'}) || !length($env{'form.mode'}));
1122: $request->print('
1123: <script type="text/javascript">
1124: window.location = "'.&url($newfilename,'js'). $query .'";
1125: </script>');
1126: } else {
1127: $request->print($warnings.$result);
1128: $request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
1129: $request->print('</form>');
1130: $request->print('<form action="'.&url($newfilename).
1131: '" method="post"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
1132: $request->print('<form action="'.&url($fn).
1133: '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1134: }
1135: }
1136: return;
1137: }
1138:
1139: sub filename_check {
1140: my ($newfilename) = @_;
1141: ##Informs User (name).(number).(extension) not allowed
1142: if($newfilename =~ /\.(\d+)\.(\w+)$/){
1143: $r->print('<span class="LC_error">'.$newfilename.
1144: ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
1145: ' '.&mt('Not Allowed').'</span>');
1146: return;
1147: }
1148: if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
1149: $r->print('<span class="LC_error">'.$newfilename.
1150: ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
1151: ' '.&mt('Not Allowed').'</span>');
1152: return;
1153: }
1154: return 'ok';
1155: }
1156:
1157: =pod
1158:
1159: =item phaseone($r, $fn, $uname, $udom)
1160:
1161: Peforms phase one processing of the request. In phase one, error messages
1162: are returned if the request cannot be performed (e.g. attempts to manipulate
1163: files that are nonexistent). If the operation can be performed, what is
1164: about to be done will be presented to the user for confirmation. If the
1165: user confirms the request, then phase two is executed, the action
1166: performed and reported to the user.
1167:
1168: Parameters:
1169:
1170: =over 4
1171:
1172: =item $r - request object [in] - The Apache request being executed.
1173:
1174: =item $fn = string [in] - The filename being manipulated by the
1175: request.
1176:
1177: =item $uname - string [in] Name of user logged in and doing this action.
1178:
1179: =item $udom - string [in] Domain name under which the user logged in.
1180:
1181: =back
1182:
1183: =cut
1184:
1185: sub phaseone {
1186: my ($r,$fn,$uname,$udom)=@_;
1187:
1188: my $doingdir=0;
1189: if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
1190: my ($newfilename,$error,$warnings) =
1191: &cleanDest($env{'form.newfilename'},$doingdir,$fn,$uname,$udom);
1192: unless ($error) {
1193: ($newfilename,$error)=&relativeDest($fn,$newfilename,$uname,$udom);
1194: }
1195: if ($error) {
1196: my $dirlist;
1197: if ($fn=~m{^(.*/)[^/]+$}) {
1198: $dirlist=$1;
1199: } else {
1200: $dirlist=$fn;
1201: }
1202: if ($warnings) {
1203: $r->print($warnings);
1204: }
1205: $r->print('<div class="LC_error">'.$error.'</div>'.
1206: '<p><a href="'.&url($dirlist).'">'.&mt('Return to Directory').
1207: '</a></p>');
1208: return;
1209: }
1210: $r->print('<form action="/adm/cfile" method="post" name="phaseone">'."\n".
1211: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'."\n".
1212: '<input type="hidden" name="phase" value="two" />'."\n".
1213: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />'."\n");
1214:
1215: if ($env{'form.action'} eq 'newfile' ||
1216: $env{'form.action'} eq 'newhtmlfile' ||
1217: $env{'form.action'} eq 'newproblemfile' ||
1218: $env{'form.action'} eq 'newpagefile' ||
1219: $env{'form.action'} eq 'newsequencefile' ||
1220: $env{'form.action'} eq 'newrightsfile' ||
1221: $env{'form.action'} eq 'newstyfile' ||
1222: $env{'form.action'} eq 'newtaskfile' ||
1223: $env{'form.action'} eq 'newlibraryfile' ||
1224: $env{'form.action'} eq 'Select Action') {
1225: my $empty=&mt('Type Name Here');
1226: if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
1227: &NewFile1($r, $uname, $udom, $fn, $newfilename, $warnings);
1228: } else {
1229: if ($warnings) {
1230: $r->print($warnings);
1231: }
1232: $r->print('<p class="LC_error">'
1233: .&mt('No new filename specified.')
1234: .'</p></form>'
1235: );
1236: }
1237: } else {
1238: if ($warnings) {
1239: $r->print($warnings);
1240: }
1241: if ($env{'form.action'} eq 'rename') {
1242: &Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
1243: } elsif ($env{'form.action'} eq 'move') {
1244: &Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
1245: } elsif ($env{'form.action'} eq 'delete') {
1246: &Delete1($r, $uname, $udom, $fn);
1247: } elsif ($env{'form.action'} eq 'decompress') {
1248: &Decompress1($r, $uname, $udom, $fn);
1249: } elsif ($env{'form.action'} eq 'archive') {
1250: if (($uname eq $env{'user.name'}) && ($udom eq $env{'user.domain'})) {
1251: &Archive1($r,$fn);
1252: } else {
1253: $r->print('<p class="LC_error">'
1254: .&mt('Archiving of Authoring Spaces is only permitted by Author')
1255: .'</p></form>'
1256: );
1257: }
1258: } elsif ($env{'form.action'} eq 'copy') {
1259: if ($newfilename) {
1260: &Copy1($r, $uname, $udom, $fn, $newfilename);
1261: } else {
1262: $r->print('<p class="LC_error">'
1263: .&mt('No new filename specified.')
1264: .'</p></form>'
1265: );
1266: }
1267: } elsif ($env{'form.action'} eq 'newdir') {
1268: my $mode = '';
1269: if (exists($env{'form.callingmode'}) ) {
1270: $mode = $env{'form.callingmode'};
1271: }
1272: &NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
1273: }
1274: }
1275: }
1276:
1277: =pod
1278:
1279: =item Rename2($request, $user, $directory, $oldfile, $newfile)
1280:
1281: Performs phase 2 processing of a rename reequest. This is where the
1282: actual rename is performed.
1283:
1284: Parameters
1285:
1286: =over 4
1287:
1288: =item $request - Apache request object [in] The request being processed.
1289:
1290: =item $user - string [in] The name of the user initiating the request.
1291:
1292: =item $directory - string [in] The name of the directory relative to the
1293: construction space top level of the renamed file.
1294:
1295: =item $oldfile - Name of the file.
1296:
1297: =item $newfile - Name of the new file.
1298:
1299: =back
1300:
1301: Returns:
1302:
1303: =over 4
1304:
1305: =item 1 Success.
1306:
1307: =item 0 Failure.
1308:
1309: =cut
1310:
1311: sub Rename2 {
1312:
1313: my ($request, $user, $directory, $oldfile, $newfile) = @_;
1314:
1315: &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
1316: " new file ".$newfile."\n");
1317: &Debug($request, "Target is: ".$directory.'/'.
1318: $newfile);
1319: if (-e $oldfile) {
1320:
1321: my $oRN=$oldfile;
1322: my $nRN=$newfile;
1323: unless (rename($oldfile,$newfile)) {
1324: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1325: return 0;
1326: }
1327: ## If old name.(extension) exits, move under new name.
1328: ## If it doesn't exist and a new.(extension) exists
1329: ## delete it (only concern when renaming over files)
1330: my $tmp1=$oRN.'.meta';
1331: my $tmp2=$nRN.'.meta';
1332: if(-e $tmp1){
1333: unless(rename($tmp1,$tmp2)){ }
1334: } elsif(-e $tmp2){
1335: unlink $tmp2;
1336: }
1337: $tmp1=$oRN.'.save';
1338: $tmp2=$nRN.'.save';
1339: if(-e $tmp1){
1340: unless(rename($tmp1,$tmp2)){ }
1341: } elsif(-e $tmp2){
1342: unlink $tmp2;
1343: }
1344: $tmp1=$oRN.'.log';
1345: $tmp2=$nRN.'.log';
1346: if(-e $tmp1){
1347: unless(rename($tmp1,$tmp2)){ }
1348: } elsif(-e $tmp2){
1349: unlink $tmp2;
1350: }
1351: $tmp1=$oRN.'.bak';
1352: $tmp2=$nRN.'.bak';
1353: if(-e $tmp1){
1354: unless(rename($tmp1,$tmp2)){ }
1355: } elsif(-e $tmp2){
1356: unlink $tmp2;
1357: }
1358: } else {
1359: $request->print(
1360: '<p class="LC_error">'
1361: .&mt('No such file: [_1]',
1362: &display($oldfile))
1363: .'</p></form>'
1364: );
1365: return 0;
1366: }
1367: return 1;
1368: }
1369:
1370: =pod
1371:
1372: =item Delete2($request, $user, $filename)
1373:
1374: Performs phase two of a delete. The user has confirmed that they want
1375: to delete the selected file. The file is deleted and the results of the
1376: delete attempt are indicated.
1377:
1378: Parameters:
1379:
1380: =over 4
1381:
1382: =item $request - Apache Request object [in] the request object for the current
1383: delete operation.
1384:
1385: =item $user - string [in] The name of the user initiating the delete
1386: request.
1387:
1388: =item $filename - string [in] The name of the file, relative to construction
1389: space, to delete.
1390:
1391: =back
1392:
1393: Returns:
1394: 1 - success.
1395: 0 - Failure.
1396:
1397: =cut
1398:
1399: sub Delete2 {
1400: my ($request, $user, $filename) = @_;
1401: if (-d $filename) {
1402: unless (&empty_directory($filename,'Delete2')) {
1403: $request->print('<span class="LC_error">'.&mt('Error: Directory Non Empty').'</span>');
1404: return 0;
1405: } else {
1406: if(-e $filename) {
1407: unless(rmdir($filename)) {
1408: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1409: return 0;
1410: }
1411: } else {
1412: $request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1413: return 0;
1414: }
1415: }
1416: } else {
1417: if(-e $filename) {
1418: unless(unlink($filename)) {
1419: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1420: return 0;
1421: }
1422: } else {
1423: $request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1424: return 0;
1425: }
1426: }
1427: return 1;
1428: }
1429:
1430: =pod
1431:
1432: =item Copy2($request, $username, $dir, $oldfile, $newfile)
1433:
1434: Performs phase 2 of a copy. The file is copied and the status
1435: of that copy is reported back to the user.
1436:
1437: =over 4
1438:
1439: =item $request - Apache request object [in]; the apache request currently
1440: being executed.
1441:
1442: =item $username - string [in] Name of the user who is requesting the copy.
1443:
1444: =item $dir - string [in] Directory path relative to the construction space
1445: of the destination file.
1446:
1447: =item $oldfile - string [in] Name of the source file.
1448:
1449: =item $newfile - string [in] Name of the destination file.
1450:
1451:
1452: =back
1453:
1454: Returns 0 failure, and 1 successs.
1455:
1456: =cut
1457:
1458: sub Copy2 {
1459: my ($request, $username, $dir, $oldfile, $newfile) = @_;
1460: &Debug($request ,"Will try to copy $oldfile to $newfile");
1461: if(-e $oldfile) {
1462: if ($oldfile eq $newfile) {
1463: $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>');
1464: return 1;
1465: }
1466: unless (copy($oldfile, $newfile)) {
1467: $request->print('<span class="LC_error">'.&mt('copy Error').': '.$!.'</span>');
1468: return 0;
1469: } elsif (!chmod(0660, $newfile)) {
1470: $request->print('<span class="LC_error">'.&mt('chmod error').': '.$!.'</span>');
1471: return 0;
1472: } elsif (-e $oldfile.'.meta' &&
1473: !copy($oldfile.'.meta', $newfile.'.meta') &&
1474: !chmod(0660, $newfile.'.meta')) {
1475: $request->print('<span class="LC_error">'.&mt('copy metadata error').
1476: ': '.$!.'</span>');
1477: return 0;
1478: } else {
1479: return 1;
1480: }
1481: } else {
1482: $request->print('<p class="LC_error">'.&mt('No such file').'</p>');
1483: return 0;
1484: }
1485: return 1;
1486: }
1487:
1488: =pod
1489:
1490: =item NewDir2($request, $user, $newdirectory)
1491:
1492: Performs phase 2 processing of directory creation. This involves creating the directory and
1493: reporting the results of that creation to the user.
1494:
1495: Parameters:
1496: =over 4
1497:
1498: =item $request - Apache request object [in]. Object representing the current HTTP request.
1499:
1500: =item $user - string [in] The name of the user that is initiating the request.
1501:
1502: =item $newdirectory - string [in] The full path of the directory being created.
1503:
1504: =back
1505:
1506: Returns 0 - failure 1 - success.
1507:
1508: =cut
1509:
1510: sub NewDir2 {
1511: my ($request, $user, $newdirectory) = @_;
1512:
1513: unless(mkdir($newdirectory, 02770)) {
1514: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1515: return 0;
1516: }
1517: unless(chmod(02770, ($newdirectory))) {
1518: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1519: return 0;
1520: }
1521: return 1;
1522: }
1523:
1524: sub decompress2 {
1525: my ($r, $user, $dir, $file) = @_;
1526: &Apache::lonnet::appenv({'cgi.file' => $file});
1527: &Apache::lonnet::appenv({'cgi.dir' => $dir});
1528: my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
1529: $r->print($result);
1530: &Apache::lonnet::delenv('cgi.file');
1531: &Apache::lonnet::delenv('cgi.dir');
1532: return 1;
1533: }
1534:
1535: sub Archive2 {
1536: my ($r,$uname,$udom,$fn,$identifier) = @_;
1537: my %options = (
1538: dir => $fn,
1539: uname => $uname,
1540: udom => $udom,
1541: );
1542: if ($env{'form.adload'}) {
1543: $options{'adload'} = 1;
1544: if ($env{'form.archivefname'} ne '') {
1545: $env{'form.archivefname'} =~ s{\.+}{.}g;
1546: $options{'fname'} = $env{'form.archivefname'};
1547: }
1548: if ($env{'form.archiveext'} ne '') {
1549: $options{'extension'} = $env{'form.archiveext'};
1550: }
1551: }
1552: 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);
1553: my (@include,%oktypes);
1554: map { $oktypes{$_} = 1; } @filetypes;
1555: my @posstypes = &Apache::loncommon::get_env_multiple('form.filetype');
1556: foreach my $type (@posstypes) {
1557: if ($oktypes{$type}) {
1558: push(@include,$type);
1559: }
1560: }
1561: if (scalar(@include) == scalar(@filetypes)) {
1562: $options{'types'} = 'all';
1563: } else {
1564: $options{'types'} = join(',',@include);
1565: }
1566: if (exists($env{'form.recurse'})) {
1567: $options{'recurse'} = 1;
1568: }
1569: if (exists($env{'form.encrypt'})) {
1570: if ($env{'form.enckey'} ne '') {
1571: $options{'encrypt'} = $env{'form.enckey'};
1572: }
1573: }
1574: $options{'format'} = 'tar';
1575: $options{'compress'} = 'gzip';
1576: if ((exists($env{'form.format'})) && $env{'form.format'} =~ /^zip$/i) {
1577: $options{'format'} = 'zip';
1578: delete($options{'compress'});
1579: } elsif ((exists($env{'form.compress'})) && ($env{'form.compress'} =~ /^(xz|bzip2)$/i)) {
1580: $options{'compress'} = lc($env{'form.compress'});
1581: }
1582: my $key = 'cgi.'.$identifier.'.archive';
1583: my $storestring = &Apache::lonnet::freeze_escape(\%options);
1584: &Apache::lonnet::appenv({$key => $storestring,
1585: 'cgi.author.archive' => $identifier});
1586: return 1;
1587: }
1588:
1589: sub Archive3 {
1590: my ($hashref) = @_;
1591: if (ref($hashref) eq 'HASH') {
1592: if (($hashref->{'uname'} eq $env{'user.name'}) &&
1593: ($hashref->{'udom'} eq $env{'user.domain'}) &&
1594: ($env{'environment.canarchive'}) &&
1595: ($env{'form.delarchive'})) {
1596: my $filesdest = $Apache::lonnet::perlvar{'lonPrtDir'}.'/'.$env{'user.name'}.'_'.$env{'user.domain'}.'_archive_'.$env{'form.delarchive'};
1597: if (-e $filesdest) {
1598: my $size = (stat($filesdest))[7];
1599: if (unlink($filesdest)) {
1600: my ($identifier,$suffix) = split(/\./,$env{'form.delarchive'},2);
1601: if (($identifier) && (exists($env{'cgi.'.$identifier.'.archive'}))) {
1602: my $delres = &Apache::lonnet::delenv('cgi.'.$identifier.'.archive');
1603: if (($delres eq 'ok') &&
1604: (exists($env{'cgi.author.archive'})) &&
1605: ($env{'cgi.author.archive'} eq $identifier)) {
1606: &Apache::lonnet::authorarchivelog($hashref,$size,$filesdest,'delete');
1607: &Apache::lonnet::delenv('cgi.author.archive');
1608: }
1609: }
1610: return 1;
1611: }
1612: }
1613: }
1614: }
1615: return 0;
1616: }
1617:
1618: =pod
1619:
1620: =item phasetwo($r, $fn, $uname, $udom,$identifier)
1621:
1622: Controls the phase 2 processing of file management
1623: requests for construction space. In phase one, the user
1624: was asked to confirm the operation. In phase 2, the operation
1625: is performed and the result is shown.
1626:
1627: The strategy is to break out the processing into specific action processors
1628: named action2 where action is the requested action and the 2 denotes
1629: phase 2 processing.
1630:
1631: Parameters:
1632:
1633: =over 4
1634:
1635: =item $r - Apache Request object [in] The request object for this httpd
1636: transaction.
1637:
1638: =item $fn - string [in] A filename indicating the object that is being
1639: manipulated.
1640:
1641: =item $uname - string [in] The name of the user initiating the file management
1642: request.
1643:
1644: =item $udom - string [in] The login domain of the user initiating the
1645: file management request.
1646: =back
1647:
1648: =cut
1649:
1650: sub phasetwo {
1651: my ($r,$fn,$uname,$udom,$identifier)=@_;
1652:
1653: &Debug($r, "loncfile - Entering phase 2 for $fn");
1654:
1655: # Break down the file into its component pieces.
1656:
1657: my $dir; # Directory path
1658: my $main; # Filename.
1659: my $suffix; # Extension.
1660: if ($fn=~m:(.*)/([^/]+):) {
1661: $dir=$1; # Directory path
1662: $main=$2; # Filename.
1663: }
1664: if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1665: $suffix=$1; #This is the actually filename extension if it exists
1666: $main=~s/\.\w+$//; #strip the extension
1667: }
1668: my $dest; #
1669: my $dest_dir; # On success this is where we'll go.
1670: my $disp_newname; #
1671: my $dest_newname; #
1672: &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1673: &Debug($r," newfilename = ".$env{'form.newfilename'});
1674:
1675: my $conspace=$fn;
1676:
1677: &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1678:
1679: &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1680:
1681: # Select the appropriate processing sub.
1682: if ($env{'form.action'} eq 'decompress') {
1683: $main .= '.'.$suffix;
1684: if(!&decompress2($r, $uname, $dir, $main)) {
1685: return ;
1686: }
1687: $dest = $dir."/.";
1688: } elsif ($env{'form.action'} eq 'archive') {
1689: if (($env{'environment.canarchive'}) &&
1690: ($env{'user.name'} eq $uname) &&
1691: ($env{'user.domain'} eq $udom)) {
1692: &Archive2($r,$uname,$udom,$fn,$identifier);
1693: } else {
1694: $r->print(&mt('You do not have permission to export to an archive file in this Authoring Space'));
1695: }
1696: return;
1697: } elsif ($env{'form.action'} eq 'rename' ||
1698: $env{'form.action'} eq 'move') {
1699: if($env{'form.newfilename'}) {
1700: if (!defined($dir)) {
1701: $fn=~m:^(.*)/:;
1702: $dir=$1;
1703: }
1704: if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1705: return;
1706: }
1707: $dest = $dir."/";
1708: $dest_newname = $env{'form.newfilename'};
1709: $env{'form.newfilename'} =~ /.+(\/.+$)/;
1710: $disp_newname = $1;
1711: $disp_newname =~ s/\///;
1712: }
1713: } elsif ($env{'form.action'} eq 'delete') {
1714: if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1715: return ;
1716: }
1717: # Once a resource is deleted, we just list the directory that
1718: # previously held it.
1719: #
1720: $dest = $dir."/."; # Parent dir.
1721: } elsif ($env{'form.action'} eq 'copy') {
1722: if($env{'form.newfilename'}) {
1723: if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1724: return ;
1725: }
1726: $dest = $env{'form.newfilename'};
1727: } else {
1728: $r->print('<p class="LC_error">'.&mt('No New filename specified').'</p></form>');
1729: return;
1730: }
1731:
1732: } elsif ($env{'form.action'} eq 'newdir') {
1733: my $newdir= $env{'form.newfilename'};
1734: if(!&NewDir2($r, $uname, $newdir)) {
1735: return;
1736: }
1737: $dest = $newdir."/";
1738: }
1739: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1740: $r->print(
1741: '<p>'
1742: .&Apache::lonhtmlcommon::confirm_success(&mt('Done'))
1743: .'<br /><a href="javascript:self.close()">'.&mt('Continue').'</a>'
1744: .'</p>'
1745: );
1746: } else {
1747: if ($env{'form.action'} eq 'rename') {
1748: $r->print(
1749: '<p>'.&Apache::lonhtmlcommon::confirm_success(&mt('Done')).'</p>'
1750: .&Apache::lonhtmlcommon::actionbox(
1751: ['<a href="'.&url($dest).'">'.&mt('Return to Directory').'</a>',
1752: '<a href="'.&url($dest_newname).'">'.$disp_newname.'</a>']));
1753: } else {
1754: $r->print(&done($dest));
1755: }
1756: }
1757: }
1758:
1759: sub handler {
1760:
1761: $r=shift;
1762:
1763: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename','mode']);
1764:
1765: &Debug($r, "loncfile.pm - handler entered");
1766: &Debug($r, " filename: ".$env{'form.filename'});
1767: &Debug($r, " newfilename: ".$env{'form.newfilename'});
1768: #
1769: # Determine the root filename
1770: # This could come in as "filename", which actually is a URL, or
1771: # as "qualifiedfilename", which is indeed a real filename in filesystem,
1772: # or in value of decompress form element, or need to be extracted
1773: # from %env from hashref retrieved for cgi.<id>.archive key, where id
1774: # is a unique cgi_id created when an Author creates an archive of
1775: # Authoring Space for download.
1776: #
1777: my ($fn,$archiveref);
1778:
1779: if ($env{'form.filename'}) {
1780: &Debug($r, "test: $env{'form.filename'}");
1781: $fn=&unescape($env{'form.filename'});
1782: $fn=&URLToPath($fn);
1783: } elsif ($env{'form.delarchive'}) {
1784: my ($delarchive,$suffix) = split(/\./,$env{'form.delarchive'});
1785: if (($delarchive) && (exists($env{'cgi.'.$delarchive.'.archive'}))) {
1786: $archiveref = &Apache::lonnet::thaw_unescape($env{'cgi.'.$delarchive.'.archive'});
1787: if (ref($archiveref) eq 'HASH') {
1788: $fn = $archiveref->{'dir'};
1789: }
1790: }
1791: } elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {
1792: #Just hijack the script only the first time around to inject the
1793: #correct information for further processing
1794: if ($env{'form.decompress'} ne '') {
1795: $fn=&unescape($env{'form.decompress'});
1796: $fn=&URLToPath($fn);
1797: $env{'form.action'}="decompress";
1798: }
1799: } elsif ($env{'form.qualifiedfilename'}) {
1800: $fn=$env{'form.qualifiedfilename'};
1801: } else {
1802: &Debug($r, "loncfile::handler - no form.filename");
1803: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1804: ' unspecified filename for cfile', $r->filename);
1805: return HTTP_NOT_FOUND;
1806: }
1807:
1808: unless ($fn) {
1809: &Debug($r, "loncfile::handler - doctored url is empty");
1810: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1811: ' trying to cfile non-existing file', $r->filename);
1812: return HTTP_NOT_FOUND;
1813: }
1814:
1815: # ----------------------------------------------------------- Start page output
1816:
1817: my ($uname,$udom) = &Apache::lonnet::constructaccess($fn);
1818: &Debug($r,
1819: "loncfile::handler constructaccess uname = $uname domain = $udom");
1820: if (($uname eq '') || ($udom eq '')) {
1821: $r->log_reason($uname.' at '.$udom.
1822: ' trying to manipulate file '.$env{'form.filename'}.
1823: ' ('.$fn.') - not authorized',
1824: $r->filename);
1825: return HTTP_NOT_ACCEPTABLE;
1826: }
1827: if (($env{'form.delarchive'}) &&
1828: ($env{'environment.canarchive'})) {
1829: &Apache::loncommon::content_type($r,'text/plain');
1830: $r->send_http_header;
1831: if (($env{'user.name'} eq $uname) &&
1832: ($env{'user.domain'} eq $udom)) {
1833: $r->print(&Archive3($archiveref));
1834: } else {
1835: $r->print(&mt('You do not have permission to export to an archive file in this Authoring Space'));
1836: }
1837: return OK;
1838: }
1839:
1840: &Apache::loncommon::content_type($r,'text/html');
1841: $r->send_http_header;
1842:
1843: # Declarations for items used for directory archive requests
1844: my ($js,$identifier,$defext,$archive_earlyout,$archive_idnum);
1845: my $args = {};
1846:
1847: if (($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') &&
1848: (($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport'))) {
1849: my $newdirname = $env{'form.newfilename'};
1850: &js_escape(\$newdirname);
1851: $js = <<"ENDJS";
1852: <script type="text/javascript">
1853: // <![CDATA[
1854: function writeDone() {
1855: window.focus();
1856: opener.document.info.newdir.value = "$newdirname";
1857: setTimeout("self.close()",10000);
1858: }
1859: // ]]>
1860: </script>
1861: ENDJS
1862: $args->{'add_entries'} = { onload => "writeDone()" };
1863: } elsif (($env{'form.action'} eq 'archive') &&
1864: ($env{'environment.canarchive'})) {
1865: # Check if author already has an archive request in process
1866: ($archive_earlyout,$archive_idnum) = &archive_in_progress();
1867: # Check if archive request was in process which author wishes to terminate
1868: if ($env{'form.remove_archive_request'}) {
1869: if ($env{'form.remove_archive_request'} eq $archive_idnum) {
1870: if (exists($env{'cgi.'.$archive_idnum.'.archive'})) {
1871: my $archiveref = &Apache::lonnet::thaw_unescape($env{'cgi.'.$archive_idnum.'.archive'});
1872: if (ref($archiveref) eq 'HASH') {
1873: $env{'form.delarchive'} = $archive_idnum.$archiveref->{'extension'};
1874: if (&Archive3($archiveref)) {
1875: ($archive_earlyout,$archive_idnum) = &archive_in_progress();
1876: }
1877: delete($env{'form.delarchive'});
1878: }
1879: }
1880: }
1881: }
1882: if ($archive_earlyout) {
1883: my $conftext =
1884: &mt('Removing an existing request will terminate an active download of the archive file.');
1885: &js_escape(\$conftext);
1886: $js = <<"ENDJS";
1887: <script type="text/javascript">
1888: // <![CDATA[
1889: function confirmation(form) {
1890: if (form.remove_archive_request.length) {
1891: for (var i=0; i<form.remove_archive_request.length; i++) {
1892: if (form.remove_archive_request[i].checked) {
1893: if (form.remove_archive_request[i].value == '$archive_idnum') {
1894: if (!confirm('$conftext')) {
1895: return false;
1896: }
1897: }
1898: }
1899: }
1900: }
1901: return true;
1902: }
1903: // ]]>
1904: </script>
1905:
1906: ENDJS
1907: } else {
1908: if ($env{'form.phase'} eq 'two') {
1909: $identifier = &Apache::loncommon::get_cgi_id();
1910: $args->{'redirect'} = [0.1,"/cgi-bin/archive.pl?$identifier"];
1911: } else {
1912: my (%location_of,%defaults);
1913: $defext = &archive_tools(\%location_of,\%defaults);
1914: my $check_uncheck_js = &Apache::loncommon::check_uncheck_jscript();
1915: $js = <<"ENDJS";
1916: <script type="text/javascript">
1917: // <![CDATA[
1918: function toggleCompression(form) {
1919: if (document.getElementById('tar_compression')) {
1920: if (form.format.length > 1) {
1921: for (var i=0; i<form.format.length; i++) {
1922: if (form.format[i].checked) {
1923: if (form.format[i].value == 'zip') {
1924: document.getElementById('tar_compression').style.display = 'none';
1925: } else if (form.format[i].value == 'tar') {
1926: document.getElementById('tar_compression').style.display = 'block';
1927: }
1928: break;
1929: }
1930: }
1931: }
1932: }
1933: setArchiveExt(form);
1934: return;
1935: }
1936:
1937: function setArchiveExt(form) {
1938: var newfmt;
1939: var newcomp;
1940: var newdef;
1941: if (document.getElementById('archiveext')) {
1942: if (form.format.length) {
1943: for (var i=0; i<form.format.length; i++) {
1944: if (form.format[i].checked) {
1945: newfmt = form.format[i].value;
1946: break;
1947: }
1948: }
1949: } else {
1950: newfmt = form.format[0];
1951: }
1952: if (newfmt == 'tar') {
1953: if (document.getElementById('tar_compression')) {
1954: if (form.compress.length) {
1955: for (var i=0; i<form.compress.length; i++) {
1956: if (form.compress[i].checked) {
1957: newcomp = form.compress[i].value;
1958: break;
1959: }
1960: }
1961: } else {
1962: newcomp = form.compress[0];
1963: }
1964: }
1965: if (newcomp == 'gzip') {
1966: newdef = newfmt+'.gz';
1967: } else if (newcomp == 'bzip2') {
1968: newdef = newfmt+'.bz2';
1969: } else if (newcomp == 'xz') {
1970: newdef = newfmt+'.'+newcomp;
1971: } else {
1972: newdef = newfmt;
1973: }
1974: } else if (newfmt == 'zip') {
1975: newdef = newfmt;
1976: }
1977: if ((newdef == '') || (newdef == undefined) || (newdef == null)) {
1978: newdef = '.$defext';
1979: }
1980: document.getElementById('archiveext').value = newdef;
1981: }
1982: }
1983:
1984: function resetForm() {
1985: if (document.phaseone.filetype.length) {
1986: for (var i=0; i<document.phaseone.filetype.length; i++) {
1987: document.phaseone.filetype[i].checked = false;
1988: }
1989: }
1990: if (document.getElementById('tar_compression')) {
1991: if (document.phaseone.format.length) {
1992: document.getElementById('tar_compression').style.display = 'block';
1993: for (var i=0; i<document.phaseone.format.length; i++) {
1994: if (document.phaseone.format[i].value == 'tar') {
1995: document.phaseone.format[i].checked = true;
1996: } else {
1997: document.phaseone.format[i].checked = false;
1998: }
1999: }
2000: }
2001: if (document.phaseone.compress.length) {
2002: for (var i=0; i<document.phaseone.compress.length; i++) {
2003: if (document.phaseone.compress[i].value == 'gzip') {
2004: document.phaseone.compress[i].checked = true;
2005: } else {
2006: document.phaseone.compress[i].checked = false;
2007: }
2008: }
2009: }
2010: }
2011: document.phaseone.recurse.checked = false;
2012: var a = document.createElement('a');
2013: var vis;
2014: if (typeof a.download != "undefined") {
2015: document.phaseone.adload.value = '1';
2016: if (document.getElementById('archive_saveas')) {
2017: document.getElementById('archive_saveas').style.display = 'block';
2018: vis = '1';
2019: }
2020: }
2021: if (vis == '1') {
2022: if (document.getElementById('archiveext')) {
2023: document.getElementById('archiveext').value='.$defext';
2024: }
2025: } else {
2026: if (document.getElementById('archive_saveas')) {
2027: document.getElementById('archive_saveas').style.display = 'none';
2028: }
2029: if (document.getElementById('archiveext')) {
2030: document.getElementById('archiveext').value='';
2031: }
2032: }
2033: }
2034:
2035: $check_uncheck_js
2036:
2037: // ]]>
2038: </script>
2039:
2040: ENDJS
2041: $args->{'add_entries'} = { onload => "resetForm()" };
2042: }
2043: }
2044: }
2045: my $londocroot = $r->dir_config('lonDocRoot');
2046: my $trailfile = $fn;
2047: $trailfile =~ s{^/(priv/)}{$londocroot/$1};
2048:
2049: # Breadcrumbs
2050: my $crsauthor;
2051: my $text = 'Authoring Space';
2052: my $title = 'Authoring Space File Operation',
2053: my $href = &Apache::loncommon::authorspace(&url($fn));
2054: if ($env{'request.course.id'}) {
2055: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
2056: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
2057: if ($href eq "/priv/$cdom/$cnum/") {
2058: $text = 'Course Authoring Space';
2059: $title = 'Course Authoring Space File Operation',
2060: $crsauthor = 1;
2061: }
2062: }
2063: &Apache::lonhtmlcommon::clear_breadcrumbs();
2064: &Apache::lonhtmlcommon::add_breadcrumb({
2065: 'text' => $text,
2066: 'href' => $href,
2067: });
2068: &Apache::lonhtmlcommon::add_breadcrumb({
2069: 'text' => 'File Operation',
2070: 'title' => $title,
2071: 'href' => '',
2072: });
2073:
2074: $r->print(&Apache::loncommon::start_page($title,$js,$args)
2075: .&Apache::lonhtmlcommon::breadcrumbs()
2076: .&Apache::loncommon::head_subbox(
2077: &Apache::loncommon::CSTR_pageheader($trailfile))
2078: );
2079:
2080: unless ($env{'form.action'} eq 'archive') {
2081: $r->print('<p>'.&mt('Location').': '.&display($fn).'</p>');
2082: }
2083:
2084: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
2085: unless ($crsauthor) {
2086: $r->print('<p class="LC_info">'
2087: .&mt('Co-Author [_1]',$uname.':'.$udom)
2088: .'</p>'
2089: );
2090: }
2091: }
2092:
2093:
2094: &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
2095: my %action = &Apache::lonlocal::texthash(
2096: 'delete' => 'Delete',
2097: 'rename' => 'Rename',
2098: 'move' => 'Move',
2099: 'newdir' => 'New Directory',
2100: 'decompress' => 'Decompress',
2101: 'archive' => 'Export directory to archive file',
2102: 'copy' => 'Copy',
2103: 'newfile' => 'New Resource',
2104: 'newhtmlfile' => 'New Resource',
2105: 'newproblemfile' => 'New Resource',
2106: 'newpagefile' => 'New Resource',
2107: 'newsequencefile' => 'New Resource',
2108: 'newrightsfile' => 'New Resource',
2109: 'newstyfile' => 'New Resource',
2110: 'newtaskfile' => 'New Resource',
2111: 'newlibraryfile' => 'New Resource',
2112: 'Select Action' => 'New Resource',
2113: );
2114: if ($action{$env{'form.action'}}) {
2115: if ($crsauthor) {
2116: my @disallowed = qw(page sequence rights library);
2117: my $newtype;
2118: if ($env{'form.action'} =~ /^new(\w+)file$/) {
2119: $newtype = $1;
2120: } elsif ($env{'form.action'} eq 'newfile') {
2121: ($newtype) = ($env{'form.newfilename'} =~ m{\.([^/.]+)$});
2122: $newtype = lc($newtype);
2123: }
2124: if (($newtype ne '') &&
2125: (grep(/^\Q$newtype\E$/,@disallowed))) {
2126: $r->print('<p class="LC_error">'
2127: .&mt('Creation of a new file of type: [_1] is not permitted in Course Authoring Space',$newtype)
2128: .'</p>'
2129: .&Apache::loncommon::end_page()
2130: );
2131: return OK;
2132: }
2133: if ($env{'form.action'} eq 'archive') {
2134: $r->print('<p>'.&mt('Location').': '.&display($fn).'</p>'."\n".
2135: '<p class="LC_error">'.
2136: &mt('Export to an archive file is not permitted in Course Authoring Space').
2137: '</p>'."\n".
2138: &Apache::loncommon::end_page());
2139: return OK;
2140: }
2141: } elsif ($env{'form.action'} eq 'archive') {
2142: if ($env{'environment.canarchive'}) {
2143: if ($archive_earlyout) {
2144: my $fname = &url($fn);
2145: my $title = $action{$env{'form.action'}};
2146: &cancel_archive_form($r,$title,$fname,$archive_earlyout,$archive_idnum);
2147: &CloseForm1($r,$fn);
2148: $r->print(&Apache::loncommon::end_page());
2149: return OK;
2150: }
2151: } else {
2152: $r->print('<p>'.&mt('Location').': '.&display($fn).'</p>'."\n".
2153: '<p class="LC_error">'.
2154: &mt('You do not have permission to export to an archive file in this Authoring Space').
2155: '</p>'."\n".
2156: &Apache::loncommon::end_page());
2157: return OK;
2158: }
2159: }
2160: $r->print('<h2>'.$action{$env{'form.action'}}.'</h2>'."\n");
2161: } else {
2162: $r->print('<p class="LC_error">'
2163: .&mt('Unknown Action: [_1]',$env{'form.action'})
2164: .'</p>'
2165: .&Apache::loncommon::end_page()
2166: );
2167: return OK;
2168: }
2169:
2170: if ($env{'form.phase'} eq 'two') {
2171: &Debug($r, "loncfile::handler entering phase2");
2172: &phasetwo($r,$fn,$uname,$udom,$identifier);
2173: } else {
2174: &Debug($r, "loncfile::handler entering phase1");
2175: &phaseone($r,$fn,$uname,$udom);
2176: }
2177:
2178: $r->print(&Apache::loncommon::end_page());
2179: return OK;
2180: }
2181:
2182: 1;
2183: __END__
2184:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>