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