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