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