File:  [LON-CAPA] / loncom / interface / lonsource.pm
Revision 1.33: download - view: text, annotated - select for diffs
Sat May 23 18:10:02 2015 UTC (9 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Show only body in pop-up.
- "Close Window" button form element needs to be inside <form></form>.

    1: # The LearningOnline Network with CAPA
    2: # Source Code handler
    3: #
    4: # $Id: lonsource.pm,v 1.33 2015/05/23 18:10:02 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: #Figure out if we are author or co-author
   92:     my ($role,$author_name,$domain)=&copy_author();
   93: 
   94: # Construct path to copy and filter out any possibly nasty stuff
   95:     my $path_to_new_file = $r->dir_config('lonDocRoot').
   96:                            "/priv/$domain/$author_name/$newpath/$listname";
   97:     $path_to_new_file=~s/\.\.//g;
   98:     $path_to_new_file=~s/\~//g;
   99:     $path_to_new_file=~s/\/+/\//g;
  100: 
  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: 
  103:     my ($uname,$udom)= &Apache::lonnet::constructaccess($path_to_new_file);
  104: 
  105:     if (!$uname || !$udom) {
  106: 	$r->print(&Apache::loncommon::start_page('Not Allowed',undef,{'only_body' => 1}));
  107: 	$r->print(&mt('Not allowed to create file [_1]', $path_to_new_file));
  108: 	$r->print(&Apache::loncommon::end_page());
  109: 	return;
  110:     }
  111: 
  112:     #allowed
  113:     $r->print(&Apache::loncommon::start_page('Copying Source',undef,{'only_body' => 1}));
  114:     my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
  115:     $r->print($result);
  116:     if(($result) && ($result =~ m|published|) ) {
  117: 	&delete_copy_file($r, $newpath, $filename, $path_to_new_file, '1');
  118:     } elsif(($result) && ($result =~ m|exists!|)) {
  119: 	&confirm($r, $newpath, $filename, $path_to_new_file);
  120:     } else {
  121: 	&copy_file($r, $newpath, $filename, $path_to_new_file);
  122:     }
  123:     
  124:     $r->print(&Apache::loncommon::end_page());
  125: }
  126: 
  127: sub confirm {
  128:     my ($r, $newpath, $filename, $path_to_new_file) = @_;
  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 />');
  130:     $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
  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>');
  138: }
  139: 
  140: sub delete_copy_file {
  141:     my ($r, $newpath, $filename, $path_to_new_file, $type) = @_;
  142:     if($type eq '1') {
  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 />');
  148:         $r->print('<form name="delete_done" action="/adm/source" target="_parent" method="post">'
  149:                  .'<input type="button" value="'.&mt('Close Window').'" name="close"'
  150:                  .' onclick="window.close()" />'
  151:                  .'</p>');
  152:     } else {
  153:         $r->print(&Apache::loncommon::start_page('Copying Source',undef,{'only_body' => 1}));
  154:         if(-e $path_to_new_file) {
  155:             unless(unlink($path_to_new_file)) {
  156:                 $r->print('<p class="LC_error"">'.&mt('Error:').' '.$!.'</p>');
  157:                 return 0;
  158:             }
  159:         } else {
  160:             $r->print('<p class="LC_error">'.&mt('No such file').'</p>');
  161:             return 0;
  162:         }
  163:         &copy_file($r, $newpath, $filename, $path_to_new_file);
  164:         $r->print(&Apache::loncommon::end_page());
  165:     }
  166: }
  167: 
  168: sub copy_file {
  169:     my ($r, $newpath, $filename, $path_to_new_file) = @_;
  170:     $r->print('<b>'.&mt('Creating directories').'</b>');
  171: 
  172: #Figure out if we are author or co-author
  173:     my ($role,$author_name,$domain)=&copy_author();
  174: 
  175:     my $path = $r->dir_config('lonDocRoot')."/priv/$domain/$author_name/";
  176:     my @directories = split(/\//,$newpath);
  177: 
  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)) {
  185:                     $r->print('<p class="LC_error">'.&mt('Error:').' '.$!.'</p>');
  186:                     return 0;
  187:                 }
  188:                 unless(chmod(02770, ($path))) {
  189:                     $r->print('<p class="LC_error"> '.&mt('Error:').' '.$!.'</p>');
  190:                     return 0;
  191:                 }
  192:             }
  193:         } else { } #Just move along
  194: 
  195:     }
  196:     $r->print('<br /><b>'.&mt('Copying File').'</b>');
  197:     my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
  198:     my $file_output = &includemeta(&Apache::lonnet::getfile($problem_filename),$filename);
  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 />");
  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>');
  208:     #Some 1.3'ish feature is to include the derivative feature, will go here..'
  209: }
  210: 
  211: sub print_item {
  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" />
  247:               <input type="submit" value="'.&mt('Copy to Authoring Space').'" />
  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:             }
  273:         }
  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:                  );
  284:     }
  285:     $r->print(&Apache::loncommon::end_page());
  286:     return;
  287: }
  288: 
  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:     }
  303:     if ($file_output eq '-1') {
  304:         return;
  305:     } else {
  306:         return $file_output;
  307:     }
  308: }
  309: 
  310: sub handler {
  311:     my $r=shift;
  312:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  313:                                             ['filename','listname']);
  314:     my $filename = $env{'form.filename'};
  315:     my $listname = $env{'form.listname'};
  316:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
  317:     if ($source ne 'open') {
  318:         $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
  319:         return HTTP_NOT_ACCEPTABLE;
  320:     }
  321:     unless (&Apache::lonnet::allowed('bre',$filename)) {
  322:         $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
  323:         return HTTP_NOT_ACCEPTABLE;
  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:     }
  329: 
  330:     &Apache::loncommon::content_type($r,'text/html');
  331:     $r->send_http_header;
  332: 
  333:     if ($env{'form.action'} eq 'stage2') {
  334:         &stage_2($r,$filename,$listname);
  335:     } elsif($env{'form.action'} eq 'copy_stage') {
  336:         &copy_stage($r,$filename,$listname,$env{'form.newpath'});
  337:     } elsif($env{'form.action'} eq 'delete_confirm') {
  338:         &delete_copy_file($r,$env{'form.newpath'},$filename, $env{'form.path'}, '0');
  339:     } else {
  340:         &print_item($r,$filename,$listname);
  341:     }
  342:     return OK;
  343: }
  344: 
  345: 1;
  346: 
  347: 

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