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

1.1       taceyjo1    1: # The LearningOnline Network with CAPA
1.2       albertel    2: # Souce Code handler
1.1       taceyjo1    3: #
1.5     ! taceyjo1    4: # $Id: lonsource.pm,v 1.4 2004/07/07 00:02:00 taceyjo1 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;
                     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: 
                     44: sub make_link {
1.4       taceyjo1   45:     my ($filename, $listname) = @_;
1.2       albertel   46:     my $sourcelink = "http://".$ENV{'SERVER_NAME'}.
1.5     ! taceyjo1   47:                      "/adm/source/?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:     $filename = $filename;
        !            55:     &Apache::loncommon::content_type($r,'text/html');
        !            56:     my ($uname, $udom) = &Apache::loncacc::constructaccess('/~'.$author.'/',$r->dir_config('lonDefDomain'));
        !            57:     $r->send_http_header;
        !            58:     $r->print('<html><head><title>LON-CAPA Move source to construction space</title>');
        !            59:     $r->print(&Apache::loncommon::bodytag('Problem source code moving operation'));
        !            60:     $r->print("Please enter the directory that you would like the source code to go into, a default has also been 			provided <br />");
        !            61:     $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 />");
        !            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" name="newpath" value="/shared_source/'.$author.'" />&nbsp;
        !            67:               <input type="submit" value="Copy" />
        !            68:               </form>');
        !            69:     return OK;
1.4       taceyjo1   70: }
                     71: 
                     72: 
                     73: sub copy_stage {
                     74:     my ($r, $filename, $listname, $newpath) = @_;
                     75:     my $role;
                     76:     my $domain;
                     77:     my $author_name;
1.5     ! taceyjo1   78: #Figure out if we are author or co-author
        !            79: 
        !            80:     if($ENV{'request.role'} =~ m|ca.|) {
        !            81:         ($role, $domain, $author_name) = split(/\//,$ENV{'request.role'});
1.4       taceyjo1   82:     } else {
1.5     ! taceyjo1   83:         $role = "au.";
        !            84:         $domain = $ENV{'user.domain'};
        !            85:         $author_name = $ENV{'user.name'};
        !            86:     }
        !            87: 
1.4       taceyjo1   88:     my $path_to_new_file = '/home/'.$author_name.'/public_html/'.$newpath.'/'.$listname;
1.5     ! taceyjo1   89: #Just checking again for access as we want to make sure that it is really ok now that we have the real path
        !            90: 
1.4       taceyjo1   91:     my ($uname,$udom)= &Apache::loncacc::constructaccess($path_to_new_file,$domain);
                     92:     unless (($uname) && ($udom)) {
1.5     ! taceyjo1   93:         return HTTP_NOT_ACCEPTABLE;
1.4       taceyjo1   94:     }
                     95:     &Apache::loncommon::content_type($r,'text/html');
                     96:     $r->send_http_header;
                     97:     $r->print('<html><head><title>LON-CAPA Move source to construction space</title>');
1.5     ! taceyjo1   98:     $r->print(&Apache::loncommon::bodytag('Copying Source'));
        !            99:     my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
        !           100:     $r->print($result);
        !           101:     if(($result) && ($result =~ m|published|) ) {
        !           102:         &delete_file($r, $author_name, $newpath, $filename, $path_to_new_file, '1');
        !           103:     } elsif(($result) && ($result =~ m|exists!|)) {
        !           104:         &confirm($r, $author_name, $newpath, $filename, $path_to_new_file);
        !           105:     } else {
        !           106:         &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
        !           107:     }
        !           108: 
        !           109:     return OK;
        !           110: 
1.4       taceyjo1  111: }
                    112: 
                    113: sub confirm {
1.5     ! taceyjo1  114:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
        !           115:     $r->print("<b>Press delete to remove file and replace it with a copy of the source you are viewing</b><br /><br /	>");
        !           116:     $r->print('<form name="delete_confirm" action="/adm/source/" target="_parent" method="post">
        !           117:               <input type="hidden" name="filename" value="'.$filename.'" />
        !           118:               <input type="hidden" name="path" value="'.$path_to_new_file.'" />
        !           119:               <input type="hidden" name="author" value="'.$author_name.'" />
        !           120:               <input type="hidden" name="newpath" value="'.$newpath.'" />
        !           121:               <input type="hidden" name="action" value="delete_confirm" />
        !           122: 
        !           123:               <input type="submit" value="Delete" />
        !           124:               </form>');
1.4       taceyjo1  125: }
                    126: 
                    127: sub delete_file {
1.5     ! taceyjo1  128:     my ($r, $author_name, $newpath, $filename, $path_to_new_file, $type) = @_;
        !           129:     if($type eq '1') {
        !           130:         $r->print("<b>Cannot delete non-obsolete published file</b><br />Please
        !           131:               use the code view in previous window to use shared code<br /><br />");
        !           132:         $r->print('<input type="button" value="Close Window" name="close" onClick="window.close()">');
        !           133:     } else {
        !           134:         if(-e $path_to_new_file) {
        !           135:             unless(unlink($path_to_new_file)) {
        !           136:                 $r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
        !           137:                 return 0;
        !           138:             }
        !           139:         } else {
        !           140:             $r->print('<p> '.&mt('No such file').'. </p></form>');
        !           141:             return 0;
        !           142:         }
        !           143:         &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
        !           144:     }
1.1       taceyjo1  145: }
                    146: 
1.4       taceyjo1  147: sub copy_file {
1.5     ! taceyjo1  148:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
        !           149:     $r->print("<b>Creating directories</b>");
        !           150:     my $path = '/home/'.$author_name.'/public_html/';
        !           151:     my @directories = split(/\//,$newpath);
        !           152:     foreach my $now_checking (@directories) {
        !           153:         if($now_checking ne '') {
        !           154:             $path = $path.'/'.$now_checking;
        !           155:             if(-e $path) {} #More moving along, isn't recursion fun'
        !           156: 
        !           157:             else {
        !           158:                 unless(mkdir($path, 02770)) {
        !           159:                     $r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
        !           160:                     return 0;
        !           161:                 }
        !           162:                 unless(chmod(02770, ($path))) {
        !           163:                     $r->print('<font color="red"> '.&mt('Error').': '.$!.'</font>');
        !           164:                     return 0;
        !           165:                 }
        !           166:             }
        !           167:         } else { } #Just move along
        !           168: 
        !           169:     }
        !           170:     $r->print("<br /><b>Copying File</b>");
        !           171:     my $problem_filename = "/home/httpd/html".$filename;
        !           172:     my $file_output = &Apache::lonnet::getfile($problem_filename);
        !           173:     my $fs=Apache::File->new(">$path_to_new_file");
        !           174:     if (defined($fs)) {
        !           175:         print $fs $file_output;
        !           176:     }
        !           177:     $r->print("<br /><br />");
        !           178:     $r->print('<input type="button" value="Close Window" name="close" onClick="window.close()">');
        !           179:     #Some 1.3'ish feature is to include the derivative feature, will go here..'
        !           180: }
1.4       taceyjo1  181: 
1.1       taceyjo1  182: sub print_item {
1.5     ! taceyjo1  183:     my ($r, $filename) = @_;
        !           184:     $filename = "/home/httpd/html".$filename;
        !           185:     &Apache::lonnet::logthis("print_item filename = $filename");
        !           186:     my $file_output = &Apache::lonnet::getfile($filename);
        !           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: 
                    204: 
1.5     ! taceyjo1  205: sub handler {
1.2       albertel  206:     my $r=shift;
1.3       www       207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.5     ! taceyjo1  208:                                             ['filename','listname']);
1.3       www       209:     my $filename = $ENV{'form.filename'};
1.4       taceyjo1  210:     my $listname = $ENV{'form.listname'};
1.3       www       211:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
                    212:     if ($source ne 'open') {
1.5     ! taceyjo1  213:         $ENV{'user.error.msg'}="$filename:cre:1:1:Source code not available";
        !           214:         return HTTP_NOT_ACCEPTABLE;
1.3       www       215:     }
1.4       taceyjo1  216:     if ((!&Apache::lonnet::allowed('cre',$filename)) ||
1.5     ! taceyjo1  217:             (!&Apache::lonnet::allowed('bre',$filename))) {
        !           218:         $ENV{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
        !           219:         return HTTP_NOT_ACCEPTABLE;
        !           220:     }
        !           221:     if ($ENV{'form.action'} eq 'stage2') {
        !           222:         my $author = &Apache::lonnet::metadata($filename,'authorspace');
        !           223:         $author =~ m|@|;
        !           224:         $author = $`; #This just tells who the author name is for later processing.
        !           225:         &stage_2($r, $ENV{'form.filename'}, $author, $listname);
        !           226:     }
        !           227:     elsif($ENV{'form.action'} eq 'copy_stage') {
        !           228:         &copy_stage($r, $filename,$ENV{'form.listname'},$ENV{'form.newpath'});
        !           229:     }
        !           230:     elsif($ENV{'form.action'} eq 'delete_confirm') {
        !           231:         &Apache::loncommon::content_type($r,'text/html');
        !           232:         $r->send_http_header;
        !           233:         &delete_file($r, $ENV{'form.author'}, $ENV{'form.newpath'}, $ENV{'form.filename'}, $ENV{'form.path'}, '0');
        !           234:     }
        !           235:     else {
        !           236:         &Apache::loncommon::content_type($r,'text/html');
        !           237:         $r->send_http_header;
        !           238:         $r->print('<form name="copy" action="/adm/source/" target="_parent" method="post">
        !           239:                   <input type="button" value="Close Window" name="close" onClick="window.close()">
        !           240:                   <input type="hidden" name="filename" value="'.$filename.'" />
        !           241:                   <input type="hidden" name="listname" value="'.$listname.'" />
        !           242:                   <input type="hidden" name="action" value="stage2" />
        !           243:                   <input type="submit" value="Copy to CSTR" />
        !           244:                   </form>');
        !           245:         $r->print('<hr />');
        !           246:         &print_item($r, $ENV{'form.filename'});
        !           247:     }
1.2       albertel  248:     return OK;
1.1       taceyjo1  249: }
                    250: 
                    251: 1;
1.5     ! taceyjo1  252: 
        !           253: 

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