Annotation of loncom/publisher/lonupload.pm, revision 1.64

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Handler to upload files into construction space
                      3: #
1.64    ! raeburn     4: # $Id: lonupload.pm,v 1.63 2013/07/02 19:04:49 raeburn Exp $
1.8       matthew     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: #
1.10      harris41   28: ###
1.1       www        29: 
1.39      jms        30: =head1 NAME
                     31: 
                     32: Apache::lonupload - upload files into construction space
                     33: 
                     34: =head1 SYNOPSIS
                     35: 
                     36: Invoked by /etc/httpd/conf/srm.conf:
                     37: 
                     38:  <Location /adm/upload>
                     39:  PerlAccessHandler       Apache::lonacc
                     40:  SetHandler perl-script
                     41:  PerlHandler Apache::lonupload
                     42:  ErrorDocument     403 /adm/login
                     43:  ErrorDocument     404 /adm/notfound.html
                     44:  ErrorDocument     406 /adm/unauthorized.html
                     45:  ErrorDocument	  500 /adm/errorhandler
                     46:  </Location>
                     47: 
                     48: =head1 INTRODUCTION
                     49: 
                     50: This module uploads a file sitting on a client computer into 
                     51: library server construction space.
                     52: 
                     53: This is part of the LearningOnline Network with CAPA project
                     54: described at http://www.lon-capa.org.
                     55: 
                     56: =head1 HANDLER SUBROUTINE
                     57: 
                     58: This routine is called by Apache and mod_perl.
                     59: 
                     60: =over 4
                     61: 
                     62: =item *
                     63: 
                     64: Initialize variables
                     65: 
                     66: =item *
                     67: 
                     68: Start page output
                     69: 
                     70: =item *
                     71: 
1.51      raeburn    72: output relevant interface phase (phaseone, phasetwo, phasethree or phasefour)
1.39      jms        73: 
                     74: =item *
                     75: 
                     76: (phase one is to specify upload file; phase two is to handle conditions
                     77: subsequent to specification--like overwriting an existing file; phase three
                     78: is to handle processing of secondary uploads - of embedded objects in an
                     79: html file).
                     80: 
                     81: =back
                     82: 
                     83: =head1 OTHER SUBROUTINES
                     84: 
1.40      jms        85: =over
1.39      jms        86: 
1.40      jms        87: =item phaseone()
1.39      jms        88: 
1.40      jms        89: Interface for specifying file to upload.
1.39      jms        90: 
1.40      jms        91: =item phasetwo()
1.39      jms        92: 
1.40      jms        93: Interface for handling post-conditions about uploading (such
1.39      jms        94: as overwriting an existing file).
                     95: 
1.40      jms        96: =item phasethree()
1.39      jms        97: 
1.40      jms        98: Interface for handling secondary uploads of embedded objects
1.39      jms        99: in an html file.
                    100: 
1.51      raeburn   101: =item phasefour()
                    102: 
                    103: Interface for handling optional renaming of links to embedded
                    104: objects. 
                    105: 
1.40      jms       106: =item upfile_store()
1.39      jms       107: 
1.40      jms       108: Store contents of uploaded file into temporary space.  Invoked
1.39      jms       109: by phaseone subroutine.
                    110: 
1.40      jms       111: =item check_extension()
1.39      jms       112: 
1.40      jms       113: Checks if filename extension is permitted and checks type
1.39      jms       114:  of file - if html file, calls parser to check for embedded objects.
                    115:  Invoked by phasetwo subroutine.
                    116: 
                    117: =back
                    118: 
                    119: =cut
                    120: 
1.1       www       121: package Apache::lonupload;
                    122: 
                    123: use strict;
                    124: use Apache::File;
                    125: use File::Copy;
