Annotation of loncom/interface/lonsource.pm, revision 1.33

1.1       taceyjo1    1: # The LearningOnline Network with CAPA
1.23      bisitz      2: # Source Code handler
1.1       taceyjo1    3: #
1.33    ! raeburn     4: # $Id: lonsource.pm,v 1.32 2014/02/11 15:49:15 bisitz Exp $
1.1       taceyjo1    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: ###
                     29: 
                     30: 
                     31: package Apache::lonsource;
                     32: 
                     33: use strict;
1.11      albertel   34: use Apache::lonnet;
1.1       taceyjo1   35: use Apache::loncommon();
                     36: use Apache::lonhtmlcommon();
                     37: use Apache::lonsequence();
                     38: use Apache::Constants qw(:common :http);
                     39: use Apache::lonmeta;
                     40: use Apache::File;
                     41: use Apache::lonlocal;
                     42: use HTML::Entities;
1.28      raeburn    43: use LONCAPA qw(:DEFAULT :match);
1.1       taceyjo1   44: 
                     45: sub make_link {
1.4       taceyjo1   46:     my ($filename, $listname) = @_;
1.18      albertel   47:     my $sourcelink = "/adm/source?inhibitmenu=yes&filename=".$filename."&listname=".$listname;
1.2       albertel   48: 
                     49:     return $sourcelink;
1.1       taceyjo1   50: }
                     51: 
                     52: sub stage_2 {
1.26      www        53:     my ($r, $filename, $listname) = @_;
                     54:     my ($author)=($filename=~/\/res\/[^\/]+\/([^\/]+)\//);
1.33    ! raeburn    55:     $r->print(&Apache::loncommon::start_page('Copy Problem Source Code to Authoring Space',undef,
        !            56:                                              {'only_body' => 1,})
1.21      bisitz     57:              .&mt('Please enter the directory that you would like the source code to go into.')
                     58:              .'<p>'
1.31      bisitz     59:              .&mt('Note: the path is in reference to the root of your Authoring Space,'
1.21      bisitz     60:                  .' and new directories will be automatically created.')
                     61:              .'</p>');
1.13      www        62:     $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
1.5       taceyjo1   63:               <input type="hidden" name="filename" value="'.$filename.'" />
                     64:               <input type="hidden" name="listname" value="'.$listname.'" />
                     65:               <input type="hidden" name="action" value="copy_stage" />
1.21      bisitz     66:               <input type="text" size="50" name="newpath" value="/'.&mt('shared_source').'/'.$author.'" />&nbsp;
                     67:               <input type="submit" value="'.&mt('Copy').'" />
1.33    ! raeburn    68:               </form>'.
        !            69:               &Apache::loncommon::end_page());
1.5       taceyjo1   70:     return OK;
1.4       taceyjo1   71: }
                     72: 
