Annotation of loncom/interface/lonsource.pm, revision 1.22

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

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