1.13      foxr      126: use File::Basename;
1.1       www       127: use Apache::Constants qw(:common :http :methods);
1.10      harris41  128: use Apache::loncommon();
1.13      foxr      129: use Apache::lonnet;
1.14      foxr      130: use HTML::Entities();
1.20      www       131: use Apache::lonlocal;
1.29      albertel  132: use Apache::lonnet;
1.34      albertel  133: use LONCAPA();
1.12      foxr      134: 
                    135: my $DEBUG=0;
                    136: 
                    137: sub Debug {
1.30      albertel  138:     # Put out the indicated message but only if DEBUG is true.
1.22      albertel  139:     if ($DEBUG) {
1.30      albertel  140: 	my ($r,$message) = @_;
                    141: 	$r->log_reason($message);
1.22      albertel  142:     }
1.12      foxr      143: }
1.1       www       144: 
1.2       www       145: sub upfile_store {
                    146:     my $r=shift;
                    147: 	
1.29      albertel  148:     my $fname=$env{'form.upfile.filename'};
1.2       www       149:     $fname=~s/\W//g;
                    150:     
1.29      albertel  151:     chomp($env{'form.upfile'});
1.1       www       152:   
1.29      albertel  153:     my $datatoken=$env{'user.name'}.'_'.$env{'user.domain'}.
1.2       www       154: 		  '_upload_'.$fname.'_'.time.'_'.$$;
                    155:     {
                    156:        my $fh=Apache::File->new('>'.$r->dir_config('lonDaemons').
                    157:                                    '/tmp/'.$datatoken.'.tmp');
1.29      albertel  158:        print $fh $env{'form.upfile'};
1.1       www       159:     }
1.2       www       160:     return $datatoken;
                    161: }
                    162: 
                    163: sub phaseone {
1.63      raeburn   164:     my ($r,$fn,$mode,$uname,$udom)=@_;
1.25      raeburn   165:     my $action = '/adm/upload';
                    166:     if ($mode eq 'testbank') {
                    167:         $action = '/adm/testbank';
                    168:     } elsif ($mode eq 'imsimport') {
                    169:         $action = '/adm/imsimport';
                    170:     }
1.49      bisitz    171: 
                    172:     # Check for file to be uploaded
1.29      albertel  173:     $env{'form.upfile.filename'}=~s/\\/\//g;
                    174:     $env{'form.upfile.filename'}=~s/^.*\/([^\/]+)$/$1/;
1.49      bisitz    175:     if (!$env{'form.upfile.filename'}) {
1.63      raeburn   176:         $r->print('<p class="LC_warning">'.&mt('No upload file specified.').'</p>'.
                    177:                   &earlyout($fn,$uname,$udom));
1.49      bisitz    178:         return;
                    179:     }
                    180: 
1.55      www       181:     # Append the name of the uploaded file
1.49      bisitz    182:     $fn.=$env{'form.upfile.filename'};
                    183:     $fn=~s/(\/)+/\//g;
                    184: 
                    185:     # Check for illegal filename
                    186:     &Debug($r, "Filename for upload: $fn");
                    187:     if (!(($fn) && ($fn!~/\/$/))) {
                    188:         $r->print('<p class="LC_warning">'.&mt('Illegal filename.').'</p>');
                    189:         return;
                    190:     }
1.63      raeburn   191:     # Check if quota exceeded
                    192:     my $filesize = length($env{'form.upfile'});
                    193:     if (!$filesize) {
                    194:         $r->print('<p class="LC_warning">'.
                    195:                   &mt('Unable to upload [_1]. (size = [_2] bytes)',
                    196:                       '<span class="LC_filename">'.$env{'form.upfile.filename'}.'</span>',
                    197:                       $filesize).'<br />'.
                    198:                   &mt('Either the file you attempted to upload was empty, or your web browser was unable to read its contents.').'<br />'.
                    199:                   '</p>'.
                    200:                   &earlyout($fn,$uname,$udom));
                    201:         return;
                    202:     }
                    203:     $filesize = int($filesize/1000); #expressed in kb
1.64    ! raeburn   204:     my $authorspace = $Apache::lonnet::perlvar{'lonDocRoot'}."/priv/$udom/$uname";
        !           205:     my $output = &Apache::loncommon::excess_filesize_authorspace($uname,$udom,$authorspace,
        !           206:                                                                  $env{'form.upfile.filename'},$filesize,'upload');
        !           207:     if ($output) {
        !           208:         $r->print($output.&earlyout($fn,$uname,$udom));
1.63      raeburn   209:         return;
                    210:     }
1.64    ! raeburn   211: 
1.55      www       212: # Split part that I can change from the part that I cannot change
                    213:     my ($fn1,$fn2)=($fn=~/^(\/priv\/[^\/]+\/[^\/]+\/)(.*)$/);
1.49      bisitz    214:     # Display additional options for upload
                    215:     # and upload button
                    216:     $r->print(
                    217:         '<form action="'.$action.'" method="post" name="fileupload">'
                    218:        .'<input type="hidden" name="phase" value="two" />'
                    219:        .'<input type="hidden" name="datatoken" value="'.&upfile_store.'" />'
                    220:     );
                    221:     $r->print(
                    222:         &Apache::lonhtmlcommon::start_pick_box()
                    223:        .&Apache::lonhtmlcommon::row_title(&mt('Save uploaded file as'))
1.55      www       224:        .'<span class="LC_filename">'.$fn1.'</span>'
                    225:        .'<input type="hidden" name="filename1" value="'.$fn1.'" />'
                    226:        .'<input type="text" size="50" name="filename2" value="'.$fn2.'" />'
1.49      bisitz    227:        .&Apache::lonhtmlcommon::row_closure()
                    228:        .&Apache::lonhtmlcommon::row_title(&mt('File Type'))
                    229:        .'<select name="filetype">'
                    230:        .'<option value="standard" selected="selected">'.&mt('Regular file').'</option>'
                    231:        .'<option value="testbank">'.&mt('Testbank file').'</option>'
                    232:        .'<option value="imsimport">'.&mt('IMS package').'</option>'
                    233:        .'</select>'.&Apache::loncommon::help_open_topic("Uploading_File_Options")
                    234:        .&Apache::lonhtmlcommon::row_closure(1)
                    235:        .&Apache::lonhtmlcommon::end_pick_box()
                    236:     );
                    237:     $r->print(
                    238:         '<p>'
                    239:        .'<input type="button" value="'.&mt('Upload').'" onclick="javascript:verifyForm()"/>'
                    240:        .'</p>'
                    241:        .'</form>'
                    242:     );
1.13      foxr      243: 
1.49      bisitz    244:    # Check for bad extension and warn user
                    245:     if ($fn=~/\.(\w+)$/ && 
                    246:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.41      bisitz    247:                 $r->print('<p class="LC_error">'
1.49      bisitz    248:                           .&mt('The extension on this file, [_1], is reserved internally by LON-CAPA.',
                    249:                                '<span class="LC_filename">'.$1.'</span>')
1.41      bisitz    250:                           .' <br />'.&mt('Please change the extension.')
                    251:                           .'</p>');
1.49      bisitz    252:     } elsif($fn=~/\.(\w+)$/ && 
                    253:                     !defined(&Apache::loncommon::fileembstyle($1))) {
1.41      bisitz    254:                 $r->print('<p class="LC_error">'
1.49      bisitz    255:                          .&mt('The extension on this file, [_1], is not recognized by LON-CAPA.',
                    256:                               '<span class="LC_filename">'.$1.'</span>')
1.41      bisitz    257:                          .' <br />'.&mt('Please change the extension.')
                    258:                          .'</p>');
1.22      albertel  259:     }
1.1       www       260: }
                    261: 
                    262: sub phasetwo {
1.58      raeburn   263:     my ($r,$fn,$mode)=@_;
1.55      www       264: 
1.37      raeburn   265:     my $output;
1.25      raeburn   266:     my $action = '/adm/upload';
                    267:     my $returnflag = '';
                    268:     if ($mode eq 'testbank') {
                    269:         $action = '/adm/testbank';
                    270:     } elsif ($mode eq 'imsimport') {
                    271:         $action = '/adm/imsimport';
                    272:     }
1.22      albertel  273:     $fn=~s/\/+/\//g;
1.55      www       274:     if ($fn) {
1.58      raeburn   275: 	my $target= $r->dir_config('lonDocRoot').'/'.$fn;
1.22      albertel  276: 	&Debug($r, "target -> ".$target);
1.13      foxr      277: #     target is the full filesystem path of the destination file.
1.22      albertel  278: 	my $base = &File::Basename::basename($fn);
                    279: 	my $path = &File::Basename::dirname($fn);
1.26      albertel  280: 	$base    = &HTML::Entities::encode($base,'<>&"');
1.22      albertel  281: 	my $url  = $path."/".$base; 
                    282: 	&Debug($r, "URL is now ".$url);
1.29      albertel  283: 	my $datatoken=$env{'form.datatoken'};
1.22      albertel  284: 	if (($fn) && ($datatoken)) {
1.36      www       285:             if ($env{'form.cancel'}) {
                    286:                 my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
                    287:                 my $dirpath=$path.'/';
                    288:                 $dirpath=~s/\/+/\//g;
1.49      bisitz    289:                 $output .= '<p class="LC_warning">'.&mt('Upload cancelled.').'</p>'
                    290:                           .'<p><a href="'.$dirpath.'">'.
                    291:                           &mt('Back to Directory').'</a></p>';
                    292:             } elsif ((-e $target) && (!$env{'form.override'})) {
                    293:                 $output .= '<form action="'.$action.'" method="post">'
                    294:                           .'<p class="LC_warning">'
                    295:                           .&mt('File [_1] already exists.',
                    296:                                '<span class="LC_filename">'.$fn.'</span>')
                    297:                          .'<input type="hidden" name="phase" value="two" />'
                    298:                          .'<input type="hidden" name="filename" value="'.$url.'" />'
                    299:                          .'<input type="hidden" name="datatoken" value="'.$datatoken.'" />'
                    300:                          .'<p>'
                    301:                          .'<input type="submit" name="cancel" value="'.&mt('Cancel').'" />'
                    302:                          .' <input type="submit" name="override" value="'.&mt('Overwrite').'" />'
                    303:                          .'</p>'
                    304:                          .'</form>';
1.36      www       305:             } else {
1.22      albertel  306: 		my $source=$r->dir_config('lonDaemons').'/tmp/'.$datatoken.'.tmp';
1.27      www       307: 		my $dirpath=$path.'/';
                    308: 		$dirpath=~s/\/+/\//g;
1.22      albertel  309: 		# Check for bad extension and disallow upload
1.37      raeburn   310:                 my $result;
                    311:                 ($result,$returnflag) = &check_extension($fn,$mode,$source,$target,$action,$dirpath,$url);
                    312:                 $output .= $result;
1.22      albertel  313: 	    }
                    314: 	} else {
1.37      raeburn   315: 	    $output .= '<span class="LC_error">'.
1.22      albertel  316: 		      &mt('Please use browser "Back" button and pick a filename').
1.37      raeburn   317: 		      '</span><br />';
1.22      albertel  318: 	}
1.1       www       319:     } else {
1.37      raeburn   320: 	$output .= '<span class="LC_error">'.
                    321: 		   &mt('Please use browser "Back" button and pick a filename').
                    322: 		   '</span><br />';
                    323:     }
                    324:     return ($output,$returnflag);
                    325: }
                    326: 
                    327: sub check_extension {
                    328:     my ($fn,$mode,$source,$target,$action,$dirpath,$url) = @_;
                    329:     my ($result,$returnflag);
                    330:     # Check for bad extension and disallow upload
                    331:     if ($fn=~/\.(\w+)$/ &&
                    332:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
1.49      bisitz    333:         $result .= '<p class="LC_warning">'.
                    334:                    &mt('File [_1] could not be copied.',
                    335:                        '<span class="LC_filename">'.$fn.'</span> ').
                    336:                    '<br />'.
                    337:                    &mt('The extension on this file is reserved internally by LON-CAPA.').
                    338:                    '</p>';
1.37      raeburn   339:     } elsif ($fn=~/\.(\w+)$/ &&
                    340:              !defined(&Apache::loncommon::fileembstyle($1))) {
1.49      bisitz    341:         $result .= '<p class="LC_warning">'.
                    342:                    &mt('File [_1] could not be copied.',
                    343:                        '<span class="LC_filename">'.$fn.'</span> ').
                    344:                    '<br />'.
                    345:                    &mt('The extension on this file is not recognized by LON-CAPA.').
                    346:                    '</p>';
1.37      raeburn   347:     } elsif (-d $target) {
1.49      bisitz    348:         $result .= '<p class="LC_warning">'.
                    349:                    &mt('File [_1] could not be copied.',
                    350:                        '<span class="LC_filename">'.$fn.'</span>').
                    351:                    '<br />'.
                    352:                    &mt('The target is an existing directory.').
                    353:                    '</p>';
1.37      raeburn   354:     } elsif (copy($source,$target)) {
                    355:         chmod(0660, $target); # Set permissions to rw-rw---.
                    356:         if ($mode eq 'testbank' || $mode eq 'imsimport') {
                    357:             $returnflag = 'ok';
1.49      bisitz    358:             $result .= '<p class="LC_success">'
                    359:                       .&mt('Your file - [_1] - was uploaded successfully.',
                    360:                            '<span class="LC_filename">'.$fn.'<span>')
                    361:                       .'</p>';
1.37      raeburn   362:         } else {
1.49      bisitz    363:             $result .= '<p class="LC_success">'
                    364:                       .&mt('File copied.')  
                    365:                       .'</p>';
1.37      raeburn   366:         }
                    367:         # Check for embedded objects.
                    368:         my (%allfiles,%codebase);
                    369:         my ($text,$header,$css,$js);
                    370:         if (($mode ne 'imsimport') && ($target =~ /\.(htm|html|shtml)$/i)) {
                    371:             my (%allfiles,%codebase);
                    372:             &Apache::lonnet::extract_embedded_items($target,\%allfiles,\%codebase);
                    373:             if (keys(%allfiles) > 0) {
1.50      raeburn   374:                 my ($currentpath) = ($url =~ m{^(.+)/[^/]+$});
1.51      raeburn   375:                 my $state = &embedded_form_elems('upload_embedded',$url,$mode);
                    376:                 my ($embedded,$num,$pathchg) = 
                    377:                     &Apache::loncommon::ask_for_embedded_content($action,$state,\%allfiles,
                    378:                                                                  \%codebase,
                    379:                                                                  {'error_on_invalid_names'   => 1,
                    380:                                                                   'ignore_remote_references' => 1,
                    381:                                                                   'current_path'             => $currentpath});
1.50      raeburn   382:                 if ($embedded) {
1.51      raeburn   383:                     $result .= '<h3>'.&mt('Reference Warning').'</h3>';
                    384:                     if ($num) {
                    385:                         $result .= '<p>'.&mt('Completed upload of the file.').' '.&mt('This file contained references to other files.').'</p>'.
                    386:                                    '<p>'.&mt('Please select the locations from which the referenced files are to be uploaded.').'</p>'.
                    387:                                    $embedded;
                    388:                         if ($mode eq 'testbank') {
                    389:                             $returnflag = 'embedded';
                    390:                             $result .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without these files.','<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
                    391:                         }
                    392:                     } else {
                    393:                         $result .= '<p>'.&mt('Completed upload of the file.').'</p>'.$embedded;
                    394:                         if ($pathchg) {
                    395:                             if ($mode eq 'testbank') {
                    396:                                 $returnflag = 'embedded';
                    397:                                 $result .=  '<p>'.&mt('Or [_1]continue[_2] the testbank import without modifying the references(s).','<a href="javascript:document.testbankForm.submit();">','</a>').'</p>';
                    398:                             }
                    399:                         }
                    400:                     }
1.37      raeburn   401:                 }
                    402:             }
                    403:         }
                    404:         if (($mode ne 'imsimport') && ($mode ne 'testbank')) {
1.49      bisitz    405:             $result .= '<br /><a href="'.$url.'">'.
                    406:                         &mt('View file').'</a>';
1.37      raeburn   407:         }
                    408:     } else {
                    409:         $result .= &mt('Failed to copy: [_1].',$!);
                    410:     }
                    411:     if ($mode ne 'imsimport' && $mode ne 'testbank') {
1.49      bisitz    412:         $result .= '<br /><a href="'.$dirpath.'">'.
                    413:                    &mt('Back to Directory').'</a><br />';
1.37      raeburn   414:     }
                    415:     return ($result,$returnflag);
                    416: }
                    417: 
                    418: sub phasethree {
                    419:     my ($r,$fn,$uname,$udom,$mode) = @_;
1.55      www       420: 
1.51      raeburn   421:     my $action = '/adm/upload'; 
                    422:     if ($mode eq 'testbank') {
                    423:         $action = '/adm/testbank';
                    424:     } elsif ($mode eq 'imsimport') {
                    425:         $action = '/adm/imsimport';
                    426:     }
1.56      raeburn   427:     my $url_root = "/priv/$udom/$uname";
                    428:     my $dir_root = $r->dir_config('lonDocRoot').$url_root;
1.51      raeburn   429:     my $path = &File::Basename::dirname($fn);
1.58      raeburn   430:     $path =~ s{^\Q$url_root\E}{};
1.51      raeburn   431:     my $filename = &HTML::Entities::encode($env{'form.filename'},'<>&"');
                    432:     my $state = &embedded_form_elems('modify_orightml',$filename,$mode).
                    433:                 '<input type="hidden" name="phase" value="four" />';
                    434:     my ($result,$returnflag) = 
                    435:         &Apache::loncommon::upload_embedded($mode,$path,$uname,$udom,
                    436:                                             $dir_root,$url_root,undef,
                    437:                                             undef,undef,$state,$action);
                    438:     if ($mode ne 'imsimport' && $mode ne 'testbank') {
1.58      raeburn   439:         $result .= '<br /><h3><a href="'.$fn.'">'.
1.51      raeburn   440:                   &mt('View main file').'</a></h3>'.
                    441:                   '<h3><a href="'.$url_root.$path.'">'.
                    442:                   &mt('Back to Directory').'</a></h3><br />';
                    443:     }
                    444:     return ($result,$returnflag);
                    445: }
                    446: 
                    447: sub embedded_form_elems {
                    448:     my ($action,$filename,$mode) = @_;
                    449:     return <<STATE;
                    450:     <input type="hidden" name="action" value="$action" />
                    451:     <input type="hidden" name="mode" value="$mode" />
                    452:     <input type="hidden" name="filename" value="$filename" />
                    453: STATE
                    454: }
                    455: 
                    456: sub phasefour {
                    457:     my ($r,$fn,$uname,$udom,$mode) = @_;
1.55      www       458: 
1.51      raeburn   459:     my $action = '/adm/upload';
                    460:     if ($mode eq 'testbank') {
                    461:         $action = '/adm/testbank';
                    462:     } elsif ($mode eq 'imsimport') {
                    463:         $action = '/adm/imsimport';
                    464:     }
1.37      raeburn   465:     my $result;
1.56      raeburn   466:     my $url_root = "/priv/$udom/$uname";
                    467:     my $dir_root = $r->dir_config('lonDocRoot').$url_root;
1.37      raeburn   468:     my $path = &File::Basename::dirname($fn);
1.58      raeburn   469:     $path =~ s{^\Q$url_root\E}{};
1.60      raeburn   470:     my $outcome = 
                    471:         &Apache::loncommon::modify_html_refs($mode,$path,$uname,$udom,$dir_root);
                    472:     $result .= $outcome;
1.37      raeburn   473:     if ($mode ne 'imsimport' && $mode ne 'testbank') {
1.58      raeburn   474:         $result .= '<br /><h3><a href="'.$fn.'">'.
1.51      raeburn   475:                   &mt('View main file').'</a></h3>'.
                    476:                   '<h3><a href="'.$url_root.$path.'">'.
                    477:                   &mt('Back to Directory').'</a></h3><br />';
1.1       www       478:     }
1.37      raeburn   479:     return $result;
1.1       www       480: }
                    481: 
