File:  [LON-CAPA] / loncom / interface / lonsource.pm
Revision 1.34: download - view: text, annotated - select for diffs
Sat May 23 21:02:39 2015 UTC (9 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- add missing closing </form> tag.
- return from subroutines.

    1: # The LearningOnline Network with CAPA
    2: # Source Code handler
    3: #
    4: # $Id: lonsource.pm,v 1.34 2015/05/23 21:02:39 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:     return;
  126: }
  127: 
  128: sub confirm {
  129:     my ($r, $newpath, $filename, $path_to_new_file) = @_;
  130:     $r->print('<b>'.&mt('Press delete to remove file and replace it with a copy of the source you are viewing.').'</b><br /><br />');
  131:     $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
  132:               <input type="hidden" name="filename" value="'.$filename.'" />
  133:               <input type="hidden" name="path" value="'.$path_to_new_file.'" />
  134:               <input type="hidden" name="newpath" value="'.$newpath.'" />
  135:               <input type="hidden" name="action" value="delete_confirm" />
  136: 
  137:               <input type="submit" value="Delete" />
  138:               </form>');
  139:     return;
  140: }
  141: 
  142: sub delete_copy_file {
  143:     my ($r, $newpath, $filename, $path_to_new_file, $type) = @_;
  144:     if($type eq '1') {
  145:         $r->print('<p><span class="LC_warning">'
  146:                  .&mt('Cannot delete non-obsolete published file.')
  147:                  .'</span><br />'
  148:                  .&mt('Please use the code view in previous window to use shared code.')
  149:                  .'<br /><br />');
  150:         $r->print('<form name="delete_done" action="/adm/source" target="_parent" method="post">'
  151:                  .'<input type="button" value="'.&mt('Close Window').'" name="close"'
  152:                  .' onclick="window.close()" />'
  153:                  .'</form></p>');
  154:     } else {
  155:         $r->print(&Apache::loncommon::start_page('Copying Source',undef,{'only_body' => 1}));
  156:         if(-e $path_to_new_file) {
  157:             unless(unlink($path_to_new_file)) {
  158:                 $r->print('<p class="LC_error"">'.&mt('Error:').' '.$!.'</p>');
  159:                 return 0;
  160:             }
  161:         } else {
  162:             $r->print('<p class="LC_error">'.&mt('No such file').'</p>');
  163:             return 0;
  164:         }
  165:         &copy_file($r, $newpath, $filename, $path_to_new_file);
  166:         $r->print(&Apache::loncommon::end_page());
  167:     }
  168:     return;
  169: }
  170: 
  171: sub copy_file {
  172:     my ($r, $newpath, $filename, $path_to_new_file) = @_;
  173:     $r->print('<b>'.&mt('Creating directories').'</b>');
  174: 
  175: #Figure out if we are author or co-author
  176:     my ($role,$author_name,$domain)=&copy_author();
  177: 
  178:     my $path = $r->dir_config('lonDocRoot')."/priv/$domain/$author_name/";
  179:     my @directories = split(/\//,$newpath);
  180: 
  181:     foreach my $now_checking (@directories) {
  182:         if($now_checking ne '') {
  183:             $path = $path.'/'.$now_checking;
  184:             if(-e $path) {} #More moving along, isn't recursion fun'
  185: 
  186:             else {
  187:                 unless(mkdir($path, 02770)) {
  188:                     $r->print('<p class="LC_error">'.&mt('Error:').' '.$!.'</p>');
  189:                     return 0;
  190:                 }
  191:                 unless(chmod(02770, ($path))) {
  192:                     $r->print('<p class="LC_error"> '.&mt('Error:').' '.$!.'</p>');
  193:                     return 0;
  194:                 }
  195:             }
  196:         } else { } #Just move along
  197: 
  198:     }
  199:     $r->print('<br /><b>'.&mt('Copying File').'</b>');
  200:     my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
  201:     my $file_output = &includemeta(&Apache::lonnet::getfile($problem_filename),$filename);
  202:     my $fs=Apache::File->new(">$path_to_new_file");
  203:     if (defined($fs)) {
  204:         print $fs $file_output;
  205:     }
  206:     $r->print("<br /><br />");
  207:     $r->print('<form name="copied_file" action="/adm/source" target="_parent" method="post">'
  208:               .'<input type="button" value="'
  209:               .&mt('Close Window').'" name="close" onclick="window.close()" />'
  210:               .'</form>');
  211:     #Some 1.3'ish feature is to include the derivative feature, will go here..'
  212:     return;
  213: }
  214: 
  215: sub print_item {
  216:     my ($r,$filename,$listname) = @_;
  217:     my $file_output = 
  218:         &includemeta(&Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$filename),
  219:                                               $filename);
  220:     $r->print(&Apache::loncommon::start_page('View Source Code',undef,
  221:                                              {'only_body' => 1}));
  222:     if ($file_output ne '') {
  223:         my $access_to_cstr;
  224:         my $lonhost = $r->dir_config('lonHostID');
  225:         if (&Apache::lonnet::is_library($lonhost)) {
  226:             my @possdoms = &Apache::lonnet::current_machine_domains();
  227:             foreach my $dom (@possdoms) {
  228:                 if ($env{"user.role.au./$dom/"}) {
  229:                     $access_to_cstr = 1;
  230:                     last;  
  231:                 }
  232:             }
  233:             unless ($access_to_cstr) {
  234:                 foreach my $key (keys(%env)) {
  235:                     if ($key =~ m{^\Quser.role.ca./\E($match_domain)/}) {
  236:                         my $adom = $1;
  237:                         if (grep(/^\Q$adom\E$/,@possdoms)) {
  238:                             $access_to_cstr = 1;
  239:                             last;
  240:                         }
  241:                     }
  242:                 }
  243:             }
  244:             if ($access_to_cstr) {
  245:                 $r->print('
  246:              <form name="copy" action="/adm/source" target="_parent" method="post">
  247:               <input type="button" value="'.&mt('Close Window').'" name="close" onclick="window.close();" />
  248:               <input type="hidden" name="filename" value="'.$filename.'" />
  249:               <input type="hidden" name="listname" value="'.$listname.'" />
  250:               <input type="hidden" name="action" value="stage2" />
  251:               <input type="submit" value="'.&mt('Copy to Authoring Space').'" />
  252:              </form><hr />
  253:                 ');
  254:             } else {
  255:                 $r->print('<p><span class="LC_info">'.
  256:                           &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.').
  257:                           '</span></p><a href="javascript:window.close();">'.&mt('Close Window').
  258:                           '</a><br /><hr />'
  259:                          );
  260:             }
  261:         } else {
  262:             $r->print('<p><span class="LC_info">'.
  263:                       &mt('Source code is displayed, but you can not copy to Authoring Space on this server.').
  264:                           '</span></p><a href="javascript:window.close();">'.&mt('Close Window').
  265:                           '</a><br /><hr />'
  266:                      );
  267: 
  268:         }
  269:         my $count=0;
  270:         my $maxlength=-1;
  271:         foreach (split ("\n", $file_output)) {
  272:             $count+=int(length($_)/79);
  273:             $count++;
  274:             if (length($_) > $maxlength) {
  275:                 $maxlength = length($_);
  276:             }
  277:         }
  278:         my $rows = $count;
  279:         my $cols = $maxlength;
  280:         $r->print('<form name="showsrc" action="" method="post" onsubmit="return false">'."\n".
  281:                   '<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
  282:                   &HTML::Entities::encode($file_output,'<>&"').'</textarea></form>');
  283:     } else {
  284:         $r->print('<p class="LC_warning">'.
  285:                   &mt('Unable to retrieve file contents.').
  286:                   '</p><a href="javascript:window.close();">'.&mt('Close Window').'</a>'
  287:                  );
  288:     }
  289:     $r->print(&Apache::loncommon::end_page());
  290:     return;
  291: }
  292: 
  293: sub includemeta {
  294:     my ($file_output,$orgfilename)=@_;
  295:     my $escfilename=&escape($orgfilename);
  296:     my $copytime=time;
  297:     if ($file_output=~/\<meta\s*name\=\"isbasedonres\"/i) {
  298: 	$file_output=~s/(\<meta\s*name\=\"isbasedonres\"\s*content\=\"[^\"]*)\"/$1\,\Q$escfilename\E\"/i;
  299:     } else {
  300: 	$file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedonres\" content=\"\Q$escfilename\E\" \/\>/i;
  301:     }
  302:     if ($file_output=~/\<meta\s*name\=\"isbasedontime\"/i) {
  303: 	$file_output=~s/(\<meta\s*name\=\"isbasedontime\"\s*content\=\"[^\"]*)\"/$1\,\Q$copytime\E\"/i;
  304:     } else {
  305: 	$file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedontime\" content=\"\Q$copytime\E\" \/\>/i;
  306:     }
  307:     if ($file_output eq '-1') {
  308:         return;
  309:     } else {
  310:         return $file_output;
  311:     }
  312: }
  313: 
  314: sub handler {
  315:     my $r=shift;
  316:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  317:                                             ['filename','listname']);
  318:     my $filename = $env{'form.filename'};
  319:     my $listname = $env{'form.listname'};
  320:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
  321:     if ($source ne 'open') {
  322:         $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
  323:         return HTTP_NOT_ACCEPTABLE;
  324:     }
  325:     unless (&Apache::lonnet::allowed('bre',$filename)) {
  326:         $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
  327:         return HTTP_NOT_ACCEPTABLE;
  328:     }
  329:     unless (&Apache::lonnet::allowed('cre','/')) {
  330:         $env{'user.error.msg'}="$filename:cre:1:1:Access to source code denied";
  331:         return HTTP_NOT_ACCEPTABLE;
  332:     }
  333: 
  334:     &Apache::loncommon::content_type($r,'text/html');
  335:     $r->send_http_header;
  336: 
  337:     if ($env{'form.action'} eq 'stage2') {
  338:         &stage_2($r,$filename,$listname);
  339:     } elsif($env{'form.action'} eq 'copy_stage') {
  340:         &copy_stage($r,$filename,$listname,$env{'form.newpath'});
  341:     } elsif($env{'form.action'} eq 'delete_confirm') {
  342:         &delete_copy_file($r,$env{'form.newpath'},$filename, $env{'form.path'}, '0');
  343:     } else {
  344:         &print_item($r,$filename,$listname);
  345:     }
  346:     return OK;
  347: }
  348: 
  349: 1;
  350: 
  351: 

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