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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to rename files, etc, in construction space
                      3: #
1.8     ! albertel    4: # $Id: loncfile.pm,v 1.7 2001/12/04 15:34:57 albertel Exp $
1.7       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: #
1.1       www        29: # (Handler to retrieve an old version of a file
                     30: #
                     31: # (Publication Handler
                     32: # 
                     33: # (TeX Content Handler
                     34: #
                     35: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
                     36: #
                     37: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
                     38: # 03/23 Guy Albertelli
                     39: # 03/24,03/29 Gerd Kortemeyer)
                     40: #
1.5       www        41: # 03/31,04/03,05/02,05/09,06/23,06/24 Gerd Kortemeyer)
1.1       www        42: #
                     43: # 06/23 Gerd Kortemeyer
                     44: 
                     45: package Apache::loncfile;
                     46: 
                     47: use strict;
                     48: use Apache::File;
                     49: use File::Copy;
                     50: use Apache::Constants qw(:common :http :methods);
                     51: use Apache::loncacc;
                     52: 
1.8     ! albertel   53: sub exists {
        !            54:     my ($uname,$udom,$dir,$newfile)=@_;
        !            55:     my $published='/home/httpd/html/res/'.$udom.'/'.$uname.'/'.$dir.'/'.
        !            56: 	$ENV{'form.newfilename'};
        !            57:     my $construct='/home/'.$uname.'/public_html/'.$dir.'/'.
        !            58: 	$ENV{'form.newfilename'};
        !            59:     my $result;
        !            60:     if (-e $published) {
        !            61: 	$result.='<p><font color=red>Warning: target file exists, and has been published!</font></p>';
        !            62:     } elsif ( -e $construct ) {
        !            63: 	$result.='<p><font color=red>Warning: target file exists!</font></p>';
        !            64:     }
        !            65:     return $result;
        !            66: }
        !            67: 
        !            68: sub checksuffix {
        !            69:     my ($old,$new) = @_;
        !            70:     my $result;
        !            71:     my $oldsuffix;
        !            72:     my $newsuffix;
        !            73:     if ($new=~m:(.*/*)([^/]+)\.(\w+)$:) { $newsuffix=$3; }
        !            74:     if ($old=~m:(.*)/+([^/]+)\.(\w+)$:) { $oldsuffix=$3; }
        !            75:     if ($oldsuffix ne $newsuffix) {
        !            76: 	$result.='<p><font color=red>Warning: change of MIME type!</font></p>';
        !            77:     }
        !            78:     return $result;
        !            79: }
        !            80: 
1.1       www        81: sub phaseone {
                     82:     my ($r,$fn,$uname,$udom)=@_;
                     83: 
1.8     ! albertel   84:     $fn=~m:(.*)/([^/]+)\.(\w+)$:;
1.3       www        85:     my $dir=$1;
                     86:     my $main=$2;
                     87:     my $suffix=$3;
1.1       www        88: 
1.3       www        89:     my $conspace='/home/'.$uname.'/public_html'.$fn;
1.1       www        90: 
1.3       www        91:     $r->print('<form action=/adm/cfile method=post>'.
1.1       www        92: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
                     93:               '<input type=hidden name=phase value=two>'.
1.3       www        94:               '<input type=hidden name=action value='.$ENV{'form.action'}.'>');
1.8     ! albertel   95: 
1.3       www        96:     if ($ENV{'form.action'} eq 'rename') {
                     97: 	if (-e $conspace) {
                     98: 	    if ($ENV{'form.newfilename'}) {
1.8     ! albertel   99: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
        !           100: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
1.4       www       101: 	       $r->print('<input type=hidden name=newfilename value="'.
                    102:                          $ENV{'form.newfilename'}.
                    103:                          '"><p>Rename <tt>'.$fn.'</tt> to <tt>'.
1.8     ! albertel  104:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       105: 	    } else {
1.8     ! albertel  106: 	       $r->print('<p>No new filename specified.</p></form>');
1.3       www       107:                return;
                    108: 	    }
                    109:         } else {
1.8     ! albertel  110: 	    $r->print('<p>No such file.</p></form>');
1.3       www       111:             return;
                    112:         }
                    113:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    114: 	if (-e $conspace) {
1.8     ! albertel  115:             $r->print('<p>Delete <tt>'.$fn.'</tt>?</p>');
1.3       www       116:         } else {
1.8     ! albertel  117: 	    $r->print('<p>No such file.</p></form>');
1.3       www       118:             return;
1.1       www       119:         }
1.3       www       120:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    121: 	if (-e $conspace) {
                    122: 	    if ($ENV{'form.newfilename'}) {
1.8     ! albertel  123: 		$r->print(&checksuffix($fn,$ENV{'form.newfilename'}));
        !           124: 		$r->print(&exists($uname,$udom,$dir,$ENV{'form.newfilename'}));
1.4       www       125: 	       $r->print('<input type=hidden name=newfilename value="'.
                    126:                          $ENV{'form.newfilename'}.
                    127:                          '"><p>Copy <tt>'.$fn.'</tt> to <tt>'.
1.8     ! albertel  128:                          $dir.'/'.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       129: 	    } else {
1.8     ! albertel  130: 	       $r->print('<p>No new filename specified.</p></form>');
1.3       www       131:                return;
                    132: 	    }
                    133:         } else {
1.8     ! albertel  134: 	    $r->print('<p>No such file.</p></form>');
1.3       www       135:             return;
                    136:         }
                    137:     } elsif ($ENV{'form.action'} eq 'newdir') {
1.4       www       138:         my $newdir='/home/'.$uname.'/public_html/'.
                    139:                    $fn.$ENV{'form.newfilename'};
                    140: 	if (-e $newdir) {
1.8     ! albertel  141:             $r->print('<p>Directory exists.</p></form>');
1.3       www       142:             return;
                    143:         }
1.4       www       144: 	$r->print('<input type=hidden name=newfilename value="'.
                    145:                   $ENV{'form.newfilename'}.
                    146:                   '"><p>Make new directory <tt>'.
1.8     ! albertel  147:                   $fn.$ENV{'form.newfilename'}.'</tt>?</p>');
1.3       www       148:        
1.1       www       149:     }
1.8     ! albertel  150:     $r->print('<p><input type=submit value=Continue></p></form>');
        !           151:     $r->print('<form action="/priv/'.$uname.$fn.
        !           152: 	      '" method="GET"><p><input type=submit value=Cancel></p></form>');
        !           153: 
1.1       www       154: }
                    155: 
                    156: sub phasetwo {
                    157:     my ($r,$fn,$uname,$udom)=@_;
1.5       www       158: 
                    159:     $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
                    160:     my $dir=$1;
                    161:     my $main=$2;
                    162:     my $suffix=$3;
                    163: 
                    164:     my $conspace='/home/'.$uname.'/public_html'.$fn;
                    165: 
                    166:     if ($ENV{'form.action'} eq 'rename') {
                    167: 	if (-e $conspace) {
                    168: 	    if ($ENV{'form.newfilename'}) {
                    169:                unless (rename('/home/'.$uname.'/public_html'.$fn,
                    170:           '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    171: 	    $r->print('<font color=red>Error: '.$!.'</font>');
                    172:                }
                    173:             }
                    174:         } else {
                    175: 	    $r->print('<p>No such file.</form>');
                    176:             return;
                    177:         }
                    178:     } elsif ($ENV{'form.action'} eq 'delete') { 
                    179: 	if (-e $conspace) {
                    180:             unless (unlink('/home/'.$uname.'/public_html'.$fn)) {
                    181: 	       $r->print('<font color=red>Error: '.$!.'</font>');
                    182:             }
                    183:         } else {
                    184: 	    $r->print('<p>No such file.</form>');
                    185:             return;
                    186:         }
                    187:     } elsif ($ENV{'form.action'} eq 'copy') { 
                    188: 	if (-e $conspace) {
                    189: 	    if ($ENV{'form.newfilename'}) {
                    190:                unless (copy('/home/'.$uname.'/public_html'.$fn,
                    191:            '/home/'.$uname.'/public_html'.$dir.'/'.$ENV{'form.newfilename'})) {
                    192: 	          $r->print('<font color=red>Error: '.$!.'</font>');
                    193:                }
                    194: 	    } else {
                    195: 	       $r->print('<p>No new filename specified.</form>');
                    196:                return;
                    197: 	    }
                    198:         } else {
                    199: 	    $r->print('<p>No such file.</form>');
                    200:             return;
                    201:         }
                    202:     } elsif ($ENV{'form.action'} eq 'newdir') {
                    203:         my $newdir='/home/'.$uname.'/public_html/'.
                    204:                    $fn.$ENV{'form.newfilename'};
                    205:         unless (mkdir($newdir,0770)) {
                    206: 	    $r->print('<font color=red>Error: '.$!.'</font>');
                    207:         }
1.6       www       208:         $r->print('<h3><a href="/priv/'.$uname.$fn.'/">Done</a></h3>');
                    209:         return;
1.5       www       210:     }
                    211:     $r->print('<h3><a href="/priv/'.$uname.$dir.'/">Done</a></h3>');
1.1       www       212: }
                    213: 
                    214: sub handler {
                    215: 
                    216:   my $r=shift;
                    217: 
                    218:   my $fn;
                    219: 
                    220:   if ($ENV{'form.filename'}) {
                    221:       $fn=$ENV{'form.filename'};
                    222:       $fn=~s/^http\:\/\/[^\/]+//;
                    223:   } else {
                    224:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    225:          ' unspecified filename for cfile', $r->filename); 
                    226:      return HTTP_NOT_FOUND;
                    227:   }
                    228: 
                    229:   unless ($fn) { 
                    230:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
                    231:          ' trying to cfile non-existing file', $r->filename); 
                    232:      return HTTP_NOT_FOUND;
                    233:   } 
                    234: 
                    235: # ----------------------------------------------------------- Start page output
                    236:   my $uname;
                    237:   my $udom;
                    238: 
                    239:   ($uname,$udom)=
                    240:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                    241:   unless (($uname) && ($udom)) {
                    242:      $r->log_reason($uname.' at '.$udom.
1.4       www       243:          ' trying to manipulate file '.$ENV{'form.filename'}.
1.1       www       244:          ' ('.$fn.') - not authorized', 
                    245:          $r->filename); 
                    246:      return HTTP_NOT_ACCEPTABLE;
                    247:   }
                    248: 
                    249:   $fn=~s/\/\~(\w+)//;
                    250: 
                    251:   $r->content_type('text/html');
                    252:   $r->send_http_header;
                    253: 
                    254:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
                    255: 
                    256:   $r->print(
                    257:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
                    258: 
                    259:   
                    260:   $r->print('<h1>Construction Space <tt>'.$fn.'</tt></h1>');
                    261:   
                    262:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
                    263:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
                    264:                '</font></h3>');
                    265:   }
                    266: 
1.2       www       267:   if ($ENV{'form.action'} eq 'delete') {
                    268:       $r->print('<h3>Delete</h3>');
                    269:   } elsif ($ENV{'form.action'} eq 'rename') {
                    270:       $r->print('<h3>Rename</h3>');
                    271:   } elsif ($ENV{'form.action'} eq 'newdir') {
                    272:       $r->print('<h3>New Directory</h3>');
                    273:   } elsif ($ENV{'form.action'} eq 'copy') {
                    274:       $r->print('<h3>Copy</h3>');
                    275:   } else {
                    276:      $r->print('<p>Unknown Action</body></html>');
                    277:      return OK;  
                    278:   }
1.1       www       279:   if ($ENV{'form.phase'} eq 'two') {
1.4       www       280:       &phasetwo($r,$fn,$uname,$udom);
1.1       www       281:   } else {
1.3       www       282:       &phaseone($r,$fn,$uname,$udom);
1.1       www       283:   }
                    284: 
                    285:   $r->print('</body></html>');
                    286:   return OK;  
                    287: }
                    288: 
                    289: 1;
                    290: __END__

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