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