Annotation of loncom/publisher/loncfile.pm, revision 1.10

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.10    ! foxr       13: # $Id: loncfile.pm,v 1.9 2002/05/24 05:11:40 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.1       www        56: 
                     57: package Apache::loncfile;
                     58: 
                     59: use strict;
                     60: use Apache::File;
                     61: use File::Copy;
                     62: use Apache::Constants qw(:common :http :methods);
                     63: use Apache::loncacc;
1.9       foxr       64: use Apache::Log ();
                     65: 
1.10    ! foxr       66: my $DEBUG=0;
        !            67: my $r;				# Needs to be global for some stuff RF.
1.9       foxr       68: #
                     69: #  Debug
                     70: #    If debugging is enabled puts out a debuggin message determined by the
                     71: #    caller.  The debug message goes to the Apache error log file.
                     72: #
                     73: #  Parameters:
                     74: #     r       - Apache request [in]
                     75: #     message - String [in] 
                     76: # Returns:
                     77: #    nothing.
                     78: sub Debug {
                     79:     my $r       = shift;
                     80:     my $log     = $r->log;
                     81:     my $message = shift;
                     82:     if ($DEBUG) {
                     83: 	$log->debug($message);    
                     84:     }
                     85: }
1.10    ! foxr       86: #
        !            87: #   URLToPath
        !            88: #     Convert a URL to a file system path.
        !            89: #
        !            90: #   In order to manipulate the construction space objects, it's necessary
        !            91: #   to access url identified objects a filespace objects.  This function
        !            92: #   translates a construction space URL to a file system path.
        !            93: # Parameters:
        !            94: #    Url    - string [in] The url to convert.
        !            95: # Returns:
        !            96: #    The corresponing file system path.
        !            97: sub URLToPath
        !            98: {
        !            99:     my $Url = shift;
        !           100:     &Debug($r, "UrlToPath got: $Url");
        !           101:     $Url=~ s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
        !           102:     $Url=~ s/^http\:\/\/[^\/]+//;
        !           103:     &Debug($r, "Returning $Url \n");
        !           104:     return $Url;
        !           105: }
1.8       albertel  106: sub exists {
                    107:     my ($uname,$udom,$dir,$newfile)=@_;
                    108:     my $published='/home/httpd/html/res/'.$udom.'/'.$uname.'/'.$dir.'/'.
                    109: 	$ENV{'form.newfilename'};
                    110:     my $construct='/home/'.$uname.'/public_html/'.$dir.'/'.
                    111: 	$ENV{'form.newfilename'};
                    112:     my $result;
                    113:     if (-e $published) {
                    114: 	$result.='<p><font color=red>Warning: target file exists, and has been published!</font></p>';
                    115:     } elsif ( -e $construct ) {
                    116: 	$result.='<p><font color=red>Warning: target file exists!</font></p>';
                    117:     }
                    118:     return $result;
                    119: }
                    120: 
                    121: sub checksuffix {
                    122:     my ($old,$new) = @_;
                    123:     my $result;
                    124:     my $oldsuffix;
                    125:     my $newsuffix;
                    126:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
                    127:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
                    128:     if ($oldsuffix ne $newsuffix) {
                    129: 	$result.='<p><font color=red>Warning: change of MIME type!</font></p>';
                    130:     }
                    131:     return $result;
                    132: }
                    133: 
