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.75 2006/04/10 22:47:18 albertel 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 - Construction 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::loncacc;
72: use Apache::lonnet;
73: use Apache::loncommon();
74: use Apache::lonlocal;
75:
76: my $DEBUG=0;
77: my $r; # Needs to be global for some stuff RF.
78:
79: =pod
80:
81: =item Debug($request, $message)
82:
83: If debugging is enabled puts out a debugging message determined by the
84: caller. The debug message goes to the Apache error log file. Debugging
85: is enabled by setting the module global DEBUG variable to nonzero (TRUE).
86:
87: Parameters:
88:
89: =over 4
90:
91: =item $request - The current request operation.
92:
93: =item $message - The message to put in the log file.
94:
95: =back
96:
97: Returns:
98: nothing.
99:
100: =cut
101:
102: sub Debug {
103: # Put out the indicated message butonly if DEBUG is true.
104: if ($DEBUG) {
105: my ($r,$message) = @_;
106: $r->log_reason($message);
107: }
108: }
109:
110: =pod
111:
112: =item URLToPath($url)
113:
114: Convert a URL to a file system path.
115:
116: In order to manipulate the construction space objects, it is necessary
117: to access url identified objects a filespace objects. This function
118: translates a construction space URL to a file system path.
119: Parameters:
120:
121: =over 4
122:
123: =item Url - string [in] The url to convert.
124:
125: =back
126:
127: Returns:
128:
129: =over 4
130:
131: =item The corresponding file system path.
132:
133: =back
134:
135: Global References
136:
137: =over 4
138:
139: =item $r - Request object [in] Referenced in the &Debug calls.
140:
141: =back
142:
143: =cut
144:
145: sub URLToPath {
146: my $Url = shift;
147: &Debug($r, "UrlToPath got: $Url");
148: $Url=~ s/\/+/\//g;
149: $Url=~ s/^http\:\/\/[^\/]+//;
150: $Url=~ s/^\///;
151: $Url=~ s/(\~|priv\/)(\w+)\//\/home\/$2\/public_html\//;
152: &Debug($r, "Returning $Url \n");
153: return $Url;
154: }
155:
156: sub url {
157: my $fn=shift;
158: $fn=~s/^\/home\/(\w+)\/public\_html/\/priv\/$1/;
159: $fn=&HTML::Entities::encode($fn,'<>"&');
160: return $fn;
161: }
162:
163: sub display {
164: my $fn=shift;
165: $fn=~s-^/home/(\w+)/public_html-/priv/$1-;
166: return '<tt>'.$fn.'</tt>';
167: }
168:
169:
170: # see if the file is
171: # a) published (return 0 if not)
172: # b) if, so obsolete (return 0 if not)
173:
174: sub obsolete_unpub {
175: my ($user,$domain,$construct)=@_;
176: my $published=$construct;
177: $published=~
178: s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//;
179: if (-e $published) {
180: if (&Apache::lonnet::metadata($published,'obsolete')) {
181: return 1;
182: }
183: return 0;
184: } else {
185: return 1;
186: }
187: }
188:
189: # see if directory is empty
190: # ignores any .meta, .save, .bak, and .log files created for a previously
191: # published file, which has since been marked obsolete and deleted.
192: sub empty_directory {
193: my ($dirname,$phase) = @_;
194: if (opendir DIR, $dirname) {
195: my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and ..
196: if (@files) {
197: my @orphans = grep(/\.(meta|save|log|bak)$/,@files);
198: if (scalar(@files) - scalar(@orphans) > 0) {
199: return 0;
200: } else {
201: if (($phase eq 'Delete2') && (@orphans > 0)) {
202: foreach my $file (@orphans) {
203: if ($file =~ /\.(meta|save|log|bak)$/) {
204: unlink($dirname.$file);
205: }
206: }
207: }
208: }
209: }
210: closedir(DIR);
211: return 1;
212: }
213: return 0;
214: }
215:
216: =pod
217:
218: =item exists($user, $domain, $file)
219:
220: Determine if a resource file name has been published or exists
221: in the construction space.
222:
223: Parameters:
224:
225: =over 4
226:
227: =item $user - string [in] - Name of the user for which to check.
228:
229: =item $domain - string [in] - Name of the domain in which the resource
230: might have been published.
231:
232: =item $file - string [in] - Name of the file.
233:
234: =back
235:
236: Returns:
237:
238: =over 4
239:
240: =item string - Either where the resource exists as an html string that can
241: be embedded in a dialog or an empty string if the resource
242: does not exist.
243:
244: =back
245:
246: =cut
247:
248: sub exists {
249: my ($user, $domain, $construct) = @_;
250: my $published=$construct;
251: $published=~
252: s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//;
253: my $result='';
254: if ( -d $construct ) {
255: return &mt('Error: destination for operation is an existing directory.');
256: }
257: if ( -e $published) {
258: $result.='<p><font color="red">'.&mt('Warning: target file exists, and has been published!').'</font></p>';
259: } elsif ( -e $construct) {
260: $result.='<p><font color="red">'.&mt('Warning: target file exists!').'</font></p>';
261: }
262: return $result;
263: }
264:
265: =pod
266:
267: =item checksuffix($old, $new)
268:
269: Determine if a resource filename suffix (the stuff after the .) would change
270: as a result of this operation.
271:
272: Parameters:
273:
274: =over 4
275:
276: =item $old = string [in] Previous filename.
277:
278: =item $new = string [in] Resultant filename.
279:
280: =back
281:
282: Returns:
283:
284: =over 4
285:
286: =item Empty string if everything worked.
287:
288: =item String containing an error message if there was a problem.
289:
290: =back
291:
292: =cut
293:
294: sub checksuffix {
295: my ($old,$new) = @_;
296: my $result;
297: my $oldsuffix;
298: my $newsuffix;
299: if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
300: if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
301: if ($oldsuffix ne $newsuffix) {
302: $result.=
303: '<p><font color="red">'.&mt('Warning: change of MIME type!').'</font></p>';
304: }
305: return $result;
306: }
307:
308: sub cleanDest {
309: my ($request,$dest,$subdir,$fn,$uname)=@_;
310: #remove bad characters
311: my $foundbad=0;
312: if ($subdir && $dest =~/\./) {
313: $foundbad=1;
314: $dest=~s/\.//g;
315: }
316: if ($dest=~/[\#\?&%\":]/) {
317: $foundbad=1;
318: $dest=~s/[\#\?&%\":]//g;
319: }
320: if ($dest=~m|/|) {
321: my ($newpath)=($dest=~m|(.*)/|);
322: $newpath=&relativeDest($fn,$newpath,$uname);
323: if (! -d "$newpath") {
324: $request->print("<p><font color=\"red\">".&mt('You have requested to create file in directory [_1] which doesn\'t exist. The requested directory path has been removed from the requested file name.','"<tt>'.$newpath.'</tt>"')."</font></p>");
325: $dest=~s|.*/||;
326: }
327: }
328: if ($foundbad) {
329: $request->print("<p><font color=\"red\">".&mt('Invalid characters in requested name have been removed.')."</font></p>");
330: }
331: return $dest;
332: }
333:
334: sub relativeDest {
335: my ($fn,$newfilename,$uname)=@_;
336: if ($newfilename=~/^\//) {
337: # absolute, simply add path
338: $newfilename='/home/'.$uname.'/public_html/';
339: } else {
340: my $dir=$fn;
341: $dir=~s/\/[^\/]+$//;
342: $newfilename=$dir.'/'.$newfilename;
343: }
344: $newfilename=~s://+:/:g; # remove duplicate /
345: while ($newfilename=~m:/\.\./:) {
346: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
347: }
348: return $newfilename;
349: }
350:
351: =pod
352:
353: =item CloseForm1($request, $user, $file)
354:
355: Close of a form on the successful completion of phase 1 processing
356:
357: Parameters:
358:
359: =over 4
360:
361: =item $request - Apache Request Object [in] - Apache server request object.
362:
363: =item $cancelurl - the url to go to on cancel.
364:
365: =back
366:
367: =cut
368:
369: sub CloseForm1 {
370: my ($request, $fn) = @_;
371: $request->print('<p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
372: $request->print('<form action="'.&url($fn).
373: '" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
374: }
375:
376:
377: =pod
378:
379: =item CloseForm2($request, $user, $directory)
380:
381: Successfully close off the phase 2 form.
382:
383: Parameters:
384:
385: =over 4
386:
387: =item $request - Apache Request object [in] - The request that is being
388: executed.
389:
390: =item $user - string [in] - Name of the user that is initiating the
391: request.
392:
393: =item $directory - string [in] - Directory in which the operation is
394: being done relative to the top level construction space
395: directory.
396:
397: =back
398:
399: =cut
400:
401: sub CloseForm2 {
402: my ($request, $user, $fn) = @_;
403: $request->print('<h3><a href="'.&url($fn).'/">'.&mt('Done').'</a></h3>');
404: }
405:
406: =pod
407:
408: =item Rename1($request, $filename, $user, $domain, $dir)
409:
410: Perform phase 1 processing of the file rename operation.
411:
412: Parameters:
413:
414: =over 4
415:
416: =item $request - Apache Request Object [in] The request object for the
417: current request.
418:
419: =item $filename - The filename relative to construction space.
420:
421: =item $user - Name of the user making the request.
422:
423: =item $domain - User login domain.
424:
425: =item $dir - Directory specification of the path to the file.
426:
427: =back
428:
429: Side effects:
430:
431: =over 4
432:
433: =item A new form is displayed prompting for confirmation. The newfilename
434: hidden field of this form is loaded with
435: new filename relative to the current directory ($dir).
436:
437: =back
438:
439: =cut
440:
441: sub Rename1 {
442: my ($request, $user, $domain, $fn, $newfilename, $style) = @_;
443:
444: if(-e $fn) {
445: if($newfilename) {
446: # is dest a dir
447: if ($style eq 'move') {
448: if (-d $newfilename) {
449: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
450: }
451: }
452: if ($newfilename =~ m|/[^\.]+$|) {
453: #no extension add on original extension
454: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
455: $newfilename.='.'.$1;
456: }
457: }
458: $request->print(&checksuffix($fn, $newfilename));
459: #renaming a dir, delete the trailing /
460: #remove second to last element for current dir
461: if (-d $fn) {
462: $newfilename=~/\.(\w+)$/;
463: if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
464: $request->print('<br /><font color="red">'.
465: &mt('Cannot change MIME type of a directory').
466: '</font>'.
467: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
468: return;
469: }
470: $newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
471: }
472: $newfilename=~s://+:/:g; # remove duplicate /
473: while ($newfilename=~m:/\.\./:) {
474: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
475: }
476: my $return=&exists($user, $domain, $newfilename);
477: $request->print($return);
478: if ($return =~/^Error:/) {
479: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
480: return;
481: }
482: unless (&obsolete_unpub($user,$domain,$fn)) {
483: $request->print('<h3>'.&mt('Cannot rename or move non-obsolete published file').'</h3>'.
484: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
485: return;
486: }
487: my $action;
488: if ($style eq 'rename') {
489: $action=&mt('Rename');
490: } else {
491: $action=&mt('Move');
492: }
493: $request->print('<input type="hidden" name="newfilename" value="'.
494: $newfilename.
495: '" /><p>'.$action.' '.&display($fn).
496: '</tt><br />to '.&display($newfilename).'?</p>');
497: &CloseForm1($request, $fn);
498: } else {
499: $request->print('<p>'.&mt('No new filename specified.').'</p></form>');
500: return;
501: }
502: } else {
503: $request->print('<p> '.&mt('No such file').': '.&display($fn).'</p></form>');
504: return;
505: }
506:
507: }
508:
509: =pod
510:
511: =item Delete1
512:
513: Performs phase 1 processing of the delete operation. In phase one
514: we just check to be sure the file exists.
515:
516: Parameters:
517:
518: =over 4
519:
520: =item $request - Apache Request Object [in] request object for the current
521: request.
522:
523: =item $user - string [in] Name of the user initiating the request.
524:
525: =item $domain - string [in] Domain the initiating user is logged in as
526:
527: =item $filename - string [in] Source filename.
528:
529: =back
530:
531: =cut
532:
533: sub Delete1 {
534: my ($request, $user, $domain, $fn) = @_;
535:
536: if( -e $fn) {
537: $request->print('<input type="hidden" name="newfilename" value="'.
538: $fn.'"/>');
539: if (-d $fn) {
540: unless (&empty_directory($fn,'Delete1')) {
541: $request->print('<h3>'.&mt('Only empty directories may be deleted.').'</h3>'.
542: 'You must delete the contents of the directory first.<br />'.
543: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
544: return;
545: }
546: } else {
547: unless (&obsolete_unpub($user,$domain,$fn)) {
548: $request->print('<h3>'.&mt('Cannot delete non-obsolete published file').'</h3>'.
549: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
550: return;
551: }
552: }
553: $request->print('<p>'.&mt('Delete').' '.&display($fn).'?</p>');
554: &CloseForm1($request, $fn);
555: } else {
556: $request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
557: }
558: }
559:
560: =pod
561:
562: =item Copy1($request, $user, $domain, $filename, $newfilename)
563:
564: Performs phase 1 processing of the construction space copy command.
565: Ensure that the source file exists. Ensure that a destination exists,
566: also warn if the destination already exists.
567:
568: Parameters:
569:
570: =over 4
571:
572: =item $request - Apache Request Object [in] request object for the current
573: request.
574:
575: =item $user - string [in] Name of the user initiating the request.
576:
577: =item $domain - string [in] Domain the initiating user is logged in as
578:
579: =item $fn - string [in] Source filename.
580:
581: =item $newfilename-string [in] Destination filename.
582:
583: =back
584:
585: =cut
586:
587: sub Copy1 {
588: my ($request, $user, $domain, $fn, $newfilename) = @_;
589:
590: if(-e $fn) {
591: # is dest a dir
592: if (-d $newfilename) {
593: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
594: }
595: if ($newfilename =~ m|/[^\.]+$|) {
596: #no extension add on original extension
597: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) { $newfilename.='.'.$1; }
598: }
599: $newfilename=~s://+:/:g; # remove duplicate /
600: while ($newfilename=~m:/\.\./:) {
601: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
602: }
603: $request->print(&checksuffix($fn,$newfilename));
604: my $return=&exists($user, $domain, $newfilename);
605: $request->print($return);
606: if ($return =~/^Error:/) {
607: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
608: return;
609: }
610: $request->print('<input type="hidden" name="newfilename" value="'.
611: $newfilename.
612: '" /><p>'.&mt('Copy').' '.&display($fn).'<br />to '.
613: &display($newfilename).'?</p>');
614: &CloseForm1($request, $fn);
615: } else {
616: $request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
617: }
618: }
619:
620: =pod
621:
622: =item NewDir1
623:
624: Does all phase 1 processing of directory creation:
625: Ensures that the user provides a new directory name,
626: and that the directory does not already exist.
627:
628: Parameters:
629:
630: =over 4
631:
632: =item $request - Apache Request Object [in] - Server request object for the
633: current url.
634:
635: =item $username - Name of the user that is requesting the directory creation.
636:
637: =item $domain - Domain user is in
638:
639: =item $fn - source file.
640:
641: =item $newdir - Name of the directory to be created; path relative to the
642: top level of construction space.
643: =back
644:
645: Side Effects:
646:
647: =over 4
648:
649: =item A new form is displayed. Clicking on the confirmation button
650: causes the newdir operation to transition into phase 2. The hidden field
651: "newfilename" is set with the construction space path to the new directory.
652:
653:
654: =back
655:
656: =cut
657:
658:
659: sub NewDir1 {
660: my ($request, $username, $domain, $fn, $newfilename, $mode) = @_;
661:
662: my $result=&exists($username,$domain,$newfilename);
663: if ($result) {
664: $request->print('<font color="red">'.$result.'</font></form>');
665: } else {
666: if ($mode eq 'testbank') {
667: $request->print('<input type="hidden" name="callingmode" value="testbank">');
668: } elsif ($mode eq 'imsimport') {
669: $request->print('<input type="hidden" name="callingmode" value="imsimport">');
670: }
671: $request->print('<input type="hidden" name="newfilename" value="'.
672: $newfilename.'" /><p>'.&mt('Make new directory').' '.
673: &display($newfilename).'?</p>');
674: &CloseForm1($request, $fn);
675: }
676: }
677:
678:
679: sub Decompress1 {
680: my ($request, $user, $domain, $fn) = @_;
681: if( -e $fn) {
682: $request->print('<input type="hidden" name="newfilename" value="'.$fn.'"/>');
683: $request->print('<p>'.&mt('Decompress').' '.&display($fn).'?</p>');
684: &CloseForm1($request, $fn);
685: } else {
686: $request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
687: }
688: }
689:
690: =pod
691:
692: =item NewFile1
693:
694: Does all phase 1 processing of file creation:
695: Ensures that the user provides a new filename, adds proper extension
696: if needed and that the file does not already exist, if it is a html,
697: problem, page, or sequence, it then creates a form link to hand the
698: actual creation off to the proper handler.
699:
700: Parameters:
701:
702: =over 4
703:
704: =item $request - Apache Request Object [in] - Server request object for the
705: current url.
706:
707: =item $username - Name of the user that is requesting the directory creation.
708:
709: =item $domain - Name of the domain of the user
710:
711: =item $fn - Source file name
712:
713: =item $newfilename
714: - Name of the file to be created; no path information
715: =back
716:
717: Side Effects:
718:
719: =over 4
720:
721: =item 2 new forms are displayed. Clicking on the confirmation button
722: causes the browser to attempt to load the specfied URL, allowing the
723: proper handler to take care of file creation. There is also a Cancel
724: button which returns you to the driectory listing you came from
725:
726: =back
727:
728: =cut
729:
730: sub NewFile1 {
731: my ($request, $user, $domain, $fn, $newfilename) = @_;
732:
733: if ($env{'form.action'} =~ /new(.+)file/) {
734: my $extension=$1;
735:
736: ##Informs User (name).(number).(extension) not allowed
737: if($newfilename =~ /\.(\d+)\.(\w+)$/){
738: $r->print('<font color="red">'.$newfilename.
739: ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
740: ' '.&mt('Not Allowed').'</font>');
741: return;
742: }
743: if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
744: $r->print('<font color="red">'.$newfilename.
745: ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
746: ' '.&mt('Not Allowed').'</font>');
747: return;
748: }
749: if ($newfilename !~ /\Q.$extension\E$/) {
750: if ($newfilename =~ m|^[^\.]*\.([^\.]+)$|) {
751: #already has an extension strip it and add in expected one
752: $newfilename =~ s|.([^\.]+)$||;
753: }
754: $newfilename.=".$extension";
755: }
756: }
757: my $result=&exists($user,$domain,$newfilename);
758: if($result) {
759: $request->print('<font color="red">'.$result.'</font></form>');
760: } else {
761: $request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
762: $request->print('</form>');
763: $request->print('<form action="'.&url($newfilename).
764: '" method="POST"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
765: $request->print('<form action="'.&url($fn).
766: '" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
767: }
768: }
769:
770: =pod
771:
772: =item phaseone($r, $fn, $uname, $udom)
773:
774: Peforms phase one processing of the request. In phase one, error messages
775: are returned if the request cannot be performed (e.g. attempts to manipulate
776: files that are nonexistent). If the operation can be performed, what is
777: about to be done will be presented to the user for confirmation. If the
778: user confirms the request, then phase two is executed, the action
779: performed and reported to the user.
780:
781: Parameters:
782:
783: =over 4
784:
785: =item $r - request object [in] - The Apache request being executed.
786:
787: =item $fn = string [in] - The filename being manipulated by the
788: request.
789:
790: =item $uname - string [in] Name of user logged in and doing this action.
791:
792: =item $udom - string [in] Domain name under which the user logged in.
793:
794: =back
795:
796: =cut
797:
798: sub phaseone {
799: my ($r,$fn,$uname,$udom)=@_;
800:
801: my $doingdir=0;
802: if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
803: my $newfilename=&cleanDest($r,$env{'form.newfilename'},$doingdir,$fn,$uname);
804: $newfilename=&relativeDest($fn,$newfilename,$uname);
805: $r->print('<form action="/adm/cfile" method="post">'.
806: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
807: '<input type="hidden" name="phase" value="two" />'.
808: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
809:
810: if ($env{'form.action'} eq 'rename') {
811: &Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
812: } elsif ($env{'form.action'} eq 'move') {
813: &Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
814: } elsif ($env{'form.action'} eq 'delete') {
815: &Delete1($r, $uname, $udom, $fn);
816: } elsif ($env{'form.action'} eq 'decompress') {
817: &Decompress1($r, $uname, $udom, $fn);
818: } elsif ($env{'form.action'} eq 'copy') {
819: if($newfilename) {
820: &Copy1($r, $uname, $udom, $fn, $newfilename);
821: } else {
822: $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
823: }
824: } elsif ($env{'form.action'} eq 'newdir') {
825: my $mode = '';
826: if (exists($env{'form.callingmode'}) ) {
827: $mode = $env{'form.callingmode'};
828: }
829: &NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
830: } elsif ($env{'form.action'} eq 'newfile' ||
831: $env{'form.action'} eq 'newhtmlfile' ||
832: $env{'form.action'} eq 'newproblemfile' ||
833: $env{'form.action'} eq 'newpagefile' ||
834: $env{'form.action'} eq 'newsequencefile' ||
835: $env{'form.action'} eq 'newrightsfile' ||
836: $env{'form.action'} eq 'newstyfile' ||
837: $env{'form.action'} eq 'newlibraryfile' ||
838: $env{'form.action'} eq 'Select Action') {
839: my $empty=&mt('Type Name Here');
840: if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
841: &NewFile1($r, $uname, $udom, $fn, $newfilename);
842: } else {
843: $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
844: }
845: }
846: }
847:
848: =pod
849:
850: =item Rename2($request, $user, $directory, $oldfile, $newfile)
851:
852: Performs phase 2 processing of a rename reequest. This is where the
853: actual rename is performed.
854:
855: Parameters
856:
857: =over 4
858:
859: =item $request - Apache request object [in] The request being processed.
860:
861: =item $user - string [in] The name of the user initiating the request.
862:
863: =item $directory - string [in] The name of the directory relative to the
864: construction space top level of the renamed file.
865:
866: =item $oldfile - Name of the file.
867:
868: =item $newfile - Name of the new file.
869:
870: =back
871:
872: Returns:
873:
874: =over 4
875:
876: =item 1 Success.
877:
878: =item 0 Failure.
879:
880: =cut
881:
882: sub Rename2 {
883:
884: my ($request, $user, $directory, $oldfile, $newfile) = @_;
885:
886: &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
887: " new file ".$newfile."\n");
888: &Debug($request, "Target is: ".$directory.'/'.
889: $newfile);
890: if (-e $oldfile) {
891:
892: my $oRN=$oldfile;
893: my $nRN=$newfile;
894: unless (rename($oldfile,$newfile)) {
895: $request->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
896: return 0;
897: }
898: ## If old name.(extension) exits, move under new name.
899: ## If it doesn't exist and a new.(extension) exists
900: ## delete it (only concern when renaming over files)
901: my $tmp1=$oRN.'.meta';
902: my $tmp2=$nRN.'.meta';
903: if(-e $tmp1){
904: unless(rename($tmp1,$tmp2)){ }
905: } elsif(-e $tmp2){
906: unlink $tmp2;
907: }
908: $tmp1=$oRN.'.save';
909: $tmp2=$nRN.'.save';
910: if(-e $tmp1){
911: unless(rename($tmp1,$tmp2)){ }
912: } elsif(-e $tmp2){
913: unlink $tmp2;
914: }
915: $tmp1=$oRN.'.log';
916: $tmp2=$nRN.'.log';
917: if(-e $tmp1){
918: unless(rename($tmp1,$tmp2)){ }
919: } elsif(-e $tmp2){
920: unlink $tmp2;
921: }
922: $tmp1=$oRN.'.bak';
923: $tmp2=$nRN.'.bak';
924: if(-e $tmp1){
925: unless(rename($tmp1,$tmp2)){ }
926: } elsif(-e $tmp2){
927: unlink $tmp2;
928: }
929: } else {
930: $request->print("<p> ".&mt('No such file').": ".&display($oldfile).'</p></form>');
931: return 0;
932: }
933: return 1;
934: }
935:
936: =pod
937:
938: =item Delete2($request, $user, $filename)
939:
940: Performs phase two of a delete. The user has confirmed that they want
941: to delete the selected file. The file is deleted and the results of the
942: delete attempt are indicated.
943:
944: Parameters:
945:
946: =over 4
947:
948: =item $request - Apache Request object [in] the request object for the current
949: delete operation.
950:
951: =item $user - string [in] The name of the user initiating the delete
952: request.
953:
954: =item $filename - string [in] The name of the file, relative to construction
955: space, to delete.
956:
957: =back
958:
959: Returns:
960: 1 - success.
961: 0 - Failure.
962:
963: =cut
964:
965: sub Delete2 {
966: my ($request, $user, $filename) = @_;
967: if (-d $filename) {
968: unless (&empty_directory($filename,'Delete2')) {
969: $request->print('<font color="red"> '.&mt('Error: Directory Non Empty').'</font>');
970: return 0;
971: } else {
972: if(-e $filename) {
973: unless(rmdir($filename)) {
974: $request->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
975: return 0;
976: }
977: } else {
978: $request->print('<p> '.&mt('No such file').'. </p></form>');
979: return 0;
980: }
981: }
982: } else {
983: if(-e $filename) {
984: unless(unlink($filename)) {
985: $request->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
986: return 0;
987: }
988: } else {
989: $request->print('<p> '.&mt('No such file').'. </p></form>');
990: return 0;
991: }
992: }
993: return 1;
994: }
995:
996: =pod
997:
998: =item Copy2($request, $username, $dir, $oldfile, $newfile)
999:
1000: Performs phase 2 of a copy. The file is copied and the status
1001: of that copy is reported back to the user.
1002:
1003: =over 4
1004:
1005: =item $request - Apache request object [in]; the apache request currently
1006: being executed.
1007:
1008: =item $username - string [in] Name of the user who is requesting the copy.
1009:
1010: =item $dir - string [in] Directory path relative to the construction space
1011: of the destination file.
1012:
1013: =item $oldfile - string [in] Name of the source file.
1014:
1015: =item $newfile - string [in] Name of the destination file.
1016:
1017:
1018: =back
1019:
1020: Returns 0 failure, and 1 successs.
1021:
1022: =cut
1023:
1024: sub Copy2 {
1025: my ($request, $username, $dir, $oldfile, $newfile) = @_;
1026: &Debug($request ,"Will try to copy $oldfile to $newfile");
1027: if(-e $oldfile) {
1028: if ($oldfile eq $newfile) {
1029: $request->print('<font color="red"> '.&mt('Warning').': '.&mt('Name of new file is the same as name of old file').' - '.&mt('no action taken').'.</font>');
1030: return 1;
1031: }
1032: unless (copy($oldfile, $newfile)) {
1033: $request->print('<font color="red"> '.&mt('copy Error').': '.$!.'</font>');
1034: return 0;
1035: } elsif (!chmod(0660, $newfile)) {
1036: $request->print('<font color="red"> '.&mt('chmod error').': '.$!.'</font>');
1037: return 0;
1038: } elsif (-e $oldfile.'.meta' &&
1039: !copy($oldfile.'.meta', $newfile.'.meta') &&
1040: !chmod(0660, $newfile.'.meta')) {
1041: $request->print('<font color="red"> '.&mt('copy metadata error').
1042: ': '.$!.'</font>');
1043: return 0;
1044: } else {
1045: return 1;
1046: }
1047: } else {
1048: $request->print('<p> '.&mt('No such file').' </p>');
1049: return 0;
1050: }
1051: return 1;
1052: }
1053:
1054: =pod
1055:
1056: =item NewDir2($request, $user, $newdirectory)
1057:
1058: Performs phase 2 processing of directory creation. This involves creating the directory and
1059: reporting the results of that creation to the user.
1060:
1061: Parameters:
1062: =over 4
1063:
1064: =item $request - Apache request object [in]. Object representing the current HTTP request.
1065:
1066: =item $user - string [in] The name of the user that is initiating the request.
1067:
1068: =item $newdirectory - string [in] The full path of the directory being created.
1069:
1070: =back
1071:
1072: Returns 0 - failure 1 - success.
1073:
1074: =cut
1075:
1076: sub NewDir2 {
1077: my ($request, $user, $newdirectory) = @_;
1078:
1079: unless(mkdir($newdirectory, 02770)) {
1080: $request->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
1081: return 0;
1082: }
1083: unless(chmod(02770, ($newdirectory))) {
1084: $request->print('<font color="red"> '.&mt('Error').': '.$!.'</font>');
1085: return 0;
1086: }
1087: return 1;
1088: }
1089:
1090: sub decompress2 {
1091: my ($r, $user, $dir, $file) = @_;
1092: &Apache::lonnet::appenv('cgi.file' => $file);
1093: &Apache::lonnet::appenv('cgi.dir' => $dir);
1094: my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
1095: $r->print($result);
1096: &Apache::lonnet::delenv('cgi.file');
1097: &Apache::lonnet::delenv('cgi.dir');
1098: return 1;
1099: }
1100:
1101: =pod
1102:
1103: =item phasetwo($r, $fn, $uname, $udom)
1104:
1105: Controls the phase 2 processing of file management
1106: requests for construction space. In phase one, the user
1107: was asked to confirm the operation. In phase 2, the operation
1108: is performed and the result is shown.
1109:
1110: The strategy is to break out the processing into specific action processors
1111: named action2 where action is the requested action and the 2 denotes
1112: phase 2 processing.
1113:
1114: Parameters:
1115:
1116: =over 4
1117:
1118: =item $r - Apache Request object [in] The request object for this httpd
1119: transaction.
1120:
1121: =item $fn - string [in] A filename indicating the object that is being
1122: manipulated.
1123:
1124: =item $uname - string [in] The name of the user initiating the file management
1125: request.
1126:
1127: =item $udom - string [in] The login domain of the user initiating the
1128: file management request.
1129: =back
1130:
1131: =cut
1132:
1133: sub phasetwo {
1134: my ($r,$fn,$uname,$udom)=@_;
1135:
1136: &Debug($r, "loncfile - Entering phase 2 for $fn");
1137:
1138: # Break down the file into it's component pieces.
1139:
1140: my $dir; # Directory path
1141: my $main; # Filename.
1142: my $suffix; # Extension.
1143: if ($fn=~m:(.*)/([^/]+):) {
1144: $dir=$1; # Directory path
1145: $main=$2; # Filename.
1146: }
1147: if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1148: $suffix=$1; #This is the actually filename extension if it exists
1149: $main=~s/\.\w+$//; #strip the extension
1150: }
1151: my $dest; # On success this is where we'll go.
1152:
1153: &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1154: &Debug($r," newfilename = ".$env{'form.newfilename'});
1155:
1156: my $conspace=$fn;
1157:
1158: &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1159:
1160: &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1161:
1162: # Select the appropriate processing sub.
1163: if ($env{'form.action'} eq 'decompress') {
1164: $main .= '.'.$suffix;
1165: if(!&decompress2($r, $uname, $dir, $main)) {
1166: return ;
1167: }
1168: $dest = $dir."/.";
1169: } elsif ($env{'form.action'} eq 'rename' ||
1170: $env{'form.action'} eq 'move') {
1171: if($env{'form.newfilename'}) {
1172: if (!defined($dir)) {
1173: $fn=~m:^(.*)/:;
1174: $dir=$1;
1175: }
1176: if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1177: return;
1178: }
1179: $dest = $env{'form.newfilename'};
1180: }
1181: } elsif ($env{'form.action'} eq 'delete') {
1182: if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1183: return ;
1184: }
1185: # Once a resource is deleted, we just list the directory that
1186: # previously held it.
1187: #
1188: $dest = $dir."/."; # Parent dir.
1189: } elsif ($env{'form.action'} eq 'copy') {
1190: if($env{'form.newfilename'}) {
1191: if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1192: return ;
1193: }
1194: $dest = $env{'form.newfilename'};
1195: } else {
1196: $r->print('<p>'.&mt('No New filename specified').'</p></form>');
1197: return;
1198: }
1199:
1200: } elsif ($env{'form.action'} eq 'newdir') {
1201: my $newdir= $env{'form.newfilename'};
1202: if(!&NewDir2($r, $uname, $newdir)) {
1203: return;
1204: }
1205: $dest = $newdir."/";
1206: }
1207: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1208: $r->print('<h3><a href="javascript:self.close()">'.&mt('Done').'</a></h3>');
1209: } else {
1210: $r->print('<h3><a href="'.&url($dest).'">'.&mt('Done').'</a></h3>');
1211: }
1212: }
1213:
1214: sub handler {
1215:
1216: $r=shift;
1217:
1218: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename']);
1219:
1220: &Debug($r, "loncfile.pm - handler entered");
1221: &Debug($r, " filename: ".$env{'form.filename'});
1222: &Debug($r, " newfilename: ".$env{'form.newfilename'});
1223: #
1224: # Determine the root filename
1225: # This could come in as "filename", which actually is a URL, or
1226: # as "qualifiedfilename", which is indeed a real filename in filesystem
1227: #
1228: my $fn;
1229:
1230: if ($env{'form.filename'}) {
1231: &Debug($r, "test: $env{'form.filename'}");
1232: $fn=&Apache::lonnet::unescape($env{'form.filename'});
1233: $fn=&URLToPath($fn);
1234: } elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {
1235: #Just hijack the script only the first time around to inject the
1236: #correct information for further processing
1237: $fn=&Apache::lonnet::unescape($env{'form.decompress'});
1238: $fn=&URLToPath($fn);
1239: $env{'form.action'}="decompress";
1240: } elsif ($env{'form.qualifiedfilename'}) {
1241: $fn=$env{'form.qualifiedfilename'};
1242: } else {
1243: &Debug($r, "loncfile::handler - no form.filename");
1244: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1245: ' unspecified filename for cfile', $r->filename);
1246: return HTTP_NOT_FOUND;
1247: }
1248:
1249: unless ($fn) {
1250: &Debug($r, "loncfile::handler - doctored url is empty");
1251: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1252: ' trying to cfile non-existing file', $r->filename);
1253: return HTTP_NOT_FOUND;
1254: }
1255:
1256: # ----------------------------------------------------------- Start page output
1257: my $uname;
1258: my $udom;
1259:
1260: ($uname,$udom)=
1261: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1262: &Debug($r,
1263: "loncfile::handler constructaccess uname = $uname domain = $udom");
1264: unless (($uname) && ($udom)) {
1265: $r->log_reason($uname.' at '.$udom.
1266: ' trying to manipulate file '.$env{'form.filename'}.
1267: ' ('.$fn.') - not authorized',
1268: $r->filename);
1269: return HTTP_NOT_ACCEPTABLE;
1270: }
1271:
1272:
1273: &Apache::loncommon::content_type($r,'text/html');
1274: $r->send_http_header;
1275:
1276: my (%loaditem,$js);
1277:
1278: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1279: my $newdirname = $env{'form.newfilename'};
1280: $js = qq|
1281: <script type="text/javascript">
1282: function writeDone() {
1283: var winName = window.opener
1284: window.focus();
1285: winName.document.dataForm.newdir.value = "$newdirname"
1286: setTimeout("self.close()",10000)
1287: }
1288: </script>
1289: |;
1290: $loaditem{'onload'} => "writeDone()";
1291: }
1292:
1293: $r->print(&Apache::loncommon::start_page('Construction Space File Operation',
1294: $js,
1295: {'add_entries' => \%loaditem,}));
1296:
1297: $r->print('<h3>'.&mt('Location').': '.&display($fn).'</h3>');
1298:
1299: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1300: $r->print('<h3><font color="red">'.&mt('Co-Author').': '.$uname.' at '.$udom.
1301: '</font></h3>');
1302: }
1303:
1304:
1305: &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
1306: if ($env{'form.action'} eq 'delete') {
1307: $r->print('<h3>'.&mt('Delete').'</h3>');
1308: } elsif ($env{'form.action'} eq 'rename') {
1309: $r->print('<h3>'.&mt('Rename').'</h3>');
1310: } elsif ($env{'form.action'} eq 'move') {
1311: $r->print('<h3>'.&mt('Move').'</h3>');
1312: } elsif ($env{'form.action'} eq 'newdir') {
1313: $r->print('<h3>'.&mt('New Directory').'</h3>');
1314: } elsif ($env{'form.action'} eq 'decompress') {
1315: $r->print('<h3>'.&mt('Decompress').'</h3>');
1316: } elsif ($env{'form.action'} eq 'copy') {
1317: $r->print('<h3>'.&mt('Copy').'</h3>');
1318: } elsif ($env{'form.action'} eq 'newfile' ||
1319: $env{'form.action'} eq 'newhtmlfile' ||
1320: $env{'form.action'} eq 'newproblemfile' ||
1321: $env{'form.action'} eq 'newpagefile' ||
1322: $env{'form.action'} eq 'newsequencefile' ||
1323: $env{'form.action'} eq 'newrightsfile' ||
1324: $env{'form.action'} eq 'newstyfile' ||
1325: $env{'form.action'} eq 'newlibraryfile' ||
1326: $env{'form.action'} eq 'Select Action' ) {
1327: $r->print('<h3>'.&mt('New Resource').'</h3>');
1328: } else {
1329: $r->print('<p>'.&mt('Unknown Action').' '.$env{'form.action'}.' </p>'.
1330: &Apache::loncommon::end_page());
1331: return OK;
1332: }
1333: if ($env{'form.phase'} eq 'two') {
1334: &Debug($r, "loncfile::handler entering phase2");
1335: &phasetwo($r,$fn,$uname,$udom);
1336: } else {
1337: &Debug($r, "loncfile::handler entering phase1");
1338: &phaseone($r,$fn,$uname,$udom);
1339: }
1340:
1341: $r->print(&Apache::loncommon::end_page());
1342: return OK;
1343: }
1344:
1345: 1;
1346: __END__
1347:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>