File:  [LON-CAPA] / loncom / interface / lonsource.pm
Revision 1.24: download - view: text, annotated - select for diffs
Thu Aug 13 11:00:38 2009 UTC (14 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_99_1, version_2_8_99_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, bz6209-base, bz6209, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2
- xhtml.

    1: # The LearningOnline Network with CAPA
    2: # Source Code handler
    3: #
    4: # $Id: lonsource.pm,v 1.24 2009/08/13 11:00:38 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;
   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, $author, $listname) = @_;
   54:     my ($uname, $udom) = &Apache::loncacc::constructaccess('/~'.$author.'/',$r->dir_config('lonDefDomain'));
   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>');
   61:     $r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
   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" />
   65:               <input type="text" size="50" name="newpath" value="/'.&mt('shared_source').'/'.$author.'" />&nbsp;
   66:               <input type="submit" value="'.&mt('Copy').'" />
   67:               </form>');
   68:     return OK;
   69: }
   70: 
   71: 
   72: sub copy_stage {
   73:     my ($r, $filename, $listname, $newpath) = @_;
   74: 
   75: #Figure out if we are author or co-author
   76:     my $role;
   77:     my $domain;
   78:     my $author_name;
   79:     if ($env{'request.role'} =~ m{^ca\.}) {
   80:         ($role, $domain, $author_name) = split(/\//,$env{'request.role'});
   81:     } else {
   82:         $role = "au.";
   83:         $domain = $env{'user.domain'};
   84:         $author_name = $env{'user.name'};
   85:     }
   86: 
   87:     my $path_to_new_file = '/home/'.$author_name.'/public_html/'.$newpath.'/'.$listname;
   88: #Just checking again for access as we want to make sure that it is really ok now that we have the real path
   89: 
   90:     my ($uname,$udom)= &Apache::loncacc::constructaccess($path_to_new_file,$domain);
   91: 
   92:     if (!$uname || !$udom) {
   93: 	$r->print(&Apache::loncommon::start_page('Not Allowed'));
   94: 	$r->print(&mt('Not allowed to create file [_1]', $path_to_new_file));
   95: 	$r->print(&Apache::loncommon::end_page());
   96: 	return;
   97:     }
   98: 
   99: 
  100:     #allowed
  101:     $r->print(&Apache::loncommon::start_page('Copying Source'));
  102:     my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
  103:     $r->print($result);
  104:     if(($result) && ($result =~ m|published|) ) {
  105: 	&delete_copy_file($r, $author_name, $newpath, $filename, $path_to_new_file, '1');
  106:     } elsif(($result) && ($result =~ m|exists!|)) {
  107: 	&confirm($r, $author_name, $newpath, $filename, $path_to_new_file);
  108:     } else {
  109: 	&copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
  110:     }
  111:     
  112:     $r->print(&Apache::loncommon::end_page());
  113: }
  114: 
  115: sub confirm {
  116:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
  117:     $r->print("<b>Press delete to remove file and replace it with a copy of the source you are viewing</b><br /><br /	>");
  118:     $r->print('<form name="delete_confirm" action="/adm/source" target="_parent" method="post">
  119:               <input type="hidden" name="filename" value="'.$filename.'" />
  120:               <input type="hidden" name="path" value="'.$path_to_new_file.'" />
  121:               <input type="hidden" name="author" value="'.$author_name.'" />
  122:               <input type="hidden" name="newpath" value="'.$newpath.'" />
  123:               <input type="hidden" name="action" value="delete_confirm" />
  124: 
  125:               <input type="submit" value="Delete" />
  126:               </form>');
  127: }
  128: 
  129: sub delete_copy_file {
  130:     my ($r, $author_name, $newpath, $filename, $path_to_new_file, $type) = @_;
  131:     if($type eq '1') {
  132:         $r->print('<p><span class="LC_warning">'
  133:                  .&mt('Cannot delete non-obsolete published file.')
  134:                  .'</span><br />'
  135:                  .&mt('Please use the code view in previous window to use shared code.')
  136:                  .'<br /><br />');
  137:         $r->print('<input type="button" value="Close Window" name="close"'
  138:                  .' onclick="window.close()" />'
  139:                  .'</p>');
  140:     } else {
  141:         if(-e $path_to_new_file) {
  142:             unless(unlink($path_to_new_file)) {
  143:                 $r->print('<p class="LC_error"">'.&mt('Error:').' '.$!.'</p>');
  144:                 return 0;
  145:             }
  146:         } else {
  147:             $r->print('<p class="LC_error">'.&mt('No such file').'</p></form>');
  148:             return 0;
  149:         }
  150:         &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
  151:     }
  152: }
  153: 
  154: sub copy_file {
  155:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
  156:     $r->print("<b>Creating directories</b>");
  157:     my $path = '/home/'.$author_name.'/public_html/';
  158:     my @directories = split(/\//,$newpath);
  159:     foreach my $now_checking (@directories) {
  160:         if($now_checking ne '') {
  161:             $path = $path.'/'.$now_checking;
  162:             if(-e $path) {} #More moving along, isn't recursion fun'
  163: 
  164:             else {
  165:                 unless(mkdir($path, 02770)) {
  166:                     $r->print('<p class="LC_error">'.&mt('Error:').' '.$!.'</p>');
  167:                     return 0;
  168:                 }
  169:                 unless(chmod(02770, ($path))) {
  170:                     $r->print('<p class="LC_error"> '.&mt('Error:').' '.$!.'</p>');
  171:                     return 0;
  172:                 }
  173:             }
  174:         } else { } #Just move along
  175: 
  176:     }
  177:     $r->print("<br /><b>Copying File</b>");
  178:     my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
  179:     my $file_output = &includemeta(&Apache::lonnet::getfile($problem_filename),$filename);
  180:     my $fs=Apache::File->new(">$path_to_new_file");
  181:     if (defined($fs)) {
  182:         print $fs $file_output;
  183:     }
  184:     $r->print("<br /><br />");
  185:     $r->print('<input type="button" value="Close Window" name="close" onclick="window.close()" />');
  186:     #Some 1.3'ish feature is to include the derivative feature, will go here..'
  187: }
  188: 
  189: sub print_item {
  190:     my ($r, $filename) = @_;
  191:     my $file_output = &includemeta(&Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.$filename),$filename);
  192:     my $count=0;
  193:     my $maxlength=-1;
  194:     foreach (split ("\n", $file_output)) {
  195:         $count+=int(length($_)/79);
  196:         $count++;
  197:         if (length($_) > $maxlength) {
  198:             $maxlength = length($_);
  199:         }
  200:     }
  201:     my $rows = $count;
  202:     my $cols = $maxlength;
  203:     $r->print('<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
  204:               &HTML::Entities::encode($file_output,'<>&"').'</textarea>');
  205:     return OK;
  206: 
  207: }
  208: 
  209: sub includemeta {
  210:     my ($file_output,$orgfilename)=@_;
  211:     my $escfilename=&escape($orgfilename);
  212:     my $copytime=time;
  213:     if ($file_output=~/\<meta\s*name\=\"isbasedonres\"/i) {
  214: 	$file_output=~s/(\<meta\s*name\=\"isbasedonres\"\s*content\=\"[^\"]*)\"/$1\,\Q$escfilename\E\"/i;
  215:     } else {
  216: 	$file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedonres\" content=\"\Q$escfilename\E\" \/\>/i;
  217:     }
  218:     if ($file_output=~/\<meta\s*name\=\"isbasedontime\"/i) {
  219: 	$file_output=~s/(\<meta\s*name\=\"isbasedontime\"\s*content\=\"[^\"]*)\"/$1\,\Q$copytime\E\"/i;
  220:     } else {
  221: 	$file_output=~s/(\<(?:html|problem)[^\>]*\>)/$1\n\<meta name=\"isbasedontime\" content=\"\Q$copytime\E\" \/\>/i;
  222:     }
  223:     return $file_output;
  224: }
  225: 
  226: sub handler {
  227:     my $r=shift;
  228:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  229:                                             ['filename','listname']);
  230:     my $filename = $env{'form.filename'};
  231:     my $listname = $env{'form.listname'};
  232:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
  233:     if ($source ne 'open') {
  234:         $env{'user.error.msg'}="$filename:cre:1:1:Source code not available";
  235:         return HTTP_NOT_ACCEPTABLE;
  236:     } 
  237:     unless ((&Apache::lonnet::allowed('bre',$filename)) &&
  238: 	    (&Apache::lonnet::allowed('cre','/'))) {
  239:         $env{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
  240:         return HTTP_NOT_ACCEPTABLE;
  241:     } 
  242: 
  243:     &Apache::loncommon::content_type($r,'text/html');
  244:     $r->send_http_header;
  245: 
  246:     if ($env{'form.action'} eq 'stage2') {
  247:         my $author = &Apache::lonnet::metadata($filename,'authorspace');
  248: 	#strip the domain of the author name
  249: 	if ($author =~ /:/) {
  250: 	    ($author) = split(/:/,$author);
  251: 	} else {
  252: 	    ($author) = split(/@/,$author);
  253: 	}
  254:         &stage_2($r, $env{'form.filename'}, $author, $listname);
  255:     } elsif($env{'form.action'} eq 'copy_stage') {
  256:         &copy_stage($r, $filename,$env{'form.listname'},$env{'form.newpath'});
  257:     } elsif($env{'form.action'} eq 'delete_confirm') {
  258:         &delete_copy_file($r, $env{'form.author'}, $env{'form.newpath'}, $env{'form.filename'}, $env{'form.path'}, '0');
  259:     } else {
  260: 	$r->print('<form name="copy" action="/adm/source" target="_parent" method="post">
  261:                   <input type="button" value="'.&mt('Close Window').'" name="close" onclick="window.close()" />
  262:                   <input type="hidden" name="filename" value="'.$filename.'" />
  263:                   <input type="hidden" name="listname" value="'.$listname.'" />
  264:                   <input type="hidden" name="action" value="stage2" />
  265:                   <input type="submit" value="'.&mt('Copy to Construction Space').'" />
  266:                   </form>');
  267:         $r->print('<hr />');
  268:         &print_item($r, $env{'form.filename'});
  269:     }
  270:     return OK;
  271: }
  272: 
  273: 1;
  274: 
  275: 

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