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

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

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