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