File:  [LON-CAPA] / loncom / interface / lonsource.pm
Revision 1.36: download - view: text, annotated - select for diffs
Mon Sep 18 16:58:08 2017 UTC (6 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Double escape filename and listname so URLs containing special characters
  are displayed correctly in pop-up window.

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

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