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