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

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

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