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