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