Annotation of loncom/publisher/loncfile.pm, revision 1.33
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.33 ! www 12: # $Id: loncfile.pm,v 1.32 2003/06/19 21:04:37 albertel 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' ||
882: $ENV{'form.action'} eq 'Select Action') {
1.25 albertel 883: if($ENV{'form.newfilename'}) {
884: my $newfilename = $ENV{'form.newfilename'};
885: if (!defined($dir)) {
886: $fn=~m:(.*)/:;
887: $dir=$1;
888: }
889: &NewFile1($r, $uname, $udom, $dir, $newfilename);
890: } else {
891: $r->print('<p>No new filename specified.</p></form>');
1.22 albertel 892: }
1.12 foxr 893: }
894: }
895:
896: =pod
897:
898: =item Rename2($request, $user, $directory, $oldfile, $newfile)
899:
1.17 harris41 900: Performs phase 2 processing of a rename reequest. This is where the
1.12 foxr 901: actual rename is performed.
902:
903: Parameters
904:
905: =over 4
906:
907: =item $request - Apache request object [in] The request being processed.
908:
909: =item $user - string [in] The name of the user initiating the request.
910:
911: =item $directory - string [in] The name of the directory relative to the
912: construction space top level of the renamed file.
913:
914: =item $oldfile - Name of the file.
915:
916: =item $newfile - Name of the new file.
917:
918: =back
919:
920: Returns:
921:
922: =over 4
923:
924: =item 1 Success.
925:
926: =item 0 Failure.
927:
928: =cut
929:
930: sub Rename2 {
931:
932: my ($request, $user, $directory, $oldfile, $newfile) = @_;
933:
934: &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
935: " new file ".$newfile."\n");
936: &Debug($request, "Target is: ".$directory.'/'.
937: $newfile);
938:
939: if(-e $oldfile) {
1.27 albertel 940: my $dest;
941:
942: if ($oldfile =~ m|/$|) {
943: #renaming a dir
944: $oldfile =~ s|/$||;
945: $dest=$directory;
946: $dest=~s|(/)([^/]*)$|$1|;
947: $dest.='/'.$newfile;
948: } else {
949: $dest=$directory.'/'.$newfile;
950: }
951:
952: unless(rename($oldfile,$dest)) {
1.23 albertel 953: $request->print('<font color="red">Error: '.$!.'</font>');
1.12 foxr 954: return 0;
955: } else {}
956: } else {
957: $request->print("<p> No such file: /home".$user.'/public_html'.
958: $oldfile.' </p></form>');
959: return 0;
960: }
961: return 1;
962: }
963: =pod
964:
965: =item Delete2($request, $user, $filename)
966:
967: Performs phase two of a delete. The user has confirmed that they want
968: to delete the selected file. The file is deleted and the results of the
969: delete attempt are indicated.
970:
971: Parameters:
972:
973: =over 4
974:
975: =item $request - Apache Request object [in] the request object for the current
976: delete operation.
977:
978: =item $user - string [in] The name of the user initiating the delete
979: request.
980:
1.17 harris41 981: =item $filename - string [in] The name of the file, relative to construction
982: space, to delete.
1.12 foxr 983:
984: =back
985:
986: Returns:
987: 1 - success.
988: 0 - Failure.
989:
990: =cut
991:
992: sub Delete2 {
993: my ($request, $user, $filename) = @_;
994:
995: if(-e $filename) {
996: unless(unlink($filename)) {
1.23 albertel 997: $request->print('<font color="red">Error: '.$!.'</font>');
1.12 foxr 998: return 0;
999: }
1000: } else {
1.23 albertel 1001: $request->print('<p> No such file. </p></form');
1.12 foxr 1002: return 0;
1003: }
1004: return 1;
1005: }
1006:
1007: =pod
1008:
1009: =item Copy2($request, $username, $dir, $oldfile, $newfile)
1010:
1011: Performs phase 2 of a copy. The file is copied and the status
1012: of that copy is reported back to the user.
1013:
1014: =over 4
1015:
1016: =item $request - Apache request object [in]; the apache request currently
1017: being executed.
1018:
1019: =item $username - string [in] Name of the user who is requesting the copy.
1020:
1021: =item $dir - string [in] Directory path relative to the construction space
1022: of the destination file.
1023:
1024: =item $oldfile - string [in] Name of the source file.
1025:
1026: =item $newfile - string [in] Name of the destination file.
1027:
1028:
1029: =back
1030:
1031: Returns 0 failure, and 0 successs.
1032:
1033: =cut
1.1 www 1034:
1.12 foxr 1035: sub Copy2 {
1036: my ($request, $username, $dir, $oldfile, $newfile) = @_;
1037: &Debug($request ,"Will try to copy $oldfile to $newfile");
1038: if(-e $oldfile) {
1039: unless (copy($oldfile, $newfile)) {
1.23 albertel 1040: $request->print('<font color="red"> copy Error: '.$!.'</font>');
1.12 foxr 1041: return 0;
1042: } else {
1.14 foxr 1043: unless (chmod(0660, $newfile)) {
1.23 albertel 1044: $request->print('<font color="red"> chmod error: '.$!.'</font>');
1.14 foxr 1045: return 0;
1046: }
1.12 foxr 1047: return 1;
1048: }
1.11 albertel 1049: } else {
1.12 foxr 1050: $request->print('<p> No such file </p>');
1051: return 0;
1.11 albertel 1052: }
1.12 foxr 1053: return 1;
1054: }
1055: =pod
1056:
1057: =item NewDir2($request, $user, $newdirectory)
1.1 www 1058:
1.12 foxr 1059: Performs phase 2 processing of directory creation. This involves creating the directory and
1060: reporting the results of that creation to the user.
1061:
1062: Parameters:
1063: =over 4
1064:
1065: =item $request - Apache request object [in]. Object representing the current HTTP request.
1066:
1067: =item $user - string [in] The name of the user that is initiating the request.
1068:
1069: =item $newdirectory - string [in] The full path of the directory being created.
1070:
1071: =back
1072:
1073: Returns 0 - failure 1 - success.
1074:
1075: =cut
1.8 albertel 1076:
1.12 foxr 1077: sub NewDir2 {
1078: my ($request, $user, $newdirectory) = @_;
1079:
1080: unless(mkdir($newdirectory, 02770)) {
1.23 albertel 1081: $request->print('<font color="red">Error: '.$!.'</font>');
1.12 foxr 1082: return 0;
1083: }
1084: unless(chmod(02770, ($newdirectory))) {
1.23 albertel 1085: $request->print('<font color="red"> Error: '.$!.'</font>');
1.12 foxr 1086: return 0;
1087: }
1088: return 1;
1.1 www 1089: }
1090:
1.12 foxr 1091: =pod
1092:
1093: =item phasetwo($r, $fn, $uname, $udom)
1094:
1095: Controls the phase 2 processing of file management
1096: requests for construction space. In phase one, the user
1097: was asked to confirm the operation. In phase 2, the operation
1098: is performed and the result is shown.
1099:
1100: The strategy is to break out the processing into specific action processors
1101: named action2 where action is the requested action and the 2 denotes
1102: phase 2 processing.
1103:
1104: Parameters:
1105:
1106: =over 4
1107:
1108: =item $r - Apache Request object [in] The request object for this httpd
1109: transaction.
1110:
1111: =item $fn - string [in] A filename indicating the object that is being
1112: manipulated.
1113:
1114: =item $uname - string [in] The name of the user initiating the file management
1115: request.
1116:
1117: =item $udom - string [in] The login domain of the user initiating the
1118: file management request.
1119: =back
1120:
1121: =cut
1122:
1.1 www 1123: sub phasetwo {
1124: my ($r,$fn,$uname,$udom)=@_;
1.12 foxr 1125:
1.9 foxr 1126: &Debug($r, "loncfile - Entering phase 2 for $fn");
1.12 foxr 1127:
1128: # Break down the file into it's component pieces.
1129:
1.27 albertel 1130: my $dir; # Directory path
1131: my $main; # Filename.
1132: my $suffix; # Extension.
1133:
1134: if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) {
1135: $dir=$1; # Directory path
1136: $main=$2; # Filename.
1137: $suffix=$3; # Extension.
1138: }
1139:
1.12 foxr 1140: my $dest; # On success this is where we'll go.
1.9 foxr 1141:
1.12 foxr 1142: &Debug($r,
1143: "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1144: &Debug($r,
1145: " newfilename = ".$ENV{'form.newfilename'});
1.9 foxr 1146:
1147: my $conspace=$fn;
1.12 foxr 1148:
1149: &Debug($r,
1150: "loncfile::phase2 Full construction space name: $conspace");
1151:
1152: &Debug($r,
1153: "loncfie::phase2 action is $ENV{'form.action'}");
1154:
1155: # Select the appropriate processing sub.
1156:
1157: if ($ENV{'form.action'} eq 'rename') { # Rename.
1158: if($ENV{'form.newfilename'}) {
1.27 albertel 1159: if (!defined($dir)) {
1160: $fn=~m:^(.*)/:;
1161: $dir=$1;
1162: }
1.12 foxr 1163: if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
1164: return;
1165: }
1166: # Prepend the directory to the new name to form the basis of the
1167: # url of the new resource.
1168: #
1.27 albertel 1169: #renaming a dir
1170: #remove last element for current dir
1171: if ($fn =~ m|/$|) { $dir =~ s|/[^/]*$||; }
1.12 foxr 1172: $dest = $dir."/".$ENV{'form.newfilename'};
1173: }
1.5 www 1174: } elsif ($ENV{'form.action'} eq 'delete') {
1.12 foxr 1175: if(!&Delete2($r, $uname, $ENV{'form.newfilename'})) {
1176: return ;
1177: }
1178: # Once a resource is deleted, we just list the directory that
1179: # previously held it.
1180: #
1.20 albertel 1181: $dest = $dir."/."; # Parent dir.
1.5 www 1182: } elsif ($ENV{'form.action'} eq 'copy') {
1.12 foxr 1183: if($ENV{'form.newfilename'}) {
1184: if(!&Copy2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
1185: return
1186: }
1187: $dest = $ENV{'form.newfilename'};
1188:
1189: } else {
1.23 albertel 1190: $r->print('<p>No New filename specified</p></form>');
1.12 foxr 1191: return;
1192: }
1193:
1.5 www 1194: } elsif ($ENV{'form.action'} eq 'newdir') {
1.12 foxr 1195: #
1196: # Since the newfilename form field is construction space
1197: # relative, ew need to prepend the current path; now in $fn.
1198: #
1.9 foxr 1199: my $newdir= $fn.$ENV{'form.newfilename'};
1.12 foxr 1200: if(!&NewDir2($r, $uname, $newdir)) {
1201: return;
1202: }
1203: $dest = $newdir."/"
1204: }
1205: #
1206: # Substitute for priv for the first home in $dir to get our
1207: # construction space path.
1208: #
1.22 albertel 1209: $dest=&MakeFinalUrl($r,$dest);
1210:
1211: $r->print('<h3><a href="'.$dest.'">Done</a></h3>');
1212: }
1213:
1214: sub MakeFinalUrl {
1215: my($r,$dest)=@_;
1.12 foxr 1216: &Debug($r, "Final url is: $dest");
1.20 albertel 1217: $dest =~ s|/home/|/priv/|;
1218: $dest =~ s|/public_html||;
1.22 albertel 1219:
1.18 foxr 1220: my $base = &File::Basename::basename($dest);
1.15 foxr 1221: my $dpath= &File::Basename::dirname($dest);
1.20 albertel 1222: if ($base eq '.') { $base=''; }
1.18 foxr 1223: $dest = &HTML::Entities::encode($dpath.'/'.$base);
1.15 foxr 1224:
1.12 foxr 1225: &Debug($r, "Final url after rewrite: $dest");
1.22 albertel 1226: return $dest;
1.1 www 1227: }
1228:
1229: sub handler {
1230:
1.10 foxr 1231: $r=shift;
1.1 www 1232:
1.9 foxr 1233:
1234: &Debug($r, "loncfile.pm - handler entered");
1.12 foxr 1235: &Debug($r, " filename: ".$ENV{'form.filename'});
1236: &Debug($r, " newfilename: ".$ENV{'form.newfilename'});
1.9 foxr 1237:
1.1 www 1238: my $fn;
1239:
1240: if ($ENV{'form.filename'}) {
1.22 albertel 1241: $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
1.9 foxr 1242: &Debug($r, "loncfile::handler - raw url: $fn");
1.10 foxr 1243: # $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
1244: # $fn=~s/^http\:\/\/[^\/]+//;
1245: $fn=URLToPath($fn);
1.9 foxr 1246: &Debug($r, "loncfile::handler - doctored url: $fn");
1247:
1.1 www 1248: } else {
1.9 foxr 1249: &Debug($r, "loncfile::handler - no form.filename");
1.1 www 1250: $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1251: ' unspecified filename for cfile', $r->filename);
1252: return HTTP_NOT_FOUND;
1253: }
1254:
1255: unless ($fn) {
1.9 foxr 1256: &Debug($r, "loncfile::handler - doctored url is empty");
1.1 www 1257: $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
1258: ' trying to cfile non-existing file', $r->filename);
1259: return HTTP_NOT_FOUND;
1260: }
1261:
1262: # ----------------------------------------------------------- Start page output
1263: my $uname;
1264: my $udom;
1265:
1266: ($uname,$udom)=
1267: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1.9 foxr 1268: &Debug($r,
1269: "loncfile::handler constructaccess uname = $uname domain = $udom");
1.1 www 1270: unless (($uname) && ($udom)) {
1271: $r->log_reason($uname.' at '.$udom.
1.4 www 1272: ' trying to manipulate file '.$ENV{'form.filename'}.
1.1 www 1273: ' ('.$fn.') - not authorized',
1274: $r->filename);
1275: return HTTP_NOT_ACCEPTABLE;
1276: }
1277:
1278: $fn=~s/\/\~(\w+)//;
1.9 foxr 1279: &Debug($r, "loncfile::handler ~ removed filename: $fn");
1.1 www 1280:
1281: $r->content_type('text/html');
1282: $r->send_http_header;
1283:
1284: $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
1285:
1.33 ! www 1286: $r->print(&Apache::loncommon::bodytag('File Operation'));
1.1 www 1287:
1288:
1289: $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
1290:
1291: if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
1.23 albertel 1292: $r->print('<h3><font color="red">Co-Author: '.$uname.' at '.$udom.
1.1 www 1293: '</font></h3>');
1294: }
1295:
1.9 foxr 1296:
1297: &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
1.2 www 1298: if ($ENV{'form.action'} eq 'delete') {
1.9 foxr 1299:
1.2 www 1300: $r->print('<h3>Delete</h3>');
1301: } elsif ($ENV{'form.action'} eq 'rename') {
1302: $r->print('<h3>Rename</h3>');
1303: } elsif ($ENV{'form.action'} eq 'newdir') {
1304: $r->print('<h3>New Directory</h3>');
1305: } elsif ($ENV{'form.action'} eq 'copy') {
1306: $r->print('<h3>Copy</h3>');
1.22 albertel 1307: } elsif ($ENV{'form.action'} eq 'newfile' ||
1308: $ENV{'form.action'} eq 'newhtmlfile' ||
1.28 www 1309: $ENV{'form.action'} eq 'newproblemfile' ||
1310: $ENV{'form.action'} eq 'newpagefile' ||
1.30 albertel 1311: $ENV{'form.action'} eq 'newsequencefile' ||
1312: $ENV{'form.action'} eq 'Select Action' ) {
1.22 albertel 1313: $r->print('<h3>New Resource</h3>');
1.2 www 1314: } else {
1.30 albertel 1315: $r->print('<p>Unknown Action '.$ENV{'form.action'}.' </p></body></html>');
1.2 www 1316: return OK;
1317: }
1.1 www 1318: if ($ENV{'form.phase'} eq 'two') {
1.9 foxr 1319: &Debug($r, "loncfile::handler entering phase2");
1.4 www 1320: &phasetwo($r,$fn,$uname,$udom);
1.1 www 1321: } else {
1.9 foxr 1322: &Debug($r, "loncfile::handler entering phase1");
1.3 www 1323: &phaseone($r,$fn,$uname,$udom);
1.1 www 1324: }
1325:
1326: $r->print('</body></html>');
1327: return OK;
1328: }
1329:
1330: 1;
1331: __END__
1.9 foxr 1332:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>