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

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: #
        !            13: # $Id: loncfile.pm,v 1.8 2002/01/21 17:13:49 albertel 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: 
        !            66: my $DEBUG=1;
        !            67: 
        !            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.1       www        86: 
1.8       albertel   87: sub exists {
                     88:     my ($uname,$udom,$dir,$newfile)=@_;
                     89:     my $published='/home/httpd/html/res/'.$udom.'/'.$uname.'/'.$dir.'/'.
                     90: 	$ENV{'form.newfilename'};
                     91:     my $construct='/home/'.$uname.'/public_html/'.$dir.'/'.
                     92: 	$ENV{'form.newfilename'};
                     93:     my $result;
                     94:     if (-e $published) {
                     95: 	$result.='<p><font color=red>Warning: target file exists, and has been published!</font></p>';
                     96:     } elsif ( -e $construct ) {
                     97: 	$result.='<p><font color=red>Warning: target file exists!</font></p>';
                     98:     }
                     99:     return $result;
                    100: }
                    101: 
                    102: sub checksuffix {
                    103:     my ($old,$new) = @_;
                    104:     my $result;
                    105:     my $oldsuffix;
                    106:     my $newsuffix;
                    107:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
                    108:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
                    109:     if ($oldsuffix ne $newsuffix) {
                    110: 	$result.='<p><font color=red>Warning: change of MIME type!</font></p>';
                    111:     }
                    112:     return $result;
                    113: }
                    114: 
