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

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.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.11    ! albertel  142:     my $conspace;
        !           143:     if ($fn =~ m-^/home/-) {
        !           144:         $conspace=$fn;
        !           145:     } else {
        !           146:         $conspace='/home/'.$uname.'/public_html'.$fn;
        !           147:     }
1.1       www       148: 
1.3       www       149:     $r->print('<form action=/adm/cfile method=post>'.
1.1       www       150: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
                    151:               '<input type=hidden name=phase value=two>'.
1.3       www       152:               '<input type=hidden name=action value='.$ENV{'form.action'}.'>');
1.8       albertel  153: 
1.3       www       154:     if ($ENV{'form.action'} eq 'rename') {
                    155: 	if (-e $conspace) {
                    156: 	    if ($ENV{'form.newfilename'}) {
1.8       albertel  157: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
                    158: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
1.4       www       159: 	       $r->print('<input type=hidden name=newfilename value="'.
                    160:                          $ENV{'form.newfilename'}.
                    161:                          '"><p>Rename <tt>'.$fn.'</tt> to <tt>'.
1.8       albertel  162:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       163: 	    } else {
1.8       albertel  164: 	       $r->print('<p>No new filename specified.</p></form>');
1.3       www       165:                return;
                    166: 	    }
                    167:         } else {
1.8       albertel  168: 	    $r->print('<p>No such file.</p></form>');
1.3       www       169:             return;
                    170:         }
                    171:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    172: 	if (-e $conspace) {
1.8       albertel  173:             $r->print('<p>Delete <tt>'.$fn.'</tt>?</p>');
1.3       www       174:         } else {
1.8       albertel  175: 	    $r->print('<p>No such file.</p></form>');
1.3       www       176:             return;
1.1       www       177:         }
1.3       www       178:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    179: 	if (-e $conspace) {
                    180: 	    if ($ENV{'form.newfilename'}) {
1.8       albertel  181: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
                    182: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
1.4       www       183: 	       $r->print('<input type=hidden name=newfilename value="'.
                    184:                          $ENV{'form.newfilename'}.
                    185:                          '"><p>Copy <tt>'.$fn.'</tt> to <tt>'.
1.8       albertel  186:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       187: 	    } else {
1.8       albertel  188: 	       $r->print('<p>No new filename specified.</p></form>');
1.3       www       189:                return;
                    190: 	    }
                    191:         } else {
1.8       albertel  192: 	    $r->print('<p>No such file.</p></form>');
1.3       www       193:             return;
                    194:         }
                    195:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.4       www       196:         my $newdir='/home/'.$uname.'/public_html/'.
                    197:                    $fn.$ENV{'form.newfilename'};
                    198: 	if (-e $newdir) {
1.8       albertel  199:             $r->print('<p>Directory exists.</p></form>');
1.3       www       200:             return;
                    201:         }
1.4       www       202: 	$r->print('<input type=hidden name=newfilename value="'.
                    203:                   $ENV{'form.newfilename'}.
                    204:                   '"><p>Make new directory <tt>'.
1.8       albertel  205:                   $fn.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       206:        
1.1       www       207:     }
1.8       albertel  208:     $r->print('<p><input type=submit value=Continue></p></form>');
                    209:     $r->print('<form action="/priv/'.$uname.$fn.
                    210: 	      '" method="GET"><p><input type=submit value=Cancel></p></form>');
                    211: 
1.1       www       212: }
                    213: 
                    214: sub phasetwo {
                    215:     my ($r,$fn,$uname,$udom)=@_;
1.5       www       216: 
1.9       foxr      217:     &Debug($r, "loncfile - Entering phase 2 for $fn");
                    218: 
1.5       www       219:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
                    220:     my $dir=$1;
                    221:     my $main=$2;
                    222:     my $suffix=$3;
1.11    ! albertel  223:     $dir =~ s-^/[^/]*/[^/]*/[^/]*--;
1.9       foxr      224:     
                    225:     
                    226:     &Debug($r, "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
                    227: 
                    228:     my $conspace=$fn;
                    229: 
                    230:     &Debug($r, "loncfile::phase2 Full construction space name: $conspace");
1.5       www       231: 
1.9       foxr      232:     &Debug($r, "loncfie::phase2 action is $ENV{'form.action'}");
1.5       www       233: 
                    234:     if ($ENV{'form.action'} eq 'rename') {
                    235: 	if (-e $conspace) {
                    236: 	    if ($ENV{'form.newfilename'}) {
1.11    ! albertel  237:                unless (rename($fn,
1.5       www       238:           '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    239: 	    $r->print('<font color=red>Error: '.$!.'</font>');
                    240:                }
                    241:             }
                    242:         } else {
                    243: 	    $r->print('<p>No such file.</form>');
                    244:             return;
                    245:         }
                    246:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    247: 	if (-e $conspace) {
1.11    ! albertel  248:             unless (unlink($fn)) {
1.5       www       249: 	       $r->print('<font color=red>Error: '.$!.'</font>');
                    250:             }
                    251:         } else {
                    252: 	    $r->print('<p>No such file.</form>');
                    253:             return;
                    254:         }
                    255:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    256: 	if (-e $conspace) {
                    257: 	    if ($ENV{'form.newfilename'}) {
1.11    ! albertel  258:                unless (copy($fn,
1.5       www       259:            '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    260: 	          $r->print('<font color=red>Error: '.$!.'</font>');
                    261:                }
                    262: 	    } else {
                    263: 	       $r->print('<p>No new filename specified.</form>');
                    264:                return;
                    265: 	    }
                    266:         } else {
                    267: 	    $r->print('<p>No such file.</form>');
                    268:             return;
                    269:         }
                    270:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.9       foxr      271:         my $newdir= $fn.$ENV{'form.newfilename'};
                    272: 
                    273: 	&Debug($r, "loncfile::phasetwo - new directory name: $newdir");
                    274: 
1.5       www       275:         unless (mkdir($newdir,0770)) {
                    276: 	    $r->print('<font color=red>Error: '.$!.'</font>');
1.9       foxr      277: 	    &Debug($r, "loncfile::phasetwo - mkdir failed $!");
1.5       www       278:         }
1.9       foxr      279: 	&Debug($r, "Done button: uname = $uname, dir = $dir, fn = $fn");
                    280: 	my $url = '/priv/'.$uname.$newdir.'/';
                    281: 	&Debug($r, "URL[1] = ".$url);
                    282: 	$url =~ s/\/home\/$uname\/public_html//o;
                    283:         &Debug($r, "URL = ".$url);
                    284: 
                    285:         $r->print('<h3><a href="'.$url.'">Done</a></h3>');
1.6       www       286:         return;
1.5       www       287:     }
                    288:     $r->print('<h3><a href="/priv/'.$uname.$dir.'/">Done</a></h3>');
1.1       www       289: }
                    290: 
                    291: sub handler {
                    292: 
1.10      foxr      293:   $r=shift;
1.1       www       294: 
1.9       foxr      295: 
                    296:   &Debug($r, "loncfile.pm - handler entered");
                    297: 
1.1       www       298:   my $fn;
                    299: 
                    300:   if ($ENV{'form.filename'}) {
                    301:       $fn=$ENV{'form.filename'};
1.9       foxr      302:       &Debug($r, "loncfile::handler - raw url: $fn");
1.10      foxr      303: #      $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
                    304: #      $fn=~s/^http\:\/\/[^\/]+//;
                    305:       $fn=URLToPath($fn);
1.9       foxr      306:       &Debug($r, "loncfile::handler - doctored url: $fn");
                    307: 
1.1       www       308:   } else {
1.9       foxr      309:       &Debug($r, "loncfile::handler - no form.filename");
1.1       www       310:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    311:          ' unspecified filename for cfile', $r->filename); 
                    312:      return HTTP_NOT_FOUND;
                    313:   }
                    314: 
                    315:   unless ($fn) { 
1.9       foxr      316:       &Debug($r, "loncfile::handler - doctored url is empty");
1.1       www       317:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    318:          ' trying to cfile non-existing file', $r->filename); 
                    319:      return HTTP_NOT_FOUND;
                    320:   } 
                    321: 
                    322: # ----------------------------------------------------------- Start page output
                    323:   my $uname;
                    324:   my $udom;
                    325: 
                    326:   ($uname,$udom)=
                    327:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1.9       foxr      328:   &Debug($r, 
                    329: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
1.1       www       330:   unless (($uname) && ($udom)) {
                    331:      $r->log_reason($uname.' at '.$udom.
1.4       www       332:          ' trying to manipulate file '.$ENV{'form.filename'}.
1.1       www       333:          ' ('.$fn.') - not authorized', 
                    334:          $r->filename); 
                    335:      return HTTP_NOT_ACCEPTABLE;
                    336:   }
                    337: 
                    338:   $fn=~s/\/\~(\w+)//;
1.9       foxr      339:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
1.1       www       340: 
                    341:   $r->content_type('text/html');
                    342:   $r->send_http_header;
                    343: 
                    344:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    345: 
                    346:   $r->print(
                    347:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
                    348: 
                    349:   
                    350:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
                    351:   
                    352:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    353:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
                    354:                '</font></h3>');
                    355:   }
                    356: 
1.9       foxr      357: 
                    358:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
1.2       www       359:   if ($ENV{'form.action'} eq 'delete') {
1.9       foxr      360:       
1.2       www       361:       $r->print('<h3>Delete</h3>');
                    362:   } elsif ($ENV{'form.action'} eq 'rename') {
                    363:       $r->print('<h3>Rename</h3>');
                    364:   } elsif ($ENV{'form.action'} eq 'newdir') {
                    365:       $r->print('<h3>New Directory</h3>');
                    366:   } elsif ($ENV{'form.action'} eq 'copy') {
                    367:       $r->print('<h3>Copy</h3>');
                    368:   } else {
                    369:      $r->print('<p>Unknown Action</body></html>');
                    370:      return OK;  
                    371:   }
1.1       www       372:   if ($ENV{'form.phase'} eq 'two') {
1.9       foxr      373:       &Debug($r, "loncfile::handler  entering phase2");
1.4       www       374:       &phasetwo($r,$fn,$uname,$udom);
1.1       www       375:   } else {
1.9       foxr      376:       &Debug($r, "loncfile::handler  entering phase1");
1.3       www       377:       &phaseone($r,$fn,$uname,$udom);
1.1       www       378:   }
                    379: 
                    380:   $r->print('</body></html>');
                    381:   return OK;  
                    382: }
                    383: 
                    384: 1;
                    385: __END__
1.9       foxr      386: 

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