1: # The LearningOnline Network with CAPA
2: # Handler to rename files, etc, in construction space
3: #
4: # This file responds to the various buttons and events
5: # in the top frame of the construction space directory.
6: # Each event is processed in two phases. The first phase
7: # presents a page that describes the proposed action to the user
8: # and requests confirmation. The second phase commits the action
9: # and displays a page showing the results of the action.
10: #
11: #
12: # $Id: loncfile.pm,v 1.91 2008/12/19 14:01:16 raeburn Exp $
13: #
14: # Copyright Michigan State University Board of Trustees
15: #
16: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
17: #
18: # LON-CAPA is free software; you can redistribute it and/or modify
19: # it under the terms of the GNU General Public License as published by
20: # the Free Software Foundation; either version 2 of the License, or
21: # (at your option) any later version.
22: #
23: # LON-CAPA is distributed in the hope that it will be useful,
24: # but WITHOUT ANY WARRANTY; without even the implied warranty of
25: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26: # GNU General Public License for more details.
27: #
28: # You should have received a copy of the GNU General Public License
29: # along with LON-CAPA; if not, write to the Free Software
30: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31: #
32: # /home/httpd/html/adm/gpl.txt
33: #
34: # http://www.lon-capa.org/
35: #
36: =pod
37:
38: =head1 NAME
39:
40: Apache::loncfile - Construction space file management.
41:
42: =head1 SYNOPSIS
43:
44: Content handler for buttons on the top frame of the construction space
45: directory.
46:
47: =head1 INTRODUCTION
48:
49: loncfile is invoked when buttons in the top frame of the construction
50: space directory listing are clicked. All operations proceed in two phases.
51: The first phase describes to the user exactly what will be done. If the user
52: confirms the operation, the second phase commits the operation and indicates
53: completion. When the user dismisses the output of phase2, they are returned to
54: an "appropriate" directory listing in general.
55:
56: This is part of the LearningOnline Network with CAPA project
57: described at http://www.lon-capa.org.
58:
59: =head2 Subroutines
60:
61: =cut
62:
63: package Apache::loncfile;
64:
65: use strict;
66: use Apache::File;
67: use File::Basename;
68: use File::Copy;
69: use HTML::Entities();
70: use Apache::Constants qw(:common :http :methods);
71: use Apache::loncacc;
72: use Apache::lonnet;
73: use Apache::loncommon();
74: use Apache::lonlocal;
75: use LONCAPA qw(:DEFAULT :match);
76:
77:
78: my $DEBUG=0;
79: my $r; # Needs to be global for some stuff RF.
80:
81: =pod
82:
83: =item Debug($request, $message)
84:
85: If debugging is enabled puts out a debugging message determined by the
86: caller. The debug message goes to the Apache error log file. Debugging
87: is enabled by setting the module global DEBUG variable to nonzero (TRUE).
88:
89: Parameters:
90:
91: =over 4
92:
93: =item $request - The current request operation.
94:
95: =item $message - The message to put in the log file.
96:
97: =back
98:
99: Returns:
100: nothing.
101:
102: =cut
103:
104: sub Debug {
105: # Put out the indicated message butonly if DEBUG is true.
106: if ($DEBUG) {
107: my ($r,$message) = @_;
108: $r->log_reason($message);
109: }
110: }
111:
112: sub done {
113: my ($url)=@_;
114: my $done=&mt("Done");
115: return(<<ENDDONE);
116: <a href="$url">$done</a>
117: <script type="text/javascript">
118: location.href="$url";
119: </script>
120: ENDDONE
121: }
122:
123: =pod
124:
125: =item URLToPath($url)
126:
127: Convert a URL to a file system path.
128:
129: In order to manipulate the construction space objects, it is necessary
130: to access url identified objects a filespace objects. This function
131: translates a construction space URL to a file system path.
132: Parameters:
133:
134: =over 4
135:
136: =item Url - string [in] The url to convert.
137:
138: =back
139:
140: Returns:
141:
142: =over 4
143:
144: =item The corresponding file system path.
145:
146: =back
147:
148: Global References
149:
150: =over 4
151:
152: =item $r - Request object [in] Referenced in the &Debug calls.
153:
154: =back
155:
156: =cut
157:
158: sub URLToPath {
159: my $Url = shift;
160: &Debug($r, "UrlToPath got: $Url");
161: $Url=~ s/\/+/\//g;
162: $Url=~ s/^https?\:\/\/[^\/]+//;
163: $Url=~ s/^\///;
164: $Url=~ s/(\~|priv\/)($match_username)\//\/home\/$2\/public_html\//;
165: &Debug($r, "Returning $Url \n");
166: return $Url;
167: }
168:
169: sub url {
170: my $fn=shift;
171: $fn=~s/^\/home\/($match_username)\/public\_html/\/priv\/$1/;
172: $fn=&HTML::Entities::encode($fn,'<>"&');
173: return $fn;
174: }
175:
176: sub display {
177: my $fn=shift;
178: $fn=~s-^/home/($match_username)/public_html-/priv/$1-;
179: return '<span class="LC_filename">'.$fn.'</span>';
180: }
181:
182:
183: # see if the file is
184: # a) published (return 0 if not)
185: # b) if, so obsolete (return 0 if not)
186:
187: sub obsolete_unpub {
188: my ($user,$domain,$construct)=@_;
189: my $published=$construct;
190: $published=~
191: s/^\/home\/$user\/public\_html\//\/home\/httpd\/html\/res\/$domain\/$user\//;
192: if (-e $published) {
193: if (&Apache::lonnet::metadata($published,'obsolete')) {
194: return 1;
195: }
196: return 0;
197: } else {
198: return 1;
199: }
200: }
201:
202: # see if directory is empty
203: # ignores any .meta, .save, .bak, and .log files created for a previously
204: # published file, which has since been marked obsolete and deleted.
205: sub empty_directory {
206: my ($dirname,$phase) = @_;
207: if (opendir DIR, $dirname) {
208: my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and ..
209: if (@files) {
210: my @orphans = grep(/\.(meta|save|log|bak)$/,@files);
211: if (scalar(@files) - scalar(@orphans) > 0) {
212: return 0;
213: } else {
214: if (($phase eq 'Delete2') && (@orphans > 0)) {
215: foreach my $file (@orphans) {
216: if ($file =~ /\.(meta|save|log|bak)$/) {
217: unlink($dirname.$file);
218: }
219: }
220: }
221: }
222: }
223: closedir(DIR);
224: return 1;
225: }
226: return 0;
227: }
228:
229: =pod
230:
231: =item exists($user, $domain, $file)
232:
233: Determine if a resource file name has been published or exists
234: in the construction space.
235:
236: Parameters:
237:
238: =over 4
239:
240: =item $user - string [in] - Name of the user for which to check.
241:
242: =item $domain - string [in] - Name of the domain in which the resource
243: might have been published.
244:
245: =item $file - string [in] - Name of the file.
246:
247: =item $creating - string [in] - optional, type of object being created,
248: either 'directory' or 'file'. Defaults to
249: 'file' if unspecified.
250:
251: =back
252:
253: Returns:
254:
255: =over 4
256:
257: =item string - Either undef, 'warning' or 'error' depending on the
258: type of problem
259:
260: =item string - Either where the resource exists as an html string that can
261: be embedded in a dialog or an empty string if the resource
262: does not exist.
263:
264: =back
265:
266: =cut
267:
268: sub exists {
269: my ($user, $domain, $construct, $creating) = @_;
270: $creating ||= 'file';
271:
272: my $published=$construct;
273: $published=~
274: s{^/home/$user/public_html/}{/home/httpd/html/res/$domain/$user/};
275: my ($type,$result);
276: if ( -d $construct ) {
277: return ('error','<p><span class="LC_error">'.&mt('Error: destination for operation is an existing directory.').'</span></p>');
278:
279: }
280:
281: if ( -e $published) {
282: if ( -e $construct ) {
283: $type = 'warning';
284: $result.='<p><span class="LC_warning">'.&mt('Warning: target file exists, and has been published!').'</span></p>';
285: } else {
286: my $published_type = (-d $published) ? 'directory' : 'file';
287:
288: if ($published_type eq $creating) {
289: $type = 'warning';
290: $result.='<p><span class="LC_warning">'.&mt("Warning: a published $published_type of this name exists.").'</span></p>';
291: } else {
292: $type = 'error';
293: $result.='<p><span class="LC_error">'.&mt("Error: a published $published_type of this name exists.").'</span></p>';
294: }
295: }
296: } elsif ( -e $construct) {
297: $type = 'warning';
298: $result.='<p><span class="LC_warning">'.&mt('Warning: target file exists!').'</span></p>';
299: }
300:
301: return ($type,$result);
302: }
303:
304: =pod
305:
306: =item checksuffix($old, $new)
307:
308: Determine if a resource filename suffix (the stuff after the .) would change
309: as a result of this operation.
310:
311: Parameters:
312:
313: =over 4
314:
315: =item $old = string [in] Previous filename.
316:
317: =item $new = string [in] Resultant filename.
318:
319: =back
320:
321: Returns:
322:
323: =over 4
324:
325: =item Empty string if everything worked.
326:
327: =item String containing an error message if there was a problem.
328:
329: =back
330:
331: =cut
332:
333: sub checksuffix {
334: my ($old,$new) = @_;
335: my $result;
336: my $oldsuffix;
337: my $newsuffix;
338: if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
339: if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
340: if (lc($oldsuffix) ne lc($newsuffix)) {
341: $result.=
342: '<p><span class="LC_warning">'.&mt('Warning: change of MIME type!').'</span></p>';
343: }
344: return $result;
345: }
346:
347: sub cleanDest {
348: my ($request,$dest,$subdir,$fn,$uname)=@_;
349: #remove bad characters
350: my $foundbad=0;
351: if ($subdir && $dest =~/\./) {
352: $foundbad=1;
353: $dest=~s/\.//g;
354: }
355: $dest =~ s/(\s+$|^\s+)//g;
356: if ($dest=~/[\#\?&%\":]/) {
357: $foundbad=1;
358: $dest=~s/[\#\?&%\":]//g;
359: }
360: if ($dest=~m|/|) {
361: my ($newpath)=($dest=~m|(.*)/|);
362: $newpath=&relativeDest($fn,$newpath,$uname);
363: if (! -d "$newpath") {
364: $request->print("<p><span class=\"LC_error\">".&mt('You have requested to create file in directory [_1] which doesn\'t exist. The requested directory path has been removed from the requested file name.','"'.&display($newpath).'"')."</span></p>");
365: $dest=~s|.*/||;
366: }
367: }
368: if ($dest =~ /\.(\d+)\.(\w+)$/){
369: $request->print('<span class="LC_error">'
370: .&mt('Bad filename [_1].<br /> <tt>(name).(number).(extension)</tt> not allowed. <br /> Removing the <tt>.number.</tt> from requested filename.',&display($dest))
371: .'</span>');
372: $dest =~ s/\.(\d+)(\.\w+)$/$2/;
373: }
374: if ($foundbad) {
375: $request->print("<p><span class=\"LC_error\">".&mt('Invalid characters in requested name have been removed.')."</span></p>");
376: }
377: return $dest;
378: }
379:
380: sub relativeDest {
381: my ($fn,$newfilename,$uname)=@_;
382: if ($newfilename=~/^\//) {
383: # absolute, simply add path
384: $newfilename='/home/'.$uname.'/public_html/';
385: } else {
386: my $dir=$fn;
387: $dir=~s/\/[^\/]+$//;
388: $newfilename=$dir.'/'.$newfilename;
389: }
390: $newfilename=~s://+:/:g; # remove duplicate /
391: while ($newfilename=~m:/\.\./:) {
392: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
393: }
394: return $newfilename;
395: }
396:
397: =pod
398:
399: =item CloseForm1($request, $user, $file)
400:
401: Close of a form on the successful completion of phase 1 processing
402:
403: Parameters:
404:
405: =over 4
406:
407: =item $request - Apache Request Object [in] - Apache server request object.
408:
409: =item $cancelurl - the url to go to on cancel.
410:
411: =back
412:
413: =cut
414:
415: sub CloseForm1 {
416: my ($request, $fn) = @_;
417: $request->print('<p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
418: $request->print('<form action="'.&url($fn).
419: '" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
420: }
421:
422:
423: =pod
424:
425: =item CloseForm2($request, $user, $directory)
426:
427: Successfully close off the phase 2 form.
428:
429: Parameters:
430:
431: =over 4
432:
433: =item $request - Apache Request object [in] - The request that is being
434: executed.
435:
436: =item $user - string [in] - Name of the user that is initiating the
437: request.
438:
439: =item $directory - string [in] - Directory in which the operation is
440: being done relative to the top level construction space
441: directory.
442:
443: =back
444:
445: =cut
446:
447: sub CloseForm2 {
448: my ($request, $user, $fn) = @_;
449: $request->print(&done(&url($fn)));
450: }
451:
452: =pod
453:
454: =item Rename1($request, $filename, $user, $domain, $dir)
455:
456: Perform phase 1 processing of the file rename operation.
457:
458: Parameters:
459:
460: =over 4
461:
462: =item $request - Apache Request Object [in] The request object for the
463: current request.
464:
465: =item $filename - The filename relative to construction space.
466:
467: =item $user - Name of the user making the request.
468:
469: =item $domain - User login domain.
470:
471: =item $dir - Directory specification of the path to the file.
472:
473: =back
474:
475: Side effects:
476:
477: =over 4
478:
479: =item A new form is displayed prompting for confirmation. The newfilename
480: hidden field of this form is loaded with
481: new filename relative to the current directory ($dir).
482:
483: =back
484:
485: =cut
486:
487: sub Rename1 {
488: my ($request, $user, $domain, $fn, $newfilename, $style) = @_;
489:
490: if(-e $fn) {
491: if($newfilename) {
492: # is dest a dir
493: if ($style eq 'move') {
494: if (-d $newfilename) {
495: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
496: }
497: }
498: if ($newfilename =~ m|/[^\.]+$|) {
499: #no extension add on original extension
500: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
501: $newfilename.='.'.$1;
502: }
503: }
504: $request->print(&checksuffix($fn, $newfilename));
505: #renaming a dir, delete the trailing /
506: #remove second to last element for current dir
507: if (-d $fn) {
508: $newfilename=~/\.(\w+)$/;
509: if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
510: $request->print('<br /><span classr="LC_warning">'.
511: &mt('Cannot change MIME type of a directory').
512: '</span>'.
513: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
514: return;
515: }
516: $newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
517: }
518: $newfilename=~s://+:/:g; # remove duplicate /
519: while ($newfilename=~m:/\.\./:) {
520: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
521: }
522: my ($type, $return)=&exists($user, $domain, $newfilename);
523: $request->print($return);
524: if ($type eq 'error') {
525: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
526: return;
527: }
528: unless (&obsolete_unpub($user,$domain,$fn)) {
529: $request->print('<h3>'.&mt('Cannot rename or move non-obsolete published file').'</h3>'.
530: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
531: return;
532: }
533: my $action;
534: if ($style eq 'rename') {
535: $action=&mt('Rename');
536: } else {
537: $action=&mt('Move');
538: }
539: $request->print('<input type="hidden" name="newfilename" value="'.
540: $newfilename.
541: '" /><p>'.$action.' '.&display($fn).
542: '</p><br />to '.&display($newfilename).'?</p>');
543: &CloseForm1($request, $fn);
544: } else {
545: $request->print('<p>'.&mt('No new filename specified.').'</p></form>');
546: return;
547: }
548: } else {
549: $request->print('<p> '.&mt('No such file').': '.&display($fn).'</p></form>');
550: return;
551: }
552:
553: }
554:
555: =pod
556:
557: =item Delete1
558:
559: Performs phase 1 processing of the delete operation. In phase one
560: we just check to be sure the file exists.
561:
562: Parameters:
563:
564: =over 4
565:
566: =item $request - Apache Request Object [in] request object for the current
567: request.
568:
569: =item $user - string [in] Name of the user initiating the request.
570:
571: =item $domain - string [in] Domain the initiating user is logged in as
572:
573: =item $filename - string [in] Source filename.
574:
575: =back
576:
577: =cut
578:
579: sub Delete1 {
580: my ($request, $user, $domain, $fn) = @_;
581:
582: if( -e $fn) {
583: $request->print('<input type="hidden" name="newfilename" value="'.
584: $fn.'"/>');
585: if (-d $fn) {
586: unless (&empty_directory($fn,'Delete1')) {
587: $request->print('<h3>'.&mt('Only empty directories may be deleted.').'</h3>'.
588: 'You must delete the contents of the directory first.<br />'.
589: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
590: return;
591: }
592: } else {
593: unless (&obsolete_unpub($user,$domain,$fn)) {
594: $request->print('<h3>'.&mt('Cannot delete non-obsolete published file').'</h3>'.
595: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
596: return;
597: }
598: }
599: $request->print('<p>'.&mt('Delete').' '.&display($fn).'?</p>');
600: &CloseForm1($request, $fn);
601: } else {
602: $request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
603: }
604: }
605:
606: =pod
607:
608: =item Copy1($request, $user, $domain, $filename, $newfilename)
609:
610: Performs phase 1 processing of the construction space copy command.
611: Ensure that the source file exists. Ensure that a destination exists,
612: also warn if the destination already exists.
613:
614: Parameters:
615:
616: =over 4
617:
618: =item $request - Apache Request Object [in] request object for the current
619: request.
620:
621: =item $user - string [in] Name of the user initiating the request.
622:
623: =item $domain - string [in] Domain the initiating user is logged in as
624:
625: =item $fn - string [in] Source filename.
626:
627: =item $newfilename-string [in] Destination filename.
628:
629: =back
630:
631: =cut
632:
633: sub Copy1 {
634: my ($request, $user, $domain, $fn, $newfilename) = @_;
635:
636: if(-e $fn) {
637: # is dest a dir
638: if (-d $newfilename) {
639: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
640: }
641: if ($newfilename =~ m|/[^\.]+$|) {
642: #no extension add on original extension
643: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) { $newfilename.='.'.$1; }
644: }
645: $newfilename=~s://+:/:g; # remove duplicate /
646: while ($newfilename=~m:/\.\./:) {
647: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
648: }
649: $request->print(&checksuffix($fn,$newfilename));
650: my ($type,$return)=&exists($user, $domain, $newfilename);
651: $request->print($return);
652: if ($type eq 'error') {
653: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
654: return;
655: }
656: $request->print('<input type="hidden" name="newfilename" value="'.
657: $newfilename.
658: '" /><p>'.&mt('Copy').' '.&display($fn).'<br />to '.
659: &display($newfilename).'?</p>');
660: &CloseForm1($request, $fn);
661: } else {
662: $request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
663: }
664: }
665:
666: =pod
667:
668: =item NewDir1
669:
670: Does all phase 1 processing of directory creation:
671: Ensures that the user provides a new directory name,
672: and that the directory does not already exist.
673:
674: Parameters:
675:
676: =over 4
677:
678: =item $request - Apache Request Object [in] - Server request object for the
679: current url.
680:
681: =item $username - Name of the user that is requesting the directory creation.
682:
683: =item $domain - Domain user is in
684:
685: =item $fn - source file.
686:
687: =item $newdir - Name of the directory to be created; path relative to the
688: top level of construction space.
689: =back
690:
691: Side Effects:
692:
693: =over 4
694:
695: =item A new form is displayed. Clicking on the confirmation button
696: causes the newdir operation to transition into phase 2. The hidden field
697: "newfilename" is set with the construction space path to the new directory.
698:
699:
700: =back
701:
702: =cut
703:
704:
705: sub NewDir1 {
706: my ($request, $username, $domain, $fn, $newfilename, $mode) = @_;
707:
708: my ($type, $result)=&exists($username,$domain,$newfilename,'directory');
709: $request->print($result);
710: if ($type eq 'error') {
711: $request->print('</form>');
712: } else {
713: if ($mode eq 'testbank') {
714: $request->print('<input type="hidden" name="callingmode" value="testbank">');
715: } elsif ($mode eq 'imsimport') {
716: $request->print('<input type="hidden" name="callingmode" value="imsimport">');
717: }
718: $request->print('<input type="hidden" name="newfilename" value="'.
719: $newfilename.'" /><p>'.&mt('Make new directory').' '.
720: &display($newfilename).'?</p>');
721: &CloseForm1($request, $fn);
722: }
723: }
724:
725:
726: sub Decompress1 {
727: my ($request, $user, $domain, $fn) = @_;
728: if( -e $fn) {
729: $request->print('<input type="hidden" name="newfilename" value="'.$fn.'"/>');
730: $request->print('<p>'.&mt('Decompress').' '.&display($fn).'?</p>');
731: &CloseForm1($request, $fn);
732: } else {
733: $request->print('<p>'.&mt('No such file').': '.&display($fn).'</p></form>');
734: }
735: }
736:
737: =pod
738:
739: =item NewFile1
740:
741: Does all phase 1 processing of file creation:
742: Ensures that the user provides a new filename, adds proper extension
743: if needed and that the file does not already exist, if it is a html,
744: problem, page, or sequence, it then creates a form link to hand the
745: actual creation off to the proper handler.
746:
747: Parameters:
748:
749: =over 4
750:
751: =item $request - Apache Request Object [in] - Server request object for the
752: current url.
753:
754: =item $username - Name of the user that is requesting the directory creation.
755:
756: =item $domain - Name of the domain of the user
757:
758: =item $fn - Source file name
759:
760: =item $newfilename
761: - Name of the file to be created; no path information
762: =back
763:
764: Side Effects:
765:
766: =over 4
767:
768: =item 2 new forms are displayed. Clicking on the confirmation button
769: causes the browser to attempt to load the specfied URL, allowing the
770: proper handler to take care of file creation. There is also a Cancel
771: button which returns you to the driectory listing you came from
772:
773: =back
774:
775: =cut
776:
777: sub NewFile1 {
778: my ($request, $user, $domain, $fn, $newfilename) = @_;
779:
780: if ($env{'form.action'} =~ /new(.+)file/) {
781: my $extension=$1;
782:
783: ##Informs User (name).(number).(extension) not allowed
784: if($newfilename =~ /\.(\d+)\.(\w+)$/){
785: $r->print('<span class="LC_error">'.$newfilename.
786: ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
787: ' '.&mt('Not Allowed').'</span>');
788: return;
789: }
790: if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
791: $r->print('<span class="LC_error">'.$newfilename.
792: ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
793: ' '.&mt('Not Allowed').'</span>');
794: return;
795: }
796: if ($newfilename !~ /\Q.$extension\E$/) {
797: if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) {
798: #already has an extension strip it and add in expected one
799: $newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|;
800: }
801: $newfilename.=".$extension";
802: }
803: }
804: my ($type, $result)=&exists($user,$domain,$newfilename);
805: $request->print($result);
806: if ($type eq 'error') {
807: $request->print('</form>');
808: } else {
809:
810: $request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
811: $request->print('</form>');
812: $request->print('<form action="'.&url($newfilename).
813: '" method="POST"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
814: $request->print('<form action="'.&url($fn).
815: '" method="POST"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
816: }
817: }
818:
819: =pod
820:
821: =item phaseone($r, $fn, $uname, $udom)
822:
823: Peforms phase one processing of the request. In phase one, error messages
824: are returned if the request cannot be performed (e.g. attempts to manipulate
825: files that are nonexistent). If the operation can be performed, what is
826: about to be done will be presented to the user for confirmation. If the
827: user confirms the request, then phase two is executed, the action
828: performed and reported to the user.
829:
830: Parameters:
831:
832: =over 4
833:
834: =item $r - request object [in] - The Apache request being executed.
835:
836: =item $fn = string [in] - The filename being manipulated by the
837: request.
838:
839: =item $uname - string [in] Name of user logged in and doing this action.
840:
841: =item $udom - string [in] Domain name under which the user logged in.
842:
843: =back
844:
845: =cut
846:
847: sub phaseone {
848: my ($r,$fn,$uname,$udom)=@_;
849:
850: my $doingdir=0;
851: if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
852: my $newfilename=&cleanDest($r,$env{'form.newfilename'},$doingdir,$fn,$uname);
853: $newfilename=&relativeDest($fn,$newfilename,$uname);
854: $r->print('<form action="/adm/cfile" method="post">'.
855: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
856: '<input type="hidden" name="phase" value="two" />'.
857: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
858:
859: if ($env{'form.action'} eq 'rename') {
860: &Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
861: } elsif ($env{'form.action'} eq 'move') {
862: &Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
863: } elsif ($env{'form.action'} eq 'delete') {
864: &Delete1($r, $uname, $udom, $fn);
865: } elsif ($env{'form.action'} eq 'decompress') {
866: &Decompress1($r, $uname, $udom, $fn);
867: } elsif ($env{'form.action'} eq 'copy') {
868: if($newfilename) {
869: &Copy1($r, $uname, $udom, $fn, $newfilename);
870: } else {
871: $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
872: }
873: } elsif ($env{'form.action'} eq 'newdir') {
874: my $mode = '';
875: if (exists($env{'form.callingmode'}) ) {
876: $mode = $env{'form.callingmode'};
877: }
878: &NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
879: } elsif ($env{'form.action'} eq 'newfile' ||
880: $env{'form.action'} eq 'newhtmlfile' ||
881: $env{'form.action'} eq 'newproblemfile' ||
882: $env{'form.action'} eq 'newpagefile' ||
883: $env{'form.action'} eq 'newsequencefile' ||
884: $env{'form.action'} eq 'newrightsfile' ||
885: $env{'form.action'} eq 'newstyfile' ||
886: $env{'form.action'} eq 'newtaskfile' ||
887: $env{'form.action'} eq 'newlibraryfile' ||
888: $env{'form.action'} eq 'Select Action') {
889: my $empty=&mt('Type Name Here');
890: if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
891: &NewFile1($r, $uname, $udom, $fn, $newfilename);
892: } else {
893: $r->print('<p>'.&mt('No new filename specified.').'</p></form>');
894: }
895: }
896: }
897:
898: =pod
899:
900: =item Rename2($request, $user, $directory, $oldfile, $newfile)
901:
902: Performs phase 2 processing of a rename reequest. This is where the
903: actual rename is performed.
904:
905: Parameters
906:
907: =over 4
908:
909: =item $request - Apache request object [in] The request being processed.
910:
911: =item $user - string [in] The name of the user initiating the request.
912:
913: =item $directory - string [in] The name of the directory relative to the
914: construction space top level of the renamed file.
915:
916: =item $oldfile - Name of the file.
917:
918: =item $newfile - Name of the new file.
919:
920: =back
921:
922: Returns:
923:
924: =over 4
925:
926: =item 1 Success.
927:
928: =item 0 Failure.
929:
930: =cut
931:
932: sub Rename2 {
933:
934: my ($request, $user, $directory, $oldfile, $newfile) = @_;
935:
936: &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
937: " new file ".$newfile."\n");
938: &Debug($request, "Target is: ".$directory.'/'.
939: $newfile);
940: if (-e $oldfile) {
941:
942: my $oRN=$oldfile;
943: my $nRN=$newfile;
944: unless (rename($oldfile,$newfile)) {
945: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
946: return 0;
947: }
948: ## If old name.(extension) exits, move under new name.
949: ## If it doesn't exist and a new.(extension) exists
950: ## delete it (only concern when renaming over files)
951: my $tmp1=$oRN.'.meta';
952: my $tmp2=$nRN.'.meta';
953: if(-e $tmp1){
954: unless(rename($tmp1,$tmp2)){ }
955: } elsif(-e $tmp2){
956: unlink $tmp2;
957: }
958: $tmp1=$oRN.'.save';
959: $tmp2=$nRN.'.save';
960: if(-e $tmp1){
961: unless(rename($tmp1,$tmp2)){ }
962: } elsif(-e $tmp2){
963: unlink $tmp2;
964: }
965: $tmp1=$oRN.'.log';
966: $tmp2=$nRN.'.log';
967: if(-e $tmp1){
968: unless(rename($tmp1,$tmp2)){ }
969: } elsif(-e $tmp2){
970: unlink $tmp2;
971: }
972: $tmp1=$oRN.'.bak';
973: $tmp2=$nRN.'.bak';
974: if(-e $tmp1){
975: unless(rename($tmp1,$tmp2)){ }
976: } elsif(-e $tmp2){
977: unlink $tmp2;
978: }
979: } else {
980: $request->print("<p> ".&mt('No such file').": ".&display($oldfile).'</p></form>');
981: return 0;
982: }
983: return 1;
984: }
985:
986: =pod
987:
988: =item Delete2($request, $user, $filename)
989:
990: Performs phase two of a delete. The user has confirmed that they want
991: to delete the selected file. The file is deleted and the results of the
992: delete attempt are indicated.
993:
994: Parameters:
995:
996: =over 4
997:
998: =item $request - Apache Request object [in] the request object for the current
999: delete operation.
1000:
1001: =item $user - string [in] The name of the user initiating the delete
1002: request.
1003:
1004: =item $filename - string [in] The name of the file, relative to construction
1005: space, to delete.
1006:
1007: =back
1008:
1009: Returns:
1010: 1 - success.
1011: 0 - Failure.
1012:
1013: =cut
1014:
1015: sub Delete2 {
1016: my ($request, $user, $filename) = @_;
1017: if (-d $filename) {
1018: unless (&empty_directory($filename,'Delete2')) {
1019: $request->print('<span class="LC_error">'.&mt('Error: Directory Non Empty').'</span>');
1020: return 0;
1021: } else {
1022: if(-e $filename) {
1023: unless(rmdir($filename)) {
1024: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1025: return 0;
1026: }
1027: } else {
1028: $request->print('<p> '.&mt('No such file').'. </p></form>');
1029: return 0;
1030: }
1031: }
1032: } else {
1033: if(-e $filename) {
1034: unless(unlink($filename)) {
1035: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1036: return 0;
1037: }
1038: } else {
1039: $request->print('<p> '.&mt('No such file').'. </p></form>');
1040: return 0;
1041: }
1042: }
1043: return 1;
1044: }
1045:
1046: =pod
1047:
1048: =item Copy2($request, $username, $dir, $oldfile, $newfile)
1049:
1050: Performs phase 2 of a copy. The file is copied and the status
1051: of that copy is reported back to the user.
1052:
1053: =over 4
1054:
1055: =item $request - Apache request object [in]; the apache request currently
1056: being executed.
1057:
1058: =item $username - string [in] Name of the user who is requesting the copy.
1059:
1060: =item $dir - string [in] Directory path relative to the construction space
1061: of the destination file.
1062:
1063: =item $oldfile - string [in] Name of the source file.
1064:
1065: =item $newfile - string [in] Name of the destination file.
1066:
1067:
1068: =back
1069:
1070: Returns 0 failure, and 1 successs.
1071:
1072: =cut
1073:
1074: sub Copy2 {
1075: my ($request, $username, $dir, $oldfile, $newfile) = @_;
1076: &Debug($request ,"Will try to copy $oldfile to $newfile");
1077: if(-e $oldfile) {
1078: if ($oldfile eq $newfile) {
1079: $request->print('<span class="LC_error">'.&mt('Warning').': '.&mt('Name of new file is the same as name of old file').' - '.&mt('no action taken').'.</span>');
1080: return 1;
1081: }
1082: unless (copy($oldfile, $newfile)) {
1083: $request->print('<span class="LC_error">'.&mt('copy Error').': '.$!.'</span>');
1084: return 0;
1085: } elsif (!chmod(0660, $newfile)) {
1086: $request->print('<span class="LC_error">'.&mt('chmod error').': '.$!.'</span>');
1087: return 0;
1088: } elsif (-e $oldfile.'.meta' &&
1089: !copy($oldfile.'.meta', $newfile.'.meta') &&
1090: !chmod(0660, $newfile.'.meta')) {
1091: $request->print('<span class="LC_error">'.&mt('copy metadata error').
1092: ': '.$!.'</span>');
1093: return 0;
1094: } else {
1095: return 1;
1096: }
1097: } else {
1098: $request->print('<p> '.&mt('No such file').' </p>');
1099: return 0;
1100: }
1101: return 1;
1102: }
1103:
1104: =pod
1105:
1106: =item NewDir2($request, $user, $newdirectory)
1107:
1108: Performs phase 2 processing of directory creation. This involves creating the directory and
1109: reporting the results of that creation to the user.
1110:
1111: Parameters:
1112: =over 4
1113:
1114: =item $request - Apache request object [in]. Object representing the current HTTP request.
1115:
1116: =item $user - string [in] The name of the user that is initiating the request.
1117:
1118: =item $newdirectory - string [in] The full path of the directory being created.
1119:
1120: =back
1121:
1122: Returns 0 - failure 1 - success.
1123:
1124: =cut
1125:
1126: sub NewDir2 {
1127: my ($request, $user, $newdirectory) = @_;
1128:
1129: unless(mkdir($newdirectory, 02770)) {
1130: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1131: return 0;
1132: }
1133: unless(chmod(02770, ($newdirectory))) {
1134: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1135: return 0;
1136: }
1137: return 1;
1138: }
1139:
1140: sub decompress2 {
1141: my ($r, $user, $dir, $file) = @_;
1142: &Apache::lonnet::appenv({'cgi.file' => $file});
1143: &Apache::lonnet::appenv({'cgi.dir' => $dir});
1144: my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
1145: $r->print($result);
1146: &Apache::lonnet::delenv('cgi.file');
1147: &Apache::lonnet::delenv('cgi.dir');
1148: return 1;
1149: }
1150:
1151: =pod
1152:
1153: =item phasetwo($r, $fn, $uname, $udom)
1154:
1155: Controls the phase 2 processing of file management
1156: requests for construction space. In phase one, the user
1157: was asked to confirm the operation. In phase 2, the operation
1158: is performed and the result is shown.
1159:
1160: The strategy is to break out the processing into specific action processors
1161: named action2 where action is the requested action and the 2 denotes
1162: phase 2 processing.
1163:
1164: Parameters:
1165:
1166: =over 4
1167:
1168: =item $r - Apache Request object [in] The request object for this httpd
1169: transaction.
1170:
1171: =item $fn - string [in] A filename indicating the object that is being
1172: manipulated.
1173:
1174: =item $uname - string [in] The name of the user initiating the file management
1175: request.
1176:
1177: =item $udom - string [in] The login domain of the user initiating the
1178: file management request.
1179: =back
1180:
1181: =cut
1182:
1183: sub phasetwo {
1184: my ($r,$fn,$uname,$udom)=@_;
1185:
1186: &Debug($r, "loncfile - Entering phase 2 for $fn");
1187:
1188: # Break down the file into its component pieces.
1189:
1190: my $dir; # Directory path
1191: my $main; # Filename.
1192: my $suffix; # Extension.
1193: if ($fn=~m:(.*)/([^/]+):) {
1194: $dir=$1; # Directory path
1195: $main=$2; # Filename.
1196: }
1197: if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1198: $suffix=$1; #This is the actually filename extension if it exists
1199: $main=~s/\.\w+$//; #strip the extension
1200: }
1201: my $dest; #
1202: my $dest_dir; # On success this is where we'll go.
1203: my $disp_newname; #
1204: my $dest_newname; #
1205: &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1206: &Debug($r," newfilename = ".$env{'form.newfilename'});
1207:
1208: my $conspace=$fn;
1209:
1210: &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1211:
1212: &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1213:
1214: # Select the appropriate processing sub.
1215: if ($env{'form.action'} eq 'decompress') {
1216: $main .= '.'.$suffix;
1217: if(!&decompress2($r, $uname, $dir, $main)) {
1218: return ;
1219: }
1220: $dest = $dir."/.";
1221: } elsif ($env{'form.action'} eq 'rename' ||
1222: $env{'form.action'} eq 'move') {
1223: if($env{'form.newfilename'}) {
1224: if (!defined($dir)) {
1225: $fn=~m:^(.*)/:;
1226: $dir=$1;
1227: }
1228: if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1229: return;
1230: }
1231: $dest = $dir."/";
1232: $dest_newname = $env{'form.newfilename'};
1233: $env{'form.newfilename'} =~ /.+(\/.+$)/;
1234: $disp_newname = $1;
1235: $disp_newname =~ s/\///;
1236: }
1237: } elsif ($env{'form.action'} eq 'delete') {
1238: if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1239: return ;
1240: }
1241: # Once a resource is deleted, we just list the directory that
1242: # previously held it.
1243: #
1244: $dest = $dir."/."; # Parent dir.
1245: } elsif ($env{'form.action'} eq 'copy') {
1246: if($env{'form.newfilename'}) {
1247: if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1248: return ;
1249: }
1250: $dest = $env{'form.newfilename'};
1251: } else {
1252: $r->print('<p>'.&mt('No New filename specified').'</p></form>');
1253: return;
1254: }
1255:
1256: } elsif ($env{'form.action'} eq 'newdir') {
1257: my $newdir= $env{'form.newfilename'};
1258: if(!&NewDir2($r, $uname, $newdir)) {
1259: return;
1260: }
1261: $dest = $newdir."/";
1262: }
1263: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1264: $r->print('<h3><a href="javascript:self.close()">'.&mt('Done').'</a></h3>');
1265: } else {
1266: if ($env{'form.action'} eq 'rename') {
1267: $r->print('<h3><a href="'.&url($dest).'">'.&mt('Return to Directory').'</a></h3>');
1268: $r->print('<h3><a href="'.&url($dest_newname).'">'.$disp_newname.'</a></h3>');
1269: } else {
1270: $r->print(&done(&url($dest)));
1271: }
1272: }
1273: }
1274:
1275: sub handler {
1276:
1277: $r=shift;
1278:
1279: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename']);
1280:
1281: &Debug($r, "loncfile.pm - handler entered");
1282: &Debug($r, " filename: ".$env{'form.filename'});
1283: &Debug($r, " newfilename: ".$env{'form.newfilename'});
1284: #
1285: # Determine the root filename
1286: # This could come in as "filename", which actually is a URL, or
1287: # as "qualifiedfilename", which is indeed a real filename in filesystem
1288: #
1289: my $fn;
1290:
1291: if ($env{'form.filename'}) {
1292: &Debug($r, "test: $env{'form.filename'}");
1293: $fn=&unescape($env{'form.filename'});
1294: $fn=&URLToPath($fn);
1295: } elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {
1296: #Just hijack the script only the first time around to inject the
1297: #correct information for further processing
1298: $fn=&unescape($env{'form.decompress'});
1299: $fn=&URLToPath($fn);
1300: $env{'form.action'}="decompress";
1301: } elsif ($env{'form.qualifiedfilename'}) {
1302: $fn=$env{'form.qualifiedfilename'};
1303: } else {
1304: &Debug($r, "loncfile::handler - no form.filename");
1305: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1306: ' unspecified filename for cfile', $r->filename);
1307: return HTTP_NOT_FOUND;
1308: }
1309:
1310: unless ($fn) {
1311: &Debug($r, "loncfile::handler - doctored url is empty");
1312: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1313: ' trying to cfile non-existing file', $r->filename);
1314: return HTTP_NOT_FOUND;
1315: }
1316:
1317: # ----------------------------------------------------------- Start page output
1318: my $uname;
1319: my $udom;
1320:
1321: ($uname,$udom)=
1322: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1323: &Debug($r,
1324: "loncfile::handler constructaccess uname = $uname domain = $udom");
1325: unless (($uname) && ($udom)) {
1326: $r->log_reason($uname.' at '.$udom.
1327: ' trying to manipulate file '.$env{'form.filename'}.
1328: ' ('.$fn.') - not authorized',
1329: $r->filename);
1330: return HTTP_NOT_ACCEPTABLE;
1331: }
1332:
1333:
1334: &Apache::loncommon::content_type($r,'text/html');
1335: $r->send_http_header;
1336:
1337: my (%loaditem,$js);
1338:
1339: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1340: my $newdirname = $env{'form.newfilename'};
1341: $js = qq|
1342: <script type="text/javascript">
1343: function writeDone() {
1344: window.focus();
1345: opener.document.info.newdir.value = "$newdirname";
1346: setTimeout("self.close()",10000);
1347: }
1348: </script>
1349: |;
1350: $loaditem{'onload'} = "writeDone()";
1351: }
1352:
1353: $r->print(&Apache::loncommon::start_page('Construction Space File Operation',
1354: $js,
1355: {'add_entries' => \%loaditem,}));
1356:
1357: $r->print('<h3>'.&mt('Location').': '.&display($fn).'</h3>');
1358:
1359: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1360: $r->print('<h3><span class="LC_error">'.&mt('Co-Author').': '.$uname.' at '.$udom.
1361: '</span></h3>');
1362: }
1363:
1364:
1365: &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
1366: if ($env{'form.action'} eq 'delete') {
1367: $r->print('<h3>'.&mt('Delete').'</h3>');
1368: } elsif ($env{'form.action'} eq 'rename') {
1369: $r->print('<h3>'.&mt('Rename').'</h3>');
1370: } elsif ($env{'form.action'} eq 'move') {
1371: $r->print('<h3>'.&mt('Move').'</h3>');
1372: } elsif ($env{'form.action'} eq 'newdir') {
1373: $r->print('<h3>'.&mt('New Directory').'</h3>');
1374: } elsif ($env{'form.action'} eq 'decompress') {
1375: $r->print('<h3>'.&mt('Decompress').'</h3>');
1376: } elsif ($env{'form.action'} eq 'copy') {
1377: $r->print('<h3>'.&mt('Copy').'</h3>');
1378: } elsif ($env{'form.action'} eq 'newfile' ||
1379: $env{'form.action'} eq 'newhtmlfile' ||
1380: $env{'form.action'} eq 'newproblemfile' ||
1381: $env{'form.action'} eq 'newpagefile' ||
1382: $env{'form.action'} eq 'newsequencefile' ||
1383: $env{'form.action'} eq 'newrightsfile' ||
1384: $env{'form.action'} eq 'newstyfile' ||
1385: $env{'form.action'} eq 'newtaskfile' ||
1386: $env{'form.action'} eq 'newlibraryfile' ||
1387: $env{'form.action'} eq 'Select Action' ) {
1388: $r->print('<h3>'.&mt('New Resource').'</h3>');
1389: } else {
1390: $r->print('<p>'.&mt('Unknown Action').' '.$env{'form.action'}.' </p>'.
1391: &Apache::loncommon::end_page());
1392: return OK;
1393: }
1394: if ($env{'form.phase'} eq 'two') {
1395: &Debug($r, "loncfile::handler entering phase2");
1396: &phasetwo($r,$fn,$uname,$udom);
1397: } else {
1398: &Debug($r, "loncfile::handler entering phase1");
1399: &phaseone($r,$fn,$uname,$udom);
1400: }
1401:
1402: $r->print(&Apache::loncommon::end_page());
1403: return OK;
1404: }
1405:
1406: 1;
1407: __END__
1408:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>