1.63      raeburn   482: sub earlyout {
                    483:     my ($fn,$uname,$udom) = @_;
                    484:     if ($fn =~ m{^(/priv/$udom/$uname(?:.*)/)[^/]*}) {
                    485:         return &Apache::lonhtmlcommon::actionbox(
                    486:                ['<a href="'.$1.'">'.&mt('Return to Directory').'</a>']);
                    487:     }
                    488:     return;
                    489: }
                    490: 
1.10      harris41  491: # ---------------------------------------------------------------- Main Handler
1.1       www       492: sub handler {
                    493: 
1.22      albertel  494:     my $r=shift;
1.25      raeburn   495:     my $javascript = '';
1.55      www       496:     my $fn=$env{'form.filename'};
                    497: 
                    498:     if ($env{'form.filename1'}) {
                    499:        $fn=$env{'form.filename1'}.$env{'form.filename2'};
                    500:     }
                    501:     $fn=~s/\/+/\//g;
                    502: 
                    503:     unless ($fn) {
                    504:         $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
                    505:                        ' unspecified filename for upload', $r->filename);
                    506:         return HTTP_NOT_FOUND;
1.22      albertel  507:     }
                    508: 
1.61      raeburn   509:     my ($uname,$udom)=&Apache::lonnet::constructaccess($fn);
1.58      raeburn   510: 
                    511:     unless (($uname) && ($udom)) {
                    512:         $r->log_reason($uname.' at '.$udom.
                    513:                        ' trying to publish file '.$env{'form.filename'}.
                    514:                        ' - not authorized',
                    515:                        $r->filename);
                    516:         return HTTP_NOT_ACCEPTABLE;
                    517:     }
                    518: 
                    519: # ----------------------------------------------------------- Start page output
                    520: 
                    521:     &Apache::loncommon::content_type($r,'text/html');
                    522:     $r->send_http_header;
                    523: 
