Annotation of loncom/publisher/loncfile.pm, revision 1.127
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.127 ! raeburn 12: # $Id: loncfile.pm,v 1.126 2023/07/14 14:32:57 raeburn 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:
1.119 raeburn 40: Apache::loncfile - Authoring space file management.
1.12 foxr 41:
42: =head1 SYNOPSIS
1.127 ! raeburn 43:
! 44: Content handler for buttons on the top frame of the construction space
1.12 foxr 45: directory.
46:
47: =head1 INTRODUCTION
48:
1.127 ! raeburn 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);
1.15 foxr 71: use Apache::lonnet;
1.33 www 72: use Apache::loncommon();
1.47 sakharuk 73: use Apache::lonlocal;
1.80 albertel 74: use LONCAPA qw(:DEFAULT :match);
75:
1.9 foxr 76:
1.45 taceyjo1 77: my $DEBUG=0;
1.10 foxr 78: my $r; # Needs to be global for some stuff RF.
1.12 foxr 79:
80: =pod
81:
82: =item Debug($request, $message)
83:
1.17 harris41 84: If debugging is enabled puts out a debugging message determined by the
1.12 foxr 85: caller. The debug message goes to the Apache error log file. Debugging
1.17 harris41 86: is enabled by setting the module global DEBUG variable to nonzero (TRUE).
1.12 foxr 87:
88: Parameters:
89:
90: =over 4
1.127 ! raeburn 91:
1.17 harris41 92: =item $request - The current request operation.
1.12 foxr 93:
1.17 harris41 94: =item $message - The message to put in the log file.
1.12 foxr 95:
96: =back
1.127 ! raeburn 97:
1.12 foxr 98: Returns:
99: nothing.
100:
101: =cut
102:
1.9 foxr 103: sub Debug {
1.117 bisitz 104: # Put out the indicated message but only if DEBUG is true.
1.55 albertel 105: if ($DEBUG) {
1.73 albertel 106: my ($r,$message) = @_;
1.55 albertel 107: $r->log_reason($message);
108: }
1.12 foxr 109: }
110:
1.89 www 111: sub done {
1.125 raeburn 112: my ($destfn) = @_;
1.117 bisitz 113: return
114: '<p>'
115: .&Apache::lonhtmlcommon::confirm_success(&mt("Done"))
1.125 raeburn 116: .'<br /><a href="'.&url($destfn).'">'.&mt("Continue").'</a>'
1.117 bisitz 117: .'<script type="text/javascript">'
1.125 raeburn 118: .'location.href="'.&url($destfn,'js').'";'
1.117 bisitz 119: .'</script>'
120: .'</p>';
1.89 www 121: }
122:
1.12 foxr 123: =pod
124:
125: =item URLToPath($url)
126:
127: Convert a URL to a file system path.
1.127 ! raeburn 128:
1.12 foxr 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.
1.127 ! raeburn 137:
1.12 foxr 138: =back
1.127 ! raeburn 139:
1.12 foxr 140: Returns:
141:
142: =over 4
143:
1.127 ! raeburn 144: =item The corresponding file system path.
1.12 foxr 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 {
1.55 albertel 159: my $Url = shift;
160: &Debug($r, "UrlToPath got: $Url");
1.113 raeburn 161: $Url=~ s{^https?\://[^/]+}{};
162: $Url=~ s{//+}{/}g;
163: $Url=~ s{^/}{};
164: $Url=$Apache::lonnet::perlvar{'lonDocRoot'}."/$Url";
1.55 albertel 165: &Debug($r, "Returning $Url \n");
166: return $Url;
1.12 foxr 167: }
168:
1.36 www 169: sub url {
1.125 raeburn 170: my ($fn,$context) = @_;
1.113 raeburn 171: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
172: $fn=~ s/^\Q$londocroot\E//;
173: $fn=~s{/\./}{/}g;
1.125 raeburn 174: if ($context eq 'js') {
175: &js_escape(\$fn);
176: } else {
177: $fn=&HTML::Entities::encode($fn,'\'<>"&');
178: }
1.36 www 179: return $fn;
180: }
181:
182: sub display {
183: my $fn=shift;
1.113 raeburn 184: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
185: $fn=~s/^\Q$londocroot\E//;
186: $fn=~s{/\./}{/}g;
1.87 albertel 187: return '<span class="LC_filename">'.$fn.'</span>';
1.36 www 188: }
189:
1.50 www 190:
191: # see if the file is
192: # a) published (return 0 if not)
193: # b) if, so obsolete (return 0 if not)
194:
195: sub obsolete_unpub {
196: my ($user,$domain,$construct)=@_;
1.112 raeburn 197: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.50 www 198: my $published=$construct;
1.112 raeburn 199: $published=~s{^\Q$londocroot/priv/\E}{$londocroot/res/};
1.50 www 200: if (-e $published) {
201: if (&Apache::lonnet::metadata($published,'obsolete')) {
202: return 1;
203: }
204: return 0;
205: } else {
206: return 1;
207: }
208: }
209:
1.70 raeburn 210: # see if directory is empty
1.74 www 211: # ignores any .meta, .save, .bak, and .log files created for a previously
1.70 raeburn 212: # published file, which has since been marked obsolete and deleted.
1.127 ! raeburn 213: # ignores a .DS_Store file put there when viewing directory via webDAV on MacOS.
1.70 raeburn 214: sub empty_directory {
215: my ($dirname,$phase) = @_;
216: if (opendir DIR, $dirname) {
217: my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and ..
1.127 ! raeburn 218: if (@files) {
1.115 raeburn 219: my @orphans = grep(/\.(meta|save|log|bak|DS_Store)$/,@files);
1.127 ! raeburn 220: if (scalar(@files) - scalar(@orphans) > 0) {
1.70 raeburn 221: return 0;
222: } else {
223: if (($phase eq 'Delete2') && (@orphans > 0)) {
224: foreach my $file (@orphans) {
1.74 www 225: if ($file =~ /\.(meta|save|log|bak)$/) {
1.70 raeburn 226: unlink($dirname.$file);
227: }
228: }
229: }
230: }
231: }
232: closedir(DIR);
233: return 1;
234: }
235: return 0;
236: }
1.50 www 237:
1.12 foxr 238: =pod
239:
1.39 www 240: =item exists($user, $domain, $file)
1.12 foxr 241:
1.118 bisitz 242: Determine if a resource filename has been published or exists
1.12 foxr 243: in the construction space.
244:
245: Parameters:
246:
247: =over 4
248:
1.85 albertel 249: =item $user - string [in] - Name of the user for which to check.
1.12 foxr 250:
1.85 albertel 251: =item $domain - string [in] - Name of the domain in which the resource
1.12 foxr 252: might have been published.
253:
1.85 albertel 254: =item $file - string [in] - Name of the file.
255:
256: =item $creating - string [in] - optional, type of object being created,
257: either 'directory' or 'file'. Defaults to
258: 'file' if unspecified.
1.12 foxr 259:
260: =back
261:
262: Returns:
263:
264: =over 4
265:
1.83 albertel 266: =item string - Either undef, 'warning' or 'error' depending on the
267: type of problem
268:
1.12 foxr 269: =item string - Either where the resource exists as an html string that can
270: be embedded in a dialog or an empty string if the resource
271: does not exist.
1.127 ! raeburn 272:
1.12 foxr 273: =back
274:
275: =cut
276:
1.8 albertel 277: sub exists {
1.85 albertel 278: my ($user, $domain, $construct, $creating) = @_;
279: $creating ||= 'file';
280:
1.112 raeburn 281: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.55 albertel 282: my $published=$construct;
1.112 raeburn 283: $published=~s{^\Q$londocroot/priv/\E}{$londocroot/res/};
1.83 albertel 284: my ($type,$result);
1.55 albertel 285: if ( -d $construct ) {
1.124 raeburn 286: return ('error','<p class="LC_error">'.&mt('Error: destination for operation is an existing directory.').'</p>');
1.83 albertel 287:
1.55 albertel 288: }
1.83 albertel 289:
1.55 albertel 290: if ( -e $published) {
1.83 albertel 291: if ( -e $construct ) {
292: $type = 'warning';
1.124 raeburn 293: $result.='<p class="LC_warning">'.&mt('Warning: target file exists, and has been published!').'</p>';
1.83 albertel 294: } else {
1.85 albertel 295: my $published_type = (-d $published) ? 'directory' : 'file';
296:
297: if ($published_type eq $creating) {
298: $type = 'warning';
1.124 raeburn 299: $result.='<p class="LC_warning">'.&mt("Warning: a published $published_type of this name exists.").'</p>';
1.85 albertel 300: } else {
301: $type = 'error';
1.124 raeburn 302: $result.='<p class="LC_error">'.&mt("Error: a published $published_type of this name exists.").'</p>';
1.85 albertel 303: }
1.83 albertel 304: }
1.55 albertel 305: } elsif ( -e $construct) {
1.83 albertel 306: $type = 'warning';
1.124 raeburn 307: $result.='<p class="LC_warning">'.&mt('Warning: target file exists!').'</p>';
1.55 albertel 308: }
1.83 albertel 309:
310: return ($type,$result);
1.8 albertel 311: }
312:
1.12 foxr 313: =pod
314:
315: =item checksuffix($old, $new)
1.127 ! raeburn 316:
1.12 foxr 317: Determine if a resource filename suffix (the stuff after the .) would change
318: as a result of this operation.
319:
320: Parameters:
321:
322: =over 4
323:
324: =item $old = string [in] Previous filename.
325:
326: =item $new = string [in] Resultant filename.
327:
328: =back
329:
330: Returns:
331:
332: =over 4
333:
1.17 harris41 334: =item Empty string if everything worked.
1.12 foxr 335:
336: =item String containing an error message if there was a problem.
337:
338: =back
339:
340: =cut
341:
1.8 albertel 342: sub checksuffix {
343: my ($old,$new) = @_;
344: my $result;
345: my $oldsuffix;
346: my $newsuffix;
347: if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
348: if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
1.82 albertel 349: if (lc($oldsuffix) ne lc($newsuffix)) {
1.12 foxr 350: $result.=
1.124 raeburn 351: '<p class="LC_warning">'.&mt('Warning: change of MIME type!').'></p>';
1.8 albertel 352: }
353: return $result;
354: }
1.32 albertel 355:
356: sub cleanDest {
1.125 raeburn 357: my ($dest,$subdir,$fn,$uname,$udom)=@_;
1.32 albertel 358: #remove bad characters
1.58 albertel 359: my $foundbad=0;
1.125 raeburn 360: my $warnings;
1.106 raeburn 361: my $error='';
1.58 albertel 362: if ($subdir && $dest =~/\./) {
363: $foundbad=1;
364: $dest=~s/\.//g;
365: }
1.87 albertel 366: $dest =~ s/(\s+$|^\s+)//g;
1.72 albertel 367: if ($dest=~/[\#\?&%\":]/) {
1.58 albertel 368: $foundbad=1;
1.72 albertel 369: $dest=~s/[\#\?&%\":]//g;
1.58 albertel 370: }
1.63 albertel 371: if ($dest=~m|/|) {
372: my ($newpath)=($dest=~m|(.*)/|);
1.109 www 373: ($newpath,$error)=&relativeDest($fn,$newpath,$uname,$udom);
1.64 albertel 374: if (! -d "$newpath") {
1.125 raeburn 375: $warnings = '<p class="LC_warning">'
376: .&mt("You have requested to create file in directory [_1] which doesn't exist. The requested directory path has been removed from the requested filename."
377: ,&display($newpath))
378: .'</p>';
1.63 albertel 379: $dest=~s|.*/||;
380: }
381: }
1.125 raeburn 382: if ($dest =~ /\.(\d+)\.(\w+)$/) {
383: $warnings .= '<p class="LC_warning">'
384: .&mt('Bad filename [_1]',&display($dest))
385: .'<br />'
386: .&mt('[_1](name).(number).(extension)[_2] not allowed.','<tt>','</tt>')
387: .'<br />'
388: .&mt('Removing the [_1].number.[_2] from requested filename.','<tt>','</tt>')
389: .'</p>';
1.84 albertel 390: $dest =~ s/\.(\d+)(\.\w+)$/$2/;
391: }
1.58 albertel 392: if ($foundbad) {
1.125 raeburn 393: $warnings .= '<p class="LC_warning">'
394: .&mt('Invalid characters in requested name have been removed.')
395: .'</p>';
1.32 albertel 396: }
1.125 raeburn 397: return ($dest,$error,$warnings);
1.32 albertel 398: }
399:
1.36 www 400: sub relativeDest {
1.109 www 401: my ($fn,$newfilename,$uname,$udom)=@_;
1.106 raeburn 402: my $error = '';
1.36 www 403: if ($newfilename=~/^\//) {
404: # absolute, simply add path
1.113 raeburn 405: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
406: $newfilename="$londocroot/res/$udom/$uname/";
1.36 www 407: } else {
408: my $dir=$fn;
1.113 raeburn 409: $dir=~s{/[^/]+$}{};
1.36 www 410: $newfilename=$dir.'/'.$newfilename;
411: }
1.113 raeburn 412: $newfilename=~s{//+}{/}g; # remove duplicate /
413: while ($newfilename=~m{/\.\./}) {
414: $newfilename=~ s{/[^/]+/\.\./}{/}g; #remove dir/..
1.36 www 415: }
1.116 raeburn 416: my ($authorname,$authordom)=&Apache::lonnet::constructaccess($newfilename);
1.111 www 417: unless (($authorname) && ($authordom)) {
418: my $otherdir = &display($newfilename);
419: $error = &mt('Access denied to [_1]',$otherdir);
1.106 raeburn 420: }
421: return ($newfilename,$error);
1.36 www 422: }
423:
1.12 foxr 424: =pod
425:
426: =item CloseForm1($request, $user, $file)
427:
428: Close of a form on the successful completion of phase 1 processing
429:
430: Parameters:
431:
432: =over 4
433:
434: =item $request - Apache Request Object [in] - Apache server request object.
435:
1.13 foxr 436: =item $cancelurl - the url to go to on cancel.
1.12 foxr 437:
438: =back
439:
440: =cut
441:
442: sub CloseForm1 {
1.55 albertel 443: my ($request, $fn) = @_;
1.117 bisitz 444: $request->print('<input type="submit" value="'.&mt('Continue').'" /></form>');
445: $request->print(' <form action="'.&url($fn).'" method="post">'.
446: '<input type="submit" value="'.&mt('Cancel').'" /></form>');
1.12 foxr 447: }
448:
449:
450: =pod
451:
452: =item CloseForm2($request, $user, $directory)
453:
454: Successfully close off the phase 2 form.
455:
456: Parameters:
457:
458: =over 4
459:
460: =item $request - Apache Request object [in] - The request that is being
461: executed.
462:
463: =item $user - string [in] - Name of the user that is initiating the
464: request.
465:
1.127 ! raeburn 466: =item $directory - string [in] - Directory in which the operation is
1.12 foxr 467: being done relative to the top level construction space
468: directory.
469:
470: =back
471:
472: =cut
473:
474: sub CloseForm2 {
1.55 albertel 475: my ($request, $user, $fn) = @_;
1.125 raeburn 476: $request->print(&done($fn));
1.12 foxr 477: }
478:
479: =pod
480:
481: =item Rename1($request, $filename, $user, $domain, $dir)
1.127 ! raeburn 482:
1.12 foxr 483: Perform phase 1 processing of the file rename operation.
484:
485: Parameters:
486:
487: =over 4
488:
1.127 ! raeburn 489: =item $request - Apache Request Object [in] The request object for the
1.12 foxr 490: current request.
491:
492: =item $filename - The filename relative to construction space.
493:
494: =item $user - Name of the user making the request.
495:
496: =item $domain - User login domain.
497:
498: =item $dir - Directory specification of the path to the file.
499:
500: =back
501:
502: Side effects:
503:
504: =over 4
505:
506: =item A new form is displayed prompting for confirmation. The newfilename
507: hidden field of this form is loaded with
508: new filename relative to the current directory ($dir).
509:
510: =back
511:
1.127 ! raeburn 512: =cut
1.12 foxr 513:
514: sub Rename1 {
1.52 albertel 515: my ($request, $user, $domain, $fn, $newfilename, $style) = @_;
1.36 www 516:
517: if(-e $fn) {
518: if($newfilename) {
1.42 albertel 519: # is dest a dir
1.52 albertel 520: if ($style eq 'move') {
521: if (-d $newfilename) {
522: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
523: }
1.42 albertel 524: }
1.29 albertel 525: if ($newfilename =~ m|/[^\.]+$|) {
1.36 www 526: #no extension add on original extension
527: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) {
1.24 albertel 528: $newfilename.='.'.$1;
529: }
530: }
1.36 www 531: $request->print(&checksuffix($fn, $newfilename));
1.27 albertel 532: #renaming a dir, delete the trailing /
1.39 www 533: #remove second to last element for current dir
534: if (-d $fn) {
1.53 www 535: $newfilename=~/\.(\w+)$/;
536: if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
1.100 bisitz 537: $request->print('<p><span class="LC_error">'.
538: &mt('Cannot change MIME type of a directory.').
1.82 albertel 539: '</span>'.
1.100 bisitz 540: '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>');
1.53 www 541: return;
542: }
1.39 www 543: $newfilename=~s/\/[^\/]+\/([^\/]+)$/\/$1/;
1.27 albertel 544: }
1.42 albertel 545: $newfilename=~s://+:/:g; # remove duplicate /
546: while ($newfilename=~m:/\.\./:) {
547: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
548: }
1.83 albertel 549: my ($type, $return)=&exists($user, $domain, $newfilename);
1.21 albertel 550: $request->print($return);
1.83 albertel 551: if ($type eq 'error') {
1.47 sakharuk 552: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
1.21 albertel 553: return;
554: }
1.50 www 555: unless (&obsolete_unpub($user,$domain,$fn)) {
1.100 bisitz 556: $request->print('<p><span class="LC_error">'
557: .&mt('Cannot rename or move non-obsolete published file.')
558: .'</span><br />'
559: .'<a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
560: );
1.50 www 561: return;
562: }
1.52 albertel 563: my $action;
564: if ($style eq 'rename') {
1.100 bisitz 565: $action='Rename';
1.52 albertel 566: } else {
1.100 bisitz 567: $action='Move';
1.52 albertel 568: }
1.100 bisitz 569: $request->print('<input type="hidden" name="newfilename" value="'
570: .$newfilename.'" />'
571: .'<p>'
1.103 bisitz 572: .&mt($action.' [_1] to [_2]?',
573: &display($fn),
574: &display($newfilename))
1.100 bisitz 575: .'</p>'
576: );
1.36 www 577: &CloseForm1($request, $fn);
1.12 foxr 578: } else {
1.100 bisitz 579: $request->print('<p class="LC_error">'.&mt('No new filename specified.').'</p></form>');
1.12 foxr 580: return;
581: }
582: } else {
1.100 bisitz 583: $request->print('<p class="LC_error">'
1.103 bisitz 584: .&mt('No such file: [_1]',
585: &display($fn))
1.100 bisitz 586: .'</p></form>'
587: );
1.12 foxr 588: return;
589: }
1.127 ! raeburn 590:
1.12 foxr 591: }
1.55 albertel 592:
1.12 foxr 593: =pod
594:
595: =item Delete1
596:
597: Performs phase 1 processing of the delete operation. In phase one
598: we just check to be sure the file exists.
599:
600: Parameters:
601:
602: =over 4
603:
1.127 ! raeburn 604: =item $request - Apache Request Object [in] request object for the current
1.12 foxr 605: request.
606:
1.36 www 607: =item $user - string [in] Name of the user initiating the request.
1.12 foxr 608:
1.36 www 609: =item $domain - string [in] Domain the initiating user is logged in as
1.13 foxr 610:
1.36 www 611: =item $filename - string [in] Source filename.
1.12 foxr 612:
613: =back
614:
615: =cut
616:
617: sub Delete1 {
1.55 albertel 618: my ($request, $user, $domain, $fn) = @_;
1.12 foxr 619:
1.55 albertel 620: if( -e $fn) {
621: $request->print('<input type="hidden" name="newfilename" value="'.
1.95 bisitz 622: $fn.'" />');
1.70 raeburn 623: if (-d $fn) {
624: unless (&empty_directory($fn,'Delete1')) {
1.100 bisitz 625: $request->print('<p>'
626: .'<span class="LC_error">'
627: .&mt('Only empty directories may be deleted.')
628: .'</span><br />'
629: .&mt('You must delete the contents of the directory first.')
630: .'</p>'
631: .'<p><a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
632: );
1.70 raeburn 633: return;
634: }
1.127 ! raeburn 635: } else {
1.70 raeburn 636: unless (&obsolete_unpub($user,$domain,$fn)) {
1.100 bisitz 637: $request->print('<p><span class="LC_error">'
638: .&mt('Cannot delete non-obsolete published file.')
639: .'</span><br />'
640: .'<a href="'.&url($fn).'">'.&mt('Cancel').'</a></p>'
641: );
1.70 raeburn 642: return;
643: }
644: }
1.100 bisitz 645: $request->print('<p>'
1.103 bisitz 646: .&mt('Delete [_1]?',
647: &display($fn))
1.100 bisitz 648: .'</p>'
649: );
1.55 albertel 650: &CloseForm1($request, $fn);
651: } else {
1.100 bisitz 652: $request->print('<p class="LC_error">'
1.103 bisitz 653: .&mt('No such file: [_1]',
654: &display($fn))
1.100 bisitz 655: .'</p></form>'
656: );
1.50 www 657: }
1.12 foxr 658: }
659:
660: =pod
661:
662: =item Copy1($request, $user, $domain, $filename, $newfilename)
663:
664: Performs phase 1 processing of the construction space copy command.
1.17 harris41 665: Ensure that the source file exists. Ensure that a destination exists,
666: also warn if the destination already exists.
1.12 foxr 667:
668: Parameters:
669:
670: =over 4
671:
1.127 ! raeburn 672: =item $request - Apache Request Object [in] request object for the current
1.12 foxr 673: request.
674:
675: =item $user - string [in] Name of the user initiating the request.
676:
677: =item $domain - string [in] Domain the initiating user is logged in as
678:
1.36 www 679: =item $fn - string [in] Source filename.
1.12 foxr 680:
681: =item $newfilename-string [in] Destination filename.
682:
683: =back
684:
685: =cut
686:
687: sub Copy1 {
1.42 albertel 688: my ($request, $user, $domain, $fn, $newfilename) = @_;
1.12 foxr 689:
1.42 albertel 690: if(-e $fn) {
691: # is dest a dir
692: if (-d $newfilename) {
693: if ($fn =~ m|/([^/]*)$|) { $newfilename .= '/'.$1; }
694: }
695: if ($newfilename =~ m|/[^\.]+$|) {
696: #no extension add on original extension
697: if ($fn =~ m|/[^\.]*\.([^\.]+)$|) { $newfilename.='.'.$1; }
1.127 ! raeburn 698: }
1.42 albertel 699: $newfilename=~s://+:/:g; # remove duplicate /
700: while ($newfilename=~m:/\.\./:) {
701: $newfilename=~ s:/[^/]+/\.\./:/:g; #remove dir/..
702: }
703: $request->print(&checksuffix($fn,$newfilename));
1.83 albertel 704: my ($type,$return)=&exists($user, $domain, $newfilename);
1.42 albertel 705: $request->print($return);
1.83 albertel 706: if ($type eq 'error') {
1.120 raeburn 707: $request->print('<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a></form>');
1.42 albertel 708: return;
709: }
1.120 raeburn 710: # Check if there is enough space.
711: my @fileinfo = stat($fn);
712: my ($dir,$fname) = ($fn =~ m{^(.+/)([^/]+)$});
713: my $filesize = $fileinfo[7];
714: $filesize = int($filesize/1000); #expressed in kb
1.122 raeburn 715: my $output = &Apache::loncommon::excess_filesize_warning($user,$domain,'author',
1.121 bisitz 716: $fname,$filesize,'copy');
1.120 raeburn 717: if ($output) {
718: $request->print($output.'<br /><a href="'.&url($dir).'">'.&mt('Cancel').'</a></form>');
719: return;
720: }
1.103 bisitz 721: $request->print(
722: '<input type="hidden" name="newfilename"'
723: .' value="'.$newfilename.'" />'
724: .'<p>'
725: .&mt('Copy [_1] to [_2]?',
726: &display($fn),
727: &display($newfilename))
728: .'</p>'
1.100 bisitz 729: );
1.42 albertel 730: &CloseForm1($request, $fn);
731: } else {
1.100 bisitz 732: $request->print('<p class="LC_error">'
1.103 bisitz 733: .&mt('No such file: [_1]',
734: &display($fn))
1.100 bisitz 735: .'</p></form>'
736: );
1.21 albertel 737: }
1.19 albertel 738: }
739:
740: =pod
741:
1.12 foxr 742: =item NewDir1
1.127 ! raeburn 743:
1.12 foxr 744: Does all phase 1 processing of directory creation:
745: Ensures that the user provides a new directory name,
746: and that the directory does not already exist.
747:
748: Parameters:
749:
750: =over 4
751:
752: =item $request - Apache Request Object [in] - Server request object for the
1.17 harris41 753: current url.
1.12 foxr 754:
755: =item $username - Name of the user that is requesting the directory creation.
756:
1.36 www 757: =item $domain - Domain user is in
758:
759: =item $fn - source file.
1.12 foxr 760:
1.127 ! raeburn 761: =item $newdir - Name of the directory to be created; path relative to the
1.12 foxr 762: top level of construction space.
763: =back
764:
765: Side Effects:
766:
767: =over 4
768:
769: =item A new form is displayed. Clicking on the confirmation button
770: causes the newdir operation to transition into phase 2. The hidden field
771: "newfilename" is set with the construction space path to the new directory.
772:
773:
774: =back
775:
776: =cut
777:
778:
1.55 albertel 779: sub NewDir1 {
780: my ($request, $username, $domain, $fn, $newfilename, $mode) = @_;
781:
1.85 albertel 782: my ($type, $result)=&exists($username,$domain,$newfilename,'directory');
1.83 albertel 783: $request->print($result);
1.85 albertel 784: if ($type eq 'error') {
1.83 albertel 785: $request->print('</form>');
1.55 albertel 786: } else {
1.104 raeburn 787: if (($mode eq 'testbank') || ($mode eq 'imsimport')) {
788: $request->print('<input type="hidden" name="callingmode" value="'.$mode.'" />'."\n".
789: '<input type="hidden" name="inhibitmenu" value="yes" />');
1.55 albertel 790: }
1.100 bisitz 791: $request->print('<input type="hidden" name="newfilename" value="'
792: .$newfilename.'" />'
793: .'<p>'
1.103 bisitz 794: .&mt('Make new directory [_1]?',
795: &display($newfilename))
1.100 bisitz 796: .'</p>'
797: );
1.55 albertel 798: &CloseForm1($request, $fn);
799: }
1.12 foxr 800: }
801:
1.44 taceyjo1 802:
803: sub Decompress1 {
1.55 albertel 804: my ($request, $user, $domain, $fn) = @_;
805: if( -e $fn) {
1.95 bisitz 806: $request->print('<input type="hidden" name="newfilename" value="'.$fn.'" />');
1.100 bisitz 807: $request->print('<p>'
1.103 bisitz 808: .&mt('Decompress [_1]?',
809: &display($fn))
1.100 bisitz 810: .'</p>'
811: );
1.44 taceyjo1 812: &CloseForm1($request, $fn);
1.55 albertel 813: } else {
1.100 bisitz 814: $request->print('<p class="LC_error">'
1.103 bisitz 815: .&mt('No such file: [_1]',
816: &display($fn))
1.100 bisitz 817: .'</p></form>'
818: );
1.55 albertel 819: }
1.44 taceyjo1 820: }
1.55 albertel 821:
1.12 foxr 822: =pod
823:
1.22 albertel 824: =item NewFile1
1.127 ! raeburn 825:
1.22 albertel 826: Does all phase 1 processing of file creation:
827: Ensures that the user provides a new filename, adds proper extension
828: if needed and that the file does not already exist, if it is a html,
829: problem, page, or sequence, it then creates a form link to hand the
830: actual creation off to the proper handler.
831:
832: Parameters:
833:
834: =over 4
835:
836: =item $request - Apache Request Object [in] - Server request object for the
837: current url.
838:
839: =item $username - Name of the user that is requesting the directory creation.
840:
841: =item $domain - Name of the domain of the user
842:
1.118 bisitz 843: =item $fn - Source filename
1.22 albertel 844:
845: =item $newfilename
846: - Name of the file to be created; no path information
1.125 raeburn 847:
848: =item $warnings - Information about changes to filename made by cleanDest().
849:
1.22 albertel 850: =back
851:
852: Side Effects:
853:
854: =over 4
855:
856: =item 2 new forms are displayed. Clicking on the confirmation button
857: causes the browser to attempt to load the specfied URL, allowing the
1.36 www 858: proper handler to take care of file creation. There is also a Cancel
1.117 bisitz 859: button which returns you to the directory listing you came from
1.22 albertel 860:
861: =back
862:
863: =cut
864:
865: sub NewFile1 {
1.125 raeburn 866: my ($request, $user, $domain, $fn, $newfilename, $warnings) = @_;
867: return if (&filename_check($newfilename,$warnings) ne 'ok');
1.22 albertel 868:
1.67 albertel 869: if ($env{'form.action'} =~ /new(.+)file/) {
1.22 albertel 870: my $extension=$1;
871: if ($newfilename !~ /\Q.$extension\E$/) {
1.81 albertel 872: if ($newfilename =~ m|/[^/.]*\.(?:[^/.]+)$|) {
1.26 albertel 873: #already has an extension strip it and add in expected one
1.81 albertel 874: $newfilename =~ s|(/[^./])\.(?:[^.]+)$|$1|;
1.26 albertel 875: }
1.22 albertel 876: $newfilename.=".$extension";
877: }
1.31 albertel 878: }
1.83 albertel 879: my ($type, $result)=&exists($user,$domain,$newfilename);
880: if ($type eq 'error') {
1.125 raeburn 881: $request->print($warnings.$result);
1.83 albertel 882: $request->print('</form>');
1.39 www 883: } else {
1.96 raeburn 884: my $extension;
885:
886: if ($newfilename =~ m{[^/.]+\.([^/.]+)$}) {
887: $extension = $1;
888: }
889:
1.102 raeburn 890: my @okexts = qw(xml html xhtml htm xhtm problem page sequence rights sty task library js css txt);
1.96 raeburn 891: if (($extension eq '') || (!grep(/^\Q$extension\E/,@okexts))) {
892: my $validexts = '.'.join(', .',@okexts);
1.125 raeburn 893: $request->print($warnings.$result);
1.96 raeburn 894: $request->print('<p class="LC_warning">'.
895: &mt('Invalid filename: ').&display($newfilename).'</p><p>'.
896: &mt('The name of the new file needs to end with an appropriate file extension to indicate the type of file to create.').'<br />'.
897: &mt('The following are valid extensions: [_1].',$validexts).
898: '</p></form><p>'.
899: '<form name="fileaction" action="/adm/cfile" method="post">'.
900: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
901: '<input type="hidden" name="action" value="newfile" />'.
1.118 bisitz 902: '<span class ="LC_nobreak">'.&mt('Enter a filename: ').'<input type="text" name="newfilename" value="Type Name Here" onfocus="if (this.value == '."'Type Name Here') this.value=''".'" /> <input type="submit" value="Go" />'.
1.96 raeburn 903: '</span></form></p>'.
904: '<p><form action="'.&url($fn).
1.97 bisitz 905: '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></form></p>');
1.125 raeburn 906: } elsif (($type ne 'warning') && ($warnings eq '') && ($result eq '')) {
1.123 golterma 907: my $query = "";
908: $query .= "?mode=" . $env{'form.mode'} unless (!exists($env{'form.mode'}) || !length($env{'form.mode'}));
909: $request->print('
910: <script type="text/javascript">
1.125 raeburn 911: window.location = "'.&url($newfilename,'js'). $query .'";
1.123 golterma 912: </script>');
913: } else {
1.125 raeburn 914: $request->print($warnings.$result);
1.123 golterma 915: $request->print('<p>'.&mt('Make new file').' '.&display($newfilename).'?</p>');
916: $request->print('</form>');
917: $request->print('<form action="'.&url($newfilename).
918: '" method="post"><p><input type="submit" value="'.&mt('Continue').'" /></p></form>');
919: $request->print('<form action="'.&url($fn).
920: '" method="post"><p><input type="submit" value="'.&mt('Cancel').'" /></p></form>');
1.96 raeburn 921: }
1.22 albertel 922: }
1.96 raeburn 923: return;
924: }
925:
926: sub filename_check {
927: my ($newfilename) = @_;
928: ##Informs User (name).(number).(extension) not allowed
929: if($newfilename =~ /\.(\d+)\.(\w+)$/){
930: $r->print('<span class="LC_error">'.$newfilename.
931: ' - '.&mt('Bad Filename').'<br />('.&mt('name').').('.&mt('number').').('.&mt('extension').') '.
932: ' '.&mt('Not Allowed').'</span>');
933: return;
934: }
935: if($newfilename =~ /(\:\:\:|\&\&\&|\_\_\_)/){
936: $r->print('<span class="LC_error">'.$newfilename.
937: ' - '.&mt('Bad Filename').'<br />('.&mt('Must not include').' '.$1.') '.
938: ' '.&mt('Not Allowed').'</span>');
939: return;
940: }
1.127 ! raeburn 941: return 'ok';
1.22 albertel 942: }
943:
944: =pod
945:
1.12 foxr 946: =item phaseone($r, $fn, $uname, $udom)
947:
948: Peforms phase one processing of the request. In phase one, error messages
949: are returned if the request cannot be performed (e.g. attempts to manipulate
950: files that are nonexistent). If the operation can be performed, what is
951: about to be done will be presented to the user for confirmation. If the
1.127 ! raeburn 952: user confirms the request, then phase two is executed, the action
1.12 foxr 953: performed and reported to the user.
954:
955: Parameters:
956:
957: =over 4
958:
959: =item $r - request object [in] - The Apache request being executed.
960:
1.127 ! raeburn 961: =item $fn = string [in] - The filename being manipulated by the
1.12 foxr 962: request.
963:
964: =item $uname - string [in] Name of user logged in and doing this action.
965:
1.127 ! raeburn 966: =item $udom - string [in] Domain name under which the user logged in.
1.12 foxr 967:
968: =back
969:
970: =cut
1.8 albertel 971:
1.1 www 972: sub phaseone {
1.55 albertel 973: my ($r,$fn,$uname,$udom)=@_;
1.127 ! raeburn 974:
1.58 albertel 975: my $doingdir=0;
1.67 albertel 976: if ($env{'form.action'} eq 'newdir') { $doingdir=1; }
1.127 ! raeburn 977: my ($newfilename,$error,$warnings) =
1.125 raeburn 978: &cleanDest($env{'form.newfilename'},$doingdir,$fn,$uname,$udom);
1.106 raeburn 979: unless ($error) {
1.109 www 980: ($newfilename,$error)=&relativeDest($fn,$newfilename,$uname,$udom);
1.106 raeburn 981: }
982: if ($error) {
983: my $dirlist;
984: if ($fn=~m{^(.*/)[^/]+$}) {
985: $dirlist=$1;
986: } else {
1.127 ! raeburn 987: $dirlist=$fn;
1.106 raeburn 988: }
1.125 raeburn 989: if ($warnings) {
990: $r->print($warnings);
991: }
1.106 raeburn 992: $r->print('<div class="LC_error">'.$error.'</div>'.
1.117 bisitz 993: '<p><a href="'.&url($dirlist).'">'.&mt('Return to Directory').
994: '</a></p>');
1.106 raeburn 995: return;
996: }
1.55 albertel 997: $r->print('<form action="/adm/cfile" method="post">'.
998: '<input type="hidden" name="qualifiedfilename" value="'.$fn.'" />'.
999: '<input type="hidden" name="phase" value="two" />'.
1.67 albertel 1000: '<input type="hidden" name="action" value="'.$env{'form.action'}.'" />');
1.125 raeburn 1001:
1002: if ($env{'form.action'} eq 'newfile' ||
1003: $env{'form.action'} eq 'newhtmlfile' ||
1004: $env{'form.action'} eq 'newproblemfile' ||
1005: $env{'form.action'} eq 'newpagefile' ||
1006: $env{'form.action'} eq 'newsequencefile' ||
1007: $env{'form.action'} eq 'newrightsfile' ||
1008: $env{'form.action'} eq 'newstyfile' ||
1009: $env{'form.action'} eq 'newtaskfile' ||
1010: $env{'form.action'} eq 'newlibraryfile' ||
1011: $env{'form.action'} eq 'Select Action') {
1.65 www 1012: my $empty=&mt('Type Name Here');
1.125 raeburn 1013: if (($newfilename!~/\/$/) && ($newfilename!~/$empty$/)) {
1014: &NewFile1($r, $uname, $udom, $fn, $newfilename, $warnings);
1015: } else {
1016: if ($warnings) {
1017: $r->print($warnings);
1018: }
1.100 bisitz 1019: $r->print('<p class="LC_error">'
1020: .&mt('No new filename specified.')
1021: .'</p></form>'
1022: );
1.125 raeburn 1023: }
1024: } else {
1025: if ($warnings) {
1026: $r->print($warnings);
1027: }
1028: if ($env{'form.action'} eq 'rename') {
1029: &Rename1($r, $uname, $udom, $fn, $newfilename, 'rename');
1030: } elsif ($env{'form.action'} eq 'move') {
1031: &Rename1($r, $uname, $udom, $fn, $newfilename, 'move');
1.127 ! raeburn 1032: } elsif ($env{'form.action'} eq 'delete') {
1.125 raeburn 1033: &Delete1($r, $uname, $udom, $fn);
1034: } elsif ($env{'form.action'} eq 'decompress') {
1035: &Decompress1($r, $uname, $udom, $fn);
1.127 ! raeburn 1036: } elsif ($env{'form.action'} eq 'copy') {
1.125 raeburn 1037: if ($newfilename) {
1038: &Copy1($r, $uname, $udom, $fn, $newfilename);
1039: } else {
1040: $r->print('<p class="LC_error">'
1041: .&mt('No new filename specified.')
1042: .'</p></form>'
1043: );
1044: }
1045: } elsif ($env{'form.action'} eq 'newdir') {
1046: my $mode = '';
1047: if (exists($env{'form.callingmode'}) ) {
1048: $mode = $env{'form.callingmode'};
1049: }
1050: &NewDir1($r, $uname, $udom, $fn, $newfilename, $mode);
1051: }
1.55 albertel 1052: }
1.12 foxr 1053: }
1054:
1055: =pod
1056:
1057: =item Rename2($request, $user, $directory, $oldfile, $newfile)
1058:
1.17 harris41 1059: Performs phase 2 processing of a rename reequest. This is where the
1.12 foxr 1060: actual rename is performed.
1061:
1062: Parameters
1063:
1064: =over 4
1065:
1066: =item $request - Apache request object [in] The request being processed.
1067:
1068: =item $user - string [in] The name of the user initiating the request.
1069:
1070: =item $directory - string [in] The name of the directory relative to the
1071: construction space top level of the renamed file.
1072:
1073: =item $oldfile - Name of the file.
1074:
1075: =item $newfile - Name of the new file.
1076:
1077: =back
1078:
1079: Returns:
1080:
1081: =over 4
1082:
1083: =item 1 Success.
1084:
1085: =item 0 Failure.
1086:
1087: =cut
1088:
1089: sub Rename2 {
1090:
1.55 albertel 1091: my ($request, $user, $directory, $oldfile, $newfile) = @_;
1.12 foxr 1092:
1.55 albertel 1093: &Debug($request, "Rename2 directory: ".$directory." old file: ".$oldfile.
1094: " new file ".$newfile."\n");
1095: &Debug($request, "Target is: ".$directory.'/'.
1096: $newfile);
1097: if (-e $oldfile) {
1098:
1099: my $oRN=$oldfile;
1100: my $nRN=$newfile;
1101: unless (rename($oldfile,$newfile)) {
1.83 albertel 1102: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55 albertel 1103: return 0;
1104: }
1105: ## If old name.(extension) exits, move under new name.
1.127 ! raeburn 1106: ## If it doesn't exist and a new.(extension) exists
1.55 albertel 1107: ## delete it (only concern when renaming over files)
1108: my $tmp1=$oRN.'.meta';
1109: my $tmp2=$nRN.'.meta';
1110: if(-e $tmp1){
1111: unless(rename($tmp1,$tmp2)){ }
1112: } elsif(-e $tmp2){
1113: unlink $tmp2;
1114: }
1115: $tmp1=$oRN.'.save';
1116: $tmp2=$nRN.'.save';
1117: if(-e $tmp1){
1118: unless(rename($tmp1,$tmp2)){ }
1119: } elsif(-e $tmp2){
1120: unlink $tmp2;
1121: }
1122: $tmp1=$oRN.'.log';
1123: $tmp2=$nRN.'.log';
1124: if(-e $tmp1){
1125: unless(rename($tmp1,$tmp2)){ }
1126: } elsif(-e $tmp2){
1127: unlink $tmp2;
1128: }
1129: $tmp1=$oRN.'.bak';
1130: $tmp2=$nRN.'.bak';
1131: if(-e $tmp1){
1132: unless(rename($tmp1,$tmp2)){ }
1133: } elsif(-e $tmp2){
1134: unlink $tmp2;
1135: }
1136: } else {
1.103 bisitz 1137: $request->print(
1.117 bisitz 1138: '<p class="LC_error">'
1.103 bisitz 1139: .&mt('No such file: [_1]',
1140: &display($oldfile))
1141: .'</p></form>'
1.100 bisitz 1142: );
1.55 albertel 1143: return 0;
1144: }
1145: return 1;
1.12 foxr 1146: }
1.55 albertel 1147:
1.12 foxr 1148: =pod
1149:
1150: =item Delete2($request, $user, $filename)
1151:
1.127 ! raeburn 1152: Performs phase two of a delete. The user has confirmed that they want
1.12 foxr 1153: to delete the selected file. The file is deleted and the results of the
1154: delete attempt are indicated.
1155:
1156: Parameters:
1157:
1158: =over 4
1159:
1160: =item $request - Apache Request object [in] the request object for the current
1161: delete operation.
1162:
1163: =item $user - string [in] The name of the user initiating the delete
1164: request.
1165:
1.17 harris41 1166: =item $filename - string [in] The name of the file, relative to construction
1167: space, to delete.
1.12 foxr 1168:
1169: =back
1170:
1171: Returns:
1172: 1 - success.
1173: 0 - Failure.
1174:
1175: =cut
1176:
1177: sub Delete2 {
1.55 albertel 1178: my ($request, $user, $filename) = @_;
1.127 ! raeburn 1179: if (-d $filename) {
! 1180: unless (&empty_directory($filename,'Delete2')) {
! 1181: $request->print('<span class="LC_error">'.&mt('Error: Directory Non Empty').'</span>');
1.55 albertel 1182: return 0;
1.127 ! raeburn 1183: } else {
1.55 albertel 1184: if(-e $filename) {
1185: unless(rmdir($filename)) {
1.83 albertel 1186: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55 albertel 1187: return 0;
1188: }
1189: } else {
1.100 bisitz 1190: $request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1.55 albertel 1191: return 0;
1192: }
1193: }
1.48 taceyjo1 1194: } else {
1.55 albertel 1195: if(-e $filename) {
1196: unless(unlink($filename)) {
1.83 albertel 1197: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55 albertel 1198: return 0;
1199: }
1200: } else {
1.100 bisitz 1201: $request->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
1.55 albertel 1202: return 0;
1.46 taceyjo1 1203: }
1.55 albertel 1204: }
1205: return 1;
1.12 foxr 1206: }
1207:
1208: =pod
1209:
1210: =item Copy2($request, $username, $dir, $oldfile, $newfile)
1211:
1.127 ! raeburn 1212: Performs phase 2 of a copy. The file is copied and the status
1.12 foxr 1213: of that copy is reported back to the user.
1214:
1215: =over 4
1216:
1217: =item $request - Apache request object [in]; the apache request currently
1218: being executed.
1219:
1220: =item $username - string [in] Name of the user who is requesting the copy.
1221:
1222: =item $dir - string [in] Directory path relative to the construction space
1223: of the destination file.
1224:
1225: =item $oldfile - string [in] Name of the source file.
1226:
1227: =item $newfile - string [in] Name of the destination file.
1228:
1229:
1230: =back
1231:
1.71 raeburn 1232: Returns 0 failure, and 1 successs.
1.12 foxr 1233:
1234: =cut
1.1 www 1235:
1.12 foxr 1236: sub Copy2 {
1237: my ($request, $username, $dir, $oldfile, $newfile) = @_;
1238: &Debug($request ,"Will try to copy $oldfile to $newfile");
1239: if(-e $oldfile) {
1.71 raeburn 1240: if ($oldfile eq $newfile) {
1.83 albertel 1241: $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>');
1.71 raeburn 1242: return 1;
1243: }
1.12 foxr 1244: unless (copy($oldfile, $newfile)) {
1.83 albertel 1245: $request->print('<span class="LC_error">'.&mt('copy Error').': '.$!.'</span>');
1.12 foxr 1246: return 0;
1.61 albertel 1247: } elsif (!chmod(0660, $newfile)) {
1.83 albertel 1248: $request->print('<span class="LC_error">'.&mt('chmod error').': '.$!.'</span>');
1.61 albertel 1249: return 0;
1.127 ! raeburn 1250: } elsif (-e $oldfile.'.meta' &&
1.61 albertel 1251: !copy($oldfile.'.meta', $newfile.'.meta') &&
1252: !chmod(0660, $newfile.'.meta')) {
1.83 albertel 1253: $request->print('<span class="LC_error">'.&mt('copy metadata error').
1254: ': '.$!.'</span>');
1.61 albertel 1255: return 0;
1.12 foxr 1256: } else {
1257: return 1;
1258: }
1.11 albertel 1259: } else {
1.100 bisitz 1260: $request->print('<p class="LC_error">'.&mt('No such file').'</p>');
1.12 foxr 1261: return 0;
1.11 albertel 1262: }
1.12 foxr 1263: return 1;
1264: }
1.55 albertel 1265:
1.12 foxr 1266: =pod
1267:
1268: =item NewDir2($request, $user, $newdirectory)
1.1 www 1269:
1.12 foxr 1270: Performs phase 2 processing of directory creation. This involves creating the directory and
1271: reporting the results of that creation to the user.
1272:
1273: Parameters:
1274: =over 4
1275:
1276: =item $request - Apache request object [in]. Object representing the current HTTP request.
1277:
1278: =item $user - string [in] The name of the user that is initiating the request.
1279:
1280: =item $newdirectory - string [in] The full path of the directory being created.
1281:
1282: =back
1283:
1284: Returns 0 - failure 1 - success.
1285:
1286: =cut
1.8 albertel 1287:
1.12 foxr 1288: sub NewDir2 {
1.55 albertel 1289: my ($request, $user, $newdirectory) = @_;
1.127 ! raeburn 1290:
1.55 albertel 1291: unless(mkdir($newdirectory, 02770)) {
1.83 albertel 1292: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55 albertel 1293: return 0;
1294: }
1295: unless(chmod(02770, ($newdirectory))) {
1.83 albertel 1296: $request->print('<span class="LC_error">'.&mt('Error').': '.$!.'</span>');
1.55 albertel 1297: return 0;
1298: }
1299: return 1;
1.1 www 1300: }
1.55 albertel 1301:
1.44 taceyjo1 1302: sub decompress2 {
1.55 albertel 1303: my ($r, $user, $dir, $file) = @_;
1.88 raeburn 1304: &Apache::lonnet::appenv({'cgi.file' => $file});
1305: &Apache::lonnet::appenv({'cgi.dir' => $dir});
1.55 albertel 1306: my $result=&Apache::lonnet::ssi_body('/cgi-bin/decompress.pl');
1307: $r->print($result);
1308: &Apache::lonnet::delenv('cgi.file');
1309: &Apache::lonnet::delenv('cgi.dir');
1310: return 1;
1.44 taceyjo1 1311: }
1.55 albertel 1312:
1.12 foxr 1313: =pod
1314:
1315: =item phasetwo($r, $fn, $uname, $udom)
1316:
1317: Controls the phase 2 processing of file management
1318: requests for construction space. In phase one, the user
1319: was asked to confirm the operation. In phase 2, the operation
1320: is performed and the result is shown.
1321:
1322: The strategy is to break out the processing into specific action processors
1.127 ! raeburn 1323: named action2 where action is the requested action and the 2 denotes
1.12 foxr 1324: phase 2 processing.
1325:
1326: Parameters:
1327:
1328: =over 4
1329:
1330: =item $r - Apache Request object [in] The request object for this httpd
1331: transaction.
1332:
1333: =item $fn - string [in] A filename indicating the object that is being
1334: manipulated.
1335:
1336: =item $uname - string [in] The name of the user initiating the file management
1337: request.
1338:
1339: =item $udom - string [in] The login domain of the user initiating the
1340: file management request.
1341: =back
1342:
1343: =cut
1344:
1.1 www 1345: sub phasetwo {
1346: my ($r,$fn,$uname,$udom)=@_;
1.127 ! raeburn 1347:
1.9 foxr 1348: &Debug($r, "loncfile - Entering phase 2 for $fn");
1.127 ! raeburn 1349:
1.78 banghart 1350: # Break down the file into its component pieces.
1.127 ! raeburn 1351:
1.27 albertel 1352: my $dir; # Directory path
1353: my $main; # Filename.
1354: my $suffix; # Extension.
1.46 taceyjo1 1355: if ($fn=~m:(.*)/([^/]+):) {
1.27 albertel 1356: $dir=$1; # Directory path
1357: $main=$2; # Filename.
1.54 albertel 1358: }
1359: if($main=~m:\.(\w+)$:){ # Fixes problems with filenames with no extensions
1.68 albertel 1360: $suffix=$1; #This is the actually filename extension if it exists
1.66 albertel 1361: $main=~s/\.\w+$//; #strip the extension
1.54 albertel 1362: }
1.79 banghart 1363: my $dest; #
1364: my $dest_dir; # On success this is where we'll go.
1365: my $disp_newname; #
1366: my $dest_newname; #
1.55 albertel 1367: &Debug($r,"loncfile::phase2 dir = $dir main = $main suffix = $suffix");
1.67 albertel 1368: &Debug($r," newfilename = ".$env{'form.newfilename'});
1.9 foxr 1369:
1370: my $conspace=$fn;
1.127 ! raeburn 1371:
1.55 albertel 1372: &Debug($r,"loncfile::phase2 Full construction space name: $conspace");
1.127 ! raeburn 1373:
1.67 albertel 1374: &Debug($r,"loncfie::phase2 action is $env{'form.action'}");
1.127 ! raeburn 1375:
1.12 foxr 1376: # Select the appropriate processing sub.
1.127 ! raeburn 1377: if ($env{'form.action'} eq 'decompress') {
1.66 albertel 1378: $main .= '.'.$suffix;
1.55 albertel 1379: if(!&decompress2($r, $uname, $dir, $main)) {
1380: return ;
1381: }
1382: $dest = $dir."/.";
1.67 albertel 1383: } elsif ($env{'form.action'} eq 'rename' ||
1384: $env{'form.action'} eq 'move') {
1385: if($env{'form.newfilename'}) {
1.27 albertel 1386: if (!defined($dir)) {
1387: $fn=~m:^(.*)/:;
1.127 ! raeburn 1388: $dir=$1;
1.27 albertel 1389: }
1.67 albertel 1390: if(!&Rename2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.12 foxr 1391: return;
1392: }
1.78 banghart 1393: $dest = $dir."/";
1.79 banghart 1394: $dest_newname = $env{'form.newfilename'};
1395: $env{'form.newfilename'} =~ /.+(\/.+$)/;
1396: $disp_newname = $1;
1397: $disp_newname =~ s/\///;
1.12 foxr 1398: }
1.127 ! raeburn 1399: } elsif ($env{'form.action'} eq 'delete') {
1.67 albertel 1400: if(!&Delete2($r, $uname, $env{'form.newfilename'})) {
1.12 foxr 1401: return ;
1402: }
1403: # Once a resource is deleted, we just list the directory that
1404: # previously held it.
1405: #
1.20 albertel 1406: $dest = $dir."/."; # Parent dir.
1.127 ! raeburn 1407: } elsif ($env{'form.action'} eq 'copy') {
1.67 albertel 1408: if($env{'form.newfilename'}) {
1409: if(!&Copy2($r, $uname, $dir, $fn, $env{'form.newfilename'})) {
1.44 taceyjo1 1410: return ;
1.55 albertel 1411: }
1.67 albertel 1412: $dest = $env{'form.newfilename'};
1.55 albertel 1413: } else {
1.100 bisitz 1414: $r->print('<p class="LC_error">'.&mt('No New filename specified').'</p></form>');
1.12 foxr 1415: return;
1416: }
1417:
1.67 albertel 1418: } elsif ($env{'form.action'} eq 'newdir') {
1419: my $newdir= $env{'form.newfilename'};
1.12 foxr 1420: if(!&NewDir2($r, $uname, $newdir)) {
1421: return;
1422: }
1.55 albertel 1423: $dest = $newdir."/";
1.12 foxr 1424: }
1.67 albertel 1425: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1.117 bisitz 1426: $r->print(
1427: '<p>'
1428: .&Apache::lonhtmlcommon::confirm_success(&mt('Done'))
1429: .'<br /><a href="javascript:self.close()">'.&mt('Continue').'</a>'
1430: .'</p>'
1431: );
1.51 raeburn 1432: } else {
1.79 banghart 1433: if ($env{'form.action'} eq 'rename') {
1.117 bisitz 1434: $r->print(
1435: '<p>'.&Apache::lonhtmlcommon::confirm_success(&mt('Done')).'</p>'
1436: .&Apache::lonhtmlcommon::actionbox(
1437: ['<a href="'.&url($dest).'">'.&mt('Return to Directory').'</a>',
1438: '<a href="'.&url($dest_newname).'">'.$disp_newname.'</a>']));
1.79 banghart 1439: } else {
1.125 raeburn 1440: $r->print(&done($dest));
1.79 banghart 1441: }
1.51 raeburn 1442: }
1.1 www 1443: }
1444:
1445: sub handler {
1446:
1.55 albertel 1447: $r=shift;
1.1 www 1448:
1.123 golterma 1449: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['decompress','action','filename','newfilename','mode']);
1.9 foxr 1450:
1.55 albertel 1451: &Debug($r, "loncfile.pm - handler entered");
1.67 albertel 1452: &Debug($r, " filename: ".$env{'form.filename'});
1453: &Debug($r, " newfilename: ".$env{'form.newfilename'});
1.36 www 1454: #
1455: # Determine the root filename
1456: # This could come in as "filename", which actually is a URL, or
1457: # as "qualifiedfilename", which is indeed a real filename in filesystem
1458: #
1.55 albertel 1459: my $fn;
1.1 www 1460:
1.67 albertel 1461: if ($env{'form.filename'}) {
1462: &Debug($r, "test: $env{'form.filename'}");
1.77 www 1463: $fn=&unescape($env{'form.filename'});
1.55 albertel 1464: $fn=&URLToPath($fn);
1.127 ! raeburn 1465: } elsif($ENV{'QUERY_STRING'} && $env{'form.phase'} ne 'two') {
1.55 albertel 1466: #Just hijack the script only the first time around to inject the
1467: #correct information for further processing
1.77 www 1468: $fn=&unescape($env{'form.decompress'});
1.44 taceyjo1 1469: $fn=&URLToPath($fn);
1.67 albertel 1470: $env{'form.action'}="decompress";
1471: } elsif ($env{'form.qualifiedfilename'}) {
1472: $fn=$env{'form.qualifiedfilename'};
1.55 albertel 1473: } else {
1474: &Debug($r, "loncfile::handler - no form.filename");
1.67 albertel 1475: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.127 ! raeburn 1476: ' unspecified filename for cfile', $r->filename);
1.55 albertel 1477: return HTTP_NOT_FOUND;
1478: }
1.44 taceyjo1 1479:
1.126 raeburn 1480: unless ($fn) {
1.55 albertel 1481: &Debug($r, "loncfile::handler - doctored url is empty");
1.67 albertel 1482: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
1.127 ! raeburn 1483: ' trying to cfile non-existing file', $r->filename);
1.55 albertel 1484: return HTTP_NOT_FOUND;
1.127 ! raeburn 1485: }
1.1 www 1486:
1487: # ----------------------------------------------------------- Start page output
1.55 albertel 1488:
1.116 raeburn 1489: my ($uname,$udom) = &Apache::lonnet::constructaccess($fn);
1.127 ! raeburn 1490: &Debug($r,
1.55 albertel 1491: "loncfile::handler constructaccess uname = $uname domain = $udom");
1.113 raeburn 1492: if (($uname eq '') || ($udom eq '')) {
1.55 albertel 1493: $r->log_reason($uname.' at '.$udom.
1.67 albertel 1494: ' trying to manipulate file '.$env{'form.filename'}.
1.127 ! raeburn 1495: ' ('.$fn.') - not authorized',
! 1496: $r->filename);
1.55 albertel 1497: return HTTP_NOT_ACCEPTABLE;
1498: }
1499:
1.1 www 1500:
1.55 albertel 1501: &Apache::loncommon::content_type($r,'text/html');
1502: $r->send_http_header;
1503:
1.75 albertel 1504: my (%loaditem,$js);
1505:
1.67 albertel 1506: if ( ($env{'form.action'} eq 'newdir') && ($env{'form.phase'} eq 'two') && ( ($env{'form.callingmode'} eq 'testbank') || ($env{'form.callingmode'} eq 'imsimport') ) ) {
1507: my $newdirname = $env{'form.newfilename'};
1.75 albertel 1508: $js = qq|
1509: <script type="text/javascript">
1.51 raeburn 1510: function writeDone() {
1511: window.focus();
1.90 raeburn 1512: opener.document.info.newdir.value = "$newdirname";
1513: setTimeout("self.close()",10000);
1.51 raeburn 1514: }
1515: </script>
1.75 albertel 1516: |;
1.76 albertel 1517: $loaditem{'onload'} = "writeDone()";
1.55 albertel 1518: }
1.113 raeburn 1519:
1520: my $londocroot = $r->dir_config('lonDocRoot');
1521: my $trailfile = $fn;
1522: $trailfile =~ s{^/(priv/)}{$londocroot/$1};
1.126 raeburn 1523:
1.99 bisitz 1524: # Breadcrumbs
1.126 raeburn 1525: my $crsauthor;
1526: my $text = 'Authoring Space';
1527: my $title = 'Authoring Space File Operation',
1528: my $href = &Apache::loncommon::authorspace(&url($fn));
1529: if ($env{'request.course.id'}) {
1530: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
1531: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1532: if ($href eq "/priv/$cdom/$cnum/") {
1533: $text = 'Course Authoring Space';
1534: $title = 'Course Authoring Space File Operation',
1535: $crsauthor = 1;
1536: }
1537: }
1.99 bisitz 1538: &Apache::lonhtmlcommon::clear_breadcrumbs();
1539: &Apache::lonhtmlcommon::add_breadcrumb({
1.126 raeburn 1540: 'text' => $text,
1541: 'href' => $href,
1.99 bisitz 1542: });
1543: &Apache::lonhtmlcommon::add_breadcrumb({
1544: 'text' => 'File Operation',
1.126 raeburn 1545: 'title' => $title,
1.99 bisitz 1546: 'href' => '',
1547: });
1548:
1.126 raeburn 1549: $r->print(&Apache::loncommon::start_page($title,
1.75 albertel 1550: $js,
1.99 bisitz 1551: {'add_entries' => \%loaditem,})
1552: .&Apache::lonhtmlcommon::breadcrumbs()
1553: .&Apache::loncommon::head_subbox(
1.113 raeburn 1554: &Apache::loncommon::CSTR_pageheader($trailfile))
1.99 bisitz 1555: );
1.127 ! raeburn 1556:
1.117 bisitz 1557: $r->print('<p>'.&mt('Location').': '.&display($fn).'</p>');
1.127 ! raeburn 1558:
1.67 albertel 1559: if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.127 ! raeburn 1560: unless ($crsauthor) {
1.126 raeburn 1561: $r->print('<p class="LC_info">'
1562: .&mt('Co-Author [_1]',$uname.':'.$udom)
1563: .'</p>'
1564: );
1565: }
1.55 albertel 1566: }
1567:
1568:
1.67 albertel 1569: &Debug($r, "loncfile::handler Form action is $env{'form.action'} ");
1.117 bisitz 1570: my %action = &Apache::lonlocal::texthash(
1571: 'delete' => 'Delete',
1572: 'rename' => 'Rename',
1573: 'move' => 'Move',
1574: 'newdir' => 'New Directory',
1575: 'decompress' => 'Decompress',
1576: 'copy' => 'Copy',
1577: 'newfile' => 'New Resource',
1578: 'newhtmlfile' => 'New Resource',
1579: 'newproblemfile' => 'New Resource',
1580: 'newpagefile' => 'New Resource',
1581: 'newsequencefile' => 'New Resource',
1582: 'newrightsfile' => 'New Resource',
1583: 'newstyfile' => 'New Resource',
1584: 'newtaskfile' => 'New Resource',
1585: 'newlibraryfile' => 'New Resource',
1586: 'Select Action' => 'New Resource',
1587: );
1588: if ($action{$env{'form.action'}}) {
1.126 raeburn 1589: if ($crsauthor) {
1590: my @disallowed = qw(page sequence rights library);
1591: my $newtype;
1592: if ($env{'form.action'} =~ /^new(\w+)file$/) {
1593: $newtype = $1;
1594: } elsif ($env{'form.action'} eq 'newfile') {
1595: ($newtype) = ($env{'form.newfilename'} =~ m{\.([^/.]+)$});
1596: $newtype = lc($newtype);
1597: }
1598: if (($newtype ne '') &&
1599: (grep(/^\Q$newtype\E$/,@disallowed))) {
1600: $r->print('<p class="LC_error">'
1601: .&mt('Creation of a new file of type: [_1] is not permitted in Course Authoring Space',$newtype)
1602: .'</p>'
1603: .&Apache::loncommon::end_page()
1604: );
1605: return OK;
1606: }
1607: }
1.117 bisitz 1608: $r->print('<h2>'.$action{$env{'form.action'}}.'</h2>');
1.55 albertel 1609: } else {
1.100 bisitz 1610: $r->print('<p class="LC_error">'
1.117 bisitz 1611: .&mt('Unknown Action: [_1]',$env{'form.action'})
1.100 bisitz 1612: .'</p>'
1613: .&Apache::loncommon::end_page()
1614: );
1.117 bisitz 1615: return OK;
1.55 albertel 1616: }
1.117 bisitz 1617:
1.67 albertel 1618: if ($env{'form.phase'} eq 'two') {
1.55 albertel 1619: &Debug($r, "loncfile::handler entering phase2");
1620: &phasetwo($r,$fn,$uname,$udom);
1621: } else {
1622: &Debug($r, "loncfile::handler entering phase1");
1623: &phaseone($r,$fn,$uname,$udom);
1624: }
1.1 www 1625:
1.75 albertel 1626: $r->print(&Apache::loncommon::end_page());
1.127 ! raeburn 1627: return OK;
1.1 www 1628: }
1629:
1630: 1;
1631: __END__
1.9 foxr 1632:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>