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