1.29      albertel  524:     unless ($env{'form.phase'} eq 'two') {
1.58      raeburn   525:         $javascript = <<"ENDJS";
                    526: <script type="text/javascript">
                    527: // <![CDATA[
1.25      raeburn   528: function verifyForm() {
1.28      raeburn   529:     var mode = document.fileupload.filetype.options[document.fileupload.filetype.selectedIndex].value
1.25      raeburn   530:     if (mode == "testbank") {
1.28      raeburn   531:         document.fileupload.action = "/adm/testbank";
1.25      raeburn   532:     }
                    533:     if (mode == "imsimport") {
1.28      raeburn   534:         document.fileupload.action = "/adm/imsimport";
1.25      raeburn   535:     }
                    536:     if (mode == "standard") {
1.28      raeburn   537:         document.fileupload.action = "/adm/upload";
1.25      raeburn   538:     }
1.28      raeburn   539:     document.fileupload.submit();
1.25      raeburn   540: }
1.58      raeburn   541: // ]]>
                    542: </script>
                    543: ENDJS
1.25      raeburn   544:     }
1.1       www       545: 
1.58      raeburn   546:     my $londocroot = $r->dir_config('lonDocRoot');
                    547:     my $trailfile = $fn;
                    548:     $trailfile =~ s{^/(priv/)}{$londocroot/$1};