1.26      www        73: sub copy_author {
1.4       taceyjo1   74:     my $role;
                     75:     my $domain;
                     76:     my $author_name;
1.19      albertel   77:     if ($env{'request.role'} =~ m{^ca\.}) {
1.11      albertel   78:         ($role, $domain, $author_name) = split(/\//,$env{'request.role'});
1.4       taceyjo1   79:     } else {
1.5       taceyjo1   80:         $role = "au.";
1.11      albertel   81:         $domain = $env{'user.domain'};
                     82:         $author_name = $env{'user.name'};
1.5       taceyjo1   83:     }
1.26      www        84:     return ($role,$author_name,$domain);
                     85: }
                     86: 
                     87: 
                     88: sub copy_stage {
                     89:     my ($r, $filename, $listname, $newpath) = @_;
                     90: 
                     91: #Figure out if we are author or co-author
                     92:     my ($role,$author_name,$domain)=&copy_author();
1.5       taceyjo1   93: 
1.26      www        94: # Construct path to copy and filter out any possibly nasty stuff
1.27      raeburn    95:     my $path_to_new_file = $r->dir_config('lonDocRoot').
                     96:                            "/priv/$domain/$author_name/$newpath/$listname";
1.26      www        97:     $path_to_new_file=~s/\.\.//g;
                     98:     $path_to_new_file=~s/\~//g;
                     99:     $path_to_new_file=~s/\/+/\//g;
                    100: 
1.5       taceyjo1  101: #Just checking again for access as we want to make sure that it is really ok now that we have the real path
                    102: 
1.29      raeburn   103:     my ($uname,$udom)= &Apache::lonnet::constructaccess($path_to_new_file);
1.20      albertel  104: 
                    105:     if (!$uname || !$udom) {
1.33    ! raeburn   106: 	$r->print(&Apache::loncommon::start_page('Not Allowed',undef,{'only_body' => 1}));
1.20      albertel  107: 	$r->print(&mt('Not allowed to create file [_1]', $path_to_new_file));
                    108: 	$r->print(&Apache::loncommon::end_page());
                    109: 	return;
1.4       taceyjo1  110:     }
1.20      albertel  111: 
                    112:     #allowed
1.33    ! raeburn   113:     $r->print(&Apache::loncommon::start_page('Copying Source',undef,{'only_body' => 1}));
1.5       taceyjo1  114:     my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
                    115:     $r->print($result);
                    116:     if(($result) && ($result =~ m|published|) ) {
1.26      www       117: 	&delete_copy_file($r, $newpath, $filename, $path_to_new_file, '1');
1.5       taceyjo1  118:     } elsif(($result) && ($result =~ m|exists!|)) {
1.26      www       119: 	&confirm($r, $newpath, $filename, $path_to_new_file);
1.5       taceyjo1  120:     } else {
1.26      www       121: 	&copy_file($r, $newpath, $filename, $path_to_new_file);
1.5       taceyjo1  122:     }
1.20      albertel  123:     
                    124:     $r->print(&Apache::loncommon::end_page());
1.4       taceyjo1  125: }
                    126: 
                    127: sub confirm {
1.26      www       128:     my ($r, $newpath, $filename, $path_to_new_file) = @_;
1.32      bisitz    129:     $r->print('<b>'.&mt('Press delete to remove file and replace it with a copy of the source you are viewing.').'</b><br /><br />');
1.13      www       130:     $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
1.5       taceyjo1  131:               <input type="hidden" name="filename" value="'.$filename.'" />
                    132:               <input type="hidden" name="path" value="'.$path_to_new_file.'" />
                    133:               <input type="hidden" name="newpath" value="'.$newpath.'" />
                    134:               <input type="hidden" name="action" value="delete_confirm" />
                    135: 
                    136:               <input type="submit" value="Delete" />
                    137:               </form>');
1.4       taceyjo1  138: }
                    139: 
1.6       taceyjo1  140: sub delete_copy_file {
1.26      www       141:     my ($r, $newpath, $filename, $path_to_new_file, $type) = @_;
1.5       taceyjo1  142:     if($type eq '1') {
1.23      bisitz    143:         $r->print('<p><span class="LC_warning">'
                    144:                  .&mt('Cannot delete non-obsolete published file.')
                    145:                  .'</span><br />'
                    146:                  .&mt('Please use the code view in previous window to use shared code.')
                    147:                  .'<br /><br />');
1.33    ! raeburn   148:         $r->print('<form name="delete_done" action="/adm/source" target="_parent" method="post">'
        !           149:                  .'<input type="button" value="'.&mt('Close Window').'" name="close"'
1.24      raeburn   150:                  .' onclick="window.close()" />'
1.23      bisitz    151:                  .'</p>');
1.5       taceyjo1  152:     } else {
1.33    ! raeburn   153:         $r->print(&Apache::loncommon::start_page('Copying Source',undef,{'only_body' => 1}));
1.5       taceyjo1  154:         if(-e $path_to_new_file) {
                    155:             unless(unlink($path_to_new_file)) {
1.23      bisitz    156:                 $r->print('<p class="LC_error"">'.&mt('Error:').' '.$!.'</p>');
1.5       taceyjo1  157:                 return 0;
                    158:             }
                    159:         } else {
1.33    ! raeburn   160:             $r->print('<p class="LC_error">'.&mt('No such file').'</p>');
1.5       taceyjo1  161:             return 0;
                    162:         }
1.26      www       163:         &copy_file($r, $newpath, $filename, $path_to_new_file);
1.33    ! raeburn   164:         $r->print(&Apache::loncommon::end_page());
1.5       taceyjo1  165:     }
1.1       taceyjo1  166: }
                    167: 
1.4       taceyjo1  168: sub copy_file {
1.26      www       169:     my ($r, $newpath, $filename, $path_to_new_file) = @_;
1.32      bisitz    170:     $r->print('<b>'.&mt('Creating directories').'</b>');
1.26      www       171: 
                    172: #Figure out if we are author or co-author
                    173:     my ($role,$author_name,$domain)=&copy_author();
                    174: 
1.27      raeburn   175:     my $path = $r->dir_config('lonDocRoot')."/priv/$domain/$author_name/";
1.5       taceyjo1  176:     my @directories = split(/\//,$newpath);
1.26      www       177: 
1.5       taceyjo1  178:     foreach my $now_checking (@directories) {
                    179:         if($now_checking ne '') {
                    180:             $path = $path.'/'.$now_checking;
                    181:             if(-e $path) {} #More moving along, isn't recursion fun'
                    182: 
                    183:             else {
                    184:                 unless(mkdir($path, 02770)) {
1.23      bisitz    185:                     $r->print('<p class="LC_error">'.&mt('Error:').' '.$!.'</p>');
1.5       taceyjo1  186:                     return 0;
                    187:                 }
                    188:                 unless(chmod(02770, ($path))) {
1.23      bisitz    189:                     $r->print('<p class="LC_error"> '.&mt('Error:').' '.$!.'</p>');
1.5       taceyjo1  190:                     return 0;
                    191:                 }
                    192:             }
                    193:         } else { } #Just move along
                    194: 
                    195:     }
1.32      bisitz    196:     $r->print('<br /><b>'.&mt('Copying File').'</b>');
1.6       taceyjo1  197:     my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
1.17      www       198:     my $file_output = &includemeta(&Apache::lonnet::getfile($problem_filename),$filename);
1.5       taceyjo1  199:     my $fs=Apache::File->new(">$path_to_new_file");
                    200:     if (defined($fs)) {
                    201:         print $fs $file_output;
                    202:     }
                    203:     $r->print("<br /><br />");
1.33    ! raeburn   204:     $r->print('<form name="copied_file" action="/adm/source" target="_parent" method="post">'
        !           205:               .'<input type="button" value="'
        !           206:               .&mt('Close Window').'" name="close" onclick="window.close()" />'
        !           207:               .'</form>');
1.5       taceyjo1  208:     #Some 1.3'ish feature is to include the derivative feature, will go here..'
                    209: }
1.4       taceyjo1  210: 
1.1       taceyjo1  211: sub print_item {
1.28      raeburn   212:     my ($r,$filename,$listname) = @_;
                    213:     my $file_output = 
                    214:         &includemeta(&Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$filename),
                    215:                                               $filename);
                    216:     $r->print(&Apache::loncommon::start_page('View Source Code',undef,
                    217:                                              {'only_body' => 1}));
                    218:     if ($file_output ne '') {
                    219:         my $access_to_cstr;
                    220:         my $lonhost = $r->dir_config('lonHostID');
                    221:         if (&Apache::lonnet::is_library($lonhost)) {
                    222:             my @possdoms = &Apache::lonnet::current_machine_domains();
                    223:             foreach my $dom (@possdoms) {
                    224:                 if ($env{"user.role.au./$dom/"}) {
                    225:                     $access_to_cstr = 1;
                    226:                     last;  
                    227:                 }
                    228:             }
                    229:             unless ($access_to_cstr) {
                    230:                 foreach my $key (keys(%env)) {
                    231:                     if ($key =~ m{^\Quser.role.ca./\E($match_domain)/}) {
                    232:                         my $adom = $1;
                    233:                         if (grep(/^\Q$adom\E$/,@possdoms)) {
                    234:                             $access_to_cstr = 1;
                    235:                             last;
                    236:                         }
                    237:                     }
                    238:                 }
                    239:             }
                    240:             if ($access_to_cstr) {
                    241:                 $r->print('
                    242:              <form name="copy" action="/adm/source" target="_parent" method="post">
                    243:               <input type="button" value="'.&mt('Close Window').'" name="close" onclick="window.close();" />
                    244:               <input type="hidden" name="filename" value="'.$filename.'" />
                    245:               <input type="hidden" name="listname" value="'.$listname.'" />
                    246:               <input type="hidden" name="action" value="stage2" />
1.30      raeburn   247:               <input type="submit" value="'.&mt('Copy to Authoring Space').'" />
1.28      raeburn   248:              </form><hr />
                    249:                 ');
                    250:             } else {
                    251:                 $r->print('<p><span class="LC_info">'.
                    252:                           &mt('Source code is displayed, but you can not copy to Authoring Space, as you do not have an author or co-author role on this server.').
                    253:                           '</span></p><a href="javascript:window.close();">'.&mt('Close Window').
                    254:                           '</a><br /><hr />'
                    255:                          );
                    256:             }
                    257:         } else {
                    258:             $r->print('<p><span class="LC_info">'.
                    259:                       &mt('Source code is displayed, but you can not copy to Authoring Space on this server.').
                    260:                           '</span></p><a href="javascript:window.close();">'.&mt('Close Window').
                    261:                           '</a><br /><hr />'
                    262:                      );
                    263: 
                    264:         }
                    265:         my $count=0;
                    266:         my $maxlength=-1;
                    267:         foreach (split ("\n", $file_output)) {
                    268:             $count+=int(length($_)/79);
                    269:             $count++;
                    270:             if (length($_) > $maxlength) {
                    271:                 $maxlength = length($_);
                    272:             }
1.5       taceyjo1  273:         }
1.28      raeburn   274:         my $rows = $count;
                    275:         my $cols = $maxlength;
                    276:         $r->print('<form name="showsrc" action="" method="post" onsubmit="return false">'."\n".
                    277:                   '<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
                    278:                   &HTML::Entities::encode($file_output,'<>&"').'</textarea></form>');
                    279:     } else {
                    280:         $r->print('<p class="LC_warning">'.
                    281:                   &mt('Unable to retrieve file contents.').
                    282:                   '</p><a href="javascript:window.close();">'.&mt('Close Window').'</a>'
                    283:                  );
1.5       taceyjo1  284:     }
1.28      raeburn   285:     $r->print(&Apache::loncommon::end_page());
                    286:     return;
1.1       taceyjo1  287: }
                    288: 
1.17      www       289: sub includemeta {
                    290:     my ($file_output,$orgfilename)=@_;
                    291:     my $escfilename=&escape($orgfilename);
                    292:     my $copytime=time;
                    293:     if ($file_output=~/\<meta\s*name\=\"isbasedonres\"/i) {
                    294: 	$file_output=~s/(\<meta\s*name\=\"isbasedonres\"\s*content\=\"[^\"]*)\"/$1\,\Q$escfilename\E\"/i;
                    295:     } else {
                    296: 	$file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedonres\" content=\"\Q$escfilename\E\" \/\>/i;
                    297:     }
                    298:     if ($file_output=~/\<meta\s*name\=\"isbasedontime\"/i) {
                    299: 	$file_output=~s/(\<meta\s*name\=\"isbasedontime\"\s*content\=\"[^\"]*)\"/$1\,\Q$copytime\E\"/i;
                    300:     } else {
                    301: 	$file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedontime\" content=\"\Q$copytime\E\" \/\>/i;
                    302:     }
1.28      raeburn   303:     if ($file_output eq '-1') {
                    304:         return;
                    305:     } else {
                    306:         return $file_output;
                    307:     }
1.17      www       308: }
1.1       taceyjo1  309: 
1.5       taceyjo1  310: sub handler {
1.2       albertel  311:     my $r=shift;
1.3       www       312:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.5       taceyjo1  313:                                             ['filename','listname']);
1.11      albertel  314:     my $filename = $env{'form.filename'};
                    315:     my $listname = $env{'form.listname'};
1.3       www       316:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
                    317:     if ($source ne 'open') {
1.11      albertel  318:         $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
1.5       taceyjo1  319:         return HTTP_NOT_ACCEPTABLE;
1.28      raeburn   320:     }
                    321:     unless (&Apache::lonnet::allowed('bre',$filename)) {
1.11      albertel  322:         $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
1.5       taceyjo1  323:         return HTTP_NOT_ACCEPTABLE;
1.28      raeburn   324:     }
                    325:     unless (&Apache::lonnet::allowed('cre','/')) {
                    326:         $env{'user.error.msg'}="$filename:cre:1:1:Access to source code denied";
                    327:         return HTTP_NOT_ACCEPTABLE;
                    328:     }
1.20      albertel  329: 
                    330:     &Apache::loncommon::content_type($r,'text/html');
                    331:     $r->send_http_header;
                    332: 
1.11      albertel  333:     if ($env{'form.action'} eq 'stage2') {
1.28      raeburn   334:         &stage_2($r,$filename,$listname);
1.11      albertel  335:     } elsif($env{'form.action'} eq 'copy_stage') {
1.28      raeburn   336:         &copy_stage($r,$filename,$listname,$env{'form.newpath'});
1.11      albertel  337:     } elsif($env{'form.action'} eq 'delete_confirm') {
1.28      raeburn   338:         &delete_copy_file($r,$env{'form.newpath'},$filename, $env{'form.path'}, '0');
1.6       taceyjo1  339:     } else {
1.28      raeburn   340:         &print_item($r,$filename,$listname);
1.5       taceyjo1  341:     }
1.2       albertel  342:     return OK;
1.1       taceyjo1  343: }
                    344: 
                    345: 1;
1.5       taceyjo1  346: 
                    347: 

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