1.1       www       115: sub phaseone {
                    116:     my ($r,$fn,$uname,$udom)=@_;
                    117: 
1.8       albertel  118:     $fn=~m:(.*)/([^/]+)\.(\w+)$:;
1.3       www       119:     my $dir=$1;
                    120:     my $main=$2;
                    121:     my $suffix=$3;
1.1       www       122: 
1.3       www       123:     my $conspace='/home/'.$uname.'/public_html'.$fn;
1.1       www       124: 
1.3       www       125:     $r->print('<form action=/adm/cfile method=post>'.
1.1       www       126: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
                    127:               '<input type=hidden name=phase value=two>'.
1.3       www       128:               '<input type=hidden name=action value='.$ENV{'form.action'}.'>');
1.8       albertel  129: 
1.3       www       130:     if ($ENV{'form.action'} eq 'rename') {
                    131: 	if (-e $conspace) {
                    132: 	    if ($ENV{'form.newfilename'}) {
1.8       albertel  133: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
                    134: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
1.4       www       135: 	       $r->print('<input type=hidden name=newfilename value="'.
                    136:                          $ENV{'form.newfilename'}.
                    137:                          '"><p>Rename <tt>'.$fn.'</tt> to <tt>'.
1.8       albertel  138:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       139: 	    } else {
1.8       albertel  140: 	       $r->print('<p>No new filename specified.</p></form>');
1.3       www       141:                return;
                    142: 	    }
                    143:         } else {
1.8       albertel  144: 	    $r->print('<p>No such file.</p></form>');
1.3       www       145:             return;
                    146:         }
                    147:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    148: 	if (-e $conspace) {
1.8       albertel  149:             $r->print('<p>Delete <tt>'.$fn.'</tt>?</p>');
1.3       www       150:         } else {
1.8       albertel  151: 	    $r->print('<p>No such file.</p></form>');
1.3       www       152:             return;
1.1       www       153:         }
1.3       www       154:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    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>Copy <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 'newdir') {
1.4       www       172:         my $newdir='/home/'.$uname.'/public_html/'.
                    173:                    $fn.$ENV{'form.newfilename'};
                    174: 	if (-e $newdir) {
1.8       albertel  175:             $r->print('<p>Directory exists.</p></form>');
1.3       www       176:             return;
                    177:         }
1.4       www       178: 	$r->print('<input type=hidden name=newfilename value="'.
                    179:                   $ENV{'form.newfilename'}.
                    180:                   '"><p>Make new directory <tt>'.
1.8       albertel  181:                   $fn.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       182:        
1.1       www       183:     }
1.8       albertel  184:     $r->print('<p><input type=submit value=Continue></p></form>');
                    185:     $r->print('<form action="/priv/'.$uname.$fn.
                    186: 	      '" method="GET"><p><input type=submit value=Cancel></p></form>');
                    187: 
1.1       www       188: }
                    189: 
                    190: sub phasetwo {
                    191:     my ($r,$fn,$uname,$udom)=@_;
1.5       www       192: 
1.9     ! foxr      193:     &Debug($r, "loncfile - Entering phase 2 for $fn");
        !           194: 
1.5       www       195:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
                    196:     my $dir=$1;
                    197:     my $main=$2;
                    198:     my $suffix=$3;
1.9     ! foxr      199:     
        !           200:     
        !           201:     &Debug($r, "loncfile::phase2 dir = $dir main = $main suffix = $suffix");
        !           202: 
        !           203:     my $conspace=$fn;
        !           204: 
        !           205:     &Debug($r, "loncfile::phase2 Full construction space name: $conspace");
1.5       www       206: 
1.9     ! foxr      207:     &Debug($r, "loncfie::phase2 action is $ENV{'form.action'}");
1.5       www       208: 
                    209:     if ($ENV{'form.action'} eq 'rename') {
                    210: 	if (-e $conspace) {
                    211: 	    if ($ENV{'form.newfilename'}) {
                    212:                unless (rename('/home/'.$uname.'/public_html'.$fn,
                    213:           '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    214: 	    $r->print('<font color=red>Error: '.$!.'</font>');
                    215:                }
                    216:             }
                    217:         } else {
                    218: 	    $r->print('<p>No such file.</form>');
                    219:             return;
                    220:         }
                    221:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    222: 	if (-e $conspace) {
                    223:             unless (unlink('/home/'.$uname.'/public_html'.$fn)) {
                    224: 	       $r->print('<font color=red>Error: '.$!.'</font>');
                    225:             }
                    226:         } else {
                    227: 	    $r->print('<p>No such file.</form>');
                    228:             return;
                    229:         }
                    230:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    231: 	if (-e $conspace) {
                    232: 	    if ($ENV{'form.newfilename'}) {
                    233:                unless (copy('/home/'.$uname.'/public_html'.$fn,
                    234:            '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    235: 	          $r->print('<font color=red>Error: '.$!.'</font>');
                    236:                }
                    237: 	    } else {
                    238: 	       $r->print('<p>No new filename specified.</form>');
                    239:                return;
                    240: 	    }
                    241:         } else {
                    242: 	    $r->print('<p>No such file.</form>');
                    243:             return;
                    244:         }
                    245:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.9     ! foxr      246:         my $newdir= $fn.$ENV{'form.newfilename'};
        !           247: 
        !           248: 	&Debug($r, "loncfile::phasetwo - new directory name: $newdir");
        !           249: 
1.5       www       250:         unless (mkdir($newdir,0770)) {
                    251: 	    $r->print('<font color=red>Error: '.$!.'</font>');
1.9     ! foxr      252: 	    &Debug($r, "loncfile::phasetwo - mkdir failed $!");
1.5       www       253:         }
1.9     ! foxr      254: 	&Debug($r, "Done button: uname = $uname, dir = $dir, fn = $fn");
        !           255: 	my $url = '/priv/'.$uname.$newdir.'/';
        !           256: 	&Debug($r, "URL[1] = ".$url);
        !           257: 	$url =~ s/\/home\/$uname\/public_html//o;
        !           258:         &Debug($r, "URL = ".$url);
        !           259: 
        !           260:         $r->print('<h3><a href="'.$url.'">Done</a></h3>');
1.6       www       261:         return;
1.5       www       262:     }
                    263:     $r->print('<h3><a href="/priv/'.$uname.$dir.'/">Done</a></h3>');
1.1       www       264: }
                    265: 
                    266: sub handler {
                    267: 
                    268:   my $r=shift;
                    269: 
1.9     ! foxr      270: 
        !           271:   &Debug($r, "loncfile.pm - handler entered");
        !           272: 
1.1       www       273:   my $fn;
                    274: 
                    275:   if ($ENV{'form.filename'}) {
                    276:       $fn=$ENV{'form.filename'};
1.9     ! foxr      277:       &Debug($r, "loncfile::handler - raw url: $fn");
        !           278:       $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
1.1       www       279:       $fn=~s/^http\:\/\/[^\/]+//;
1.9     ! foxr      280:       &Debug($r, "loncfile::handler - doctored url: $fn");
        !           281: 
1.1       www       282:   } else {
1.9     ! foxr      283:       &Debug($r, "loncfile::handler - no form.filename");
1.1       www       284:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    285:          ' unspecified filename for cfile', $r->filename); 
                    286:      return HTTP_NOT_FOUND;
                    287:   }
                    288: 
                    289:   unless ($fn) { 
1.9     ! foxr      290:       &Debug($r, "loncfile::handler - doctored url is empty");
1.1       www       291:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    292:          ' trying to cfile non-existing file', $r->filename); 
                    293:      return HTTP_NOT_FOUND;
                    294:   } 
                    295: 
                    296: # ----------------------------------------------------------- Start page output
                    297:   my $uname;
                    298:   my $udom;
                    299: 
                    300:   ($uname,$udom)=
                    301:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
1.9     ! foxr      302:   &Debug($r, 
        !           303: 	 "loncfile::handler constructaccess uname = $uname domain = $udom");
1.1       www       304:   unless (($uname) && ($udom)) {
                    305:      $r->log_reason($uname.' at '.$udom.
1.4       www       306:          ' trying to manipulate file '.$ENV{'form.filename'}.
1.1       www       307:          ' ('.$fn.') - not authorized', 
                    308:          $r->filename); 
                    309:      return HTTP_NOT_ACCEPTABLE;
                    310:   }
                    311: 
                    312:   $fn=~s/\/\~(\w+)//;
1.9     ! foxr      313:   &Debug($r, "loncfile::handler ~ removed filename: $fn");
1.1       www       314: 
                    315:   $r->content_type('text/html');
                    316:   $r->send_http_header;
                    317: 
                    318:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    319: 
                    320:   $r->print(
                    321:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
                    322: 
                    323:   
                    324:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
                    325:   
                    326:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    327:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
                    328:                '</font></h3>');
                    329:   }
                    330: 
1.9     ! foxr      331: 
        !           332:   &Debug($r, "loncfile::handler Form action is $ENV{'form.action'} ");
1.2       www       333:   if ($ENV{'form.action'} eq 'delete') {
1.9     ! foxr      334:       
1.2       www       335:       $r->print('<h3>Delete</h3>');
                    336:   } elsif ($ENV{'form.action'} eq 'rename') {
                    337:       $r->print('<h3>Rename</h3>');
                    338:   } elsif ($ENV{'form.action'} eq 'newdir') {
                    339:       $r->print('<h3>New Directory</h3>');
                    340:   } elsif ($ENV{'form.action'} eq 'copy') {
                    341:       $r->print('<h3>Copy</h3>');
                    342:   } else {
                    343:      $r->print('<p>Unknown Action</body></html>');
                    344:      return OK;  
                    345:   }
1.1       www       346:   if ($ENV{'form.phase'} eq 'two') {
1.9     ! foxr      347:       &Debug($r, "loncfile::handler  entering phase2");
1.4       www       348:       &phasetwo($r,$fn,$uname,$udom);
1.1       www       349:   } else {
1.9     ! foxr      350:       &Debug($r, "loncfile::handler  entering phase1");
1.3       www       351:       &phaseone($r,$fn,$uname,$udom);
1.1       www       352:   }
                    353: 
                    354:   $r->print('</body></html>');
                    355:   return OK;  
                    356: }
                    357: 
                    358: 1;
                    359: __END__
1.9     ! foxr      360: 

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