1.1       www       549: 
1.47      bisitz    550:     # Breadcrumbs
1.59      raeburn   551:     my $brcrum = [{'href' => &Apache::loncommon::authorspace($fn),
1.62      raeburn   552:                    'text' => 'Authoring Space'},
1.47      bisitz    553:                   {'href' => '/adm/upload',
1.62      raeburn   554:                    'text' => 'Upload file to Authoring Space'}];
                    555:     $r->print(&Apache::loncommon::start_page('Upload file to Authoring Space',
1.47      bisitz    556:                                              $javascript,
                    557:                                              {'bread_crumbs' => $brcrum,})
                    558:              .&Apache::loncommon::head_subbox(
1.58      raeburn   559:                 &Apache::loncommon::CSTR_pageheader($trailfile))
1.47      bisitz    560:     );
1.3       www       561:   
1.29      albertel  562:     if (($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) {
1.52      www       563:         $r->print('<p class="LC_info">'
1.46      bisitz    564:                  .&mt('Co-Author [_1]',$uname.':'.$udom)
1.44      bisitz    565:                  .'</p>'
                    566:         );
1.22      albertel  567:     }
1.51      raeburn   568:     if ($env{'form.phase'} eq 'four') {
                    569:         my $output = &phasefour($r,$fn,$uname,$udom,'author');
                    570:         $r->print($output);
                    571:     } elsif ($env{'form.phase'} eq 'three') {
1.53      raeburn   572:         my ($output,$rtnflag) = &phasethree($r,$fn,$uname,$udom,'author');
1.37      raeburn   573:         $r->print($output);
                    574:     } elsif ($env{'form.phase'} eq 'two') {
1.58      raeburn   575: 	my ($output,$returnflag) = &phasetwo($r,$fn);
1.37      raeburn   576:         $r->print($output);
1.22      albertel  577:     } else {
1.63      raeburn   578: 	&phaseone($r,$fn,undef,$uname,$udom);
1.22      albertel  579:     }
1.1       www       580: 
1.31      albertel  581:     $r->print(&Apache::loncommon::end_page());
1.22      albertel  582:     return OK;  
1.1       www       583: }
1.7       www       584: 
                    585: 1;
                    586: __END__
1.10      harris41  587: 
                    588: 

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