1.1       www       134: sub phaseone {
                    135:     my ($r,$fn,$uname,$udom)=@_;
                    136: 
1.8       albertel  137:     $fn=~m:(.*)/([^/]+)\.(\w+)$:;
1.3       www       138:     my $dir=$1;
                    139:     my $main=$2;
                    140:     my $suffix=$3;
1.1       www       141: 
1.3       www       142:     my $conspace='/home/'.$uname.'/public_html'.$fn;
1.1       www       143: 
1.3       www       144:     $r->print('<form action=/adm/cfile method=post>'.
1.1       www       145: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
                    146:               '<input type=hidden name=phase value=two>'.
1.3       www       147:               '<input type=hidden name=action value='.$ENV{'form.action'}.'>');
1.8       albertel  148: 
1.3       www       149:     if ($ENV{'form.action'} eq 'rename') {
                    150: 	if (-e $conspace) {
                    151: 	    if ($ENV{'form.newfilename'}) {
1.8       albertel  152: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
                    153: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
1.4       www       154: 	       $r->print('<input type=hidden name=newfilename value="'.
                    155:                          $ENV{'form.newfilename'}.
                    156:                          '"><p>Rename <tt>'.$fn.'</tt> to <tt>'.
1.8       albertel  157:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       158: 	    } else {
1.8       albertel  159: 	       $r->print('<p>No new filename specified.</p></form>');
1.3       www       160:                return;
                    161: 	    }
                    162:         } else {
1.8       albertel  163: 	    $r->print('<p>No such file.</p></form>');
1.3       www       164:             return;
                    165:         }
                    166:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    167: 	if (-e $conspace) {
1.8       albertel  168:             $r->print('<p>Delete <tt>'.$fn.'</tt>?</p>');
1.3       www       169:         } else {
1.8       albertel  170: 	    $r->print('<p>No such file.</p></form>');
1.3       www       171:             return;
1.1       www       172:         }
1.3       www       173:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    174: 	if (-e $conspace) {
                    175: 	    if ($ENV{'form.newfilename'}) {
1.8       albertel  176: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
                    177: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
1.4       www       178: 	       $r->print('<input type=hidden name=newfilename value="'.
                    179:                          $ENV{'form.newfilename'}.
                    180:                          '"><p>Copy <tt>'.$fn.'</tt> to <tt>'.
1.8       albertel  181:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       182: 	    } else {
1.8       albertel  183: 	       $r->print('<p>No new filename specified.</p></form>');
1.3       www       184:                return;
                    185: 	    }
                    186:         } else {
1.8       albertel  187: 	    $r->print('<p>No such file.</p></form>');
1.3       www       188:             return;
                    189:         }
                    190:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.4       www       191:         my $newdir='/home/'.$uname.'/public_html/'.
                    192:                    $fn.$ENV{'form.newfilename'};
                    193: 	if (-e $newdir) {
1.8       albertel  194:             $r->print('<p>Directory exists.</p></form>');
1.3       www       195:             return;
                    196:         }
1.4       www       197: 	$r->print('<input type=hidden name=newfilename value="'.
                    198:                   $ENV{'form.newfilename'}.
                    199:                   '"><p>Make new directory <tt>'.
1.8       albertel  200:                   $fn.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       201:        
1.1       www       202:     }
1.8       albertel  203:     $r->print('<p><input type=submit value=Continue></p></form>');
                    204:     $r->print('<form action="/priv/'.$uname.$fn.
                    205: 	      '" method="GET"><p><input type=submit value=Cancel></p></form>');
                    206: 
1.1       www       207: }
                    208: 
                    209: sub phasetwo {
                    210:     my ($r,$fn,$uname,$udom)=@_;
1.5       www       211: 
1.9       foxr      212:     &Debug($r, "loncfile - Entering phase 2 for $fn");
                    213: 
1.5       www       214:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
                    215:     my $dir=$1;
                    216:     my $main=$2;
                    217:     my $suffix=$3;
1.9       foxr      218:     
                    219:     
                    220:     &Debug($r, "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
                    221: 
                    222:     my $conspace=$fn;
                    223: 
                    224:     &Debug($r, "loncfile::phase2 Full construction space name: $conspace");
1.5       www       225: 
1.9       foxr      226:     &Debug($r, "loncfie::phase2 action is $ENV{'form.action'}");
1.5       www       227: 
                    228:     if ($ENV{'form.action'} eq 'rename') {
                    229: 	if (-e $conspace) {
                    230: 	    if ($ENV{'form.newfilename'}) {
                    231:                unless (rename('/home/'.$uname.'/public_html'.$fn,
                    232:           '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    233: 	    $r->print('<font color=red>Error: '.$!.'</font>');
                    234:                }
                    235:             }
                    236:         } else {
                    237: 	    $r->print('<p>No such file.</form>');
                    238:             return;
                    239:         }
                    240:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    241: 	if (-e $conspace) {
                    242:             unless (unlink('/home/'.$uname.'/public_html'.$fn)) {
                    243: 	       $r->print('<font color=red>Error: '.$!.'</font>');
                    244:             }
                    245:         } else {
                    246: 	    $r->print('<p>No such file.</form>');
                    247:             return;
                    248:         }
                    249:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    250: 	if (-e $conspace) {
                    251: 	    if ($ENV{'form.newfilename'}) {
                    252:                unless (copy('/home/'.$uname.'/public_html'.$fn,
                    253:            '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    254: 	          $r->print('<font color=red>Error: '.$!.'</font>');
                    255:                }
                    256: 	    } else {
                    257: 	       $r->print('<p>No new filename specified.</form>');
                    258:                return;
                    259: 	    }
                    260:         } else {
                    261: 	    $r->print('<p>No such file.</form>');
                    262:             return;
                    263:         }
                    264:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.9       foxr      265:         my $newdir= $fn.$ENV{'form.newfilename'};
                    266: 
                    267: 	&Debug($r, "loncfile::phasetwo - new directory name: $newdir");
                    268: 
1.5       www       269:         unless (mkdir($newdir,0770)) {
                    270: 	    $r->print('<font color=red>Error: '.$!.'</font>');
1.9       foxr      271: 	    &Debug($r, "loncfile::phasetwo - mkdir failed $!");
1.5       www       272:         }
1.9       foxr      273: 	&Debug($r, "Done button: uname = $uname, dir = $dir, fn = $fn");
                    274: 	my $url = '/priv/'.$uname.$newdir.'/';
                    275: 	&Debug($r, "URL[1] = ".$url);
                    276: 	$url =~ s/\/home\/$uname\/public_html//o;
                    277:         &Debug($r, "URL = ".$url);
                    278: 
                    279:         $r->print('<h3><a href="'.$url.'">Done</a></h3>');
1.6       www       280:         return;
1.5       www       281:     }
                    282:     $r->print('<h3><a href="/priv/'.$uname.$dir.'/">Done</a></h3>');
1.1       www       283: }
                    284: 
                    285: sub handler {
                    286: 
1.10    ! foxr      287:   $r=shift;
1.1       www       288: 
1.9       foxr      289: 
                    290:   &Debug($r, "loncfile.pm - handler entered");
                    291: 
1.1       www       292:   my $fn;
                    293: 
                    294:   if ($ENV{'form.filename'}) {
                    295:       $fn=$ENV{'form.filename'};
1.9       foxr      296:       &Debug($r, "loncfile::handler - raw url: $fn");
1.10    ! foxr      297: #      $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
        !           298: #      $fn=~s/^http\:\/\/[^\/]+//;
        !           299:       $fn=URLToPath($fn);
1.9       foxr      300:       &Debug($r, "loncfile::handler - doctored url: $fn");
                    301: 
1.1       www       302:   } else {
1.9       foxr      303:       &Debug($r, "loncfile::handler - no form.filename");
1.1       www       304:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    305:          ' unspecified filename for cfile', $r->filename); 
                    306:      return HTTP_NOT_FOUND;
                    307:   }
                    308: 
                    309:   unless ($fn) { 
1.9       foxr      310:       &Debug($r, "loncfile::handler - doctored url is empty");
1.1       www       311:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    312:          ' trying to cfile non-existing file', $r->filename); 
                    313:      return HTTP_NOT_FOUND;
                    314:   } 
                    315: 
                    316: # ----------------------------------------------------------- Start page output
                    317:   my $uname;
                    318:   my $udom;
                    319: 
                    320:   ($uname,$udom)=
                    321:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1.9       foxr      322:   &Debug($r, 
                    323: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
1.1       www       324:   unless (($uname) && ($udom)) {
                    325:      $r->log_reason($uname.' at '.$udom.
1.4       www       326:          ' trying to manipulate file '.$ENV{'form.filename'}.
1.1       www       327:          ' ('.$fn.') - not authorized', 
                    328:          $r->filename); 
                    329:      return HTTP_NOT_ACCEPTABLE;
                    330:   }
                    331: 
                    332:   $fn=~s/\/\~(\w+)//;
1.9       foxr      333:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
1.1       www       334: 
                    335:   $r->content_type('text/html');
                    336:   $r->send_http_header;
                    337: 
                    338:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    339: 
                    340:   $r->print(
                    341:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
                    342: 
                    343:   
                    344:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
                    345:   
                    346:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    347:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
                    348:                '</font></h3>');
                    349:   }
                    350: 
1.9       foxr      351: 
                    352:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
1.2       www       353:   if ($ENV{'form.action'} eq 'delete') {
1.9       foxr      354:       
1.2       www       355:       $r->print('<h3>Delete</h3>');
                    356:   } elsif ($ENV{'form.action'} eq 'rename') {
                    357:       $r->print('<h3>Rename</h3>');
                    358:   } elsif ($ENV{'form.action'} eq 'newdir') {
                    359:       $r->print('<h3>New Directory</h3>');
                    360:   } elsif ($ENV{'form.action'} eq 'copy') {
                    361:       $r->print('<h3>Copy</h3>');
                    362:   } else {
                    363:      $r->print('<p>Unknown Action</body></html>');
                    364:      return OK;  
                    365:   }
1.1       www       366:   if ($ENV{'form.phase'} eq 'two') {
1.9       foxr      367:       &Debug($r, "loncfile::handler  entering phase2");
1.4       www       368:       &phasetwo($r,$fn,$uname,$udom);
1.1       www       369:   } else {
1.9       foxr      370:       &Debug($r, "loncfile::handler  entering phase1");
1.3       www       371:       &phaseone($r,$fn,$uname,$udom);
1.1       www       372:   }
                    373: 
                    374:   $r->print('</body></html>');
                    375:   return OK;  
                    376: }
                    377: 
                    378: 1;
                    379: __END__
1.9       foxr      380: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>