File:  [LON-CAPA] / loncom / interface / lonsource.pm
Revision 1.35: download - view: text, annotated - select for diffs
Mon May 25 15:36:11 2015 UTC (9 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_2_uiuc, version_2_11_2_educog, version_2_11_2, version_2_11_1, HEAD
- Code used to generate path to new file moved to new subroutine:
  &get_path_to_newfile() to facilitate reuse.

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

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