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